blob: c6fa8a7da344e145f27265f7f1d9d3d5467e0bf9 [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>
Damjan Marion1ba0fa42018-03-04 17:19:08 +010045#include <vlib/linux/vfio.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070046
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <dirent.h>
Damjan Mariona42cd342016-04-13 18:03:20 +020051#include <sys/ioctl.h>
52#include <net/if.h>
53#include <linux/ethtool.h>
54#include <linux/sockios.h>
Damjan Marioncef87f12017-10-05 15:32:41 +020055#include <linux/vfio.h>
56#include <sys/eventfd.h>
57
58static const char *sysfs_pci_dev_path = "/sys/bus/pci/devices";
59static const char *sysfs_pci_drv_path = "/sys/bus/pci/drivers";
Damjan Marion2752b892017-12-11 15:55:56 +010060static char *sysfs_mod_vfio_noiommu =
61 "/sys/module/vfio/parameters/enable_unsafe_noiommu_mode";
Damjan Mariona42cd342016-04-13 18:03:20 +020062
Damjan Marion23227982018-10-22 13:38:57 +020063#define pci_log_debug(vm, dev, f, ...) \
64 vlib_log(VLIB_LOG_LEVEL_DEBUG, pci_main.log_default, "%U: " f, \
65 format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
66#define pci_log_err(vm, dev, f, ...) \
67 vlib_log(VLIB_LOG_LEVEL_ERR, pci_main.log_default, "%U: " f, \
68 format_vlib_pci_addr, vlib_pci_get_addr(vm, dev->handle), ## __VA_ARGS__)
69
Dave Barach9b8ffd92016-07-08 08:13:45 -040070typedef struct
71{
Damjan Marion2060db82018-03-04 17:37:15 +010072 int fd;
73 void *addr;
74 size_t size;
75} linux_pci_region_t;
76
Damjan Marion599a16b2018-03-04 19:35:23 +010077typedef struct
78{
79 int fd;
80 u32 clib_file_index;
81 union
82 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010083 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010084 pci_msix_handler_function_t *msix_handler;
85 };
86} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010087
88typedef enum
89{
90 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
91 LINUX_PCI_DEVICE_TYPE_UIO,
92 LINUX_PCI_DEVICE_TYPE_VFIO,
93} linux_pci_device_type_t;
94
95typedef struct
96{
97 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +020098 vlib_pci_dev_handle_t handle;
99 vlib_pci_addr_t addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200100
101 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +0100102 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200103
104 /* File descriptor for config space read/write. */
105 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100106 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200107
Damjan Marionf9a968e2018-02-25 23:26:22 +0100108 /* Device File descriptor */
109 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200110
Mohsin Kazmi95276ca2018-09-28 11:45:01 +0200111 /* read/write file descriptor for io bar */
112 int io_fd;
113 u64 io_offset;
114
Damjan Marion5a206ea2016-05-12 22:11:03 +0200115 /* Minor device for uio device. */
116 u32 uio_minor;
117
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100118 /* Interrupt handlers */
119 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100120 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200121
122 /* private data */
123 uword private_data;
124
Damjan Marion23227982018-10-22 13:38:57 +0200125 u8 supports_va_dma;
126
Damjan Marion5a206ea2016-05-12 22:11:03 +0200127} linux_pci_device_t;
128
129/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400130typedef struct
131{
132 vlib_main_t *vlib_main;
133 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200134
Damjan Marion5a206ea2016-05-12 22:11:03 +0200135} linux_pci_main_t;
136
137extern linux_pci_main_t linux_pci_main;
138
Damjan Marion599a16b2018-03-04 19:35:23 +0100139static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200140linux_pci_get_device (vlib_pci_dev_handle_t h)
141{
142 linux_pci_main_t *lpm = &linux_pci_main;
143 return pool_elt_at_index (lpm->linux_pci_devices, h);
144}
145
146uword
Damjan Marion23227982018-10-22 13:38:57 +0200147vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200148{
149 linux_pci_device_t *d = linux_pci_get_device (h);
150 return d->private_data;
151}
152
153void
Damjan Marion23227982018-10-22 13:38:57 +0200154vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
155 uword private_data)
Damjan Marioncef87f12017-10-05 15:32:41 +0200156{
157 linux_pci_device_t *d = linux_pci_get_device (h);
158 d->private_data = private_data;
159}
160
161vlib_pci_addr_t *
Damjan Marion23227982018-10-22 13:38:57 +0200162vlib_pci_get_addr (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marioncef87f12017-10-05 15:32:41 +0200163{
Damjan Marion599a16b2018-03-04 19:35:23 +0100164 linux_pci_device_t *d = linux_pci_get_device (h);
165 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200166}
167
Damjan Marion5a206ea2016-05-12 22:11:03 +0200168/* Call to allocate/initialize the pci subsystem.
169 This is not an init function so that users can explicitly enable
170 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400171clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200172
Ed Warnickecb9cada2015-12-08 15:45:58 -0700173linux_pci_main_t linux_pci_main;
174
Damjan Marioncef87f12017-10-05 15:32:41 +0200175vlib_pci_device_info_t *
Damjan Marion23227982018-10-22 13:38:57 +0200176vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
177 clib_error_t ** error)
Damjan Marioncef87f12017-10-05 15:32:41 +0200178{
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100179 linux_vfio_main_t *lvm = &vfio_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200180 clib_error_t *err;
181 vlib_pci_device_info_t *di;
182 u8 *f = 0;
183 u32 tmp;
184 int fd;
185
186 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
Dave Barachb7b92992018-10-17 10:38:51 -0400187 clib_memset (di, 0, sizeof (vlib_pci_device_info_t));
Damjan Marioncef87f12017-10-05 15:32:41 +0200188 di->addr.as_u32 = addr->as_u32;
189
190 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
191 format_vlib_pci_addr, addr);
192
193 f = format (0, "%v/config%c", dev_dir_name, 0);
194 fd = open ((char *) f, O_RDWR);
195
196 /* Try read-only access if write fails. */
197 if (fd < 0)
198 fd = open ((char *) f, O_RDONLY);
199
200 if (fd < 0)
201 {
202 err = clib_error_return_unix (0, "open `%s'", f);
203 goto error;
204 }
205
206 /* You can only read more that 64 bytes of config space as root; so we try to
207 read the full space but fall back to just the first 64 bytes. */
Damjan Marion44bcc202018-03-04 16:44:26 +0100208 if (read (fd, &di->config_data, sizeof (di->config_data)) <
209 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200210 {
211 err = clib_error_return_unix (0, "read `%s'", f);
212 close (fd);
213 goto error;
214 }
215
216 {
217 static pci_config_header_t all_ones;
218 if (all_ones.vendor_id == 0)
Dave Barachb7b92992018-10-17 10:38:51 -0400219 clib_memset (&all_ones, ~0, sizeof (all_ones));
Damjan Marioncef87f12017-10-05 15:32:41 +0200220
221 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
222 {
223 err = clib_error_return (0, "invalid PCI config for `%s'", f);
224 close (fd);
225 goto error;
226 }
227 }
228
229 if (di->config0.header.header_type == 0)
230 pci_config_type0_little_to_host (&di->config0);
231 else
232 pci_config_type1_little_to_host (&di->config1);
233
234 di->numa_node = -1;
235 vec_reset_length (f);
236 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
237 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
238 if (err)
239 {
240 di->numa_node = -1;
241 clib_error_free (err);
242 }
243
244 vec_reset_length (f);
245 f = format (f, "%v/class%c", dev_dir_name, 0);
246 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
247 if (err)
248 goto error;
249 di->device_class = tmp >> 8;
250
251 vec_reset_length (f);
252 f = format (f, "%v/vendor%c", dev_dir_name, 0);
253 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
254 if (err)
255 goto error;
256 di->vendor_id = tmp;
257
258 vec_reset_length (f);
259 f = format (f, "%v/device%c", dev_dir_name, 0);
260 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
261 if (err)
262 goto error;
263 di->device_id = tmp;
264
265 vec_reset_length (f);
266 f = format (f, "%v/driver%c", dev_dir_name, 0);
267 di->driver_name = clib_sysfs_link_to_name ((char *) f);
268
269 di->iommu_group = -1;
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100270 if (lvm->container_fd != -1)
Damjan Marioncef87f12017-10-05 15:32:41 +0200271 {
272 u8 *tmpstr;
273 vec_reset_length (f);
274 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
275 tmpstr = clib_sysfs_link_to_name ((char *) f);
276 if (tmpstr)
277 {
278 di->iommu_group = atoi ((char *) tmpstr);
279 vec_free (tmpstr);
280 }
Damjan Marion20ba1642018-03-26 15:35:33 +0200281 vec_reset_length (f);
282 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
283 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
284 if (err == 0)
285 {
286 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
287 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
288 vec_free (tmpstr);
289 }
290 else
291 clib_error_free (err);
Damjan Marioncef87f12017-10-05 15:32:41 +0200292 }
293
Damjan Marion96504182017-12-10 23:54:46 +0100294 close (fd);
295
Damjan Marioncef87f12017-10-05 15:32:41 +0200296 vec_reset_length (f);
297 f = format (f, "%v/vpd%c", dev_dir_name, 0);
298 fd = open ((char *) f, O_RDONLY);
299 if (fd >= 0)
300 {
301 while (1)
302 {
303 u8 tag[3];
304 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100305 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200306
307 if (read (fd, &tag, 3) != 3)
308 break;
309
310 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
311 break;
312
313 len = (tag[2] << 8) | tag[1];
314 vec_validate (data, len);
315
316 if (read (fd, data, len) != len)
317 {
318 vec_free (data);
319 break;
320 }
321 if (tag[0] == 0x82)
322 di->product_name = data;
323 else if (tag[0] == 0x90)
324 di->vpd_r = data;
325 else if (tag[0] == 0x91)
326 di->vpd_w = data;
327
328 data = 0;
329 }
330 close (fd);
331 }
332
333 goto done;
334
335error:
336 vlib_pci_free_device_info (di);
337 di = 0;
338
339done:
340 vec_free (f);
341 vec_free (dev_dir_name);
342 if (error)
343 *error = err;
344 else
345 clib_error_free (err);
346 return di;
347}
348
Damjan Marion2752b892017-12-11 15:55:56 +0100349static int
350directory_exists (char *path)
351{
352 struct stat s = { 0 };
353 if (stat (path, &s) == -1)
354 return 0;
355
356 return S_ISDIR (s.st_mode);
357}
358
Damjan Mariona42cd342016-04-13 18:03:20 +0200359clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200360vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
361 char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700362{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400363 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200364 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200365 DIR *dir = 0;
366 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200367 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200368 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200369 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
370 format_vlib_pci_addr, addr);
371
Damjan Marion23227982018-10-22 13:38:57 +0200372 di = vlib_pci_get_device_info (vm, addr, &error);
Damjan Marioncef87f12017-10-05 15:32:41 +0200373
374 if (error)
375 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700376
Damjan Marion2752b892017-12-11 15:55:56 +0100377 if (strncmp ("auto", uio_drv_name, 5) == 0)
378 {
379 int vfio_pci_loaded = 0;
380
381 if (directory_exists ("/sys/module/vfio_pci"))
382 vfio_pci_loaded = 1;
383
384 if (di->iommu_group != -1)
385 {
386 /* device is bound to IOMMU group */
387 if (!vfio_pci_loaded)
388 {
389 error = clib_error_return (0, "Skipping PCI device %U: device "
390 "is bound to IOMMU group and "
391 "vfio-pci driver is not loaded",
392 format_vlib_pci_addr, addr);
393 goto done;
394 }
395 else
396 uio_drv_name = "vfio-pci";
397 }
398 else
399 {
400 /* device is not bound to IOMMU group so we have multiple options */
401 if (vfio_pci_loaded &&
402 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
403 uio_drv_name = "vfio-pci";
404 else if (directory_exists ("/sys/module/uio_pci_generic"))
405 uio_drv_name = "uio_pci_generic";
406 else if (directory_exists ("/sys/module/igb_uio"))
407 uio_drv_name = "igb_uio";
408 else
409 {
410 clib_error_free (error);
411 error = clib_error_return (0, "Skipping PCI device %U: missing "
412 "kernel VFIO or UIO driver",
413 format_vlib_pci_addr, addr);
414 goto done;
415 }
416 clib_error_free (error);
417 }
418 }
419
Damjan Mariona7f74572017-05-23 18:32:21 +0200420 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200421 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200422 vec_reset_length (s);
423
Damjan Mariona7f74572017-05-23 18:32:21 +0200424 if (driver_name &&
425 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
426 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
427 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200428 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200429
Damjan Mariona42cd342016-04-13 18:03:20 +0200430 /* walk trough all linux interfaces and if interface belonging to
431 this device is founf check if interface is admin up */
432 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200433 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200434
435 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700436 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200437 error = clib_error_return (0, "Skipping PCI device %U: failed to "
438 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200439 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200440 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441 }
442
Dave Barach9b8ffd92016-07-08 08:13:45 -0400443 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400444 if (fd < 0)
445 {
446 error = clib_error_return_unix (0, "socket");
447 goto done;
448 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200449
Dave Barach9b8ffd92016-07-08 08:13:45 -0400450 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700451 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200452 struct ifreq ifr;
453 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700454
Dave Barach9b8ffd92016-07-08 08:13:45 -0400455 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200456 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700457
Dave Barachb7b92992018-10-17 10:38:51 -0400458 clib_memset (&ifr, 0, sizeof ifr);
459 clib_memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200460 ifr.ifr_data = (char *) &drvinfo;
Marco Varlese99d7a722018-06-27 09:54:44 +0200461 strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name));
462 ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0';
Damjan Mariona42cd342016-04-13 18:03:20 +0200463 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400464 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400465 {
John Loe2821212016-07-13 18:13:02 -0400466 /* Some interfaces (eg "lo") don't support this ioctl */
467 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700468 clib_unix_warning ("ioctl fetch intf %s bus info error",
469 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400470 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400471 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200472
473 if (strcmp ((char *) s, drvinfo.bus_info))
474 continue;
475
Dave Barachb7b92992018-10-17 10:38:51 -0400476 clib_memset (&ifr, 0, sizeof (ifr));
Marco Varlese99d7a722018-06-27 09:54:44 +0200477 strncpy (ifr.ifr_name, e->d_name, sizeof (ifr.ifr_name));
478 ifr.ifr_name[ARRAY_LEN (ifr.ifr_name) - 1] = '\0';
Chris Luke370e9e32016-07-07 11:01:17 -0400479 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400480 {
481 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
482 e->d_name);
483 close (fd);
484 goto done;
485 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200486
487 if (ifr.ifr_flags & IFF_UP)
488 {
489 error = clib_error_return (0, "Skipping PCI device %U as host "
490 "interface %s is up",
Damjan Marioncef87f12017-10-05 15:32:41 +0200491 format_vlib_pci_addr, addr, e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400492 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200493 goto done;
494 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700495 }
496
Damjan Mariona42cd342016-04-13 18:03:20 +0200497 close (fd);
498 vec_reset_length (s);
499
500 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200501 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200502 vec_reset_length (s);
503
Damjan Mariona7f74572017-05-23 18:32:21 +0200504 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
505 if (access ((char *) s, F_OK) == 0)
506 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200507 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200508 clear_driver_override = 1;
509 }
510 else
511 {
512 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200513 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
514 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
515 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200516 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200517 vec_reset_length (s);
518
Damjan Marioncef87f12017-10-05 15:32:41 +0200519 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
520 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200521 vec_reset_length (s);
522
523 if (clear_driver_override)
524 {
525 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200526 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200527 vec_reset_length (s);
528 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200529
530done:
531 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700532 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200533 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200534 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700535 return error;
536}
537
Ed Warnickecb9cada2015-12-08 15:45:58 -0700538
539static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400540scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700541{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400542 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700543 unformat_input_t input;
544
545 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
546
Dave Barach9b8ffd92016-07-08 08:13:45 -0400547 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700548 abort ();
549
550 unformat_free (&input);
551 return 0;
552}
553
Dave Barach9b8ffd92016-07-08 08:13:45 -0400554static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200555vfio_set_irqs (vlib_main_t * vm, linux_pci_device_t * p, u32 index, u32 start,
556 u32 count, u32 flags, int *efds)
Damjan Marion599a16b2018-03-04 19:35:23 +0100557{
558 int data_len = efds ? count * sizeof (int) : 0;
559 u8 buf[sizeof (struct vfio_irq_set) + data_len];
Damjan Marion23227982018-10-22 13:38:57 +0200560 struct vfio_irq_info ii = { 0 };
Damjan Marion599a16b2018-03-04 19:35:23 +0100561 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
562
563
Damjan Marion23227982018-10-22 13:38:57 +0200564 ii.argsz = sizeof (struct vfio_irq_info);
565 ii.index = index;
Damjan Marion599a16b2018-03-04 19:35:23 +0100566
Damjan Marion23227982018-10-22 13:38:57 +0200567 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Marion599a16b2018-03-04 19:35:23 +0100568 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
569 "'%U'", format_vlib_pci_addr, &p->addr);
570
Damjan Marion23227982018-10-22 13:38:57 +0200571 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
572 __func__, ii.index, ii.count,
573 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
574 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
575 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
576 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
577 ii.flags);
578
579 if (ii.count < start + count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100580 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
581 "'%U'", format_vlib_pci_addr, &p->addr);
582
583
584 if (efds)
585 {
586 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
587 clib_memcpy (&irq_set->data, efds, data_len);
588 }
589 else
590 flags |= VFIO_IRQ_SET_DATA_NONE;
591
592 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
593 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
594
595 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
596 irq_set->index = index;
597 irq_set->start = start;
598 irq_set->count = count;
599 irq_set->flags = flags;
600
601 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
602 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
603 "[index = %u, start = %u, count = %u, "
604 "flags = 0x%x]",
605 format_vlib_pci_addr, &p->addr,
606 index, start, count, flags);
607 return 0;
608}
609
610static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200611linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612{
Damjan Marion23227982018-10-22 13:38:57 +0200613 vlib_main_t *vm = vlib_get_main ();
Damjan Marion5a206ea2016-05-12 22:11:03 +0200614 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200615 vlib_pci_dev_handle_t h = uf->private_data;
616 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100617 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700618
Damjan Marion5a206ea2016-05-12 22:11:03 +0200619 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400620 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700621
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100622 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200623 irq->intx_handler (vm, h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200624
Damjan Marion23227982018-10-22 13:38:57 +0200625 vlib_pci_intr_enable (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700626
627 return /* no error */ 0;
628}
629
Dave Barach9b8ffd92016-07-08 08:13:45 -0400630static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200631linux_pci_vfio_unmask_intx (vlib_main_t * vm, linux_pci_device_t * d)
Damjan Marioncef87f12017-10-05 15:32:41 +0200632{
Damjan Marion23227982018-10-22 13:38:57 +0200633 return vfio_set_irqs (vm, d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100634 VFIO_IRQ_SET_ACTION_UNMASK, 0);
635}
636
Damjan Marioncef87f12017-10-05 15:32:41 +0200637static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200638linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700639{
640 u32 error_index = (u32) uf->private_data;
641
642 return clib_error_return (0, "pci device %d: error", error_index);
643}
644
Damjan Marioncef87f12017-10-05 15:32:41 +0200645static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100646linux_pci_vfio_msix_read_ready (clib_file_t * uf)
647{
Damjan Marion23227982018-10-22 13:38:57 +0200648 vlib_main_t *vm = vlib_get_main ();
Damjan Marion599a16b2018-03-04 19:35:23 +0100649 int __attribute__ ((unused)) rv;
650 vlib_pci_dev_handle_t h = uf->private_data >> 16;
651 u16 line = uf->private_data & 0xffff;
652 linux_pci_device_t *p = linux_pci_get_device (h);
653 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
654
655 u64 icount;
656 rv = read (uf->file_descriptor, &icount, sizeof (icount));
657
658 if (irq->msix_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200659 irq->msix_handler (vm, h, line);
Damjan Marion599a16b2018-03-04 19:35:23 +0100660
661 return /* no error */ 0;
662}
663
664static clib_error_t *
665linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700666{
Damjan Marion23227982018-10-22 13:38:57 +0200667 vlib_main_t *vm = vlib_get_main ();
Damjan Marioncef87f12017-10-05 15:32:41 +0200668 int __attribute__ ((unused)) rv;
669 vlib_pci_dev_handle_t h = uf->private_data;
670 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100671 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700672
Damjan Marioncef87f12017-10-05 15:32:41 +0200673 u64 icount;
674 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700675
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100676 if (irq->intx_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200677 irq->intx_handler (vm, h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700678
Damjan Marion23227982018-10-22 13:38:57 +0200679 linux_pci_vfio_unmask_intx (vm, p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700680
Damjan Marioncef87f12017-10-05 15:32:41 +0200681 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700682}
683
Damjan Marioncef87f12017-10-05 15:32:41 +0200684static clib_error_t *
685linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700686{
Damjan Marioncef87f12017-10-05 15:32:41 +0200687 u32 error_index = (u32) uf->private_data;
688
689 return clib_error_return (0, "pci device %d: error", error_index);
690}
691
692static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200693add_device_uio (vlib_main_t * vm, linux_pci_device_t * p,
694 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200695{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100696 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200697 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200698 u8 *s = 0;
699
Damjan Marionf9a968e2018-02-25 23:26:22 +0100700 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100701 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200702
703 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
704 format_vlib_pci_addr, &di->addr, 0);
705
706 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100707 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200708 vec_reset_length (s);
709
710 if (p->config_fd == -1)
711 {
712 err = clib_error_return_unix (0, "open '%s'", s);
713 goto error;
714 }
715
716 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
717 format_vlib_pci_addr, &di->addr);
718 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
719 1);
720 vec_reset_length (s);
721
722 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100723 p->fd = open ((char *) s, O_RDWR);
724 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200725 {
726 err = clib_error_return_unix (0, "open '%s'", s);
727 goto error;
728 }
729
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100730 if (r && r->interrupt_handler)
Damjan Marion23227982018-10-22 13:38:57 +0200731 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200732
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100733 if (r && r->init_function)
734 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200735
736error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200737 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200738 if (err)
739 {
Damjan Marion96504182017-12-10 23:54:46 +0100740 if (p->config_fd != -1)
741 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100742 if (p->fd != -1)
743 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200744 }
745 return err;
746}
747
Damjan Marion599a16b2018-03-04 19:35:23 +0100748clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200749vlib_pci_register_intx_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100750 pci_intx_handler_function_t * intx_handler)
751{
752 linux_pci_device_t *p = linux_pci_get_device (h);
753 clib_file_t t = { 0 };
754 linux_pci_irq_t *irq = &p->intx_irq;
755 ASSERT (irq->fd == -1);
756
757 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
758 {
Damjan Marion23227982018-10-22 13:38:57 +0200759 struct vfio_irq_info ii = { 0 };
760 ii.argsz = sizeof (struct vfio_irq_info);
761 ii.index = VFIO_PCI_INTX_IRQ_INDEX;
762 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &ii) < 0)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100763 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
764 "%U'", format_vlib_pci_addr, &p->addr);
Damjan Marion23227982018-10-22 13:38:57 +0200765 pci_log_debug (vm, p, "%s index:%u count:%u flags: %s%s%s%s(0x%x)",
766 __func__, ii.index, ii.count,
767 ii.flags & VFIO_IRQ_INFO_EVENTFD ? "eventfd " : "",
768 ii.flags & VFIO_IRQ_INFO_MASKABLE ? "maskable " : "",
769 ii.flags & VFIO_IRQ_INFO_AUTOMASKED ? "automasked " : "",
770 ii.flags & VFIO_IRQ_INFO_NORESIZE ? "noresize " : "",
771 ii.flags);
772 if (ii.count != 1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100773 return clib_error_return (0, "INTx interrupt does not exist on device"
774 "'%U'", format_vlib_pci_addr, &p->addr);
775
776 irq->fd = eventfd (0, EFD_NONBLOCK);
777 if (irq->fd == -1)
778 return clib_error_return_unix (0, "eventfd");
Damjan Marion23227982018-10-22 13:38:57 +0200779
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100780 t.file_descriptor = irq->fd;
781 t.read_function = linux_pci_vfio_intx_read_ready;
782 }
783 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
784 {
785 t.file_descriptor = p->fd;
786 t.read_function = linux_pci_uio_read_ready;
787 }
788 else
789 return 0;
790
791 t.error_function = linux_pci_uio_error_ready;
792 t.private_data = p->handle;
793 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
794 irq->clib_file_index = clib_file_add (&file_main, &t);
795 irq->intx_handler = intx_handler;
796 return 0;
797}
798
799clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200800vlib_pci_register_msix_handler (vlib_main_t * vm, vlib_pci_dev_handle_t h,
801 u32 start, u32 count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100802 pci_msix_handler_function_t * msix_handler)
803{
804 clib_error_t *err = 0;
805 linux_pci_device_t *p = linux_pci_get_device (h);
806 u32 i;
807
808 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
809 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
810 "support");
811
812 /* *INDENT-OFF* */
813 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
814 { .fd = -1});
815 /* *INDENT-ON* */
816
817 for (i = start; i < start + count; i++)
818 {
819 clib_file_t t = { 0 };
820 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
821 ASSERT (irq->fd == -1);
822
823 irq->fd = eventfd (0, EFD_NONBLOCK);
824 if (irq->fd == -1)
825 {
826 err = clib_error_return_unix (0, "eventfd");
827 goto error;
828 }
829
830 t.read_function = linux_pci_vfio_msix_read_ready;
831 t.file_descriptor = irq->fd;
832 t.error_function = linux_pci_vfio_error_ready;
833 t.private_data = p->handle << 16 | i;
834 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
835 &p->addr, i);
836 irq->clib_file_index = clib_file_add (&file_main, &t);
837 irq->msix_handler = msix_handler;
838 }
839
840 return 0;
841
842error:
843 while (i-- > start)
844 {
845 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
846 if (irq->fd != -1)
847 {
848 clib_file_del_by_index (&file_main, irq->clib_file_index);
849 close (irq->fd);
850 irq->fd = -1;
851 }
852 }
853 return err;
854}
855
856clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200857vlib_pci_enable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
858 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100859{
860 linux_pci_device_t *p = linux_pci_get_device (h);
861 int fds[count];
862 int i;
863
864 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
865 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
866 "support");
867
868 for (i = start; i < start + count; i++)
869 {
870 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
871 fds[i] = irq->fd;
872 }
873
Damjan Marion23227982018-10-22 13:38:57 +0200874 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100875 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
876}
877
878clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200879vlib_pci_disable_msix_irq (vlib_main_t * vm, vlib_pci_dev_handle_t h,
880 u16 start, u16 count)
Damjan Marion599a16b2018-03-04 19:35:23 +0100881{
882 linux_pci_device_t *p = linux_pci_get_device (h);
883 int i, fds[count];
884
885 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
886 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
887 "support");
888
889 for (i = start; i < start + count; i++)
890 fds[i] = -1;
891
Damjan Marion23227982018-10-22 13:38:57 +0200892 return vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
Damjan Marion599a16b2018-03-04 19:35:23 +0100893 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
894}
895
Damjan Marioncef87f12017-10-05 15:32:41 +0200896static clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200897add_device_vfio (vlib_main_t * vm, linux_pci_device_t * p,
898 vlib_pci_device_info_t * di, pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200899{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100900 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100901 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +0100902 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +0200903 clib_error_t *err = 0;
904 u8 *s = 0;
Damjan Marion23227982018-10-22 13:38:57 +0200905 int is_noiommu;
Damjan Marioncef87f12017-10-05 15:32:41 +0200906
Damjan Marion2060db82018-03-04 17:37:15 +0100907 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200908
Damjan Marion23227982018-10-22 13:38:57 +0200909 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd, &is_noiommu)))
Damjan Marioncef87f12017-10-05 15:32:41 +0200910 return err;
911
Damjan Marion23227982018-10-22 13:38:57 +0200912 if (is_noiommu == 0)
913 p->supports_va_dma = 1;
914
Damjan Marioncef87f12017-10-05 15:32:41 +0200915 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100916 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200917 {
918 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
919 format_vlib_pci_addr, &di->addr);
920 goto error;
921 }
922
Damjan Marionf313b742018-03-05 14:08:33 +0100923 reg.argsz = sizeof (struct vfio_region_info);
924 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
925 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
926 {
927 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
928 format_vlib_pci_addr, &di->addr);
929 goto error;
930 }
Damjan Marion23227982018-10-22 13:38:57 +0200931
932 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
933 "flags: %s%s%s(0x%x)", __func__,
934 reg.index, reg.size, reg.offset,
935 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
936 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
937 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
938 reg.flags);
939
Damjan Marionf313b742018-03-05 14:08:33 +0100940 p->config_offset = reg.offset;
941 p->config_fd = p->fd;
942
Damjan Marioncef87f12017-10-05 15:32:41 +0200943 /* reset if device supports it */
944 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100945 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200946 {
947 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
948 format_vlib_pci_addr, &di->addr);
949 goto error;
950 }
951
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100952 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +0200953 {
Damjan Marion23227982018-10-22 13:38:57 +0200954 vlib_pci_register_intx_handler (vm, p->handle, r->interrupt_handler);
955 linux_pci_vfio_unmask_intx (vm, p);
Damjan Marioncef87f12017-10-05 15:32:41 +0200956 }
957
Damjan Marion68b4da62018-09-30 18:26:20 +0200958 if (p->supports_va_dma)
959 {
960 vlib_buffer_pool_t *bp;
961 /* *INDENT-OFF* */
962 vec_foreach (bp, buffer_main.buffer_pools)
963 {
964 u32 i;
965 vlib_physmem_map_t *pm;
966 pm = vlib_physmem_get_map (vm, bp->physmem_map_index);
967 for (i = 0; i < pm->n_pages; i++)
968 vfio_map_physmem_page (vm, pm->base + (i << pm->log2_page_size));
969 }
970 /* *INDENT-ON* */
971 }
972
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100973 if (r && r->init_function)
974 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200975
976error:
977 vec_free (s);
978 if (err)
979 {
Damjan Marionf9a968e2018-02-25 23:26:22 +0100980 if (p->fd != -1)
981 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700982 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +0100983 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700984 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +0200985 }
986 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700987}
988
989/* Configuration space read/write. */
990clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +0200991vlib_pci_read_write_config (vlib_main_t * vm, vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +0200992 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400993 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700994{
Damjan Marioncef87f12017-10-05 15:32:41 +0200995 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700996 int n;
997
Ed Warnickecb9cada2015-12-08 15:45:58 -0700998 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100999 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001000 else
Damjan Marionf9a968e2018-02-25 23:26:22 +01001001 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001002
1003 if (n != n_bytes)
1004 return clib_error_return_unix (0, "%s",
1005 read_or_write == VLIB_READ
1006 ? "read" : "write");
1007
1008 return 0;
1009}
1010
1011static clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001012vlib_pci_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 bar, int *fd,
1013 u64 * size, u64 * offset)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001014{
Damjan Marioncef87f12017-10-05 15:32:41 +02001015 linux_pci_device_t *p = linux_pci_get_device (h);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001016 clib_error_t *error = 0;
1017 int _fd = -1;
1018 u64 _size = 0, _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001019
1020 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001021
1022 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001023
Damjan Marion2060db82018-03-04 17:37:15 +01001024 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001025 {
Damjan Marion2060db82018-03-04 17:37:15 +01001026 u8 *file_name;
1027 struct stat stat_buf;
1028 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
1029 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001030
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001031 _fd = open ((char *) file_name, O_RDWR);
1032 if (_fd < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001033 {
1034 error = clib_error_return_unix (0, "open `%s'", file_name);
1035 vec_free (file_name);
1036 return error;
1037 }
1038
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001039 if (fstat (_fd, &stat_buf) < 0)
Damjan Marion2060db82018-03-04 17:37:15 +01001040 {
1041 error = clib_error_return_unix (0, "fstat `%s'", file_name);
1042 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001043 close (_fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001044 return error;
1045 }
1046
1047 vec_free (file_name);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001048 _size = stat_buf.st_size;
1049 _offset = 0;
Damjan Marion2060db82018-03-04 17:37:15 +01001050 }
1051 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001052 {
Damjan Marion2060db82018-03-04 17:37:15 +01001053 struct vfio_region_info reg = { 0 };
1054 reg.argsz = sizeof (struct vfio_region_info);
1055 reg.index = bar;
1056 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1057 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1058 "'%U'", format_vlib_pci_addr,
1059 &p->addr);
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001060 _fd = p->fd;
1061 _size = reg.size;
1062 _offset = reg.offset;
Damjan Marion23227982018-10-22 13:38:57 +02001063 pci_log_debug (vm, p, "%s region_info index:%u size:0x%lx offset:0x%lx "
1064 "flags: %s%s%s(0x%x)", __func__,
1065 reg.index, reg.size, reg.offset,
1066 reg.flags & VFIO_REGION_INFO_FLAG_READ ? "rd " : "",
1067 reg.flags & VFIO_REGION_INFO_FLAG_WRITE ? "wr " : "",
1068 reg.flags & VFIO_REGION_INFO_FLAG_MMAP ? "mmap " : "",
1069 reg.flags);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001070 }
Damjan Marion2060db82018-03-04 17:37:15 +01001071 else
1072 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001073
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001074 *fd = _fd;
1075 *size = _size;
1076 *offset = _offset;
1077
1078 return error;
1079}
1080
1081static clib_error_t *
1082vlib_pci_map_region_int (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1083 u32 bar, u8 * addr, void **result)
1084{
1085 linux_pci_device_t *p = linux_pci_get_device (h);
1086 int fd = -1;
1087 clib_error_t *error;
1088 int flags = MAP_SHARED;
1089 u64 size = 0, offset = 0;
1090
1091 pci_log_debug (vm, p, "map region %u to va %p", bar, addr);
1092
1093 if ((error = vlib_pci_region (vm, h, bar, &fd, &size, &offset)))
1094 return error;
1095
1096 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO && addr != 0)
1097 flags |= MAP_FIXED;
1098
Damjan Marion2060db82018-03-04 17:37:15 +01001099 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001100 if (*result == (void *) -1)
1101 {
Damjan Marion2060db82018-03-04 17:37:15 +01001102 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1103 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001104 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001105 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001106 }
Damjan Marion2060db82018-03-04 17:37:15 +01001107
1108 /* *INDENT-OFF* */
1109 vec_validate_init_empty (p->regions, bar,
1110 (linux_pci_region_t) { .fd = -1});
1111 /* *INDENT-ON* */
1112 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1113 p->regions[bar].fd = fd;
1114 p->regions[bar].addr = *result;
1115 p->regions[bar].size = size;
1116 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001117}
1118
1119clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001120vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource,
1121 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001122{
Damjan Marion23227982018-10-22 13:38:57 +02001123 return (vlib_pci_map_region_int (vm, h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001124}
1125
1126clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001127vlib_pci_map_region_fixed (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1128 u32 resource, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001129{
Damjan Marion23227982018-10-22 13:38:57 +02001130 return (vlib_pci_map_region_int (vm, h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001131}
1132
Damjan Marionf313b742018-03-05 14:08:33 +01001133clib_error_t *
Mohsin Kazmi95276ca2018-09-28 11:45:01 +02001134vlib_pci_io_region (vlib_main_t * vm, vlib_pci_dev_handle_t h, u32 resource)
1135{
1136 linux_pci_device_t *p = linux_pci_get_device (h);
1137 clib_error_t *error = 0;
1138 int fd = -1;
1139 u64 size = 0, offset = 0;
1140
1141 if ((error = vlib_pci_region (vm, h, resource, &fd, &size, &offset)))
1142 return error;
1143
1144 p->io_fd = fd;
1145 p->io_offset = offset;
1146 return error;
1147}
1148
1149clib_error_t *
1150vlib_pci_read_write_io (vlib_main_t * vm, vlib_pci_dev_handle_t h,
1151 vlib_read_or_write_t read_or_write,
1152 uword offset, void *data, u32 length)
1153{
1154 linux_pci_device_t *p = linux_pci_get_device (h);
1155 int n = 0;
1156
1157 if (read_or_write == VLIB_READ)
1158 n = pread (p->io_fd, data, length, p->io_offset + offset);
1159 else
1160 n = pwrite (p->io_fd, data, length, p->io_offset + offset);
1161
1162 if (n != length)
1163 return clib_error_return_unix (0, "%s",
1164 read_or_write == VLIB_READ
1165 ? "read" : "write");
1166 return 0;
1167}
1168
1169clib_error_t *
Damjan Marion68b4da62018-09-30 18:26:20 +02001170vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h, void *ptr)
1171{
1172 linux_pci_device_t *p = linux_pci_get_device (h);
1173
1174 if (!p->supports_va_dma)
1175 return 0;
1176
1177 return vfio_map_physmem_page (vm, ptr);
1178}
1179
1180int
1181vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h)
1182{
1183 linux_pci_device_t *p = linux_pci_get_device (h);
1184
1185 return p->supports_va_dma != 0;
1186}
1187
1188clib_error_t *
Damjan Marion23227982018-10-22 13:38:57 +02001189vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
Damjan Marionf313b742018-03-05 14:08:33 +01001190 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1191{
1192 linux_pci_main_t *lpm = &linux_pci_main;
1193 vlib_pci_device_info_t *di;
1194 linux_pci_device_t *p;
1195 clib_error_t *err = 0;
1196 pci_device_id_t *i;
1197
Damjan Marion23227982018-10-22 13:38:57 +02001198 di = vlib_pci_get_device_info (vm, addr, &err);
Damjan Marionf313b742018-03-05 14:08:33 +01001199
1200 if (err)
1201 return err;
1202 for (i = ids; i->vendor_id != 0; i++)
1203 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1204 break;
1205
1206 if (i->vendor_id == 0)
1207 return clib_error_return (0, "Wrong vendor or device id");
1208
1209 pool_get (lpm->linux_pci_devices, p);
1210 p->handle = p - lpm->linux_pci_devices;
Damjan Marion23227982018-10-22 13:38:57 +02001211 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf313b742018-03-05 14:08:33 +01001212 p->intx_irq.fd = -1;
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001213 /*
1214 * pci io bar read/write fd
1215 */
1216 p->io_fd = -1;
Damjan Marionf313b742018-03-05 14:08:33 +01001217
Damjan Marion23227982018-10-22 13:38:57 +02001218 pci_log_debug (vm, p, "open vid:0x%04x did:0x%04x driver:%s iommu_group:%d",
1219 di->vendor_id, di->device_id, di->driver_name,
1220 di->iommu_group);
1221
1222 if (strncmp ("vfio-pci", (char *) di->driver_name, 8) == 0)
1223 err = add_device_vfio (vm, p, di, 0);
1224 else if (strncmp ("uio_pci_generic", (char *) di->driver_name, 8) == 0)
1225 err = add_device_uio (vm, p, di, 0);
Damjan Marionf313b742018-03-05 14:08:33 +01001226 else
Damjan Marion23227982018-10-22 13:38:57 +02001227 err = clib_error_create ("device not bound to 'vfio-pci' or "
1228 "'uio_pci_generic' kernel module");
Damjan Marionf313b742018-03-05 14:08:33 +01001229 if (err)
1230 goto error;
1231
1232 *handle = p->handle;
1233
1234error:
1235 vlib_pci_free_device_info (di);
1236 if (err)
1237 {
Damjan Marion23227982018-10-22 13:38:57 +02001238 pci_log_err (vm, p, "%U", format_clib_error, err);
Dave Barachb7b92992018-10-17 10:38:51 -04001239 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001240 pool_put (lpm->linux_pci_devices, p);
1241 }
1242
1243 return err;
1244}
1245
1246void
Damjan Marion23227982018-10-22 13:38:57 +02001247vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h)
Damjan Marionf313b742018-03-05 14:08:33 +01001248{
1249 linux_pci_main_t *lpm = &linux_pci_main;
1250 linux_pci_device_t *p = linux_pci_get_device (h);
1251 linux_pci_irq_t *irq;
1252 linux_pci_region_t *res;
1253 clib_error_t *err = 0;
1254
1255 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1256 {
1257 irq = &p->intx_irq;
1258 clib_file_del_by_index (&file_main, irq->clib_file_index);
1259 close (p->config_fd);
Mohsin Kazmi3d3b9552018-10-24 14:05:34 +02001260 if (p->io_fd != -1)
1261 close (p->io_fd);
Damjan Marionf313b742018-03-05 14:08:33 +01001262 }
1263 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1264 {
1265 irq = &p->intx_irq;
1266 /* close INTx irqs */
1267 if (irq->fd != -1)
1268 {
Damjan Marion23227982018-10-22 13:38:57 +02001269 err = vfio_set_irqs (vm, p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001270 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1271 clib_error_free (err);
1272 clib_file_del_by_index (&file_main, irq->clib_file_index);
1273 close (irq->fd);
1274 }
1275
1276 /* close MSI-X irqs */
1277 if (vec_len (p->msix_irqs))
1278 {
Damjan Marion23227982018-10-22 13:38:57 +02001279 err = vfio_set_irqs (vm, p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
Damjan Marionf313b742018-03-05 14:08:33 +01001280 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1281 clib_error_free (err);
1282 /* *INDENT-OFF* */
1283 vec_foreach (irq, p->msix_irqs)
1284 {
1285 if (irq->fd == -1)
1286 continue;
1287 clib_file_del_by_index (&file_main, irq->clib_file_index);
1288 close (irq->fd);
1289 }
1290 /* *INDENT-ON* */
1291 vec_free (p->msix_irqs);
1292 }
1293 }
1294
1295 /* *INDENT-OFF* */
1296 vec_foreach (res, p->regions)
1297 {
1298 if (res->size == 0)
1299 continue;
1300 munmap (res->addr, res->size);
1301 if (res->fd != -1)
1302 close (res->fd);
1303 }
1304 /* *INDENT-ON* */
1305 vec_free (p->regions);
1306
1307 close (p->fd);
Dave Barachb7b92992018-10-17 10:38:51 -04001308 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marionf313b742018-03-05 14:08:33 +01001309 pool_put (lpm->linux_pci_devices, p);
1310}
1311
Dave Barach9b8ffd92016-07-08 08:13:45 -04001312void
Damjan Marion23227982018-10-22 13:38:57 +02001313init_device_from_registered (vlib_main_t * vm, vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001314{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001315 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001316 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001317 pci_device_registration_t *r;
1318 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001319 clib_error_t *err = 0;
1320 linux_pci_device_t *p;
1321
1322 pool_get (lpm->linux_pci_devices, p);
1323 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001324 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001325
Damjan Marion5a206ea2016-05-12 22:11:03 +02001326 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001327
1328 while (r)
1329 {
1330 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001331 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001332 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001333 if (di->iommu_group != -1)
Damjan Marion23227982018-10-22 13:38:57 +02001334 err = add_device_vfio (vm, p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001335 else
Damjan Marion23227982018-10-22 13:38:57 +02001336 err = add_device_uio (vm, p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001337
Damjan Marioncef87f12017-10-05 15:32:41 +02001338 if (err)
1339 clib_error_report (err);
1340 else
1341 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001342 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001343 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001344 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001345
Ed Warnickecb9cada2015-12-08 15:45:58 -07001346 /* No driver, close the PCI config-space FD */
Dave Barachb7b92992018-10-17 10:38:51 -04001347 clib_memset (p, 0, sizeof (linux_pci_device_t));
Damjan Marioncef87f12017-10-05 15:32:41 +02001348 pool_put (lpm->linux_pci_devices, p);
1349}
1350
1351static clib_error_t *
1352scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1353{
1354 vlib_pci_addr_t addr, **addrv = arg;
1355 unformat_input_t input;
1356 clib_error_t *err = 0;
1357
1358 unformat_init_string (&input, (char *) dev_dir_name,
1359 vec_len (dev_dir_name));
1360
1361 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1362 unformat_vlib_pci_addr, &addr))
1363 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1364
1365 unformat_free (&input);
1366
1367 if (err)
1368 return err;
1369
1370 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001371 return 0;
1372}
1373
Damjan Marioncef87f12017-10-05 15:32:41 +02001374static int
1375pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001376{
Damjan Marioncef87f12017-10-05 15:32:41 +02001377 vlib_pci_addr_t *a1 = v1;
1378 vlib_pci_addr_t *a2 = v2;
1379
1380 if (a1->domain > a2->domain)
1381 return 1;
1382 if (a1->domain < a2->domain)
1383 return -1;
1384 if (a1->bus > a2->bus)
1385 return 1;
1386 if (a1->bus < a2->bus)
1387 return -1;
1388 if (a1->slot > a2->slot)
1389 return 1;
1390 if (a1->slot < a2->slot)
1391 return -1;
1392 if (a1->function > a2->function)
1393 return 1;
1394 if (a1->function < a2->function)
1395 return -1;
1396 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001397}
1398
Damjan Marioncef87f12017-10-05 15:32:41 +02001399vlib_pci_addr_t *
1400vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001401{
Damjan Marioncef87f12017-10-05 15:32:41 +02001402 vlib_pci_addr_t *addrs = 0;
1403 clib_error_t *err;
1404 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1405 &addrs, /* scan_dirs */ 0);
1406 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001407 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001408 vec_free (addrs);
1409 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001410 }
1411
Damjan Marioncef87f12017-10-05 15:32:41 +02001412 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001413
Damjan Marioncef87f12017-10-05 15:32:41 +02001414 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001415}
1416
Dave Barach9b8ffd92016-07-08 08:13:45 -04001417clib_error_t *
1418linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001419{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001420 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001421 vlib_pci_addr_t *addr = 0, *addrs;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001422 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001423
1424 pm->vlib_main = vm;
1425
1426 if ((error = vlib_call_init_function (vm, unix_input_init)))
1427 return error;
1428
Dave Barach9b8ffd92016-07-08 08:13:45 -04001429 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001430
Damjan Marioncef87f12017-10-05 15:32:41 +02001431 addrs = vlib_pci_get_all_dev_addrs ();
1432 /* *INDENT-OFF* */
1433 vec_foreach (addr, addrs)
1434 {
1435 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +02001436 if ((d = vlib_pci_get_device_info (vm, addr, 0)))
Damjan Marioncef87f12017-10-05 15:32:41 +02001437 {
Damjan Marion23227982018-10-22 13:38:57 +02001438 init_device_from_registered (vm, d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001439 vlib_pci_free_device_info (d);
1440 }
1441 }
1442 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001443
1444 return error;
1445}
1446
Damjan Marion5a206ea2016-05-12 22:11:03 +02001447VLIB_INIT_FUNCTION (linux_pci_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001448
1449/*
1450 * fd.io coding-style-patch-verification: ON
1451 *
1452 * Local Variables:
1453 * eval: (c-set-style "gnu")
1454 * End:
1455 */