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