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