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";
59 static char *sysfs_mod_vfio_noiommu =
60 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
64 vlib_pci_dev_handle_t handle;
67 /* Resource file descriptors. */
70 /* File descriptor for config space read/write. */
73 /* File descriptor for /dev/uio%d */
76 /* Minor device for uio device. */
82 /* Index given by clib_file_add. */
85 /* Interrupt handler */
86 void (*interrupt_handler) (vlib_pci_dev_handle_t h);
98 } linux_pci_vfio_iommu_group_t;
100 /* Pool of PCI devices. */
103 vlib_main_t *vlib_main;
104 linux_pci_device_t *linux_pci_devices;
107 int vfio_container_fd;
110 /* pool of IOMMU groups */
111 linux_pci_vfio_iommu_group_t *iommu_groups;
113 /* iommu group pool index by group id hash */
114 uword *iommu_pool_index_by_group;
118 extern linux_pci_main_t linux_pci_main;
121 linux_pci_get_device (vlib_pci_dev_handle_t h)
123 linux_pci_main_t *lpm = &linux_pci_main;
124 return pool_elt_at_index (lpm->linux_pci_devices, h);
128 vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
130 linux_pci_device_t *d = linux_pci_get_device (h);
131 return d->private_data;
135 vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
137 linux_pci_device_t *d = linux_pci_get_device (h);
138 d->private_data = private_data;
142 vlib_pci_get_addr (vlib_pci_dev_handle_t h)
144 linux_pci_device_t *lpm = linux_pci_get_device (h);
148 /* Call to allocate/initialize the pci subsystem.
149 This is not an init function so that users can explicitly enable
150 pci only when it's needed. */
151 clib_error_t *pci_bus_init (vlib_main_t * vm);
153 linux_pci_main_t linux_pci_main;
155 vlib_pci_device_info_t *
156 vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
158 linux_pci_main_t *lpm = &linux_pci_main;
160 vlib_pci_device_info_t *di;
165 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
166 memset (di, 0, sizeof (vlib_pci_device_info_t));
167 di->addr.as_u32 = addr->as_u32;
169 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
170 format_vlib_pci_addr, addr);
172 f = format (0, "%v/config%c", dev_dir_name, 0);
173 fd = open ((char *) f, O_RDWR);
175 /* Try read-only access if write fails. */
177 fd = open ((char *) f, O_RDONLY);
181 err = clib_error_return_unix (0, "open `%s'", f);
185 /* You can only read more that 64 bytes of config space as root; so we try to
186 read the full space but fall back to just the first 64 bytes. */
187 if (read (fd, &di->config_data, sizeof (di->config_data)) !=
188 sizeof (di->config_data)
189 && read (fd, &di->config0,
190 sizeof (di->config0)) != sizeof (di->config0))
192 err = clib_error_return_unix (0, "read `%s'", f);
198 static pci_config_header_t all_ones;
199 if (all_ones.vendor_id == 0)
200 memset (&all_ones, ~0, sizeof (all_ones));
202 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
204 err = clib_error_return (0, "invalid PCI config for `%s'", f);
210 if (di->config0.header.header_type == 0)
211 pci_config_type0_little_to_host (&di->config0);
213 pci_config_type1_little_to_host (&di->config1);
216 vec_reset_length (f);
217 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
218 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
222 clib_error_free (err);
225 vec_reset_length (f);
226 f = format (f, "%v/class%c", dev_dir_name, 0);
227 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
230 di->device_class = tmp >> 8;
232 vec_reset_length (f);
233 f = format (f, "%v/vendor%c", dev_dir_name, 0);
234 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
239 vec_reset_length (f);
240 f = format (f, "%v/device%c", dev_dir_name, 0);
241 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
246 vec_reset_length (f);
247 f = format (f, "%v/driver%c", dev_dir_name, 0);
248 di->driver_name = clib_sysfs_link_to_name ((char *) f);
250 di->iommu_group = -1;
251 if (lpm->vfio_container_fd != -1)
254 vec_reset_length (f);
255 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
256 tmpstr = clib_sysfs_link_to_name ((char *) f);
259 di->iommu_group = atoi ((char *) tmpstr);
266 vec_reset_length (f);
267 f = format (f, "%v/vpd%c", dev_dir_name, 0);
268 fd = open ((char *) f, O_RDONLY);
277 if (read (fd, &tag, 3) != 3)
280 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
283 len = (tag[2] << 8) | tag[1];
284 vec_validate (data, len);
286 if (read (fd, data, len) != len)
292 di->product_name = data;
293 else if (tag[0] == 0x90)
295 else if (tag[0] == 0x91)
306 vlib_pci_free_device_info (di);
311 vec_free (dev_dir_name);
315 clib_error_free (err);
320 directory_exists (char *path)
322 struct stat s = { 0 };
323 if (stat (path, &s) == -1)
326 return S_ISDIR (s.st_mode);
330 vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
332 clib_error_t *error = 0;
333 u8 *s = 0, *driver_name = 0;
336 vlib_pci_device_info_t *di;
337 int fd, clear_driver_override = 0;
338 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
339 format_vlib_pci_addr, addr);
341 di = vlib_pci_get_device_info (addr, &error);
346 if (strncmp ("auto", uio_drv_name, 5) == 0)
348 int vfio_pci_loaded = 0;
350 if (directory_exists ("/sys/module/vfio_pci"))
353 if (di->iommu_group != -1)
355 /* device is bound to IOMMU group */
356 if (!vfio_pci_loaded)
358 error = clib_error_return (0, "Skipping PCI device %U: device "
359 "is bound to IOMMU group and "
360 "vfio-pci driver is not loaded",
361 format_vlib_pci_addr, addr);
365 uio_drv_name = "vfio-pci";
369 /* device is not bound to IOMMU group so we have multiple options */
370 if (vfio_pci_loaded &&
371 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
372 uio_drv_name = "vfio-pci";
373 else if (directory_exists ("/sys/module/uio_pci_generic"))
374 uio_drv_name = "uio_pci_generic";
375 else if (directory_exists ("/sys/module/igb_uio"))
376 uio_drv_name = "igb_uio";
379 clib_error_free (error);
380 error = clib_error_return (0, "Skipping PCI device %U: missing "
381 "kernel VFIO or UIO driver",
382 format_vlib_pci_addr, addr);
385 clib_error_free (error);
389 s = format (s, "%v/driver%c", dev_dir_name, 0);
390 driver_name = clib_sysfs_link_to_name ((char *) s);
391 vec_reset_length (s);
394 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
395 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
396 (strcmp ("igb_uio", (char *) driver_name) == 0)))
399 /* walk trough all linux interfaces and if interface belonging to
400 this device is founf check if interface is admin up */
401 dir = opendir ("/sys/class/net");
402 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
406 error = clib_error_return (0, "Skipping PCI device %U: failed to "
407 "read /sys/class/net",
408 format_vlib_pci_addr, addr);
412 fd = socket (PF_INET, SOCK_DGRAM, 0);
415 error = clib_error_return_unix (0, "socket");
419 while ((e = readdir (dir)))
422 struct ethtool_drvinfo drvinfo;
424 if (e->d_name[0] == '.') /* skip . and .. */
427 memset (&ifr, 0, sizeof ifr);
428 memset (&drvinfo, 0, sizeof drvinfo);
429 ifr.ifr_data = (char *) &drvinfo;
430 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
431 drvinfo.cmd = ETHTOOL_GDRVINFO;
432 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
434 /* Some interfaces (eg "lo") don't support this ioctl */
435 if ((errno != ENOTSUP) && (errno != ENODEV))
436 clib_unix_warning ("ioctl fetch intf %s bus info error",
441 if (strcmp ((char *) s, drvinfo.bus_info))
444 memset (&ifr, 0, sizeof (ifr));
445 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
446 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
448 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
454 if (ifr.ifr_flags & IFF_UP)
456 error = clib_error_return (0, "Skipping PCI device %U as host "
457 "interface %s is up",
458 format_vlib_pci_addr, addr, e->d_name);
465 vec_reset_length (s);
467 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
468 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
469 vec_reset_length (s);
471 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
472 if (access ((char *) s, F_OK) == 0)
474 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
475 clear_driver_override = 1;
479 vec_reset_length (s);
480 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
481 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
484 vec_reset_length (s);
486 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
487 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
488 vec_reset_length (s);
490 if (clear_driver_override)
492 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
493 clib_sysfs_write ((char *) s, "%c", 0);
494 vec_reset_length (s);
500 vec_free (dev_dir_name);
501 vec_free (driver_name);
506 static clib_error_t *
507 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
509 linux_pci_device_t *l = arg;
510 unformat_input_t input;
512 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
514 if (!unformat (&input, "uio%d", &l->uio_minor))
517 unformat_free (&input);
521 static clib_error_t *
522 linux_pci_uio_read_ready (clib_file_t * uf)
524 int __attribute__ ((unused)) rv;
525 vlib_pci_dev_handle_t h = uf->private_data;
526 linux_pci_device_t *p = linux_pci_get_device (h);
529 rv = read (uf->file_descriptor, &icount, 4);
531 if (p->interrupt_handler)
532 p->interrupt_handler (h);
534 vlib_pci_intr_enable (h);
536 return /* no error */ 0;
539 static clib_error_t *
540 linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
542 clib_error_t *err = 0;
543 struct vfio_irq_set i = {
544 .argsz = sizeof (struct vfio_irq_set),
547 .index = VFIO_PCI_INTX_IRQ_INDEX,
548 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
551 if (ioctl (d->vfio_device_fd, VFIO_DEVICE_SET_IRQS, &i) < 0)
553 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
554 format_vlib_pci_addr, &d->addr);
559 static clib_error_t *
560 linux_pci_uio_error_ready (clib_file_t * uf)
562 u32 error_index = (u32) uf->private_data;
564 return clib_error_return (0, "pci device %d: error", error_index);
567 static clib_error_t *
568 linux_pci_vfio_read_ready (clib_file_t * uf)
570 int __attribute__ ((unused)) rv;
571 vlib_pci_dev_handle_t h = uf->private_data;
572 linux_pci_device_t *p = linux_pci_get_device (h);
575 rv = read (uf->file_descriptor, &icount, sizeof (icount));
577 if (p->interrupt_handler)
578 p->interrupt_handler (h);
580 linux_pci_vfio_unmask_intx (p);
582 return /* no error */ 0;
585 static clib_error_t *
586 linux_pci_vfio_error_ready (clib_file_t * uf)
588 u32 error_index = (u32) uf->private_data;
590 return clib_error_return (0, "pci device %d: error", error_index);
593 static clib_error_t *
594 add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
595 vlib_pci_device_info_t * di, pci_device_registration_t * r)
597 clib_error_t *err = 0;
598 clib_file_t t = { 0 };
601 p->addr.as_u32 = di->addr.as_u32;
604 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
605 format_vlib_pci_addr, &di->addr, 0);
607 p->config_fd = open ((char *) s, O_RDWR);
608 vec_reset_length (s);
610 if (p->config_fd == -1)
612 err = clib_error_return_unix (0, "open '%s'", s);
616 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
617 format_vlib_pci_addr, &di->addr);
618 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
620 vec_reset_length (s);
622 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
623 p->uio_fd = open ((char *) s, O_RDWR);
626 err = clib_error_return_unix (0, "open '%s'", s);
630 t.read_function = linux_pci_uio_read_ready;
631 t.file_descriptor = p->uio_fd;
632 t.error_function = linux_pci_uio_error_ready;
633 t.private_data = p->handle;
635 p->clib_file_index = clib_file_add (&file_main, &t);
636 p->interrupt_handler = r->interrupt_handler;
637 err = r->init_function (vm, p->handle);
643 if (p->config_fd != -1)
644 close (p->config_fd);
651 static linux_pci_vfio_iommu_group_t *
652 get_vfio_iommu_group (int group)
654 linux_pci_main_t *lpm = &linux_pci_main;
657 p = hash_get (lpm->iommu_pool_index_by_group, group);
659 return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
662 static clib_error_t *
663 open_vfio_iommu_group (int group)
665 linux_pci_main_t *lpm = &linux_pci_main;
666 linux_pci_vfio_iommu_group_t *g;
667 clib_error_t *err = 0;
668 struct vfio_group_status group_status;
672 g = get_vfio_iommu_group (group);
678 s = format (s, "/dev/vfio/%u%c", group, 0);
679 fd = open ((char *) s, O_RDWR);
681 return clib_error_return_unix (0, "open '%s'", s);
683 group_status.argsz = sizeof (group_status);
684 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
686 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
691 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
693 err = clib_error_return (0, "iommu group %d is not viable (not all "
694 "devices in this group bound to vfio-pci)",
699 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
701 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
706 if (lpm->vfio_iommu_mode == 0)
708 if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
711 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
715 lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
719 pool_get (lpm->iommu_groups, g);
722 hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
730 static clib_error_t *
731 add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
732 vlib_pci_device_info_t * di, pci_device_registration_t * r)
734 linux_pci_vfio_iommu_group_t *g;
735 struct vfio_device_info device_info;
736 struct vfio_irq_info irq_info = { 0 };
737 clib_error_t *err = 0;
741 p->addr.as_u32 = di->addr.as_u32;
743 if (di->driver_name == 0 ||
744 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
745 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
746 "vfio-pci", format_vlib_pci_addr, &di->addr,
749 if ((err = open_vfio_iommu_group (di->iommu_group)))
752 g = get_vfio_iommu_group (di->iommu_group);
754 s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
755 if ((dfd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
757 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
758 format_vlib_pci_addr, &di->addr);
761 vec_reset_length (s);
762 p->vfio_device_fd = dfd;
764 device_info.argsz = sizeof (device_info);
765 if (ioctl (dfd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
767 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
768 format_vlib_pci_addr, &di->addr);
772 irq_info.argsz = sizeof (struct vfio_irq_info);
773 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
774 if (ioctl (dfd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
776 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
777 "'%U'", format_vlib_pci_addr, &di->addr);
781 /* reset if device supports it */
782 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
783 if (ioctl (dfd, VFIO_DEVICE_RESET) < 0)
785 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
786 format_vlib_pci_addr, &di->addr);
790 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) && irq_info.count == 1)
792 u8 buf[sizeof (struct vfio_irq_set) + sizeof (int)] = { 0 };
793 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
794 clib_file_t t = { 0 };
795 int efd = eventfd (0, EFD_NONBLOCK);
797 irq_set->argsz = sizeof (buf);
799 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
801 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
802 clib_memcpy (&irq_set->data, &efd, sizeof (int));
804 if (ioctl (dfd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
806 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
807 format_vlib_pci_addr, &di->addr);
811 t.read_function = linux_pci_vfio_read_ready;
812 t.file_descriptor = efd;
813 t.error_function = linux_pci_vfio_error_ready;
814 t.private_data = p->handle;
815 p->clib_file_index = clib_file_add (&file_main, &t);
817 /* unmask the interrupt */
818 linux_pci_vfio_unmask_intx (p);
821 p->interrupt_handler = r->interrupt_handler;
823 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
824 format_vlib_pci_addr, &di->addr, 0);
826 p->config_fd = open ((char *) s, O_RDWR);
827 vec_reset_length (s);
829 if (p->config_fd == -1)
831 err = clib_error_return_unix (0, "open '%s'", s);
835 err = r->init_function (vm, p->handle);
843 if (p->config_fd != -1)
844 close (p->config_fd);
849 /* Configuration space read/write. */
851 vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
852 vlib_read_or_write_t read_or_write,
853 uword address, void *data, u32 n_bytes)
855 linux_pci_device_t *p = linux_pci_get_device (h);
858 if (read_or_write == VLIB_READ)
859 n = pread (p->config_fd, data, n_bytes, address);
861 n = pwrite (p->config_fd, data, n_bytes, address);
864 return clib_error_return_unix (0, "%s",
865 read_or_write == VLIB_READ
871 static clib_error_t *
872 vlib_pci_map_resource_int (vlib_pci_dev_handle_t h,
873 u32 resource, u8 * addr, void **result)
875 linux_pci_device_t *p = linux_pci_get_device (h);
876 struct stat stat_buf;
880 int flags = MAP_SHARED;
884 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
885 format_vlib_pci_addr, &p->addr, resource, 0);
887 fd = open ((char *) file_name, O_RDWR);
890 error = clib_error_return_unix (0, "open `%s'", file_name);
894 if (fstat (fd, &stat_buf) < 0)
896 error = clib_error_return_unix (0, "fstat `%s'", file_name);
900 vec_validate (p->resource_fds, resource);
901 p->resource_fds[resource] = fd;
905 *result = mmap (addr,
906 /* size */ stat_buf.st_size,
907 PROT_READ | PROT_WRITE, flags,
910 if (*result == (void *) -1)
912 error = clib_error_return_unix (0, "mmap `%s'", file_name);
922 vec_free (file_name);
927 vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource, void **result)
929 return (vlib_pci_map_resource_int (h, resource, 0 /* addr */ , result));
933 vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
934 u32 resource, u8 * addr, void **result)
936 return (vlib_pci_map_resource_int (h, resource, addr, result));
940 init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
942 vlib_pci_main_t *pm = &pci_main;
943 linux_pci_main_t *lpm = &linux_pci_main;
944 pci_device_registration_t *r;
946 clib_error_t *err = 0;
947 linux_pci_device_t *p;
949 pool_get (lpm->linux_pci_devices, p);
950 p->handle = p - lpm->linux_pci_devices;
952 r = pm->pci_device_registrations;
956 for (i = r->supported_devices; i->vendor_id != 0; i++)
957 if (i->vendor_id == di->config0.header.vendor_id &&
958 i->device_id == di->config0.header.device_id)
960 if (di->iommu_group != -1)
961 err = add_device_vfio (vm, p, di, r);
963 err = add_device_uio (vm, p, di, r);
966 clib_error_report (err);
970 r = r->next_registration;
973 /* No driver, close the PCI config-space FD */
974 memset (p, 0, sizeof (linux_pci_device_t));
975 pool_put (lpm->linux_pci_devices, p);
978 static clib_error_t *
979 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
981 vlib_pci_addr_t addr, **addrv = arg;
982 unformat_input_t input;
983 clib_error_t *err = 0;
985 unformat_init_string (&input, (char *) dev_dir_name,
986 vec_len (dev_dir_name));
988 if (!unformat (&input, "/sys/bus/pci/devices/%U",
989 unformat_vlib_pci_addr, &addr))
990 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
992 unformat_free (&input);
997 vec_add1 (*addrv, addr);
1002 pci_addr_cmp (void *v1, void *v2)
1004 vlib_pci_addr_t *a1 = v1;
1005 vlib_pci_addr_t *a2 = v2;
1007 if (a1->domain > a2->domain)
1009 if (a1->domain < a2->domain)
1011 if (a1->bus > a2->bus)
1013 if (a1->bus < a2->bus)
1015 if (a1->slot > a2->slot)
1017 if (a1->slot < a2->slot)
1019 if (a1->function > a2->function)
1021 if (a1->function < a2->function)
1027 vlib_pci_get_all_dev_addrs ()
1029 vlib_pci_addr_t *addrs = 0;
1031 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1032 &addrs, /* scan_dirs */ 0);
1039 vec_sort_with_function (addrs, pci_addr_cmp);
1045 linux_pci_init (vlib_main_t * vm)
1047 vlib_pci_main_t *pm = &pci_main;
1048 linux_pci_main_t *lpm = &linux_pci_main;
1049 vlib_pci_addr_t *addr = 0, *addrs;
1050 clib_error_t *error;
1055 if ((error = vlib_call_init_function (vm, unix_input_init)))
1058 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1060 fd = open ("/dev/vfio/vfio", O_RDWR);
1062 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1068 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1074 lpm->vfio_container_fd = fd;
1075 lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1077 addrs = vlib_pci_get_all_dev_addrs ();
1079 vec_foreach (addr, addrs)
1081 vlib_pci_device_info_t *d;
1082 if ((d = vlib_pci_get_device_info (addr, 0)))
1084 init_device_from_registered (vm, d);
1085 vlib_pci_free_device_info (d);
1093 VLIB_INIT_FUNCTION (linux_pci_init);
1096 * fd.io coding-style-patch-verification: ON
1099 * eval: (c-set-style "gnu")