8ef805736d6f1766c2e2922357f368aeb2c64095
[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 struct pci_device_list pci_device_list;
87
88 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
89 {
90         struct rte_devargs *devargs;
91
92         TAILQ_FOREACH(devargs, &devargs_list, next) {
93                 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
94                         devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
95                         continue;
96                 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
97                         return devargs;
98         }
99         return NULL;
100 }
101
102 /* map a particular resource from a file */
103 void *
104 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
105                  int additional_flags)
106 {
107         void *mapaddr;
108
109         /* Map the PCI memory resource of device */
110         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
111                         MAP_SHARED | additional_flags, fd, offset);
112         if (mapaddr == MAP_FAILED) {
113                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
114                         __func__, fd, requested_addr,
115                         (unsigned long)size, (unsigned long)offset,
116                         strerror(errno), mapaddr);
117         } else
118                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
119
120         return mapaddr;
121 }
122
123 /* unmap a particular resource */
124 void
125 pci_unmap_resource(void *requested_addr, size_t size)
126 {
127         if (requested_addr == NULL)
128                 return;
129
130         /* Unmap the PCI memory resource of device */
131         if (munmap(requested_addr, size)) {
132                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
133                         __func__, requested_addr, (unsigned long)size,
134                         strerror(errno));
135         } else
136                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
137                                 requested_addr);
138 }
139
140 /* Map pci device */
141 static int
142 pci_map_device(struct rte_pci_device *dev)
143 {
144         int ret = -1;
145
146         /* try mapping the NIC resources using VFIO if it exists */
147         switch (dev->kdrv) {
148         case RTE_KDRV_VFIO:
149 #ifdef VFIO_PRESENT
150                 if (pci_vfio_is_enabled())
151                         ret = pci_vfio_map_resource(dev);
152 #endif
153                 break;
154         case RTE_KDRV_IGB_UIO:
155         case RTE_KDRV_UIO_GENERIC:
156         case RTE_KDRV_NIC_UIO:
157                 /* map resources for devices that use uio */
158                 ret = pci_uio_map_resource(dev);
159                 break;
160         default:
161                 RTE_LOG(DEBUG, EAL, "  Not managed by a supported kernel driver,"
162                         " skipped\n");
163                 ret = 1;
164                 break;
165         }
166
167         return ret;
168 }
169
170 #ifdef RTE_LIBRTE_EAL_HOTPLUG
171 /* Unmap pci device */
172 static void
173 pci_unmap_device(struct rte_pci_device *dev)
174 {
175         if (dev == NULL)
176                 return;
177
178         /* try unmapping the NIC resources using VFIO if it exists */
179         switch (dev->kdrv) {
180         case RTE_KDRV_VFIO:
181                 RTE_LOG(ERR, EAL, "Hotplug doesn't support vfio yet\n");
182                 break;
183         case RTE_KDRV_IGB_UIO:
184         case RTE_KDRV_UIO_GENERIC:
185         case RTE_KDRV_NIC_UIO:
186                 /* unmap resources for devices that use uio */
187                 pci_uio_unmap_resource(dev);
188                 break;
189         default:
190                 RTE_LOG(DEBUG, EAL, "  Not managed by a supported kernel driver,"
191                         " skipped\n");
192                 break;
193         }
194 }
195 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
196
197 /*
198  * If vendor/device ID match, call the devinit() function of the
199  * driver.
200  */
201 static int
202 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
203 {
204         int ret;
205         const struct rte_pci_id *id_table;
206
207         for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
208
209                 /* check if device's identifiers match the driver's ones */
210                 if (id_table->vendor_id != dev->id.vendor_id &&
211                                 id_table->vendor_id != PCI_ANY_ID)
212                         continue;
213                 if (id_table->device_id != dev->id.device_id &&
214                                 id_table->device_id != PCI_ANY_ID)
215                         continue;
216                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
217                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
218                         continue;
219                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
220                                 id_table->subsystem_device_id != PCI_ANY_ID)
221                         continue;
222
223                 struct rte_pci_addr *loc = &dev->addr;
224
225                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
226                                 loc->domain, loc->bus, loc->devid, loc->function,
227                                 dev->numa_node);
228
229                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
230                                 dev->id.device_id, dr->name);
231
232                 /* no initialization when blacklisted, return without error */
233                 if (dev->devargs != NULL &&
234                         dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
235                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
236                         return 1;
237                 }
238
239                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
240 #ifdef RTE_PCI_CONFIG
241                         /*
242                          * Set PCIe config space for high performance.
243                          * Return value can be ignored.
244                          */
245                         pci_config_space_set(dev);
246 #endif
247                         /* map resources for devices that use igb_uio */
248                         ret = pci_map_device(dev);
249                         if (ret != 0)
250                                 return ret;
251                 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
252                                 rte_eal_process_type() == RTE_PROC_PRIMARY) {
253                         /* unbind current driver */
254                         if (pci_unbind_kernel_driver(dev) < 0)
255                                 return -1;
256                 }
257
258                 /* reference driver structure */
259                 dev->driver = dr;
260
261                 /* call the driver devinit() function */
262                 return dr->devinit(dr, dev);
263         }
264         /* return positive value if driver is not found */
265         return 1;
266 }
267
268 #ifdef RTE_LIBRTE_EAL_HOTPLUG
269 /*
270  * If vendor/device ID match, call the devuninit() function of the
271  * driver.
272  */
273 static int
274 rte_eal_pci_close_one_driver(struct rte_pci_driver *dr,
275                 struct rte_pci_device *dev)
276 {
277         const struct rte_pci_id *id_table;
278
279         if ((dr == NULL) || (dev == NULL))
280                 return -EINVAL;
281
282         for (id_table = dr->id_table; id_table->vendor_id != 0; id_table++) {
283
284                 /* check if device's identifiers match the driver's ones */
285                 if (id_table->vendor_id != dev->id.vendor_id &&
286                                 id_table->vendor_id != PCI_ANY_ID)
287                         continue;
288                 if (id_table->device_id != dev->id.device_id &&
289                                 id_table->device_id != PCI_ANY_ID)
290                         continue;
291                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
292                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
293                         continue;
294                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
295                                 id_table->subsystem_device_id != PCI_ANY_ID)
296                         continue;
297
298                 struct rte_pci_addr *loc = &dev->addr;
299
300                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
301                                 loc->domain, loc->bus, loc->devid,
302                                 loc->function, dev->numa_node);
303
304                 RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
305                                 dev->id.device_id, dr->name);
306
307                 /* call the driver devuninit() function */
308                 if (dr->devuninit && (dr->devuninit(dev) < 0))
309                         return -1;      /* negative value is an error */
310
311                 /* clear driver structure */
312                 dev->driver = NULL;
313
314                 if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
315                         /* unmap resources for devices that use igb_uio */
316                         pci_unmap_device(dev);
317
318                 return 0;
319         }
320
321         /* return positive value if driver is not found */
322         return 1;
323 }
324 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
325
326 /*
327  * If vendor/device ID match, call the devinit() function of all
328  * registered driver for the given device. Return -1 if initialization
329  * failed, return 1 if no driver is found for this device.
330  */
331 static int
332 pci_probe_all_drivers(struct rte_pci_device *dev)
333 {
334         struct rte_pci_driver *dr = NULL;
335         int rc = 0;
336
337         if (dev == NULL)
338                 return -1;
339
340         TAILQ_FOREACH(dr, &pci_driver_list, next) {
341                 rc = rte_eal_pci_probe_one_driver(dr, dev);
342                 if (rc < 0)
343                         /* negative value is an error */
344                         return -1;
345                 if (rc > 0)
346                         /* positive value means driver not found */
347                         continue;
348                 return 0;
349         }
350         return 1;
351 }
352
353 #ifdef RTE_LIBRTE_EAL_HOTPLUG
354 /*
355  * If vendor/device ID match, call the devuninit() function of all
356  * registered driver for the given device. Return -1 if initialization
357  * failed, return 1 if no driver is found for this device.
358  */
359 static int
360 pci_close_all_drivers(struct rte_pci_device *dev)
361 {
362         struct rte_pci_driver *dr = NULL;
363         int rc = 0;
364
365         if (dev == NULL)
366                 return -1;
367
368         TAILQ_FOREACH(dr, &pci_driver_list, next) {
369                 rc = rte_eal_pci_close_one_driver(dr, dev);
370                 if (rc < 0)
371                         /* negative value is an error */
372                         return -1;
373                 if (rc > 0)
374                         /* positive value means driver not found */
375                         continue;
376                 return 0;
377         }
378         return 1;
379 }
380
381 /*
382  * Find the pci device specified by pci address, then invoke probe function of
383  * the driver of the devive.
384  */
385 int
386 rte_eal_pci_probe_one(const struct rte_pci_addr *addr)
387 {
388         struct rte_pci_device *dev = NULL;
389         int ret = 0;
390
391         if (addr == NULL)
392                 return -1;
393
394         TAILQ_FOREACH(dev, &pci_device_list, next) {
395                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
396                         continue;
397
398                 ret = pci_probe_all_drivers(dev);
399                 if (ret < 0)
400                         goto err_return;
401                 return 0;
402         }
403         return -1;
404
405 err_return:
406         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
407                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
408                         dev->addr.devid, dev->addr.function);
409         return -1;
410 }
411
412 /*
413  * Find the pci device specified by pci address, then invoke close function of
414  * the driver of the devive.
415  */
416 int
417 rte_eal_pci_close_one(const struct rte_pci_addr *addr)
418 {
419         struct rte_pci_device *dev = NULL;
420         int ret = 0;
421
422         if (addr == NULL)
423                 return -1;
424
425         TAILQ_FOREACH(dev, &pci_device_list, next) {
426                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
427                         continue;
428
429                 ret = pci_close_all_drivers(dev);
430                 if (ret < 0)
431                         goto err_return;
432
433                 TAILQ_REMOVE(&pci_device_list, dev, next);
434                 return 0;
435         }
436         return -1;
437
438 err_return:
439         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
440                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
441                         dev->addr.devid, dev->addr.function);
442         return -1;
443 }
444 #endif /* RTE_LIBRTE_EAL_HOTPLUG */
445
446 /*
447  * Scan the content of the PCI bus, and call the devinit() function for
448  * all registered drivers that have a matching entry in its id_table
449  * for discovered devices.
450  */
451 int
452 rte_eal_pci_probe(void)
453 {
454         struct rte_pci_device *dev = NULL;
455         struct rte_devargs *devargs;
456         int probe_all = 0;
457         int ret = 0;
458
459         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
460                 probe_all = 1;
461
462         TAILQ_FOREACH(dev, &pci_device_list, next) {
463
464                 /* set devargs in PCI structure */
465                 devargs = pci_devargs_lookup(dev);
466                 if (devargs != NULL)
467                         dev->devargs = devargs;
468
469                 /* probe all or only whitelisted devices */
470                 if (probe_all)
471                         ret = pci_probe_all_drivers(dev);
472                 else if (devargs != NULL &&
473                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
474                         ret = pci_probe_all_drivers(dev);
475                 if (ret < 0)
476                         rte_exit(EXIT_FAILURE, "Requested device " PCI_PRI_FMT
477                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
478                                  dev->addr.devid, dev->addr.function);
479         }
480
481         return 0;
482 }
483
484 /* dump one device */
485 static int
486 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
487 {
488         int i;
489
490         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
491                dev->addr.devid, dev->addr.function);
492         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
493                dev->id.device_id);
494
495         for (i = 0; i != sizeof(dev->mem_resource) /
496                 sizeof(dev->mem_resource[0]); i++) {
497                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
498                         dev->mem_resource[i].phys_addr,
499                         dev->mem_resource[i].len);
500         }
501         return 0;
502 }
503
504 /* dump devices on the bus */
505 void
506 rte_eal_pci_dump(FILE *f)
507 {
508         struct rte_pci_device *dev = NULL;
509
510         TAILQ_FOREACH(dev, &pci_device_list, next) {
511                 pci_dump_one_device(f, dev);
512         }
513 }
514
515 /* register a driver */
516 void
517 rte_eal_pci_register(struct rte_pci_driver *driver)
518 {
519         TAILQ_INSERT_TAIL(&pci_driver_list, driver, next);
520 }
521
522 /* unregister a driver */
523 void
524 rte_eal_pci_unregister(struct rte_pci_driver *driver)
525 {
526         TAILQ_REMOVE(&pci_driver_list, driver, next);
527 }