78b097e61d3dacf372f04bdc1905122038951e8b
[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  *   Copyright 2013-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
12  *     * Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     * Redistributions in binary form must reproduce the above copyright
15  *       notice, this list of conditions and the following disclaimer in
16  *       the documentation and/or other materials provided with the
17  *       distribution.
18  *     * Neither the name of Intel Corporation nor the names of its
19  *       contributors may be used to endorse or promote products derived
20  *       from this software without specific prior written permission.
21  *
22  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #include <string.h>
36 #include <inttypes.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <sys/queue.h>
41 #include <sys/mman.h>
42
43 #include <rte_errno.h>
44 #include <rte_interrupts.h>
45 #include <rte_log.h>
46 #include <rte_bus.h>
47 #include <rte_pci.h>
48 #include <rte_per_lcore.h>
49 #include <rte_memory.h>
50 #include <rte_memzone.h>
51 #include <rte_eal.h>
52 #include <rte_string_fns.h>
53 #include <rte_common.h>
54 #include <rte_devargs.h>
55
56 #include "eal_private.h"
57
58 extern struct rte_pci_bus rte_pci_bus;
59
60 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
61
62 const char *pci_get_sysfs_path(void)
63 {
64         const char *path = NULL;
65
66         path = getenv("SYSFS_PCI_DEVICES");
67         if (path == NULL)
68                 return SYSFS_PCI_DEVICES;
69
70         return path;
71 }
72
73 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
74 {
75         struct rte_devargs *devargs;
76
77         TAILQ_FOREACH(devargs, &devargs_list, next) {
78                 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
79                         devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
80                         continue;
81                 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
82                         return devargs;
83         }
84         return NULL;
85 }
86
87 /* map a particular resource from a file */
88 void *
89 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
90                  int additional_flags)
91 {
92         void *mapaddr;
93
94         /* Map the PCI memory resource of device */
95         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
96                         MAP_SHARED | additional_flags, fd, offset);
97         if (mapaddr == MAP_FAILED) {
98                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
99                         __func__, fd, requested_addr,
100                         (unsigned long)size, (unsigned long)offset,
101                         strerror(errno), mapaddr);
102         } else
103                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
104
105         return mapaddr;
106 }
107
108 /* unmap a particular resource */
109 void
110 pci_unmap_resource(void *requested_addr, size_t size)
111 {
112         if (requested_addr == NULL)
113                 return;
114
115         /* Unmap the PCI memory resource of device */
116         if (munmap(requested_addr, size)) {
117                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
118                         __func__, requested_addr, (unsigned long)size,
119                         strerror(errno));
120         } else
121                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
122                                 requested_addr);
123 }
124
125 /*
126  * Match the PCI Driver and Device using the ID Table
127  *
128  * @param pci_drv
129  *      PCI driver from which ID table would be extracted
130  * @param pci_dev
131  *      PCI device to match against the driver
132  * @return
133  *      1 for successful match
134  *      0 for unsuccessful match
135  */
136 static int
137 rte_pci_match(const struct rte_pci_driver *pci_drv,
138               const struct rte_pci_device *pci_dev)
139 {
140         const struct rte_pci_id *id_table;
141
142         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
143              id_table++) {
144                 /* check if device's identifiers match the driver's ones */
145                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
146                                 id_table->vendor_id != PCI_ANY_ID)
147                         continue;
148                 if (id_table->device_id != pci_dev->id.device_id &&
149                                 id_table->device_id != PCI_ANY_ID)
150                         continue;
151                 if (id_table->subsystem_vendor_id !=
152                     pci_dev->id.subsystem_vendor_id &&
153                     id_table->subsystem_vendor_id != PCI_ANY_ID)
154                         continue;
155                 if (id_table->subsystem_device_id !=
156                     pci_dev->id.subsystem_device_id &&
157                     id_table->subsystem_device_id != PCI_ANY_ID)
158                         continue;
159                 if (id_table->class_id != pci_dev->id.class_id &&
160                                 id_table->class_id != RTE_CLASS_ANY_ID)
161                         continue;
162
163                 return 1;
164         }
165
166         return 0;
167 }
168
169 /*
170  * If vendor/device ID match, call the probe() function of the
171  * driver.
172  */
173 static int
174 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
175                          struct rte_pci_device *dev)
176 {
177         int ret;
178         struct rte_pci_addr *loc;
179
180         if ((dr == NULL) || (dev == NULL))
181                 return -EINVAL;
182
183         loc = &dev->addr;
184
185         /* The device is not blacklisted; Check if driver supports it */
186         if (!rte_pci_match(dr, dev)) {
187                 /* Match of device and driver failed */
188                 RTE_LOG(DEBUG, EAL, "Driver (%s) doesn't match the device\n",
189                         dr->driver.name);
190                 return 1;
191         }
192
193         RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
194                         loc->domain, loc->bus, loc->devid, loc->function,
195                         dev->device.numa_node);
196
197         /* no initialization when blacklisted, return without error */
198         if (dev->device.devargs != NULL &&
199                 dev->device.devargs->type ==
200                         RTE_DEVTYPE_BLACKLISTED_PCI) {
201                 RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
202                         " initializing\n");
203                 return 1;
204         }
205
206         RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
207                 dev->id.device_id, dr->driver.name);
208
209         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
210                 /* map resources for devices that use igb_uio */
211                 ret = rte_pci_map_device(dev);
212                 if (ret != 0)
213                         return ret;
214         }
215
216         /* reference driver structure */
217         dev->driver = dr;
218         dev->device.driver = &dr->driver;
219
220         /* call the driver probe() function */
221         ret = dr->probe(dr, dev);
222         if (ret) {
223                 dev->driver = NULL;
224                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
225                         /* Don't unmap if device is unsupported and
226                          * driver needs mapped resources.
227                          */
228                         !(ret > 0 &&
229                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
230                         rte_pci_unmap_device(dev);
231         }
232
233         return ret;
234 }
235
236 /*
237  * If vendor/device ID match, call the remove() function of the
238  * driver.
239  */
240 static int
241 rte_pci_detach_dev(struct rte_pci_device *dev)
242 {
243         struct rte_pci_addr *loc;
244         struct rte_pci_driver *dr;
245
246         if (dev == NULL)
247                 return -EINVAL;
248
249         dr = dev->driver;
250         loc = &dev->addr;
251
252         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
253                         loc->domain, loc->bus, loc->devid,
254                         loc->function, dev->device.numa_node);
255
256         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
257                         dev->id.device_id, dr->driver.name);
258
259         if (dr->remove && (dr->remove(dev) < 0))
260                 return -1;      /* negative value is an error */
261
262         /* clear driver structure */
263         dev->driver = NULL;
264
265         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
266                 /* unmap resources for devices that use igb_uio */
267                 rte_pci_unmap_device(dev);
268
269         return 0;
270 }
271
272 /*
273  * If vendor/device ID match, call the probe() function of all
274  * registered driver for the given device. Return -1 if initialization
275  * failed, return 1 if no driver is found for this device.
276  */
277 static int
278 pci_probe_all_drivers(struct rte_pci_device *dev)
279 {
280         struct rte_pci_driver *dr = NULL;
281         int rc = 0;
282
283         if (dev == NULL)
284                 return -1;
285
286         /* Check if a driver is already loaded */
287         if (dev->driver != NULL)
288                 return 0;
289
290         FOREACH_DRIVER_ON_PCIBUS(dr) {
291                 rc = rte_pci_probe_one_driver(dr, dev);
292                 if (rc < 0)
293                         /* negative value is an error */
294                         return -1;
295                 if (rc > 0)
296                         /* positive value means driver doesn't support it */
297                         continue;
298                 return 0;
299         }
300         return 1;
301 }
302
303 /*
304  * Find the pci device specified by pci address, then invoke probe function of
305  * the driver of the devive.
306  */
307 int
308 rte_pci_probe_one(const struct rte_pci_addr *addr)
309 {
310         struct rte_pci_device *dev = NULL;
311
312         int ret = 0;
313
314         if (addr == NULL)
315                 return -1;
316
317         /* update current pci device in global list, kernel bindings might have
318          * changed since last time we looked at it.
319          */
320         if (pci_update_device(addr) < 0)
321                 goto err_return;
322
323         FOREACH_DEVICE_ON_PCIBUS(dev) {
324                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
325                         continue;
326
327                 ret = pci_probe_all_drivers(dev);
328                 if (ret)
329                         goto err_return;
330                 return 0;
331         }
332         return -1;
333
334 err_return:
335         RTE_LOG(WARNING, EAL,
336                 "Requested device " PCI_PRI_FMT " cannot be used\n",
337                 addr->domain, addr->bus, addr->devid, addr->function);
338         return -1;
339 }
340
341 /*
342  * Detach device specified by its pci address.
343  */
344 int
345 rte_pci_detach(const struct rte_pci_addr *addr)
346 {
347         struct rte_pci_device *dev = NULL;
348         int ret = 0;
349
350         if (addr == NULL)
351                 return -1;
352
353         FOREACH_DEVICE_ON_PCIBUS(dev) {
354                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
355                         continue;
356
357                 ret = rte_pci_detach_dev(dev);
358                 if (ret < 0)
359                         /* negative value is an error */
360                         goto err_return;
361                 if (ret > 0)
362                         /* positive value means driver doesn't support it */
363                         continue;
364
365                 rte_pci_remove_device(dev);
366                 free(dev);
367                 return 0;
368         }
369         return -1;
370
371 err_return:
372         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
373                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
374                         dev->addr.devid, dev->addr.function);
375         return -1;
376 }
377
378 /*
379  * Scan the content of the PCI bus, and call the probe() function for
380  * all registered drivers that have a matching entry in its id_table
381  * for discovered devices.
382  */
383 int
384 rte_pci_probe(void)
385 {
386         struct rte_pci_device *dev = NULL;
387         size_t probed = 0, failed = 0;
388         struct rte_devargs *devargs;
389         int probe_all = 0;
390         int ret = 0;
391
392         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
393                 probe_all = 1;
394
395         FOREACH_DEVICE_ON_PCIBUS(dev) {
396                 probed++;
397
398                 /* set devargs in PCI structure */
399                 devargs = pci_devargs_lookup(dev);
400                 if (devargs != NULL)
401                         dev->device.devargs = devargs;
402
403                 /* probe all or only whitelisted devices */
404                 if (probe_all)
405                         ret = pci_probe_all_drivers(dev);
406                 else if (devargs != NULL &&
407                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
408                         ret = pci_probe_all_drivers(dev);
409                 if (ret < 0) {
410                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
411                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
412                                  dev->addr.devid, dev->addr.function);
413                         rte_errno = errno;
414                         failed++;
415                         ret = 0;
416                 }
417         }
418
419         return (probed && probed == failed) ? -1 : 0;
420 }
421
422 /* dump one device */
423 static int
424 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
425 {
426         int i;
427
428         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
429                dev->addr.devid, dev->addr.function);
430         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
431                dev->id.device_id);
432
433         for (i = 0; i != sizeof(dev->mem_resource) /
434                 sizeof(dev->mem_resource[0]); i++) {
435                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
436                         dev->mem_resource[i].phys_addr,
437                         dev->mem_resource[i].len);
438         }
439         return 0;
440 }
441
442 /* dump devices on the bus */
443 void
444 rte_pci_dump(FILE *f)
445 {
446         struct rte_pci_device *dev = NULL;
447
448         FOREACH_DEVICE_ON_PCIBUS(dev) {
449                 pci_dump_one_device(f, dev);
450         }
451 }
452
453 /* register a driver */
454 void
455 rte_pci_register(struct rte_pci_driver *driver)
456 {
457         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
458         driver->bus = &rte_pci_bus;
459 }
460
461 /* unregister a driver */
462 void
463 rte_pci_unregister(struct rte_pci_driver *driver)
464 {
465         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
466         driver->bus = NULL;
467 }
468
469 /* Add a device to PCI bus */
470 void
471 rte_pci_add_device(struct rte_pci_device *pci_dev)
472 {
473         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
474 }
475
476 /* Insert a device into a predefined position in PCI bus */
477 void
478 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
479                       struct rte_pci_device *new_pci_dev)
480 {
481         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
482 }
483
484 /* Remove a device from PCI bus */
485 void
486 rte_pci_remove_device(struct rte_pci_device *pci_dev)
487 {
488         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
489 }
490
491 struct rte_pci_bus rte_pci_bus = {
492         .bus = {
493                 .scan = rte_pci_scan,
494                 .probe = rte_pci_probe,
495         },
496         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
497         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
498 };
499
500 RTE_REGISTER_BUS(PCI_BUS_NAME, rte_pci_bus.bus);