-
Notifications
You must be signed in to change notification settings - Fork 15
/
libscsi.c
1736 lines (1576 loc) · 50.4 KB
/
libscsi.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) 2006 - 2021 *
* This Software Provided *
* By *
* Robin's Nest Software Inc. *
* *
* Permission to use, copy, modify, distribute and sell this software and *
* its documentation for any purpose and without fee is hereby granted, *
* provided that the above copyright notice appear in all copies and that *
* both that copyright notice and this permission notice appear in the *
* supporting documentation, and that the name of the author not be used *
* in advertising or publicity pertaining to distribution of the software *
* without specific, written prior permission. *
* *
* THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, *
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN *
* NO EVENT SHALL HE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL *
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR *
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS *
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF *
* THIS SOFTWARE. *
* *
****************************************************************************/
/*
* Module: libscsi.c
* Author: Robin T. Miller
* Date: March 24th, 2005
*
* Description:
* This module contains common SCSI functions, which are meant to be
* a front-end to calling the underlying OS dependent SCSI functions,
* when appropriate, to send a SCSI CDB (Command Descriptor Block).
*
* Modification History:
*
* November 4th, 2021 by Robin T. Miller
* Merge DecodeDeviceIdentifier() from spt for hythen control.
*
* July 18th, 2019 by Robin T. Miller
* Do not retry "target port in standby state" sense code/qualifier,
* otherwise we loop forever, since we do not have retry limit on this error.
*
* January 23rd, 2013 by Robin T. Miller
* Update libExecuteCdb() to allow a user defined execute CDB function.
* Added command retries, including OS specific error recovery.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h>
#include "dt.h"
//#include "libscsi.h"
#include "scsi_cdbs.h"
#include "scsi_opcodes.h"
/*
* Local Declarations:
*/
/*
* Forward Declarations:
*/
hbool_t isSenseRetryable(scsi_generic_t *sgp, int scsi_status, scsi_sense_t *ssp);
void ReportCdbScsiInformation(scsi_generic_t *sgp);
int verify_inquiry_header(inquiry_t *inquiry, inquiry_header_t *inqh, unsigned char page);
/* ======================================================================== */
scsi_generic_t *
init_scsi_generic(void)
{
scsi_generic_t *sgp;
sgp = Malloc(NULL, sizeof(*sgp));
if (sgp == NULL) return (NULL);
init_scsi_defaults(sgp);
return (sgp);
}
void
init_scsi_defaults(scsi_generic_t *sgp)
{
scsi_addr_t *sap;
/*
* Initial Defaults:
*/
sgp->fd = INVALID_HANDLE_VALUE;
sgp->sense_length = RequestSenseDataLength;
sgp->sense_data = malloc_palign(NULL, sgp->sense_length, 0);
sgp->debug = ScsiDebugFlagDefault;
sgp->errlog = ScsiErrorFlagDefault;
sgp->timeout = ScsiDefaultTimeout;
sgp->qtag_type = SG_SIMPLE_Q;
/*
* Recovery Parameters:
*/
sgp->recovery_flag = ScsiRecoveryFlagDefault;
sgp->recovery_delay = ScsiRecoveryDelayDefault;
sgp->recovery_limit = ScsiRecoveryRetriesDefault;
sap = &sgp->scsi_addr;
/* Note: Only AIX uses this, but must be -1 for any path! */
sap->scsi_path = -1; /* Indicates no path specified. */
return;
}
hbool_t
libIsRetriable(scsi_generic_t *sgp)
{
hbool_t retriable = False;
/* Note: This section is dt specified, but required to avoid looping! */
if ( PROGRAM_TERMINATING || COMMAND_INTERRUPTED ) {
return(retriable);
}
if ( sgp->opaque ) {
dinfo_t *dip = sgp->opaque;
if ( (dip->di_trigger_active == False) && THREAD_TERMINATING(dip) ) {
return(retriable);
}
}
/* end of dt specific! */
if (sgp->recovery_retries++ < sgp->recovery_limit) {
/*
* Try OS specific first, then check for common retriables.
*/
retriable = os_is_retriable(sgp);
if (retriable == False) {
scsi_sense_t *ssp = sgp->sense_data;
unsigned char sense_key, asc, asq;
/* Get sense key and sense code/qualifiers. */
GetSenseErrors(ssp, &sense_key, &asc, &asq);
if (sgp->debug == True) {
print_scsi_status(sgp, sgp->scsi_status, sense_key, asc, asq);
}
if ( (sgp->scsi_status == SCSI_BUSY) ||
(sgp->scsi_status == SCSI_QUEUE_FULL) ) {
retriable = True;
} else if (sgp->scsi_status == SCSI_CHECK_CONDITION) {
if (sense_key == SKV_UNIT_ATTENTION) {
if (asc != ASC_RECOVERED_DATA) {
retriable = True;
}
} else if ( (sense_key == SKV_NOT_READY) &&
(asc == ASC_NOT_READY) ) {
/* Lots of reasons, but we retry them all! */
/* Includes:
* "Logical unit is in process of becoming ready"
* "Logical unit not ready, space allocation in progress"
* Note: We'll be more selective, if this becomes an issue!
*/
/* Note: We do not retry this error, or we'll loop forever! */
/* (0x4, 0xb) - Logical unit not accessible, target port in standby state */
if (asq != ASQ_STANDBY_STATE) {
retriable = True;
}
}
}
}
}
return (retriable);
}
/* ======================================================================== */
/*
* libExecuteCdb() = Execute a SCSI Command Descriptor Block (CDB).
*
* Inputs:
* sgp = Pointer to SCSI generic pointer.
*
* Return Value:
* Returns 0/-1 for Success/Failure.
*/
int
libExecuteCdb(scsi_generic_t *sgp)
{
int error;
hbool_t retriable;
/*
* Allow user to define their own execute CDB function.
*/
if (sgp->execute_cdb && sgp->opaque) {
error = (*sgp->execute_cdb)(sgp->opaque, sgp);
return (error);
}
sgp->recovery_retries = 0;
do {
retriable = False;
/*
* Ensure the sense data is cleared for emiting status.
*/
memset(sgp->sense_data, '\0', sgp->sense_length);
/* Clear these too, since IOCTL may fail so never get updated! */
sgp->os_error = 0; /* The system call error (if any). */
sgp->scsi_status = sgp->driver_status = sgp->host_status = sgp->data_resid = 0;
/*
* Call OS dependent SCSI Pass-Through (spt) function.
*/
error = os_spt(sgp);
//if ( (sgp->error == True) && (sgp->sense_valid == True) ) {
// MapSenseDescriptorToFixed(sgp->sense_data);
//}
if (((error == FAILURE) || (sgp->error == True)) && sgp->recovery_flag) {
if (sgp->recovery_retries == sgp->recovery_limit) {
Fprintf(sgp->opaque, "Exceeded retry limit (%u) for this request!\n", sgp->recovery_limit);
} else {
retriable = libIsRetriable(sgp);
if (retriable == True) {
(void)os_sleep(sgp->recovery_delay);
if (sgp->errlog == True) {
/* Folks wish to see the actual error too! */
if (error == FAILURE) { /* The system call failed! */
libReportIoctlError(sgp, True);
} else {
libReportScsiError(sgp, True);
}
Fprintf(sgp->opaque, "Warning: Retrying %s after %u second delay, retry #%u...\n",
sgp->cdb_name, sgp->recovery_delay, sgp->recovery_retries);
}
}
}
}
} while (retriable == True);
if (error == FAILURE) { /* The system call failed! */
if (sgp->errlog) {
libReportIoctlError(sgp, sgp->warn_on_error);
}
} else if ( sgp->error && (sgp->errlog || sgp->debug) ) { /* SCSI error. */
libReportScsiError(sgp, sgp->warn_on_error);
}
if (sgp->error == True) {
error = FAILURE; /* Tell caller we've had an error! */
}
return (error);
}
/* Note: This can be replaced after spt is integrated! */
void
ReportCdbScsiInformation(scsi_generic_t *sgp)
{
char efmt_buffer[LARGE_BUFFER_SIZE];
char *to = efmt_buffer;
int i, slen;
to += sprintf(to, "SCSI CDB: ");
for (i = 0; (i < sgp->cdb_size); i++) {
slen = Sprintf(to, "%02x ", sgp->cdb[i]);
to += slen;
}
if (i) {
slen--; to--; *to = '\0';
}
to += sprintf(to, ", dir=");
if (sgp->data_dir == scsi_data_none) {
slen = sprintf(to, "%s", "none");
} else if (sgp->data_dir == scsi_data_read) {
slen = sprintf(to, "%s", "read");
} else if (sgp->data_dir == scsi_data_write) {
slen = sprintf(to, "%s", "write");
} else {
slen = 0;
}
to += slen;
to += sprintf(to, ", length=");
to += sprintf(to, "%u", sgp->data_length);
Fprintf(sgp->opaque, "%s\n", efmt_buffer);
return;
}
void
libReportIoctlError(scsi_generic_t *sgp, hbool_t warn_on_error)
{
if (sgp->errlog == True) {
time_t error_time = time((time_t *) 0);
Fprintf(sgp->opaque, "%s: Error occurred on %s",
(warn_on_error == True) ? "Warning" : "ERROR",
ctime(&error_time));
Fprintf(sgp->opaque, "%s failed on device %s\n", sgp->cdb_name, sgp->dsf);
ReportCdbScsiInformation(sgp);
}
return;
}
void
libReportScsiError(scsi_generic_t *sgp, hbool_t warn_on_error)
{
time_t error_time = time((time_t *) 0);
char *host_msg = os_host_status_msg(sgp);
char *driver_msg = os_driver_status_msg(sgp);
scsi_sense_t *ssp = sgp->sense_data;
unsigned char sense_key, asc, asq;
char *ascq_msg;
/* Get sense key and sense code/qualifiers. */
GetSenseErrors(ssp, &sense_key, &asc, &asq);
ascq_msg = ScsiAscqMsg(asc, asq);
Fprintf(sgp->opaque, "%s: Error occurred on %s",
(warn_on_error == True) ? "Warning" : "ERROR",
ctime(&error_time));
Fprintf(sgp->opaque, "%s failed on device %s\n", sgp->cdb_name, sgp->dsf);
ReportCdbScsiInformation(sgp);
Fprintf(sgp->opaque, "SCSI Status = %#x (%s)\n", sgp->scsi_status, ScsiStatus(sgp->scsi_status));
if (host_msg && driver_msg) {
Fprintf(sgp->opaque, "Host Status = %#x (%s), Driver Status = %#x (%s)\n",
sgp->host_status, host_msg, sgp->driver_status, driver_msg);
} else if (host_msg || driver_msg) {
if (host_msg) {
Fprintf(sgp->opaque, "Host Status = %#x (%s)\n", sgp->host_status, host_msg);
}
if (driver_msg) {
Fprintf(sgp->opaque, "Driver Status = %#x (%s)\n", sgp->driver_status, driver_msg);
}
} else if (sgp->host_status || sgp->driver_status) {
Fprintf(sgp->opaque, "Host Status = %#x, Driver Status = %#x\n",
sgp->host_status, sgp->driver_status);
}
Fprintf(sgp->opaque, "Sense Key = %d = %s, Sense Code/Qualifier = (%#x, %#x)",
sense_key, SenseKeyMsg(sense_key),
asc, asq);
if (ascq_msg) {
Fprint(sgp->opaque, " - %s", ascq_msg);
}
Fprintnl(sgp->opaque);
if ( ssp->error_code && (sgp->debug || sgp->sense_flag) ) {
DumpSenseData(sgp, ssp);
}
return;
}
void
libReportScsiSense(scsi_generic_t *sgp, int scsi_status, scsi_sense_t *ssp)
{
char *ascq_msg = ScsiAscqMsg(ssp->asc, ssp->asq);
Fprintf(sgp->opaque, "SCSI Status = %#x (%s)\n", scsi_status, ScsiStatus(scsi_status));
Fprintf(sgp->opaque, "Sense Key = %d = %s, Sense Code/Qualifier = (%#x, %#x)",
ssp->sense_key, SenseKeyMsg(ssp->sense_key),
ssp->asc, ssp->asq);
if (ascq_msg) {
Fprint(sgp->opaque, " - %s", ascq_msg);
}
Fprintnl(sgp->opaque);
return;
}
/* ======================================================================== */
/*
* NOTE: These API's were written a long time ago, with the ability to invoke
* them without defining SCSI structures, for ease of use. Since then, I have
* found it easier (and more efficient) to expose structs to external callers.
* Removing the optional sg allocation and initialization makes this cleaner!
*/
#include "inquiry.h"
/* ======================================================================== */
/*
* Declarations/Definitions for Inquiry Command:
*/
#define InquiryName "Inquiry"
#define InquiryOpcode 0x12
#define InquiryCdbSize 6
#define InquiryTimeout ScsiDefaultTimeout
/*
* Inquiry() - Send a SCSI Inquiry Command.
*
* Inputs:
* fd = The file descriptor to issue bus reset to.
* dsf = The device special file (raw or "sg" for Linux).
* debug = Flag to control debug output.
* errlog = Flag to control error logging. (True logs error)
* (False suppesses)
* sap = Pointer to SCSI address (optional).
* sgpp = Pointer to scsi generic pointer (optional).
* data = Buffer for received Inquiry data.
* len = The length of the data buffer.
* page = The Inquiry VPD page (if any).
* flags = OS specific SCSI flags (if any).
* timeout = The timeout value (in ms).
*
* Return Value:
* Returns the status from the IOCTL request which is:
* 0 = Success, -1 = Failure
*
* Note: If the caller supplies a SCSI generic pointer, then
* it's the callers responsibility to free this structure, along
* with the data buffer and sense buffer. This capability is
* provided so the caller can examine SCSI status, sense data,
* and data transfers, to make (more) intelligent decisions.
*/
int
Inquiry(HANDLE fd, char *dsf, hbool_t debug, hbool_t errlog,
scsi_addr_t *sap, scsi_generic_t **sgpp,
void *data, unsigned int len, unsigned char page,
unsigned int sflags, unsigned int timeout)
{
scsi_generic_t *sgp;
struct Inquiry_CDB *cdb;
int error;
/*
* Setup and/or allocate a SCSI generic data structure.
*/
if (sgpp && *sgpp) {
sgp = *sgpp;
} else {
sgp = init_scsi_generic();
}
if (sgp->fd == INVALID_HANDLE_VALUE) {
sgp->fd = fd;
sgp->dsf = dsf;
}
memset(sgp->cdb, 0, sizeof(sgp->cdb));
if (data && len) memset(data, 0, len);
cdb = (struct Inquiry_CDB *)sgp->cdb;
cdb->opcode = InquiryOpcode;
if (page) {
cdb->pgcode = page;
cdb->evpd = 1;
}
cdb->alclen = len;
sgp->cdb_size = InquiryCdbSize;
sgp->cdb_name = InquiryName;
sgp->data_dir = scsi_data_read;
sgp->data_buffer= data;
sgp->data_length= len;
sgp->errlog = errlog;
sgp->iface = NULL;
sgp->sflags = sflags;
sgp->timeout = (timeout) ? timeout : InquiryTimeout;
sgp->debug = debug;
/*
* If a SCSI address was specified, do a structure copy.
*/
if (sap) {
sgp->scsi_addr = *sap; /* Copy the SCSI address info. */
}
error = libExecuteCdb(sgp);
/*
* If the user supplied a pointer, send the SCSI generic data
* back to them for further analysis.
*/
if (sgpp) {
if (*sgpp == NULL) {
*sgpp = sgp; /* Return the generic data pointer. */
}
} else {
free_palign(sgp->opaque, sgp->sense_data);
free(sgp);
}
return(error);
}
int
verify_inquiry_header(inquiry_t *inquiry, inquiry_header_t *inqh, unsigned char page)
{
if ( (StoH(inqh->inq_page_length) == 0) ||
(inqh->inq_page_code != page) ||
(inqh->inq_dtype != inquiry->inq_dtype) ) {
return(FAILURE);
}
return(SUCCESS);
}
/* ======================================================================== */
/*
* GetDeviceIdentifier() - Gets Inquiry Device ID Page.
*
* Description:
* This function decodes each of the device ID descriptors and applies
* and precedence algorthm to find the *best* device identifier (see table).
*
* Note: This API is a wrapper around Inquiry, originally designed to simply
* return an identifier string. Therefore, a buffer and length are omitted.
*
* Inputs:
* fd = The file descriptor.
* dsf = The device special file (raw or "sg" for Linux).
* debug = Flag to control debug output.
* errlog = Flag to control error logging. (True logs error)
* (False suppesses)
* sap = Pointer to SCSI address (optional).
* sgpp = Pointer to SCSI generic pointer (optional).
* inqp = Pointer to device Inquiry data.
* timeout = The timeout value (in ms).
*
* Return Value:
* Returns NULL of no device ID page or it's invalid.
* Otherwise, returns a pointer to a malloc'd buffer w/ID.
*/
char *
GetDeviceIdentifier(HANDLE fd, char *dsf, hbool_t debug, hbool_t errlog,
scsi_addr_t *sap, scsi_generic_t **sgpp,
void *inqp, unsigned int timeout)
{
void *opaque = NULL;
inquiry_t *inquiry = inqp;
inquiry_page_t inquiry_data;
inquiry_page_t *inquiry_page = &inquiry_data;
inquiry_header_t *inqh = &inquiry_page->inquiry_hdr;
unsigned char page = INQ_DEVICE_PAGE;
int status;
status = Inquiry(fd, dsf, debug, errlog, NULL, NULL, inquiry_page,
sizeof(*inquiry_page), page, 0, timeout);
if (status != SUCCESS) return(NULL);
if (verify_inquiry_header(inquiry, inqh, page) == FAILURE) return(NULL);
return( DecodeDeviceIdentifier(opaque, inquiry, inquiry_page, False) );
}
/*
* DecodeDeviceIdentifier() - Decode the Inquiry Device Identifier.
*
* Note: We allow separate decode, since we may be extracting different parts
* of the device ID page, such as LUN device ID and target port address, and we
* wish to avoid multiple Inquiry page requests (we may have lots of devices).
*
* Return Value:
* The LUN device identifier string or NULL if none found.
* The buffer is dynamically allocated, so caller must free it.
*/
char *
DecodeDeviceIdentifier(void *opaque, inquiry_t *inquiry,
inquiry_page_t *inquiry_page, hbool_t hyphens)
{
inquiry_ident_descriptor_t *iid;
size_t page_length;
char *bp = NULL;
/* Identifiers in order of precedence: (the "Smart" way :-) */
/* REMEMBER: The lower values have *higher* precedence!!! */
enum pidt {
REGEXT, REG, EXT_V, EXT_0, EUI64, TY1_VID, BINARY, ASCII, NONE
};
enum pidt pid_type = NONE; /* Precedence ID type. */
page_length = (size_t)StoH(inquiry_page->inquiry_hdr.inq_page_length);
iid = (inquiry_ident_descriptor_t *)inquiry_page->inquiry_page_data;
/*
* Notes:
* - We loop through ALL descriptors, enforcing the precedence
* order defined above (see enum pidt). This is because some
* devices return more than one identifier.
*/
while ( (ssize_t)page_length > 0 ) {
unsigned char *fptr = (unsigned char *)iid + sizeof(*iid);
switch (iid->iid_code_set) {
case IID_CODE_SET_ASCII: {
/* Only accept Vendor ID's of Type 1. */
if ( (pid_type > TY1_VID) &&
(iid->iid_ident_type == IID_ID_TYPE_T10_VID) ) {
int id_len = iid->iid_ident_length + sizeof(inquiry->inq_pid);
if (bp) {
free(bp) ; bp = NULL;
};
bp = (char *)malloc(id_len + 1);
if (bp == NULL) return(NULL);
pid_type = TY1_VID;
memcpy(bp, inquiry->inq_pid, sizeof(inquiry->inq_pid));
memcpy((bp + sizeof(inquiry->inq_pid)), fptr, iid->iid_ident_length);
}
/* Continue looping looking for IEEE identifier. */
break;
} /* end case IID_CODE_SET_ASCII */
case IID_CODE_SET_BINARY: {
/*
* This is the preferred (unique) identifier.
*/
switch (iid->iid_ident_type) {
case IID_ID_TYPE_NAA: {
enum pidt npid_type;
int i = 0;
/*
* NAA is the high order 4 bits of the 1st byte.
*/
switch ( (*fptr >> 4) & 0xF) {
case NAA_IEEE_REG_EXTENDED:
npid_type = REGEXT;
break;
case NAA_IEEE_REGISTERED:
npid_type = REG;
break;
case NAA_IEEE_EXTENDED:
npid_type = EXT_V;
break;
case 0x1: /* ???? */
npid_type = EXT_0;
break;
default:
/* unrecognized */
npid_type = BINARY;
break;
}
/*
* If the previous precedence ID is of lower priority,
* that is a higher value, then make this pid the new.
*/
if ( (pid_type > npid_type) ) {
int blen = (iid->iid_ident_length * 3);
char *bptr;
pid_type = npid_type; /* Set the new precedence type */
if (bp) {
free(bp) ; bp = NULL;
};
bptr = bp = Malloc(opaque, blen);
if (bp == NULL) return(NULL);
if (hyphens == False) {
bptr += sprintf(bptr, "0x");
}
/* Format as: xxxx-xxxx... */
while (i < (int)iid->iid_ident_length) {
bptr += sprintf(bptr, "%02x", fptr[i++]);
if (hyphens == True) {
if (( (i % 2) == 0) &&
(i < (int)iid->iid_ident_length) ) {
bptr += sprintf(bptr, "-");
}
}
}
}
break;
}
case IID_ID_TYPE_EUI64: {
int blen, i = 0;
char *bptr;
if ( (pid_type <= EUI64) ) {
break;
}
pid_type = EUI64;
blen = (iid->iid_ident_length * 3);
if (bp) {
free(bp) ; bp = NULL;
};
bptr = bp = (char *)malloc(blen);
if (bp == NULL) return(NULL);
if (hyphens == False) {
bptr += sprintf(bptr, "0x");
}
/* Format as: xxxx-xxxx... */
while (i < (int)iid->iid_ident_length) {
bptr += sprintf(bptr, "%02x", fptr[i++]);
if (hyphens == True) {
if (( (i % 2) == 0) &&
(i < (int)iid->iid_ident_length) ) {
bptr += sprintf(bptr, "-");
}
}
}
break;
}
case IID_ID_TYPE_VS:
case IID_ID_TYPE_T10_VID:
case IID_ID_TYPE_RELTGTPORT:
case IID_ID_TYPE_TGTPORTGRP:
case IID_ID_TYPE_LOGUNITGRP:
case IID_ID_TYPE_MD5LOGUNIT:
case IID_ID_TYPE_SCSI_NAME:
case IID_ID_TYPE_PROTOPORT:
break;
default: {
/* Note: We need updated with new descriptors added! */
//Fprintf(opaque, "Unknown identifier type %#x\n", iid->iid_ident_type);
break;
}
} /* switch (iid->iid_ident_type) */
break;
} /* end case IID_CODE_SET_BINARY */
case IID_CODE_SET_ISO_IEC:
break;
default: {
//Fprintf(opaque, "Unknown identifier code set %#x\n", iid->iid_code_set);
break;
} /* end case of code set. */
} /* switch (iid->iid_code_set) */
page_length -= iid->iid_ident_length + sizeof(*iid);
iid = (inquiry_ident_descriptor_t *)((ptr_t) iid + iid->iid_ident_length + sizeof(*iid));
} /* while ( (ssize_t)page_length > 0 ) */
/* NOTE: Caller MUST free allocated buffer! */
return(bp);
}
/*
* GetSerialNumber() - Gets Inquiry Serial Number Page.
*
* Inputs:
* fd = The file descriptor.
* dsf = The device special file (raw or "sg" for Linux).
* errlog = Flag to control error logging. (True logs error)
* (False suppesses)
* sap = Pointer to SCSI address (optional).
* sgpp = Pointer to SCSI generic pointer (optional).
* inqp = Pointer to device Inquiry data.
* timeout = The timeout value (in ms).
*
* Return Value:
* Returns NULL if no serial number page or it's invalid.
* Otherwise, returns a pointer to a malloc'd buffer w/serial #.
*/
char *
GetSerialNumber(HANDLE fd, char *dsf, hbool_t debug, hbool_t errlog,
scsi_addr_t *sap, scsi_generic_t **sgpp,
void *inqp, unsigned int timeout)
{
inquiry_t *inquiry = inqp;
inquiry_page_t inquiry_data;
inquiry_page_t *inquiry_page = &inquiry_data;
inquiry_header_t *inqh = &inquiry_page->inquiry_hdr;
unsigned char page = INQ_SERIAL_PAGE;
size_t page_length;
char *bp;
int status;
status = Inquiry(fd, dsf, debug, errlog, NULL, sgpp,
inquiry_page, sizeof(*inquiry_page), page, 0, timeout);
if (status != SUCCESS) return(NULL);
if (verify_inquiry_header(inquiry, inqh, page) == FAILURE) return(NULL);
page_length = (size_t)StoH(inquiry_page->inquiry_hdr.inq_page_length);
bp = (char *)Malloc(NULL, (page_length + 1) );
if (bp == NULL) return(NULL);
strncpy (bp, (char *)inquiry_page->inquiry_page_data, page_length);
bp[page_length] = '\0';
/* NOTE: Caller MUST free allocated buffer! */
return(bp);
}
/*
* MgmtNetworkAddress() - Gets Inquiry Managment Network Address.
*
* Inputs:
* fd = The file descriptor.
* dsf = The device special file (raw or "sg" for Linux).
* errlog = Flag to control error logging. (True logs error)
* (False suppesses)
* sap = Pointer to SCSI address (optional).
* sgpp = Pointer to SCSI generic pointer (optional).
* inqp = Pointer to device Inquiry data.
* timeout = The timeout value (in ms).
*
* Return Value:
* Returns NULL if no serial number page or it's invalid.
* Otherwise, returns a pointer to a malloc'd buffer w/mgmt address.
*/
char *
GetMgmtNetworkAddress(HANDLE fd, char *dsf, hbool_t debug, hbool_t errlog,
scsi_addr_t *sap, scsi_generic_t **sgpp,
void *inqp, unsigned int timeout)
{
inquiry_t *inquiry = inqp;
inquiry_page_t inquiry_data;
inquiry_page_t *inquiry_page = &inquiry_data;
inquiry_header_t *inqh = &inquiry_page->inquiry_hdr;
inquiry_network_service_page_t *inap;
unsigned char page = INQ_MGMT_NET_ADDR_PAGE;
size_t address_length;
char *bp;
int status;
status = Inquiry(fd, dsf, debug, errlog, NULL, sgpp,
inquiry_page, sizeof(*inquiry_page), page, 0, timeout);
if (status != SUCCESS) return(NULL);
if (verify_inquiry_header(inquiry, inqh, page) == FAILURE) return(NULL);
inap = (inquiry_network_service_page_t *)&inquiry_page->inquiry_page_data;
address_length = (size_t)StoH(inap->address_length);
if (address_length == 0) return(NULL);
bp = (char *)Malloc(NULL, (address_length + 1) );
if (bp == NULL) return(NULL);
strncpy(bp, (char *)inap->address, address_length);
bp[address_length] = '\0';
/* NOTE: Caller MUST free allocated buffer! */
return(bp);
}
/*
* GetUniqueID - Get The Devices' Unique ID.
*
* Description:
* This function returns a unique identifer for this device.
* We attemp to obtain the Inquiry Device ID page first, then
* if that fails we attempt to obtain the serial num,ber page.
*
* Inputs:
* fd = The file descriptor.
* dsf = The device special file (raw or "sg" for Linux).
* identifier = Pointer to buffer to return identifier.
* idt = The ID type(s) to attempt (IDT_BOTHIDS, IDT_DEVICEID, IDT_SERIALID)
* debug = Flag to control debug output.
* errlog = Flag to control error logging. (True logs error)
* (False suppesses)
* timeout = The timeout value (in ms).
*
* Returns:
* Returns IDT_NONE and set identifier ptr to NULL if an error occurs.
* Returns IDT_DEVICEID if identifier points to an actual device ID.
* Returns IDT_SERIALID if identifier points to a manufactured identifier,
* using Inquiry vendor/product info and serial number page.
*
* Note: The identifier is dynamically allocated, so the caller is
* responsible to free that memory, when it's no longer desired.
*/
idt_t
GetUniqueID(HANDLE fd, char *dsf, hbool_t debug,
char **identifier, idt_t idt,
hbool_t errlog, unsigned int timeout)
{
inquiry_t inquiry_data;
inquiry_t *inquiry = &inquiry_data;
char *serial_number;
int status;
*identifier = NULL;
/*
* Start off by requesting the standard Inquiry data.
*/
status = Inquiry(fd, dsf, debug, errlog, NULL, NULL,
inquiry, sizeof(*inquiry), 0, 0, timeout);
if (status != SUCCESS) {
return(IDT_NONE);
}
if ( (idt == IDT_BOTHIDS) || (idt == IDT_DEVICEID) ) {
/*
* The preferred ID, is from Inquiry Page 0x83 (Device ID).
*/
if (*identifier = GetDeviceIdentifier(fd, dsf, debug, errlog,
NULL, NULL, inquiry, timeout)) {
return(IDT_DEVICEID);
}
}
if ( (idt == IDT_BOTHIDS) || (idt == IDT_SERIALID) ) {
/*
* The less preferred WWID, is the serial number prepended with
* the vendor and product names to attempt uniqueness.
*/
if (serial_number = GetSerialNumber(fd, dsf, debug, errlog,
NULL, NULL, inquiry, timeout)) {
*identifier = Malloc(NULL, MAX_INQ_LEN + INQ_VID_LEN + INQ_PID_LEN);
*identifier[0] = '\0';
(void)strncpy(*identifier, (char *)inquiry->inq_vid, INQ_VID_LEN);
(void)strncat(*identifier, (char *)inquiry->inq_pid, INQ_PID_LEN);
(void)strcat(*identifier, serial_number);
(void)free(serial_number);
return(IDT_SERIALID);
}
}
return(IDT_NONE);
}
/* ======================================================================== */
/*
* Declarations/Definitions for Read Capacity(10) Command:
*/
#define ReadCapacity10Name "Read Capacity(10)"
#define ReadCapacity10Opcode 0x25
#define ReadCapacity10CdbSize 10
#define ReadCapacity10Timeout ScsiDefaultTimeout
/*
* ReadCapacity10() - Issue Read Capacity (10) Command.
*
* Inputs:
* fd = The file descriptor to issue bus reset to.
* dsf = The device special file (raw or "sg" for Linux).
* debug = Flag to control debug output.
* errlog = Flag to control error logging. (True logs error)
* (False suppesses)
* sap = Pointer to SCSI address (optional).
* sgpp = Pointer to SCSI generic pointer (optional).
* data = Buffer for received capacity data.
* len = The length of the data buffer.
* sflags = OS specific SCSI flags (if any).
* timeout = The timeout value (in ms).
*
* Return Value:
* Returns the status from the IOCTL request which is:
* 0 = Success, -1 = Failure
*
* Note: If the caller supplies a SCSI generic pointer, then
* it's the callers responsibility to free this structure, along
* with the data buffer and sense buffer. This capability is
* provided so the caller can examine SCSI status, sense data,
* and data transfers, to make (more) intelligent decisions.
*/
int
ReadCapacity10(HANDLE fd, char *dsf, hbool_t debug, hbool_t errlog,
scsi_addr_t *sap, scsi_generic_t **sgpp,
void *data, unsigned int len,
unsigned int sflags, unsigned int timeout)
{
scsi_generic_t *sgp;
int error;
/*
* Setup and/or allocate a SCSI generic data structure.
*/
if (sgpp && *sgpp) {
sgp = *sgpp;
} else {
sgp = init_scsi_generic();
}
if (sgp->fd == INVALID_HANDLE_VALUE) {
sgp->fd = fd;
sgp->dsf = dsf;
}
memset(sgp->cdb, 0, sizeof(sgp->cdb));
if (data && len) memset(data, 0, len);
sgp->cdb[0] = ReadCapacity10Opcode;
sgp->cdb_size = ReadCapacity10CdbSize;
sgp->cdb_name = ReadCapacity10Name;
sgp->data_dir = scsi_data_read;
sgp->data_buffer= data;
sgp->data_length= len;
sgp->errlog = errlog;
sgp->iface = NULL;
sgp->timeout = (timeout) ? timeout : ReadCapacity10Timeout;
sgp->debug = debug;
/*
* If a SCSI address was specified, do a structure copy.
*/
if (sap) {
sgp->scsi_addr = *sap; /* Copy the SCSI address info. */
}
error = libExecuteCdb(sgp);
/*
* If the user supplied a pointer, send the SCSI generic data
* back to them for further analysis.
*/
if (sgpp) {
if (*sgpp == NULL) {
*sgpp = sgp; /* Return the generic data pointer. */
}
} else {
free_palign(sgp->opaque, sgp->sense_data);
free(sgp);
}
return(error);
}
/* ======================================================================== */
/*
* Declarations/Definitions for Read Capacity(16) Command:
*/
#define ReadCapacity16Name "Read Capacity(16)"
#define ReadCapacity16Opcode 0x9e
#define ReadCapacity16Subcode 0x10