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>
54 #include <linux/vfio.h>
55 #include <sys/eventfd.h>
57 static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
58 static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
62 vlib_pci_dev_handle_t handle;
65 /* Resource file descriptors. */
68 /* File descriptor for config space read/write. */
71 /* File descriptor for /dev/uio%d */
74 /* Minor device for uio device. */
80 /* Index given by clib_file_add. */
83 /* Interrupt handler */
84 void (*interrupt_handler) (vlib_pci_dev_handle_t h);
96 } linux_pci_vfio_iommu_group_t;
98 /* Pool of PCI devices. */
101 vlib_main_t *vlib_main;
102 linux_pci_device_t *linux_pci_devices;
105 int vfio_container_fd;
108 /* pool of IOMMU groups */
109 linux_pci_vfio_iommu_group_t *iommu_groups;
111 /* iommu group pool index by group id hash */
112 uword *iommu_pool_index_by_group;
116 extern linux_pci_main_t linux_pci_main;
119 linux_pci_get_device (vlib_pci_dev_handle_t h)
121 linux_pci_main_t *lpm = &linux_pci_main;
122 return pool_elt_at_index (lpm->linux_pci_devices, h);
126 vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
128 linux_pci_device_t *d = linux_pci_get_device (h);
129 return d->private_data;
133 vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
135 linux_pci_device_t *d = linux_pci_get_device (h);
136 d->private_data = private_data;
140 vlib_pci_get_addr (vlib_pci_dev_handle_t h)
142 linux_pci_device_t *lpm = linux_pci_get_device (h);
146 /* Call to allocate/initialize the pci subsystem.
147 This is not an init function so that users can explicitly enable
148 pci only when it's needed. */
149 clib_error_t *pci_bus_init (vlib_main_t * vm);
151 linux_pci_main_t linux_pci_main;
153 vlib_pci_device_info_t *
154 vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
156 linux_pci_main_t *lpm = &linux_pci_main;
158 vlib_pci_device_info_t *di;
163 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
164 memset (di, 0, sizeof (vlib_pci_device_info_t));
165 di->addr.as_u32 = addr->as_u32;
167 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
168 format_vlib_pci_addr, addr);
170 f = format (0, "%v/config%c", dev_dir_name, 0);
171 fd = open ((char *) f, O_RDWR);
173 /* Try read-only access if write fails. */
175 fd = open ((char *) f, O_RDONLY);
179 err = clib_error_return_unix (0, "open `%s'", f);
183 /* You can only read more that 64 bytes of config space as root; so we try to
184 read the full space but fall back to just the first 64 bytes. */
185 if (read (fd, &di->config_data, sizeof (di->config_data)) !=
186 sizeof (di->config_data)
187 && read (fd, &di->config0,
188 sizeof (di->config0)) != sizeof (di->config0))
190 err = clib_error_return_unix (0, "read `%s'", f);
196 static pci_config_header_t all_ones;
197 if (all_ones.vendor_id == 0)
198 memset (&all_ones, ~0, sizeof (all_ones));
200 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
202 err = clib_error_return (0, "invalid PCI config for `%s'", f);
208 if (di->config0.header.header_type == 0)
209 pci_config_type0_little_to_host (&di->config0);
211 pci_config_type1_little_to_host (&di->config1);
214 vec_reset_length (f);
215 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
216 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
220 clib_error_free (err);
223 vec_reset_length (f);
224 f = format (f, "%v/class%c", dev_dir_name, 0);
225 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
228 di->device_class = tmp >> 8;
230 vec_reset_length (f);
231 f = format (f, "%v/vendor%c", dev_dir_name, 0);
232 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
237 vec_reset_length (f);
238 f = format (f, "%v/device%c", dev_dir_name, 0);
239 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
244 vec_reset_length (f);
245 f = format (f, "%v/driver%c", dev_dir_name, 0);
246 di->driver_name = clib_sysfs_link_to_name ((char *) f);
248 di->iommu_group = -1;
249 if (lpm->vfio_container_fd != -1)
252 vec_reset_length (f);
253 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
254 tmpstr = clib_sysfs_link_to_name ((char *) f);
257 di->iommu_group = atoi ((char *) tmpstr);
264 vec_reset_length (f);
265 f = format (f, "%v/vpd%c", dev_dir_name, 0);
266 fd = open ((char *) f, O_RDONLY);
275 if (read (fd, &tag, 3) != 3)
278 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
281 len = (tag[2] << 8) | tag[1];
282 vec_validate (data, len);
284 if (read (fd, data, len) != len)
290 di->product_name = data;
291 else if (tag[0] == 0x90)
293 else if (tag[0] == 0x91)
304 vlib_pci_free_device_info (di);
309 vec_free (dev_dir_name);
313 clib_error_free (err);
318 vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
320 clib_error_t *error = 0;
321 u8 *s = 0, *driver_name = 0;
324 vlib_pci_device_info_t *di;
325 int fd, clear_driver_override = 0;
326 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
327 format_vlib_pci_addr, addr);
329 di = vlib_pci_get_device_info (addr, &error);
334 s = format (s, "%v/driver%c", dev_dir_name, 0);
335 driver_name = clib_sysfs_link_to_name ((char *) s);
336 vec_reset_length (s);
339 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
340 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
341 (strcmp ("igb_uio", (char *) driver_name) == 0)))
344 /* walk trough all linux interfaces and if interface belonging to
345 this device is founf check if interface is admin up */
346 dir = opendir ("/sys/class/net");
347 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
351 error = clib_error_return (0, "Skipping PCI device %U: failed to "
352 "read /sys/class/net",
353 format_vlib_pci_addr, addr);
357 fd = socket (PF_INET, SOCK_DGRAM, 0);
360 error = clib_error_return_unix (0, "socket");
364 while ((e = readdir (dir)))
367 struct ethtool_drvinfo drvinfo;
369 if (e->d_name[0] == '.') /* skip . and .. */
372 memset (&ifr, 0, sizeof ifr);
373 memset (&drvinfo, 0, sizeof drvinfo);
374 ifr.ifr_data = (char *) &drvinfo;
375 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
376 drvinfo.cmd = ETHTOOL_GDRVINFO;
377 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
379 /* Some interfaces (eg "lo") don't support this ioctl */
380 if ((errno != ENOTSUP) && (errno != ENODEV))
381 clib_unix_warning ("ioctl fetch intf %s bus info error",
386 if (strcmp ((char *) s, drvinfo.bus_info))
389 memset (&ifr, 0, sizeof (ifr));
390 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
391 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
393 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
399 if (ifr.ifr_flags & IFF_UP)
401 error = clib_error_return (0, "Skipping PCI device %U as host "
402 "interface %s is up",
403 format_vlib_pci_addr, addr, e->d_name);
410 vec_reset_length (s);
412 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
413 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
414 vec_reset_length (s);
416 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
417 if (access ((char *) s, F_OK) == 0)
419 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
420 clear_driver_override = 1;
424 vec_reset_length (s);
425 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
426 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
429 vec_reset_length (s);
431 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
432 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
433 vec_reset_length (s);
435 if (clear_driver_override)
437 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
438 clib_sysfs_write ((char *) s, "%c", 0);
439 vec_reset_length (s);
445 vec_free (dev_dir_name);
446 vec_free (driver_name);
451 static clib_error_t *
452 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
454 linux_pci_device_t *l = arg;
455 unformat_input_t input;
457 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
459 if (!unformat (&input, "uio%d", &l->uio_minor))
462 unformat_free (&input);
466 static clib_error_t *
467 linux_pci_uio_read_ready (clib_file_t * uf)
469 int __attribute__ ((unused)) rv;
470 vlib_pci_dev_handle_t h = uf->private_data;
471 linux_pci_device_t *p = linux_pci_get_device (h);
474 rv = read (uf->file_descriptor, &icount, 4);
476 if (p->interrupt_handler)
477 p->interrupt_handler (h);
479 vlib_pci_intr_enable (h);
481 return /* no error */ 0;
484 static clib_error_t *
485 linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
487 clib_error_t *err = 0;
488 struct vfio_irq_set i = {
489 .argsz = sizeof (struct vfio_irq_set),
492 .index = VFIO_PCI_INTX_IRQ_INDEX,
493 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
496 if (ioctl (d->vfio_device_fd, VFIO_DEVICE_SET_IRQS, &i) < 0)
498 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
499 format_vlib_pci_addr, &d->addr);
504 static clib_error_t *
505 linux_pci_uio_error_ready (clib_file_t * uf)
507 u32 error_index = (u32) uf->private_data;
509 return clib_error_return (0, "pci device %d: error", error_index);
512 static clib_error_t *
513 linux_pci_vfio_read_ready (clib_file_t * uf)
515 int __attribute__ ((unused)) rv;
516 vlib_pci_dev_handle_t h = uf->private_data;
517 linux_pci_device_t *p = linux_pci_get_device (h);
520 rv = read (uf->file_descriptor, &icount, sizeof (icount));
522 if (p->interrupt_handler)
523 p->interrupt_handler (h);
525 linux_pci_vfio_unmask_intx (p);
527 return /* no error */ 0;
530 static clib_error_t *
531 linux_pci_vfio_error_ready (clib_file_t * uf)
533 u32 error_index = (u32) uf->private_data;
535 return clib_error_return (0, "pci device %d: error", error_index);
538 static clib_error_t *
539 add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
540 vlib_pci_device_info_t * di, pci_device_registration_t * r)
542 clib_error_t *err = 0;
543 clib_file_t t = { 0 };
546 p->addr.as_u32 = di->addr.as_u32;
549 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
550 format_vlib_pci_addr, &di->addr, 0);
552 p->config_fd = open ((char *) s, O_RDWR);
553 vec_reset_length (s);
555 if (p->config_fd == -1)
557 err = clib_error_return_unix (0, "open '%s'", s);
561 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
562 format_vlib_pci_addr, &di->addr);
563 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
565 vec_reset_length (s);
567 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
568 p->uio_fd = open ((char *) s, O_RDWR);
571 err = clib_error_return_unix (0, "open '%s'", s);
575 t.read_function = linux_pci_uio_read_ready;
576 t.file_descriptor = p->uio_fd;
577 t.error_function = linux_pci_uio_error_ready;
578 t.private_data = p->handle;
580 p->clib_file_index = clib_file_add (&file_main, &t);
581 p->interrupt_handler = r->interrupt_handler;
582 err = r->init_function (vm, p->handle);
588 if (p->config_fd != -1)
589 close (p->config_fd);
596 static linux_pci_vfio_iommu_group_t *
597 get_vfio_iommu_group (int group)
599 linux_pci_main_t *lpm = &linux_pci_main;
602 p = hash_get (lpm->iommu_pool_index_by_group, group);
604 return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
607 static clib_error_t *
608 open_vfio_iommu_group (int group)
610 linux_pci_main_t *lpm = &linux_pci_main;
611 linux_pci_vfio_iommu_group_t *g;
612 clib_error_t *err = 0;
613 struct vfio_group_status group_status;
617 g = get_vfio_iommu_group (group);
623 s = format (s, "/dev/vfio/%u%c", group, 0);
624 fd = open ((char *) s, O_RDWR);
626 return clib_error_return_unix (0, "open '%s'", s);
628 group_status.argsz = sizeof (group_status);
629 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
631 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
636 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
638 err = clib_error_return (0, "iommu group %d is not viable (not all "
639 "devices in this group bound to vfio-pci)",
644 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
646 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
651 if (lpm->vfio_iommu_mode == 0)
653 if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
656 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
660 lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
664 pool_get (lpm->iommu_groups, g);
667 hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
675 static clib_error_t *
676 add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
677 vlib_pci_device_info_t * di, pci_device_registration_t * r)
679 linux_pci_vfio_iommu_group_t *g;
680 struct vfio_device_info device_info;
681 struct vfio_irq_info irq_info = { 0 };
682 clib_error_t *err = 0;
686 p->addr.as_u32 = di->addr.as_u32;
688 if (di->driver_name == 0 ||
689 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
690 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
691 "vfio-pci", format_vlib_pci_addr, &di->addr,
694 if ((err = open_vfio_iommu_group (di->iommu_group)))
697 g = get_vfio_iommu_group (di->iommu_group);
699 s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
700 if ((dfd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
702 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
703 format_vlib_pci_addr, &di->addr);
706 vec_reset_length (s);
707 p->vfio_device_fd = dfd;
709 device_info.argsz = sizeof (device_info);
710 if (ioctl (dfd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
712 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
713 format_vlib_pci_addr, &di->addr);
717 irq_info.argsz = sizeof (struct vfio_irq_info);
718 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
719 if (ioctl (dfd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
721 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
722 "'%U'", format_vlib_pci_addr, &di->addr);
726 /* reset if device supports it */
727 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
728 if (ioctl (dfd, VFIO_DEVICE_RESET) < 0)
730 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
731 format_vlib_pci_addr, &di->addr);
735 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) && irq_info.count == 1)
737 u8 buf[sizeof (struct vfio_irq_set) + sizeof (int)] = { 0 };
738 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
739 clib_file_t t = { 0 };
740 int efd = eventfd (0, EFD_NONBLOCK);
742 irq_set->argsz = sizeof (buf);
744 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
746 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
747 clib_memcpy (&irq_set->data, &efd, sizeof (int));
749 if (ioctl (dfd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
751 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
752 format_vlib_pci_addr, &di->addr);
756 t.read_function = linux_pci_vfio_read_ready;
757 t.file_descriptor = efd;
758 t.error_function = linux_pci_vfio_error_ready;
759 t.private_data = p->handle;
760 p->clib_file_index = clib_file_add (&file_main, &t);
762 /* unmask the interrupt */
763 linux_pci_vfio_unmask_intx (p);
766 p->interrupt_handler = r->interrupt_handler;
768 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
769 format_vlib_pci_addr, &di->addr, 0);
771 p->config_fd = open ((char *) s, O_RDWR);
772 vec_reset_length (s);
774 if (p->config_fd == -1)
776 err = clib_error_return_unix (0, "open '%s'", s);
780 err = r->init_function (vm, p->handle);
788 if (p->config_fd != -1)
789 close (p->config_fd);
794 /* Configuration space read/write. */
796 vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
797 vlib_read_or_write_t read_or_write,
798 uword address, void *data, u32 n_bytes)
800 linux_pci_device_t *p = linux_pci_get_device (h);
803 if (read_or_write == VLIB_READ)
804 n = pread (p->config_fd, data, n_bytes, address);
806 n = pwrite (p->config_fd, data, n_bytes, address);
809 return clib_error_return_unix (0, "%s",
810 read_or_write == VLIB_READ
816 static clib_error_t *
817 vlib_pci_map_resource_int (vlib_pci_dev_handle_t h,
818 u32 resource, u8 * addr, void **result)
820 linux_pci_device_t *p = linux_pci_get_device (h);
821 struct stat stat_buf;
825 int flags = MAP_SHARED;
829 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
830 format_vlib_pci_addr, &p->addr, resource, 0);
832 fd = open ((char *) file_name, O_RDWR);
835 error = clib_error_return_unix (0, "open `%s'", file_name);
839 if (fstat (fd, &stat_buf) < 0)
841 error = clib_error_return_unix (0, "fstat `%s'", file_name);
845 vec_validate (p->resource_fds, resource);
846 p->resource_fds[resource] = fd;
850 *result = mmap (addr,
851 /* size */ stat_buf.st_size,
852 PROT_READ | PROT_WRITE, flags,
855 if (*result == (void *) -1)
857 error = clib_error_return_unix (0, "mmap `%s'", file_name);
867 vec_free (file_name);
872 vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource, void **result)
874 return (vlib_pci_map_resource_int (h, resource, 0 /* addr */ , result));
878 vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
879 u32 resource, u8 * addr, void **result)
881 return (vlib_pci_map_resource_int (h, resource, addr, result));
885 init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
887 vlib_pci_main_t *pm = &pci_main;
888 linux_pci_main_t *lpm = &linux_pci_main;
889 pci_device_registration_t *r;
891 clib_error_t *err = 0;
892 linux_pci_device_t *p;
894 pool_get (lpm->linux_pci_devices, p);
895 p->handle = p - lpm->linux_pci_devices;
897 r = pm->pci_device_registrations;
901 for (i = r->supported_devices; i->vendor_id != 0; i++)
902 if (i->vendor_id == di->config0.header.vendor_id &&
903 i->device_id == di->config0.header.device_id)
905 if (di->iommu_group != -1)
906 err = add_device_vfio (vm, p, di, r);
908 err = add_device_uio (vm, p, di, r);
911 clib_error_report (err);
915 r = r->next_registration;
918 /* No driver, close the PCI config-space FD */
919 memset (p, 0, sizeof (linux_pci_device_t));
920 pool_put (lpm->linux_pci_devices, p);
923 static clib_error_t *
924 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
926 vlib_pci_addr_t addr, **addrv = arg;
927 unformat_input_t input;
928 clib_error_t *err = 0;
930 unformat_init_string (&input, (char *) dev_dir_name,
931 vec_len (dev_dir_name));
933 if (!unformat (&input, "/sys/bus/pci/devices/%U",
934 unformat_vlib_pci_addr, &addr))
935 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
937 unformat_free (&input);
942 vec_add1 (*addrv, addr);
947 pci_addr_cmp (void *v1, void *v2)
949 vlib_pci_addr_t *a1 = v1;
950 vlib_pci_addr_t *a2 = v2;
952 if (a1->domain > a2->domain)
954 if (a1->domain < a2->domain)
956 if (a1->bus > a2->bus)
958 if (a1->bus < a2->bus)
960 if (a1->slot > a2->slot)
962 if (a1->slot < a2->slot)
964 if (a1->function > a2->function)
966 if (a1->function < a2->function)
972 vlib_pci_get_all_dev_addrs ()
974 vlib_pci_addr_t *addrs = 0;
976 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
977 &addrs, /* scan_dirs */ 0);
984 vec_sort_with_function (addrs, pci_addr_cmp);
990 linux_pci_init (vlib_main_t * vm)
992 vlib_pci_main_t *pm = &pci_main;
993 linux_pci_main_t *lpm = &linux_pci_main;
994 vlib_pci_addr_t *addr = 0, *addrs;
1000 if ((error = vlib_call_init_function (vm, unix_input_init)))
1003 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1005 fd = open ("/dev/vfio/vfio", O_RDWR);
1007 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1013 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1019 lpm->vfio_container_fd = fd;
1020 lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1022 addrs = vlib_pci_get_all_dev_addrs ();
1024 vec_foreach (addr, addrs)
1026 vlib_pci_device_info_t *d;
1027 if ((d = vlib_pci_get_device_info (addr, 0)))
1029 init_device_from_registered (vm, d);
1030 vlib_pci_free_device_info (d);
1038 VLIB_INIT_FUNCTION (linux_pci_init);
1041 * fd.io coding-style-patch-verification: ON
1044 * eval: (c-set-style "gnu")