blob: 7284e6675fdd639195ccdbf17abf08cd45d7c8a6 [file] [log] [blame]
Damjan Marion5a206ea2016-05-12 22:11:03 +02001/*
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>
Tom Jones36d099a2024-01-29 10:34:21 +000050#ifdef __linux__
Damjan Marion5a206ea2016-05-12 22:11:03 +020051#include <linux/ethtool.h>
52#include <linux/sockios.h>
Tom Jones36d099a2024-01-29 10:34:21 +000053#endif /* __linux__ */
Damjan Marion5a206ea2016-05-12 22:11:03 +020054
55vlib_pci_main_t pci_main;
56
Damjan Marion00ea98a2023-07-28 13:19:49 +020057VLIB_REGISTER_LOG_CLASS (pci_log, static) = {
58 .class_name = "pci",
59};
60
61#define log_debug(h, f, ...) \
62 vlib_log (VLIB_LOG_LEVEL_DEBUG, pci_log.class, "%U: " f, \
63 format_vlib_pci_log, h, ##__VA_ARGS__)
64
65u8 *
66format_vlib_pci_log (u8 *s, va_list *va)
67{
68 vlib_pci_dev_handle_t h = va_arg (*va, vlib_pci_dev_handle_t);
69 return format (s, "%U", format_vlib_pci_addr,
70 vlib_pci_get_addr (vlib_get_main (), h));
71}
72
73vlib_pci_device_info_t *__attribute__ ((weak))
74vlib_pci_get_device_info (vlib_main_t *vm, vlib_pci_addr_t *addr,
75 clib_error_t **error)
Damjan Marion7d221612016-12-26 11:39:42 +010076{
Damjan Marioncef87f12017-10-05 15:32:41 +020077 if (error)
78 *error = clib_error_return (0, "unsupported");
79 return 0;
80}
Damjan Marion7d221612016-12-26 11:39:42 +010081
Ray Kinsella5714a492021-11-02 13:33:44 +000082clib_error_t *__attribute__ ((weak))
83vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
84{
85 return 0;
86}
87
Damjan Marioncef87f12017-10-05 15:32:41 +020088vlib_pci_addr_t * __attribute__ ((weak)) vlib_pci_get_all_dev_addrs ()
89{
90 return 0;
Damjan Marion7d221612016-12-26 11:39:42 +010091}
92
Damjan Marion5a206ea2016-05-12 22:11:03 +020093static clib_error_t *
Damjan Marion00ea98a2023-07-28 13:19:49 +020094_vlib_pci_config_set_control_bit (vlib_main_t *vm, vlib_pci_dev_handle_t h,
95 u16 bit, int new_val, int *already_set)
96{
97 u16 control, old;
98 clib_error_t *err;
99
100 err = vlib_pci_read_write_config (
101 vm, h, VLIB_READ, STRUCT_OFFSET_OF (vlib_pci_config_t, command), &old,
102 STRUCT_SIZE_OF (vlib_pci_config_t, command));
103
104 if (err)
105 return err;
106
107 control = new_val ? old | bit : old & ~bit;
108 *already_set = old == control;
109 if (*already_set)
110 return 0;
111
112 return vlib_pci_read_write_config (
113 vm, h, VLIB_WRITE, STRUCT_OFFSET_OF (vlib_pci_config_t, command), &control,
114 STRUCT_SIZE_OF (vlib_pci_config_t, command));
115}
116
117clib_error_t *
118vlib_pci_intr_enable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
119{
120 const vlib_pci_config_reg_command_t cmd = { .intx_disable = 1 };
121 clib_error_t *err;
Guillaume Solignac687707d2024-01-18 12:01:57 +0100122 int already_set = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +0200123
124 err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 0, &already_set);
125 log_debug (h, "interrupt%senabled", already_set ? " " : " already ");
126 return err;
127}
128
129clib_error_t *
130vlib_pci_intr_disable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
131{
132 const vlib_pci_config_reg_command_t cmd = { .intx_disable = 1 };
133 clib_error_t *err;
Guillaume Solignac687707d2024-01-18 12:01:57 +0100134 int already_set = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +0200135
136 err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 1, &already_set);
137 log_debug (h, "interrupt%sdisabled", already_set ? " " : " already ");
138 return err;
139}
140
141clib_error_t *
142vlib_pci_bus_master_enable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
143{
144 const vlib_pci_config_reg_command_t cmd = { .bus_master = 1 };
145 clib_error_t *err;
Guillaume Solignac687707d2024-01-18 12:01:57 +0100146 int already_set = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +0200147
148 err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 1, &already_set);
149 log_debug (h, "bus-master%senabled", already_set ? " " : " already ");
150 return err;
151}
152
153clib_error_t *
154vlib_pci_bus_master_disable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
155{
156 const vlib_pci_config_reg_command_t cmd = { .bus_master = 1 };
157 clib_error_t *err;
Guillaume Solignac687707d2024-01-18 12:01:57 +0100158 int already_set = 0;
Damjan Marion00ea98a2023-07-28 13:19:49 +0200159
160 err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 0, &already_set);
161 log_debug (h, "bus-master%sdisabled", already_set ? " " : " already ");
162 return err;
163}
164
165clib_error_t *
166vlib_pci_function_level_reset (vlib_main_t *vm, vlib_pci_dev_handle_t h)
167{
168 vlib_pci_config_t cfg;
169 pci_capability_pcie_t *cap;
170 pci_capability_pcie_dev_control_t dev_control;
171 clib_error_t *err;
172 u8 offset;
173
174 log_debug (h, "function level reset");
175
176 err = vlib_pci_read_write_config (vm, h, VLIB_READ, 0, &cfg, sizeof (cfg));
177 if (err)
178 return err;
179
180 offset = cfg.cap_ptr;
Vratko Polakfee94592023-08-23 18:39:25 +0200181 do
Damjan Marion00ea98a2023-07-28 13:19:49 +0200182 {
183 cap = (pci_capability_pcie_t *) (cfg.data + offset);
184
185 if (cap->capability_id == PCI_CAP_ID_PCIE)
186 break;
187
188 offset = cap->next_offset;
189 }
Vratko Polakfee94592023-08-23 18:39:25 +0200190 while (offset);
Damjan Marion00ea98a2023-07-28 13:19:49 +0200191
192 if (cap->capability_id != PCI_CAP_ID_PCIE)
193 return clib_error_return (0, "PCIe capability config not found");
194
195 if (cap->dev_caps.flr_capable == 0)
196 return clib_error_return (0, "PCIe function level reset not supported");
197
198 dev_control = cap->dev_control;
199 dev_control.function_level_reset = 1;
200
201 if ((err = vlib_pci_write_config_u16 (
202 vm, h, offset + STRUCT_OFFSET_OF (pci_capability_pcie_t, dev_control),
203 &dev_control.as_u16)))
204 return err;
205
206 return 0;
207}
208
209static clib_error_t *
Damjan Marion5a206ea2016-05-12 22:11:03 +0200210show_pci_fn (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -0400211 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200212{
Damjan Marioncef87f12017-10-05 15:32:41 +0200213 vlib_pci_addr_t *addr = 0, *addrs;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200214 int show_all = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -0400215 u8 *s = 0;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200216
217 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
218 {
219 if (unformat (input, "all"))
Dave Barach9b8ffd92016-07-08 08:13:45 -0400220 show_all = 1;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200221 else
Dave Barach9b8ffd92016-07-08 08:13:45 -0400222 return clib_error_return (0, "unknown input `%U'",
223 format_unformat_error, input);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200224 }
225
Damjan Marion4a348e82020-11-25 17:13:38 +0100226 vlib_cli_output (vm, "%-13s%-5s%-12s%-14s%-16s%-32s%s",
Damjan Marion7d221612016-12-26 11:39:42 +0100227 "Address", "Sock", "VID:PID", "Link Speed", "Driver",
228 "Product Name", "Vital Product Data");
Damjan Marion5a206ea2016-05-12 22:11:03 +0200229
Damjan Marioncef87f12017-10-05 15:32:41 +0200230 addrs = vlib_pci_get_all_dev_addrs ();
Damjan Marion14361002021-03-11 12:17:33 +0100231
Damjan Marioncef87f12017-10-05 15:32:41 +0200232 vec_foreach (addr, addrs)
233 {
234 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +0200235 d = vlib_pci_get_device_info (vm, addr, 0);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200236
Damjan Marioncef87f12017-10-05 15:32:41 +0200237 if (!d)
238 continue;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200239
Damjan Marioncef87f12017-10-05 15:32:41 +0200240 if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all)
Damjan Marion14361002021-03-11 12:17:33 +0100241 continue;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200242
Damjan Marion14361002021-03-11 12:17:33 +0100243 vec_reset_length (s);
244 if (d->numa_node >= 0)
245 s = format (s, " %d", d->numa_node);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200246
Damjan Marion14361002021-03-11 12:17:33 +0100247 vlib_cli_output (
248 vm, "%-13U%-5v%04x:%04x %-14U%-16s%-32v%U", format_vlib_pci_addr,
249 addr, s, d->vendor_id, d->device_id, format_vlib_pci_link_speed, d,
250 d->driver_name ? (char *) d->driver_name : "", d->product_name,
251 format_vlib_pci_vpd, d->vpd_r, (u8 *) 0);
252 vlib_pci_free_device_info (d);
Damjan Marioncef87f12017-10-05 15:32:41 +0200253 }
Damjan Marion5a206ea2016-05-12 22:11:03 +0200254
Dave Barach9b8ffd92016-07-08 08:13:45 -0400255 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200256 vec_free (addrs);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200257 return 0;
258}
259
260uword
261unformat_vlib_pci_addr (unformat_input_t * input, va_list * args)
262{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400263 vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200264 u32 x[4];
265
266 if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
267 return 0;
268
Dave Barach9b8ffd92016-07-08 08:13:45 -0400269 addr->domain = x[0];
270 addr->bus = x[1];
271 addr->slot = x[2];
Damjan Marion5a206ea2016-05-12 22:11:03 +0200272 addr->function = x[3];
273
274 return 1;
275}
276
277u8 *
278format_vlib_pci_addr (u8 * s, va_list * va)
279{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400280 vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200281 return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
282 addr->slot, addr->function);
283}
284
285u8 *
Ray Kinsella6efe0252021-11-02 13:26:49 +0000286format_vlib_pci_link_port (u8 *s, va_list *va)
287{
Damjan Marion00ea98a2023-07-28 13:19:49 +0200288 vlib_pci_config_t *c = va_arg (*va, vlib_pci_config_t *);
289 pci_capability_pcie_t *r = pci_config_find_capability (c, PCI_CAP_ID_PCIE);
Ray Kinsella6efe0252021-11-02 13:26:49 +0000290
291 if (!r)
292 return format (s, "unknown");
293
Damjan Marion00ea98a2023-07-28 13:19:49 +0200294 return format (s, "P%d", r->link_caps.port_number);
295}
296
297static u8 *
298_vlib_pci_link_speed (u8 *s, u8 speed, u8 width)
299{
300 static char *speeds[] = {
301 [1] = "2.5", [2] = "5.0", [3] = "8.0", [4] = "16.0", [5] = "32.0"
302 };
303
Dave Wallace63335e42023-08-22 12:51:09 -0400304 if (speed >= ARRAY_LEN (speeds) || speeds[speed] == 0)
Damjan Marion00ea98a2023-07-28 13:19:49 +0200305 s = format (s, "unknown speed");
306 else
307 s = format (s, "%s GT/s", speeds[speed]);
308
309 return format (s, " x%u", width);
Ray Kinsella6efe0252021-11-02 13:26:49 +0000310}
311
312u8 *
Damjan Marion00ea98a2023-07-28 13:19:49 +0200313format_vlib_pci_link_speed (u8 *s, va_list *va)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200314{
Damjan Marion00ea98a2023-07-28 13:19:49 +0200315 vlib_pci_config_t *c = va_arg (*va, vlib_pci_config_t *);
316 pci_capability_pcie_t *r = pci_config_find_capability (c, PCI_CAP_ID_PCIE);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200317
318 if (!r)
319 return format (s, "unknown");
320
Damjan Marion00ea98a2023-07-28 13:19:49 +0200321 return _vlib_pci_link_speed (s, r->link_status.link_speed,
322 r->link_status.negotiated_link_width);
323}
Damjan Marion5a206ea2016-05-12 22:11:03 +0200324
Damjan Marion00ea98a2023-07-28 13:19:49 +0200325u8 *
326format_vlib_pci_link_speed_cap (u8 *s, va_list *va)
327{
328 vlib_pci_config_t *c = va_arg (*va, vlib_pci_config_t *);
329 pci_capability_pcie_t *r = pci_config_find_capability (c, PCI_CAP_ID_PCIE);
330
331 if (!r)
332 return format (s, "unknown");
333
334 return _vlib_pci_link_speed (s, r->link_caps.max_link_speed,
335 r->link_caps.max_link_width);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200336}
337
Damjan Marion7d221612016-12-26 11:39:42 +0100338u8 *
339format_vlib_pci_vpd (u8 * s, va_list * args)
340{
341 u8 *data = va_arg (*args, u8 *);
342 u8 *id = va_arg (*args, u8 *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200343 u32 indent = format_get_indent (s);
Damjan Marion7d221612016-12-26 11:39:42 +0100344 char *string_types[] = { "PN", "EC", "SN", "MN", 0 };
345 uword p = 0;
346 int first_line = 1;
347
348 if (vec_len (data) < 3)
349 return s;
350
351 while (p + 3 < vec_len (data))
352 {
353
354 if (data[p] == 0 && data[p + 1] == 0)
355 return s;
356
357 if (p + data[p + 2] > vec_len (data))
358 return s;
359
360 if (id == 0)
361 {
362 int is_string = 0;
363 char **c = string_types;
364
365 while (c[0])
366 {
367 if (*(u16 *) & data[p] == *(u16 *) c[0])
368 is_string = 1;
369 c++;
370 }
371
372 if (data[p + 2])
373 {
374 if (!first_line)
375 s = format (s, "\n%U", format_white_space, indent);
376 else
377 {
378 first_line = 0;
379 s = format (s, " ");
380 }
381
382 s = format (s, "%c%c: ", data[p], data[p + 1]);
383 if (is_string)
384 vec_add (s, data + p + 3, data[p + 2]);
385 else
386 {
387 int i;
388 const int max_bytes = 8;
389 s = format (s, "0x");
390 for (i = 0; i < clib_min (data[p + 2], max_bytes); i++)
391 s = format (s, " %02x", data[p + 3 + i]);
392
393 if (data[p + 2] > max_bytes)
394 s = format (s, " ...");
395 }
396 }
397 }
398 else if (*(u16 *) & data[p] == *(u16 *) id)
399 {
400 vec_add (s, data + p + 3, data[p + 2]);
401 return s;
402 }
403
404 p += 3 + data[p + 2];
405 }
406
407 return s;
408}
409
Damjan Marion5a206ea2016-05-12 22:11:03 +0200410VLIB_CLI_COMMAND (show_pci_command, static) = {
411 .path = "show pci",
412 .short_help = "show pci [all]",
413 .function = show_pci_fn,
414};