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