5ee100e673746ff1c7c8458d6427fd71affd2a78
[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_memcpy.h>
51 #include <rte_memzone.h>
52 #include <rte_eal.h>
53 #include <rte_string_fns.h>
54 #include <rte_common.h>
55 #include <rte_devargs.h>
56
57 #include "eal_private.h"
58
59 extern struct rte_pci_bus rte_pci_bus;
60
61 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
62
63 const char *pci_get_sysfs_path(void)
64 {
65         const char *path = NULL;
66
67         path = getenv("SYSFS_PCI_DEVICES");
68         if (path == NULL)
69                 return SYSFS_PCI_DEVICES;
70
71         return path;
72 }
73
74 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
75 {
76         struct rte_devargs *devargs;
77
78         TAILQ_FOREACH(devargs, &devargs_list, next) {
79                 if (devargs->type != RTE_DEVTYPE_BLACKLISTED_PCI &&
80                         devargs->type != RTE_DEVTYPE_WHITELISTED_PCI)
81                         continue;
82                 if (!rte_eal_compare_pci_addr(&dev->addr, &devargs->pci.addr))
83                         return devargs;
84         }
85         return NULL;
86 }
87
88 /* map a particular resource from a file */
89 void *
90 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
91                  int additional_flags)
92 {
93         void *mapaddr;
94
95         /* Map the PCI memory resource of device */
96         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
97                         MAP_SHARED | additional_flags, fd, offset);
98         if (mapaddr == MAP_FAILED) {
99                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
100                         __func__, fd, requested_addr,
101                         (unsigned long)size, (unsigned long)offset,
102                         strerror(errno), mapaddr);
103         } else
104                 RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
105
106         return mapaddr;
107 }
108
109 /* unmap a particular resource */
110 void
111 pci_unmap_resource(void *requested_addr, size_t size)
112 {
113         if (requested_addr == NULL)
114                 return;
115
116         /* Unmap the PCI memory resource of device */
117         if (munmap(requested_addr, size)) {
118                 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
119                         __func__, requested_addr, (unsigned long)size,
120                         strerror(errno));
121         } else
122                 RTE_LOG(DEBUG, EAL, "  PCI memory unmapped at %p\n",
123                                 requested_addr);
124 }
125
126 /*
127  * Match the PCI Driver and Device using the ID Table
128  *
129  * @param pci_drv
130  *      PCI driver from which ID table would be extracted
131  * @param pci_dev
132  *      PCI device to match against the driver
133  * @return
134  *      1 for successful match
135  *      0 for unsuccessful match
136  */
137 static int
138 rte_pci_match(const struct rte_pci_driver *pci_drv,
139               const struct rte_pci_device *pci_dev)
140 {
141         const struct rte_pci_id *id_table;
142
143         for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
144              id_table++) {
145                 /* check if device's identifiers match the driver's ones */
146                 if (id_table->vendor_id != pci_dev->id.vendor_id &&
147                                 id_table->vendor_id != PCI_ANY_ID)
148                         continue;
149                 if (id_table->device_id != pci_dev->id.device_id &&
150                                 id_table->device_id != PCI_ANY_ID)
151                         continue;
152                 if (id_table->subsystem_vendor_id !=
153                     pci_dev->id.subsystem_vendor_id &&
154                     id_table->subsystem_vendor_id != PCI_ANY_ID)
155                         continue;
156                 if (id_table->subsystem_device_id !=
157                     pci_dev->id.subsystem_device_id &&
158                     id_table->subsystem_device_id != PCI_ANY_ID)
159                         continue;
160                 if (id_table->class_id != pci_dev->id.class_id &&
161                                 id_table->class_id != RTE_CLASS_ANY_ID)
162                         continue;
163
164                 return 1;
165         }
166
167         return 0;
168 }
169
170 /*
171  * If vendor/device ID match, call the probe() function of the
172  * driver.
173  */
174 static int
175 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
176                          struct rte_pci_device *dev)
177 {
178         int ret;
179         struct rte_pci_addr *loc;
180
181         if ((dr == NULL) || (dev == NULL))
182                 return -EINVAL;
183
184         loc = &dev->addr;
185
186         /* The device is not blacklisted; Check if driver supports it */
187         if (!rte_pci_match(dr, dev))
188                 /* Match of device and driver failed */
189                 return 1;
190
191         RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
192                         loc->domain, loc->bus, loc->devid, loc->function,
193                         dev->device.numa_node);
194
195         /* no initialization when blacklisted, return without error */
196         if (dev->device.devargs != NULL &&
197                 dev->device.devargs->type ==
198                         RTE_DEVTYPE_BLACKLISTED_PCI) {
199                 RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
200                         " initializing\n");
201                 return 1;
202         }
203
204         RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
205                 dev->id.device_id, dr->driver.name);
206
207         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
208                 /* map resources for devices that use igb_uio */
209                 ret = rte_pci_map_device(dev);
210                 if (ret != 0)
211                         return ret;
212         }
213
214         /* reference driver structure */
215         dev->driver = dr;
216         dev->device.driver = &dr->driver;
217
218         /* call the driver probe() function */
219         ret = dr->probe(dr, dev);
220         if (ret) {
221                 dev->driver = NULL;
222                 if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
223                         /* Don't unmap if device is unsupported and
224                          * driver needs mapped resources.
225                          */
226                         !(ret > 0 &&
227                                 (dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
228                         rte_pci_unmap_device(dev);
229         }
230
231         return ret;
232 }
233
234 /*
235  * If vendor/device ID match, call the remove() function of the
236  * driver.
237  */
238 static int
239 rte_pci_detach_dev(struct rte_pci_device *dev)
240 {
241         struct rte_pci_addr *loc;
242         struct rte_pci_driver *dr;
243
244         if (dev == NULL)
245                 return -EINVAL;
246
247         dr = dev->driver;
248         loc = &dev->addr;
249
250         RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
251                         loc->domain, loc->bus, loc->devid,
252                         loc->function, dev->device.numa_node);
253
254         RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
255                         dev->id.device_id, dr->driver.name);
256
257         if (dr->remove && (dr->remove(dev) < 0))
258                 return -1;      /* negative value is an error */
259
260         /* clear driver structure */
261         dev->driver = NULL;
262
263         if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
264                 /* unmap resources for devices that use igb_uio */
265                 rte_pci_unmap_device(dev);
266
267         return 0;
268 }
269
270 /*
271  * If vendor/device ID match, call the probe() function of all
272  * registered driver for the given device. Return -1 if initialization
273  * failed, return 1 if no driver is found for this device.
274  */
275 static int
276 pci_probe_all_drivers(struct rte_pci_device *dev)
277 {
278         struct rte_pci_driver *dr = NULL;
279         int rc = 0;
280
281         if (dev == NULL)
282                 return -1;
283
284         /* Check if a driver is already loaded */
285         if (dev->driver != NULL)
286                 return 0;
287
288         FOREACH_DRIVER_ON_PCIBUS(dr) {
289                 rc = rte_pci_probe_one_driver(dr, dev);
290                 if (rc < 0)
291                         /* negative value is an error */
292                         return -1;
293                 if (rc > 0)
294                         /* positive value means driver doesn't support it */
295                         continue;
296                 return 0;
297         }
298         return 1;
299 }
300
301 /*
302  * Find the pci device specified by pci address, then invoke probe function of
303  * the driver of the devive.
304  */
305 int
306 rte_pci_probe_one(const struct rte_pci_addr *addr)
307 {
308         struct rte_pci_device *dev = NULL;
309
310         int ret = 0;
311
312         if (addr == NULL)
313                 return -1;
314
315         /* update current pci device in global list, kernel bindings might have
316          * changed since last time we looked at it.
317          */
318         if (pci_update_device(addr) < 0)
319                 goto err_return;
320
321         FOREACH_DEVICE_ON_PCIBUS(dev) {
322                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
323                         continue;
324
325                 ret = pci_probe_all_drivers(dev);
326                 if (ret)
327                         goto err_return;
328                 return 0;
329         }
330         return -1;
331
332 err_return:
333         RTE_LOG(WARNING, EAL,
334                 "Requested device " PCI_PRI_FMT " cannot be used\n",
335                 addr->domain, addr->bus, addr->devid, addr->function);
336         return -1;
337 }
338
339 /*
340  * Detach device specified by its pci address.
341  */
342 int
343 rte_pci_detach(const struct rte_pci_addr *addr)
344 {
345         struct rte_pci_device *dev = NULL;
346         int ret = 0;
347
348         if (addr == NULL)
349                 return -1;
350
351         FOREACH_DEVICE_ON_PCIBUS(dev) {
352                 if (rte_eal_compare_pci_addr(&dev->addr, addr))
353                         continue;
354
355                 ret = rte_pci_detach_dev(dev);
356                 if (ret < 0)
357                         /* negative value is an error */
358                         goto err_return;
359                 if (ret > 0)
360                         /* positive value means driver doesn't support it */
361                         continue;
362
363                 rte_pci_remove_device(dev);
364                 free(dev);
365                 return 0;
366         }
367         return -1;
368
369 err_return:
370         RTE_LOG(WARNING, EAL, "Requested device " PCI_PRI_FMT
371                         " cannot be used\n", dev->addr.domain, dev->addr.bus,
372                         dev->addr.devid, dev->addr.function);
373         return -1;
374 }
375
376 /*
377  * Scan the content of the PCI bus, and call the probe() function for
378  * all registered drivers that have a matching entry in its id_table
379  * for discovered devices.
380  */
381 int
382 rte_pci_probe(void)
383 {
384         struct rte_pci_device *dev = NULL;
385         size_t probed = 0, failed = 0;
386         struct rte_devargs *devargs;
387         int probe_all = 0;
388         int ret = 0;
389
390         if (rte_eal_devargs_type_count(RTE_DEVTYPE_WHITELISTED_PCI) == 0)
391                 probe_all = 1;
392
393         FOREACH_DEVICE_ON_PCIBUS(dev) {
394                 probed++;
395
396                 /* set devargs in PCI structure */
397                 devargs = pci_devargs_lookup(dev);
398                 if (devargs != NULL)
399                         dev->device.devargs = devargs;
400
401                 /* probe all or only whitelisted devices */
402                 if (probe_all)
403                         ret = pci_probe_all_drivers(dev);
404                 else if (devargs != NULL &&
405                         devargs->type == RTE_DEVTYPE_WHITELISTED_PCI)
406                         ret = pci_probe_all_drivers(dev);
407                 if (ret < 0) {
408                         RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
409                                  " cannot be used\n", dev->addr.domain, dev->addr.bus,
410                                  dev->addr.devid, dev->addr.function);
411                         rte_errno = errno;
412                         failed++;
413                         ret = 0;
414                 }
415         }
416
417         return (probed && probed == failed) ? -1 : 0;
418 }
419
420 /* dump one device */
421 static int
422 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
423 {
424         int i;
425
426         fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
427                dev->addr.devid, dev->addr.function);
428         fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
429                dev->id.device_id);
430
431         for (i = 0; i != sizeof(dev->mem_resource) /
432                 sizeof(dev->mem_resource[0]); i++) {
433                 fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
434                         dev->mem_resource[i].phys_addr,
435                         dev->mem_resource[i].len);
436         }
437         return 0;
438 }
439
440 /* dump devices on the bus */
441 void
442 rte_pci_dump(FILE *f)
443 {
444         struct rte_pci_device *dev = NULL;
445
446         FOREACH_DEVICE_ON_PCIBUS(dev) {
447                 pci_dump_one_device(f, dev);
448         }
449 }
450
451 /* register a driver */
452 void
453 rte_pci_register(struct rte_pci_driver *driver)
454 {
455         TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
456         driver->bus = &rte_pci_bus;
457 }
458
459 /* unregister a driver */
460 void
461 rte_pci_unregister(struct rte_pci_driver *driver)
462 {
463         TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
464         driver->bus = NULL;
465 }
466
467 /* Add a device to PCI bus */
468 void
469 rte_pci_add_device(struct rte_pci_device *pci_dev)
470 {
471         TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
472 }
473
474 /* Insert a device into a predefined position in PCI bus */
475 void
476 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
477                       struct rte_pci_device *new_pci_dev)
478 {
479         TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
480 }
481
482 /* Remove a device from PCI bus */
483 void
484 rte_pci_remove_device(struct rte_pci_device *pci_dev)
485 {
486         TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
487 }
488
489 static struct rte_device *
490 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
491                 const void *data)
492 {
493         struct rte_pci_device *dev;
494         bool start_found = !start;
495
496         FOREACH_DEVICE_ON_PCIBUS(dev) {
497                 if (!start_found) {
498                         if (&dev->device == start)
499                                 start_found = 1;
500                         continue;
501                 }
502                 if (cmp(&dev->device, data) == 0)
503                         return &dev->device;
504         }
505
506         return NULL;
507 }
508
509 static int
510 pci_plug(struct rte_device *dev, const char *devargs __rte_unused)
511 {
512         struct rte_pci_device *pdev;
513         struct rte_pci_addr *addr;
514
515         addr = &RTE_DEV_TO_PCI(dev)->addr;
516
517         /* Find the current device holding this address in the bus. */
518         FOREACH_DEVICE_ON_PCIBUS(pdev) {
519                 if (rte_eal_compare_pci_addr(&pdev->addr, addr) == 0)
520                         return rte_pci_probe_one(addr);
521         }
522
523         rte_errno = ENODEV;
524         return -1;
525 }
526
527 static int
528 pci_unplug(struct rte_device *dev)
529 {
530         struct rte_pci_device *pdev;
531
532         pdev = RTE_DEV_TO_PCI(dev);
533         if (rte_pci_detach(&pdev->addr) != 0) {
534                 rte_errno = ENODEV;
535                 return -1;
536         }
537         return 0;
538 }
539
540 struct rte_pci_bus rte_pci_bus = {
541         .bus = {
542                 .scan = rte_pci_scan,
543                 .probe = rte_pci_probe,
544                 .find_device = pci_find_device,
545                 .plug = pci_plug,
546                 .unplug = pci_unplug,
547         },
548         .device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
549         .driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
550 };
551
552 RTE_REGISTER_BUS(PCI_BUS_NAME, rte_pci_bus.bus);