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