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