1ab92c9c1074289c76ced6aa20eb74a4b3c1e615
[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  * If vendor/device ID match, call the probe() function of the
158  * driver.
159  */
160 static int
161 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
162 {
163         int ret;
164         const struct rte_pci_id *id_table;
165
166         for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
167
168                 /* check if device's identifiers match the driver's ones */
169                 if (id_table->vendor_id != dev->id.vendor_id &&
170                                 id_table->vendor_id != PCI_ANY_ID)
171                         continue;
172                 if (id_table->device_id != dev->id.device_id &&
173                                 id_table->device_id != PCI_ANY_ID)
174                         continue;
175                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
176                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
177                         continue;
178                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
179                                 id_table->subsystem_device_id != PCI_ANY_ID)
180                         continue;
181                 if (id_table->class_id != dev->id.class_id &&
182                                 id_table->class_id != RTE_CLASS_ANY_ID)
183                         continue;
184
185                 struct rte_pci_addr *loc = &dev->addr;
186
187                 RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
188                                 loc->domain, loc->bus, loc->devid, loc->function,
189                                 dev->device.numa_node);
190
191                 /* no initialization when blacklisted, return without error */
192                 if (dev->device.devargs != NULL &&
193                         dev->device.devargs->type ==
194                                 RTE_DEVTYPE_BLACKLISTED_PCI) {
195                         RTE_LOG(INFO, EAL, "  Device is blacklisted, not initializing\n");
196                         return 1;
197                 }
198
199                 RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
200                                 dev->id.device_id, dr->driver.name);
201
202                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
203                         /* map resources for devices that use igb_uio or VFIO */
204                         ret = rte_eal_pci_map_device(dev);
205                         if (ret != 0)
206                                 return ret;
207                 }
208
209                 /* reference driver structure */
210                 dev->driver = dr;
211
212                 /* call the driver probe() function */
213                 ret = dr->probe(dr, dev);
214                 if (ret) {
215                         dev->driver = NULL;
216                         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
217                                 rte_eal_pci_unmap_device(dev);
218                 }
219
220                 return ret;
221         }
222         /* return positive value if driver doesn't support this device */
223         return 1;
224 }
225
226 /*
227  * If vendor/device ID match, call the remove() function of the
228  * driver.
229  */
230 static int
231 rte_eal_pci_detach_dev(struct rte_pci_driver *dr,
232                 struct rte_pci_device *dev)
233 {
234         const struct rte_pci_id *id_table;
235
236         if ((dr == NULL) || (dev == NULL))
237                 return -EINVAL;
238
239         for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
240
241                 /* check if device's identifiers match the driver's ones */
242                 if (id_table->vendor_id != dev->id.vendor_id &&
243                                 id_table->vendor_id != PCI_ANY_ID)
244                         continue;
245                 if (id_table->device_id != dev->id.device_id &&
246                                 id_table->device_id != PCI_ANY_ID)
247                         continue;
248                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
249                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
250                         continue;
251                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
252                                 id_table->subsystem_device_id != PCI_ANY_ID)
253                         continue;
254
255                 struct rte_pci_addr *loc = &dev->addr;
256
257                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
258                                 loc->domain, loc->bus, loc->devid,
259                                 loc->function, dev->device.numa_node);
260
261                 RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
262                                 dev->id.device_id, dr->driver.name);
263
264                 if (dr->remove && (dr->remove(dev) < 0))
265                         return -1;      /* negative value is an error */
266
267                 /* clear driver structure */
268                 dev->driver = NULL;
269
270                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
271                         /* unmap resources for devices that use igb_uio */
272                         rte_eal_pci_unmap_device(dev);
273
274                 return 0;
275         }
276
277         /* return positive value if driver doesn't support this device */
278         return 1;
279 }
280
281 /*
282  * If vendor/device ID match, call the probe() function of all
283  * registered driver for the given device. Return -1 if initialization
284  * failed, return 1 if no driver is found for this device.
285  */
286 static int
287 pci_probe_all_drivers(struct rte_pci_device *dev)
288 {
289         struct rte_pci_driver *dr = NULL;
290         int rc = 0;
291
292         if (dev == NULL)
293                 return -1;
294
295         /* Check if a driver is already loaded */
296         if (dev->driver != NULL)
297                 return 0;
298
299         TAILQ_FOREACH(dr, &pci_driver_list, next) {
300                 rc = rte_eal_pci_probe_one_driver(dr, dev);
301                 if (rc < 0)
302                         /* negative value is an error */
303                         return -1;
304                 if (rc > 0)
305                         /* positive value means driver doesn't support it */
306                         continue;
307                 return 0;
308         }
309         return 1;
310 }
311
312 /*
313  * If vendor/device ID match, call the remove() function of all
314  * registered driver for the given device. Return -1 if initialization
315  * failed, return 1 if no driver is found for this device.
316  */
317 static int
318 pci_detach_all_drivers(struct rte_pci_device *dev)
319 {
320         struct rte_pci_driver *dr = NULL;
321         int rc = 0;
322
323         if (dev == NULL)
324                 return -1;
325
326         TAILQ_FOREACH(dr, &pci_driver_list, next) {
327                 rc = rte_eal_pci_detach_dev(dr, dev);
328                 if (rc < 0)
329                         /* negative value is an error */
330                         return -1;
331                 if (rc > 0)
332                         /* positive value means driver doesn't support it */
333                         continue;
334                 return 0;
335         }
336         return 1;
337 }
338
339 /*
340  * Find the pci device specified by pci address, then invoke probe function of
341  * the driver of the devive.
342  */
343 int
344 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
345 {
346         struct rte_pci_device *dev = NULL;
347         int ret = 0;
348
349         if (addr == NULL)
350                 return -1;
351
352         /* update current pci device in global list, kernel bindings might have
353          * changed since last time we looked at it.
354          */
355         if (pci_update_device(addr) < 0)
356                 goto err_return;
357
358         TAILQ_FOREACH(dev, &pci_device_list, next) {
359                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
360                         continue;
361
362                 ret = pci_probe_all_drivers(dev);
363                 if (ret)
364                         goto err_return;
365                 return 0;
366         }
367         return -1;
368
369 err_return:
370         RTE_LOG(WARNING, EAL,
371                 "Requested device " PCI_PRI_FMT " cannot be used\n",
372                 addr->domain, addr->bus, addr->devid, addr->function);
373         return -1;
374 }
375
376 /*
377  * Detach device specified by its pci address.
378  */
379 int
380 rte_eal_pci_detach(const struct rte_pci_addr *addr)
381 {
382         struct rte_pci_device *dev = NULL;
383         int ret = 0;
384
385         if (addr == NULL)
386                 return -1;
387
388         TAILQ_FOREACH(dev, &pci_device_list, next) {
389                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
390                         continue;
391
392                 ret = pci_detach_all_drivers(dev);
393                 if (ret < 0)
394                         goto err_return;
395
396                 TAILQ_REMOVE(&pci_device_list, dev, next);
397                 free(dev);
398                 return 0;
399         }
400         return -1;
401
402 err_return:
403         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
404                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
405                         dev->addr.devid, dev->addr.function);
406         return -1;
407 }
408
409 /*
410  * Scan the content of the PCI bus, and call the probe() function for
411  * all registered drivers that have a matching entry in its id_table
412  * for discovered devices.
413  */
414 int
415 rte_eal_pci_probe(void)
416 {
417         struct rte_pci_device *dev = NULL;
418         size_t probed = 0, failed = 0;
419         struct rte_devargs *devargs;
420         int probe_all = 0;
421         int ret = 0;
422
423         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
424                 probe_all = 1;
425
426         TAILQ_FOREACH(dev, &pci_device_list, next) {
427                 probed++;
428
429                 /* set devargs in PCI structure */
430                 devargs = pci_devargs_lookup(dev);
431                 if (devargs != NULL)
432                         dev->device.devargs = devargs;
433
434                 /* probe all or only whitelisted devices */
435                 if (probe_all)
436                         ret = pci_probe_all_drivers(dev);
437                 else if (devargs != NULL &&
438                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
439                         ret = pci_probe_all_drivers(dev);
440                 if (ret < 0) {
441                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
442                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
443                                  dev->addr.devid, dev->addr.function);
444                         rte_errno = errno;
445                         failed++;
446                         ret = 0;
447                 }
448         }
449
450         return (probed && probed == failed) ? -1 : 0;
451 }
452
453 /* dump one device */
454 static int
455 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
456 {
457         int i;
458
459         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
460                dev->addr.devid, dev->addr.function);
461         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
462                dev->id.device_id);
463
464         for (i = 0; i != sizeof(dev->mem_resource) /
465                 sizeof(dev->mem_resource[0]); i++) {
466                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
467                         dev->mem_resource[i].phys_addr,
468                         dev->mem_resource[i].len);
469         }
470         return 0;
471 }
472
473 /* dump devices on the bus */
474 void
475 rte_eal_pci_dump(FILE *f)
476 {
477         struct rte_pci_device *dev = NULL;
478
479         TAILQ_FOREACH(dev, &pci_device_list, next) {
480                 pci_dump_one_device(f, dev);
481         }
482 }
483
484 /* register a driver */
485 void
486 rte_eal_pci_register(struct rte_pci_driver *driver)
487 {
488         TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
489         rte_eal_driver_register(&driver->driver);
490 }
491
492 /* unregister a driver */
493 void
494 rte_eal_pci_unregister(struct rte_pci_driver *driver)
495 {
496         rte_eal_driver_unregister(&driver->driver);
497         TAILQ_REMOVE(&pci_driver_list, driver, next);
498 }