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>
45 #include <vlib/linux/vfio.h>
47 #include <sys/types.h>
51 #include <sys/ioctl.h>
53 #include <linux/ethtool.h>
54 #include <linux/sockios.h>
55 #include <linux/vfio.h>
56 #include <sys/eventfd.h>
58 static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
59 static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
60 static char *sysfs_mod_vfio_noiommu =
61 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
76 pci_intx_handler_function_t *intx_handler;
77 pci_msix_handler_function_t *msix_handler;
83 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
84 LINUX_PCI_DEVICE_TYPE_UIO,
85 LINUX_PCI_DEVICE_TYPE_VFIO,
86 } linux_pci_device_type_t;
90 linux_pci_device_type_t type;
91 vlib_pci_dev_handle_t handle;
94 /* Resource file descriptors. */
95 linux_pci_region_t *regions;
97 /* File descriptor for config space read/write. */
101 /* Device File descriptor */
104 /* Minor device for uio device. */
107 /* Interrupt handlers */
108 linux_pci_irq_t intx_irq;
109 linux_pci_irq_t *msix_irqs;
114 } linux_pci_device_t;
116 /* Pool of PCI devices. */
119 vlib_main_t *vlib_main;
120 linux_pci_device_t *linux_pci_devices;
124 extern linux_pci_main_t linux_pci_main;
126 static linux_pci_device_t *
127 linux_pci_get_device (vlib_pci_dev_handle_t h)
129 linux_pci_main_t *lpm = &linux_pci_main;
130 return pool_elt_at_index (lpm->linux_pci_devices, h);
134 vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
136 linux_pci_device_t *d = linux_pci_get_device (h);
137 return d->private_data;
141 vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
143 linux_pci_device_t *d = linux_pci_get_device (h);
144 d->private_data = private_data;
148 vlib_pci_get_addr (vlib_pci_dev_handle_t h)
150 linux_pci_device_t *d = linux_pci_get_device (h);
154 /* Call to allocate/initialize the pci subsystem.
155 This is not an init function so that users can explicitly enable
156 pci only when it's needed. */
157 clib_error_t *pci_bus_init (vlib_main_t * vm);
159 linux_pci_main_t linux_pci_main;
161 vlib_pci_device_info_t *
162 vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
164 linux_vfio_main_t *lvm = &vfio_main;
166 vlib_pci_device_info_t *di;
171 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
172 memset (di, 0, sizeof (vlib_pci_device_info_t));
173 di->addr.as_u32 = addr->as_u32;
175 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
176 format_vlib_pci_addr, addr);
178 f = format (0, "%v/config%c", dev_dir_name, 0);
179 fd = open ((char *) f, O_RDWR);
181 /* Try read-only access if write fails. */
183 fd = open ((char *) f, O_RDONLY);
187 err = clib_error_return_unix (0, "open `%s'", f);
191 /* You can only read more that 64 bytes of config space as root; so we try to
192 read the full space but fall back to just the first 64 bytes. */
193 if (read (fd, &di->config_data, sizeof (di->config_data)) <
194 sizeof (di->config0))
196 err = clib_error_return_unix (0, "read `%s'", f);
202 static pci_config_header_t all_ones;
203 if (all_ones.vendor_id == 0)
204 memset (&all_ones, ~0, sizeof (all_ones));
206 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
208 err = clib_error_return (0, "invalid PCI config for `%s'", f);
214 if (di->config0.header.header_type == 0)
215 pci_config_type0_little_to_host (&di->config0);
217 pci_config_type1_little_to_host (&di->config1);
220 vec_reset_length (f);
221 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
222 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
226 clib_error_free (err);
229 vec_reset_length (f);
230 f = format (f, "%v/class%c", dev_dir_name, 0);
231 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
234 di->device_class = tmp >> 8;
236 vec_reset_length (f);
237 f = format (f, "%v/vendor%c", dev_dir_name, 0);
238 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
243 vec_reset_length (f);
244 f = format (f, "%v/device%c", dev_dir_name, 0);
245 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
250 vec_reset_length (f);
251 f = format (f, "%v/driver%c", dev_dir_name, 0);
252 di->driver_name = clib_sysfs_link_to_name ((char *) f);
254 di->iommu_group = -1;
255 if (lvm->container_fd != -1)
258 vec_reset_length (f);
259 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
260 tmpstr = clib_sysfs_link_to_name ((char *) f);
263 di->iommu_group = atoi ((char *) tmpstr);
270 vec_reset_length (f);
271 f = format (f, "%v/vpd%c", dev_dir_name, 0);
272 fd = open ((char *) f, O_RDONLY);
281 if (read (fd, &tag, 3) != 3)
284 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
287 len = (tag[2] << 8) | tag[1];
288 vec_validate (data, len);
290 if (read (fd, data, len) != len)
296 di->product_name = data;
297 else if (tag[0] == 0x90)
299 else if (tag[0] == 0x91)
310 vlib_pci_free_device_info (di);
315 vec_free (dev_dir_name);
319 clib_error_free (err);
324 directory_exists (char *path)
326 struct stat s = { 0 };
327 if (stat (path, &s) == -1)
330 return S_ISDIR (s.st_mode);
334 vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
336 clib_error_t *error = 0;
337 u8 *s = 0, *driver_name = 0;
340 vlib_pci_device_info_t *di;
341 int fd, clear_driver_override = 0;
342 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
343 format_vlib_pci_addr, addr);
345 di = vlib_pci_get_device_info (addr, &error);
350 if (strncmp ("auto", uio_drv_name, 5) == 0)
352 int vfio_pci_loaded = 0;
354 if (directory_exists ("/sys/module/vfio_pci"))
357 if (di->iommu_group != -1)
359 /* device is bound to IOMMU group */
360 if (!vfio_pci_loaded)
362 error = clib_error_return (0, "Skipping PCI device %U: device "
363 "is bound to IOMMU group and "
364 "vfio-pci driver is not loaded",
365 format_vlib_pci_addr, addr);
369 uio_drv_name = "vfio-pci";
373 /* device is not bound to IOMMU group so we have multiple options */
374 if (vfio_pci_loaded &&
375 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
376 uio_drv_name = "vfio-pci";
377 else if (directory_exists ("/sys/module/uio_pci_generic"))
378 uio_drv_name = "uio_pci_generic";
379 else if (directory_exists ("/sys/module/igb_uio"))
380 uio_drv_name = "igb_uio";
383 clib_error_free (error);
384 error = clib_error_return (0, "Skipping PCI device %U: missing "
385 "kernel VFIO or UIO driver",
386 format_vlib_pci_addr, addr);
389 clib_error_free (error);
393 s = format (s, "%v/driver%c", dev_dir_name, 0);
394 driver_name = clib_sysfs_link_to_name ((char *) s);
395 vec_reset_length (s);
398 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
399 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
400 (strcmp ("igb_uio", (char *) driver_name) == 0)))
403 /* walk trough all linux interfaces and if interface belonging to
404 this device is founf check if interface is admin up */
405 dir = opendir ("/sys/class/net");
406 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
410 error = clib_error_return (0, "Skipping PCI device %U: failed to "
411 "read /sys/class/net",
412 format_vlib_pci_addr, addr);
416 fd = socket (PF_INET, SOCK_DGRAM, 0);
419 error = clib_error_return_unix (0, "socket");
423 while ((e = readdir (dir)))
426 struct ethtool_drvinfo drvinfo;
428 if (e->d_name[0] == '.') /* skip . and .. */
431 memset (&ifr, 0, sizeof ifr);
432 memset (&drvinfo, 0, sizeof drvinfo);
433 ifr.ifr_data = (char *) &drvinfo;
434 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
435 drvinfo.cmd = ETHTOOL_GDRVINFO;
436 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
438 /* Some interfaces (eg "lo") don't support this ioctl */
439 if ((errno != ENOTSUP) && (errno != ENODEV))
440 clib_unix_warning ("ioctl fetch intf %s bus info error",
445 if (strcmp ((char *) s, drvinfo.bus_info))
448 memset (&ifr, 0, sizeof (ifr));
449 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
450 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
452 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
458 if (ifr.ifr_flags & IFF_UP)
460 error = clib_error_return (0, "Skipping PCI device %U as host "
461 "interface %s is up",
462 format_vlib_pci_addr, addr, e->d_name);
469 vec_reset_length (s);
471 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
472 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
473 vec_reset_length (s);
475 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
476 if (access ((char *) s, F_OK) == 0)
478 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
479 clear_driver_override = 1;
483 vec_reset_length (s);
484 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
485 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
488 vec_reset_length (s);
490 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
491 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
492 vec_reset_length (s);
494 if (clear_driver_override)
496 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
497 clib_sysfs_write ((char *) s, "%c", 0);
498 vec_reset_length (s);
504 vec_free (dev_dir_name);
505 vec_free (driver_name);
510 static clib_error_t *
511 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
513 linux_pci_device_t *l = arg;
514 unformat_input_t input;
516 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
518 if (!unformat (&input, "uio%d", &l->uio_minor))
521 unformat_free (&input);
525 static clib_error_t *
526 vfio_set_irqs (linux_pci_device_t * p, u32 index, u32 start, u32 count,
527 u32 flags, int *efds)
529 int data_len = efds ? count * sizeof (int) : 0;
530 u8 buf[sizeof (struct vfio_irq_set) + data_len];
531 struct vfio_irq_info irq_info = { 0 };
532 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
535 irq_info.argsz = sizeof (struct vfio_irq_info);
536 irq_info.index = index;
538 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
539 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
540 "'%U'", format_vlib_pci_addr, &p->addr);
542 if (irq_info.count < start + count)
543 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
544 "'%U'", format_vlib_pci_addr, &p->addr);
549 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
550 clib_memcpy (&irq_set->data, efds, data_len);
553 flags |= VFIO_IRQ_SET_DATA_NONE;
555 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
556 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
558 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
559 irq_set->index = index;
560 irq_set->start = start;
561 irq_set->count = count;
562 irq_set->flags = flags;
564 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
565 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
566 "[index = %u, start = %u, count = %u, "
568 format_vlib_pci_addr, &p->addr,
569 index, start, count, flags);
573 static clib_error_t *
574 linux_pci_uio_read_ready (clib_file_t * uf)
576 int __attribute__ ((unused)) rv;
577 vlib_pci_dev_handle_t h = uf->private_data;
578 linux_pci_device_t *p = linux_pci_get_device (h);
579 linux_pci_irq_t *irq = &p->intx_irq;
582 rv = read (uf->file_descriptor, &icount, 4);
584 if (irq->intx_handler)
585 irq->intx_handler (h);
587 vlib_pci_intr_enable (h);
589 return /* no error */ 0;
592 static clib_error_t *
593 linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
595 return vfio_set_irqs (d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
596 VFIO_IRQ_SET_ACTION_UNMASK, 0);
599 static clib_error_t *
600 linux_pci_uio_error_ready (clib_file_t * uf)
602 u32 error_index = (u32) uf->private_data;
604 return clib_error_return (0, "pci device %d: error", error_index);
607 static clib_error_t *
608 linux_pci_vfio_msix_read_ready (clib_file_t * uf)
610 int __attribute__ ((unused)) rv;
611 vlib_pci_dev_handle_t h = uf->private_data >> 16;
612 u16 line = uf->private_data & 0xffff;
613 linux_pci_device_t *p = linux_pci_get_device (h);
614 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
617 rv = read (uf->file_descriptor, &icount, sizeof (icount));
619 if (irq->msix_handler)
620 irq->msix_handler (h, line);
622 return /* no error */ 0;
625 static clib_error_t *
626 linux_pci_vfio_intx_read_ready (clib_file_t * uf)
628 int __attribute__ ((unused)) rv;
629 vlib_pci_dev_handle_t h = uf->private_data;
630 linux_pci_device_t *p = linux_pci_get_device (h);
631 linux_pci_irq_t *irq = &p->intx_irq;
634 rv = read (uf->file_descriptor, &icount, sizeof (icount));
636 if (irq->intx_handler)
637 irq->intx_handler (h);
639 linux_pci_vfio_unmask_intx (p);
641 return /* no error */ 0;
644 static clib_error_t *
645 linux_pci_vfio_error_ready (clib_file_t * uf)
647 u32 error_index = (u32) uf->private_data;
649 return clib_error_return (0, "pci device %d: error", error_index);
652 static clib_error_t *
653 add_device_uio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
654 pci_device_registration_t * r)
656 linux_pci_main_t *lpm = &linux_pci_main;
657 clib_error_t *err = 0;
660 p->addr.as_u32 = di->addr.as_u32;
662 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
664 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
665 format_vlib_pci_addr, &di->addr, 0);
667 p->config_fd = open ((char *) s, O_RDWR);
668 p->config_offset = 0;
669 vec_reset_length (s);
671 if (p->config_fd == -1)
673 err = clib_error_return_unix (0, "open '%s'", s);
677 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
678 format_vlib_pci_addr, &di->addr);
679 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
681 vec_reset_length (s);
683 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
684 p->fd = open ((char *) s, O_RDWR);
687 err = clib_error_return_unix (0, "open '%s'", s);
691 if (r && r->interrupt_handler)
692 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
694 if (r && r->init_function)
695 err = r->init_function (lpm->vlib_main, p->handle);
701 if (p->config_fd != -1)
702 close (p->config_fd);
710 vlib_pci_register_intx_handler (vlib_pci_dev_handle_t h,
711 pci_intx_handler_function_t * intx_handler)
713 linux_pci_device_t *p = linux_pci_get_device (h);
714 clib_file_t t = { 0 };
715 linux_pci_irq_t *irq = &p->intx_irq;
716 ASSERT (irq->fd == -1);
718 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
720 struct vfio_irq_info irq_info = { 0 };
721 irq_info.argsz = sizeof (struct vfio_irq_info);
722 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
723 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
724 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
725 "%U'", format_vlib_pci_addr, &p->addr);
726 if (irq_info.count != 1)
727 return clib_error_return (0, "INTx interrupt does not exist on device"
728 "'%U'", format_vlib_pci_addr, &p->addr);
730 irq->fd = eventfd (0, EFD_NONBLOCK);
732 return clib_error_return_unix (0, "eventfd");
733 t.file_descriptor = irq->fd;
734 t.read_function = linux_pci_vfio_intx_read_ready;
736 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
738 t.file_descriptor = p->fd;
739 t.read_function = linux_pci_uio_read_ready;
744 t.error_function = linux_pci_uio_error_ready;
745 t.private_data = p->handle;
746 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
747 irq->clib_file_index = clib_file_add (&file_main, &t);
748 irq->intx_handler = intx_handler;
753 vlib_pci_register_msix_handler (vlib_pci_dev_handle_t h, u32 start, u32 count,
754 pci_msix_handler_function_t * msix_handler)
756 clib_error_t *err = 0;
757 linux_pci_device_t *p = linux_pci_get_device (h);
760 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
761 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
765 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
769 for (i = start; i < start + count; i++)
771 clib_file_t t = { 0 };
772 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
773 ASSERT (irq->fd == -1);
775 irq->fd = eventfd (0, EFD_NONBLOCK);
778 err = clib_error_return_unix (0, "eventfd");
782 t.read_function = linux_pci_vfio_msix_read_ready;
783 t.file_descriptor = irq->fd;
784 t.error_function = linux_pci_vfio_error_ready;
785 t.private_data = p->handle << 16 | i;
786 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
788 irq->clib_file_index = clib_file_add (&file_main, &t);
789 irq->msix_handler = msix_handler;
797 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
800 clib_file_del_by_index (&file_main, irq->clib_file_index);
809 vlib_pci_enable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
811 linux_pci_device_t *p = linux_pci_get_device (h);
815 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
816 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
819 for (i = start; i < start + count; i++)
821 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
825 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
826 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
830 vlib_pci_disable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
832 linux_pci_device_t *p = linux_pci_get_device (h);
835 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
836 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
839 for (i = start; i < start + count; i++)
842 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
843 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
846 static clib_error_t *
847 add_device_vfio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
848 pci_device_registration_t * r)
850 linux_pci_main_t *lpm = &linux_pci_main;
851 struct vfio_device_info device_info = { 0 };
852 struct vfio_region_info reg = { 0 };
853 clib_error_t *err = 0;
856 p->addr.as_u32 = di->addr.as_u32;
857 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
859 if (di->driver_name == 0 ||
860 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
861 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
862 "vfio-pci", format_vlib_pci_addr, &di->addr,
865 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd)))
868 device_info.argsz = sizeof (device_info);
869 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
871 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
872 format_vlib_pci_addr, &di->addr);
876 reg.argsz = sizeof (struct vfio_region_info);
877 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
878 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
880 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
881 format_vlib_pci_addr, &di->addr);
884 p->config_offset = reg.offset;
885 p->config_fd = p->fd;
887 /* reset if device supports it */
888 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
889 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
891 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
892 format_vlib_pci_addr, &di->addr);
896 if (r && r->interrupt_handler)
898 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
899 linux_pci_vfio_unmask_intx (p);
902 if (r && r->init_function)
903 err = r->init_function (lpm->vlib_main, p->handle);
911 if (p->config_fd != -1)
912 close (p->config_fd);
917 /* Configuration space read/write. */
919 vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
920 vlib_read_or_write_t read_or_write,
921 uword address, void *data, u32 n_bytes)
923 linux_pci_device_t *p = linux_pci_get_device (h);
926 if (read_or_write == VLIB_READ)
927 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
929 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
932 return clib_error_return_unix (0, "%s",
933 read_or_write == VLIB_READ
939 static clib_error_t *
940 vlib_pci_map_region_int (vlib_pci_dev_handle_t h,
941 u32 bar, u8 * addr, void **result)
943 linux_pci_device_t *p = linux_pci_get_device (h);
946 int flags = MAP_SHARED;
947 u64 size = 0, offset = 0;
953 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
956 struct stat stat_buf;
957 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
958 format_vlib_pci_addr, &p->addr, bar, 0);
960 fd = open ((char *) file_name, O_RDWR);
963 error = clib_error_return_unix (0, "open `%s'", file_name);
964 vec_free (file_name);
968 if (fstat (fd, &stat_buf) < 0)
970 error = clib_error_return_unix (0, "fstat `%s'", file_name);
971 vec_free (file_name);
976 vec_free (file_name);
979 size = stat_buf.st_size;
982 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
984 struct vfio_region_info reg = { 0 };
985 reg.argsz = sizeof (struct vfio_region_info);
987 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, ®) < 0)
988 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
989 "'%U'", format_vlib_pci_addr,
998 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
999 if (*result == (void *) -1)
1001 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1002 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1008 vec_validate_init_empty (p->regions, bar,
1009 (linux_pci_region_t) { .fd = -1});
1011 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1012 p->regions[bar].fd = fd;
1013 p->regions[bar].addr = *result;
1014 p->regions[bar].size = size;
1019 vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource, void **result)
1021 return (vlib_pci_map_region_int (h, resource, 0 /* addr */ , result));
1025 vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h, u32 resource, u8 * addr,
1028 return (vlib_pci_map_region_int (h, resource, addr, result));
1032 vlib_pci_device_open (vlib_pci_addr_t * addr,
1033 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1035 linux_pci_main_t *lpm = &linux_pci_main;
1036 vlib_pci_device_info_t *di;
1037 linux_pci_device_t *p;
1038 clib_error_t *err = 0;
1041 di = vlib_pci_get_device_info (addr, &err);
1045 for (i = ids; i->vendor_id != 0; i++)
1046 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1049 if (i->vendor_id == 0)
1050 return clib_error_return (0, "Wrong vendor or device id");
1052 pool_get (lpm->linux_pci_devices, p);
1053 p->handle = p - lpm->linux_pci_devices;
1054 p->intx_irq.fd = -1;
1056 if (di->iommu_group != -1)
1057 err = add_device_vfio (p, di, 0);
1059 err = add_device_uio (p, di, 0);
1063 *handle = p->handle;
1066 vlib_pci_free_device_info (di);
1069 memset (p, 0, sizeof (linux_pci_device_t));
1070 pool_put (lpm->linux_pci_devices, p);
1077 vlib_pci_device_close (vlib_pci_dev_handle_t h)
1079 linux_pci_main_t *lpm = &linux_pci_main;
1080 linux_pci_device_t *p = linux_pci_get_device (h);
1081 linux_pci_irq_t *irq;
1082 linux_pci_region_t *res;
1083 clib_error_t *err = 0;
1085 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1088 clib_file_del_by_index (&file_main, irq->clib_file_index);
1089 close (p->config_fd);
1091 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1094 /* close INTx irqs */
1097 err = vfio_set_irqs (p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
1098 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1099 clib_error_free (err);
1100 clib_file_del_by_index (&file_main, irq->clib_file_index);
1104 /* close MSI-X irqs */
1105 if (vec_len (p->msix_irqs))
1107 err = vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
1108 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1109 clib_error_free (err);
1111 vec_foreach (irq, p->msix_irqs)
1115 clib_file_del_by_index (&file_main, irq->clib_file_index);
1119 vec_free (p->msix_irqs);
1124 vec_foreach (res, p->regions)
1128 munmap (res->addr, res->size);
1133 vec_free (p->regions);
1136 memset (p, 0, sizeof (linux_pci_device_t));
1137 pool_put (lpm->linux_pci_devices, p);
1141 init_device_from_registered (vlib_pci_device_info_t * di)
1143 vlib_pci_main_t *pm = &pci_main;
1144 linux_pci_main_t *lpm = &linux_pci_main;
1145 pci_device_registration_t *r;
1147 clib_error_t *err = 0;
1148 linux_pci_device_t *p;
1150 pool_get (lpm->linux_pci_devices, p);
1151 p->handle = p - lpm->linux_pci_devices;
1152 p->intx_irq.fd = -1;
1154 r = pm->pci_device_registrations;
1158 for (i = r->supported_devices; i->vendor_id != 0; i++)
1159 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1161 if (di->iommu_group != -1)
1162 err = add_device_vfio (p, di, r);
1164 err = add_device_uio (p, di, r);
1167 clib_error_report (err);
1171 r = r->next_registration;
1174 /* No driver, close the PCI config-space FD */
1175 memset (p, 0, sizeof (linux_pci_device_t));
1176 pool_put (lpm->linux_pci_devices, p);
1179 static clib_error_t *
1180 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1182 vlib_pci_addr_t addr, **addrv = arg;
1183 unformat_input_t input;
1184 clib_error_t *err = 0;
1186 unformat_init_string (&input, (char *) dev_dir_name,
1187 vec_len (dev_dir_name));
1189 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1190 unformat_vlib_pci_addr, &addr))
1191 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1193 unformat_free (&input);
1198 vec_add1 (*addrv, addr);
1203 pci_addr_cmp (void *v1, void *v2)
1205 vlib_pci_addr_t *a1 = v1;
1206 vlib_pci_addr_t *a2 = v2;
1208 if (a1->domain > a2->domain)
1210 if (a1->domain < a2->domain)
1212 if (a1->bus > a2->bus)
1214 if (a1->bus < a2->bus)
1216 if (a1->slot > a2->slot)
1218 if (a1->slot < a2->slot)
1220 if (a1->function > a2->function)
1222 if (a1->function < a2->function)
1228 vlib_pci_get_all_dev_addrs ()
1230 vlib_pci_addr_t *addrs = 0;
1232 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1233 &addrs, /* scan_dirs */ 0);
1240 vec_sort_with_function (addrs, pci_addr_cmp);
1246 linux_pci_init (vlib_main_t * vm)
1248 vlib_pci_main_t *pm = &pci_main;
1249 vlib_pci_addr_t *addr = 0, *addrs;
1250 clib_error_t *error;
1254 if ((error = vlib_call_init_function (vm, unix_input_init)))
1257 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1259 addrs = vlib_pci_get_all_dev_addrs ();
1261 vec_foreach (addr, addrs)
1263 vlib_pci_device_info_t *d;
1264 if ((d = vlib_pci_get_device_info (addr, 0)))
1266 init_device_from_registered (d);
1267 vlib_pci_free_device_info (d);
1275 VLIB_INIT_FUNCTION (linux_pci_init);
1278 * fd.io coding-style-patch-verification: ON
1281 * eval: (c-set-style "gnu")