Debian Codegraph @

src/nvme/mi.c

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 /*
3 * This file is part of libnvme.
4 * Copyright (c) 2021 Code Construct Pty Ltd
5 *
6 * Authors: Jeremy Kerr <[email protected]>
7 */
8
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <time.h>
14 #include <unistd.h>
15
16 #include <ccan/array_size/array_size.h>
17 #include <ccan/ccan/minmax/minmax.h>
18 #include <ccan/endian/endian.h>
19
20 #include "log.h"
21 #include "mi.h"
22 #include "private.h"
23
24 #define NUM_ENABLES (256u)
25
26 _Static_assert(sizeof(struct nvme_mi_aem_supported_list_header) == 5,
27 "size_of_nvme_mi_aem_supported_list_header_is_not_5_bytes");
28 _Static_assert(sizeof(struct nvme_mi_aem_supported_item) == 3,
29 "sizeof_nvme_mi_aem_supported_item_is_not_3_bytes");
30 _Static_assert(sizeof(struct nvme_mi_aem_enable_item) == 3,
31 "size_of_ae_enable_item_t_is_not_3_bytes");
32 _Static_assert(sizeof(struct nvme_mi_aem_enable_list_header) == 5,
33 "size_of_nvme_mi_aem_enable_list_header_is_not_5_bytes");
34 _Static_assert(sizeof(struct nvme_mi_aem_occ_data) == 9,
35 "size_of_nvme_mi_aem_occ_data_is_not_9_bytes");
36 _Static_assert(sizeof(struct nvme_mi_aem_occ_list_hdr) == 7,
37 "size_of_nvme_mi_aem_occ_list_hdr_is_not_7_bytes");
38
39 static int nvme_mi_get_async_message(nvme_mi_ep_t ep,
40 struct nvme_mi_aem_msg *aem_msg, size_t *aem_msg_len);
41
42 static const int default_timeout = 1000; /* milliseconds; endpoints may
43 override */
44
45 static bool nvme_mi_probe_enabled_default(void)
46 {
47 char *val;
48
49 val = getenv("LIBNVME_MI_PROBE_ENABLED");
50 if (!val)
51 return true;
52
53 return strcmp(val, "0") &&
54 strcasecmp(val, "false") &&
55 strncasecmp(val, "disable", 7);
56
57 }
58
59 /* MI-equivalent of nvme_create_root, but avoids clashing symbol names
60 * when linking against both libnvme and libnvme-mi.
61 */
62 nvme_root_t nvme_mi_create_root(FILE *fp, int log_level)
63 {
64 struct nvme_root *r;
65 int fd;
66
67 r = calloc(1, sizeof(*r));
68 if (!r) {
69 errno = ENOMEM;
70 return NULL;
71 }
72
73 if (fp) {
74 fd = fileno(fp);
75 if (fd < 0) {
76 free(r);
77 return NULL;
78 }
79 } else
80 fd = STDERR_FILENO;
81
82 r->log.fd = fd;
83 r->log.level = log_level;
84
85 r->mi_probe_enabled = nvme_mi_probe_enabled_default();
86
87 list_head_init(&r->hosts);
88 list_head_init(&r->endpoints);
89
90 return r;
91 }
92
93 void nvme_mi_free_root(nvme_root_t root)
94 {
95 nvme_mi_ep_t ep, tmp;
96
97 nvme_mi_for_each_endpoint_safe(root, ep, tmp)
98 nvme_mi_close(ep);
99
100 free(root);
101 }
102
103 void nvme_mi_set_probe_enabled(nvme_root_t root, bool enabled)
104 {
105 root->mi_probe_enabled = enabled;
106 }
107
108 static void nvme_mi_record_resp_time(struct nvme_mi_ep *ep)
109 {
110 int rc;
111
112 rc = clock_gettime(CLOCK_MONOTONIC, &ep->last_resp_time);
113 ep->last_resp_time_valid = !rc;
114 }
115
116 static bool nvme_mi_compare_vid_mn(struct nvme_mi_ep *ep,
117 struct nvme_id_ctrl *id,
118 __u16 vid, const char *mn)
119
120 {
121 int len;
122
123 len = strlen(mn);
124 if (len >= sizeof(id->mn)) {
125 nvme_msg(ep->root, LOG_ERR,
126 "Internal error: invalid model number for %s\n",
127 __func__);
128 return false;
129 }
130
131 return le16_to_cpu(id->vid) == vid && !strncmp(id->mn, mn, len);
132 }
133
134 static void __nvme_mi_format_mn(struct nvme_id_ctrl *id,
135 char *mn, size_t mn_len)
136 {
137 const size_t id_mn_size = sizeof(id->mn);
138 int i;
139
140 /* A BUILD_ASSERT() would be nice here, but we're not const enough for
141 * that
142 */
143 if (mn_len <= id_mn_size)
144 abort();
145
146 memcpy(mn, id->mn, id_mn_size);
147 mn[id_mn_size] = '\0';
148
149 for (i = id_mn_size - 1; i >= 0; i--) {
150 if (mn[i] != '\0' && mn[i] != ' ')
151 break;
152 mn[i] = '\0';
153 }
154 }
155
156 #define nvme_mi_format_mn(id, m) __nvme_mi_format_mn(id, m, sizeof(m))
157
158 void nvme_mi_ep_probe(struct nvme_mi_ep *ep)
159 {
160 struct nvme_identify_args id_args = { 0 };
161 struct nvme_id_ctrl id = { 0 };
162 struct nvme_mi_ctrl *ctrl;
163 int rc;
164
165 /* Ensure the probe occurs at most once. This isn't just to mitigate doubling
166 * a linear stream of commands, it also terminates recursion via the
167 * nvme_mi_submit() call issued by nvme_mi_admin_identify_partial() below.
168 */
169 if (ep->quirks_probed)
170 return;
171
172 /* Mark ep->quirks as valid. Note that for the purpose of quirk probing,
173 * the quirk probe itself cannot rely on quirks, and so the fact that none are
174 * yet set is desirable. The request that triggered nvme_mi_submit() will have
175 * an initialised ep->quirks when we return from the root probe call.
176 */
177 ep->quirks_probed = true;
178
179 if (!ep->root->mi_probe_enabled)
180 return;
181
182 /* start with no quirks, detect as we go */
183 ep->quirks = 0;
184
185 ctrl = nvme_mi_init_ctrl(ep, 0);
186 if (!ctrl)
187 return;
188
189 /* Do enough of an identify (assuming controller 0) to retrieve
190 * device and firmware identification information. This gives us the
191 * following fields in id:
192 *
193 * - vid (PCI vendor ID)
194 * - ssvid (PCI subsystem vendor ID)
195 * - sn (Serial number)
196 * - mn (Model number)
197 * - fr (Firmware revision)
198 *
199 * all other fields - rab and onwards - will be zero!
200 */
201 id_args.args_size = sizeof(id_args);
202 id_args.data = &id;
203 id_args.cns = NVME_IDENTIFY_CNS_CTRL;
204 id_args.nsid = NVME_NSID_NONE;
205 id_args.cntid = 0;
206 id_args.csi = NVME_CSI_NVM;
207
208 rc = nvme_mi_admin_identify_partial(ctrl, &id_args, 0,
209 offsetof(struct nvme_id_ctrl, rab));
210 if (rc) {
211 nvme_msg(ep->root, LOG_WARNING,
212 "Identify Controller failed, no quirks applied\n");
213 goto out_close;
214 }
215
216 /* Samsung MZUL2512: cannot receive commands sent within ~1ms of
217 * the previous response. Set an inter-command delay of 1.2ms for
218 * a little extra tolerance.
219 */
220 if (nvme_mi_compare_vid_mn(ep, &id, 0x144d, "MZUL2512HCJQ")) {
221 ep->quirks |= NVME_QUIRK_MIN_INTER_COMMAND_TIME;
222 ep->inter_command_us = 1200;
223 }
224
225 /* If we're quirking for the inter-command time, record the last
226 * command time now, so we don't conflict with the just-sent identify.
227 */
228 if (ep->quirks & NVME_QUIRK_MIN_INTER_COMMAND_TIME)
229 nvme_mi_record_resp_time(ep);
230
231 if (ep->quirks) {
232 char tmp[sizeof(id.mn) + 1];
233
234 nvme_mi_format_mn(&id, tmp);
235 nvme_msg(ep->root, LOG_DEBUG,
236 "device %02x:%s: applying quirks 0x%08lx\n",
237 id.vid, tmp, ep->quirks);
238 }
239
240 out_close:
241 nvme_mi_close_ctrl(ctrl);
242 }
243
244 static const int nsec_per_sec = 1000 * 1000 * 1000;
245 /* timercmp and timersub, but for struct timespec */
246 #define timespec_cmp(a, b, CMP) \
247 (((a)->tv_sec == (b)->tv_sec) \
248 ? ((a)->tv_nsec CMP (b)->tv_nsec) \
249 : ((a)->tv_sec CMP (b)->tv_sec))
250
251 #define timespec_sub(a, b, result) \
252 do { \
253 (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
254 (result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
255 if ((result)->tv_nsec < 0) { \
256 --(result)->tv_sec; \
257 (result)->tv_nsec += nsec_per_sec; \
258 } \
259 } while (0)
260
261 static void nvme_mi_insert_delay(struct nvme_mi_ep *ep)
262 {
263 struct timespec now, next, delay;
264 int rc;
265
266 if (!ep->last_resp_time_valid)
267 return;
268
269 /* calculate earliest next command time */
270 next.tv_nsec = ep->last_resp_time.tv_nsec + ep->inter_command_us * 1000;
271 next.tv_sec = ep->last_resp_time.tv_sec;
272 if (next.tv_nsec > nsec_per_sec) {
273 next.tv_nsec -= nsec_per_sec;
274 next.tv_sec += 1;
275 }
276
277 rc = clock_gettime(CLOCK_MONOTONIC, &now);
278 if (rc) {
279 /* not much we can do; continue immediately */
280 return;
281 }
282
283 if (timespec_cmp(&now, &next, >=))
284 return;
285
286 timespec_sub(&next, &now, &delay);
287
288 nanosleep(&delay, NULL);
289 }
290
291 struct nvme_mi_ep *nvme_mi_init_ep(nvme_root_t root)
292 {
293 struct nvme_mi_ep *ep;
294
295 ep = calloc(1, sizeof(*ep));
296 if (!ep)
297 return NULL;
298
299 list_node_init(&ep->root_entry);
300 ep->root = root;
301 ep->quirks_probed = false;
302 ep->controllers_scanned = false;
303 ep->timeout = default_timeout;
304 ep->mprt_max = 0;
305 list_head_init(&ep->controllers);
306
307 list_add(&root->endpoints, &ep->root_entry);
308
309 return ep;
310 }
311
312 int nvme_mi_ep_set_timeout(nvme_mi_ep_t ep, unsigned int timeout_ms)
313 {
314 if (ep->transport->check_timeout) {
315 int rc;
316 rc = ep->transport->check_timeout(ep, timeout_ms);
317 if (rc)
318 return rc;
319 }
320
321 ep->timeout = timeout_ms;
322 return 0;
323 }
324
325 void nvme_mi_ep_set_mprt_max(nvme_mi_ep_t ep, unsigned int mprt_max_ms)
326 {
327 ep->mprt_max = mprt_max_ms;
328 }
329
330 unsigned int nvme_mi_ep_get_timeout(nvme_mi_ep_t ep)
331 {
332 return ep->timeout;
333 }
334
335 static bool nvme_mi_ep_has_quirk(nvme_mi_ep_t ep, unsigned long quirk)
336 {
337 return ep->quirks & quirk;
338 }
339
340 struct nvme_mi_ctrl *nvme_mi_init_ctrl(nvme_mi_ep_t ep, __u16 ctrl_id)
341 {
342 struct nvme_mi_ctrl *ctrl;
343
344 ctrl = malloc(sizeof(*ctrl));
345 if (!ctrl)
346 return NULL;
347
348 ctrl->ep = ep;
349 ctrl->id = ctrl_id;
350
351 list_add_tail(&ep->controllers, &ctrl->ep_entry);
352
353 return ctrl;
354 }
355
356 __u16 nvme_mi_ctrl_id(nvme_mi_ctrl_t ctrl)
357 {
358 return ctrl->id;
359 }
360
361 int nvme_mi_scan_ep(nvme_mi_ep_t ep, bool force_rescan)
362 {
363 struct nvme_ctrl_list list;
364 unsigned int i, n_ctrl;
365 int rc;
366
367 if (ep->controllers_scanned) {
368 if (force_rescan) {
369 struct nvme_mi_ctrl *ctrl, *tmp;
370 nvme_mi_for_each_ctrl_safe(ep, ctrl, tmp)
371 nvme_mi_close_ctrl(ctrl);
372 } else {
373 return 0;
374 }
375 }
376
377 rc = nvme_mi_mi_read_mi_data_ctrl_list(ep, 0, &list);
378 if (rc)
379 return rc;
380
381 n_ctrl = le16_to_cpu(list.num);
382 if (n_ctrl > NVME_ID_CTRL_LIST_MAX) {
383 errno = EPROTO;
384 return -1;
385 }
386
387 for (i = 0; i < n_ctrl; i++) {
388 struct nvme_mi_ctrl *ctrl;
389 __u16 id;
390
391 id = le16_to_cpu(list.identifier[i]);
392
393 ctrl = nvme_mi_init_ctrl(ep, id);
394 if (!ctrl)
395 break;
396 }
397
398 ep->controllers_scanned = true;
399 return 0;
400 }
401
402 __u32 nvme_mi_crc32_update(__u32 crc, void *data, size_t len)
403 {
404 int i;
405
406 while (len--) {
407 crc ^= *(unsigned char *)(data++);
408 for (i = 0; i < 8; i++)
409 crc = (crc >> 1) ^ ((crc & 1) ? 0x82F63B78 : 0);
410 }
411 return crc;
412 }
413
414 static void nvme_mi_calc_req_mic(struct nvme_mi_req *req)
415 {
416 __u32 crc = 0xffffffff;
417
418 crc = nvme_mi_crc32_update(crc, req->hdr, req->hdr_len);
419 crc = nvme_mi_crc32_update(crc, req->data, req->data_len);
420
421 req->mic = ~crc;
422 }
423
424 /* returns zero on correct MIC */
425 static int nvme_mi_verify_resp_mic(struct nvme_mi_resp *resp)
426 {
427 __u32 crc = 0xffffffff;
428
429 crc = nvme_mi_crc32_update(crc, resp->hdr, resp->hdr_len);
430 crc = nvme_mi_crc32_update(crc, resp->data, resp->data_len);
431
432 return resp->mic != ~crc;
433 }
434
435 __attribute__((weak)) void *nvme_mi_submit_entry(__u8 type, const struct nvme_mi_msg_hdr *hdr,
436 size_t hdr_len, const void *data, size_t data_len)
437 {
438 return NULL;
439 }
440
441 __attribute__((weak)) void nvme_mi_submit_exit(__u8 type, const struct nvme_mi_msg_hdr *hdr,
442 size_t hdr_len, const void *data, size_t data_len,
443 void *user_data) { }
444
445
446 int nvme_mi_async_read(nvme_mi_ep_t ep, struct nvme_mi_resp *resp)
447 {
448 if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_MIN_INTER_COMMAND_TIME))
449 nvme_mi_record_resp_time(ep);
450
451 int rc = ep->transport->aem_read(ep, resp);
452
453 if (rc && errno == EWOULDBLOCK) {
454 //Sometimes we might get owned tag data from the wrong endpoint.
455 //This isn't an error, but we shouldn't process it here
456 resp->data_len = 0;//No data to process
457 return 0;
458 } else if (rc) {
459 nvme_msg(ep->root, LOG_INFO, "transport failure\n");
460 return rc;
461 }
462
463 if (ep->transport->mic_enabled) {
464 rc = nvme_mi_verify_resp_mic(resp);
465 if (rc) {
466 nvme_msg(ep->root, LOG_WARNING, "crc mismatch\n");
467 errno = EBADMSG;
468 return -1;
469 }
470 }
471
472 //TODO: There's a bunch of overlap with the nvme_mi_submit. Maybe we make common helpers
473
474 /* basic response checks */
475 if (resp->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
476 nvme_msg(ep->root, LOG_DEBUG,
477 "Bad response header len: %zd\n", resp->hdr_len);
478 errno = EPROTO;
479 return -1;
480 }
481
482 if (resp->hdr->type != NVME_MI_MSGTYPE_NVME) {
483 nvme_msg(ep->root, LOG_DEBUG,
484 "Invalid message type 0x%02x\n", resp->hdr->type);
485 errno = EPROTO;
486 return -1;
487 }
488
489 if (!(resp->hdr->nmp & ~(NVME_MI_ROR_REQ << 7))) {
490 nvme_msg(ep->root, LOG_DEBUG,
491 "ROR value in response indicates a response\n");
492 errno = EIO;
493 return -1;
494 }
495
496 if (!(resp->hdr->nmp & (NVME_MI_MT_AE << 3))) {
497 nvme_msg(ep->root, LOG_DEBUG,
498 "NMIMT does not indicate AEM\n");
499 resp->data_len = 0;//No data to process
500 return 0;
501 }
502
503 return 0;
504 }
505
506
507 int nvme_mi_submit(nvme_mi_ep_t ep, struct nvme_mi_req *req,
508 struct nvme_mi_resp *resp)
509 {
510 int rc;
511 void *user_data;
512
513 user_data = nvme_mi_submit_entry(req->hdr->type, req->hdr, req->hdr_len, req->data,
514 req->data_len);
515
516 if (req->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
517 errno = EINVAL;
518 return -1;
519 }
520
521 if (req->hdr_len & 0x3) {
522 errno = EINVAL;
523 return -1;
524 }
525
526 if (resp->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
527 errno = EINVAL;
528 return -1;
529 }
530
531 if (resp->hdr_len & 0x3) {
532 errno = EINVAL;
533 return -1;
534 }
535
536 nvme_mi_ep_probe(ep);
537
538 if (ep->transport->mic_enabled)
539 nvme_mi_calc_req_mic(req);
540
541 if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_MIN_INTER_COMMAND_TIME))
542 nvme_mi_insert_delay(ep);
543
544 rc = ep->transport->submit(ep, req, resp);
545
546 if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_MIN_INTER_COMMAND_TIME))
547 nvme_mi_record_resp_time(ep);
548
549 if (rc) {
550 nvme_msg(ep->root, LOG_INFO, "transport failure\n");
551 return rc;
552 }
553
554 if (ep->transport->mic_enabled) {
555 rc = nvme_mi_verify_resp_mic(resp);
556 if (rc) {
557 nvme_msg(ep->root, LOG_WARNING, "crc mismatch\n");
558 errno = EBADMSG;
559 return -1;
560 }
561 }
562
563 /* basic response checks */
564 if (resp->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
565 nvme_msg(ep->root, LOG_DEBUG,
566 "Bad response header len: %zd\n", resp->hdr_len);
567 errno = EPROTO;
568 return -1;
569 }
570
571 if (resp->hdr->type != NVME_MI_MSGTYPE_NVME) {
572 nvme_msg(ep->root, LOG_DEBUG,
573 "Invalid message type 0x%02x\n", resp->hdr->type);
574 errno = EPROTO;
575 return -1;
576 }
577
578 if (!(resp->hdr->nmp & (NVME_MI_ROR_RSP << 7))) {
579 nvme_msg(ep->root, LOG_DEBUG,
580 "ROR value in response indicates a request\n");
581 errno = EIO;
582 return -1;
583 }
584
585 if ((resp->hdr->nmp & 0x1) != (req->hdr->nmp & 0x1)) {
586 nvme_msg(ep->root, LOG_WARNING,
587 "Command slot mismatch: req %d, resp %d\n",
588 req->hdr->nmp & 0x1,
589 resp->hdr->nmp & 0x1);
590 errno = EIO;
591 return -1;
592 }
593
594 nvme_mi_submit_exit(resp->hdr->type, resp->hdr, resp->hdr_len, resp->data, resp->data_len,
595 user_data);
596
597 return 0;
598 }
599
600 int nvme_mi_set_csi(nvme_mi_ep_t ep, uint8_t csi)
601 {
602 uint8_t csi_bit = (csi) ? 1 : 0;
603
604 if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_CSI_1_NOT_SUPPORTED) && csi_bit)
605 return -1;
606
607 ep->csi = csi_bit;
608
609 return 0;
610 }
611
612 static void nvme_mi_admin_init_req(nvme_mi_ep_t ep,
613 struct nvme_mi_req *req,
614 struct nvme_mi_admin_req_hdr *hdr,
615 __u16 ctrl_id, __u8 opcode)
616 {
617 memset(req, 0, sizeof(*req));
618 memset(hdr, 0, sizeof(*hdr));
619
620 hdr->hdr.type = NVME_MI_MSGTYPE_NVME;
621 hdr->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
622 (NVME_MI_MT_ADMIN << 3) |
623 (ep->csi & 1);
624 hdr->opcode = opcode;
625 hdr->ctrl_id = cpu_to_le16(ctrl_id);
626
627 req->hdr = &hdr->hdr;
628 req->hdr_len = sizeof(*hdr);
629 }
630
631 static void nvme_mi_admin_init_resp(struct nvme_mi_resp *resp,
632 struct nvme_mi_admin_resp_hdr *hdr)
633 {
634 memset(resp, 0, sizeof(*resp));
635 resp->hdr = &hdr->hdr;
636 resp->hdr_len = sizeof(*hdr);
637 }
638
639 static void nvme_mi_control_init_req(nvme_mi_ep_t ep,
640 struct nvme_mi_req *req,
641 struct nvme_mi_control_req *control_req,
642 __u8 opcode, __u16 cpsp)
643 {
644 memset(req, 0, sizeof(*req));
645 memset(control_req, 0, sizeof(*control_req));
646
647 control_req->hdr.type = NVME_MI_MSGTYPE_NVME;
648 control_req->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
649 (NVME_MI_MT_CONTROL << 3) |
650 (ep->csi & 1);
651 control_req->opcode = opcode;
652 control_req->cpsp = cpu_to_le16(cpsp);
653
654 req->hdr = &control_req->hdr;
655 req->hdr_len = sizeof(*control_req);
656 }
657
658 static void nvme_mi_control_init_resp(struct nvme_mi_resp *resp,
659 struct nvme_mi_control_resp *control_resp)
660 {
661 memset(resp, 0, sizeof(*resp));
662 resp->hdr = &control_resp->hdr;
663 resp->hdr_len = sizeof(*control_resp);
664 }
665
666 static int nvme_mi_admin_parse_status(struct nvme_mi_resp *resp, __u32 *result)
667 {
668 struct nvme_mi_admin_resp_hdr *admin_hdr;
669 struct nvme_mi_msg_resp *resp_hdr;
670 __u32 nvme_status;
671 __u32 nvme_result;
672
673 /* we have a few different sources of "result" here: the status header
674 * in the MI response, the cdw3 status field, and (command specific)
675 * return values in cdw0. The latter is returned in the result pointer,
676 * the former two generate return values here
677 */
678
679 if (resp->hdr_len < sizeof(*resp_hdr)) {
680 errno = -EPROTO;
681 return -1;
682 }
683 resp_hdr = (struct nvme_mi_msg_resp *)resp->hdr;
684
685 /* If we have a MI error, we can't be sure there's an admin header
686 * following; return just the MI status, with the status type
687 * indicator of MI.
688 */
689 if (resp_hdr->status)
690 return resp_hdr->status |
691 (NVME_STATUS_TYPE_MI << NVME_STATUS_TYPE_SHIFT);
692
693 /* We shouldn't hit this, as we'd have an error reported earlier.
694 * However, for pointer safety, ensure we have a full admin header
695 */
696 if (resp->hdr_len < sizeof(*admin_hdr)) {
697 errno = EPROTO;
698 return -1;
699 }
700
701 admin_hdr = (struct nvme_mi_admin_resp_hdr *)resp->hdr;
702 nvme_result = le32_to_cpu(admin_hdr->cdw0);
703
704 /* Shift down 17 here: the SC starts at bit 17, and the NVME_SC_*
705 * definitions align to this bit (and up). The CRD, MORE and DNR
706 * bits are defined accordingly (eg., DNR is 0x4000).
707 */
708 nvme_status = le32_to_cpu(admin_hdr->cdw3) >> 17;
709
710 /* the result pointer, optionally stored if the caller needs it */
711 if (result)
712 *result = nvme_result;
713
714 return nvme_status;
715 }
716
717 static int nvme_mi_control_parse_status(struct nvme_mi_resp *resp, __u16 *cpsr)
718 {
719 struct nvme_mi_control_resp *control_resp;
720
721 if (resp->hdr_len < sizeof(*control_resp)) {
722 errno = -EPROTO;
723 return -1;
724 }
725 control_resp = (struct nvme_mi_control_resp *)resp->hdr;
726
727 if (control_resp->status)
728 return control_resp->status |
729 (NVME_STATUS_TYPE_MI << NVME_STATUS_TYPE_SHIFT);
730
731 if (cpsr)
732 *cpsr = le16_to_cpu(control_resp->cpsr);
733
734 return control_resp->status;
735 }
736
737 static int nvme_mi_get_async_message(nvme_mi_ep_t ep,
738 struct nvme_mi_aem_msg *aem_msg,
739 size_t *aem_msg_len)
740 {
741 struct nvme_mi_resp resp;
742
743 memset(&resp, 0, sizeof(resp));
744 resp.hdr = &aem_msg->hdr;
745 resp.hdr_len = sizeof(struct nvme_mi_msg_hdr);
746 resp.data = &aem_msg->occ_list_hdr;
747 resp.data_len = *aem_msg_len;
748
749 int rc = nvme_mi_async_read(ep, &resp);
750
751 if (rc)
752 return rc;
753
754 *aem_msg_len = resp.data_len;
755 return 0;
756 }
757
758
759 int nvme_mi_admin_xfer(nvme_mi_ctrl_t ctrl,
760 struct nvme_mi_admin_req_hdr *admin_req,
761 size_t req_data_size,
762 struct nvme_mi_admin_resp_hdr *admin_resp,
763 off_t resp_data_offset,
764 size_t *resp_data_size)
765 {
766 struct nvme_mi_resp resp;
767 struct nvme_mi_req req;
768 __u32 dlen, doff;
769 int rc;
770
771 /* length/offset checks. The common _submit() API will do further
772 * checking on the message lengths too, so these are kept specific
773 * to the requirements of the Admin command set
774 */
775
776 /* NVMe-MI v1.2 imposes a limit of 4096 bytes on the dlen field */
777 if (*resp_data_size > 4096) {
778 errno = EINVAL;
779 return -1;
780 }
781
782 /* we only have 32 bits of offset */
783 if (resp_data_offset > 0xffffffff) {
784 errno = EINVAL;
785 return -1;
786 }
787
788 /* request and response lengths & offset must be aligned */
789 if ((req_data_size & 0x3) ||
790 (*resp_data_size & 0x3) ||
791 (resp_data_offset & 0x3)) {
792 errno = EINVAL;
793 return -1;
794 }
795
796 /* bidirectional not permitted (see DLEN definition) */
797 if (req_data_size && *resp_data_size) {
798 errno = EINVAL;
799 return -1;
800 }
801
802 if (!*resp_data_size && resp_data_offset) {
803 errno = EINVAL;
804 return -1;
805 }
806
807 admin_req->hdr.type = NVME_MI_MSGTYPE_NVME;
808 admin_req->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
809 (NVME_MI_MT_ADMIN << 3) |
810 (ctrl->ep->csi & 1);
811
812 admin_req->ctrl_id = cpu_to_le16(ctrl->id);
813 memset(&req, 0, sizeof(req));
814 req.hdr = &admin_req->hdr;
815 req.hdr_len = sizeof(*admin_req);
816 req.data = admin_req + 1;
817 req.data_len = req_data_size;
818
819 memset(&resp, 0, sizeof(resp));
820 resp.hdr = &admin_resp->hdr;
821 resp.hdr_len = sizeof(*admin_resp);
822 resp.data = admin_resp + 1;
823 resp.data_len = *resp_data_size;
824
825 /* limit the response size, specify offset */
826 admin_req->flags = 0x3;
827
828 /* dlen and doff have different interpretations depending on the data direction */
829 if (req_data_size) {
830 dlen = req_data_size & 0xffffffff;
831 doff = 0;
832 } else {
833 dlen = *resp_data_size & 0xffffffff;
834 doff = resp_data_offset & 0xffffffff;
835 }
836 admin_req->dlen = cpu_to_le32(dlen);
837 admin_req->doff = cpu_to_le32(doff);
838
839 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
840 if (rc)
841 return rc;
842
843 *resp_data_size = resp.data_len;
844
845 return 0;
846 }
847
848 int nvme_mi_admin_admin_passthru(nvme_mi_ctrl_t ctrl, __u8 opcode, __u8 flags,
849 __u16 rsvd, __u32 nsid, __u32 cdw2, __u32 cdw3,
850 __u32 cdw10, __u32 cdw11, __u32 cdw12,
851 __u32 cdw13, __u32 cdw14, __u32 cdw15,
852 __u32 data_len, void *data, __u32 metadata_len,
853 void *metadata, __u32 timeout_ms, __u32 *result)
854 {
855 /* Input parameters flags, rsvd, metadata, metadata_len are not used */
856 struct nvme_mi_admin_resp_hdr resp_hdr;
857 struct nvme_mi_admin_req_hdr req_hdr;
858 struct nvme_mi_resp resp;
859 struct nvme_mi_req req;
860 unsigned int timeout_save;
861 int rc;
862 int direction = opcode & 0x3;
863 bool has_write_data = false;
864 bool has_read_data = false;
865
866 if (direction == NVME_DATA_TFR_BIDIRECTIONAL) {
867 nvme_msg(ctrl->ep->root, LOG_ERR,
868 "nvme_mi_admin_admin_passthru doesn't support bidirectional commands\n");
869 errno = EINVAL;
870 return -1;
871 }
872
873 if (data_len > 4096) {
874 nvme_msg(ctrl->ep->root, LOG_ERR,
875 "nvme_mi_admin_admin_passthru doesn't support data_len over 4096 bytes.\n");
876 errno = EINVAL;
877 return -1;
878 }
879
880 if (data != NULL && data_len != 0) {
881 if (direction == NVME_DATA_TFR_HOST_TO_CTRL)
882 has_write_data = true;
883 if (direction == NVME_DATA_TFR_CTRL_TO_HOST)
884 has_read_data = true;
885 }
886
887 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id, opcode);
888 req_hdr.cdw1 = cpu_to_le32(nsid);
889 req_hdr.cdw2 = cpu_to_le32(cdw2);
890 req_hdr.cdw3 = cpu_to_le32(cdw3);
891 req_hdr.cdw10 = cpu_to_le32(cdw10);
892 req_hdr.cdw11 = cpu_to_le32(cdw11);
893 req_hdr.cdw12 = cpu_to_le32(cdw12);
894 req_hdr.cdw13 = cpu_to_le32(cdw13);
895 req_hdr.cdw14 = cpu_to_le32(cdw14);
896 req_hdr.cdw15 = cpu_to_le32(cdw15);
897 req_hdr.doff = 0;
898 if (data_len != 0) {
899 req_hdr.dlen = cpu_to_le32(data_len);
900 /* Bit 0 set to 1 means DLEN contains a value */
901 req_hdr.flags = 0x1;
902 }
903
904 if (has_write_data) {
905 req.data = data;
906 req.data_len = data_len;
907 }
908
909 nvme_mi_admin_init_resp(&resp, &resp_hdr);
910
911 if (has_read_data) {
912 resp.data = data;
913 resp.data_len = data_len;
914 }
915
916 /* if the user has specified a custom timeout, save the current
917 * timeout and override
918 */
919 if (timeout_ms != 0) {
920 timeout_save = nvme_mi_ep_get_timeout(ctrl->ep);
921 nvme_mi_ep_set_timeout(ctrl->ep, timeout_ms);
922 }
923 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
924 if (timeout_ms != 0)
925 nvme_mi_ep_set_timeout(ctrl->ep, timeout_save);
926
927 if (rc)
928 return rc;
929
930 rc = nvme_mi_admin_parse_status(&resp, result);
931 if (rc)
932 return rc;
933
934 if (has_read_data && (resp.data_len != data_len)) {
935 errno = EPROTO;
936 return -1;
937 }
938
939 return 0;
940 }
941
942 int nvme_mi_admin_identify_partial(nvme_mi_ctrl_t ctrl,
943 struct nvme_identify_args *args,
944 off_t offset, size_t size)
945 {
946 struct nvme_mi_admin_resp_hdr resp_hdr;
947 struct nvme_mi_admin_req_hdr req_hdr;
948 struct nvme_mi_resp resp;
949 struct nvme_mi_req req;
950 int rc;
951
952 if (args->args_size < sizeof(*args)) {
953 errno = EINVAL;
954 return -1;
955 }
956
957 if (!size || size > 0xffffffff) {
958 errno = EINVAL;
959 return -1;
960 }
961
962 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id, nvme_admin_identify);
963 req_hdr.cdw1 = cpu_to_le32(args->nsid);
964 req_hdr.cdw10 = cpu_to_le32(args->cntid << 16 | args->cns);
965 req_hdr.cdw11 = cpu_to_le32((args->csi & 0xff) << 24 |
966 args->cns_specific_id);
967 req_hdr.cdw14 = cpu_to_le32(args->uuidx);
968 req_hdr.dlen = cpu_to_le32(size & 0xffffffff);
969 req_hdr.flags = 0x1;
970 if (offset) {
971 req_hdr.flags |= 0x2;
972 req_hdr.doff = cpu_to_le32(offset);
973 }
974
975 nvme_mi_admin_init_resp(&resp, &resp_hdr);
976 resp.data = args->data;
977 resp.data_len = size;
978
979 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
980 if (rc)
981 return rc;
982
983 rc = nvme_mi_admin_parse_status(&resp, args->result);
984 if (rc)
985 return rc;
986
987 /* callers will expect a full response; if the data buffer isn't
988 * fully valid, return an error */
989 if (resp.data_len != size) {
990 errno = EPROTO;
991 return -1;
992 }
993
994 return 0;
995 }
996
997 int nvme_mi_control(nvme_mi_ep_t ep, __u8 opcode,
998 __u16 cpsp, __u16 *result_cpsr)
999 {
1000 struct nvme_mi_control_resp control_resp;
1001 struct nvme_mi_control_req control_req;
1002 struct nvme_mi_resp resp;
1003 struct nvme_mi_req req;
1004 int rc = 0;
1005
1006 nvme_mi_control_init_req(ep, &req, &control_req, opcode, cpsp);
1007 nvme_mi_control_init_resp(&resp, &control_resp);
1008
1009 rc = nvme_mi_submit(ep, &req, &resp);
1010 if (rc)
1011 return rc;
1012
1013 rc = nvme_mi_control_parse_status(&resp, result_cpsr);
1014 if (rc)
1015 return rc;
1016
1017 return 0;
1018 }
1019
1020 /* retrieves a MCTP-messsage-sized chunk of log page data. offset and len are
1021 * specified within the args->data area. The `offset` parameter is a relative
1022 * offset to the args->lpo !
1023 *
1024 * What's more, we change the LPO of original command to chunk the request
1025 * message into proper size which is allowed by MI interface. One reason is that
1026 * this option seems to be supported better by devices. For more information
1027 * about this option, please check https://github.com/linux-nvme/libnvme/pull/539
1028 * */
1029 static int __nvme_mi_admin_get_log(nvme_mi_ctrl_t ctrl,
1030 const struct nvme_get_log_args *args,
1031 off_t offset, size_t *lenp, bool final)
1032 {
1033 __u64 log_page_offset = args->lpo + offset;
1034 struct nvme_mi_admin_resp_hdr resp_hdr;
1035 struct nvme_mi_admin_req_hdr req_hdr;
1036 struct nvme_mi_resp resp;
1037 struct nvme_mi_req req;
1038 size_t len;
1039 __u32 ndw;
1040 int rc;
1041
1042 /* MI spec requires that the data length field is less than or equal
1043 * to 4096 */
1044 len = *lenp;
1045 if (!len || len > 4096 || len < 4) {
1046 errno = EINVAL;
1047 return -1;
1048 }
1049
1050 if (offset < 0 || offset >= args->len || offset + len > args->len) {
1051 errno = EINVAL;
1052 return -1;
1053 }
1054
1055 ndw = (len >> 2) - 1;
1056
1057 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1058 nvme_admin_get_log_page);
1059 req_hdr.cdw1 = cpu_to_le32(args->nsid);
1060 req_hdr.cdw10 = cpu_to_le32((ndw & 0xffff) << 16 |
1061 ((!final || args->rae) ? 1 : 0) << 15 |
1062 args->lsp << 8 |
1063 (args->lid & 0xff));
1064 req_hdr.cdw11 = cpu_to_le32(args->lsi << 16 |
1065 ndw >> 16);
1066 req_hdr.cdw12 = cpu_to_le32(log_page_offset & 0xffffffff);
1067 req_hdr.cdw13 = cpu_to_le32(log_page_offset >> 32);
1068 req_hdr.cdw14 = cpu_to_le32(args->csi << 24 |
1069 (args->ot ? 1 : 0) << 23 |
1070 args->uuidx);
1071 req_hdr.flags = 0x1;
1072 req_hdr.dlen = cpu_to_le32(len & 0xffffffff);
1073
1074 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1075 resp.data = args->log + offset;
1076 resp.data_len = len;
1077
1078 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1079 if (rc)
1080 return rc;
1081
1082 rc = nvme_mi_admin_parse_status(&resp, args->result);
1083 if (!rc)
1084 *lenp = resp.data_len;
1085
1086 return rc;
1087 }
1088
1089 int nvme_mi_admin_get_log_page(nvme_mi_ctrl_t ctrl, __u32 xfer_size,
1090 struct nvme_get_log_args *args)
1091 {
1092 const size_t max_xfer_size = xfer_size;
1093 off_t xfer_offset;
1094 int rc = 0;
1095
1096 if (args->args_size < sizeof(*args)) {
1097 errno = EINVAL;
1098 return -1;
1099 }
1100
1101 if (args->ot && (args->len > max_xfer_size)) {
1102 errno = EINVAL;
1103 return -1;
1104 }
1105
1106 for (xfer_offset = 0; xfer_offset < args->len;) {
1107 size_t xfered_size, cur_xfer_size = max_xfer_size;
1108 bool final;
1109
1110 if (xfer_offset + cur_xfer_size > args->len)
1111 cur_xfer_size = args->len - xfer_offset;
1112
1113 xfered_size = cur_xfer_size;
1114
1115 final = xfer_offset + cur_xfer_size >= args->len;
1116
1117 /* xfered_size is used as both input and output parameter */
1118 rc = __nvme_mi_admin_get_log(ctrl, args, xfer_offset,
1119 &xfered_size, final);
1120 if (rc)
1121 break;
1122
1123 xfer_offset += xfered_size;
1124 /* if we returned less data than expected, consider that
1125 * the end of the log page */
1126 if (xfered_size != cur_xfer_size)
1127 break;
1128 }
1129
1130 if (!rc)
1131 args->len = xfer_offset;
1132
1133 return rc;
1134 }
1135
1136 int nvme_mi_admin_get_log(nvme_mi_ctrl_t ctrl, struct nvme_get_log_args *args)
1137 {
1138 return nvme_mi_admin_get_log_page(ctrl, 4096, args);
1139 }
1140
1141 static int read_ana_chunk(nvme_mi_ctrl_t ctrl, enum nvme_log_ana_lsp lsp, bool rae,
1142 __u8 *log, __u8 **read, __u8 *to_read, __u8 *log_end)
1143 {
1144 if (to_read > log_end) {
1145 errno = ENOSPC;
1146 return -1;
1147 }
1148
1149 while (*read < to_read) {
1150 __u32 len = min_t(__u32, log_end - *read, NVME_LOG_PAGE_PDU_SIZE);
1151 int ret;
1152
1153 ret = nvme_mi_admin_get_log_ana(ctrl, lsp, rae,
1154 *read - log, len, *read);
1155 if (ret)
1156 return ret;
1157
1158 *read += len;
1159 }
1160 return 0;
1161 }
1162
1163 static int try_read_ana(nvme_mi_ctrl_t ctrl, enum nvme_log_ana_lsp lsp, bool rae,
1164 struct nvme_ana_log *log, __u8 *log_end,
1165 __u8 *read, __u8 **to_read, bool *may_retry)
1166 {
1167 __u16 ngrps = le16_to_cpu(log->ngrps);
1168
1169 while (ngrps--) {
1170 __u8 *group = *to_read;
1171 int ret;
1172 __le32 nnsids;
1173
1174 *to_read += sizeof(*log->descs);
1175 ret = read_ana_chunk(ctrl, lsp, rae,
1176 (__u8 *)log, &read, *to_read, log_end);
1177 if (ret) {
1178 /*
1179 * If the provided buffer isn't long enough,
1180 * the log page may have changed while reading it
1181 * and the computed length was inaccurate.
1182 * Have the caller check chgcnt and retry.
1183 */
1184 *may_retry = errno == ENOSPC;
1185 return ret;
1186 }
1187
1188 /*
1189 * struct nvme_ana_group_desc has 8-byte alignment
1190 * but the group pointer is only 4-byte aligned.
1191 * Don't dereference the misaligned pointer.
1192 */
1193 memcpy(&nnsids,
1194 group + offsetof(struct nvme_ana_group_desc, nnsids),
1195 sizeof(nnsids));
1196 *to_read += le32_to_cpu(nnsids) * sizeof(__le32);
1197 ret = read_ana_chunk(ctrl, lsp, rae,
1198 (__u8 *)log, &read, *to_read, log_end);
1199 if (ret) {
1200 *may_retry = errno == ENOSPC;
1201 return ret;
1202 }
1203 }
1204
1205 *may_retry = true;
1206 return 0;
1207 }
1208
1209 int nvme_mi_admin_get_ana_log_atomic(nvme_mi_ctrl_t ctrl, bool rgo, bool rae,
1210 unsigned int retries,
1211 struct nvme_ana_log *log, __u32 *len)
1212 {
1213 const enum nvme_log_ana_lsp lsp =
1214 rgo ? NVME_LOG_ANA_LSP_RGO_GROUPS_ONLY : 0;
1215 /* Get Log Page can only fetch multiples of dwords */
1216 __u8 * const log_end = (__u8 *)log + (*len & -4);
1217 __u8 *read = (__u8 *)log;
1218 __u8 *to_read;
1219 int ret;
1220
1221 if (!retries) {
1222 errno = EINVAL;
1223 return -1;
1224 }
1225
1226 to_read = (__u8 *)log->descs;
1227 ret = read_ana_chunk(ctrl, lsp, rae,
1228 (__u8 *)log, &read, to_read, log_end);
1229 if (ret)
1230 return ret;
1231
1232 do {
1233 bool may_retry = false;
1234 int saved_ret;
1235 int saved_errno;
1236 __le64 chgcnt;
1237
1238 saved_ret = try_read_ana(ctrl, lsp, rae, log, log_end,
1239 read, &to_read, &may_retry);
1240 /*
1241 * If the log page was read with multiple Get Log Page commands,
1242 * chgcnt must be checked afterwards to ensure atomicity
1243 */
1244 *len = to_read - (__u8 *)log;
1245 if (*len <= NVME_LOG_PAGE_PDU_SIZE || !may_retry)
1246 return saved_ret;
1247
1248 saved_errno = errno;
1249 chgcnt = log->chgcnt;
1250 read = (__u8 *)log;
1251 to_read = (__u8 *)log->descs;
1252 ret = read_ana_chunk(ctrl, lsp, rae,
1253 (__u8 *)log, &read, to_read, log_end);
1254 if (ret)
1255 return ret;
1256
1257 if (log->chgcnt == chgcnt) {
1258 /* Log hasn't changed; return try_read_ana() result */
1259 errno = saved_errno;
1260 return saved_ret;
1261 }
1262 } while (--retries);
1263
1264 errno = EAGAIN;
1265 return -1;
1266 }
1267
1268 int nvme_mi_admin_security_send(nvme_mi_ctrl_t ctrl,
1269 struct nvme_security_send_args *args)
1270 {
1271
1272 struct nvme_mi_admin_resp_hdr resp_hdr;
1273 struct nvme_mi_admin_req_hdr req_hdr;
1274 struct nvme_mi_resp resp;
1275 struct nvme_mi_req req;
1276 int rc;
1277
1278 if (args->args_size < sizeof(*args)) {
1279 errno = EINVAL;
1280 return -1;
1281 }
1282
1283 if (args->data_len > 4096) {
1284 errno = EINVAL;
1285 return -1;
1286 }
1287
1288 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1289 nvme_admin_security_send);
1290
1291 req_hdr.cdw10 = cpu_to_le32(args->secp << 24 |
1292 args->spsp1 << 16 |
1293 args->spsp0 << 8 |
1294 args->nssf);
1295
1296 req_hdr.cdw11 = cpu_to_le32(args->data_len & 0xffffffff);
1297
1298 req_hdr.flags = 0x1;
1299 req_hdr.dlen = cpu_to_le32(args->data_len & 0xffffffff);
1300 req.data = args->data;
1301 req.data_len = args->data_len;
1302
1303 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1304
1305 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1306 if (rc)
1307 return rc;
1308
1309 return nvme_mi_admin_parse_status(&resp, args->result);
1310 }
1311
1312 int nvme_mi_admin_security_recv(nvme_mi_ctrl_t ctrl,
1313 struct nvme_security_receive_args *args)
1314 {
1315
1316 struct nvme_mi_admin_resp_hdr resp_hdr;
1317 struct nvme_mi_admin_req_hdr req_hdr;
1318 struct nvme_mi_resp resp;
1319 struct nvme_mi_req req;
1320 int rc;
1321
1322 if (args->args_size < sizeof(*args)) {
1323 errno = EINVAL;
1324 return -1;
1325 }
1326
1327 if (args->data_len > 4096) {
1328 errno = EINVAL;
1329 return -1;
1330 }
1331
1332 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1333 nvme_admin_security_recv);
1334
1335 req_hdr.cdw10 = cpu_to_le32(args->secp << 24 |
1336 args->spsp1 << 16 |
1337 args->spsp0 << 8 |
1338 args->nssf);
1339
1340 req_hdr.cdw11 = cpu_to_le32(args->data_len & 0xffffffff);
1341
1342 req_hdr.flags = 0x1;
1343 req_hdr.dlen = cpu_to_le32(args->data_len & 0xffffffff);
1344
1345 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1346 resp.data = args->data;
1347 resp.data_len = args->data_len;
1348
1349 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1350 if (rc)
1351 return rc;
1352
1353 rc = nvme_mi_admin_parse_status(&resp, args->result);
1354 if (rc)
1355 return rc;
1356
1357 args->data_len = resp.data_len;
1358
1359 return 0;
1360 }
1361
1362 int nvme_mi_admin_get_features(nvme_mi_ctrl_t ctrl,
1363 struct nvme_get_features_args *args)
1364 {
1365 struct nvme_mi_admin_resp_hdr resp_hdr;
1366 struct nvme_mi_admin_req_hdr req_hdr;
1367 struct nvme_mi_resp resp;
1368 struct nvme_mi_req req;
1369 int rc;
1370
1371 if (args->args_size < sizeof(*args)) {
1372 errno = EINVAL;
1373 return -1;
1374 }
1375
1376 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1377 nvme_admin_get_features);
1378
1379 req_hdr.cdw1 = cpu_to_le32(args->nsid);
1380 req_hdr.cdw10 = cpu_to_le32((args->sel & 0x7) << 8 | args->fid);
1381 req_hdr.cdw14 = cpu_to_le32(args->uuidx & 0x7f);
1382 req_hdr.cdw11 = cpu_to_le32(args->cdw11);
1383
1384 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1385 resp.data = args->data;
1386 resp.data_len = args->data_len;
1387
1388 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1389 if (rc)
1390 return rc;
1391
1392 rc = nvme_mi_admin_parse_status(&resp, args->result);
1393 if (rc)
1394 return rc;
1395
1396 args->data_len = resp.data_len;
1397
1398 return 0;
1399 }
1400
1401 static int __nvme_mi_admin_get_features(nvme_mi_ctrl_t ctrl, enum nvme_features_id fid,
1402 enum nvme_get_features_sel sel, __u32 *result)
1403 {
1404 struct nvme_get_features_args args = {
1405 .args_size = sizeof(args),
1406 .fid = fid,
1407 .nsid = NVME_NSID_NONE,
1408 .sel = sel,
1409 .cdw11 = 0,
1410 .uuidx = NVME_UUID_NONE,
1411 .data_len = 0,
1412 .data = NULL,
1413 .timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
1414 .result = result,
1415 };
1416
1417 return nvme_mi_admin_get_features(ctrl, &args);
1418 }
1419
1420 int nvme_mi_admin_get_features_arbitration(nvme_mi_ctrl_t ctrl, enum nvme_get_features_sel sel,
1421 __u32 *result)
1422 {
1423 return __nvme_mi_admin_get_features(ctrl, NVME_FEAT_FID_ARBITRATION, sel, result);
1424 }
1425
1426 int nvme_mi_admin_get_features_power_mgmt(nvme_mi_ctrl_t ctrl, enum nvme_get_features_sel sel,
1427 __u32 *result)
1428 {
1429 return __nvme_mi_admin_get_features(ctrl, NVME_FEAT_FID_POWER_MGMT, sel, result);
1430 }
1431
1432 int nvme_mi_admin_set_features(nvme_mi_ctrl_t ctrl,
1433 struct nvme_set_features_args *args)
1434 {
1435 struct nvme_mi_admin_resp_hdr resp_hdr;
1436 struct nvme_mi_admin_req_hdr req_hdr;
1437 struct nvme_mi_resp resp;
1438 struct nvme_mi_req req;
1439 int rc;
1440
1441 if (args->args_size < sizeof(*args)) {
1442 errno = EINVAL;
1443 return -1;
1444 }
1445
1446 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1447 nvme_admin_set_features);
1448
1449 req_hdr.cdw1 = cpu_to_le32(args->nsid);
1450 req_hdr.cdw10 = cpu_to_le32((__u32)!!args->save << 31 |
1451 (args->fid & 0xff));
1452 req_hdr.cdw14 = cpu_to_le32(args->uuidx & 0x7f);
1453 req_hdr.cdw11 = cpu_to_le32(args->cdw11);
1454 req_hdr.cdw12 = cpu_to_le32(args->cdw12);
1455 req_hdr.cdw13 = cpu_to_le32(args->cdw13);
1456 req_hdr.cdw15 = cpu_to_le32(args->cdw15);
1457
1458 req.data_len = args->data_len;
1459 req.data = args->data;
1460
1461 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1462
1463 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1464 if (rc)
1465 return rc;
1466
1467 rc = nvme_mi_admin_parse_status(&resp, args->result);
1468 if (rc)
1469 return rc;
1470
1471 args->data_len = resp.data_len;
1472
1473 return 0;
1474 }
1475
1476 static int __nvme_mi_admin_set_features(nvme_mi_ctrl_t ctrl, __u8 fid, __u32 cdw11, bool save,
1477 __u32 *result)
1478 {
1479 struct nvme_set_features_args args = {
1480 .args_size = sizeof(args),
1481 .nsid = NVME_NSID_NONE,
1482 .cdw11 = cdw11,
1483 .cdw12 = 0,
1484 .save = save,
1485 .uuidx = NVME_UUID_NONE,
1486 .cdw15 = 0,
1487 .data_len = 0,
1488 .data = NULL,
1489 .timeout = NVME_DEFAULT_IOCTL_TIMEOUT,
1490 .result = result,
1491 };
1492 return nvme_mi_admin_set_features(ctrl, &args);
1493 }
1494
1495 int nvme_mi_admin_set_features_power_mgmt(nvme_mi_ctrl_t ctrl, __u8 ps, __u8 wh, bool save,
1496 __u32 *result)
1497 {
1498 __u32 value = NVME_SET(ps, FEAT_PWRMGMT_PS) | NVME_SET(wh, FEAT_PWRMGMT_WH);
1499
1500 return __nvme_mi_admin_set_features(ctrl, NVME_FEAT_FID_POWER_MGMT, value, save, result);
1501 }
1502
1503 int nvme_mi_admin_ns_mgmt(nvme_mi_ctrl_t ctrl,
1504 struct nvme_ns_mgmt_args *args)
1505 {
1506 const size_t size_v1 = sizeof_args(struct nvme_ns_mgmt_args, csi, __u64);
1507 const size_t size_v2 = sizeof_args(struct nvme_ns_mgmt_args, data, __u64);
1508 struct nvme_mi_admin_resp_hdr resp_hdr;
1509 struct nvme_mi_admin_req_hdr req_hdr;
1510 struct nvme_mi_resp resp;
1511 struct nvme_mi_req req;
1512 int rc;
1513 size_t data_len;
1514
1515 if (args->args_size < size_v1 || args->args_size > size_v2) {
1516 errno = EINVAL;
1517 return -1;
1518 }
1519
1520 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1521 nvme_admin_ns_mgmt);
1522
1523 req_hdr.cdw1 = cpu_to_le32(args->nsid);
1524 req_hdr.cdw10 = cpu_to_le32(args->sel & 0xf);
1525 req_hdr.cdw11 = cpu_to_le32(args->csi << 24);
1526
1527 if (args->args_size == size_v2) {
1528 if (args->data) {
1529 req.data = args->data;
1530 data_len = sizeof(*args->data);
1531 }
1532 }
1533 else {
1534 if (args->ns) {
1535 req.data = args->ns;
1536 data_len = sizeof(*args->ns);
1537 }
1538 }
1539
1540 if (req.data) {
1541 req.data_len = data_len;
1542 req_hdr.dlen = cpu_to_le32(data_len);
1543 req_hdr.flags = 0x1;
1544 }
1545
1546 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1547
1548 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1549 if (rc)
1550 return rc;
1551
1552 return nvme_mi_admin_parse_status(&resp, args->result);
1553 }
1554
1555 int nvme_mi_admin_ns_attach(nvme_mi_ctrl_t ctrl,
1556 struct nvme_ns_attach_args *args)
1557 {
1558 struct nvme_mi_admin_resp_hdr resp_hdr;
1559 struct nvme_mi_admin_req_hdr req_hdr;
1560 struct nvme_mi_resp resp;
1561 struct nvme_mi_req req;
1562 int rc;
1563
1564 if (args->args_size < sizeof(*args)) {
1565 errno = EINVAL;
1566 return -1;
1567 }
1568
1569 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1570 nvme_admin_ns_attach);
1571
1572 req_hdr.cdw1 = cpu_to_le32(args->nsid);
1573 req_hdr.cdw10 = cpu_to_le32(args->sel & 0xf);
1574 req.data = args->ctrlist;
1575 req.data_len = sizeof(*args->ctrlist);
1576 req_hdr.dlen = cpu_to_le32(sizeof(*args->ctrlist));
1577 req_hdr.flags = 0x1;
1578
1579 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1580
1581 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1582 if (rc)
1583 return rc;
1584
1585 return nvme_mi_admin_parse_status(&resp, args->result);
1586 }
1587
1588 int nvme_mi_admin_fw_download(nvme_mi_ctrl_t ctrl,
1589 struct nvme_fw_download_args *args)
1590 {
1591 struct nvme_mi_admin_resp_hdr resp_hdr;
1592 struct nvme_mi_admin_req_hdr req_hdr;
1593 struct nvme_mi_resp resp;
1594 struct nvme_mi_req req;
1595 int rc;
1596
1597 if (args->args_size < sizeof(*args)) {
1598 errno = EINVAL;
1599 return -1;
1600 }
1601
1602 if ((args->data_len & 0x3) || (!args->data_len)) {
1603 errno = EINVAL;
1604 return -1;
1605 }
1606
1607 if (args->offset & 0x3) {
1608 errno = EINVAL;
1609 return -1;
1610 }
1611
1612 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1613 nvme_admin_fw_download);
1614
1615 req_hdr.cdw10 = cpu_to_le32((args->data_len >> 2) - 1);
1616 req_hdr.cdw11 = cpu_to_le32(args->offset >> 2);
1617 req.data = args->data;
1618 req.data_len = args->data_len;
1619 req_hdr.dlen = cpu_to_le32(args->data_len);
1620 req_hdr.flags = 0x1;
1621
1622 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1623
1624 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1625 if (rc)
1626 return rc;
1627
1628 return nvme_mi_admin_parse_status(&resp, NULL);
1629 }
1630
1631 int nvme_mi_admin_fw_commit(nvme_mi_ctrl_t ctrl,
1632 struct nvme_fw_commit_args *args)
1633 {
1634 struct nvme_mi_admin_resp_hdr resp_hdr;
1635 struct nvme_mi_admin_req_hdr req_hdr;
1636 struct nvme_mi_resp resp;
1637 struct nvme_mi_req req;
1638 int rc;
1639
1640 if (args->args_size < sizeof(*args)) {
1641 errno = EINVAL;
1642 return -1;
1643 }
1644
1645 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1646 nvme_admin_fw_commit);
1647
1648 req_hdr.cdw10 = cpu_to_le32(((__u32)(args->bpid & 0x1) << 31) |
1649 ((args->action & 0x7) << 3) |
1650 ((args->slot & 0x7) << 0));
1651
1652 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1653
1654 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1655 if (rc)
1656 return rc;
1657
1658 return nvme_mi_admin_parse_status(&resp, NULL);
1659 }
1660
1661 int nvme_mi_admin_format_nvm(nvme_mi_ctrl_t ctrl,
1662 struct nvme_format_nvm_args *args)
1663 {
1664 struct nvme_mi_admin_resp_hdr resp_hdr;
1665 struct nvme_mi_admin_req_hdr req_hdr;
1666 struct nvme_mi_resp resp;
1667 struct nvme_mi_req req;
1668 int rc;
1669
1670 if (args->args_size < sizeof(*args)) {
1671 errno = EINVAL;
1672 return -1;
1673 }
1674
1675 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1676 nvme_admin_format_nvm);
1677
1678 req_hdr.cdw1 = cpu_to_le32(args->nsid);
1679 req_hdr.cdw10 = cpu_to_le32(((args->lbafu & 0x3) << 12)
1680 | ((args->ses & 0x7) << 9)
1681 | ((args->pil & 0x1) << 8)
1682 | ((args->pi & 0x7) << 5)
1683 | ((args->mset & 0x1) << 4)
1684 | ((args->lbaf & 0xf) << 0));
1685
1686 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1687
1688 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1689 if (rc)
1690 return rc;
1691
1692 return nvme_mi_admin_parse_status(&resp, args->result);
1693 }
1694
1695 int nvme_mi_admin_sanitize_nvm(nvme_mi_ctrl_t ctrl,
1696 struct nvme_sanitize_nvm_args *args)
1697 {
1698 struct nvme_mi_admin_resp_hdr resp_hdr;
1699 struct nvme_mi_admin_req_hdr req_hdr;
1700 struct nvme_mi_resp resp;
1701 struct nvme_mi_req req;
1702 int rc;
1703
1704 if (args->args_size < sizeof(*args)) {
1705 errno = EINVAL;
1706 return -1;
1707 }
1708
1709 nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id,
1710 nvme_admin_sanitize_nvm);
1711
1712 req_hdr.cdw10 = cpu_to_le32(((args->nodas ? 1 : 0) << 9)
1713 | ((args->oipbp ? 1 : 0) << 8)
1714 | ((args->owpass & 0xf) << 4)
1715 | ((args->ause ? 1 : 0) << 3)
1716 | ((args->sanact & 0x7) << 0));
1717 req_hdr.cdw11 = cpu_to_le32(args->ovrpat);
1718
1719 nvme_mi_admin_init_resp(&resp, &resp_hdr);
1720
1721 rc = nvme_mi_submit(ctrl->ep, &req, &resp);
1722 if (rc)
1723 return rc;
1724
1725 return nvme_mi_admin_parse_status(&resp, args->result);
1726 }
1727
1728 static void nvme_mi_mi_init_req(nvme_mi_ep_t ep,
1729 struct nvme_mi_req *req,
1730 struct nvme_mi_mi_req_hdr *hdr,
1731 __u32 cdw0, __u8 opcode)
1732 {
1733 memset(req, 0, sizeof(*req));
1734 memset(hdr, 0, sizeof(*hdr));
1735
1736 hdr->hdr.type = NVME_MI_MSGTYPE_NVME;
1737 hdr->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
1738 (NVME_MI_MT_MI << 3) |
1739 (ep->csi & 1);
1740 hdr->opcode = opcode;
1741 hdr->cdw0 = cpu_to_le32(cdw0);
1742
1743 req->hdr = &hdr->hdr;
1744 req->hdr_len = sizeof(*hdr);
1745 }
1746
1747 static int nvme_mi_read_data(nvme_mi_ep_t ep, __u32 cdw0,
1748 void *data, size_t *data_len)
1749 {
1750 struct nvme_mi_mi_resp_hdr resp_hdr;
1751 struct nvme_mi_mi_req_hdr req_hdr;
1752 struct nvme_mi_resp resp;
1753 struct nvme_mi_req req;
1754 int rc;
1755
1756 nvme_mi_mi_init_req(ep, &req, &req_hdr, cdw0,
1757 nvme_mi_mi_opcode_mi_data_read);
1758
1759 memset(&resp, 0, sizeof(resp));
1760 resp.hdr = &resp_hdr.hdr;
1761 resp.hdr_len = sizeof(resp_hdr);
1762 resp.data = data;
1763 resp.data_len = *data_len;
1764
1765 rc = nvme_mi_submit(ep, &req, &resp);
1766 if (rc)
1767 return rc;
1768
1769 if (resp_hdr.status)
1770 return resp_hdr.status;
1771
1772 *data_len = resp.data_len;
1773
1774 return 0;
1775 }
1776
1777 int nvme_mi_mi_xfer(nvme_mi_ep_t ep,
1778 struct nvme_mi_mi_req_hdr *mi_req,
1779 size_t req_data_size,
1780 struct nvme_mi_mi_resp_hdr *mi_resp,
1781 size_t *resp_data_size)
1782 {
1783 int rc;
1784 struct nvme_mi_req req;
1785 struct nvme_mi_resp resp;
1786
1787 /* There is nothing in the spec to define this limit but going with the limits
1788 * from the admin message types for DLEN seems like a reasonable starting point
1789 * to check for coding errors
1790 */
1791 const size_t mi_data_xfer_size_limit = 4096;
1792
1793 /* length/offset checks. The common _submit() API will do further
1794 * checking on the message lengths too, so these are kept specific
1795 * to the requirements of the particular command set
1796 */
1797
1798 if (*resp_data_size > mi_data_xfer_size_limit) {
1799 errno = EINVAL;
1800 return -1;
1801 }
1802
1803 mi_req->hdr.type = NVME_MI_MSGTYPE_NVME;
1804 mi_req->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
1805 (NVME_MI_MT_MI << 3) |
1806 (ep->csi & 1);
1807
1808 memset(&req, 0, sizeof(req));
1809 req.hdr = &mi_req->hdr;
1810 req.hdr_len = sizeof(*mi_req);
1811 req.data = mi_req + 1;
1812 req.data_len = req_data_size;
1813
1814 memset(&resp, 0, sizeof(resp));
1815 resp.hdr = &mi_resp->hdr;
1816 resp.hdr_len = sizeof(*mi_resp);
1817 resp.data = mi_resp + 1;
1818 resp.data_len = *resp_data_size;
1819
1820 rc = nvme_mi_submit(ep, &req, &resp);
1821 if (rc)
1822 return rc;
1823
1824 *resp_data_size = resp.data_len;
1825
1826 return 0;
1827 }
1828
1829 int nvme_mi_mi_read_mi_data_subsys(nvme_mi_ep_t ep,
1830 struct nvme_mi_read_nvm_ss_info *s)
1831 {
1832 size_t len;
1833 __u32 cdw0;
1834 int rc;
1835
1836 cdw0 = (__u8)nvme_mi_dtyp_subsys_info << 24;
1837 len = sizeof(*s);
1838
1839 rc = nvme_mi_read_data(ep, cdw0, s, &len);
1840 if (rc)
1841 return rc;
1842
1843 if (len != sizeof(*s)) {
1844 nvme_msg(ep->root, LOG_WARNING,
1845 "MI read data length mismatch: "
1846 "got %zd bytes, expected %zd\n",
1847 len, sizeof(*s));
1848 errno = EPROTO;
1849 return -1;
1850 }
1851
1852 return 0;
1853 }
1854
1855 int nvme_mi_mi_read_mi_data_port(nvme_mi_ep_t ep, __u8 portid,
1856 struct nvme_mi_read_port_info *p)
1857 {
1858 size_t len;
1859 __u32 cdw0;
1860 int rc;
1861
1862 cdw0 = ((__u8)nvme_mi_dtyp_port_info << 24) | (portid << 16);
1863 len = sizeof(*p);
1864
1865 rc = nvme_mi_read_data(ep, cdw0, p, &len);
1866 if (rc)
1867 return rc;
1868
1869 if (len != sizeof(*p)) {
1870 errno = EPROTO;
1871 return -1;
1872 }
1873
1874 return 0;
1875 }
1876
1877 int nvme_mi_mi_read_mi_data_ctrl_list(nvme_mi_ep_t ep, __u8 start_ctrlid,
1878 struct nvme_ctrl_list *list)
1879 {
1880 size_t len;
1881 __u32 cdw0;
1882 int rc;
1883
1884 cdw0 = ((__u8)nvme_mi_dtyp_ctrl_list << 24) | (start_ctrlid << 16);
1885 len = sizeof(*list);
1886
1887 rc = nvme_mi_read_data(ep, cdw0, list, &len);
1888 if (rc)
1889 return rc;
1890
1891 return 0;
1892 }
1893
1894 int nvme_mi_mi_read_mi_data_ctrl(nvme_mi_ep_t ep, __u16 ctrl_id,
1895 struct nvme_mi_read_ctrl_info *ctrl)
1896 {
1897 size_t len;
1898 __u32 cdw0;
1899 int rc;
1900
1901 cdw0 = ((__u8)nvme_mi_dtyp_ctrl_info << 24) | cpu_to_le16(ctrl_id);
1902 len = sizeof(*ctrl);
1903
1904 rc = nvme_mi_read_data(ep, cdw0, ctrl, &len);
1905 if (rc)
1906 return rc;
1907
1908 if (len != sizeof(*ctrl)) {
1909 errno = EPROTO;
1910 return -1;
1911 }
1912
1913 return 0;
1914 }
1915
1916 int nvme_mi_mi_subsystem_health_status_poll(nvme_mi_ep_t ep, bool clear,
1917 struct nvme_mi_nvm_ss_health_status *sshs)
1918 {
1919 struct nvme_mi_mi_resp_hdr resp_hdr;
1920 struct nvme_mi_mi_req_hdr req_hdr;
1921 struct nvme_mi_resp resp;
1922 struct nvme_mi_req req;
1923 int rc;
1924
1925 nvme_mi_mi_init_req(ep, &req, &req_hdr, 0,
1926 nvme_mi_mi_opcode_subsys_health_status_poll);
1927 req_hdr.cdw1 = (clear ? 1 : 0) << 31;
1928
1929 memset(&resp, 0, sizeof(resp));
1930 resp.hdr = &resp_hdr.hdr;
1931 resp.hdr_len = sizeof(resp_hdr);
1932 resp.data = sshs;
1933 resp.data_len = sizeof(*sshs);
1934
1935 rc = nvme_mi_submit(ep, &req, &resp);
1936 if (rc)
1937 return rc;
1938
1939 if (resp_hdr.status)
1940 return resp_hdr.status;
1941
1942 if (resp.data_len != sizeof(*sshs)) {
1943 nvme_msg(ep->root, LOG_WARNING,
1944 "MI Subsystem Health Status length mismatch: "
1945 "got %zd bytes, expected %zd\n",
1946 resp.data_len, sizeof(*sshs));
1947 errno = EPROTO;
1948 return -1;
1949 }
1950
1951 return 0;
1952 }
1953
1954 int nvme_mi_mi_config_set_get_ex(nvme_mi_ep_t ep, __u8 opcode, __u32 dw0,
1955 __u32 dw1, void *data_out, size_t data_out_len,
1956 void *data_in, size_t *data_in_len, __u32 *nmresp)
1957 {
1958 struct nvme_mi_mi_resp_hdr resp_hdr;
1959 struct nvme_mi_mi_req_hdr req_hdr;
1960 struct nvme_mi_resp resp;
1961 struct nvme_mi_req req;
1962 int rc;
1963
1964 nvme_mi_mi_init_req(ep, &req, &req_hdr, dw0, opcode);
1965 req_hdr.cdw1 = cpu_to_le32(dw1);
1966 req.data = data_out;
1967 req.data_len = data_out_len;
1968
1969 memset(&resp, 0, sizeof(resp));
1970 resp.hdr = &resp_hdr.hdr;
1971 resp.hdr_len = sizeof(resp_hdr);
1972 resp.data = data_in;
1973 resp.data_len = *data_in_len;
1974
1975 rc = nvme_mi_submit(ep, &req, &resp);
1976 if (rc)
1977 return rc;
1978
1979 if (resp_hdr.status)
1980 return resp_hdr.status;
1981
1982 *data_in_len = resp.data_len;
1983
1984 if (nmresp) {
1985 *nmresp = resp_hdr.nmresp[0] |
1986 resp_hdr.nmresp[1] << 8 |
1987 resp_hdr.nmresp[2] << 16;
1988 }
1989
1990 return 0;
1991 }
1992
1993 int nvme_mi_mi_config_get(nvme_mi_ep_t ep, __u32 dw0, __u32 dw1,
1994 __u32 *nmresp)
1995 {
1996 size_t data_in_len = 0;
1997
1998 return nvme_mi_mi_config_set_get_ex(ep,
1999 nvme_mi_mi_opcode_configuration_get,
2000 dw0,
2001 dw1,
2002 NULL,
2003 0,
2004 NULL,
2005 &data_in_len,
2006 nmresp);
2007 }
2008
2009 int nvme_mi_mi_config_set(nvme_mi_ep_t ep, __u32 dw0, __u32 dw1)
2010 {
2011 size_t data_in_len = 0;
2012
2013 return nvme_mi_mi_config_set_get_ex(ep,
2014 nvme_mi_mi_opcode_configuration_set,
2015 dw0,
2016 dw1,
2017 NULL,
2018 0,
2019 NULL,
2020 &data_in_len,
2021 NULL);
2022 }
2023
2024 int nvme_mi_mi_config_get_async_event(nvme_mi_ep_t ep,
2025 __u8 *aeelver,
2026 struct nvme_mi_aem_supported_list *list,
2027 size_t *list_num_bytes)
2028 {
2029
2030 __u32 dw0 = NVME_MI_CONFIG_AE;
2031 __u32 aeelvertemp = 0;
2032
2033 int rc = nvme_mi_mi_config_set_get_ex(ep,
2034 nvme_mi_mi_opcode_configuration_get,
2035 dw0,
2036 0,
2037 NULL,
2038 0,
2039 list,
2040 list_num_bytes,
2041 &aeelvertemp);
2042
2043 if (rc)
2044 return rc;
2045
2046 *aeelver = 0x000F & aeelvertemp;
2047
2048 return 0;
2049 }
2050
2051 int nvme_mi_mi_config_set_async_event(nvme_mi_ep_t ep,
2052 bool envfa,
2053 bool empfa,
2054 bool encfa,
2055 __u8 aemd,
2056 __u8 aerd,
2057 struct nvme_mi_aem_enable_list *enable_list,
2058 size_t enable_list_size,
2059 struct nvme_mi_aem_occ_list_hdr *occ_list,
2060 size_t *occ_list_size)
2061 {
2062
2063 __u32 dw0 = ((__u32)envfa << 26) |
2064 ((__u32)empfa << 25) |
2065 ((__u32)encfa << 24) |
2066 ((__u32)aemd << 16) |
2067 ((__u16) aerd << 8) | NVME_MI_CONFIG_AE;
2068
2069 //Basic checks here on lengths
2070 if (enable_list_size < sizeof(struct nvme_mi_aem_enable_list) ||
2071 (sizeof(struct nvme_mi_aem_enable_list) +
2072 enable_list->hdr.numaee * sizeof(struct nvme_mi_aem_enable_item)
2073 > enable_list_size)
2074 ) {
2075 errno = EINVAL;
2076 return -1;
2077 }
2078
2079 //Some very baseic header checks
2080 if (enable_list->hdr.aeelhl != sizeof(struct nvme_mi_aem_enable_list_header) ||
2081 enable_list->hdr.aeelver != 0) {
2082 errno = EINVAL;
2083 return -1;
2084 }
2085
2086 return nvme_mi_mi_config_set_get_ex(ep,
2087 nvme_mi_mi_opcode_configuration_set,
2088 dw0,
2089 0,
2090 enable_list,
2091 enable_list_size,
2092 occ_list,
2093 occ_list_size,
2094 NULL);
2095 }
2096
2097
2098 void nvme_mi_close(nvme_mi_ep_t ep)
2099 {
2100 struct nvme_mi_ctrl *ctrl, *tmp;
2101
2102 /* don't look for controllers during destruction */
2103 ep->controllers_scanned = true;
2104
2105 nvme_mi_for_each_ctrl_safe(ep, ctrl, tmp)
2106 nvme_mi_close_ctrl(ctrl);
2107
2108 if (ep->transport && ep->transport->close)
2109 ep->transport->close(ep);
2110 list_del(&ep->root_entry);
2111 free(ep);
2112 }
2113
2114 void nvme_mi_close_ctrl(nvme_mi_ctrl_t ctrl)
2115 {
2116 list_del(&ctrl->ep_entry);
2117 free(ctrl);
2118 }
2119
2120 char *nvme_mi_endpoint_desc(nvme_mi_ep_t ep)
2121 {
2122 char tsbuf[101], *s = NULL;
2123 size_t tslen;
2124 int rc;
2125
2126 rc = -1;
2127 memset(tsbuf, 0, sizeof(tsbuf));
2128 if (ep->transport->desc_ep)
2129 rc = ep->transport->desc_ep(ep, tsbuf, sizeof(tsbuf) - 1);
2130
2131 if (!rc) {
2132 /* don't overflow if the transport gives us an invalid string */
2133 tsbuf[sizeof(tsbuf)-1] = '\0';
2134 tslen = strlen(tsbuf);
2135 } else {
2136 tslen = 0;
2137 }
2138
2139 if (tslen)
2140 rc = asprintf(&s, "%s: %s", ep->transport->name, tsbuf);
2141 else
2142 rc = asprintf(&s, "%s endpoint", ep->transport->name);
2143
2144 if (rc < 0)
2145 return NULL;
2146
2147 return s;
2148 }
2149
2150 nvme_mi_ep_t nvme_mi_first_endpoint(nvme_root_t m)
2151 {
2152 return list_top(&m->endpoints, struct nvme_mi_ep, root_entry);
2153 }
2154
2155 nvme_mi_ep_t nvme_mi_next_endpoint(nvme_root_t m, nvme_mi_ep_t ep)
2156 {
2157 return ep ? list_next(&m->endpoints, ep, root_entry) : NULL;
2158 }
2159
2160 nvme_mi_ctrl_t nvme_mi_first_ctrl(nvme_mi_ep_t ep)
2161 {
2162 return list_top(&ep->controllers, struct nvme_mi_ctrl, ep_entry);
2163 }
2164
2165 nvme_mi_ctrl_t nvme_mi_next_ctrl(nvme_mi_ep_t ep, nvme_mi_ctrl_t c)
2166 {
2167 return c ? list_next(&ep->controllers, c, ep_entry) : NULL;
2168 }
2169
2170
2171 static const char *const mi_status[] = {
2172 [NVME_MI_RESP_MPR] = "More Processing Required: The command message is in progress and requires more time to complete processing",
2173 [NVME_MI_RESP_INTERNAL_ERR] = "Internal Error: The request message could not be processed due to a vendor-specific error",
2174 [NVME_MI_RESP_INVALID_OPCODE] = "Invalid Command Opcode",
2175 [NVME_MI_RESP_INVALID_PARAM] = "Invalid Parameter",
2176 [NVME_MI_RESP_INVALID_CMD_SIZE] = "Invalid Command Size: The size of the message body of the request was different than expected",
2177 [NVME_MI_RESP_INVALID_INPUT_SIZE] = "Invalid Command Input Data Size: The command requires data and contains too much or too little data",
2178 [NVME_MI_RESP_ACCESS_DENIED] = "Access Denied. Processing prohibited due to a vendor-specific mechanism of the Command and Feature lockdown function",
2179 [NVME_MI_RESP_VPD_UPDATES_EXCEEDED] = "VPD Updates Exceeded",
2180 [NVME_MI_RESP_PCIE_INACCESSIBLE] = "PCIe Inaccessible. The PCIe functionality is not available at this time",
2181 [NVME_MI_RESP_MEB_SANITIZED] = "Management Endpoint Buffer Cleared Due to Sanitize",
2182 [NVME_MI_RESP_ENC_SERV_FAILURE] = "Enclosure Services Failure",
2183 [NVME_MI_RESP_ENC_SERV_XFER_FAILURE] = "Enclosure Services Transfer Failure: Communication with the Enclosure Services Process has failed",
2184 [NVME_MI_RESP_ENC_FAILURE] = "An unrecoverable enclosure failure has been detected by the Enclosuer Services Process",
2185 [NVME_MI_RESP_ENC_XFER_REFUSED] = "Enclosure Services Transfer Refused: The NVM Subsystem or Enclosure Services Process indicated an error or an invalid format in communication",
2186 [NVME_MI_RESP_ENC_FUNC_UNSUP] = "Unsupported Enclosure Function: An SES Send command has been attempted to a simple Subenclosure",
2187 [NVME_MI_RESP_ENC_SERV_UNAVAIL] = "Enclosure Services Unavailable: The NVM Subsystem or Enclosure Services Process has encountered an error but may become available again",
2188 [NVME_MI_RESP_ENC_DEGRADED] = "Enclosure Degraded: A noncritical failure has been detected by the Enclosure Services Process",
2189 [NVME_MI_RESP_SANITIZE_IN_PROGRESS] = "Sanitize In Progress: The requested command is prohibited while a sanitize operation is in progress",
2190 };
2191
2192 /* kept in mi.c while we have a split libnvme/libnvme-mi; consider moving
2193 * to utils.c (with nvme_status_to_string) if we ever merge. */
2194 const char *nvme_mi_status_to_string(int status)
2195 {
2196 const char *s = "Unknown status";
2197
2198 if (status < ARRAY_SIZE(mi_status) && mi_status[status])
2199 s = mi_status[status];
2200
2201 return s;
2202 }
2203
2204 bool nvme_mi_aem_aeei_get_aee(__le16 aeei)
2205 {
2206 return !!(le16_to_cpu(aeei) & 0x8000);
2207 }
2208
2209 __u8 nvme_mi_aem_aeei_get_aeeid(__le16 aeei)
2210 {
2211 return (le16_to_cpu(aeei) & 0xFF);
2212 }
2213
2214 void nvme_mi_aem_aeei_set_aeeid(struct nvme_mi_aem_enable_item *item, __u8 aeeid)
2215 {
2216 __u16 temp = le16_to_cpu(item->aeei);
2217
2218 item->aeei = cpu_to_le16((temp & 0xFF00) | aeeid);
2219 }
2220
2221 void nvme_mi_aem_aeei_set_aee(struct nvme_mi_aem_enable_item *item, bool enabled)
2222 {
2223 __u16 temp = le16_to_cpu(item->aeei);
2224 __u8 bit = (enabled) ? 1 : 0;
2225
2226 item->aeei = cpu_to_le16((temp & 0xFF) | (bit << 15));
2227 }
2228
2229 bool nvme_mi_aem_aesi_get_aese(__le16 aesi)
2230 {
2231 return !!(le16_to_cpu(aesi) & 0x8000);
2232 }
2233
2234 __u8 nvme_mi_aem_aesi_get_aesid(__le16 aesi)
2235 {
2236 return le16_to_cpu(aesi) & 0xff;
2237 }
2238
2239 void nvme_mi_aem_aesi_set_aesid(struct nvme_mi_aem_supported_item *item, __u8 aesid)
2240 {
2241 __u16 temp = le16_to_cpu(item->aesi);
2242
2243 item->aesi = cpu_to_le16((temp & 0xFF00) | aesid);
2244 }
2245
2246 void nvme_mi_aem_aesi_set_aee(struct nvme_mi_aem_supported_item *item, bool enabled)
2247 {
2248 __u16 temp = le16_to_cpu(item->aesi);
2249 __u8 bit = (enabled) ? 1 : 0;
2250
2251 item->aesi = cpu_to_le16((temp & 0xFF) | (bit << 15));
2252 }
2253
2254 __u8 nvme_mi_aem_aemti_get_aemgn(__u8 aemti)
2255 {
2256 return aemti >> 3 & 0x1f;
2257 }
2258
2259 __u32 nvme_mi_aem_aeolli_get_aeoltl(__u8 *aeolli)
2260 {
2261 //First 23-bits contain the aeoltl
2262 __u32 aeoltl = aeolli[0] | (aeolli[1] << 8) | (aeolli[2] << 16);
2263
2264 return aeoltl & 0x7FFFFF;
2265 }
2266
2267 void nvme_mi_aem_aeolli_set_aeoltl(struct nvme_mi_aem_occ_list_hdr *hdr, __u32 aeoltl)
2268 {
2269 hdr->aeolli[0] = aeoltl & 0xFF;
2270 hdr->aeolli[1] = (aeoltl >> 8) & 0xFF;
2271 hdr->aeolli[2] = (hdr->aeolli[2] & 0b10000000) | ((aeoltl >> 16) & 0x7F);
2272 }
2273
2274 static int validate_enabled_list(struct nvme_mi_aem_supported_list *list, size_t len)
2275 {
2276 if (list->hdr.aeslver != 0) {
2277 errno = EPROTO;
2278 return -1;
2279 }
2280 if (list->hdr.aeslhl != sizeof(struct nvme_mi_aem_supported_list)) {
2281 errno = EPROTO;
2282 return -1;
2283 }
2284 if (list->hdr.aest > len ||
2285 list->hdr.aest !=
2286 list->hdr.aeslhl + list->hdr.numaes * sizeof(struct nvme_mi_aem_supported_item)) {
2287 errno = EPROTO;
2288 return -1;
2289 }
2290 return 0;
2291 }
2292 static int validate_occ_list_update_ctx(
2293 struct nvme_mi_aem_occ_list_hdr *occ_header,
2294 size_t len,
2295 struct nvme_mi_aem_ctx *ctx,
2296 bool check_generation_num)
2297 {
2298 //Make sure header fields have valid data
2299 if (len < sizeof(*occ_header)) {
2300 errno = EPROTO;
2301 goto err_cleanup;
2302 } else if (occ_header->aelver != 0 ||
2303 occ_header->aeolhl != sizeof(*occ_header)) {
2304 //Make sure header is the right version and length
2305 errno = EPROTO;
2306 goto err_cleanup;
2307 } else if (nvme_mi_aem_aeolli_get_aeoltl(occ_header->aeolli) > len) {
2308 //Full length is bigger than the data that was received
2309 errno = EPROTO;
2310 goto err_cleanup;
2311 } else if (check_generation_num &&
2312 ctx->last_generation_num ==
2313 (int) nvme_mi_aem_aemti_get_aemgn(occ_header->aemti)) {
2314 //This is a duplicate and shouldn't be parsed.
2315 //Let's just act like there's no updates
2316 occ_header->numaeo = 0;
2317 } else if (check_generation_num) {
2318 ctx->last_generation_num =
2319 nvme_mi_aem_aemti_get_aemgn(occ_header->aemti);
2320 }
2321
2322 //Header is fine. Let's go through the data
2323 //First, we should update our context appropriately
2324 ctx->occ_header = occ_header;
2325
2326 //Data starts after header
2327 ctx->list_current = (struct nvme_mi_aem_occ_data *) (occ_header + 1);
2328 ctx->list_current_index = 0;
2329 ctx->list_start = ctx->list_current;
2330
2331 struct nvme_mi_aem_occ_data *current = ctx->list_current;
2332 size_t bytes_so_far = ctx->occ_header->aeolhl;
2333
2334 for (int i = 0; i < occ_header->numaeo; i++) {
2335 //Validate this item
2336 if (current->aelhlen != sizeof(*current)) {
2337 errno = EPROTO;
2338 goto err_cleanup;
2339 } else if (!ctx->callbacks.enabled_map.enabled[current->aeoui.aeoi]) {
2340 //This is unexpected as this AE shouldn't be enabled
2341 errno = EPROTO;
2342 goto err_cleanup;
2343 }
2344
2345 //Okay, check data lengths, including this header and the specific data(s)
2346 uint32_t offset = sizeof(*current) + current->aeosil + current->aeovsil;
2347
2348 bytes_so_far += offset;
2349 if (bytes_so_far > nvme_mi_aem_aeolli_get_aeoltl(occ_header->aeolli)) {
2350 errno = EPROTO;
2351 goto err_cleanup;
2352 }
2353
2354 current = (struct nvme_mi_aem_occ_data *)((uint8_t *)current + offset);
2355 }
2356
2357 return 0;
2358
2359 err_cleanup:
2360 return -1;
2361 }
2362
2363 int nvme_mi_aem_get_fd(nvme_mi_ep_t ep)
2364 {
2365 if (!ep || !ep->aem_ctx || !ep->transport || !ep->transport->aem_fd)
2366 return -1;
2367
2368 return ep->transport->aem_fd(ep);
2369 }
2370
2371 static void reset_list_info(struct nvme_mi_aem_ctx *ctx)
2372 {
2373 //Reset context information
2374 ctx->list_current_index = -1;
2375 ctx->list_start = NULL;
2376 ctx->list_current = NULL;
2377 ctx->occ_header = NULL;
2378 }
2379
2380 static int aem_sync(nvme_mi_ep_t ep,
2381 bool envfa,
2382 bool empfa,
2383 bool encfa,
2384 __u8 aemd,
2385 __u8 aerd,
2386 struct nvme_mi_aem_enable_item *items,
2387 __u8 num_items,
2388 struct nvme_mi_aem_occ_list_hdr *resp,
2389 size_t *resp_len
2390 )
2391 {
2392 size_t msg_len =
2393 sizeof(struct nvme_mi_aem_enable_list_header) +
2394 num_items * sizeof(struct nvme_mi_aem_enable_item);
2395
2396 struct nvme_mi_aem_enable_list_header *request = malloc(msg_len);
2397
2398 if (!request)
2399 return -1;
2400
2401 request->aeelhl = sizeof(struct nvme_mi_aem_enable_list_header);
2402 request->numaee = num_items;
2403 request->aeelver = 0;
2404 request->aeetl = msg_len;
2405
2406 //Data follows header
2407 struct nvme_mi_aem_enable_item *msg_items = (struct nvme_mi_aem_enable_item *)(request + 1);
2408
2409 //Let's be explicit about what's enabled and what's not
2410 for (int i = 0; i < num_items; i++) {
2411 msg_items[i] = items[i];
2412 msg_items[i].aeel = sizeof(msg_items[i]);
2413 }
2414
2415 //Send it
2416 int rc = nvme_mi_mi_config_set_async_event(ep,
2417 envfa,
2418 empfa,
2419 encfa,
2420 aemd,
2421 aerd,
2422 (struct nvme_mi_aem_enable_list *)request,
2423 msg_len,
2424 resp,
2425 resp_len);
2426
2427 free(request);
2428 return rc;
2429 }
2430
2431 static int aem_disable_enabled(nvme_mi_ep_t ep)
2432 {
2433 struct nvme_mi_aem_enabled_map already_enabled = {false};
2434 uint8_t response_buffer[4096] = {0};
2435 size_t response_len = sizeof(response_buffer);
2436 struct nvme_mi_aem_occ_list_hdr *response =
2437 (struct nvme_mi_aem_occ_list_hdr *)response_buffer;
2438
2439 // First, let's figure out if anything is already enabled that we need to
2440 // disable
2441 int rc = nvme_mi_aem_get_enabled(ep, &already_enabled);
2442
2443 if (rc)
2444 return rc;
2445
2446 int sync_data_count = 0;
2447
2448 //Add the enabled items to the list of things to disable
2449 struct nvme_mi_aem_enable_item sync_data[NUM_ENABLES] = {0};
2450
2451 for (int i = 0; i < NUM_ENABLES; i++) {
2452 if (already_enabled.enabled[i]) {
2453 nvme_mi_aem_aeei_set_aeeid(&sync_data[sync_data_count], i);
2454 nvme_mi_aem_aeei_set_aee(&sync_data[sync_data_count], false);
2455 sync_data_count++;
2456 }
2457 }
2458
2459 rc = aem_sync(ep, false, false, false, 1, 0,
2460 sync_data, sync_data_count, response, &response_len);
2461
2462 if (rc)
2463 return rc;
2464
2465 //Now, allow a purge of the aem fd because we could have
2466 //received some events during this process
2467 rc = ep->transport->aem_purge(ep);
2468
2469 return rc;
2470 }
2471
2472 int nvme_mi_aem_enable(nvme_mi_ep_t ep,
2473 struct nvme_mi_aem_config *config,
2474 void *userdata)
2475 {
2476 if (!ep || !config || !config->aem_handler)
2477 return -1;
2478
2479 int rc = nvme_mi_aem_open(ep);
2480
2481 if (rc < 0)
2482 return rc;
2483
2484 int sync_data_count = 0;
2485 struct nvme_mi_aem_enable_item sync_data[NUM_ENABLES] = {0};
2486
2487 uint8_t response_buffer[4096] = {0};
2488 size_t response_len = sizeof(response_buffer);
2489 struct nvme_mi_aem_occ_list_hdr *response =
2490 (struct nvme_mi_aem_occ_list_hdr *)response_buffer;
2491
2492 //It's possible we're already enabled
2493 if (!ep->aem_ctx)
2494 ep->aem_ctx = malloc(sizeof(*ep->aem_ctx));
2495 if (!(ep->aem_ctx))
2496 return -1;
2497
2498 memset(ep->aem_ctx, 0, sizeof(*ep->aem_ctx));
2499 ep->aem_ctx->last_generation_num = -1;//Invalid
2500 reset_list_info((ep->aem_ctx));
2501 ep->aem_ctx->callbacks = *config;
2502
2503 rc = aem_disable_enabled(ep);
2504 if (rc)
2505 goto cleanup_ctx;
2506
2507 //Now, let's do a fresh enable of what's asked
2508 for (int i = 0; i < NUM_ENABLES; i++) {
2509 if (config->enabled_map.enabled[i]) {
2510 nvme_mi_aem_aeei_set_aeeid(&sync_data[sync_data_count], i);
2511 nvme_mi_aem_aeei_set_aee(&sync_data[sync_data_count], true);
2512 sync_data_count++;
2513 }
2514 }
2515
2516 rc = aem_sync(ep, config->envfa, config->empfa,
2517 config->encfa, config->aemd, config->aerd,
2518 sync_data, sync_data_count, response, &response_len);
2519 if (rc)
2520 goto cleanup_ctx;
2521
2522 //Parse the response and fire events
2523 rc = validate_occ_list_update_ctx(response,
2524 response_len,
2525 ep->aem_ctx,
2526 false /*generation # shouldn't matter*/);
2527 if (rc)
2528 goto cleanup_ctx;
2529
2530 if (response->numaeo) {
2531 //Return value unused here
2532 config->aem_handler(ep, response->numaeo, userdata);
2533 }
2534
2535 cleanup_ctx:
2536 // Clear these because they won't point to valid memory anymore
2537 reset_list_info(ep->aem_ctx);
2538
2539 if (rc) {
2540 free(ep->aem_ctx);
2541 ep->aem_ctx = NULL;
2542 }
2543 return rc;
2544 }
2545
2546 int nvme_mi_aem_get_enabled(nvme_mi_ep_t ep,
2547 struct nvme_mi_aem_enabled_map *enabled_map)
2548 {
2549 if (!ep || !enabled_map)
2550 return -1;
2551
2552 int rc = 0;
2553
2554 unsigned char aeelver;
2555 size_t ae_list_bytes = NUM_ENABLES * sizeof(struct nvme_mi_aem_supported_list);
2556 struct nvme_mi_aem_supported_list *enabled_list = malloc(ae_list_bytes);
2557
2558 if (!enabled_list)
2559 return -1;
2560
2561 rc = nvme_mi_mi_config_get_async_event(
2562 ep, &aeelver, enabled_list, &ae_list_bytes);
2563 if (rc)
2564 goto cleanup;
2565
2566 rc = validate_enabled_list(enabled_list, ae_list_bytes);
2567 if (rc)
2568 goto cleanup;
2569
2570 memset(enabled_map, 0, sizeof(*enabled_map));
2571
2572 struct nvme_mi_aem_enable_item *items =
2573 (struct nvme_mi_aem_enable_item *)(enabled_list + 1);
2574
2575 for (int i = 0; i < enabled_list->hdr.numaes; i++) {
2576 __u8 aeeid = nvme_mi_aem_aeei_get_aeeid(items[i].aeei);
2577 bool enabled = nvme_mi_aem_aeei_get_aee(items[i].aeei);
2578
2579 enabled_map->enabled[aeeid] = enabled;
2580 }
2581
2582 cleanup:
2583 free(enabled_list);
2584 return rc;
2585 }
2586
2587 int nvme_mi_aem_disable(nvme_mi_ep_t ep)
2588 {
2589 if (!ep)
2590 return -1;
2591
2592 int rc = aem_disable_enabled(ep);
2593
2594 free(ep->aem_ctx);
2595 ep->aem_ctx = NULL;
2596
2597 return rc;
2598 }
2599
2600 /*When inside a aem_handler, call with the aem_ctx and struct will be populated with next
2601 *event information. Will return NULL when end of parsing (or error) is occurred.
2602 *spec_info and vend_spec_info must be copied to persist as they will not be valid after
2603 *the aem_handler has returned.
2604 */
2605 struct nvme_mi_event *nvme_mi_aem_get_next_event(nvme_mi_ep_t ep)
2606 {
2607 if (!ep || !ep->aem_ctx ||
2608 !ep->aem_ctx->list_current ||
2609 ep->aem_ctx->list_current_index == -1 ||
2610 !ep->aem_ctx->occ_header) {
2611 return NULL;
2612 }
2613
2614 if (ep->aem_ctx->occ_header->numaeo <= ep->aem_ctx->list_current_index)
2615 return NULL;
2616
2617 struct nvme_mi_aem_ctx *aem_ctx = ep->aem_ctx;
2618 struct nvme_mi_aem_occ_data *current = aem_ctx->list_current;
2619
2620 aem_ctx->event.aeoi = current->aeoui.aeoi;
2621 aem_ctx->event.aessi = current->aeoui.aessi;
2622 aem_ctx->event.aeocidi = current->aeoui.aeocidi;
2623 aem_ctx->event.spec_info_len = current->aeosil;
2624 aem_ctx->event.vend_spec_info_len = current->aeovsil;
2625 //Now the pointers
2626 aem_ctx->event.spec_info = ((uint8_t *)current + current->aelhlen);
2627 aem_ctx->event.vend_spec_info =
2628 ((uint8_t *)aem_ctx->event.spec_info + aem_ctx->event.spec_info_len);
2629
2630 //Let's grab the next item (if there is any).
2631 aem_ctx->list_current_index++;
2632 aem_ctx->list_current =
2633 (struct nvme_mi_aem_occ_data *)
2634 ((uint8_t *)aem_ctx->event.vend_spec_info + aem_ctx->event.vend_spec_info_len);
2635
2636 return &aem_ctx->event;
2637 }
2638
2639 /* POLLIN has indicated events. This function reads and processes them.
2640 * A callback will likely be invoked.
2641 */
2642 int nvme_mi_aem_process(nvme_mi_ep_t ep, void *userdata)
2643 {
2644 int rc = 0;
2645 uint8_t response_buffer[4096];
2646 struct nvme_mi_aem_msg *response = (struct nvme_mi_aem_msg *)response_buffer;
2647 size_t response_len = sizeof(response_buffer) - sizeof(struct nvme_mi_aem_msg);
2648
2649 if (!ep || !ep->aem_ctx)
2650 return -1;
2651
2652 memset(response_buffer, 0, sizeof(response_buffer));
2653
2654 //Reset context information
2655 reset_list_info(ep->aem_ctx);
2656
2657 rc = nvme_mi_get_async_message(ep, response, &response_len);
2658 if (rc)
2659 goto cleanup;
2660
2661 if (!response_len) {
2662 //If no error and response length zero, we've likely received an owned
2663 //tag message from a different endpoint than this path is responsible
2664 //for monitoring.
2665 goto cleanup;
2666 }
2667
2668 //Parse the response and fire events
2669 rc = validate_occ_list_update_ctx(&response->occ_list_hdr,
2670 response_len,
2671 ep->aem_ctx,
2672 true /*Ensure unique generation number*/);
2673 if (rc)
2674 goto cleanup;
2675
2676 if (response->occ_list_hdr.numaeo) {
2677 enum nvme_mi_aem_handler_next_action action =
2678 ep->aem_ctx->callbacks.aem_handler(ep,
2679 response->occ_list_hdr.numaeo,
2680 userdata);
2681
2682 reset_list_info(ep->aem_ctx);
2683
2684 if (action == NVME_MI_AEM_HNA_ACK) {
2685 response_len = sizeof(response_buffer);
2686
2687 rc = nvme_mi_aem_ack(ep, &response->occ_list_hdr, &response_len);
2688 if (rc)
2689 goto cleanup;
2690
2691 //The Ack is not guaranteed to have data
2692 if (response_len && response->occ_list_hdr.numaeo) {
2693 rc = validate_occ_list_update_ctx(&response->occ_list_hdr,
2694 response_len,
2695 ep->aem_ctx,
2696 true);
2697 //Callbacks based on ack
2698 if (rc == 0 && response->occ_list_hdr.numaeo) {
2699 //Return value unused here
2700 ep->aem_ctx->callbacks.aem_handler(ep,
2701 response->occ_list_hdr.numaeo,
2702 userdata);
2703 }
2704 }
2705 }
2706 } else {
2707 //This is unexpected unless we have duplicates. But those shouldn't be acked
2708 }
2709
2710 cleanup:
2711 reset_list_info(ep->aem_ctx);
2712 return rc;
2713 }
2714
2715