FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
pci.c
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 /*
16  * pci.c: Linux user space PCI bus management.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
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:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
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.
38  */
39 
40 #include <vppinfra/linux/sysfs.h>
41 
42 #include <vlib/vlib.h>
43 #include <vlib/pci/pci.h>
44 #include <vlib/unix/unix.h>
45 #include <vlib/linux/vfio.h>
46 
47 #include <sys/types.h>
48 #include <sys/stat.h>
49 #include <fcntl.h>
50 #include <dirent.h>
51 #include <sys/ioctl.h>
52 #include <net/if.h>
53 #include <linux/ethtool.h>
54 #include <linux/sockios.h>
55 #include <linux/vfio.h>
56 #include <sys/eventfd.h>
57 
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";
62 
63 #define pci_log_debug(vm, dev, f, ...) \
64  vlib_log(VLIB_LOG_LEVEL_DEBUG, pci_main.log_default, "%U: " f, \
65  format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
66 #define pci_log_err(vm, dev, f, ...) \
67  vlib_log(VLIB_LOG_LEVEL_ERR, pci_main.log_default, "%U: " f, \
68  format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
69 
70 typedef struct
71 {
72  int fd;
73  void *addr;
74  size_t size;
76 
77 typedef struct
78 {
79  int fd;
81  union
82  {
85  };
87 
88 typedef enum
89 {
94 
95 typedef struct
96 {
99  vlib_pci_addr_t addr;
101 
102  /* Resource file descriptors. */
104 
105  /* File descriptor for config space read/write. */
108 
109  /* Device File descriptor */
110  int fd;
111 
112  /* read/write file descriptor for io bar */
113  int io_fd;
115 
116  /* Minor device for uio device. */
118 
119  /* Interrupt handlers */
122 
123  /* private data */
125 
127 
129 
130 /* Pool of PCI devices. */
131 typedef struct
132 {
135 
137 
139 
140 static linux_pci_device_t *
142 {
144  return pool_elt_at_index (lpm->linux_pci_devices, h);
145 }
146 
147 uword
149 {
151  return d->private_data;
152 }
153 
154 void
156  uword private_data)
157 {
159  d->private_data = private_data;
160 }
161 
162 vlib_pci_addr_t *
164 {
166  return &d->addr;
167 }
168 
169 u32
171 {
173  return d->numa_node;
174 }
175 
176 u32
178 {
180 
182  {
183  struct vfio_irq_info ii = { 0 };
184 
185  ii.argsz = sizeof (struct vfio_irq_info);
186  ii.index = VFIO_PCI_MSIX_IRQ_INDEX;
187  if (ioctl (d->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
188  return 0;
189  return ii.count;
190  }
191  return 0;
192 }
193 
194 /* Call to allocate/initialize the pci subsystem.
195  This is not an init function so that users can explicitly enable
196  pci only when it's needed. */
198 
200 
202 vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
203  clib_error_t ** error)
204 {
205  clib_error_t *err;
207  u8 *f = 0;
208  u32 tmp;
209  int fd;
210  u8 *tmpstr;
211  clib_bitmap_t *bmp = 0;
212 
213  di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
214  clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
215  di->addr.as_u32 = addr->as_u32;
216 
217  u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
218  format_vlib_pci_addr, addr);
219 
220  f = format (0, "%v/config%c", dev_dir_name, 0);
221  fd = open ((char *) f, O_RDWR);
222 
223  /* Try read-only access if write fails. */
224  if (fd < 0)
225  fd = open ((char *) f, O_RDONLY);
226 
227  if (fd < 0)
228  {
229  err = clib_error_return_unix (0, "open `%s'", f);
230  goto error;
231  }
232 
233  /* You can only read more that 64 bytes of config space as root; so we try to
234  read the full space but fall back to just the first 64 bytes. */
235  if (read (fd, &di->config_data, sizeof (di->config_data)) <
236  sizeof (di->config0))
237  {
238  err = clib_error_return_unix (0, "read `%s'", f);
239  close (fd);
240  goto error;
241  }
242 
243  {
244  static pci_config_header_t all_ones;
245  if (all_ones.vendor_id == 0)
246  clib_memset (&all_ones, ~0, sizeof (all_ones));
247 
248  if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
249  {
250  err = clib_error_return (0, "invalid PCI config for `%s'", f);
251  close (fd);
252  goto error;
253  }
254  }
255 
256  if (di->config0.header.header_type == 0)
258  else
260 
261  di->numa_node = -1;
262  vec_reset_length (f);
263  f = format (f, "%v/numa_node%c", dev_dir_name, 0);
264  err = clib_sysfs_read ((char *) f, "%d", &di->numa_node);
265  if (err)
266  {
267  di->numa_node = -1;
268  clib_error_free (err);
269  }
270  if (di->numa_node == -1)
271  {
272  /* if '/sys/bus/pci/devices/<device id>/numa_node' returns -1 and
273  it is a SMP system, set numa_node to 0. */
274  if ((err = clib_sysfs_read ("/sys/devices/system/node/online", "%U",
275  unformat_bitmap_list, &bmp)))
276  clib_error_free (err);
277  if (clib_bitmap_count_set_bits (bmp) == 1)
278  di->numa_node = 0;
279  }
280 
281  vec_reset_length (f);
282  f = format (f, "%v/class%c", dev_dir_name, 0);
283  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
284  if (err)
285  goto error;
286  di->device_class = tmp >> 8;
287 
288  vec_reset_length (f);
289  f = format (f, "%v/vendor%c", dev_dir_name, 0);
290  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
291  if (err)
292  goto error;
293  di->vendor_id = tmp;
294 
295  vec_reset_length (f);
296  f = format (f, "%v/device%c", dev_dir_name, 0);
297  err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
298  if (err)
299  goto error;
300  di->device_id = tmp;
301 
302  vec_reset_length (f);
303  f = format (f, "%v/driver%c", dev_dir_name, 0);
304  di->driver_name = clib_sysfs_link_to_name ((char *) f);
305  if (!di->driver_name)
306  di->driver_name = format (0, "<NONE>%c", 0);
307 
308  di->iommu_group = -1;
309  vec_reset_length (f);
310  f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
311  tmpstr = clib_sysfs_link_to_name ((char *) f);
312  if (tmpstr)
313  {
314  di->iommu_group = atoi ((char *) tmpstr);
315  vec_free (tmpstr);
316  }
317 
318  vec_reset_length (f);
319  f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
320  err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
321  if (err == 0)
322  {
323  if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
325  vec_free (tmpstr);
326  }
327  else
328  clib_error_free (err);
329 
330  close (fd);
331 
332  vec_reset_length (f);
333  f = format (f, "%v/vpd%c", dev_dir_name, 0);
334  fd = open ((char *) f, O_RDONLY);
335  if (fd >= 0)
336  {
337  while (1)
338  {
339  u8 tag[3];
340  u8 *data = 0;
341  uword len;
342 
343  if (read (fd, &tag, 3) != 3)
344  break;
345 
346  if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
347  break;
348 
349  len = (tag[2] << 8) | tag[1];
350  vec_validate (data, len);
351 
352  if (read (fd, data, len) != len)
353  {
354  vec_free (data);
355  break;
356  }
357  if (tag[0] == 0x82)
358  di->product_name = data;
359  else if (tag[0] == 0x90)
360  di->vpd_r = data;
361  else if (tag[0] == 0x91)
362  di->vpd_w = data;
363 
364  data = 0;
365  }
366  close (fd);
367  }
368 
369  goto done;
370 
371 error:
373  di = 0;
374 
375 done:
376  vec_free (bmp);
377  vec_free (f);
378  vec_free (dev_dir_name);
379  if (error)
380  *error = err;
381  else
382  clib_error_free (err);
383  return di;
384 }
385 
386 static int
388 {
389  struct stat s = { 0 };
390  if (stat (path, &s) == -1)
391  return 0;
392 
393  return S_ISDIR (s.st_mode);
394 }
395 
396 clib_error_t *
397 vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
398  char *uio_drv_name)
399 {
400  clib_error_t *error = 0;
401  u8 *s = 0, *driver_name = 0;
402  DIR *dir = 0;
403  struct dirent *e;
405  int fd, clear_driver_override = 0;
406  u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
407  format_vlib_pci_addr, addr);
408 
409  di = vlib_pci_get_device_info (vm, addr, &error);
410 
411  if (error)
412  return error;
413 
414  if (strncmp ("auto", uio_drv_name, 5) == 0)
415  {
416  int vfio_pci_loaded = 0;
417 
418  if (directory_exists ("/sys/module/vfio_pci"))
419  vfio_pci_loaded = 1;
420 
421  if (di->iommu_group != -1)
422  {
423  /* device is bound to IOMMU group */
424  if (!vfio_pci_loaded)
425  {
426  error = clib_error_return (0, "Skipping PCI device %U: device "
427  "is bound to IOMMU group and "
428  "vfio-pci driver is not loaded",
429  format_vlib_pci_addr, addr);
430  goto done;
431  }
432  else
433  uio_drv_name = "vfio-pci";
434  }
435  else
436  {
437  /* device is not bound to IOMMU group so we have multiple options */
438  if (vfio_pci_loaded &&
439  (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
440  uio_drv_name = "vfio-pci";
441  else if (directory_exists ("/sys/module/uio_pci_generic"))
442  uio_drv_name = "uio_pci_generic";
443  else if (directory_exists ("/sys/module/igb_uio"))
444  uio_drv_name = "igb_uio";
445  else
446  {
447  clib_error_free (error);
448  error = clib_error_return (0, "Skipping PCI device %U: missing "
449  "kernel VFIO or UIO driver",
450  format_vlib_pci_addr, addr);
451  goto done;
452  }
453  clib_error_free (error);
454  }
455  }
456 
457  s = format (s, "%v/driver%c", dev_dir_name, 0);
458  driver_name = clib_sysfs_link_to_name ((char *) s);
459  vec_reset_length (s);
460 
461  if (driver_name &&
462  ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
463  (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
464  (strcmp ("igb_uio", (char *) driver_name) == 0)))
465  goto done;
466 
467  /* walk trough all linux interfaces and if interface belonging to
468  this device is founf check if interface is admin up */
469  dir = opendir ("/sys/class/net");
470  s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
471 
472  if (!dir)
473  {
474  error = clib_error_return (0, "Skipping PCI device %U: failed to "
475  "read /sys/class/net",
476  format_vlib_pci_addr, addr);
477  goto done;
478  }
479 
480  fd = socket (PF_INET, SOCK_DGRAM, 0);
481  if (fd < 0)
482  {
483  error = clib_error_return_unix (0, "socket");
484  goto done;
485  }
486 
487  while ((e = readdir (dir)))
488  {
489  struct ifreq ifr;
490  struct ethtool_drvinfo drvinfo;
491 
492  if (e->d_name[0] == '.') /* skip . and .. */
493  continue;
494 
495  clib_memset (&ifr, 0, sizeof ifr);
496  clib_memset (&drvinfo, 0, sizeof drvinfo);
497  ifr.ifr_data = (char *) &drvinfo;
498  clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
499 
500  drvinfo.cmd = ETHTOOL_GDRVINFO;
501  if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
502  {
503  /* Some interfaces (eg "lo") don't support this ioctl */
504  if ((errno != ENOTSUP) && (errno != ENODEV))
505  clib_unix_warning ("ioctl fetch intf %s bus info error",
506  e->d_name);
507  continue;
508  }
509 
510  if (strcmp ((char *) s, drvinfo.bus_info))
511  continue;
512 
513  clib_memset (&ifr, 0, sizeof (ifr));
514  clib_strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name) - 1);
515 
516  if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
517  {
518  error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
519  e->d_name);
520  close (fd);
521  goto done;
522  }
523 
524  if (ifr.ifr_flags & IFF_UP)
525  {
526  error = clib_error_return (0, "Skipping PCI device %U as host "
527  "interface %s is up",
528  format_vlib_pci_addr, addr, e->d_name);
529  close (fd);
530  goto done;
531  }
532  }
533 
534  close (fd);
535  vec_reset_length (s);
536 
537  s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
538  clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
539  vec_reset_length (s);
540 
541  s = format (s, "%v/driver_override%c", dev_dir_name, 0);
542  if (access ((char *) s, F_OK) == 0)
543  {
544  clib_sysfs_write ((char *) s, "%s", uio_drv_name);
545  clear_driver_override = 1;
546  }
547  else
548  {
549  vec_reset_length (s);
550  s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
551  clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
552  di->device_id);
553  }
554  vec_reset_length (s);
555 
556  s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
557  clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
558  vec_reset_length (s);
559 
560  if (clear_driver_override)
561  {
562  s = format (s, "%v/driver_override%c", dev_dir_name, 0);
563  clib_sysfs_write ((char *) s, "%c", 0);
564  vec_reset_length (s);
565  }
566 
567 done:
568  closedir (dir);
569  vec_free (s);
570  vec_free (dev_dir_name);
571  vec_free (driver_name);
572  return error;
573 }
574 
575 
576 static clib_error_t *
577 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
578 {
579  linux_pci_device_t *l = arg;
580  unformat_input_t input;
581 
582  unformat_init_string (&input, (char *) file_name, vec_len (file_name));
583 
584  if (!unformat (&input, "uio%d", &l->uio_minor))
585  abort ();
586 
587  unformat_free (&input);
588  return 0;
589 }
590 
591 static clib_error_t *
593  u32 count, u32 flags, int *efds)
594 {
595  int data_len = efds ? count * sizeof (int) : 0;
596  u8 buf[sizeof (struct vfio_irq_set) + data_len];
597  struct vfio_irq_info ii = { 0 };
598  struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
599 
600 
601  ii.argsz = sizeof (struct vfio_irq_info);
602  ii.index = index;
603 
604  if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
605  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
606  "'%U'", format_vlib_pci_addr, &p->addr);
607 
608  pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
609  __func__, ii.index, ii.count,
610  ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
611  ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
612  ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
613  ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
614  ii.flags);
615 
616  if (ii.count < start + count)
617  return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
618  "'%U'", format_vlib_pci_addr, &p->addr);
619 
620 
621  if (efds)
622  {
623  flags |= VFIO_IRQ_SET_DATA_EVENTFD;
624  clib_memcpy_fast (&irq_set->data, efds, data_len);
625  }
626  else
627  flags |= VFIO_IRQ_SET_DATA_NONE;
628 
629  ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
630  (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
631 
632  irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
633  irq_set->index = index;
634  irq_set->start = start;
635  irq_set->count = count;
636  irq_set->flags = flags;
637 
638  if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
639  return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
640  "[index = %u, start = %u, count = %u, "
641  "flags = 0x%x]",
643  index, start, count, flags);
644  return 0;
645 }
646 
647 static clib_error_t *
649 {
650  vlib_main_t *vm = vlib_get_main ();
651  int __attribute__ ((unused)) rv;
654  linux_pci_irq_t *irq = &p->intx_irq;
655 
656  u32 icount;
657  rv = read (uf->file_descriptor, &icount, 4);
658 
659  if (irq->intx_handler)
660  irq->intx_handler (vm, h);
661 
662  vlib_pci_intr_enable (vm, h);
663 
664  return /* no error */ 0;
665 }
666 
667 static clib_error_t *
669 {
670  return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
671  VFIO_IRQ_SET_ACTION_UNMASK, 0);
672 }
673 
674 static clib_error_t *
676 {
677  u32 error_index = (u32) uf->private_data;
678 
679  return clib_error_return (0, "pci device %d: error", error_index);
680 }
681 
682 static clib_error_t *
684 {
685  vlib_main_t *vm = vlib_get_main ();
686  int __attribute__ ((unused)) rv;
688  u16 line = uf->private_data & 0xffff;
690  linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
691 
692  u64 icount;
693  rv = read (uf->file_descriptor, &icount, sizeof (icount));
694 
695  if (irq->msix_handler)
696  irq->msix_handler (vm, h, line);
697 
698  return /* no error */ 0;
699 }
700 
701 static clib_error_t *
703 {
704  vlib_main_t *vm = vlib_get_main ();
705  int __attribute__ ((unused)) rv;
708  linux_pci_irq_t *irq = &p->intx_irq;
709 
710  u64 icount;
711  rv = read (uf->file_descriptor, &icount, sizeof (icount));
712 
713  if (irq->intx_handler)
714  irq->intx_handler (vm, h);
715 
717 
718  return /* no error */ 0;
719 }
720 
721 static clib_error_t *
723 {
724  u32 error_index = (u32) uf->private_data;
725 
726  return clib_error_return (0, "pci device %d: error", error_index);
727 }
728 
729 static clib_error_t *
732 {
734  clib_error_t *err = 0;
735  u8 *s = 0;
736 
737  p->fd = -1;
739 
740  s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
741  format_vlib_pci_addr, &di->addr, 0);
742 
743  p->config_fd = open ((char *) s, O_RDWR);
744  p->config_offset = 0;
745  vec_reset_length (s);
746 
747  if (p->config_fd == -1)
748  {
749  err = clib_error_return_unix (0, "open '%s'", s);
750  goto error;
751  }
752 
753  s = format (0, "%s/%U/uio%c", sysfs_pci_dev_path,
754  format_vlib_pci_addr, &di->addr, 0);
755  foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
756  1);
757  vec_reset_length (s);
758 
759  s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
760  p->fd = open ((char *) s, O_RDWR);
761  if (p->fd < 0)
762  {
763  err = clib_error_return_unix (0, "open '%s'", s);
764  goto error;
765  }
766 
767  if (r && r->interrupt_handler)
768  vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
769 
770  if (r && r->init_function)
771  err = r->init_function (lpm->vlib_main, p->handle);
772 
773 error:
774  vec_free (s);
775  if (err)
776  {
777  if (p->config_fd != -1)
778  close (p->config_fd);
779  if (p->fd != -1)
780  close (p->fd);
781  }
782  return err;
783 }
784 
785 clib_error_t *
787  pci_intx_handler_function_t * intx_handler)
788 {
790  clib_file_t t = { 0 };
791  linux_pci_irq_t *irq = &p->intx_irq;
792  ASSERT (irq->fd == -1);
793 
795  {
796  struct vfio_irq_info ii = { 0 };
797  ii.argsz = sizeof (struct vfio_irq_info);
798  ii.index = VFIO_PCI_INTX_IRQ_INDEX;
799  if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
800  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
801  "%U'", format_vlib_pci_addr, &p->addr);
802  pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
803  __func__, ii.index, ii.count,
804  ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
805  ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
806  ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
807  ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
808  ii.flags);
809  if (ii.count != 1)
810  return clib_error_return (0, "INTx interrupt does not exist on device"
811  "'%U'", format_vlib_pci_addr, &p->addr);
812 
813  irq->fd = eventfd (0, EFD_NONBLOCK);
814  if (irq->fd == -1)
815  return clib_error_return_unix (0, "eventfd");
816 
817  t.file_descriptor = irq->fd;
819  }
820  else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
821  {
822  t.file_descriptor = p->fd;
824  }
825  else
826  return 0;
827 
829  t.private_data = p->handle;
830  t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
832  irq->intx_handler = intx_handler;
833  return 0;
834 }
835 
836 clib_error_t *
838  u32 start, u32 count,
839  pci_msix_handler_function_t * msix_handler)
840 {
841  clib_error_t *err = 0;
843  u32 i;
844 
846  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
847  "support");
848 
849  /* *INDENT-OFF* */
850  vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
851  { .fd = -1});
852  /* *INDENT-ON* */
853 
854  for (i = start; i < start + count; i++)
855  {
856  clib_file_t t = { 0 };
858  ASSERT (irq->fd == -1);
859 
860  irq->fd = eventfd (0, EFD_NONBLOCK);
861  if (irq->fd == -1)
862  {
863  err = clib_error_return_unix (0, "eventfd");
864  goto error;
865  }
866 
868  t.file_descriptor = irq->fd;
870  t.private_data = p->handle << 16 | i;
871  t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
872  &p->addr, i);
874  irq->msix_handler = msix_handler;
875  }
876 
877  return 0;
878 
879 error:
880  while (i-- > start)
881  {
883  if (irq->fd != -1)
884  {
886  close (irq->fd);
887  irq->fd = -1;
888  }
889  }
890  return err;
891 }
892 
893 clib_error_t *
895  u16 start, u16 count)
896 {
898  int fds[count];
899  int i;
900 
902  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
903  "support");
904 
905  for (i = start; i < start + count; i++)
906  {
908  fds[i] = irq->fd;
909  }
910 
911  return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
912  VFIO_IRQ_SET_ACTION_TRIGGER, fds);
913 }
914 
915 clib_error_t *
917  u16 start, u16 count)
918 {
920  int i, fds[count];
921 
923  return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
924  "support");
925 
926  for (i = start; i < start + count; i++)
927  fds[i] = -1;
928 
929  return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
930  VFIO_IRQ_SET_ACTION_TRIGGER, fds);
931 }
932 
933 static clib_error_t *
936 {
938  struct vfio_device_info device_info = { 0 };
939  struct vfio_region_info reg = { 0 };
940  clib_error_t *err = 0;
941  u8 *s = 0;
942  int is_noiommu;
943 
945 
946  if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
947  return err;
948 
949  if (is_noiommu == 0)
950  p->supports_va_dma = 1;
951 
952  device_info.argsz = sizeof (device_info);
953  if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
954  {
955  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
956  format_vlib_pci_addr, &di->addr);
957  goto error;
958  }
959 
960  reg.argsz = sizeof (struct vfio_region_info);
961  reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
962  if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
963  {
964  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
965  format_vlib_pci_addr, &di->addr);
966  goto error;
967  }
968 
969  pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, &reg);
970 
971  p->config_offset = reg.offset;
972  p->config_fd = p->fd;
973 
974  /* reset if device supports it */
975  if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
976  if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
977  {
978  err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
979  format_vlib_pci_addr, &di->addr);
980  goto error;
981  }
982 
983  if (r && r->interrupt_handler)
984  {
985  vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
987  }
988 
989  if (p->supports_va_dma)
990  {
991  vlib_buffer_pool_t *bp;
992  /* *INDENT-OFF* */
994  {
995  u32 i;
996  vlib_physmem_map_t *pm;
998  for (i = 0; i < pm->n_pages; i++)
999  vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
1000  }
1001  /* *INDENT-ON* */
1002  }
1003 
1004  if (r && r->init_function)
1005  err = r->init_function (lpm->vlib_main, p->handle);
1006 
1007 error:
1008  vec_free (s);
1009  if (err)
1010  {
1011  if (p->fd != -1)
1012  close (p->fd);
1013  if (p->config_fd != -1 && p->config_fd != p->fd)
1014  close (p->config_fd);
1015  p->config_fd = p->fd = -1;
1016  }
1017  return err;
1018 }
1019 
1020 /* Configuration space read/write. */
1021 clib_error_t *
1023  vlib_read_or_write_t read_or_write,
1024  uword address, void *data, u32 n_bytes)
1025 {
1027  int n;
1028 
1029  if (read_or_write == VLIB_READ)
1030  n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
1031  else
1032  n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
1033 
1034  if (n != n_bytes)
1035  return clib_error_return_unix (0, "%s",
1036  read_or_write == VLIB_READ
1037  ? "read" : "write");
1038 
1039  return 0;
1040 }
1041 
1042 static clib_error_t *
1044  u64 * size, u64 * offset)
1045 {
1047  clib_error_t *error = 0;
1048  int _fd = -1;
1049  u64 _size = 0, _offset = 0;
1050 
1051  ASSERT (bar <= 5);
1052 
1053  error = 0;
1054 
1055  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1056  {
1057  u8 *file_name;
1058  struct stat stat_buf;
1059  file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1060  format_vlib_pci_addr, &p->addr, bar, 0);
1061 
1062  _fd = open ((char *) file_name, O_RDWR);
1063  if (_fd < 0)
1064  {
1065  error = clib_error_return_unix (0, "open `%s'", file_name);
1066  vec_free (file_name);
1067  return error;
1068  }
1069 
1070  if (fstat (_fd, &stat_buf) < 0)
1071  {
1072  error = clib_error_return_unix (0, "fstat `%s'", file_name);
1073  vec_free (file_name);
1074  close (_fd);
1075  return error;
1076  }
1077 
1078  vec_free (file_name);
1079  _size = stat_buf.st_size;
1080  _offset = 0;
1081  }
1082  else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1083  {
1084  struct vfio_region_info *r;
1085  u32 sz = sizeof (struct vfio_region_info);
1086  again:
1087  r = clib_mem_alloc (sz);
1088  clib_memset (r, 0, sz);
1089  r->argsz = sz;
1090  r->index = bar;
1091  if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, r) < 0)
1092  return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1093  "'%U'", format_vlib_pci_addr,
1094  &p->addr);
1095  if (sz != r->argsz)
1096  {
1097  sz = r->argsz;
1098  clib_mem_free (r);
1099  goto again;
1100  }
1101  _fd = p->fd;
1102  _size = r->size;
1103  _offset = r->offset;
1104  pci_log_debug (vm, p, "%s %U", __func__, format_vfio_region_info, r);
1105  clib_mem_free (r);
1106  }
1107  else
1108  ASSERT (0);
1109 
1110  *fd = _fd;
1111  *size = _size;
1112  *offset = _offset;
1113 
1114  return error;
1115 }
1116 
1117 static clib_error_t *
1119  u32 bar, u8 * addr, void **result)
1120 {
1122  int fd = -1;
1123  clib_error_t *error;
1124  int flags = MAP_SHARED;
1125  u64 size = 0, offset = 0;
1126  u16 command;
1127 
1128  pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1129 
1130  if ((error = vlib_pci_read_config_u16 (vm, h, 4, &command)))
1131  return error;
1132 
1133  if (!(command & PCI_COMMAND_MEMORY))
1134  {
1135  pci_log_debug (vm, p, "setting memory enable bit");
1136  command |= PCI_COMMAND_MEMORY;
1137  if ((error = vlib_pci_write_config_u16 (vm, h, 4, &command)))
1138  return error;
1139  }
1140 
1141  if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1142  return error;
1143 
1144  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && addr != 0)
1145  flags |= MAP_FIXED;
1146 
1147  *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
1148  if (*result == (void *) -1)
1149  {
1150  error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1151  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && (fd != -1))
1152  close (fd);
1153  return error;
1154  }
1155 
1156  /* *INDENT-OFF* */
1158  (linux_pci_region_t) { .fd = -1});
1159  /* *INDENT-ON* */
1160  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1161  p->regions[bar].fd = fd;
1162  p->regions[bar].addr = *result;
1163  p->regions[bar].size = size;
1164  return 0;
1165 }
1166 
1167 clib_error_t *
1169  void **result)
1170 {
1171  return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
1172 }
1173 
1174 clib_error_t *
1176  u32 resource, u8 * addr, void **result)
1177 {
1178  return (vlib_pci_map_region_int (vm, h, resource, addr, result));
1179 }
1180 
1181 clib_error_t *
1183 {
1185  clib_error_t *error = 0;
1186  int fd = -1;
1187  u64 size = 0, offset = 0;
1188 
1189  if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1190  return error;
1191 
1192  p->io_fd = fd;
1193  p->io_offset = offset;
1194  return error;
1195 }
1196 
1197 clib_error_t *
1199  vlib_read_or_write_t read_or_write,
1200  uword offset, void *data, u32 length)
1201 {
1203  int n = 0;
1204 
1205  if (read_or_write == VLIB_READ)
1206  n = pread (p->io_fd, data, length, p->io_offset + offset);
1207  else
1208  n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1209 
1210  if (n != length)
1211  return clib_error_return_unix (0, "%s",
1212  read_or_write == VLIB_READ
1213  ? "read" : "write");
1214  return 0;
1215 }
1216 
1217 clib_error_t *
1219 {
1221 
1222  if (!p->supports_va_dma)
1223  return 0;
1224 
1225  return vfio_map_physmem_page (vm, ptr);
1226 }
1227 
1228 int
1230 {
1232 
1233  return p->supports_va_dma != 0;
1234 }
1235 
1236 clib_error_t *
1237 vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
1238  pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1239 {
1242  linux_pci_device_t *p;
1243  clib_error_t *err = 0;
1244  pci_device_id_t *i;
1245 
1246  di = vlib_pci_get_device_info (vm, addr, &err);
1247 
1248  if (err)
1249  return err;
1250  for (i = ids; i->vendor_id != 0; i++)
1251  if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1252  break;
1253 
1254  if (i->vendor_id == 0)
1255  return clib_error_return (0, "Wrong vendor or device id");
1256 
1257  pool_get (lpm->linux_pci_devices, p);
1258  p->handle = p - lpm->linux_pci_devices;
1259  p->addr.as_u32 = di->addr.as_u32;
1260  p->intx_irq.fd = -1;
1261  p->intx_irq.clib_file_index = -1;
1262  p->numa_node = di->numa_node;
1263  /*
1264  * pci io bar read/write fd
1265  */
1266  p->io_fd = -1;
1267 
1268  pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1269  di->vendor_id, di->device_id, di->driver_name,
1270  di->iommu_group);
1271 
1272  if (clib_strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
1273  err = add_device_vfio (vm, p, di, 0);
1274  else if (clib_strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
1275  err = add_device_uio (vm, p, di, 0);
1276  else
1277  err = clib_error_create ("device not bound to 'vfio-pci' or "
1278  "'uio_pci_generic' kernel module");
1279  if (err)
1280  goto error;
1281 
1282  *handle = p->handle;
1283 
1284 error:
1286  if (err)
1287  {
1288  pci_log_err (vm, p, "%U", format_clib_error, err);
1289  clib_memset (p, 0, sizeof (linux_pci_device_t));
1290  pool_put (lpm->linux_pci_devices, p);
1291  }
1292 
1293  return err;
1294 }
1295 
1296 void
1298 {
1301  linux_pci_irq_t *irq;
1302  linux_pci_region_t *res;
1303  clib_error_t *err = 0;
1304 
1305  if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1306  {
1307  irq = &p->intx_irq;
1308  if (irq->clib_file_index != -1)
1310  close (p->config_fd);
1311  if (p->io_fd != -1)
1312  close (p->io_fd);
1313  }
1314  else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1315  {
1316  irq = &p->intx_irq;
1317  /* close INTx irqs */
1318  if (irq->fd != -1)
1319  {
1320  err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
1321  VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1322  clib_error_free (err);
1323  if (irq->clib_file_index != -1)
1325  close (irq->fd);
1326  }
1327 
1328  /* close MSI-X irqs */
1329  if (vec_len (p->msix_irqs))
1330  {
1331  err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
1332  VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1333  clib_error_free (err);
1334  /* *INDENT-OFF* */
1335  vec_foreach (irq, p->msix_irqs)
1336  {
1337  if (irq->fd == -1)
1338  continue;
1340  close (irq->fd);
1341  }
1342  /* *INDENT-ON* */
1343  vec_free (p->msix_irqs);
1344  }
1345  }
1346 
1347  /* *INDENT-OFF* */
1348  vec_foreach (res, p->regions)
1349  {
1350  if (res->size == 0)
1351  continue;
1352  munmap (res->addr, res->size);
1353  if (res->fd != -1)
1354  close (res->fd);
1355  }
1356  /* *INDENT-ON* */
1357  vec_free (p->regions);
1358 
1359  close (p->fd);
1360  clib_memset (p, 0, sizeof (linux_pci_device_t));
1361  pool_put (lpm->linux_pci_devices, p);
1362 }
1363 
1364 void
1366 {
1367  vlib_pci_main_t *pm = &pci_main;
1370  pci_device_id_t *i;
1371  clib_error_t *err = 0;
1372  linux_pci_device_t *p;
1373 
1374  pool_get (lpm->linux_pci_devices, p);
1375  p->handle = p - lpm->linux_pci_devices;
1376  p->intx_irq.fd = -1;
1377 
1378  r = pm->pci_device_registrations;
1379 
1380  while (r)
1381  {
1382  for (i = r->supported_devices; i->vendor_id != 0; i++)
1383  if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1384  {
1385  if (di->iommu_group != -1)
1386  err = add_device_vfio (vm, p, di, r);
1387  else
1388  err = add_device_uio (vm, p, di, r);
1389 
1390  if (err)
1391  clib_error_report (err);
1392  else
1393  return;
1394  }
1395  r = r->next_registration;
1396  }
1397 
1398  /* No driver, close the PCI config-space FD */
1399  clib_memset (p, 0, sizeof (linux_pci_device_t));
1400  pool_put (lpm->linux_pci_devices, p);
1401 }
1402 
1403 static clib_error_t *
1404 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1405 {
1406  vlib_pci_addr_t addr, **addrv = arg;
1407  unformat_input_t input;
1408  clib_error_t *err = 0;
1409 
1410  unformat_init_string (&input, (char *) dev_dir_name,
1411  vec_len (dev_dir_name));
1412 
1413  if (!unformat (&input, "/sys/bus/pci/devices/%U",
1414  unformat_vlib_pci_addr, &addr))
1415  err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1416 
1417  unformat_free (&input);
1418 
1419  if (err)
1420  return err;
1421 
1422  vec_add1 (*addrv, addr);
1423  return 0;
1424 }
1425 
1426 static int
1427 pci_addr_cmp (void *v1, void *v2)
1428 {
1429  vlib_pci_addr_t *a1 = v1;
1430  vlib_pci_addr_t *a2 = v2;
1431 
1432  if (a1->domain > a2->domain)
1433  return 1;
1434  if (a1->domain < a2->domain)
1435  return -1;
1436  if (a1->bus > a2->bus)
1437  return 1;
1438  if (a1->bus < a2->bus)
1439  return -1;
1440  if (a1->slot > a2->slot)
1441  return 1;
1442  if (a1->slot < a2->slot)
1443  return -1;
1444  if (a1->function > a2->function)
1445  return 1;
1446  if (a1->function < a2->function)
1447  return -1;
1448  return 0;
1449 }
1450 
1451 vlib_pci_addr_t *
1453 {
1454  vlib_pci_addr_t *addrs = 0;
1455  clib_error_t *err;
1457  &addrs, /* scan_dirs */ 0);
1458  if (err)
1459  {
1460  vec_free (addrs);
1461  return 0;
1462  }
1463 
1465 
1466  return addrs;
1467 }
1468 
1469 clib_error_t *
1471 {
1472  vlib_pci_main_t *pm = &pci_main;
1473  vlib_pci_addr_t *addr = 0, *addrs;
1474 
1475  pm->vlib_main = vm;
1476 
1477  ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
1478 
1479  addrs = vlib_pci_get_all_dev_addrs ();
1480  /* *INDENT-OFF* */
1481  vec_foreach (addr, addrs)
1482  {
1484  if ((d = vlib_pci_get_device_info (vm, addr, 0)))
1485  {
1488  }
1489  }
1490  /* *INDENT-ON* */
1491 
1492  return 0;
1493 }
1494 
1495 /* *INDENT-OFF* */
1497 {
1498  .runs_after = VLIB_INITS("unix_input_init"),
1499 };
1500 /* *INDENT-ON* */
1501 
1502 /*
1503  * fd.io coding-style-patch-verification: ON
1504  *
1505  * Local Variables:
1506  * eval: (c-set-style "gnu")
1507  * End:
1508  */
static clib_error_t * linux_pci_vfio_unmask_intx(vlib_main_t *vm, linux_pci_device_t *d)
Definition: pci.c:668
clib_error_t * pci_bus_init(vlib_main_t *vm)
Definition: pci.c:252
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:509
u8 count
Definition: dhcp.api:208
clib_error_t * vlib_pci_map_region_fixed(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, u8 *addr, void **result)
Definition: pci.c:1175
u8 * format_clib_error(u8 *s, va_list *va)
Definition: error.c:191
static clib_error_t * vlib_pci_intr_enable(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.h:239
#define clib_strncmp(s1, s2, n)
Definition: string.h:863
static const char * sysfs_pci_dev_path
Definition: pci.c:58
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
linux_pci_device_type_t type
Definition: pci.c:97
linux_pci_irq_t intx_irq
Definition: pci.c:120
unsigned long u64
Definition: types.h:89
pci_device_registration_t * pci_device_registrations
Definition: pci.h:156
void vlib_pci_device_close(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1297
vlib_read_or_write_t
Definition: defs.h:62
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
vlib_pci_main_t pci_main
Definition: pci.c:53
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
u32 vlib_pci_get_num_msix_interrupts(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:177
u32 physmem_map_index
Definition: buffer.h:430
static int directory_exists(char *path)
Definition: pci.c:387
u32 file_descriptor
Definition: file.h:54
static clib_error_t * scan_pci_addr(void *arg, u8 *dev_dir_name, u8 *ignored)
Definition: pci.c:1404
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:592
static const char uio_drv_name[]
Definition: vmbus.c:39
static void vlib_pci_free_device_info(vlib_pci_device_info_t *di)
Definition: pci.h:114
static int pci_addr_cmp(void *v1, void *v2)
Definition: pci.c:1427
vlib_buffer_main_t * buffer_main
Definition: main.h:183
static clib_error_t * linux_pci_uio_read_ready(clib_file_t *uf)
Definition: pci.c:648
clib_error_t * vlib_pci_enable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
Definition: pci.c:894
vlib_main_t * vm
Definition: in2out_ed.c:1582
void() pci_msix_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle, u16 line)
Definition: pci.h:132
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
vl_api_fib_path_t path
Definition: mfib_types.api:34
vlib_physmem_map_t * vlib_physmem_get_map(vlib_main_t *vm, u32 index)
Definition: physmem.c:87
#define pci_log_err(vm, dev, f,...)
Definition: pci.c:66
vlib_pci_addr_t addr
Definition: pci.c:99
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:252
vhost_vring_addr_t addr
Definition: vhost_user.h:111
u8 supports_va_dma
Definition: pci.c:126
clib_error_t * clib_sysfs_write(char *file_name, char *fmt,...)
Definition: sysfs.c:26
static clib_error_t * linux_pci_vfio_error_ready(clib_file_t *uf)
Definition: pci.c:722
unsigned char u8
Definition: types.h:56
void * addr
Definition: pci.c:73
u8 data[128]
Definition: ipsec_types.api:89
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
clib_file_function_t * read_function
Definition: file.h:67
clib_error_t * vlib_pci_read_write_config(vlib_main_t *vm, vlib_pci_dev_handle_t h, vlib_read_or_write_t read_or_write, uword address, void *data, u32 n_bytes)
Definition: pci.c:1022
static clib_error_t * linux_pci_vfio_intx_read_ready(clib_file_t *uf)
Definition: pci.c:702
static clib_error_t * vlib_pci_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 bar, int *fd, u64 *size, u64 *offset)
Definition: pci.c:1043
int fd
Definition: pci.c:79
static void pci_config_type1_little_to_host(pci_config_type1_regs_t *r)
Definition: pci_config.h:353
clib_error_t * vlib_pci_map_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h, void *ptr)
Definition: pci.c:1218
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:173
static clib_error_t * linux_pci_vfio_msix_read_ready(clib_file_t *uf)
Definition: pci.c:683
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
#define clib_error_return(e, args...)
Definition: error.h:99
vlib_pci_addr_t * vlib_pci_get_all_dev_addrs()
Definition: pci.c:1452
clib_file_main_t file_main
Definition: main.c:63
static char * sysfs_mod_vfio_noiommu
Definition: pci.c:60
vlib_main_t * vlib_main
Definition: pci.c:133
unsigned int u32
Definition: types.h:88
linux_pci_irq_t * msix_irqs
Definition: pci.c:121
#define clib_error_create(args...)
Definition: error.h:96
clib_error_t * linux_vfio_group_get_device_fd(vlib_pci_addr_t *addr, int *fdp, int *is_noiommu)
Definition: vfio.c:177
pci_config_type1_regs_t config1
Definition: pci.h:88
vlib_pci_dev_handle_t handle
Definition: pci.c:98
void unformat_init_string(unformat_input_t *input, char *string, int string_len)
Definition: unformat.c:1029
uword private_data
Definition: pci.c:124
vlib_pci_device_info_t * vlib_pci_get_device_info(vlib_main_t *vm, vlib_pci_addr_t *addr, clib_error_t **error)
Definition: pci.c:202
u8 config_data[256]
Definition: pci.h:89
u8 * description
Definition: file.h:70
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
u32 clib_file_index
Definition: pci.c:80
static const char * sysfs_pci_drv_path
Definition: pci.c:59
void vlib_pci_set_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h, uword private_data)
Definition: pci.c:155
clib_error_t * vlib_pci_bind_to_uio(vlib_main_t *vm, vlib_pci_addr_t *addr, char *uio_drv_name)
Definition: pci.c:397
static clib_error_t * scan_uio_dir(void *arg, u8 *path_name, u8 *file_name)
Definition: pci.c:577
struct _unformat_input_t unformat_input_t
unsigned short u16
Definition: types.h:57
u8 data_len
Definition: ikev2_types.api:24
#define clib_error_return_unix(e, args...)
Definition: error.h:102
u32 size
Definition: vhost_user.h:106
vec_header_t h
Definition: buffer.c:322
void di(unformat_input_t *i)
Definition: unformat.c:163
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:302
vlib_buffer_pool_t * buffer_pools
Definition: buffer.h:454
clib_error_t * vlib_pci_register_msix_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 start, u32 count, pci_msix_handler_function_t *msix_handler)
Definition: pci.c:837
linux_pci_main_t linux_pci_main
Definition: pci.c:199
clib_error_t * linux_pci_init(vlib_main_t *vm)
Definition: pci.c:1470
void init_device_from_registered(vlib_main_t *vm, vlib_pci_device_info_t *di)
Definition: pci.c:1365
uword vlib_pci_get_private_data(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:148
u32 vlib_pci_dev_handle_t
Definition: pci.h:97
clib_error_t * clib_sysfs_read(char *file_name, char *fmt,...)
Definition: sysfs.c:50
u8 len
Definition: ip_types.api:92
linux_pci_device_type_t
Definition: pci.c:88
u8 * format_vlib_pci_addr(u8 *s, va_list *va)
Definition: pci.c:141
static void pci_config_type0_little_to_host(pci_config_type0_regs_t *r)
Definition: pci_config.h:279
u32 vlib_pci_get_numa_node(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:170
#define clib_strncpy(d, s, n)
Definition: string.h:978
#define VLIB_PCI_DEVICE_INFO_F_NOIOMMU
Definition: pci.h:63
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:380
u8 * driver_name
Definition: pci.h:82
#define pci_log_debug(vm, dev, f,...)
Definition: pci.c:63
clib_error_t * vlib_pci_read_write_io(vlib_main_t *vm, vlib_pci_dev_handle_t h, vlib_read_or_write_t read_or_write, uword offset, void *data, u32 length)
Definition: pci.c:1198
void() pci_intx_handler_function_t(vlib_main_t *vm, vlib_pci_dev_handle_t handle)
Definition: pci.h:130
clib_error_t * vlib_pci_disable_msix_irq(vlib_main_t *vm, vlib_pci_dev_handle_t h, u16 start, u16 count)
Definition: pci.c:916
pci_msix_handler_function_t * msix_handler
Definition: pci.c:84
#define PCI_COMMAND_MEMORY
Definition: pci_config.h:182
vlib_pci_addr_t * vlib_pci_get_addr(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:163
size_t size
Definition: pci.c:74
#define ASSERT(truth)
static clib_error_t * vfio_set_irqs(vlib_main_t *vm, linux_pci_device_t *p, u32 index, u32 start, u32 count, u32 flags, int *efds)
Definition: pci.c:592
manual_print typedef address
Definition: ip_types.api:85
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
Definition: file.h:96
static void clib_file_del_by_index(clib_file_main_t *um, uword index)
Definition: file.h:119
static clib_error_t * linux_pci_uio_error_ready(clib_file_t *uf)
Definition: pci.c:675
pci_config_type0_regs_t config0
Definition: pci.h:87
static void clib_mem_free(void *p)
Definition: mem.h:215
#define clib_error_report(e)
Definition: error.h:113
clib_error_t * vfio_map_physmem_page(vlib_main_t *vm, void *addr)
Definition: vfio.c:38
vlib_pci_addr_t addr
Definition: pci.h:66
clib_error_t * vlib_pci_register_intx_handler(vlib_main_t *vm, vlib_pci_dev_handle_t h, pci_intx_handler_function_t *intx_handler)
Definition: pci.c:786
static void * clib_mem_alloc(uword size)
Definition: mem.h:157
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
static uword clib_bitmap_count_set_bits(uword *ai)
Return the number of set bits in a bitmap.
Definition: bitmap.h:462
template key/value backing page structure
Definition: bihash_doc.h:44
Definition: defs.h:64
static clib_error_t * vlib_pci_map_region_int(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 bar, u8 *addr, void **result)
Definition: pci.c:1118
static clib_error_t * add_device_vfio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
Definition: pci.c:934
clib_error_t * foreach_directory_file(char *dir_name, clib_error_t *(*f)(void *arg, u8 *path_name, u8 *file_name), void *arg, int scan_dirs)
Definition: util.c:49
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
uword unformat_vlib_pci_addr(unformat_input_t *input, va_list *args)
Definition: pci.c:124
u64 uword
Definition: types.h:112
#define vec_sort_with_function(vec, f)
Sort a vector using the supplied element comparison function.
Definition: vec.h:1055
static void unformat_free(unformat_input_t *i)
Definition: format.h:163
#define clib_unix_warning(format, args...)
Definition: error.h:68
u32 index
Definition: flow_types.api:221
#define clib_error_free(e)
Definition: error.h:86
clib_error_t * vlib_pci_io_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource)
Definition: pci.c:1182
clib_error_t * vlib_pci_map_region(vlib_main_t *vm, vlib_pci_dev_handle_t h, u32 resource, void **result)
Definition: pci.c:1168
clib_file_function_t * error_function
Definition: file.h:67
linux_pci_region_t * regions
Definition: pci.c:103
pci_intx_handler_function_t * intx_handler
Definition: pci.c:83
struct clib_bihash_value offset
template key/value backing page structure
u16 device_id
Definition: pci.h:127
int vlib_pci_supports_virtual_addr_dma(vlib_main_t *vm, vlib_pci_dev_handle_t h)
Definition: pci.c:1229
struct _pci_device_registration pci_device_registration_t
uword clib_bitmap_t
Definition: bitmap.h:50
#define vec_foreach(var, vec)
Vector iterator.
uword private_data
Definition: file.h:64
format_function_t format_vfio_region_info
Definition: vfio.h:52
Definition: file.h:51
static clib_error_t * add_device_uio(vlib_main_t *vm, linux_pci_device_t *p, vlib_pci_device_info_t *di, pci_device_registration_t *r)
Definition: pci.c:730
#define vec_validate_init_empty(V, I, INIT)
Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment)
Definition: vec.h:556
u16 device_class
Definition: pci.h:72
linux_pci_device_t * linux_pci_devices
Definition: pci.c:134
vlib_main_t * vlib_main
Definition: pci.h:155
clib_error_t * vlib_pci_device_open(vlib_main_t *vm, vlib_pci_addr_t *addr, pci_device_id_t ids[], vlib_pci_dev_handle_t *handle)
Definition: pci.c:1237
pci_config_header_t header
Definition: pci_config.h:247
u64 config_offset
Definition: pci.c:107
#define VLIB_INITS(...)
Definition: init.h:357
u8 * product_name
Definition: pci.h:77
u16 vendor_id
Definition: pci.h:127
uword unformat(unformat_input_t *i, const char *fmt,...)
Definition: unformat.c:978
u8 * clib_sysfs_link_to_name(char *link)
Definition: sysfs.c:90
static linux_pci_device_t * linux_pci_get_device(vlib_pci_dev_handle_t h)
Definition: pci.c:141