1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
|
/*
* Copyright (c) 2016 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* pci.c: Linux user space PCI bus management.
*
* Copyright (c) 2008 Eliot Dresselhaus
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <vlib/vlib.h>
#include <vlib/pci/pci.h>
#include <vlib/unix/unix.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/ioctl.h>
#include <net/if.h>
#ifdef __linux__
#include <linux/ethtool.h>
#include <linux/sockios.h>
#endif /* __linux__ */
vlib_pci_main_t pci_main;
VLIB_REGISTER_LOG_CLASS (pci_log, static) = {
.class_name = "pci",
};
#define log_debug(h, f, ...) \
vlib_log (VLIB_LOG_LEVEL_DEBUG, pci_log.class, "%U: " f, \
format_vlib_pci_log, h, ##__VA_ARGS__)
u8 *
format_vlib_pci_log (u8 *s, va_list *va)
{
vlib_pci_dev_handle_t h = va_arg (*va, vlib_pci_dev_handle_t);
return format (s, "%U", format_vlib_pci_addr,
vlib_pci_get_addr (vlib_get_main (), h));
}
vlib_pci_device_info_t *__attribute__ ((weak))
vlib_pci_get_device_info (vlib_main_t *vm, vlib_pci_addr_t *addr,
clib_error_t **error)
{
if (error)
*error = clib_error_return (0, "unsupported");
return 0;
}
clib_error_t *__attribute__ ((weak))
vlib_pci_get_device_root_bus (vlib_pci_addr_t *addr, vlib_pci_addr_t *root_bus)
{
return 0;
}
vlib_pci_addr_t * __attribute__ ((weak)) vlib_pci_get_all_dev_addrs ()
{
return 0;
}
static clib_error_t *
_vlib_pci_config_set_control_bit (vlib_main_t *vm, vlib_pci_dev_handle_t h,
u16 bit, int new_val, int *already_set)
{
u16 control, old;
clib_error_t *err;
err = vlib_pci_read_write_config (
vm, h, VLIB_READ, STRUCT_OFFSET_OF (vlib_pci_config_t, command), &old,
STRUCT_SIZE_OF (vlib_pci_config_t, command));
if (err)
return err;
control = new_val ? old | bit : old & ~bit;
*already_set = old == control;
if (*already_set)
return 0;
return vlib_pci_read_write_config (
vm, h, VLIB_WRITE, STRUCT_OFFSET_OF (vlib_pci_config_t, command), &control,
STRUCT_SIZE_OF (vlib_pci_config_t, command));
}
clib_error_t *
vlib_pci_intr_enable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
const vlib_pci_config_reg_command_t cmd = { .intx_disable = 1 };
clib_error_t *err;
int already_set;
err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 0, &already_set);
log_debug (h, "interrupt%senabled", already_set ? " " : " already ");
return err;
}
clib_error_t *
vlib_pci_intr_disable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
const vlib_pci_config_reg_command_t cmd = { .intx_disable = 1 };
clib_error_t *err;
int already_set;
err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 1, &already_set);
log_debug (h, "interrupt%sdisabled", already_set ? " " : " already ");
return err;
}
clib_error_t *
vlib_pci_bus_master_enable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
const vlib_pci_config_reg_command_t cmd = { .bus_master = 1 };
clib_error_t *err;
int already_set;
err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 1, &already_set);
log_debug (h, "bus-master%senabled", already_set ? " " : " already ");
return err;
}
clib_error_t *
vlib_pci_bus_master_disable (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
const vlib_pci_config_reg_command_t cmd = { .bus_master = 1 };
clib_error_t *err;
int already_set;
err = _vlib_pci_config_set_control_bit (vm, h, cmd.as_u16, 0, &already_set);
log_debug (h, "bus-master%sdisabled", already_set ? " " : " already ");
return err;
}
clib_error_t *
vlib_pci_function_level_reset (vlib_main_t *vm, vlib_pci_dev_handle_t h)
{
vlib_pci_config_t cfg;
pci_capability_pcie_t *cap;
pci_capability_pcie_dev_control_t dev_control;
clib_error_t *err;
u8 offset;
log_debug (h, "function level reset");
err = vlib_pci_read_write_config (vm, h, VLIB_READ, 0, &cfg, sizeof (cfg));
if (err)
return err;
offset = cfg.cap_ptr;
do
{
cap = (pci_capability_pcie_t *) (cfg.data + offset);
if (cap->capability_id == PCI_CAP_ID_PCIE)
break;
offset = cap->next_offset;
}
while (offset);
if (cap->capability_id != PCI_CAP_ID_PCIE)
return clib_error_return (0, "PCIe capability config not found");
if (cap->dev_caps.flr_capable == 0)
return clib_error_return (0, "PCIe function level reset not supported");
dev_control = cap->dev_control;
dev_control.function_level_reset = 1;
if ((err = vlib_pci_write_config_u16 (
vm, h, offset + STRUCT_OFFSET_OF (pci_capability_pcie_t, dev_control),
&dev_control.as_u16)))
return err;
return 0;
}
static clib_error_t *
show_pci_fn (vlib_main_t * vm,
unformat_input_t * input, vlib_cli_command_t * cmd)
{
vlib_pci_addr_t *addr = 0, *addrs;
int show_all = 0;
u8 *s = 0;
while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
{
if (unformat (input, "all"))
show_all = 1;
else
return clib_error_return (0, "unknown input `%U'",
format_unformat_error, input);
}
vlib_cli_output (vm, "%-13s%-5s%-12s%-14s%-16s%-32s%s",
"Address", "Sock", "VID:PID", "Link Speed", "Driver",
"Product Name", "Vital Product Data");
addrs = vlib_pci_get_all_dev_addrs ();
vec_foreach (addr, addrs)
{
vlib_pci_device_info_t *d;
d = vlib_pci_get_device_info (vm, addr, 0);
if (!d)
continue;
if (d->device_class != PCI_CLASS_NETWORK_ETHERNET && !show_all)
continue;
vec_reset_length (s);
if (d->numa_node >= 0)
s = format (s, " %d", d->numa_node);
vlib_cli_output (
vm, "%-13U%-5v%04x:%04x %-14U%-16s%-32v%U", format_vlib_pci_addr,
addr, s, d->vendor_id, d->device_id, format_vlib_pci_link_speed, d,
d->driver_name ? (char *) d->driver_name : "", d->product_name,
format_vlib_pci_vpd, d->vpd_r, (u8 *) 0);
vlib_pci_free_device_info (d);
}
vec_free (s);
vec_free (addrs);
return 0;
}
uword
unformat_vlib_pci_addr (unformat_input_t * input, va_list * args)
{
vlib_pci_addr_t *addr = va_arg (*args, vlib_pci_addr_t *);
u32 x[4];
if (!unformat (input, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
return 0;
addr->domain = x[0];
addr->bus = x[1];
addr->slot = x[2];
addr->function = x[3];
return 1;
}
u8 *
format_vlib_pci_addr (u8 * s, va_list * va)
{
vlib_pci_addr_t *addr = va_arg (*va, vlib_pci_addr_t *);
return format (s, "%04x:%02x:%02x.%x", addr->domain, addr->bus,
addr->slot, addr->function);
}
u8 *
format_vlib_pci_link_port (u8 *s, va_list *va)
{
vlib_pci_config_t *c = va_arg (*va, vlib_pci_config_t *);
pci_capability_pcie_t *r = pci_config_find_capability (c, PCI_CAP_ID_PCIE);
if (!r)
return format (s, "unknown");
return format (s, "P%d", r->link_caps.port_number);
}
static u8 *
_vlib_pci_link_speed (u8 *s, u8 speed, u8 width)
{
static char *speeds[] = {
[1] = "2.5", [2] = "5.0", [3] = "8.0", [4] = "16.0", [5] = "32.0"
};
if (speed >= ARRAY_LEN (speeds) || speeds[speed] == 0)
s = format (s, "unknown speed");
else
s = format (s, "%s GT/s", speeds[speed]);
return format (s, " x%u", width);
}
u8 *
format_vlib_pci_link_speed (u8 *s, va_list *va)
{
vlib_pci_config_t *c = va_arg (*va, vlib_pci_config_t *);
pci_capability_pcie_t *r = pci_config_find_capability (c, PCI_CAP_ID_PCIE);
if (!r)
return format (s, "unknown");
return _vlib_pci_link_speed (s, r->link_status.link_speed,
r->link_status.negotiated_link_width);
}
u8 *
format_vlib_pci_link_speed_cap (u8 *s, va_list *va)
{
vlib_pci_config_t *c = va_arg (*va, vlib_pci_config_t *);
pci_capability_pcie_t *r = pci_config_find_capability (c, PCI_CAP_ID_PCIE);
if (!r)
return format (s, "unknown");
return _vlib_pci_link_speed (s, r->link_caps.max_link_speed,
r->link_caps.max_link_width);
}
u8 *
format_vlib_pci_vpd (u8 * s, va_list * args)
{
u8 *data = va_arg (*args, u8 *);
u8 *id = va_arg (*args, u8 *);
u32 indent = format_get_indent (s);
char *string_types[] = { "PN", "EC", "SN", "MN", 0 };
uword p = 0;
int first_line = 1;
if (vec_len (data) < 3)
return s;
while (p + 3 < vec_len (data))
{
if (data[p] == 0 && data[p + 1] == 0)
return s;
if (p + data[p + 2] > vec_len (data))
return s;
if (id == 0)
{
int is_string = 0;
char **c = string_types;
while (c[0])
{
if (*(u16 *) & data[p] == *(u16 *) c[0])
is_string = 1;
c++;
}
if (data[p + 2])
{
if (!first_line)
s = format (s, "\n%U", format_white_space, indent);
else
{
first_line = 0;
s = format (s, " ");
}
s = format (s, "%c%c: ", data[p], data[p + 1]);
if (is_string)
vec_add (s, data + p + 3, data[p + 2]);
else
{
int i;
const int max_bytes = 8;
s = format (s, "0x");
for (i = 0; i < clib_min (data[p + 2], max_bytes); i++)
s = format (s, " %02x", data[p + 3 + i]);
if (data[p + 2] > max_bytes)
s = format (s, " ...");
}
}
}
else if (*(u16 *) & data[p] == *(u16 *) id)
{
vec_add (s, data + p + 3, data[p + 2]);
return s;
}
p += 3 + data[p + 2];
}
return s;
}
VLIB_CLI_COMMAND (show_pci_command, static) = {
.path = "show pci",
.short_help = "show pci [all]",
.function = show_pci_fn,
};
|