-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathscsiprint.cpp
4006 lines (3809 loc) · 141 KB
/
scsiprint.cpp
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
/*
* scsiprint.cpp
*
* Home page of code is: https://www.smartmontools.org
*
* Copyright (C) 2002-11 Bruce Allen
* Copyright (C) 2000 Michael Cornwell <[email protected]>
* Copyright (C) 2003-23 Douglas Gilbert <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#define __STDC_FORMAT_MACROS 1 // enable PRI* for C++
#include <inttypes.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "scsicmds.h"
#include "atacmds.h" // dont_print_serial_number
#include "dev_interface.h"
#include "scsiprint.h"
#include "smartctl.h"
#include "utility.h"
#include "sg_unaligned.h"
#include "farmcmds.h"
#include "farmprint.h"
#define GBUF_SIZE 65532
const char * scsiprint_c_cvsid = "$Id$"
SCSIPRINT_H_CVSID;
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
uint8_t gBuf[GBUF_SIZE];
#define LOG_RESP_LEN 252
#define LOG_RESP_LONG_LEN ((62 * 256) + 252)
#define LOG_RESP_TAPE_ALERT_LEN 0x144
/* Supported log pages + Supported log pages and subpages maximum count */
#define SCSI_SUPP_LOG_PAGES_MAX_COUNT (252 + (62 * 128) + 126)
/* Log pages supported */
static bool gSmartLPage = false; /* Informational Exceptions log page */
static bool gTempLPage = false;
static bool gSelfTestLPage = false;
static bool gStartStopLPage = false;
static bool gReadECounterLPage = false;
static bool gWriteECounterLPage = false;
static bool gVerifyECounterLPage = false;
static bool gNonMediumELPage = false;
static bool gLastNErrorEvLPage = false;
static bool gBackgroundResultsLPage = false;
static bool gProtocolSpecificLPage = false;
static bool gTapeAlertsLPage = false;
static bool gSSMediaLPage = false;
static bool gFormatStatusLPage = false;
static bool gEnviroReportingLPage = false;
static bool gEnviroLimitsLPage = false;
static bool gUtilizationLPage = false;
static bool gPendDefectsLPage = false;
static bool gBackgroundOpLPage = false;
static bool gLPSMisalignLPage = false;
static bool gTapeDeviceStatsLPage = false;
static bool gZBDeviceStatsLPage = false;
static bool gGenStatsAndPerfLPage = false;
/* Vendor specific log pages */
static bool gSeagateCacheLPage = false;
static bool gSeagateFactoryLPage = false;
static bool gSeagateFarmLPage = false;
/* Mode pages supported */
static bool gIecMPage = true; /* N.B. assume it until we know otherwise */
/* Remember last successful mode sense/select command */
static int modese_len = 0;
/* Remember this value from the most recent INQUIRY */
static int scsi_version;
#define SCSI_VERSION_SPC_4 0x6
#define SCSI_VERSION_SPC_5 0x7
#define SCSI_VERSION_SPC_6 0xd /* T10/BSR INCITS 566, proposed in 23-015r0 */
#define SCSI_VERSION_HIGHEST SCSI_VERSION_SPC_6
/* T10 vendor identification. Should match entry in last Annex of SPC
* drafts and standards (e.g. SPC-4). */
static char scsi_vendor[8+1];
#define T10_VENDOR_SEAGATE "SEAGATE"
#define T10_VENDOR_HITACHI_1 "HITACHI"
#define T10_VENDOR_HITACHI_2 "HL-DT-ST"
#define T10_VENDOR_HITACHI_3 "HGST"
static const char * logSenStr = "Log Sense";
static const char * logSenRspStr = "Log Sense response";
static const char * gsap_s = "General statistics and performance";
static const char * ssm_s = "Solid state media";
static const char * zbds_s = "Zoned block device statistics";
static const char * lp_s = "log page";
static bool
seagate_or_hitachi(void)
{
return ((0 == memcmp(scsi_vendor, T10_VENDOR_SEAGATE,
strlen(T10_VENDOR_SEAGATE))) ||
(0 == memcmp(scsi_vendor, T10_VENDOR_HITACHI_1,
strlen(T10_VENDOR_HITACHI_1))) ||
(0 == memcmp(scsi_vendor, T10_VENDOR_HITACHI_2,
strlen(T10_VENDOR_HITACHI_2))) ||
(0 == memcmp(scsi_vendor, T10_VENDOR_HITACHI_3,
strlen(T10_VENDOR_HITACHI_3))));
}
static bool
all_ffs(const uint8_t * bp, int b_len)
{
if ((nullptr == bp) || (b_len <= 0))
return false;
for (--b_len; b_len >= 0; --b_len) {
if (0xff != bp[b_len])
return false;
}
return true;
}
// trim from right. By default trims whitespace.
static std::string rtrim(const std::string& s, const char* t = " \t\n\r\f\v")
{
std::string r(s);
r.erase(r.find_last_not_of(t) + 1);
return r;
}
static void
scsiGetSupportedLogPages(scsi_device * device)
{
bool got_subpages = false;
int k, err, resp_len, num_unreported, num_unreported_spg;
int supp_lpg_and_spg_count = 0;
const uint8_t * up;
uint8_t sup_lpgs[LOG_RESP_LEN];
struct scsi_supp_log_pages supp_lpg_and_spg[SCSI_SUPP_LOG_PAGES_MAX_COUNT];
memset(gBuf, 0, LOG_RESP_LEN);
memset(supp_lpg_and_spg, 0, sizeof(supp_lpg_and_spg));
if (SC_NO_SUPPORT == device->cmd_support_level(LOG_SENSE, false, 0)) {
if (scsi_debugmode > 0)
pout("%s: RSOC says %s not supported\n", __func__, logSenStr);
return;
}
/* Get supported log pages */
if ((err = scsiLogSense(device, SUPPORTED_LPAGES, 0, gBuf,
LOG_RESP_LEN, 0 /* do double fetch */))) {
if (scsi_debugmode > 0)
pout("%s for supported pages failed [%s]\n", logSenStr,
scsiErrString(err));
/* try one more time with defined length, workaround for the bug #678
found with ST8000NM0075/E001 */
err = scsiLogSense(device, SUPPORTED_LPAGES, 0, gBuf,
LOG_RESP_LEN, 68); /* 64 max pages + 4b header */
if (scsi_debugmode > 0)
pout("%s for supported pages failed (second attempt) [%s]\n",
logSenStr, scsiErrString(err));
if (err)
return;
}
memcpy(sup_lpgs, gBuf, LOG_RESP_LEN);
resp_len = gBuf[3];
up = gBuf + LOGPAGEHDRSIZE;
for (k = 0; k < resp_len; k += 1) {
uint8_t page_code = 0x3f & up[k];
supp_lpg_and_spg[supp_lpg_and_spg_count++] = {page_code, 0};
}
if (SC_NO_SUPPORT ==
device->cmd_support_level(LOG_SENSE, false, 0,
true /* does it support subpages ? */))
goto skip_subpages;
/* Get supported log pages and subpages. Most drives seems to include the
supported log pages here as well, but some drives such as the Samsung
PM1643a will only report the additional log pages with subpages here */
if ((scsi_version >= SCSI_VERSION_SPC_4) &&
(scsi_version <= SCSI_VERSION_HIGHEST)) {
/* unclear what code T10 will choose for SPC-6 */
if ((err = scsiLogSense(device, SUPPORTED_LPAGES, SUPP_SPAGE_L_SPAGE,
gBuf, LOG_RESP_LONG_LEN,
-1 /* just single not double fetch */))) {
if (scsi_debugmode > 0)
pout("%s for supported pages and subpages failed [%s]\n",
logSenStr, scsiErrString(err));
} else {
/* Ensure we didn't get the same answer than without the subpages */
if (0 == memcmp(gBuf, sup_lpgs, LOG_RESP_LEN)) {
if (scsi_debugmode > 0)
pout("%s: %s ignored subpage field, bad\n",
__func__, logSenRspStr);
} else if (! ((0x40 & gBuf[0]) &&
(SUPP_SPAGE_L_SPAGE == gBuf[1]))) {
if (scsi_debugmode > 0)
pout("%s supported subpages is bad SPF=%u SUBPG=%u\n",
logSenRspStr, !! (0x40 & gBuf[0]), gBuf[2]);
} else {
got_subpages = true;
}
}
}
if (got_subpages) {
resp_len = sg_get_unaligned_be16(gBuf + 2);
up = gBuf + LOGPAGEHDRSIZE;
for (k = 0; k < resp_len; k += 2) {
uint8_t page_code = 0x3f & up[k];
uint8_t subpage_code = up[k+1];
supp_lpg_and_spg[supp_lpg_and_spg_count++] = {page_code, subpage_code};
}
}
skip_subpages:
num_unreported = 0;
num_unreported_spg = 0;
for (k = 0; k < supp_lpg_and_spg_count; k += 1) {
struct scsi_supp_log_pages supp_lpg = supp_lpg_and_spg[k];
switch (supp_lpg.page_code)
{
case SUPPORTED_LPAGES:
if (! ((NO_SUBPAGE_L_SPAGE == supp_lpg.subpage_code) ||
(SUPP_SPAGE_L_SPAGE == supp_lpg.subpage_code))) {
if (scsi_debugmode > 1)
pout("%s: Strange Log page number: 0x0,0x%x\n",
__func__, supp_lpg.subpage_code);
}
break;
case READ_ERROR_COUNTER_LPAGE:
gReadECounterLPage = true;
break;
case WRITE_ERROR_COUNTER_LPAGE:
gWriteECounterLPage = true;
break;
case VERIFY_ERROR_COUNTER_LPAGE:
gVerifyECounterLPage = true;
break;
case LAST_N_ERROR_EVENTS_LPAGE:
gLastNErrorEvLPage = true;
break;
case NON_MEDIUM_ERROR_LPAGE:
gNonMediumELPage = true;
break;
case TEMPERATURE_LPAGE:
if (NO_SUBPAGE_L_SPAGE == supp_lpg.subpage_code)
gTempLPage = true;
else if (ENVIRO_REP_L_SPAGE == supp_lpg.subpage_code)
gEnviroReportingLPage = true;
else if (ENVIRO_LIMITS_L_SPAGE == supp_lpg.subpage_code)
gEnviroLimitsLPage = true;
else if (SUPP_SPAGE_L_SPAGE != supp_lpg.subpage_code) {
++num_unreported;
++num_unreported_spg;
}
/* WDC/HGST report <lpage>,0xff tuples for all supported
lpages; Seagate doesn't. T10 does not exclude the
reporting of <lpage>,0xff so it is not an error. */
break;
case STARTSTOP_CYCLE_COUNTER_LPAGE:
if (NO_SUBPAGE_L_SPAGE == supp_lpg.subpage_code)
gStartStopLPage = true;
else if (UTILIZATION_L_SPAGE == supp_lpg.subpage_code)
gUtilizationLPage = true;
else if (SUPP_SPAGE_L_SPAGE != supp_lpg.subpage_code) {
++num_unreported;
++num_unreported_spg;
}
break;
case SELFTEST_RESULTS_LPAGE:
gSelfTestLPage = true;
break;
case IE_LPAGE:
gSmartLPage = true;
break;
case DEVICE_STATS_LPAGE:
if (NO_SUBPAGE_L_SPAGE == supp_lpg.subpage_code)
gTapeDeviceStatsLPage = true;
else if (ZB_DEV_STATS_L_SPAGE == supp_lpg.subpage_code)
gZBDeviceStatsLPage = true;
break;
case BACKGROUND_RESULTS_LPAGE:
if (NO_SUBPAGE_L_SPAGE == supp_lpg.subpage_code)
gBackgroundResultsLPage = true;
else if (PEND_DEFECTS_L_SPAGE == supp_lpg.subpage_code)
gPendDefectsLPage = true;
else if (BACKGROUND_OP_L_SPAGE == supp_lpg.subpage_code)
gBackgroundOpLPage = true;
else if (LPS_MISALIGN_L_SPAGE == supp_lpg.subpage_code)
gLPSMisalignLPage = true;
else if (SUPP_SPAGE_L_SPAGE != supp_lpg.subpage_code) {
++num_unreported;
++num_unreported_spg;
}
break;
case PROTOCOL_SPECIFIC_LPAGE:
gProtocolSpecificLPage = true;
break;
case GEN_STATS_PERF_LPAGE:
gGenStatsAndPerfLPage = true;
break;
case TAPE_ALERTS_LPAGE:
gTapeAlertsLPage = true;
break;
case SS_MEDIA_LPAGE:
gSSMediaLPage = true;
break;
case FORMAT_STATUS_LPAGE:
gFormatStatusLPage = true;
break;
case SEAGATE_CACHE_LPAGE:
if (failuretest_permissive) {
gSeagateCacheLPage = true;
break;
}
if (seagate_or_hitachi())
gSeagateCacheLPage = true;
break;
case SEAGATE_FACTORY_LPAGE:
if (failuretest_permissive) {
gSeagateFactoryLPage = true;
break;
}
if (seagate_or_hitachi())
gSeagateFactoryLPage = true;
break;
case SEAGATE_FARM_LPAGE:
if (scsiIsSeagate(scsi_vendor)) {
if (SEAGATE_FARM_CURRENT_L_SPAGE == supp_lpg.subpage_code) {
gSeagateFarmLPage = true;
} else if (SUPP_SPAGE_L_SPAGE != supp_lpg.subpage_code) {
++num_unreported;
++num_unreported_spg;
}
}
break;
default:
if (supp_lpg.page_code < 0x30) { /* don't count VS pages */
++num_unreported;
if ((supp_lpg.subpage_code > 0) &&
(SUPP_SPAGE_L_SPAGE != supp_lpg.subpage_code))
++num_unreported_spg;
}
break;
}
}
if (scsi_debugmode > 1)
pout("%s: number of unreported (standard) %ss: %d (sub-pages: %d)\n",
__func__, lp_s, num_unreported, num_unreported_spg);
}
/* Returns 0 if ok, -1 if can't check IE, -2 if can check and bad
(or at least something to report). */
static int
scsiGetSmartData(scsi_device * device, bool attribs)
{
uint8_t asc;
uint8_t ascq;
uint8_t currenttemp = 255;
uint8_t triptemp = 255;
const char * cp;
int err = 0;
char b[128];
print_on();
if (scsiCheckIE(device, gSmartLPage, gTempLPage, &asc, &ascq,
¤ttemp, &triptemp)) {
/* error message already announced */
print_off();
return -1;
}
print_off();
cp = scsiGetIEString(asc, ascq, b, sizeof(b));
if (cp) {
err = -2;
print_on();
jout("SMART Health Status: %s [asc=%x, ascq=%x]\n", cp, asc, ascq);
print_off();
jglb["smart_status"]["passed"] = false;
jglb["smart_status"]["scsi"]["asc"] = asc;
jglb["smart_status"]["scsi"]["ascq"] = ascq;
jglb["smart_status"]["scsi"]["ie_string"] = cp;
}
else if (gIecMPage) {
jout("SMART Health Status: OK\n");
jglb["smart_status"]["passed"] = true;
}
if (attribs && !gTempLPage) {
if (255 == currenttemp)
pout("Current Drive Temperature: <not available>\n");
else {
jout("Current Drive Temperature: %d C\n", currenttemp);
jglb["temperature"]["current"] = currenttemp;
}
if (255 == triptemp)
pout("Drive Trip Temperature: <not available>\n");
else {
jout("Drive Trip Temperature: %d C\n", triptemp);
jglb["temperature"]["drive_trip"] = triptemp;
}
}
pout("\n");
return err;
}
// Returns number of logged errors or zero if none or -1 if fetching
// TapeAlerts fails
static const char * const severities = "CWI";
static int
scsiPrintActiveTapeAlerts(scsi_device * device, int peripheral_type,
bool from_health)
{
unsigned short pagelength;
unsigned short parametercode;
int i, k, j, m, err;
const char *s;
const char *ts;
int failures = 0;
const char * pad = from_health ? "" : " ";
static const char * const tapealert_s = "scsi_tapealert";
jout("\nTapeAlert %s:\n", lp_s);
print_on();
if ((err = scsiLogSense(device, TAPE_ALERTS_LPAGE, 0, gBuf,
LOG_RESP_TAPE_ALERT_LEN, LOG_RESP_TAPE_ALERT_LEN))) {
pout("%s Failed [%s]\n", __func__, scsiErrString(err));
print_off();
return -1;
}
if (gBuf[0] != 0x2e) {
pout("%sTapeAlerts %s Failed\n", pad, logSenStr);
print_off();
return -1;
}
pagelength = sg_get_unaligned_be16(gBuf + 2);
json::ref jref = jglb[tapealert_s]["status"];
for (s=severities, k = 0, j = 0; *s; s++, ++k) {
for (i = 4, m = 0; i < pagelength; i += 5, ++k, ++m) {
parametercode = sg_get_unaligned_be16(gBuf + i);
if (gBuf[i + 4]) {
ts = SCSI_PT_MEDIUM_CHANGER == peripheral_type ?
scsiTapeAlertsChangerDevice(parametercode) :
scsiTapeAlertsTapeDevice(parametercode);
if (*ts == *s) {
if (!failures)
jout("%sTapeAlert Errors (C=Critical, W=Warning, "
"I=Informational):\n", pad);
jout("%s[0x%02x] %s\n", pad, parametercode, ts);
jref[j]["descriptor_idx"] = m + 1;
jref[j]["parameter_code"] = parametercode;
jref[j]["string"] = ts;
++j;
failures += 1;
}
}
}
}
print_off();
if (! failures) {
jout("%sTapeAlert: OK\n", pad);
jglb[tapealert_s]["status"] = "Good";
}
return failures;
}
static void
scsiGetStartStopData(scsi_device * device)
{
int err, len, k, extra;
unsigned char * ucp;
char b[32];
const char * q;
static const char * jname = "scsi_start_stop_cycle_counter";
if ((err = scsiLogSense(device, STARTSTOP_CYCLE_COUNTER_LPAGE, 0, gBuf,
LOG_RESP_LEN, 0))) {
print_on();
pout("%s Failed [%s]\n", __func__, scsiErrString(err));
print_off();
return;
}
if ((gBuf[0] & 0x3f) != STARTSTOP_CYCLE_COUNTER_LPAGE) {
print_on();
pout("StartStop %s Failed, page mismatch\n", logSenStr);
print_off();
return;
}
len = sg_get_unaligned_be16(gBuf + 2);
ucp = gBuf + 4;
for (k = len; k > 0; k -= extra, ucp += extra) {
if (k < 3) {
print_on();
pout("StartStop %s: short\n", logSenRspStr);
print_off();
return;
}
extra = ucp[3] + 4;
int pc = sg_get_unaligned_be16(ucp + 0);
uint32_t u = (extra > 7) ? sg_get_unaligned_be32(ucp + 4) : 0;
bool is_all_ffs = (extra > 7) ? all_ffs(ucp + 4, 4) : false;
switch (pc) {
case 1:
if (10 == extra) {
jout("Manufactured in week %.2s of year %.4s\n", ucp + 8,
ucp + 4);
snprintf(b, sizeof(b), "%.4s", ucp + 4);
jglb[jname]["year_of_manufacture"] = b;
snprintf(b, sizeof(b), "%.2s", ucp + 8);
jglb[jname]["week_of_manufacture"] = b;
}
break;
case 2:
/* ignore Accounting date */
break;
case 3:
if ((extra > 7) && (! is_all_ffs)) {
q = "Specified cycle count over device lifetime";
jout("%s: %u\n", q, u);
jglb[jname][json::str2key(q)] = u;
}
break;
case 4:
if ((extra > 7) && (! is_all_ffs)) {
q = "Accumulated start-stop cycles";
jout("%s: %u\n", q, u);
jglb[jname][json::str2key(q)] = u;
}
break;
case 5:
if ((extra > 7) && (! is_all_ffs)) {
q = "Specified load-unload count over device lifetime";
jout("%s: %u\n", q, u);
jglb[jname][json::str2key(q)] = u;
}
break;
case 6:
if ((extra > 7) && (! is_all_ffs)) {
q = "Accumulated load-unload cycles";
jout("%s: %u\n", q, u);
jglb[jname][json::str2key(q)] = u;
}
break;
default:
/* ignore */
break;
}
}
}
/* PENDING_DEFECTS_SUBPG [0x15,0x1] introduced: SBC-4 */
static void
scsiPrintPendingDefectsLPage(scsi_device * device)
{
static const char * pDefStr = "Pending Defects";
static const char * jname = "scsi_pending_defects";
int err;
if ((err = scsiLogSense(device, BACKGROUND_RESULTS_LPAGE,
PEND_DEFECTS_L_SPAGE, gBuf, LOG_RESP_LONG_LEN,
0))) {
print_on();
pout("%s Failed [%s]\n", __func__, scsiErrString(err));
print_off();
return;
}
if (((gBuf[0] & 0x3f) != BACKGROUND_RESULTS_LPAGE) &&
(gBuf[1] != PEND_DEFECTS_L_SPAGE)) {
print_on();
pout("%s %s, page mismatch\n", pDefStr, logSenRspStr);
print_off();
return;
}
int num = sg_get_unaligned_be16(gBuf + 2);
if (num > LOG_RESP_LONG_LEN) {
print_on();
pout("%s %s too long\n", pDefStr, logSenRspStr);
print_off();
return;
}
const uint8_t * bp = gBuf + 4;
while (num > 3) {
int pc = sg_get_unaligned_be16(bp + 0);
int pl = bp[3] + 4;
uint32_t count, poh;
uint64_t lba;
switch (pc) {
case 0x0:
jout(" Pending defect count:");
if ((pl < 8) || (num < 8)) {
print_on();
pout("%s truncated descriptor\n", pDefStr);
print_off();
return;
}
count = sg_get_unaligned_be32(bp + 4);
jglb[jname]["count"] = count;
if (0 == count)
jout("0 %s\n", pDefStr);
else if (1 == count)
jout("1 Pending Defect, LBA and accumulated_power_on_hours "
"follow\n");
else
jout("%u %s: index, LBA and accumulated_power_on_hours "
"follow\n", count, pDefStr);
break;
default:
if ((pl < 16) || (num < 16)) {
print_on();
pout("%s truncated descriptor\n", pDefStr);
print_off();
return;
}
poh = sg_get_unaligned_be32(bp + 4);
lba = sg_get_unaligned_be64(bp + 8);
jout(" %4d: 0x%-16" PRIx64 ", %5u\n", pc, lba, poh);
{
json::ref jref = jglb[jname]["table"][pc];
jref["lba"] = lba;
jref["accum_power_on_hours"] = poh;
}
break;
}
num -= pl;
bp += pl;
}
}
static void
scsiPrintGrownDefectListLen(scsi_device * device, bool prefer12)
{
bool got_rd12;
int err, dl_format;
unsigned int dl_len, div;
static const char * hname = "Read defect list";
memset(gBuf, 0, 8);
if (prefer12) {
err = scsiReadDefect12(device, 0 /* req_plist */, 1 /* req_glist */,
4 /* format: bytes from index */,
0 /* addr desc index */, gBuf, 8);
got_rd12 = (0 == err);
if (err) {
if (scsi_debugmode > 0) {
print_on();
pout("%s (12) Failed: %s\n", hname, scsiErrString(err));
print_off();
}
}
} else { /* still try Read Defect(12) first, if not found try RD(10) */
err = scsiReadDefect12(device, 0 /* req_plist */, 1 /* req_glist */,
4 /* format: bytes from index */,
0 /* addr desc index */, gBuf, 8);
if (2 == err) { /* command not supported */
err = scsiReadDefect10(device, 0 /* req_plist */,
1 /* req_glist */,
4 /* format: bytes from index */, gBuf, 4);
if (2 == err) { /* command not supported */
if (scsi_debugmode > 0) {
print_on();
pout("%s (10) Failed: %s\n", hname, scsiErrString(err));
print_off();
}
return;
} else if (101 == err) /* Defect list not found, leave quietly */
return;
else {
if (scsi_debugmode > 0) {
print_on();
pout("%s (12) Failed: %s\n", hname, scsiErrString(err));
print_off();
}
return;
}
} else
got_rd12 = true;
}
if (got_rd12) {
int generation = sg_get_unaligned_be16(gBuf + 2);
if ((generation > 1) && (scsi_debugmode > 0)) {
print_on();
pout("%s (12): generation=%d\n", hname, generation);
print_off();
}
dl_len = sg_get_unaligned_be32(gBuf + 4);
} else
dl_len = sg_get_unaligned_be16(gBuf + 2);
if (0x8 != (gBuf[1] & 0x18)) {
print_on();
pout("%s: asked for grown list but didn't get it\n", hname);
print_off();
return;
}
div = 0;
dl_format = (gBuf[1] & 0x7);
switch (dl_format) {
case 0: /* short block */
div = 4;
break;
case 1: /* extended bytes from index */
case 2: /* extended physical sector */
/* extended = 1; # might use in future */
div = 8;
break;
case 3: /* long block */
case 4: /* bytes from index */
case 5: /* physical sector */
div = 8;
break;
case 6: /* vendor specific */
break;
default:
print_on();
pout("defect list format %d unknown\n", dl_format);
print_off();
break;
}
if (0 == dl_len) {
jout("Elements in grown defect list: 0\n\n");
jglb["scsi_grown_defect_list"] = 0;
}
else {
if (0 == div)
pout("Grown defect list length=%u bytes [unknown "
"number of elements]\n\n", dl_len);
else {
jout("Elements in grown defect list: %u\n\n", dl_len / div);
jglb["scsi_grown_defect_list"] = dl_len / div;
}
}
}
static uint64_t
variableLengthIntegerParam(const unsigned char * ucp)
{
static const size_t sz_u64 = (int)sizeof(uint64_t);
unsigned int u = ucp[3];
const unsigned char * xp = ucp + 4;
if (u > sz_u64) {
xp += (u - sz_u64);
u = sz_u64;
}
return sg_get_unaligned_be(u, xp + 0);
}
static void
scsiPrintSeagateCacheLPage(scsi_device * device)
{
int num, pl, pc, err, len;
unsigned char * ucp;
static const char * seaCacStr = "Seagate Cache";
if ((err = scsiLogSense(device, SEAGATE_CACHE_LPAGE, 0, gBuf,
LOG_RESP_LEN, 0))) {
if (scsi_debugmode > 0) {
print_on();
pout("%s %s Failed: %s\n", seaCacStr, logSenStr,
scsiErrString(err));
print_off();
}
return;
}
if ((gBuf[0] & 0x3f) != SEAGATE_CACHE_LPAGE) {
if (scsi_debugmode > 0) {
print_on();
pout("%s %s, page mismatch\n", seaCacStr, logSenRspStr);
print_off();
}
return;
}
len = sg_get_unaligned_be16(gBuf + 2) + 4;
num = len - 4;
ucp = &gBuf[0] + 4;
while (num > 3) {
pc = sg_get_unaligned_be16(ucp + 0);
pl = ucp[3] + 4;
switch (pc) {
case 0: case 1: case 2: case 3: case 4:
break;
default:
if (scsi_debugmode > 0) {
print_on();
pout("Vendor (%s) lpage has unexpected parameter, skip\n",
seaCacStr);
print_off();
}
return;
}
num -= pl;
ucp += pl;
}
pout("Vendor (%s) information\n", seaCacStr);
num = len - 4;
ucp = &gBuf[0] + 4;
while (num > 3) {
pc = sg_get_unaligned_be16(ucp + 0);
pl = ucp[3] + 4;
switch (pc) {
case 0: pout(" Blocks sent to initiator"); break;
case 1: pout(" Blocks received from initiator"); break;
case 2: pout(" Blocks read from cache and sent to initiator"); break;
case 3: pout(" Number of read and write commands whose size "
"<= segment size"); break;
case 4: pout(" Number of read and write commands whose size "
"> segment size"); break;
default: pout(" Unknown Seagate parameter code [0x%x]", pc); break;
}
pout(" = %" PRIu64 "\n", variableLengthIntegerParam(ucp));
num -= pl;
ucp += pl;
}
pout("\n");
}
static void
scsiPrintSeagateFactoryLPage(scsi_device * device)
{
int num, pl, pc, len, err, good, bad;
unsigned char * ucp;
uint64_t ull;
if ((err = scsiLogSense(device, SEAGATE_FACTORY_LPAGE, 0, gBuf,
LOG_RESP_LEN, 0))) {
if (scsi_debugmode > 0) {
print_on();
pout("%s Failed [%s]\n", __func__, scsiErrString(err));
print_off();
}
return;
}
if ((gBuf[0] & 0x3f) != SEAGATE_FACTORY_LPAGE) {
if (scsi_debugmode > 0) {
print_on();
pout("Seagate/Hitachi Factory %s, page mismatch\n", logSenRspStr);
print_off();
}
return;
}
len = sg_get_unaligned_be16(gBuf + 2) + 4;
num = len - 4;
ucp = &gBuf[0] + 4;
good = 0;
bad = 0;
while (num > 3) {
pc = sg_get_unaligned_be16(ucp + 0);
pl = ucp[3] + 4;
switch (pc) {
case 0: case 8:
++good;
break;
default:
++bad;
break;
}
num -= pl;
ucp += pl;
}
if ((good < 2) || (bad > 4)) { /* heuristic */
if (scsi_debugmode > 0) {
print_on();
pout("\nVendor (Seagate/Hitachi) factory lpage has too many "
"unexpected parameters, skip\n");
print_off();
}
return;
}
pout("Vendor (Seagate/Hitachi) factory information\n");
num = len - 4;
ucp = &gBuf[0] + 4;
while (num > 3) {
pc = sg_get_unaligned_be16(ucp + 0);
pl = ucp[3] + 4;
good = 0;
switch (pc) {
case 0: jout(" number of hours powered up");
good = 1;
break;
case 8: pout(" number of minutes until next internal SMART test");
good = 1;
break;
default:
if (scsi_debugmode > 0) {
print_on();
pout("Vendor (Seagate/Hitachi) factory lpage: "
"unknown parameter code [0x%x]\n", pc);
print_off();
}
break;
}
if (good) {
ull = variableLengthIntegerParam(ucp);
if (0 == pc) {
jout(" = %.2f\n", ull / 60.0 );
jglb["power_on_time"]["hours"] = ull / 60;
jglb["power_on_time"]["minutes"] = ull % 60;
}
else
pout(" = %" PRIu64 "\n", ull);
}
num -= pl;
ucp += pl;
}
pout("\n");
}
static void
scsiPrintErrorCounterLog(scsi_device * device)
{
struct scsiErrorCounter errCounterArr[3];
struct scsiErrorCounter * ecp;
int found[3] = {0, 0, 0};
if (gReadECounterLPage && (0 == scsiLogSense(device,
READ_ERROR_COUNTER_LPAGE, 0, gBuf, LOG_RESP_LEN, 0))) {
scsiDecodeErrCounterPage(gBuf, &errCounterArr[0], LOG_RESP_LEN);
found[0] = 1;
}
if (gWriteECounterLPage && (0 == scsiLogSense(device,
WRITE_ERROR_COUNTER_LPAGE, 0, gBuf, LOG_RESP_LEN, 0))) {
scsiDecodeErrCounterPage(gBuf, &errCounterArr[1], LOG_RESP_LEN);
found[1] = 1;
}
if (gVerifyECounterLPage && (0 == scsiLogSense(device,
VERIFY_ERROR_COUNTER_LPAGE, 0, gBuf, LOG_RESP_LEN, 0))) {
scsiDecodeErrCounterPage(gBuf, &errCounterArr[2], LOG_RESP_LEN);
ecp = &errCounterArr[2];
for (int k = 0; k < 7; ++k) {
if (ecp->gotPC[k] && ecp->counter[k]) {
found[2] = 1;
break;
}
}
}
if (found[0] || found[1] || found[2]) {
pout("Error counter log:\n");
pout(" Errors Corrected by Total "
"Correction Gigabytes Total\n");
pout(" ECC rereads/ errors "
"algorithm processed uncorrected\n");
pout(" fast | delayed rewrites corrected "
"invocations [10^9 bytes] errors\n");
json::ref jref = jglb["scsi_error_counter_log"];
for (int k = 0; k < 3; ++k) {
if (! found[k])
continue;
ecp = &errCounterArr[k];
static const char * const pageNames[3] =
{"read: ", "write: ", "verify: "};
static const char * jpageNames[3] =
{"read", "write", "verify"};
jout("%s%8" PRIu64 " %8" PRIu64 " %8" PRIu64 " %8" PRIu64
" %8" PRIu64, pageNames[k], ecp->counter[0],
ecp->counter[1], ecp->counter[2], ecp->counter[3],
ecp->counter[4]);
double processed_gb = ecp->counter[5] / 1000000000.0;
jout(" %12.3f %8" PRIu64 "\n", processed_gb,
ecp->counter[6]);
// Error counter log info
jref[jpageNames[k]]["errors_corrected_by_eccfast"] = ecp->counter[0];
jref[jpageNames[k]]["errors_corrected_by_eccdelayed"] = ecp->counter[1];
jref[jpageNames[k]]["errors_corrected_by_rereads_rewrites"] = ecp->counter[2];
jref[jpageNames[k]]["total_errors_corrected"] = ecp->counter[3];
jref[jpageNames[k]]["correction_algorithm_invocations"] = ecp->counter[4];
jref[jpageNames[k]]["gigabytes_processed"] = strprintf("%.3f", processed_gb);
jref[jpageNames[k]]["total_uncorrected_errors"] = ecp->counter[6];
}
}
else
pout("Error Counter logging not supported\n");
if (gNonMediumELPage && (0 == scsiLogSense(device,
NON_MEDIUM_ERROR_LPAGE, 0, gBuf, LOG_RESP_LEN, 0))) {
struct scsiNonMediumError nme;
scsiDecodeNonMediumErrPage(gBuf, &nme, LOG_RESP_LEN);