Debian Codegraph @

test/mi.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /**
3 * This file is part of libnvme.
4 * Copyright (c) 2022 Code Construct
5 */
6
7 #undef NDEBUG
8 #include <assert.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <unistd.h>
12
13 #include <ccan/array_size/array_size.h>
14 #include <ccan/endian/endian.h>
15
16 /* we define a custom transport, so need the internal headers */
17 #include "nvme/private.h"
18
19 #include "libnvme-mi.h"
20
21 #include "utils.h"
22
23 typedef int (*test_submit_cb)(struct nvme_mi_ep *ep,
24 struct nvme_mi_req *req,
25 struct nvme_mi_resp *resp,
26 void *data);
27
28 struct test_transport_data {
29 unsigned int magic;
30 bool named;
31 test_submit_cb submit_cb;
32 void *submit_cb_data;
33 };
34
35 static const int test_transport_magic = 0x74657374;
36
37 static int test_transport_submit(struct nvme_mi_ep *ep,
38 struct nvme_mi_req *req,
39 struct nvme_mi_resp *resp)
40 {
41 struct test_transport_data *tpd = ep->transport_data;
42
43 assert(tpd->magic == test_transport_magic);
44
45 /* start from a minimal response: zeroed data, nmp to match request */
46 memset(resp->hdr, 0, resp->hdr_len);
47 if (resp->data_len)
48 memset(resp->data, 0, resp->data_len);
49 resp->hdr->type = NVME_MI_MSGTYPE_NVME;
50 resp->hdr->nmp = req->hdr->nmp | (NVME_MI_ROR_RSP << 7);
51
52 if (tpd->submit_cb)
53 return tpd->submit_cb(ep, req, resp, tpd->submit_cb_data);
54
55 return 0;
56 }
57
58 static void test_transport_close(struct nvme_mi_ep *ep)
59 {
60 struct test_transport_data *tpd = ep->transport_data;
61 assert(tpd->magic == test_transport_magic);
62 free(tpd);
63 }
64
65 static int test_transport_desc_ep(struct nvme_mi_ep *ep,
66 char *buf, size_t len)
67 {
68 struct test_transport_data *tpd = ep->transport_data;
69
70 assert(tpd->magic == test_transport_magic);
71
72 if (!tpd->named)
73 return -1;
74
75 snprintf(buf, len, "test endpoint 0x%x", tpd->magic);
76
77 return 0;
78 }
79
80 /* internal test helper to generate correct response crc */
81 static void test_transport_resp_calc_mic(struct nvme_mi_resp *resp)
82 {
83 extern __u32 nvme_mi_crc32_update(__u32 crc, void *data, size_t len);
84 __u32 crc = 0xffffffff;
85
86 crc = nvme_mi_crc32_update(crc, resp->hdr, resp->hdr_len);
87 crc = nvme_mi_crc32_update(crc, resp->data, resp->data_len);
88
89 resp->mic = ~crc;
90 }
91
92 static const struct nvme_mi_transport test_transport = {
93 .name = "test-mi",
94 .mic_enabled = true,
95 .submit = test_transport_submit,
96 .close = test_transport_close,
97 .desc_ep = test_transport_desc_ep,
98 //The following aren't actually used by the test_transport
99 .aem_fd = NULL,
100 .aem_purge = NULL,
101 .aem_read = NULL,
102 };
103
104 static void test_set_transport_callback(nvme_mi_ep_t ep, test_submit_cb cb,
105 void *data)
106 {
107 struct test_transport_data *tpd = ep->transport_data;
108 assert(tpd->magic == test_transport_magic);
109
110 tpd->submit_cb = cb;
111 tpd->submit_cb_data = data;
112 }
113
114 nvme_mi_ep_t nvme_mi_open_test(nvme_root_t root)
115 {
116 struct test_transport_data *tpd;
117 struct nvme_mi_ep *ep;
118
119 ep = nvme_mi_init_ep(root);
120 assert(ep);
121
122 /* preempt the quirk probe to avoid clutter */
123 ep->quirks_probed = true;
124
125 tpd = malloc(sizeof(*tpd));
126 assert(tpd);
127
128 tpd->magic = test_transport_magic;
129 tpd->named = true;
130
131 ep->transport = &test_transport;
132 ep->transport_data = tpd;
133
134 return ep;
135 }
136
137 unsigned int count_root_eps(nvme_root_t root)
138 {
139 unsigned int i = 0;
140 nvme_mi_ep_t ep;
141
142 nvme_mi_for_each_endpoint(root, ep)
143 i++;
144
145 return i;
146 }
147
148 /* test that the root->endpoints list is updated on endpoint
149 * creation/destruction */
150 static void test_endpoint_lifetime(nvme_mi_ep_t ep)
151 {
152 nvme_root_t root = ep->root;
153 unsigned int count;
154 nvme_mi_ep_t ep2;
155
156 count = count_root_eps(root);
157 assert(count == 1);
158
159 ep2 = nvme_mi_open_test(root);
160 count = count_root_eps(root);
161 assert(count == 2);
162
163 nvme_mi_close(ep2);
164 count = count_root_eps(root);
165 assert(count == 1);
166 }
167
168 unsigned int count_ep_controllers(nvme_mi_ep_t ep)
169 {
170 unsigned int i = 0;
171 nvme_mi_ctrl_t ctrl;
172
173 nvme_mi_for_each_ctrl(ep, ctrl)
174 i++;
175
176 return i;
177 }
178
179 /* test that the ep->controllers list is updated on controller
180 * creation/destruction */
181 static void test_ctrl_lifetime(nvme_mi_ep_t ep)
182 {
183 nvme_mi_ctrl_t c1, c2;
184 int count;
185
186 ep->controllers_scanned = true;
187
188 count = count_ep_controllers(ep);
189 assert(count == 0);
190
191 c1 = nvme_mi_init_ctrl(ep, 1);
192 count = count_ep_controllers(ep);
193 assert(count == 1);
194
195 c2 = nvme_mi_init_ctrl(ep, 2);
196 count = count_ep_controllers(ep);
197 assert(count == 2);
198
199 nvme_mi_close_ctrl(c1);
200 count = count_ep_controllers(ep);
201 assert(count == 1);
202
203 nvme_mi_close_ctrl(c2);
204 count = count_ep_controllers(ep);
205 assert(count == 0);
206 }
207
208
209 /* test: basic read MI datastructure command */
210 static int test_read_mi_data_cb(struct nvme_mi_ep *ep,
211 struct nvme_mi_req *req,
212 struct nvme_mi_resp *resp,
213 void *data)
214 {
215 __u8 ror, mt, *hdr, *buf;
216
217 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
218
219 ror = req->hdr->nmp >> 7;
220 mt = req->hdr->nmp >> 3 & 0x7;
221 assert(ror == NVME_MI_ROR_REQ);
222 assert(mt == NVME_MI_MT_MI);
223
224 /* do we have enough for a mi header? */
225 assert(req->hdr_len == sizeof(struct nvme_mi_mi_req_hdr));
226
227 /* inspect response as raw bytes */
228 hdr = (__u8 *)req->hdr;
229 assert(hdr[4] == nvme_mi_mi_opcode_mi_data_read);
230
231 /* create basic response */
232 assert(resp->hdr_len >= sizeof(struct nvme_mi_mi_resp_hdr));
233 assert(resp->data_len >= 4);
234
235 hdr = (__u8 *)resp->hdr;
236 hdr[4] = 0; /* status */
237
238 buf = (__u8 *)resp->data;
239 memset(buf, 0, resp->data_len);
240 buf[0] = 1; /* NUMP */
241 buf[1] = 1; /* MJR */
242 buf[2] = 2; /* MNR */
243
244 test_transport_resp_calc_mic(resp);
245
246 return 0;
247 }
248
249 static void test_read_mi_data(nvme_mi_ep_t ep)
250 {
251 struct nvme_mi_read_nvm_ss_info ss_info;
252 int rc;
253
254 test_set_transport_callback(ep, test_read_mi_data_cb, NULL);
255
256 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
257 assert(rc == 0);
258 }
259
260 /* test: failed transport */
261 static int test_transport_fail_cb(struct nvme_mi_ep *ep,
262 struct nvme_mi_req *req,
263 struct nvme_mi_resp *resp,
264 void *data)
265 {
266 return -1;
267 }
268
269 static void test_transport_fail(nvme_mi_ep_t ep)
270 {
271 struct nvme_mi_read_nvm_ss_info ss_info;
272 int rc;
273
274 test_set_transport_callback(ep, test_transport_fail_cb, NULL);
275 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
276 assert(rc != 0);
277 }
278
279 static void test_transport_describe(nvme_mi_ep_t ep)
280 {
281 struct test_transport_data *tpd;
282 char *str;
283
284 tpd = (struct test_transport_data *)ep->transport_data;
285
286 tpd->named = false;
287 str = nvme_mi_endpoint_desc(ep);
288 assert(str);
289 assert(!strcmp(str, "test-mi endpoint"));
290 free(str);
291
292 tpd->named = true;
293 str = nvme_mi_endpoint_desc(ep);
294 assert(str);
295 assert(!strcmp(str, "test-mi: test endpoint 0x74657374"));
296 free(str);
297 }
298
299 /* test: invalid crc */
300 static int test_invalid_crc_cb(struct nvme_mi_ep *ep,
301 struct nvme_mi_req *req,
302 struct nvme_mi_resp *resp,
303 void *data)
304 {
305 resp->mic = 0;
306 return 0;
307 }
308
309 static void test_invalid_crc(nvme_mi_ep_t ep)
310 {
311 struct nvme_mi_read_nvm_ss_info ss_info;
312 int rc;
313
314 test_set_transport_callback(ep, test_invalid_crc_cb, NULL);
315 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
316 assert(rc < 0);
317 }
318
319 /* test: test that the controller list populates the endpoint's list of
320 * controllers */
321 static int test_scan_ctrl_list_cb(struct nvme_mi_ep *ep,
322 struct nvme_mi_req *req,
323 struct nvme_mi_resp *resp,
324 void *data)
325 {
326 __u8 ror, mt, *hdr, *buf;
327
328 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
329
330 ror = req->hdr->nmp >> 7;
331 mt = req->hdr->nmp >> 3 & 0x7;
332 assert(ror == NVME_MI_ROR_REQ);
333 assert(mt == NVME_MI_MT_MI);
334
335 /* do we have enough for a mi header? */
336 assert(req->hdr_len == sizeof(struct nvme_mi_mi_req_hdr));
337
338 /* inspect response as raw bytes */
339 hdr = (__u8 *)req->hdr;
340 assert(hdr[4] == nvme_mi_mi_opcode_mi_data_read);
341 assert(hdr[11] == nvme_mi_dtyp_ctrl_list);
342
343 /* create basic response */
344 assert(resp->hdr_len >= sizeof(struct nvme_mi_mi_resp_hdr));
345 assert(resp->data_len >= 4);
346
347 hdr = (__u8 *)resp->hdr;
348 hdr[4] = 0; /* status */
349
350 buf = (__u8 *)resp->data;
351 memset(buf, 0, resp->data_len);
352 buf[0] = 3; buf[1] = 0; /* num controllers */
353 buf[2] = 1; buf[3] = 0; /* id 1 */
354 buf[4] = 4; buf[5] = 0; /* id 4 */
355 buf[6] = 5; buf[7] = 0; /* id 5 */
356
357 test_transport_resp_calc_mic(resp);
358
359 return 0;
360 }
361
362 static void test_scan_ctrl_list(nvme_mi_ep_t ep)
363 {
364 struct nvme_mi_ctrl *ctrl;
365
366 ep->controllers_scanned = false;
367
368 test_set_transport_callback(ep, test_scan_ctrl_list_cb, NULL);
369
370 nvme_mi_scan_ep(ep, false);
371
372 ctrl = nvme_mi_first_ctrl(ep);
373 assert(ctrl);
374 assert(ctrl->id == 1);
375
376 ctrl = nvme_mi_next_ctrl(ep, ctrl);
377 assert(ctrl);
378 assert(ctrl->id == 4);
379
380 ctrl = nvme_mi_next_ctrl(ep, ctrl);
381 assert(ctrl);
382 assert(ctrl->id == 5);
383
384 ctrl = nvme_mi_next_ctrl(ep, ctrl);
385 assert(ctrl == NULL);
386 }
387
388 /* test: simple NVMe admin request/response */
389 static int test_admin_id_cb(struct nvme_mi_ep *ep,
390 struct nvme_mi_req *req,
391 struct nvme_mi_resp *resp,
392 void *data)
393 {
394 __u8 ror, mt, *hdr;
395 __u32 dlen, cdw10;
396 __u16 ctrl_id;
397 __u8 flags;
398
399 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
400
401 ror = req->hdr->nmp >> 7;
402 mt = req->hdr->nmp >> 3 & 0x7;
403 assert(ror == NVME_MI_ROR_REQ);
404 assert(mt == NVME_MI_MT_ADMIN);
405
406 /* do we have enough for a mi header? */
407 assert(req->hdr_len == sizeof(struct nvme_mi_admin_req_hdr));
408
409 /* inspect response as raw bytes */
410 hdr = (__u8 *)req->hdr;
411 assert(hdr[4] == nvme_admin_identify);
412 flags = hdr[5];
413
414 ctrl_id = hdr[7] << 8 | hdr[6];
415 assert(ctrl_id == 0x5); /* controller id */
416
417 /* we requested a full id; if we've set the length flag,
418 * ensure the length matches */
419 dlen = hdr[35] << 24 | hdr[34] << 16 | hdr[33] << 8 | hdr[32];
420 if (flags & 0x1) {
421 assert(dlen == sizeof(struct nvme_id_ctrl));
422 }
423 assert(!(flags & 0x2));
424
425 /* CNS value of 1 in cdw10 field */
426 cdw10 = hdr[47] << 24 | hdr[46] << 16 | hdr[45] << 8 | hdr[44];
427 assert(cdw10 == 0x1);
428
429 /* create valid (but somewhat empty) response */
430 hdr = (__u8 *)resp->hdr;
431 hdr[4] = 0x00; /* status: success */
432
433 test_transport_resp_calc_mic(resp);
434
435 return 0;
436 }
437
438 static void test_admin_id(nvme_mi_ep_t ep)
439 {
440 struct nvme_id_ctrl id;
441 nvme_mi_ctrl_t ctrl;
442 int rc;
443
444 test_set_transport_callback(ep, test_admin_id_cb, NULL);
445
446 ctrl = nvme_mi_init_ctrl(ep, 5);
447 assert(ctrl);
448
449 rc = nvme_mi_admin_identify_ctrl(ctrl, &id);
450 assert(rc == 0);
451 }
452
453 /* test: simple NVMe error response, error reported in the MI header */
454 static int test_admin_err_mi_resp_cb(struct nvme_mi_ep *ep,
455 struct nvme_mi_req *req,
456 struct nvme_mi_resp *resp,
457 void *data)
458 {
459 __u8 ror, mt, *hdr;
460
461 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
462
463 ror = req->hdr->nmp >> 7;
464 mt = req->hdr->nmp >> 3 & 0x7;
465 assert(ror == NVME_MI_ROR_REQ);
466 assert(mt == NVME_MI_MT_ADMIN);
467
468 /* do we have enough for a mi header? */
469 assert(req->hdr_len == sizeof(struct nvme_mi_admin_req_hdr));
470
471 /* inspect response as raw bytes */
472 hdr = (__u8 *)req->hdr;
473 assert(hdr[4] == nvme_admin_identify);
474
475 /* we need at least 8 bytes for error information */
476 assert(resp->hdr_len >= 8);
477
478 /* create error response */
479 hdr = (__u8 *)resp->hdr;
480 hdr[4] = 0x02; /* status: internal error */
481 hdr[5] = 0;
482 hdr[6] = 0;
483 hdr[7] = 0;
484 resp->hdr_len = 8;
485 resp->data_len = 0;
486
487 test_transport_resp_calc_mic(resp);
488
489 return 0;
490 }
491
492 static void test_admin_err_mi_resp(nvme_mi_ep_t ep)
493 {
494 struct nvme_id_ctrl id;
495 nvme_mi_ctrl_t ctrl;
496 int rc;
497
498 test_set_transport_callback(ep, test_admin_err_mi_resp_cb, NULL);
499
500 ctrl = nvme_mi_init_ctrl(ep, 1);
501 assert(ctrl);
502
503 rc = nvme_mi_admin_identify_ctrl(ctrl, &id);
504 assert(rc != 0);
505 assert(nvme_status_get_type(rc) == NVME_STATUS_TYPE_MI);
506 assert(nvme_status_get_value(rc) == NVME_MI_RESP_INTERNAL_ERR);
507 }
508
509 /* test: NVMe Admin error, with the error reported in the Admin response */
510 static int test_admin_err_nvme_resp_cb(struct nvme_mi_ep *ep,
511 struct nvme_mi_req *req,
512 struct nvme_mi_resp *resp,
513 void *data)
514 {
515 __u8 ror, mt, *hdr;
516
517 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
518
519 ror = req->hdr->nmp >> 7;
520 mt = req->hdr->nmp >> 3 & 0x7;
521 assert(ror == NVME_MI_ROR_REQ);
522 assert(mt == NVME_MI_MT_ADMIN);
523
524 /* do we have enough for a mi header? */
525 assert(req->hdr_len == sizeof(struct nvme_mi_admin_req_hdr));
526
527 /* inspect response as raw bytes */
528 hdr = (__u8 *)req->hdr;
529 assert(hdr[4] == nvme_admin_identify);
530
531 /* we need at least 8 bytes for error information */
532 assert(resp->hdr_len >= sizeof(struct nvme_mi_admin_resp_hdr));
533
534 /* create error response */
535 hdr = (__u8 *)resp->hdr;
536 hdr[4] = 0; /* MI status: success */
537 hdr[5] = 0;
538 hdr[6] = 0;
539 hdr[7] = 0;
540
541 hdr[16] = 0; /* cdw3: SC: internal, SCT: generic, DNR */
542 hdr[17] = 0;
543 hdr[18] = 0x0c;
544 hdr[19] = 0x80;
545
546 resp->hdr_len = sizeof(struct nvme_mi_admin_resp_hdr);
547 resp->data_len = 0;
548
549 test_transport_resp_calc_mic(resp);
550
551 return 0;
552 }
553
554 static void test_admin_err_nvme_resp(nvme_mi_ep_t ep)
555 {
556 struct nvme_id_ctrl id;
557 nvme_mi_ctrl_t ctrl;
558 int rc;
559
560 test_set_transport_callback(ep, test_admin_err_nvme_resp_cb, NULL);
561
562 ctrl = nvme_mi_init_ctrl(ep, 1);
563 assert(ctrl);
564
565 rc = nvme_mi_admin_identify_ctrl(ctrl, &id);
566 assert(rc != 0);
567 assert(nvme_status_get_type(rc) == NVME_STATUS_TYPE_NVME);
568 assert(nvme_status_get_value(rc) ==
569 (NVME_SC_INTERNAL | (NVME_SCT_GENERIC << NVME_SCT_SHIFT)
570 | NVME_SC_DNR));
571 }
572
573 /* invalid command transfers */
574 static int test_rejected_command_cb(struct nvme_mi_ep *ep,
575 struct nvme_mi_req *req,
576 struct nvme_mi_resp *resp,
577 void *data)
578 {
579 /* none of the tests should result in message transfer */
580 assert(0);
581 return -1;
582 }
583
584 static void test_admin_invalid_formats(nvme_mi_ep_t ep)
585 {
586 struct {
587 struct nvme_mi_admin_req_hdr hdr;
588 uint8_t data[4];
589 } req = { 0 };
590 struct nvme_mi_admin_resp_hdr resp = { 0 };
591 nvme_mi_ctrl_t ctrl;
592 size_t len;
593 int rc;
594
595 test_set_transport_callback(ep, test_rejected_command_cb, NULL);
596
597 ctrl = nvme_mi_init_ctrl(ep, 1);
598 assert(ctrl);
599
600 /* unaligned req size */
601 len = 0;
602 rc = nvme_mi_admin_xfer(ctrl, &req.hdr, 1, &resp, 0, &len);
603 assert(rc != 0);
604
605 /* unaligned resp size */
606 len = 1;
607 rc = nvme_mi_admin_xfer(ctrl, &req.hdr, 0, &resp, 0, &len);
608 assert(rc != 0);
609
610 /* unaligned resp offset */
611 len = 4;
612 rc = nvme_mi_admin_xfer(ctrl, &req.hdr, 0, &resp, 1, &len);
613 assert(rc != 0);
614
615 /* resp too large */
616 len = 4096 + 4;
617 rc = nvme_mi_admin_xfer(ctrl, &req.hdr, 0, &resp, 0, &len);
618 assert(rc != 0);
619
620 /* resp offset too large */
621 len = 4;
622 rc = nvme_mi_admin_xfer(ctrl, &req.hdr, 0, &resp, (off_t)1 << 32, &len);
623 assert(rc != 0);
624
625 /* resp offset with no len */
626 len = 0;
627 rc = nvme_mi_admin_xfer(ctrl, &req.hdr, 0, &resp, 4, &len);
628 assert(rc != 0);
629
630 /* req and resp payloads */
631 len = 4;
632 rc = nvme_mi_admin_xfer(ctrl, &req.hdr, 4, &resp, 0, &len);
633 assert(rc != 0);
634 }
635
636 static void test_mi_invalid_formats(nvme_mi_ep_t ep)
637 {
638 struct {
639 struct nvme_mi_mi_req_hdr hdr;
640 uint8_t data[4];
641 } req = { 0 };
642 struct nvme_mi_mi_resp_hdr resp = { 0 };
643 nvme_mi_ctrl_t ctrl;
644 size_t len;
645 int rc;
646
647 test_set_transport_callback(ep, test_rejected_command_cb, NULL);
648
649 ctrl = nvme_mi_init_ctrl(ep, 1);
650 assert(ctrl);
651
652 /* resp too large */
653 len = 4096 + 4;
654 rc = nvme_mi_mi_xfer(ep, &req.hdr, 0, &resp, &len);
655 assert(rc != 0);
656 }
657
658 /* test: header length too small */
659 static int test_resp_hdr_small_cb(struct nvme_mi_ep *ep,
660 struct nvme_mi_req *req,
661 struct nvme_mi_resp *resp,
662 void *data)
663 {
664 resp->hdr_len = 2;
665 test_transport_resp_calc_mic(resp);
666 return 0;
667 }
668
669 static void test_resp_hdr_small(nvme_mi_ep_t ep)
670 {
671 struct nvme_mi_read_nvm_ss_info ss_info;
672 int rc;
673
674 test_set_transport_callback(ep, test_resp_hdr_small_cb, NULL);
675
676 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
677 assert(rc != 0);
678 }
679
680 /* test: respond with a request message */
681 static int test_resp_req_cb(struct nvme_mi_ep *ep,
682 struct nvme_mi_req *req,
683 struct nvme_mi_resp *resp,
684 void *data)
685 {
686 resp->hdr->nmp &= ~(NVME_MI_ROR_RSP << 7);
687 test_transport_resp_calc_mic(resp);
688 return 0;
689 }
690
691 static void test_resp_req(nvme_mi_ep_t ep)
692 {
693 struct nvme_mi_read_nvm_ss_info ss_info;
694 int rc;
695
696 test_set_transport_callback(ep, test_resp_req_cb, NULL);
697
698 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
699 assert(rc != 0);
700 }
701
702 /* test: invalid MCTP type in response */
703 static int test_resp_invalid_type_cb(struct nvme_mi_ep *ep,
704 struct nvme_mi_req *req,
705 struct nvme_mi_resp *resp,
706 void *data)
707 {
708 resp->hdr->type = 0x3;
709 test_transport_resp_calc_mic(resp);
710 return 0;
711 }
712
713 static void test_resp_invalid_type(nvme_mi_ep_t ep)
714 {
715 struct nvme_mi_read_nvm_ss_info ss_info;
716 int rc;
717
718 test_set_transport_callback(ep, test_resp_invalid_type_cb, NULL);
719
720 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
721 assert(rc != 0);
722 }
723
724 /* test: response with mis-matching command slot */
725 static int test_resp_csi_invert_cb(struct nvme_mi_ep *ep,
726 struct nvme_mi_req *req,
727 struct nvme_mi_resp *resp,
728 void *data)
729 {
730 resp->hdr->nmp ^= 0x1;
731 test_transport_resp_calc_mic(resp);
732 return 0;
733 }
734
735 /* test: validation of proper csi setting */
736 static int test_resp_csi_check_cb(struct nvme_mi_ep *ep,
737 struct nvme_mi_req *req,
738 struct nvme_mi_resp *resp,
739 void *data)
740 {
741 assert((req->hdr->nmp & 1) == (ep->csi & 1));
742 return 0;
743 }
744
745 /* test: Ensure that csi bit is set properly in the request */
746 static void test_resp_csi_request(nvme_mi_ep_t ep)
747 {
748 struct nvme_mi_read_nvm_ss_info ss_info;
749 int rc;
750
751 test_set_transport_callback(ep, test_resp_csi_check_cb, NULL);
752
753 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
754 assert(rc != 0);
755
756 nvme_mi_set_csi(ep, 1);//Change CSI
757
758 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
759 assert(rc != 0);
760
761 nvme_mi_set_csi(ep, 0);//Change CSI
762 }
763
764 /* test: Ensure that when csi bit set wrong in response,
765 * it results in an error
766 */
767 static void test_resp_csi_mismatch(nvme_mi_ep_t ep)
768 {
769 struct nvme_mi_read_nvm_ss_info ss_info;
770 int rc;
771
772 test_set_transport_callback(ep, test_resp_csi_invert_cb, NULL);
773
774 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
775 assert(rc != 0);
776
777 nvme_mi_set_csi(ep, 1);//Change CSI
778
779 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
780 assert(rc != 0);
781
782 nvme_mi_set_csi(ep, 0);//Change CSI
783 }
784
785 /* test: config get MTU request & response layout, ensure we're handling
786 * endianness in the 3-byte NMRESP field correctly */
787 static int test_mi_config_get_mtu_cb(struct nvme_mi_ep *ep,
788 struct nvme_mi_req *req,
789 struct nvme_mi_resp *resp,
790 void *data)
791 {
792 struct nvme_mi_mi_resp_hdr *mi_resp;
793 uint8_t *buf;
794
795 assert(req->hdr_len == sizeof(struct nvme_mi_mi_req_hdr));
796 assert(req->data_len == 0);
797
798 /* validate req as raw bytes */
799 buf = (void *)req->hdr;
800 assert(buf[4] == nvme_mi_mi_opcode_configuration_get);
801 /* dword 0: port and config id */
802 assert(buf[11] == 0x5);
803 assert(buf[8] == NVME_MI_CONFIG_MCTP_MTU);
804
805 /* set MTU in response */
806 mi_resp = (void *)resp->hdr;
807 mi_resp->nmresp[1] = 0x12;
808 mi_resp->nmresp[0] = 0x34;
809 resp->hdr_len = sizeof(*mi_resp);
810 resp->data_len = 0;
811
812 test_transport_resp_calc_mic(resp);
813 return 0;
814 }
815
816 static void test_mi_config_get_mtu(nvme_mi_ep_t ep)
817 {
818 uint16_t mtu;
819 int rc;
820
821 test_set_transport_callback(ep, test_mi_config_get_mtu_cb, NULL);
822
823 rc = nvme_mi_mi_config_get_mctp_mtu(ep, 5, &mtu);
824 assert(rc == 0);
825 assert(mtu == 0x1234);
826 }
827
828 /* test: config set SMBus freq, both valid and invalid */
829 static int test_mi_config_set_freq_cb(struct nvme_mi_ep *ep,
830 struct nvme_mi_req *req,
831 struct nvme_mi_resp *resp,
832 void *data)
833 {
834 struct nvme_mi_mi_resp_hdr *mi_resp;
835 uint8_t *buf;
836
837 assert(req->hdr_len == sizeof(struct nvme_mi_mi_req_hdr));
838 assert(req->data_len == 0);
839
840 /* validate req as raw bytes */
841 buf = (void *)req->hdr;
842 assert(buf[4] == nvme_mi_mi_opcode_configuration_set);
843 /* dword 0: port and config id */
844 assert(buf[11] == 0x5);
845 assert(buf[8] == NVME_MI_CONFIG_SMBUS_FREQ);
846
847 mi_resp = (void *)resp->hdr;
848 resp->hdr_len = sizeof(*mi_resp);
849 resp->data_len = 0;
850
851 /* accept 100 & 400, reject others */
852 switch (buf[9]) {
853 case NVME_MI_CONFIG_SMBUS_FREQ_100kHz:
854 case NVME_MI_CONFIG_SMBUS_FREQ_400kHz:
855 mi_resp->status = 0;
856 break;
857 case NVME_MI_CONFIG_SMBUS_FREQ_1MHz:
858 default:
859 mi_resp->status = 0x4;
860 break;
861 }
862
863 test_transport_resp_calc_mic(resp);
864 return 0;
865 }
866
867 static void test_mi_config_set_freq(nvme_mi_ep_t ep)
868 {
869 int rc;
870
871 test_set_transport_callback(ep, test_mi_config_set_freq_cb, NULL);
872
873 rc = nvme_mi_mi_config_set_smbus_freq(ep, 5,
874 NVME_MI_CONFIG_SMBUS_FREQ_100kHz);
875 assert(rc == 0);
876 }
877
878 static void test_mi_config_set_freq_invalid(nvme_mi_ep_t ep)
879 {
880 int rc;
881
882 test_set_transport_callback(ep, test_mi_config_set_freq_cb, NULL);
883
884 rc = nvme_mi_mi_config_set_smbus_freq(ep, 5,
885 NVME_MI_CONFIG_SMBUS_FREQ_1MHz);
886 assert(rc == 4);
887 }
888
889 /* Get Features callback, implementing Arbitration (which doesn't return
890 * additional data) and Timestamp (which does).
891 */
892 static int test_admin_get_features_cb(struct nvme_mi_ep *ep,
893 struct nvme_mi_req *req,
894 struct nvme_mi_resp *resp,
895 void *data)
896 {
897 __u8 sel, fid, ror, mt, *rq_hdr, *rs_hdr, *rs_data;
898 __u16 ctrl_id;
899 int i;
900
901 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
902
903 ror = req->hdr->nmp >> 7;
904 mt = req->hdr->nmp >> 3 & 0x7;
905 assert(ror == NVME_MI_ROR_REQ);
906 assert(mt == NVME_MI_MT_ADMIN);
907
908 /* do we have enough for a mi header? */
909 assert(req->hdr_len == sizeof(struct nvme_mi_admin_req_hdr));
910
911 /* inspect response as raw bytes */
912 rq_hdr = (__u8 *)req->hdr;
913
914 /* opcode */
915 assert(rq_hdr[4] == nvme_admin_get_features);
916
917 /* controller */
918 ctrl_id = rq_hdr[7] << 8 | rq_hdr[6];
919 assert(ctrl_id == 0x5); /* controller id */
920
921 /* sel & fid from lower bytes of cdw10 */
922 fid = rq_hdr[44];
923 sel = rq_hdr[45] & 0x7;
924
925 /* reserved fields */
926 assert(!(rq_hdr[46] || rq_hdr[47] || rq_hdr[45] & 0xf8));
927
928 assert(sel == 0x00);
929
930 rs_hdr = (__u8 *)resp->hdr;
931 rs_hdr[4] = 0x00; /* status: success */
932 rs_data = resp->data;
933
934 /* feature-id specific checks, and response generation */
935 switch (fid) {
936 case NVME_FEAT_FID_ARBITRATION:
937 /* arbitrary (hah!) arbitration value in cdw0 of response */
938 rs_hdr[8] = 1;
939 rs_hdr[9] = 2;
940 rs_hdr[10] = 3;
941 rs_hdr[11] = 4;
942 resp->data_len = 0;
943 break;
944
945 case NVME_FEAT_FID_TIMESTAMP:
946 resp->data_len = 8;
947 for (i = 0; i < 6; i++)
948 rs_data[i] = i;
949 rs_data[6] = 1;
950 break;
951
952 default:
953 assert(0);
954 }
955
956 test_transport_resp_calc_mic(resp);
957
958 return 0;
959 }
960
961 static void test_get_features_nodata(nvme_mi_ep_t ep)
962 {
963 struct nvme_get_features_args args = { 0 };
964 nvme_mi_ctrl_t ctrl;
965 uint32_t res;
966 int rc;
967
968 test_set_transport_callback(ep, test_admin_get_features_cb, NULL);
969
970 ctrl = nvme_mi_init_ctrl(ep, 5);
971 assert(ctrl);
972
973 args.args_size = sizeof(args);
974 args.fid = NVME_FEAT_FID_ARBITRATION;
975 args.sel = 0;
976 args.result = &res;
977
978 rc = nvme_mi_admin_get_features(ctrl, &args);
979 assert(rc == 0);
980 assert(args.data_len == 0);
981 assert(res == 0x04030201);
982 }
983
984 static void test_get_features_data(nvme_mi_ep_t ep)
985 {
986 struct nvme_get_features_args args = { 0 };
987 struct nvme_timestamp tstamp;
988 nvme_mi_ctrl_t ctrl;
989 uint8_t exp[6];
990 uint32_t res;
991 int rc, i;
992
993 test_set_transport_callback(ep, test_admin_get_features_cb, NULL);
994
995 ctrl = nvme_mi_init_ctrl(ep, 5);
996 assert(ctrl);
997
998 args.args_size = sizeof(args);
999 args.fid = NVME_FEAT_FID_TIMESTAMP;
1000 args.sel = 0;
1001 args.result = &res;
1002 args.data = &tstamp;
1003 args.data_len = sizeof(tstamp);
1004
1005 /* expected timestamp value */
1006 for (i = 0; i < sizeof(tstamp.timestamp); i++)
1007 exp[i] = i;
1008
1009 rc = nvme_mi_admin_get_features(ctrl, &args);
1010 assert(rc == 0);
1011 assert(args.data_len == sizeof(tstamp));
1012 assert(tstamp.attr == 1);
1013 assert(!memcmp(tstamp.timestamp, exp, sizeof(tstamp.timestamp)));
1014 }
1015
1016 /* Set Features callback for timestamp */
1017 static int test_admin_set_features_cb(struct nvme_mi_ep *ep,
1018 struct nvme_mi_req *req,
1019 struct nvme_mi_resp *resp,
1020 void *data)
1021 {
1022 __u8 save, fid, ror, mt, *rq_hdr, *rq_data, *rs_hdr;
1023 __u16 ctrl_id;
1024 uint8_t ts[6];
1025 int i;
1026
1027 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
1028
1029 ror = req->hdr->nmp >> 7;
1030 mt = req->hdr->nmp >> 3 & 0x7;
1031 assert(ror == NVME_MI_ROR_REQ);
1032 assert(mt == NVME_MI_MT_ADMIN);
1033 assert(req->hdr_len == sizeof(struct nvme_mi_admin_req_hdr));
1034 assert(req->data_len == 8);
1035
1036 rq_hdr = (__u8 *)req->hdr;
1037 rq_data = req->data;
1038
1039 /* opcode */
1040 assert(rq_hdr[4] == nvme_admin_set_features);
1041
1042 /* controller */
1043 ctrl_id = rq_hdr[7] << 8 | rq_hdr[6];
1044 assert(ctrl_id == 0x5); /* controller id */
1045
1046 /* fid from lower bytes of cdw10, save from top bit */
1047 fid = rq_hdr[44];
1048 save = rq_hdr[47] & 0x80;
1049
1050 /* reserved fields */
1051 assert(!(rq_hdr[45] || rq_hdr[46]));
1052
1053 assert(fid == NVME_FEAT_FID_TIMESTAMP);
1054 assert(save == 0x80);
1055
1056 for (i = 0; i < sizeof(ts); i++)
1057 ts[i] = i;
1058 assert(!memcmp(ts, rq_data, sizeof(ts)));
1059
1060 rs_hdr = (__u8 *)resp->hdr;
1061 rs_hdr[4] = 0x00;
1062
1063 test_transport_resp_calc_mic(resp);
1064
1065 return 0;
1066 }
1067
1068 static void test_set_features(nvme_mi_ep_t ep)
1069 {
1070 struct nvme_set_features_args args = { 0 };
1071 struct nvme_timestamp tstamp = { 0 };
1072 nvme_mi_ctrl_t ctrl;
1073 uint32_t res;
1074 int rc, i;
1075
1076 test_set_transport_callback(ep, test_admin_set_features_cb, NULL);
1077
1078 ctrl = nvme_mi_init_ctrl(ep, 5);
1079 assert(ctrl);
1080
1081 for (i = 0; i < sizeof(tstamp.timestamp); i++)
1082 tstamp.timestamp[i] = i;
1083
1084 args.args_size = sizeof(args);
1085 args.fid = NVME_FEAT_FID_TIMESTAMP;
1086 args.save = 1;
1087 args.result = &res;
1088 args.data = &tstamp;
1089 args.data_len = sizeof(tstamp);
1090
1091 rc = nvme_mi_admin_set_features(ctrl, &args);
1092 assert(rc == 0);
1093 assert(args.data_len == 0);
1094 }
1095
1096 enum ns_type {
1097 NS_ACTIVE,
1098 NS_ALLOC,
1099 };
1100
1101 static int test_admin_id_ns_list_cb(struct nvme_mi_ep *ep,
1102 struct nvme_mi_req *req,
1103 struct nvme_mi_resp *resp,
1104 void *data)
1105 {
1106 struct nvme_ns_list *list;
1107 enum ns_type type;
1108 int offset;
1109 __u8 *hdr;
1110 __u16 cns;
1111
1112 hdr = (__u8 *)req->hdr;
1113 assert(hdr[4] == nvme_admin_identify);
1114
1115 assert(req->data_len == 0);
1116
1117 cns = hdr[45] << 8 | hdr[44];
1118
1119 /* NSID */
1120 assert(hdr[8] == 1 && !hdr[9] && !hdr[10] && !hdr[11]);
1121
1122 type = *(enum ns_type *)data;
1123 resp->data_len = sizeof(*list);
1124 list = resp->data;
1125
1126 switch (type) {
1127 case NS_ALLOC:
1128 assert(cns == NVME_IDENTIFY_CNS_ALLOCATED_NS_LIST);
1129 offset = 2;
1130 break;
1131 case NS_ACTIVE:
1132 assert(cns == NVME_IDENTIFY_CNS_NS_ACTIVE_LIST);
1133 offset = 4;
1134 break;
1135 default:
1136 assert(0);
1137 }
1138
1139 list->ns[0] = cpu_to_le32(offset);
1140 list->ns[1] = cpu_to_le32(offset + 1);
1141
1142 test_transport_resp_calc_mic(resp);
1143
1144 return 0;
1145 }
1146
1147 static void test_admin_id_alloc_ns_list(struct nvme_mi_ep *ep)
1148 {
1149 struct nvme_ns_list list;
1150 nvme_mi_ctrl_t ctrl;
1151 enum ns_type type;
1152 int rc;
1153
1154 type = NS_ALLOC;
1155 test_set_transport_callback(ep, test_admin_id_ns_list_cb, &type);
1156
1157 ctrl = nvme_mi_init_ctrl(ep, 5);
1158 assert(ctrl);
1159
1160 rc = nvme_mi_admin_identify_allocated_ns_list(ctrl, 1, &list);
1161 assert(!rc);
1162
1163 assert(le32_to_cpu(list.ns[0]) == 2);
1164 assert(le32_to_cpu(list.ns[1]) == 3);
1165 assert(le32_to_cpu(list.ns[2]) == 0);
1166 }
1167
1168 static void test_admin_id_active_ns_list(struct nvme_mi_ep *ep)
1169 {
1170 struct nvme_ns_list list;
1171 nvme_mi_ctrl_t ctrl;
1172 enum ns_type type;
1173 int rc;
1174
1175 type = NS_ACTIVE;
1176 test_set_transport_callback(ep, test_admin_id_ns_list_cb, &type);
1177
1178 ctrl = nvme_mi_init_ctrl(ep, 5);
1179 assert(ctrl);
1180
1181 rc = nvme_mi_admin_identify_active_ns_list(ctrl, 1, &list);
1182 assert(!rc);
1183
1184 assert(le32_to_cpu(list.ns[0]) == 4);
1185 assert(le32_to_cpu(list.ns[1]) == 5);
1186 assert(le32_to_cpu(list.ns[2]) == 0);
1187 }
1188
1189 static int test_admin_id_ns_cb(struct nvme_mi_ep *ep,
1190 struct nvme_mi_req *req,
1191 struct nvme_mi_resp *resp,
1192 void *data)
1193 {
1194 struct nvme_id_ns *id;
1195 enum ns_type type;
1196 __u16 nsid, cns;
1197 __u8 *hdr;
1198
1199 hdr = (__u8 *)req->hdr;
1200 assert(hdr[4] == nvme_admin_identify);
1201
1202 assert(req->data_len == 0);
1203
1204 cns = hdr[45] << 8 | hdr[44];
1205
1206 /* NSID */
1207 nsid = hdr[8];
1208 assert(!hdr[9] && !hdr[10] && !hdr[11]);
1209
1210 type = *(enum ns_type *)data;
1211 resp->data_len = sizeof(*id);
1212 id = resp->data;
1213 id->nsze = cpu_to_le64(nsid);
1214
1215 switch (type) {
1216 case NS_ALLOC:
1217 assert(cns == NVME_IDENTIFY_CNS_ALLOCATED_NS);
1218 break;
1219 case NS_ACTIVE:
1220 assert(cns == NVME_IDENTIFY_CNS_NS);
1221 break;
1222 default:
1223 assert(0);
1224 }
1225
1226 test_transport_resp_calc_mic(resp);
1227
1228 return 0;
1229 }
1230
1231 static void test_admin_id_alloc_ns(struct nvme_mi_ep *ep)
1232 {
1233 struct nvme_id_ns id;
1234 nvme_mi_ctrl_t ctrl;
1235 enum ns_type type;
1236 int rc;
1237
1238 type = NS_ALLOC;
1239 test_set_transport_callback(ep, test_admin_id_ns_cb, &type);
1240
1241 ctrl = nvme_mi_init_ctrl(ep, 5);
1242 assert(ctrl);
1243
1244 rc = nvme_mi_admin_identify_allocated_ns(ctrl, 1, &id);
1245 assert(!rc);
1246 assert(le64_to_cpu(id.nsze) == 1);
1247 }
1248
1249 static void test_admin_id_active_ns(struct nvme_mi_ep *ep)
1250 {
1251 struct nvme_id_ns id;
1252 nvme_mi_ctrl_t ctrl;
1253 enum ns_type type;
1254 int rc;
1255
1256 type = NS_ACTIVE;
1257 test_set_transport_callback(ep, test_admin_id_ns_cb, &type);
1258
1259 ctrl = nvme_mi_init_ctrl(ep, 5);
1260 assert(ctrl);
1261
1262 rc = nvme_mi_admin_identify_ns(ctrl, 1, &id);
1263 assert(!rc);
1264 assert(le64_to_cpu(id.nsze) == 1);
1265 }
1266
1267 static int test_admin_id_nsid_ctrl_list_cb(struct nvme_mi_ep *ep,
1268 struct nvme_mi_req *req,
1269 struct nvme_mi_resp *resp,
1270 void *data)
1271 {
1272 __u16 cns, ctrlid;
1273 __u32 nsid;
1274 __u8 *hdr;
1275
1276 hdr = (__u8 *)req->hdr;
1277 assert(hdr[4] == nvme_admin_identify);
1278
1279 assert(req->data_len == 0);
1280
1281 cns = hdr[45] << 8 | hdr[44];
1282 assert(cns == NVME_IDENTIFY_CNS_NS_CTRL_LIST);
1283
1284 nsid = hdr[11] << 24 | hdr[10] << 16 | hdr[9] << 8 | hdr[8];
1285 assert(nsid == 0x01020304);
1286
1287 ctrlid = hdr[47] << 8 | hdr[46];
1288 assert(ctrlid == 5);
1289
1290 resp->data_len = sizeof(struct nvme_ctrl_list);
1291 test_transport_resp_calc_mic(resp);
1292
1293 return 0;
1294 }
1295
1296 static void test_admin_id_nsid_ctrl_list(struct nvme_mi_ep *ep)
1297 {
1298 struct nvme_ctrl_list list;
1299 nvme_mi_ctrl_t ctrl;
1300 int rc;
1301
1302 test_set_transport_callback(ep, test_admin_id_nsid_ctrl_list_cb, NULL);
1303
1304 ctrl = nvme_mi_init_ctrl(ep, 5);
1305 assert(ctrl);
1306
1307 rc = nvme_mi_admin_identify_nsid_ctrl_list(ctrl, 0x01020304, 5, &list);
1308 assert(!rc);
1309 }
1310
1311 static int test_admin_id_secondary_ctrl_list_cb(struct nvme_mi_ep *ep,
1312 struct nvme_mi_req *req,
1313 struct nvme_mi_resp *resp,
1314 void *data)
1315 {
1316 __u16 cns, ctrlid;
1317 __u8 *hdr;
1318
1319 hdr = (__u8 *)req->hdr;
1320 assert(hdr[4] == nvme_admin_identify);
1321
1322 assert(req->data_len == 0);
1323
1324 cns = hdr[45] << 8 | hdr[44];
1325 assert(cns == NVME_IDENTIFY_CNS_SECONDARY_CTRL_LIST);
1326
1327 ctrlid = hdr[47] << 8 | hdr[46];
1328 assert(ctrlid == 5);
1329
1330 resp->data_len = sizeof(struct nvme_secondary_ctrl_list);
1331 test_transport_resp_calc_mic(resp);
1332
1333 return 0;
1334 }
1335
1336 static void test_admin_id_secondary_ctrl_list(struct nvme_mi_ep *ep)
1337 {
1338 struct nvme_secondary_ctrl_list list;
1339 nvme_mi_ctrl_t ctrl;
1340 int rc;
1341
1342 test_set_transport_callback(ep, test_admin_id_secondary_ctrl_list_cb,
1343 NULL);
1344
1345 ctrl = nvme_mi_init_ctrl(ep, 5);
1346 assert(ctrl);
1347
1348 rc = nvme_mi_admin_identify_secondary_ctrl_list(ctrl, 5, &list);
1349 assert(!rc);
1350 }
1351
1352 static int test_admin_ns_mgmt_cb(struct nvme_mi_ep *ep,
1353 struct nvme_mi_req *req,
1354 struct nvme_mi_resp *resp,
1355 void *data)
1356 {
1357 __u8 *rq_hdr, *rs_hdr, sel, csi;
1358 struct nvme_ns_mgmt_host_sw_specified *create_data;
1359 __u32 nsid;
1360
1361 rq_hdr = (__u8 *)req->hdr;
1362 assert(rq_hdr[4] == nvme_admin_ns_mgmt);
1363
1364 sel = rq_hdr[44];
1365 csi = rq_hdr[45];
1366 nsid = rq_hdr[11] << 24 | rq_hdr[10] << 16 | rq_hdr[9] << 8 | rq_hdr[8];
1367
1368 rs_hdr = (__u8 *)resp->hdr;
1369
1370 switch (sel) {
1371 case NVME_NS_MGMT_SEL_CREATE:
1372 assert(req->data_len == sizeof(struct nvme_ns_mgmt_host_sw_specified));
1373 create_data = req->data;
1374
1375 /* No NSID on created namespaces */
1376 assert(nsid == 0);
1377 assert(csi == 0);
1378
1379 /* allow operations on nsze == 42, reject others */
1380 if (le64_to_cpu(create_data->nsze) != 42) {
1381 rs_hdr[4] = 0;
1382 /* response cdw0 is created NSID */
1383 rs_hdr[8] = 0x04;
1384 rs_hdr[9] = 0x03;
1385 rs_hdr[10] = 0x02;
1386 rs_hdr[11] = 0x01;
1387 } else {
1388 rs_hdr[4] = NVME_MI_RESP_INVALID_PARAM;
1389 }
1390 break;
1391
1392 case NVME_NS_MGMT_SEL_DELETE:
1393 assert(req->data_len == 0);
1394 /* NSID required on delete */
1395 assert(nsid == 0x05060708);
1396 break;
1397
1398 default:
1399 assert(0);
1400 }
1401
1402 test_transport_resp_calc_mic(resp);
1403
1404 return 0;
1405 }
1406
1407 static void test_admin_ns_mgmt_create(struct nvme_mi_ep *ep)
1408 {
1409 struct nvme_ns_mgmt_host_sw_specified data = { 0 };
1410 nvme_mi_ctrl_t ctrl;
1411 __u32 ns;
1412 int rc;
1413
1414 test_set_transport_callback(ep, test_admin_ns_mgmt_cb, NULL);
1415
1416 ctrl = nvme_mi_init_ctrl(ep, 5);
1417 assert(ctrl);
1418
1419 rc = nvme_mi_admin_ns_mgmt_create(ctrl, NULL, 0, &ns, &data);
1420 assert(!rc);
1421 assert(ns == 0x01020304);
1422
1423 data.nsze = cpu_to_le64(42);
1424 rc = nvme_mi_admin_ns_mgmt_create(ctrl, NULL, 0, &ns, &data);
1425 assert(rc);
1426 }
1427
1428 static void test_admin_ns_mgmt_delete(struct nvme_mi_ep *ep)
1429 {
1430 nvme_mi_ctrl_t ctrl;
1431 int rc;
1432
1433 test_set_transport_callback(ep, test_admin_ns_mgmt_cb, NULL);
1434
1435 ctrl = nvme_mi_init_ctrl(ep, 5);
1436 assert(ctrl);
1437
1438 rc = nvme_mi_admin_ns_mgmt_delete(ctrl, 0x05060708);
1439 assert(!rc);
1440 }
1441
1442 struct attach_op {
1443 enum {
1444 NS_ATTACH,
1445 NS_DETACH,
1446 } op;
1447 struct nvme_ctrl_list *list;
1448 };
1449
1450 static int test_admin_ns_attach_cb(struct nvme_mi_ep *ep,
1451 struct nvme_mi_req *req,
1452 struct nvme_mi_resp *resp,
1453 void *data)
1454 {
1455 struct attach_op *op = data;
1456 __u8 *rq_hdr, sel;
1457 __u32 nsid;
1458
1459 rq_hdr = (__u8 *)req->hdr;
1460 assert(rq_hdr[4] == nvme_admin_ns_attach);
1461
1462 sel = rq_hdr[44];
1463 nsid = rq_hdr[11] << 24 | rq_hdr[10] << 16 | rq_hdr[9] << 8 | rq_hdr[8];
1464
1465 assert(req->data_len == sizeof(*op->list));
1466
1467 assert(nsid == 0x02030405);
1468 switch (op->op) {
1469 case NS_ATTACH:
1470 assert(sel == NVME_NS_ATTACH_SEL_CTRL_ATTACH);
1471 break;
1472 case NS_DETACH:
1473 assert(sel == NVME_NS_ATTACH_SEL_CTRL_DEATTACH);
1474 break;
1475 default:
1476 assert(0);
1477 }
1478
1479 assert(!memcmp(req->data, op->list, sizeof(*op->list)));
1480
1481 test_transport_resp_calc_mic(resp);
1482
1483 return 0;
1484 }
1485
1486 static void test_admin_ns_attach(struct nvme_mi_ep *ep)
1487 {
1488 struct nvme_ctrl_list list = { 0 };
1489 struct attach_op aop;
1490 nvme_mi_ctrl_t ctrl;
1491 int rc;
1492
1493 list.num = cpu_to_le16(2);
1494 list.identifier[0] = 4;
1495 list.identifier[1] = 5;
1496
1497 aop.op = NS_ATTACH;
1498 aop.list = &list;
1499
1500 test_set_transport_callback(ep, test_admin_ns_attach_cb, &aop);
1501
1502 ctrl = nvme_mi_init_ctrl(ep, 5);
1503 assert(ctrl);
1504
1505 rc = nvme_mi_admin_ns_attach_ctrls(ctrl, 0x02030405, &list);
1506 assert(!rc);
1507 }
1508
1509 static void test_admin_ns_detach(struct nvme_mi_ep *ep)
1510 {
1511 struct nvme_ctrl_list list = { 0 };
1512 struct attach_op aop;
1513 nvme_mi_ctrl_t ctrl;
1514 int rc;
1515
1516 list.num = cpu_to_le16(2);
1517 list.identifier[0] = 6;
1518 list.identifier[1] = 7;
1519
1520 aop.op = NS_DETACH;
1521 aop.list = &list;
1522
1523 test_set_transport_callback(ep, test_admin_ns_attach_cb, &aop);
1524
1525 ctrl = nvme_mi_init_ctrl(ep, 5);
1526 assert(ctrl);
1527
1528 rc = nvme_mi_admin_ns_detach_ctrls(ctrl, 0x02030405, &list);
1529 assert(!rc);
1530 }
1531
1532 struct fw_download_info {
1533 uint32_t offset;
1534 uint32_t len;
1535 void *data;
1536 };
1537
1538 static int test_admin_fw_download_cb(struct nvme_mi_ep *ep,
1539 struct nvme_mi_req *req,
1540 struct nvme_mi_resp *resp,
1541 void *data)
1542 {
1543 struct fw_download_info *info = data;
1544 __u32 len, off;
1545 __u8 *rq_hdr;
1546
1547 rq_hdr = (__u8 *)req->hdr;
1548 assert(rq_hdr[4] == nvme_admin_fw_download);
1549
1550 len = rq_hdr[47] << 24 | rq_hdr[46] << 16 | rq_hdr[45] << 8 | rq_hdr[44];
1551 off = rq_hdr[51] << 24 | rq_hdr[50] << 16 | rq_hdr[49] << 8 | rq_hdr[48];
1552
1553 assert(off << 2 == info->offset);
1554 assert(((len+1) << 2) == info->len);
1555
1556 /* ensure that the request len matches too */
1557 assert(req->data_len == info->len);
1558
1559 assert(!memcmp(req->data, info->data, len));
1560
1561 test_transport_resp_calc_mic(resp);
1562
1563 return 0;
1564 }
1565
1566 static void test_admin_fw_download(struct nvme_mi_ep *ep)
1567 {
1568 struct nvme_fw_download_args args = { 0 };
1569 struct fw_download_info info;
1570 unsigned char fw[4096];
1571 nvme_mi_ctrl_t ctrl;
1572 int rc, i;
1573
1574 for (i = 0; i < sizeof(fw); i++)
1575 fw[i] = i % 0xff;
1576
1577 info.offset = 0;
1578 info.len = 0;
1579 info.data = fw;
1580 args.data = fw;
1581 args.args_size = sizeof(args);
1582
1583 test_set_transport_callback(ep, test_admin_fw_download_cb, &info);
1584
1585 ctrl = nvme_mi_init_ctrl(ep, 5);
1586 assert(ctrl);
1587
1588 /* invalid (zero) len */
1589 args.data_len = info.len = 1;
1590 args.offset = info.offset = 0;
1591 rc = nvme_mi_admin_fw_download(ctrl, &args);
1592 assert(rc);
1593
1594 /* invalid (unaligned) len */
1595 args.data_len = info.len = 1;
1596 args.offset = info.offset = 0;
1597 rc = nvme_mi_admin_fw_download(ctrl, &args);
1598 assert(rc);
1599
1600 /* invalid offset */
1601 args.data_len = info.len = 4;
1602 args.offset = info.offset = 1;
1603 rc = nvme_mi_admin_fw_download(ctrl, &args);
1604 assert(rc);
1605
1606 /* smallest len */
1607 args.data_len = info.len = 4;
1608 args.offset = info.offset = 0;
1609 rc = nvme_mi_admin_fw_download(ctrl, &args);
1610 assert(!rc);
1611
1612 /* largest len */
1613 args.data_len = info.len = 4096;
1614 args.offset = info.offset = 0;
1615 rc = nvme_mi_admin_fw_download(ctrl, &args);
1616 assert(!rc);
1617
1618 /* offset value */
1619 args.data_len = info.len = 4096;
1620 args.offset = info.offset = 4096;
1621 rc = nvme_mi_admin_fw_download(ctrl, &args);
1622 assert(!rc);
1623 }
1624
1625 struct fw_commit_info {
1626 __u8 bpid;
1627 __u8 action;
1628 __u8 slot;
1629 };
1630
1631 static int test_admin_fw_commit_cb(struct nvme_mi_ep *ep,
1632 struct nvme_mi_req *req,
1633 struct nvme_mi_resp *resp,
1634 void *data)
1635 {
1636 struct fw_commit_info *info = data;
1637 __u8 bpid, action, slot;
1638 __u8 *rq_hdr;
1639
1640 rq_hdr = (__u8 *)req->hdr;
1641 assert(rq_hdr[4] == nvme_admin_fw_commit);
1642
1643 bpid = (rq_hdr[47] >> 7) & 0x1;
1644 slot = rq_hdr[44] & 0x7;
1645 action = (rq_hdr[44] >> 3) & 0x7;
1646
1647 assert(!!bpid == !!info->bpid);
1648 assert(slot == info->slot);
1649 assert(action == info->action);
1650
1651 test_transport_resp_calc_mic(resp);
1652
1653 return 0;
1654 }
1655
1656 static void test_admin_fw_commit(struct nvme_mi_ep *ep)
1657 {
1658 struct nvme_fw_commit_args args = { 0 };
1659 struct fw_commit_info info;
1660 nvme_mi_ctrl_t ctrl;
1661 int rc;
1662
1663 args.args_size = sizeof(args);
1664 info.bpid = args.bpid = 0;
1665
1666 test_set_transport_callback(ep, test_admin_fw_commit_cb, &info);
1667
1668 ctrl = nvme_mi_init_ctrl(ep, 5);
1669 assert(ctrl);
1670
1671 /* all zeros */
1672 info.bpid = args.bpid = 0;
1673 info.slot = args.slot = 0;
1674 info.action = args.action = 0;
1675 rc = nvme_mi_admin_fw_commit(ctrl, &args);
1676 assert(!rc);
1677
1678 /* all ones */
1679 info.bpid = args.bpid = 1;
1680 info.slot = args.slot = 0x7;
1681 info.action = args.action = 0x7;
1682 rc = nvme_mi_admin_fw_commit(ctrl, &args);
1683 assert(!rc);
1684
1685 /* correct fields */
1686 info.bpid = args.bpid = 1;
1687 info.slot = args.slot = 2;
1688 info.action = args.action = 3;
1689 rc = nvme_mi_admin_fw_commit(ctrl, &args);
1690 assert(!rc);
1691 }
1692
1693 struct format_data {
1694 __u32 nsid;
1695 __u8 lbafu;
1696 __u8 ses;
1697 __u8 pil;
1698 __u8 pi;
1699 __u8 mset;
1700 __u8 lbafl;
1701 };
1702
1703 static int test_admin_format_nvm_cb(struct nvme_mi_ep *ep,
1704 struct nvme_mi_req *req,
1705 struct nvme_mi_resp *resp,
1706 void *data)
1707 {
1708 struct nvme_format_nvm_args *args = data;
1709 __u8 *rq_hdr;
1710 __u32 nsid;
1711
1712 assert(req->data_len == 0);
1713
1714 rq_hdr = (__u8 *)req->hdr;
1715
1716 assert(rq_hdr[4] == nvme_admin_format_nvm);
1717
1718 nsid = (__u32)rq_hdr[11] << 24
1719 | rq_hdr[10] << 16
1720 | rq_hdr[9] << 8
1721 | rq_hdr[8];
1722 assert(nsid == args->nsid);
1723
1724 assert(((rq_hdr[44] >> 0) & 0xf) == args->lbaf);
1725 assert(((rq_hdr[44] >> 4) & 0x1) == args->mset);
1726 assert(((rq_hdr[44] >> 5) & 0x7) == args->pi);
1727
1728 assert(((rq_hdr[45] >> 0) & 0x1) == args->pil);
1729 assert(((rq_hdr[45] >> 1) & 0x7) == args->ses);
1730 assert(((rq_hdr[45] >> 4) & 0x3) == args->lbafu);
1731
1732 test_transport_resp_calc_mic(resp);
1733
1734 return 0;
1735 }
1736
1737 static void test_admin_format_nvm(struct nvme_mi_ep *ep)
1738 {
1739 struct nvme_format_nvm_args args = { 0 };
1740 nvme_mi_ctrl_t ctrl;
1741 int rc;
1742
1743 ctrl = nvme_mi_init_ctrl(ep, 5);
1744 assert(ctrl);
1745
1746 test_set_transport_callback(ep, test_admin_format_nvm_cb, &args);
1747
1748 /* ensure we have the cdw0 bit field encoding correct, by testing twice
1749 * with inverted bit values */
1750 args.args_size = sizeof(args);
1751 args.nsid = 0x04030201;
1752 args.lbafu = 0x3;
1753 args.ses = 0x0;
1754 args.pil = 0x1;
1755 args.pi = 0x0;
1756 args.mset = 0x1;
1757 args.lbaf = 0x0;
1758
1759 rc = nvme_mi_admin_format_nvm(ctrl, &args);
1760 assert(!rc);
1761
1762 args.nsid = ~args.nsid;
1763 args.lbafu = 0;
1764 args.ses = 0x7;
1765 args.pil = 0x0;
1766 args.pi = 0x7;
1767 args.mset = 0x0;
1768 args.lbaf = 0xf;
1769
1770 rc = nvme_mi_admin_format_nvm(ctrl, &args);
1771 assert(!rc);
1772 }
1773
1774 static int test_admin_sanitize_nvm_cb(struct nvme_mi_ep *ep,
1775 struct nvme_mi_req *req,
1776 struct nvme_mi_resp *resp,
1777 void *data)
1778 {
1779 struct nvme_sanitize_nvm_args *args = data;
1780 __u8 *rq_hdr;
1781 __u32 ovrpat;
1782
1783 assert(req->data_len == 0);
1784
1785 rq_hdr = (__u8 *)req->hdr;
1786
1787 assert(rq_hdr[4] == nvme_admin_sanitize_nvm);
1788
1789 assert(((rq_hdr[44] >> 0) & 0x7) == args->sanact);
1790 assert(((rq_hdr[44] >> 3) & 0x1) == args->ause);
1791 assert(((rq_hdr[44] >> 4) & 0xf) == args->owpass);
1792
1793 assert(((rq_hdr[45] >> 0) & 0x1) == args->oipbp);
1794 assert(((rq_hdr[45] >> 1) & 0x1) == args->nodas);
1795
1796 ovrpat = (__u32)rq_hdr[51] << 24 | rq_hdr[50] << 16 |
1797 rq_hdr[49] << 8 | rq_hdr[48];
1798 assert(ovrpat == args->ovrpat);
1799
1800 test_transport_resp_calc_mic(resp);
1801
1802 return 0;
1803 }
1804
1805 static void test_admin_sanitize_nvm(struct nvme_mi_ep *ep)
1806 {
1807 struct nvme_sanitize_nvm_args args = { 0 };
1808 nvme_mi_ctrl_t ctrl;
1809 int rc;
1810
1811 ctrl = nvme_mi_init_ctrl(ep, 5);
1812 assert(ctrl);
1813
1814 test_set_transport_callback(ep, test_admin_sanitize_nvm_cb, &args);
1815
1816 args.args_size = sizeof(args);
1817 args.sanact = 0x7;
1818 args.ause = 0x0;
1819 args.owpass = 0xf;
1820 args.oipbp = 0x0;
1821 args.nodas = 0x1;
1822 args.ovrpat = ~0x04030201;
1823
1824 rc = nvme_mi_admin_sanitize_nvm(ctrl, &args);
1825 assert(!rc);
1826
1827 args.sanact = 0x0;
1828 args.ause = 0x1;
1829 args.owpass = 0x0;
1830 args.oipbp = 0x1;
1831 args.nodas = 0x0;
1832 args.ovrpat = 0x04030201;
1833
1834 rc = nvme_mi_admin_sanitize_nvm(ctrl, &args);
1835 assert(!rc);
1836 }
1837
1838 /* test that we set the correct offset and size on get_log() calls that
1839 * are split into multiple requests */
1840 struct log_data {
1841 int n;
1842 };
1843
1844 static int test_admin_get_log_split_cb(struct nvme_mi_ep *ep,
1845 struct nvme_mi_req *req,
1846 struct nvme_mi_resp *resp,
1847 void *data)
1848 {
1849 uint32_t log_page_offset_lower;
1850 struct log_data *ldata = data;
1851 uint32_t len, off;
1852 __u8 *rq_hdr;
1853
1854 assert(req->data_len == 0);
1855
1856 rq_hdr = (__u8 *)req->hdr;
1857
1858 assert(rq_hdr[4] == nvme_admin_get_log_page);
1859
1860 /* from the MI message's DOFST/DLEN fields */
1861 off = rq_hdr[31] << 24 | rq_hdr[30] << 16 | rq_hdr[29] << 8 | rq_hdr[28];
1862 len = rq_hdr[35] << 24 | rq_hdr[34] << 16 | rq_hdr[33] << 8 | rq_hdr[32];
1863
1864 /* From the MI message's Command Dword 12 */
1865 log_page_offset_lower = rq_hdr[55] << 24 | rq_hdr[54] << 16 | rq_hdr[53] << 8 | rq_hdr[52];
1866
1867 /* we should have a full-sized start and middle, and a short end */
1868 switch (ldata->n) {
1869 case 0:
1870 assert(log_page_offset_lower == 0);
1871 assert(len == 4096);
1872 assert(off == 0);
1873 break;
1874 case 1:
1875 assert(log_page_offset_lower == 4096);
1876 assert(len == 4096);
1877 assert(off == 0);
1878 break;
1879 case 2:
1880 assert(log_page_offset_lower == 8192);
1881 assert(len == 4);
1882 assert(off == 0);
1883 break;
1884 default:
1885 assert(0);
1886 }
1887
1888 /* ensure we've sized the expected response correctly */
1889 assert(resp->data_len == len);
1890 memset(resp->data, ldata->n & 0xff, len);
1891
1892 test_transport_resp_calc_mic(resp);
1893
1894 ldata->n++;
1895
1896 return 0;
1897 }
1898
1899 static void test_admin_get_log_split(struct nvme_mi_ep *ep)
1900 {
1901 struct nvme_get_log_args args = { 0 };
1902 unsigned char buf[4096 * 2 + 4];
1903 struct log_data ldata;
1904 nvme_mi_ctrl_t ctrl;
1905 int rc;
1906
1907 ldata.n = 0;
1908 test_set_transport_callback(ep, test_admin_get_log_split_cb, &ldata);
1909
1910 ctrl = nvme_mi_init_ctrl(ep, 5);
1911
1912 args.args_size = sizeof(args);
1913 args.lid = 1;
1914 args.log = buf;
1915 args.len = sizeof(buf);
1916 args.lpo = 0;
1917 args.ot = false;
1918
1919 rc = nvme_mi_admin_get_log(ctrl, &args);
1920
1921 assert(!rc);
1922
1923 /* we should have sent three commands */
1924 assert(ldata.n == 3);
1925 }
1926
1927 static int test_endpoint_quirk_probe_cb_stage2(struct nvme_mi_ep *ep,
1928 struct nvme_mi_req *req,
1929 struct nvme_mi_resp *resp,
1930 void *data)
1931 {
1932 return test_read_mi_data_cb(ep, req, resp, data);
1933 }
1934
1935 static int test_endpoint_quirk_probe_cb_stage1(struct nvme_mi_ep *ep,
1936 struct nvme_mi_req *req,
1937 struct nvme_mi_resp *resp,
1938 void *data)
1939 {
1940 struct nvme_mi_admin_req_hdr *admin_req;
1941 __u8 ror, mt;
1942
1943 assert(req->hdr->type == NVME_MI_MSGTYPE_NVME);
1944
1945 ror = req->hdr->nmp >> 7;
1946 mt = req->hdr->nmp >> 3 & 0x7;
1947 assert(ror == NVME_MI_ROR_REQ);
1948 assert(mt == NVME_MI_MT_ADMIN);
1949
1950 assert(req->hdr_len == sizeof(struct nvme_mi_admin_req_hdr));
1951
1952 admin_req = (struct nvme_mi_admin_req_hdr *)req->hdr;
1953 assert(admin_req->opcode == nvme_admin_identify);
1954 assert(le32_to_cpu(admin_req->doff) == 0);
1955 assert(le32_to_cpu(admin_req->dlen) == offsetof(struct nvme_id_ctrl, rab));
1956
1957 test_set_transport_callback(ep, test_endpoint_quirk_probe_cb_stage2, data);
1958
1959 return 0;
1960 }
1961
1962 static void test_endpoint_quirk_probe(struct nvme_mi_ep *ep)
1963 {
1964 struct nvme_mi_read_nvm_ss_info ss_info;
1965 int rc;
1966
1967 /* force the probe to occur */
1968 ep->quirks_probed = false;
1969
1970 test_set_transport_callback(ep, test_endpoint_quirk_probe_cb_stage1, NULL);
1971
1972 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
1973 assert(rc == 0);
1974 }
1975
1976 struct req_dlen_doff_data {
1977 enum {
1978 DATA_DIR_IN,
1979 DATA_DIR_OUT,
1980 } direction;
1981 unsigned int req_len;
1982 unsigned int resp_len;
1983 unsigned int exp_doff;
1984 };
1985
1986 static int test_admin_dlen_doff_cb(struct nvme_mi_ep *ep,
1987 struct nvme_mi_req *req,
1988 struct nvme_mi_resp *resp,
1989 void *data)
1990 {
1991 struct req_dlen_doff_data *args = data;
1992 __u8 *hdr = (__u8 *)req->hdr;
1993 __u32 dlen, doff;
1994
1995 dlen = hdr[35] << 24 | hdr[34] << 16 | hdr[33] << 8 | hdr[32];
1996 doff = hdr[39] << 24 | hdr[38] << 16 | hdr[37] << 8 | hdr[36];
1997
1998 if (args->direction == DATA_DIR_OUT) {
1999 assert(dlen == args->req_len);
2000 assert(dlen == req->data_len);
2001 assert(doff == 0);
2002 } else {
2003 assert(dlen == args->resp_len);
2004 assert(dlen == resp->data_len);
2005 assert(doff == args->exp_doff);
2006 }
2007
2008 /* minimal valid response */
2009 hdr = (__u8 *)resp->hdr;
2010 hdr[4] = 0x00; /* status: success */
2011
2012 test_transport_resp_calc_mic(resp);
2013
2014 return 0;
2015 }
2016
2017 /* Check dlen value on admin_xfer requests that include data. */
2018 static void test_admin_dlen_doff_req(struct nvme_mi_ep *ep)
2019 {
2020 struct {
2021 struct nvme_mi_admin_req_hdr hdr;
2022 unsigned char data[4096];
2023 } admin_req = { 0 };
2024 struct nvme_mi_admin_resp_hdr admin_resp = { 0 };
2025 struct req_dlen_doff_data data = { 0 };
2026 size_t resp_sz = 0;
2027 nvme_mi_ctrl_t ctrl;
2028 int rc;
2029
2030 data.direction = DATA_DIR_OUT;
2031 data.req_len = sizeof(admin_req.data);
2032
2033 test_set_transport_callback(ep, test_admin_dlen_doff_cb, &data);
2034
2035 ctrl = nvme_mi_init_ctrl(ep, 0);
2036 assert(ctrl);
2037
2038 rc = nvme_mi_admin_xfer(ctrl, &admin_req.hdr, sizeof(admin_req.data),
2039 &admin_resp, 0, &resp_sz);
2040
2041 assert(!rc);
2042 };
2043
2044 /* Check dlen value on admin_xfer requests that return data in their response.
2045 */
2046 static void test_admin_dlen_doff_resp(struct nvme_mi_ep *ep)
2047 {
2048 struct {
2049 struct nvme_mi_admin_resp_hdr hdr;
2050 unsigned char data[4096];
2051 } admin_resp = { 0 };
2052 struct nvme_mi_admin_req_hdr admin_req = { 0 };
2053 struct req_dlen_doff_data data = { 0 };
2054 nvme_mi_ctrl_t ctrl;
2055 size_t resp_sz;
2056 int rc;
2057
2058 data.direction = DATA_DIR_IN;
2059 data.resp_len = sizeof(admin_resp.data);
2060 resp_sz = sizeof(admin_resp.data);
2061
2062 test_set_transport_callback(ep, test_admin_dlen_doff_cb, &data);
2063
2064 ctrl = nvme_mi_init_ctrl(ep, 0);
2065 assert(ctrl);
2066
2067 rc = nvme_mi_admin_xfer(ctrl, &admin_req, 0, &admin_resp.hdr, 0,
2068 &resp_sz);
2069
2070 assert(!rc);
2071 };
2072
2073 #define DEFINE_TEST(name) { #name, test_ ## name }
2074 struct test {
2075 const char *name;
2076 void (*fn)(nvme_mi_ep_t);
2077 } tests[] = {
2078 DEFINE_TEST(endpoint_lifetime),
2079 DEFINE_TEST(ctrl_lifetime),
2080 DEFINE_TEST(read_mi_data),
2081 DEFINE_TEST(transport_fail),
2082 DEFINE_TEST(transport_describe),
2083 DEFINE_TEST(scan_ctrl_list),
2084 DEFINE_TEST(invalid_crc),
2085 DEFINE_TEST(admin_id),
2086 DEFINE_TEST(admin_err_mi_resp),
2087 DEFINE_TEST(admin_err_nvme_resp),
2088 DEFINE_TEST(admin_invalid_formats),
2089 DEFINE_TEST(resp_req),
2090 DEFINE_TEST(resp_hdr_small),
2091 DEFINE_TEST(resp_invalid_type),
2092 DEFINE_TEST(resp_csi_request),
2093 DEFINE_TEST(resp_csi_mismatch),
2094 DEFINE_TEST(mi_config_get_mtu),
2095 DEFINE_TEST(mi_config_set_freq),
2096 DEFINE_TEST(mi_config_set_freq_invalid),
2097 DEFINE_TEST(get_features_nodata),
2098 DEFINE_TEST(get_features_data),
2099 DEFINE_TEST(set_features),
2100 DEFINE_TEST(admin_id_alloc_ns_list),
2101 DEFINE_TEST(admin_id_active_ns_list),
2102 DEFINE_TEST(admin_id_alloc_ns),
2103 DEFINE_TEST(admin_id_active_ns),
2104 DEFINE_TEST(admin_id_nsid_ctrl_list),
2105 DEFINE_TEST(admin_id_secondary_ctrl_list),
2106 DEFINE_TEST(admin_ns_mgmt_create),
2107 DEFINE_TEST(admin_ns_mgmt_delete),
2108 DEFINE_TEST(admin_ns_attach),
2109 DEFINE_TEST(admin_ns_detach),
2110 DEFINE_TEST(admin_fw_download),
2111 DEFINE_TEST(admin_fw_commit),
2112 DEFINE_TEST(admin_format_nvm),
2113 DEFINE_TEST(admin_sanitize_nvm),
2114 DEFINE_TEST(admin_get_log_split),
2115 DEFINE_TEST(endpoint_quirk_probe),
2116 DEFINE_TEST(admin_dlen_doff_req),
2117 DEFINE_TEST(admin_dlen_doff_resp),
2118 DEFINE_TEST(mi_invalid_formats),
2119 };
2120
2121 static void run_test(struct test *test, FILE *logfd, nvme_mi_ep_t ep)
2122 {
2123 printf("Running test %s...", test->name);
2124 fflush(stdout);
2125 test->fn(ep);
2126 /* tests will assert on failure; if we're here, we're OK */
2127 printf(" OK\n");
2128 test_print_log_buf(logfd);
2129 }
2130
2131 int main(void)
2132 {
2133 nvme_root_t root;
2134 nvme_mi_ep_t ep;
2135 unsigned int i;
2136 FILE *fd;
2137
2138 fd = test_setup_log();
2139
2140 root = nvme_mi_create_root(fd, DEFAULT_LOGLEVEL);
2141 assert(root);
2142
2143 ep = nvme_mi_open_test(root);
2144 assert(ep);
2145
2146 for (i = 0; i < ARRAY_SIZE(tests); i++) {
2147 run_test(&tests[i], fd, ep);
2148 }
2149
2150 nvme_mi_close(ep);
2151 nvme_mi_free_root(root);
2152
2153 test_close_log(fd);
2154
2155 return EXIT_SUCCESS;
2156 }