1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Intel IFC VF NIC driver for virtio dataplane offloading 4 * 5 * Copyright (C) 2020 Intel Corporation. 6 * 7 * Author: Zhu Lingshan <lingshan.zhu@intel.com> 8 * 9 */ 10 11 #include <linux/interrupt.h> 12 #include <linux/module.h> 13 #include <linux/pci.h> 14 #include <linux/sysfs.h> 15 #include "ifcvf_base.h" 16 17 #define VERSION_STRING "0.1" 18 #define DRIVER_AUTHOR "Intel Corporation" 19 #define IFCVF_DRIVER_NAME "ifcvf" 20 21 static irqreturn_t ifcvf_config_changed(int irq, void *arg) 22 { 23 struct ifcvf_hw *vf = arg; 24 25 if (vf->config_cb.callback) 26 return vf->config_cb.callback(vf->config_cb.private); 27 28 return IRQ_HANDLED; 29 } 30 31 static irqreturn_t ifcvf_intr_handler(int irq, void *arg) 32 { 33 struct vring_info *vring = arg; 34 35 if (vring->cb.callback) 36 return vring->cb.callback(vring->cb.private); 37 38 return IRQ_HANDLED; 39 } 40 41 static void ifcvf_free_irq_vectors(void *data) 42 { 43 pci_free_irq_vectors(data); 44 } 45 46 static void ifcvf_free_irq(struct ifcvf_adapter *adapter, int queues) 47 { 48 struct pci_dev *pdev = adapter->pdev; 49 struct ifcvf_hw *vf = &adapter->vf; 50 int i; 51 52 53 for (i = 0; i < queues; i++) { 54 devm_free_irq(&pdev->dev, vf->vring[i].irq, &vf->vring[i]); 55 vf->vring[i].irq = -EINVAL; 56 } 57 58 ifcvf_free_irq_vectors(pdev); 59 } 60 61 static int ifcvf_request_irq(struct ifcvf_adapter *adapter) 62 { 63 struct pci_dev *pdev = adapter->pdev; 64 struct ifcvf_hw *vf = &adapter->vf; 65 int vector, i, ret, irq; 66 67 ret = pci_alloc_irq_vectors(pdev, IFCVF_MAX_INTR, 68 IFCVF_MAX_INTR, PCI_IRQ_MSIX); 69 if (ret < 0) { 70 IFCVF_ERR(pdev, "Failed to alloc IRQ vectors\n"); 71 return ret; 72 } 73 74 snprintf(vf->config_msix_name, 256, "ifcvf[%s]-config\n", 75 pci_name(pdev)); 76 vector = 0; 77 irq = pci_irq_vector(pdev, vector); 78 ret = devm_request_irq(&pdev->dev, irq, 79 ifcvf_config_changed, 0, 80 vf->config_msix_name, vf); 81 82 for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) { 83 snprintf(vf->vring[i].msix_name, 256, "ifcvf[%s]-%d\n", 84 pci_name(pdev), i); 85 vector = i + IFCVF_MSI_QUEUE_OFF; 86 irq = pci_irq_vector(pdev, vector); 87 ret = devm_request_irq(&pdev->dev, irq, 88 ifcvf_intr_handler, 0, 89 vf->vring[i].msix_name, 90 &vf->vring[i]); 91 if (ret) { 92 IFCVF_ERR(pdev, 93 "Failed to request irq for vq %d\n", i); 94 ifcvf_free_irq(adapter, i); 95 96 return ret; 97 } 98 99 vf->vring[i].irq = irq; 100 } 101 102 return 0; 103 } 104 105 static int ifcvf_start_datapath(void *private) 106 { 107 struct ifcvf_hw *vf = ifcvf_private_to_vf(private); 108 u8 status; 109 int ret; 110 111 vf->nr_vring = IFCVF_MAX_QUEUE_PAIRS * 2; 112 ret = ifcvf_start_hw(vf); 113 if (ret < 0) { 114 status = ifcvf_get_status(vf); 115 status |= VIRTIO_CONFIG_S_FAILED; 116 ifcvf_set_status(vf, status); 117 } 118 119 return ret; 120 } 121 122 static int ifcvf_stop_datapath(void *private) 123 { 124 struct ifcvf_hw *vf = ifcvf_private_to_vf(private); 125 int i; 126 127 for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) 128 vf->vring[i].cb.callback = NULL; 129 130 ifcvf_stop_hw(vf); 131 132 return 0; 133 } 134 135 static void ifcvf_reset_vring(struct ifcvf_adapter *adapter) 136 { 137 struct ifcvf_hw *vf = ifcvf_private_to_vf(adapter); 138 int i; 139 140 for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) { 141 vf->vring[i].last_avail_idx = 0; 142 vf->vring[i].desc = 0; 143 vf->vring[i].avail = 0; 144 vf->vring[i].used = 0; 145 vf->vring[i].ready = 0; 146 vf->vring[i].cb.callback = NULL; 147 vf->vring[i].cb.private = NULL; 148 } 149 150 ifcvf_reset(vf); 151 } 152 153 static struct ifcvf_adapter *vdpa_to_adapter(struct vdpa_device *vdpa_dev) 154 { 155 return container_of(vdpa_dev, struct ifcvf_adapter, vdpa); 156 } 157 158 static struct ifcvf_hw *vdpa_to_vf(struct vdpa_device *vdpa_dev) 159 { 160 struct ifcvf_adapter *adapter = vdpa_to_adapter(vdpa_dev); 161 162 return &adapter->vf; 163 } 164 165 static u64 ifcvf_vdpa_get_features(struct vdpa_device *vdpa_dev) 166 { 167 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 168 u64 features; 169 170 features = ifcvf_get_features(vf) & IFCVF_SUPPORTED_FEATURES; 171 172 return features; 173 } 174 175 static int ifcvf_vdpa_set_features(struct vdpa_device *vdpa_dev, u64 features) 176 { 177 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 178 179 vf->req_features = features; 180 181 return 0; 182 } 183 184 static u8 ifcvf_vdpa_get_status(struct vdpa_device *vdpa_dev) 185 { 186 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 187 188 return ifcvf_get_status(vf); 189 } 190 191 static void ifcvf_vdpa_set_status(struct vdpa_device *vdpa_dev, u8 status) 192 { 193 struct ifcvf_adapter *adapter; 194 struct ifcvf_hw *vf; 195 u8 status_old; 196 int ret; 197 198 vf = vdpa_to_vf(vdpa_dev); 199 adapter = dev_get_drvdata(vdpa_dev->dev.parent); 200 status_old = ifcvf_get_status(vf); 201 202 if (status_old == status) 203 return; 204 205 if ((status_old & VIRTIO_CONFIG_S_DRIVER_OK) && 206 !(status & VIRTIO_CONFIG_S_DRIVER_OK)) { 207 ifcvf_stop_datapath(adapter); 208 ifcvf_free_irq(adapter, IFCVF_MAX_QUEUE_PAIRS * 2); 209 } 210 211 if (status == 0) { 212 ifcvf_reset_vring(adapter); 213 return; 214 } 215 216 if ((status & VIRTIO_CONFIG_S_DRIVER_OK) && 217 !(status_old & VIRTIO_CONFIG_S_DRIVER_OK)) { 218 ret = ifcvf_request_irq(adapter); 219 if (ret) { 220 status = ifcvf_get_status(vf); 221 status |= VIRTIO_CONFIG_S_FAILED; 222 ifcvf_set_status(vf, status); 223 return; 224 } 225 226 if (ifcvf_start_datapath(adapter) < 0) 227 IFCVF_ERR(adapter->pdev, 228 "Failed to set ifcvf vdpa status %u\n", 229 status); 230 } 231 232 ifcvf_set_status(vf, status); 233 } 234 235 static u16 ifcvf_vdpa_get_vq_num_max(struct vdpa_device *vdpa_dev) 236 { 237 return IFCVF_QUEUE_MAX; 238 } 239 240 static int ifcvf_vdpa_get_vq_state(struct vdpa_device *vdpa_dev, u16 qid, 241 struct vdpa_vq_state *state) 242 { 243 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 244 245 state->avail_index = ifcvf_get_vq_state(vf, qid); 246 return 0; 247 } 248 249 static int ifcvf_vdpa_set_vq_state(struct vdpa_device *vdpa_dev, u16 qid, 250 const struct vdpa_vq_state *state) 251 { 252 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 253 254 return ifcvf_set_vq_state(vf, qid, state->avail_index); 255 } 256 257 static void ifcvf_vdpa_set_vq_cb(struct vdpa_device *vdpa_dev, u16 qid, 258 struct vdpa_callback *cb) 259 { 260 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 261 262 vf->vring[qid].cb = *cb; 263 } 264 265 static void ifcvf_vdpa_set_vq_ready(struct vdpa_device *vdpa_dev, 266 u16 qid, bool ready) 267 { 268 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 269 270 vf->vring[qid].ready = ready; 271 } 272 273 static bool ifcvf_vdpa_get_vq_ready(struct vdpa_device *vdpa_dev, u16 qid) 274 { 275 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 276 277 return vf->vring[qid].ready; 278 } 279 280 static void ifcvf_vdpa_set_vq_num(struct vdpa_device *vdpa_dev, u16 qid, 281 u32 num) 282 { 283 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 284 285 vf->vring[qid].size = num; 286 } 287 288 static int ifcvf_vdpa_set_vq_address(struct vdpa_device *vdpa_dev, u16 qid, 289 u64 desc_area, u64 driver_area, 290 u64 device_area) 291 { 292 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 293 294 vf->vring[qid].desc = desc_area; 295 vf->vring[qid].avail = driver_area; 296 vf->vring[qid].used = device_area; 297 298 return 0; 299 } 300 301 static void ifcvf_vdpa_kick_vq(struct vdpa_device *vdpa_dev, u16 qid) 302 { 303 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 304 305 ifcvf_notify_queue(vf, qid); 306 } 307 308 static u32 ifcvf_vdpa_get_generation(struct vdpa_device *vdpa_dev) 309 { 310 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 311 312 return ioread8(&vf->common_cfg->config_generation); 313 } 314 315 static u32 ifcvf_vdpa_get_device_id(struct vdpa_device *vdpa_dev) 316 { 317 return VIRTIO_ID_NET; 318 } 319 320 static u32 ifcvf_vdpa_get_vendor_id(struct vdpa_device *vdpa_dev) 321 { 322 return IFCVF_SUBSYS_VENDOR_ID; 323 } 324 325 static u32 ifcvf_vdpa_get_vq_align(struct vdpa_device *vdpa_dev) 326 { 327 return IFCVF_QUEUE_ALIGNMENT; 328 } 329 330 static void ifcvf_vdpa_get_config(struct vdpa_device *vdpa_dev, 331 unsigned int offset, 332 void *buf, unsigned int len) 333 { 334 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 335 336 WARN_ON(offset + len > sizeof(struct virtio_net_config)); 337 ifcvf_read_net_config(vf, offset, buf, len); 338 } 339 340 static void ifcvf_vdpa_set_config(struct vdpa_device *vdpa_dev, 341 unsigned int offset, const void *buf, 342 unsigned int len) 343 { 344 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 345 346 WARN_ON(offset + len > sizeof(struct virtio_net_config)); 347 ifcvf_write_net_config(vf, offset, buf, len); 348 } 349 350 static void ifcvf_vdpa_set_config_cb(struct vdpa_device *vdpa_dev, 351 struct vdpa_callback *cb) 352 { 353 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 354 355 vf->config_cb.callback = cb->callback; 356 vf->config_cb.private = cb->private; 357 } 358 359 static int ifcvf_vdpa_get_vq_irq(struct vdpa_device *vdpa_dev, 360 u16 qid) 361 { 362 struct ifcvf_hw *vf = vdpa_to_vf(vdpa_dev); 363 364 return vf->vring[qid].irq; 365 } 366 367 /* 368 * IFCVF currently does't have on-chip IOMMU, so not 369 * implemented set_map()/dma_map()/dma_unmap() 370 */ 371 static const struct vdpa_config_ops ifc_vdpa_ops = { 372 .get_features = ifcvf_vdpa_get_features, 373 .set_features = ifcvf_vdpa_set_features, 374 .get_status = ifcvf_vdpa_get_status, 375 .set_status = ifcvf_vdpa_set_status, 376 .get_vq_num_max = ifcvf_vdpa_get_vq_num_max, 377 .get_vq_state = ifcvf_vdpa_get_vq_state, 378 .set_vq_state = ifcvf_vdpa_set_vq_state, 379 .set_vq_cb = ifcvf_vdpa_set_vq_cb, 380 .set_vq_ready = ifcvf_vdpa_set_vq_ready, 381 .get_vq_ready = ifcvf_vdpa_get_vq_ready, 382 .set_vq_num = ifcvf_vdpa_set_vq_num, 383 .set_vq_address = ifcvf_vdpa_set_vq_address, 384 .get_vq_irq = ifcvf_vdpa_get_vq_irq, 385 .kick_vq = ifcvf_vdpa_kick_vq, 386 .get_generation = ifcvf_vdpa_get_generation, 387 .get_device_id = ifcvf_vdpa_get_device_id, 388 .get_vendor_id = ifcvf_vdpa_get_vendor_id, 389 .get_vq_align = ifcvf_vdpa_get_vq_align, 390 .get_config = ifcvf_vdpa_get_config, 391 .set_config = ifcvf_vdpa_set_config, 392 .set_config_cb = ifcvf_vdpa_set_config_cb, 393 }; 394 395 static int ifcvf_probe(struct pci_dev *pdev, const struct pci_device_id *id) 396 { 397 struct device *dev = &pdev->dev; 398 struct ifcvf_adapter *adapter; 399 struct ifcvf_hw *vf; 400 int ret, i; 401 402 ret = pcim_enable_device(pdev); 403 if (ret) { 404 IFCVF_ERR(pdev, "Failed to enable device\n"); 405 return ret; 406 } 407 408 ret = pcim_iomap_regions(pdev, BIT(0) | BIT(2) | BIT(4), 409 IFCVF_DRIVER_NAME); 410 if (ret) { 411 IFCVF_ERR(pdev, "Failed to request MMIO region\n"); 412 return ret; 413 } 414 415 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64)); 416 if (ret) { 417 IFCVF_ERR(pdev, "No usable DMA confiugration\n"); 418 return ret; 419 } 420 421 ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 422 if (ret) { 423 IFCVF_ERR(pdev, 424 "No usable coherent DMA confiugration\n"); 425 return ret; 426 } 427 428 ret = devm_add_action_or_reset(dev, ifcvf_free_irq_vectors, pdev); 429 if (ret) { 430 IFCVF_ERR(pdev, 431 "Failed for adding devres for freeing irq vectors\n"); 432 return ret; 433 } 434 435 adapter = vdpa_alloc_device(struct ifcvf_adapter, vdpa, 436 dev, &ifc_vdpa_ops, 437 IFCVF_MAX_QUEUE_PAIRS * 2); 438 if (adapter == NULL) { 439 IFCVF_ERR(pdev, "Failed to allocate vDPA structure"); 440 return -ENOMEM; 441 } 442 443 pci_set_master(pdev); 444 pci_set_drvdata(pdev, adapter); 445 446 vf = &adapter->vf; 447 vf->base = pcim_iomap_table(pdev); 448 449 adapter->pdev = pdev; 450 adapter->vdpa.dma_dev = &pdev->dev; 451 452 ret = ifcvf_init_hw(vf, pdev); 453 if (ret) { 454 IFCVF_ERR(pdev, "Failed to init IFCVF hw\n"); 455 goto err; 456 } 457 458 for (i = 0; i < IFCVF_MAX_QUEUE_PAIRS * 2; i++) 459 vf->vring[i].irq = -EINVAL; 460 461 ret = vdpa_register_device(&adapter->vdpa); 462 if (ret) { 463 IFCVF_ERR(pdev, "Failed to register ifcvf to vdpa bus"); 464 goto err; 465 } 466 467 return 0; 468 469 err: 470 put_device(&adapter->vdpa.dev); 471 return ret; 472 } 473 474 static void ifcvf_remove(struct pci_dev *pdev) 475 { 476 struct ifcvf_adapter *adapter = pci_get_drvdata(pdev); 477 478 vdpa_unregister_device(&adapter->vdpa); 479 } 480 481 static struct pci_device_id ifcvf_pci_ids[] = { 482 { PCI_DEVICE_SUB(IFCVF_VENDOR_ID, 483 IFCVF_DEVICE_ID, 484 IFCVF_SUBSYS_VENDOR_ID, 485 IFCVF_SUBSYS_DEVICE_ID) }, 486 { 0 }, 487 }; 488 MODULE_DEVICE_TABLE(pci, ifcvf_pci_ids); 489 490 static struct pci_driver ifcvf_driver = { 491 .name = IFCVF_DRIVER_NAME, 492 .id_table = ifcvf_pci_ids, 493 .probe = ifcvf_probe, 494 .remove = ifcvf_remove, 495 }; 496 497 module_pci_driver(ifcvf_driver); 498 499 MODULE_LICENSE("GPL v2"); 500 MODULE_VERSION(VERSION_STRING); 501