Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright (c) Samsung Electronics Co., Ltd.
3 : : * All rights reserved.
4 : : */
5 : :
6 : : #include "spdk/stdinc.h"
7 : : #include "spdk/nvme.h"
8 : : #include "spdk/util.h"
9 : : #include "spdk/env.h"
10 : :
11 : : struct ctrlr {
12 : : struct spdk_nvme_transport_id trid;
13 : : struct spdk_nvme_ctrlr *ctrlr;
14 : : char *write_buf;
15 : : char *read_buf;
16 : : int write_completed;
17 : : };
18 : :
19 : : static struct ctrlr g_ctrlr;
20 : :
21 : : static void cleanup(void);
22 : :
23 : : static void
24 : 0 : fill_pattern(char *buf, size_t num_bytes, char pattern)
25 : : {
26 : : size_t i;
27 : :
28 [ # # ]: 0 : for (i = 0; i < num_bytes; i++) {
29 : 0 : buf[i] = pattern;
30 : : }
31 : 0 : }
32 : :
33 : : static void
34 : 0 : write_complete(void *arg, const struct spdk_nvme_cpl *completion)
35 : : {
36 : 0 : printf("Boot Partition Write - SCT : %d, SC : %d\n",
37 [ # # ]: 0 : completion->status.sct, completion->status.sc);
38 : 0 : g_ctrlr.write_completed = 1;
39 : 0 : }
40 : :
41 : : static int
42 : 0 : boot_partition_test(void)
43 : : {
44 : : struct spdk_nvme_ctrlr *ctrlr;
45 : : union spdk_nvme_cap_register cap;
46 : : int rc;
47 : : union spdk_nvme_bpinfo_register bpinfo;
48 : : unsigned int bpsize;
49 : : unsigned int bpsize_in_4k;
50 : :
51 : 0 : ctrlr = g_ctrlr.ctrlr;
52 : :
53 : 0 : cap = spdk_nvme_ctrlr_get_regs_cap(ctrlr);
54 : :
55 [ # # ]: 0 : if (cap.bits.bps) {
56 : 0 : printf("Boot Partitions are Supported by the Controller\n");
57 : : } else {
58 : 0 : printf("Boot Partitions are Not Supported by the Controller\n");
59 : 0 : return -ENOTSUP;
60 : : }
61 : :
62 : 0 : bpinfo = spdk_nvme_ctrlr_get_regs_bpinfo(ctrlr);
63 : 0 : bpsize = bpinfo.bits.bpsz * 131072;
64 : 0 : bpsize_in_4k = bpsize / 4096;
65 : :
66 : 0 : printf("Boot Partition Info\n");
67 : 0 : printf("Active Boot Partition ID : %d\n", bpinfo.bits.abpid);
68 : 0 : printf("Boot Read Status : %d\n", bpinfo.bits.brs);
69 : 0 : printf("Boot Partition Size : %d bytes\n", bpsize);
70 : :
71 : 0 : g_ctrlr.write_buf = spdk_zmalloc(bpsize, 0x1000, NULL,
72 : : SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
73 : :
74 [ # # ]: 0 : if (g_ctrlr.write_buf == NULL) {
75 : 0 : printf("Error - could not allocate write buffer for test\n");
76 : 0 : cleanup();
77 : 0 : return -ENOMEM;
78 : : }
79 : :
80 : 0 : g_ctrlr.read_buf = spdk_memzone_reserve("boot_partition", bpsize,
81 : : SPDK_ENV_SOCKET_ID_ANY, 0);
82 : :
83 [ # # ]: 0 : if (g_ctrlr.read_buf == NULL) {
84 : 0 : printf("Error - could not allocate read buffer for test\n");
85 : 0 : cleanup();
86 : 0 : return -ENOMEM;
87 : : }
88 : :
89 : 0 : fill_pattern(g_ctrlr.write_buf, bpsize, 0xDE);
90 : :
91 : 0 : g_ctrlr.write_completed = 0;
92 : 0 : rc = spdk_nvme_ctrlr_write_boot_partition(ctrlr, g_ctrlr.write_buf,
93 : : bpsize, 0, write_complete, NULL);
94 [ # # ]: 0 : if (rc) {
95 : 0 : printf("Error - Boot Partition write failure. rc: %d", rc);
96 : 0 : cleanup();
97 : 0 : return rc;
98 : : }
99 : :
100 [ # # ]: 0 : while (!g_ctrlr.write_completed) {
101 : 0 : spdk_nvme_ctrlr_process_admin_completions(ctrlr);
102 : : }
103 : :
104 : 0 : rc = spdk_nvme_ctrlr_read_boot_partition_start(ctrlr, g_ctrlr.read_buf,
105 : : bpsize_in_4k, 0, 0);
106 : :
107 [ # # ]: 0 : if (rc) {
108 : 0 : printf("Error - Boot Partition read start failure. rc: %d", rc);
109 : 0 : cleanup();
110 : 0 : return rc;
111 : : }
112 : :
113 : : do {
114 : 0 : rc = spdk_nvme_ctrlr_read_boot_partition_poll(ctrlr);
115 [ # # ]: 0 : } while (rc == -EAGAIN);
116 : :
117 [ # # ]: 0 : if (rc != 0) {
118 : 0 : printf("Error - Boot Partition read poll failure. rc: %d", rc);
119 : 0 : cleanup();
120 : 0 : return rc;
121 : : }
122 : :
123 [ # # # # ]: 0 : rc = memcmp(g_ctrlr.write_buf, g_ctrlr.read_buf, bpsize);
124 [ # # ]: 0 : if (rc) {
125 : 0 : printf("Error - Boot Partition written data does not match Boot Partition read data, rc: %d\n", rc);
126 : 0 : cleanup();
127 : 0 : return rc;
128 : : }
129 : :
130 : 0 : printf("Boot Partition 0 written data matches Boot Partition 0 read data\n");
131 : :
132 : 0 : fill_pattern(g_ctrlr.write_buf, bpsize, 0xAD);
133 : :
134 : 0 : g_ctrlr.write_completed = 0;
135 : 0 : rc = spdk_nvme_ctrlr_write_boot_partition(ctrlr, g_ctrlr.write_buf,
136 : : bpsize, 1, write_complete, NULL);
137 [ # # ]: 0 : if (rc) {
138 : 0 : printf("Error - Boot Partition write failure. rc: %d", rc);
139 : 0 : cleanup();
140 : 0 : return rc;
141 : : }
142 : :
143 [ # # ]: 0 : while (!g_ctrlr.write_completed) {
144 : 0 : spdk_nvme_ctrlr_process_admin_completions(ctrlr);
145 : : }
146 : :
147 : 0 : rc = spdk_nvme_ctrlr_read_boot_partition_start(ctrlr, g_ctrlr.read_buf,
148 : : bpsize_in_4k, 0, 1);
149 : :
150 [ # # ]: 0 : if (rc) {
151 : 0 : printf("Error - Boot Partition read start failure. rc: %d", rc);
152 : 0 : cleanup();
153 : 0 : return rc;
154 : : }
155 : :
156 : : do {
157 : 0 : rc = spdk_nvme_ctrlr_read_boot_partition_poll(ctrlr);
158 [ # # ]: 0 : } while (rc == -EAGAIN);
159 : :
160 [ # # ]: 0 : if (rc != 0) {
161 : 0 : printf("Error - Boot Partition read poll failure. rc: %d", rc);
162 : 0 : cleanup();
163 : 0 : return rc;
164 : : }
165 : :
166 [ # # # # ]: 0 : rc = memcmp(g_ctrlr.write_buf, g_ctrlr.read_buf, bpsize);
167 [ # # ]: 0 : if (rc) {
168 : 0 : printf("Error - Boot Partition written data does not match Boot Partition read data, rc: %d\n", rc);
169 : 0 : cleanup();
170 : 0 : return rc;
171 : : }
172 : :
173 : 0 : printf("Boot Partition 1 written data matches Boot Partition 1 read data\n");
174 : :
175 : 0 : cleanup();
176 : :
177 : 0 : return 0;
178 : : }
179 : :
180 : : static void
181 : 0 : cleanup(void)
182 : : {
183 : 0 : spdk_memzone_free("boot_partition");
184 : 0 : spdk_free(g_ctrlr.write_buf);
185 : 0 : spdk_nvme_detach(g_ctrlr.ctrlr);
186 : 0 : }
187 : :
188 : : static void
189 : 0 : usage(char *program_name)
190 : : {
191 [ # # ]: 0 : printf("%s Option (Mandatory)", program_name);
192 : 0 : printf("\n");
193 [ # # ]: 0 : printf("\t[-p PCIe address of the NVMe Device with Boot Partition support]\n");
194 : 0 : printf("\n");
195 : 0 : }
196 : :
197 : : static int
198 : 0 : parse_args(int argc, char **argv)
199 : : {
200 : : int op;
201 : 0 : unsigned num_args = 0;
202 : :
203 [ # # # # : 0 : while ((op = getopt(argc, argv, "p:")) != -1) {
# # ]
204 [ # # ]: 0 : switch (op) {
205 : 0 : case 'p':
206 [ # # ]: 0 : snprintf(&g_ctrlr.trid.traddr[0], SPDK_NVMF_TRADDR_MAX_LEN + 1,
207 : : "%s", optarg);
208 : :
209 : 0 : g_ctrlr.trid.trtype = SPDK_NVME_TRANSPORT_PCIE;
210 : :
211 : 0 : spdk_nvme_transport_id_populate_trstring(&g_ctrlr.trid,
212 : : spdk_nvme_transport_id_trtype_str(g_ctrlr.trid.trtype));
213 : :
214 : 0 : num_args++;
215 : 0 : break;
216 : 0 : default:
217 : 0 : usage(argv[0]);
218 : 0 : return 1;
219 : : }
220 : : }
221 : :
222 [ # # ]: 0 : if (num_args != 1) {
223 : 0 : usage(argv[0]);
224 : 0 : return 1;
225 : : }
226 : :
227 : 0 : return 0;
228 : : }
229 : :
230 : : int
231 : 0 : main(int argc, char **argv)
232 : : {
233 : : int rc;
234 : 0 : struct spdk_env_opts opts;
235 : :
236 : : /*
237 : : * Parse the input arguments. For now we use the following
238 : : * format list:
239 : : *
240 : : * -p <pci id>
241 : : *
242 : : */
243 : 0 : rc = parse_args(argc, argv);
244 [ # # ]: 0 : if (rc) {
245 [ # # # # ]: 0 : fprintf(stderr, "Error in parse_args(): %d\n", rc);
246 : 0 : return rc;
247 : : }
248 : :
249 : 0 : spdk_env_opts_init(&opts);
250 : 0 : opts.name = "boot_partition";
251 : 0 : opts.shm_id = 0;
252 [ # # ]: 0 : if (spdk_env_init(&opts) < 0) {
253 [ # # # # ]: 0 : fprintf(stderr, "Unable to initialize SPDK env\n");
254 : 0 : return 1;
255 : : }
256 : :
257 [ # # ]: 0 : printf("Initializing NVMe Controller\n");
258 : :
259 : 0 : g_ctrlr.ctrlr = spdk_nvme_connect(&g_ctrlr.trid, NULL, 0);
260 [ # # ]: 0 : if (!g_ctrlr.ctrlr) {
261 [ # # # # ]: 0 : fprintf(stderr, "spdk_nvme_connect() failed\n");
262 : 0 : return 1;
263 : : }
264 : :
265 [ # # ]: 0 : printf("Initialization complete.\n");
266 : 0 : rc = boot_partition_test();
267 : 0 : return rc;
268 : : }
|