1 /*- 2 * BSD LICENSE 3 * 4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved. 5 * Copyright 2013-2014 6WIND S.A. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * * Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * * Neither the name of Intel Corporation nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <string.h> 36 #include <inttypes.h> 37 #include <stdint.h> 38 #include <stdlib.h> 39 #include <stdio.h> 40 #include <sys/queue.h> 41 #include <sys/mman.h> 42 43 #include <rte_errno.h> 44 #include <rte_interrupts.h> 45 #include <rte_log.h> 46 #include <rte_bus.h> 47 #include <rte_pci.h> 48 #include <rte_bus_pci.h> 49 #include <rte_per_lcore.h> 50 #include <rte_memory.h> 51 #include <rte_eal.h> 52 #include <rte_string_fns.h> 53 #include <rte_common.h> 54 #include <rte_devargs.h> 55 56 #include "private.h" 57 58 extern struct rte_pci_bus rte_pci_bus; 59 60 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices" 61 62 const char *rte_pci_get_sysfs_path(void) 63 { 64 const char *path = NULL; 65 66 path = getenv("SYSFS_PCI_DEVICES"); 67 if (path == NULL) 68 return SYSFS_PCI_DEVICES; 69 70 return path; 71 } 72 73 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev) 74 { 75 struct rte_devargs *devargs; 76 struct rte_pci_addr addr; 77 struct rte_bus *pbus; 78 79 pbus = rte_bus_find_by_name("pci"); 80 TAILQ_FOREACH(devargs, &devargs_list, next) { 81 if (devargs->bus != pbus) 82 continue; 83 devargs->bus->parse(devargs->name, &addr); 84 if (!rte_pci_addr_cmp(&dev->addr, &addr)) 85 return devargs; 86 } 87 return NULL; 88 } 89 90 void 91 pci_name_set(struct rte_pci_device *dev) 92 { 93 struct rte_devargs *devargs; 94 95 /* Each device has its internal, canonical name set. */ 96 rte_pci_device_name(&dev->addr, 97 dev->name, sizeof(dev->name)); 98 devargs = pci_devargs_lookup(dev); 99 dev->device.devargs = devargs; 100 /* In blacklist mode, if the device is not blacklisted, no 101 * rte_devargs exists for it. 102 */ 103 if (devargs != NULL) 104 /* If an rte_devargs exists, the generic rte_device uses the 105 * given name as its namea 106 */ 107 dev->device.name = dev->device.devargs->name; 108 else 109 /* Otherwise, it uses the internal, canonical form. */ 110 dev->device.name = dev->name; 111 } 112 113 /* 114 * Match the PCI Driver and Device using the ID Table 115 */ 116 int 117 rte_pci_match(const struct rte_pci_driver *pci_drv, 118 const struct rte_pci_device *pci_dev) 119 { 120 const struct rte_pci_id *id_table; 121 122 for (id_table = pci_drv->id_table; id_table->vendor_id != 0; 123 id_table++) { 124 /* check if device's identifiers match the driver's ones */ 125 if (id_table->vendor_id != pci_dev->id.vendor_id && 126 id_table->vendor_id != PCI_ANY_ID) 127 continue; 128 if (id_table->device_id != pci_dev->id.device_id && 129 id_table->device_id != PCI_ANY_ID) 130 continue; 131 if (id_table->subsystem_vendor_id != 132 pci_dev->id.subsystem_vendor_id && 133 id_table->subsystem_vendor_id != PCI_ANY_ID) 134 continue; 135 if (id_table->subsystem_device_id != 136 pci_dev->id.subsystem_device_id && 137 id_table->subsystem_device_id != PCI_ANY_ID) 138 continue; 139 if (id_table->class_id != pci_dev->id.class_id && 140 id_table->class_id != RTE_CLASS_ANY_ID) 141 continue; 142 143 return 1; 144 } 145 146 return 0; 147 } 148 149 /* 150 * If vendor/device ID match, call the probe() function of the 151 * driver. 152 */ 153 static int 154 rte_pci_probe_one_driver(struct rte_pci_driver *dr, 155 struct rte_pci_device *dev) 156 { 157 int ret; 158 struct rte_pci_addr *loc; 159 160 if ((dr == NULL) || (dev == NULL)) 161 return -EINVAL; 162 163 loc = &dev->addr; 164 165 /* The device is not blacklisted; Check if driver supports it */ 166 if (!rte_pci_match(dr, dev)) 167 /* Match of device and driver failed */ 168 return 1; 169 170 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n", 171 loc->domain, loc->bus, loc->devid, loc->function, 172 dev->device.numa_node); 173 174 /* no initialization when blacklisted, return without error */ 175 if (dev->device.devargs != NULL && 176 dev->device.devargs->policy == 177 RTE_DEV_BLACKLISTED) { 178 RTE_LOG(INFO, EAL, " Device is blacklisted, not" 179 " initializing\n"); 180 return 1; 181 } 182 183 if (dev->device.numa_node < 0) { 184 RTE_LOG(WARNING, EAL, " Invalid NUMA socket, default to 0\n"); 185 dev->device.numa_node = 0; 186 } 187 188 RTE_LOG(INFO, EAL, " probe driver: %x:%x %s\n", dev->id.vendor_id, 189 dev->id.device_id, dr->driver.name); 190 191 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) { 192 /* map resources for devices that use igb_uio */ 193 ret = rte_pci_map_device(dev); 194 if (ret != 0) 195 return ret; 196 } 197 198 /* reference driver structure */ 199 dev->driver = dr; 200 dev->device.driver = &dr->driver; 201 202 /* call the driver probe() function */ 203 ret = dr->probe(dr, dev); 204 if (ret) { 205 dev->driver = NULL; 206 dev->device.driver = NULL; 207 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) && 208 /* Don't unmap if device is unsupported and 209 * driver needs mapped resources. 210 */ 211 !(ret > 0 && 212 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES))) 213 rte_pci_unmap_device(dev); 214 } 215 216 return ret; 217 } 218 219 /* 220 * If vendor/device ID match, call the remove() function of the 221 * driver. 222 */ 223 static int 224 rte_pci_detach_dev(struct rte_pci_device *dev) 225 { 226 struct rte_pci_addr *loc; 227 struct rte_pci_driver *dr; 228 int ret = 0; 229 230 if (dev == NULL) 231 return -EINVAL; 232 233 dr = dev->driver; 234 loc = &dev->addr; 235 236 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n", 237 loc->domain, loc->bus, loc->devid, 238 loc->function, dev->device.numa_node); 239 240 RTE_LOG(DEBUG, EAL, " remove driver: %x:%x %s\n", dev->id.vendor_id, 241 dev->id.device_id, dr->driver.name); 242 243 if (dr->remove) { 244 ret = dr->remove(dev); 245 if (ret < 0) 246 return ret; 247 } 248 249 /* clear driver structure */ 250 dev->driver = NULL; 251 252 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) 253 /* unmap resources for devices that use igb_uio */ 254 rte_pci_unmap_device(dev); 255 256 return 0; 257 } 258 259 /* 260 * If vendor/device ID match, call the probe() function of all 261 * registered driver for the given device. Return -1 if initialization 262 * failed, return 1 if no driver is found for this device. 263 */ 264 static int 265 pci_probe_all_drivers(struct rte_pci_device *dev) 266 { 267 struct rte_pci_driver *dr = NULL; 268 int rc = 0; 269 270 if (dev == NULL) 271 return -1; 272 273 /* Check if a driver is already loaded */ 274 if (dev->driver != NULL) 275 return 0; 276 277 FOREACH_DRIVER_ON_PCIBUS(dr) { 278 rc = rte_pci_probe_one_driver(dr, dev); 279 if (rc < 0) 280 /* negative value is an error */ 281 return -1; 282 if (rc > 0) 283 /* positive value means driver doesn't support it */ 284 continue; 285 return 0; 286 } 287 return 1; 288 } 289 290 /* 291 * Find the pci device specified by pci address, then invoke probe function of 292 * the driver of the device. 293 */ 294 int 295 rte_pci_probe_one(const struct rte_pci_addr *addr) 296 { 297 struct rte_pci_device *dev = NULL; 298 299 int ret = 0; 300 301 if (addr == NULL) 302 return -1; 303 304 /* update current pci device in global list, kernel bindings might have 305 * changed since last time we looked at it. 306 */ 307 if (pci_update_device(addr) < 0) 308 goto err_return; 309 310 FOREACH_DEVICE_ON_PCIBUS(dev) { 311 if (rte_pci_addr_cmp(&dev->addr, addr)) 312 continue; 313 314 ret = pci_probe_all_drivers(dev); 315 if (ret) 316 goto err_return; 317 return 0; 318 } 319 return -1; 320 321 err_return: 322 RTE_LOG(WARNING, EAL, 323 "Requested device " PCI_PRI_FMT " cannot be used\n", 324 addr->domain, addr->bus, addr->devid, addr->function); 325 return -1; 326 } 327 328 /* 329 * Detach device specified by its pci address. 330 */ 331 int 332 rte_pci_detach(const struct rte_pci_addr *addr) 333 { 334 struct rte_pci_device *dev = NULL; 335 int ret = 0; 336 337 if (addr == NULL) 338 return -1; 339 340 FOREACH_DEVICE_ON_PCIBUS(dev) { 341 if (rte_pci_addr_cmp(&dev->addr, addr)) 342 continue; 343 344 ret = rte_pci_detach_dev(dev); 345 if (ret < 0) 346 /* negative value is an error */ 347 goto err_return; 348 if (ret > 0) 349 /* positive value means driver doesn't support it */ 350 continue; 351 352 rte_pci_remove_device(dev); 353 free(dev); 354 return 0; 355 } 356 return -1; 357 358 err_return: 359 RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT 360 " cannot be used\n", dev->addr.domain, dev->addr.bus, 361 dev->addr.devid, dev->addr.function); 362 return -1; 363 } 364 365 /* 366 * Scan the content of the PCI bus, and call the probe() function for 367 * all registered drivers that have a matching entry in its id_table 368 * for discovered devices. 369 */ 370 int 371 rte_pci_probe(void) 372 { 373 struct rte_pci_device *dev = NULL; 374 size_t probed = 0, failed = 0; 375 struct rte_devargs *devargs; 376 int probe_all = 0; 377 int ret = 0; 378 379 if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST) 380 probe_all = 1; 381 382 FOREACH_DEVICE_ON_PCIBUS(dev) { 383 probed++; 384 385 devargs = dev->device.devargs; 386 /* probe all or only whitelisted devices */ 387 if (probe_all) 388 ret = pci_probe_all_drivers(dev); 389 else if (devargs != NULL && 390 devargs->policy == RTE_DEV_WHITELISTED) 391 ret = pci_probe_all_drivers(dev); 392 if (ret < 0) { 393 RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT 394 " cannot be used\n", dev->addr.domain, dev->addr.bus, 395 dev->addr.devid, dev->addr.function); 396 rte_errno = errno; 397 failed++; 398 ret = 0; 399 } 400 } 401 402 return (probed && probed == failed) ? -1 : 0; 403 } 404 405 /* dump one device */ 406 static int 407 pci_dump_one_device(FILE *f, struct rte_pci_device *dev) 408 { 409 int i; 410 411 fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus, 412 dev->addr.devid, dev->addr.function); 413 fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id, 414 dev->id.device_id); 415 416 for (i = 0; i != sizeof(dev->mem_resource) / 417 sizeof(dev->mem_resource[0]); i++) { 418 fprintf(f, " %16.16"PRIx64" %16.16"PRIx64"\n", 419 dev->mem_resource[i].phys_addr, 420 dev->mem_resource[i].len); 421 } 422 return 0; 423 } 424 425 /* dump devices on the bus */ 426 void 427 rte_pci_dump(FILE *f) 428 { 429 struct rte_pci_device *dev = NULL; 430 431 FOREACH_DEVICE_ON_PCIBUS(dev) { 432 pci_dump_one_device(f, dev); 433 } 434 } 435 436 static int 437 pci_parse(const char *name, void *addr) 438 { 439 struct rte_pci_addr *out = addr; 440 struct rte_pci_addr pci_addr; 441 bool parse; 442 443 parse = (rte_pci_addr_parse(name, &pci_addr) == 0); 444 if (parse && addr != NULL) 445 *out = pci_addr; 446 return parse == false; 447 } 448 449 /* register a driver */ 450 void 451 rte_pci_register(struct rte_pci_driver *driver) 452 { 453 TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next); 454 driver->bus = &rte_pci_bus; 455 } 456 457 /* unregister a driver */ 458 void 459 rte_pci_unregister(struct rte_pci_driver *driver) 460 { 461 TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next); 462 driver->bus = NULL; 463 } 464 465 /* Add a device to PCI bus */ 466 void 467 rte_pci_add_device(struct rte_pci_device *pci_dev) 468 { 469 TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next); 470 } 471 472 /* Insert a device into a predefined position in PCI bus */ 473 void 474 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev, 475 struct rte_pci_device *new_pci_dev) 476 { 477 TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next); 478 } 479 480 /* Remove a device from PCI bus */ 481 void 482 rte_pci_remove_device(struct rte_pci_device *pci_dev) 483 { 484 TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next); 485 } 486 487 static struct rte_device * 488 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp, 489 const void *data) 490 { 491 struct rte_pci_device *dev; 492 493 FOREACH_DEVICE_ON_PCIBUS(dev) { 494 if (start && &dev->device == start) { 495 start = NULL; /* starting point found */ 496 continue; 497 } 498 if (cmp(&dev->device, data) == 0) 499 return &dev->device; 500 } 501 502 return NULL; 503 } 504 505 static int 506 pci_plug(struct rte_device *dev) 507 { 508 return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev)); 509 } 510 511 static int 512 pci_unplug(struct rte_device *dev) 513 { 514 struct rte_pci_device *pdev; 515 int ret; 516 517 pdev = RTE_DEV_TO_PCI(dev); 518 ret = rte_pci_detach_dev(pdev); 519 if (ret == 0) { 520 rte_pci_remove_device(pdev); 521 free(pdev); 522 } 523 return ret; 524 } 525 526 struct rte_pci_bus rte_pci_bus = { 527 .bus = { 528 .scan = rte_pci_scan, 529 .probe = rte_pci_probe, 530 .find_device = pci_find_device, 531 .plug = pci_plug, 532 .unplug = pci_unplug, 533 .parse = pci_parse, 534 .get_iommu_class = rte_pci_get_iommu_class, 535 }, 536 .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list), 537 .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list), 538 }; 539 540 RTE_REGISTER_BUS(pci, rte_pci_bus.bus); 541