-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsg_pt_win32.c
3154 lines (2936 loc) · 104 KB
/
sg_pt_win32.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-2023 Douglas Gilbert.
* All rights reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the BSD_LICENSE file.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
/* sg_pt_win32 version 1.36 20231124 */
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "sg_lib.h"
#include "sg_unaligned.h"
#include "sg_pt.h"
#include "sg_pt_win32.h"
#include "sg_nvme.h"
#include "sg_snt.h"
#include "sg_pr2serr.h"
/* Comment the following line out to use the pre-W10 NVMe pass-through */
#define W10_NVME_NON_PASSTHRU 1
#ifndef O_EXCL
// #define O_EXCL 0x80 // cygwin ??
// #define O_EXCL 0x80 // Linux
#define O_EXCL 0x400 // mingw
#warning "O_EXCL not defined"
#endif
#define SCSI_INQUIRY_OPC 0x12
#define SCSI_REPORT_LUNS_OPC 0xa0
#define SCSI_TEST_UNIT_READY_OPC 0x0
#define SCSI_REQUEST_SENSE_OPC 0x3
#define SCSI_SEND_DIAGNOSTIC_OPC 0x1d
#define SCSI_RECEIVE_DIAGNOSTIC_OPC 0x1c
#define SCSI_MAINT_IN_OPC 0xa3
#define SCSI_REP_SUP_OPCS_OPC 0xc
#define SCSI_REP_SUP_TMFS_OPC 0xd
#define SCSI_MODE_SENSE10_OPC 0x5a
#define SCSI_MODE_SELECT10_OPC 0x55
/* Additional Sense Code (ASC) */
#define NO_ADDITIONAL_SENSE 0x0
#define LOGICAL_UNIT_NOT_READY 0x4
#define LOGICAL_UNIT_COMMUNICATION_FAILURE 0x8
#define UNRECOVERED_READ_ERR 0x11
#define PARAMETER_LIST_LENGTH_ERR 0x1a
#define INVALID_OPCODE 0x20
#define LBA_OUT_OF_RANGE 0x21
#define INVALID_FIELD_IN_CDB 0x24
#define INVALID_FIELD_IN_PARAM_LIST 0x26
#define UA_RESET_ASC 0x29
#define UA_CHANGED_ASC 0x2a
#define TARGET_CHANGED_ASC 0x3f
#define LUNS_CHANGED_ASCQ 0x0e
#define INSUFF_RES_ASC 0x55
#define INSUFF_RES_ASCQ 0x3
#define LOW_POWER_COND_ON_ASC 0x5e /* ASCQ=0 */
#define POWER_ON_RESET_ASCQ 0x0
#define BUS_RESET_ASCQ 0x2 /* scsi bus reset occurred */
#define MODE_CHANGED_ASCQ 0x1 /* mode parameters changed */
#define CAPACITY_CHANGED_ASCQ 0x9
#define SAVING_PARAMS_UNSUP 0x39
#define TRANSPORT_PROBLEM 0x4b
#define THRESHOLD_EXCEEDED 0x5d
#define LOW_POWER_COND_ON 0x5e
#define MISCOMPARE_VERIFY_ASC 0x1d
#define MICROCODE_CHANGED_ASCQ 0x1 /* with TARGET_CHANGED_ASC */
#define MICROCODE_CHANGED_WO_RESET_ASCQ 0x16
/* Use the Microsoft SCSI Pass Through (SPT) interface. It has two
* variants: "SPT" where data is double buffered; and "SPTD" where data
* pointers to the user space are passed to the OS. Only Windows
* 2000 and later (i.e. not 95,98 or ME).
* There is no ASPI interface which relies on a dll from adaptec.
* This code uses cygwin facilities and is built in a cygwin
* shell. It can be run in a normal DOS shell if the cygwin1.dll
* file is put in an appropriate place.
* This code can build in a MinGW environment.
*
* N.B. MSDN says that the "SPT" interface (i.e. double buffered)
* should be used for small amounts of data (it says "< 16 KB").
* The direct variant (i.e. IOCTL_SCSI_PASS_THROUGH_DIRECT) should
* be used for larger amounts of data but the buffer needs to be
* "cache aligned". Is that 16 byte alignment or greater?
*
* This code will default to indirect (i.e. double buffered) access
* unless the WIN32_SPT_DIRECT preprocessor constant is defined in
* config.h . In version 1.12 runtime selection of direct and indirect
* access was added; the default is still determined by the
* WIN32_SPT_DIRECT preprocessor constant.
*/
#define DEF_TIMEOUT 60 /* 60 seconds */
#define MAX_OPEN_SIMULT 8
#define WIN32_FDOFFSET 32
union STORAGE_DEVICE_DESCRIPTOR_DATA {
STORAGE_DEVICE_DESCRIPTOR desc;
char raw[256];
};
union STORAGE_DEVICE_UID_DATA {
STORAGE_DEVICE_UNIQUE_IDENTIFIER desc;
char raw[1060];
};
struct sg_pt_handle {
bool in_use;
bool not_claimed;
bool checked_handle;
bool bus_type_failed;
bool is_nvme;
bool got_physical_drive;
HANDLE fh;
char adapter[32]; /* for example: '\\.\scsi3' */
int bus; /* a.k.a. PathId in MS docs */
int target;
int lun;
int scsi_pdt; /* Peripheral Device Type, PDT_ALL if not known */
// uint32_t nvme_nsid; /* how do we find this given file handle ?? */
int verbose; /* tunnel verbose through to scsi_pt_close_device */
char dname[20];
struct sg_snt_dev_state_t dev_stat; // owner
};
/* Start zeroed but need to zeroed before use because could be re-use */
static struct sg_pt_handle handle_arr[MAX_OPEN_SIMULT];
struct sg_pt_win32_scsi {
bool is_nvme;
bool nvme_direct; /* false: our SNTL; true: received NVMe command */
bool mdxfer_out; /* direction of metadata xfer, true->data-out */
bool have_nvme_cmd;
bool is_read;
int sense_len;
int scsi_status;
int resid;
int sense_resid;
int in_err;
int os_err; /* pseudo unix error */
int transport_err; /* windows error number */
int dev_fd; /* -1 for no "file descriptor" given */
uint32_t nvme_nsid; /* 1 to 0xfffffffe are possibly valid, 0
* implies dev_fd is not a NVMe device
* (is_nvme=false) or has no storage (e.g.
* enclosure rather than disk) */
uint32_t nvme_result; /* DW0 from completion queue */
uint32_t nvme_status; /* SCT|SC: DW3 27:17 from completion queue,
* note: the DNR+More bit are not there.
* The whole 16 byte completion q entry is
* sent back as sense data */
uint32_t dxfer_len;
uint32_t mdxfer_len;
uint8_t * dxferp;
uint8_t * mdxferp; /* NVMe has metadata buffer */
uint8_t * sensep;
uint8_t * nvme_id_ctlp;
uint8_t * free_nvme_id_ctlp;
struct sg_snt_dev_state_t * dev_statp; /* points to handle's dev_stat */
uint8_t nvme_cmd[64];
union {
SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER swb_d;
/* Last entry in structure so data buffer can be extended */
SCSI_PASS_THROUGH_WITH_BUFFERS swb_i;
};
};
/* embed pointer so can change on fly if (non-direct) data buffer
* is not big enough */
struct sg_pt_base {
struct sg_pt_win32_scsi * implp;
};
#ifdef WIN32_SPT_DIRECT
static int spt_direct = 1;
#else
static int spt_direct = 0;
#endif
static int nvme_pt(struct sg_pt_win32_scsi * psp, struct sg_pt_handle * shp,
int time_secs, int vb);
/* Request SPT direct interface when state_direct is 1, state_direct set
* to 0 for the SPT indirect interface. */
void
scsi_pt_win32_direct(int state_direct)
{
spt_direct = state_direct;
}
/* Returns current SPT interface state, 1 for direct, 0 for indirect */
int
scsi_pt_win32_spt_state(void)
{
return spt_direct;
}
static const char *
bus_type_str(int bt)
{
switch (bt)
{
case BusTypeUnknown:
return "Unknown";
case BusTypeScsi:
return "Scsi";
case BusTypeAtapi:
return "Atapi";
case BusTypeAta:
return "Ata";
case BusType1394:
return "1394";
case BusTypeSsa:
return "Ssa";
case BusTypeFibre:
return "Fibre";
case BusTypeUsb:
return "Usb";
case BusTypeRAID:
return "RAID";
case BusTypeiScsi:
return "iScsi";
case BusTypeSas:
return "Sas";
case BusTypeSata:
return "Sata";
case BusTypeSd:
return "Sd";
case BusTypeMmc:
return "Mmc";
case BusTypeVirtual:
return "Virt";
case BusTypeFileBackedVirtual:
return "FBVir";
#ifdef BusTypeSpaces
case BusTypeSpaces:
#else
case 0x10:
#endif
return "Spaces";
#ifdef BusTypeNvme
case BusTypeNvme:
#else
case 0x11:
#endif
return "NVMe";
#ifdef BusTypeSCM
case BusTypeSCM:
#else
case 0x12:
#endif
return "SCM";
#ifdef BusTypeUfs
case BusTypeUfs:
#else
case 0x13:
#endif
return "Ufs";
case 0x14:
return "Max";
case 0x7f:
return "Max Reserved";
default:
return "_unknown";
}
}
static char *
get_err_str(DWORD err, int max_b_len, char * b)
{
LPVOID lpMsgBuf = NULL;
int k, num, ch;
memset(b, 0, max_b_len);
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
err,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
num = lpMsgBuf ? lstrlen((LPCTSTR)lpMsgBuf) : 0;
if (num < 1)
return b;
num = (num < max_b_len) ? num : (max_b_len - 1);
for (k = 0; k < num; ++k) {
ch = *((LPCTSTR)lpMsgBuf + k);
if ((ch >= 0x0) && (ch < 0x7f))
b[k] = ch & 0x7f;
else
b[k] = '?';
}
return b;
}
/* Returns pointer to sg_pt_handle object given Unix like device_fd. If
* device_fd is invalid or not open returns NULL. If psp is non-NULL and
* NULL is returned then ENODEV is placed in psp->os_err. */
static struct sg_pt_handle *
get_open_pt_handle(struct sg_pt_win32_scsi * psp, int device_fd, bool vbb)
{
int index = device_fd - WIN32_FDOFFSET;
struct sg_pt_handle * shp;
if ((index < 0) || (index >= WIN32_FDOFFSET)) {
if (vbb)
pr2ws("Bad file descriptor\n");
if (psp)
psp->os_err = EBADF;
return NULL;
}
shp = handle_arr + index;
if (! shp->in_use) {
if (vbb)
pr2ws("File descriptor closed??\n");
if (psp)
psp->os_err = ENODEV;
return NULL;
}
return shp;
}
/* Returns >= 0 if successful. If error in Unix returns negated errno. */
int
scsi_pt_open_device(const char * device_name, bool read_only, int vb)
{
int oflags = 0 /* O_NONBLOCK*/ ;
oflags |= (read_only ? 0 : 0); /* was ... ? O_RDONLY : O_RDWR) */
return scsi_pt_open_flags(device_name, oflags, vb);
}
/*
* Similar to scsi_pt_open_device() but takes Unix style open flags OR-ed
* together. The 'flags' argument is ignored in Windows.
* Returns >= 0 if successful, otherwise returns negated errno.
* Optionally accept leading "\\.\". If given something of the form
* "SCSI<num>:<bus>,<target>,<lun>" where the values in angle brackets
* are integers, then will attempt to open "\\.\SCSI<num>:" and save the
* other three values for the DeviceIoControl call. The trailing ".<lun>"
* is optionally and if not given 0 is assumed. Since "PhysicalDrive"
* is a lot of keystrokes, "PD" is accepted and converted to the longer
* form.
*/
int
scsi_pt_open_flags(const char * device_name, int flags, int vb)
{
bool got_scsi_name = false;
int len, k, adapter_num, bus, target, lun, off, index, num, pd_num;
int share_mode;
struct sg_pt_handle * shp;
char buff[8];
share_mode = (O_EXCL & flags) ? 0 : (FILE_SHARE_READ | FILE_SHARE_WRITE);
/* lock */
for (k = 0; k < MAX_OPEN_SIMULT; k++)
if (! handle_arr[k].in_use)
break;
if (k == MAX_OPEN_SIMULT) {
if (vb)
pr2ws("too many open handles (%d)\n", MAX_OPEN_SIMULT);
return -EMFILE;
} else {
/* clear any previous contents */
memset(handle_arr + k, 0, sizeof(struct sg_pt_handle));
handle_arr[k].in_use = true;
}
/* unlock */
index = k;
shp = handle_arr + index;
#if (HAVE_NVME && (! IGNORE_NVME))
sg_snt_init_dev_stat(&shp->dev_stat);
#endif
adapter_num = 0;
bus = 0; /* also known as 'PathId' in MS docs */
target = 0;
lun = 0;
len = (int)strlen(device_name);
k = (int)sizeof(shp->dname);
if (len < k)
strcpy(shp->dname, device_name);
else if (len == k)
memcpy(shp->dname, device_name, k - 1);
else /* trim on left */
memcpy(shp->dname, device_name + (len - k), k - 1);
shp->dname[k - 1] = '\0';
if ((len > 4) && (0 == strncmp("\\\\.\\", device_name, 4)))
off = 4;
else
off = 0;
if (len > (off + 2)) {
buff[0] = toupper((int)device_name[off + 0]);
buff[1] = toupper((int)device_name[off + 1]);
if (0 == strncmp("PD", buff, 2)) {
num = sscanf(device_name + off + 2, "%d", &pd_num);
if (1 == num)
shp->got_physical_drive = true;
}
if (! shp->got_physical_drive) {
buff[2] = toupper((int)device_name[off + 2]);
buff[3] = toupper((int)device_name[off + 3]);
if (0 == strncmp("SCSI", buff, 4)) {
num = sscanf(device_name + off + 4, "%d:%d,%d,%d",
&adapter_num, &bus, &target, &lun);
if (num < 3) {
if (vb)
pr2ws("expected format like: "
"'SCSI<port>:<bus>,<target>[,<lun>]'\n");
shp->in_use = false;
return -EINVAL;
}
got_scsi_name = true;
}
}
}
shp->bus = bus;
shp->target = target;
shp->lun = lun;
shp->scsi_pdt = PDT_ALL;
shp->verbose = vb;
memset(shp->adapter, 0, sizeof(shp->adapter));
memcpy(shp->adapter, "\\\\.\\", 4);
if (shp->got_physical_drive)
snprintf(shp->adapter + 4, sizeof(shp->adapter) - 5,
"PhysicalDrive%d", pd_num);
else if (got_scsi_name)
snprintf(shp->adapter + 4, sizeof(shp->adapter) - 5, "SCSI%d:",
adapter_num);
else
snprintf(shp->adapter + 4, sizeof(shp->adapter) - 5, "%s",
device_name + off);
if (vb > 4)
pr2ws("%s: CreateFile('%s'), bus=%d, target=%d, lun=%d\n", __func__,
shp->adapter, bus, target, lun);
#if 1
shp->fh = CreateFile(shp->adapter, GENERIC_READ | GENERIC_WRITE,
share_mode, NULL, OPEN_EXISTING, 0, NULL);
#endif
#if 0
shp->fh = CreateFileA(shp->adapter, GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(SECURITY_ATTRIBUTES *)0, OPEN_EXISTING, 0, 0);
// No GENERIC_READ/WRITE access required, works without admin rights (W10)
shp->fh = CreateFileA(shp->adapter, 0, FILE_SHARE_READ | FILE_SHARE_WRITE,
(SECURITY_ATTRIBUTES *)0, OPEN_EXISTING, 0, (HANDLE)0);
#endif
if (shp->fh == INVALID_HANDLE_VALUE) {
if (vb) {
uint32_t err = (uint32_t)GetLastError();
char b[128];
pr2ws("%s: CreateFile error: %s [%u]\n", __func__,
get_err_str(err, sizeof(b), b), err);
}
shp->in_use = false;
return -ENODEV;
}
return index + WIN32_FDOFFSET;
}
/* Returns 0 if successful. If device_id seems wild returns -ENODEV,
* other errors return 0. If CloseHandle() fails and verbose > 0 then
* outputs warning with value from GetLastError(). The verbose value
* defaults to zero and is potentially set from the most recent call
* to scsi_pt_open_device() or do_scsi_pt(). */
int
scsi_pt_close_device(int device_fd)
{
struct sg_pt_handle * shp = get_open_pt_handle(NULL, device_fd, false);
if (NULL == shp)
return -ENODEV;
if ((! CloseHandle(shp->fh)) && shp->verbose)
pr2ws("Windows CloseHandle error=%u\n", (unsigned int)GetLastError());
shp->bus = 0;
shp->target = 0;
shp->lun = 0;
memset(shp->adapter, 0, sizeof(shp->adapter));
shp->in_use = false;
shp->verbose = 0;
shp->dname[0] = '\0';
return 0;
}
/* Attempt to return device's SCSI peripheral device type (pdt), a number
* between 0 (disks) and 31 (not given) by calling IOCTL_SCSI_GET_INQUIRY_DATA
* on the adapter. Returns -EIO on error and -999 if not found. */
static int
get_scsi_pdt(struct sg_pt_handle *shp, int vb)
{
const int alloc_sz = 8192;
int j;
int ret = -999;
BOOL ok;
ULONG dummy;
DWORD err;
BYTE wbus;
uint8_t * inqBuf;
uint8_t * free_inqBuf;
char b[128];
if (vb > 2)
pr2ws("%s: enter, adapter: %s\n", __func__, shp->adapter);
inqBuf = sg_memalign(alloc_sz, 0 /* page size */, &free_inqBuf, false);
if (NULL == inqBuf) {
pr2ws("%s: unable to allocate %d bytes\n", __func__, alloc_sz);
return -ENOMEM;
}
ok = DeviceIoControl(shp->fh, IOCTL_SCSI_GET_INQUIRY_DATA,
NULL, 0, inqBuf, alloc_sz, &dummy, NULL);
if (ok) {
PSCSI_ADAPTER_BUS_INFO ai;
PSCSI_BUS_DATA pbd;
PSCSI_INQUIRY_DATA pid;
int num_lus, off;
ai = (PSCSI_ADAPTER_BUS_INFO)inqBuf;
for (wbus = 0; wbus < ai->NumberOfBusses; ++wbus) {
pbd = ai->BusData + wbus;
num_lus = pbd->NumberOfLogicalUnits;
off = pbd->InquiryDataOffset;
for (j = 0; j < num_lus; ++j) {
if ((off < (int)sizeof(SCSI_ADAPTER_BUS_INFO)) ||
(off > (alloc_sz - (int)sizeof(SCSI_INQUIRY_DATA))))
break;
pid = (PSCSI_INQUIRY_DATA)(inqBuf + off);
if ((shp->bus == pid->PathId) &&
(shp->target == pid->TargetId) &&
(shp->lun == pid->Lun)) { /* got match */
shp->scsi_pdt = pid->InquiryData[0] & PDT_MASK;
shp->not_claimed = ! pid->DeviceClaimed;
shp->checked_handle = true;
shp->bus_type_failed = false;
if (vb > 3)
pr2ws("%s: found, scsi_pdt=%d, claimed=%d, "
"target=%d, lun=%d\n", __func__, shp->scsi_pdt,
pid->DeviceClaimed, shp->target, shp->lun);
ret = shp->scsi_pdt;
goto fini;
}
off = pid->NextInquiryDataOffset;
}
}
} else {
err = GetLastError();
if (vb > 1)
pr2ws("%s: IOCTL_SCSI_GET_INQUIRY_DATA failed err=%u\n\t%s",
shp->adapter, (unsigned int)err,
get_err_str(err, sizeof(b), b));
ret = -EIO;
}
fini:
if (free_inqBuf)
free(free_inqBuf);
return ret; /* no match after checking all PathIds, Targets and LUs */
}
/* Returns 0 on success, negated errno if error */
static int
get_bus_type(struct sg_pt_handle *shp, const char *dname,
STORAGE_BUS_TYPE * btp, int vb)
{
DWORD num_out, err;
STORAGE_BUS_TYPE bt;
union STORAGE_DEVICE_DESCRIPTOR_DATA sddd;
STORAGE_PROPERTY_QUERY query = {StorageDeviceProperty,
PropertyStandardQuery, {0} };
char b[256];
memset(&sddd, 0, sizeof(sddd));
if (! DeviceIoControl(shp->fh, IOCTL_STORAGE_QUERY_PROPERTY,
&query, sizeof(query), &sddd, sizeof(sddd),
&num_out, NULL)) {
if (vb > 2) {
err = GetLastError();
pr2ws("%s IOCTL_STORAGE_QUERY_PROPERTY(Devprop) failed, "
"Error: %s [%u]\n", dname, get_err_str(err, sizeof(b), b),
(uint32_t)err);
}
shp->bus_type_failed = true;
return -EIO;
}
bt = sddd.desc.BusType;
if (vb > 2) {
pr2ws("%s: Bus type: %s\n", __func__, bus_type_str((int)bt));
if (vb > 3) {
pr2ws("Storage Device Descriptor Data:\n");
hex2stderr((const uint8_t *)&sddd, num_out, 0);
}
}
if (shp) {
shp->checked_handle = true;
shp->bus_type_failed = false;
shp->is_nvme = (BusTypeNvme == bt);
}
if (btp)
*btp = bt;
return 0;
}
/* Assumes dev_fd is an "open" file handle associated with device_name. If
* the implementation (possibly for one OS) cannot determine from dev_fd if
* a SCSI or NVMe pass-through is referenced, then it might guess based on
* device_name. Returns 1 if SCSI generic pass-though device, returns 2 if
* secondary SCSI pass-through device (in Linux a bsg device); returns 3 is
* char NVMe device (i.e. no NSID); returns 4 if block NVMe device (includes
* NSID), or 0 if something else (e.g. ATA block device) or dev_fd < 0.
* If error, returns negated errno (operating system) value. */
int
check_pt_file_handle(int device_fd, const char * device_name, int vb)
{
int res;
STORAGE_BUS_TYPE bt;
const char * dnp = device_name;
struct sg_pt_handle * shp;
if (vb > 3)
pr2ws("%s: device_name: %s\n", __func__, dnp);
shp = get_open_pt_handle(NULL, device_fd, vb > 1);
if (NULL == shp) {
pr2ws("%s: device_fd (%s) bad or not in_use ??\n", __func__,
dnp ? dnp : "");
return -ENODEV;
}
if (shp->bus_type_failed) {
if (vb > 2)
pr2ws("%s: skip because get_bus_type() has failed\n", __func__);
return 0;
}
dnp = dnp ? dnp : shp->dname;
res = get_bus_type(shp, dnp, &bt, vb);
if (res < 0) {
if (! shp->got_physical_drive) {
res = get_scsi_pdt(shp, vb);
if (res >= 0)
return 1;
}
return res;
}
return (BusTypeNvme == bt) ? 3 : 1;
/* NVMe "char" ?? device, could be enclosure: 3 */
/* SCSI generic pass-though device: 1 */
}
#if (HAVE_NVME && (! IGNORE_NVME))
static bool checked_ev_dsense = false;
static bool ev_dsense = false;
#endif
struct sg_pt_base *
construct_scsi_pt_obj_with_fd(int dev_fd, int vb)
{
int res;
struct sg_pt_win32_scsi * psp;
struct sg_pt_base * vp = NULL;
struct sg_pt_handle * shp = NULL;
if (dev_fd >= 0) {
shp = get_open_pt_handle(NULL, dev_fd, vb > 1);
if (NULL == shp) {
if (vb)
pr2ws("%s: dev_fd is not open\n", __func__);
return NULL;
}
if (! (shp->bus_type_failed || shp->checked_handle)) {
res = get_bus_type(shp, shp->dname, NULL, vb);
if (res < 0) {
if (! shp->got_physical_drive)
res = get_scsi_pdt(shp, vb);
if ((res < 0) && (vb > 1))
pr2ws("%s: get_bus_type() errno=%d, continue\n", __func__,
-res);
}
}
}
psp = (struct sg_pt_win32_scsi *)calloc(sizeof(struct sg_pt_win32_scsi),
1);
if (psp) {
psp->dev_fd = (dev_fd < 0) ? -1 : dev_fd;
if (shp) {
psp->is_nvme = shp->is_nvme;
psp->dev_statp = &shp->dev_stat;
#if (HAVE_NVME && (! IGNORE_NVME))
sg_snt_init_dev_stat(psp->dev_statp);
if (! checked_ev_dsense) {
ev_dsense = sg_get_initial_dsense();
checked_ev_dsense = true;
}
shp->dev_stat.scsi_dsense = ev_dsense;
#endif
}
if (psp->is_nvme) {
; /* should be 'psp->nvme_nsid = shp->nvme_nsid' */
} else if (spt_direct) {
psp->swb_d.spt.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
psp->swb_d.spt.SenseInfoLength = SCSI_MAX_SENSE_LEN;
psp->swb_d.spt.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, ucSenseBuf);
psp->swb_d.spt.TimeOutValue = DEF_TIMEOUT;
} else {
psp->swb_i.spt.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
psp->swb_i.spt.SenseInfoLength = SCSI_MAX_SENSE_LEN;
psp->swb_i.spt.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, ucSenseBuf);
psp->swb_i.spt.TimeOutValue = DEF_TIMEOUT;
}
vp = (struct sg_pt_base *)malloc(sizeof(struct sg_pt_win32_scsi *));
/* yes, allocating the size of a pointer (4 or 8 bytes) */
if (vp)
vp->implp = psp;
else
free(psp);
}
if ((NULL == vp) && vb)
pr2ws("%s: about to return NULL, space problem\n", __func__);
return vp;
}
struct sg_pt_base *
construct_scsi_pt_obj(void)
{
return construct_scsi_pt_obj_with_fd(-1, 0);
}
void
destruct_scsi_pt_obj(struct sg_pt_base * vp)
{
if (vp) {
struct sg_pt_win32_scsi * psp = vp->implp;
if (psp) {
free(psp);
}
free(vp);
}
}
/* Forget any previous dev_han and install the one given. May attempt to
* find file type (e.g. if pass-though) from OS so there could be an error.
* Returns 0 for success or the same value as get_scsi_pt_os_err()
* will return. dev_han should be >= 0 for a valid file handle or -1 . */
int
set_pt_file_handle(struct sg_pt_base * vp, int dev_han, int vb)
{
int res;
struct sg_pt_win32_scsi * psp;
if (NULL == vp) {
if (vb)
pr2ws(">>>> %s: pointer to object is NULL\n", __func__);
return EINVAL;
}
if ((psp = vp->implp)) {
struct sg_pt_handle * shp;
if (dev_han < 0) {
psp->dev_fd = -1;
psp->is_nvme = false;
psp->nvme_nsid = 0;
return 0;
}
shp = get_open_pt_handle(psp, dev_han, vb > 1);
if (NULL == shp) {
if (vb)
pr2ws("%s: dev_han (%d) is invalid\n", __func__, dev_han);
psp->os_err = EINVAL;
return psp->os_err;
}
psp->os_err = 0;
psp->transport_err = 0;
psp->in_err = 0;
psp->scsi_status = 0;
psp->dev_fd = dev_han;
if (! (shp->bus_type_failed || shp->checked_handle)) {
res = get_bus_type(shp, shp->dname, NULL, vb);
if (res < 0) {
res = get_scsi_pdt(shp, vb);
if (res >= 0) /* clears shp->bus_type_failed on success */
psp->os_err = 0;
}
if ((res < 0) && (vb > 2))
pr2ws("%s: get_bus_type() errno=%d\n", __func__, -res);
}
if (shp->bus_type_failed)
psp->os_err = EIO;
if (psp->os_err)
return psp->os_err;
psp->is_nvme = shp->is_nvme;
psp->nvme_nsid = 0; /* should be 'psp->nvme_nsid = shp->nvme_nsid' */
psp->dev_statp = &shp->dev_stat;
}
return 0;
}
/* Valid file handles (which is the return value) are >= 0 . Returns -1
* if there is no valid file handle. */
int
get_pt_file_handle(const struct sg_pt_base * vp)
{
const struct sg_pt_win32_scsi * psp;
if (vp) {
psp = vp->implp;
return psp ? psp->dev_fd : -1;
}
return -1;
}
/* Keep state information such as dev_fd and nvme_nsid */
void
clear_scsi_pt_obj(struct sg_pt_base * vp)
{
bool is_nvme;
int dev_fd;
uint32_t nvme_nsid;
struct sg_pt_win32_scsi * psp = vp->implp;
struct sg_snt_dev_state_t * dsp;
if (psp) {
dev_fd = psp->dev_fd;
is_nvme = psp->is_nvme;
nvme_nsid = psp->nvme_nsid;
dsp = psp->dev_statp;
memset(psp, 0, sizeof(struct sg_pt_win32_scsi));
if (spt_direct) {
psp->swb_d.spt.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
psp->swb_d.spt.SenseInfoLength = SCSI_MAX_SENSE_LEN;
psp->swb_d.spt.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, ucSenseBuf);
psp->swb_d.spt.TimeOutValue = DEF_TIMEOUT;
} else {
psp->swb_i.spt.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
psp->swb_i.spt.SenseInfoLength = SCSI_MAX_SENSE_LEN;
psp->swb_i.spt.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, ucSenseBuf);
psp->swb_i.spt.TimeOutValue = DEF_TIMEOUT;
}
psp->dev_fd = dev_fd;
psp->is_nvme = is_nvme;
psp->nvme_nsid = nvme_nsid;
psp->dev_statp = dsp;
}
}
void
partial_clear_scsi_pt_obj(struct sg_pt_base * vp)
{
struct sg_pt_win32_scsi * psp = vp->implp;
if (NULL == psp)
return;
psp->in_err = 0;
psp->os_err = 0;
psp->transport_err = 0;
psp->scsi_status = 0;
if (spt_direct) {
psp->swb_d.spt.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
psp->swb_d.spt.SenseInfoLength = SCSI_MAX_SENSE_LEN;
psp->swb_d.spt.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, ucSenseBuf);
psp->swb_d.spt.TimeOutValue = DEF_TIMEOUT;
} else {
psp->swb_i.spt.DataIn = SCSI_IOCTL_DATA_UNSPECIFIED;
psp->swb_i.spt.SenseInfoLength = SCSI_MAX_SENSE_LEN;
psp->swb_i.spt.SenseInfoOffset =
offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS, ucSenseBuf);
psp->swb_i.spt.TimeOutValue = DEF_TIMEOUT;
}
}
void
set_scsi_pt_cdb(struct sg_pt_base * vp, const uint8_t * cdb,
int cdb_len)
{
bool scsi_cdb = sg_is_scsi_cdb(cdb, cdb_len);
struct sg_pt_win32_scsi * psp = vp->implp;
if (! scsi_cdb) {
psp->have_nvme_cmd = true;
memcpy(psp->nvme_cmd, cdb, cdb_len);
} else if (spt_direct) {
if (cdb_len > (int)sizeof(psp->swb_d.spt.Cdb)) {
++psp->in_err;
return;
}
memcpy(psp->swb_d.spt.Cdb, cdb, cdb_len);
psp->swb_d.spt.CdbLength = cdb_len;
} else {
if (cdb_len > (int)sizeof(psp->swb_i.spt.Cdb)) {
++psp->in_err;
return;
}
memcpy(psp->swb_i.spt.Cdb, cdb, cdb_len);
psp->swb_i.spt.CdbLength = cdb_len;
}
}
int
get_scsi_pt_cdb_len(const struct sg_pt_base * vp)
{
const struct sg_pt_win32_scsi * psp = vp->implp;
return spt_direct ? psp->swb_d.spt.CdbLength : psp->swb_i.spt.CdbLength;
}
uint8_t *
get_scsi_pt_cdb_buf(const struct sg_pt_base * vp)
{
const struct sg_pt_win32_scsi * psp = vp->implp;
if (spt_direct) {
if (psp->swb_d.spt.CdbLength > 0)
return (uint8_t *)(psp->swb_d.spt.Cdb);
else
return NULL;
} else {
if (psp->swb_i.spt.CdbLength > 0)
return (uint8_t *)(psp->swb_i.spt.Cdb);
else
return NULL;
}
}
void
set_scsi_pt_sense(struct sg_pt_base * vp, uint8_t * sense, int sense_len)
{
struct sg_pt_win32_scsi * psp = vp->implp;
if (sense && (sense_len > 0))
memset(sense, 0, sense_len);
psp->sensep = sense;
psp->sense_len = sense_len;
}
/* from device */
void
set_scsi_pt_data_in(struct sg_pt_base * vp, uint8_t * dxferp,
int dxfer_len)
{
struct sg_pt_win32_scsi * psp = vp->implp;
if (psp->dxferp)
++psp->in_err;
if (dxfer_len > 0) {
psp->dxferp = dxferp;
psp->dxfer_len = (uint32_t)dxfer_len;
psp->is_read = true;
if (spt_direct)
psp->swb_d.spt.DataIn = SCSI_IOCTL_DATA_IN;
else
psp->swb_i.spt.DataIn = SCSI_IOCTL_DATA_IN;
}
}
/* to device */
void
set_scsi_pt_data_out(struct sg_pt_base * vp, const uint8_t * dxferp,
int dxfer_len)
{
struct sg_pt_win32_scsi * psp = vp->implp;
if (psp->dxferp)
++psp->in_err;
if (dxfer_len > 0) {
psp->dxferp = (uint8_t *)dxferp;
psp->dxfer_len = (uint32_t)dxfer_len;
if (spt_direct)
psp->swb_d.spt.DataIn = SCSI_IOCTL_DATA_OUT;
else
psp->swb_i.spt.DataIn = SCSI_IOCTL_DATA_OUT;
}
}
void
set_pt_metadata_xfer(struct sg_pt_base * vp, uint8_t * mdxferp,
uint32_t mdxfer_len, bool out_true)
{
struct sg_pt_win32_scsi * psp = vp->implp;