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