-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrscdump.c
561 lines (474 loc) · 16.1 KB
/
rscdump.c
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. */
#include <stdio.h>
#include <stddef.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mlx5ctlu.h"
#include "ifcutil.h"
#include "mlx5_ifc.h"
#include "reg.h"
#include "mlx5lib.h"
static void print_rsc_dump_reg(void *rscdmp)
{
#define print_fld(fld) printf("\t%s %d\n", #fld, MLX5_GET(resource_dump, rscdmp, fld))
#define print_fldx(fld) printf("\t%s %x\n", #fld, MLX5_GET(resource_dump, rscdmp, fld))
#define print_fld64(fld) printf("\t%s 0x%lx\n", #fld, MLX5_GET64(resource_dump, rscdmp, fld))
print_fldx(segment_type);
print_fld(seq_num);
print_fld(more_dump);
print_fld64(device_opaque);
print_fldx(index1);
print_fldx(index2);
print_fld(num_of_obj1);
print_fld(num_of_obj2);
print_fldx(mkey);
print_fld64(address);
print_fld(inline_dump);
print_fld(size);
#undef print_fld
#undef print_fldx
}
static int mlx5_rsc_dump_iter(struct mlx5u_dev *dev, void *in, void *out)
{
int reg_size = MLX5_ST_SZ_BYTES(resource_dump);
u32 out_seq_num, in_seq_num;
int err;
dbg_msg(1, "Resource dump access reg IN:\n");
if (verbosity_level)
print_rsc_dump_reg(in);
err = mlx5_access_reg(dev, in, reg_size, out, reg_size,
MLX5_REG_RESOURCE_DUMP, 0, 0);
if (err) {
err_msg("MLX5_REG_RESOURCE_DUMP: Failed to access err %d\n", err);
return -EIO;
}
dbg_msg(1, "Resource dump access reg OUT:\n");
if (verbosity_level)
print_rsc_dump_reg(out);
in_seq_num = MLX5_GET(resource_dump, in, seq_num);
out_seq_num = MLX5_GET(resource_dump, out, seq_num);
if (out_seq_num && (in_seq_num + 1 != out_seq_num))
return -EREMOTEIO;
/* In case more_dump was set in the device reply, user shall recall the resource_dump command.
resource_type, with seq_num, more_dump, index1, index2, num_of_obj1, num_of_obj2,
device_opaque and more_dump are set to the same value that was returned by the device upon
last return. segment_type shall be kept as was in the original call. */
if (MLX5_GET(resource_dump, out, more_dump))
{
#define cpy_out2in(fld) MLX5_SET(resource_dump, in, fld, MLX5_GET(resource_dump, out, fld))
cpy_out2in(seq_num);
cpy_out2in(index1);
cpy_out2in(index2);
cpy_out2in(num_of_obj1);
cpy_out2in(num_of_obj2);
cpy_out2in(more_dump);
MLX5_SET64(resource_dump, in, device_opaque, MLX5_GET64(resource_dump, out, device_opaque));
return 1;
}
return 0;
}
typedef struct {
int umem;
int type;
int index1;
int index2;
int vhca_id;
} Args;
static void *rscdump_collect(struct mlx5u_dev *dev, Args args, int *total)
{
u8 in[MLX5_ST_SZ_BYTES(resource_dump)] = {};
u8 out[MLX5_ST_SZ_BYTES(resource_dump)] = {};
void *data = NULL;
int size = 4069;
int err;
size = *total ? *total: size;
data = malloc(size);
if (!data) {
err_msg("Failed to allocate memory size %d\n", size);
return NULL;
}
MLX5_SET(resource_dump, in, segment_type, args.type);
MLX5_SET(resource_dump, in, index1, args.index1);
MLX5_SET(resource_dump, in, index2, args.index2);
MLX5_SET(resource_dump, in, vhca_id, args.vhca_id);
MLX5_SET(resource_dump, in, inline_dump, 1);
do {
err = mlx5_rsc_dump_iter(dev, in, out);
if (err < 0) {
err_msg("Resource dump: Failed to access err %d\n", err);
return *total ? data : NULL;
}
// copy inline data to data
void *iter_data = MLX5_ADDR_OF(resource_dump, out, inline_data);
int iter_size = MLX5_GET(resource_dump, out, size);
if ( *total + iter_size > size) { /* no room for new data */
void *new_data = realloc(data, size * 2);
if (!new_data) {
err_msg("Failed to re-allocate memory size %d\n", size * 2);
return data;
}
data = new_data;
size *= 2;
}
memcpy(data + *total, iter_data, iter_size);
*total += iter_size;
} while (err > 0);
return data;
}
static void *rscdump_collect_umem(struct mlx5u_dev *dev, Args args,
struct mlx5_umem_buff *umem_buff)
{
u8 in[MLX5_ST_SZ_BYTES(resource_dump)] = {};
u8 out[MLX5_ST_SZ_BYTES(resource_dump)] = {};
int remain = umem_buff->size;
int err;
int i = 0;
void *temp_buffer = malloc(1024 * 1024);
if (!temp_buffer) {
err_msg("Failed to allocate temp buffer\n");
return NULL;
}
MLX5_SET(resource_dump, in, segment_type, args.type);
MLX5_SET(resource_dump, in, index1, args.index1);
MLX5_SET(resource_dump, in, index2, args.index2);
MLX5_SET(resource_dump, in, vhca_id, args.vhca_id);
MLX5_SET(resource_dump, in, inline_dump, 0);
MLX5_SET(resource_dump, in, mkey, umem_buff->umem_mkey);
MLX5_SET64(resource_dump, in, address, (uint64_t)umem_buff->buff);
MLX5_SET(resource_dump, in, size, remain);
int total = 0;
do {
err = mlx5_rsc_dump_iter(dev, in, out);
if (err < 0) {
err_msg("Resource dump: Failed to access err %d\n", err);
return total ? temp_buffer : NULL;
}
int iter_size = MLX5_GET(resource_dump, out, size);
if (iter_size < remain)
remain -= iter_size;
info_msg("[%d] Resource dump:\n", i++);
info_msg("\tcollected %d bytes, remain %d bytes, total %zu, has_more %d\n",
iter_size, remain, umem_buff->size - remain, err > 0);
// copy from data into temp buffer
hexdump(umem_buff->buff, iter_size);
memcpy(temp_buffer + total, umem_buff->buff, iter_size);
total = total + iter_size;
if (remain == 0)
break;
} while (err > 0);
return temp_buffer;
}
enum mlx5_rsc_sgmt_type {
/* resource segments range 0x0000 to 0xfeff */
MLX5_RSC_SGMT_TYPE_DEVICE_DUMP = 0x1000,
/* end resource segment range */
MLX5_RSC_SGMT_TYPE_NOTICE = 0xfff9,
MLX5_RSC_SGMT_TYPE_CMD = 0xfffa,
MLX5_RSC_SGMT_TYPE_TERMINATE = 0xfffb,
MLX5_RSC_SGMT_TYPE_ERROR = 0xfffc,
MLX5_RSC_SGMT_TYPE_REFERENCE = 0xfffd,
MLX5_RSC_SGMT_TYPE_INFO = 0xfffe,
MLX5_RSC_SGMT_TYPE_MENU = 0xffff,
};
static const char *sgmt_type2str(enum mlx5_rsc_sgmt_type sgmt_type)
{
switch (sgmt_type) {
case MLX5_RSC_SGMT_TYPE_DEVICE_DUMP:
return "Device dump";
case MLX5_RSC_SGMT_TYPE_NOTICE:
return "Notice";
case MLX5_RSC_SGMT_TYPE_CMD:
return "Command";
case MLX5_RSC_SGMT_TYPE_TERMINATE:
return "Terminate";
case MLX5_RSC_SGMT_TYPE_ERROR:
return "Error";
case MLX5_RSC_SGMT_TYPE_REFERENCE:
return "Reference";
case MLX5_RSC_SGMT_TYPE_INFO:
return "Info";
case MLX5_RSC_SGMT_TYPE_MENU:
return "Menu";
default:
if (sgmt_type >= 0x0000 && sgmt_type <= 0xfeff)
return "A Resource";
return "Unknown";
}
}
static int mlx5_rsc_sgmt_type_by_name(char *name)
{
return -1; /* Not implemented */
}
static void print_menu_record(void *record)
{
int seg_type = MLX5_GET(resource_dump_menu_record, record, segment_type);
#define print_fld(fld) printf("\t%s: %d\n", #fld, MLX5_GET(resource_dump_menu_record, record, fld))
//#define print_fldx(fld) printf("\t%s: 0x%x\n", #fld, MLX5_GET(resource_dump_menu_record, record, fld))
printf("\tsegment_type: 0x%x (%s)\n", seg_type, sgmt_type2str(seg_type));
// print_fldx(segment_type);
print_fld(support_index1);
print_fld(must_have_index1);
print_fld(support_index2);
print_fld(must_have_index2);
print_fld(support_num_of_obj1);
print_fld(must_have_num_of_obj1);
print_fld(support_num_of_obj2);
print_fld(must_have_num_of_obj2);
print_fld(num_of_obj1_supports_all);
print_fld(num_of_obj1_supports_active);
print_fld(num_of_obj2_supports_all);
print_fld(num_of_obj2_supports_active);
printf("\tsegment_name: %s\n", (char *)MLX5_ADDR_OF(resource_dump_menu_record, record, segment_name[0]));
printf("\tindex1_name: %s\n", (char *)MLX5_ADDR_OF(resource_dump_menu_record, record, index1_name[0]));
printf("\tindex2_name: %s\n", (char *)MLX5_ADDR_OF(resource_dump_menu_record, record, index2_name[0]));
}
static void parse_menu_segment(void *menu)
{
int num_records = 0;
void *records;
int size = 0;
num_records = MLX5_GET(resource_dump_menu_segment, menu, num_of_records);
info_msg("Resource dump menu size %d num of records %d\n", size, num_records);
records = MLX5_ADDR_OF(resource_dump_menu_segment, menu, record[0]);
for (int i = 0; i < num_records; i++) {
void *record = records + i * MLX5_ST_SZ_BYTES(resource_dump_menu_record);
printf("Menu Record %d\n", i);
print_menu_record(record);
}
}
struct sgmnt_hdr {
enum mlx5_rsc_sgmt_type segment_type;
u32 len; /* bytes */
};
static void get_sgmnt_hdr(void *sgmnt, struct sgmnt_hdr *sgmnt_hdr)
{
sgmnt_hdr->segment_type = MLX5_GET(resource_dump_segment_header, sgmnt, segment_type);
sgmnt_hdr->len = MLX5_GET(resource_dump_segment_header, sgmnt, length_dw) * 4;
}
static void parse_err_segment(void *segment)
{
char str[64] = {};
memcpy(str, MLX5_ADDR_OF(resource_dump_error_segment, segment, error),
MLX5_FLD_SZ_BYTES(resource_dump_error_segment, error));
printf("\terror: syndrome 0x%x, %s\n",
MLX5_GET(resource_dump_error_segment, segment, syndrome_id),
str);
}
static void parse_notice_segment(void *segment)
{
char str[64] = {};
memcpy(str, MLX5_ADDR_OF(resource_dump_notice_segment, segment, notice),
MLX5_FLD_SZ_BYTES(resource_dump_notice_segment, notice));
printf("\tnotice: syndrome 0x%x, %s\n",
MLX5_GET(resource_dump_notice_segment, segment, syndrome_id),
str);
}
static void parse_reference_segment(void *segment)
{
printf("\treference_segment: 0x%x, index1 0x%x, index2 0x%x, num_of_obj1 %d, num_of_obj2 %d\n",
MLX5_GET(resource_dump_reference_segment, segment, reference_segment),
MLX5_GET(resource_dump_reference_segment, segment, index1),
MLX5_GET(resource_dump_reference_segment, segment, index2),
MLX5_GET(resource_dump_reference_segment, segment, num_of_obj1),
MLX5_GET(resource_dump_reference_segment, segment, num_of_obj2));
}
static void print_sgmt_header(void *hdr)
{
struct sgmnt_hdr sgmnt_hdr = {};
get_sgmnt_hdr(hdr, &sgmnt_hdr);
info_msg("%s Segment: type 0x%x, length %d\n",
sgmt_type2str(sgmnt_hdr.segment_type),
sgmnt_hdr.segment_type,
sgmnt_hdr.len);
}
static int parse_segment(void *segment) {
struct sgmnt_hdr sgmnt_hdr = {};
get_sgmnt_hdr(segment, &sgmnt_hdr);
print_sgmt_header(segment);
switch (sgmnt_hdr.segment_type) {
case MLX5_RSC_SGMT_TYPE_TERMINATE:
info_msg("end: %s\n", sgmt_type2str(sgmnt_hdr.segment_type));
return 1;
case MLX5_RSC_SGMT_TYPE_ERROR:
parse_err_segment(segment);
return 1;
case MLX5_RSC_SGMT_TYPE_NOTICE:
parse_notice_segment(segment);
return 0;
case MLX5_RSC_SGMT_TYPE_REFERENCE:
parse_reference_segment(segment);
return 0;
case MLX5_RSC_SGMT_TYPE_MENU:
parse_menu_segment(segment);
return 0;
default:
break;
}
if (sgmnt_hdr.segment_type > 0xfeff) /* Not a resource segment */
return 0;
info_msg("\tindex1 0x%x, index2 0x%x\n",
MLX5_GET(resource_dump_resource_segment, segment, index1),
MLX5_GET(resource_dump_resource_segment, segment, index2));
hexdump(MLX5_ADDR_OF(resource_dump_resource_segment, segment, payload),
sgmnt_hdr.len - MLX5_ST_SZ_BYTES(resource_dump_resource_segment));
return 0;
}
static void parse_resource_dump(void *data, int size)
{
void *info = MLX5_ADDR_OF(resource_dump_response, data, info);
info_msg("Resource dump size %d\n", size);
print_sgmt_header(info);
printf("\tdump_version %d, hw_version %d, fw_version 0%x\n",
MLX5_GET(resource_dump_info_segment, info, dump_version),
MLX5_GET(resource_dump_info_segment, info, hw_version),
MLX5_GET(resource_dump_info_segment, info, fw_version));
void *cmd = MLX5_ADDR_OF(resource_dump_response, data, cmd);
print_sgmt_header(cmd);
printf("\tsegment_called 0x%x, vhca_id 0x%x, index1 0x%x, index2 0x%x, num_of_obj1 %d, num_of_obj2 %d\n",
MLX5_GET(resource_dump_command_segment, cmd, segment_called),
MLX5_GET(resource_dump_command_segment, cmd, vhca_id),
MLX5_GET(resource_dump_command_segment, cmd, index1),
MLX5_GET(resource_dump_command_segment, cmd, index2),
MLX5_GET(resource_dump_command_segment, cmd, num_of_obj1),
MLX5_GET(resource_dump_command_segment, cmd, num_of_obj2));
if (size < MLX5_ST_SZ_BYTES(resource_dump_response) + MLX5_ST_SZ_BYTES(resource_dump_terminate_segment))
return;
void *segment = MLX5_ADDR_OF(resource_dump_response, data, segment[0]);
while (!parse_segment(segment)) {
segment = segment + MLX5_GET(resource_dump_segment_header, segment, length_dw) * 4;
if (segment - data >= size) {
err_msg("Segment overflow %ld, data size %d\n", segment - data, size);
break;
}
}
}
static struct mlx5_umem_buff *umem_buff = NULL;
static void mlx5_rsc_dump(struct mlx5u_dev *dev, Args args)
{
void *data;
int size = 0;
if (!umem_buff) {
data = rscdump_collect(dev, args, &size);
if (!data)
return;
} else {
data = rscdump_collect_umem(dev, args, umem_buff);
if (!data)
return;
size = umem_buff->size;
}
parse_resource_dump(data, size);
if (!umem_buff)
free(data);
}
static int str_starts_with(const char *str, const char *prefix)
{
return strncmp(str, prefix, strlen(prefix)) == 0;
}
static int parse_rsc_dump_type_arg(char *arg)
{
enum mlx5_rsc_sgmt_type type = 0;
char byname[64] = {};
if (sscanf(arg, "--type=0x%x", &type) == 1)
return type;
if (sscanf(arg, "--type=%s", byname) > 0)
return mlx5_rsc_sgmt_type_by_name(byname);
return 0;
}
static const char *usage = "[--umem=<size KB>] [--type=<type>] [--idx1=<index1>] [--idx2=<index2>] [--vhcaid=<vhca_id>] [--help]";
static const char *usage_core_dump = "[--umem=<size KB>] [--help]";
static Args parse_args(int argc, char *argv[])
{
Args args = {0};
const char *usage_str;
args.type = -1;
if (strcmp(argv[0], "coredump") == 0)
usage_str = usage_core_dump;
else
usage_str = usage;
for (int i = 1; i < argc; i++) {
if (str_starts_with(argv[i], "--umem=")) {
args.umem = atoi(argv[i] + 7);
} else if (str_starts_with(argv[i], "--type=")) {
args.type = parse_rsc_dump_type_arg(argv[i]);
} else if (str_starts_with(argv[i], "--idx1=")) {
sscanf(argv[i] + 7, "0x%x", &args.index1);
} else if (str_starts_with(argv[i], "--idx2=")) {
sscanf(argv[i] + 7, "0x%x", &args.index2);
} else if (str_starts_with(argv[i], "--vhcaid=")) {
args.vhca_id = atoi(argv[i] + 9);
} else if (str_starts_with(argv[i], "--help")) {
printf("Usage: %s %s\n", argv[0], usage_str);
exit(0);
} else {
printf("Unknown argument %s\n", argv[i]);
printf("Usage: %s %s\n", argv[0], usage_str);
exit(1);
}
}
return args;
}
static void mlx5_core_dump(struct mlx5u_dev *dev);
int do_rscdump(struct mlx5u_dev *dev, int argc, char *argv[])
{
Args args = parse_args(argc, argv);
uint32_t umem_pdn = 0;
if (args.type < 0)
args.type = MLX5_RSC_SGMT_TYPE_MENU;
printf("umem %d, type 0x%x, index1 0x%x, index2 0x%x, vhca_id=0x%x\n",
args.umem, args.type, args.index1, args.index2, args.vhca_id);
if (args.umem) {
size_t umem_buff_size = args.umem * 1024;
int err;
err = mlx5lib_alloc_pd(dev, &umem_pdn, 0);
if (err)
return err;
umem_buff = mlx5lib_alloc_umem_mkey_buff(dev, umem_buff_size, umem_pdn);
if (!umem_buff) {
mlx5lib_dealloc_pd(dev, umem_pdn, 0);
return -ENOMEM;
}
}
if (!strcmp(argv[0], "rscdump"))
mlx5_rsc_dump(dev, args);
else if (!strcmp(argv[0], "coredump"))
mlx5_core_dump(dev);
else
err_msg("Unknown command \"%s\"\n", argv[0]);
if (umem_buff) {
mlx5lib_free_umem_mkey_buff(dev, umem_buff);
mlx5lib_dealloc_pd(dev, umem_pdn, 0);
}
return 0;
}
/* this is not part of rsc dump, but they share umem implementation */
static void mlx5_core_dump(struct mlx5u_dev *dev)
{
u8 in[MLX5_ST_SZ_BYTES(core_dump_reg)] = {};
u8 out[MLX5_ST_SZ_BYTES(core_dump_reg)] = {};
int err;
if (!umem_buff) {
err_msg("please allocate umem buff first\n");
return;
}
MLX5_SET(core_dump_reg, in, core_dump_type,
MLX5_CORE_DUMP_REG_CORE_DUMP_TYPE_CR_DUMP_TO_MEM);
MLX5_SET64(core_dump_reg, in, address, (uint64_t)umem_buff->buff);
MLX5_SET(core_dump_reg, in, size, umem_buff->size);
MLX5_SET(core_dump_reg, in, mkey, umem_buff->umem_mkey);
err = mlx5_access_reg(dev, in, sizeof(in), out, sizeof(out),
MLX5_REG_CORE_DUMP, 0, 0);
if (err) {
err_msg("Failed to access core dump err %d\n", err);
return;
}
hexdump(umem_buff->buff, MLX5_GET(core_dump_reg, out, size));
info_msg("Core dump done\n");
info_msg("Core dump size %d\n", MLX5_GET(core_dump_reg, out, size));
info_msg("Core dump address 0x%lx\n", MLX5_GET64(core_dump_reg, out, address));
info_msg("Core dump cookie 0x%lx\n", MLX5_GET64(core_dump_reg, out, cookie));
info_msg("More Dump %d\n", MLX5_GET(core_dump_reg, out, more_dump));
}