forked from linux-nvme/libnvme
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmi.c
More file actions
2658 lines (2150 loc) · 63.9 KB
/
mi.c
File metadata and controls
2658 lines (2150 loc) · 63.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* This file is part of libnvme.
* Copyright (c) 2021 Code Construct Pty Ltd
*
* Authors: Jeremy Kerr <[email protected]>
*/
#include <errno.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <ccan/array_size/array_size.h>
#include <ccan/ccan/minmax/minmax.h>
#include <ccan/endian/endian.h>
#include "log.h"
#include "mi.h"
#include "private.h"
#define NUM_ENABLES (256u)
_Static_assert(sizeof(struct nvme_mi_aem_supported_list_header) == 5,
"size_of_nvme_mi_aem_supported_list_header_is_not_5_bytes");
_Static_assert(sizeof(struct nvme_mi_aem_supported_item) == 3,
"sizeof_nvme_mi_aem_supported_item_is_not_3_bytes");
_Static_assert(sizeof(struct nvme_mi_aem_enable_item) == 3,
"size_of_ae_enable_item_t_is_not_3_bytes");
_Static_assert(sizeof(struct nvme_mi_aem_enable_list_header) == 5,
"size_of_nvme_mi_aem_enable_list_header_is_not_5_bytes");
_Static_assert(sizeof(struct nvme_mi_aem_occ_data) == 9,
"size_of_nvme_mi_aem_occ_data_is_not_9_bytes");
_Static_assert(sizeof(struct nvme_mi_aem_occ_list_hdr) == 7,
"size_of_nvme_mi_aem_occ_list_hdr_is_not_7_bytes");
static int nvme_mi_get_async_message(nvme_mi_ep_t ep,
struct nvme_mi_aem_msg *aem_msg, size_t *aem_msg_len);
static const int default_timeout = 1000; /* milliseconds; endpoints may
override */
static bool nvme_mi_probe_enabled_default(void)
{
char *val;
val = getenv("LIBNVME_MI_PROBE_ENABLED");
if (!val)
return true;
return strcmp(val, "0") &&
strcasecmp(val, "false") &&
strncasecmp(val, "disable", 7);
}
/* MI-equivalent of nvme_create_root, but avoids clashing symbol names
* when linking against both libnvme and libnvme-mi.
*/
nvme_root_t nvme_mi_create_root(FILE *fp, int log_level)
{
struct nvme_root *r;
int fd;
r = calloc(1, sizeof(*r));
if (!r) {
errno = ENOMEM;
return NULL;
}
if (fp) {
fd = fileno(fp);
if (fd < 0) {
free(r);
return NULL;
}
} else
fd = STDERR_FILENO;
r->log.fd = fd;
r->log.level = log_level;
r->mi_probe_enabled = nvme_mi_probe_enabled_default();
list_head_init(&r->hosts);
list_head_init(&r->endpoints);
return r;
}
void nvme_mi_free_root(nvme_root_t root)
{
nvme_mi_ep_t ep, tmp;
nvme_mi_for_each_endpoint_safe(root, ep, tmp)
nvme_mi_close(ep);
free(root);
}
void nvme_mi_set_probe_enabled(nvme_root_t root, bool enabled)
{
root->mi_probe_enabled = enabled;
}
static void nvme_mi_record_resp_time(struct nvme_mi_ep *ep)
{
int rc;
rc = clock_gettime(CLOCK_MONOTONIC, &ep->last_resp_time);
ep->last_resp_time_valid = !rc;
}
static bool nvme_mi_compare_vid_mn(struct nvme_mi_ep *ep,
struct nvme_id_ctrl *id,
__u16 vid, const char *mn)
{
int len;
len = strlen(mn);
if (len >= sizeof(id->mn)) {
nvme_msg(ep->root, LOG_ERR,
"Internal error: invalid model number for %s\n",
__func__);
return false;
}
return le16_to_cpu(id->vid) == vid && !strncmp(id->mn, mn, len);
}
static void __nvme_mi_format_mn(struct nvme_id_ctrl *id,
char *mn, size_t mn_len)
{
const size_t id_mn_size = sizeof(id->mn);
int i;
/* A BUILD_ASSERT() would be nice here, but we're not const enough for
* that
*/
if (mn_len <= id_mn_size)
abort();
memcpy(mn, id->mn, id_mn_size);
mn[id_mn_size] = '\0';
for (i = id_mn_size - 1; i >= 0; i--) {
if (mn[i] != '\0' && mn[i] != ' ')
break;
mn[i] = '\0';
}
}
#define nvme_mi_format_mn(id, m) __nvme_mi_format_mn(id, m, sizeof(m))
void nvme_mi_ep_probe(struct nvme_mi_ep *ep)
{
struct nvme_identify_args id_args = { 0 };
struct nvme_id_ctrl id = { 0 };
struct nvme_mi_ctrl *ctrl;
int rc;
/* Ensure the probe occurs at most once. This isn't just to mitigate doubling
* a linear stream of commands, it also terminates recursion via the
* nvme_mi_submit() call issued by nvme_mi_admin_identify_partial() below.
*/
if (ep->quirks_probed)
return;
/* Mark ep->quirks as valid. Note that for the purpose of quirk probing,
* the quirk probe itself cannot rely on quirks, and so the fact that none are
* yet set is desirable. The request that triggered nvme_mi_submit() will have
* an initialised ep->quirks when we return from the root probe call.
*/
ep->quirks_probed = true;
if (!ep->root->mi_probe_enabled)
return;
/* start with no quirks, detect as we go */
ep->quirks = 0;
ctrl = nvme_mi_init_ctrl(ep, 0);
if (!ctrl)
return;
/* Do enough of an identify (assuming controller 0) to retrieve
* device and firmware identification information. This gives us the
* following fields in id:
*
* - vid (PCI vendor ID)
* - ssvid (PCI subsystem vendor ID)
* - sn (Serial number)
* - mn (Model number)
* - fr (Firmware revision)
*
* all other fields - rab and onwards - will be zero!
*/
id_args.args_size = sizeof(id_args);
id_args.data = &id;
id_args.cns = NVME_IDENTIFY_CNS_CTRL;
id_args.nsid = NVME_NSID_NONE;
id_args.cntid = 0;
id_args.csi = NVME_CSI_NVM;
rc = nvme_mi_admin_identify_partial(ctrl, &id_args, 0,
offsetof(struct nvme_id_ctrl, rab));
if (rc) {
nvme_msg(ep->root, LOG_WARNING,
"Identify Controller failed, no quirks applied\n");
goto out_close;
}
/* Samsung MZUL2512: cannot receive commands sent within ~1ms of
* the previous response. Set an inter-command delay of 1.2ms for
* a little extra tolerance.
*/
if (nvme_mi_compare_vid_mn(ep, &id, 0x144d, "MZUL2512HCJQ")) {
ep->quirks |= NVME_QUIRK_MIN_INTER_COMMAND_TIME;
ep->inter_command_us = 1200;
}
/* If we're quirking for the inter-command time, record the last
* command time now, so we don't conflict with the just-sent identify.
*/
if (ep->quirks & NVME_QUIRK_MIN_INTER_COMMAND_TIME)
nvme_mi_record_resp_time(ep);
if (ep->quirks) {
char tmp[sizeof(id.mn) + 1];
nvme_mi_format_mn(&id, tmp);
nvme_msg(ep->root, LOG_DEBUG,
"device %02x:%s: applying quirks 0x%08lx\n",
id.vid, tmp, ep->quirks);
}
out_close:
nvme_mi_close_ctrl(ctrl);
}
static const int nsec_per_sec = 1000 * 1000 * 1000;
/* timercmp and timersub, but for struct timespec */
#define timespec_cmp(a, b, CMP) \
(((a)->tv_sec == (b)->tv_sec) \
? ((a)->tv_nsec CMP (b)->tv_nsec) \
: ((a)->tv_sec CMP (b)->tv_sec))
#define timespec_sub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_nsec = (a)->tv_nsec - (b)->tv_nsec; \
if ((result)->tv_nsec < 0) { \
--(result)->tv_sec; \
(result)->tv_nsec += nsec_per_sec; \
} \
} while (0)
static void nvme_mi_insert_delay(struct nvme_mi_ep *ep)
{
struct timespec now, next, delay;
int rc;
if (!ep->last_resp_time_valid)
return;
/* calculate earliest next command time */
next.tv_nsec = ep->last_resp_time.tv_nsec + ep->inter_command_us * 1000;
next.tv_sec = ep->last_resp_time.tv_sec;
if (next.tv_nsec > nsec_per_sec) {
next.tv_nsec -= nsec_per_sec;
next.tv_sec += 1;
}
rc = clock_gettime(CLOCK_MONOTONIC, &now);
if (rc) {
/* not much we can do; continue immediately */
return;
}
if (timespec_cmp(&now, &next, >=))
return;
timespec_sub(&next, &now, &delay);
nanosleep(&delay, NULL);
}
struct nvme_mi_ep *nvme_mi_init_ep(nvme_root_t root)
{
struct nvme_mi_ep *ep;
ep = calloc(1, sizeof(*ep));
if (!ep)
return NULL;
list_node_init(&ep->root_entry);
ep->root = root;
ep->quirks_probed = false;
ep->controllers_scanned = false;
ep->timeout = default_timeout;
ep->mprt_max = 0;
list_head_init(&ep->controllers);
list_add(&root->endpoints, &ep->root_entry);
return ep;
}
int nvme_mi_ep_set_timeout(nvme_mi_ep_t ep, unsigned int timeout_ms)
{
if (ep->transport->check_timeout) {
int rc;
rc = ep->transport->check_timeout(ep, timeout_ms);
if (rc)
return rc;
}
ep->timeout = timeout_ms;
return 0;
}
void nvme_mi_ep_set_mprt_max(nvme_mi_ep_t ep, unsigned int mprt_max_ms)
{
ep->mprt_max = mprt_max_ms;
}
unsigned int nvme_mi_ep_get_timeout(nvme_mi_ep_t ep)
{
return ep->timeout;
}
static bool nvme_mi_ep_has_quirk(nvme_mi_ep_t ep, unsigned long quirk)
{
return ep->quirks & quirk;
}
struct nvme_mi_ctrl *nvme_mi_init_ctrl(nvme_mi_ep_t ep, __u16 ctrl_id)
{
struct nvme_mi_ctrl *ctrl;
ctrl = malloc(sizeof(*ctrl));
if (!ctrl)
return NULL;
ctrl->ep = ep;
ctrl->id = ctrl_id;
list_add_tail(&ep->controllers, &ctrl->ep_entry);
return ctrl;
}
__u16 nvme_mi_ctrl_id(nvme_mi_ctrl_t ctrl)
{
return ctrl->id;
}
int nvme_mi_scan_ep(nvme_mi_ep_t ep, bool force_rescan)
{
struct nvme_ctrl_list list;
unsigned int i, n_ctrl;
int rc;
if (ep->controllers_scanned) {
if (force_rescan) {
struct nvme_mi_ctrl *ctrl, *tmp;
nvme_mi_for_each_ctrl_safe(ep, ctrl, tmp)
nvme_mi_close_ctrl(ctrl);
} else {
return 0;
}
}
rc = nvme_mi_mi_read_mi_data_ctrl_list(ep, 0, &list);
if (rc)
return rc;
n_ctrl = le16_to_cpu(list.num);
if (n_ctrl > NVME_ID_CTRL_LIST_MAX) {
errno = EPROTO;
return -1;
}
for (i = 0; i < n_ctrl; i++) {
struct nvme_mi_ctrl *ctrl;
__u16 id;
id = le16_to_cpu(list.identifier[i]);
ctrl = nvme_mi_init_ctrl(ep, id);
if (!ctrl)
break;
}
ep->controllers_scanned = true;
return 0;
}
__u32 nvme_mi_crc32_update(__u32 crc, void *data, size_t len)
{
int i;
while (len--) {
crc ^= *(unsigned char *)(data++);
for (i = 0; i < 8; i++)
crc = (crc >> 1) ^ ((crc & 1) ? 0x82F63B78 : 0);
}
return crc;
}
static void nvme_mi_calc_req_mic(struct nvme_mi_req *req)
{
__u32 crc = 0xffffffff;
crc = nvme_mi_crc32_update(crc, req->hdr, req->hdr_len);
crc = nvme_mi_crc32_update(crc, req->data, req->data_len);
req->mic = ~crc;
}
/* returns zero on correct MIC */
static int nvme_mi_verify_resp_mic(struct nvme_mi_resp *resp)
{
__u32 crc = 0xffffffff;
crc = nvme_mi_crc32_update(crc, resp->hdr, resp->hdr_len);
crc = nvme_mi_crc32_update(crc, resp->data, resp->data_len);
return resp->mic != ~crc;
}
__attribute__((weak)) void *nvme_mi_submit_entry(__u8 type, const struct nvme_mi_msg_hdr *hdr,
size_t hdr_len, const void *data, size_t data_len)
{
return NULL;
}
__attribute__((weak)) void nvme_mi_submit_exit(__u8 type, const struct nvme_mi_msg_hdr *hdr,
size_t hdr_len, const void *data, size_t data_len,
void *user_data) { }
int nvme_mi_async_read(nvme_mi_ep_t ep, struct nvme_mi_resp *resp)
{
if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_MIN_INTER_COMMAND_TIME))
nvme_mi_record_resp_time(ep);
int rc = ep->transport->aem_read(ep, resp);
if (rc && errno == EWOULDBLOCK) {
//Sometimes we might get owned tag data from the wrong endpoint.
//This isn't an error, but we shouldn't process it here
resp->data_len = 0;//No data to process
return 0;
} else if (rc) {
nvme_msg(ep->root, LOG_INFO, "transport failure\n");
return rc;
}
if (ep->transport->mic_enabled) {
rc = nvme_mi_verify_resp_mic(resp);
if (rc) {
nvme_msg(ep->root, LOG_WARNING, "crc mismatch\n");
errno = EBADMSG;
return -1;
}
}
//TODO: There's a bunch of overlap with the nvme_mi_submit. Maybe we make common helpers
/* basic response checks */
if (resp->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
nvme_msg(ep->root, LOG_DEBUG,
"Bad response header len: %zd\n", resp->hdr_len);
errno = EPROTO;
return -1;
}
if (resp->hdr->type != NVME_MI_MSGTYPE_NVME) {
nvme_msg(ep->root, LOG_DEBUG,
"Invalid message type 0x%02x\n", resp->hdr->type);
errno = EPROTO;
return -1;
}
if (!(resp->hdr->nmp & ~(NVME_MI_ROR_REQ << 7))) {
nvme_msg(ep->root, LOG_DEBUG,
"ROR value in response indicates a response\n");
errno = EIO;
return -1;
}
if (!(resp->hdr->nmp & (NVME_MI_MT_AE << 3))) {
nvme_msg(ep->root, LOG_DEBUG,
"NMIMT does not indicate AEM\n");
resp->data_len = 0;//No data to process
return 0;
}
return 0;
}
int nvme_mi_submit(nvme_mi_ep_t ep, struct nvme_mi_req *req,
struct nvme_mi_resp *resp)
{
int rc;
void *user_data;
user_data = nvme_mi_submit_entry(req->hdr->type, req->hdr, req->hdr_len, req->data,
req->data_len);
if (req->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
errno = EINVAL;
return -1;
}
if (req->hdr_len & 0x3) {
errno = EINVAL;
return -1;
}
if (resp->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
errno = EINVAL;
return -1;
}
if (resp->hdr_len & 0x3) {
errno = EINVAL;
return -1;
}
nvme_mi_ep_probe(ep);
if (ep->transport->mic_enabled)
nvme_mi_calc_req_mic(req);
if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_MIN_INTER_COMMAND_TIME))
nvme_mi_insert_delay(ep);
rc = ep->transport->submit(ep, req, resp);
if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_MIN_INTER_COMMAND_TIME))
nvme_mi_record_resp_time(ep);
if (rc) {
nvme_msg(ep->root, LOG_INFO, "transport failure\n");
return rc;
}
if (ep->transport->mic_enabled) {
rc = nvme_mi_verify_resp_mic(resp);
if (rc) {
nvme_msg(ep->root, LOG_WARNING, "crc mismatch\n");
errno = EBADMSG;
return -1;
}
}
/* basic response checks */
if (resp->hdr_len < sizeof(struct nvme_mi_msg_hdr)) {
nvme_msg(ep->root, LOG_DEBUG,
"Bad response header len: %zd\n", resp->hdr_len);
errno = EPROTO;
return -1;
}
if (resp->hdr->type != NVME_MI_MSGTYPE_NVME) {
nvme_msg(ep->root, LOG_DEBUG,
"Invalid message type 0x%02x\n", resp->hdr->type);
errno = EPROTO;
return -1;
}
if (!(resp->hdr->nmp & (NVME_MI_ROR_RSP << 7))) {
nvme_msg(ep->root, LOG_DEBUG,
"ROR value in response indicates a request\n");
errno = EIO;
return -1;
}
if ((resp->hdr->nmp & 0x1) != (req->hdr->nmp & 0x1)) {
nvme_msg(ep->root, LOG_WARNING,
"Command slot mismatch: req %d, resp %d\n",
req->hdr->nmp & 0x1,
resp->hdr->nmp & 0x1);
errno = EIO;
return -1;
}
nvme_mi_submit_exit(resp->hdr->type, resp->hdr, resp->hdr_len, resp->data, resp->data_len,
user_data);
return 0;
}
int nvme_mi_set_csi(nvme_mi_ep_t ep, uint8_t csi)
{
uint8_t csi_bit = (csi) ? 1 : 0;
if (nvme_mi_ep_has_quirk(ep, NVME_QUIRK_CSI_1_NOT_SUPPORTED) && csi_bit)
return -1;
ep->csi = csi_bit;
return 0;
}
static void nvme_mi_admin_init_req(nvme_mi_ep_t ep,
struct nvme_mi_req *req,
struct nvme_mi_admin_req_hdr *hdr,
__u16 ctrl_id, __u8 opcode)
{
memset(req, 0, sizeof(*req));
memset(hdr, 0, sizeof(*hdr));
hdr->hdr.type = NVME_MI_MSGTYPE_NVME;
hdr->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
(NVME_MI_MT_ADMIN << 3) |
(ep->csi & 1);
hdr->opcode = opcode;
hdr->ctrl_id = cpu_to_le16(ctrl_id);
req->hdr = &hdr->hdr;
req->hdr_len = sizeof(*hdr);
}
static void nvme_mi_admin_init_resp(struct nvme_mi_resp *resp,
struct nvme_mi_admin_resp_hdr *hdr)
{
memset(resp, 0, sizeof(*resp));
resp->hdr = &hdr->hdr;
resp->hdr_len = sizeof(*hdr);
}
static void nvme_mi_control_init_req(nvme_mi_ep_t ep,
struct nvme_mi_req *req,
struct nvme_mi_control_req *control_req,
__u8 opcode, __u16 cpsp)
{
memset(req, 0, sizeof(*req));
memset(control_req, 0, sizeof(*control_req));
control_req->hdr.type = NVME_MI_MSGTYPE_NVME;
control_req->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
(NVME_MI_MT_CONTROL << 3) |
(ep->csi & 1);
control_req->opcode = opcode;
control_req->cpsp = cpu_to_le16(cpsp);
req->hdr = &control_req->hdr;
req->hdr_len = sizeof(*control_req);
}
static void nvme_mi_control_init_resp(struct nvme_mi_resp *resp,
struct nvme_mi_control_resp *control_resp)
{
memset(resp, 0, sizeof(*resp));
resp->hdr = &control_resp->hdr;
resp->hdr_len = sizeof(*control_resp);
}
static int nvme_mi_admin_parse_status(struct nvme_mi_resp *resp, __u32 *result)
{
struct nvme_mi_admin_resp_hdr *admin_hdr;
struct nvme_mi_msg_resp *resp_hdr;
__u32 nvme_status;
__u32 nvme_result;
/* we have a few different sources of "result" here: the status header
* in the MI response, the cdw3 status field, and (command specific)
* return values in cdw0. The latter is returned in the result pointer,
* the former two generate return values here
*/
if (resp->hdr_len < sizeof(*resp_hdr)) {
errno = -EPROTO;
return -1;
}
resp_hdr = (struct nvme_mi_msg_resp *)resp->hdr;
/* If we have a MI error, we can't be sure there's an admin header
* following; return just the MI status, with the status type
* indicator of MI.
*/
if (resp_hdr->status)
return resp_hdr->status |
(NVME_STATUS_TYPE_MI << NVME_STATUS_TYPE_SHIFT);
/* We shouldn't hit this, as we'd have an error reported earlier.
* However, for pointer safety, ensure we have a full admin header
*/
if (resp->hdr_len < sizeof(*admin_hdr)) {
errno = EPROTO;
return -1;
}
admin_hdr = (struct nvme_mi_admin_resp_hdr *)resp->hdr;
nvme_result = le32_to_cpu(admin_hdr->cdw0);
/* Shift down 17 here: the SC starts at bit 17, and the NVME_SC_*
* definitions align to this bit (and up). The CRD, MORE and DNR
* bits are defined accordingly (eg., DNR is 0x4000).
*/
nvme_status = le32_to_cpu(admin_hdr->cdw3) >> 17;
/* the result pointer, optionally stored if the caller needs it */
if (result)
*result = nvme_result;
return nvme_status;
}
static int nvme_mi_control_parse_status(struct nvme_mi_resp *resp, __u16 *cpsr)
{
struct nvme_mi_control_resp *control_resp;
if (resp->hdr_len < sizeof(*control_resp)) {
errno = -EPROTO;
return -1;
}
control_resp = (struct nvme_mi_control_resp *)resp->hdr;
if (control_resp->status)
return control_resp->status |
(NVME_STATUS_TYPE_MI << NVME_STATUS_TYPE_SHIFT);
if (cpsr)
*cpsr = le16_to_cpu(control_resp->cpsr);
return control_resp->status;
}
static int nvme_mi_get_async_message(nvme_mi_ep_t ep,
struct nvme_mi_aem_msg *aem_msg,
size_t *aem_msg_len)
{
struct nvme_mi_resp resp;
memset(&resp, 0, sizeof(resp));
resp.hdr = &aem_msg->hdr;
resp.hdr_len = sizeof(struct nvme_mi_msg_hdr);
resp.data = &aem_msg->occ_list_hdr;
resp.data_len = *aem_msg_len;
int rc = nvme_mi_async_read(ep, &resp);
if (rc)
return rc;
*aem_msg_len = resp.data_len;
return 0;
}
int nvme_mi_admin_xfer(nvme_mi_ctrl_t ctrl,
struct nvme_mi_admin_req_hdr *admin_req,
size_t req_data_size,
struct nvme_mi_admin_resp_hdr *admin_resp,
off_t resp_data_offset,
size_t *resp_data_size)
{
struct nvme_mi_resp resp;
struct nvme_mi_req req;
__u32 dlen, doff;
int rc;
/* length/offset checks. The common _submit() API will do further
* checking on the message lengths too, so these are kept specific
* to the requirements of the Admin command set
*/
/* NVMe-MI v1.2 imposes a limit of 4096 bytes on the dlen field */
if (*resp_data_size > 4096) {
errno = EINVAL;
return -1;
}
/* we only have 32 bits of offset */
if (resp_data_offset > 0xffffffff) {
errno = EINVAL;
return -1;
}
/* request and response lengths & offset must be aligned */
if ((req_data_size & 0x3) ||
(*resp_data_size & 0x3) ||
(resp_data_offset & 0x3)) {
errno = EINVAL;
return -1;
}
/* bidirectional not permitted (see DLEN definition) */
if (req_data_size && *resp_data_size) {
errno = EINVAL;
return -1;
}
if (!*resp_data_size && resp_data_offset) {
errno = EINVAL;
return -1;
}
admin_req->hdr.type = NVME_MI_MSGTYPE_NVME;
admin_req->hdr.nmp = (NVME_MI_ROR_REQ << 7) |
(NVME_MI_MT_ADMIN << 3) |
(ctrl->ep->csi & 1);
admin_req->ctrl_id = cpu_to_le16(ctrl->id);
memset(&req, 0, sizeof(req));
req.hdr = &admin_req->hdr;
req.hdr_len = sizeof(*admin_req);
req.data = admin_req + 1;
req.data_len = req_data_size;
memset(&resp, 0, sizeof(resp));
resp.hdr = &admin_resp->hdr;
resp.hdr_len = sizeof(*admin_resp);
resp.data = admin_resp + 1;
resp.data_len = *resp_data_size;
/* limit the response size, specify offset */
admin_req->flags = 0x3;
/* dlen and doff have different interpretations depending on the data direction */
if (req_data_size) {
dlen = req_data_size & 0xffffffff;
doff = 0;
} else {
dlen = *resp_data_size & 0xffffffff;
doff = resp_data_offset & 0xffffffff;
}
admin_req->dlen = cpu_to_le32(dlen);
admin_req->doff = cpu_to_le32(doff);
rc = nvme_mi_submit(ctrl->ep, &req, &resp);
if (rc)
return rc;
*resp_data_size = resp.data_len;
return 0;
}
int nvme_mi_admin_admin_passthru(nvme_mi_ctrl_t ctrl, __u8 opcode, __u8 flags,
__u16 rsvd, __u32 nsid, __u32 cdw2, __u32 cdw3,
__u32 cdw10, __u32 cdw11, __u32 cdw12,
__u32 cdw13, __u32 cdw14, __u32 cdw15,
__u32 data_len, void *data, __u32 metadata_len,
void *metadata, __u32 timeout_ms, __u32 *result)
{
/* Input parameters flags, rsvd, metadata, metadata_len are not used */
struct nvme_mi_admin_resp_hdr resp_hdr;
struct nvme_mi_admin_req_hdr req_hdr;
struct nvme_mi_resp resp;
struct nvme_mi_req req;
unsigned int timeout_save;
int rc;
int direction = opcode & 0x3;
bool has_write_data = false;
bool has_read_data = false;
if (direction == NVME_DATA_TFR_BIDIRECTIONAL) {
nvme_msg(ctrl->ep->root, LOG_ERR,
"nvme_mi_admin_admin_passthru doesn't support bidirectional commands\n");
errno = EINVAL;
return -1;
}
if (data_len > 4096) {
nvme_msg(ctrl->ep->root, LOG_ERR,
"nvme_mi_admin_admin_passthru doesn't support data_len over 4096 bytes.\n");
errno = EINVAL;
return -1;
}
if (data != NULL && data_len != 0) {
if (direction == NVME_DATA_TFR_HOST_TO_CTRL)
has_write_data = true;
if (direction == NVME_DATA_TFR_CTRL_TO_HOST)
has_read_data = true;
}
nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id, opcode);
req_hdr.cdw1 = cpu_to_le32(nsid);
req_hdr.cdw2 = cpu_to_le32(cdw2);
req_hdr.cdw3 = cpu_to_le32(cdw3);
req_hdr.cdw10 = cpu_to_le32(cdw10);
req_hdr.cdw11 = cpu_to_le32(cdw11);
req_hdr.cdw12 = cpu_to_le32(cdw12);
req_hdr.cdw13 = cpu_to_le32(cdw13);
req_hdr.cdw14 = cpu_to_le32(cdw14);
req_hdr.cdw15 = cpu_to_le32(cdw15);
req_hdr.doff = 0;
if (data_len != 0) {
req_hdr.dlen = cpu_to_le32(data_len);
/* Bit 0 set to 1 means DLEN contains a value */
req_hdr.flags = 0x1;
}
if (has_write_data) {
req.data = data;
req.data_len = data_len;
}
nvme_mi_admin_init_resp(&resp, &resp_hdr);
if (has_read_data) {
resp.data = data;
resp.data_len = data_len;
}
/* if the user has specified a custom timeout, save the current
* timeout and override
*/
if (timeout_ms != 0) {
timeout_save = nvme_mi_ep_get_timeout(ctrl->ep);
nvme_mi_ep_set_timeout(ctrl->ep, timeout_ms);
}
rc = nvme_mi_submit(ctrl->ep, &req, &resp);
if (timeout_ms != 0)
nvme_mi_ep_set_timeout(ctrl->ep, timeout_save);
if (rc)
return rc;
rc = nvme_mi_admin_parse_status(&resp, result);
if (rc)
return rc;
if (has_read_data && (resp.data_len != data_len)) {
errno = EPROTO;
return -1;
}
return 0;
}
int nvme_mi_admin_identify_partial(nvme_mi_ctrl_t ctrl,
struct nvme_identify_args *args,
off_t offset, size_t size)
{
struct nvme_mi_admin_resp_hdr resp_hdr;
struct nvme_mi_admin_req_hdr req_hdr;
struct nvme_mi_resp resp;
struct nvme_mi_req req;
int rc;
if (args->args_size < sizeof(*args)) {
errno = EINVAL;
return -1;
}
if (!size || size > 0xffffffff) {
errno = EINVAL;
return -1;
}
nvme_mi_admin_init_req(ctrl->ep, &req, &req_hdr, ctrl->id, nvme_admin_identify);
req_hdr.cdw1 = cpu_to_le32(args->nsid);
req_hdr.cdw10 = cpu_to_le32(args->cntid << 16 | args->cns);
req_hdr.cdw11 = cpu_to_le32((args->csi & 0xff) << 24 |
args->cns_specific_id);
req_hdr.cdw14 = cpu_to_le32(args->uuidx);
req_hdr.dlen = cpu_to_le32(size & 0xffffffff);
req_hdr.flags = 0x1;
if (offset) {
req_hdr.flags |= 0x2;
req_hdr.doff = cpu_to_le32(offset);
}
nvme_mi_admin_init_resp(&resp, &resp_hdr);
resp.data = args->data;
resp.data_len = size;
rc = nvme_mi_submit(ctrl->ep, &req, &resp);
if (rc)
return rc;
rc = nvme_mi_admin_parse_status(&resp, args->result);
if (rc)
return rc;
/* callers will expect a full response; if the data buffer isn't
* fully valid, return an error */
if (resp.data_len != size) {
errno = EPROTO;
return -1;
}
return 0;
}
int nvme_mi_control(nvme_mi_ep_t ep, __u8 opcode,
__u16 cpsp, __u16 *result_cpsr)
{
struct nvme_mi_control_resp control_resp;