blob: 8aa0e29248c0e47564fe8cdd17e2a13caa835266 [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 Marion2060db82018-03-04 17:37:15 +010064 int fd;
65 void *addr;
66 size_t size;
67} linux_pci_region_t;
68
Damjan Marion599a16b2018-03-04 19:35:23 +010069typedef struct
70{
71 int fd;
72 u32 clib_file_index;
73 union
74 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010075 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010076 pci_msix_handler_function_t *msix_handler;
77 };
78} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010079
80typedef enum
81{
82 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
83 LINUX_PCI_DEVICE_TYPE_UIO,
84 LINUX_PCI_DEVICE_TYPE_VFIO,
85} linux_pci_device_type_t;
86
87typedef struct
88{
89 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +020090 vlib_pci_dev_handle_t handle;
91 vlib_pci_addr_t addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +020092
93 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +010094 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +020095
96 /* File descriptor for config space read/write. */
97 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +010098 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +020099
Damjan Marionf9a968e2018-02-25 23:26:22 +0100100 /* Device File descriptor */
101 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200102
103 /* Minor device for uio device. */
104 u32 uio_minor;
105
Damjan Marion56dd5432017-09-08 19:52:02 +0200106 /* Index given by clib_file_add. */
107 u32 clib_file_index;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200108
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100109 /* Interrupt handlers */
110 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100111 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200112
113 /* private data */
114 uword private_data;
115
Damjan Marion5a206ea2016-05-12 22:11:03 +0200116} linux_pci_device_t;
117
Damjan Marioncef87f12017-10-05 15:32:41 +0200118typedef struct
119{
120 int group;
121 int fd;
122 int refcnt;
123} linux_pci_vfio_iommu_group_t;
124
Damjan Marion5a206ea2016-05-12 22:11:03 +0200125/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400126typedef struct
127{
128 vlib_main_t *vlib_main;
129 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200130
131 /* VFIO */
132 int vfio_container_fd;
133 int vfio_iommu_mode;
134
135 /* pool of IOMMU groups */
136 linux_pci_vfio_iommu_group_t *iommu_groups;
137
138 /* iommu group pool index by group id hash */
139 uword *iommu_pool_index_by_group;
140
Damjan Marion5a206ea2016-05-12 22:11:03 +0200141} linux_pci_main_t;
142
143extern linux_pci_main_t linux_pci_main;
144
Damjan Marion599a16b2018-03-04 19:35:23 +0100145static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200146linux_pci_get_device (vlib_pci_dev_handle_t h)
147{
148 linux_pci_main_t *lpm = &linux_pci_main;
149 return pool_elt_at_index (lpm->linux_pci_devices, h);
150}
151
152uword
153vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
154{
155 linux_pci_device_t *d = linux_pci_get_device (h);
156 return d->private_data;
157}
158
159void
160vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
161{
162 linux_pci_device_t *d = linux_pci_get_device (h);
163 d->private_data = private_data;
164}
165
166vlib_pci_addr_t *
167vlib_pci_get_addr (vlib_pci_dev_handle_t h)
168{
Damjan Marion599a16b2018-03-04 19:35:23 +0100169 linux_pci_device_t *d = linux_pci_get_device (h);
170 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200171}
172
Damjan Marion5a206ea2016-05-12 22:11:03 +0200173/* Call to allocate/initialize the pci subsystem.
174 This is not an init function so that users can explicitly enable
175 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400176clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200177
Ed Warnickecb9cada2015-12-08 15:45:58 -0700178linux_pci_main_t linux_pci_main;
179
Damjan Marioncef87f12017-10-05 15:32:41 +0200180vlib_pci_device_info_t *
181vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
182{
183 linux_pci_main_t *lpm = &linux_pci_main;
184 clib_error_t *err;
185 vlib_pci_device_info_t *di;
186 u8 *f = 0;
187 u32 tmp;
188 int fd;
189
190 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
191 memset (di, 0, sizeof (vlib_pci_device_info_t));
192 di->addr.as_u32 = addr->as_u32;
193
194 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
195 format_vlib_pci_addr, addr);
196
197 f = format (0, "%v/config%c", dev_dir_name, 0);
198 fd = open ((char *) f, O_RDWR);
199
200 /* Try read-only access if write fails. */
201 if (fd < 0)
202 fd = open ((char *) f, O_RDONLY);
203
204 if (fd < 0)
205 {
206 err = clib_error_return_unix (0, "open `%s'", f);
207 goto error;
208 }
209
210 /* You can only read more that 64 bytes of config space as root; so we try to
211 read the full space but fall back to just the first 64 bytes. */
Damjan Marion44bcc202018-03-04 16:44:26 +0100212 if (read (fd, &di->config_data, sizeof (di->config_data)) <
213 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200214 {
215 err = clib_error_return_unix (0, "read `%s'", f);
216 close (fd);
217 goto error;
218 }
219
220 {
221 static pci_config_header_t all_ones;
222 if (all_ones.vendor_id == 0)
223 memset (&all_ones, ~0, sizeof (all_ones));
224
225 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
226 {
227 err = clib_error_return (0, "invalid PCI config for `%s'", f);
228 close (fd);
229 goto error;
230 }
231 }
232
233 if (di->config0.header.header_type == 0)
234 pci_config_type0_little_to_host (&di->config0);
235 else
236 pci_config_type1_little_to_host (&di->config1);
237
238 di->numa_node = -1;
239 vec_reset_length (f);
240 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
241 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
242 if (err)
243 {
244 di->numa_node = -1;
245 clib_error_free (err);
246 }
247
248 vec_reset_length (f);
249 f = format (f, "%v/class%c", dev_dir_name, 0);
250 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
251 if (err)
252 goto error;
253 di->device_class = tmp >> 8;
254
255 vec_reset_length (f);
256 f = format (f, "%v/vendor%c", dev_dir_name, 0);
257 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
258 if (err)
259 goto error;
260 di->vendor_id = tmp;
261
262 vec_reset_length (f);
263 f = format (f, "%v/device%c", dev_dir_name, 0);
264 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
265 if (err)
266 goto error;
267 di->device_id = tmp;
268
269 vec_reset_length (f);
270 f = format (f, "%v/driver%c", dev_dir_name, 0);
271 di->driver_name = clib_sysfs_link_to_name ((char *) f);
272
273 di->iommu_group = -1;
274 if (lpm->vfio_container_fd != -1)
275 {
276 u8 *tmpstr;
277 vec_reset_length (f);
278 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
279 tmpstr = clib_sysfs_link_to_name ((char *) f);
280 if (tmpstr)
281 {
282 di->iommu_group = atoi ((char *) tmpstr);
283 vec_free (tmpstr);
284 }
285 }
286
Damjan Marion96504182017-12-10 23:54:46 +0100287 close (fd);
288
Damjan Marioncef87f12017-10-05 15:32:41 +0200289 vec_reset_length (f);
290 f = format (f, "%v/vpd%c", dev_dir_name, 0);
291 fd = open ((char *) f, O_RDONLY);
292 if (fd >= 0)
293 {
294 while (1)
295 {
296 u8 tag[3];
297 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100298 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200299
300 if (read (fd, &tag, 3) != 3)
301 break;
302
303 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
304 break;
305
306 len = (tag[2] << 8) | tag[1];
307 vec_validate (data, len);
308
309 if (read (fd, data, len) != len)
310 {
311 vec_free (data);
312 break;
313 }
314 if (tag[0] == 0x82)
315 di->product_name = data;
316 else if (tag[0] == 0x90)
317 di->vpd_r = data;
318 else if (tag[0] == 0x91)
319 di->vpd_w = data;
320
321 data = 0;
322 }
323 close (fd);
324 }
325
326 goto done;
327
328error:
329 vlib_pci_free_device_info (di);
330 di = 0;
331
332done:
333 vec_free (f);
334 vec_free (dev_dir_name);
335 if (error)
336 *error = err;
337 else
338 clib_error_free (err);
339 return di;
340}
341
Damjan Marion2752b892017-12-11 15:55:56 +0100342static int
343directory_exists (char *path)
344{
345 struct stat s = { 0 };
346 if (stat (path, &s) == -1)
347 return 0;
348
349 return S_ISDIR (s.st_mode);
350}
351
Damjan Mariona42cd342016-04-13 18:03:20 +0200352clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200353vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700354{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400355 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200356 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200357 DIR *dir = 0;
358 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200359 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200360 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200361 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
362 format_vlib_pci_addr, addr);
363
364 di = vlib_pci_get_device_info (addr, &error);
365
366 if (error)
367 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700368
Damjan Marion2752b892017-12-11 15:55:56 +0100369 if (strncmp ("auto", uio_drv_name, 5) == 0)
370 {
371 int vfio_pci_loaded = 0;
372
373 if (directory_exists ("/sys/module/vfio_pci"))
374 vfio_pci_loaded = 1;
375
376 if (di->iommu_group != -1)
377 {
378 /* device is bound to IOMMU group */
379 if (!vfio_pci_loaded)
380 {
381 error = clib_error_return (0, "Skipping PCI device %U: device "
382 "is bound to IOMMU group and "
383 "vfio-pci driver is not loaded",
384 format_vlib_pci_addr, addr);
385 goto done;
386 }
387 else
388 uio_drv_name = "vfio-pci";
389 }
390 else
391 {
392 /* device is not bound to IOMMU group so we have multiple options */
393 if (vfio_pci_loaded &&
394 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
395 uio_drv_name = "vfio-pci";
396 else if (directory_exists ("/sys/module/uio_pci_generic"))
397 uio_drv_name = "uio_pci_generic";
398 else if (directory_exists ("/sys/module/igb_uio"))
399 uio_drv_name = "igb_uio";
400 else
401 {
402 clib_error_free (error);
403 error = clib_error_return (0, "Skipping PCI device %U: missing "
404 "kernel VFIO or UIO driver",
405 format_vlib_pci_addr, addr);
406 goto done;
407 }
408 clib_error_free (error);
409 }
410 }
411
Damjan Mariona7f74572017-05-23 18:32:21 +0200412 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200413 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200414 vec_reset_length (s);
415
Damjan Mariona7f74572017-05-23 18:32:21 +0200416 if (driver_name &&
417 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
418 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
419 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200420 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200421
Damjan Mariona42cd342016-04-13 18:03:20 +0200422 /* walk trough all linux interfaces and if interface belonging to
423 this device is founf check if interface is admin up */
424 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200425 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200426
427 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700428 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200429 error = clib_error_return (0, "Skipping PCI device %U: failed to "
430 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200431 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200432 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700433 }
434
Dave Barach9b8ffd92016-07-08 08:13:45 -0400435 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400436 if (fd < 0)
437 {
438 error = clib_error_return_unix (0, "socket");
439 goto done;
440 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200441
Dave Barach9b8ffd92016-07-08 08:13:45 -0400442 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700443 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200444 struct ifreq ifr;
445 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700446
Dave Barach9b8ffd92016-07-08 08:13:45 -0400447 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200448 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700449
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 memset (&ifr, 0, sizeof ifr);
451 memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200452 ifr.ifr_data = (char *) &drvinfo;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400453 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
Damjan Mariona42cd342016-04-13 18:03:20 +0200454 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400455 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400456 {
John Loe2821212016-07-13 18:13:02 -0400457 /* Some interfaces (eg "lo") don't support this ioctl */
458 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700459 clib_unix_warning ("ioctl fetch intf %s bus info error",
460 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400461 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400462 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200463
464 if (strcmp ((char *) s, drvinfo.bus_info))
465 continue;
466
Dave Barach9b8ffd92016-07-08 08:13:45 -0400467 memset (&ifr, 0, sizeof (ifr));
Chris Luke370e9e32016-07-07 11:01:17 -0400468 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
469 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400470 {
471 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
472 e->d_name);
473 close (fd);
474 goto done;
475 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200476
477 if (ifr.ifr_flags & IFF_UP)
478 {
479 error = clib_error_return (0, "Skipping PCI device %U as host "
480 "interface %s is up",
Damjan Marioncef87f12017-10-05 15:32:41 +0200481 format_vlib_pci_addr, addr, e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400482 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200483 goto done;
484 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700485 }
486
Damjan Mariona42cd342016-04-13 18:03:20 +0200487 close (fd);
488 vec_reset_length (s);
489
490 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200491 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200492 vec_reset_length (s);
493
Damjan Mariona7f74572017-05-23 18:32:21 +0200494 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
495 if (access ((char *) s, F_OK) == 0)
496 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200497 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200498 clear_driver_override = 1;
499 }
500 else
501 {
502 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200503 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
504 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
505 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200506 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200507 vec_reset_length (s);
508
Damjan Marioncef87f12017-10-05 15:32:41 +0200509 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
510 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200511 vec_reset_length (s);
512
513 if (clear_driver_override)
514 {
515 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200516 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200517 vec_reset_length (s);
518 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200519
520done:
521 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700522 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200523 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200524 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 return error;
526}
527
Ed Warnickecb9cada2015-12-08 15:45:58 -0700528
529static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400530scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700531{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400532 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700533 unformat_input_t input;
534
535 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
536
Dave Barach9b8ffd92016-07-08 08:13:45 -0400537 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538 abort ();
539
540 unformat_free (&input);
541 return 0;
542}
543
Dave Barach9b8ffd92016-07-08 08:13:45 -0400544static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100545vfio_set_irqs (linux_pci_device_t * p, u32 index, u32 start, u32 count,
546 u32 flags, int *efds)
547{
548 int data_len = efds ? count * sizeof (int) : 0;
549 u8 buf[sizeof (struct vfio_irq_set) + data_len];
550 struct vfio_irq_info irq_info = { 0 };
551 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
552
553
554 irq_info.argsz = sizeof (struct vfio_irq_info);
555 irq_info.index = index;
556
557 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
558 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
559 "'%U'", format_vlib_pci_addr, &p->addr);
560
561 if (irq_info.count < start + count)
562 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
563 "'%U'", format_vlib_pci_addr, &p->addr);
564
565
566 if (efds)
567 {
568 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
569 clib_memcpy (&irq_set->data, efds, data_len);
570 }
571 else
572 flags |= VFIO_IRQ_SET_DATA_NONE;
573
574 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
575 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
576
577 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
578 irq_set->index = index;
579 irq_set->start = start;
580 irq_set->count = count;
581 irq_set->flags = flags;
582
583 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
584 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
585 "[index = %u, start = %u, count = %u, "
586 "flags = 0x%x]",
587 format_vlib_pci_addr, &p->addr,
588 index, start, count, flags);
589 return 0;
590}
591
592static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200593linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594{
Damjan Marion5a206ea2016-05-12 22:11:03 +0200595 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200596 vlib_pci_dev_handle_t h = uf->private_data;
597 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100598 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
Damjan Marion5a206ea2016-05-12 22:11:03 +0200600 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400601 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700602
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100603 if (irq->intx_handler)
604 irq->intx_handler (h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200605
Damjan Marioncef87f12017-10-05 15:32:41 +0200606 vlib_pci_intr_enable (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700607
608 return /* no error */ 0;
609}
610
Dave Barach9b8ffd92016-07-08 08:13:45 -0400611static clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200612linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
613{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100614 return vfio_set_irqs (d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100615 VFIO_IRQ_SET_ACTION_UNMASK, 0);
616}
617
Damjan Marioncef87f12017-10-05 15:32:41 +0200618static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200619linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700620{
621 u32 error_index = (u32) uf->private_data;
622
623 return clib_error_return (0, "pci device %d: error", error_index);
624}
625
Damjan Marioncef87f12017-10-05 15:32:41 +0200626static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100627linux_pci_vfio_msix_read_ready (clib_file_t * uf)
628{
629 int __attribute__ ((unused)) rv;
630 vlib_pci_dev_handle_t h = uf->private_data >> 16;
631 u16 line = uf->private_data & 0xffff;
632 linux_pci_device_t *p = linux_pci_get_device (h);
633 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
634
635 u64 icount;
636 rv = read (uf->file_descriptor, &icount, sizeof (icount));
637
638 if (irq->msix_handler)
639 irq->msix_handler (h, line);
640
641 return /* no error */ 0;
642}
643
644static clib_error_t *
645linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646{
Damjan Marioncef87f12017-10-05 15:32:41 +0200647 int __attribute__ ((unused)) rv;
648 vlib_pci_dev_handle_t h = uf->private_data;
649 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100650 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Damjan Marioncef87f12017-10-05 15:32:41 +0200652 u64 icount;
653 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700654
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100655 if (irq->intx_handler)
656 irq->intx_handler (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657
Damjan Marioncef87f12017-10-05 15:32:41 +0200658 linux_pci_vfio_unmask_intx (p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700659
Damjan Marioncef87f12017-10-05 15:32:41 +0200660 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700661}
662
Damjan Marioncef87f12017-10-05 15:32:41 +0200663static clib_error_t *
664linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700665{
Damjan Marioncef87f12017-10-05 15:32:41 +0200666 u32 error_index = (u32) uf->private_data;
667
668 return clib_error_return (0, "pci device %d: error", error_index);
669}
670
671static clib_error_t *
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100672add_device_uio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
673 pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200674{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100675 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200676 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200677 u8 *s = 0;
678
679 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100680 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100681 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200682
683 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
684 format_vlib_pci_addr, &di->addr, 0);
685
686 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100687 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200688 vec_reset_length (s);
689
690 if (p->config_fd == -1)
691 {
692 err = clib_error_return_unix (0, "open '%s'", s);
693 goto error;
694 }
695
696 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
697 format_vlib_pci_addr, &di->addr);
698 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
699 1);
700 vec_reset_length (s);
701
702 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100703 p->fd = open ((char *) s, O_RDWR);
704 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200705 {
706 err = clib_error_return_unix (0, "open '%s'", s);
707 goto error;
708 }
709
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100710 if (r && r->interrupt_handler)
711 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200712
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100713 if (r && r->init_function)
714 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200715
716error:
717 free (s);
718 if (err)
719 {
Damjan Marion96504182017-12-10 23:54:46 +0100720 if (p->config_fd != -1)
721 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100722 if (p->fd != -1)
723 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200724 }
725 return err;
726}
727
Damjan Marion599a16b2018-03-04 19:35:23 +0100728clib_error_t *
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100729vlib_pci_register_intx_handler (vlib_pci_dev_handle_t h,
730 pci_intx_handler_function_t * intx_handler)
731{
732 linux_pci_device_t *p = linux_pci_get_device (h);
733 clib_file_t t = { 0 };
734 linux_pci_irq_t *irq = &p->intx_irq;
735 ASSERT (irq->fd == -1);
736
737 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
738 {
739 struct vfio_irq_info irq_info = { 0 };
740 irq_info.argsz = sizeof (struct vfio_irq_info);
741 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
742 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
743 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
744 "%U'", format_vlib_pci_addr, &p->addr);
745 if (irq_info.count != 1)
746 return clib_error_return (0, "INTx interrupt does not exist on device"
747 "'%U'", format_vlib_pci_addr, &p->addr);
748
749 irq->fd = eventfd (0, EFD_NONBLOCK);
750 if (irq->fd == -1)
751 return clib_error_return_unix (0, "eventfd");
752 t.file_descriptor = irq->fd;
753 t.read_function = linux_pci_vfio_intx_read_ready;
754 }
755 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
756 {
757 t.file_descriptor = p->fd;
758 t.read_function = linux_pci_uio_read_ready;
759 }
760 else
761 return 0;
762
763 t.error_function = linux_pci_uio_error_ready;
764 t.private_data = p->handle;
765 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
766 irq->clib_file_index = clib_file_add (&file_main, &t);
767 irq->intx_handler = intx_handler;
768 return 0;
769}
770
771clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100772vlib_pci_register_msix_handler (vlib_pci_dev_handle_t h, u32 start, u32 count,
773 pci_msix_handler_function_t * msix_handler)
774{
775 clib_error_t *err = 0;
776 linux_pci_device_t *p = linux_pci_get_device (h);
777 u32 i;
778
779 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
780 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
781 "support");
782
783 /* *INDENT-OFF* */
784 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
785 { .fd = -1});
786 /* *INDENT-ON* */
787
788 for (i = start; i < start + count; i++)
789 {
790 clib_file_t t = { 0 };
791 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
792 ASSERT (irq->fd == -1);
793
794 irq->fd = eventfd (0, EFD_NONBLOCK);
795 if (irq->fd == -1)
796 {
797 err = clib_error_return_unix (0, "eventfd");
798 goto error;
799 }
800
801 t.read_function = linux_pci_vfio_msix_read_ready;
802 t.file_descriptor = irq->fd;
803 t.error_function = linux_pci_vfio_error_ready;
804 t.private_data = p->handle << 16 | i;
805 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
806 &p->addr, i);
807 irq->clib_file_index = clib_file_add (&file_main, &t);
808 irq->msix_handler = msix_handler;
809 }
810
811 return 0;
812
813error:
814 while (i-- > start)
815 {
816 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
817 if (irq->fd != -1)
818 {
819 clib_file_del_by_index (&file_main, irq->clib_file_index);
820 close (irq->fd);
821 irq->fd = -1;
822 }
823 }
824 return err;
825}
826
827clib_error_t *
828vlib_pci_enable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
829{
830 linux_pci_device_t *p = linux_pci_get_device (h);
831 int fds[count];
832 int i;
833
834 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
835 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
836 "support");
837
838 for (i = start; i < start + count; i++)
839 {
840 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
841 fds[i] = irq->fd;
842 }
843
844 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
845 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
846}
847
848clib_error_t *
849vlib_pci_disable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
850{
851 linux_pci_device_t *p = linux_pci_get_device (h);
852 int i, fds[count];
853
854 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
855 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
856 "support");
857
858 for (i = start; i < start + count; i++)
859 fds[i] = -1;
860
861 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
862 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
863}
864
Damjan Marioncef87f12017-10-05 15:32:41 +0200865static linux_pci_vfio_iommu_group_t *
866get_vfio_iommu_group (int group)
867{
868 linux_pci_main_t *lpm = &linux_pci_main;
869 uword *p;
870
871 p = hash_get (lpm->iommu_pool_index_by_group, group);
872
873 return p ? pool_elt_at_index (lpm->iommu_groups, p[0]) : 0;
874}
875
876static clib_error_t *
877open_vfio_iommu_group (int group)
878{
879 linux_pci_main_t *lpm = &linux_pci_main;
880 linux_pci_vfio_iommu_group_t *g;
881 clib_error_t *err = 0;
882 struct vfio_group_status group_status;
883 u8 *s = 0;
884 int fd;
885
886 g = get_vfio_iommu_group (group);
887 if (g)
888 {
889 g->refcnt++;
890 return 0;
891 }
892 s = format (s, "/dev/vfio/%u%c", group, 0);
893 fd = open ((char *) s, O_RDWR);
894 if (fd < 0)
Damjan Marion96504182017-12-10 23:54:46 +0100895 return clib_error_return_unix (0, "open '%s'", s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200896
897 group_status.argsz = sizeof (group_status);
898 if (ioctl (fd, VFIO_GROUP_GET_STATUS, &group_status) < 0)
899 {
900 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_STATUS) '%s'",
901 s);
902 goto error;
903 }
904
905 if (!(group_status.flags & VFIO_GROUP_FLAGS_VIABLE))
906 {
907 err = clib_error_return (0, "iommu group %d is not viable (not all "
908 "devices in this group bound to vfio-pci)",
909 group);
910 goto error;
911 }
912
913 if (ioctl (fd, VFIO_GROUP_SET_CONTAINER, &lpm->vfio_container_fd) < 0)
914 {
915 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_SET_CONTAINER) '%s'",
916 s);
917 goto error;
918 }
919
920 if (lpm->vfio_iommu_mode == 0)
921 {
922 if (ioctl (lpm->vfio_container_fd, VFIO_SET_IOMMU, VFIO_TYPE1_IOMMU) <
923 0)
924 {
925 err = clib_error_return_unix (0, "ioctl(VFIO_SET_IOMMU) "
926 "'/dev/vfio/vfio'");
927 goto error;
928 }
929 lpm->vfio_iommu_mode = VFIO_TYPE1_IOMMU;
930 }
931
932
933 pool_get (lpm->iommu_groups, g);
934 g->fd = fd;
935 g->refcnt = 1;
936 hash_set (lpm->iommu_pool_index_by_group, group, g - lpm->iommu_groups);
937 vec_free (s);
938 return 0;
939error:
940 close (fd);
941 return err;
942}
943
944static clib_error_t *
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100945add_device_vfio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
946 pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200947{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100948 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200949 linux_pci_vfio_iommu_group_t *g;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100950 struct vfio_device_info device_info = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +0200951 clib_error_t *err = 0;
952 u8 *s = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200953
954 p->addr.as_u32 = di->addr.as_u32;
Damjan Marion2060db82018-03-04 17:37:15 +0100955 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200956
957 if (di->driver_name == 0 ||
958 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
959 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
960 "vfio-pci", format_vlib_pci_addr, &di->addr,
961 di->iommu_group);
962
963 if ((err = open_vfio_iommu_group (di->iommu_group)))
964 return err;
965
966 g = get_vfio_iommu_group (di->iommu_group);
967
968 s = format (s, "%U%c", format_vlib_pci_addr, &di->addr, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100969 if ((p->fd = ioctl (g->fd, VFIO_GROUP_GET_DEVICE_FD, (char *) s)) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200970 {
971 err = clib_error_return_unix (0, "ioctl(VFIO_GROUP_GET_DEVICE_FD) '%U'",
972 format_vlib_pci_addr, &di->addr);
973 goto error;
974 }
975 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200976
977 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100978 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200979 {
980 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
981 format_vlib_pci_addr, &di->addr);
982 goto error;
983 }
984
Damjan Marioncef87f12017-10-05 15:32:41 +0200985 /* reset if device supports it */
986 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100987 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200988 {
989 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
990 format_vlib_pci_addr, &di->addr);
991 goto error;
992 }
993
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100994 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +0200995 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100996 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200997 linux_pci_vfio_unmask_intx (p);
998 }
999
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001000 if (r && r->init_function)
1001 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +02001002
1003error:
1004 vec_free (s);
1005 if (err)
1006 {
Damjan Marionf9a968e2018-02-25 23:26:22 +01001007 if (p->fd != -1)
1008 close (p->fd);
Damjan Marion96504182017-12-10 23:54:46 +01001009 if (p->config_fd != -1)
1010 close (p->config_fd);
Damjan Marioncef87f12017-10-05 15:32:41 +02001011 }
1012 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001013}
1014
1015/* Configuration space read/write. */
1016clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +02001017vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +02001018 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -04001019 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001020{
Damjan Marioncef87f12017-10-05 15:32:41 +02001021 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001022 int n;
1023
Ed Warnickecb9cada2015-12-08 15:45:58 -07001024 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +01001025 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001026 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001027 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028
1029 if (n != n_bytes)
1030 return clib_error_return_unix (0, "%s",
1031 read_or_write == VLIB_READ
1032 ? "read" : "write");
1033
1034 return 0;
1035}
1036
1037static clib_error_t *
Damjan Marion2060db82018-03-04 17:37:15 +01001038vlib_pci_map_region_int (vlib_pci_dev_handle_t h,
1039 u32 bar, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001040{
Damjan Marioncef87f12017-10-05 15:32:41 +02001041 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Marion2060db82018-03-04 17:37:15 +01001042 int fd = -1;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001043 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001044 int flags = MAP_SHARED;
Damjan Marion2060db82018-03-04 17:37:15 +01001045 u64 size = 0, offset = 0;
1046
1047 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001048
1049 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001050
Damjan Marion2060db82018-03-04 17:37:15 +01001051 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 {
Damjan Marion2060db82018-03-04 17:37:15 +01001053 u8 *file_name;
1054 struct stat stat_buf;
1055 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1056 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001057
Damjan Marion2060db82018-03-04 17:37:15 +01001058 fd = open ((char *) file_name, O_RDWR);
1059 if (fd < 0)
1060 {
1061 error = clib_error_return_unix (0, "open `%s'", file_name);
1062 vec_free (file_name);
1063 return error;
1064 }
1065
1066 if (fstat (fd, &stat_buf) < 0)
1067 {
1068 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1069 vec_free (file_name);
1070 close (fd);
1071 return error;
1072 }
1073
1074 vec_free (file_name);
1075 if (addr != 0)
1076 flags |= MAP_FIXED;
1077 size = stat_buf.st_size;
1078 offset = 0;
1079 }
1080 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001081 {
Damjan Marion2060db82018-03-04 17:37:15 +01001082 struct vfio_region_info reg = { 0 };
1083 reg.argsz = sizeof (struct vfio_region_info);
1084 reg.index = bar;
1085 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1086 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1087 "'%U'", format_vlib_pci_addr,
1088 &p->addr);
1089 fd = p->fd;
1090 size = reg.size;
1091 offset = reg.offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001092 }
Damjan Marion2060db82018-03-04 17:37:15 +01001093 else
1094 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001095
Damjan Marion2060db82018-03-04 17:37:15 +01001096 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001097 if (*result == (void *) -1)
1098 {
Damjan Marion2060db82018-03-04 17:37:15 +01001099 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1100 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001101 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001102 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001103 }
Damjan Marion2060db82018-03-04 17:37:15 +01001104
1105 /* *INDENT-OFF* */
1106 vec_validate_init_empty (p->regions, bar,
1107 (linux_pci_region_t) { .fd = -1});
1108 /* *INDENT-ON* */
1109 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1110 p->regions[bar].fd = fd;
1111 p->regions[bar].addr = *result;
1112 p->regions[bar].size = size;
1113 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001114}
1115
1116clib_error_t *
Damjan Marion2060db82018-03-04 17:37:15 +01001117vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001118{
Damjan Marion2060db82018-03-04 17:37:15 +01001119 return (vlib_pci_map_region_int (h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001120}
1121
1122clib_error_t *
Damjan Marion2060db82018-03-04 17:37:15 +01001123vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h, u32 resource, u8 * addr,
1124 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001125{
Damjan Marion2060db82018-03-04 17:37:15 +01001126 return (vlib_pci_map_region_int (h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001127}
1128
Dave Barach9b8ffd92016-07-08 08:13:45 -04001129void
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001130init_device_from_registered (vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001132 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001133 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001134 pci_device_registration_t *r;
1135 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001136 clib_error_t *err = 0;
1137 linux_pci_device_t *p;
1138
1139 pool_get (lpm->linux_pci_devices, p);
1140 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001141 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001142
Damjan Marion5a206ea2016-05-12 22:11:03 +02001143 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001144
1145 while (r)
1146 {
1147 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001148 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001149 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001150 if (di->iommu_group != -1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001151 err = add_device_vfio (p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001152 else
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001153 err = add_device_uio (p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001154
Damjan Marioncef87f12017-10-05 15:32:41 +02001155 if (err)
1156 clib_error_report (err);
1157 else
1158 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001159 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001160 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001161 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001162
Ed Warnickecb9cada2015-12-08 15:45:58 -07001163 /* No driver, close the PCI config-space FD */
Damjan Marioncef87f12017-10-05 15:32:41 +02001164 memset (p, 0, sizeof (linux_pci_device_t));
1165 pool_put (lpm->linux_pci_devices, p);
1166}
1167
1168static clib_error_t *
1169scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1170{
1171 vlib_pci_addr_t addr, **addrv = arg;
1172 unformat_input_t input;
1173 clib_error_t *err = 0;
1174
1175 unformat_init_string (&input, (char *) dev_dir_name,
1176 vec_len (dev_dir_name));
1177
1178 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1179 unformat_vlib_pci_addr, &addr))
1180 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1181
1182 unformat_free (&input);
1183
1184 if (err)
1185 return err;
1186
1187 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001188 return 0;
1189}
1190
Damjan Marioncef87f12017-10-05 15:32:41 +02001191static int
1192pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001193{
Damjan Marioncef87f12017-10-05 15:32:41 +02001194 vlib_pci_addr_t *a1 = v1;
1195 vlib_pci_addr_t *a2 = v2;
1196
1197 if (a1->domain > a2->domain)
1198 return 1;
1199 if (a1->domain < a2->domain)
1200 return -1;
1201 if (a1->bus > a2->bus)
1202 return 1;
1203 if (a1->bus < a2->bus)
1204 return -1;
1205 if (a1->slot > a2->slot)
1206 return 1;
1207 if (a1->slot < a2->slot)
1208 return -1;
1209 if (a1->function > a2->function)
1210 return 1;
1211 if (a1->function < a2->function)
1212 return -1;
1213 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001214}
1215
Damjan Marioncef87f12017-10-05 15:32:41 +02001216vlib_pci_addr_t *
1217vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001218{
Damjan Marioncef87f12017-10-05 15:32:41 +02001219 vlib_pci_addr_t *addrs = 0;
1220 clib_error_t *err;
1221 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1222 &addrs, /* scan_dirs */ 0);
1223 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001224 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001225 vec_free (addrs);
1226 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001227 }
1228
Damjan Marioncef87f12017-10-05 15:32:41 +02001229 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001230
Damjan Marioncef87f12017-10-05 15:32:41 +02001231 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001232}
1233
Dave Barach9b8ffd92016-07-08 08:13:45 -04001234clib_error_t *
1235linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001236{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001237 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001238 linux_pci_main_t *lpm = &linux_pci_main;
1239 vlib_pci_addr_t *addr = 0, *addrs;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001240 clib_error_t *error;
Damjan Marioncef87f12017-10-05 15:32:41 +02001241 int fd;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001242
1243 pm->vlib_main = vm;
1244
1245 if ((error = vlib_call_init_function (vm, unix_input_init)))
1246 return error;
1247
Dave Barach9b8ffd92016-07-08 08:13:45 -04001248 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001249
Damjan Marioncef87f12017-10-05 15:32:41 +02001250 fd = open ("/dev/vfio/vfio", O_RDWR);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001251
Damjan Marioncef87f12017-10-05 15:32:41 +02001252 if ((fd != -1) && (ioctl (fd, VFIO_GET_API_VERSION) != VFIO_API_VERSION))
1253 {
1254 close (fd);
1255 fd = -1;
1256 }
1257
1258 if ((fd != -1) && (ioctl (fd, VFIO_CHECK_EXTENSION, VFIO_TYPE1_IOMMU) == 0))
1259 {
1260 close (fd);
1261 fd = -1;
1262 }
1263
1264 lpm->vfio_container_fd = fd;
1265 lpm->iommu_pool_index_by_group = hash_create (0, sizeof (uword));
1266
1267 addrs = vlib_pci_get_all_dev_addrs ();
1268 /* *INDENT-OFF* */
1269 vec_foreach (addr, addrs)
1270 {
1271 vlib_pci_device_info_t *d;
1272 if ((d = vlib_pci_get_device_info (addr, 0)))
1273 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001274 init_device_from_registered (d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001275 vlib_pci_free_device_info (d);
1276 }
1277 }
1278 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001279
1280 return error;
1281}
1282
Damjan Marion5a206ea2016-05-12 22:11:03 +02001283VLIB_INIT_FUNCTION (linux_pci_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001284
1285/*
1286 * fd.io coding-style-patch-verification: ON
1287 *
1288 * Local Variables:
1289 * eval: (c-set-style "gnu")
1290 * End:
1291 */