Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | * pci.c: Linux user space PCI bus management. |
| 17 | * |
| 18 | * Copyright (c) 2008 Eliot Dresselhaus |
| 19 | * |
| 20 | * Permission is hereby granted, free of charge, to any person obtaining |
| 21 | * a copy of this software and associated documentation files (the |
| 22 | * "Software"), to deal in the Software without restriction, including |
| 23 | * without limitation the rights to use, copy, modify, merge, publish, |
| 24 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 25 | * permit persons to whom the Software is furnished to do so, subject to |
| 26 | * the following conditions: |
| 27 | * |
| 28 | * The above copyright notice and this permission notice shall be |
| 29 | * included in all copies or substantial portions of the Software. |
| 30 | * |
| 31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 33 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 35 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 36 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 37 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 38 | */ |
| 39 | |
| 40 | #include <vlib/vlib.h> |
| 41 | #include <vlib/pci/pci.h> |
| 42 | #include <vlib/unix/unix.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 43 | |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <dirent.h> |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 48 | #include <sys/ioctl.h> |
| 49 | #include <net/if.h> |
| 50 | #include <linux/ethtool.h> |
| 51 | #include <linux/sockios.h> |
| 52 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 55 | /* /sys/bus/pci/devices/... directory name for this device. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 56 | u8 *dev_dir_name; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 57 | |
| 58 | /* Resource file descriptors. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 59 | int *resource_fds; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 60 | |
| 61 | /* File descriptor for config space read/write. */ |
| 62 | int config_fd; |
| 63 | |
| 64 | /* File descriptor for /dev/uio%d */ |
| 65 | int uio_fd; |
| 66 | |
| 67 | /* Minor device for uio device. */ |
| 68 | u32 uio_minor; |
| 69 | |
| 70 | /* Index given by unix_file_add. */ |
| 71 | u32 unix_file_index; |
| 72 | |
| 73 | } linux_pci_device_t; |
| 74 | |
| 75 | /* Pool of PCI devices. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 76 | typedef struct |
| 77 | { |
| 78 | vlib_main_t *vlib_main; |
| 79 | linux_pci_device_t *linux_pci_devices; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 80 | } linux_pci_main_t; |
| 81 | |
| 82 | extern linux_pci_main_t linux_pci_main; |
| 83 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 84 | /* Call to allocate/initialize the pci subsystem. |
| 85 | This is not an init function so that users can explicitly enable |
| 86 | pci only when it's needed. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 87 | clib_error_t *pci_bus_init (vlib_main_t * vm); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 88 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 89 | clib_error_t *vlib_pci_bind_to_uio (vlib_pci_device_t * d, |
| 90 | char *uio_driver_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | |
| 92 | linux_pci_main_t linux_pci_main; |
| 93 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 94 | clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 95 | vlib_pci_bind_to_uio (vlib_pci_device_t * d, char *uio_driver_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 97 | clib_error_t *error = 0; |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 98 | u8 *s = 0, *driver_name = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 99 | DIR *dir = 0; |
| 100 | struct dirent *e; |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 101 | int fd, clear_driver_override = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 102 | u8 *dev_dir_name = format (0, "/sys/bus/pci/devices/%U", |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 103 | format_vlib_pci_addr, &d->bus_address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 105 | s = format (s, "%v/driver%c", dev_dir_name, 0); |
| 106 | driver_name = vlib_sysfs_link_to_name ((char *) s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 107 | vec_reset_length (s); |
| 108 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 109 | if (driver_name && |
| 110 | ((strcmp ("vfio-pci", (char *) driver_name) == 0) || |
| 111 | (strcmp ("uio_pci_generic", (char *) driver_name) == 0) || |
| 112 | (strcmp ("igb_uio", (char *) driver_name) == 0))) |
Damjan Marion | 3c785e0 | 2017-05-08 18:37:54 +0200 | [diff] [blame] | 113 | goto done; |
Damjan Marion | 3c785e0 | 2017-05-08 18:37:54 +0200 | [diff] [blame] | 114 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 115 | /* walk trough all linux interfaces and if interface belonging to |
| 116 | this device is founf check if interface is admin up */ |
| 117 | dir = opendir ("/sys/class/net"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 118 | s = format (s, "%U%c", format_vlib_pci_addr, &d->bus_address, 0); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 119 | |
| 120 | if (!dir) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 122 | error = clib_error_return (0, "Skipping PCI device %U: failed to " |
| 123 | "read /sys/class/net", |
| 124 | format_vlib_pci_addr, &d->bus_address); |
| 125 | goto done; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 128 | fd = socket (PF_INET, SOCK_DGRAM, 0); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 129 | if (fd < 0) |
| 130 | { |
| 131 | error = clib_error_return_unix (0, "socket"); |
| 132 | goto done; |
| 133 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 134 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 135 | while ((e = readdir (dir))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 136 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 137 | struct ifreq ifr; |
| 138 | struct ethtool_drvinfo drvinfo; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 139 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 140 | if (e->d_name[0] == '.') /* skip . and .. */ |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 141 | continue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 142 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 143 | memset (&ifr, 0, sizeof ifr); |
| 144 | memset (&drvinfo, 0, sizeof drvinfo); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 145 | ifr.ifr_data = (char *) &drvinfo; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 146 | strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 147 | drvinfo.cmd = ETHTOOL_GDRVINFO; |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 148 | if (ioctl (fd, SIOCETHTOOL, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 149 | { |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 150 | /* Some interfaces (eg "lo") don't support this ioctl */ |
| 151 | if ((errno != ENOTSUP) && (errno != ENODEV)) |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame] | 152 | clib_unix_warning ("ioctl fetch intf %s bus info error", |
| 153 | e->d_name); |
John Lo | e282121 | 2016-07-13 18:13:02 -0400 | [diff] [blame] | 154 | continue; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 155 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 156 | |
| 157 | if (strcmp ((char *) s, drvinfo.bus_info)) |
| 158 | continue; |
| 159 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 160 | memset (&ifr, 0, sizeof (ifr)); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 161 | strncpy (ifr.ifr_name, e->d_name, IFNAMSIZ - 1); |
| 162 | if (ioctl (fd, SIOCGIFFLAGS, &ifr) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 163 | { |
| 164 | error = clib_error_return_unix (0, "ioctl fetch intf %s flags", |
| 165 | e->d_name); |
| 166 | close (fd); |
| 167 | goto done; |
| 168 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 169 | |
| 170 | if (ifr.ifr_flags & IFF_UP) |
| 171 | { |
| 172 | error = clib_error_return (0, "Skipping PCI device %U as host " |
| 173 | "interface %s is up", |
| 174 | format_vlib_pci_addr, &d->bus_address, |
| 175 | e->d_name); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 176 | close (fd); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 177 | goto done; |
| 178 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 181 | close (fd); |
| 182 | vec_reset_length (s); |
| 183 | |
| 184 | s = format (s, "%v/driver/unbind%c", dev_dir_name, 0); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 185 | vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 186 | vec_reset_length (s); |
| 187 | |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 188 | s = format (s, "%v/driver_override%c", dev_dir_name, 0); |
| 189 | if (access ((char *) s, F_OK) == 0) |
| 190 | { |
| 191 | vlib_sysfs_write ((char *) s, "%s", uio_driver_name); |
| 192 | clear_driver_override = 1; |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | vec_reset_length (s); |
| 197 | s = format (s, "/sys/bus/pci/drivers/%s/new_id%c", uio_driver_name, 0); |
| 198 | vlib_sysfs_write ((char *) s, "0x%04x 0x%04x", d->vendor_id, |
| 199 | d->device_id); |
| 200 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 201 | vec_reset_length (s); |
| 202 | |
| 203 | s = format (s, "/sys/bus/pci/drivers/%s/bind%c", uio_driver_name, 0); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 204 | vlib_sysfs_write ((char *) s, "%U", format_vlib_pci_addr, &d->bus_address); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 205 | vec_reset_length (s); |
| 206 | |
| 207 | if (clear_driver_override) |
| 208 | { |
| 209 | s = format (s, "%v/driver_override%c", dev_dir_name, 0); |
| 210 | vlib_sysfs_write ((char *) s, "%c", 0); |
| 211 | vec_reset_length (s); |
| 212 | } |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 213 | |
| 214 | done: |
| 215 | closedir (dir); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 216 | vec_free (s); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 217 | vec_free (dev_dir_name); |
Damjan Marion | a7f7457 | 2017-05-23 18:32:21 +0200 | [diff] [blame] | 218 | vec_free (driver_name); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 219 | return error; |
| 220 | } |
| 221 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 222 | |
| 223 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 224 | scan_uio_dir (void *arg, u8 * path_name, u8 * file_name) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 225 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 226 | linux_pci_device_t *l = arg; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 227 | unformat_input_t input; |
| 228 | |
| 229 | unformat_init_string (&input, (char *) file_name, vec_len (file_name)); |
| 230 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 231 | if (!unformat (&input, "uio%d", &l->uio_minor)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 232 | abort (); |
| 233 | |
| 234 | unformat_free (&input); |
| 235 | return 0; |
| 236 | } |
| 237 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 238 | static clib_error_t * |
| 239 | linux_pci_uio_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 240 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 241 | vlib_pci_main_t *pm = &pci_main; |
| 242 | vlib_pci_device_t *d; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 243 | int __attribute__ ((unused)) rv; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 244 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 245 | u32 icount; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 246 | rv = read (uf->file_descriptor, &icount, 4); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 248 | d = pool_elt_at_index (pm->pci_devs, uf->private_data); |
| 249 | |
| 250 | if (d->interrupt_handler) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 251 | d->interrupt_handler (d); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 252 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 253 | vlib_pci_intr_enable (d); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 254 | |
| 255 | return /* no error */ 0; |
| 256 | } |
| 257 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 258 | static clib_error_t * |
| 259 | linux_pci_uio_error_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 260 | { |
| 261 | u32 error_index = (u32) uf->private_data; |
| 262 | |
| 263 | return clib_error_return (0, "pci device %d: error", error_index); |
| 264 | } |
| 265 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 266 | static void |
| 267 | add_device (vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 268 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 269 | vlib_pci_main_t *pm = &pci_main; |
| 270 | linux_pci_main_t *lpm = &linux_pci_main; |
| 271 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 272 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 273 | pool_get (lpm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 274 | l[0] = pdev[0]; |
| 275 | |
| 276 | l->dev_dir_name = vec_dup (l->dev_dir_name); |
| 277 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 278 | dev->os_handle = l - lpm->linux_pci_devices; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 279 | |
| 280 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 281 | u8 *uio_dir = format (0, "%s/uio", l->dev_dir_name); |
| 282 | foreach_directory_file ((char *) uio_dir, scan_uio_dir, l, /* scan_dirs */ |
| 283 | 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 284 | vec_free (uio_dir); |
| 285 | } |
| 286 | |
| 287 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 288 | char *uio_name = (char *) format (0, "/dev/uio%d%c", l->uio_minor, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 289 | l->uio_fd = open (uio_name, O_RDWR); |
| 290 | if (l->uio_fd < 0) |
| 291 | clib_unix_error ("open `%s'", uio_name); |
| 292 | vec_free (uio_name); |
| 293 | } |
| 294 | |
| 295 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 296 | unix_file_t template = { 0 }; |
| 297 | unix_main_t *um = &unix_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 298 | |
| 299 | template.read_function = linux_pci_uio_read_ready; |
| 300 | template.file_descriptor = l->uio_fd; |
| 301 | template.error_function = linux_pci_uio_error_ready; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 302 | template.private_data = dev - pm->pci_devs; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 303 | |
| 304 | l->unix_file_index = unix_file_add (um, &template); |
| 305 | } |
| 306 | } |
| 307 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 308 | static void |
| 309 | linux_pci_device_free (linux_pci_device_t * l) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 310 | { |
| 311 | int i; |
| 312 | for (i = 0; i < vec_len (l->resource_fds); i++) |
| 313 | if (l->resource_fds[i] > 0) |
| 314 | close (l->resource_fds[i]); |
| 315 | if (l->config_fd > 0) |
| 316 | close (l->config_fd); |
| 317 | if (l->uio_fd > 0) |
| 318 | close (l->uio_fd); |
| 319 | vec_free (l->resource_fds); |
| 320 | vec_free (l->dev_dir_name); |
| 321 | } |
| 322 | |
| 323 | /* Configuration space read/write. */ |
| 324 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 325 | vlib_pci_read_write_config (vlib_pci_device_t * dev, |
| 326 | vlib_read_or_write_t read_or_write, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 327 | uword address, void *data, u32 n_bytes) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 328 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 329 | linux_pci_main_t *lpm = &linux_pci_main; |
| 330 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 331 | int n; |
| 332 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 333 | p = pool_elt_at_index (lpm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 334 | |
| 335 | if (read_or_write == VLIB_READ) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 336 | n = pread (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 337 | else |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 338 | n = pwrite (p->config_fd, data, n_bytes, address); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 339 | |
| 340 | if (n != n_bytes) |
| 341 | return clib_error_return_unix (0, "%s", |
| 342 | read_or_write == VLIB_READ |
| 343 | ? "read" : "write"); |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static clib_error_t * |
| 349 | os_map_pci_resource_internal (uword os_handle, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 350 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 351 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 352 | linux_pci_main_t *pm = &linux_pci_main; |
| 353 | linux_pci_device_t *p; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 354 | struct stat stat_buf; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 355 | u8 *file_name; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 356 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 357 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | int flags = MAP_SHARED; |
| 359 | |
| 360 | error = 0; |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 361 | p = pool_elt_at_index (pm->linux_pci_devices, os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | |
| 363 | file_name = format (0, "%v/resource%d%c", p->dev_dir_name, resource, 0); |
| 364 | fd = open ((char *) file_name, O_RDWR); |
| 365 | if (fd < 0) |
| 366 | { |
| 367 | error = clib_error_return_unix (0, "open `%s'", file_name); |
| 368 | goto done; |
| 369 | } |
| 370 | |
| 371 | if (fstat (fd, &stat_buf) < 0) |
| 372 | { |
| 373 | error = clib_error_return_unix (0, "fstat `%s'", file_name); |
| 374 | goto done; |
| 375 | } |
| 376 | |
| 377 | vec_validate (p->resource_fds, resource); |
| 378 | p->resource_fds[resource] = fd; |
| 379 | if (addr != 0) |
| 380 | flags |= MAP_FIXED; |
| 381 | |
| 382 | *result = mmap (addr, |
| 383 | /* size */ stat_buf.st_size, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 384 | PROT_READ | PROT_WRITE, flags, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 385 | /* file */ fd, |
| 386 | /* offset */ 0); |
| 387 | if (*result == (void *) -1) |
| 388 | { |
| 389 | error = clib_error_return_unix (0, "mmap `%s'", file_name); |
| 390 | goto done; |
| 391 | } |
| 392 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 393 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | if (error) |
| 395 | { |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 396 | if (fd >= 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | close (fd); |
| 398 | } |
| 399 | vec_free (file_name); |
| 400 | return error; |
| 401 | } |
| 402 | |
| 403 | clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 404 | vlib_pci_map_resource (vlib_pci_device_t * dev, u32 resource, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 405 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 406 | return (os_map_pci_resource_internal |
| 407 | (dev->os_handle, resource, 0 /* addr */ , |
| 408 | result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | clib_error_t * |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 412 | vlib_pci_map_resource_fixed (vlib_pci_device_t * dev, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 413 | u32 resource, u8 * addr, void **result) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 414 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 415 | return (os_map_pci_resource_internal |
| 416 | (dev->os_handle, resource, addr, result)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 417 | } |
| 418 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 419 | void |
| 420 | vlib_pci_free_device (vlib_pci_device_t * dev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 421 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 422 | linux_pci_main_t *pm = &linux_pci_main; |
| 423 | linux_pci_device_t *l; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 424 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 425 | l = pool_elt_at_index (pm->linux_pci_devices, dev->os_handle); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 426 | linux_pci_device_free (l); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 427 | pool_put (pm->linux_pci_devices, l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 430 | pci_device_registration_t * __attribute__ ((unused)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 431 | pci_device_next_registered (pci_device_registration_t * r) |
| 432 | { |
| 433 | uword i; |
| 434 | |
| 435 | /* Null vendor id marks end of initialized list. */ |
| 436 | for (i = 0; r->supported_devices[i].vendor_id != 0; i++) |
| 437 | ; |
| 438 | |
| 439 | return clib_elf_section_data_next (r, i * sizeof (r->supported_devices[0])); |
| 440 | } |
| 441 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 442 | static clib_error_t * |
| 443 | init_device_from_registered (vlib_main_t * vm, |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 444 | vlib_pci_device_t * dev, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 445 | linux_pci_device_t * pdev) |
| 446 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 447 | vlib_pci_main_t *pm = &pci_main; |
| 448 | pci_device_registration_t *r; |
| 449 | pci_device_id_t *i; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 450 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 451 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 452 | r = pm->pci_device_registrations; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 453 | |
| 454 | while (r) |
| 455 | { |
| 456 | for (i = r->supported_devices; i->vendor_id != 0; i++) |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 457 | if (i->vendor_id == dev->vendor_id && i->device_id == dev->device_id) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 458 | { |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 459 | error = vlib_pci_bind_to_uio (dev, "uio_pci_generic"); |
| 460 | if (error) |
| 461 | { |
| 462 | clib_error_report (error); |
| 463 | continue; |
| 464 | } |
| 465 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 466 | add_device (dev, pdev); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 467 | dev->interrupt_handler = r->interrupt_handler; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 468 | return r->init_function (vm, dev); |
| 469 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 470 | r = r->next_registration; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 471 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 472 | /* No driver, close the PCI config-space FD */ |
| 473 | close (pdev->config_fd); |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static clib_error_t * |
| 478 | init_device (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 479 | vlib_pci_device_t * dev, linux_pci_device_t * pdev) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 480 | { |
| 481 | return init_device_from_registered (vm, dev, pdev); |
| 482 | } |
| 483 | |
| 484 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 485 | scan_device (void *arg, u8 * dev_dir_name, u8 * ignored) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 486 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 487 | vlib_main_t *vm = arg; |
| 488 | vlib_pci_main_t *pm = &pci_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 489 | int fd; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 490 | u8 *f; |
| 491 | clib_error_t *error = 0; |
| 492 | vlib_pci_device_t *dev; |
| 493 | linux_pci_device_t pdev = { 0 }; |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 494 | u32 tmp; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 495 | |
| 496 | f = format (0, "%v/config%c", dev_dir_name, 0); |
| 497 | fd = open ((char *) f, O_RDWR); |
| 498 | |
| 499 | /* Try read-only access if write fails. */ |
| 500 | if (fd < 0) |
| 501 | fd = open ((char *) f, O_RDONLY); |
| 502 | |
| 503 | if (fd < 0) |
| 504 | { |
| 505 | error = clib_error_return_unix (0, "open `%s'", f); |
| 506 | goto done; |
| 507 | } |
| 508 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 509 | pool_get (pm->pci_devs, dev); |
| 510 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 511 | /* You can only read more that 64 bytes of config space as root; so we try to |
| 512 | read the full space but fall back to just the first 64 bytes. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 513 | if (read (fd, &dev->config_data, sizeof (dev->config_data)) != |
| 514 | sizeof (dev->config_data) |
| 515 | && read (fd, &dev->config0, |
| 516 | sizeof (dev->config0)) != sizeof (dev->config0)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | { |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 518 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | error = clib_error_return_unix (0, "read `%s'", f); |
Chris Luke | 370e9e3 | 2016-07-07 11:01:17 -0400 | [diff] [blame] | 520 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 521 | goto done; |
| 522 | } |
| 523 | |
| 524 | { |
| 525 | static pci_config_header_t all_ones; |
| 526 | if (all_ones.vendor_id == 0) |
| 527 | memset (&all_ones, ~0, sizeof (all_ones)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 528 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 529 | if (!memcmp (&dev->config0.header, &all_ones, sizeof (all_ones))) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 530 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 531 | pool_put (pm->pci_devs, dev); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 532 | error = clib_error_return (0, "invalid PCI config for `%s'", f); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 533 | close (fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 534 | goto done; |
| 535 | } |
| 536 | } |
| 537 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 538 | if (dev->config0.header.header_type == 0) |
| 539 | pci_config_type0_little_to_host (&dev->config0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | else |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 541 | pci_config_type1_little_to_host (&dev->config1); |
| 542 | |
| 543 | /* Parse bus, dev, function from directory name. */ |
| 544 | { |
| 545 | unformat_input_t input; |
| 546 | |
| 547 | unformat_init_string (&input, (char *) dev_dir_name, |
| 548 | vec_len (dev_dir_name)); |
| 549 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 550 | if (!unformat (&input, "/sys/bus/pci/devices/%U", |
| 551 | unformat_vlib_pci_addr, &dev->bus_address)) |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 552 | abort (); |
| 553 | |
| 554 | unformat_free (&input); |
| 555 | |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 556 | } |
| 557 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | |
| 559 | pdev.config_fd = fd; |
| 560 | pdev.dev_dir_name = dev_dir_name; |
| 561 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 562 | hash_set (pm->pci_dev_index_by_pci_addr, dev->bus_address.as_u32, |
| 563 | dev - pm->pci_devs); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 564 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 565 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 566 | f = format (f, "%v/vpd%c", dev_dir_name, 0); |
| 567 | fd = open ((char *) f, O_RDONLY); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 568 | if (fd >= 0) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 569 | { |
| 570 | while (1) |
| 571 | { |
| 572 | u8 tag[3]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 573 | u8 *data = 0; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 574 | int len; |
| 575 | |
| 576 | if (read (fd, &tag, 3) != 3) |
| 577 | break; |
| 578 | |
| 579 | if (tag[0] != 0x82 && tag[0] != 0x90 && tag[0] != 0x91) |
| 580 | break; |
| 581 | |
| 582 | len = (tag[2] << 8) | tag[1]; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 583 | vec_validate (data, len); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 584 | |
| 585 | if (read (fd, data, len) != len) |
| 586 | { |
| 587 | vec_free (data); |
| 588 | break; |
| 589 | } |
| 590 | if (tag[0] == 0x82) |
| 591 | dev->product_name = data; |
| 592 | else if (tag[0] == 0x90) |
| 593 | dev->vpd_r = data; |
| 594 | else if (tag[0] == 0x91) |
| 595 | dev->vpd_w = data; |
| 596 | |
| 597 | data = 0; |
| 598 | } |
| 599 | close (fd); |
| 600 | } |
| 601 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 602 | dev->numa_node = -1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 603 | vec_reset_length (f); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 604 | f = format (f, "%v/numa_node%c", dev_dir_name, 0); |
| 605 | vlib_sysfs_read ((char *) f, "%u", &dev->numa_node); |
| 606 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 607 | vec_reset_length (f); |
| 608 | f = format (f, "%v/class%c", dev_dir_name, 0); |
| 609 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 610 | dev->device_class = tmp >> 8; |
| 611 | |
| 612 | vec_reset_length (f); |
| 613 | f = format (f, "%v/vendor%c", dev_dir_name, 0); |
| 614 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 615 | dev->vendor_id = tmp; |
| 616 | |
| 617 | vec_reset_length (f); |
| 618 | f = format (f, "%v/device%c", dev_dir_name, 0); |
| 619 | vlib_sysfs_read ((char *) f, "0x%x", &tmp); |
| 620 | dev->device_id = tmp; |
| 621 | |
Damjan Marion | 191a1a4 | 2017-07-10 15:38:21 +0200 | [diff] [blame^] | 622 | error = init_device (vm, dev, &pdev); |
| 623 | |
| 624 | vec_reset_length (f); |
| 625 | f = format (f, "%v/driver%c", dev_dir_name, 0); |
| 626 | dev->driver_name = vlib_sysfs_link_to_name ((char *) f); |
| 627 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 628 | done: |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 629 | vec_free (f); |
| 630 | return error; |
| 631 | } |
| 632 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 633 | clib_error_t * |
| 634 | linux_pci_init (vlib_main_t * vm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 636 | vlib_pci_main_t *pm = &pci_main; |
| 637 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 638 | |
| 639 | pm->vlib_main = vm; |
| 640 | |
| 641 | if ((error = vlib_call_init_function (vm, unix_input_init))) |
| 642 | return error; |
| 643 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 644 | ASSERT (sizeof (vlib_pci_addr_t) == sizeof (u32)); |
Damjan Marion | a42cd34 | 2016-04-13 18:03:20 +0200 | [diff] [blame] | 645 | pm->pci_dev_index_by_pci_addr = hash_create (0, sizeof (uword)); |
| 646 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 647 | error = foreach_directory_file ("/sys/bus/pci/devices", scan_device, vm, |
| 648 | /* scan_dirs */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 649 | |
| 650 | /* Complain and continue. might not be root, etc. */ |
| 651 | if (error) |
| 652 | clib_error_report (error); |
| 653 | |
| 654 | return error; |
| 655 | } |
| 656 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 657 | VLIB_INIT_FUNCTION (linux_pci_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 658 | |
| 659 | /* |
| 660 | * fd.io coding-style-patch-verification: ON |
| 661 | * |
| 662 | * Local Variables: |
| 663 | * eval: (c-set-style "gnu") |
| 664 | * End: |
| 665 | */ |