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