blob: 11480ca081c831c965974b8d6cad83e0de90232a [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
Dave Barach9b8ffd92016-07-08 08:13:45 -040063typedef struct
64{
Damjan Marion2060db82018-03-04 17:37:15 +010065 int fd;
66 void *addr;
67 size_t size;
68} linux_pci_region_t;
69
Damjan Marion599a16b2018-03-04 19:35:23 +010070typedef struct
71{
72 int fd;
73 u32 clib_file_index;
74 union
75 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +010076 pci_intx_handler_function_t *intx_handler;
Damjan Marion599a16b2018-03-04 19:35:23 +010077 pci_msix_handler_function_t *msix_handler;
78 };
79} linux_pci_irq_t;
Damjan Marion2060db82018-03-04 17:37:15 +010080
81typedef enum
82{
83 LINUX_PCI_DEVICE_TYPE_UNKNOWN,
84 LINUX_PCI_DEVICE_TYPE_UIO,
85 LINUX_PCI_DEVICE_TYPE_VFIO,
86} linux_pci_device_type_t;
87
88typedef struct
89{
90 linux_pci_device_type_t type;
Damjan Marioncef87f12017-10-05 15:32:41 +020091 vlib_pci_dev_handle_t handle;
92 vlib_pci_addr_t addr;
Damjan Marion5a206ea2016-05-12 22:11:03 +020093
94 /* Resource file descriptors. */
Damjan Marion2060db82018-03-04 17:37:15 +010095 linux_pci_region_t *regions;
Damjan Marion5a206ea2016-05-12 22:11:03 +020096
97 /* File descriptor for config space read/write. */
98 int config_fd;
Damjan Marionf9a968e2018-02-25 23:26:22 +010099 u64 config_offset;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200100
Damjan Marionf9a968e2018-02-25 23:26:22 +0100101 /* Device File descriptor */
102 int fd;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200103
104 /* Minor device for uio device. */
105 u32 uio_minor;
106
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100107 /* Interrupt handlers */
108 linux_pci_irq_t intx_irq;
Damjan Marion599a16b2018-03-04 19:35:23 +0100109 linux_pci_irq_t *msix_irqs;
Damjan Marioncef87f12017-10-05 15:32:41 +0200110
111 /* private data */
112 uword private_data;
113
Damjan Marion5a206ea2016-05-12 22:11:03 +0200114} linux_pci_device_t;
115
116/* Pool of PCI devices. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400117typedef struct
118{
119 vlib_main_t *vlib_main;
120 linux_pci_device_t *linux_pci_devices;
Damjan Marioncef87f12017-10-05 15:32:41 +0200121
Damjan Marion5a206ea2016-05-12 22:11:03 +0200122} linux_pci_main_t;
123
124extern linux_pci_main_t linux_pci_main;
125
Damjan Marion599a16b2018-03-04 19:35:23 +0100126static linux_pci_device_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200127linux_pci_get_device (vlib_pci_dev_handle_t h)
128{
129 linux_pci_main_t *lpm = &linux_pci_main;
130 return pool_elt_at_index (lpm->linux_pci_devices, h);
131}
132
133uword
134vlib_pci_get_private_data (vlib_pci_dev_handle_t h)
135{
136 linux_pci_device_t *d = linux_pci_get_device (h);
137 return d->private_data;
138}
139
140void
141vlib_pci_set_private_data (vlib_pci_dev_handle_t h, uword private_data)
142{
143 linux_pci_device_t *d = linux_pci_get_device (h);
144 d->private_data = private_data;
145}
146
147vlib_pci_addr_t *
148vlib_pci_get_addr (vlib_pci_dev_handle_t h)
149{
Damjan Marion599a16b2018-03-04 19:35:23 +0100150 linux_pci_device_t *d = linux_pci_get_device (h);
151 return &d->addr;
Damjan Marioncef87f12017-10-05 15:32:41 +0200152}
153
Damjan Marion5a206ea2016-05-12 22:11:03 +0200154/* Call to allocate/initialize the pci subsystem.
155 This is not an init function so that users can explicitly enable
156 pci only when it's needed. */
Dave Barach9b8ffd92016-07-08 08:13:45 -0400157clib_error_t *pci_bus_init (vlib_main_t * vm);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200158
Ed Warnickecb9cada2015-12-08 15:45:58 -0700159linux_pci_main_t linux_pci_main;
160
Damjan Marioncef87f12017-10-05 15:32:41 +0200161vlib_pci_device_info_t *
162vlib_pci_get_device_info (vlib_pci_addr_t * addr, clib_error_t ** error)
163{
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100164 linux_vfio_main_t *lvm = &vfio_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200165 clib_error_t *err;
166 vlib_pci_device_info_t *di;
167 u8 *f = 0;
168 u32 tmp;
169 int fd;
170
171 di = clib_mem_alloc (sizeof (vlib_pci_device_info_t));
172 memset (di, 0, sizeof (vlib_pci_device_info_t));
173 di->addr.as_u32 = addr->as_u32;
174
175 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
176 format_vlib_pci_addr, addr);
177
178 f = format (0, "%v/config%c", dev_dir_name, 0);
179 fd = open ((char *) f, O_RDWR);
180
181 /* Try read-only access if write fails. */
182 if (fd < 0)
183 fd = open ((char *) f, O_RDONLY);
184
185 if (fd < 0)
186 {
187 err = clib_error_return_unix (0, "open `%s'", f);
188 goto error;
189 }
190
191 /* You can only read more that 64 bytes of config space as root; so we try to
192 read the full space but fall back to just the first 64 bytes. */
Damjan Marion44bcc202018-03-04 16:44:26 +0100193 if (read (fd, &di->config_data, sizeof (di->config_data)) <
194 sizeof (di->config0))
Damjan Marioncef87f12017-10-05 15:32:41 +0200195 {
196 err = clib_error_return_unix (0, "read `%s'", f);
197 close (fd);
198 goto error;
199 }
200
201 {
202 static pci_config_header_t all_ones;
203 if (all_ones.vendor_id == 0)
204 memset (&all_ones, ~0, sizeof (all_ones));
205
206 if (!memcmp (&di->config0.header, &all_ones, sizeof (all_ones)))
207 {
208 err = clib_error_return (0, "invalid PCI config for `%s'", f);
209 close (fd);
210 goto error;
211 }
212 }
213
214 if (di->config0.header.header_type == 0)
215 pci_config_type0_little_to_host (&di->config0);
216 else
217 pci_config_type1_little_to_host (&di->config1);
218
219 di->numa_node = -1;
220 vec_reset_length (f);
221 f = format (f, "%v/numa_node%c", dev_dir_name, 0);
222 err = clib_sysfs_read ((char *) f, "%u", &di->numa_node);
223 if (err)
224 {
225 di->numa_node = -1;
226 clib_error_free (err);
227 }
228
229 vec_reset_length (f);
230 f = format (f, "%v/class%c", dev_dir_name, 0);
231 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
232 if (err)
233 goto error;
234 di->device_class = tmp >> 8;
235
236 vec_reset_length (f);
237 f = format (f, "%v/vendor%c", dev_dir_name, 0);
238 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
239 if (err)
240 goto error;
241 di->vendor_id = tmp;
242
243 vec_reset_length (f);
244 f = format (f, "%v/device%c", dev_dir_name, 0);
245 err = clib_sysfs_read ((char *) f, "0x%x", &tmp);
246 if (err)
247 goto error;
248 di->device_id = tmp;
249
250 vec_reset_length (f);
251 f = format (f, "%v/driver%c", dev_dir_name, 0);
252 di->driver_name = clib_sysfs_link_to_name ((char *) f);
253
254 di->iommu_group = -1;
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100255 if (lvm->container_fd != -1)
Damjan Marioncef87f12017-10-05 15:32:41 +0200256 {
257 u8 *tmpstr;
258 vec_reset_length (f);
259 f = format (f, "%v/iommu_group%c", dev_dir_name, 0);
260 tmpstr = clib_sysfs_link_to_name ((char *) f);
261 if (tmpstr)
262 {
263 di->iommu_group = atoi ((char *) tmpstr);
264 vec_free (tmpstr);
265 }
Damjan Marion20ba1642018-03-26 15:35:33 +0200266 vec_reset_length (f);
267 f = format (f, "%v/iommu_group/name%c", dev_dir_name, 0);
268 err = clib_sysfs_read ((char *) f, "%s", &tmpstr);
269 if (err == 0)
270 {
271 if (strncmp ((char *) tmpstr, "vfio-noiommu", 12) == 0)
272 di->flags |= VLIB_PCI_DEVICE_INFO_F_NOIOMMU;
273 vec_free (tmpstr);
274 }
275 else
276 clib_error_free (err);
Damjan Marioncef87f12017-10-05 15:32:41 +0200277 }
278
Damjan Marion96504182017-12-10 23:54:46 +0100279 close (fd);
280
Damjan Marioncef87f12017-10-05 15:32:41 +0200281 vec_reset_length (f);
282 f = format (f, "%v/vpd%c", dev_dir_name, 0);
283 fd = open ((char *) f, O_RDONLY);
284 if (fd >= 0)
285 {
286 while (1)
287 {
288 u8 tag[3];
289 u8 *data = 0;
Damjan Marion96504182017-12-10 23:54:46 +0100290 uword len;
Damjan Marioncef87f12017-10-05 15:32:41 +0200291
292 if (read (fd, &tag, 3) != 3)
293 break;
294
295 if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91)
296 break;
297
298 len = (tag[2] << 8) | tag[1];
299 vec_validate (data, len);
300
301 if (read (fd, data, len) != len)
302 {
303 vec_free (data);
304 break;
305 }
306 if (tag[0] == 0x82)
307 di->product_name = data;
308 else if (tag[0] == 0x90)
309 di->vpd_r = data;
310 else if (tag[0] == 0x91)
311 di->vpd_w = data;
312
313 data = 0;
314 }
315 close (fd);
316 }
317
318 goto done;
319
320error:
321 vlib_pci_free_device_info (di);
322 di = 0;
323
324done:
325 vec_free (f);
326 vec_free (dev_dir_name);
327 if (error)
328 *error = err;
329 else
330 clib_error_free (err);
331 return di;
332}
333
Damjan Marion2752b892017-12-11 15:55:56 +0100334static int
335directory_exists (char *path)
336{
337 struct stat s = { 0 };
338 if (stat (path, &s) == -1)
339 return 0;
340
341 return S_ISDIR (s.st_mode);
342}
343
Damjan Mariona42cd342016-04-13 18:03:20 +0200344clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200345vlib_pci_bind_to_uio (vlib_pci_addr_t * addr, char *uio_drv_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700346{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400347 clib_error_t *error = 0;
Damjan Mariona7f74572017-05-23 18:32:21 +0200348 u8 *s = 0, *driver_name = 0;
Damjan Mariona42cd342016-04-13 18:03:20 +0200349 DIR *dir = 0;
350 struct dirent *e;
Damjan Marioncef87f12017-10-05 15:32:41 +0200351 vlib_pci_device_info_t *di;
Damjan Mariona7f74572017-05-23 18:32:21 +0200352 int fd, clear_driver_override = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200353 u8 *dev_dir_name = format (0, "%s/%U", sysfs_pci_dev_path,
354 format_vlib_pci_addr, addr);
355
356 di = vlib_pci_get_device_info (addr, &error);
357
358 if (error)
359 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700360
Damjan Marion2752b892017-12-11 15:55:56 +0100361 if (strncmp ("auto", uio_drv_name, 5) == 0)
362 {
363 int vfio_pci_loaded = 0;
364
365 if (directory_exists ("/sys/module/vfio_pci"))
366 vfio_pci_loaded = 1;
367
368 if (di->iommu_group != -1)
369 {
370 /* device is bound to IOMMU group */
371 if (!vfio_pci_loaded)
372 {
373 error = clib_error_return (0, "Skipping PCI device %U: device "
374 "is bound to IOMMU group and "
375 "vfio-pci driver is not loaded",
376 format_vlib_pci_addr, addr);
377 goto done;
378 }
379 else
380 uio_drv_name = "vfio-pci";
381 }
382 else
383 {
384 /* device is not bound to IOMMU group so we have multiple options */
385 if (vfio_pci_loaded &&
386 (error = clib_sysfs_write (sysfs_mod_vfio_noiommu, "Y")) == 0)
387 uio_drv_name = "vfio-pci";
388 else if (directory_exists ("/sys/module/uio_pci_generic"))
389 uio_drv_name = "uio_pci_generic";
390 else if (directory_exists ("/sys/module/igb_uio"))
391 uio_drv_name = "igb_uio";
392 else
393 {
394 clib_error_free (error);
395 error = clib_error_return (0, "Skipping PCI device %U: missing "
396 "kernel VFIO or UIO driver",
397 format_vlib_pci_addr, addr);
398 goto done;
399 }
400 clib_error_free (error);
401 }
402 }
403
Damjan Mariona7f74572017-05-23 18:32:21 +0200404 s = format (s, "%v/driver%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200405 driver_name = clib_sysfs_link_to_name ((char *) s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200406 vec_reset_length (s);
407
Damjan Mariona7f74572017-05-23 18:32:21 +0200408 if (driver_name &&
409 ((strcmp ("vfio-pci", (char *) driver_name) == 0) ||
410 (strcmp ("uio_pci_generic", (char *) driver_name) == 0) ||
411 (strcmp ("igb_uio", (char *) driver_name) == 0)))
Damjan Marion3c785e02017-05-08 18:37:54 +0200412 goto done;
Damjan Marion3c785e02017-05-08 18:37:54 +0200413
Damjan Mariona42cd342016-04-13 18:03:20 +0200414 /* walk trough all linux interfaces and if interface belonging to
415 this device is founf check if interface is admin up */
416 dir = opendir ("/sys/class/net");
Damjan Marioncef87f12017-10-05 15:32:41 +0200417 s = format (s, "%U%c", format_vlib_pci_addr, addr, 0);
Damjan Mariona42cd342016-04-13 18:03:20 +0200418
419 if (!dir)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700420 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200421 error = clib_error_return (0, "Skipping PCI device %U: failed to "
422 "read /sys/class/net",
Damjan Marioncef87f12017-10-05 15:32:41 +0200423 format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200424 goto done;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700425 }
426
Dave Barach9b8ffd92016-07-08 08:13:45 -0400427 fd = socket (PF_INET, SOCK_DGRAM, 0);
Chris Luke370e9e32016-07-07 11:01:17 -0400428 if (fd < 0)
429 {
430 error = clib_error_return_unix (0, "socket");
431 goto done;
432 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200433
Dave Barach9b8ffd92016-07-08 08:13:45 -0400434 while ((e = readdir (dir)))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700435 {
Damjan Mariona42cd342016-04-13 18:03:20 +0200436 struct ifreq ifr;
437 struct ethtool_drvinfo drvinfo;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700438
Dave Barach9b8ffd92016-07-08 08:13:45 -0400439 if (e->d_name[0] == '.') /* skip . and .. */
Damjan Mariona42cd342016-04-13 18:03:20 +0200440 continue;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700441
Dave Barach9b8ffd92016-07-08 08:13:45 -0400442 memset (&ifr, 0, sizeof ifr);
443 memset (&drvinfo, 0, sizeof drvinfo);
Damjan Mariona42cd342016-04-13 18:03:20 +0200444 ifr.ifr_data = (char *) &drvinfo;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400445 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
Damjan Mariona42cd342016-04-13 18:03:20 +0200446 drvinfo.cmd = ETHTOOL_GDRVINFO;
Chris Luke370e9e32016-07-07 11:01:17 -0400447 if (ioctl (fd, SIOCETHTOOL, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400448 {
John Loe2821212016-07-13 18:13:02 -0400449 /* Some interfaces (eg "lo") don't support this ioctl */
450 if ((errno != ENOTSUP) && (errno != ENODEV))
Ed Warnicke853e7202016-08-12 11:42:26 -0700451 clib_unix_warning ("ioctl fetch intf %s bus info error",
452 e->d_name);
John Loe2821212016-07-13 18:13:02 -0400453 continue;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400454 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200455
456 if (strcmp ((char *) s, drvinfo.bus_info))
457 continue;
458
Dave Barach9b8ffd92016-07-08 08:13:45 -0400459 memset (&ifr, 0, sizeof (ifr));
Chris Luke370e9e32016-07-07 11:01:17 -0400460 strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1);
461 if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0)
Dave Barach9b8ffd92016-07-08 08:13:45 -0400462 {
463 error = clib_error_return_unix (0, "ioctl fetch intf %s flags",
464 e->d_name);
465 close (fd);
466 goto done;
467 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200468
469 if (ifr.ifr_flags & IFF_UP)
470 {
471 error = clib_error_return (0, "Skipping PCI device %U as host "
472 "interface %s is up",
Damjan Marioncef87f12017-10-05 15:32:41 +0200473 format_vlib_pci_addr, addr, e->d_name);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400474 close (fd);
Damjan Mariona42cd342016-04-13 18:03:20 +0200475 goto done;
476 }
Ed Warnickecb9cada2015-12-08 15:45:58 -0700477 }
478
Damjan Mariona42cd342016-04-13 18:03:20 +0200479 close (fd);
480 vec_reset_length (s);
481
482 s = format (s, "%v/driver/unbind%c", dev_dir_name, 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200483 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona42cd342016-04-13 18:03:20 +0200484 vec_reset_length (s);
485
Damjan Mariona7f74572017-05-23 18:32:21 +0200486 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
487 if (access ((char *) s, F_OK) == 0)
488 {
Damjan Marioncef87f12017-10-05 15:32:41 +0200489 clib_sysfs_write ((char *) s, "%s", uio_drv_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200490 clear_driver_override = 1;
491 }
492 else
493 {
494 vec_reset_length (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200495 s = format (s, "%s/%s/new_id%c", sysfs_pci_drv_path, uio_drv_name, 0);
496 clib_sysfs_write ((char *) s, "0x%04x 0x%04x", di->vendor_id,
497 di->device_id);
Damjan Mariona7f74572017-05-23 18:32:21 +0200498 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200499 vec_reset_length (s);
500
Damjan Marioncef87f12017-10-05 15:32:41 +0200501 s = format (s, "%s/%s/bind%c", sysfs_pci_drv_path, uio_drv_name, 0);
502 clib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, addr);
Damjan Mariona7f74572017-05-23 18:32:21 +0200503 vec_reset_length (s);
504
505 if (clear_driver_override)
506 {
507 s = format (s, "%v/driver_override%c", dev_dir_name, 0);
Damjan Marion01914ce2017-09-14 19:04:50 +0200508 clib_sysfs_write ((char *) s, "%c", 0);
Damjan Mariona7f74572017-05-23 18:32:21 +0200509 vec_reset_length (s);
510 }
Damjan Mariona42cd342016-04-13 18:03:20 +0200511
512done:
513 closedir (dir);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700514 vec_free (s);
Damjan Mariona42cd342016-04-13 18:03:20 +0200515 vec_free (dev_dir_name);
Damjan Mariona7f74572017-05-23 18:32:21 +0200516 vec_free (driver_name);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700517 return error;
518}
519
Ed Warnickecb9cada2015-12-08 15:45:58 -0700520
521static clib_error_t *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400522scan_uio_dir (void *arg, u8 * path_name, u8 * file_name)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700523{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400524 linux_pci_device_t *l = arg;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700525 unformat_input_t input;
526
527 unformat_init_string (&input, (char *) file_name, vec_len (file_name));
528
Dave Barach9b8ffd92016-07-08 08:13:45 -0400529 if (!unformat (&input, "uio%d", &l->uio_minor))
Ed Warnickecb9cada2015-12-08 15:45:58 -0700530 abort ();
531
532 unformat_free (&input);
533 return 0;
534}
535
Dave Barach9b8ffd92016-07-08 08:13:45 -0400536static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100537vfio_set_irqs (linux_pci_device_t * p, u32 index, u32 start, u32 count,
538 u32 flags, int *efds)
539{
540 int data_len = efds ? count * sizeof (int) : 0;
541 u8 buf[sizeof (struct vfio_irq_set) + data_len];
542 struct vfio_irq_info irq_info = { 0 };
543 struct vfio_irq_set *irq_set = (struct vfio_irq_set *) buf;
544
545
546 irq_info.argsz = sizeof (struct vfio_irq_info);
547 irq_info.index = index;
548
549 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
550 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) "
551 "'%U'", format_vlib_pci_addr, &p->addr);
552
553 if (irq_info.count < start + count)
554 return clib_error_return_unix (0, "vfio_set_irq: unexistng interrupt on "
555 "'%U'", format_vlib_pci_addr, &p->addr);
556
557
558 if (efds)
559 {
560 flags |= VFIO_IRQ_SET_DATA_EVENTFD;
561 clib_memcpy (&irq_set->data, efds, data_len);
562 }
563 else
564 flags |= VFIO_IRQ_SET_DATA_NONE;
565
566 ASSERT ((flags & (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD)) !=
567 (VFIO_IRQ_SET_DATA_NONE | VFIO_IRQ_SET_DATA_EVENTFD));
568
569 irq_set->argsz = sizeof (struct vfio_irq_set) + data_len;
570 irq_set->index = index;
571 irq_set->start = start;
572 irq_set->count = count;
573 irq_set->flags = flags;
574
575 if (ioctl (p->fd, VFIO_DEVICE_SET_IRQS, irq_set) < 0)
576 return clib_error_return_unix (0, "%U:ioctl(VFIO_DEVICE_SET_IRQS) "
577 "[index = %u, start = %u, count = %u, "
578 "flags = 0x%x]",
579 format_vlib_pci_addr, &p->addr,
580 index, start, count, flags);
581 return 0;
582}
583
584static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200585linux_pci_uio_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700586{
Damjan Marion5a206ea2016-05-12 22:11:03 +0200587 int __attribute__ ((unused)) rv;
Damjan Marioncef87f12017-10-05 15:32:41 +0200588 vlib_pci_dev_handle_t h = uf->private_data;
589 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100590 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700591
Damjan Marion5a206ea2016-05-12 22:11:03 +0200592 u32 icount;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400593 rv = read (uf->file_descriptor, &icount, 4);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700594
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100595 if (irq->intx_handler)
596 irq->intx_handler (h);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200597
Damjan Marioncef87f12017-10-05 15:32:41 +0200598 vlib_pci_intr_enable (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700599
600 return /* no error */ 0;
601}
602
Dave Barach9b8ffd92016-07-08 08:13:45 -0400603static clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200604linux_pci_vfio_unmask_intx (linux_pci_device_t * d)
605{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100606 return vfio_set_irqs (d, VFIO_PCI_INTX_IRQ_INDEX, 0, 1,
Damjan Marion599a16b2018-03-04 19:35:23 +0100607 VFIO_IRQ_SET_ACTION_UNMASK, 0);
608}
609
Damjan Marioncef87f12017-10-05 15:32:41 +0200610static clib_error_t *
Damjan Marion56dd5432017-09-08 19:52:02 +0200611linux_pci_uio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700612{
613 u32 error_index = (u32) uf->private_data;
614
615 return clib_error_return (0, "pci device %d: error", error_index);
616}
617
Damjan Marioncef87f12017-10-05 15:32:41 +0200618static clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100619linux_pci_vfio_msix_read_ready (clib_file_t * uf)
620{
621 int __attribute__ ((unused)) rv;
622 vlib_pci_dev_handle_t h = uf->private_data >> 16;
623 u16 line = uf->private_data & 0xffff;
624 linux_pci_device_t *p = linux_pci_get_device (h);
625 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, line);
626
627 u64 icount;
628 rv = read (uf->file_descriptor, &icount, sizeof (icount));
629
630 if (irq->msix_handler)
631 irq->msix_handler (h, line);
632
633 return /* no error */ 0;
634}
635
636static clib_error_t *
637linux_pci_vfio_intx_read_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700638{
Damjan Marioncef87f12017-10-05 15:32:41 +0200639 int __attribute__ ((unused)) rv;
640 vlib_pci_dev_handle_t h = uf->private_data;
641 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100642 linux_pci_irq_t *irq = &p->intx_irq;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700643
Damjan Marioncef87f12017-10-05 15:32:41 +0200644 u64 icount;
645 rv = read (uf->file_descriptor, &icount, sizeof (icount));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700646
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100647 if (irq->intx_handler)
648 irq->intx_handler (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700649
Damjan Marioncef87f12017-10-05 15:32:41 +0200650 linux_pci_vfio_unmask_intx (p);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700651
Damjan Marioncef87f12017-10-05 15:32:41 +0200652 return /* no error */ 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700653}
654
Damjan Marioncef87f12017-10-05 15:32:41 +0200655static clib_error_t *
656linux_pci_vfio_error_ready (clib_file_t * uf)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700657{
Damjan Marioncef87f12017-10-05 15:32:41 +0200658 u32 error_index = (u32) uf->private_data;
659
660 return clib_error_return (0, "pci device %d: error", error_index);
661}
662
663static clib_error_t *
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100664add_device_uio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
665 pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200666{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100667 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +0200668 clib_error_t *err = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200669 u8 *s = 0;
670
671 p->addr.as_u32 = di->addr.as_u32;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100672 p->fd = -1;
Damjan Marion2060db82018-03-04 17:37:15 +0100673 p->type = LINUX_PCI_DEVICE_TYPE_UIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200674
675 s = format (s, "%s/%U/config%c", sysfs_pci_dev_path,
676 format_vlib_pci_addr, &di->addr, 0);
677
678 p->config_fd = open ((char *) s, O_RDWR);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100679 p->config_offset = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200680 vec_reset_length (s);
681
682 if (p->config_fd == -1)
683 {
684 err = clib_error_return_unix (0, "open '%s'", s);
685 goto error;
686 }
687
688 s = format (0, "%s/%U/uio", sysfs_pci_dev_path,
689 format_vlib_pci_addr, &di->addr);
690 foreach_directory_file ((char *) s, scan_uio_dir, p, /* scan_dirs */
691 1);
692 vec_reset_length (s);
693
694 s = format (s, "/dev/uio%d%c", p->uio_minor, 0);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100695 p->fd = open ((char *) s, O_RDWR);
696 if (p->fd < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200697 {
698 err = clib_error_return_unix (0, "open '%s'", s);
699 goto error;
700 }
701
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100702 if (r && r->interrupt_handler)
703 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200704
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100705 if (r && r->init_function)
706 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200707
708error:
Damjan Marion20ba1642018-03-26 15:35:33 +0200709 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200710 if (err)
711 {
Damjan Marion96504182017-12-10 23:54:46 +0100712 if (p->config_fd != -1)
713 close (p->config_fd);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100714 if (p->fd != -1)
715 close (p->fd);
Damjan Marioncef87f12017-10-05 15:32:41 +0200716 }
717 return err;
718}
719
Damjan Marion599a16b2018-03-04 19:35:23 +0100720clib_error_t *
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100721vlib_pci_register_intx_handler (vlib_pci_dev_handle_t h,
722 pci_intx_handler_function_t * intx_handler)
723{
724 linux_pci_device_t *p = linux_pci_get_device (h);
725 clib_file_t t = { 0 };
726 linux_pci_irq_t *irq = &p->intx_irq;
727 ASSERT (irq->fd == -1);
728
729 if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
730 {
731 struct vfio_irq_info irq_info = { 0 };
732 irq_info.argsz = sizeof (struct vfio_irq_info);
733 irq_info.index = VFIO_PCI_INTX_IRQ_INDEX;
734 if (ioctl (p->fd, VFIO_DEVICE_GET_IRQ_INFO, &irq_info) < 0)
735 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_IRQ_INFO) '"
736 "%U'", format_vlib_pci_addr, &p->addr);
737 if (irq_info.count != 1)
738 return clib_error_return (0, "INTx interrupt does not exist on device"
739 "'%U'", format_vlib_pci_addr, &p->addr);
740
741 irq->fd = eventfd (0, EFD_NONBLOCK);
742 if (irq->fd == -1)
743 return clib_error_return_unix (0, "eventfd");
744 t.file_descriptor = irq->fd;
745 t.read_function = linux_pci_vfio_intx_read_ready;
746 }
747 else if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
748 {
749 t.file_descriptor = p->fd;
750 t.read_function = linux_pci_uio_read_ready;
751 }
752 else
753 return 0;
754
755 t.error_function = linux_pci_uio_error_ready;
756 t.private_data = p->handle;
757 t.description = format (0, "PCI %U INTx", format_vlib_pci_addr, &p->addr);
758 irq->clib_file_index = clib_file_add (&file_main, &t);
759 irq->intx_handler = intx_handler;
760 return 0;
761}
762
763clib_error_t *
Damjan Marion599a16b2018-03-04 19:35:23 +0100764vlib_pci_register_msix_handler (vlib_pci_dev_handle_t h, u32 start, u32 count,
765 pci_msix_handler_function_t * msix_handler)
766{
767 clib_error_t *err = 0;
768 linux_pci_device_t *p = linux_pci_get_device (h);
769 u32 i;
770
771 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
772 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
773 "support");
774
775 /* *INDENT-OFF* */
776 vec_validate_init_empty (p->msix_irqs, start + count - 1, (linux_pci_irq_t)
777 { .fd = -1});
778 /* *INDENT-ON* */
779
780 for (i = start; i < start + count; i++)
781 {
782 clib_file_t t = { 0 };
783 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
784 ASSERT (irq->fd == -1);
785
786 irq->fd = eventfd (0, EFD_NONBLOCK);
787 if (irq->fd == -1)
788 {
789 err = clib_error_return_unix (0, "eventfd");
790 goto error;
791 }
792
793 t.read_function = linux_pci_vfio_msix_read_ready;
794 t.file_descriptor = irq->fd;
795 t.error_function = linux_pci_vfio_error_ready;
796 t.private_data = p->handle << 16 | i;
797 t.description = format (0, "PCI %U MSI-X #%u", format_vlib_pci_addr,
798 &p->addr, i);
799 irq->clib_file_index = clib_file_add (&file_main, &t);
800 irq->msix_handler = msix_handler;
801 }
802
803 return 0;
804
805error:
806 while (i-- > start)
807 {
808 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
809 if (irq->fd != -1)
810 {
811 clib_file_del_by_index (&file_main, irq->clib_file_index);
812 close (irq->fd);
813 irq->fd = -1;
814 }
815 }
816 return err;
817}
818
819clib_error_t *
820vlib_pci_enable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
821{
822 linux_pci_device_t *p = linux_pci_get_device (h);
823 int fds[count];
824 int i;
825
826 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
827 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
828 "support");
829
830 for (i = start; i < start + count; i++)
831 {
832 linux_pci_irq_t *irq = vec_elt_at_index (p->msix_irqs, i);
833 fds[i] = irq->fd;
834 }
835
836 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
837 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
838}
839
840clib_error_t *
841vlib_pci_disable_msix_irq (vlib_pci_dev_handle_t h, u16 start, u16 count)
842{
843 linux_pci_device_t *p = linux_pci_get_device (h);
844 int i, fds[count];
845
846 if (p->type != LINUX_PCI_DEVICE_TYPE_VFIO)
847 return clib_error_return (0, "vfio driver is needed for MSI-X interrupt "
848 "support");
849
850 for (i = start; i < start + count; i++)
851 fds[i] = -1;
852
853 return vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, start, count,
854 VFIO_IRQ_SET_ACTION_TRIGGER, fds);
855}
856
Damjan Marioncef87f12017-10-05 15:32:41 +0200857static clib_error_t *
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100858add_device_vfio (linux_pci_device_t * p, vlib_pci_device_info_t * di,
859 pci_device_registration_t * r)
Damjan Marioncef87f12017-10-05 15:32:41 +0200860{
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100861 linux_pci_main_t *lpm = &linux_pci_main;
Damjan Marionf9a968e2018-02-25 23:26:22 +0100862 struct vfio_device_info device_info = { 0 };
Damjan Marionf313b742018-03-05 14:08:33 +0100863 struct vfio_region_info reg = { 0 };
Damjan Marioncef87f12017-10-05 15:32:41 +0200864 clib_error_t *err = 0;
865 u8 *s = 0;
Damjan Marioncef87f12017-10-05 15:32:41 +0200866
867 p->addr.as_u32 = di->addr.as_u32;
Damjan Marion2060db82018-03-04 17:37:15 +0100868 p->type = LINUX_PCI_DEVICE_TYPE_VFIO;
Damjan Marioncef87f12017-10-05 15:32:41 +0200869
870 if (di->driver_name == 0 ||
871 (strcmp ("vfio-pci", (char *) di->driver_name) != 0))
872 return clib_error_return (0, "Device '%U' (iommu group %d) not bound to "
873 "vfio-pci", format_vlib_pci_addr, &di->addr,
874 di->iommu_group);
875
Damjan Marion1ba0fa42018-03-04 17:19:08 +0100876 if ((err = linux_vfio_group_get_device_fd (&p->addr, &p->fd)))
Damjan Marioncef87f12017-10-05 15:32:41 +0200877 return err;
878
Damjan Marioncef87f12017-10-05 15:32:41 +0200879 device_info.argsz = sizeof (device_info);
Damjan Marionf9a968e2018-02-25 23:26:22 +0100880 if (ioctl (p->fd, VFIO_DEVICE_GET_INFO, &device_info) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200881 {
882 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
883 format_vlib_pci_addr, &di->addr);
884 goto error;
885 }
886
Damjan Marionf313b742018-03-05 14:08:33 +0100887 reg.argsz = sizeof (struct vfio_region_info);
888 reg.index = VFIO_PCI_CONFIG_REGION_INDEX;
889 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
890 {
891 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) '%U'",
892 format_vlib_pci_addr, &di->addr);
893 goto error;
894 }
895 p->config_offset = reg.offset;
896 p->config_fd = p->fd;
897
Damjan Marioncef87f12017-10-05 15:32:41 +0200898 /* reset if device supports it */
899 if (device_info.flags & VFIO_DEVICE_FLAGS_RESET)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100900 if (ioctl (p->fd, VFIO_DEVICE_RESET) < 0)
Damjan Marioncef87f12017-10-05 15:32:41 +0200901 {
902 err = clib_error_return_unix (0, "ioctl(VFIO_DEVICE_RESET) '%U'",
903 format_vlib_pci_addr, &di->addr);
904 goto error;
905 }
906
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100907 if (r && r->interrupt_handler)
Damjan Marioncef87f12017-10-05 15:32:41 +0200908 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100909 vlib_pci_register_intx_handler (p->handle, r->interrupt_handler);
Damjan Marioncef87f12017-10-05 15:32:41 +0200910 linux_pci_vfio_unmask_intx (p);
911 }
912
Damjan Mariond5ded2d2018-03-05 10:18:50 +0100913 if (r && r->init_function)
914 err = r->init_function (lpm->vlib_main, p->handle);
Damjan Marioncef87f12017-10-05 15:32:41 +0200915
916error:
917 vec_free (s);
918 if (err)
919 {
Damjan Marionf9a968e2018-02-25 23:26:22 +0100920 if (p->fd != -1)
921 close (p->fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700922 if (p->config_fd != -1 && p->config_fd != p->fd)
Damjan Marion96504182017-12-10 23:54:46 +0100923 close (p->config_fd);
Chris Luke30684ac2018-03-29 12:56:58 -0700924 p->config_fd = p->fd = -1;
Damjan Marioncef87f12017-10-05 15:32:41 +0200925 }
926 return err;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700927}
928
929/* Configuration space read/write. */
930clib_error_t *
Damjan Marioncef87f12017-10-05 15:32:41 +0200931vlib_pci_read_write_config (vlib_pci_dev_handle_t h,
Damjan Marion5a206ea2016-05-12 22:11:03 +0200932 vlib_read_or_write_t read_or_write,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400933 uword address, void *data, u32 n_bytes)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700934{
Damjan Marioncef87f12017-10-05 15:32:41 +0200935 linux_pci_device_t *p = linux_pci_get_device (h);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700936 int n;
937
Ed Warnickecb9cada2015-12-08 15:45:58 -0700938 if (read_or_write == VLIB_READ)
Damjan Marionf9a968e2018-02-25 23:26:22 +0100939 n = pread (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700940 else
Damjan Marionf9a968e2018-02-25 23:26:22 +0100941 n = pwrite (p->config_fd, data, n_bytes, p->config_offset + address);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700942
943 if (n != n_bytes)
944 return clib_error_return_unix (0, "%s",
945 read_or_write == VLIB_READ
946 ? "read" : "write");
947
948 return 0;
949}
950
951static clib_error_t *
Damjan Marion2060db82018-03-04 17:37:15 +0100952vlib_pci_map_region_int (vlib_pci_dev_handle_t h,
953 u32 bar, u8 * addr, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700954{
Damjan Marioncef87f12017-10-05 15:32:41 +0200955 linux_pci_device_t *p = linux_pci_get_device (h);
Damjan Marion2060db82018-03-04 17:37:15 +0100956 int fd = -1;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400957 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700958 int flags = MAP_SHARED;
Damjan Marion2060db82018-03-04 17:37:15 +0100959 u64 size = 0, offset = 0;
960
961 ASSERT (bar <= 5);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700962
963 error = 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -0700964
Damjan Marion2060db82018-03-04 17:37:15 +0100965 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700966 {
Damjan Marion2060db82018-03-04 17:37:15 +0100967 u8 *file_name;
968 struct stat stat_buf;
969 file_name = format (0, "%s/%U/resource%d%c", sysfs_pci_dev_path,
970 format_vlib_pci_addr, &p->addr, bar, 0);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700971
Damjan Marion2060db82018-03-04 17:37:15 +0100972 fd = open ((char *) file_name, O_RDWR);
973 if (fd < 0)
974 {
975 error = clib_error_return_unix (0, "open `%s'", file_name);
976 vec_free (file_name);
977 return error;
978 }
979
980 if (fstat (fd, &stat_buf) < 0)
981 {
982 error = clib_error_return_unix (0, "fstat `%s'", file_name);
983 vec_free (file_name);
984 close (fd);
985 return error;
986 }
987
988 vec_free (file_name);
989 if (addr != 0)
990 flags |= MAP_FIXED;
991 size = stat_buf.st_size;
992 offset = 0;
993 }
994 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700995 {
Damjan Marion2060db82018-03-04 17:37:15 +0100996 struct vfio_region_info reg = { 0 };
997 reg.argsz = sizeof (struct vfio_region_info);
998 reg.index = bar;
999 if (ioctl (p->fd, VFIO_DEVICE_GET_REGION_INFO, &reg) < 0)
1000 return clib_error_return_unix (0, "ioctl(VFIO_DEVICE_GET_INFO) "
1001 "'%U'", format_vlib_pci_addr,
1002 &p->addr);
1003 fd = p->fd;
1004 size = reg.size;
1005 offset = reg.offset;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001006 }
Damjan Marion2060db82018-03-04 17:37:15 +01001007 else
1008 ASSERT (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001009
Damjan Marion2060db82018-03-04 17:37:15 +01001010 *result = mmap (addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001011 if (*result == (void *) -1)
1012 {
Damjan Marion2060db82018-03-04 17:37:15 +01001013 error = clib_error_return_unix (0, "mmap `BAR%u'", bar);
1014 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001015 close (fd);
Damjan Marion2060db82018-03-04 17:37:15 +01001016 return error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001017 }
Damjan Marion2060db82018-03-04 17:37:15 +01001018
1019 /* *INDENT-OFF* */
1020 vec_validate_init_empty (p->regions, bar,
1021 (linux_pci_region_t) { .fd = -1});
1022 /* *INDENT-ON* */
1023 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1024 p->regions[bar].fd = fd;
1025 p->regions[bar].addr = *result;
1026 p->regions[bar].size = size;
1027 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001028}
1029
1030clib_error_t *
Damjan Marion2060db82018-03-04 17:37:15 +01001031vlib_pci_map_region (vlib_pci_dev_handle_t h, u32 resource, void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001032{
Damjan Marion2060db82018-03-04 17:37:15 +01001033 return (vlib_pci_map_region_int (h, resource, 0 /* addr */ , result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001034}
1035
1036clib_error_t *
Damjan Marion2060db82018-03-04 17:37:15 +01001037vlib_pci_map_region_fixed (vlib_pci_dev_handle_t h, u32 resource, u8 * addr,
1038 void **result)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001039{
Damjan Marion2060db82018-03-04 17:37:15 +01001040 return (vlib_pci_map_region_int (h, resource, addr, result));
Ed Warnickecb9cada2015-12-08 15:45:58 -07001041}
1042
Damjan Marionf313b742018-03-05 14:08:33 +01001043clib_error_t *
1044vlib_pci_device_open (vlib_pci_addr_t * addr,
1045 pci_device_id_t ids[], vlib_pci_dev_handle_t * handle)
1046{
1047 linux_pci_main_t *lpm = &linux_pci_main;
1048 vlib_pci_device_info_t *di;
1049 linux_pci_device_t *p;
1050 clib_error_t *err = 0;
1051 pci_device_id_t *i;
1052
1053 di = vlib_pci_get_device_info (addr, &err);
1054
1055 if (err)
1056 return err;
1057 for (i = ids; i->vendor_id != 0; i++)
1058 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
1059 break;
1060
1061 if (i->vendor_id == 0)
1062 return clib_error_return (0, "Wrong vendor or device id");
1063
1064 pool_get (lpm->linux_pci_devices, p);
1065 p->handle = p - lpm->linux_pci_devices;
1066 p->intx_irq.fd = -1;
1067
1068 if (di->iommu_group != -1)
1069 err = add_device_vfio (p, di, 0);
1070 else
1071 err = add_device_uio (p, di, 0);
1072 if (err)
1073 goto error;
1074
1075 *handle = p->handle;
1076
1077error:
1078 vlib_pci_free_device_info (di);
1079 if (err)
1080 {
1081 memset (p, 0, sizeof (linux_pci_device_t));
1082 pool_put (lpm->linux_pci_devices, p);
1083 }
1084
1085 return err;
1086}
1087
1088void
1089vlib_pci_device_close (vlib_pci_dev_handle_t h)
1090{
1091 linux_pci_main_t *lpm = &linux_pci_main;
1092 linux_pci_device_t *p = linux_pci_get_device (h);
1093 linux_pci_irq_t *irq;
1094 linux_pci_region_t *res;
1095 clib_error_t *err = 0;
1096
1097 if (p->type == LINUX_PCI_DEVICE_TYPE_UIO)
1098 {
1099 irq = &p->intx_irq;
1100 clib_file_del_by_index (&file_main, irq->clib_file_index);
1101 close (p->config_fd);
1102 }
1103 else if (p->type == LINUX_PCI_DEVICE_TYPE_VFIO)
1104 {
1105 irq = &p->intx_irq;
1106 /* close INTx irqs */
1107 if (irq->fd != -1)
1108 {
1109 err = vfio_set_irqs (p, VFIO_PCI_INTX_IRQ_INDEX, 0, 0,
1110 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1111 clib_error_free (err);
1112 clib_file_del_by_index (&file_main, irq->clib_file_index);
1113 close (irq->fd);
1114 }
1115
1116 /* close MSI-X irqs */
1117 if (vec_len (p->msix_irqs))
1118 {
1119 err = vfio_set_irqs (p, VFIO_PCI_MSIX_IRQ_INDEX, 0, 0,
1120 VFIO_IRQ_SET_ACTION_TRIGGER, 0);
1121 clib_error_free (err);
1122 /* *INDENT-OFF* */
1123 vec_foreach (irq, p->msix_irqs)
1124 {
1125 if (irq->fd == -1)
1126 continue;
1127 clib_file_del_by_index (&file_main, irq->clib_file_index);
1128 close (irq->fd);
1129 }
1130 /* *INDENT-ON* */
1131 vec_free (p->msix_irqs);
1132 }
1133 }
1134
1135 /* *INDENT-OFF* */
1136 vec_foreach (res, p->regions)
1137 {
1138 if (res->size == 0)
1139 continue;
1140 munmap (res->addr, res->size);
1141 if (res->fd != -1)
1142 close (res->fd);
1143 }
1144 /* *INDENT-ON* */
1145 vec_free (p->regions);
1146
1147 close (p->fd);
1148 memset (p, 0, sizeof (linux_pci_device_t));
1149 pool_put (lpm->linux_pci_devices, p);
1150}
1151
Dave Barach9b8ffd92016-07-08 08:13:45 -04001152void
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001153init_device_from_registered (vlib_pci_device_info_t * di)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001154{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001155 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001156 linux_pci_main_t *lpm = &linux_pci_main;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001157 pci_device_registration_t *r;
1158 pci_device_id_t *i;
Damjan Marioncef87f12017-10-05 15:32:41 +02001159 clib_error_t *err = 0;
1160 linux_pci_device_t *p;
1161
1162 pool_get (lpm->linux_pci_devices, p);
1163 p->handle = p - lpm->linux_pci_devices;
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001164 p->intx_irq.fd = -1;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001165
Damjan Marion5a206ea2016-05-12 22:11:03 +02001166 r = pm->pci_device_registrations;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001167
1168 while (r)
1169 {
1170 for (i = r->supported_devices; i->vendor_id != 0; i++)
Damjan Marion3a593822018-02-17 14:06:53 +01001171 if (i->vendor_id == di->vendor_id && i->device_id == di->device_id)
Dave Barach9b8ffd92016-07-08 08:13:45 -04001172 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001173 if (di->iommu_group != -1)
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001174 err = add_device_vfio (p, di, r);
Damjan Marioncef87f12017-10-05 15:32:41 +02001175 else
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001176 err = add_device_uio (p, di, r);
Damjan Marion5a206ea2016-05-12 22:11:03 +02001177
Damjan Marioncef87f12017-10-05 15:32:41 +02001178 if (err)
1179 clib_error_report (err);
1180 else
1181 return;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001182 }
Ed Warnickecb9cada2015-12-08 15:45:58 -07001183 r = r->next_registration;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001184 }
Damjan Marioncef87f12017-10-05 15:32:41 +02001185
Ed Warnickecb9cada2015-12-08 15:45:58 -07001186 /* No driver, close the PCI config-space FD */
Damjan Marioncef87f12017-10-05 15:32:41 +02001187 memset (p, 0, sizeof (linux_pci_device_t));
1188 pool_put (lpm->linux_pci_devices, p);
1189}
1190
1191static clib_error_t *
1192scan_pci_addr (void *arg, u8 * dev_dir_name, u8 * ignored)
1193{
1194 vlib_pci_addr_t addr, **addrv = arg;
1195 unformat_input_t input;
1196 clib_error_t *err = 0;
1197
1198 unformat_init_string (&input, (char *) dev_dir_name,
1199 vec_len (dev_dir_name));
1200
1201 if (!unformat (&input, "/sys/bus/pci/devices/%U",
1202 unformat_vlib_pci_addr, &addr))
1203 err = clib_error_return (0, "unformat error `%v`", dev_dir_name);
1204
1205 unformat_free (&input);
1206
1207 if (err)
1208 return err;
1209
1210 vec_add1 (*addrv, addr);
Ed Warnickecb9cada2015-12-08 15:45:58 -07001211 return 0;
1212}
1213
Damjan Marioncef87f12017-10-05 15:32:41 +02001214static int
1215pci_addr_cmp (void *v1, void *v2)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001216{
Damjan Marioncef87f12017-10-05 15:32:41 +02001217 vlib_pci_addr_t *a1 = v1;
1218 vlib_pci_addr_t *a2 = v2;
1219
1220 if (a1->domain > a2->domain)
1221 return 1;
1222 if (a1->domain < a2->domain)
1223 return -1;
1224 if (a1->bus > a2->bus)
1225 return 1;
1226 if (a1->bus < a2->bus)
1227 return -1;
1228 if (a1->slot > a2->slot)
1229 return 1;
1230 if (a1->slot < a2->slot)
1231 return -1;
1232 if (a1->function > a2->function)
1233 return 1;
1234 if (a1->function < a2->function)
1235 return -1;
1236 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001237}
1238
Damjan Marioncef87f12017-10-05 15:32:41 +02001239vlib_pci_addr_t *
1240vlib_pci_get_all_dev_addrs ()
Ed Warnickecb9cada2015-12-08 15:45:58 -07001241{
Damjan Marioncef87f12017-10-05 15:32:41 +02001242 vlib_pci_addr_t *addrs = 0;
1243 clib_error_t *err;
1244 err = foreach_directory_file ((char *) sysfs_pci_dev_path, scan_pci_addr,
1245 &addrs, /* scan_dirs */ 0);
1246 if (err)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001247 {
Damjan Marioncef87f12017-10-05 15:32:41 +02001248 vec_free (addrs);
1249 return 0;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001250 }
1251
Damjan Marioncef87f12017-10-05 15:32:41 +02001252 vec_sort_with_function (addrs, pci_addr_cmp);
Damjan Mariona42cd342016-04-13 18:03:20 +02001253
Damjan Marioncef87f12017-10-05 15:32:41 +02001254 return addrs;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001255}
1256
Dave Barach9b8ffd92016-07-08 08:13:45 -04001257clib_error_t *
1258linux_pci_init (vlib_main_t * vm)
Ed Warnickecb9cada2015-12-08 15:45:58 -07001259{
Dave Barach9b8ffd92016-07-08 08:13:45 -04001260 vlib_pci_main_t *pm = &pci_main;
Damjan Marioncef87f12017-10-05 15:32:41 +02001261 vlib_pci_addr_t *addr = 0, *addrs;
Dave Barach9b8ffd92016-07-08 08:13:45 -04001262 clib_error_t *error;
Ed Warnickecb9cada2015-12-08 15:45:58 -07001263
1264 pm->vlib_main = vm;
1265
1266 if ((error = vlib_call_init_function (vm, unix_input_init)))
1267 return error;
1268
Dave Barach9b8ffd92016-07-08 08:13:45 -04001269 ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32));
Damjan Mariona42cd342016-04-13 18:03:20 +02001270
Damjan Marioncef87f12017-10-05 15:32:41 +02001271 addrs = vlib_pci_get_all_dev_addrs ();
1272 /* *INDENT-OFF* */
1273 vec_foreach (addr, addrs)
1274 {
1275 vlib_pci_device_info_t *d;
1276 if ((d = vlib_pci_get_device_info (addr, 0)))
1277 {
Damjan Mariond5ded2d2018-03-05 10:18:50 +01001278 init_device_from_registered (d);
Damjan Marioncef87f12017-10-05 15:32:41 +02001279 vlib_pci_free_device_info (d);
1280 }
1281 }
1282 /* *INDENT-ON* */
Ed Warnickecb9cada2015-12-08 15:45:58 -07001283
1284 return error;
1285}
1286
Damjan Marion5a206ea2016-05-12 22:11:03 +02001287VLIB_INIT_FUNCTION (linux_pci_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -04001288
1289/*
1290 * fd.io coding-style-patch-verification: ON
1291 *
1292 * Local Variables:
1293 * eval: (c-set-style "gnu")
1294 * End:
1295 */