Debian Codegraph @

test/mi-mctp.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 <errno.h>
10 #include <fcntl.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <sys/socket.h>
16
17
18 #include <ccan/array_size/array_size.h>
19 #include <ccan/endian/endian.h>
20
21 #include "libnvme-mi.h"
22 #include "nvme/private.h"
23 #include "utils.h"
24
25 #if HAVE_LINUX_MCTP_H
26 #include <linux/mctp.h>
27 #else
28 #include "nvme/mi-mctp-compat.h"
29 #endif
30
31 /* 4096 byte max MCTP message, plus space for header data */
32 #define MAX_BUFSIZ 8192
33
34 struct test_peer;
35
36 typedef int (*rx_test_fn)(struct test_peer *peer, void *buf, size_t len, int sd);
37 typedef int (*poll_test_fn)(struct test_peer *peer,
38 struct pollfd *fds, nfds_t nfds, int timeout);
39
40 #define TEST_PEER_SD_COMMANDS_IDX (0)
41 #define TEST_PEER_SD_AEMS_IDX (1)
42
43 /* Our fake MCTP "peer".
44 *
45 * The terms TX (transmit) and RX (receive) are from the perspective of
46 * the NVMe device. TX is device-to-libnvme, RX is libnvme-to-device.
47 *
48 * The RX and TX buffers are linear versions of the data sent and received by
49 * libnvme-mi, and *include* the MCTP message type byte (even though it's
50 * omitted in the sendmsg/recvmsg interface), so that the buffer inspection
51 * in the tests can exactly match the NVMe-MI spec packet diagrams.
52 */
53 static struct test_peer {
54 /* rx (sendmsg) data sent from libnvme, and return value */
55 unsigned char rx_buf[MAX_BUFSIZ];
56 size_t rx_buf_len;
57 ssize_t rx_rc; /* if zero, return the sendmsg len */
58 int rx_errno;
59
60 /* tx (recvmsg) data to be received by libnvme and return value */
61 unsigned char tx_buf[MAX_BUFSIZ];
62 size_t tx_buf_len;
63 ssize_t tx_rc; /* if zero, return the recvmsg len */
64 int tx_errno;
65
66 /* Optional, called before TX, may set tx_buf according to request.
67 * Return value stored in tx_res, may be used by test */
68 rx_test_fn tx_fn;
69 void *tx_data;
70 int tx_fn_res;
71
72 poll_test_fn poll_fn;
73 void *poll_data;
74
75 /* store sd from socket() setup */
76 int sd[2];
77 } test_peer;
78
79 /* ensure tests start from a standard state */
80 void reset_test_peer(void)
81 {
82 int temp_sd[2] = {test_peer.sd[TEST_PEER_SD_COMMANDS_IDX],
83 test_peer.sd[TEST_PEER_SD_AEMS_IDX]};
84
85 memset(&test_peer, 0, sizeof(test_peer));
86 test_peer.tx_buf[0] = NVME_MI_MSGTYPE_NVME;
87 test_peer.rx_buf[0] = NVME_MI_MSGTYPE_NVME;
88 memcpy(test_peer.sd, temp_sd, 2*sizeof(*temp_sd));
89 }
90
91 /* calculate MIC of peer-to-libnvme data, expand buf by 4 bytes and insert
92 * the new MIC */
93 static void test_set_tx_mic(struct test_peer *peer)
94 {
95 extern __u32 nvme_mi_crc32_update(__u32 crc, void *data, size_t len);
96 __u32 crc = 0xffffffff;
97 __le32 crc_le;
98
99 assert(peer->tx_buf_len + sizeof(crc_le) <= MAX_BUFSIZ);
100
101 crc = nvme_mi_crc32_update(crc, peer->tx_buf, peer->tx_buf_len);
102 crc_le = cpu_to_le32(~crc);
103 memcpy(peer->tx_buf + peer->tx_buf_len, &crc_le, sizeof(crc_le));
104 peer->tx_buf_len += sizeof(crc_le);
105 }
106
107 int __wrap_msg_socket(void)
108 {
109 /* we do an open here to give the mi-mctp code something to close() */
110 test_peer.sd[TEST_PEER_SD_COMMANDS_IDX] = open("/dev/null", 0);
111 return test_peer.sd[TEST_PEER_SD_COMMANDS_IDX];
112 }
113
114 int __wrap_aem_socket(__u8 eid, unsigned int network)
115 {
116 /* we do an open here to give the mi-mctp code something to close() */
117 test_peer.sd[TEST_PEER_SD_AEMS_IDX] = open("/dev/null", 0);
118 return test_peer.sd[TEST_PEER_SD_AEMS_IDX];
119 }
120
121 ssize_t __wrap_sendmsg(int sd, const struct msghdr *hdr, int flags)
122 {
123 size_t i, pos;
124
125 assert(sd == test_peer.sd[TEST_PEER_SD_COMMANDS_IDX]);
126
127 test_peer.rx_buf[0] = NVME_MI_MSGTYPE_NVME;
128
129 /* gather iovec into buf */
130 for (i = 0, pos = 1; i < hdr->msg_iovlen; i++) {
131 struct iovec *iov = &hdr->msg_iov[i];
132
133 assert(pos + iov->iov_len < MAX_BUFSIZ - 1);
134 memcpy(test_peer.rx_buf + pos, iov->iov_base, iov->iov_len);
135 pos += iov->iov_len;
136 }
137
138 test_peer.rx_buf_len = pos;
139
140 errno = test_peer.rx_errno;
141
142 return test_peer.rx_rc ?: (pos - 1);
143 }
144
145 ssize_t __wrap_recvmsg(int sd, struct msghdr *hdr, int flags)
146 {
147 size_t i, pos, len;
148
149 assert(sd == test_peer.sd[TEST_PEER_SD_COMMANDS_IDX] ||
150 sd == test_peer.sd[TEST_PEER_SD_AEMS_IDX]);
151
152 //Check for purge case
153 if (flags & MSG_TRUNC)
154 return 0;
155
156 if (test_peer.tx_fn) {
157 test_peer.tx_fn_res = test_peer.tx_fn(&test_peer,
158 test_peer.rx_buf,
159 test_peer.rx_buf_len,
160 sd);
161 } else {
162 if (sd == test_peer.sd[TEST_PEER_SD_COMMANDS_IDX] && test_peer.tx_buf_len == 0) {
163 errno = EAGAIN;
164 return -1;
165 }
166 /* set up a few default response fields; caller may have
167 * initialised the rest of the response */
168 test_peer.tx_buf[0] = NVME_MI_MSGTYPE_NVME;
169 test_peer.tx_buf[1] = test_peer.rx_buf[1] | (NVME_MI_ROR_RSP << 7);
170 test_set_tx_mic(&test_peer);
171 }
172
173 /* scatter buf into iovec */
174 for (i = 0, pos = 1; i < hdr->msg_iovlen && pos < test_peer.tx_buf_len;
175 i++) {
176 struct iovec *iov = &hdr->msg_iov[i];
177
178 len = iov->iov_len;
179 if (len > test_peer.tx_buf_len - pos)
180 len = test_peer.tx_buf_len - pos;
181
182 memcpy(iov->iov_base, test_peer.tx_buf + pos, len);
183 pos += len;
184 }
185
186 errno = test_peer.tx_errno;
187
188 test_peer.tx_buf_len = 0; //Clear since this is sent
189 return test_peer.tx_rc ?: (pos - 1);
190 }
191
192 int __wrap_poll(struct pollfd *fds, nfds_t nfds, int timeout)
193 {
194 if (!test_peer.poll_fn)
195 return 1;
196
197 return test_peer.poll_fn(&test_peer, fds, nfds, timeout);
198 }
199
200 struct mctp_ioc_tag_ctl;
201
202 #ifdef SIOCMCTPALLOCTAG
203 int test_ioctl_tag(int sd, unsigned long req, struct mctp_ioc_tag_ctl *ctl)
204 {
205 assert(sd == test_peer.sd[TEST_PEER_SD_COMMANDS_IDX]);
206
207 switch (req) {
208 case SIOCMCTPALLOCTAG:
209 ctl->tag = 1 | MCTP_TAG_PREALLOC | MCTP_TAG_OWNER;
210 break;
211 case SIOCMCTPDROPTAG:
212 assert(ctl->tag == (1 | MCTP_TAG_PREALLOC | MCTP_TAG_OWNER));
213 break;
214 };
215
216 return 0;
217 }
218 #else
219 int test_ioctl_tag(int sd, unsigned long req, struct mctp_ioc_tag_ctl *ctl)
220 {
221 assert(sd == test_peer.sd[TEST_PEER_SD_COMMANDS_IDX]);
222 return 0;
223 }
224 #endif
225
226 static struct __mi_mctp_socket_ops ops = {
227 __wrap_msg_socket,
228 __wrap_aem_socket,
229 __wrap_sendmsg,
230 __wrap_recvmsg,
231 __wrap_poll,
232 test_ioctl_tag,
233 };
234
235 /* tests */
236 static void test_rx_err(nvme_mi_ep_t ep, struct test_peer *peer)
237 {
238 struct nvme_mi_read_nvm_ss_info ss_info;
239 int rc;
240
241 peer->rx_rc = -1;
242
243 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
244 assert(rc != 0);
245 }
246
247 static int tx_none(struct test_peer *peer, void *buf, size_t len, int sd)
248 {
249 return 0;
250 }
251
252 static void test_tx_none(nvme_mi_ep_t ep, struct test_peer *peer)
253 {
254 struct nvme_mi_read_nvm_ss_info ss_info;
255 int rc;
256
257 peer->tx_buf_len = 0;
258 peer->tx_fn = tx_none;
259
260 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
261 assert(rc != 0);
262 }
263
264 static void test_tx_err(nvme_mi_ep_t ep, struct test_peer *peer)
265 {
266 struct nvme_mi_read_nvm_ss_info ss_info;
267 int rc;
268
269 peer->tx_rc = -1;
270
271 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
272 assert(rc != 0);
273 }
274
275 static void test_tx_short(nvme_mi_ep_t ep, struct test_peer *peer)
276 {
277 struct nvme_mi_read_nvm_ss_info ss_info;
278 int rc;
279
280 peer->tx_buf_len = 11;
281
282 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
283 assert(rc != 0);
284 }
285
286 static int poll_fn_err(struct test_peer *peer, struct pollfd *fds,
287 nfds_t nfds, int timeout)
288 {
289 return -1;
290 }
291
292 static void test_poll_err(nvme_mi_ep_t ep, struct test_peer *peer)
293 {
294 struct nvme_mi_read_nvm_ss_info ss_info;
295 int rc;
296
297 peer->poll_fn = poll_fn_err;
298
299 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
300 assert(rc != 0);
301 }
302
303 static void test_read_mi_data(nvme_mi_ep_t ep, struct test_peer *peer)
304 {
305 struct nvme_mi_read_nvm_ss_info ss_info;
306 int rc;
307
308 /* empty response data */
309 peer->tx_buf_len = 8 + 32;
310
311 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
312 assert(rc == 0);
313 }
314
315 static void test_mi_resp_err(nvme_mi_ep_t ep, struct test_peer *peer)
316 {
317 struct nvme_mi_read_nvm_ss_info ss_info;
318 int rc;
319
320 /* simple error response */
321 peer->tx_buf[4] = 0x02; /* internal error */
322 peer->tx_buf_len = 8;
323
324 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
325 assert(rc == 0x2);
326 }
327
328 static void setup_unaligned_ctrl_list_resp(struct test_peer *peer)
329 {
330 /* even number of controllers */
331 peer->tx_buf[8] = 0x02;
332 peer->tx_buf[9] = 0x00;
333
334 /* controller ID 1 */
335 peer->tx_buf[10] = 0x01;
336 peer->tx_buf[11] = 0x00;
337
338 /* controller ID 2 */
339 peer->tx_buf[12] = 0x02;
340 peer->tx_buf[13] = 0x00;
341
342 peer->tx_buf_len = 14;
343 }
344
345 /* Will call through the xfer/submit API expecting a full-sized list (so
346 * resp->data_len is set to sizeof(list)), but the endpoint will return an
347 * unaligned short list.
348 */
349 static void test_mi_resp_unaligned(nvme_mi_ep_t ep, struct test_peer *peer)
350 {
351 struct nvme_ctrl_list list;
352 int rc;
353
354 setup_unaligned_ctrl_list_resp(peer);
355
356 memset(&list, 0, sizeof(list));
357
358 rc = nvme_mi_mi_read_mi_data_ctrl_list(ep, 0, &list);
359 assert(rc == 0);
360
361 assert(le16_to_cpu(list.num) == 2);
362 assert(le16_to_cpu(list.identifier[0]) == 1);
363 assert(le16_to_cpu(list.identifier[1]) == 2);
364 }
365
366 /* Will call through the xfer/submit API expecting an unaligned list,
367 * and get a response of exactly that size.
368 */
369 static void test_mi_resp_unaligned_expected(nvme_mi_ep_t ep,
370 struct test_peer *peer)
371 {
372 /* direct access to the raw submit() API */
373 extern int nvme_mi_submit(nvme_mi_ep_t ep, struct nvme_mi_req *req,
374 struct nvme_mi_resp *resp);
375 struct nvme_mi_mi_resp_hdr resp_hdr;
376 struct nvme_mi_mi_req_hdr req_hdr;
377 struct nvme_ctrl_list list;
378 struct nvme_mi_resp resp;
379 struct nvme_mi_req req;
380 int rc;
381
382 setup_unaligned_ctrl_list_resp(peer);
383
384 memset(&list, 0, sizeof(list));
385
386 memset(&req_hdr, 0, sizeof(req_hdr));
387 req_hdr.hdr.type = NVME_MI_MSGTYPE_NVME;
388 req_hdr.hdr.nmp = (NVME_MI_ROR_REQ << 7) | (NVME_MI_MT_MI << 3);
389 req_hdr.opcode = nvme_mi_mi_opcode_mi_data_read;
390 req_hdr.cdw0 = cpu_to_le32(nvme_mi_dtyp_ctrl_list << 24);
391
392 memset(&req, 0, sizeof(req));
393 req.hdr = &req_hdr.hdr;
394 req.hdr_len = sizeof(req_hdr);
395
396 memset(&resp, 0, sizeof(resp));
397 resp.hdr = &resp_hdr.hdr;
398 resp.hdr_len = sizeof(resp_hdr);
399 resp.data = &list;
400 resp.data_len = peer->tx_buf_len;
401
402 rc = nvme_mi_submit(ep, &req, &resp);
403 assert(rc == 0);
404 assert(resp.data_len == 6); /* 2-byte length, 2*2 byte controller IDs */
405
406 assert(le16_to_cpu(list.num) == 2);
407 assert(le16_to_cpu(list.identifier[0]) == 1);
408 assert(le16_to_cpu(list.identifier[1]) == 2);
409 }
410
411 static void test_admin_resp_err(nvme_mi_ep_t ep, struct test_peer *peer)
412 {
413 struct nvme_id_ctrl id;
414 nvme_mi_ctrl_t ctrl;
415 int rc;
416
417 ctrl = nvme_mi_init_ctrl(ep, 1);
418 assert(ctrl);
419
420 /* Simple error response, will be shorter than the expected Admin
421 * command response header. */
422 peer->tx_buf[4] = 0x02; /* internal error */
423 peer->tx_buf_len = 8;
424
425 rc = nvme_mi_admin_identify_ctrl(ctrl, &id);
426 assert(nvme_status_get_type(rc) == NVME_STATUS_TYPE_MI);
427 assert(nvme_status_get_value(rc) == NVME_MI_RESP_INTERNAL_ERR);
428 }
429
430 /* test: all 4-byte aligned response sizes - should be decoded into the
431 * response status value. We use an admin command here as the header size will
432 * be larger than the minimum header size (it contains the completion
433 * doublewords), and we need to ensure that an error response is correctly
434 * interpreted, including having the MIC extracted from the message.
435 */
436 static void test_admin_resp_sizes(nvme_mi_ep_t ep, struct test_peer *peer)
437 {
438 struct nvme_id_ctrl id;
439 nvme_mi_ctrl_t ctrl;
440 unsigned int i;
441 int rc;
442
443 ctrl = nvme_mi_init_ctrl(ep, 1);
444 assert(ctrl);
445
446 peer->tx_buf[4] = 0x02; /* internal error */
447
448 for (i = 8; i <= 4096 + 8; i+=4) {
449 peer->tx_buf_len = i;
450 rc = nvme_mi_admin_identify_ctrl(ctrl, &id);
451 assert(nvme_status_get_type(rc) == NVME_STATUS_TYPE_MI);
452 assert(nvme_status_get_value(rc) == NVME_MI_RESP_INTERNAL_ERR);
453 }
454
455 nvme_mi_close_ctrl(ctrl);
456 }
457
458 /* test: timeout value passed to poll */
459 static int poll_fn_timeout_value(struct test_peer *peer, struct pollfd *fds,
460 nfds_t nfds, int timeout)
461 {
462 assert(timeout == 3141);
463 return 1;
464 }
465
466 static void test_poll_timeout_value(nvme_mi_ep_t ep, struct test_peer *peer)
467 {
468 struct nvme_mi_read_nvm_ss_info ss_info;
469 int rc;
470
471 /* empty response data */
472 peer->tx_buf_len = 8 + 32;
473
474 peer->poll_fn = poll_fn_timeout_value;
475 nvme_mi_ep_set_timeout(ep, 3141);
476
477 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
478 assert(rc == 0);
479 }
480
481 /* test: poll timeout expiry */
482 static int poll_fn_timeout(struct test_peer *peer, struct pollfd *fds,
483 nfds_t nfds, int timeout)
484 {
485 return 0;
486 }
487
488 static void test_poll_timeout(nvme_mi_ep_t ep, struct test_peer *peer)
489 {
490 struct nvme_mi_read_nvm_ss_info ss_info;
491 int rc;
492
493 peer->poll_fn = poll_fn_timeout;
494
495 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
496 assert(rc != 0);
497 assert(errno == ETIMEDOUT);
498 }
499
500 /* test: send a More Processing Required response, then the actual response */
501 struct mpr_tx_info {
502 int msg_no;
503 bool admin_quirk;
504 size_t final_len;
505 };
506
507 static int tx_mpr(struct test_peer *peer, void *buf, size_t len, int sd)
508 {
509 struct mpr_tx_info *tx_info = peer->tx_data;
510
511 assert(sd == peer->sd[TEST_PEER_SD_COMMANDS_IDX]);
512
513 memset(peer->tx_buf, 0, sizeof(peer->tx_buf));
514 peer->tx_buf[0] = NVME_MI_MSGTYPE_NVME;
515 peer->tx_buf[1] = test_peer.rx_buf[1] | (NVME_MI_ROR_RSP << 7);
516
517 switch (tx_info->msg_no) {
518 case 1:
519 peer->tx_buf[4] = NVME_MI_RESP_MPR;
520 peer->tx_buf_len = 8;
521 if (tx_info->admin_quirk) {
522 peer->tx_buf_len = 20;
523 }
524 break;
525 case 2:
526 peer->tx_buf[4] = NVME_MI_RESP_SUCCESS;
527 peer->tx_buf_len = tx_info->final_len;
528 break;
529 default:
530 assert(0);
531 }
532
533 test_set_tx_mic(peer);
534
535 tx_info->msg_no++;
536
537 return 0;
538 }
539
540 static void test_mpr_mi(nvme_mi_ep_t ep, struct test_peer *peer)
541 {
542 struct nvme_mi_read_nvm_ss_info ss_info;
543 struct mpr_tx_info tx_info;
544 int rc;
545
546 tx_info.msg_no = 1;
547 tx_info.final_len = sizeof(struct nvme_mi_mi_resp_hdr) + sizeof(ss_info);
548 tx_info.admin_quirk = false;
549
550 peer->tx_fn = tx_mpr;
551 peer->tx_data = &tx_info;
552
553 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
554 assert(rc == 0);
555 }
556
557 static void test_mpr_admin(nvme_mi_ep_t ep, struct test_peer *peer)
558 {
559 struct mpr_tx_info tx_info;
560 struct nvme_id_ctrl id;
561 nvme_mi_ctrl_t ctrl;
562 int rc;
563
564 tx_info.msg_no = 1;
565 tx_info.final_len = sizeof(struct nvme_mi_admin_resp_hdr) + sizeof(id);
566 tx_info.admin_quirk = false;
567
568 peer->tx_fn = tx_mpr;
569 peer->tx_data = &tx_info;
570
571 ctrl = nvme_mi_init_ctrl(ep, 1);
572
573 rc = nvme_mi_admin_identify_ctrl(ctrl, &id);
574 assert(rc == 0);
575
576 nvme_mi_close_ctrl(ctrl);
577 }
578
579 /* We have seen drives that send a MPR response as a full Admin message,
580 * rather than a MI message; these have a larger message body
581 */
582 static void test_mpr_admin_quirked(nvme_mi_ep_t ep, struct test_peer *peer)
583 {
584 struct mpr_tx_info tx_info;
585 struct nvme_id_ctrl id;
586 nvme_mi_ctrl_t ctrl;
587 int rc;
588
589 tx_info.msg_no = 1;
590 tx_info.final_len = sizeof(struct nvme_mi_admin_resp_hdr) + sizeof(id);
591 tx_info.admin_quirk = true;
592
593 peer->tx_fn = tx_mpr;
594 peer->tx_data = &tx_info;
595
596 ctrl = nvme_mi_init_ctrl(ep, 1);
597
598 rc = nvme_mi_admin_identify_ctrl(ctrl, &id);
599 assert(rc == 0);
600
601 nvme_mi_close_ctrl(ctrl);
602 }
603
604 /* helpers for the MPR + poll tests */
605 struct mpr_poll_info {
606 int poll_no;
607 uint16_t mprt;
608 unsigned int timeouts[2];
609 };
610
611 static int poll_fn_mpr_poll(struct test_peer *peer, struct pollfd *fds,
612 nfds_t nfds, int timeout)
613 {
614 struct mpr_poll_info *info = peer->poll_data;
615
616 switch (info->poll_no) {
617 case 1:
618 case 2:
619 assert(timeout == info->timeouts[info->poll_no - 1]);
620 break;
621 default:
622 assert(0);
623 }
624
625 info->poll_no++;
626 return 1;
627 }
628
629 static int tx_fn_mpr_poll(struct test_peer *peer, void *buf, size_t len, int sd)
630 {
631 struct mpr_tx_info *tx_info = peer->tx_data;
632 struct mpr_poll_info *poll_info = peer->poll_data;
633 unsigned int mprt;
634
635 assert(sd == peer->sd[TEST_PEER_SD_COMMANDS_IDX]);
636
637 memset(peer->tx_buf, 0, sizeof(peer->tx_buf));
638 peer->tx_buf[0] = NVME_MI_MSGTYPE_NVME;
639 peer->tx_buf[1] = test_peer.rx_buf[1] | (NVME_MI_ROR_RSP << 7);
640
641 switch (tx_info->msg_no) {
642 case 1:
643 peer->tx_buf[4] = NVME_MI_RESP_MPR;
644 peer->tx_buf_len = 8;
645 mprt = poll_info->mprt;
646 peer->tx_buf[7] = mprt >> 8;
647 peer->tx_buf[6] = mprt & 0xff;
648 break;
649 case 2:
650 peer->tx_buf[4] = NVME_MI_RESP_SUCCESS;
651 peer->tx_buf_len = tx_info->final_len;
652 break;
653 default:
654 assert(0);
655 }
656
657 test_set_tx_mic(peer);
658
659 tx_info->msg_no++;
660
661 return 0;
662 }
663
664 /* test: correct timeout value used from MPR response */
665 static void test_mpr_timeouts(nvme_mi_ep_t ep, struct test_peer *peer)
666 {
667 struct nvme_mi_read_nvm_ss_info ss_info;
668 struct mpr_poll_info poll_info;
669 struct mpr_tx_info tx_info;
670 int rc;
671
672 nvme_mi_ep_set_timeout(ep, 3141);
673
674 tx_info.msg_no = 1;
675 tx_info.final_len = sizeof(struct nvme_mi_mi_resp_hdr) + sizeof(ss_info);
676
677 poll_info.poll_no = 1;
678 poll_info.mprt = 1234;
679 poll_info.timeouts[0] = 3141;
680 poll_info.timeouts[1] = 1234 * 100;
681
682 peer->tx_fn = tx_fn_mpr_poll;
683 peer->tx_data = &tx_info;
684
685 peer->poll_fn = poll_fn_mpr_poll;
686 peer->poll_data = &poll_info;
687
688 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
689 assert(rc == 0);
690 }
691
692 /* test: MPR value is limited to the max mpr */
693 static void test_mpr_timeout_clamp(nvme_mi_ep_t ep, struct test_peer *peer)
694 {
695 struct nvme_mi_read_nvm_ss_info ss_info;
696 struct mpr_poll_info poll_info;
697 struct mpr_tx_info tx_info;
698 int rc;
699
700 nvme_mi_ep_set_timeout(ep, 3141);
701 nvme_mi_ep_set_mprt_max(ep, 123400);
702
703 tx_info.msg_no = 1;
704 tx_info.final_len = sizeof(struct nvme_mi_mi_resp_hdr) + sizeof(ss_info);
705
706 poll_info.poll_no = 1;
707 poll_info.mprt = 1235;
708 poll_info.timeouts[0] = 3141;
709 poll_info.timeouts[1] = 1234 * 100;
710
711 peer->tx_fn = tx_fn_mpr_poll;
712 peer->tx_data = &tx_info;
713
714 peer->poll_fn = poll_fn_mpr_poll;
715 peer->poll_data = &poll_info;
716
717 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
718 assert(rc == 0);
719 }
720
721 /* test: MPR value of zero doesn't result in poll with zero timeout */
722 static void test_mpr_mprt_zero(nvme_mi_ep_t ep, struct test_peer *peer)
723 {
724 struct nvme_mi_read_nvm_ss_info ss_info;
725 struct mpr_poll_info poll_info;
726 struct mpr_tx_info tx_info;
727 int rc;
728
729 nvme_mi_ep_set_timeout(ep, 3141);
730 nvme_mi_ep_set_mprt_max(ep, 123400);
731
732 tx_info.msg_no = 1;
733 tx_info.final_len = sizeof(struct nvme_mi_mi_resp_hdr) + sizeof(ss_info);
734
735 poll_info.poll_no = 1;
736 poll_info.mprt = 0;
737 poll_info.timeouts[0] = 3141;
738 poll_info.timeouts[1] = 3141;
739
740 peer->tx_fn = tx_fn_mpr_poll;
741 peer->tx_data = &tx_info;
742
743 peer->poll_fn = poll_fn_mpr_poll;
744 peer->poll_data = &poll_info;
745
746 rc = nvme_mi_mi_read_mi_data_subsys(ep, &ss_info);
747 assert(rc == 0);
748 }
749
750 enum aem_enable_state {
751 AEM_ES_GET_ENABLED,
752 AEM_ES_SET_TO_DISABLED,
753 AEM_ES_ENABLE_SET_ENABLED,
754 AEM_ES_PROCESS,
755 AEM_ES_ACK_RESPONSE,
756 AEM_ES_ACK_RECEIVED
757 };
758
759 enum aem_failure_condition {
760 AEM_FC_NONE,
761 AEM_FC_BAD_GET_CONFIG_HEADER_LEN,
762 AEM_FC_BAD_GET_CONFIG_TOTAL_LEN,
763 AEM_FC_BAD_GET_CONFIG_BUFFER_LEN,
764 AEM_FC_BAD_OCC_RSP_HDR_LEN_SYNC,
765 AEM_FC_BAD_OCC_RSP_TOTAL_LEN_SYNC,
766 AEM_FC_BAD_OCC_RSP_BUFFER_LEN_SYNC,
767 AEM_FC_BAD_OCC_RSP_HDR_LEN_AEM,
768 AEM_FC_BAD_OCC_RSP_TOTAL_LEN_AEM,
769 AEM_FC_BAD_OCC_RSP_BUFFER_LEN_AEM,
770 };
771
772 struct aem_rcv_enable_fn_data {
773 enum aem_enable_state state;
774 enum aem_failure_condition fc;
775 struct nvme_mi_aem_enabled_map ep_enabled_map;
776 struct nvme_mi_aem_enabled_map host_enabled_map;
777 struct nvme_mi_aem_enabled_map aem_during_process_map;
778 struct nvme_mi_aem_enabled_map ack_events_map;
779 struct nvme_mi_event *events[256];
780 int callback_count;
781 };
782
783 static void populate_tx_occ_list(bool aem_not_ack,
784 struct aem_rcv_enable_fn_data *fn_data, struct nvme_mi_aem_enabled_map *to_send)
785 {
786 struct nvme_mi_mi_resp_hdr *resp_hdr =
787 (struct nvme_mi_mi_resp_hdr *)test_peer.tx_buf;
788
789 struct nvme_mi_msg_hdr *mi_msg_hdr =
790 (struct nvme_mi_msg_hdr *)test_peer.tx_buf;
791
792 size_t hdr_len = sizeof(*resp_hdr);
793
794 struct nvme_mi_aem_occ_list_hdr *list_hdr =
795 (struct nvme_mi_aem_occ_list_hdr *)(resp_hdr+1);
796
797 //For AEM, the data is actually in request format
798 //since it originates from the endpoint
799 if (aem_not_ack) {
800 list_hdr = (struct nvme_mi_aem_occ_list_hdr *)(mi_msg_hdr+1);
801 hdr_len = sizeof(*mi_msg_hdr);
802 mi_msg_hdr->nmp = (NVME_MI_MT_AE << 3);
803 } else {
804 resp_hdr->status = 0;
805 }
806
807 list_hdr->aelver = 0;
808 list_hdr->aeolhl = sizeof(*list_hdr);
809 list_hdr->numaeo = 0;
810 __u32 aeoltl = list_hdr->aeolhl;
811
812 struct nvme_mi_aem_occ_data *data =
813 (struct nvme_mi_aem_occ_data *)(list_hdr+1);
814
815 for (int i = 0; i < 255; i++) {
816 if (fn_data->events[i] && to_send->enabled[i]) {
817 struct nvme_mi_event *event = fn_data->events[i];
818
819 list_hdr->numaeo++;
820 aeoltl += sizeof(struct nvme_mi_aem_occ_data);
821 aeoltl += event->spec_info_len +
822 event->vend_spec_info_len;
823
824 data->aelhlen = sizeof(*data);
825
826 if ((fn_data->fc == AEM_FC_BAD_OCC_RSP_HDR_LEN_SYNC && !aem_not_ack) ||
827 (fn_data->fc == AEM_FC_BAD_OCC_RSP_HDR_LEN_AEM && aem_not_ack))
828 data->aelhlen--;
829
830 data->aeoui.aeocidi = event->aeocidi;
831 data->aeoui.aeoi = event->aeoi;
832 data->aeoui.aessi = event->aessi;
833 data->aeosil = event->spec_info_len;
834 data->aeovsil = event->vend_spec_info_len;
835
836 if ((fn_data->fc == AEM_FC_BAD_OCC_RSP_TOTAL_LEN_SYNC &&
837 !aem_not_ack) ||
838 (fn_data->fc == AEM_FC_BAD_OCC_RSP_TOTAL_LEN_AEM &&
839 aem_not_ack))
840 aeoltl -= 1;
841
842 //Now the data
843 uint8_t *spec = (uint8_t *)(data+1);
844
845 if (data->aeosil) {
846 memcpy(spec, event->spec_info, event->spec_info_len);
847 spec += event->spec_info_len;
848 }
849
850 if (data->aeovsil) {
851 memcpy(spec, event->vend_spec_info, event->vend_spec_info_len);
852 spec += event->vend_spec_info_len;
853 }
854
855 data = (struct nvme_mi_aem_occ_data *)(spec);
856 }
857 }
858
859 nvme_mi_aem_aeolli_set_aeoltl(list_hdr, aeoltl);
860 test_peer.tx_buf_len = hdr_len + aeoltl;
861
862 if ((fn_data->fc == AEM_FC_BAD_OCC_RSP_BUFFER_LEN_SYNC && !aem_not_ack) ||
863 (fn_data->fc == AEM_FC_BAD_OCC_RSP_BUFFER_LEN_AEM && aem_not_ack))
864 test_peer.tx_buf_len--;
865
866 test_set_tx_mic(&test_peer);
867 }
868
869 static void check_aem_sync_message(struct nvme_mi_aem_enabled_map *expected_mask,
870 struct nvme_mi_aem_enabled_map *expected_state,
871 struct aem_rcv_enable_fn_data *fn_data)
872 {
873 //Check the RX buffer for the endpoint. We should be getting a CONFIG SET AEM
874 //with all enabled items disabled
875 struct nvme_mi_mi_req_hdr *req =
876 (struct nvme_mi_mi_req_hdr *)test_peer.rx_buf;
877
878 struct nvme_mi_aem_supported_list *list =
879 (struct nvme_mi_aem_supported_list *)(req+1);
880
881 assert(req->opcode == nvme_mi_mi_opcode_configuration_set);
882 assert((le32_to_cpu(req->cdw0) & 0xFF) == NVME_MI_CONFIG_AE);
883 assert(list->hdr.aeslver == 0);
884
885 int count = 0;
886 //Count how many events we want to act are in the expected state
887 for (int i = 0; i < 256; i++) {
888 if (expected_mask->enabled[i])
889 count++;
890 }
891
892 assert(list->hdr.numaes == count);
893 assert(list->hdr.aeslhl == sizeof(struct nvme_mi_aem_supported_list));
894 assert(list->hdr.aest == list->hdr.aeslhl +
895 count * sizeof(struct nvme_mi_aem_supported_item));
896
897 struct nvme_mi_aem_supported_item *item =
898 (struct nvme_mi_aem_supported_item *)(list+1);
899
900 //Check the items
901 for (int i = 0; i < 256; i++) {
902 if (expected_mask->enabled[i]) {
903 bool found = false;
904
905 for (int j = 0; j < count; j++) {
906 if (nvme_mi_aem_aesi_get_aesid(item[j].aesi) == i &&
907 nvme_mi_aem_aesi_get_aese(item[j].aesi) ==
908 expected_state->enabled[i]) {
909 found = true;
910 break;
911 }
912 }
913 assert(found);
914 }
915 }
916 }
917
918 static int aem_rcv_enable_fn(struct test_peer *peer, void *buf, size_t len, int sd)
919 {
920 struct aem_rcv_enable_fn_data *fn_data = peer->tx_data;
921 struct nvme_mi_mi_resp_hdr *tx_hdr = (struct nvme_mi_mi_resp_hdr *)peer->tx_buf;
922
923 /* set up a few default response fields; caller may have
924 * initialised the rest of the response
925 */
926 test_peer.tx_buf[0] = NVME_MI_MSGTYPE_NVME;
927 test_peer.tx_buf[1] = test_peer.rx_buf[1] | (NVME_MI_ROR_RSP << 7);
928 tx_hdr->status = 0;
929
930 switch (fn_data->state) {
931 case AEM_ES_GET_ENABLED:
932 {
933 assert(sd == peer->sd[TEST_PEER_SD_COMMANDS_IDX]);
934
935 //First, we want to return some data about what is already enabled
936 struct nvme_mi_aem_supported_list_header *list_hdr =
937 (struct nvme_mi_aem_supported_list_header *)(tx_hdr+1);
938
939 if (fn_data->fc == AEM_FC_BAD_GET_CONFIG_HEADER_LEN)
940 list_hdr->aeslhl =
941 sizeof(struct nvme_mi_aem_supported_list_header) - 1;
942 else
943 list_hdr->aeslhl =
944 sizeof(struct nvme_mi_aem_supported_list_header);
945
946 list_hdr->aeslver = 0;
947 struct nvme_mi_aem_supported_item *item =
948 (struct nvme_mi_aem_supported_item *)(list_hdr+1);
949 int item_count = 0;
950
951 list_hdr->numaes = 0;
952 //Count how many events we want to act are enabled
953 for (int i = 0; i < 256; i++) {
954 if (fn_data->ep_enabled_map.enabled[i]) {
955 list_hdr->numaes++;
956 nvme_mi_aem_aesi_set_aesid(&item[item_count], i);
957 nvme_mi_aem_aesi_set_aee(&item[item_count], 1);
958 item[item_count].aesl =
959 sizeof(struct nvme_mi_aem_supported_item);
960 item_count++;
961 }
962 }
963
964 list_hdr->aest = list_hdr->aeslhl +
965 list_hdr->numaes * sizeof(struct nvme_mi_aem_supported_item);
966 if (fn_data->fc == AEM_FC_BAD_GET_CONFIG_TOTAL_LEN)
967 list_hdr->aest--;//Shrink
968
969 test_peer.tx_buf_len =
970 sizeof(struct nvme_mi_mi_resp_hdr) + list_hdr->aest;
971 if (fn_data->fc == AEM_FC_BAD_GET_CONFIG_BUFFER_LEN)
972 test_peer.tx_buf_len--;
973
974 test_set_tx_mic(&test_peer);
975
976 fn_data->state = AEM_ES_SET_TO_DISABLED;
977 break;
978 }
979 case AEM_ES_SET_TO_DISABLED:
980 {
981 assert(sd == peer->sd[TEST_PEER_SD_COMMANDS_IDX]);
982
983 struct nvme_mi_aem_enabled_map expected = {false};
984 //The items in the ep_enabled_map should get disabled
985 check_aem_sync_message(&fn_data->ep_enabled_map, &expected, fn_data);
986
987 //Need to queue a reasonable response with no OCC
988 struct nvme_mi_mi_resp_hdr *tx_hdr =
989 (struct nvme_mi_mi_resp_hdr *)test_peer.tx_buf;
990 struct nvme_mi_aem_occ_list_hdr *list_hdr =
991 (struct nvme_mi_aem_occ_list_hdr *)(tx_hdr+1);
992
993 list_hdr->aelver = 0;
994 list_hdr->aeolhl = sizeof(*list_hdr);
995 list_hdr->numaeo = 0;
996 nvme_mi_aem_aeolli_set_aeoltl(list_hdr, list_hdr->aeolhl);
997
998 test_peer.tx_buf_len = sizeof(struct nvme_mi_mi_resp_hdr) +
999 nvme_mi_aem_aeolli_get_aeoltl(list_hdr->aeolli);
1000
1001 test_set_tx_mic(&test_peer);
1002
1003 fn_data->state = AEM_ES_ENABLE_SET_ENABLED;
1004 break;
1005 }
1006 case AEM_ES_ENABLE_SET_ENABLED:
1007 assert(sd == peer->sd[TEST_PEER_SD_COMMANDS_IDX]);
1008
1009 //We should verify the right things are enabled
1010 //The items in the host enable map should get enabled
1011 check_aem_sync_message(&fn_data->host_enabled_map,
1012 &fn_data->host_enabled_map, fn_data);
1013
1014 //Prepare an OCC list response
1015 populate_tx_occ_list(false, fn_data, &fn_data->host_enabled_map);
1016
1017 fn_data->state = AEM_ES_PROCESS;
1018 break;
1019 case AEM_ES_PROCESS:
1020 //This case is actually a TX without any request from the host
1021 assert(sd == peer->sd[TEST_PEER_SD_AEMS_IDX]);
1022
1023 //Prepare an OCC list response
1024 populate_tx_occ_list(true, fn_data, &fn_data->aem_during_process_map);
1025
1026 fn_data->state = AEM_ES_ACK_RESPONSE;
1027 break;
1028 case AEM_ES_ACK_RESPONSE:
1029 assert(sd == peer->sd[TEST_PEER_SD_COMMANDS_IDX]);
1030
1031 //Prepare an OCC list response
1032 populate_tx_occ_list(false, fn_data, &fn_data->ack_events_map);
1033
1034 fn_data->state = AEM_ES_ACK_RECEIVED;
1035 break;
1036 default:
1037 assert(false);//Not expected
1038 }
1039
1040 return 0;
1041 }
1042
1043 enum nvme_mi_aem_handler_next_action aem_handler(nvme_mi_ep_t ep, size_t num_events, void *userdata)
1044 {
1045 struct aem_rcv_enable_fn_data *fn_data = userdata;
1046
1047 fn_data->callback_count++;
1048
1049 switch (fn_data->state) {
1050 case AEM_ES_PROCESS:
1051 case AEM_ES_ACK_RESPONSE:
1052 case AEM_ES_ACK_RECEIVED:
1053 {
1054 //This means we just sent out first OCC data
1055 int item_count = 0;
1056 struct nvme_mi_aem_enabled_map *map;
1057
1058 //Count how many events we want to act are enabled
1059 switch (fn_data->state) {
1060 case AEM_ES_PROCESS:
1061 map = &fn_data->host_enabled_map;
1062 break;
1063 case AEM_ES_ACK_RESPONSE:
1064 map = &fn_data->aem_during_process_map;
1065 break;
1066 case AEM_ES_ACK_RECEIVED:
1067 map = &fn_data->ack_events_map;
1068 break;
1069 default:
1070 assert(false);
1071 }
1072
1073 for (int i = 0; i < 256; i++)
1074 if (map->enabled[i])
1075 item_count++;
1076
1077 assert(num_events == item_count);
1078
1079 for (int i = 0; i < num_events; i++) {
1080 struct nvme_mi_event *e = nvme_mi_aem_get_next_event(ep);
1081 uint8_t idx = e->aeoi;
1082
1083 assert(fn_data->events[idx]);
1084 assert(fn_data->host_enabled_map.enabled[idx]);
1085 assert(fn_data->events[idx]->aeocidi == e->aeocidi);
1086 assert(fn_data->events[idx]->aessi == e->aessi);
1087 assert(fn_data->events[idx]->spec_info_len ==
1088 e->spec_info_len);
1089 assert(memcmp(fn_data->events[idx]->spec_info,
1090 e->spec_info, e->spec_info_len) == 0);
1091 assert(fn_data->events[idx]->vend_spec_info_len ==
1092 e->vend_spec_info_len);
1093 assert(memcmp(fn_data->events[idx]->vend_spec_info,
1094 e->vend_spec_info, e->vend_spec_info_len) == 0);
1095 }
1096
1097 assert(nvme_mi_aem_get_next_event(ep) == NULL);
1098 break;
1099 }
1100 default:
1101 assert(false);
1102 }
1103
1104 return NVME_MI_AEM_HNA_ACK;
1105 }
1106
1107 static void aem_test_aem_api_helper(nvme_mi_ep_t ep,
1108 struct nvme_mi_aem_config *config, int expected_event_count)
1109 {
1110 struct aem_rcv_enable_fn_data *fn_data =
1111 (struct aem_rcv_enable_fn_data *)test_peer.tx_data;
1112 int rc = 0;
1113
1114 test_peer.tx_fn = aem_rcv_enable_fn;
1115
1116 //This should not work outside the handler
1117 assert(nvme_mi_aem_get_next_event(ep) == NULL);
1118
1119 rc = nvme_mi_aem_enable(ep, config, test_peer.tx_data);
1120 assert(rc == 0);
1121
1122 //This should not work outside the handler
1123 assert(nvme_mi_aem_get_next_event(ep) == NULL);
1124
1125 rc = nvme_mi_aem_process(ep, test_peer.tx_data);
1126 assert(rc == 0);
1127
1128 //One for initial enable, one for AEM. No ACK events
1129 assert(fn_data->callback_count == expected_event_count);
1130
1131 //This should not work outside the handler
1132 assert(nvme_mi_aem_get_next_event(ep) == NULL);
1133 }
1134
1135 static void aem_test_aem_disable_helper(nvme_mi_ep_t ep,
1136 struct aem_rcv_enable_fn_data *fn_data)
1137 {
1138 memcpy(&fn_data->ep_enabled_map, &fn_data->host_enabled_map,
1139 sizeof(fn_data->host_enabled_map));
1140
1141 fn_data->state = AEM_ES_GET_ENABLED;//This is the flow for disabling
1142 assert(nvme_mi_aem_disable(ep) == 0);
1143 }
1144
1145 static void test_mi_aem_ep_based_failure_helper(nvme_mi_ep_t ep,
1146 enum aem_failure_condition fc, struct test_peer *peer)
1147 {
1148 struct aem_rcv_enable_fn_data fn_data = {0};
1149 struct nvme_mi_aem_config config = {0};
1150
1151 config.aemd = 1;
1152 config.aerd = 2;
1153 config.enabled_map.enabled[3] = true;
1154 fn_data.aem_during_process_map.enabled[3] = true;
1155 struct nvme_mi_event e = {0};
1156
1157 e.aeoi = 3;
1158 e.spec_info_len = 0;
1159 fn_data.events[3] = &e;
1160
1161 memcpy(&fn_data.host_enabled_map, &config.enabled_map, sizeof(config.enabled_map));
1162
1163 config.aem_handler = aem_handler;
1164 peer->tx_data = (void *) &fn_data;
1165 peer->tx_fn = aem_rcv_enable_fn;
1166
1167 fn_data.fc = fc;
1168 switch (fc) {
1169 case AEM_FC_BAD_GET_CONFIG_HEADER_LEN:
1170 case AEM_FC_BAD_GET_CONFIG_TOTAL_LEN:
1171 case AEM_FC_BAD_GET_CONFIG_BUFFER_LEN:
1172 case AEM_FC_BAD_OCC_RSP_HDR_LEN_SYNC:
1173 case AEM_FC_BAD_OCC_RSP_TOTAL_LEN_SYNC:
1174 case AEM_FC_BAD_OCC_RSP_BUFFER_LEN_SYNC:
1175 //These all should fail before processing
1176 assert(nvme_mi_aem_enable(ep, &config, &fn_data) == -1);
1177 assert(errno == EPROTO);
1178 break;
1179 case AEM_FC_BAD_OCC_RSP_HDR_LEN_AEM:
1180 case AEM_FC_BAD_OCC_RSP_TOTAL_LEN_AEM:
1181 case AEM_FC_BAD_OCC_RSP_BUFFER_LEN_AEM:
1182 //These should fail on the processing
1183 assert(nvme_mi_aem_enable(ep, &config, &fn_data) == 0);
1184 assert(nvme_mi_aem_process(ep, &fn_data) == -1);
1185 assert(errno == EPROTO);
1186 break;
1187 default:
1188 assert(false);//Unexpected
1189 }
1190 }
1191
1192 /* test: Check validation of endpoint messages in various stages of aem handling */
1193 static void test_mi_aem_ep_based_failure_conditions(nvme_mi_ep_t ep, struct test_peer *peer)
1194 {
1195 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_GET_CONFIG_HEADER_LEN, peer);
1196 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_GET_CONFIG_TOTAL_LEN, peer);
1197 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_GET_CONFIG_BUFFER_LEN, peer);
1198 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_OCC_RSP_HDR_LEN_SYNC, peer);
1199 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_OCC_RSP_HDR_LEN_AEM, peer);
1200 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_OCC_RSP_TOTAL_LEN_SYNC, peer);
1201 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_OCC_RSP_TOTAL_LEN_AEM, peer);
1202 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_OCC_RSP_BUFFER_LEN_SYNC, peer);
1203 test_mi_aem_ep_based_failure_helper(ep, AEM_FC_BAD_OCC_RSP_BUFFER_LEN_AEM, peer);
1204 }
1205
1206 /* test: Check aem process logic when API used improperly */
1207 static void test_mi_aem_enable_invalid_usage(nvme_mi_ep_t ep, struct test_peer *peer)
1208 {
1209 struct nvme_mi_aem_config config = {0};
1210
1211 config.aem_handler = aem_handler;
1212 config.enabled_map.enabled[0] = false;
1213 config.aemd = 1;
1214 config.aerd = 2;
1215
1216 //Call with invalid config due to nothing enabled
1217 assert(nvme_mi_aem_enable(ep, &config, NULL) == -1);
1218
1219 config.aem_handler = NULL;
1220 config.enabled_map.enabled[0] = true;
1221
1222 //Call with invalid config due to no callback
1223 assert(nvme_mi_aem_enable(ep, &config, NULL) == -1);
1224
1225 //Call with invalid config due to being NULL
1226 assert(nvme_mi_aem_enable(ep, NULL, NULL) == -1);
1227
1228 config.aem_handler = aem_handler;
1229 config.enabled_map.enabled[0] = true;
1230
1231 //Call with invalid endpoint
1232 assert(nvme_mi_aem_enable(NULL, &config, NULL) == -1);
1233 }
1234
1235 /* test: Check aem process logic when API used improperly */
1236 static void test_mi_aem_process_invalid_usage(nvme_mi_ep_t ep, struct test_peer *peer)
1237 {
1238 //Without calling enable first
1239 assert(nvme_mi_aem_process(ep, NULL) == -1);
1240
1241 //Call with invalid ep
1242 assert(nvme_mi_aem_process(NULL, NULL) == -1);
1243 }
1244
1245 /* test: Check aem disable logic when API used improperly */
1246 static void test_mi_aem_disable_invalid_usage(nvme_mi_ep_t ep, struct test_peer *peer)
1247 {
1248 assert(nvme_mi_aem_disable(NULL) == -1);
1249 }
1250
1251 static void test_mi_aem_get_enabled_invalid_usage(nvme_mi_ep_t ep, struct test_peer *peer)
1252 {
1253 struct nvme_mi_aem_enabled_map map;
1254
1255 assert(nvme_mi_aem_get_enabled(ep, NULL) == -1);
1256 assert(nvme_mi_aem_get_enabled(NULL, &map) == -1);
1257 }
1258
1259 /* test: Check aem get enabled logic*/
1260 static void test_mi_aem_get_enabled(nvme_mi_ep_t ep, struct test_peer *peer)
1261 {
1262 //When no events enabled on Endpoint
1263 struct aem_rcv_enable_fn_data fn_data = {0};
1264 struct nvme_mi_aem_enabled_map map;
1265
1266 test_peer.tx_fn = aem_rcv_enable_fn;
1267 peer->tx_data = (void *) &fn_data;
1268 fn_data.ep_enabled_map.enabled[8] = true;
1269 fn_data.ep_enabled_map.enabled[20] = true;
1270 fn_data.ep_enabled_map.enabled[51] = true;
1271 fn_data.ep_enabled_map.enabled[255] = true;
1272
1273 assert(nvme_mi_aem_get_enabled(ep, &map) == 0);
1274 assert(memcmp(&fn_data.ep_enabled_map, &map, sizeof(map)) == 0);
1275 }
1276
1277
1278 /* test: Check aem disable logic when called without an enable */
1279 static void test_mi_aem_disable_no_enable(nvme_mi_ep_t ep, struct test_peer *peer)
1280 {
1281 //When no events enabled on Endpoint
1282 struct aem_rcv_enable_fn_data fn_data = {0};
1283
1284 test_peer.tx_fn = aem_rcv_enable_fn;
1285 peer->tx_data = (void *) &fn_data;
1286
1287 aem_test_aem_disable_helper(ep, &fn_data);
1288
1289 //When some events enabled on Endpoint
1290 fn_data.ep_enabled_map.enabled[45] = true;
1291
1292 aem_test_aem_disable_helper(ep, &fn_data);
1293 }
1294
1295 /* test: Check aem enable logic with ack carrying events */
1296 static void test_mi_aem_api_w_ack_events(nvme_mi_ep_t ep, struct test_peer *peer)
1297 {
1298 struct aem_rcv_enable_fn_data fn_data = {0};
1299 struct nvme_mi_aem_config config = {0};
1300
1301 config.aemd = 1;
1302 config.aerd = 2;
1303 peer->tx_data = (void *) &fn_data;
1304 config.aem_handler = aem_handler;
1305
1306 config.enabled_map.enabled[5] = true;
1307 config.enabled_map.enabled[15] = true;
1308
1309 fn_data.aem_during_process_map.enabled[5] = true;
1310
1311 //No ack_events_map will be enabled in this test
1312 fn_data.ack_events_map.enabled[15] = true;
1313
1314 //Will have EP have nothing enabled at start (ep_enabled_map)
1315
1316 struct nvme_mi_event ev5 = {0};
1317
1318 ev5.aeoi = 5;
1319 ev5.aeocidi = 2;
1320 ev5.aessi = 3;
1321
1322 struct nvme_mi_event ev15 = {0};
1323 uint8_t ev15_spec[] = { 45, 15};
1324
1325 ev15.aeoi = 15;
1326 ev15.aeocidi = 60213;
1327 ev15.aessi = 200;
1328 ev15.spec_info = ev15_spec;
1329 ev15.spec_info_len = sizeof(ev15_spec);
1330
1331 fn_data.events[5] = &ev5;
1332 fn_data.events[15] = &ev15;
1333
1334 memcpy(&fn_data.host_enabled_map, &config.enabled_map, sizeof(config.enabled_map));
1335
1336 aem_test_aem_api_helper(ep, &config, 3);
1337
1338 aem_test_aem_disable_helper(ep, &fn_data);
1339 }
1340
1341 /* test: Check aem enable logic */
1342 static void test_mi_aem_api_simple(nvme_mi_ep_t ep, struct test_peer *peer)
1343 {
1344 struct aem_rcv_enable_fn_data fn_data = {0};
1345 struct nvme_mi_aem_config config = {0};
1346
1347 config.aemd = 1;
1348 config.aerd = 2;
1349 peer->tx_data = (void *) &fn_data;
1350 config.aem_handler = aem_handler;
1351
1352 config.enabled_map.enabled[1] = true;
1353 config.enabled_map.enabled[3] = true;
1354 config.enabled_map.enabled[16] = true;
1355
1356 fn_data.aem_during_process_map.enabled[3] = true;
1357
1358 //No ack_events_map will be enabled in this test
1359
1360 fn_data.ep_enabled_map.enabled[3] = true;
1361 fn_data.ep_enabled_map.enabled[20] = true;
1362 fn_data.ep_enabled_map.enabled[200] = true;
1363
1364 struct nvme_mi_event ev1 = {0};
1365 uint8_t ev1_spec[] = { 98, 56, 32, 12};
1366
1367 ev1.aeoi = 1;
1368 ev1.aeocidi = 2;
1369 ev1.aessi = 3;
1370 ev1.spec_info = ev1_spec;
1371 ev1.spec_info_len = sizeof(ev1_spec);
1372
1373 struct nvme_mi_event ev3 = {0};
1374 uint8_t ev3_spec[] = { 45, 15};
1375
1376 ev3.aeoi = 3;
1377 ev3.aeocidi = 4;
1378 ev3.aessi = 5;
1379 ev3.spec_info = ev3_spec;
1380 ev3.spec_info_len = sizeof(ev3_spec);
1381
1382 struct nvme_mi_event ev16 = {0};
1383
1384 ev16.aeoi = 16;
1385 ev16.aeocidi = 6;
1386 ev16.aessi = 7;
1387
1388 fn_data.events[1] = &ev1;
1389 fn_data.events[3] = &ev3;
1390 fn_data.events[16] = &ev16;
1391
1392 memcpy(&fn_data.host_enabled_map, &config.enabled_map, sizeof(config.enabled_map));
1393
1394 aem_test_aem_api_helper(ep, &config, 2);
1395
1396 aem_test_aem_disable_helper(ep, &fn_data);
1397 }
1398
1399 #define DEFINE_TEST(name) { #name, test_ ## name }
1400 struct test {
1401 const char *name;
1402 void (*fn)(nvme_mi_ep_t, struct test_peer *);
1403 } tests[] = {
1404 DEFINE_TEST(rx_err),
1405 DEFINE_TEST(tx_none),
1406 DEFINE_TEST(tx_err),
1407 DEFINE_TEST(tx_short),
1408 DEFINE_TEST(read_mi_data),
1409 DEFINE_TEST(poll_err),
1410 DEFINE_TEST(mi_resp_err),
1411 DEFINE_TEST(mi_resp_unaligned),
1412 DEFINE_TEST(mi_resp_unaligned_expected),
1413 DEFINE_TEST(admin_resp_err),
1414 DEFINE_TEST(admin_resp_sizes),
1415 DEFINE_TEST(poll_timeout_value),
1416 DEFINE_TEST(poll_timeout),
1417 DEFINE_TEST(mpr_mi),
1418 DEFINE_TEST(mpr_admin),
1419 DEFINE_TEST(mpr_admin_quirked),
1420 DEFINE_TEST(mpr_timeouts),
1421 DEFINE_TEST(mpr_timeout_clamp),
1422 DEFINE_TEST(mpr_mprt_zero),
1423 DEFINE_TEST(mi_aem_api_simple),
1424 DEFINE_TEST(mi_aem_api_w_ack_events),
1425 DEFINE_TEST(mi_aem_disable_no_enable),
1426 DEFINE_TEST(mi_aem_process_invalid_usage),
1427 DEFINE_TEST(mi_aem_enable_invalid_usage),
1428 DEFINE_TEST(mi_aem_disable_invalid_usage),
1429 DEFINE_TEST(mi_aem_get_enabled),
1430 DEFINE_TEST(mi_aem_get_enabled_invalid_usage),
1431 DEFINE_TEST(mi_aem_ep_based_failure_conditions),
1432 };
1433
1434 static void run_test(struct test *test, FILE *logfd, nvme_mi_ep_t ep,
1435 struct test_peer *peer)
1436 {
1437 printf("Running test %s...", test->name);
1438 fflush(stdout);
1439 test->fn(ep, peer);
1440 printf(" OK\n");
1441 test_print_log_buf(logfd);
1442 }
1443
1444 int main(void)
1445 {
1446 nvme_root_t root;
1447 nvme_mi_ep_t ep;
1448 unsigned int i;
1449 FILE *fd;
1450
1451 fd = test_setup_log();
1452
1453 __nvme_mi_mctp_set_ops(&ops);
1454
1455 root = nvme_mi_create_root(fd, DEFAULT_LOGLEVEL);
1456 assert(root);
1457
1458 ep = nvme_mi_open_mctp(root, 0, 0);
1459 assert(ep);
1460
1461 for (i = 0; i < ARRAY_SIZE(tests); i++) {
1462 reset_test_peer();
1463 run_test(&tests[i], fd, ep, &test_peer);
1464 }
1465
1466 nvme_mi_close(ep);
1467 nvme_mi_free_root(root);
1468
1469 test_close_log(fd);
1470
1471 return EXIT_SUCCESS;
1472 }