xref: /openbmc/u-boot/drivers/nvme/nvme_show.c (revision d9b23e26)
1 /*
2  * Copyright (C) 2017 NXP Semiconductors
3  * Copyright (C) 2017 Bin Meng <bmeng.cn@gmail.com>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <memalign.h>
12 #include <nvme.h>
13 #include "nvme.h"
14 
15 static void print_optional_admin_cmd(u16 oacs, int devnum)
16 {
17 	printf("Blk device %d: Optional Admin Command Support:\n",
18 	       devnum);
19 	printf("\tNamespace Management/Attachment: %s\n",
20 	       oacs & 0x08 ? "yes" : "no");
21 	printf("\tFirmware Commit/Image download: %s\n",
22 	       oacs & 0x04 ? "yes" : "no");
23 	printf("\tFormat NVM: %s\n",
24 	       oacs & 0x02 ? "yes" : "no");
25 	printf("\tSecurity Send/Receive: %s\n",
26 	       oacs & 0x01 ? "yes" : "no");
27 }
28 
29 static void print_optional_nvm_cmd(u16 oncs, int devnum)
30 {
31 	printf("Blk device %d: Optional NVM Command Support:\n",
32 	       devnum);
33 	printf("\tReservation: %s\n",
34 	       oncs & 0x10 ? "yes" : "no");
35 	printf("\tSave/Select field in the Set/Get features: %s\n",
36 	       oncs & 0x08 ? "yes" : "no");
37 	printf("\tWrite Zeroes: %s\n",
38 	       oncs & 0x04 ? "yes" : "no");
39 	printf("\tDataset Management: %s\n",
40 	       oncs & 0x02 ? "yes" : "no");
41 	printf("\tWrite Uncorrectable: %s\n",
42 	       oncs & 0x01 ? "yes" : "no");
43 }
44 
45 static void print_format_nvme_attributes(u8 fna, int devnum)
46 {
47 	printf("Blk device %d: Format NVM Attributes:\n", devnum);
48 	printf("\tSupport Cryptographic Erase: %s\n",
49 	       fna & 0x04 ? "yes" : "No");
50 	printf("\tSupport erase a particular namespace: %s\n",
51 	       fna & 0x02 ? "No" : "Yes");
52 	printf("\tSupport format a particular namespace: %s\n",
53 	       fna & 0x01 ? "No" : "Yes");
54 }
55 
56 static void print_format(struct nvme_lbaf *lbaf)
57 {
58 	u8 str[][10] = {"Best", "Better", "Good", "Degraded"};
59 
60 	printf("\t\tMetadata Size: %d\n", le16_to_cpu(lbaf->ms));
61 	printf("\t\tLBA Data Size: %d\n", 1 << lbaf->ds);
62 	printf("\t\tRelative Performance: %s\n", str[lbaf->rp & 0x03]);
63 }
64 
65 static void print_formats(struct nvme_id_ns *id, struct nvme_ns *ns)
66 {
67 	int i;
68 
69 	printf("Blk device %d: LBA Format Support:\n", ns->devnum);
70 
71 	for (i = 0; i < id->nlbaf; i++) {
72 		printf("\tLBA Foramt %d Support: ", i);
73 		if (i == ns->flbas)
74 			printf("(current)\n");
75 		else
76 			printf("\n");
77 		print_format(id->lbaf + i);
78 	}
79 }
80 
81 static void print_data_protect_cap(u8 dpc, int devnum)
82 {
83 	printf("Blk device %d: End-to-End Data", devnum);
84 	printf("Protect Capabilities:\n");
85 	printf("\tAs last eight bytes: %s\n",
86 	       dpc & 0x10 ? "yes" : "No");
87 	printf("\tAs first eight bytes: %s\n",
88 	       dpc & 0x08 ? "yes" : "No");
89 	printf("\tSupport Type3: %s\n",
90 	       dpc & 0x04 ? "yes" : "No");
91 	printf("\tSupport Type2: %s\n",
92 	       dpc & 0x02 ? "yes" : "No");
93 	printf("\tSupport Type1: %s\n",
94 	       dpc & 0x01 ? "yes" : "No");
95 }
96 
97 static void print_metadata_cap(u8 mc, int devnum)
98 {
99 	printf("Blk device %d: Metadata capabilities:\n", devnum);
100 	printf("\tAs part of a separate buffer: %s\n",
101 	       mc & 0x02 ? "yes" : "No");
102 	printf("\tAs part of an extended data LBA: %s\n",
103 	       mc & 0x01 ? "yes" : "No");
104 }
105 
106 int nvme_print_info(struct udevice *udev)
107 {
108 	struct nvme_ns *ns = dev_get_priv(udev);
109 	struct nvme_dev *dev = ns->dev;
110 	ALLOC_CACHE_ALIGN_BUFFER(char, buf_ns, sizeof(struct nvme_id_ns));
111 	struct nvme_id_ns *id = (struct nvme_id_ns *)buf_ns;
112 	ALLOC_CACHE_ALIGN_BUFFER(char, buf_ctrl, sizeof(struct nvme_id_ctrl));
113 	struct nvme_id_ctrl *ctrl = (struct nvme_id_ctrl *)buf_ctrl;
114 
115 	if (nvme_identify(dev, 0, 1, (dma_addr_t)ctrl))
116 		return -EIO;
117 
118 	print_optional_admin_cmd(le16_to_cpu(ctrl->oacs), ns->devnum);
119 	print_optional_nvm_cmd(le16_to_cpu(ctrl->oncs), ns->devnum);
120 	print_format_nvme_attributes(ctrl->fna, ns->devnum);
121 
122 	if (nvme_identify(dev, ns->ns_id, 0, (dma_addr_t)id))
123 		return -EIO;
124 
125 	print_formats(id, ns);
126 	print_data_protect_cap(id->dpc, ns->devnum);
127 	print_metadata_cap(id->mc, ns->devnum);
128 
129 	return 0;
130 }
131