pci: move uio mapping in a dedicated file
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_pci.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <dirent.h>
36 #include <sys/mman.h>
37
38 #include <rte_log.h>
39 #include <rte_pci.h>
40 #include <rte_tailq.h>
41 #include <rte_eal_memconfig.h>
42 #include <rte_malloc.h>
43 #include <rte_devargs.h>
44
45 #include "rte_pci_dev_ids.h"
46 #include "eal_filesystem.h"
47 #include "eal_private.h"
48 #include "eal_pci_init.h"
49
50 /**
51  * @file
52  * PCI probing under linux
53  *
54  * This code is used to simulate a PCI probe by parsing information in sysfs.
55  * When a registered device matches a driver, it is then initialized with
56  * IGB_UIO driver (or doesn't initialize, if the device wasn't bound to it).
57  */
58
59 struct mapped_pci_res_list *pci_res_list = NULL;
60
61 /* unbind kernel driver for this device */
62 static int
63 pci_unbind_kernel_driver(struct rte_pci_device *dev)
64 {
65         int n;
66         FILE *f;
67         char filename[PATH_MAX];
68         char buf[BUFSIZ];
69         struct rte_pci_addr *loc = &dev->addr;
70
71         /* open /sys/bus/pci/devices/AAAA:BB:CC.D/driver */
72         rte_snprintf(filename, sizeof(filename),
73                  SYSFS_PCI_DEVICES "/" PCI_PRI_FMT "/driver/unbind",
74                  loc->domain, loc->bus, loc->devid, loc->function);
75
76         f = fopen(filename, "w");
77         if (f == NULL) /* device was not bound */
78                 return 0;
79
80         n = rte_snprintf(buf, sizeof(buf), PCI_PRI_FMT "\n",
81                      loc->domain, loc->bus, loc->devid, loc->function);
82         if ((n < 0) || (n >= (int)sizeof(buf))) {
83                 RTE_LOG(ERR, EAL, "%s(): rte_snprintf failed\n", __func__);
84                 goto error;
85         }
86         if (fwrite(buf, n, 1, f) == 0) {
87                 RTE_LOG(ERR, EAL, "%s(): could not write to %s\n", __func__,
88                                 filename);
89                 goto error;
90         }
91
92         fclose(f);
93         return 0;
94
95 error:
96         fclose(f);
97         return -1;
98 }
99
100 /* map a particular resource from a file */
101 void *
102 pci_map_resource(void * requested_addr, int fd, off_t offset, size_t size)
103 {
104         void *mapaddr;
105
106         /* Map the PCI memory resource of device */
107         mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
108                         MAP_SHARED, fd, offset);
109         if (mapaddr == MAP_FAILED ||
110                         (requested_addr != NULL && mapaddr != requested_addr)) {
111                 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
112                         __func__, fd, requested_addr,
113                         (unsigned long)size, (unsigned long)offset,
114                         strerror(errno), mapaddr);
115                 goto fail;
116         }
117
118         RTE_LOG(DEBUG, EAL, "  PCI memory mapped at %p\n", mapaddr);
119
120         return mapaddr;
121
122 fail:
123         return NULL;
124 }
125
126 /* parse the "resource" sysfs file */
127 #define IORESOURCE_MEM  0x00000200
128
129 static int
130 pci_parse_sysfs_resource(const char *filename, struct rte_pci_device *dev)
131 {
132         FILE *f;
133         char buf[BUFSIZ];
134         union pci_resource_info {
135                 struct {
136                         char *phys_addr;
137                         char *end_addr;
138                         char *flags;
139                 };
140                 char *ptrs[PCI_RESOURCE_FMT_NVAL];
141         } res_info;
142         int i;
143         uint64_t phys_addr, end_addr, flags;
144
145         f = fopen(filename, "r");
146         if (f == NULL) {
147                 RTE_LOG(ERR, EAL, "Cannot open sysfs resource\n");
148                 return -1;
149         }
150
151         for (i = 0; i<PCI_MAX_RESOURCE; i++) {
152
153                 if (fgets(buf, sizeof(buf), f) == NULL) {
154                         RTE_LOG(ERR, EAL,
155                                 "%s(): cannot read resource\n", __func__);
156                         goto error;
157                 }
158
159                 if (rte_strsplit(buf, sizeof(buf), res_info.ptrs, 3, ' ') != 3) {
160                         RTE_LOG(ERR, EAL,
161                                 "%s(): bad resource format\n", __func__);
162                         goto error;
163                 }
164                 errno = 0;
165                 phys_addr = strtoull(res_info.phys_addr, NULL, 16);
166                 end_addr = strtoull(res_info.end_addr, NULL, 16);
167                 flags = strtoull(res_info.flags, NULL, 16);
168                 if (errno != 0) {
169                         RTE_LOG(ERR, EAL,
170                                 "%s(): bad resource format\n", __func__);
171                         goto error;
172                 }
173
174                 if (flags & IORESOURCE_MEM) {
175                         dev->mem_resource[i].phys_addr = phys_addr;
176                         dev->mem_resource[i].len = end_addr - phys_addr + 1;
177                         /* not mapped for now */
178                         dev->mem_resource[i].addr = NULL;
179                 }
180         }
181         fclose(f);
182         return 0;
183
184 error:
185         fclose(f);
186         return -1;
187 }
188
189 /* Compare two PCI device addresses. */
190 static int
191 pci_addr_comparison(struct rte_pci_addr *addr, struct rte_pci_addr *addr2)
192 {
193         uint64_t dev_addr = (addr->domain << 24) + (addr->bus << 16) + (addr->devid << 8) + addr->function;
194         uint64_t dev_addr2 = (addr2->domain << 24) + (addr2->bus << 16) + (addr2->devid << 8) + addr2->function;
195
196         if (dev_addr > dev_addr2)
197                 return 1;
198         else
199                 return 0;
200 }
201
202
203 /* Scan one pci sysfs entry, and fill the devices list from it. */
204 static int
205 pci_scan_one(const char *dirname, uint16_t domain, uint8_t bus,
206              uint8_t devid, uint8_t function)
207 {
208         char filename[PATH_MAX];
209         unsigned long tmp;
210         struct rte_pci_device *dev;
211
212         dev = malloc(sizeof(*dev));
213         if (dev == NULL) {
214                 return -1;
215         }
216
217         memset(dev, 0, sizeof(*dev));
218         dev->addr.domain = domain;
219         dev->addr.bus = bus;
220         dev->addr.devid = devid;
221         dev->addr.function = function;
222
223         /* get vendor id */
224         rte_snprintf(filename, sizeof(filename), "%s/vendor", dirname);
225         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
226                 free(dev);
227                 return -1;
228         }
229         dev->id.vendor_id = (uint16_t)tmp;
230
231         /* get device id */
232         rte_snprintf(filename, sizeof(filename), "%s/device", dirname);
233         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
234                 free(dev);
235                 return -1;
236         }
237         dev->id.device_id = (uint16_t)tmp;
238
239         /* get subsystem_vendor id */
240         rte_snprintf(filename, sizeof(filename), "%s/subsystem_vendor",
241                  dirname);
242         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
243                 free(dev);
244                 return -1;
245         }
246         dev->id.subsystem_vendor_id = (uint16_t)tmp;
247
248         /* get subsystem_device id */
249         rte_snprintf(filename, sizeof(filename), "%s/subsystem_device",
250                  dirname);
251         if (eal_parse_sysfs_value(filename, &tmp) < 0) {
252                 free(dev);
253                 return -1;
254         }
255         dev->id.subsystem_device_id = (uint16_t)tmp;
256
257         /* get max_vfs */
258         dev->max_vfs = 0;
259         rte_snprintf(filename, sizeof(filename), "%s/max_vfs", dirname);
260         if (!access(filename, F_OK) &&
261             eal_parse_sysfs_value(filename, &tmp) == 0) {
262                 dev->max_vfs = (uint16_t)tmp;
263         }
264
265         /* get numa node */
266         rte_snprintf(filename, sizeof(filename), "%s/numa_node",
267                  dirname);
268         if (access(filename, R_OK) != 0) {
269                 /* if no NUMA support just set node to 0 */
270                 dev->numa_node = -1;
271         } else {
272                 if (eal_parse_sysfs_value(filename, &tmp) < 0) {
273                         free(dev);
274                         return -1;
275                 }
276                 dev->numa_node = tmp;
277         }
278
279         /* parse resources */
280         rte_snprintf(filename, sizeof(filename), "%s/resource", dirname);
281         if (pci_parse_sysfs_resource(filename, dev) < 0) {
282                 RTE_LOG(ERR, EAL, "%s(): cannot parse resource\n", __func__);
283                 free(dev);
284                 return -1;
285         }
286
287         /* device is valid, add in list (sorted) */
288         if (TAILQ_EMPTY(&pci_device_list)) {
289                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
290         }
291         else {
292                 struct rte_pci_device *dev2 = NULL;
293
294                 TAILQ_FOREACH(dev2, &pci_device_list, next) {
295                         if (pci_addr_comparison(&dev->addr, &dev2->addr))
296                                 continue;
297                         else {
298                                 TAILQ_INSERT_BEFORE(dev2, dev, next);
299                                 return 0;
300                         }
301                 }
302                 TAILQ_INSERT_TAIL(&pci_device_list, dev, next);
303         }
304
305         return 0;
306 }
307
308 /*
309  * split up a pci address into its constituent parts.
310  */
311 static int
312 parse_pci_addr_format(const char *buf, int bufsize, uint16_t *domain,
313                 uint8_t *bus, uint8_t *devid, uint8_t *function)
314 {
315         /* first split on ':' */
316         union splitaddr {
317                 struct {
318                         char *domain;
319                         char *bus;
320                         char *devid;
321                         char *function;
322                 };
323                 char *str[PCI_FMT_NVAL]; /* last element-separator is "." not ":" */
324         } splitaddr;
325
326         char *buf_copy = strndup(buf, bufsize);
327         if (buf_copy == NULL)
328                 return -1;
329
330         if (rte_strsplit(buf_copy, bufsize, splitaddr.str, PCI_FMT_NVAL, ':')
331                         != PCI_FMT_NVAL - 1)
332                 goto error;
333         /* final split is on '.' between devid and function */
334         splitaddr.function = strchr(splitaddr.devid,'.');
335         if (splitaddr.function == NULL)
336                 goto error;
337         *splitaddr.function++ = '\0';
338
339         /* now convert to int values */
340         errno = 0;
341         *domain = (uint16_t)strtoul(splitaddr.domain, NULL, 16);
342         *bus = (uint8_t)strtoul(splitaddr.bus, NULL, 16);
343         *devid = (uint8_t)strtoul(splitaddr.devid, NULL, 16);
344         *function = (uint8_t)strtoul(splitaddr.function, NULL, 10);
345         if (errno != 0)
346                 goto error;
347
348         free(buf_copy); /* free the copy made with strdup */
349         return 0;
350 error:
351         free(buf_copy);
352         return -1;
353 }
354
355 /*
356  * Scan the content of the PCI bus, and the devices in the devices
357  * list
358  */
359 static int
360 pci_scan(void)
361 {
362         struct dirent *e;
363         DIR *dir;
364         char dirname[PATH_MAX];
365         uint16_t domain;
366         uint8_t bus, devid, function;
367
368         dir = opendir(SYSFS_PCI_DEVICES);
369         if (dir == NULL) {
370                 RTE_LOG(ERR, EAL, "%s(): opendir failed: %s\n",
371                         __func__, strerror(errno));
372                 return -1;
373         }
374
375         while ((e = readdir(dir)) != NULL) {
376                 if (e->d_name[0] == '.')
377                         continue;
378
379                 if (parse_pci_addr_format(e->d_name, sizeof(e->d_name), &domain,
380                                 &bus, &devid, &function) != 0)
381                         continue;
382
383                 rte_snprintf(dirname, sizeof(dirname), "%s/%s", SYSFS_PCI_DEVICES,
384                          e->d_name);
385                 if (pci_scan_one(dirname, domain, bus, devid, function) < 0)
386                         goto error;
387         }
388         closedir(dir);
389         return 0;
390
391 error:
392         closedir(dir);
393         return -1;
394 }
395
396 /*
397  * If vendor/device ID match, call the devinit() function of the
398  * driver.
399  */
400 int
401 rte_eal_pci_probe_one_driver(struct rte_pci_driver *dr, struct rte_pci_device *dev)
402 {
403         struct rte_pci_id *id_table;
404
405         for (id_table = dr->id_table ; id_table->vendor_id != 0; id_table++) {
406
407                 /* check if device's identifiers match the driver's ones */
408                 if (id_table->vendor_id != dev->id.vendor_id &&
409                                 id_table->vendor_id != PCI_ANY_ID)
410                         continue;
411                 if (id_table->device_id != dev->id.device_id &&
412                                 id_table->device_id != PCI_ANY_ID)
413                         continue;
414                 if (id_table->subsystem_vendor_id != dev->id.subsystem_vendor_id &&
415                                 id_table->subsystem_vendor_id != PCI_ANY_ID)
416                         continue;
417                 if (id_table->subsystem_device_id != dev->id.subsystem_device_id &&
418                                 id_table->subsystem_device_id != PCI_ANY_ID)
419                         continue;
420
421                 struct rte_pci_addr *loc = &dev->addr;
422
423                 RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
424                                 loc->domain, loc->bus, loc->devid, loc->function,
425                                 dev->numa_node);
426
427                 RTE_LOG(DEBUG, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
428                                 dev->id.device_id, dr->name);
429
430                 /* no initialization when blacklisted, return without error */
431                 if (dev->devargs != NULL &&
432                         dev->devargs->type == RTE_DEVTYPE_BLACKLISTED_PCI) {
433                         RTE_LOG(DEBUG, EAL, "  Device is blacklisted, not initializing\n");
434                         return 0;
435                 }
436
437                 if (dr->drv_flags & RTE_PCI_DRV_NEED_IGB_UIO) {
438                         /* map resources for devices that use igb_uio */
439                         if (pci_uio_map_resource(dev) < 0)
440                                 return -1;
441                 } else if (dr->drv_flags & RTE_PCI_DRV_FORCE_UNBIND &&
442                            rte_eal_process_type() == RTE_PROC_PRIMARY) {
443                         /* unbind current driver */
444                         if (pci_unbind_kernel_driver(dev) < 0)
445                                 return -1;
446                 }
447
448                 /* reference driver structure */
449                 dev->driver = dr;
450
451                 /* call the driver devinit() function */
452                 return dr->devinit(dr, dev);
453         }
454         /* return positive value if driver is not found */
455         return 1;
456 }
457
458 /* Init the PCI EAL subsystem */
459 int
460 rte_eal_pci_init(void)
461 {
462         TAILQ_INIT(&pci_driver_list);
463         TAILQ_INIT(&pci_device_list);
464         pci_res_list = RTE_TAILQ_RESERVE_BY_IDX(RTE_TAILQ_PCI,
465                         mapped_pci_res_list);
466
467         /* for debug purposes, PCI can be disabled */
468         if (internal_config.no_pci)
469                 return 0;
470
471         if (pci_scan() < 0) {
472                 RTE_LOG(ERR, EAL, "%s(): Cannot scan PCI bus\n", __func__);
473                 return -1;
474         }
475         return 0;
476 }