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 | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 55 | vlib_pci_device_t * |
| 56 | vlib_get_pci_device (vlib_pci_addr_t * addr) |
| 57 | { |
| 58 | vlib_pci_main_t *pm = &pci_main; |
| 59 | uword *p; |
| 60 | p = hash_get (pm->pci_dev_index_by_pci_addr, addr->as_u32); |
| 61 | |
| 62 | if (p == 0) |
| 63 | return 0; |
| 64 | |
| 65 | return vec_elt_at_index (pm->pci_devs, p[0]); |
| 66 | } |
| 67 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 68 | static clib_error_t * |
| 69 | show_pci_fn (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 70 | unformat_input_t * input, vlib_cli_command_t * cmd) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 71 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 72 | vlib_pci_main_t *pm = &pci_main; |
| 73 | vlib_pci_device_t *d; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 74 | int show_all = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 75 | u8 *s = 0; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 76 | |
| 77 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 78 | { |
| 79 | if (unformat (input, "all")) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 80 | show_all = 1; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 81 | else |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 82 | return clib_error_return (0, "unknown input `%U'", |
| 83 | format_unformat_error, input); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 86 | vlib_cli_output (vm, "%-13s%-5s%-12s%-13s%-16s%-32s%s", |
| 87 | "Address", "Sock", "VID:PID", "Link Speed", "Driver", |
| 88 | "Product Name", "Vital Product Data"); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 89 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 90 | /* *INDENT-OFF* */ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 91 | pool_foreach (d, pm->pci_devs, ({ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 92 | |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 93 | if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 94 | continue; |
| 95 | |
| 96 | vec_reset_length (s); |
| 97 | |
| 98 | if (d->numa_node >= 0) |
| 99 | s = format (s, " %d", d->numa_node); |
| 100 | |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 101 | vlib_cli_output (vm, "%-13U%-5v%04x:%04x %-13U%-16s%-32v%U", |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 102 | format_vlib_pci_addr, &d->bus_address, s, |
Damjan Marion | b1b1a14 | 2016-08-23 00:53:22 +0200 | [diff] [blame] | 103 | d->vendor_id, d->device_id, |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 104 | format_vlib_pci_link_speed, d, |
| 105 | d->driver_name ? (char *) d->driver_name : "", |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 106 | d->product_name, |
| 107 | format_vlib_pci_vpd, d->vpd_r, 0); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 108 | })); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 109 | /* *INDENT-ON* */ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 110 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 111 | vec_free (s); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | uword |
| 116 | unformat_vlib_pci_addr (unformat_input_t * input, va_list * args) |
| 117 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 118 | vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 119 | u32 x[4]; |
| 120 | |
| 121 | if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3])) |
| 122 | return 0; |
| 123 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 124 | addr->domain = x[0]; |
| 125 | addr->bus = x[1]; |
| 126 | addr->slot = x[2]; |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 127 | addr->function = x[3]; |
| 128 | |
| 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | u8 * |
| 133 | format_vlib_pci_addr (u8 * s, va_list * va) |
| 134 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 135 | vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 136 | return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus, |
| 137 | addr->slot, addr->function); |
| 138 | } |
| 139 | |
| 140 | u8 * |
| 141 | format_vlib_pci_handle (u8 * s, va_list * va) |
| 142 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 143 | vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 144 | return format (s, "%x/%x/%x", addr->bus, addr->slot, addr->function); |
| 145 | } |
| 146 | |
| 147 | u8 * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 148 | format_vlib_pci_link_speed (u8 * s, va_list * va) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 149 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 150 | vlib_pci_device_t *d = va_arg (*va, vlib_pci_device_t *); |
| 151 | pcie_config_regs_t *r = |
| 152 | pci_config_find_capability (&d->config0, PCI_CAP_ID_PCIE); |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 153 | int width; |
| 154 | |
| 155 | if (!r) |
| 156 | return format (s, "unknown"); |
| 157 | |
| 158 | width = (r->link_status >> 4) & 0x3f; |
| 159 | |
| 160 | if ((r->link_status & 0xf) == 1) |
| 161 | return format (s, "2.5 GT/s x%u", width); |
| 162 | if ((r->link_status & 0xf) == 2) |
| 163 | return format (s, "5.0 GT/s x%u", width); |
| 164 | if ((r->link_status & 0xf) == 3) |
| 165 | return format (s, "8.0 GT/s x%u", width); |
| 166 | return format (s, "unknown"); |
| 167 | } |
| 168 | |
Damjan Marion | 7d22161 | 2016-12-26 11:39:42 +0100 | [diff] [blame] | 169 | u8 * |
| 170 | format_vlib_pci_vpd (u8 * s, va_list * args) |
| 171 | { |
| 172 | u8 *data = va_arg (*args, u8 *); |
| 173 | u8 *id = va_arg (*args, u8 *); |
| 174 | uword indent = format_get_indent (s); |
| 175 | char *string_types[] = { "PN", "EC", "SN", "MN", 0 }; |
| 176 | uword p = 0; |
| 177 | int first_line = 1; |
| 178 | |
| 179 | if (vec_len (data) < 3) |
| 180 | return s; |
| 181 | |
| 182 | while (p + 3 < vec_len (data)) |
| 183 | { |
| 184 | |
| 185 | if (data[p] == 0 && data[p + 1] == 0) |
| 186 | return s; |
| 187 | |
| 188 | if (p + data[p + 2] > vec_len (data)) |
| 189 | return s; |
| 190 | |
| 191 | if (id == 0) |
| 192 | { |
| 193 | int is_string = 0; |
| 194 | char **c = string_types; |
| 195 | |
| 196 | while (c[0]) |
| 197 | { |
| 198 | if (*(u16 *) & data[p] == *(u16 *) c[0]) |
| 199 | is_string = 1; |
| 200 | c++; |
| 201 | } |
| 202 | |
| 203 | if (data[p + 2]) |
| 204 | { |
| 205 | if (!first_line) |
| 206 | s = format (s, "\n%U", format_white_space, indent); |
| 207 | else |
| 208 | { |
| 209 | first_line = 0; |
| 210 | s = format (s, " "); |
| 211 | } |
| 212 | |
| 213 | s = format (s, "%c%c: ", data[p], data[p + 1]); |
| 214 | if (is_string) |
| 215 | vec_add (s, data + p + 3, data[p + 2]); |
| 216 | else |
| 217 | { |
| 218 | int i; |
| 219 | const int max_bytes = 8; |
| 220 | s = format (s, "0x"); |
| 221 | for (i = 0; i < clib_min (data[p + 2], max_bytes); i++) |
| 222 | s = format (s, " %02x", data[p + 3 + i]); |
| 223 | |
| 224 | if (data[p + 2] > max_bytes) |
| 225 | s = format (s, " ..."); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | else if (*(u16 *) & data[p] == *(u16 *) id) |
| 230 | { |
| 231 | vec_add (s, data + p + 3, data[p + 2]); |
| 232 | return s; |
| 233 | } |
| 234 | |
| 235 | p += 3 + data[p + 2]; |
| 236 | } |
| 237 | |
| 238 | return s; |
| 239 | } |
| 240 | |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 241 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 242 | /* *INDENT-OFF* */ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 243 | VLIB_CLI_COMMAND (show_pci_command, static) = { |
| 244 | .path = "show pci", |
| 245 | .short_help = "show pci [all]", |
| 246 | .function = show_pci_fn, |
| 247 | }; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 248 | /* *INDENT-ON* */ |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 249 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 250 | clib_error_t * |
| 251 | pci_bus_init (vlib_main_t * vm) |
Damjan Marion | 5a206ea | 2016-05-12 22:11:03 +0200 | [diff] [blame] | 252 | { |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | VLIB_INIT_FUNCTION (pci_bus_init); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * fd.io coding-style-patch-verification: ON |
| 260 | * |
| 261 | * Local Variables: |
| 262 | * eval: (c-set-style "gnu") |
| 263 | * End: |
| 264 | */ |