forked from linux-nvme/nvme-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wdc-nvme.c
2342 lines (2049 loc) · 76.1 KB
/
wdc-nvme.c
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
/*
* Copyright (c) 2015-2018 Western Digital Corporation or its affiliates.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
* Author: Chaitanya Kulkarni <[email protected]>,
* Dong Ho <[email protected]>,
* Jeff Lien <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include "linux/nvme_ioctl.h"
#include "nvme.h"
#include "nvme-print.h"
#include "nvme-ioctl.h"
#include "plugin.h"
#include "json.h"
#include "argconfig.h"
#include "suffix.h"
#include <sys/ioctl.h>
#define CREATE_CMD
#include "wdc-nvme.h"
#include "wdc-utils.h"
#define WRITE_SIZE (sizeof(__u8) * 4096)
#define WDC_NVME_SUBCMD_SHIFT 8
#define WDC_NVME_LOG_SIZE_DATA_LEN 0x08
/* Device Config */
#define WDC_NVME_VID 0x1c58
#define WDC_NVME_SN100_DEV_ID 0x0003
#define WDC_NVME_SN200_DEV_ID 0x0023
#define WDC_NVME_VID_2 0x1b96
#define WDC_NVME_SN310_DEV_ID 0x2200
#define WDC_NVME_SN510_DEV_ID 0x2300
#define WDC_NVME_SNDK_VID 0x15b7
#define WDC_NVME_SXSLCL_DEV_ID 0x2001
#define WDC_NVME_SN520_DEV_ID_1 0x5003
#define WDC_NVME_SN520_DEV_ID_2 0x5005
#define WDC_NVME_SN720_DEV_ID 0x5002
/* Capture Diagnostics */
#define WDC_NVME_CAP_DIAG_HEADER_TOC_SIZE WDC_NVME_LOG_SIZE_DATA_LEN
#define WDC_NVME_CAP_DIAG_OPCODE 0xE6
#define WDC_NVME_CAP_DIAG_CMD_OPCODE 0xC6
#define WDC_NVME_CAP_DIAG_SUBCMD 0x00
#define WDC_NVME_CAP_DIAG_CMD 0x00
/* Crash dump */
#define WDC_NVME_CRASH_DUMP_SIZE_OPCODE WDC_NVME_CAP_DIAG_CMD_OPCODE
#define WDC_NVME_CRASH_DUMP_SIZE_DATA_LEN WDC_NVME_LOG_SIZE_DATA_LEN
#define WDC_NVME_CRASH_DUMP_SIZE_NDT 0x02
#define WDC_NVME_CRASH_DUMP_SIZE_CMD 0x20
#define WDC_NVME_CRASH_DUMP_SIZE_SUBCMD 0x03
#define WDC_NVME_CRASH_DUMP_OPCODE WDC_NVME_CAP_DIAG_CMD_OPCODE
#define WDC_NVME_CRASH_DUMP_CMD 0x20
#define WDC_NVME_CRASH_DUMP_SUBCMD 0x04
/* Drive Log */
#define WDC_NVME_DRIVE_LOG_SIZE_OPCODE WDC_NVME_CAP_DIAG_CMD_OPCODE
#define WDC_NVME_DRIVE_LOG_SIZE_DATA_LEN WDC_NVME_LOG_SIZE_DATA_LEN
#define WDC_NVME_DRIVE_LOG_SIZE_NDT 0x02
#define WDC_NVME_DRIVE_LOG_SIZE_CMD 0x20
#define WDC_NVME_DRIVE_LOG_SIZE_SUBCMD 0x01
#define WDC_NVME_DRIVE_LOG_OPCODE WDC_NVME_CAP_DIAG_CMD_OPCODE
#define WDC_NVME_DRIVE_LOG_CMD WDC_NVME_LOG_SIZE_DATA_LEN
#define WDC_NVME_DRIVE_LOG_SUBCMD 0x00
/* Purge and Purge Monitor */
#define WDC_NVME_PURGE_CMD_OPCODE 0xDD
#define WDC_NVME_PURGE_MONITOR_OPCODE 0xDE
#define WDC_NVME_PURGE_MONITOR_DATA_LEN 0x2F
#define WDC_NVME_PURGE_MONITOR_CMD_CDW10 0x0000000C
#define WDC_NVME_PURGE_MONITOR_TIMEOUT 0x7530
#define WDC_NVME_PURGE_CMD_SEQ_ERR 0x0C
#define WDC_NVME_PURGE_INT_DEV_ERR 0x06
#define WDC_NVME_PURGE_STATE_IDLE 0x00
#define WDC_NVME_PURGE_STATE_DONE 0x01
#define WDC_NVME_PURGE_STATE_BUSY 0x02
#define WDC_NVME_PURGE_STATE_REQ_PWR_CYC 0x03
#define WDC_NVME_PURGE_STATE_PWR_CYC_PURGE 0x04
/* Clear dumps */
#define WDC_NVME_CLEAR_DUMP_OPCODE 0xFF
#define WDC_NVME_CLEAR_CRASH_DUMP_CMD 0x03
#define WDC_NVME_CLEAR_CRASH_DUMP_SUBCMD 0x05
/* Additional Smart Log */
#define WDC_ADD_LOG_BUF_LEN 0x4000
#define WDC_NVME_ADD_LOG_OPCODE 0xC1
#define WDC_GET_LOG_PAGE_SSD_PERFORMANCE 0x37
#define WDC_NVME_GET_STAT_PERF_INTERVAL_LIFETIME 0x0F
/* C2 Log Page */
#define WDC_NVME_GET_AVAILABLE_LOG_PAGES_OPCODE 0xC2
#define WDC_C2_LOG_BUF_LEN 0x1000
#define WDC_C2_LOG_PAGES_SUPPORTED_ID 0x08
/* CA Log Page */
#define WDC_NVME_GET_DEVICE_INFO_LOG_OPCODE 0xCA
#define WDC_CA_LOG_BUF_LEN 0x80
/* Clear PCIe Correctable Errors */
#define WDC_NVME_CLEAR_PCIE_CORR_OPCODE WDC_NVME_CAP_DIAG_CMD_OPCODE
#define WDC_NVME_CLEAR_PCIE_CORR_CMD 0x22
#define WDC_NVME_CLEAR_PCIE_CORR_SUBCMD 0x04
/* Drive Essentials */
#define WDC_DE_DEFAULT_NUMBER_OF_ERROR_ENTRIES 64
#define WDC_DE_GENERIC_BUFFER_SIZE 80
#define WDC_DE_GLOBAL_NSID 0xFFFFFFFF
#define WDC_DE_DEFAULT_NAMESPACE_ID 0x01
#define WDC_DE_PATH_SEPARATOR "/"
#define WDC_DE_TAR_FILES "*.bin"
#define WDC_DE_TAR_FILE_EXTN ".tar.gz"
#define WDC_DE_TAR_CMD "tar -czf"
/* VU Opcodes */
#define WDC_DE_VU_READ_SIZE_OPCODE 0xC0
#define WDC_DE_VU_READ_BUFFER_OPCODE 0xC2
#define WDC_DE_FILE_HEADER_SIZE 4
#define WDC_DE_FILE_OFFSET_SIZE 2
#define WDC_DE_FILE_NAME_SIZE 32
#define WDC_DE_VU_READ_BUFFER_STANDARD_OFFSET 0x8000
#define WDC_DE_READ_MAX_TRANSFER_SIZE 0x8000
#define WDC_DE_MANUFACTURING_INFO_PAGE_FILE_NAME "manufacturing_info" /* Unique log entry page name. */
#define WDC_DE_CORE_DUMP_FILE_NAME "core_dump"
#define WDC_DE_EVENT_LOG_FILE_NAME "event_log"
#define WDC_DE_DESTN_SPI 1
#define WDC_DE_DUMPTRACE_DESTINATION 6
typedef enum _NVME_FEATURES_SELECT
{
FS_CURRENT = 0,
FS_DEFAULT = 1,
FS_SAVED = 2,
FS_SUPPORTED_CAPBILITIES = 3
} NVME_FEATURES_SELECT;
typedef enum _NVME_FEATURE_IDENTIFIERS
{
FID_ARBITRATION = 0x01,
FID_POWER_MANAGEMENT = 0x02,
FID_LBA_RANGE_TYPE = 0x03,
FID_TEMPERATURE_THRESHOLD = 0x04,
FID_ERROR_RECOVERY = 0x05,
FID_VOLATILE_WRITE_CACHE = 0x06,
FID_NUMBER_OF_QUEUES = 0x07,
FID_INTERRUPT_COALESCING = 0x08,
FID_INTERRUPT_VECTOR_CONFIGURATION = 0x09,
FID_WRITE_ATOMICITY = 0x0A,
FID_ASYNCHRONOUS_EVENT_CONFIGURATION = 0x0B,
FID_AUTONOMOUS_POWER_STATE_TRANSITION = 0x0C,
/*Below FID's are NVM Command Set Specific*/
FID_SOFTWARE_PROGRESS_MARKER = 0x80,
FID_HOST_IDENTIFIER = 0x81,
FID_RESERVATION_NOTIFICATION_MASK = 0x82,
FID_RESERVATION_PERSISTENCE = 0x83
} NVME_FEATURE_IDENTIFIERS;
typedef enum
{
WDC_DE_TYPE_IDENTIFY = 0x1,
WDC_DE_TYPE_SMARTATTRIBUTEDUMP = 0x2,
WDC_DE_TYPE_EVENTLOG = 0x4,
WDC_DE_TYPE_DUMPTRACE = 0x8,
WDC_DE_TYPE_DUMPSNAPSHOT = 0x10,
WDC_DE_TYPE_ATA_LOGS = 0x20,
WDC_DE_TYPE_SMART_LOGS = 0x40,
WDC_DE_TYPE_SCSI_LOGS = 0x80,
WDC_DE_TYPE_SCSI_MODE_PAGES = 0x100,
WDC_DE_TYPE_NVMe_FEATURES = 0x200,
WDC_DE_TYPE_DUMPSMARTERRORLOG3 = 0x400,
WDC_DE_TYPE_DUMPLOG3E = 0x800,
WDC_DE_TYPE_DUMPSCRAM = 0x1000,
WDC_DE_TYPE_PCU_LOG = 0x2000,
WDC_DE_TYPE_DUMP_ERROR_LOGS = 0x4000,
WDC_DE_TYPE_FW_SLOT_LOGS = 0x8000,
WDC_DE_TYPE_MEDIA_SETTINGS = 0x10000,
WDC_DE_TYPE_SMART_DATA = 0x20000,
WDC_DE_TYPE_NVME_SETTINGS = 0x40000,
WDC_DE_TYPE_NVME_ERROR_LOGS = 0x80000,
WDC_DE_TYPE_NVME_LOGS = 0x100000,
WDC_DE_TYPE_UART_LOGS = 0x200000,
WDC_DE_TYPE_DLOGS_SPI = 0x400000,
WDC_DE_TYPE_DLOGS_RAM = 0x800000,
WDC_DE_TYPE_NVME_MANF_INFO = 0x2000000,
WDC_DE_TYPE_NONE = 0x1000000,
WDC_DE_TYPE_ALL = 0xFFFFFFF,
} WDC_DRIVE_ESSENTIAL_TYPE;
typedef struct __attribute__((__packed__)) _WDC_DE_VU_FILE_META_DATA
{
__u8 fileName[WDC_DE_FILE_NAME_SIZE];
__u16 fileID;
__u64 fileSize;
} WDC_DE_VU_FILE_META_DATA, *PWDC_DE_VU_FILE_META_DATA;
typedef struct _WDC_DRIVE_ESSENTIALS
{
WDC_DE_VU_FILE_META_DATA metaData;
WDC_DRIVE_ESSENTIAL_TYPE essentialType;
} WDC_DRIVE_ESSENTIALS;
typedef struct _WDC_DE_VU_LOG_DIRECTORY
{
WDC_DRIVE_ESSENTIALS *logEntry; /* Caller to allocate memory */
__u32 maxNumLogEntries; /* Caller to input memory allocated */
__u32 numOfValidLogEntries; /* API will output this value */
} WDC_DE_VU_LOG_DIRECTORY,*PWDC_DE_VU_LOG_DIRECTORY;
typedef struct _WDC_DE_CSA_FEATURE_ID_LIST
{
NVME_FEATURE_IDENTIFIERS featureId;
__u8 featureName[WDC_DE_GENERIC_BUFFER_SIZE];
} WDC_DE_CSA_FEATURE_ID_LIST;
static WDC_DE_CSA_FEATURE_ID_LIST deFeatureIdList[] =
{
{0x00 , "Dummy Placeholder"},
{FID_ARBITRATION , "Arbitration"},
{FID_POWER_MANAGEMENT , "PowerMgmnt"},
{FID_LBA_RANGE_TYPE , "LbaRangeType"},
{FID_TEMPERATURE_THRESHOLD , "TempThreshold"},
{FID_ERROR_RECOVERY , "ErrorRecovery"},
{FID_VOLATILE_WRITE_CACHE , "VolatileWriteCache"},
{FID_NUMBER_OF_QUEUES , "NumOfQueues"},
{FID_INTERRUPT_COALESCING , "InterruptCoalesing"},
{FID_INTERRUPT_VECTOR_CONFIGURATION , "InterruptVectorConfig"},
{FID_WRITE_ATOMICITY , "WriteAtomicity"},
{FID_ASYNCHRONOUS_EVENT_CONFIGURATION , "AsynEventConfig"},
{FID_AUTONOMOUS_POWER_STATE_TRANSITION , "AutonomousPowerState"},
};
typedef enum _NVME_VU_DE_LOGPAGE_NAMES
{
NVME_DE_LOGPAGE_E3 = 0x01,
NVME_DE_LOGPAGE_C0 = 0x02
} NVME_VU_DE_LOGPAGE_NAMES;
typedef struct _NVME_VU_DE_LOGPAGE_LIST
{
NVME_VU_DE_LOGPAGE_NAMES logPageName;
__u32 logPageId;
__u32 logPageLen;
char logPageIdStr[4];
} NVME_VU_DE_LOGPAGE_LIST, *PNVME_VU_DE_LOGPAGE_LIST;
typedef struct _WDC_NVME_DE_VU_LOGPAGES
{
NVME_VU_DE_LOGPAGE_NAMES vuLogPageReqd;
__u32 numOfVULogPages;
} WDC_NVME_DE_VU_LOGPAGES, *PWDC_NVME_DE_VU_LOGPAGES;
static NVME_VU_DE_LOGPAGE_LIST deVULogPagesList[] =
{
{ NVME_DE_LOGPAGE_E3, 0xE3, 1072, "0xe3"},
{ NVME_DE_LOGPAGE_C0, 0xC0, 512, "0xc0"}
};
static int wdc_get_serial_name(int fd, char *file, size_t len, char *suffix);
static int wdc_create_log_file(char *file, __u8 *drive_log_data,
__u32 drive_log_length);
static int wdc_do_clear_dump(int fd, __u8 opcode, __u32 cdw12);
static int wdc_do_dump(int fd, __u32 opcode,__u32 data_len, __u32 cdw10,
__u32 cdw12, __u32 dump_length, char *file);
static int wdc_do_crash_dump(int fd, char *file);
static int wdc_crash_dump(int fd, char *file);
static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
struct plugin *plugin);
static int wdc_do_drive_log(int fd, char *file);
static int wdc_drive_log(int argc, char **argv, struct command *command,
struct plugin *plugin);
static const char* wdc_purge_mon_status_to_string(__u32 status);
static int wdc_purge(int argc, char **argv,
struct command *command, struct plugin *plugin);
static int wdc_purge_monitor(int argc, char **argv,
struct command *command, struct plugin *plugin);
static int wdc_nvme_check_supported_log_page(int fd, __u8 log_id);
static int wdc_clear_pcie_corr(int argc, char **argv, struct command *command,
struct plugin *plugin);
static int wdc_do_drive_essentials(int fd, char *dir, char *key);
static int wdc_drive_essentials(int argc, char **argv, struct command *command,
struct plugin *plugin);
/* Drive log data size */
struct wdc_log_size {
__le32 log_size;
};
/* Purge monitor response */
struct wdc_nvme_purge_monitor_data {
__le16 rsvd1;
__le16 rsvd2;
__le16 first_erase_failure_cnt;
__le16 second_erase_failure_cnt;
__le16 rsvd3;
__le16 programm_failure_cnt;
__le32 rsvd4;
__le32 rsvd5;
__le32 entire_progress_total;
__le32 entire_progress_current;
__u8 rsvd6[14];
};
/* Additional Smart Log */
struct wdc_log_page_header {
uint8_t num_subpages;
uint8_t reserved;
__le16 total_log_size;
};
struct wdc_log_page_subpage_header {
uint8_t spcode;
uint8_t pcset;
__le16 subpage_length;
};
struct wdc_ssd_perf_stats {
__le64 hr_cmds; /* Host Read Commands */
__le64 hr_blks; /* Host Read Blocks */
__le64 hr_ch_cmds; /* Host Read Cache Hit Commands */
__le64 hr_ch_blks; /* Host Read Cache Hit Blocks */
__le64 hr_st_cmds; /* Host Read Stalled Commands */
__le64 hw_cmds; /* Host Write Commands */
__le64 hw_blks; /* Host Write Blocks */
__le64 hw_os_cmds; /* Host Write Odd Start Commands */
__le64 hw_oe_cmds; /* Host Write Odd End Commands */
__le64 hw_st_cmds; /* Host Write Commands Stalled */
__le64 nr_cmds; /* NAND Read Commands */
__le64 nr_blks; /* NAND Read Blocks */
__le64 nw_cmds; /* NAND Write Commands */
__le64 nw_blks; /* NAND Write Blocks */
__le64 nrbw; /* NAND Read Before Write */
};
/* Additional C2 Log Page */
struct wdc_c2_log_page_header {
__le32 length;
__le32 version;
};
struct wdc_c2_log_subpage_header {
__le32 length;
__le32 entry_id;
__le32 data;
};
struct wdc_c2_cbs_data {
__le32 length;
__u8 data[];
};
struct __attribute__((__packed__)) wdc_ssd_ca_perf_stats {
__le64 nand_bytes_wr_lo; /* 0x00 - NAND Bytes Written lo */
__le64 nand_bytes_wr_hi; /* 0x08 - NAND Bytes Written hi */
__le64 nand_bytes_rd_lo; /* 0x10 - NAND Bytes Read lo */
__le64 nand_bytes_rd_hi; /* 0x18 - NAND Bytes Read hi */
__le64 nand_bad_block; /* 0x20 - NAND Bad Block Count */
__le64 uncorr_read_count; /* 0x28 - Uncorrectable Read Count */
__le64 ecc_error_count; /* 0x30 - Soft ECC Error Count */
__le32 ssd_detect_count; /* 0x38 - SSD End to End Detection Count */
__le32 ssd_correct_count; /* 0x3C - SSD End to End Correction Count */
__le32 data_percent_used; /* 0x40 - System Data Percent Used */
__le32 data_erase_max; /* 0x44 - User Data Erase Counts */
__le32 data_erase_min; /* 0x48 - User Data Erase Counts */
__le64 refresh_count; /* 0x4c - Refresh Count */
__le64 program_fail; /* 0x54 - Program Fail Count */
__le64 user_erase_fail; /* 0x5C - User Data Erase Fail Count */
__le64 system_erase_fail; /* 0x64 - System Area Erase Fail Count */
__le16 thermal_throttle_status; /* 0x6C - Thermal Throttling Status */
__le16 thermal_throttle_count; /* 0x6E - Thermal Throttling Count */
__le64 pcie_corr_error; /* 0x70 - pcie Correctable Error Count */
__le32 rsvd1; /* 0x78 - Reserved */
__le32 rsvd2; /* 0x7C - Reserved */
};
static double safe_div_fp(double numerator, double denominator)
{
return denominator ? numerator / denominator : 0;
}
static double calc_percent(uint64_t numerator, uint64_t denominator)
{
return denominator ?
(uint64_t)(((double)numerator / (double)denominator) * 100) : 0;
}
static int wdc_get_pci_dev_id(int *device_id)
{
int fd, ret = -1;
char *base, path[512], *id;
base = nvme_char_from_block((char *)devicename);
sprintf(path, "/sys/class/nvme/%s/device/device", base);
fd = open(path, O_RDONLY);
if (fd < 0) {
sprintf(path, "/sys/class/misc/%s/device/device", base);
fd = open(path, O_RDONLY);
}
if (fd < 0) {
fprintf(stderr, "%s: did not find a pci device\n", __func__);
return -1;
}
id = calloc(1, 32);
if (!id)
goto close_fd;
ret = read(fd, id, 32);
if (ret < 0) {
fprintf(stderr, "%s: Read of pci device id failed\n", __func__);
} else {
if (id[strlen(id) - 1] == '\n')
id[strlen(id) - 1] = '\0';
/* convert the device id string to an int */
*device_id = (int)strtol(&id[2], NULL, 16);
ret = 0;
}
free(id);
close_fd:
close(fd);
return ret;
}
static bool wdc_check_device(int fd)
{
int ret;
bool supported;
struct nvme_id_ctrl ctrl;
int device_id;
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
ret = nvme_identify_ctrl(fd, &ctrl);
if (ret) {
fprintf(stderr, "ERROR : WDC : nvme_identify_ctrl() failed "
"0x%x\n", ret);
return false;
}
ret = wdc_get_pci_dev_id((int *)&device_id);
if (ret < 0)
return false;
supported = false;
/* WDC : Use PCI Vendor and Device ID's to identify WDC Devices */
if ((le32_to_cpu(ctrl.vid) == WDC_NVME_VID) &&
((device_id == WDC_NVME_SN100_DEV_ID) ||
(device_id == WDC_NVME_SN200_DEV_ID)))
supported = true;
else if ((le32_to_cpu(ctrl.vid) == WDC_NVME_SNDK_VID) &&
(device_id == WDC_NVME_SXSLCL_DEV_ID))
supported = true;
else if ((le32_to_cpu(ctrl.vid) == WDC_NVME_VID_2) &&
((device_id == WDC_NVME_SN310_DEV_ID) ||
(device_id == WDC_NVME_SN510_DEV_ID)))
supported = true;
else
fprintf(stderr, "WARNING : WDC not supported, Vendor ID = 0x%x, Device ID = 0x%x\n", le32_to_cpu(ctrl.vid), device_id);
return supported;
}
static bool wdc_check_device_sxslcl(int fd)
{
int ret;
struct nvme_id_ctrl ctrl;
int device_id;
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
ret = nvme_identify_ctrl(fd, &ctrl);
if (ret) {
fprintf(stderr, "ERROR : WDC : nvme_identify_ctrl() failed "
"0x%x\n", ret);
return false;
}
ret = wdc_get_pci_dev_id((int *)&device_id);
if (ret < 0)
return false;
/* WDC : Use PCI Vendor and Device ID's to identify WDC Devices */
if ((le32_to_cpu(ctrl.vid) == WDC_NVME_SNDK_VID) &&
(device_id == WDC_NVME_SXSLCL_DEV_ID))
return true;
else
return false;
}
static bool wdc_check_device_sn100(int fd)
{
int ret;
struct nvme_id_ctrl ctrl;
int device_id;
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
ret = nvme_identify_ctrl(fd, &ctrl);
if (ret) {
fprintf(stderr, "ERROR : WDC : nvme_identify_ctrl() failed "
"0x%x\n", ret);
return false;
}
ret = wdc_get_pci_dev_id((int *)&device_id);
if (ret < 0)
return false;
/* WDC : Use PCI Vendor and Device ID's to identify WDC Devices */
if ((le32_to_cpu(ctrl.vid) == WDC_NVME_VID) &&
(device_id == WDC_NVME_SN100_DEV_ID))
return true;
else
return false;
}
static bool wdc_check_device_sn200(int fd)
{
int ret;
struct nvme_id_ctrl ctrl;
int device_id;
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
ret = nvme_identify_ctrl(fd, &ctrl);
if (ret) {
fprintf(stderr, "ERROR : WDC : nvme_identify_ctrl() failed "
"0x%x\n", ret);
return false;
}
ret = wdc_get_pci_dev_id((int *)&device_id);
if (ret < 0)
return false;
/* WDC : Use PCI Vendor and Device ID's to identify WDC Devices */
if ((le32_to_cpu(ctrl.vid) == WDC_NVME_VID) &&
(device_id == WDC_NVME_SN200_DEV_ID))
return true;
else
return false;
}
static int wdc_get_serial_name(int fd, char *file, size_t len, char *suffix)
{
int i;
int ret;
int res_len = 0;
char orig[PATH_MAX] = {0};
struct nvme_id_ctrl ctrl;
int ctrl_sn_len = sizeof (ctrl.sn);
i = sizeof (ctrl.sn) - 1;
strncpy(orig, file, PATH_MAX - 1);
memset(file, 0, len);
memset(&ctrl, 0, sizeof (struct nvme_id_ctrl));
ret = nvme_identify_ctrl(fd, &ctrl);
if (ret) {
fprintf(stderr, "ERROR : WDC : nvme_identify_ctrl() failed "
"0x%x\n", ret);
return -1;
}
/* Remove trailing spaces from the name */
while (i && ctrl.sn[i] == ' ') {
ctrl.sn[i] = '\0';
i--;
}
if (ctrl.sn[sizeof (ctrl.sn) - 1] == '\0') {
ctrl_sn_len = strlen(ctrl.sn);
}
res_len = snprintf(file, len, "%s%.*s%s.bin", orig, ctrl_sn_len, ctrl.sn, suffix);
if (len <= res_len) {
fprintf(stderr, "ERROR : WDC : cannot format serial number due to data "
"of unexpected length\n");
return -1;
}
return 0;
}
static int wdc_create_log_file(char *file, __u8 *drive_log_data,
__u32 drive_log_length)
{
int fd;
int ret;
if (drive_log_length == 0) {
fprintf(stderr, "ERROR : WDC: invalid log file length\n");
return -1;
}
fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd < 0) {
fprintf(stderr, "ERROR : WDC: open : %s\n", strerror(errno));
return -1;
}
while (drive_log_length > WRITE_SIZE) {
ret = write(fd, drive_log_data, WRITE_SIZE);
if (ret < 0) {
fprintf (stderr, "ERROR : WDC: write : %s\n", strerror(errno));
return -1;
}
drive_log_data += WRITE_SIZE;
drive_log_length -= WRITE_SIZE;
}
ret = write(fd, drive_log_data, drive_log_length);
if (ret < 0) {
fprintf(stderr, "ERROR : WDC : write : %s\n", strerror(errno));
return -1;
}
if (fsync(fd) < 0) {
fprintf(stderr, "ERROR : WDC : fsync : %s\n", strerror(errno));
return -1;
}
close(fd);
return 0;
}
static int wdc_nvme_check_supported_log_page(int fd, __u8 log_id)
{
int i;
int ret = -1;
int found = 0;
__u8* data;
__u32 length = 0;
struct wdc_c2_cbs_data *cbs_data;
struct wdc_c2_log_page_header *hdr_ptr;
struct wdc_c2_log_subpage_header *sph;
if ((data = (__u8*) malloc(sizeof (__u8) * WDC_C2_LOG_BUF_LEN)) == NULL) {
fprintf(stderr, "ERROR : WDC : malloc : %s\n", strerror(errno));
return ret;
}
memset(data, 0, sizeof (__u8) * WDC_C2_LOG_BUF_LEN);
/* get the log page length */
ret = nvme_get_log(fd, 0xFFFFFFFF, WDC_NVME_GET_AVAILABLE_LOG_PAGES_OPCODE,
false, WDC_C2_LOG_BUF_LEN, data);
if (ret) {
fprintf(stderr, "ERROR : WDC : Unable to get C2 Log Page length, ret = %d\n", ret);
goto out;
}
hdr_ptr = (struct wdc_c2_log_page_header *)data;
if (hdr_ptr->length > WDC_C2_LOG_BUF_LEN) {
fprintf(stderr, "ERROR : WDC : data length > buffer size : 0x%x\n", hdr_ptr->length);
goto out;
}
ret = nvme_get_log(fd, 0xFFFFFFFF, WDC_NVME_GET_AVAILABLE_LOG_PAGES_OPCODE,
false, hdr_ptr->length, data);
/* parse the data until the List of log page ID's is found */
if (ret) {
fprintf(stderr, "ERROR : WDC : Unable to read C2 Log Page data, ret = %d\n", ret);
goto out;
}
length = sizeof(struct wdc_c2_log_page_header);
while (length < hdr_ptr->length) {
sph = (struct wdc_c2_log_subpage_header *)(data + length);
if (sph->entry_id == WDC_C2_LOG_PAGES_SUPPORTED_ID) {
cbs_data = (struct wdc_c2_cbs_data *)&sph->data;
for (i = 0; i < cbs_data->length; i++) {
if (log_id == cbs_data->data[i]) {
found = 1;
ret = 0;
break;
}
}
if (!found) {
fprintf(stderr, "ERROR : WDC : Log Page 0x%x not supported\n", log_id);
fprintf(stderr, "WDC : Supported Log Pages:\n");
/* print the supported pages */
d((__u8 *)&sph->data + 4, sph->length - 12, 16, 1);
ret = -1;
}
break;
}
length += le32_to_cpu(sph->length);
}
out:
free(data);
return ret;
}
static int wdc_do_clear_dump(int fd, __u8 opcode, __u32 cdw12)
{
int ret;
struct nvme_admin_cmd admin_cmd;
memset(&admin_cmd, 0, sizeof (struct nvme_admin_cmd));
admin_cmd.opcode = opcode;
admin_cmd.cdw12 = cdw12;
ret = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, &admin_cmd);
if (ret != 0) {
fprintf(stdout, "ERROR : WDC : Crash dump erase failed\n");
}
fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
return ret;
}
static __u32 wdc_dump_length(int fd, __u32 opcode, __u32 cdw10, __u32 cdw12, __u32 *dump_length)
{
int ret;
__u8 buf[WDC_NVME_LOG_SIZE_DATA_LEN] = {0};
struct wdc_log_size *l;
struct nvme_admin_cmd admin_cmd;
l = (struct wdc_log_size *) buf;
memset(&admin_cmd, 0, sizeof (struct nvme_admin_cmd));
admin_cmd.opcode = opcode;
admin_cmd.addr = (__u64)(uintptr_t)buf;
admin_cmd.data_len = WDC_NVME_LOG_SIZE_DATA_LEN;
admin_cmd.cdw10 = cdw10;
admin_cmd.cdw12 = cdw12;
ret = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, &admin_cmd);
if (ret != 0) {
l->log_size = 0;
ret = -1;
fprintf(stderr, "ERROR : WDC : reading dump length failed\n");
fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
return ret;
}
if (opcode == WDC_NVME_CAP_DIAG_OPCODE)
*dump_length = buf[0x04] << 24 | buf[0x05] << 16 | buf[0x06] << 8 | buf[0x07];
else
*dump_length = le32_to_cpu(l->log_size);
return ret;
}
static int wdc_do_dump(int fd, __u32 opcode,__u32 data_len, __u32 cdw10,
__u32 cdw12, __u32 dump_length, char *file)
{
int ret;
__u8 *dump_data;
struct nvme_admin_cmd admin_cmd;
dump_data = (__u8 *) malloc(sizeof (__u8) * dump_length);
if (dump_data == NULL) {
fprintf(stderr, "ERROR : malloc : %s\n", strerror(errno));
return -1;
}
memset(dump_data, 0, sizeof (__u8) * dump_length);
memset(&admin_cmd, 0, sizeof (struct nvme_admin_cmd));
admin_cmd.opcode = opcode;
admin_cmd.addr = (__u64)(uintptr_t)dump_data;
admin_cmd.data_len = data_len;
admin_cmd.cdw10 = cdw10;
admin_cmd.cdw12 = cdw12;
ret = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, &admin_cmd);
fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret), ret);
if (ret == 0) {
ret = wdc_create_log_file(file, dump_data, dump_length);
}
free(dump_data);
return ret;
}
static int wdc_do_cap_diag(int fd, char *file)
{
int ret;
__u32 cap_diag_length;
ret = wdc_dump_length(fd, WDC_NVME_CAP_DIAG_OPCODE,
WDC_NVME_CAP_DIAG_HEADER_TOC_SIZE,
0x00,
&cap_diag_length);
if (ret == -1) {
return -1;
}
if (cap_diag_length == 0) {
fprintf(stderr, "INFO : WDC : Capture Dignostics log is empty\n");
} else {
ret = wdc_do_dump(fd, WDC_NVME_CAP_DIAG_OPCODE, cap_diag_length,
cap_diag_length,
(WDC_NVME_CAP_DIAG_SUBCMD << WDC_NVME_SUBCMD_SHIFT) |
WDC_NVME_CAP_DIAG_CMD, cap_diag_length, file);
}
return ret;
}
static int wdc_cap_diag(int argc, char **argv, struct command *command,
struct plugin *plugin)
{
const char *desc = "Capture Diagnostics Log.";
const char *file = "Output file pathname.";
char f[PATH_MAX] = {0};
int fd;
struct config {
char *file;
};
struct config cfg = {
.file = NULL
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-file", 'o', "FILE", CFG_STRING, &cfg.file, required_argument, file},
{ NULL, '\0', NULL, CFG_NONE, NULL, no_argument, desc},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, NULL, 0);
if (fd < 0)
return fd;
wdc_check_device(fd);
if (cfg.file != NULL) {
strncpy(f, cfg.file, PATH_MAX - 1);
}
if (wdc_get_serial_name(fd, f, PATH_MAX, "cap_diag") == -1) {
fprintf(stderr, "ERROR : WDC: failed to generate file name\n");
return -1;
}
return wdc_do_cap_diag(fd, f);
}
static int wdc_do_crash_dump(int fd, char *file)
{
int ret;
__u32 crash_dump_length;
__u8 opcode = WDC_NVME_CLEAR_DUMP_OPCODE;
__u32 cdw12 = ((WDC_NVME_CLEAR_CRASH_DUMP_SUBCMD << WDC_NVME_SUBCMD_SHIFT) |
WDC_NVME_CLEAR_CRASH_DUMP_CMD);
ret = wdc_dump_length(fd, WDC_NVME_CRASH_DUMP_SIZE_OPCODE,
WDC_NVME_CRASH_DUMP_SIZE_NDT,
((WDC_NVME_CRASH_DUMP_SIZE_SUBCMD <<
WDC_NVME_SUBCMD_SHIFT) | WDC_NVME_CRASH_DUMP_SIZE_CMD),
&crash_dump_length);
if (ret == -1) {
return -1;
}
if (crash_dump_length == 0) {
fprintf(stderr, "INFO : WDC: Crash dump is empty\n");
} else {
ret = wdc_do_dump(fd, WDC_NVME_CRASH_DUMP_OPCODE, crash_dump_length,
crash_dump_length,
(WDC_NVME_CRASH_DUMP_SUBCMD << WDC_NVME_SUBCMD_SHIFT) |
WDC_NVME_CRASH_DUMP_CMD, crash_dump_length, file);
if (ret == 0)
ret = wdc_do_clear_dump(fd, opcode, cdw12);
}
return ret;
}
static int wdc_crash_dump(int fd, char *file)
{
char f[PATH_MAX] = {0};
if (file != NULL) {
strncpy(f, file, PATH_MAX - 1);
}
if (wdc_get_serial_name(fd, f, PATH_MAX, "crash_dump") == -1) {
fprintf(stderr, "ERROR : WDC : failed to generate file name\n");
return -1;
}
return wdc_do_crash_dump(fd, f);
}
static int wdc_do_drive_log(int fd, char *file)
{
int ret;
__u8 *drive_log_data;
__u32 drive_log_length;
struct nvme_admin_cmd admin_cmd;
ret = wdc_dump_length(fd, WDC_NVME_DRIVE_LOG_SIZE_OPCODE,
WDC_NVME_DRIVE_LOG_SIZE_NDT,
(WDC_NVME_DRIVE_LOG_SIZE_SUBCMD <<
WDC_NVME_SUBCMD_SHIFT | WDC_NVME_DRIVE_LOG_SIZE_CMD),
&drive_log_length);
if (ret == -1) {
return -1;
}
drive_log_data = (__u8 *) malloc(sizeof (__u8) * drive_log_length);
if (drive_log_data == NULL) {
fprintf(stderr, "ERROR : WDC : malloc : %s\n", strerror(errno));
return -1;
}
memset(drive_log_data, 0, sizeof (__u8) * drive_log_length);
memset(&admin_cmd, 0, sizeof (struct nvme_admin_cmd));
admin_cmd.opcode = WDC_NVME_DRIVE_LOG_OPCODE;
admin_cmd.addr = (__u64)(uintptr_t)drive_log_data;
admin_cmd.data_len = drive_log_length;
admin_cmd.cdw10 = drive_log_length;
admin_cmd.cdw12 = ((WDC_NVME_DRIVE_LOG_SUBCMD <<
WDC_NVME_SUBCMD_SHIFT) | WDC_NVME_DRIVE_LOG_SIZE_CMD);
ret = nvme_submit_passthru(fd, NVME_IOCTL_ADMIN_CMD, &admin_cmd);
fprintf(stderr, "NVMe Status:%s(%x)\n", nvme_status_to_string(ret),
ret);
if (ret == 0) {
ret = wdc_create_log_file(file, drive_log_data, drive_log_length);
}
free(drive_log_data);
return ret;
}
static int wdc_drive_log(int argc, char **argv, struct command *command,
struct plugin *plugin)
{
const char *desc = "Capture Drive Log.";
const char *file = "Output file pathname.";
char f[PATH_MAX] = {0};
int fd;
struct config {
char *file;
};
struct config cfg = {
.file = NULL
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-file", 'o', "FILE", CFG_STRING, &cfg.file, required_argument, file},
{ NULL, '\0', NULL, CFG_NONE, NULL, no_argument, desc},
{NULL}
};
fd = parse_and_open(argc, argv, desc, command_line_options, NULL, 0);
if (fd < 0)
return fd;
wdc_check_device(fd);
if (cfg.file != NULL) {
strncpy(f, cfg.file, PATH_MAX - 1);
}
if (wdc_get_serial_name(fd, f, PATH_MAX, "drive_log") == -1) {
fprintf(stderr, "ERROR : WDC : failed to generate file name\n");
return -1;
}
return wdc_do_drive_log(fd, f);
}
static int wdc_get_crash_dump(int argc, char **argv, struct command *command,
struct plugin *plugin)
{
const char *desc = "Get Crash Dump.";
const char *file = "Output file pathname.";
int fd;
int ret;
struct config {
char *file;
};
struct config cfg = {
.file = NULL,
};
const struct argconfig_commandline_options command_line_options[] = {
{"output-file", 'o', "FILE", CFG_STRING, &cfg.file, required_argument, file},