blob: 19be9ff8d18a2f4b71c0debc6884b254e2792ef7 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
Damjan Marion5a206ea2016-05-12 22:11:03 +02002 * Copyright (c) 2016 Cisco and/or its affiliates.
Ed Warnickecb9cada2015-12-08 15:45:58 -07003 * 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
Damjan Marion01914ce2017-09-14 19:04:50 +020040#include <vppinfra/linux/sysfs.h>
41
Ed Warnickecb9cada2015-12-08 15:45:58 -070042#include <vlib/vlib.h>
43#include <vlib/pci/pci.h>
44#include <vlib/unix/unix.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070045
46#include <sys/types.h>
47#include <sys/stat.h>
48#include <fcntl.h>
49#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020050#include <sys/ioctl.h>
51#include <net/if.h>
52#include <linux/ethtool.h>
53#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020054#include <linux/vfio.h>
55#include <sys/eventfd.h>
56
57static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
58static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010059static char *sysfs_mod_vfio_noiommu =
60 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020061
Dave Barach9b8ffd92016-07-08 08:13:45 -040062typedef struct
63{
Damjan Marioncef87f12017-10-05 15:32:41 +020064 vlib_pci_dev_handle_t handle;
65 vlib_pci_addr_t addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +020066
67 /* Resource file descriptors. */
Dave Barach9b8ffd92016-07-08 08:13:45 -040068 int *resource_fds;
Damjan Marion5a206ea2016-05-12 22:11:03 +020069
70 /* File descriptor for config space read/write. */
71 int config_fd;
72
73 /* File descriptor for /dev/uio%d */
74 int uio_fd;
75
76 /* Minor device for uio device. */
77 u32 uio_minor;
78
Damjan Marioncef87f12017-10-05 15:32:41 +020079 /* vfio */
80 int vfio_device_fd;
81
Damjan Marion56dd5432017-09-08 19:52:02 +020082 /* Index given by clib_file_add. */
83 u32 clib_file_index;
Damjan Marion5a206ea2016-05-12 22:11:03 +020084
Damjan Marioncef87f12017-10-05 15:32:41 +020085 /* Interrupt handler */
86 void (*interrupt_handler) (vlib_pci_dev_handle_t h);
87
88 /* private data */
89 uword private_data;
90
Damjan Marion5a206ea2016-05-12 22:11:03 +020091} linux_pci_device_t;
92
Damjan Marioncef87f12017-10-05 15:32:41 +020093typedef struct
94{
95 int group;
96 int fd;
97 int refcnt;
98} linux_pci_vfio_iommu_group_t;
99
Damjan Marion5a206ea2016-05-12 22:11:03 +0200100/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400101typedef struct
102{
103 vlib_main_t *vlib_main;
104 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200105
106 /* VFIO */
107 int vfio_container_fd;
108 int vfio_iommu_mode;
109
110 /* pool of IOMMU groups */
111 linux_pci_vfio_iommu_group_t *iommu_groups;
112
113 /* iommu group pool index by group id hash */
114 uword *iommu_pool_index_by_group;
115
Damjan Marion5a206ea2016-05-12 22:11:03 +0200116} linux_pci_main_t;
117
118extern linux_pci_main_t linux_pci_main;
119
Damjan Marioncef87f12017-10-05 15:32:41 +0200120linux_pci_device_t *
121linux_pci_get_device (vlib_pci_dev_handle_t h)
122{
123 linux_pci_main_t *lpm = &linux_pci_main;
124 return pool_elt_at_index (lpm->linux_pci_devices, h);
125}
126
127uword
128vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
129{
130 linux_pci_device_t *d = linux_pci_get_device (h);
131 return d->private_data;
132}
133
134void
135vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
136{
137 linux_pci_device_t *d = linux_pci_get_device (h);
138 d->private_data = private_data;
139}
140
141vlib_pci_addr_t *
142vlib_pci_get_addr (vlib_pci_dev_handle_t h)
143{
144 linux_pci_device_t *lpm = linux_pci_get_device (h);
145 return &lpm->addr;
146}
147
Damjan Marion5a206ea2016-05-12 22:11:03 +0200148/* Call to allocate/initialize the pci subsystem.
149 This is not an init function so that users can explicitly enable
150 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400151clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200152
Ed Warnickecb9cada2015-12-08 15:45:58 -0700153linux_pci_main_t linux_pci_main;
154
Damjan Marioncef87f12017-10-05 15:32:41 +0200155vlib_pci_device_info_t *
156vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
157{
158 linux_pci_main_t *lpm = &linux_pci_main;
159 clib_error_t *err;
160 vlib_pci_device_info_t *di;
161 u8 *f = 0;
162 u32 tmp;
163 int fd;
164
165 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
166 memset (di, 0, sizeof (vlib_pci_device_info_t));
167 di->addr.as_u32 = addr->as_u32;
168
169 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
170 format_vlib_pci_addr, addr);
171
172 f = format (0, "%v/config%c", dev_dir_name, 0);
173 fd = open ((char *) f, O_RDWR);
174
175 /* Try read-only access if write fails. */
176 if (fd < 0)
177 fd = open ((char *) f, O_RDONLY);
178
179 if (fd < 0)
180 {
181 err = clib_error_return_unix (0, "open `%s'", f);
182 goto error;
183 }
184
185 /* You can only read more that 64 bytes of config space as root; so we try to
186 read the full space but fall back to just the first 64 bytes. */
187 if (read (fd, &di->config_data, sizeof (di->config_data)) !=
188 sizeof (di->config_data)
189 && read (fd, &di->config0,
190 sizeof (di->config0)) != sizeof (di->config0))
191 {
192 err = clib_error_return_unix (0, "read `%s'", f);
193 close (fd);
194 goto error;
195 }
196
197 {
198 static pci_config_header_t all_ones;
199 if (all_ones.vendor_id == 0)
200 memset (&all_ones, ~0, sizeof (all_ones));
201
202 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
203 {
204 err = clib_error_return (0, "invalid PCI config for `%s'", f);
205 close (fd);
206 goto error;
207 }
208 }
209
210 if (di->config0.header.header_type == 0)
211 pci_config_type0_little_to_host (&di->config0);
212 else
213 pci_config_type1_little_to_host (&di->config1);
214
215 di->numa_node = -1;
216 vec_reset_length (f);
217 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
218 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
219 if (err)
220 {
221 di->numa_node = -1;
222 clib_error_free (err);
223 }
224
225 vec_reset_length (f);
226 f = format (f, "%v/class%c", dev_dir_name, 0);
227 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
228 if (err)
229 goto error;
230 di->device_class = tmp >> 8;
231
232 vec_reset_length (f);
233 f = format (f, "%v/vendor%c", dev_dir_name, 0);
234 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
235 if (err)
236 goto error;
237 di->vendor_id = tmp;
238
239 vec_reset_length (f);
240 f = format (f, "%v/device%c", dev_dir_name, 0);
241 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
242 if (err)
243 goto error;
244 di->device_id = tmp;
245
246 vec_reset_length (f);
247 f = format (f, "%v/driver%c", dev_dir_name, 0);
248 di->driver_name = clib_sysfs_link_to_name ((char *) f);
249
250 di->iommu_group = -1;
251 if (lpm->vfio_container_fd != -1)
252 {
253 u8 *tmpstr;
254 vec_reset_length (f);
255 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
256 tmpstr = clib_sysfs_link_to_name ((char *) f);
257 if (tmpstr)
258 {
259 di->iommu_group = atoi ((char *) tmpstr);
260 vec_free (tmpstr);
261 }
262 }
263
Damjan Marion96504182017-12-10 23:54:46 +0100264 close (fd);
265
Damjan Marioncef87f12017-10-05 15:32:41 +0200266 vec_reset_length (f);
267 f = format (f, "%v/vpd%c", dev_dir_name, 0);
268 fd = open ((char *) f, O_RDONLY);
269 if (fd >= 0)
270 {
271 while (1)
272 {
273 u8 tag[3];
274 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100275 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200276
277 if (read (fd, &tag, 3) != 3)
278 break;
279
280 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
281 break;
282
283 len = (tag[2] << 8) | tag[1];
284 vec_validate (data, len);
285
286 if (read (fd, data, len) != len)
287 {
288 vec_free (data);
289 break;
290 }
291 if (tag[0] == 0x82)
292 di->product_name = data;
293 else if (tag[0] == 0x90)
294 di->vpd_r = data;
295 else if (tag[0] == 0x91)
296 di->vpd_w = data;
297
298 data = 0;
299 }
300 close (fd);
301 }
302
303 goto done;
304
305error:
306 vlib_pci_free_device_info (di);
307 di = 0;
308
309done:
310 vec_free (f);
311 vec_free (dev_dir_name);
312 if (error)
313 *error = err;
314 else
315 clib_error_free (err);
316 return di;
317}
318
Damjan Marion2752b892017-12-11 15:55:56 +0100319static int
320directory_exists (char *path)
321{
322 struct stat s = { 0 };
323 if (stat (path, &s) == -1)
324 return 0;
325
326 return S_ISDIR (s.st_mode);
327}
328
Damjan Mariona42cd342016-04-13 18:03:20 +0200329clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200330vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700331{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400332 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200333 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200334 DIR *dir = 0;
335 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200336 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200337 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200338 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
339 format_vlib_pci_addr, addr);
340
341 di = vlib_pci_get_device_info (addr, &error);
342
343 if (error)
344 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700345
Damjan Marion2752b892017-12-11 15:55:56 +0100346 if (strncmp ("auto", uio_drv_name, 5) == 0)
347 {
348 int vfio_pci_loaded = 0;
349
350 if (directory_exists ("/sys/module/vfio_pci"))
351 vfio_pci_loaded = 1;
352
353 if (di->iommu_group != -1)
354 {
355 /* device is bound to IOMMU group */
356 if (!vfio_pci_loaded)
357 {
358 error = clib_error_return (0, "Skipping PCI device %U: device "
359 "is bound to IOMMU group and "
360 "vfio-pci driver is not loaded",
361 format_vlib_pci_addr, addr);
362 goto done;
363 }
364 else
365 uio_drv_name = "vfio-pci";
366 }
367 else
368 {
369 /* device is not bound to IOMMU group so we have multiple options */
370 if (vfio_pci_loaded &&
371 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
372 uio_drv_name = "vfio-pci";
373 else if (directory_exists ("/sys/module/uio_pci_generic"))
374 uio_drv_name = "uio_pci_generic";
375 else if (directory_exists ("/sys/module/igb_uio"))
376 uio_drv_name = "igb_uio";
377 else
378 {
379 clib_error_free (error);
380 error = clib_error_return (0, "Skipping PCI device %U: missing "
381 "kernel VFIO or UIO driver",
382 format_vlib_pci_addr, addr);
383 goto done;
384 }
385 clib_error_free (error);
386 }
387 }
388
Damjan Mariona7f74572017-05-23 18:32:21 +0200389 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200390 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200391 vec_reset_length (s);
392
Damjan Mariona7f74572017-05-23 18:32:21 +0200393 if (driver_name &&
394 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
395 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
396 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200397 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200398
Damjan Mariona42cd342016-04-13 18:03:20 +0200399 /* walk trough all linux interfaces and if interface belonging to
400 this device is founf check if interface is admin up */
401 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200402 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200403
404 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700405 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200406 error = clib_error_return (0, "Skipping PCI device %U: failed to "
407 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200408 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200409 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700410 }
411
Dave Barach9b8ffd92016-07-08 08:13:45 -0400412 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400413 if (fd < 0)
414 {
415 error = clib_error_return_unix (0, "socket");
416 goto done;
417 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200418
Dave Barach9b8ffd92016-07-08 08:13:45 -0400419 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200421 struct ifreq ifr;
422 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700423
Dave Barach9b8ffd92016-07-08 08:13:45 -0400424 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200425 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700426
Dave Barach9b8ffd92016-07-08 08:13:45 -0400427 memset (&ifr, 0, sizeof ifr);
428 memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200429 ifr.ifr_data = (char *) &drvinfo;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400430 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
Damjan Mariona42cd342016-04-13 18:03:20 +0200431 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400432 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400433 {
John Loe2821212016-07-13 18:13:02 -0400434 /* Some interfaces (eg "lo") don't support this ioctl */
435 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700436 clib_unix_warning ("ioctl fetch intf %s bus info error",
437 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400438 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200440
441 if (strcmp ((char *) s, drvinfo.bus_info))
442 continue;
443
Dave Barach9b8ffd92016-07-08 08:13:45 -0400444 memset (&ifr, 0, sizeof (ifr));
Chris Luke370e9e32016-07-07 11:01:17 -0400445 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
446 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 {
448 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
449 e->d_name);
450 close (fd);
451 goto done;
452 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200453
454 if (ifr.ifr_flags & IFF_UP)
455 {
456 error = clib_error_return (0, "Skipping PCI device %U as host "
457 "interface %s is up",
Damjan Marioncef87f12017-10-05 15:32:41 +0200458 format_vlib_pci_addr, addr, e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400459 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200460 goto done;
461 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700462 }
463
Damjan Mariona42cd342016-04-13 18:03:20 +0200464 close (fd);
465 vec_reset_length (s);
466
467 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200468 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200469 vec_reset_length (s);
470
Damjan Mariona7f74572017-05-23 18:32:21 +0200471 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
472 if (access ((char *) s, F_OK) == 0)
473 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200474 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200475 clear_driver_override = 1;
476 }
477 else
478 {
479 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200480 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
481 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
482 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200483 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200484 vec_reset_length (s);
485
Damjan Marioncef87f12017-10-05 15:32:41 +0200486 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
487 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200488 vec_reset_length (s);
489
490 if (clear_driver_override)
491 {
492 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200493 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200494 vec_reset_length (s);
495 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200496
497done:
498 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700499 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200500 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200501 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700502 return error;
503}
504
Ed Warnickecb9cada2015-12-08 15:45:58 -0700505
506static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400507scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700508{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400509 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700510 unformat_input_t input;
511
512 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
513
Dave Barach9b8ffd92016-07-08 08:13:45 -0400514 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700515 abort ();
516
517 unformat_free (&input);
518 return 0;
519}
520
Dave Barach9b8ffd92016-07-08 08:13:45 -0400521static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200522linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523{
Damjan Marion5a206ea2016-05-12 22:11:03 +0200524 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200525 vlib_pci_dev_handle_t h = uf->private_data;
526 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700527
Damjan Marion5a206ea2016-05-12 22:11:03 +0200528 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530
Damjan Marioncef87f12017-10-05 15:32:41 +0200531 if (p->interrupt_handler)
532 p->interrupt_handler (h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200533
Damjan Marioncef87f12017-10-05 15:32:41 +0200534 vlib_pci_intr_enable (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535
536 return /* no error */ 0;
537}
538
Dave Barach9b8ffd92016-07-08 08:13:45 -0400539static clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200540linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
541{
542 clib_error_t *err = 0;
543 struct vfio_irq_set i = {
544 .argsz = sizeof (struct vfio_irq_set),
545 .start = 0,
546 .count = 1,
547 .index = VFIO_PCI_INTX_IRQ_INDEX,
548 .flags = VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_ACTION_UNMASK,
549 };
550
551 if (ioctl (d->vfio_device_fd, VFIO_DEVICE_SET_IRQS, &i) < 0)
552 {
553 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
554 format_vlib_pci_addr, &d->addr);
555 }
556 return err;
557}
558
559static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200560linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700561{
562 u32 error_index = (u32) uf->private_data;
563
564 return clib_error_return (0, "pci device %d: error", error_index);
565}
566
Damjan Marioncef87f12017-10-05 15:32:41 +0200567static clib_error_t *
568linux_pci_vfio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700569{
Damjan Marioncef87f12017-10-05 15:32:41 +0200570 int __attribute__ ((unused)) rv;
571 vlib_pci_dev_handle_t h = uf->private_data;
572 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700573
Damjan Marioncef87f12017-10-05 15:32:41 +0200574 u64 icount;
575 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700576
Damjan Marioncef87f12017-10-05 15:32:41 +0200577 if (p->interrupt_handler)
578 p->interrupt_handler (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700579
Damjan Marioncef87f12017-10-05 15:32:41 +0200580 linux_pci_vfio_unmask_intx (p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700581
Damjan Marioncef87f12017-10-05 15:32:41 +0200582 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700583}
584
Damjan Marioncef87f12017-10-05 15:32:41 +0200585static clib_error_t *
586linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700587{
Damjan Marioncef87f12017-10-05 15:32:41 +0200588 u32 error_index = (u32) uf->private_data;
589
590 return clib_error_return (0, "pci device %d: error", error_index);
591}
592
593static clib_error_t *
594add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
595 vlib_pci_device_info_t * di, pci_device_registration_t * r)
596{
597 clib_error_t *err = 0;
598 clib_file_t t = { 0 };
599 u8 *s = 0;
600
601 p->addr.as_u32 = di->addr.as_u32;
602 p->uio_fd = -1;
603
604 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
605 format_vlib_pci_addr, &di->addr, 0);
606
607 p->config_fd = open ((char *) s, O_RDWR);
608 vec_reset_length (s);
609
610 if (p->config_fd == -1)
611 {
612 err = clib_error_return_unix (0, "open '%s'", s);
613 goto error;
614 }
615
616 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
617 format_vlib_pci_addr, &di->addr);
618 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
619 1);
620 vec_reset_length (s);
621
622 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
623 p->uio_fd = open ((char *) s, O_RDWR);
624 if (p->uio_fd < 0)
625 {
626 err = clib_error_return_unix (0, "open '%s'", s);
627 goto error;
628 }
629
630 t.read_function = linux_pci_uio_read_ready;
631 t.file_descriptor = p->uio_fd;
632 t.error_function = linux_pci_uio_error_ready;
633 t.private_data = p->handle;
634
635 p->clib_file_index = clib_file_add (&file_main, &t);
636 p->interrupt_handler = r->interrupt_handler;
637 err = r->init_function (vm, p->handle);
638
639error:
640 free (s);
641 if (err)
642 {
Damjan Marion96504182017-12-10 23:54:46 +0100643 if (p->config_fd != -1)
644 close (p->config_fd);
645 if (p->uio_fd != -1)
646 close (p->uio_fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200647 }
648 return err;
649}
650
651static linux_pci_vfio_iommu_group_t *
652get_vfio_iommu_group (int group)
653{
654 linux_pci_main_t *lpm = &linux_pci_main;
655 uword *p;
656
657 p = hash_get (lpm->iommu_pool_index_by_group, group);
658
659 return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
660}
661
662static clib_error_t *
663open_vfio_iommu_group (int group)
664{
665 linux_pci_main_t *lpm = &linux_pci_main;
666 linux_pci_vfio_iommu_group_t *g;
667 clib_error_t *err = 0;
668 struct vfio_group_status group_status;
669 u8 *s = 0;
670 int fd;
671
672 g = get_vfio_iommu_group (group);
673 if (g)
674 {
675 g->refcnt++;
676 return 0;
677 }
678 s = format (s, "/dev/vfio/%u%c", group, 0);
679 fd = open ((char *) s, O_RDWR);
680 if (fd < 0)
Damjan Marion96504182017-12-10 23:54:46 +0100681 return clib_error_return_unix (0, "open '%s'", s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200682
683 group_status.argsz = sizeof (group_status);
684 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
685 {
686 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
687 s);
688 goto error;
689 }
690
691 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
692 {
693 err = clib_error_return (0, "iommu group %d is not viable (not all "
694 "devices in this group bound to vfio-pci)",
695 group);
696 goto error;
697 }
698
699 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
700 {
701 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
702 s);
703 goto error;
704 }
705
706 if (lpm->vfio_iommu_mode == 0)
707 {
708 if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
709 0)
710 {
711 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
712 "'/dev/vfio/vfio'");
713 goto error;
714 }
715 lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
716 }
717
718
719 pool_get (lpm->iommu_groups, g);
720 g->fd = fd;
721 g->refcnt = 1;
722 hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
723 vec_free (s);
724 return 0;
725error:
726 close (fd);
727 return err;
728}
729
730static clib_error_t *
731add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
732 vlib_pci_device_info_t * di, pci_device_registration_t * r)
733{
734 linux_pci_vfio_iommu_group_t *g;
735 struct vfio_device_info device_info;
736 struct vfio_irq_info irq_info = { 0 };
737 clib_error_t *err = 0;
738 u8 *s = 0;
739 int dfd;
740
741 p->addr.as_u32 = di->addr.as_u32;
742
743 if (di->driver_name == 0 ||
744 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
745 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
746 "vfio-pci", format_vlib_pci_addr, &di->addr,
747 di->iommu_group);
748
749 if ((err = open_vfio_iommu_group (di->iommu_group)))
750 return err;
751
752 g = get_vfio_iommu_group (di->iommu_group);
753
754 s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
755 if ((dfd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
756 {
757 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
758 format_vlib_pci_addr, &di->addr);
759 goto error;
760 }
761 vec_reset_length (s);
762 p->vfio_device_fd = dfd;
763
764 device_info.argsz = sizeof (device_info);
765 if (ioctl (dfd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
766 {
767 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
768 format_vlib_pci_addr, &di->addr);
769 goto error;
770 }
771
772 irq_info.argsz = sizeof (struct vfio_irq_info);
773 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
774 if (ioctl (dfd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
775 {
776 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
777 "'%U'", format_vlib_pci_addr, &di->addr);
778 goto error;
779 }
780
781 /* reset if device supports it */
782 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
783 if (ioctl (dfd, VFIO_DEVICE_RESET) < 0)
784 {
785 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
786 format_vlib_pci_addr, &di->addr);
787 goto error;
788 }
789
790 if ((irq_info.flags & VFIO_IRQ_INFO_EVENTFD) && irq_info.count == 1)
791 {
792 u8 buf[sizeof (struct vfio_irq_set) + sizeof (int)] = { 0 };
793 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
794 clib_file_t t = { 0 };
795 int efd = eventfd (0, EFD_NONBLOCK);
796
797 irq_set->argsz = sizeof (buf);
798 irq_set->count = 1;
799 irq_set->index = VFIO_PCI_INTX_IRQ_INDEX;
800 irq_set->flags =
801 VFIO_IRQ_SET_DATA_EVENTFD | VFIO_IRQ_SET_ACTION_TRIGGER;
802 clib_memcpy (&irq_set->data, &efd, sizeof (int));
803
804 if (ioctl (dfd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
805 {
806 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_SET_IRQS) '%U'",
807 format_vlib_pci_addr, &di->addr);
808 goto error;
809 }
810
811 t.read_function = linux_pci_vfio_read_ready;
812 t.file_descriptor = efd;
813 t.error_function = linux_pci_vfio_error_ready;
814 t.private_data = p->handle;
815 p->clib_file_index = clib_file_add (&file_main, &t);
816
817 /* unmask the interrupt */
818 linux_pci_vfio_unmask_intx (p);
819 }
820
821 p->interrupt_handler = r->interrupt_handler;
822
823 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
824 format_vlib_pci_addr, &di->addr, 0);
825
826 p->config_fd = open ((char *) s, O_RDWR);
827 vec_reset_length (s);
828
829 if (p->config_fd == -1)
830 {
831 err = clib_error_return_unix (0, "open '%s'", s);
832 goto error;
833 }
834
835 err = r->init_function (vm, p->handle);
836
837error:
838 vec_free (s);
839 if (err)
840 {
Damjan Marion96504182017-12-10 23:54:46 +0100841 if (dfd != -1)
842 close (dfd);
843 if (p->config_fd != -1)
844 close (p->config_fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200845 }
846 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700847}
848
849/* Configuration space read/write. */
850clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200851vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +0200852 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400853 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700854{
Damjan Marioncef87f12017-10-05 15:32:41 +0200855 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700856 int n;
857
Ed Warnickecb9cada2015-12-08 15:45:58 -0700858 if (read_or_write == VLIB_READ)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200859 n = pread (p->config_fd, data, n_bytes, address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700860 else
Damjan Marion5a206ea2016-05-12 22:11:03 +0200861 n = pwrite (p->config_fd, data, n_bytes, address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700862
863 if (n != n_bytes)
864 return clib_error_return_unix (0, "%s",
865 read_or_write == VLIB_READ
866 ? "read" : "write");
867
868 return 0;
869}
870
871static clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200872vlib_pci_map_resource_int (vlib_pci_dev_handle_t h,
873 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700874{
Damjan Marioncef87f12017-10-05 15:32:41 +0200875 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700876 struct stat stat_buf;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400877 u8 *file_name;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700878 int fd;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400879 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700880 int flags = MAP_SHARED;
881
882 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700883
Damjan Marioncef87f12017-10-05 15:32:41 +0200884 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
885 format_vlib_pci_addr, &p->addr, resource, 0);
886
Ed Warnickecb9cada2015-12-08 15:45:58 -0700887 fd = open ((char *) file_name, O_RDWR);
888 if (fd < 0)
889 {
890 error = clib_error_return_unix (0, "open `%s'", file_name);
891 goto done;
892 }
893
894 if (fstat (fd, &stat_buf) < 0)
895 {
896 error = clib_error_return_unix (0, "fstat `%s'", file_name);
897 goto done;
898 }
899
900 vec_validate (p->resource_fds, resource);
901 p->resource_fds[resource] = fd;
902 if (addr != 0)
903 flags |= MAP_FIXED;
904
905 *result = mmap (addr,
906 /* size */ stat_buf.st_size,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400907 PROT_READ | PROT_WRITE, flags,
Ed Warnickecb9cada2015-12-08 15:45:58 -0700908 /* file */ fd,
909 /* offset */ 0);
910 if (*result == (void *) -1)
911 {
912 error = clib_error_return_unix (0, "mmap `%s'", file_name);
913 goto done;
914 }
915
Dave Barach9b8ffd92016-07-08 08:13:45 -0400916done:
Ed Warnickecb9cada2015-12-08 15:45:58 -0700917 if (error)
918 {
Chris Luke370e9e32016-07-07 11:01:17 -0400919 if (fd >= 0)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700920 close (fd);
921 }
922 vec_free (file_name);
923 return error;
924}
925
926clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200927vlib_pci_map_resource (vlib_pci_dev_handle_t h, u32 resource, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700928{
Damjan Marioncef87f12017-10-05 15:32:41 +0200929 return (vlib_pci_map_resource_int (h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700930}
931
932clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200933vlib_pci_map_resource_fixed (vlib_pci_dev_handle_t h,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400934 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700935{
Damjan Marioncef87f12017-10-05 15:32:41 +0200936 return (vlib_pci_map_resource_int (h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700937}
938
Dave Barach9b8ffd92016-07-08 08:13:45 -0400939void
Damjan Marioncef87f12017-10-05 15:32:41 +0200940init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700941{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400942 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200943 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400944 pci_device_registration_t *r;
945 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +0200946 clib_error_t *err = 0;
947 linux_pci_device_t *p;
948
949 pool_get (lpm->linux_pci_devices, p);
950 p->handle = p - lpm->linux_pci_devices;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700951
Damjan Marion5a206ea2016-05-12 22:11:03 +0200952 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700953
954 while (r)
955 {
956 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +0100957 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400958 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200959 if (di->iommu_group != -1)
960 err = add_device_vfio (vm, p, di, r);
961 else
962 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200963
Damjan Marioncef87f12017-10-05 15:32:41 +0200964 if (err)
965 clib_error_report (err);
966 else
967 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400968 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700969 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400970 }
Damjan Marioncef87f12017-10-05 15:32:41 +0200971
Ed Warnickecb9cada2015-12-08 15:45:58 -0700972 /* No driver, close the PCI config-space FD */
Damjan Marioncef87f12017-10-05 15:32:41 +0200973 memset (p, 0, sizeof (linux_pci_device_t));
974 pool_put (lpm->linux_pci_devices, p);
975}
976
977static clib_error_t *
978scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
979{
980 vlib_pci_addr_t addr, **addrv = arg;
981 unformat_input_t input;
982 clib_error_t *err = 0;
983
984 unformat_init_string (&input, (char *) dev_dir_name,
985 vec_len (dev_dir_name));
986
987 if (!unformat (&input, "/sys/bus/pci/devices/%U",
988 unformat_vlib_pci_addr, &addr))
989 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
990
991 unformat_free (&input);
992
993 if (err)
994 return err;
995
996 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700997 return 0;
998}
999
Damjan Marioncef87f12017-10-05 15:32:41 +02001000static int
1001pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002{
Damjan Marioncef87f12017-10-05 15:32:41 +02001003 vlib_pci_addr_t *a1 = v1;
1004 vlib_pci_addr_t *a2 = v2;
1005
1006 if (a1->domain > a2->domain)
1007 return 1;
1008 if (a1->domain < a2->domain)
1009 return -1;
1010 if (a1->bus > a2->bus)
1011 return 1;
1012 if (a1->bus < a2->bus)
1013 return -1;
1014 if (a1->slot > a2->slot)
1015 return 1;
1016 if (a1->slot < a2->slot)
1017 return -1;
1018 if (a1->function > a2->function)
1019 return 1;
1020 if (a1->function < a2->function)
1021 return -1;
1022 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023}
1024
Damjan Marioncef87f12017-10-05 15:32:41 +02001025vlib_pci_addr_t *
1026vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001027{
Damjan Marioncef87f12017-10-05 15:32:41 +02001028 vlib_pci_addr_t *addrs = 0;
1029 clib_error_t *err;
1030 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1031 &addrs, /* scan_dirs */ 0);
1032 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001033 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001034 vec_free (addrs);
1035 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001036 }
1037
Damjan Marioncef87f12017-10-05 15:32:41 +02001038 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001039
Damjan Marioncef87f12017-10-05 15:32:41 +02001040 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041}
1042
Dave Barach9b8ffd92016-07-08 08:13:45 -04001043clib_error_t *
1044linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001045{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001046 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001047 linux_pci_main_t *lpm = &linux_pci_main;
1048 vlib_pci_addr_t *addr = 0, *addrs;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001049 clib_error_t *error;
Damjan Marioncef87f12017-10-05 15:32:41 +02001050 int fd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001051
1052 pm->vlib_main = vm;
1053
1054 if ((error = vlib_call_init_function (vm, unix_input_init)))
1055 return error;
1056
Dave Barach9b8ffd92016-07-08 08:13:45 -04001057 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001058
Damjan Marioncef87f12017-10-05 15:32:41 +02001059 fd = open ("/dev/vfio/vfio", O_RDWR);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001060
Damjan Marioncef87f12017-10-05 15:32:41 +02001061 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1062 {
1063 close (fd);
1064 fd = -1;
1065 }
1066
1067 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1068 {
1069 close (fd);
1070 fd = -1;
1071 }
1072
1073 lpm->vfio_container_fd = fd;
1074 lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1075
1076 addrs = vlib_pci_get_all_dev_addrs ();
1077 /* *INDENT-OFF* */
1078 vec_foreach (addr, addrs)
1079 {
1080 vlib_pci_device_info_t *d;
1081 if ((d = vlib_pci_get_device_info (addr, 0)))
1082 {
1083 init_device_from_registered (vm, d);
1084 vlib_pci_free_device_info (d);
1085 }
1086 }
1087 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001088
1089 return error;
1090}
1091
Damjan Marion5a206ea2016-05-12 22:11:03 +02001092VLIB_INIT_FUNCTION (linux_pci_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001093
1094/*
1095 * fd.io coding-style-patch-verification: ON
1096 *
1097 * Local Variables:
1098 * eval: (c-set-style "gnu")
1099 * End:
1100 */