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>
44 #include <sys/types.h>
48 #include <sys/ioctl.h>
50 #include <linux/ethtool.h>
51 #include <linux/sockios.h>
55 /* /sys/bus/pci/devices/... directory name for this device. */
58 /* Resource file descriptors. */
61 /* File descriptor for config space read/write. */
64 /* File descriptor for /dev/uio%d */
67 /* Minor device for uio device. */
70 /* Index given by unix_file_add. */
75 /* Pool of PCI devices. */
78 vlib_main_t *vlib_main;
79 linux_pci_device_t *linux_pci_devices;
82 extern linux_pci_main_t linux_pci_main;
84 /* Call to allocate/initialize the pci subsystem.
85 This is not an init function so that users can explicitly enable
86 pci only when it's needed. */
87 clib_error_t *pci_bus_init (vlib_main_t * vm);
89 clib_error_t *vlib_pci_bind_to_uio (vlib_pci_device_t * d,
90 char *uio_driver_name);
92 linux_pci_main_t linux_pci_main;
95 vlib_pci_bind_to_uio (vlib_pci_device_t * d, char *uio_driver_name)
97 clib_error_t *error = 0;
102 u8 *dev_dir_name = format (0, "/sys/bus/pci/devices/%U",
103 format_vlib_pci_addr, &d->bus_address);
105 /* if uio sub-directory exists, we are fine, device is
106 already bound to UIO driver */
107 s = format (s, "%v/uio%c", dev_dir_name, 0);
108 if (access ((char *) s, F_OK) == 0)
110 vec_reset_length (s);
112 /* walk trough all linux interfaces and if interface belonging to
113 this device is founf check if interface is admin up */
114 dir = opendir ("/sys/class/net");
115 s = format (s, "%U%c", format_vlib_pci_addr, &d->bus_address, 0);
119 error = clib_error_return (0, "Skipping PCI device %U: failed to "
120 "read /sys/class/net",
121 format_vlib_pci_addr, &d->bus_address);
125 fd = socket (PF_INET, SOCK_DGRAM, 0);
128 error = clib_error_return_unix (0, "socket");
132 while ((e = readdir (dir)))
135 struct ethtool_drvinfo drvinfo;
137 if (e->d_name[0] == '.') /* skip . and .. */
140 memset (&ifr, 0, sizeof ifr);
141 memset (&drvinfo, 0, sizeof drvinfo);
142 ifr.ifr_data = (char *) &drvinfo;
143 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
144 drvinfo.cmd = ETHTOOL_GDRVINFO;
145 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
147 /* Some interfaces (eg "lo") don't support this ioctl */
148 if ((errno != ENOTSUP) && (errno != ENODEV))
149 clib_unix_warning ("ioctl fetch intf %s bus info error",
154 if (strcmp ((char *) s, drvinfo.bus_info))
157 memset (&ifr, 0, sizeof (ifr));
158 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
159 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
161 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
167 if (ifr.ifr_flags & IFF_UP)
169 error = clib_error_return (0, "Skipping PCI device %U as host "
170 "interface %s is up",
171 format_vlib_pci_addr, &d->bus_address,
179 vec_reset_length (s);
181 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
182 vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
183 vec_reset_length (s);
185 s = format (s, "/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0);
186 vlib_sysfs_write ((char *) s, "0x%04x 0x%04x", d->vendor_id, d->device_id);
187 vec_reset_length (s);
189 s = format (s, "/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0);
190 vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
195 vec_free (dev_dir_name);
200 static clib_error_t *
201 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
203 linux_pci_device_t *l = arg;
204 unformat_input_t input;
206 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
208 if (!unformat (&input, "uio%d", &l->uio_minor))
211 unformat_free (&input);
215 static clib_error_t *
216 linux_pci_uio_read_ready (unix_file_t * uf)
218 vlib_pci_main_t *pm = &pci_main;
219 vlib_pci_device_t *d;
220 int __attribute__ ((unused)) rv;
223 rv = read (uf->file_descriptor, &icount, 4);
225 d = pool_elt_at_index (pm->pci_devs, uf->private_data);
227 if (d->interrupt_handler)
228 d->interrupt_handler (d);
230 vlib_pci_intr_enable (d);
232 return /* no error */ 0;
235 static clib_error_t *
236 linux_pci_uio_error_ready (unix_file_t * uf)
238 u32 error_index = (u32) uf->private_data;
240 return clib_error_return (0, "pci device %d: error", error_index);
244 add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev)
246 vlib_pci_main_t *pm = &pci_main;
247 linux_pci_main_t *lpm = &linux_pci_main;
248 linux_pci_device_t *l;
250 pool_get (lpm->linux_pci_devices, l);
253 l->dev_dir_name = vec_dup (l->dev_dir_name);
255 dev->os_handle = l - lpm->linux_pci_devices;
258 u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name);
259 foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */
265 char *uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0);
266 l->uio_fd = open (uio_name, O_RDWR);
268 clib_unix_error ("open `%s'", uio_name);
273 unix_file_t template = { 0 };
274 unix_main_t *um = &unix_main;
276 template.read_function = linux_pci_uio_read_ready;
277 template.file_descriptor = l->uio_fd;
278 template.error_function = linux_pci_uio_error_ready;
279 template.private_data = dev - pm->pci_devs;
281 l->unix_file_index = unix_file_add (um, &template);
286 linux_pci_device_free (linux_pci_device_t * l)
289 for (i = 0; i < vec_len (l->resource_fds); i++)
290 if (l->resource_fds[i] > 0)
291 close (l->resource_fds[i]);
292 if (l->config_fd > 0)
293 close (l->config_fd);
296 vec_free (l->resource_fds);
297 vec_free (l->dev_dir_name);
300 /* Configuration space read/write. */
302 vlib_pci_read_write_config (vlib_pci_device_t * dev,
303 vlib_read_or_write_t read_or_write,
304 uword address, void *data, u32 n_bytes)
306 linux_pci_main_t *lpm = &linux_pci_main;
307 linux_pci_device_t *p;
310 p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle);
312 if (read_or_write == VLIB_READ)
313 n = pread (p->config_fd, data, n_bytes, address);
315 n = pwrite (p->config_fd, data, n_bytes, address);
318 return clib_error_return_unix (0, "%s",
319 read_or_write == VLIB_READ
325 static clib_error_t *
326 os_map_pci_resource_internal (uword os_handle,
327 u32 resource, u8 * addr, void **result)
329 linux_pci_main_t *pm = &linux_pci_main;
330 linux_pci_device_t *p;
331 struct stat stat_buf;
335 int flags = MAP_SHARED;
338 p = pool_elt_at_index (pm->linux_pci_devices, os_handle);
340 file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
341 fd = open ((char *) file_name, O_RDWR);
344 error = clib_error_return_unix (0, "open `%s'", file_name);
348 if (fstat (fd, &stat_buf) < 0)
350 error = clib_error_return_unix (0, "fstat `%s'", file_name);
354 vec_validate (p->resource_fds, resource);
355 p->resource_fds[resource] = fd;
359 *result = mmap (addr,
360 /* size */ stat_buf.st_size,
361 PROT_READ | PROT_WRITE, flags,
364 if (*result == (void *) -1)
366 error = clib_error_return_unix (0, "mmap `%s'", file_name);
376 vec_free (file_name);
381 vlib_pci_map_resource (vlib_pci_device_t * dev, u32 resource, void **result)
383 return (os_map_pci_resource_internal
384 (dev->os_handle, resource, 0 /* addr */ ,
389 vlib_pci_map_resource_fixed (vlib_pci_device_t * dev,
390 u32 resource, u8 * addr, void **result)
392 return (os_map_pci_resource_internal
393 (dev->os_handle, resource, addr, result));
397 vlib_pci_free_device (vlib_pci_device_t * dev)
399 linux_pci_main_t *pm = &linux_pci_main;
400 linux_pci_device_t *l;
402 l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle);
403 linux_pci_device_free (l);
404 pool_put (pm->linux_pci_devices, l);
407 pci_device_registration_t * __attribute__ ((unused))
408 pci_device_next_registered (pci_device_registration_t * r)
412 /* Null vendor id marks end of initialized list. */
413 for (i = 0; r->supported_devices[i].vendor_id != 0; i++)
416 return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0]));
419 static clib_error_t *
420 init_device_from_registered (vlib_main_t * vm,
421 vlib_pci_device_t * dev,
422 linux_pci_device_t * pdev)
424 vlib_pci_main_t *pm = &pci_main;
425 pci_device_registration_t *r;
429 r = pm->pci_device_registrations;
433 for (i = r->supported_devices; i->vendor_id != 0; i++)
434 if (i->vendor_id == dev->vendor_id && i->device_id == dev->device_id)
436 error = vlib_pci_bind_to_uio (dev, "uio_pci_generic");
439 clib_error_report (error);
443 add_device (dev, pdev);
444 dev->interrupt_handler = r->interrupt_handler;
445 return r->init_function (vm, dev);
447 r = r->next_registration;
449 /* No driver, close the PCI config-space FD */
450 close (pdev->config_fd);
454 static clib_error_t *
455 init_device (vlib_main_t * vm,
456 vlib_pci_device_t * dev, linux_pci_device_t * pdev)
458 return init_device_from_registered (vm, dev, pdev);
461 static clib_error_t *
462 scan_device (void *arg, u8 * dev_dir_name, u8 * ignored)
464 vlib_main_t *vm = arg;
465 vlib_pci_main_t *pm = &pci_main;
468 clib_error_t *error = 0;
469 vlib_pci_device_t *dev;
470 linux_pci_device_t pdev = { 0 };
473 f = format (0, "%v/config%c", dev_dir_name, 0);
474 fd = open ((char *) f, O_RDWR);
476 /* Try read-only access if write fails. */
478 fd = open ((char *) f, O_RDONLY);
482 error = clib_error_return_unix (0, "open `%s'", f);
486 pool_get (pm->pci_devs, dev);
488 /* You can only read more that 64 bytes of config space as root; so we try to
489 read the full space but fall back to just the first 64 bytes. */
490 if (read (fd, &dev->config_data, sizeof (dev->config_data)) !=
491 sizeof (dev->config_data)
492 && read (fd, &dev->config0,
493 sizeof (dev->config0)) != sizeof (dev->config0))
495 pool_put (pm->pci_devs, dev);
496 error = clib_error_return_unix (0, "read `%s'", f);
502 static pci_config_header_t all_ones;
503 if (all_ones.vendor_id == 0)
504 memset (&all_ones, ~0, sizeof (all_ones));
506 if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones)))
508 pool_put (pm->pci_devs, dev);
509 error = clib_error_return (0, "invalid PCI config for `%s'", f);
515 if (dev->config0.header.header_type == 0)
516 pci_config_type0_little_to_host (&dev->config0);
518 pci_config_type1_little_to_host (&dev->config1);
520 /* Parse bus, dev, function from directory name. */
522 unformat_input_t input;
524 unformat_init_string (&input, (char *) dev_dir_name,
525 vec_len (dev_dir_name));
527 if (!unformat (&input, "/sys/bus/pci/devices/%U",
528 unformat_vlib_pci_addr, &dev->bus_address))
531 unformat_free (&input);
537 pdev.dev_dir_name = dev_dir_name;
539 hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32,
542 error = init_device (vm, dev, &pdev);
544 vec_reset_length (f);
545 f = format (f, "%v/vpd%c", dev_dir_name, 0);
546 fd = open ((char *) f, O_RDONLY);
555 if (read (fd, &tag, 3) != 3)
558 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
561 len = (tag[2] << 8) | tag[1];
562 vec_validate (data, len);
564 if (read (fd, data, len) != len)
570 dev->product_name = data;
571 else if (tag[0] == 0x90)
573 else if (tag[0] == 0x91)
581 vec_reset_length (f);
582 f = format (f, "%v/driver%c", dev_dir_name, 0);
583 dev->driver_name = vlib_sysfs_link_to_name ((char *) f);
586 vec_reset_length (f);
587 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
588 vlib_sysfs_read ((char *) f, "%u", &dev->numa_node);
590 vec_reset_length (f);
591 f = format (f, "%v/class%c", dev_dir_name, 0);
592 vlib_sysfs_read ((char *) f, "0x%x", &tmp);
593 dev->device_class = tmp >> 8;
595 vec_reset_length (f);
596 f = format (f, "%v/vendor%c", dev_dir_name, 0);
597 vlib_sysfs_read ((char *) f, "0x%x", &tmp);
598 dev->vendor_id = tmp;
600 vec_reset_length (f);
601 f = format (f, "%v/device%c", dev_dir_name, 0);
602 vlib_sysfs_read ((char *) f, "0x%x", &tmp);
603 dev->device_id = tmp;
611 linux_pci_init (vlib_main_t * vm)
613 vlib_pci_main_t *pm = &pci_main;
618 if ((error = vlib_call_init_function (vm, unix_input_init)))
621 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
622 pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword));
624 error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm,
627 /* Complain and continue. might not be root, etc. */
629 clib_error_report (error);
634 VLIB_INIT_FUNCTION (linux_pci_init);
637 * fd.io coding-style-patch-verification: ON
640 * eval: (c-set-style "gnu")