2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 * pci.c: Linux user space PCI bus management.
18 * Copyright (c) 2008 Eliot Dresselhaus
20 * Permission is hereby granted, free of charge, to any person obtaining
21 * a copy of this software and associated documentation files (the
22 * "Software"), to deal in the Software without restriction, including
23 * without limitation the rights to use, copy, modify, merge, publish,
24 * distribute, sublicense, and/or sell copies of the Software, and to
25 * permit persons to whom the Software is furnished to do so, subject to
26 * the following conditions:
28 * The above copyright notice and this permission notice shall be
29 * included in all copies or substantial portions of the Software.
31 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 #include <vppinfra/linux/sysfs.h>
42 #include <vlib/vlib.h>
43 #include <vlib/pci/pci.h>
44 #include <vlib/unix/unix.h>
46 #include <sys/types.h>
50 #include <sys/ioctl.h>
52 #include <linux/ethtool.h>
53 #include <linux/sockios.h>
57 /* /sys/bus/pci/devices/... directory name for this device. */
60 /* Resource file descriptors. */
63 /* File descriptor for config space read/write. */
66 /* File descriptor for /dev/uio%d */
69 /* Minor device for uio device. */
72 /* Index given by clib_file_add. */
77 /* Pool of PCI devices. */
80 vlib_main_t *vlib_main;
81 linux_pci_device_t *linux_pci_devices;
84 extern linux_pci_main_t linux_pci_main;
86 /* Call to allocate/initialize the pci subsystem.
87 This is not an init function so that users can explicitly enable
88 pci only when it's needed. */
89 clib_error_t *pci_bus_init (vlib_main_t * vm);
91 clib_error_t *vlib_pci_bind_to_uio (vlib_pci_device_t * d,
92 char *uio_driver_name);
94 linux_pci_main_t linux_pci_main;
97 vlib_pci_bind_to_uio (vlib_pci_device_t * d, char *uio_driver_name)
99 clib_error_t *error = 0;
100 u8 *s = 0, *driver_name = 0;
103 int fd, clear_driver_override = 0;
104 u8 *dev_dir_name = format (0, "/sys/bus/pci/devices/%U",
105 format_vlib_pci_addr, &d->bus_address);
107 s = format (s, "%v/driver%c", dev_dir_name, 0);
108 driver_name = clib_sysfs_link_to_name ((char *) s);
109 vec_reset_length (s);
112 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
113 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
114 (strcmp ("igb_uio", (char *) driver_name) == 0)))
117 /* walk trough all linux interfaces and if interface belonging to
118 this device is founf check if interface is admin up */
119 dir = opendir ("/sys/class/net");
120 s = format (s, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
124 error = clib_error_return (0, "Skipping PCI device %U: failed to "
125 "read /sys/class/net",
126 format_vlib_pci_addr, &d->bus_address);
130 fd = socket (PF_INET, SOCK_DGRAM, 0);
133 error = clib_error_return_unix (0, "socket");
137 while ((e = readdir (dir)))
140 struct ethtool_drvinfo drvinfo;
142 if (e->d_name[0] == '.') /* skip . and .. */
145 memset (&ifr, 0, sizeof ifr);
146 memset (&drvinfo, 0, sizeof drvinfo);
147 ifr.ifr_data = (char *) &drvinfo;
148 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
149 drvinfo.cmd = ETHTOOL_GDRVINFO;
150 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
152 /* Some interfaces (eg "lo") don't support this ioctl */
153 if ((errno != ENOTSUP) && (errno != ENODEV))
154 clib_unix_warning ("ioctl fetch intf %s bus info error",
159 if (strcmp ((char *) s, drvinfo.bus_info))
162 memset (&ifr, 0, sizeof (ifr));
163 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
164 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
166 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
172 if (ifr.ifr_flags & IFF_UP)
174 error = clib_error_return (0, "Skipping PCI device %U as host "
175 "interface %s is up",
176 format_vlib_pci_addr, &d->bus_address,
184 vec_reset_length (s);
186 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
187 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
188 vec_reset_length (s);
190 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
191 if (access ((char *) s, F_OK) == 0)
193 clib_sysfs_write ((char *) s, "%s", uio_driver_name);
194 clear_driver_override = 1;
198 vec_reset_length (s);
199 s = format (s, "/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0);
200 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", d->vendor_id,
203 vec_reset_length (s);
205 s = format (s, "/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0);
206 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
207 vec_reset_length (s);
209 if (clear_driver_override)
211 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
212 clib_sysfs_write ((char *) s, "%c", 0);
213 vec_reset_length (s);
219 vec_free (dev_dir_name);
220 vec_free (driver_name);
225 static clib_error_t *
226 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
228 linux_pci_device_t *l = arg;
229 unformat_input_t input;
231 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
233 if (!unformat (&input, "uio%d", &l->uio_minor))
236 unformat_free (&input);
240 static clib_error_t *
241 linux_pci_uio_read_ready (clib_file_t * uf)
243 vlib_pci_main_t *pm = &pci_main;
244 vlib_pci_device_t *d;
245 int __attribute__ ((unused)) rv;
248 rv = read (uf->file_descriptor, &icount, 4);
250 d = pool_elt_at_index (pm->pci_devs, uf->private_data);
252 if (d->interrupt_handler)
253 d->interrupt_handler (d);
255 vlib_pci_intr_enable (d);
257 return /* no error */ 0;
260 static clib_error_t *
261 linux_pci_uio_error_ready (clib_file_t * uf)
263 u32 error_index = (u32) uf->private_data;
265 return clib_error_return (0, "pci device %d: error", error_index);
269 add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev)
271 vlib_pci_main_t *pm = &pci_main;
272 linux_pci_main_t *lpm = &linux_pci_main;
273 linux_pci_device_t *l;
275 pool_get (lpm->linux_pci_devices, l);
278 l->dev_dir_name = vec_dup (l->dev_dir_name);
280 dev->os_handle = l - lpm->linux_pci_devices;
283 u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name);
284 foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */
290 char *uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0);
291 l->uio_fd = open (uio_name, O_RDWR);
293 clib_unix_error ("open `%s'", uio_name);
298 clib_file_t template = { 0 };
300 template.read_function = linux_pci_uio_read_ready;
301 template.file_descriptor = l->uio_fd;
302 template.error_function = linux_pci_uio_error_ready;
303 template.private_data = dev - pm->pci_devs;
305 l->clib_file_index = clib_file_add (&file_main, &template);
310 linux_pci_device_free (linux_pci_device_t * l)
313 for (i = 0; i < vec_len (l->resource_fds); i++)
314 if (l->resource_fds[i] > 0)
315 close (l->resource_fds[i]);
316 if (l->config_fd > 0)
317 close (l->config_fd);
320 vec_free (l->resource_fds);
321 vec_free (l->dev_dir_name);
324 /* Configuration space read/write. */
326 vlib_pci_read_write_config (vlib_pci_device_t * dev,
327 vlib_read_or_write_t read_or_write,
328 uword address, void *data, u32 n_bytes)
330 linux_pci_main_t *lpm = &linux_pci_main;
331 linux_pci_device_t *p;
334 p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle);
336 if (read_or_write == VLIB_READ)
337 n = pread (p->config_fd, data, n_bytes, address);
339 n = pwrite (p->config_fd, data, n_bytes, address);
342 return clib_error_return_unix (0, "%s",
343 read_or_write == VLIB_READ
349 static clib_error_t *
350 os_map_pci_resource_internal (uword os_handle,
351 u32 resource, u8 * addr, void **result)
353 linux_pci_main_t *pm = &linux_pci_main;
354 linux_pci_device_t *p;
355 struct stat stat_buf;
359 int flags = MAP_SHARED;
362 p = pool_elt_at_index (pm->linux_pci_devices, os_handle);
364 file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
365 fd = open ((char *) file_name, O_RDWR);
368 error = clib_error_return_unix (0, "open `%s'", file_name);
372 if (fstat (fd, &stat_buf) < 0)
374 error = clib_error_return_unix (0, "fstat `%s'", file_name);
378 vec_validate (p->resource_fds, resource);
379 p->resource_fds[resource] = fd;
383 *result = mmap (addr,
384 /* size */ stat_buf.st_size,
385 PROT_READ | PROT_WRITE, flags,
388 if (*result == (void *) -1)
390 error = clib_error_return_unix (0, "mmap `%s'", file_name);
400 vec_free (file_name);
405 vlib_pci_map_resource (vlib_pci_device_t * dev, u32 resource, void **result)
407 return (os_map_pci_resource_internal
408 (dev->os_handle, resource, 0 /* addr */ ,
413 vlib_pci_map_resource_fixed (vlib_pci_device_t * dev,
414 u32 resource, u8 * addr, void **result)
416 return (os_map_pci_resource_internal
417 (dev->os_handle, resource, addr, result));
421 vlib_pci_free_device (vlib_pci_device_t * dev)
423 linux_pci_main_t *pm = &linux_pci_main;
424 linux_pci_device_t *l;
426 l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle);
427 linux_pci_device_free (l);
428 pool_put (pm->linux_pci_devices, l);
431 pci_device_registration_t * __attribute__ ((unused))
432 pci_device_next_registered (pci_device_registration_t * r)
436 /* Null vendor id marks end of initialized list. */
437 for (i = 0; r->supported_devices[i].vendor_id != 0; i++)
440 return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0]));
443 static clib_error_t *
444 init_device_from_registered (vlib_main_t * vm,
445 vlib_pci_device_t * dev,
446 linux_pci_device_t * pdev)
448 vlib_pci_main_t *pm = &pci_main;
449 pci_device_registration_t *r;
453 r = pm->pci_device_registrations;
457 for (i = r->supported_devices; i->vendor_id != 0; i++)
458 if (i->vendor_id == dev->vendor_id && i->device_id == dev->device_id)
460 error = vlib_pci_bind_to_uio (dev, "uio_pci_generic");
463 clib_error_report (error);
467 add_device (dev, pdev);
468 dev->interrupt_handler = r->interrupt_handler;
469 return r->init_function (vm, dev);
471 r = r->next_registration;
473 /* No driver, close the PCI config-space FD */
474 close (pdev->config_fd);
478 static clib_error_t *
479 init_device (vlib_main_t * vm,
480 vlib_pci_device_t * dev, linux_pci_device_t * pdev)
482 return init_device_from_registered (vm, dev, pdev);
485 static clib_error_t *
486 scan_device (void *arg, u8 * dev_dir_name, u8 * ignored)
488 vlib_main_t *vm = arg;
489 vlib_pci_main_t *pm = &pci_main;
492 clib_error_t *error = 0;
493 vlib_pci_device_t *dev;
494 linux_pci_device_t pdev = { 0 };
497 f = format (0, "%v/config%c", dev_dir_name, 0);
498 fd = open ((char *) f, O_RDWR);
500 /* Try read-only access if write fails. */
502 fd = open ((char *) f, O_RDONLY);
506 error = clib_error_return_unix (0, "open `%s'", f);
510 pool_get (pm->pci_devs, dev);
512 /* You can only read more that 64 bytes of config space as root; so we try to
513 read the full space but fall back to just the first 64 bytes. */
514 if (read (fd, &dev->config_data, sizeof (dev->config_data)) !=
515 sizeof (dev->config_data)
516 && read (fd, &dev->config0,
517 sizeof (dev->config0)) != sizeof (dev->config0))
519 pool_put (pm->pci_devs, dev);
520 error = clib_error_return_unix (0, "read `%s'", f);
526 static pci_config_header_t all_ones;
527 if (all_ones.vendor_id == 0)
528 memset (&all_ones, ~0, sizeof (all_ones));
530 if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones)))
532 pool_put (pm->pci_devs, dev);
533 error = clib_error_return (0, "invalid PCI config for `%s'", f);
539 if (dev->config0.header.header_type == 0)
540 pci_config_type0_little_to_host (&dev->config0);
542 pci_config_type1_little_to_host (&dev->config1);
544 /* Parse bus, dev, function from directory name. */
546 unformat_input_t input;
548 unformat_init_string (&input, (char *) dev_dir_name,
549 vec_len (dev_dir_name));
551 if (!unformat (&input, "/sys/bus/pci/devices/%U",
552 unformat_vlib_pci_addr, &dev->bus_address))
555 unformat_free (&input);
561 pdev.dev_dir_name = dev_dir_name;
563 hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32,
566 vec_reset_length (f);
567 f = format (f, "%v/vpd%c", dev_dir_name, 0);
568 fd = open ((char *) f, O_RDONLY);
577 if (read (fd, &tag, 3) != 3)
580 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
583 len = (tag[2] << 8) | tag[1];
584 vec_validate (data, len);
586 if (read (fd, data, len) != len)
592 dev->product_name = data;
593 else if (tag[0] == 0x90)
595 else if (tag[0] == 0x91)
604 vec_reset_length (f);
605 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
606 clib_sysfs_read ((char *) f, "%u", &dev->numa_node);
608 vec_reset_length (f);
609 f = format (f, "%v/class%c", dev_dir_name, 0);
610 clib_sysfs_read ((char *) f, "0x%x", &tmp);
611 dev->device_class = tmp >> 8;
613 vec_reset_length (f);
614 f = format (f, "%v/vendor%c", dev_dir_name, 0);
615 clib_sysfs_read ((char *) f, "0x%x", &tmp);
616 dev->vendor_id = tmp;
618 vec_reset_length (f);
619 f = format (f, "%v/device%c", dev_dir_name, 0);
620 clib_sysfs_read ((char *) f, "0x%x", &tmp);
621 dev->device_id = tmp;
623 error = init_device (vm, dev, &pdev);
625 vec_reset_length (f);
626 f = format (f, "%v/driver%c", dev_dir_name, 0);
627 dev->driver_name = clib_sysfs_link_to_name ((char *) f);
635 linux_pci_init (vlib_main_t * vm)
637 vlib_pci_main_t *pm = &pci_main;
642 if ((error = vlib_call_init_function (vm, unix_input_init)))
645 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
646 pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword));
648 error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm,
651 /* Complain and continue. might not be root, etc. */
653 clib_error_report (error);
658 VLIB_INIT_FUNCTION (linux_pci_init);
661 * fd.io coding-style-patch-verification: ON
664 * eval: (c-set-style "gnu")