vlib: PCI rework to support VFIO
[vpp.git] / src / vlib / linux / pci.c
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
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <dirent.h>
50 #include <sys/ioctl.h>
51 #include <net/if.h>
52 #include <linux/ethtool.h>
53 #include <linux/sockios.h>
54 #include <linux/vfio.h>
55 #include <sys/eventfd.h>
56
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
60 typedef struct
61 {
62   vlib_pci_dev_handle_t handle;
63   vlib_pci_addr_t addr;
64
65   /* Resource file descriptors. */
66   int *resource_fds;
67
68   /* File descriptor for config space read/write. */
69   int config_fd;
70
71   /* File descriptor for /dev/uio%d */
72   int uio_fd;
73
74   /* Minor device for uio device. */
75   u32 uio_minor;
76
77   /* vfio */
78   int vfio_device_fd;
79
80   /* Index given by clib_file_add. */
81   u32 clib_file_index;
82
83   /* Interrupt handler */
84   void (*interrupt_handler) (vlib_pci_dev_handle_t h);
85
86   /* private data */
87   uword private_data;
88
89 } linux_pci_device_t;
90
91 typedef struct
92 {
93   int group;
94   int fd;
95   int refcnt;
96 } linux_pci_vfio_iommu_group_t;
97
98 /* Pool of PCI devices. */
99 typedef struct
100 {
101   vlib_main_t *vlib_main;
102   linux_pci_device_t *linux_pci_devices;
103
104   /* VFIO */
105   int vfio_container_fd;
106   int vfio_iommu_mode;
107
108   /* pool of IOMMU groups */
109   linux_pci_vfio_iommu_group_t *iommu_groups;
110
111   /* iommu group pool index by group id  hash */
112   uword *iommu_pool_index_by_group;
113
114 } linux_pci_main_t;
115
116 extern linux_pci_main_t linux_pci_main;
117
118 linux_pci_device_t *
119 linux_pci_get_device (vlib_pci_dev_handle_t h)
120 {
121   linux_pci_main_t *lpm = &linux_pci_main;
122   return pool_elt_at_index (lpm->linux_pci_devices, h);
123 }
124
125 uword
126 vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
127 {
128   linux_pci_device_t *d = linux_pci_get_device (h);
129   return d->private_data;
130 }
131
132 void
133 vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
134 {
135   linux_pci_device_t *d = linux_pci_get_device (h);
136   d->private_data = private_data;
137 }
138
139 vlib_pci_addr_t *
140 vlib_pci_get_addr (vlib_pci_dev_handle_t h)
141 {
142   linux_pci_device_t *lpm = linux_pci_get_device (h);
143   return &lpm->addr;
144 }
145
146 /* Call to allocate/initialize the pci subsystem.
147    This is not an init function so that users can explicitly enable
148    pci only when it's needed. */
149 clib_error_t *pci_bus_init (vlib_main_t * vm);
150
151 linux_pci_main_t linux_pci_main;
152
153 vlib_pci_device_info_t *
154 vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
155 {
156   linux_pci_main_t *lpm = &linux_pci_main;
157   clib_error_t *err;
158   vlib_pci_device_info_t *di;
159   u8 *f = 0;
160   u32 tmp;
161   int fd;
162
163   di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
164   memset (di, 0, sizeof (vlib_pci_device_info_t));
165   di->addr.as_u32 = addr->as_u32;
166
167   u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
168                              format_vlib_pci_addr, addr);
169
170   f = format (0, "%v/config%c", dev_dir_name, 0);
171   fd = open ((char *) f, O_RDWR);
172
173   /* Try read-only access if write fails. */
174   if (fd < 0)
175     fd = open ((char *) f, O_RDONLY);
176
177   if (fd < 0)
178     {
179       err = clib_error_return_unix (0, "open `%s'", f);
180       goto error;
181     }
182
183   /* You can only read more that 64 bytes of config space as root; so we try to
184      read the full space but fall back to just the first 64 bytes. */
185   if (read (fd, &di->config_data, sizeof (di->config_data)) !=
186       sizeof (di->config_data)
187       && read (fd, &di->config0,
188                sizeof (di->config0)) != sizeof (di->config0))
189     {
190       err = clib_error_return_unix (0, "read `%s'", f);
191       close (fd);
192       goto error;
193     }
194
195   {
196     static pci_config_header_t all_ones;
197     if (all_ones.vendor_id == 0)
198       memset (&all_ones, ~0, sizeof (all_ones));
199
200     if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
201       {
202         err = clib_error_return (0, "invalid PCI config for `%s'", f);
203         close (fd);
204         goto error;
205       }
206   }
207
208   if (di->config0.header.header_type == 0)
209     pci_config_type0_little_to_host (&di->config0);
210   else
211     pci_config_type1_little_to_host (&di->config1);
212
213   di->numa_node = -1;
214   vec_reset_length (f);
215   f = format (f, "%v/numa_node%c", dev_dir_name, 0);
216   err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
217   if (err)
218     {
219       di->numa_node = -1;
220       clib_error_free (err);
221     }
222
223   vec_reset_length (f);
224   f = format (f, "%v/class%c", dev_dir_name, 0);
225   err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
226   if (err)
227     goto error;
228   di->device_class = tmp >> 8;
229
230   vec_reset_length (f);
231   f = format (f, "%v/vendor%c", dev_dir_name, 0);
232   err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
233   if (err)
234     goto error;
235   di->vendor_id = tmp;
236
237   vec_reset_length (f);
238   f = format (f, "%v/device%c", dev_dir_name, 0);
239   err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
240   if (err)
241     goto error;
242   di->device_id = tmp;
243
244   vec_reset_length (f);
245   f = format (f, "%v/driver%c", dev_dir_name, 0);
246   di->driver_name = clib_sysfs_link_to_name ((char *) f);
247
248   di->iommu_group = -1;
249   if (lpm->vfio_container_fd != -1)
250     {
251       u8 *tmpstr;
252       vec_reset_length (f);
253       f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
254       tmpstr = clib_sysfs_link_to_name ((char *) f);
255       if (tmpstr)
256         {
257           di->iommu_group = atoi ((char *) tmpstr);
258           vec_free (tmpstr);
259         }
260     }
261
262   vec_reset_length (f);
263   f = format (f, "%v/vpd%c", dev_dir_name, 0);
264   fd = open ((char *) f, O_RDONLY);
265   if (fd >= 0)
266     {
267       while (1)
268         {
269           u8 tag[3];
270           u8 *data = 0;
271           int len;
272
273           if (read (fd, &tag, 3) != 3)
274             break;
275
276           if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
277             break;
278
279           len = (tag[2] << 8) | tag[1];
280           vec_validate (data, len);
281
282           if (read (fd, data, len) != len)
283             {
284               vec_free (data);
285               break;
286             }
287           if (tag[0] == 0x82)
288             di->product_name = data;
289           else if (tag[0] == 0x90)
290             di->vpd_r = data;
291           else if (tag[0] == 0x91)
292             di->vpd_w = data;
293
294           data = 0;
295         }
296       close (fd);
297     }
298
299   goto done;
300
301 error:
302   vlib_pci_free_device_info (di);
303   di = 0;
304
305 done:
306   vec_free (f);
307   vec_free (dev_dir_name);
308   if (error)
309     *error = err;
310   else
311     clib_error_free (err);
312   return di;
313 }
314
315 clib_error_t *
316 vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
317 {
318   clib_error_t *error = 0;
319   u8 *s = 0, *driver_name = 0;
320   DIR *dir = 0;
321   struct dirent *e;
322   vlib_pci_device_info_t *di;
323   int fd, clear_driver_override = 0;
324   u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
325                              format_vlib_pci_addr, addr);
326
327   di = vlib_pci_get_device_info (addr, &error);
328
329   if (error)
330     return error;
331
332   s = format (s, "%v/driver%c", dev_dir_name, 0);
333   driver_name = clib_sysfs_link_to_name ((char *) s);
334   vec_reset_length (s);
335
336   if (driver_name &&
337       ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
338        (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
339        (strcmp ("igb_uio", (char *) driver_name) == 0)))
340     goto done;
341
342   /* walk trough all linux interfaces and if interface belonging to
343      this device is founf check if interface is admin up  */
344   dir = opendir ("/sys/class/net");
345   s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
346
347   if (!dir)
348     {
349       error = clib_error_return (0, "Skipping PCI device %U: failed to "
350                                  "read /sys/class/net",
351                                  format_vlib_pci_addr, addr);
352       goto done;
353     }
354
355   fd = socket (PF_INET, SOCK_DGRAM, 0);
356   if (fd < 0)
357     {
358       error = clib_error_return_unix (0, "socket");
359       goto done;
360     }
361
362   while ((e = readdir (dir)))
363     {
364       struct ifreq ifr;
365       struct ethtool_drvinfo drvinfo;
366
367       if (e->d_name[0] == '.')  /* skip . and .. */
368         continue;
369
370       memset (&ifr, 0, sizeof ifr);
371       memset (&drvinfo, 0, sizeof drvinfo);
372       ifr.ifr_data = (char *) &drvinfo;
373       strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
374       drvinfo.cmd = ETHTOOL_GDRVINFO;
375       if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
376         {
377           /* Some interfaces (eg "lo") don't support this ioctl */
378           if ((errno != ENOTSUP) && (errno != ENODEV))
379             clib_unix_warning ("ioctl fetch intf %s bus info error",
380                                e->d_name);
381           continue;
382         }
383
384       if (strcmp ((char *) s, drvinfo.bus_info))
385         continue;
386
387       memset (&ifr, 0, sizeof (ifr));
388       strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
389       if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
390         {
391           error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
392                                           e->d_name);
393           close (fd);
394           goto done;
395         }
396
397       if (ifr.ifr_flags & IFF_UP)
398         {
399           error = clib_error_return (0, "Skipping PCI device %U as host "
400                                      "interface %s is up",
401                                      format_vlib_pci_addr, addr, e->d_name);
402           close (fd);
403           goto done;
404         }
405     }
406
407   close (fd);
408   vec_reset_length (s);
409
410   s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
411   clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
412   vec_reset_length (s);
413
414   s = format (s, "%v/driver_override%c", dev_dir_name, 0);
415   if (access ((char *) s, F_OK) == 0)
416     {
417       clib_sysfs_write ((char *) s, "%s", uio_drv_name);
418       clear_driver_override = 1;
419     }
420   else
421     {
422       vec_reset_length (s);
423       s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
424       clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
425                         di->device_id);
426     }
427   vec_reset_length (s);
428
429   s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
430   clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
431   vec_reset_length (s);
432
433   if (clear_driver_override)
434     {
435       s = format (s, "%v/driver_override%c", dev_dir_name, 0);
436       clib_sysfs_write ((char *) s, "%c", 0);
437       vec_reset_length (s);
438     }
439
440 done:
441   closedir (dir);
442   vec_free (s);
443   vec_free (dev_dir_name);
444   vec_free (driver_name);
445   return error;
446 }
447
448
449 static clib_error_t *
450 scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
451 {
452   linux_pci_device_t *l = arg;
453   unformat_input_t input;
454
455   unformat_init_string (&input, (char *) file_name, vec_len (file_name));
456
457   if (!unformat (&input, "uio%d", &l->uio_minor))
458     abort ();
459
460   unformat_free (&input);
461   return 0;
462 }
463
464 static clib_error_t *
465 linux_pci_uio_read_ready (clib_file_t * uf)
466 {
467   int __attribute__ ((unused)) rv;
468   vlib_pci_dev_handle_t h = uf->private_data;
469   linux_pci_device_t *p = linux_pci_get_device (h);
470
471   u32 icount;
472   rv = read (uf->file_descriptor, &icount, 4);
473
474   if (p->interrupt_handler)
475     p->interrupt_handler (h);
476
477   vlib_pci_intr_enable (h);
478
479   return /* no error */ 0;
480 }
481
482 static clib_error_t *
483 linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
484 {
485   clib_error_t *err = 0;
486   struct vfio_irq_set i = {
487     .argsz = sizeof (struct vfio_irq_set),
488     .start = 0,
489     .count = 1,
490     .index = VFIO_PCI_INTX_IRQ_INDEX,
491     .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
492   };
493
494   if (ioctl (d->vfio_device_fd, VFIO_DEVICE_SET_IRQS, &i) < 0)
495     {
496       err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
497                                     format_vlib_pci_addr, &d->addr);
498     }
499   return err;
500 }
501
502 static clib_error_t *
503 linux_pci_uio_error_ready (clib_file_t * uf)
504 {
505   u32 error_index = (u32) uf->private_data;
506
507   return clib_error_return (0, "pci device %d: error", error_index);
508 }
509
510 static clib_error_t *
511 linux_pci_vfio_read_ready (clib_file_t * uf)
512 {
513   int __attribute__ ((unused)) rv;
514   vlib_pci_dev_handle_t h = uf->private_data;
515   linux_pci_device_t *p = linux_pci_get_device (h);
516
517   u64 icount;
518   rv = read (uf->file_descriptor, &icount, sizeof (icount));
519
520   if (p->interrupt_handler)
521     p->interrupt_handler (h);
522
523   linux_pci_vfio_unmask_intx (p);
524
525   return /* no error */ 0;
526 }
527
528 static clib_error_t *
529 linux_pci_vfio_error_ready (clib_file_t * uf)
530 {
531   u32 error_index = (u32) uf->private_data;
532
533   return clib_error_return (0, "pci device %d: error", error_index);
534 }
535
536 static clib_error_t *
537 add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
538                 vlib_pci_device_info_t * di, pci_device_registration_t * r)
539 {
540   clib_error_t *err = 0;
541   clib_file_t t = { 0 };
542   u8 *s = 0;
543
544   p->addr.as_u32 = di->addr.as_u32;
545   p->uio_fd = -1;
546
547   s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
548               format_vlib_pci_addr, &di->addr, 0);
549
550   p->config_fd = open ((char *) s, O_RDWR);
551   vec_reset_length (s);
552
553   if (p->config_fd == -1)
554     {
555       err = clib_error_return_unix (0, "open '%s'", s);
556       goto error;
557     }
558
559   s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
560               format_vlib_pci_addr, &di->addr);
561   foreach_directory_file ((char *) s, scan_uio_dir, p,  /* scan_dirs */
562                           1);
563   vec_reset_length (s);
564
565   s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
566   p->uio_fd = open ((char *) s, O_RDWR);
567   if (p->uio_fd < 0)
568     {
569       err = clib_error_return_unix (0, "open '%s'", s);
570       goto error;
571     }
572
573   t.read_function = linux_pci_uio_read_ready;
574   t.file_descriptor = p->uio_fd;
575   t.error_function = linux_pci_uio_error_ready;
576   t.private_data = p->handle;
577
578   p->clib_file_index = clib_file_add (&file_main, &t);
579   p->interrupt_handler = r->interrupt_handler;
580   err = r->init_function (vm, p->handle);
581
582 error:
583   free (s);
584   if (err)
585     {
586       close (p->config_fd);
587       close (p->uio_fd);
588     }
589   return err;
590 }
591
592 static linux_pci_vfio_iommu_group_t *
593 get_vfio_iommu_group (int group)
594 {
595   linux_pci_main_t *lpm = &linux_pci_main;
596   uword *p;
597
598   p = hash_get (lpm->iommu_pool_index_by_group, group);
599
600   return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
601 }
602
603 static clib_error_t *
604 open_vfio_iommu_group (int group)
605 {
606   linux_pci_main_t *lpm = &linux_pci_main;
607   linux_pci_vfio_iommu_group_t *g;
608   clib_error_t *err = 0;
609   struct vfio_group_status group_status;
610   u8 *s = 0;
611   int fd;
612
613   g = get_vfio_iommu_group (group);
614   if (g)
615     {
616       g->refcnt++;
617       return 0;
618     }
619   s = format (s, "/dev/vfio/%u%c", group, 0);
620   fd = open ((char *) s, O_RDWR);
621   if (fd < 0)
622     clib_error_return_unix (0, "open '%s'", s);
623
624   group_status.argsz = sizeof (group_status);
625   if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
626     {
627       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
628                                     s);
629       goto error;
630     }
631
632   if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
633     {
634       err = clib_error_return (0, "iommu group %d is not viable (not all "
635                                "devices in this group bound to vfio-pci)",
636                                group);
637       goto error;
638     }
639
640   if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
641     {
642       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
643                                     s);
644       goto error;
645     }
646
647   if (lpm->vfio_iommu_mode == 0)
648     {
649       if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
650           0)
651         {
652           err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
653                                         "'/dev/vfio/vfio'");
654           goto error;
655         }
656       lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
657     }
658
659
660   pool_get (lpm->iommu_groups, g);
661   g->fd = fd;
662   g->refcnt = 1;
663   hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
664   vec_free (s);
665   return 0;
666 error:
667   close (fd);
668   return err;
669 }
670
671 static clib_error_t *
672 add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
673                  vlib_pci_device_info_t * di, pci_device_registration_t * r)
674 {
675   linux_pci_vfio_iommu_group_t *g;
676   struct vfio_device_info device_info;
677   struct vfio_irq_info irq_info = { 0 };
678   clib_error_t *err = 0;
679   u8 *s = 0;
680   int dfd;
681
682   p->addr.as_u32 = di->addr.as_u32;
683
684   if (di->driver_name == 0 ||
685       (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
686     return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
687                               "vfio-pci", format_vlib_pci_addr, &di->addr,
688                               di->iommu_group);
689
690   if ((err = open_vfio_iommu_group (di->iommu_group)))
691     return err;
692
693   g = get_vfio_iommu_group (di->iommu_group);
694
695   s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
696   if ((dfd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
697     {
698       err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
699                                     format_vlib_pci_addr, &di->addr);
700       goto error;
701     }
702   vec_reset_length (s);
703   p->vfio_device_fd = dfd;
704
705   device_info.argsz = sizeof (device_info);
706   if (ioctl (dfd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
707     {
708       err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
709                                     format_vlib_pci_addr, &di->addr);
710       goto error;
711     }
712
713   irq_info.argsz = sizeof (struct vfio_irq_info);
714   irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
715   if (ioctl (dfd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
716     {
717       err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
718                                     "'%U'", format_vlib_pci_addr, &di->addr);
719       goto error;
720     }
721
722   /* reset if device supports it */
723   if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
724     if (ioctl (dfd, VFIO_DEVICE_RESET) < 0)
725       {
726         err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
727                                       format_vlib_pci_addr, &di->addr);
728         goto error;
729       }
730
731   if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) && irq_info.count == 1)
732     {
733       u8 buf[sizeof (struct vfio_irq_set) + sizeof (int)] = { 0 };
734       struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
735       clib_file_t t = { 0 };
736       int efd = eventfd (0, EFD_NONBLOCK);
737
738       irq_set->argsz = sizeof (buf);
739       irq_set->count = 1;
740       irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
741       irq_set->flags =
742         VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
743       clib_memcpy (&irq_set->data, &efd, sizeof (int));
744
745       if (ioctl (dfd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
746         {
747           err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
748                                         format_vlib_pci_addr, &di->addr);
749           goto error;
750         }
751
752       t.read_function = linux_pci_vfio_read_ready;
753       t.file_descriptor = efd;
754       t.error_function = linux_pci_vfio_error_ready;
755       t.private_data = p->handle;
756       p->clib_file_index = clib_file_add (&file_main, &t);
757
758       /* unmask the interrupt */
759       linux_pci_vfio_unmask_intx (p);
760     }
761
762   p->interrupt_handler = r->interrupt_handler;
763
764   s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
765               format_vlib_pci_addr, &di->addr, 0);
766
767   p->config_fd = open ((char *) s, O_RDWR);
768   vec_reset_length (s);
769
770   if (p->config_fd == -1)
771     {
772       err = clib_error_return_unix (0, "open '%s'", s);
773       goto error;
774     }
775
776   err = r->init_function (vm, p->handle);
777
778 error:
779   vec_free (s);
780   if (err)
781     {
782       close (dfd);
783       close (p->config_fd);
784     }
785   return err;
786 }
787
788 /* Configuration space read/write. */
789 clib_error_t *
790 vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
791                             vlib_read_or_write_t read_or_write,
792                             uword address, void *data, u32 n_bytes)
793 {
794   linux_pci_device_t *p = linux_pci_get_device (h);
795   int n;
796
797   if (read_or_write == VLIB_READ)
798     n = pread (p->config_fd, data, n_bytes, address);
799   else
800     n = pwrite (p->config_fd, data, n_bytes, address);
801
802   if (n != n_bytes)
803     return clib_error_return_unix (0, "%s",
804                                    read_or_write == VLIB_READ
805                                    ? "read" : "write");
806
807   return 0;
808 }
809
810 static clib_error_t *
811 vlib_pci_map_resource_int (vlib_pci_dev_handle_t h,
812                            u32 resource, u8 * addr, void **result)
813 {
814   linux_pci_device_t *p = linux_pci_get_device (h);
815   struct stat stat_buf;
816   u8 *file_name;
817   int fd;
818   clib_error_t *error;
819   int flags = MAP_SHARED;
820
821   error = 0;
822
823   file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
824                       format_vlib_pci_addr, &p->addr, resource, 0);
825
826   fd = open ((char *) file_name, O_RDWR);
827   if (fd < 0)
828     {
829       error = clib_error_return_unix (0, "open `%s'", file_name);
830       goto done;
831     }
832
833   if (fstat (fd, &stat_buf) < 0)
834     {
835       error = clib_error_return_unix (0, "fstat `%s'", file_name);
836       goto done;
837     }
838
839   vec_validate (p->resource_fds, resource);
840   p->resource_fds[resource] = fd;
841   if (addr != 0)
842     flags |= MAP_FIXED;
843
844   *result = mmap (addr,
845                   /* size */ stat_buf.st_size,
846                   PROT_READ | PROT_WRITE, flags,
847                   /* file */ fd,
848                   /* offset */ 0);
849   if (*result == (void *) -1)
850     {
851       error = clib_error_return_unix (0, "mmap `%s'", file_name);
852       goto done;
853     }
854
855 done:
856   if (error)
857     {
858       if (fd >= 0)
859         close (fd);
860     }
861   vec_free (file_name);
862   return error;
863 }
864
865 clib_error_t *
866 vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource, void **result)
867 {
868   return (vlib_pci_map_resource_int (h, resource, 0 /* addr */ , result));
869 }
870
871 clib_error_t *
872 vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
873                              u32 resource, u8 * addr, void **result)
874 {
875   return (vlib_pci_map_resource_int (h, resource, addr, result));
876 }
877
878 void
879 init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
880 {
881   vlib_pci_main_t *pm = &pci_main;
882   linux_pci_main_t *lpm = &linux_pci_main;
883   pci_device_registration_t *r;
884   pci_device_id_t *i;
885   clib_error_t *err = 0;
886   linux_pci_device_t *p;
887
888   pool_get (lpm->linux_pci_devices, p);
889   p->handle = p - lpm->linux_pci_devices;
890
891   r = pm->pci_device_registrations;
892
893   while (r)
894     {
895       for (i = r->supported_devices; i->vendor_id != 0; i++)
896         if (i->vendor_id == di->config0.header.vendor_id &&
897             i->device_id == di->config0.header.device_id)
898           {
899             if (di->iommu_group != -1)
900               err = add_device_vfio (vm, p, di, r);
901             else
902               err = add_device_uio (vm, p, di, r);
903
904             if (err)
905               clib_error_report (err);
906             else
907               return;
908           }
909       r = r->next_registration;
910     }
911
912   /* No driver, close the PCI config-space FD */
913   memset (p, 0, sizeof (linux_pci_device_t));
914   pool_put (lpm->linux_pci_devices, p);
915 }
916
917 static clib_error_t *
918 scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
919 {
920   vlib_pci_addr_t addr, **addrv = arg;
921   unformat_input_t input;
922   clib_error_t *err = 0;
923
924   unformat_init_string (&input, (char *) dev_dir_name,
925                         vec_len (dev_dir_name));
926
927   if (!unformat (&input, "/sys/bus/pci/devices/%U",
928                  unformat_vlib_pci_addr, &addr))
929     err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
930
931   unformat_free (&input);
932
933   if (err)
934     return err;
935
936   vec_add1 (*addrv, addr);
937   return 0;
938 }
939
940 static int
941 pci_addr_cmp (void *v1, void *v2)
942 {
943   vlib_pci_addr_t *a1 = v1;
944   vlib_pci_addr_t *a2 = v2;
945
946   if (a1->domain > a2->domain)
947     return 1;
948   if (a1->domain < a2->domain)
949     return -1;
950   if (a1->bus > a2->bus)
951     return 1;
952   if (a1->bus < a2->bus)
953     return -1;
954   if (a1->slot > a2->slot)
955     return 1;
956   if (a1->slot < a2->slot)
957     return -1;
958   if (a1->function > a2->function)
959     return 1;
960   if (a1->function < a2->function)
961     return -1;
962   return 0;
963 }
964
965 vlib_pci_addr_t *
966 vlib_pci_get_all_dev_addrs ()
967 {
968   vlib_pci_addr_t *addrs = 0;
969   clib_error_t *err;
970   err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
971                                 &addrs, /* scan_dirs */ 0);
972   if (err)
973     {
974       vec_free (addrs);
975       return 0;
976     }
977
978   vec_sort_with_function (addrs, pci_addr_cmp);
979
980   return addrs;
981 }
982
983 clib_error_t *
984 linux_pci_init (vlib_main_t * vm)
985 {
986   vlib_pci_main_t *pm = &pci_main;
987   linux_pci_main_t *lpm = &linux_pci_main;
988   vlib_pci_addr_t *addr = 0, *addrs;
989   clib_error_t *error;
990   int fd;
991
992   pm->vlib_main = vm;
993
994   if ((error = vlib_call_init_function (vm, unix_input_init)))
995     return error;
996
997   ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
998
999   fd = open ("/dev/vfio/vfio", O_RDWR);
1000
1001   if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1002     {
1003       close (fd);
1004       fd = -1;
1005     }
1006
1007   if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1008     {
1009       close (fd);
1010       fd = -1;
1011     }
1012
1013   lpm->vfio_container_fd = fd;
1014   lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1015
1016   addrs = vlib_pci_get_all_dev_addrs ();
1017   /* *INDENT-OFF* */
1018   vec_foreach (addr, addrs)
1019     {
1020       vlib_pci_device_info_t *d;
1021       if ((d = vlib_pci_get_device_info (addr, 0)))
1022         {
1023           init_device_from_registered (vm, d);
1024           vlib_pci_free_device_info (d);
1025         }
1026     }
1027   /* *INDENT-ON* */
1028
1029   return error;
1030 }
1031
1032 VLIB_INIT_FUNCTION (linux_pci_init);
1033
1034 /*
1035  * fd.io coding-style-patch-verification: ON
1036  *
1037  * Local Variables:
1038  * eval: (c-set-style "gnu")
1039  * End:
1040  */