Debian Codegraph @

test/test.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2020 Western Digital Corporation or its affiliates.
5 *
6 * Authors: Keith Busch <[email protected]>
7 */
8
9 /**
10 * Basic libnvme test: uses scan filters, single controllers, and many admin
11 * command APIs for identifications, logs, and features. No verification for
12 * specific values are performed: the test will only report which commands
13 * executed were completed successfully or with an error. User inspection of
14 * the output woould be required to know if everything is working when the
15 * program exists successfully; an ungraceful exit means a bug exists
16 * somewhere.
17 */
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdbool.h>
22 #include <inttypes.h>
23 #include <libnvme.h>
24
25 #include <ccan/endian/endian.h>
26
27 static bool nvme_match_subsysnqn_filter(nvme_subsystem_t s,
28 nvme_ctrl_t c, nvme_ns_t ns, void *f_args)
29 {
30 char *nqn_match = f_args;
31
32 if (s)
33 return strcmp(nvme_subsystem_get_nqn(s), nqn_match) == 0;
34 return true;
35 }
36
37 static int test_ctrl(nvme_ctrl_t c)
38 {
39 static __u8 buf[0x1000];
40
41 enum nvme_get_features_sel sel = NVME_GET_FEATURES_SEL_CURRENT;
42 int ret, temp, fd = nvme_ctrl_get_fd(c);
43 struct nvme_error_log_page error[64];
44 struct nvme_smart_log smart = { 0 };
45 struct nvme_firmware_slot fw = { 0 };
46 struct nvme_ns_list ns_list = { 0 };
47 struct nvme_cmd_effects_log cfx = { 0 };
48 struct nvme_self_test_log st = { 0 };
49 struct nvme_telemetry_log *telem = (void *)buf;
50 struct nvme_endurance_group_log eglog = { 0 };
51 struct nvme_ana_log *analog = (void *)buf;
52 struct nvme_resv_notification_log resvnotify = { 0 };
53 struct nvme_sanitize_log_page sanlog = { 0 };
54 struct nvme_id_uuid_list uuid = { 0 };
55 struct nvme_id_ns_granularity_list gran = { 0 };
56 struct nvme_secondary_ctrl_list sec = { 0 };
57 struct nvme_primary_ctrl_cap prim = { 0 };
58 struct nvme_ctrl_list ctrlist = { 0 };
59 struct nvme_id_ctrl id = { 0 };
60
61 __u32 result;
62
63 ret = nvme_ctrl_identify(c, &id);
64 if (ret) {
65 printf("ERROR: no identify for:%s\n", nvme_ctrl_get_name(c));
66 return ret;
67 }
68 else {
69 printf("PASSED: Identify controller\n");
70 }
71
72 ret = nvme_get_log_smart(fd, NVME_NSID_ALL, true, &smart);
73 if (ret) {
74 printf("ERROR: no smart log for:%s %#x\n", nvme_ctrl_get_name(c), ret);
75 return ret;
76 }
77 else {
78 printf("PASSED: smart log\n");
79 }
80
81 temp = ((smart.temperature[1] << 8) | smart.temperature[0]) - 273;
82 printf("Controller:%s\n", nvme_ctrl_get_name(c));
83 printf("\nIdentify:\n");
84 printf(" vid:%#04x\n", le16_to_cpu(id.vid));
85 printf(" ssvid:%#04x\n", le16_to_cpu(id.ssvid));
86 printf(" oacs:%#x\n", id.oacs);
87 printf(" lpa:%#x\n", id.lpa);
88 printf(" sn:%-.20s\n", id.sn);
89 printf(" model:%-.40s\n", id.mn);
90
91 ret = nvme_identify_allocated_ns_list(fd, 0, &ns_list);
92 if (!ret)
93 printf(" PASSED: Allocated NS List\n");
94 else
95 printf(" ERROR: Allocated NS List:%x\n", ret);
96 ret = nvme_identify_active_ns_list(fd, 0, &ns_list);
97 if (!ret)
98 printf(" PASSED: Active NS List\n");
99 else
100 printf(" ERROR: Active NS List:%x\n", ret);
101 ret = nvme_identify_ctrl_list(fd, 0, &ctrlist);
102 if (!ret)
103 printf(" PASSED: Ctrl List\n");
104 else
105 printf(" ERROR: CtrlList:%x\n", ret);
106 ret = nvme_identify_nsid_ctrl_list(fd, 1, 0, &ctrlist);
107 if (!ret)
108 printf(" PASSED: NSID Ctrl List\n");
109 else
110 printf(" ERROR: NSID CtrlList:%x\n", ret);
111 ret = nvme_identify_primary_ctrl(fd, 0, &prim);
112 if (!ret)
113 printf(" PASSED: Identify Primary\n");
114 else
115 printf(" ERROR: Identify Primary:%x\n", ret);
116 ret = nvme_identify_secondary_ctrl_list(fd, 0, &sec);
117 if (!ret)
118 printf(" PASSED: Identify Secondary\n");
119 else
120 printf(" ERROR: Identify Secondary:%x\n", ret);
121 ret = nvme_identify_ns_granularity(fd, &gran);
122 if (!ret)
123 printf(" PASSED: Identify NS granularity\n");
124 else
125 printf(" ERROR: Identify NS granularity:%x\n", ret);
126 ret = nvme_identify_uuid(fd, &uuid);
127 if (!ret)
128 printf(" PASSED: Identify UUID List\n");
129 else
130 printf(" ERROR: Identify UUID List:%x\n", ret);
131
132 printf("\nLogs\n");
133 printf(" SMART: Current temperature:%d percent used:%d%%\n", temp,
134 smart.percent_used);
135 ret = nvme_get_log_sanitize(fd, true, &sanlog);
136 if (!ret)
137 printf(" Sanitize Log:\n");
138 else
139 printf(" ERROR: Sanitize Log:%x\n", ret);
140 ret = nvme_get_log_reservation(fd, true, &resvnotify);
141 if (!ret)
142 printf(" Reservation Log\n");
143 else
144 printf(" ERROR: Reservation Log:%x\n", ret);
145 ret = nvme_get_log_ana_groups(fd, true, sizeof(buf), analog);
146 if (!ret)
147 printf(" ANA Groups\n");
148 else
149 printf(" ERROR: ANA Groups:%x\n", ret);
150 ret = nvme_get_log_endurance_group(fd, 0, &eglog);
151 if (!ret)
152 printf(" Endurance Group\n");
153 else
154 printf(" ERROR: Endurance Group:%x\n", ret);
155 ret = nvme_get_log_telemetry_ctrl(fd, true, 0, sizeof(buf), telem);
156 if (!ret)
157 printf(" Telemetry Controller\n");
158 else
159 printf(" ERROR: Telemetry Controller:%x\n", ret);
160 ret = nvme_get_log_device_self_test(fd, &st);
161 if (!ret)
162 printf(" Device Self Test\n");
163 else
164 printf(" ERROR: Device Self Test:%x\n", ret);
165 ret = nvme_get_log_cmd_effects(fd, NVME_CSI_NVM, &cfx);
166 if (!ret)
167 printf(" Command Effects\n");
168 else
169 printf(" ERROR: Command Effects:%x\n", ret);
170 ret = nvme_get_log_changed_ns_list(fd, true, &ns_list);
171 if (!ret)
172 printf(" Change NS List\n");
173 else
174 printf(" ERROR: Change NS List:%x\n", ret);
175 ret = nvme_get_log_fw_slot(fd, true, &fw);
176 if (!ret)
177 printf(" FW Slot\n");
178 else
179 printf(" ERROR: FW Slot%x\n", ret);
180 ret = nvme_get_log_error(fd, 64, true, error);
181 if (!ret)
182 printf(" Error Log\n");
183 else
184 printf(" ERROR: Error Log:%x\n", ret);
185 printf("\nFeatures\n");
186 ret = nvme_get_features_arbitration(fd, sel, &result);
187 if (!ret)
188 printf(" Arbitration:%x\n", result);
189 else if (ret > 0)
190 printf(" ERROR: Arbitration:%x\n", ret);
191 ret = nvme_get_features_power_mgmt(fd, sel, &result);
192 if (!ret)
193 printf(" Power Management:%x\n", result);
194 else if (ret > 0)
195 printf(" ERROR: Power Management:%x\n", ret);
196 ret = nvme_get_features_temp_thresh(fd, sel, &result);
197 if (!ret)
198 printf(" Temperature Threshold:%x\n", result);
199 else if (ret > 0)
200 printf(" ERROR: Temperature Threshold:%x\n", ret);
201 ret = nvme_get_features_err_recovery2(fd, sel, 0, &result);
202 if (!ret)
203 printf(" Error Recovery:%x\n", result);
204 else if (ret > 0)
205 printf(" ERROR: Error Recovery:%x\n", ret);
206 ret = nvme_get_features_volatile_wc(fd, sel, &result);
207 if (!ret)
208 printf(" Volatile Write Cache:%x\n", result);
209 else if (ret > 0)
210 printf(" ERROR: Volatile Write Cache:%x\n", ret);
211 ret = nvme_get_features_num_queues(fd, sel, &result);
212 if (!ret)
213 printf(" Number of Queues:%x\n", result);
214 else if (ret > 0)
215 printf(" ERROR: Number of Queues:%x\n", ret);
216 ret = nvme_get_features_irq_coalesce(fd, sel, &result);
217 if (!ret)
218 printf(" IRQ Coalescing:%x\n", result);
219 else if (ret > 0)
220 printf(" ERROR: IRQ Coalescing:%x\n", ret);
221 ret = nvme_get_features_write_atomic(fd, sel, &result);
222 if (!ret)
223 printf(" Write Atomic:%x\n", result);
224 else if (ret > 0)
225 printf(" ERROR: Write Atomic:%x\n", ret);
226 ret = nvme_get_features_async_event(fd, sel, &result);
227 if (!ret)
228 printf(" Asycn Event Config:%x\n", result);
229 else if (ret > 0)
230 printf(" ERROR: Asycn Event Config:%x\n", ret);
231 ret = nvme_get_features_hctm(fd, sel, &result);
232 if (!ret)
233 printf(" HCTM:%x\n", result);
234 else if (ret > 0)
235 printf(" ERROR: HCTM:%x\n", ret);
236 ret = nvme_get_features_nopsc(fd, sel, &result);
237 if (!ret)
238 printf(" NOP Power State Config:%x\n", result);
239 else if (ret > 0)
240 printf(" ERROR: NOP Power State Configrbitration:%x\n", ret);
241 ret = nvme_get_features_rrl(fd, sel, &result);
242 if (!ret)
243 printf(" Read Recover Levels:%x\n", result);
244 else if (ret > 0)
245 printf(" ERROR: Read Recover Levels:%x\n", ret);
246 ret = nvme_get_features_lba_sts_interval(fd, sel, &result);
247 if (!ret)
248 printf(" LBA Status Interval:%x\n", result);
249 else if (ret > 0)
250 printf(" ERROR: LBA Status Interval:%x\n", ret);
251 ret = nvme_get_features_sanitize(fd, sel, &result);
252 if (!ret)
253 printf(" Sanitize:%x\n", result);
254 else if (ret > 0)
255 printf(" ERROR: SW Progress Marker:%x\n", ret);
256 ret = nvme_get_features_sw_progress(fd, sel, &result);
257 if (!ret)
258 printf(" SW Progress Marker:%x\n", result);
259 else if (ret > 0)
260 printf(" ERROR: Sanitize:%x\n", ret);
261 ret = nvme_get_features_resv_mask2(fd, sel, 0, &result);
262 if (!ret)
263 printf(" Reservation Mask:%x\n", result);
264 else if (ret > 0)
265 printf(" ERROR: Reservation Mask:%x\n", ret);
266 ret = nvme_get_features_resv_persist2(fd, sel, 0, &result);
267 if (!ret)
268 printf(" Reservation Persistence:%x\n", result);
269 else if (ret > 0)
270 printf(" ERROR: Reservation Persistence:%x\n", ret);
271 return 0;
272 }
273
274 static int test_namespace(nvme_ns_t n)
275 {
276 int ret, nsid = nvme_ns_get_nsid(n), fd = nvme_ns_get_fd(n);
277 struct nvme_id_ns ns = { 0 }, allocated = { 0 };
278 struct nvme_ns_id_desc *descs;
279 __u32 result = 0;
280 __u8 flbas;
281
282 ret = nvme_ns_identify(n, &ns);
283 if (ret)
284 return ret;
285
286 nvme_id_ns_flbas_to_lbaf_inuse(ns.flbas, &flbas);
287 printf("%s: nsze:%" PRIu64 " lba size:%d\n",
288 nvme_ns_get_name(n), le64_to_cpu(ns.nsze),
289 1 << ns.lbaf[flbas].ds);
290
291 ret = nvme_identify_allocated_ns(fd, nsid, &allocated);
292 if (!ret)
293 printf(" Identify allocated ns\n");
294 else
295 printf(" ERROR: Identify allocated ns:%x\n", ret);
296 descs = malloc(NVME_IDENTIFY_DATA_SIZE);
297 if (!descs)
298 return -1;
299
300 ret = nvme_identify_ns_descs(fd, nsid, descs);
301 if (!ret)
302 printf(" Identify NS Descriptors\n");
303 else
304 printf(" ERROR: Identify NS Descriptors:%x\n", ret);
305 free(descs);
306 ret = nvme_get_features_write_protect(fd, nsid,
307 NVME_GET_FEATURES_SEL_CURRENT, &result);
308 if (!ret)
309 printf(" Write Protect:%x\n", result);
310 else if (ret > 0)
311 printf(" ERROR: Write Protect:%x\n", ret);
312 return 0;
313 }
314
315 static void print_hex(const uint8_t *x, int len)
316 {
317 int i;
318
319 for (i = 0; i < len; i++)
320 printf("%02x", x[i]);
321 }
322
323 int main(int argc, char **argv)
324 {
325 nvme_root_t r;
326 nvme_host_t h;
327 nvme_subsystem_t s;
328 nvme_ctrl_t c;
329 nvme_path_t p;
330 nvme_ns_t n;
331 const char *ctrl = "nvme4";
332 const char *nqn_match = "testnqn";
333
334 printf("Test filter for common loop back target\n");
335 r = nvme_create_root(NULL, DEFAULT_LOGLEVEL);
336 if (!r)
337 return 1;
338 nvme_scan_topology(r, nvme_match_subsysnqn_filter, (void *)nqn_match);
339 nvme_for_each_host(r, h) {
340 nvme_for_each_subsystem(h, s) {
341 printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),
342 nvme_subsystem_get_nqn(s));
343 nvme_subsystem_for_each_ctrl(s, c) {
344 printf(" %s %s %s %s\n", nvme_ctrl_get_name(c),
345 nvme_ctrl_get_transport(c),
346 nvme_ctrl_get_address(c),
347 nvme_ctrl_get_state(c));
348 }
349 }
350 }
351 printf("\n");
352
353 if (argc > 1)
354 ctrl = argv[1];
355
356 printf("Test scan specific controller\n");
357 c = nvme_scan_ctrl(r, ctrl);
358 if (c) {
359 printf("%s %s %s %s\n", nvme_ctrl_get_name(c),
360 nvme_ctrl_get_transport(c),
361 nvme_ctrl_get_address(c),
362 nvme_ctrl_get_state(c));
363 nvme_free_ctrl(c);
364 }
365 printf("\n");
366 nvme_free_tree(r);
367
368 r = nvme_scan(NULL);
369 if (!r)
370 return -1;
371
372 printf("Test walking the topology\n");
373 nvme_for_each_host(r, h) {
374 nvme_for_each_subsystem(h, s) {
375 printf("%s - NQN=%s\n", nvme_subsystem_get_name(s),
376 nvme_subsystem_get_nqn(s));
377 nvme_subsystem_for_each_ctrl(s, c) {
378 printf(" `- %s %s %s %s\n",
379 nvme_ctrl_get_name(c),
380 nvme_ctrl_get_transport(c),
381 nvme_ctrl_get_address(c),
382 nvme_ctrl_get_state(c));
383
384 nvme_ctrl_for_each_ns(c, n) {
385 char uuid_str[NVME_UUID_LEN_STRING];
386 unsigned char uuid[NVME_UUID_LEN];
387 printf(" `- %s lba size:%d lba max:%" PRIu64 "\n",
388 nvme_ns_get_name(n),
389 nvme_ns_get_lba_size(n),
390 nvme_ns_get_lba_count(n));
391 printf(" eui:");
392 print_hex(nvme_ns_get_eui64(n), 8);
393 printf(" nguid:");
394 print_hex(nvme_ns_get_nguid(n), 16);
395 nvme_ns_get_uuid(n, uuid);
396 nvme_uuid_to_string(uuid, uuid_str);
397 printf(" uuid:%s csi:%d\n", uuid_str,
398 nvme_ns_get_csi(n));
399 }
400
401 nvme_ctrl_for_each_path(c, p)
402 printf(" `- %s %s\n",
403 nvme_path_get_name(p),
404 nvme_path_get_ana_state(p));
405 }
406
407 nvme_subsystem_for_each_ns(s, n) {
408 printf(" `- %s lba size:%d lba max:%" PRIu64 "\n",
409 nvme_ns_get_name(n),
410 nvme_ns_get_lba_size(n),
411 nvme_ns_get_lba_count(n));
412 }
413 }
414 printf("\n");
415 }
416
417 printf("Test identification, logs, and features\n");
418 nvme_for_each_host(r, h) {
419 nvme_for_each_subsystem(h, s) {
420 nvme_subsystem_for_each_ctrl(s, c) {
421 test_ctrl(c);
422 printf("\n");
423 nvme_ctrl_for_each_ns(c, n) {
424 test_namespace(n);
425 printf("\n");
426 }
427 }
428 nvme_subsystem_for_each_ns(s, n) {
429 test_namespace(n);
430 printf("\n");
431 }
432 }
433 }
434 nvme_free_tree(r);
435
436 return 0;
437 }