summaryrefslogtreecommitdiff
path: root/usr/src/cmd/pcieadm/pcieadm_devs.c
blob: a831d5e8f24fe040b07ad662ae695d56192f55ec (plain)
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2022 Oxide Computer Company
 */

#include <err.h>
#include <stdio.h>
#include <unistd.h>
#include <ofmt.h>
#include <strings.h>
#include <sys/pci.h>

#include "pcieadm.h"

typedef struct pcieadm_show_devs {
	pcieadm_t *psd_pia;
	ofmt_handle_t psd_ofmt;
	boolean_t psd_funcs;
	int psd_nfilts;
	char **psd_filts;
	boolean_t *psd_used;
	uint_t psd_nprint;
} pcieadm_show_devs_t;

typedef enum pcieadm_show_devs_otype {
	PCIEADM_SDO_VID,
	PCIEADM_SDO_DID,
	PCIEADM_SDO_BDF,
	PCIEADM_SDO_BDF_BUS,
	PCIEADM_SDO_BDF_DEV,
	PCIEADM_SDO_BDF_FUNC,
	PCIEADM_SDO_DRIVER,
	PCIEADM_SDO_TYPE,
	PCIEADM_SDO_VENDOR,
	PCIEADM_SDO_DEVICE,
	PCIEADM_SDO_PATH,
	PCIEADM_SDO_MAXSPEED,
	PCIEADM_SDO_MAXWIDTH,
	PCIEADM_SDO_CURSPEED,
	PCIEADM_SDO_CURWIDTH,
	PCIEADM_SDO_SUPSPEEDS
} pcieadm_show_devs_otype_t;

typedef struct pcieadm_show_devs_ofmt {
	int psdo_vid;
	int psdo_did;
	uint_t psdo_bus;
	uint_t psdo_dev;
	uint_t psdo_func;
	const char *psdo_path;
	const char *psdo_vendor;
	const char *psdo_device;
	const char *psdo_driver;
	int psdo_instance;
	int psdo_mwidth;
	int psdo_cwidth;
	int64_t psdo_mspeed;
	int64_t psdo_cspeed;
	int psdo_nspeeds;
	int64_t *psdo_sspeeds;
} pcieadm_show_devs_ofmt_t;

static uint_t
pcieadm_speed2gen(int64_t speed)
{
	if (speed == 2500000000LL) {
		return (1);
	} else if (speed == 5000000000LL) {
		return (2);
	} else if (speed == 8000000000LL) {
		return (3);
	} else if (speed == 16000000000LL) {
		return (4);
	} else if (speed == 32000000000LL) {
		return (5);
	} else {
		return (0);
	}
}

static const char *
pcieadm_speed2str(int64_t speed)
{
	if (speed == 2500000000LL) {
		return ("2.5");
	} else if (speed == 5000000000LL) {
		return ("5.0");
	} else if (speed == 8000000000LL) {
		return ("8.0");
	} else if (speed == 16000000000LL) {
		return ("16.0");
	} else if (speed == 32000000000LL) {
		return ("32.0");
	} else {
		return (NULL);
	}
}

static boolean_t
pcieadm_show_devs_ofmt_cb(ofmt_arg_t *ofarg, char *buf, uint_t buflen)
{
	const char *str;
	pcieadm_show_devs_ofmt_t *psdo = ofarg->ofmt_cbarg;
	boolean_t first = B_TRUE;

	switch (ofarg->ofmt_id) {
	case PCIEADM_SDO_BDF:
		if (snprintf(buf, buflen, "%x/%x/%x", psdo->psdo_bus,
		    psdo->psdo_dev, psdo->psdo_func) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_BDF_BUS:
		if (snprintf(buf, buflen, "%x", psdo->psdo_bus) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_BDF_DEV:
		if (snprintf(buf, buflen, "%x", psdo->psdo_dev) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_BDF_FUNC:
		if (snprintf(buf, buflen, "%x", psdo->psdo_func) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_DRIVER:
		if (psdo->psdo_driver == NULL || psdo->psdo_instance == -1) {
			(void) snprintf(buf, buflen, "--");
		} else if (snprintf(buf, buflen, "%s%d", psdo->psdo_driver,
		    psdo->psdo_instance) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_PATH:
		if (strlcat(buf, psdo->psdo_path, buflen) >= buflen) {
			return (B_TRUE);
		}
		break;
	case PCIEADM_SDO_VID:
		if (psdo->psdo_vid == -1) {
			(void) strlcat(buf, "--", buflen);
		} else if (snprintf(buf, buflen, "%x", psdo->psdo_vid) >=
		    buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_DID:
		if (psdo->psdo_did == -1) {
			(void) strlcat(buf, "--", buflen);
		} else if (snprintf(buf, buflen, "%x", psdo->psdo_did) >=
		    buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_VENDOR:
		if (strlcat(buf, psdo->psdo_vendor, buflen) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_DEVICE:
		if (strlcat(buf, psdo->psdo_device, buflen) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_MAXWIDTH:
		if (psdo->psdo_mwidth <= 0) {
			(void) strlcat(buf, "--", buflen);
		} else if (snprintf(buf, buflen, "x%u", psdo->psdo_mwidth) >=
		    buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_CURWIDTH:
		if (psdo->psdo_cwidth <= 0) {
			(void) strlcat(buf, "--", buflen);
		} else if (snprintf(buf, buflen, "x%u", psdo->psdo_cwidth) >=
		    buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_MAXSPEED:
		str = pcieadm_speed2str(psdo->psdo_mspeed);
		if (str == NULL) {
			(void) strlcat(buf, "--", buflen);
		} else if (snprintf(buf, buflen, "%s GT/s", str) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_CURSPEED:
		str = pcieadm_speed2str(psdo->psdo_cspeed);
		if (str == NULL) {
			(void) strlcat(buf, "--", buflen);
		} else if (snprintf(buf, buflen, "%s GT/s", str) >= buflen) {
			return (B_FALSE);
		}
		break;
	case PCIEADM_SDO_SUPSPEEDS:
		buf[0] = 0;
		for (int i = 0; i < psdo->psdo_nspeeds; i++) {
			const char *str;

			str = pcieadm_speed2str(psdo->psdo_sspeeds[i]);
			if (str == NULL) {
				continue;
			}

			if (!first) {
				if (strlcat(buf, ",", buflen) >= buflen) {
					return (B_FALSE);
				}
			}
			first = B_FALSE;

			if (strlcat(buf, str, buflen) >= buflen) {
				return (B_FALSE);
			}
		}
		break;
	case PCIEADM_SDO_TYPE:
		if (pcieadm_speed2gen(psdo->psdo_cspeed) == 0 ||
		    psdo->psdo_mwidth == -1) {
			if (strlcat(buf, "PCI", buflen) >= buflen) {
				return (B_FALSE);
			}
		} else {
			if (snprintf(buf, buflen, "PCIe Gen %ux%u",
			    pcieadm_speed2gen(psdo->psdo_cspeed),
			    psdo->psdo_cwidth) >= buflen) {
				return (B_FALSE);
			}
		}
		break;
	default:
		abort();
	}
	return (B_TRUE);
}

static const char *pcieadm_show_dev_fields = "bdf,type,driver,device";
static const char *pcieadm_show_dev_speeds =
	"bdf,driver,maxspeed,curspeed,maxwidth,curwidth,supspeeds";
static const ofmt_field_t pcieadm_show_dev_ofmt[] = {
	{ "VID", 6, PCIEADM_SDO_VID, pcieadm_show_devs_ofmt_cb },
	{ "DID", 6, PCIEADM_SDO_DID, pcieadm_show_devs_ofmt_cb },
	{ "BDF", 8, PCIEADM_SDO_BDF, pcieadm_show_devs_ofmt_cb },
	{ "DRIVER", 15, PCIEADM_SDO_DRIVER, pcieadm_show_devs_ofmt_cb },
	{ "TYPE", 15, PCIEADM_SDO_TYPE, pcieadm_show_devs_ofmt_cb },
	{ "VENDOR", 30, PCIEADM_SDO_VENDOR, pcieadm_show_devs_ofmt_cb },
	{ "DEVICE", 30, PCIEADM_SDO_DEVICE, pcieadm_show_devs_ofmt_cb },
	{ "PATH", 30, PCIEADM_SDO_PATH, pcieadm_show_devs_ofmt_cb },
	{ "BUS", 4, PCIEADM_SDO_BDF_BUS, pcieadm_show_devs_ofmt_cb },
	{ "DEV", 4, PCIEADM_SDO_BDF_DEV, pcieadm_show_devs_ofmt_cb },
	{ "FUNC", 4, PCIEADM_SDO_BDF_FUNC, pcieadm_show_devs_ofmt_cb },
	{ "MAXSPEED", 10, PCIEADM_SDO_MAXSPEED, pcieadm_show_devs_ofmt_cb },
	{ "MAXWIDTH", 10, PCIEADM_SDO_MAXWIDTH, pcieadm_show_devs_ofmt_cb },
	{ "CURSPEED", 10, PCIEADM_SDO_CURSPEED, pcieadm_show_devs_ofmt_cb },
	{ "CURWIDTH", 10, PCIEADM_SDO_CURWIDTH, pcieadm_show_devs_ofmt_cb },
	{ "SUPSPEEDS", 20, PCIEADM_SDO_SUPSPEEDS, pcieadm_show_devs_ofmt_cb },
	{ NULL, 0, 0, NULL }
};

static boolean_t
pcieadm_show_devs_match(pcieadm_show_devs_t *psd,
    pcieadm_show_devs_ofmt_t *psdo)
{
	char dinst[128], bdf[128];

	if (psd->psd_nfilts == 0) {
		return (B_TRUE);
	}

	if (psdo->psdo_driver != NULL && psdo->psdo_instance != -1) {
		(void) snprintf(dinst, sizeof (dinst), "%s%d",
		    psdo->psdo_driver, psdo->psdo_instance);
	}
	(void) snprintf(bdf, sizeof (bdf), "%x/%x/%x", psdo->psdo_bus,
	    psdo->psdo_dev, psdo->psdo_func);

	for (uint_t i = 0; i < psd->psd_nfilts; i++) {
		const char *filt = psd->psd_filts[i];

		if (strcmp(filt, psdo->psdo_path) == 0) {
			psd->psd_used[i] = B_TRUE;
			return (B_TRUE);
		}

		if (strcmp(filt, bdf) == 0) {
			psd->psd_used[i] = B_TRUE;
			return (B_TRUE);
		}

		if (psdo->psdo_driver != NULL &&
		    strcmp(filt, psdo->psdo_driver) == 0) {
			psd->psd_used[i] = B_TRUE;
			return (B_TRUE);
		}

		if (psdo->psdo_driver != NULL && psdo->psdo_instance != -1 &&
		    strcmp(filt, dinst) == 0) {
			psd->psd_used[i] = B_TRUE;
			return (B_TRUE);
		}

		if (strncmp("/devices", filt, strlen("/devices")) == 0) {
			filt += strlen("/devices");
		}

		if (strcmp(filt, psdo->psdo_path) == 0) {
			psd->psd_used[i] = B_TRUE;
			return (B_TRUE);
		}
	}
	return (B_FALSE);
}

static int
pcieadm_show_devs_walk_cb(di_node_t node, void *arg)
{
	int nprop, *regs = NULL, *did, *vid, *mwidth, *cwidth;
	int64_t *mspeed, *cspeed, *sspeeds;
	char *path = NULL;
	pcieadm_show_devs_t *psd = arg;
	int ret = DI_WALK_CONTINUE;
	char venstr[64], devstr[64];
	pcieadm_show_devs_ofmt_t oarg;
	pcidb_hdl_t *pcidb = psd->psd_pia->pia_pcidb;

	bzero(&oarg, sizeof (oarg));

	path = di_devfs_path(node);
	if (path == NULL) {
		err(EXIT_FAILURE, "failed to construct devfs path for node: "
		    "%s (%s)", di_node_name(node));
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "reg", &regs);
	if (nprop <= 0) {
		errx(EXIT_FAILURE, "failed to lookup regs array for %s",
		    path);
	}

	oarg.psdo_path = path;
	oarg.psdo_bus = PCI_REG_BUS_G(regs[0]);
	oarg.psdo_dev = PCI_REG_DEV_G(regs[0]);
	oarg.psdo_func = PCI_REG_FUNC_G(regs[0]);

	if (oarg.psdo_func != 0 && !psd->psd_funcs) {
		goto done;
	}

	oarg.psdo_driver = di_driver_name(node);
	oarg.psdo_instance = di_instance(node);

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "device-id", &did);
	if (nprop != 1) {
		oarg.psdo_did = -1;
	} else {
		oarg.psdo_did = (uint16_t)*did;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node, "vendor-id", &vid);
	if (nprop != 1) {
		oarg.psdo_vid = -1;
	} else {
		oarg.psdo_vid = (uint16_t)*vid;
	}

	oarg.psdo_vendor = "--";
	if (oarg.psdo_vid != -1) {
		pcidb_vendor_t *vend = pcidb_lookup_vendor(pcidb,
		    oarg.psdo_vid);
		if (vend != NULL) {
			oarg.psdo_vendor = pcidb_vendor_name(vend);
		} else {
			(void) snprintf(venstr, sizeof (venstr),
			    "Unknown vendor: 0x%x", oarg.psdo_vid);
			oarg.psdo_vendor = venstr;
		}
	}

	oarg.psdo_device = "--";
	if (oarg.psdo_vid != -1 && oarg.psdo_did != -1) {
		pcidb_device_t *dev = pcidb_lookup_device(pcidb,
		    oarg.psdo_vid, oarg.psdo_did);
		if (dev != NULL) {
			oarg.psdo_device = pcidb_device_name(dev);
		} else {
			(void) snprintf(devstr, sizeof (devstr),
			    "Unknown device: 0x%x", oarg.psdo_did);
			oarg.psdo_device = devstr;
		}
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
	    "pcie-link-maximum-width", &mwidth);
	if (nprop != 1) {
		oarg.psdo_mwidth = -1;
	} else {
		oarg.psdo_mwidth = *mwidth;
	}

	nprop = di_prop_lookup_ints(DDI_DEV_T_ANY, node,
	    "pcie-link-current-width", &cwidth);
	if (nprop != 1) {
		oarg.psdo_cwidth = -1;
	} else {
		oarg.psdo_cwidth = *cwidth;
	}

	nprop = di_prop_lookup_int64(DDI_DEV_T_ANY, node,
	    "pcie-link-maximum-speed", &mspeed);
	if (nprop != 1) {
		oarg.psdo_mspeed = -1;
	} else {
		oarg.psdo_mspeed = *mspeed;
	}

	nprop = di_prop_lookup_int64(DDI_DEV_T_ANY, node,
	    "pcie-link-current-speed", &cspeed);
	if (nprop != 1) {
		oarg.psdo_cspeed = -1;
	} else {
		oarg.psdo_cspeed = *cspeed;
	}

	nprop = di_prop_lookup_int64(DDI_DEV_T_ANY, node,
	    "pcie-link-supported-speeds", &sspeeds);
	if (nprop > 0) {
		oarg.psdo_nspeeds = nprop;
		oarg.psdo_sspeeds = sspeeds;
	} else {
		oarg.psdo_nspeeds = 0;
		oarg.psdo_sspeeds = NULL;
	}

	if (pcieadm_show_devs_match(psd, &oarg)) {
		ofmt_print(psd->psd_ofmt, &oarg);
		psd->psd_nprint++;
	}

done:
	if (path != NULL) {
		di_devfs_path_free(path);
	}

	return (ret);
}

void
pcieadm_show_devs_usage(FILE *f)
{
	(void) fprintf(f, "\tshow-devs\t[-F] [-H] [-s | -o field[,...] [-p]] "
	    "[filter...]\n");
}

static void
pcieadm_show_devs_help(const char *fmt, ...)
{
	if (fmt != NULL) {
		va_list ap;

		va_start(ap, fmt);
		vwarnx(fmt, ap);
		va_end(ap);
		(void) fprintf(stderr, "\n");
	}

	(void) fprintf(stderr, "Usage:  %s show-devs [-F] [-H] [-s | -o "
	    "field[,...] [-p]] [filter...]\n", pcieadm_progname);

	(void) fprintf(stderr, "\nList PCI devices and functions in the "
	    "system. Each <filter> selects a set\nof devices to show and "
	    "can be a driver name, instance, /devices path, or\nb/d/f.\n\n"
	    "\t-F\t\tdo not display PCI functions\n"
	    "\t-H\t\tomit the column header\n"
	    "\t-o field\toutput fields to print\n"
	    "\t-p\t\tparsable output (requires -o)\n"
	    "\t-s\t\tlist speeds and widths\n");

}

int
pcieadm_show_devs(pcieadm_t *pcip, int argc, char *argv[])
{
	int c, ret;
	uint_t flags = 0;
	const char *fields = NULL;
	pcieadm_show_devs_t psd;
	pcieadm_di_walk_t walk;
	ofmt_status_t oferr;
	boolean_t parse = B_FALSE;
	boolean_t speeds = B_FALSE;

	/*
	 * show-devs relies solely on the devinfo snapshot we already took.
	 * Formalize our privs immediately.
	 */
	pcieadm_init_privs(pcip);

	bzero(&psd, sizeof (psd));
	psd.psd_pia = pcip;
	psd.psd_funcs = B_TRUE;

	while ((c = getopt(argc, argv, ":FHo:ps")) != -1) {
		switch (c) {
		case 'F':
			psd.psd_funcs = B_FALSE;
			break;
		case 'p':
			parse = B_TRUE;
			flags |= OFMT_PARSABLE;
			break;
		case 'H':
			flags |= OFMT_NOHEADER;
			break;
		case 's':
			speeds = B_TRUE;
			break;
		case 'o':
			fields = optarg;
			break;
		case ':':
			pcieadm_show_devs_help("option -%c requires an "
			    "argument", optopt);
			exit(EXIT_USAGE);
		case '?':
			pcieadm_show_devs_help("unknown option: -%c", optopt);
			exit(EXIT_USAGE);
		}
	}

	if (parse && fields == NULL) {
		errx(EXIT_USAGE, "-p requires fields specified with -o");
	}

	if (fields != NULL && speeds) {
		errx(EXIT_USAGE, "-s cannot be used with with -o");
	}

	if (fields == NULL) {
		if (speeds) {
			fields = pcieadm_show_dev_speeds;
		} else {
			fields = pcieadm_show_dev_fields;
		}
	}

	argc -= optind;
	argv += optind;

	if (argc > 0) {
		psd.psd_nfilts = argc;
		psd.psd_filts = argv;
		psd.psd_used = calloc(argc, sizeof (boolean_t));
		if (psd.psd_used == NULL) {
			err(EXIT_FAILURE, "failed to allocate filter tracking "
			    "memory");
		}
	}

	oferr = ofmt_open(fields, pcieadm_show_dev_ofmt, flags, 0,
	    &psd.psd_ofmt);
	ofmt_check(oferr, parse, psd.psd_ofmt, pcieadm_ofmt_errx, warnx);

	walk.pdw_arg = &psd;
	walk.pdw_func = pcieadm_show_devs_walk_cb;

	pcieadm_di_walk(pcip, &walk);

	ret = EXIT_SUCCESS;
	for (int i = 0; i < psd.psd_nfilts; i++) {
		if (!psd.psd_used[i]) {
			warnx("filter '%s' did not match any devices",
			    psd.psd_filts[i]);
			ret = EXIT_FAILURE;
		}
	}

	if (psd.psd_nprint == 0) {
		ret = EXIT_FAILURE;
	}

	return (ret);
}