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