498a3bb07fe78fcde0e696cffcd558878ff7fe00
[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_pci.h>
76 #include <rte_per_lcore.h>
77 #include <rte_memory.h>
78 #include <rte_memzone.h>
79 #include <rte_eal.h>
80 #include <rte_string_fns.h>
81 #include <rte_common.h>
82 #include <rte_devargs.h>
83
84 #include "eal_private.h"
85
86 struct pci_driver_list pci_driver_list =
87         TAILQ_HEAD_INITIALIZER(pci_driver_list);
88 struct pci_device_list pci_device_list =
89         TAILQ_HEAD_INITIALIZER(pci_device_list);
90
91 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
92
93 const char *pci_get_sysfs_path(void)
94 {
95         const char *path = NULL;
96
97         path = getenv("SYSFS_PCI_DEVICES");
98         if (path == NULL)
99                 return SYSFS_PCI_DEVICES;
100
101         return path;
102 }
103
104 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
105 {
106         struct rte_devargs *devargs;
107
108         TAILQ_FOREACH(devargs, &devargs_list, next) {
109                 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
110                         devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
111                         continue;
112                 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
113                         return devargs;
114         }
115         return NULL;
116 }
117
118 /* map a particular resource from a file */
119 void *
120 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
121                  int additional_flags)
122 {
123         void *mapaddr;
124
125         /* Map the PCI memory resource of device */
126         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
127                         MAP_SHARED | additional_flags, fd, offset);
128         if (mapaddr == MAP_FAILED) {
129                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
130                         __func__, fd, requested_addr,
131                         (unsigned long)size, (unsigned long)offset,
132                         strerror(errno), mapaddr);
133         } else
134                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
135
136         return mapaddr;
137 }
138
139 /* unmap a particular resource */
140 void
141 pci_unmap_resource(void *requested_addr, size_t size)
142 {
143         if (requested_addr == NULL)
144                 return;
145
146         /* Unmap the PCI memory resource of device */
147         if (munmap(requested_addr, size)) {
148                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
149                         __func__, requested_addr, (unsigned long)size,
150                         strerror(errno));
151         } else
152                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
153                                 requested_addr);
154 }
155
156 /*
157  * Match the PCI Driver and Device using the ID Table
158  *
159  * @param pci_drv
160  *      PCI driver from which ID table would be extracted
161  * @param pci_dev
162  *      PCI device to match against the driver
163  * @return
164  *      1 for successful match
165  *      0 for unsuccessful match
166  */
167 static int
168 rte_pci_match(const struct rte_pci_driver *pci_drv,
169               const struct rte_pci_device *pci_dev)
170 {
171         const struct rte_pci_id *id_table;
172
173         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
174              id_table++) {
175                 /* check if device's identifiers match the driver's ones */
176                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
177                                 id_table->vendor_id != PCI_ANY_ID)
178                         continue;
179                 if (id_table->device_id != pci_dev->id.device_id &&
180                                 id_table->device_id != PCI_ANY_ID)
181                         continue;
182                 if (id_table->subsystem_vendor_id !=
183                     pci_dev->id.subsystem_vendor_id &&
184                     id_table->subsystem_vendor_id != PCI_ANY_ID)
185                         continue;
186                 if (id_table->subsystem_device_id !=
187                     pci_dev->id.subsystem_device_id &&
188                     id_table->subsystem_device_id != PCI_ANY_ID)
189                         continue;
190                 if (id_table->class_id != pci_dev->id.class_id &&
191                                 id_table->class_id != RTE_CLASS_ANY_ID)
192                         continue;
193
194                 return 1;
195         }
196
197         return 0;
198 }
199
200 /*
201  * If vendor/device ID match, call the probe() function of the
202  * driver.
203  */
204 static int
205 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr,
206                              struct rte_pci_device *dev)
207 {
208         int ret;
209         struct rte_pci_addr *loc;
210
211         if ((dr == NULL) || (dev == NULL))
212                 return -EINVAL;
213
214         loc = &dev->addr;
215
216         /* The device is not blacklisted; Check if driver supports it */
217         if (!rte_pci_match(dr, dev)) {
218                 /* Match of device and driver failed */
219                 RTE_LOG(DEBUG, EAL, "Driver (%s) doesn't match the device\n",
220                         dr->driver.name);
221                 return 1;
222         }
223
224         RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
225                         loc->domain, loc->bus, loc->devid, loc->function,
226                         dev->device.numa_node);
227
228         /* no initialization when blacklisted, return without error */
229         if (dev->device.devargs != NULL &&
230                 dev->device.devargs->type ==
231                         RTE_DEVTYPE_BLACKLISTED_PCI) {
232                 RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
233                         " initializing\n");
234                 return 1;
235         }
236
237         RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
238                 dev->id.device_id, dr->driver.name);
239
240         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
241                 /* map resources for devices that use igb_uio */
242                 ret = rte_eal_pci_map_device(dev);
243                 if (ret != 0)
244                         return ret;
245         }
246
247         /* reference driver structure */
248         dev->driver = dr;
249
250         /* call the driver probe() function */
251         ret = dr->probe(dr, dev);
252         if (ret) {
253                 dev->driver = NULL;
254                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
255                         rte_eal_pci_unmap_device(dev);
256         }
257
258         return ret;
259 }
260
261 /*
262  * If vendor/device ID match, call the remove() function of the
263  * driver.
264  */
265 static int
266 rte_eal_pci_detach_dev(struct rte_pci_device *dev)
267 {
268         struct rte_pci_addr *loc;
269         struct rte_pci_driver *dr;
270
271         if (dev == NULL)
272                 return -EINVAL;
273
274         dr = dev->driver;
275         loc = &dev->addr;
276
277         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
278                         loc->domain, loc->bus, loc->devid,
279                         loc->function, dev->device.numa_node);
280
281         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
282                         dev->id.device_id, dr->driver.name);
283
284         if (dr->remove && (dr->remove(dev) < 0))
285                 return -1;      /* negative value is an error */
286
287         /* clear driver structure */
288         dev->driver = NULL;
289
290         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
291                 /* unmap resources for devices that use igb_uio */
292                 rte_eal_pci_unmap_device(dev);
293
294         return 0;
295 }
296
297 /*
298  * If vendor/device ID match, call the probe() function of all
299  * registered driver for the given device. Return -1 if initialization
300  * failed, return 1 if no driver is found for this device.
301  */
302 static int
303 pci_probe_all_drivers(struct rte_pci_device *dev)
304 {
305         struct rte_pci_driver *dr = NULL;
306         int rc = 0;
307
308         if (dev == NULL)
309                 return -1;
310
311         /* Check if a driver is already loaded */
312         if (dev->driver != NULL)
313                 return 0;
314
315         TAILQ_FOREACH(dr, &pci_driver_list, next) {
316                 rc = rte_eal_pci_probe_one_driver(dr, dev);
317                 if (rc < 0)
318                         /* negative value is an error */
319                         return -1;
320                 if (rc > 0)
321                         /* positive value means driver doesn't support it */
322                         continue;
323                 return 0;
324         }
325         return 1;
326 }
327
328 /*
329  * Find the pci device specified by pci address, then invoke probe function of
330  * the driver of the devive.
331  */
332 int
333 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
334 {
335         struct rte_pci_device *dev = NULL;
336         int ret = 0;
337
338         if (addr == NULL)
339                 return -1;
340
341         /* update current pci device in global list, kernel bindings might have
342          * changed since last time we looked at it.
343          */
344         if (pci_update_device(addr) < 0)
345                 goto err_return;
346
347         TAILQ_FOREACH(dev, &pci_device_list, next) {
348                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
349                         continue;
350
351                 ret = pci_probe_all_drivers(dev);
352                 if (ret)
353                         goto err_return;
354                 return 0;
355         }
356         return -1;
357
358 err_return:
359         RTE_LOG(WARNING, EAL,
360                 "Requested device " PCI_PRI_FMT " cannot be used\n",
361                 addr->domain, addr->bus, addr->devid, addr->function);
362         return -1;
363 }
364
365 /*
366  * Detach device specified by its pci address.
367  */
368 int
369 rte_eal_pci_detach(const struct rte_pci_addr *addr)
370 {
371         struct rte_pci_device *dev = NULL;
372         int ret = 0;
373
374         if (addr == NULL)
375                 return -1;
376
377         TAILQ_FOREACH(dev, &pci_device_list, next) {
378                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
379                         continue;
380
381                 ret = rte_eal_pci_detach_dev(dev);
382                 if (ret < 0)
383                         /* negative value is an error */
384                         goto err_return;
385                 if (ret > 0)
386                         /* positive value means driver doesn't support it */
387                         continue;
388
389                 TAILQ_REMOVE(&pci_device_list, dev, next);
390                 free(dev);
391                 return 0;
392         }
393         return -1;
394
395 err_return:
396         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
397                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
398                         dev->addr.devid, dev->addr.function);
399         return -1;
400 }
401
402 /*
403  * Scan the content of the PCI bus, and call the probe() function for
404  * all registered drivers that have a matching entry in its id_table
405  * for discovered devices.
406  */
407 int
408 rte_eal_pci_probe(void)
409 {
410         struct rte_pci_device *dev = NULL;
411         size_t probed = 0, failed = 0;
412         struct rte_devargs *devargs;
413         int probe_all = 0;
414         int ret = 0;
415
416         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
417                 probe_all = 1;
418
419         TAILQ_FOREACH(dev, &pci_device_list, next) {
420                 probed++;
421
422                 /* set devargs in PCI structure */
423                 devargs = pci_devargs_lookup(dev);
424                 if (devargs != NULL)
425                         dev->device.devargs = devargs;
426
427                 /* probe all or only whitelisted devices */
428                 if (probe_all)
429                         ret = pci_probe_all_drivers(dev);
430                 else if (devargs != NULL &&
431                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
432                         ret = pci_probe_all_drivers(dev);
433                 if (ret < 0) {
434                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
435                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
436                                  dev->addr.devid, dev->addr.function);
437                         rte_errno = errno;
438                         failed++;
439                         ret = 0;
440                 }
441         }
442
443         return (probed && probed == failed) ? -1 : 0;
444 }
445
446 /* dump one device */
447 static int
448 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
449 {
450         int i;
451
452         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
453                dev->addr.devid, dev->addr.function);
454         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
455                dev->id.device_id);
456
457         for (i = 0; i != sizeof(dev->mem_resource) /
458                 sizeof(dev->mem_resource[0]); i++) {
459                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
460                         dev->mem_resource[i].phys_addr,
461                         dev->mem_resource[i].len);
462         }
463         return 0;
464 }
465
466 /* dump devices on the bus */
467 void
468 rte_eal_pci_dump(FILE *f)
469 {
470         struct rte_pci_device *dev = NULL;
471
472         TAILQ_FOREACH(dev, &pci_device_list, next) {
473                 pci_dump_one_device(f, dev);
474         }
475 }
476
477 /* register a driver */
478 void
479 rte_eal_pci_register(struct rte_pci_driver *driver)
480 {
481         TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
482         rte_eal_driver_register(&driver->driver);
483 }
484
485 /* unregister a driver */
486 void
487 rte_eal_pci_unregister(struct rte_pci_driver *driver)
488 {
489         rte_eal_driver_unregister(&driver->driver);
490         TAILQ_REMOVE(&pci_driver_list, driver, next);
491 }