1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2012-2013 Intel Corporation
5 * All rights reserved.
6 * Copyright (C) 2018-2019 Alexander Motin <mav@FreeBSD.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <sys/param.h>
31
32#include <ctype.h>
33#include <err.h>
34#include <fcntl.h>
35#include <stddef.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
41#include "nvmecontrol.h"
42#include "nvmecontrol_ext.h"
43
44void
45nvme_print_controller(struct nvme_controller_data *cdata)
46{
47	uint8_t str[128];
48	char cbuf[UINT128_DIG + 1];
49	uint16_t oncs, oacs;
50	uint8_t compare, write_unc, dsm, t;
51	uint8_t security, fmt, fw, nsmgmt;
52	uint8_t	fw_slot1_ro, fw_num_slots;
53	uint8_t ns_smart;
54	uint8_t sqes_max, sqes_min;
55	uint8_t cqes_max, cqes_min;
56	uint8_t fwug;
57
58	oncs = cdata->oncs;
59	compare = NVMEV(NVME_CTRLR_DATA_ONCS_COMPARE, oncs);
60	write_unc = NVMEV(NVME_CTRLR_DATA_ONCS_WRITE_UNC, oncs);
61	dsm = NVMEV(NVME_CTRLR_DATA_ONCS_DSM, oncs);
62
63	oacs = cdata->oacs;
64	security = NVMEV(NVME_CTRLR_DATA_OACS_SECURITY, oacs);
65	fmt = NVMEV(NVME_CTRLR_DATA_OACS_FORMAT, oacs);
66	fw = NVMEV(NVME_CTRLR_DATA_OACS_FIRMWARE, oacs);
67	nsmgmt = NVMEV(NVME_CTRLR_DATA_OACS_NSMGMT, oacs);
68
69	fw_num_slots = NVMEV(NVME_CTRLR_DATA_FRMW_NUM_SLOTS, cdata->frmw);
70	fw_slot1_ro = NVMEV(NVME_CTRLR_DATA_FRMW_SLOT1_RO, cdata->frmw);
71	fwug = cdata->fwug;
72
73	ns_smart = NVMEV(NVME_CTRLR_DATA_LPA_NS_SMART, cdata->lpa);
74
75	sqes_min = NVMEV(NVME_CTRLR_DATA_SQES_MIN, cdata->sqes);
76	sqes_max = NVMEV(NVME_CTRLR_DATA_SQES_MAX, cdata->sqes);
77
78	cqes_min = NVMEV(NVME_CTRLR_DATA_CQES_MIN, cdata->cqes);
79	cqes_max = NVMEV(NVME_CTRLR_DATA_CQES_MAX, cdata->cqes);
80
81	printf("Controller Capabilities/Features\n");
82	printf("================================\n");
83	printf("Vendor ID:                   %04x\n", cdata->vid);
84	printf("Subsystem Vendor ID:         %04x\n", cdata->ssvid);
85	nvme_strvis(str, cdata->sn, sizeof(str), NVME_SERIAL_NUMBER_LENGTH);
86	printf("Serial Number:               %s\n", str);
87	nvme_strvis(str, cdata->mn, sizeof(str), NVME_MODEL_NUMBER_LENGTH);
88	printf("Model Number:                %s\n", str);
89	nvme_strvis(str, cdata->fr, sizeof(str), NVME_FIRMWARE_REVISION_LENGTH);
90	printf("Firmware Version:            %s\n", str);
91	printf("Recommended Arb Burst:       %d\n", cdata->rab);
92	printf("IEEE OUI Identifier:         %02x %02x %02x\n",
93		cdata->ieee[2], cdata->ieee[1], cdata->ieee[0]);
94	printf("Multi-Path I/O Capabilities: %s%s%s%s%s\n",
95	    (cdata->mic == 0) ? "Not Supported" : "",
96	    NVMEV(NVME_CTRLR_DATA_MIC_ANAR, cdata->mic) != 0 ?
97	    "Asymmetric, " : "",
98	    NVMEV(NVME_CTRLR_DATA_MIC_SRIOVVF, cdata->mic) != 0 ?
99	    "SR-IOV VF, " : "",
100	    NVMEV(NVME_CTRLR_DATA_MIC_MCTRLRS, cdata->mic) != 0 ?
101	    "Multiple controllers, " : "",
102	    NVMEV(NVME_CTRLR_DATA_MIC_MPORTS, cdata->mic) != 0 ?
103	    "Multiple ports" : "");
104	/* TODO: Use CAP.MPSMIN to determine true memory page size. */
105	printf("Max Data Transfer Size:      ");
106	if (cdata->mdts == 0)
107		printf("Unlimited\n");
108	else
109		printf("%ld bytes\n", PAGE_SIZE * (1L << cdata->mdts));
110	printf("Sanitize Crypto Erase:       %s\n",
111	    NVMEV(NVME_CTRLR_DATA_SANICAP_CES, cdata->sanicap) != 0 ?
112	    "Supported" : "Not Supported");
113	printf("Sanitize Block Erase:        %s\n",
114	    NVMEV(NVME_CTRLR_DATA_SANICAP_BES, cdata->sanicap) != 0 ?
115	    "Supported" : "Not Supported");
116	printf("Sanitize Overwrite:          %s\n",
117	    NVMEV(NVME_CTRLR_DATA_SANICAP_OWS, cdata->sanicap) != 0 ?
118	    "Supported" : "Not Supported");
119	printf("Sanitize NDI:                %s\n",
120	    NVMEV(NVME_CTRLR_DATA_SANICAP_NDI, cdata->sanicap) != 0 ?
121	    "Supported" : "Not Supported");
122	printf("Sanitize NODMMAS:            ");
123	switch (NVMEV(NVME_CTRLR_DATA_SANICAP_NODMMAS, cdata->sanicap)) {
124	case NVME_CTRLR_DATA_SANICAP_NODMMAS_UNDEF:
125		printf("Undefined\n");
126		break;
127	case NVME_CTRLR_DATA_SANICAP_NODMMAS_NO:
128		printf("No\n");
129		break;
130	case NVME_CTRLR_DATA_SANICAP_NODMMAS_YES:
131		printf("Yes\n");
132		break;
133	default:
134		printf("Unknown\n");
135		break;
136	}
137	printf("Controller ID:               0x%04x\n", cdata->ctrlr_id);
138	printf("Version:                     %d.%d.%d\n",
139	    (cdata->ver >> 16) & 0xffff, (cdata->ver >> 8) & 0xff,
140	    cdata->ver & 0xff);
141	printf("Traffic Based Keep Alive:    %sSupported\n",
142	    NVMEV(NVME_CTRLR_DATA_CTRATT_TBKAS, cdata->ctratt) ? "" : "Not ");
143	printf("Controller Type:             ");
144	switch (cdata->cntrltype) {
145	case 0:
146		printf("Not Reported\n");
147		break;
148	case 1:
149		printf("I/O Controller\n");
150		break;
151	case 2:
152		printf("Discovery Controller\n");
153		break;
154	case 3:
155		printf("Administrative Controller\n");
156		break;
157	default:
158		printf("%d (Reserved)\n", cdata->cntrltype);
159		break;
160	}
161	printf("Keep Alive Timer             ");
162	if (cdata->kas == 0)
163		printf("Not Supported\n");
164	else
165		printf("%u ms granularity\n", cdata->kas * 100);
166	printf("Maximum Outstanding Commands ");
167	if (cdata->maxcmd == 0)
168		printf("Not Specified\n");
169	else
170		printf("%u\n", cdata->maxcmd);
171	printf("\n");
172
173	printf("Admin Command Set Attributes\n");
174	printf("============================\n");
175	printf("Security Send/Receive:       %s\n",
176		security ? "Supported" : "Not Supported");
177	printf("Format NVM:                  %s\n",
178		fmt ? "Supported" : "Not Supported");
179	printf("Firmware Activate/Download:  %s\n",
180		fw ? "Supported" : "Not Supported");
181	printf("Namespace Management:        %s\n",
182		nsmgmt ? "Supported" : "Not Supported");
183	printf("Device Self-test:            %sSupported\n",
184	    NVMEV(NVME_CTRLR_DATA_OACS_SELFTEST, oacs) != 0 ? "" : "Not ");
185	printf("Directives:                  %sSupported\n",
186	    NVMEV(NVME_CTRLR_DATA_OACS_DIRECTIVES, oacs) != 0 ? "" : "Not ");
187	printf("NVMe-MI Send/Receive:        %sSupported\n",
188	    NVMEV(NVME_CTRLR_DATA_OACS_NVMEMI, oacs) != 0 ? "" : "Not ");
189	printf("Virtualization Management:   %sSupported\n",
190	    NVMEV(NVME_CTRLR_DATA_OACS_VM, oacs) != 0 ? "" : "Not ");
191	printf("Doorbell Buffer Config:      %sSupported\n",
192	    NVMEV(NVME_CTRLR_DATA_OACS_DBBUFFER, oacs) != 0 ? "" : "Not ");
193	printf("Get LBA Status:              %sSupported\n",
194	    NVMEV(NVME_CTRLR_DATA_OACS_GETLBA, oacs) != 0 ? "" : "Not ");
195	printf("Sanitize:                    ");
196	if (cdata->sanicap != 0) {
197		printf("%s%s%s\n",
198		    NVMEV(NVME_CTRLR_DATA_SANICAP_CES, cdata->sanicap) != 0 ?
199		    "crypto, " : "",
200		    NVMEV(NVME_CTRLR_DATA_SANICAP_BES, cdata->sanicap) != 0 ?
201		    "block, " : "",
202		    NVMEV(NVME_CTRLR_DATA_SANICAP_OWS, cdata->sanicap) != 0 ?
203		    "overwrite" : "");
204	} else {
205		printf("Not Supported\n");
206	}
207	printf("Abort Command Limit:         %d\n", cdata->acl+1);
208	printf("Async Event Request Limit:   %d\n", cdata->aerl+1);
209	printf("Number of Firmware Slots:    %d\n", fw_num_slots);
210	printf("Firmware Slot 1 Read-Only:   %s\n", fw_slot1_ro ? "Yes" : "No");
211	printf("Per-Namespace SMART Log:     %s\n",
212		ns_smart ? "Yes" : "No");
213	printf("Error Log Page Entries:      %d\n", cdata->elpe+1);
214	printf("Number of Power States:      %d\n", cdata->npss+1);
215	if (cdata->ver >= 0x010200) {
216		printf("Total NVM Capacity:          %s bytes\n",
217		    uint128_to_str(to128(cdata->untncap.tnvmcap),
218		    cbuf, sizeof(cbuf)));
219		printf("Unallocated NVM Capacity:    %s bytes\n",
220		    uint128_to_str(to128(cdata->untncap.unvmcap),
221		    cbuf, sizeof(cbuf)));
222	}
223	printf("Firmware Update Granularity: %02x ", fwug);
224	if (fwug == 0)
225		printf("(Not Reported)\n");
226	else if (fwug == 0xFF)
227		printf("(No Granularity)\n");
228	else
229		printf("(%d bytes)\n", ((uint32_t)fwug << 12));
230	printf("Host Buffer Preferred Size:  %llu bytes\n",
231	    (long long unsigned)cdata->hmpre * 4096);
232	printf("Host Buffer Minimum Size:    %llu bytes\n",
233	    (long long unsigned)cdata->hmmin * 4096);
234
235	printf("\n");
236	printf("NVM Command Set Attributes\n");
237	printf("==========================\n");
238	printf("Submission Queue Entry Size\n");
239	printf("  Max:                       %d\n", 1 << sqes_max);
240	printf("  Min:                       %d\n", 1 << sqes_min);
241	printf("Completion Queue Entry Size\n");
242	printf("  Max:                       %d\n", 1 << cqes_max);
243	printf("  Min:                       %d\n", 1 << cqes_min);
244	printf("Number of Namespaces:        %d\n", cdata->nn);
245	printf("Compare Command:             %s\n",
246		compare ? "Supported" : "Not Supported");
247	printf("Write Uncorrectable Command: %s\n",
248		write_unc ? "Supported" : "Not Supported");
249	printf("Dataset Management Command:  %s\n",
250		dsm ? "Supported" : "Not Supported");
251	printf("Write Zeroes Command:        %sSupported\n",
252	    NVMEV(NVME_CTRLR_DATA_ONCS_WRZERO, oncs) != 0 ? "" : "Not ");
253	printf("Save Features:               %sSupported\n",
254	    NVMEV(NVME_CTRLR_DATA_ONCS_SAVEFEAT, oncs) != 0 ? "" : "Not ");
255	printf("Reservations:                %sSupported\n",
256	    NVMEV(NVME_CTRLR_DATA_ONCS_RESERV, oncs) != 0 ? "" : "Not ");
257	printf("Timestamp feature:           %sSupported\n",
258	    NVMEV(NVME_CTRLR_DATA_ONCS_TIMESTAMP, oncs) != 0 ? "" : "Not ");
259	printf("Verify feature:              %sSupported\n",
260	    NVMEV(NVME_CTRLR_DATA_ONCS_VERIFY, oncs) != 0 ? "" : "Not ");
261	printf("Fused Operation Support:     %s%s\n",
262	    (cdata->fuses == 0) ? "Not Supported" : "",
263	    NVMEV(NVME_CTRLR_DATA_FUSES_CNW, cdata->fuses) != 0 ?
264	    "Compare and Write" : "");
265	printf("Format NVM Attributes:       %s%s Erase, %s Format\n",
266	    NVMEV(NVME_CTRLR_DATA_FNA_CRYPTO_ERASE, cdata->fna) != 0 ?
267	    "Crypto Erase, " : "",
268	    NVMEV(NVME_CTRLR_DATA_FNA_ERASE_ALL, cdata->fna) != 0 ?
269	    "All-NVM" : "Per-NS",
270	    NVMEV(NVME_CTRLR_DATA_FNA_FORMAT_ALL, cdata->fna) != 0 ?
271	    "All-NVM" : "Per-NS");
272	t = NVMEV(NVME_CTRLR_DATA_VWC_ALL, cdata->vwc);
273	printf("Volatile Write Cache:        %s%s\n",
274	    NVMEV(NVME_CTRLR_DATA_VWC_PRESENT, cdata->vwc) != 0 ?
275	    "Present" : "Not Present",
276	    (t == NVME_CTRLR_DATA_VWC_ALL_NO) ? ", no flush all" :
277	    (t == NVME_CTRLR_DATA_VWC_ALL_YES) ? ", flush all" : "");
278
279	if (cdata->ver >= 0x010201)
280		printf("\nNVM Subsystem Name:          %.256s\n", cdata->subnqn);
281
282	if (cdata->ioccsz != 0) {
283		printf("\n");
284		printf("Fabrics Attributes\n");
285		printf("==================\n");
286		printf("I/O Command Capsule Size:    %d bytes\n",
287		    cdata->ioccsz * 16);
288		printf("I/O Response Capsule Size:   %d bytes\n",
289		    cdata->iorcsz * 16);
290		printf("In Capsule Data Offset:      %d bytes\n",
291		    cdata->icdoff * 16);
292		printf("Controller Model:            %s\n",
293		    (cdata->fcatt & 1) == 0 ? "Dynamic" : "Static");
294		printf("Max SGL Descriptors:         ");
295		if (cdata->msdbd == 0)
296			printf("Unlimited\n");
297		else
298			printf("%d\n", cdata->msdbd);
299		printf("Disconnect of I/O Queues:    %sSupported\n",
300		    (cdata->ofcs & 1) == 1 ? "" : "Not ");
301	}
302}
303