blob: 7e96e1f7c6e2f742e213bbfef2e015cae060a10b [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>
50#include <linux/ethtool.h>
51#include <linux/sockios.h>
52
53vlib_pci_main_t pci_main;
54
Damjan Marioncef87f12017-10-05 15:32:41 +020055vlib_pci_device_info_t * __attribute__ ((weak))
Damjan Marion23227982018-10-22 13:38:57 +020056vlib_pci_get_device_info (vlib_main_t * vm, vlib_pci_addr_t * addr,
57 clib_error_t ** error)
Damjan Marion7d221612016-12-26 11:39:42 +010058{
Damjan Marioncef87f12017-10-05 15:32:41 +020059 if (error)
60 *error = clib_error_return (0, "unsupported");
61 return 0;
62}
Damjan Marion7d221612016-12-26 11:39:42 +010063
Damjan Marioncef87f12017-10-05 15:32:41 +020064vlib_pci_addr_t * __attribute__ ((weak)) vlib_pci_get_all_dev_addrs ()
65{
66 return 0;
Damjan Marion7d221612016-12-26 11:39:42 +010067}
68
Damjan Marion5a206ea2016-05-12 22:11:03 +020069static clib_error_t *
70show_pci_fn (vlib_main_t * vm,
Dave Barach9b8ffd92016-07-08 08:13:45 -040071 unformat_input_t * input, vlib_cli_command_t * cmd)
Damjan Marion5a206ea2016-05-12 22:11:03 +020072{
Damjan Marioncef87f12017-10-05 15:32:41 +020073 vlib_pci_addr_t *addr = 0, *addrs;
Damjan Marion5a206ea2016-05-12 22:11:03 +020074 int show_all = 0;
Dave Barach9b8ffd92016-07-08 08:13:45 -040075 u8 *s = 0;
Damjan Marion5a206ea2016-05-12 22:11:03 +020076
77 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
78 {
79 if (unformat (input, "all"))
Dave Barach9b8ffd92016-07-08 08:13:45 -040080 show_all = 1;
Damjan Marion5a206ea2016-05-12 22:11:03 +020081 else
Dave Barach9b8ffd92016-07-08 08:13:45 -040082 return clib_error_return (0, "unknown input `%U'",
83 format_unformat_error, input);
Damjan Marion5a206ea2016-05-12 22:11:03 +020084 }
85
Damjan Marion4a348e82020-11-25 17:13:38 +010086 vlib_cli_output (vm, "%-13s%-5s%-12s%-14s%-16s%-32s%s",
Damjan Marion7d221612016-12-26 11:39:42 +010087 "Address", "Sock", "VID:PID", "Link Speed", "Driver",
88 "Product Name", "Vital Product Data");
Damjan Marion5a206ea2016-05-12 22:11:03 +020089
Damjan Marioncef87f12017-10-05 15:32:41 +020090 addrs = vlib_pci_get_all_dev_addrs ();
Dave Barach9b8ffd92016-07-08 08:13:45 -040091 /* *INDENT-OFF* */
Damjan Marioncef87f12017-10-05 15:32:41 +020092 vec_foreach (addr, addrs)
93 {
94 vlib_pci_device_info_t *d;
Damjan Marion23227982018-10-22 13:38:57 +020095 d = vlib_pci_get_device_info (vm, addr, 0);
Damjan Marion5a206ea2016-05-12 22:11:03 +020096
Damjan Marioncef87f12017-10-05 15:32:41 +020097 if (!d)
98 continue;
Damjan Marion5a206ea2016-05-12 22:11:03 +020099
Damjan Marioncef87f12017-10-05 15:32:41 +0200100 if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all)
101 continue;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200102
Damjan Marioncef87f12017-10-05 15:32:41 +0200103 vec_reset_length (s);
104 if (d->numa_node >= 0)
105 s = format (s, " %d", d->numa_node);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200106
Damjan Marion4a348e82020-11-25 17:13:38 +0100107 vlib_cli_output (vm, "%-13U%-5v%04x:%04x %-14U%-16s%-32v%U",
Damjan Marioncef87f12017-10-05 15:32:41 +0200108 format_vlib_pci_addr, addr, s,
109 d->vendor_id, d->device_id,
110 format_vlib_pci_link_speed, d,
111 d->driver_name ? (char *) d->driver_name : "",
112 d->product_name,
Vratko Polakdea0b232019-12-06 16:49:36 +0100113 format_vlib_pci_vpd, d->vpd_r, (u8 *) 0);
Damjan Marioncef87f12017-10-05 15:32:41 +0200114 vlib_pci_free_device_info (d);
115 }
116 /* *INDENT-ON* */
Damjan Marion5a206ea2016-05-12 22:11:03 +0200117
Dave Barach9b8ffd92016-07-08 08:13:45 -0400118 vec_free (s);
Damjan Marioncef87f12017-10-05 15:32:41 +0200119 vec_free (addrs);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200120 return 0;
121}
122
123uword
124unformat_vlib_pci_addr (unformat_input_t * input, va_list * args)
125{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400126 vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200127 u32 x[4];
128
129 if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
130 return 0;
131
Dave Barach9b8ffd92016-07-08 08:13:45 -0400132 addr->domain = x[0];
133 addr->bus = x[1];
134 addr->slot = x[2];
Damjan Marion5a206ea2016-05-12 22:11:03 +0200135 addr->function = x[3];
136
137 return 1;
138}
139
140u8 *
141format_vlib_pci_addr (u8 * s, va_list * va)
142{
Dave Barach9b8ffd92016-07-08 08:13:45 -0400143 vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200144 return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
145 addr->slot, addr->function);
146}
147
148u8 *
Dave Barach9b8ffd92016-07-08 08:13:45 -0400149format_vlib_pci_link_speed (u8 * s, va_list * va)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200150{
Damjan Marioncef87f12017-10-05 15:32:41 +0200151 vlib_pci_device_info_t *d = va_arg (*va, vlib_pci_device_info_t *);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400152 pcie_config_regs_t *r =
153 pci_config_find_capability (&d->config0, PCI_CAP_ID_PCIE);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200154 int width;
155
156 if (!r)
157 return format (s, "unknown");
158
159 width = (r->link_status >> 4) & 0x3f;
160
161 if ((r->link_status & 0xf) == 1)
162 return format (s, "2.5 GT/s x%u", width);
163 if ((r->link_status & 0xf) == 2)
164 return format (s, "5.0 GT/s x%u", width);
165 if ((r->link_status & 0xf) == 3)
166 return format (s, "8.0 GT/s x%u", width);
Damjan Marion4a348e82020-11-25 17:13:38 +0100167 if ((r->link_status & 0xf) == 4)
168 return format (s, "16.0 GT/s x%u", width);
Damjan Marion5a206ea2016-05-12 22:11:03 +0200169 return format (s, "unknown");
170}
171
Damjan Marion7d221612016-12-26 11:39:42 +0100172u8 *
173format_vlib_pci_vpd (u8 * s, va_list * args)
174{
175 u8 *data = va_arg (*args, u8 *);
176 u8 *id = va_arg (*args, u8 *);
Christophe Fontained3c008d2017-10-02 18:10:54 +0200177 u32 indent = format_get_indent (s);
Damjan Marion7d221612016-12-26 11:39:42 +0100178 char *string_types[] = { "PN", "EC", "SN", "MN", 0 };
179 uword p = 0;
180 int first_line = 1;
181
182 if (vec_len (data) < 3)
183 return s;
184
185 while (p + 3 < vec_len (data))
186 {
187
188 if (data[p] == 0 && data[p + 1] == 0)
189 return s;
190
191 if (p + data[p + 2] > vec_len (data))
192 return s;
193
194 if (id == 0)
195 {
196 int is_string = 0;
197 char **c = string_types;
198
199 while (c[0])
200 {
201 if (*(u16 *) & data[p] == *(u16 *) c[0])
202 is_string = 1;
203 c++;
204 }
205
206 if (data[p + 2])
207 {
208 if (!first_line)
209 s = format (s, "\n%U", format_white_space, indent);
210 else
211 {
212 first_line = 0;
213 s = format (s, " ");
214 }
215
216 s = format (s, "%c%c: ", data[p], data[p + 1]);
217 if (is_string)
218 vec_add (s, data + p + 3, data[p + 2]);
219 else
220 {
221 int i;
222 const int max_bytes = 8;
223 s = format (s, "0x");
224 for (i = 0; i < clib_min (data[p + 2], max_bytes); i++)
225 s = format (s, " %02x", data[p + 3 + i]);
226
227 if (data[p + 2] > max_bytes)
228 s = format (s, " ...");
229 }
230 }
231 }
232 else if (*(u16 *) & data[p] == *(u16 *) id)
233 {
234 vec_add (s, data + p + 3, data[p + 2]);
235 return s;
236 }
237
238 p += 3 + data[p + 2];
239 }
240
241 return s;
242}
243
Damjan Marion5a206ea2016-05-12 22:11:03 +0200244
Dave Barach9b8ffd92016-07-08 08:13:45 -0400245/* *INDENT-OFF* */
Damjan Marion5a206ea2016-05-12 22:11:03 +0200246VLIB_CLI_COMMAND (show_pci_command, static) = {
247 .path = "show pci",
248 .short_help = "show pci [all]",
249 .function = show_pci_fn,
250};
Dave Barach9b8ffd92016-07-08 08:13:45 -0400251/* *INDENT-ON* */
Damjan Marion5a206ea2016-05-12 22:11:03 +0200252
Dave Barach9b8ffd92016-07-08 08:13:45 -0400253clib_error_t *
254pci_bus_init (vlib_main_t * vm)
Damjan Marion5a206ea2016-05-12 22:11:03 +0200255{
Damjan Marion23227982018-10-22 13:38:57 +0200256 vlib_pci_main_t *pm = &pci_main;
257 pm->log_default = vlib_log_register_class ("pci", 0);
258 return 0;
Damjan Marion5a206ea2016-05-12 22:11:03 +0200259}
260
261VLIB_INIT_FUNCTION (pci_bus_init);
Dave Barach9b8ffd92016-07-08 08:13:45 -0400262
263/*
264 * fd.io coding-style-patch-verification: ON
265 *
266 * Local Variables:
267 * eval: (c-set-style "gnu")
268 * End:
269 */