54931d1e548566926e128ca8cd67f6539e199057
[dpdk.git] / lib / librte_eal / common / eal_common_pci.c
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_per_lcore.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.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 "eal_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 *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_eal_compare_pci_addr(&dev->addr, &addr))
85                         return devargs;
86         }
87         return NULL;
88 }
89
90 /* Macro used by pci addr parsing functions. **/
91 #define GET_PCIADDR_FIELD(in, fd, lim, dlm)                     \
92 do {                                                            \
93         unsigned long val;                                      \
94         char *end;                                              \
95         errno = 0;                                              \
96         val = strtoul((in), &end, 16);                          \
97         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
98                 return -EINVAL;                                 \
99         (fd) = (typeof (fd))val;                                \
100         (in) = end + 1;                                         \
101 } while(0)
102
103
104 int
105 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
106 {
107         dev_addr->domain = 0;
108         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
109         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
110         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
111         return 0;
112 }
113
114 int
115 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
116 {
117         GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
118         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
119         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
120         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
121         return 0;
122 }
123
124 #undef GET_PCIADDR_FIELD
125
126 void
127 rte_pci_device_name(const struct rte_pci_addr *addr,
128                      char *output, size_t size)
129 {
130         RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
131         RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
132                             addr->domain, addr->bus,
133                             addr->devid, addr->function) >= 0);
134 }
135
136 int
137 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
138                            const struct rte_pci_addr *addr2)
139 {
140         uint64_t dev_addr, dev_addr2;
141
142         if ((addr == NULL) || (addr2 == NULL))
143                  return -1;
144
145         dev_addr = ((uint64_t)addr->domain << 24) |
146                  (addr->bus << 16) | (addr->devid << 8) | addr->function;
147         dev_addr2 = ((uint64_t)addr2->domain << 24) |
148                  (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
149
150         if (dev_addr > dev_addr2)
151                  return 1;
152         else if (dev_addr < dev_addr2)
153                  return -1;
154         else
155                  return 0;
156 }
157
158 void
159 pci_name_set(struct rte_pci_device *dev)
160 {
161         struct rte_devargs *devargs;
162
163         /* Each device has its internal, canonical name set. */
164         rte_pci_device_name(&dev->addr,
165                         dev->name, sizeof(dev->name));
166         devargs = pci_devargs_lookup(dev);
167         dev->device.devargs = devargs;
168         /* In blacklist mode, if the device is not blacklisted, no
169          * rte_devargs exists for it.
170          */
171         if (devargs != NULL)
172                 /* If an rte_devargs exists, the generic rte_device uses the
173                  * given name as its namea
174                  */
175                 dev->device.name = dev->device.devargs->name;
176         else
177                 /* Otherwise, it uses the internal, canonical form. */
178                 dev->device.name = dev->name;
179 }
180
181 /* map a particular resource from a file */
182 void *
183 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
184                  int additional_flags)
185 {
186         void *mapaddr;
187
188         /* Map the PCI memory resource of device */
189         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
190                         MAP_SHARED | additional_flags, fd, offset);
191         if (mapaddr == MAP_FAILED) {
192                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
193                         __func__, fd, requested_addr,
194                         (unsigned long)size, (unsigned long)offset,
195                         strerror(errno), mapaddr);
196         } else
197                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
198
199         return mapaddr;
200 }
201
202 /* unmap a particular resource */
203 void
204 pci_unmap_resource(void *requested_addr, size_t size)
205 {
206         if (requested_addr == NULL)
207                 return;
208
209         /* Unmap the PCI memory resource of device */
210         if (munmap(requested_addr, size)) {
211                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
212                         __func__, requested_addr, (unsigned long)size,
213                         strerror(errno));
214         } else
215                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
216                                 requested_addr);
217 }
218
219 /*
220  * Match the PCI Driver and Device using the ID Table
221  */
222 int
223 rte_pci_match(const struct rte_pci_driver *pci_drv,
224               const struct rte_pci_device *pci_dev)
225 {
226         const struct rte_pci_id *id_table;
227
228         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
229              id_table++) {
230                 /* check if device's identifiers match the driver's ones */
231                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
232                                 id_table->vendor_id != PCI_ANY_ID)
233                         continue;
234                 if (id_table->device_id != pci_dev->id.device_id &&
235                                 id_table->device_id != PCI_ANY_ID)
236                         continue;
237                 if (id_table->subsystem_vendor_id !=
238                     pci_dev->id.subsystem_vendor_id &&
239                     id_table->subsystem_vendor_id != PCI_ANY_ID)
240                         continue;
241                 if (id_table->subsystem_device_id !=
242                     pci_dev->id.subsystem_device_id &&
243                     id_table->subsystem_device_id != PCI_ANY_ID)
244                         continue;
245                 if (id_table->class_id != pci_dev->id.class_id &&
246                                 id_table->class_id != RTE_CLASS_ANY_ID)
247                         continue;
248
249                 return 1;
250         }
251
252         return 0;
253 }
254
255 /*
256  * If vendor/device ID match, call the probe() function of the
257  * driver.
258  */
259 static int
260 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
261                          struct rte_pci_device *dev)
262 {
263         int ret;
264         struct rte_pci_addr *loc;
265
266         if ((dr == NULL) || (dev == NULL))
267                 return -EINVAL;
268
269         loc = &dev->addr;
270
271         /* The device is not blacklisted; Check if driver supports it */
272         if (!rte_pci_match(dr, dev))
273                 /* Match of device and driver failed */
274                 return 1;
275
276         RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
277                         loc->domain, loc->bus, loc->devid, loc->function,
278                         dev->device.numa_node);
279
280         /* no initialization when blacklisted, return without error */
281         if (dev->device.devargs != NULL &&
282                 dev->device.devargs->policy ==
283                         RTE_DEV_BLACKLISTED) {
284                 RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
285                         " initializing\n");
286                 return 1;
287         }
288
289         if (dev->device.numa_node < 0) {
290                 RTE_LOG(WARNING, EAL, "  Invalid NUMA socket, default to 0\n");
291                 dev->device.numa_node = 0;
292         }
293
294         RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
295                 dev->id.device_id, dr->driver.name);
296
297         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
298                 /* map resources for devices that use igb_uio */
299                 ret = rte_pci_map_device(dev);
300                 if (ret != 0)
301                         return ret;
302         }
303
304         /* reference driver structure */
305         dev->driver = dr;
306         dev->device.driver = &dr->driver;
307
308         /* call the driver probe() function */
309         ret = dr->probe(dr, dev);
310         if (ret) {
311                 dev->driver = NULL;
312                 dev->device.driver = NULL;
313                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
314                         /* Don't unmap if device is unsupported and
315                          * driver needs mapped resources.
316                          */
317                         !(ret > 0 &&
318                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
319                         rte_pci_unmap_device(dev);
320         }
321
322         return ret;
323 }
324
325 /*
326  * If vendor/device ID match, call the remove() function of the
327  * driver.
328  */
329 static int
330 rte_pci_detach_dev(struct rte_pci_device *dev)
331 {
332         struct rte_pci_addr *loc;
333         struct rte_pci_driver *dr;
334         int ret = 0;
335
336         if (dev == NULL)
337                 return -EINVAL;
338
339         dr = dev->driver;
340         loc = &dev->addr;
341
342         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
343                         loc->domain, loc->bus, loc->devid,
344                         loc->function, dev->device.numa_node);
345
346         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
347                         dev->id.device_id, dr->driver.name);
348
349         if (dr->remove) {
350                 ret = dr->remove(dev);
351                 if (ret < 0)
352                         return ret;
353         }
354
355         /* clear driver structure */
356         dev->driver = NULL;
357
358         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
359                 /* unmap resources for devices that use igb_uio */
360                 rte_pci_unmap_device(dev);
361
362         return 0;
363 }
364
365 /*
366  * If vendor/device ID match, call the probe() function of all
367  * registered driver for the given device. Return -1 if initialization
368  * failed, return 1 if no driver is found for this device.
369  */
370 static int
371 pci_probe_all_drivers(struct rte_pci_device *dev)
372 {
373         struct rte_pci_driver *dr = NULL;
374         int rc = 0;
375
376         if (dev == NULL)
377                 return -1;
378
379         /* Check if a driver is already loaded */
380         if (dev->driver != NULL)
381                 return 0;
382
383         FOREACH_DRIVER_ON_PCIBUS(dr) {
384                 rc = rte_pci_probe_one_driver(dr, dev);
385                 if (rc < 0)
386                         /* negative value is an error */
387                         return -1;
388                 if (rc > 0)
389                         /* positive value means driver doesn't support it */
390                         continue;
391                 return 0;
392         }
393         return 1;
394 }
395
396 /*
397  * Find the pci device specified by pci address, then invoke probe function of
398  * the driver of the device.
399  */
400 int
401 rte_pci_probe_one(const struct rte_pci_addr *addr)
402 {
403         struct rte_pci_device *dev = NULL;
404
405         int ret = 0;
406
407         if (addr == NULL)
408                 return -1;
409
410         /* update current pci device in global list, kernel bindings might have
411          * changed since last time we looked at it.
412          */
413         if (pci_update_device(addr) < 0)
414                 goto err_return;
415
416         FOREACH_DEVICE_ON_PCIBUS(dev) {
417                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
418                         continue;
419
420                 ret = pci_probe_all_drivers(dev);
421                 if (ret)
422                         goto err_return;
423                 return 0;
424         }
425         return -1;
426
427 err_return:
428         RTE_LOG(WARNING, EAL,
429                 "Requested device " PCI_PRI_FMT " cannot be used\n",
430                 addr->domain, addr->bus, addr->devid, addr->function);
431         return -1;
432 }
433
434 /*
435  * Detach device specified by its pci address.
436  */
437 int
438 rte_pci_detach(const struct rte_pci_addr *addr)
439 {
440         struct rte_pci_device *dev = NULL;
441         int ret = 0;
442
443         if (addr == NULL)
444                 return -1;
445
446         FOREACH_DEVICE_ON_PCIBUS(dev) {
447                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
448                         continue;
449
450                 ret = rte_pci_detach_dev(dev);
451                 if (ret < 0)
452                         /* negative value is an error */
453                         goto err_return;
454                 if (ret > 0)
455                         /* positive value means driver doesn't support it */
456                         continue;
457
458                 rte_pci_remove_device(dev);
459                 free(dev);
460                 return 0;
461         }
462         return -1;
463
464 err_return:
465         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
466                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
467                         dev->addr.devid, dev->addr.function);
468         return -1;
469 }
470
471 /*
472  * Scan the content of the PCI bus, and call the probe() function for
473  * all registered drivers that have a matching entry in its id_table
474  * for discovered devices.
475  */
476 int
477 rte_pci_probe(void)
478 {
479         struct rte_pci_device *dev = NULL;
480         size_t probed = 0, failed = 0;
481         struct rte_devargs *devargs;
482         int probe_all = 0;
483         int ret = 0;
484
485         if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
486                 probe_all = 1;
487
488         FOREACH_DEVICE_ON_PCIBUS(dev) {
489                 probed++;
490
491                 devargs = dev->device.devargs;
492                 /* probe all or only whitelisted devices */
493                 if (probe_all)
494                         ret = pci_probe_all_drivers(dev);
495                 else if (devargs != NULL &&
496                         devargs->policy == RTE_DEV_WHITELISTED)
497                         ret = pci_probe_all_drivers(dev);
498                 if (ret < 0) {
499                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
500                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
501                                  dev->addr.devid, dev->addr.function);
502                         rte_errno = errno;
503                         failed++;
504                         ret = 0;
505                 }
506         }
507
508         return (probed && probed == failed) ? -1 : 0;
509 }
510
511 /* dump one device */
512 static int
513 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
514 {
515         int i;
516
517         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
518                dev->addr.devid, dev->addr.function);
519         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
520                dev->id.device_id);
521
522         for (i = 0; i != sizeof(dev->mem_resource) /
523                 sizeof(dev->mem_resource[0]); i++) {
524                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
525                         dev->mem_resource[i].phys_addr,
526                         dev->mem_resource[i].len);
527         }
528         return 0;
529 }
530
531 /* dump devices on the bus */
532 void
533 rte_pci_dump(FILE *f)
534 {
535         struct rte_pci_device *dev = NULL;
536
537         FOREACH_DEVICE_ON_PCIBUS(dev) {
538                 pci_dump_one_device(f, dev);
539         }
540 }
541
542 static int
543 pci_parse(const char *name, void *addr)
544 {
545         struct rte_pci_addr *out = addr;
546         struct rte_pci_addr pci_addr;
547         bool parse;
548
549         parse = (eal_parse_pci_BDF(name, &pci_addr) == 0 ||
550                  eal_parse_pci_DomBDF(name, &pci_addr) == 0);
551         if (parse && addr != NULL)
552                 *out = pci_addr;
553         return parse == false;
554 }
555
556 /* register a driver */
557 void
558 rte_pci_register(struct rte_pci_driver *driver)
559 {
560         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
561         driver->bus = &rte_pci_bus;
562 }
563
564 /* unregister a driver */
565 void
566 rte_pci_unregister(struct rte_pci_driver *driver)
567 {
568         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
569         driver->bus = NULL;
570 }
571
572 /* Add a device to PCI bus */
573 void
574 rte_pci_add_device(struct rte_pci_device *pci_dev)
575 {
576         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
577 }
578
579 /* Insert a device into a predefined position in PCI bus */
580 void
581 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
582                       struct rte_pci_device *new_pci_dev)
583 {
584         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
585 }
586
587 /* Remove a device from PCI bus */
588 void
589 rte_pci_remove_device(struct rte_pci_device *pci_dev)
590 {
591         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
592 }
593
594 static struct rte_device *
595 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
596                 const void *data)
597 {
598         struct rte_pci_device *dev;
599
600         FOREACH_DEVICE_ON_PCIBUS(dev) {
601                 if (start && &dev->device == start) {
602                         start = NULL; /* starting point found */
603                         continue;
604                 }
605                 if (cmp(&dev->device, data) == 0)
606                         return &dev->device;
607         }
608
609         return NULL;
610 }
611
612 static int
613 pci_plug(struct rte_device *dev)
614 {
615         return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
616 }
617
618 static int
619 pci_unplug(struct rte_device *dev)
620 {
621         struct rte_pci_device *pdev;
622         int ret;
623
624         pdev = RTE_DEV_TO_PCI(dev);
625         ret = rte_pci_detach_dev(pdev);
626         if (ret == 0) {
627                 rte_pci_remove_device(pdev);
628                 free(pdev);
629         }
630         return ret;
631 }
632
633 struct rte_pci_bus rte_pci_bus = {
634         .bus = {
635                 .scan = rte_pci_scan,
636                 .probe = rte_pci_probe,
637                 .find_device = pci_find_device,
638                 .plug = pci_plug,
639                 .unplug = pci_unplug,
640                 .parse = pci_parse,
641                 .get_iommu_class = rte_pci_get_iommu_class,
642         },
643         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
644         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
645 };
646
647 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);