devargs: make device representation generic
[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_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_eal.h>
53 #include <rte_string_fns.h>
54 #include <rte_common.h>
55 #include <rte_devargs.h>
56
57 #include "eal_private.h"
58
59 extern struct rte_pci_bus rte_pci_bus;
60
61 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
62
63 const char *pci_get_sysfs_path(void)
64 {
65         const char *path = NULL;
66
67         path = getenv("SYSFS_PCI_DEVICES");
68         if (path == NULL)
69                 return SYSFS_PCI_DEVICES;
70
71         return path;
72 }
73
74 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
75 {
76         struct rte_devargs *devargs;
77         struct rte_pci_addr addr;
78         struct rte_bus *pbus;
79
80         pbus = rte_bus_find_by_name("pci");
81         TAILQ_FOREACH(devargs, &devargs_list, next) {
82                 if (devargs->bus != pbus)
83                         continue;
84                 devargs->bus->parse(devargs->name, &addr);
85                 if (!rte_eal_compare_pci_addr(&dev->addr, &addr))
86                         return devargs;
87         }
88         return NULL;
89 }
90
91 /* map a particular resource from a file */
92 void *
93 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
94                  int additional_flags)
95 {
96         void *mapaddr;
97
98         /* Map the PCI memory resource of device */
99         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
100                         MAP_SHARED | additional_flags, fd, offset);
101         if (mapaddr == MAP_FAILED) {
102                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
103                         __func__, fd, requested_addr,
104                         (unsigned long)size, (unsigned long)offset,
105                         strerror(errno), mapaddr);
106         } else
107                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
108
109         return mapaddr;
110 }
111
112 /* unmap a particular resource */
113 void
114 pci_unmap_resource(void *requested_addr, size_t size)
115 {
116         if (requested_addr == NULL)
117                 return;
118
119         /* Unmap the PCI memory resource of device */
120         if (munmap(requested_addr, size)) {
121                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
122                         __func__, requested_addr, (unsigned long)size,
123                         strerror(errno));
124         } else
125                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
126                                 requested_addr);
127 }
128
129 /*
130  * Match the PCI Driver and Device using the ID Table
131  *
132  * @param pci_drv
133  *      PCI driver from which ID table would be extracted
134  * @param pci_dev
135  *      PCI device to match against the driver
136  * @return
137  *      1 for successful match
138  *      0 for unsuccessful match
139  */
140 static int
141 rte_pci_match(const struct rte_pci_driver *pci_drv,
142               const struct rte_pci_device *pci_dev)
143 {
144         const struct rte_pci_id *id_table;
145
146         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
147              id_table++) {
148                 /* check if device's identifiers match the driver's ones */
149                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
150                                 id_table->vendor_id != PCI_ANY_ID)
151                         continue;
152                 if (id_table->device_id != pci_dev->id.device_id &&
153                                 id_table->device_id != PCI_ANY_ID)
154                         continue;
155                 if (id_table->subsystem_vendor_id !=
156                     pci_dev->id.subsystem_vendor_id &&
157                     id_table->subsystem_vendor_id != PCI_ANY_ID)
158                         continue;
159                 if (id_table->subsystem_device_id !=
160                     pci_dev->id.subsystem_device_id &&
161                     id_table->subsystem_device_id != PCI_ANY_ID)
162                         continue;
163                 if (id_table->class_id != pci_dev->id.class_id &&
164                                 id_table->class_id != RTE_CLASS_ANY_ID)
165                         continue;
166
167                 return 1;
168         }
169
170         return 0;
171 }
172
173 /*
174  * If vendor/device ID match, call the probe() function of the
175  * driver.
176  */
177 static int
178 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
179                          struct rte_pci_device *dev)
180 {
181         int ret;
182         struct rte_pci_addr *loc;
183
184         if ((dr == NULL) || (dev == NULL))
185                 return -EINVAL;
186
187         loc = &dev->addr;
188
189         /* The device is not blacklisted; Check if driver supports it */
190         if (!rte_pci_match(dr, dev))
191                 /* Match of device and driver failed */
192                 return 1;
193
194         RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
195                         loc->domain, loc->bus, loc->devid, loc->function,
196                         dev->device.numa_node);
197
198         /* no initialization when blacklisted, return without error */
199         if (dev->device.devargs != NULL &&
200                 dev->device.devargs->type ==
201                         RTE_DEVTYPE_BLACKLISTED_PCI) {
202                 RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
203                         " initializing\n");
204                 return 1;
205         }
206
207         RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
208                 dev->id.device_id, dr->driver.name);
209
210         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
211                 /* map resources for devices that use igb_uio */
212                 ret = rte_pci_map_device(dev);
213                 if (ret != 0)
214                         return ret;
215         }
216
217         /* reference driver structure */
218         dev->driver = dr;
219         dev->device.driver = &dr->driver;
220
221         /* call the driver probe() function */
222         ret = dr->probe(dr, dev);
223         if (ret) {
224                 dev->driver = NULL;
225                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
226                         /* Don't unmap if device is unsupported and
227                          * driver needs mapped resources.
228                          */
229                         !(ret > 0 &&
230                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
231                         rte_pci_unmap_device(dev);
232         }
233
234         return ret;
235 }
236
237 /*
238  * If vendor/device ID match, call the remove() function of the
239  * driver.
240  */
241 static int
242 rte_pci_detach_dev(struct rte_pci_device *dev)
243 {
244         struct rte_pci_addr *loc;
245         struct rte_pci_driver *dr;
246
247         if (dev == NULL)
248                 return -EINVAL;
249
250         dr = dev->driver;
251         loc = &dev->addr;
252
253         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
254                         loc->domain, loc->bus, loc->devid,
255                         loc->function, dev->device.numa_node);
256
257         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
258                         dev->id.device_id, dr->driver.name);
259
260         if (dr->remove && (dr->remove(dev) < 0))
261                 return -1;      /* negative value is an error */
262
263         /* clear driver structure */
264         dev->driver = NULL;
265
266         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
267                 /* unmap resources for devices that use igb_uio */
268                 rte_pci_unmap_device(dev);
269
270         return 0;
271 }
272
273 /*
274  * If vendor/device ID match, call the probe() function of all
275  * registered driver for the given device. Return -1 if initialization
276  * failed, return 1 if no driver is found for this device.
277  */
278 static int
279 pci_probe_all_drivers(struct rte_pci_device *dev)
280 {
281         struct rte_pci_driver *dr = NULL;
282         int rc = 0;
283
284         if (dev == NULL)
285                 return -1;
286
287         /* Check if a driver is already loaded */
288         if (dev->driver != NULL)
289                 return 0;
290
291         FOREACH_DRIVER_ON_PCIBUS(dr) {
292                 rc = rte_pci_probe_one_driver(dr, dev);
293                 if (rc < 0)
294                         /* negative value is an error */
295                         return -1;
296                 if (rc > 0)
297                         /* positive value means driver doesn't support it */
298                         continue;
299                 return 0;
300         }
301         return 1;
302 }
303
304 /*
305  * Find the pci device specified by pci address, then invoke probe function of
306  * the driver of the device.
307  */
308 int
309 rte_pci_probe_one(const struct rte_pci_addr *addr)
310 {
311         struct rte_pci_device *dev = NULL;
312
313         int ret = 0;
314
315         if (addr == NULL)
316                 return -1;
317
318         /* update current pci device in global list, kernel bindings might have
319          * changed since last time we looked at it.
320          */
321         if (pci_update_device(addr) < 0)
322                 goto err_return;
323
324         FOREACH_DEVICE_ON_PCIBUS(dev) {
325                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
326                         continue;
327
328                 ret = pci_probe_all_drivers(dev);
329                 if (ret)
330                         goto err_return;
331                 return 0;
332         }
333         return -1;
334
335 err_return:
336         RTE_LOG(WARNING, EAL,
337                 "Requested device " PCI_PRI_FMT " cannot be used\n",
338                 addr->domain, addr->bus, addr->devid, addr->function);
339         return -1;
340 }
341
342 /*
343  * Detach device specified by its pci address.
344  */
345 int
346 rte_pci_detach(const struct rte_pci_addr *addr)
347 {
348         struct rte_pci_device *dev = NULL;
349         int ret = 0;
350
351         if (addr == NULL)
352                 return -1;
353
354         FOREACH_DEVICE_ON_PCIBUS(dev) {
355                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
356                         continue;
357
358                 ret = rte_pci_detach_dev(dev);
359                 if (ret < 0)
360                         /* negative value is an error */
361                         goto err_return;
362                 if (ret > 0)
363                         /* positive value means driver doesn't support it */
364                         continue;
365
366                 rte_pci_remove_device(dev);
367                 free(dev);
368                 return 0;
369         }
370         return -1;
371
372 err_return:
373         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
374                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
375                         dev->addr.devid, dev->addr.function);
376         return -1;
377 }
378
379 /*
380  * Scan the content of the PCI bus, and call the probe() function for
381  * all registered drivers that have a matching entry in its id_table
382  * for discovered devices.
383  */
384 int
385 rte_pci_probe(void)
386 {
387         struct rte_pci_device *dev = NULL;
388         size_t probed = 0, failed = 0;
389         struct rte_devargs *devargs;
390         int probe_all = 0;
391         int ret = 0;
392
393         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
394                 probe_all = 1;
395
396         FOREACH_DEVICE_ON_PCIBUS(dev) {
397                 probed++;
398
399                 /* set devargs in PCI structure */
400                 devargs = pci_devargs_lookup(dev);
401                 if (devargs != NULL)
402                         dev->device.devargs = devargs;
403
404                 /* probe all or only whitelisted devices */
405                 if (probe_all)
406                         ret = pci_probe_all_drivers(dev);
407                 else if (devargs != NULL &&
408                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
409                         ret = pci_probe_all_drivers(dev);
410                 if (ret < 0) {
411                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
412                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
413                                  dev->addr.devid, dev->addr.function);
414                         rte_errno = errno;
415                         failed++;
416                         ret = 0;
417                 }
418         }
419
420         return (probed && probed == failed) ? -1 : 0;
421 }
422
423 /* dump one device */
424 static int
425 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
426 {
427         int i;
428
429         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
430                dev->addr.devid, dev->addr.function);
431         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
432                dev->id.device_id);
433
434         for (i = 0; i != sizeof(dev->mem_resource) /
435                 sizeof(dev->mem_resource[0]); i++) {
436                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
437                         dev->mem_resource[i].phys_addr,
438                         dev->mem_resource[i].len);
439         }
440         return 0;
441 }
442
443 /* dump devices on the bus */
444 void
445 rte_pci_dump(FILE *f)
446 {
447         struct rte_pci_device *dev = NULL;
448
449         FOREACH_DEVICE_ON_PCIBUS(dev) {
450                 pci_dump_one_device(f, dev);
451         }
452 }
453
454 static int
455 pci_parse(const char *name, void *addr)
456 {
457         struct rte_pci_addr *out = addr;
458         struct rte_pci_addr pci_addr;
459         bool parse;
460
461         parse = (eal_parse_pci_BDF(name, &pci_addr) == 0 ||
462                  eal_parse_pci_DomBDF(name, &pci_addr) == 0);
463         if (parse && addr != NULL)
464                 *out = pci_addr;
465         return parse == false;
466 }
467
468 /* register a driver */
469 void
470 rte_pci_register(struct rte_pci_driver *driver)
471 {
472         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
473         driver->bus = &rte_pci_bus;
474 }
475
476 /* unregister a driver */
477 void
478 rte_pci_unregister(struct rte_pci_driver *driver)
479 {
480         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
481         driver->bus = NULL;
482 }
483
484 /* Add a device to PCI bus */
485 void
486 rte_pci_add_device(struct rte_pci_device *pci_dev)
487 {
488         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
489 }
490
491 /* Insert a device into a predefined position in PCI bus */
492 void
493 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
494                       struct rte_pci_device *new_pci_dev)
495 {
496         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
497 }
498
499 /* Remove a device from PCI bus */
500 void
501 rte_pci_remove_device(struct rte_pci_device *pci_dev)
502 {
503         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
504 }
505
506 static struct rte_device *
507 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
508                 const void *data)
509 {
510         struct rte_pci_device *dev;
511
512         FOREACH_DEVICE_ON_PCIBUS(dev) {
513                 if (start && &dev->device == start) {
514                         start = NULL; /* starting point found */
515                         continue;
516                 }
517                 if (cmp(&dev->device, data) == 0)
518                         return &dev->device;
519         }
520
521         return NULL;
522 }
523
524 static int
525 pci_plug(struct rte_device *dev, const char *devargs __rte_unused)
526 {
527         struct rte_pci_device *pdev;
528         struct rte_pci_addr *addr;
529
530         addr = &RTE_DEV_TO_PCI(dev)->addr;
531
532         /* Find the current device holding this address in the bus. */
533         FOREACH_DEVICE_ON_PCIBUS(pdev) {
534                 if (rte_eal_compare_pci_addr(&pdev->addr, addr) == 0)
535                         return rte_pci_probe_one(addr);
536         }
537
538         rte_errno = ENODEV;
539         return -1;
540 }
541
542 static int
543 pci_unplug(struct rte_device *dev)
544 {
545         struct rte_pci_device *pdev;
546
547         pdev = RTE_DEV_TO_PCI(dev);
548         if (rte_pci_detach(&pdev->addr) != 0) {
549                 rte_errno = ENODEV;
550                 return -1;
551         }
552         return 0;
553 }
554
555 struct rte_pci_bus rte_pci_bus = {
556         .bus = {
557                 .scan = rte_pci_scan,
558                 .probe = rte_pci_probe,
559                 .find_device = pci_find_device,
560                 .plug = pci_plug,
561                 .unplug = pci_unplug,
562                 .parse = pci_parse,
563         },
564         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
565         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
566 };
567
568 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);