Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 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> |
| 43 | |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <fcntl.h> |
| 47 | #include <dirent.h> |
| 48 | #include <sys/ioctl.h> |
| 49 | #include <net/if.h> |
| 50 | #include <linux/ethtool.h> |
| 51 | #include <linux/sockios.h> |
| 52 | |
| 53 | vlib_pci_main_t pci_main; |
| 54 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 55 | vlib_pci_device_info_t * __attribute__ ((weak)) |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 56 | vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr, |
| 57 | clib_error_t ** error) |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 58 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 59 | if (error) |
| 60 | *error = clib_error_return (0, "unsupported"); |
| 61 | return 0; |
| 62 | } |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 63 | |
Ray Kinsella | 5714a49 | 2021-11-02 13:33:44 +0000 | [diff] [blame] | 64 | clib_error_t *__attribute__ ((weak)) |
| 65 | vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
| 69 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 70 | vlib_pci_addr_t * __attribute__ ((weak)) vlib_pci_get_all_dev_addrs () |
| 71 | { |
| 72 | return 0; |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 73 | } |
| 74 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 75 | static clib_error_t * |
| 76 | show_pci_fn (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 77 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 78 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 79 | vlib_pci_addr_t *addr = 0, *addrs; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 80 | int show_all = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 81 | u8 *s = 0; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 82 | |
| 83 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 84 | { |
| 85 | if (unformat (input, "all")) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 86 | show_all = 1; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 87 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 88 | return clib_error_return (0, "unknown input `%U'", |
| 89 | format_unformat_error, input); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Damjan Marion | 4a348e8 | 2020-11-25 17:13:38 +0100 | [diff] [blame] | 92 | vlib_cli_output (vm, "%-13s%-5s%-12s%-14s%-16s%-32s%s", |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 93 | "Address", "Sock", "VID:PID", "Link Speed", "Driver", |
| 94 | "Product Name", "Vital Product Data"); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 95 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 96 | addrs = vlib_pci_get_all_dev_addrs (); |
Damjan Marion | 1436100 | 2021-03-11 12:17:33 +0100 | [diff] [blame] | 97 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 98 | vec_foreach (addr, addrs) |
| 99 | { |
| 100 | vlib_pci_device_info_t *d; |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 101 | d = vlib_pci_get_device_info (vm, addr, 0); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 102 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 103 | if (!d) |
| 104 | continue; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 105 | |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 106 | if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all) |
Damjan Marion | 1436100 | 2021-03-11 12:17:33 +0100 | [diff] [blame] | 107 | continue; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 108 | |
Damjan Marion | 1436100 | 2021-03-11 12:17:33 +0100 | [diff] [blame] | 109 | vec_reset_length (s); |
| 110 | if (d->numa_node >= 0) |
| 111 | s = format (s, " %d", d->numa_node); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 112 | |
Damjan Marion | 1436100 | 2021-03-11 12:17:33 +0100 | [diff] [blame] | 113 | vlib_cli_output ( |
| 114 | vm, "%-13U%-5v%04x:%04x %-14U%-16s%-32v%U", format_vlib_pci_addr, |
| 115 | addr, s, d->vendor_id, d->device_id, format_vlib_pci_link_speed, d, |
| 116 | d->driver_name ? (char *) d->driver_name : "", d->product_name, |
| 117 | format_vlib_pci_vpd, d->vpd_r, (u8 *) 0); |
| 118 | vlib_pci_free_device_info (d); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 119 | } |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 120 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 121 | vec_free (s); |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 122 | vec_free (addrs); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | uword |
| 127 | unformat_vlib_pci_addr (unformat_input_t * input, va_list * args) |
| 128 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 129 | vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 130 | u32 x[4]; |
| 131 | |
| 132 | if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3])) |
| 133 | return 0; |
| 134 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 135 | addr->domain = x[0]; |
| 136 | addr->bus = x[1]; |
| 137 | addr->slot = x[2]; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 138 | addr->function = x[3]; |
| 139 | |
| 140 | return 1; |
| 141 | } |
| 142 | |
| 143 | u8 * |
| 144 | format_vlib_pci_addr (u8 * s, va_list * va) |
| 145 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 146 | vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 147 | return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus, |
| 148 | addr->slot, addr->function); |
| 149 | } |
| 150 | |
| 151 | u8 * |
Ray Kinsella | 6efe025 | 2021-11-02 13:26:49 +0000 | [diff] [blame] | 152 | format_vlib_pci_link_port (u8 *s, va_list *va) |
| 153 | { |
| 154 | vlib_pci_device_info_t *d = va_arg (*va, vlib_pci_device_info_t *); |
| 155 | pcie_config_regs_t *r = |
| 156 | pci_config_find_capability (&d->config0, PCI_CAP_ID_PCIE); |
| 157 | |
| 158 | if (!r) |
| 159 | return format (s, "unknown"); |
| 160 | |
Ray Kinsella | 81865bc | 2021-11-08 07:22:49 +0000 | [diff] [blame] | 161 | return format (s, "P%d", r->link_capabilities >> 24); |
Ray Kinsella | 6efe025 | 2021-11-02 13:26:49 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | u8 * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 165 | format_vlib_pci_link_speed (u8 * s, va_list * va) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 166 | { |
Damjan Marion | cef87f1 | 2017-10-05 15:32:41 +0200 | [diff] [blame] | 167 | vlib_pci_device_info_t *d = va_arg (*va, vlib_pci_device_info_t *); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 168 | pcie_config_regs_t *r = |
| 169 | pci_config_find_capability (&d->config0, PCI_CAP_ID_PCIE); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 170 | int width; |
| 171 | |
| 172 | if (!r) |
| 173 | return format (s, "unknown"); |
| 174 | |
| 175 | width = (r->link_status >> 4) & 0x3f; |
| 176 | |
| 177 | if ((r->link_status & 0xf) == 1) |
| 178 | return format (s, "2.5 GT/s x%u", width); |
| 179 | if ((r->link_status & 0xf) == 2) |
| 180 | return format (s, "5.0 GT/s x%u", width); |
| 181 | if ((r->link_status & 0xf) == 3) |
| 182 | return format (s, "8.0 GT/s x%u", width); |
Damjan Marion | 4a348e8 | 2020-11-25 17:13:38 +0100 | [diff] [blame] | 183 | if ((r->link_status & 0xf) == 4) |
| 184 | return format (s, "16.0 GT/s x%u", width); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 185 | return format (s, "unknown"); |
| 186 | } |
| 187 | |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 188 | u8 * |
| 189 | format_vlib_pci_vpd (u8 * s, va_list * args) |
| 190 | { |
| 191 | u8 *data = va_arg (*args, u8 *); |
| 192 | u8 *id = va_arg (*args, u8 *); |
Christophe Fontaine | d3c008d | 2017-10-02 18:10:54 +0200 | [diff] [blame] | 193 | u32 indent = format_get_indent (s); |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 194 | char *string_types[] = { "PN", "EC", "SN", "MN", 0 }; |
| 195 | uword p = 0; |
| 196 | int first_line = 1; |
| 197 | |
| 198 | if (vec_len (data) < 3) |
| 199 | return s; |
| 200 | |
| 201 | while (p + 3 < vec_len (data)) |
| 202 | { |
| 203 | |
| 204 | if (data[p] == 0 && data[p + 1] == 0) |
| 205 | return s; |
| 206 | |
| 207 | if (p + data[p + 2] > vec_len (data)) |
| 208 | return s; |
| 209 | |
| 210 | if (id == 0) |
| 211 | { |
| 212 | int is_string = 0; |
| 213 | char **c = string_types; |
| 214 | |
| 215 | while (c[0]) |
| 216 | { |
| 217 | if (*(u16 *) & data[p] == *(u16 *) c[0]) |
| 218 | is_string = 1; |
| 219 | c++; |
| 220 | } |
| 221 | |
| 222 | if (data[p + 2]) |
| 223 | { |
| 224 | if (!first_line) |
| 225 | s = format (s, "\n%U", format_white_space, indent); |
| 226 | else |
| 227 | { |
| 228 | first_line = 0; |
| 229 | s = format (s, " "); |
| 230 | } |
| 231 | |
| 232 | s = format (s, "%c%c: ", data[p], data[p + 1]); |
| 233 | if (is_string) |
| 234 | vec_add (s, data + p + 3, data[p + 2]); |
| 235 | else |
| 236 | { |
| 237 | int i; |
| 238 | const int max_bytes = 8; |
| 239 | s = format (s, "0x"); |
| 240 | for (i = 0; i < clib_min (data[p + 2], max_bytes); i++) |
| 241 | s = format (s, " %02x", data[p + 3 + i]); |
| 242 | |
| 243 | if (data[p + 2] > max_bytes) |
| 244 | s = format (s, " ..."); |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | else if (*(u16 *) & data[p] == *(u16 *) id) |
| 249 | { |
| 250 | vec_add (s, data + p + 3, data[p + 2]); |
| 251 | return s; |
| 252 | } |
| 253 | |
| 254 | p += 3 + data[p + 2]; |
| 255 | } |
| 256 | |
| 257 | return s; |
| 258 | } |
| 259 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 260 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 261 | /* *INDENT-OFF* */ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 262 | VLIB_CLI_COMMAND (show_pci_command, static) = { |
| 263 | .path = "show pci", |
| 264 | .short_help = "show pci [all]", |
| 265 | .function = show_pci_fn, |
| 266 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 267 | /* *INDENT-ON* */ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 268 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 269 | clib_error_t * |
| 270 | pci_bus_init (vlib_main_t * vm) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 271 | { |
Damjan Marion | 2322798 | 2018-10-22 13:38:57 +0200 | [diff] [blame] | 272 | vlib_pci_main_t *pm = &pci_main; |
| 273 | pm->log_default = vlib_log_register_class ("pci", 0); |
| 274 | return 0; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | VLIB_INIT_FUNCTION (pci_bus_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * fd.io coding-style-patch-verification: ON |
| 281 | * |
| 282 | * Local Variables: |
| 283 | * eval: (c-set-style "gnu") |
| 284 | * End: |
| 285 | */ |