forked from allanliu/smartmontools
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscsicmds.cpp
2721 lines (2560 loc) · 96.2 KB
/
scsicmds.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
/*
* scsicmds.cpp
*
* Home page of code is: http://www.smartmontools.org
*
* Copyright (C) 2002-8 Bruce Allen
* Copyright (C) 1999-2000 Michael Cornwell <[email protected]>
* Copyright (C) 2003-16 Douglas Gilbert <[email protected]>
*
* 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, or (at your option)
* any later version.
*
* You should have received a copy of the GNU General Public License
* (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
*
* This code was originally developed as a Senior Thesis by Michael Cornwell
* at the Concurrent Systems Laboratory (now part of the Storage Systems
* Research Center), Jack Baskin School of Engineering, University of
* California, Santa Cruz. http://ssrc.soe.ucsc.edu/
*
*
* In the SCSI world "SMART" is a dead or withdrawn standard. In recent
* SCSI standards (since SCSI-3) it goes under the awkward name of
* "Informational Exceptions" ["IE" or "IEC" (with the "C" for "control")].
* The relevant information is spread around several SCSI draft
* standards available at http://www.t10.org . Reference is made in the
* code to the following acronyms:
* - SAM [SCSI Architectural model, versions 2 or 3]
* - SPC [SCSI Primary commands, versions 2 or 3]
* - SBC [SCSI Block commands, versions 2]
*
* Some SCSI disk vendors have snippets of "SMART" information in their
* product manuals.
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include "config.h"
#include "int64.h"
#include "scsicmds.h"
#include "atacmds.h" // FIXME: for smart_command_set only
#include "dev_interface.h"
#include "utility.h"
const char *scsicmds_c_cvsid="$Id$"
SCSICMDS_H_CVSID;
// Print SCSI debug messages?
unsigned char scsi_debugmode = 0;
supported_vpd_pages * supported_vpd_pages_p = NULL;
supported_vpd_pages::supported_vpd_pages(scsi_device * device) : num_valid(0)
{
unsigned char b[0xfc]; /* pre SPC-3 INQUIRY max response size */
memset(b, 0, sizeof(b));
if (device && (0 == scsiInquiryVpd(device, SCSI_VPD_SUPPORTED_VPD_PAGES,
b, sizeof(b)))) {
num_valid = (b[2] << 8) + b[3];
int n = sizeof(pages);
if (num_valid > n)
num_valid = n;
memcpy(pages, b + 4, num_valid);
}
}
bool
supported_vpd_pages::is_supported(int vpd_page_num) const
{
/* Supported VPD pages numbers start at offset 4 and should be in
* ascending order but don't assume that. */
for (int k = 0; k < num_valid; ++k) {
if (vpd_page_num == pages[k])
return true;
}
return false;
}
/* output binary in hex and optionally ascii */
void
dStrHex(const char* str, int len, int no_ascii)
{
const char* p = str;
char buff[82];
int a = 0;
const int bpstart = 5;
const int cpstart = 60;
int cpos = cpstart;
int bpos = bpstart;
int i, k;
if (len <= 0) return;
memset(buff,' ',80);
buff[80]='\0';
k = snprintf(buff+1, sizeof(buff)-1, "%.2x", a);
buff[k + 1] = ' ';
if (bpos >= ((bpstart + (9 * 3))))
bpos++;
for(i = 0; i < len; i++)
{
unsigned char c = *p++;
bpos += 3;
if (bpos == (bpstart + (9 * 3)))
bpos++;
snprintf(buff+bpos, sizeof(buff)-bpos, "%.2x", (int)(unsigned char)c);
buff[bpos + 2] = ' ';
if (no_ascii)
buff[cpos++] = ' ';
else {
if ((c < ' ') || (c >= 0x7f))
c='.';
buff[cpos++] = c;
}
if (cpos > (cpstart+15))
{
while (cpos > 0 && buff[cpos-1] == ' ')
cpos--;
buff[cpos] = 0;
pout("%s\n", buff);
bpos = bpstart;
cpos = cpstart;
a += 16;
memset(buff,' ',80);
k = snprintf(buff+1, sizeof(buff)-1, "%.2x", a);
buff[k + 1] = ' ';
}
}
if (cpos > cpstart)
{
while (cpos > 0 && buff[cpos-1] == ' ')
cpos--;
buff[cpos] = 0;
pout("%s\n", buff);
}
}
struct scsi_opcode_name {
UINT8 opcode;
const char * name;
};
static struct scsi_opcode_name opcode_name_arr[] = {
/* in ascending opcode order */
{TEST_UNIT_READY, "test unit ready"}, /* 0x00 */
{REQUEST_SENSE, "request sense"}, /* 0x03 */
{INQUIRY, "inquiry"}, /* 0x12 */
{MODE_SELECT, "mode select(6)"}, /* 0x15 */
{MODE_SENSE, "mode sense(6)"}, /* 0x1a */
{START_STOP_UNIT, "start stop unit"}, /* 0x1b */
{RECEIVE_DIAGNOSTIC, "receive diagnostic"}, /* 0x1c */
{SEND_DIAGNOSTIC, "send diagnostic"}, /* 0x1d */
{READ_CAPACITY_10, "read capacity(10)"}, /* 0x25 */
{READ_DEFECT_10, "read defect list(10)"}, /* 0x37 */
{LOG_SELECT, "log select"}, /* 0x4c */
{LOG_SENSE, "log sense"}, /* 0x4d */
{MODE_SELECT_10, "mode select(10)"}, /* 0x55 */
{MODE_SENSE_10, "mode sense(10)"}, /* 0x5a */
{SAT_ATA_PASSTHROUGH_16, "ata pass-through(16)"}, /* 0x85 */
{READ_CAPACITY_16, "read capacity(16)"}, /* 0x9e,0x10 */
{REPORT_LUNS, "report luns"}, /* 0xa0 */
{SAT_ATA_PASSTHROUGH_12, "ata pass-through(12)"}, /* 0xa1 */
{READ_DEFECT_12, "read defect list(12)"}, /* 0xb7 */
};
static const char * vendor_specific = "<vendor specific>";
/* Need to expand to take service action into account. For commands
* of interest the service action is in the 2nd command byte */
const char *
scsi_get_opcode_name(UINT8 opcode)
{
int len = sizeof(opcode_name_arr) / sizeof(opcode_name_arr[0]);
if (opcode >= 0xc0)
return vendor_specific;
for (int k = 0; k < len; ++k) {
struct scsi_opcode_name * onp = &opcode_name_arr[k];
if (opcode == onp->opcode)
return onp->name;
else if (opcode < onp->opcode)
return NULL;
}
return NULL;
}
void
scsi_do_sense_disect(const struct scsi_cmnd_io * io_buf,
struct scsi_sense_disect * out)
{
memset(out, 0, sizeof(struct scsi_sense_disect));
if (SCSI_STATUS_CHECK_CONDITION == io_buf->scsi_status) {
int resp_code = (io_buf->sensep[0] & 0x7f);
out->resp_code = resp_code;
if (resp_code >= 0x72) {
out->sense_key = (io_buf->sensep[1] & 0xf);
out->asc = io_buf->sensep[2];
out->ascq = io_buf->sensep[3];
} else if (resp_code >= 0x70) {
out->sense_key = (io_buf->sensep[2] & 0xf);
if (io_buf->resp_sense_len > 13) {
out->asc = io_buf->sensep[12];
out->ascq = io_buf->sensep[13];
}
}
}
}
int
scsiSimpleSenseFilter(const struct scsi_sense_disect * sinfo)
{
switch (sinfo->sense_key) {
case SCSI_SK_NO_SENSE:
case SCSI_SK_RECOVERED_ERR:
return SIMPLE_NO_ERROR;
case SCSI_SK_NOT_READY:
if (SCSI_ASC_NO_MEDIUM == sinfo->asc)
return SIMPLE_ERR_NO_MEDIUM;
else if (SCSI_ASC_NOT_READY == sinfo->asc) {
if (0x1 == sinfo->ascq)
return SIMPLE_ERR_BECOMING_READY;
else
return SIMPLE_ERR_NOT_READY;
} else
return SIMPLE_ERR_NOT_READY;
case SCSI_SK_MEDIUM_ERROR:
case SCSI_SK_HARDWARE_ERROR:
return SIMPLE_ERR_MEDIUM_HARDWARE;
case SCSI_SK_ILLEGAL_REQUEST:
if (SCSI_ASC_UNKNOWN_OPCODE == sinfo->asc)
return SIMPLE_ERR_BAD_OPCODE;
else if (SCSI_ASC_INVALID_FIELD == sinfo->asc)
return SIMPLE_ERR_BAD_FIELD;
else if (SCSI_ASC_UNKNOWN_PARAM == sinfo->asc)
return SIMPLE_ERR_BAD_PARAM;
else
return SIMPLE_ERR_BAD_PARAM; /* all other illegal request */
case SCSI_SK_UNIT_ATTENTION:
return SIMPLE_ERR_TRY_AGAIN;
case SCSI_SK_ABORTED_COMMAND:
return SIMPLE_ERR_ABORTED_COMMAND;
default:
return SIMPLE_ERR_UNKNOWN;
}
}
const char *
scsiErrString(int scsiErr)
{
if (scsiErr < 0)
return strerror(-scsiErr);
switch (scsiErr) {
case SIMPLE_NO_ERROR:
return "no error";
case SIMPLE_ERR_NOT_READY:
return "device not ready";
case SIMPLE_ERR_BAD_OPCODE:
return "unsupported scsi opcode";
case SIMPLE_ERR_BAD_FIELD:
return "unsupported field in scsi command";
case SIMPLE_ERR_BAD_PARAM:
return "badly formed scsi parameters";
case SIMPLE_ERR_BAD_RESP:
return "scsi response fails sanity test";
case SIMPLE_ERR_NO_MEDIUM:
return "no medium present";
case SIMPLE_ERR_BECOMING_READY:
return "device will be ready soon";
case SIMPLE_ERR_TRY_AGAIN:
return "unit attention reported, try again";
case SIMPLE_ERR_MEDIUM_HARDWARE:
return "medium or hardware error (serious)";
case SIMPLE_ERR_UNKNOWN:
return "unknown error (unexpected sense key)";
case SIMPLE_ERR_ABORTED_COMMAND:
return "aborted command";
default:
return "unknown error";
}
}
/* Iterates to next designation descriptor in the device identification
* VPD page. The 'initial_desig_desc' should point to start of first
* descriptor with 'page_len' being the number of valid bytes in that
* and following descriptors. To start, 'off' should point to a negative
* value, thereafter it should point to the value yielded by the previous
* call. If 0 returned then 'initial_desig_desc + *off' should be a valid
* descriptor; returns -1 if normal end condition and -2 for an abnormal
* termination. Matches association, designator_type and/or code_set when
* any of those values are greater than or equal to zero. */
int
scsi_vpd_dev_id_iter(const unsigned char * initial_desig_desc, int page_len,
int * off, int m_assoc, int m_desig_type, int m_code_set)
{
const unsigned char * ucp;
int k;
for (k = *off, ucp = initial_desig_desc ; (k + 3) < page_len; ) {
k = (k < 0) ? 0 : (k + ucp[k + 3] + 4);
if ((k + 4) > page_len)
break;
int c_set = (ucp[k] & 0xf);
if ((m_code_set >= 0) && (m_code_set != c_set))
continue;
int assoc = ((ucp[k + 1] >> 4) & 0x3);
if ((m_assoc >= 0) && (m_assoc != assoc))
continue;
int desig_type = (ucp[k + 1] & 0xf);
if ((m_desig_type >= 0) && (m_desig_type != desig_type))
continue;
*off = k;
return 0;
}
return (k == page_len) ? -1 : -2;
}
/* Decode VPD page 0x83 logical unit designator into a string. If both
* numeric address and SCSI name string present, prefer the former.
* Returns 0 on success, -1 on error with error string in s. */
int
scsi_decode_lu_dev_id(const unsigned char * b, int blen, char * s, int slen,
int * transport)
{
if (transport)
*transport = -1;
if (slen < 32) {
if (slen > 0)
s[0] = '\0';
return -1;
}
s[0] = '\0';
int si = 0;
int have_scsi_ns = 0;
int off = -1;
int u;
while ((u = scsi_vpd_dev_id_iter(b, blen, &off, -1, -1, -1)) == 0) {
const unsigned char * ucp = b + off;
int i_len = ucp[3];
if ((off + i_len + 4) > blen) {
snprintf(s+si, slen-si, "error: designator length");
return -1;
}
int assoc = ((ucp[1] >> 4) & 0x3);
if (transport && assoc && (ucp[1] & 0x80) && (*transport < 0))
*transport = (ucp[0] >> 4) & 0xf;
if (0 != assoc)
continue;
const unsigned char * ip = ucp + 4;
int c_set = (ucp[0] & 0xf);
int desig_type = (ucp[1] & 0xf);
int naa;
switch (desig_type) {
case 0: /* vendor specific */
case 1: /* T10 vendor identification */
break;
case 2: /* EUI-64 based */
if ((8 != i_len) && (12 != i_len) && (16 != i_len)) {
snprintf(s+si, slen-si, "error: EUI-64 length");
return -1;
}
if (have_scsi_ns)
si = 0;
si += snprintf(s+si, slen-si, "0x");
for (int m = 0; m < i_len; ++m)
si += snprintf(s+si, slen-si, "%02x", (unsigned int)ip[m]);
break;
case 3: /* NAA */
if (1 != c_set) {
snprintf(s+si, slen-si, "error: NAA bad code_set");
return -1;
}
naa = (ip[0] >> 4) & 0xff;
if ((naa < 2) || (naa > 6) || (4 == naa)) {
snprintf(s+si, slen-si, "error: unexpected NAA");
return -1;
}
if (have_scsi_ns)
si = 0;
if (2 == naa) { /* NAA IEEE Extended */
if (8 != i_len) {
snprintf(s+si, slen-si, "error: NAA 2 length");
return -1;
}
si += snprintf(s+si, slen-si, "0x");
for (int m = 0; m < 8; ++m)
si += snprintf(s+si, slen-si, "%02x", (unsigned int)ip[m]);
} else if ((3 == naa ) || (5 == naa)) {
/* NAA=3 Locally assigned; NAA=5 IEEE Registered */
if (8 != i_len) {
snprintf(s+si, slen-si, "error: NAA 3 or 5 length");
return -1;
}
si += snprintf(s+si, slen-si, "0x");
for (int m = 0; m < 8; ++m)
si += snprintf(s+si, slen-si, "%02x", (unsigned int)ip[m]);
} else if (6 == naa) { /* NAA IEEE Registered extended */
if (16 != i_len) {
snprintf(s+si, slen-si, "error: NAA 6 length");
return -1;
}
si += snprintf(s+si, slen-si, "0x");
for (int m = 0; m < 16; ++m)
si += snprintf(s+si, slen-si, "%02x", (unsigned int)ip[m]);
}
break;
case 4: /* Relative target port */
case 5: /* (primary) Target port group */
case 6: /* Logical unit group */
case 7: /* MD5 logical unit identifier */
break;
case 8: /* SCSI name string */
if (3 != c_set) {
snprintf(s+si, slen-si, "error: SCSI name string");
return -1;
}
/* does %s print out UTF-8 ok?? */
if (si == 0) {
si += snprintf(s+si, slen-si, "%s", (const char *)ip);
++have_scsi_ns;
}
break;
default: /* reserved */
break;
}
}
if (-2 == u) {
snprintf(s+si, slen-si, "error: bad structure");
return -1;
}
return 0;
}
/* Sends LOG SENSE command. Returns 0 if ok, 1 if device NOT READY, 2 if
command not supported, 3 if field (within command) not supported or
returns negated errno. SPC-3 sections 6.6 and 7.2 (rec 22a).
N.B. Sets PC==1 to fetch "current cumulative" log pages.
If known_resp_len > 0 then a single fetch is done for this response
length. If known_resp_len == 0 then twin fetches are performed, the
first to deduce the response length, then send the same command again
requesting the deduced response length. This protects certain fragile
HBAs. The twin fetch technique should not be used with the TapeAlert
log page since it clears its state flags after each fetch. */
int
scsiLogSense(scsi_device * device, int pagenum, int subpagenum, UINT8 *pBuf,
int bufLen, int known_resp_len)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[10];
UINT8 sense[32];
int pageLen;
if (known_resp_len > bufLen)
return -EIO;
if (known_resp_len > 0)
pageLen = known_resp_len;
else {
/* Starting twin fetch strategy: first fetch to find respone length */
pageLen = 4;
if (pageLen > bufLen)
return -EIO;
else
memset(pBuf, 0, pageLen);
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_FROM_DEVICE;
io_hdr.dxfer_len = pageLen;
io_hdr.dxferp = pBuf;
cdb[0] = LOG_SENSE;
cdb[2] = 0x40 | (pagenum & 0x3f); /* Page control (PC)==1 */
cdb[3] = subpagenum;
cdb[7] = (pageLen >> 8) & 0xff;
cdb[8] = pageLen & 0xff;
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
int res;
if ((res = scsiSimpleSenseFilter(&sinfo)))
return res;
/* sanity check on response */
if ((SUPPORTED_LPAGES != pagenum) && ((pBuf[0] & 0x3f) != pagenum))
return SIMPLE_ERR_BAD_RESP;
if (0 == ((pBuf[2] << 8) + pBuf[3]))
return SIMPLE_ERR_BAD_RESP;
pageLen = (pBuf[2] << 8) + pBuf[3] + 4;
if (4 == pageLen) /* why define a lpage with no payload? */
pageLen = 252; /* some IBM tape drives don't like double fetch */
/* some SCSI HBA don't like "odd" length transfers */
if (pageLen % 2)
pageLen += 1;
if (pageLen > bufLen)
pageLen = bufLen;
}
memset(pBuf, 0, 4);
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_FROM_DEVICE;
io_hdr.dxfer_len = pageLen;
io_hdr.dxferp = pBuf;
cdb[0] = LOG_SENSE;
cdb[2] = 0x40 | (pagenum & 0x3f); /* Page control (PC)==1 */
cdb[7] = (pageLen >> 8) & 0xff;
cdb[8] = pageLen & 0xff;
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
int status = scsiSimpleSenseFilter(&sinfo);
if (0 != status)
return status;
/* sanity check on response */
if ((SUPPORTED_LPAGES != pagenum) && ((pBuf[0] & 0x3f) != pagenum))
return SIMPLE_ERR_BAD_RESP;
if (0 == ((pBuf[2] << 8) + pBuf[3]))
return SIMPLE_ERR_BAD_RESP;
return 0;
}
/* Sends a LOG SELECT command. Can be used to set log page values
* or reset one log page (or all of them) to its defaults (typically zero).
* Returns 0 if ok, 1 if NOT READY, 2 if command not supported, * 3 if
* field in command not supported, * 4 if bad parameter to command or
* returns negated errno. SPC-4 sections 6.5 and 7.2 (rev 20) */
int
scsiLogSelect(scsi_device * device, int pcr, int sp, int pc, int pagenum,
int subpagenum, UINT8 *pBuf, int bufLen)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[10];
UINT8 sense[32];
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_TO_DEVICE;
io_hdr.dxfer_len = bufLen;
io_hdr.dxferp = pBuf;
cdb[0] = LOG_SELECT;
cdb[1] = (pcr ? 2 : 0) | (sp ? 1 : 0);
cdb[2] = ((pc << 6) & 0xc0) | (pagenum & 0x3f);
cdb[3] = (subpagenum & 0xff);
cdb[7] = ((bufLen >> 8) & 0xff);
cdb[8] = (bufLen & 0xff);
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
return scsiSimpleSenseFilter(&sinfo);
}
/* Send MODE SENSE (6 byte) command. Returns 0 if ok, 1 if NOT READY,
* 2 if command not supported (then MODE SENSE(10) should be supported),
* 3 if field in command not supported or returns negated errno.
* SPC-3 sections 6.9 and 7.4 (rev 22a) [mode subpage==0] */
int
scsiModeSense(scsi_device * device, int pagenum, int subpagenum, int pc,
UINT8 *pBuf, int bufLen)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[6];
UINT8 sense[32];
if ((bufLen < 0) || (bufLen > 255))
return -EINVAL;
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_FROM_DEVICE;
io_hdr.dxfer_len = bufLen;
io_hdr.dxferp = pBuf;
cdb[0] = MODE_SENSE;
cdb[2] = (pc << 6) | (pagenum & 0x3f);
cdb[3] = subpagenum;
cdb[4] = bufLen;
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
int status = scsiSimpleSenseFilter(&sinfo);
if (SIMPLE_ERR_TRY_AGAIN == status) {
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
status = scsiSimpleSenseFilter(&sinfo);
}
if ((0 == status) && (ALL_MODE_PAGES != pagenum)) {
int offset;
offset = scsiModePageOffset(pBuf, bufLen, 0);
if (offset < 0)
return SIMPLE_ERR_BAD_RESP;
else if (pagenum != (pBuf[offset] & 0x3f))
return SIMPLE_ERR_BAD_RESP;
}
return status;
}
/* Sends a 6 byte MODE SELECT command. Assumes given pBuf is the response
* from a corresponding 6 byte MODE SENSE command. Such a response should
* have a 4 byte header followed by 0 or more 8 byte block descriptors
* (normally 1) and then 1 mode page. Returns 0 if ok, 1 if NOT READY,
* 2 if command not supported (then MODE SELECT(10) may be supported),
* 3 if field in command not supported, 4 if bad parameter to command
* or returns negated errno. SPC-3 sections 6.7 and 7.4 (rev 22a) */
int
scsiModeSelect(scsi_device * device, int sp, UINT8 *pBuf, int bufLen)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[6];
UINT8 sense[32];
int pg_offset, pg_len, hdr_plus_1_pg;
pg_offset = 4 + pBuf[3];
if (pg_offset + 2 >= bufLen)
return -EINVAL;
pg_len = pBuf[pg_offset + 1] + 2;
hdr_plus_1_pg = pg_offset + pg_len;
if (hdr_plus_1_pg > bufLen)
return -EINVAL;
pBuf[0] = 0; /* Length of returned mode sense data reserved for SELECT */
pBuf[pg_offset] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_TO_DEVICE;
io_hdr.dxfer_len = hdr_plus_1_pg;
io_hdr.dxferp = pBuf;
cdb[0] = MODE_SELECT;
cdb[1] = 0x10 | (sp & 1); /* set PF (page format) bit always */
cdb[4] = hdr_plus_1_pg; /* make sure only one page sent */
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
return scsiSimpleSenseFilter(&sinfo);
}
/* MODE SENSE (10 byte). Returns 0 if ok, 1 if NOT READY, 2 if command
* not supported (then MODE SENSE(6) might be supported), 3 if field in
* command not supported or returns negated errno.
* SPC-3 sections 6.10 and 7.4 (rev 22a) [mode subpage==0] */
int
scsiModeSense10(scsi_device * device, int pagenum, int subpagenum, int pc,
UINT8 *pBuf, int bufLen)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[10];
UINT8 sense[32];
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_FROM_DEVICE;
io_hdr.dxfer_len = bufLen;
io_hdr.dxferp = pBuf;
cdb[0] = MODE_SENSE_10;
cdb[2] = (pc << 6) | (pagenum & 0x3f);
cdb[3] = subpagenum;
cdb[7] = (bufLen >> 8) & 0xff;
cdb[8] = bufLen & 0xff;
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
int status = scsiSimpleSenseFilter(&sinfo);
if (SIMPLE_ERR_TRY_AGAIN == status) {
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
status = scsiSimpleSenseFilter(&sinfo);
}
if ((0 == status) && (ALL_MODE_PAGES != pagenum)) {
int offset;
offset = scsiModePageOffset(pBuf, bufLen, 1);
if (offset < 0)
return SIMPLE_ERR_BAD_RESP;
else if (pagenum != (pBuf[offset] & 0x3f))
return SIMPLE_ERR_BAD_RESP;
}
return status;
}
/* Sends a 10 byte MODE SELECT command. Assumes given pBuf is the response
* from a corresponding 10 byte MODE SENSE command. Such a response should
* have a 8 byte header followed by 0 or more 8 byte block descriptors
* (normally 1) and then 1 mode page. Returns 0 if ok, 1 NOT REAFY, 2 if
* command not supported (then MODE SELECT(6) may be supported), 3 if field
* in command not supported, 4 if bad parameter to command or returns
* negated errno. SPC-3 sections 6.8 and 7.4 (rev 22a) */
int
scsiModeSelect10(scsi_device * device, int sp, UINT8 *pBuf, int bufLen)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[10];
UINT8 sense[32];
int pg_offset, pg_len, hdr_plus_1_pg;
pg_offset = 8 + (pBuf[6] << 8) + pBuf[7];
if (pg_offset + 2 >= bufLen)
return -EINVAL;
pg_len = pBuf[pg_offset + 1] + 2;
hdr_plus_1_pg = pg_offset + pg_len;
if (hdr_plus_1_pg > bufLen)
return -EINVAL;
pBuf[0] = 0;
pBuf[1] = 0; /* Length of returned mode sense data reserved for SELECT */
pBuf[pg_offset] &= 0x7f; /* Mask out PS bit from byte 0 of page data */
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_TO_DEVICE;
io_hdr.dxfer_len = hdr_plus_1_pg;
io_hdr.dxferp = pBuf;
cdb[0] = MODE_SELECT_10;
cdb[1] = 0x10 | (sp & 1); /* set PF (page format) bit always */
cdb[8] = hdr_plus_1_pg; /* make sure only one page sent */
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
return scsiSimpleSenseFilter(&sinfo);
}
/* Standard INQUIRY returns 0 for ok, anything else is a major problem.
* bufLen should be 36 for unsafe devices (like USB mass storage stuff)
* otherwise they can lock up! SPC-3 sections 6.4 and 7.6 (rev 22a) */
int
scsiStdInquiry(scsi_device * device, UINT8 *pBuf, int bufLen)
{
struct scsi_sense_disect sinfo;
struct scsi_cmnd_io io_hdr;
UINT8 cdb[6];
UINT8 sense[32];
if ((bufLen < 0) || (bufLen > 1023))
return -EINVAL;
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_FROM_DEVICE;
io_hdr.dxfer_len = bufLen;
io_hdr.dxferp = pBuf;
cdb[0] = INQUIRY;
cdb[3] = (bufLen >> 8) & 0xff;
cdb[4] = (bufLen & 0xff);
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
return scsiSimpleSenseFilter(&sinfo);
}
/* INQUIRY to fetch Vital Page Data. Returns 0 if ok, 1 if NOT READY
* (unlikely), 2 if command not supported, 3 if field in command not
* supported, 5 if response indicates that EVPD bit ignored or returns
* negated errno. SPC-3 section 6.4 and 7.6 (rev 22a) */
int
scsiInquiryVpd(scsi_device * device, int vpd_page, UINT8 *pBuf, int bufLen)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[6];
UINT8 sense[32];
int res;
/* Assume SCSI_VPD_SUPPORTED_VPD_PAGES is first VPD page fetched */
if ((SCSI_VPD_SUPPORTED_VPD_PAGES != vpd_page) &&
supported_vpd_pages_p &&
(! supported_vpd_pages_p->is_supported(vpd_page)))
return 3;
if ((bufLen < 0) || (bufLen > 1023))
return -EINVAL;
try_again:
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
if (bufLen > 1)
pBuf[1] = 0x0;
io_hdr.dxfer_dir = DXFER_FROM_DEVICE;
io_hdr.dxfer_len = bufLen;
io_hdr.dxferp = pBuf;
cdb[0] = INQUIRY;
cdb[1] = 0x1; /* set EVPD bit (enable Vital Product Data) */
cdb[2] = vpd_page;
cdb[3] = (bufLen >> 8) & 0xff;
cdb[4] = (bufLen & 0xff);
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
if ((SCSI_STATUS_CHECK_CONDITION == io_hdr.scsi_status) &&
(SCSI_SK_ILLEGAL_REQUEST == sinfo.sense_key) &&
(SCSI_ASC_INVALID_FIELD == sinfo.asc) &&
(cdb[3] > 0)) {
bufLen &= 0xff; /* make sure cdb[3] is 0 next time around */
goto try_again;
}
if ((res = scsiSimpleSenseFilter(&sinfo)))
return res;
/* Guard against devices that ignore EVPD bit and do standard INQUIRY */
if (bufLen > 1) {
if (vpd_page == pBuf[1]) {
if ((0x80 == vpd_page) && (bufLen > 2) && (0x0 != pBuf[2]))
return SIMPLE_ERR_BAD_RESP;
} else
return SIMPLE_ERR_BAD_RESP;
}
return 0;
}
/* REQUEST SENSE command. Returns 0 if ok, anything else major problem.
* SPC-3 section 6.27 (rev 22a) */
int
scsiRequestSense(scsi_device * device, struct scsi_sense_disect * sense_info)
{
struct scsi_cmnd_io io_hdr;
UINT8 cdb[6];
UINT8 sense[32];
UINT8 buff[18];
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_FROM_DEVICE;
io_hdr.dxfer_len = sizeof(buff);
io_hdr.dxferp = buff;
cdb[0] = REQUEST_SENSE;
cdb[4] = sizeof(buff);
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
io_hdr.timeout = SCSI_TIMEOUT_DEFAULT;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
if (sense_info) {
UINT8 resp_code = buff[0] & 0x7f;
sense_info->resp_code = resp_code;
sense_info->sense_key = buff[2] & 0xf;
sense_info->asc = 0;
sense_info->ascq = 0;
if ((0x70 == resp_code) || (0x71 == resp_code)) {
int len = buff[7] + 8;
if (len > 13) {
sense_info->asc = buff[12];
sense_info->ascq = buff[13];
}
}
// fill progrss indicator, if available
sense_info->progress = -1;
switch (resp_code) {
const unsigned char * ucp;
int sk, sk_pr;
case 0x70:
case 0x71:
sk = (buff[2] & 0xf);
if ((sizeof(buff) < 18) ||
((SCSI_SK_NO_SENSE != sk) && (SCSI_SK_NOT_READY != sk))) {
break;
}
if (buff[15] & 0x80) { /* SKSV bit set */
sense_info->progress = (buff[16] << 8) + buff[17];
break;
} else {
break;
}
case 0x72:
case 0x73:
/* sense key specific progress (0x2) or progress descriptor (0xa) */
sk = (buff[1] & 0xf);
sk_pr = (SCSI_SK_NO_SENSE == sk) || (SCSI_SK_NOT_READY == sk);
if (sk_pr && ((ucp = sg_scsi_sense_desc_find(buff, sizeof(buff), 2))) &&
(0x6 == ucp[1]) && (0x80 & ucp[4])) {
sense_info->progress = (ucp[5] << 8) + ucp[6];
break;
} else if (((ucp = sg_scsi_sense_desc_find(buff, sizeof(buff), 0xa))) &&
((0x6 == ucp[1]))) {
sense_info->progress = (ucp[6] << 8) + ucp[7];
break;
} else
break;
default:
return 0;
}
}
return 0;
}
/* SEND DIAGNOSTIC command. Returns 0 if ok, 1 if NOT READY, 2 if command
* not supported, 3 if field in command not supported or returns negated
* errno. SPC-3 section 6.28 (rev 22a) */
int
scsiSendDiagnostic(scsi_device * device, int functioncode, UINT8 *pBuf,
int bufLen)
{
struct scsi_cmnd_io io_hdr;
struct scsi_sense_disect sinfo;
UINT8 cdb[6];
UINT8 sense[32];
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = bufLen ? DXFER_TO_DEVICE: DXFER_NONE;
io_hdr.dxfer_len = bufLen;
io_hdr.dxferp = pBuf;
cdb[0] = SEND_DIAGNOSTIC;
if (SCSI_DIAG_DEF_SELF_TEST == functioncode)
cdb[1] = 0x4; /* SelfTest bit */
else if (SCSI_DIAG_NO_SELF_TEST != functioncode)
cdb[1] = (functioncode & 0x7) << 5; /* SelfTest _code_ */
else /* SCSI_DIAG_NO_SELF_TEST == functioncode */
cdb[1] = 0x10; /* PF bit */
cdb[3] = (bufLen >> 8) & 0xff;
cdb[4] = bufLen & 0xff;
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;
io_hdr.max_sense_len = sizeof(sense);
/* worst case is an extended foreground self test on a big disk */
io_hdr.timeout = SCSI_TIMEOUT_SELF_TEST;
if (!device->scsi_pass_through(&io_hdr))
return -device->get_errno();
scsi_do_sense_disect(&io_hdr, &sinfo);
return scsiSimpleSenseFilter(&sinfo);
}
/* TEST UNIT READY command. SPC-3 section 6.33 (rev 22a) */
static int
_testunitready(scsi_device * device, struct scsi_sense_disect * sinfo)
{
struct scsi_cmnd_io io_hdr;
UINT8 cdb[6];
UINT8 sense[32];
memset(&io_hdr, 0, sizeof(io_hdr));
memset(cdb, 0, sizeof(cdb));
io_hdr.dxfer_dir = DXFER_NONE;
io_hdr.dxfer_len = 0;
io_hdr.dxferp = NULL;
cdb[0] = TEST_UNIT_READY;
io_hdr.cmnd = cdb;
io_hdr.cmnd_len = sizeof(cdb);
io_hdr.sensep = sense;