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 <vlib/vlib.h>
41 #include <vlib/pci/pci.h>
42 #include <vlib/unix/unix.h>
43 #include <vlib/linux/sysfs.h>
45 #include <sys/types.h>
49 #include <sys/ioctl.h>
51 #include <linux/ethtool.h>
52 #include <linux/sockios.h>
56 /* /sys/bus/pci/devices/... directory name for this device. */
59 /* Resource file descriptors. */
62 /* File descriptor for config space read/write. */
65 /* File descriptor for /dev/uio%d */
68 /* Minor device for uio device. */
71 /* Index given by clib_file_add. */
76 /* Pool of PCI devices. */
79 vlib_main_t *vlib_main;
80 linux_pci_device_t *linux_pci_devices;
83 extern linux_pci_main_t linux_pci_main;
85 /* Call to allocate/initialize the pci subsystem.
86 This is not an init function so that users can explicitly enable
87 pci only when it's needed. */
88 clib_error_t *pci_bus_init (vlib_main_t * vm);
90 clib_error_t *vlib_pci_bind_to_uio (vlib_pci_device_t * d,
91 char *uio_driver_name);
93 linux_pci_main_t linux_pci_main;
96 vlib_pci_bind_to_uio (vlib_pci_device_t * d, char *uio_driver_name)
98 clib_error_t *error = 0;
99 u8 *s = 0, *driver_name = 0;
102 int fd, clear_driver_override = 0;
103 u8 *dev_dir_name = format (0, "/sys/bus/pci/devices/%U",
104 format_vlib_pci_addr, &d->bus_address);
106 s = format (s, "%v/driver%c", dev_dir_name, 0);
107 driver_name = vlib_sysfs_link_to_name ((char *) s);
108 vec_reset_length (s);
111 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
112 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
113 (strcmp ("igb_uio", (char *) driver_name) == 0)))
116 /* walk trough all linux interfaces and if interface belonging to
117 this device is founf check if interface is admin up */
118 dir = opendir ("/sys/class/net");
119 s = format (s, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
123 error = clib_error_return (0, "Skipping PCI device %U: failed to "
124 "read /sys/class/net",
125 format_vlib_pci_addr, &d->bus_address);
129 fd = socket (PF_INET, SOCK_DGRAM, 0);
132 error = clib_error_return_unix (0, "socket");
136 while ((e = readdir (dir)))
139 struct ethtool_drvinfo drvinfo;
141 if (e->d_name[0] == '.') /* skip . and .. */
144 memset (&ifr, 0, sizeof ifr);
145 memset (&drvinfo, 0, sizeof drvinfo);
146 ifr.ifr_data = (char *) &drvinfo;
147 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
148 drvinfo.cmd = ETHTOOL_GDRVINFO;
149 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
151 /* Some interfaces (eg "lo") don't support this ioctl */
152 if ((errno != ENOTSUP) && (errno != ENODEV))
153 clib_unix_warning ("ioctl fetch intf %s bus info error",
158 if (strcmp ((char *) s, drvinfo.bus_info))
161 memset (&ifr, 0, sizeof (ifr));
162 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
163 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
165 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
171 if (ifr.ifr_flags & IFF_UP)
173 error = clib_error_return (0, "Skipping PCI device %U as host "
174 "interface %s is up",
175 format_vlib_pci_addr, &d->bus_address,
183 vec_reset_length (s);
185 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
186 vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
187 vec_reset_length (s);
189 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
190 if (access ((char *) s, F_OK) == 0)
192 vlib_sysfs_write ((char *) s, "%s", uio_driver_name);
193 clear_driver_override = 1;
197 vec_reset_length (s);
198 s = format (s, "/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0);
199 vlib_sysfs_write ((char *) s, "0x%04x 0x%04x", d->vendor_id,
202 vec_reset_length (s);
204 s = format (s, "/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0);
205 vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
206 vec_reset_length (s);
208 if (clear_driver_override)
210 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
211 vlib_sysfs_write ((char *) s, "%c", 0);
212 vec_reset_length (s);
218 vec_free (dev_dir_name);
219 vec_free (driver_name);
224 static clib_error_t *
225 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
227 linux_pci_device_t *l = arg;
228 unformat_input_t input;
230 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
232 if (!unformat (&input, "uio%d", &l->uio_minor))
235 unformat_free (&input);
239 static clib_error_t *
240 linux_pci_uio_read_ready (clib_file_t * uf)
242 vlib_pci_main_t *pm = &pci_main;
243 vlib_pci_device_t *d;
244 int __attribute__ ((unused)) rv;
247 rv = read (uf->file_descriptor, &icount, 4);
249 d = pool_elt_at_index (pm->pci_devs, uf->private_data);
251 if (d->interrupt_handler)
252 d->interrupt_handler (d);
254 vlib_pci_intr_enable (d);
256 return /* no error */ 0;
259 static clib_error_t *
260 linux_pci_uio_error_ready (clib_file_t * uf)
262 u32 error_index = (u32) uf->private_data;
264 return clib_error_return (0, "pci device %d: error", error_index);
268 add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev)
270 vlib_pci_main_t *pm = &pci_main;
271 linux_pci_main_t *lpm = &linux_pci_main;
272 linux_pci_device_t *l;
274 pool_get (lpm->linux_pci_devices, l);
277 l->dev_dir_name = vec_dup (l->dev_dir_name);
279 dev->os_handle = l - lpm->linux_pci_devices;
282 u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name);
283 foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */
289 char *uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0);
290 l->uio_fd = open (uio_name, O_RDWR);
292 clib_unix_error ("open `%s'", uio_name);
297 clib_file_t template = { 0 };
299 template.read_function = linux_pci_uio_read_ready;
300 template.file_descriptor = l->uio_fd;
301 template.error_function = linux_pci_uio_error_ready;
302 template.private_data = dev - pm->pci_devs;
304 l->clib_file_index = clib_file_add (&file_main, &template);
309 linux_pci_device_free (linux_pci_device_t * l)
312 for (i = 0; i < vec_len (l->resource_fds); i++)
313 if (l->resource_fds[i] > 0)
314 close (l->resource_fds[i]);
315 if (l->config_fd > 0)
316 close (l->config_fd);
319 vec_free (l->resource_fds);
320 vec_free (l->dev_dir_name);
323 /* Configuration space read/write. */
325 vlib_pci_read_write_config (vlib_pci_device_t * dev,
326 vlib_read_or_write_t read_or_write,
327 uword address, void *data, u32 n_bytes)
329 linux_pci_main_t *lpm = &linux_pci_main;
330 linux_pci_device_t *p;
333 p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle);
335 if (read_or_write == VLIB_READ)
336 n = pread (p->config_fd, data, n_bytes, address);
338 n = pwrite (p->config_fd, data, n_bytes, address);
341 return clib_error_return_unix (0, "%s",
342 read_or_write == VLIB_READ
348 static clib_error_t *
349 os_map_pci_resource_internal (uword os_handle,
350 u32 resource, u8 * addr, void **result)
352 linux_pci_main_t *pm = &linux_pci_main;
353 linux_pci_device_t *p;
354 struct stat stat_buf;
358 int flags = MAP_SHARED;
361 p = pool_elt_at_index (pm->linux_pci_devices, os_handle);
363 file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
364 fd = open ((char *) file_name, O_RDWR);
367 error = clib_error_return_unix (0, "open `%s'", file_name);
371 if (fstat (fd, &stat_buf) < 0)
373 error = clib_error_return_unix (0, "fstat `%s'", file_name);
377 vec_validate (p->resource_fds, resource);
378 p->resource_fds[resource] = fd;
382 *result = mmap (addr,
383 /* size */ stat_buf.st_size,
384 PROT_READ | PROT_WRITE, flags,
387 if (*result == (void *) -1)
389 error = clib_error_return_unix (0, "mmap `%s'", file_name);
399 vec_free (file_name);
404 vlib_pci_map_resource (vlib_pci_device_t * dev, u32 resource, void **result)
406 return (os_map_pci_resource_internal
407 (dev->os_handle, resource, 0 /* addr */ ,
412 vlib_pci_map_resource_fixed (vlib_pci_device_t * dev,
413 u32 resource, u8 * addr, void **result)
415 return (os_map_pci_resource_internal
416 (dev->os_handle, resource, addr, result));
420 vlib_pci_free_device (vlib_pci_device_t * dev)
422 linux_pci_main_t *pm = &linux_pci_main;
423 linux_pci_device_t *l;
425 l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle);
426 linux_pci_device_free (l);
427 pool_put (pm->linux_pci_devices, l);
430 pci_device_registration_t * __attribute__ ((unused))
431 pci_device_next_registered (pci_device_registration_t * r)
435 /* Null vendor id marks end of initialized list. */
436 for (i = 0; r->supported_devices[i].vendor_id != 0; i++)
439 return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0]));
442 static clib_error_t *
443 init_device_from_registered (vlib_main_t * vm,
444 vlib_pci_device_t * dev,
445 linux_pci_device_t * pdev)
447 vlib_pci_main_t *pm = &pci_main;
448 pci_device_registration_t *r;
452 r = pm->pci_device_registrations;
456 for (i = r->supported_devices; i->vendor_id != 0; i++)
457 if (i->vendor_id == dev->vendor_id && i->device_id == dev->device_id)
459 error = vlib_pci_bind_to_uio (dev, "uio_pci_generic");
462 clib_error_report (error);
466 add_device (dev, pdev);
467 dev->interrupt_handler = r->interrupt_handler;
468 return r->init_function (vm, dev);
470 r = r->next_registration;
472 /* No driver, close the PCI config-space FD */
473 close (pdev->config_fd);
477 static clib_error_t *
478 init_device (vlib_main_t * vm,
479 vlib_pci_device_t * dev, linux_pci_device_t * pdev)
481 return init_device_from_registered (vm, dev, pdev);
484 static clib_error_t *
485 scan_device (void *arg, u8 * dev_dir_name, u8 * ignored)
487 vlib_main_t *vm = arg;
488 vlib_pci_main_t *pm = &pci_main;
491 clib_error_t *error = 0;
492 vlib_pci_device_t *dev;
493 linux_pci_device_t pdev = { 0 };
496 f = format (0, "%v/config%c", dev_dir_name, 0);
497 fd = open ((char *) f, O_RDWR);
499 /* Try read-only access if write fails. */
501 fd = open ((char *) f, O_RDONLY);
505 error = clib_error_return_unix (0, "open `%s'", f);
509 pool_get (pm->pci_devs, dev);
511 /* You can only read more that 64 bytes of config space as root; so we try to
512 read the full space but fall back to just the first 64 bytes. */
513 if (read (fd, &dev->config_data, sizeof (dev->config_data)) !=
514 sizeof (dev->config_data)
515 && read (fd, &dev->config0,
516 sizeof (dev->config0)) != sizeof (dev->config0))
518 pool_put (pm->pci_devs, dev);
519 error = clib_error_return_unix (0, "read `%s'", f);
525 static pci_config_header_t all_ones;
526 if (all_ones.vendor_id == 0)
527 memset (&all_ones, ~0, sizeof (all_ones));
529 if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones)))
531 pool_put (pm->pci_devs, dev);
532 error = clib_error_return (0, "invalid PCI config for `%s'", f);
538 if (dev->config0.header.header_type == 0)
539 pci_config_type0_little_to_host (&dev->config0);
541 pci_config_type1_little_to_host (&dev->config1);
543 /* Parse bus, dev, function from directory name. */
545 unformat_input_t input;
547 unformat_init_string (&input, (char *) dev_dir_name,
548 vec_len (dev_dir_name));
550 if (!unformat (&input, "/sys/bus/pci/devices/%U",
551 unformat_vlib_pci_addr, &dev->bus_address))
554 unformat_free (&input);
560 pdev.dev_dir_name = dev_dir_name;
562 hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32,
565 vec_reset_length (f);
566 f = format (f, "%v/vpd%c", dev_dir_name, 0);
567 fd = open ((char *) f, O_RDONLY);
576 if (read (fd, &tag, 3) != 3)
579 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
582 len = (tag[2] << 8) | tag[1];
583 vec_validate (data, len);
585 if (read (fd, data, len) != len)
591 dev->product_name = data;
592 else if (tag[0] == 0x90)
594 else if (tag[0] == 0x91)
603 vec_reset_length (f);
604 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
605 vlib_sysfs_read ((char *) f, "%u", &dev->numa_node);
607 vec_reset_length (f);
608 f = format (f, "%v/class%c", dev_dir_name, 0);
609 vlib_sysfs_read ((char *) f, "0x%x", &tmp);
610 dev->device_class = tmp >> 8;
612 vec_reset_length (f);
613 f = format (f, "%v/vendor%c", dev_dir_name, 0);
614 vlib_sysfs_read ((char *) f, "0x%x", &tmp);
615 dev->vendor_id = tmp;
617 vec_reset_length (f);
618 f = format (f, "%v/device%c", dev_dir_name, 0);
619 vlib_sysfs_read ((char *) f, "0x%x", &tmp);
620 dev->device_id = tmp;
622 error = init_device (vm, dev, &pdev);
624 vec_reset_length (f);
625 f = format (f, "%v/driver%c", dev_dir_name, 0);
626 dev->driver_name = vlib_sysfs_link_to_name ((char *) f);
634 linux_pci_init (vlib_main_t * vm)
636 vlib_pci_main_t *pm = &pci_main;
641 if ((error = vlib_call_init_function (vm, unix_input_init)))
644 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
645 pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword));
647 error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm,
650 /* Complain and continue. might not be root, etc. */
652 clib_error_report (error);
657 VLIB_INIT_FUNCTION (linux_pci_init);
660 * fd.io coding-style-patch-verification: ON
663 * eval: (c-set-style "gnu")