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