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>
54 /* /sys/bus/pci/devices/... directory name for this device. */
57 /* Resource file descriptors. */
60 /* File descriptor for config space read/write. */
63 /* File descriptor for /dev/uio%d */
66 /* Minor device for uio device. */
69 /* Index given by unix_file_add. */
74 /* Pool of PCI devices. */
76 vlib_main_t * vlib_main;
77 linux_pci_device_t * linux_pci_devices;
80 extern linux_pci_main_t linux_pci_main;
82 /* Call to allocate/initialize the pci subsystem.
83 This is not an init function so that users can explicitly enable
84 pci only when it's needed. */
85 clib_error_t * pci_bus_init (vlib_main_t * vm);
87 clib_error_t * vlib_pci_bind_to_uio (vlib_pci_device_t * d, char * uio_driver_name);
89 linux_pci_main_t linux_pci_main;
92 vlib_pci_bind_to_uio (vlib_pci_device_t * d, char * uio_driver_name)
94 clib_error_t * error = 0;
99 pci_config_header_t * c;
100 u8 * dev_dir_name = format(0, "/sys/bus/pci/devices/%U",
101 format_vlib_pci_addr, &d->bus_address);
103 c = &d->config0.header;
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);
127 while((e = readdir (dir)))
130 struct ethtool_drvinfo drvinfo;
132 if (e->d_name[0] == '.') /* skip . and .. */
135 memset(&ifr, 0, sizeof ifr);
136 memset(&drvinfo, 0, sizeof drvinfo);
137 ifr.ifr_data = (char *) &drvinfo;
138 strncpy(ifr.ifr_name, e->d_name, IFNAMSIZ);
139 drvinfo.cmd = ETHTOOL_GDRVINFO;
140 ioctl (fd, SIOCETHTOOL, &ifr);
142 if (strcmp ((char *) s, drvinfo.bus_info))
145 memset (&ifr, 0, sizeof(ifr));
146 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ);
147 ioctl (fd, SIOCGIFFLAGS, &ifr);
150 if (ifr.ifr_flags & IFF_UP)
152 error = clib_error_return (0, "Skipping PCI device %U as host "
153 "interface %s is up",
154 format_vlib_pci_addr, &d->bus_address,
161 vec_reset_length (s);
163 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
164 vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
165 vec_reset_length (s);
167 s = format (s, "/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0);
168 vlib_sysfs_write ((char *) s, "0x%04x 0x%04x", c->vendor_id, c->device_id);
169 vec_reset_length (s);
171 s = format (s, "/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0);
172 vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address);
177 vec_free (dev_dir_name);
182 static clib_error_t *
183 scan_uio_dir (void * arg, u8 * path_name, u8 * file_name)
185 linux_pci_device_t * l = arg;
186 unformat_input_t input;
188 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
190 if (! unformat (&input, "uio%d", &l->uio_minor))
193 unformat_free (&input);
197 static clib_error_t * linux_pci_uio_read_ready (unix_file_t * uf)
199 vlib_pci_main_t * pm = &pci_main;
200 vlib_pci_device_t * d;
201 int __attribute__ ((unused)) rv;
204 rv = read(uf->file_descriptor, &icount, 4);
206 d = pool_elt_at_index (pm->pci_devs, uf->private_data);
208 if (d->interrupt_handler)
209 d->interrupt_handler(d);
211 vlib_pci_intr_enable(d);
213 return /* no error */ 0;
216 static clib_error_t *linux_pci_uio_error_ready (unix_file_t *uf)
218 u32 error_index = (u32) uf->private_data;
220 return clib_error_return (0, "pci device %d: error", error_index);
223 static void add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev)
225 vlib_pci_main_t * pm = &pci_main;
226 linux_pci_main_t * lpm = &linux_pci_main;
227 linux_pci_device_t * l;
229 pool_get (lpm->linux_pci_devices, l);
232 l->dev_dir_name = vec_dup (l->dev_dir_name);
234 dev->os_handle = l - lpm->linux_pci_devices;
237 u8 * uio_dir = format (0, "%s/uio", l->dev_dir_name);
238 foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */ 1);
243 char * uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0);
244 l->uio_fd = open (uio_name, O_RDWR);
246 clib_unix_error ("open `%s'", uio_name);
251 unix_file_t template = {0};
252 unix_main_t * um = &unix_main;
254 template.read_function = linux_pci_uio_read_ready;
255 template.file_descriptor = l->uio_fd;
256 template.error_function = linux_pci_uio_error_ready;
257 template.private_data = dev - pm->pci_devs;
259 l->unix_file_index = unix_file_add (um, &template);
263 static void linux_pci_device_free (linux_pci_device_t * l)
266 for (i = 0; i < vec_len (l->resource_fds); i++)
267 if (l->resource_fds[i] > 0)
268 close (l->resource_fds[i]);
269 if (l->config_fd > 0)
270 close (l->config_fd);
273 vec_free (l->resource_fds);
274 vec_free (l->dev_dir_name);
277 /* Configuration space read/write. */
279 vlib_pci_read_write_config (vlib_pci_device_t * dev,
280 vlib_read_or_write_t read_or_write,
285 linux_pci_main_t * lpm = &linux_pci_main;
286 linux_pci_device_t * p;
289 p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle);
291 if (read_or_write == VLIB_READ)
292 n = pread (p->config_fd, data, n_bytes, address);
294 n = pwrite (p->config_fd, data, n_bytes, address);
297 return clib_error_return_unix (0, "%s",
298 read_or_write == VLIB_READ
304 static clib_error_t *
305 os_map_pci_resource_internal (uword os_handle,
310 linux_pci_main_t * pm = &linux_pci_main;
311 linux_pci_device_t * p;
312 struct stat stat_buf;
315 clib_error_t * error;
316 int flags = MAP_SHARED;
319 p = pool_elt_at_index (pm->linux_pci_devices, os_handle);
321 file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0);
322 fd = open ((char *) file_name, O_RDWR);
325 error = clib_error_return_unix (0, "open `%s'", file_name);
329 if (fstat (fd, &stat_buf) < 0)
331 error = clib_error_return_unix (0, "fstat `%s'", file_name);
335 vec_validate (p->resource_fds, resource);
336 p->resource_fds[resource] = fd;
340 *result = mmap (addr,
341 /* size */ stat_buf.st_size,
342 PROT_READ | PROT_WRITE,
346 if (*result == (void *) -1)
348 error = clib_error_return_unix (0, "mmap `%s'", file_name);
358 vec_free (file_name);
363 vlib_pci_map_resource (vlib_pci_device_t * dev,
367 return (os_map_pci_resource_internal (dev->os_handle, resource, 0 /* addr */,
372 vlib_pci_map_resource_fixed (vlib_pci_device_t * dev,
377 return (os_map_pci_resource_internal (dev->os_handle, resource, addr, result));
380 void vlib_pci_free_device (vlib_pci_device_t * dev)
382 linux_pci_main_t * pm = &linux_pci_main;
383 linux_pci_device_t * l;
385 l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle);
386 linux_pci_device_free (l);
387 pool_put (pm->linux_pci_devices, l);
390 pci_device_registration_t * __attribute__((unused))
391 pci_device_next_registered (pci_device_registration_t * r)
395 /* Null vendor id marks end of initialized list. */
396 for (i = 0; r->supported_devices[i].vendor_id != 0; i++)
399 return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0]));
402 static clib_error_t *
403 init_device_from_registered (vlib_main_t * vm,
404 vlib_pci_device_t * dev,
405 linux_pci_device_t * pdev)
407 vlib_pci_main_t * pm = &pci_main;
408 pci_device_registration_t * r;
410 pci_config_header_t * c;
411 clib_error_t * error;
413 c = &dev->config0.header;
415 r = pm->pci_device_registrations;
419 for (i = r->supported_devices; i->vendor_id != 0; i++)
420 if (i->vendor_id == c->vendor_id && i->device_id == c->device_id)
422 error = vlib_pci_bind_to_uio (dev, "uio_pci_generic");
425 clib_error_report (error);
429 add_device (dev, pdev);
430 dev->interrupt_handler = r->interrupt_handler;
431 return r->init_function (vm, dev);
433 r = r->next_registration;
435 /* No driver, close the PCI config-space FD */
436 close (pdev->config_fd);
440 static clib_error_t *
441 init_device (vlib_main_t * vm,
442 vlib_pci_device_t * dev,
443 linux_pci_device_t * pdev)
445 return init_device_from_registered (vm, dev, pdev);
448 static clib_error_t *
449 scan_device (void * arg, u8 * dev_dir_name, u8 * ignored)
451 vlib_main_t * vm = arg;
452 vlib_pci_main_t * pm = &pci_main;
455 clib_error_t * error = 0;
456 vlib_pci_device_t * dev;
457 linux_pci_device_t pdev = {0};
459 f = format (0, "%v/config%c", dev_dir_name, 0);
460 fd = open ((char *) f, O_RDWR);
462 /* Try read-only access if write fails. */
464 fd = open ((char *) f, O_RDONLY);
468 error = clib_error_return_unix (0, "open `%s'", f);
472 pool_get (pm->pci_devs, dev);
474 /* You can only read more that 64 bytes of config space as root; so we try to
475 read the full space but fall back to just the first 64 bytes. */
476 if (read (fd, &dev->config_data, sizeof (dev->config_data)) != sizeof (dev->config_data)
477 && read (fd, &dev->config0, sizeof (dev->config0)) != sizeof (dev->config0))
479 pool_put (pm->pci_devs, dev);
480 error = clib_error_return_unix (0, "read `%s'", f);
485 static pci_config_header_t all_ones;
486 if (all_ones.vendor_id == 0)
487 memset (&all_ones, ~0, sizeof (all_ones));
489 if (! memcmp (&dev->config0.header, &all_ones, sizeof (all_ones)))
491 pool_put (pm->pci_devs, dev);
492 error = clib_error_return (0, "invalid PCI config for `%s'", f);
497 if (dev->config0.header.header_type == 0)
498 pci_config_type0_little_to_host (&dev->config0);
500 pci_config_type1_little_to_host (&dev->config1);
502 /* Parse bus, dev, function from directory name. */
504 unformat_input_t input;
506 unformat_init_string (&input, (char *) dev_dir_name,
507 vec_len (dev_dir_name));
509 if (! unformat (&input, "/sys/bus/pci/devices/%U",
510 unformat_vlib_pci_addr, &dev->bus_address))
513 unformat_free (&input);
519 pdev.dev_dir_name = dev_dir_name;
521 hash_set(pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32,
524 error = init_device (vm, dev, &pdev);
527 f = format (f, "%v/vpd%c", dev_dir_name, 0);
528 fd = open ((char *) f, O_RDONLY);
537 if (read (fd, &tag, 3) != 3)
540 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
543 len = (tag[2] << 8) | tag[1];
544 vec_validate(data, len);
546 if (read (fd, data, len) != len)
552 dev->product_name = data;
553 else if (tag[0] == 0x90)
555 else if (tag[0] == 0x91)
564 f = format (f, "%v/driver%c", dev_dir_name, 0);
565 dev->driver_name = vlib_sysfs_link_to_name((char *) f);
569 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
570 vlib_sysfs_read ((char *) f, "%u", &dev->numa_node);
577 clib_error_t * linux_pci_init (vlib_main_t * vm)
579 vlib_pci_main_t * pm = &pci_main;
580 clib_error_t * error;
584 if ((error = vlib_call_init_function (vm, unix_input_init)))
587 ASSERT(sizeof(vlib_pci_addr_t) == sizeof(u32));
588 pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword));
590 error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, /* scan_dirs */ 0);
592 /* Complain and continue. might not be root, etc. */
594 clib_error_report (error);
599 VLIB_INIT_FUNCTION (linux_pci_init);