4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2004-6 Christian Franke <smartmontools-support@lists.sourceforge.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example COPYING); if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 extern smartmonctrl
* con
; // con->permissive,reportataioctl
26 extern int64_t bytes
; // malloc() byte count
32 #define assert(x) /**/
34 #define WIN32_LEAN_AND_MEAN
36 #include <stddef.h> // offsetof()
37 #include <io.h> // access()
39 #define ARGUSED(x) ((void)(x))
41 // Macro to check constants at compile time using a dummy typedef
42 #define ASSERT_CONST(c, n) \
43 typedef char assert_const_##c[((c) == (n)) ? 1 : -1]
44 #define ASSERT_SIZEOF(t, n) \
45 typedef char assert_sizeof_##t[(sizeof(t) == (n)) ? 1 : -1]
48 // Needed by '-V' option (CVS versioning) of smartd/smartctl
49 const char *os_XXXX_c_cvsid
="$Id: os_win32.cpp,v 1.49 2006/10/27 21:49:41 chrfranke Exp $"
50 ATACMDS_H_CVSID CONFIG_H_CVSID EXTERN_H_CVSID INT64_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID
;
53 #ifndef HAVE_GET_OS_VERSION_STR
54 #error define of HAVE_GET_OS_VERSION_STR missing in config.h
57 // Return build host and OS version as static string
58 const char * get_os_version_str()
60 static char vstr
[sizeof(SMARTMONTOOLS_BUILD_HOST
)-3-1+sizeof("-2003r2-sp2.1")+13];
61 char * const vptr
= vstr
+sizeof(SMARTMONTOOLS_BUILD_HOST
)-3-1;
62 const int vlen
= sizeof(vstr
)-(sizeof(SMARTMONTOOLS_BUILD_HOST
)-3);
67 // remove "-pc" to avoid long lines
68 assert(!strncmp(SMARTMONTOOLS_BUILD_HOST
+5, "pc-", 3));
69 strcpy(vstr
, "i686-"); strcpy(vstr
+5, SMARTMONTOOLS_BUILD_HOST
+5+3);
70 assert(vptr
== vstr
+strlen(vstr
) && vptr
+vlen
+1 == vstr
+sizeof(vstr
));
72 memset(&vi
, 0, sizeof(vi
));
73 vi
.dwOSVersionInfoSize
= sizeof(vi
);
74 if (!GetVersionExA((OSVERSIONINFOA
*)&vi
)) {
75 memset(&vi
, 0, sizeof(vi
));
76 vi
.dwOSVersionInfoSize
= sizeof(OSVERSIONINFOA
);
77 if (!GetVersionExA((OSVERSIONINFOA
*)&vi
))
81 if (vi
.dwPlatformId
> 0xff || vi
.dwMajorVersion
> 0xff || vi
.dwMinorVersion
> 0xff)
84 switch (vi
.dwPlatformId
<< 16 | vi
.dwMajorVersion
<< 8 | vi
.dwMinorVersion
) {
85 case VER_PLATFORM_WIN32_WINDOWS
<<16|0x0400| 0:
86 w
= (vi
.szCSDVersion
[1] == 'B' ||
87 vi
.szCSDVersion
[1] == 'C' ? "95-osr2" : "95"); break;
88 case VER_PLATFORM_WIN32_WINDOWS
<<16|0x0400|10:
89 w
= (vi
.szCSDVersion
[1] == 'A' ? "98se" : "98"); break;
90 case VER_PLATFORM_WIN32_WINDOWS
<<16|0x0400|90: w
= "me"; break;
91 //case VER_PLATFORM_WIN32_NT <<16|0x0300|51: w = "nt3.51"; break;
92 case VER_PLATFORM_WIN32_NT
<<16|0x0400| 0: w
= "nt4"; break;
93 case VER_PLATFORM_WIN32_NT
<<16|0x0500| 0: w
= "2000"; break;
94 case VER_PLATFORM_WIN32_NT
<<16|0x0500| 1:
95 w
= (!GetSystemMetrics(87/*SM_MEDIACENTER*/) ? "xp"
97 case VER_PLATFORM_WIN32_NT
<<16|0x0500| 2:
98 w
= (!GetSystemMetrics(89/*SM_SERVERR2*/) ? "2003"
100 case VER_PLATFORM_WIN32_NT
<<16|0x0600| 0: w
= "vista"; break;
101 default: w
= 0; break;
105 snprintf(vptr
, vlen
, "-%s%lu.%lu",
106 (vi
.dwPlatformId
==VER_PLATFORM_WIN32_NT
? "nt" : "9x"),
107 vi
.dwMajorVersion
, vi
.dwMinorVersion
);
108 else if (vi
.wServicePackMinor
)
109 snprintf(vptr
, vlen
, "-%s-sp%u.%u", w
, vi
.wServicePackMajor
, vi
.wServicePackMinor
);
110 else if (vi
.wServicePackMajor
)
111 snprintf(vptr
, vlen
, "-%s-sp%u", w
, vi
.wServicePackMajor
);
113 snprintf(vptr
, vlen
, "-%s", w
);
118 #define ATARAID_FDOFFSET 0x0200
120 static int ata_open(int drive
, const char * options
, int port
);
121 static void ata_close(int fd
);
122 static int ata_scan(unsigned long * drives
, int * rdriveno
, unsigned long * rdrives
);
123 static const char * ata_get_def_options(void);
125 #define ASPI_FDOFFSET 0x0100
127 static int aspi_open(unsigned adapter
, unsigned id
);
128 static void aspi_close(int fd
);
129 static int aspi_scan(unsigned long * drives
);
131 #define SPT_FDOFFSET 0x0400
133 static int spt_open(int pd_num
, int tape_num
, int sub_addr
);
134 static void spt_close(int fd
);
137 static int is_permissive()
139 if (!con
->permissive
) {
140 pout("To continue, add one or more '-T permissive' options.\n");
147 static const char * skipdev(const char * s
)
149 return (!strncmp(s
, "/dev/", 5) ? s
+ 5 : s
);
153 // tries to guess device type given the name (a path). See utility.h
154 // for return values.
155 int guess_device_type (const char * dev_name
)
157 dev_name
= skipdev(dev_name
);
158 if (!strncmp(dev_name
, "hd", 2))
159 return CONTROLLER_ATA
;
160 if (!strncmp(dev_name
, "scsi", 4))
161 return CONTROLLER_SCSI
;
162 if (!strncmp(dev_name
, "sd", 2))
163 return CONTROLLER_SCSI
;
164 if (!strncmp(dev_name
, "pd", 2))
165 return CONTROLLER_SCSI
;
166 if (!strncmp(dev_name
, "tape", 4))
167 return CONTROLLER_SCSI
;
168 return CONTROLLER_UNKNOWN
;
172 // makes a list of ATA or SCSI devices for the DEVICESCAN directive of
173 // smartd. Returns number N of devices, or -1 if out of
174 // memory. Allocates N+1 arrays: one of N pointers (devlist), the
175 // others each contain null-terminated character strings.
176 int make_device_names (char*** devlist
, const char* type
)
178 unsigned long drives
[3];
180 unsigned long rdrives
[2];
181 int i
, j
, n
, nmax
, sz
;
184 drives
[0] = drives
[1] = drives
[2] = 0;
185 rdriveno
[0] = rdriveno
[1] = -1;
186 rdrives
[0] = rdrives
[1] = 0;
188 if (!strcmp(type
, "ATA")) {
189 // bit i set => drive i present
190 n
= ata_scan(drives
, rdriveno
, rdrives
);
194 else if (!strcmp(type
, "SCSI")) {
195 // bit i set => drive with ID (i & 0x7) on adapter (i >> 3) present
196 n
= aspi_scan(drives
);
197 path
= "/dev/scsi00";
207 sz
= n
* sizeof(char **);
208 *devlist
= (char **)malloc(sz
); bytes
+= sz
;
211 for (i
= j
= 0; i
< n
; ) {
212 while (j
< nmax
&& !(drives
[j
>> 5] & (1L << (j
& 0x1f))))
216 if (j
== rdriveno
[0] || j
== rdriveno
[1]) {
217 // Add physical drives behind this logical drive
218 int ci
= (j
== rdriveno
[0] ? 0 : 1);
219 for (int pi
= 0; pi
< 32 && i
< n
; pi
++) {
220 if (!(rdrives
[ci
] & (1L << pi
)))
223 sprintf(rpath
, "/dev/hd%c,%u", 'a'+j
, pi
);
224 sz
= strlen(rpath
)+1;
225 char * s
= (char *)malloc(sz
); bytes
+= sz
;
232 char * s
= (char *)malloc(sz
); bytes
+= sz
;
237 s
[sz
-2] += j
; // /dev/hd[a-j]
240 assert((j
>> 3) <= 9);
241 s
[sz
-3] += (j
>> 3); // /dev/scsi[0-9].....
242 s
[sz
-2] += (j
& 0x7); // .....[0-7]
253 // Like open(). Return positive integer handle, only used by
254 // functions below. type="ATA" or "SCSI". If you need to store extra
255 // information about your devices, create a private internal array
256 // within this file (see os_freebsd.cpp for an example).
257 int deviceopen(const char * pathname
, char *type
)
259 pathname
= skipdev(pathname
);
260 int len
= strlen(pathname
);
262 if (!strcmp(type
, "ATA")) {
263 // hd[a-j](:[saicp]+)? => ATA 0-9 with options
264 char drive
[1+1] = "", options
[5+1] = ""; int n1
= -1, n2
= -1;
265 if ( sscanf(pathname
, "hd%1[a-j]%n:%5[saicp]%n", drive
, &n1
, options
, &n2
) >= 1
266 && ((n1
== len
&& !options
[0]) || n2
== len
) ) {
267 return ata_open(drive
[0] - 'a', options
, -1);
269 // hd[a-j],N(:[saicp]+)? => Physical drive 0-9, RAID port N, with options
270 drive
[0] = 0; options
[0] = 0; n1
= -1; n2
= -1;
272 if ( sscanf(pathname
, "hd%1[a-j],%u%n:%5[saicp]%n", drive
, &port
, &n1
, options
, &n2
) >= 2
273 && port
< 32 && ((n1
== len
&& !options
[0]) || n2
== len
) ) {
274 return ata_open(drive
[0] - 'a', options
, port
);
276 } else if (!strcmp(type
, "SCSI")) {
277 // scsi[0-9][0-f] => ASPI Adapter 0-9, ID 0-15, LUN 0
278 unsigned adapter
= ~0, id
= ~0; int n1
= -1;
279 if (sscanf(pathname
,"scsi%1u%1x%n", &adapter
, &id
, &n1
) == 2 && n1
== len
) {
280 return aspi_open(adapter
, id
);
282 // sd[a-z],N => Physical drive 0-26, RAID port N
283 char drive
[1+1] = ""; int sub_addr
= -1; n1
= -1; int n2
= -1;
284 if ( sscanf(pathname
, "sd%1[a-z]%n,%d%n", drive
, &n1
, &sub_addr
, &n2
) >= 1
285 && ((n1
== len
&& sub_addr
== -1) || (n2
== len
&& sub_addr
>= 0)) ) {
286 return spt_open(drive
[0] - 'a', -1, sub_addr
);
288 // pd<m>,N => Physical drive <m>, RAID port N
289 int pd_num
= -1; sub_addr
= -1; n1
= -1; n2
= -1;
290 if ( sscanf(pathname
, "pd%d%n,%d%n", &pd_num
, &n1
, &sub_addr
, &n2
) >= 1
291 && pd_num
>= 0 && ((n1
== len
&& sub_addr
== -1) || (n2
== len
&& sub_addr
>= 0))) {
292 return spt_open(pd_num
, -1, sub_addr
);
294 // tape<m> => tape drive <m>
295 int tape_num
= -1; n1
= -1;
296 if (sscanf(pathname
, "tape%d%n", &tape_num
, &n1
) == 1 && tape_num
>= 0 && n1
== len
) {
297 return spt_open(-1, tape_num
, -1);
306 // Like close(). Acts only on handles returned by above function.
307 // (Never called in smartctl!)
308 int deviceclose(int fd
)
310 if ((fd
& 0xff00) == ASPI_FDOFFSET
)
312 else if (fd
>= SPT_FDOFFSET
)
320 // print examples for smartctl
321 void print_smartctl_examples(){
322 printf("=================================================== SMARTCTL EXAMPLES =====\n\n"
323 " smartctl -a /dev/hda (Prints all SMART information)\n\n"
324 #ifdef HAVE_GETOPT_LONG
325 " smartctl --smart=on --offlineauto=on --saveauto=on /dev/hda\n"
326 " (Enables SMART on first disk)\n\n"
327 " smartctl -t long /dev/hda (Executes extended disk self-test)\n\n"
328 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/hda\n"
329 " (Prints Self-Test & Attribute errors)\n"
331 " smartctl -s on -o on -S on /dev/hda (Enables SMART on first disk)\n"
332 " smartctl -t long /dev/hda (Executes extended disk self-test)\n"
333 " smartctl -A -l selftest -q errorsonly /dev/hda\n"
334 " (Prints Self-Test & Attribute errors)\n"
336 " smartctl -a /dev/scsi21\n"
337 " (Prints all information for SCSI disk on ASPI adapter 2, ID 1)\n"
338 " smartctl -a /dev/sda\n"
339 " (Prints all information for SCSI disk on PhysicalDrive 0)\n"
340 " smartctl -a /dev/pd3\n"
341 " (Prints all information for SCSI disk on PhysicalDrive 3)\n"
342 " smartctl -a /dev/tape1\n"
343 " (Prints all information for SCSI tape on Tape 1)\n"
344 " smartctl -A /dev/hdb,3\n"
345 " (Prints Attributes for physical drive 3 on 3ware 9000 RAID)\n"
347 " ATA SMART access methods and ordering may be specified by modifiers\n"
348 " following the device name: /dev/hdX:[saic], where\n"
349 " 's': SMART_* IOCTLs, 'a': IOCTL_ATA_PASS_THROUGH,\n"
350 " 'i': IOCTL_IDE_PASS_THROUGH, 'c': ATA via IOCTL_SCSI_PASS_THROUGH.\n"
351 " The default on this system is /dev/hdX:%s\n", ata_get_def_options()
356 /////////////////////////////////////////////////////////////////////////////
358 /////////////////////////////////////////////////////////////////////////////
360 // SMART_* IOCTLs, also known as DFP_* (Disk Fault Protection)
362 // Deklarations from:
363 // http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ntdddisk.h?rev=1.3
365 #define FILE_READ_ACCESS 0x0001
366 #define FILE_WRITE_ACCESS 0x0002
367 #define METHOD_BUFFERED 0
368 #define CTL_CODE(DeviceType, Function, Method, Access) (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))
370 #define FILE_DEVICE_DISK 7
371 #define IOCTL_DISK_BASE FILE_DEVICE_DISK
373 #define SMART_GET_VERSION \
374 CTL_CODE(IOCTL_DISK_BASE, 0x0020, METHOD_BUFFERED, FILE_READ_ACCESS)
376 #define SMART_SEND_DRIVE_COMMAND \
377 CTL_CODE(IOCTL_DISK_BASE, 0x0021, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
379 #define SMART_RCV_DRIVE_DATA \
380 CTL_CODE(IOCTL_DISK_BASE, 0x0022, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
382 ASSERT_CONST(SMART_GET_VERSION
, 0x074080);
383 ASSERT_CONST(SMART_SEND_DRIVE_COMMAND
, 0x07c084);
384 ASSERT_CONST(SMART_RCV_DRIVE_DATA
, 0x07c088);
386 #define SMART_CYL_LOW 0x4F
387 #define SMART_CYL_HI 0xC2
392 typedef struct _GETVERSIONOUTPARAMS
{
399 } GETVERSIONOUTPARAMS
, *PGETVERSIONOUTPARAMS
, *LPGETVERSIONOUTPARAMS
;
401 ASSERT_SIZEOF(GETVERSIONOUTPARAMS
, 24);
404 #define SMART_VENDOR_3WARE 0x13C1 // identifies 3ware specific parameters
406 typedef struct _GETVERSIONINPARAMS_EX
{
412 DWORD dwDeviceMapEx
; // 3ware specific: RAID drive bit map
413 WORD wIdentifier
; // Vendor specific identifier
414 WORD wControllerId
; // 3ware specific: Controller ID (0,1,...)
416 } GETVERSIONINPARAMS_EX
, *PGETVERSIONINPARAMS_EX
, *LPGETVERSIONINPARAMS_EX
;
418 ASSERT_SIZEOF(GETVERSIONINPARAMS_EX
, sizeof(GETVERSIONOUTPARAMS
));
421 typedef struct _IDEREGS
{
423 UCHAR bSectorCountReg
;
424 UCHAR bSectorNumberReg
;
430 } IDEREGS
, *PIDEREGS
, *LPIDEREGS
;
432 typedef struct _SENDCMDINPARAMS
{
439 } SENDCMDINPARAMS
, *PSENDCMDINPARAMS
, *LPSENDCMDINPARAMS
;
441 ASSERT_SIZEOF(SENDCMDINPARAMS
, 32+1);
443 typedef struct _SENDCMDINPARAMS_EX
{
447 BYTE bPortNumber
; // 3ware specific: port number
448 WORD wIdentifier
; // Vendor specific identifier
451 } SENDCMDINPARAMS_EX
, *PSENDCMDINPARAMS_EX
, *LPSENDCMDINPARAMS_EX
;
453 ASSERT_SIZEOF(SENDCMDINPARAMS_EX
, sizeof(SENDCMDINPARAMS
));
456 /* DRIVERSTATUS.bDriverError constants (just for info, not used)
457 #define SMART_NO_ERROR 0
458 #define SMART_IDE_ERROR 1
459 #define SMART_INVALID_FLAG 2
460 #define SMART_INVALID_COMMAND 3
461 #define SMART_INVALID_BUFFER 4
462 #define SMART_INVALID_DRIVE 5
463 #define SMART_INVALID_IOCTL 6
464 #define SMART_ERROR_NO_MEM 7
465 #define SMART_INVALID_REGISTER 8
466 #define SMART_NOT_SUPPORTED 9
467 #define SMART_NO_IDE_DEVICE 10
470 typedef struct _DRIVERSTATUS
{
475 } DRIVERSTATUS
, *PDRIVERSTATUS
, *LPDRIVERSTATUS
;
477 typedef struct _SENDCMDOUTPARAMS
{
479 DRIVERSTATUS DriverStatus
;
481 } SENDCMDOUTPARAMS
, *PSENDCMDOUTPARAMS
, *LPSENDCMDOUTPARAMS
;
483 ASSERT_SIZEOF(SENDCMDOUTPARAMS
, 16+1);
488 /////////////////////////////////////////////////////////////////////////////
490 static void print_ide_regs(const IDEREGS
* r
, int out
)
492 pout("%s=0x%02x,%s=0x%02x, SC=0x%02x, NS=0x%02x, CL=0x%02x, CH=0x%02x, SEL=0x%02x\n",
493 (out
?"STS":"CMD"), r
->bCommandReg
, (out
?"ERR":" FR"), r
->bFeaturesReg
,
494 r
->bSectorCountReg
, r
->bSectorNumberReg
, r
->bCylLowReg
, r
->bCylHighReg
, r
->bDriveHeadReg
);
497 static void print_ide_regs_io(const IDEREGS
* ri
, const IDEREGS
* ro
)
499 pout(" Input : "); print_ide_regs(ri
, 0);
501 pout(" Output: "); print_ide_regs(ro
, 1);
505 /////////////////////////////////////////////////////////////////////////////
507 // call SMART_GET_VERSION, return device map or -1 on error
509 static int smart_get_version(HANDLE hdevice
, unsigned long * portmap
= 0)
511 GETVERSIONOUTPARAMS vers
;
512 const GETVERSIONINPARAMS_EX
& vers_ex
= (const GETVERSIONINPARAMS_EX
&)vers
;
515 memset(&vers
, 0, sizeof(vers
));
516 if (!DeviceIoControl(hdevice
, SMART_GET_VERSION
,
517 NULL
, 0, &vers
, sizeof(vers
), &num_out
, NULL
)) {
518 if (con
->reportataioctl
)
519 pout(" SMART_GET_VERSION failed, Error=%ld\n", GetLastError());
523 assert(num_out
== sizeof(GETVERSIONOUTPARAMS
));
526 // Return bitmask of valid RAID ports
527 if (vers_ex
.wIdentifier
!= SMART_VENDOR_3WARE
) {
528 pout(" SMART_GET_VERSION returns unknown Identifier = %04x\n"
529 " This is no 3ware 9000 controller or driver has no SMART support.\n", vers_ex
.wIdentifier
);
533 *portmap
= vers_ex
.dwDeviceMapEx
;
536 if (con
->reportataioctl
> 1) {
537 pout(" SMART_GET_VERSION suceeded, bytes returned: %lu\n"
538 " Vers = %d.%d, Caps = 0x%lx, DeviceMap = 0x%02x\n",
539 num_out
, vers
.bVersion
, vers
.bRevision
,
540 vers
.fCapabilities
, vers
.bIDEDeviceMap
);
541 if (vers_ex
.wIdentifier
== SMART_VENDOR_3WARE
)
542 pout(" Identifier = %04x(3WARE), ControllerId=%u, DeviceMapEx = 0x%08lx\n",
543 vers_ex
.wIdentifier
, vers_ex
.wControllerId
, vers_ex
.dwDeviceMapEx
);
546 // TODO: Check vers.fCapabilities here?
547 return vers
.bIDEDeviceMap
;
551 // call SMART_* ioctl
553 static int smart_ioctl(HANDLE hdevice
, int drive
, IDEREGS
* regs
, char * data
, unsigned datasize
, int port
)
555 SENDCMDINPARAMS inpar
;
556 SENDCMDINPARAMS_EX
& inpar_ex
= (SENDCMDINPARAMS_EX
&)inpar
;
558 unsigned char outbuf
[sizeof(SENDCMDOUTPARAMS
)-1 + 512];
559 const SENDCMDOUTPARAMS
* outpar
;
561 unsigned int size_out
;
564 memset(&inpar
, 0, sizeof(inpar
));
565 inpar
.irDriveRegs
= *regs
;
566 // drive is set to 0-3 on Win9x only
567 inpar
.irDriveRegs
.bDriveHeadReg
= 0xA0 | ((drive
& 1) << 4);
568 inpar
.bDriveNumber
= drive
;
572 inpar_ex
.wIdentifier
= SMART_VENDOR_3WARE
;
573 inpar_ex
.bPortNumber
= port
;
576 assert(datasize
== 0 || datasize
== 512);
578 code
= SMART_RCV_DRIVE_DATA
; name
= "SMART_RCV_DRIVE_DATA";
579 inpar
.cBufferSize
= size_out
= 512;
582 code
= SMART_SEND_DRIVE_COMMAND
; name
= "SMART_SEND_DRIVE_COMMAND";
583 if (regs
->bFeaturesReg
== ATA_SMART_STATUS
) {
584 size_out
= sizeof(IDEREGS
); // ioctl returns new IDEREGS as data
585 // Note: cBufferSize must be 0 on Win9x
586 inpar
.cBufferSize
= size_out
;
592 memset(&outbuf
, 0, sizeof(outbuf
));
594 if (!DeviceIoControl(hdevice
, code
, &inpar
, sizeof(SENDCMDINPARAMS
)-1,
595 outbuf
, sizeof(SENDCMDOUTPARAMS
)-1 + size_out
, &num_out
, NULL
)) {
596 // CAUTION: DO NOT change "regs" Parameter in this case, see ata_command_interface()
597 long err
= GetLastError();
598 if (con
->reportataioctl
&& (err
!= ERROR_INVALID_PARAMETER
|| con
->reportataioctl
> 1)) {
599 pout(" %s failed, Error=%ld\n", name
, err
);
600 print_ide_regs_io(regs
, NULL
);
602 errno
= ( err
== ERROR_INVALID_FUNCTION
/*9x*/
603 || err
== ERROR_INVALID_PARAMETER
/*NT/2K/XP*/ ? ENOSYS
: EIO
);
606 // NOTE: On Win9x, inpar.irDriveRegs now contains the returned regs
608 outpar
= (const SENDCMDOUTPARAMS
*)outbuf
;
610 if (outpar
->DriverStatus
.bDriverError
) {
611 if (con
->reportataioctl
) {
612 pout(" %s failed, DriverError=0x%02x, IDEError=0x%02x\n", name
,
613 outpar
->DriverStatus
.bDriverError
, outpar
->DriverStatus
.bIDEError
);
614 print_ide_regs_io(regs
, NULL
);
616 errno
= (!outpar
->DriverStatus
.bIDEError
? ENOSYS
: EIO
);
620 if (con
->reportataioctl
> 1) {
621 pout(" %s suceeded, bytes returned: %lu (buffer %lu)\n", name
,
622 num_out
, outpar
->cBufferSize
);
623 print_ide_regs_io(regs
, (regs
->bFeaturesReg
== ATA_SMART_STATUS
?
624 (const IDEREGS
*)(outpar
->bBuffer
) : NULL
));
628 memcpy(data
, outpar
->bBuffer
, 512);
629 else if (regs
->bFeaturesReg
== ATA_SMART_STATUS
) {
630 if (nonempty(const_cast<unsigned char *>(outpar
->bBuffer
), sizeof(IDEREGS
)))
631 *regs
= *(const IDEREGS
*)(outpar
->bBuffer
);
632 else { // Workaround for driver not returning regs
633 if (con
->reportataioctl
)
634 pout(" WARNING: driver does not return ATA registers in output buffer!\n");
635 *regs
= inpar
.irDriveRegs
;
643 /////////////////////////////////////////////////////////////////////////////
645 // IDE PASS THROUGH (2000, XP, undocumented)
647 // Based on WinATA.cpp, 2002 c't/Matthias Withopf
648 // ftp://ftp.heise.de/pub/ct/listings/0207-218.zip
650 #define FILE_DEVICE_CONTROLLER 4
651 #define IOCTL_SCSI_BASE FILE_DEVICE_CONTROLLER
653 #define IOCTL_IDE_PASS_THROUGH \
654 CTL_CODE(IOCTL_SCSI_BASE, 0x040A, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
656 ASSERT_CONST(IOCTL_IDE_PASS_THROUGH
, 0x04d028);
662 ULONG DataBufferSize
;
666 ASSERT_SIZEOF(ATA_PASS_THROUGH
, 12+1);
671 /////////////////////////////////////////////////////////////////////////////
673 static int ide_pass_through_ioctl(HANDLE hdevice
, IDEREGS
* regs
, char * data
, unsigned datasize
)
675 if (datasize
> 512) {
679 unsigned int size
= sizeof(ATA_PASS_THROUGH
)-1 + datasize
;
680 ATA_PASS_THROUGH
* buf
= (ATA_PASS_THROUGH
*)VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
682 const unsigned char magic
= 0xcf;
690 buf
->DataBufferSize
= datasize
;
692 buf
->DataBuffer
[0] = magic
;
694 if (!DeviceIoControl(hdevice
, IOCTL_IDE_PASS_THROUGH
,
695 buf
, size
, buf
, size
, &num_out
, NULL
)) {
696 long err
= GetLastError();
697 if (con
->reportataioctl
) {
698 pout(" IOCTL_IDE_PASS_THROUGH failed, Error=%ld\n", err
);
699 print_ide_regs_io(regs
, NULL
);
701 VirtualFree(buf
, 0, MEM_RELEASE
);
702 errno
= (err
== ERROR_INVALID_FUNCTION
? ENOSYS
: EIO
);
707 if (buf
->IdeReg
.bCommandReg
/*Status*/ & 0x01) {
708 if (con
->reportataioctl
) {
709 pout(" IOCTL_IDE_PASS_THROUGH command failed:\n");
710 print_ide_regs_io(regs
, &buf
->IdeReg
);
712 VirtualFree(buf
, 0, MEM_RELEASE
);
717 // Check and copy data
720 || (buf
->DataBuffer
[0] == magic
&& !nonempty(buf
->DataBuffer
+1, datasize
-1))) {
721 if (con
->reportataioctl
) {
722 pout(" IOCTL_IDE_PASS_THROUGH output data missing (%lu, %lu)\n",
723 num_out
, buf
->DataBufferSize
);
724 print_ide_regs_io(regs
, &buf
->IdeReg
);
726 VirtualFree(buf
, 0, MEM_RELEASE
);
730 memcpy(data
, buf
->DataBuffer
, datasize
);
733 if (con
->reportataioctl
> 1) {
734 pout(" IOCTL_IDE_PASS_THROUGH suceeded, bytes returned: %lu (buffer %lu)\n",
735 num_out
, buf
->DataBufferSize
);
736 print_ide_regs_io(regs
, &buf
->IdeReg
);
740 // Caution: VirtualFree() fails if parameter "dwSize" is nonzero
741 VirtualFree(buf
, 0, MEM_RELEASE
);
746 /////////////////////////////////////////////////////////////////////////////
748 // ATA PASS THROUGH (Win2003, XP SP2)
750 #define IOCTL_ATA_PASS_THROUGH \
751 CTL_CODE(IOCTL_SCSI_BASE, 0x040B, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
753 ASSERT_CONST(IOCTL_ATA_PASS_THROUGH
, 0x04d02c);
755 typedef struct _ATA_PASS_THROUGH_EX
{
761 UCHAR ReservedAsUchar
;
762 ULONG DataTransferLength
;
764 ULONG ReservedAsUlong
;
765 ULONG
/*_PTR*/ DataBufferOffset
;
766 UCHAR PreviousTaskFile
[8];
767 UCHAR CurrentTaskFile
[8];
768 } ATA_PASS_THROUGH_EX
, *PATA_PASS_THROUGH_EX
;
770 ASSERT_SIZEOF(ATA_PASS_THROUGH_EX
, 40);
772 #define ATA_FLAGS_DRDY_REQUIRED 0x01
773 #define ATA_FLAGS_DATA_IN 0x02
774 #define ATA_FLAGS_DATA_OUT 0x04
775 #define ATA_FLAGS_48BIT_COMMAND 0x08
778 /////////////////////////////////////////////////////////////////////////////
780 static int ata_pass_through_ioctl(HANDLE hdevice
, IDEREGS
* regs
, char * data
, int datasize
)
783 ATA_PASS_THROUGH_EX apt
;
785 UCHAR ucDataBuf
[512];
786 } ATA_PASS_THROUGH_EX_WITH_BUFFERS
;
788 const unsigned char magic
= 0xcf;
790 ATA_PASS_THROUGH_EX_WITH_BUFFERS ab
; memset(&ab
, 0, sizeof(ab
));
791 ab
.apt
.Length
= sizeof(ATA_PASS_THROUGH_EX
);
793 //ab.apt.TargetId = 0;
795 ab
.apt
.TimeOutValue
= 10;
796 unsigned size
= offsetof(ATA_PASS_THROUGH_EX_WITH_BUFFERS
, ucDataBuf
);
797 ab
.apt
.DataBufferOffset
= size
;
800 if (datasize
> (int)sizeof(ab
.ucDataBuf
)) {
804 ab
.apt
.AtaFlags
= ATA_FLAGS_DATA_IN
;
805 ab
.apt
.DataTransferLength
= datasize
;
807 ab
.ucDataBuf
[0] = magic
;
809 else if (datasize
< 0) {
810 if (-datasize
> (int)sizeof(ab
.ucDataBuf
)) {
814 ab
.apt
.AtaFlags
= ATA_FLAGS_DATA_OUT
;
815 ab
.apt
.DataTransferLength
= -datasize
;
817 memcpy(ab
.ucDataBuf
, data
, -datasize
);
820 assert(ab
.apt
.AtaFlags
== 0);
821 assert(ab
.apt
.DataTransferLength
== 0);
824 assert(sizeof(ab
.apt
.CurrentTaskFile
) == sizeof(IDEREGS
));
825 IDEREGS
* ctfregs
= (IDEREGS
*)ab
.apt
.CurrentTaskFile
;
829 if (!DeviceIoControl(hdevice
, IOCTL_ATA_PASS_THROUGH
,
830 &ab
, size
, &ab
, size
, &num_out
, NULL
)) {
831 long err
= GetLastError();
832 if (con
->reportataioctl
) {
833 pout(" IOCTL_ATA_PASS_THROUGH failed, Error=%ld\n", err
);
834 print_ide_regs_io(regs
, NULL
);
836 errno
= (err
== ERROR_INVALID_FUNCTION
? ENOSYS
: EIO
);
841 if (ctfregs
->bCommandReg
/*Status*/ & 0x01) {
842 if (con
->reportataioctl
) {
843 pout(" IOCTL_ATA_PASS_THROUGH command failed:\n");
844 print_ide_regs_io(regs
, ctfregs
);
850 // Check and copy data
853 || (ab
.ucDataBuf
[0] == magic
&& !nonempty(ab
.ucDataBuf
+1, datasize
-1))) {
854 if (con
->reportataioctl
) {
855 pout(" IOCTL_ATA_PASS_THROUGH output data missing (%lu)\n", num_out
);
856 print_ide_regs_io(regs
, ctfregs
);
861 memcpy(data
, ab
.ucDataBuf
, datasize
);
864 if (con
->reportataioctl
> 1) {
865 pout(" IOCTL_ATA_PASS_THROUGH suceeded, bytes returned: %lu\n", num_out
);
866 print_ide_regs_io(regs
, ctfregs
);
874 /////////////////////////////////////////////////////////////////////////////
876 // ATA PASS THROUGH via SCSI PASS THROUGH (WinNT4 only)
878 // Declarations from:
879 // http://cvs.sourceforge.net/viewcvs.py/mingw/w32api/include/ddk/ntddscsi.h?rev=1.2
881 #define IOCTL_SCSI_PASS_THROUGH \
882 CTL_CODE(IOCTL_SCSI_BASE, 0x0401, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
884 ASSERT_CONST(IOCTL_SCSI_PASS_THROUGH
, 0x04d004);
886 #define SCSI_IOCTL_DATA_OUT 0
887 #define SCSI_IOCTL_DATA_IN 1
888 #define SCSI_IOCTL_DATA_UNSPECIFIED 2
889 // undocumented SCSI opcode to for ATA passthrough
890 #define SCSIOP_ATA_PASSTHROUGH 0xCC
892 typedef struct _SCSI_PASS_THROUGH
{
899 UCHAR SenseInfoLength
;
901 ULONG DataTransferLength
;
903 ULONG
/*_PTR*/ DataBufferOffset
;
904 ULONG SenseInfoOffset
;
906 } SCSI_PASS_THROUGH
, *PSCSI_PASS_THROUGH
;
908 ASSERT_SIZEOF(SCSI_PASS_THROUGH
, 44);
911 /////////////////////////////////////////////////////////////////////////////
913 static int ata_via_scsi_pass_through_ioctl(HANDLE hdevice
, IDEREGS
* regs
, char * data
, unsigned datasize
)
916 SCSI_PASS_THROUGH spt
;
918 UCHAR ucSenseBuf
[32];
919 UCHAR ucDataBuf
[512];
920 } SCSI_PASS_THROUGH_WITH_BUFFERS
;
922 SCSI_PASS_THROUGH_WITH_BUFFERS sb
;
926 const unsigned char magic
= 0xcf;
928 memset(&sb
, 0, sizeof(sb
));
929 sb
.spt
.Length
= sizeof(SCSI_PASS_THROUGH
);
933 sb
.spt
.CdbLength
= 10; sb
.spt
.SenseInfoLength
= 24;
934 sb
.spt
.TimeOutValue
= 10;
935 sb
.spt
.SenseInfoOffset
= offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS
, ucSenseBuf
);
936 size
= offsetof(SCSI_PASS_THROUGH_WITH_BUFFERS
, ucDataBuf
);
937 sb
.spt
.DataBufferOffset
= size
;
940 if (datasize
> sizeof(sb
.ucDataBuf
)) {
944 sb
.spt
.DataIn
= SCSI_IOCTL_DATA_IN
;
945 sb
.spt
.DataTransferLength
= datasize
;
947 sb
.ucDataBuf
[0] = magic
;
950 sb
.spt
.DataIn
= SCSI_IOCTL_DATA_UNSPECIFIED
;
951 //sb.spt.DataTransferLength = 0;
954 // Use pseudo SCSI command followed by registers
955 sb
.spt
.Cdb
[0] = SCSIOP_ATA_PASSTHROUGH
;
956 cdbregs
= (IDEREGS
*)(sb
.spt
.Cdb
+2);
959 if (!DeviceIoControl(hdevice
, IOCTL_SCSI_PASS_THROUGH
,
960 &sb
, size
, &sb
, size
, &num_out
, NULL
)) {
961 long err
= GetLastError();
962 if (con
->reportataioctl
)
963 pout(" ATA via IOCTL_SCSI_PASS_THROUGH failed, Error=%ld\n", err
);
964 errno
= (err
== ERROR_INVALID_FUNCTION
? ENOSYS
: EIO
);
968 // Cannot check ATA status, because command does not return IDEREGS
970 // Check and copy data
973 || (sb
.ucDataBuf
[0] == magic
&& !nonempty(sb
.ucDataBuf
+1, datasize
-1))) {
974 if (con
->reportataioctl
) {
975 pout(" ATA via IOCTL_SCSI_PASS_THROUGH output data missing (%lu)\n", num_out
);
976 print_ide_regs_io(regs
, NULL
);
981 memcpy(data
, sb
.ucDataBuf
, datasize
);
984 if (con
->reportataioctl
> 1) {
985 pout(" ATA via IOCTL_SCSI_PASS_THROUGH suceeded, bytes returned: %lu\n", num_out
);
986 print_ide_regs_io(regs
, NULL
);
992 /////////////////////////////////////////////////////////////////////////////
994 // ATA PASS THROUGH via 3ware specific SCSI MINIPORT ioctl
996 #define IOCTL_SCSI_MINIPORT \
997 CTL_CODE(IOCTL_SCSI_BASE, 0x0402, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
999 ASSERT_CONST(IOCTL_SCSI_MINIPORT
, 0x04d008);
1001 typedef struct _SRB_IO_CONTROL
{
1008 } SRB_IO_CONTROL
, *PSRB_IO_CONTROL
;
1010 ASSERT_SIZEOF(SRB_IO_CONTROL
, 28);
1012 /////////////////////////////////////////////////////////////////////////////
1014 static int ata_via_3ware_miniport_ioctl(HANDLE hdevice
, IDEREGS
* regs
, char * data
, int datasize
, int port
)
1017 SRB_IO_CONTROL srbc
;
1021 ASSERT_SIZEOF(sb
, sizeof(SRB_IO_CONTROL
)+sizeof(IDEREGS
)+512);
1023 if (!(0 <= datasize
&& datasize
<= (int)sizeof(sb
.buffer
) && port
>= 0)) {
1027 memset(&sb
, 0, sizeof(sb
));
1028 strcpy((char *)sb
.srbc
.Signature
, "<3ware>");
1029 sb
.srbc
.HeaderLength
= sizeof(SRB_IO_CONTROL
);
1030 sb
.srbc
.Timeout
= 60; // seconds
1031 sb
.srbc
.ControlCode
= 0xA0000000;
1032 sb
.srbc
.ReturnCode
= 0;
1033 sb
.srbc
.Length
= sizeof(IDEREGS
) + (datasize
> 0 ? datasize
: 1);
1035 sb
.regs
.bReserved
= port
;
1038 if (!DeviceIoControl(hdevice
, IOCTL_SCSI_MINIPORT
,
1039 &sb
, sizeof(sb
), &sb
, sizeof(sb
), &num_out
, NULL
)) {
1040 long err
= GetLastError();
1041 if (con
->reportataioctl
) {
1042 pout(" ATA via IOCTL_SCSI_MINIPORT failed, Error=%ld\n", err
);
1043 print_ide_regs_io(regs
, NULL
);
1045 errno
= (err
== ERROR_INVALID_FUNCTION
? ENOSYS
: EIO
);
1049 if (sb
.srbc
.ReturnCode
) {
1050 if (con
->reportataioctl
) {
1051 pout(" ATA via IOCTL_SCSI_MINIPORT failed, ReturnCode=0x%08lx\n", sb
.srbc
.ReturnCode
);
1052 print_ide_regs_io(regs
, NULL
);
1060 memcpy(data
, sb
.buffer
, datasize
);
1062 if (con
->reportataioctl
> 1) {
1063 pout(" ATA via IOCTL_SCSI_MINIPORT suceeded, bytes returned: %lu\n", num_out
);
1064 print_ide_regs_io(regs
, &sb
.regs
);
1072 /////////////////////////////////////////////////////////////////////////////
1074 // 3ware specific call to update the devicemap returned by SMART_GET_VERSION.
1075 // 3DM/CLI "Rescan Controller" function does not to always update it.
1077 static int update_3ware_devicemap_ioctl(HANDLE hdevice
)
1079 SRB_IO_CONTROL srbc
;
1080 memset(&srbc
, 0, sizeof(srbc
));
1081 strcpy((char *)srbc
.Signature
, "<3ware>");
1082 srbc
.HeaderLength
= sizeof(SRB_IO_CONTROL
);
1083 srbc
.Timeout
= 60; // seconds
1084 srbc
.ControlCode
= 0xCC010014;
1085 srbc
.ReturnCode
= 0;
1089 if (!DeviceIoControl(hdevice
, IOCTL_SCSI_MINIPORT
,
1090 &srbc
, sizeof(srbc
), &srbc
, sizeof(srbc
), &num_out
, NULL
)) {
1091 long err
= GetLastError();
1092 if (con
->reportataioctl
)
1093 pout(" UPDATE DEVICEMAP via IOCTL_SCSI_MINIPORT failed, Error=%ld\n", err
);
1094 errno
= (err
== ERROR_INVALID_FUNCTION
? ENOSYS
: EIO
);
1097 if (srbc
.ReturnCode
) {
1098 if (con
->reportataioctl
)
1099 pout(" UPDATE DEVICEMAP via IOCTL_SCSI_MINIPORT failed, ReturnCode=0x%08lx\n", srbc
.ReturnCode
);
1103 if (con
->reportataioctl
> 1)
1104 pout(" UPDATE DEVICEMAP via IOCTL_SCSI_MINIPORT suceeded\n");
1109 /////////////////////////////////////////////////////////////////////////////
1111 // Call GetDevicePowerState() if available (Win98/ME/2000/XP/2003)
1112 // returns: 1=active, 0=standby, -1=error
1113 // (This would also work for SCSI drives)
1115 static int get_device_power_state(HANDLE hdevice
)
1117 static HINSTANCE h_kernel_dll
= 0;
1119 static DWORD kernel_dll_pid
= 0;
1121 static BOOL (WINAPI
* GetDevicePowerState_p
)(HANDLE
, BOOL
*) = 0;
1125 if (!GetDevicePowerState_p
1127 || kernel_dll_pid
!= GetCurrentProcessId() // detect fork()
1130 if (h_kernel_dll
== INVALID_HANDLE_VALUE
) {
1134 if (!(h_kernel_dll
= LoadLibraryA("KERNEL32.DLL"))) {
1135 pout("Cannot load KERNEL32.DLL, Error=%ld\n", GetLastError());
1136 h_kernel_dll
= (HINSTANCE
)INVALID_HANDLE_VALUE
;
1140 if (!(GetDevicePowerState_p
= (BOOL (WINAPI
*)(HANDLE
, BOOL
*))
1141 GetProcAddress(h_kernel_dll
, "GetDevicePowerState"))) {
1142 if (con
->reportataioctl
)
1143 pout(" GetDevicePowerState() not found, Error=%ld\n", GetLastError());
1144 FreeLibrary(h_kernel_dll
);
1145 h_kernel_dll
= (HINSTANCE
)INVALID_HANDLE_VALUE
;
1150 kernel_dll_pid
= GetCurrentProcessId();
1154 if (!GetDevicePowerState_p(hdevice
, &state
)) {
1155 long err
= GetLastError();
1156 if (con
->reportataioctl
)
1157 pout(" GetDevicePowerState() failed, Error=%ld\n", err
);
1158 errno
= (err
== ERROR_INVALID_FUNCTION
? ENOSYS
: EIO
);
1159 // TODO: This may not work as expected on transient errors,
1160 // because smartd interprets -1 as SLEEP mode regardless of errno.
1164 if (con
->reportataioctl
> 1)
1165 pout(" GetDevicePowerState() succeeded, state=%d\n", state
);
1170 /////////////////////////////////////////////////////////////////////////////
1172 // TODO: Put in a struct indexed by fd (or better a C++ object of course ;-)
1173 static HANDLE h_ata_ioctl
= 0;
1174 static const char * ata_def_options
;
1175 static char * ata_cur_options
;
1176 static int ata_driveno
; // Drive number
1177 static char ata_smartver_state
[10]; // SMART_GET_VERSION: 0=unknown, 1=OK, 2=failed
1179 // Print SMARTVSD error message, return errno
1181 static int smartvsd_error()
1183 char path
[MAX_PATH
];
1185 if (!(5 <= (len
= GetSystemDirectoryA(path
, MAX_PATH
)) && len
< MAX_PATH
/2))
1187 // SMARTVSD.VXD present?
1188 strcpy(path
+len
, "\\IOSUBSYS\\SMARTVSD.VXD");
1189 if (!access(path
, 0)) {
1190 // Yes, standard IDE driver used?
1192 if ( (h
= CreateFileA("\\\\.\\ESDI_506",
1193 GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1194 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
1195 && GetLastError() == ERROR_FILE_NOT_FOUND
) {
1196 pout("Standard IDE driver ESDI_506.PDR not used, or no IDE/ATA drives present.\n");
1200 if (h
!= INVALID_HANDLE_VALUE
) // should not happen
1202 pout("SMART driver SMARTVSD.VXD is installed, but not loaded.\n");
1207 strcpy(path
+len
, "\\SMARTVSD.VXD");
1208 if (!access(path
, 0)) {
1209 // Some Windows versions install SMARTVSD.VXD in SYSTEM directory
1210 // (http://support.microsoft.com/kb/265854/en-us).
1212 pout("SMART driver is not properly installed,\n"
1213 " move SMARTVSD.VXD from \"%s\" to \"%s\\IOSUBSYS\"\n"
1214 " and reboot Windows.\n", path
, path
);
1217 // Some Windows versions do not provide SMARTVSD.VXD
1218 // (http://support.microsoft.com/kb/199886/en-us).
1220 pout("SMARTVSD.VXD is missing in folder \"%s\\IOSUBSYS\".\n", path
);
1227 // Get default ATA device options
1229 static const char * ata_get_def_options()
1231 DWORD ver
= GetVersion();
1232 if ((ver
& 0x80000000) || (ver
& 0xff) < 4) // Win9x/ME
1233 return "s"; // SMART_* only
1234 else if ((ver
& 0xff) == 4) // WinNT4
1235 return "sc"; // SMART_*, SCSI_PASS_THROUGH
1236 else // WinXP, 2003, Vista
1237 return "psai"; // GetDevicePowerState(), SMART_*, ATA_, IDE_PASS_THROUGH
1243 static int ata_open(int drive
, const char * options
, int port
)
1249 // TODO: This version does not allow to open more than 1 ATA devices
1255 win9x
= ((GetVersion() & 0x80000000) != 0);
1257 if (!(0 <= drive
&& drive
<= (win9x
? 7 : 9))) {
1262 // path depends on Windows Version
1264 // Use patched "smartvse.vxd" for drives 4-7, see INSTALL file for details
1265 strcpy(devpath
, (drive
<= 3 ? "\\\\.\\SMARTVSD" : "\\\\.\\SMARTVSE"));
1267 snprintf(devpath
, sizeof(devpath
)-1, "\\\\.\\PhysicalDrive%d", drive
);
1270 if ((h_ata_ioctl
= CreateFileA(devpath
,
1271 GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1272 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
) {
1273 long err
= GetLastError();
1274 pout("Cannot open device %s, Error=%ld\n", devpath
, err
);
1275 if (err
== ERROR_FILE_NOT_FOUND
)
1276 errno
= (win9x
&& drive
<= 3 ? smartvsd_error() : ENOENT
);
1277 else if (err
== ERROR_ACCESS_DENIED
) {
1279 pout("Administrator rights are necessary to access physical drives.\n");
1288 if (con
->reportataioctl
> 1)
1289 pout("%s: successfully opened\n", devpath
);
1293 // Set default options according to Windows version
1294 if (!ata_def_options
)
1295 ata_def_options
= ata_get_def_options();
1296 options
= (port
< 0 ? ata_def_options
: "s3"); // RAID: SMART_* and SCSI_MINIPORT
1298 ata_cur_options
= strdup(options
);
1300 // NT4/2000/XP: SMART_GET_VERSION may spin up disk, so delay until first real SMART_* call
1301 ata_driveno
= drive
;
1302 if (!win9x
&& port
< 0)
1305 // Win9X/ME: Get drive map
1306 // RAID: Get port map
1307 unsigned long portmap
= 0;
1308 devmap
= smart_get_version(h_ata_ioctl
, (port
>= 0 ? &portmap
: 0));
1310 if (!is_permissive()) {
1317 ata_smartver_state
[drive
] = 1;
1320 // 3ware RAID: update devicemap first
1321 if (!update_3ware_devicemap_ioctl(h_ata_ioctl
)) {
1322 unsigned long portmap1
= 0;
1323 if (smart_get_version(h_ata_ioctl
, &portmap1
) >= 0)
1326 // Check port existence
1327 if (!(portmap
& (1L << port
))) {
1328 pout("%s: Port %d is empty or does not exist\n", devpath
, port
);
1329 if (!is_permissive()) {
1335 // Encode port into pseudo fd
1336 return (ATARAID_FDOFFSET
| port
);
1339 // Win9x/ME: Check device presence & type
1340 if (((devmap
>> (drive
& 0x3)) & 0x11) != 0x01) {
1341 unsigned char atapi
= (devmap
>> (drive
& 0x3)) & 0x10;
1342 pout("%s: Drive %d %s (IDEDeviceMap=0x%02x).\n", devpath
,
1343 drive
, (atapi
?"is an ATAPI device":"does not exist"), devmap
);
1344 // Win9x drive existence check may not work as expected
1345 // The atapi.sys driver incorrectly fills in the bIDEDeviceMap with 0x01
1346 // (The related KB Article Q196120 is no longer available)
1347 if (!is_permissive()) {
1349 errno
= (atapi
? ENOSYS
: ENOENT
);
1353 // Use drive number as fd for ioctl
1354 return (drive
& 0x3);
1358 static void ata_close(int fd
)
1361 CloseHandle(h_ata_ioctl
);
1363 if (ata_cur_options
) {
1364 free(ata_cur_options
);
1365 ata_cur_options
= 0;
1370 // Scan for ATA drives, fill bitmask of drives present, return #drives
1372 static int ata_scan(unsigned long * drives
, int * rdriveno
, unsigned long * rdrives
)
1374 int win9x
= ((GetVersion() & 0x80000000) != 0);
1377 for (i
= 0; i
<= 9; i
++) {
1379 GETVERSIONOUTPARAMS vers
;
1380 const GETVERSIONINPARAMS_EX
& vers_ex
= (const GETVERSIONINPARAMS_EX
&)vers
;
1384 strcpy(devpath
, "\\\\.\\SMARTVSD");
1386 snprintf(devpath
, sizeof(devpath
)-1, "\\\\.\\PhysicalDrive%d", i
);
1389 if ((h
= CreateFileA(devpath
,
1390 GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
1391 NULL
, OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
) {
1392 if (con
->reportataioctl
> 1)
1393 pout(" %s: Open failed, Error=%ld\n", devpath
, GetLastError());
1395 break; // SMARTVSD.VXD missing or no ATA devices
1396 continue; // Disk not found or access denied (break;?)
1400 memset(&vers
, 0, sizeof(vers
));
1401 if (!DeviceIoControl(h
, SMART_GET_VERSION
,
1402 NULL
, 0, &vers
, sizeof(vers
), &num_out
, NULL
)) {
1403 if (con
->reportataioctl
)
1404 pout(" %s: SMART_GET_VERSION failed, Error=%ld\n", devpath
, GetLastError());
1407 break; // Should not happen
1408 continue; // Non ATA disk or no SMART ioctl support (possibly SCSI disk)
1412 if (con
->reportataioctl
) {
1413 pout(" %s: SMART_GET_VERSION (%ld bytes):\n"
1414 " Vers = %d.%d, Caps = 0x%lx, DeviceMap = 0x%02x\n",
1415 devpath
, num_out
, vers
.bVersion
, vers
.bRevision
,
1416 vers
.fCapabilities
, vers
.bIDEDeviceMap
);
1417 if (vers_ex
.wIdentifier
== SMART_VENDOR_3WARE
)
1418 pout(" Identifier = %04x(3WARE), ControllerId=%u, DeviceMapEx = 0x%08lx\n",
1419 vers_ex
.wIdentifier
, vers_ex
.wControllerId
, vers_ex
.dwDeviceMapEx
);
1423 // Check ATA device presence, remove ATAPI devices
1424 drives
[0] = (vers
.bIDEDeviceMap
& 0xf) & ~((vers
.bIDEDeviceMap
>> 4) & 0xf);
1425 cnt
= (drives
[0]&1) + ((drives
[0]>>1)&1) + ((drives
[0]>>2)&1) + ((drives
[0]>>3)&1);
1429 if (vers_ex
.wIdentifier
== SMART_VENDOR_3WARE
) {
1430 // Skip if more than 2 controllers or logical drive from this controller already seen
1431 if (vers_ex
.wControllerId
>= 2 || rdriveno
[vers_ex
.wControllerId
] >= 0)
1433 assert(rdrives
[vers_ex
.wControllerId
] == 0);
1434 // Count physical drives
1436 for (int pi
= 0; pi
< 32; pi
++) {
1437 if (vers_ex
.dwDeviceMapEx
& (1L << pi
))
1441 continue; // Should not happen
1442 rdrives
[vers_ex
.wControllerId
] = vers_ex
.dwDeviceMapEx
;
1443 rdriveno
[vers_ex
.wControllerId
] = i
;
1447 // ATA drive exists and driver supports SMART ioctl
1448 drives
[0] |= (1L << i
);
1456 /////////////////////////////////////////////////////////////////////////////
1458 // Interface to ATA devices. See os_linux.c
1459 int ata_command_interface(int fd
, smart_command_set command
, int select
, char * data
)
1462 if ((fd
& ~0x1f) == ATARAID_FDOFFSET
) {
1463 // RAID Port encoded into pseudo fd
1468 if (!(0 <= fd
&& fd
<= 3)) {
1473 // CMD,CYL default to SMART, changed by P?IDENTIFY and CHECK_POWER_MODE
1474 IDEREGS regs
; memset(®s
, 0, sizeof(regs
));
1475 regs
.bCommandReg
= ATA_SMART_CMD
;
1476 regs
.bCylHighReg
= SMART_CYL_HI
; regs
.bCylLowReg
= SMART_CYL_LOW
;
1479 // Try all IOCTLS by default: SMART_*, ATA_, IDE_, SCSI_PASS_THROUGH
1480 const char * valid_options
= "saic";
1483 case CHECK_POWER_MODE
:
1484 // Not a SMART command, needs IDE register return
1485 regs
.bCommandReg
= ATA_CHECK_POWER_MODE
;
1486 regs
.bCylLowReg
= regs
.bCylHighReg
= 0;
1487 valid_options
= "pai3"; // Try GetDevicePowerState() first, ATA/IDE_PASS_THROUGH may spin up disk
1488 // Note: returns SectorCountReg in data[0]
1491 regs
.bFeaturesReg
= ATA_SMART_READ_VALUES
;
1492 regs
.bSectorNumberReg
= regs
.bSectorCountReg
= 1;
1495 case READ_THRESHOLDS
:
1496 regs
.bFeaturesReg
= ATA_SMART_READ_THRESHOLDS
;
1497 regs
.bSectorNumberReg
= regs
.bSectorCountReg
= 1;
1501 regs
.bFeaturesReg
= ATA_SMART_READ_LOG_SECTOR
;
1502 regs
.bSectorNumberReg
= select
;
1503 regs
.bSectorCountReg
= 1;
1504 valid_options
= "saic3";
1505 // Note: SMART_RCV_DRIVE_DATA supports this only on Win9x/ME
1509 regs
.bFeaturesReg
= ATA_SMART_WRITE_LOG_SECTOR
;
1510 regs
.bSectorNumberReg
= select
;
1511 regs
.bSectorCountReg
= 1;
1512 valid_options
= "a"; // ATA_PASS_THROUGH only, others don't support DATA_OUT
1513 datasize
= -512; // DATA_OUT!
1516 // Note: WinNT4/2000/XP return identify data cached during boot
1517 // (true for SMART_RCV_DRIVE_DATA and IOCTL_IDE_PASS_THROUGH)
1518 regs
.bCommandReg
= ATA_IDENTIFY_DEVICE
;
1519 regs
.bCylLowReg
= regs
.bCylHighReg
= 0;
1520 regs
.bSectorCountReg
= 1;
1524 regs
.bCommandReg
= ATA_IDENTIFY_PACKET_DEVICE
;
1525 regs
.bCylLowReg
= regs
.bCylHighReg
= 0;
1526 regs
.bSectorCountReg
= 1;
1530 regs
.bFeaturesReg
= ATA_SMART_ENABLE
;
1531 regs
.bSectorNumberReg
= 1;
1534 regs
.bFeaturesReg
= ATA_SMART_DISABLE
;
1535 regs
.bSectorNumberReg
= 1;
1538 valid_options
= "sai"; // Needs IDE register return
1540 regs
.bFeaturesReg
= ATA_SMART_STATUS
;
1543 regs
.bFeaturesReg
= ATA_SMART_AUTO_OFFLINE
;
1544 regs
.bSectorCountReg
= select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1547 regs
.bFeaturesReg
= ATA_SMART_AUTOSAVE
;
1548 regs
.bSectorCountReg
= select
; // YET NOTE - THIS IS A NON-DATA COMMAND!!
1550 case IMMEDIATE_OFFLINE
:
1551 regs
.bFeaturesReg
= ATA_SMART_IMMEDIATE_OFFLINE
;
1552 regs
.bSectorNumberReg
= select
;
1553 valid_options
= "saic3";
1554 // Note: SMART_SEND_DRIVE_COMMAND supports ABORT_SELF_TEST only on Win9x/ME
1557 pout("Unrecognized command %d in win32_ata_command_interface()\n"
1558 "Please contact " PACKAGE_BUGREPORT
"\n", command
);
1563 // Try all valid ioctls in the order specified in dev_ioctls;
1564 bool powered_up
= false;
1565 for (int i
= 0; ; i
++) {
1566 char opt
= ata_cur_options
[i
];
1569 if (command
== CHECK_POWER_MODE
&& powered_up
) {
1570 // Power up reported by GetDevicePowerState() and no ioctl available
1571 // to detect the actual mode of the drive => simulate ATA result ACTIVE/IDLE.
1572 regs
.bSectorCountReg
= 0xff;
1579 if (!strchr(valid_options
, opt
))
1580 // Invalid for this command
1584 assert(datasize
== 0 || datasize
== 512 || (opt
== 'a' && datasize
== -512));
1589 // call SMART_GET_VERSION once for each drive
1590 assert(0 <= ata_driveno
&& ata_driveno
< sizeof(ata_smartver_state
));
1591 if (ata_smartver_state
[ata_driveno
] > 1) {
1592 rc
= -1; errno
= ENOSYS
;
1595 if (!ata_smartver_state
[ata_driveno
]) {
1597 if (smart_get_version(h_ata_ioctl
) < 0) {
1598 if (!con
->permissive
) {
1599 pout("ATA/SATA driver is possibly a SCSI class driver not supporting SMART.\n");
1600 pout("If this is a SCSI disk, try \"scsi<adapter><id>\".\n");
1601 ata_smartver_state
[ata_driveno
] = 2;
1602 rc
= -1; errno
= ENOSYS
;
1607 ata_smartver_state
[ata_driveno
] = 1;
1609 rc
= smart_ioctl(h_ata_ioctl
, fd
, ®s
, data
, datasize
, port
);
1612 rc
= ata_pass_through_ioctl(h_ata_ioctl
, ®s
, data
, datasize
);
1615 rc
= ide_pass_through_ioctl(h_ata_ioctl
, ®s
, data
, datasize
);
1618 rc
= ata_via_scsi_pass_through_ioctl(h_ata_ioctl
, ®s
, data
, datasize
);
1621 rc
= ata_via_3ware_miniport_ioctl(h_ata_ioctl
, ®s
, data
, datasize
, port
);
1624 assert(command
== CHECK_POWER_MODE
&& datasize
== 0);
1625 rc
= get_device_power_state(h_ata_ioctl
);
1627 // Power down reported by GetDevicePowerState(), using a passthrough ioctl would
1628 // spin up the drive => simulate ATA result STANDBY.
1629 regs
.bSectorCountReg
= 0x00;
1632 // Power up reported by GetDevicePowerState(), but this reflects the actual mode
1633 // only if it is selected by the device driver => try a passthrough ioctl to get the
1634 // actual mode, if none available simulate ACTIVE/IDLE.
1636 errno
= ENOSYS
; rc
= -1;
1642 // Working ioctl found
1645 if (errno
!= ENOSYS
)
1646 // Abort on I/O error
1649 // CAUTION: *_ioctl() MUST NOT change "regs" Parameter in the ENOSYS case
1653 case CHECK_POWER_MODE
:
1654 // Return power mode from SectorCountReg in data[0]
1655 data
[0] = regs
.bSectorCountReg
;
1659 // Cyl low and Cyl high unchanged means "Good SMART status"
1660 if (regs
.bCylHighReg
== SMART_CYL_HI
&& regs
.bCylLowReg
== SMART_CYL_LOW
)
1663 // These values mean "Bad SMART status"
1664 if (regs
.bCylHighReg
== 0x2c && regs
.bCylLowReg
== 0xf4)
1667 // We haven't gotten output that makes sense; print out some debugging info
1668 syserror("Error SMART Status command failed");
1669 pout("Please get assistance from %s\n", PACKAGE_HOMEPAGE
);
1670 print_ide_regs(®s
, 1);
1681 #ifndef HAVE_ATA_IDENTIFY_IS_CACHED
1682 #error define of HAVE_ATA_IDENTIFY_IS_CACHED missing in config.h
1685 // Return true if OS caches the ATA identify sector
1686 int ata_identify_is_cached(int fd
)
1688 // Not RAID and WinNT4/2000/XP => true, RAID or Win9x/ME => false
1689 return (!(fd
& 0xff00) && (GetVersion() & 0x80000000) == 0);
1693 // Print not implemeted warning once
1694 static void pr_not_impl(const char * what
, int * warned
)
1699 "#######################################################################\n"
1701 "NOT IMPLEMENTED under Win32.\n"
1702 "Please contact " PACKAGE_BUGREPORT
" if\n"
1703 "you want to help in porting smartmontools to Win32.\n"
1704 "#######################################################################\n"
1710 // Interface to ATA devices behind 3ware escalade RAID controller cards. See os_linux.c
1711 int escalade_command_interface(int fd
, int disknum
, int escalade_type
, smart_command_set command
, int select
, char *data
)
1713 static int warned
= 0;
1714 ARGUSED(fd
); ARGUSED(escalade_type
); ARGUSED(command
); ARGUSED(select
); ARGUSED(data
);
1716 pout("Option '-d 3ware,%d' does not work on Windows.\n"
1717 "Controller port can be specified in the device name: '/dev/hd%c,%d'.\n\n",
1718 disknum
, 'a'+ata_driveno
, disknum
);
1725 // Interface to ATA devices behind Marvell chip-set based controllers. See os_linux.c
1726 int marvell_command_interface(int fd
, smart_command_set command
, int select
, char * data
)
1728 static int warned
= 0;
1729 ARGUSED(fd
); ARGUSED(command
); ARGUSED(select
); ARGUSED(data
);
1730 pr_not_impl("Marvell chip-set command routine marvell_command_interface()", &warned
);
1735 // Interface to ATA devices behind HighPoint Raid controllers. See os_linux.c
1736 int highpoint_command_interface(int fd
, smart_command_set command
, int select
, char * data
)
1738 static int warned
= 0;
1739 ARGUSED(fd
); ARGUSED(command
); ARGUSED(select
); ARGUSED(data
);
1740 pr_not_impl("HighPoint raid controller command routine highpoint_command_interface()", &warned
);
1746 /////////////////////////////////////////////////////////////////////////////
1747 // ASPI Interface (for SCSI devices)
1748 /////////////////////////////////////////////////////////////////////////////
1752 #define ASPI_SENSE_SIZE 18
1754 // ASPI SCSI Request block header
1757 unsigned char cmd
; // 00: Command code
1758 unsigned char status
; // 01: ASPI status
1759 unsigned char adapter
; // 02: Host adapter number
1760 unsigned char flags
; // 03: Request flags
1761 unsigned char reserved
[4]; // 04: 0
1764 // SRB for host adapter inquiry
1767 ASPI_SRB_HEAD h
; // 00: Header
1768 unsigned char adapters
; // 08: Number of adapters
1769 unsigned char target_id
; // 09: Target ID ?
1770 char manager_id
[16]; // 10: SCSI manager ID
1771 char adapter_id
[16]; // 26: Host adapter ID
1772 unsigned char parameters
[16]; // 42: Host adapter unique parmameters
1775 // SRB for get device type
1778 ASPI_SRB_HEAD h
; // 00: Header
1779 unsigned char target_id
; // 08: Target ID
1780 unsigned char lun
; // 09: LUN
1781 unsigned char devtype
; // 10: Device type
1782 unsigned char reserved
; // 11: Reserved
1788 ASPI_SRB_HEAD h
; // 00: Header
1789 unsigned char target_id
; // 08: Target ID
1790 unsigned char lun
; // 09: LUN
1791 unsigned char reserved
[2]; // 10: Reserved
1792 unsigned long data_size
; // 12: Data alloc. lenght
1793 void * data_addr
; // 16: Data buffer pointer
1794 unsigned char sense_size
; // 20: Sense alloc. length
1795 unsigned char cdb_size
; // 21: CDB length
1796 unsigned char host_status
; // 22: Host status
1797 unsigned char target_status
; // 23: Target status
1798 void * event_handle
; // 24: Event handle
1799 unsigned char workspace
[20]; // 28: ASPI workspace
1800 unsigned char cdb
[16+ASPI_SENSE_SIZE
];
1803 // Macro to retrieve start of sense information
1804 #define ASPI_SRB_SENSE(srb,cdbsz) ((srb)->cdb + 16)
1809 ASPI_SRB_HEAD h
; // Common header
1810 ASPI_SRB_INQUIRY q
; // Inquiry
1811 ASPI_SRB_DEVTYPE t
; // Device type
1812 ASPI_SRB_IO i
; // I/O
1818 #define ASPI_CMD_ADAPTER_INQUIRE 0x00
1819 #define ASPI_CMD_GET_DEVICE_TYPE 0x01
1820 #define ASPI_CMD_EXECUTE_IO 0x02
1821 #define ASPI_CMD_ABORT_IO 0x03
1824 #define ASPI_REQFLAG_DIR_TO_HOST 0x08
1825 #define ASPI_REQFLAG_DIR_TO_TARGET 0x10
1826 #define ASPI_REQFLAG_DIR_NO_XFER 0x18
1827 #define ASPI_REQFLAG_EVENT_NOTIFY 0x40
1830 #define ASPI_STATUS_IN_PROGRESS 0x00
1831 #define ASPI_STATUS_NO_ERROR 0x01
1832 #define ASPI_STATUS_ABORTED 0x02
1833 #define ASPI_STATUS_ABORT_ERR 0x03
1834 #define ASPI_STATUS_ERROR 0x04
1835 #define ASPI_STATUS_INVALID_COMMAND 0x80
1836 #define ASPI_STATUS_INVALID_ADAPTER 0x81
1837 #define ASPI_STATUS_INVALID_TARGET 0x82
1838 #define ASPI_STATUS_NO_ADAPTERS 0xE8
1840 // Adapter (host) status
1841 #define ASPI_HSTATUS_NO_ERROR 0x00
1842 #define ASPI_HSTATUS_SELECTION_TIMEOUT 0x11
1843 #define ASPI_HSTATUS_DATA_OVERRUN 0x12
1844 #define ASPI_HSTATUS_BUS_FREE 0x13
1845 #define ASPI_HSTATUS_BUS_PHASE_ERROR 0x14
1846 #define ASPI_HSTATUS_BAD_SGLIST 0x1A
1849 #define ASPI_TSTATUS_NO_ERROR 0x00
1850 #define ASPI_TSTATUS_CHECK_CONDITION 0x02
1851 #define ASPI_TSTATUS_BUSY 0x08
1852 #define ASPI_TSTATUS_RESERV_CONFLICT 0x18
1855 static HINSTANCE h_aspi_dll
; // DLL handle
1856 static UINT (* aspi_entry
)(ASPI_SRB
* srb
); // ASPI entrypoint
1857 static unsigned num_aspi_adapters
;
1860 // h_aspi_dll+aspi_entry is not inherited by Cygwin's fork()
1861 static DWORD aspi_dll_pid
; // PID of DLL owner to detect fork()
1862 #define aspi_entry_valid() (aspi_entry && (aspi_dll_pid == GetCurrentProcessId()))
1864 #define aspi_entry_valid() (!!aspi_entry)
1868 static int aspi_call(ASPI_SRB
* srb
)
1873 while (((volatile ASPI_SRB
*)srb
)->h
.status
== ASPI_STATUS_IN_PROGRESS
) {
1874 if (++i
> 100/*10sek*/) {
1875 pout("ASPI Adapter %u: Timed out\n", srb
->h
.adapter
);
1877 h_aspi_dll
= (HINSTANCE
)INVALID_HANDLE_VALUE
;
1881 if (con
->reportscsiioctl
> 1)
1882 pout("ASPI Adapter %u: Waiting (%d) ...\n", srb
->h
.adapter
, i
);
1889 // Get ASPI entrypoint from wnaspi32.dll
1891 static FARPROC
aspi_get_address(const char * name
, int verbose
)
1894 assert(h_aspi_dll
&& h_aspi_dll
!= INVALID_HANDLE_VALUE
);
1896 if (!(addr
= GetProcAddress(h_aspi_dll
, name
))) {
1898 pout("Missing %s() in WNASPI32.DLL\n", name
);
1900 FreeLibrary(h_aspi_dll
);
1901 h_aspi_dll
= (HINSTANCE
)INVALID_HANDLE_VALUE
;
1909 static int aspi_open_dll(int verbose
)
1911 UINT (*aspi_info
)(void);
1914 assert(!aspi_entry_valid());
1916 // Check structure layout
1917 assert(sizeof(ASPI_SRB_HEAD
) == 8);
1918 assert(sizeof(ASPI_SRB_INQUIRY
) == 58);
1919 assert(sizeof(ASPI_SRB_DEVTYPE
) == 12);
1920 assert(sizeof(ASPI_SRB_IO
) == 64+ASPI_SENSE_SIZE
);
1921 assert(offsetof(ASPI_SRB
,h
.cmd
) == 0);
1922 assert(offsetof(ASPI_SRB
,h
.flags
) == 3);
1923 assert(offsetof(ASPI_SRB_IO
,lun
) == 9);
1924 assert(offsetof(ASPI_SRB_IO
,data_addr
) == 16);
1925 assert(offsetof(ASPI_SRB_IO
,workspace
) == 28);
1926 assert(offsetof(ASPI_SRB_IO
,cdb
) == 48);
1928 if (h_aspi_dll
== INVALID_HANDLE_VALUE
) {
1935 if (!(h_aspi_dll
= LoadLibraryA("WNASPI32.DLL"))) {
1937 pout("Cannot load WNASPI32.DLL, Error=%ld\n", GetLastError());
1938 h_aspi_dll
= (HINSTANCE
)INVALID_HANDLE_VALUE
;
1942 if (con
->reportscsiioctl
> 1) {
1943 // Print full path of WNASPI32.DLL
1944 char path
[MAX_PATH
];
1945 if (!GetModuleFileName(h_aspi_dll
, path
, sizeof(path
)))
1946 strcpy(path
, "*unknown*");
1947 pout("Using ASPI interface \"%s\"\n", path
);
1950 // Get ASPI entrypoints
1951 if (!(aspi_info
= (UINT (*)(void))aspi_get_address("GetASPI32SupportInfo", verbose
)))
1953 if (!(aspi_entry
= (UINT (*)(ASPI_SRB
*))aspi_get_address("SendASPI32Command", verbose
)))
1956 // Init ASPI manager and get number of adapters
1957 info
= (aspi_info
)();
1958 if (con
->reportscsiioctl
> 1)
1959 pout("GetASPI32SupportInfo() returns 0x%04x\n", info
);
1960 rc
= (info
>> 8) & 0xff;
1961 if (rc
== ASPI_STATUS_NO_ADAPTERS
) {
1962 num_aspi_adapters
= 0;
1964 else if (rc
== ASPI_STATUS_NO_ERROR
) {
1965 num_aspi_adapters
= info
& 0xff;
1969 pout("Got strange 0x%04x from GetASPI32SupportInfo()\n", info
);
1971 FreeLibrary(h_aspi_dll
);
1972 h_aspi_dll
= (HINSTANCE
)INVALID_HANDLE_VALUE
;
1977 if (con
->reportscsiioctl
)
1978 pout("%u ASPI Adapter%s detected\n",num_aspi_adapters
, (num_aspi_adapters
!=1?"s":""));
1981 // save PID to detect fork() in aspi_entry_valid()
1982 aspi_dll_pid
= GetCurrentProcessId();
1984 assert(aspi_entry_valid());
1989 static int aspi_io_call(ASPI_SRB
* srb
, unsigned timeout
)
1993 if (!(event
= CreateEventA(NULL
, FALSE
, FALSE
, NULL
))) {
1994 pout("CreateEvent(): Error=%ld\n", GetLastError()); return -EIO
;
1996 srb
->i
.event_handle
= event
;
1997 srb
->h
.flags
|= ASPI_REQFLAG_EVENT_NOTIFY
;
1998 // Start ASPI request
2000 if (((volatile ASPI_SRB
*)srb
)->h
.status
== ASPI_STATUS_IN_PROGRESS
) {
2002 DWORD rc
= WaitForSingleObject(event
, timeout
*1000L);
2003 if (rc
!= WAIT_OBJECT_0
) {
2004 if (rc
== WAIT_TIMEOUT
) {
2005 pout("ASPI Adapter %u, ID %u: Timed out after %u seconds\n",
2006 srb
->h
.adapter
, srb
->i
.target_id
, timeout
);
2009 pout("WaitForSingleObject(%lx) = 0x%lx,%ld, Error=%ld\n",
2010 (unsigned long)event
, rc
, rc
, GetLastError());
2012 // TODO: ASPI_ABORT_IO command
2014 h_aspi_dll
= (HINSTANCE
)INVALID_HANDLE_VALUE
;
2023 static int aspi_open(unsigned adapter
, unsigned id
)
2026 if (!(adapter
<= 9 && id
< 16)) {
2031 if (!aspi_entry_valid()) {
2032 if (aspi_open_dll(1/*verbose*/))
2037 if (adapter
>= num_aspi_adapters
) {
2038 pout("ASPI Adapter %u does not exist (%u Adapter%s detected).\n",
2039 adapter
, num_aspi_adapters
, (num_aspi_adapters
!=1?"s":""));
2040 if (!is_permissive()) {
2047 memset(&srb
, 0, sizeof(srb
));
2048 srb
.h
.cmd
= ASPI_CMD_GET_DEVICE_TYPE
;
2049 srb
.h
.adapter
= adapter
; srb
.i
.target_id
= id
;
2050 if (aspi_call(&srb
)) {
2054 if (srb
.h
.status
!= ASPI_STATUS_NO_ERROR
) {
2055 pout("ASPI Adapter %u, ID %u: No such device (Status=0x%02x)\n", adapter
, id
, srb
.h
.status
);
2056 if (!is_permissive()) {
2057 errno
= (srb
.h
.status
== ASPI_STATUS_INVALID_TARGET
? ENOENT
: EIO
);
2061 else if (con
->reportscsiioctl
)
2062 pout("ASPI Adapter %u, ID %u: Device Type=0x%02x\n", adapter
, id
, srb
.t
.devtype
);
2064 return (ASPI_FDOFFSET
| ((adapter
& 0xf)<<4) | (id
& 0xf));
2068 static void aspi_close(int fd
)
2070 // No FreeLibrary(h_aspi_dll) to prevent problems with ASPI threads
2075 // Scan for SCSI drives, fill bitmask [adapter:0-9][id:0-7] of drives present,
2078 static int aspi_scan(unsigned long * drives
)
2083 if (!aspi_entry_valid()) {
2084 if (aspi_open_dll(con
->reportscsiioctl
/*default is quiet*/))
2088 for (ad
= 0; ad
< num_aspi_adapters
; ad
++) {
2089 ASPI_SRB srb
; unsigned id
;
2092 if (con
->reportscsiioctl
)
2093 pout(" ASPI Adapter %u: Ignored\n", ad
);
2098 memset(&srb
, 0, sizeof(srb
));
2099 srb
.h
.cmd
= ASPI_CMD_ADAPTER_INQUIRE
;
2101 if (aspi_call(&srb
))
2104 if (srb
.h
.status
!= ASPI_STATUS_NO_ERROR
) {
2105 if (con
->reportscsiioctl
)
2106 pout(" ASPI Adapter %u: Status=0x%02x\n", ad
, srb
.h
.status
);
2110 if (con
->reportscsiioctl
) {
2112 for (i
= 1; i
< 16 && srb
.q
.adapter_id
[i
]; i
++)
2113 if (!(' ' <= srb
.q
.adapter_id
[i
] && srb
.q
.adapter_id
[i
] <= '~'))
2114 srb
.q
.adapter_id
[i
] = '?';
2115 pout(" ASPI Adapter %u (\"%.16s\"):\n", ad
, srb
.q
.adapter_id
);
2118 bool ignore
= !strnicmp(srb
.q
.adapter_id
, "3ware", 5);
2120 for (id
= 0; id
<= 7; id
++) {
2122 memset(&srb
, 0, sizeof(srb
));
2123 srb
.h
.cmd
= ASPI_CMD_GET_DEVICE_TYPE
;
2124 srb
.h
.adapter
= ad
; srb
.i
.target_id
= id
;
2125 if (aspi_call(&srb
))
2127 if (srb
.h
.status
!= ASPI_STATUS_NO_ERROR
) {
2128 if (con
->reportscsiioctl
> 1)
2129 pout(" ID %u: No such device (Status=0x%02x)\n", id
, srb
.h
.status
);
2133 if (!ignore
&& srb
.t
.devtype
== 0x00/*HDD*/) {
2134 if (con
->reportscsiioctl
)
2135 pout(" ID %u: Device Type=0x%02x\n", id
, srb
.t
.devtype
);
2136 drives
[ad
>> 2] |= (1L << (((ad
& 0x3) << 3) + id
));
2139 else if (con
->reportscsiioctl
)
2140 pout(" ID %u: Device Type=0x%02x (ignored)\n", id
, srb
.t
.devtype
);
2147 /////////////////////////////////////////////////////////////////////////////
2149 // Interface to ASPI SCSI devices. See scsicmds.h and os_linux.c
2150 static int do_aspi_cmnd_io(int fd
, struct scsi_cmnd_io
* iop
, int report
)
2154 if (!aspi_entry_valid())
2156 if (!((fd
& ~0xff) == ASPI_FDOFFSET
))
2159 if (!(iop
->cmnd_len
== 6 || iop
->cmnd_len
== 10 || iop
->cmnd_len
== 12 || iop
->cmnd_len
== 16)) {
2160 pout("do_aspi_cmnd_io: bad CDB length\n");
2167 const unsigned char * ucp
= iop
->cmnd
;
2170 const int sz
= (int)sizeof(buff
);
2172 np
= scsi_get_opcode_name(ucp
[0]);
2173 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
2174 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
2175 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
2177 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
2178 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
2180 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
2181 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
2182 (trunc
? " [only first 256 bytes shown]" : ""));
2183 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
2186 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
2190 memset(&srb
, 0, sizeof(srb
));
2191 srb
.h
.cmd
= ASPI_CMD_EXECUTE_IO
;
2192 srb
.h
.adapter
= ((fd
>> 4) & 0xf);
2193 srb
.i
.target_id
= (fd
& 0xf);
2195 srb
.i
.sense_size
= ASPI_SENSE_SIZE
;
2196 srb
.i
.cdb_size
= iop
->cmnd_len
;
2197 memcpy(srb
.i
.cdb
, iop
->cmnd
, iop
->cmnd_len
);
2199 switch (iop
->dxfer_dir
) {
2201 srb
.h
.flags
= ASPI_REQFLAG_DIR_NO_XFER
;
2203 case DXFER_FROM_DEVICE
:
2204 srb
.h
.flags
= ASPI_REQFLAG_DIR_TO_HOST
;
2205 srb
.i
.data_size
= iop
->dxfer_len
;
2206 srb
.i
.data_addr
= iop
->dxferp
;
2208 case DXFER_TO_DEVICE
:
2209 srb
.h
.flags
= ASPI_REQFLAG_DIR_TO_TARGET
;
2210 srb
.i
.data_size
= iop
->dxfer_len
;
2211 srb
.i
.data_addr
= iop
->dxferp
;
2214 pout("do_aspi_cmnd_io: bad dxfer_dir\n");
2218 iop
->resp_sense_len
= 0;
2219 iop
->scsi_status
= 0;
2222 if (aspi_io_call(&srb
, (iop
->timeout
? iop
->timeout
: 60))) {
2227 if (srb
.h
.status
!= ASPI_STATUS_NO_ERROR
) {
2228 if ( srb
.h
.status
== ASPI_STATUS_ERROR
2229 && srb
.i
.host_status
== ASPI_HSTATUS_NO_ERROR
2230 && srb
.i
.target_status
== ASPI_TSTATUS_CHECK_CONDITION
) {
2232 const unsigned char * sense
= ASPI_SRB_SENSE(&srb
.i
, iop
->cmnd_len
);
2233 int len
= (ASPI_SENSE_SIZE
< iop
->max_sense_len
? ASPI_SENSE_SIZE
: iop
->max_sense_len
);
2234 iop
->scsi_status
= SCSI_STATUS_CHECK_CONDITION
;
2235 if (len
> 0 && iop
->sensep
) {
2236 memcpy(iop
->sensep
, sense
, len
);
2237 iop
->resp_sense_len
= len
;
2239 pout(" >>> Sense buffer, len=%d:\n", (int)len
);
2240 dStrHex(iop
->sensep
, len
, 1);
2244 pout(" sense_key=%x asc=%x ascq=%x\n",
2245 sense
[2] & 0xf, sense
[12], sense
[13]);
2251 pout(" ASPI call failed, (0x%02x,0x%02x,0x%02x)\n", srb
.h
.status
, srb
.i
.host_status
, srb
.i
.target_status
);
2259 if (iop
->dxfer_dir
== DXFER_FROM_DEVICE
&& report
> 1) {
2260 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
2261 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
2262 (trunc
? " [only first 256 bytes shown]" : ""));
2263 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
2270 /////////////////////////////////////////////////////////////////////////////
2271 // SPT Interface (for SCSI devices and ATA devices behind SATLs)
2272 // Only supported in NT and later
2273 /////////////////////////////////////////////////////////////////////////////
2275 #define SPT_MAXDEV 64
2277 struct spt_dev_info
{
2279 int pd_num
; // physical drive number
2280 int tape_num
; // tape number ('\\.\TAPE<n>')
2281 int sub_addr
; // addressing disks within a RAID, for example
2284 // Private table of open devices: guaranteed zero on startup since
2285 // part of static data.
2286 static struct spt_dev_info
* spt_dev_arr
[SPT_MAXDEV
];
2289 static int spt_open(int pd_num
, int tape_num
, int sub_addr
)
2292 struct spt_dev_info
* sdip
;
2296 for (k
= 0; k
< SPT_MAXDEV
; k
++)
2297 if (! spt_dev_arr
[k
])
2300 // If no free entry found, return error. We have max allowed number
2301 // of "file descriptors" already allocated.
2302 if (k
== SPT_MAXDEV
) {
2303 if (con
->reportscsiioctl
)
2304 pout("spt_open: too many open file descriptors (%d)\n",
2309 sdip
= (struct spt_dev_info
*)malloc(sizeof(struct spt_dev_info
));
2314 spt_dev_arr
[k
] = sdip
;
2315 sdip
->pd_num
= pd_num
;
2316 sdip
->tape_num
= tape_num
;
2317 sdip
->sub_addr
= sub_addr
;
2319 b
[sizeof(b
) - 1] = '\0';
2321 snprintf(b
, sizeof(b
) - 1, "\\\\.\\PhysicalDrive%d", pd_num
);
2322 else if (tape_num
>= 0)
2323 snprintf(b
, sizeof(b
) - 1, "\\\\.\\TAPE%d", tape_num
);
2325 if (con
->reportscsiioctl
)
2326 pout("spt_open: bad parameters\n");
2332 if ((h
= CreateFileA(b
, GENERIC_READ
|GENERIC_WRITE
,
2333 FILE_SHARE_READ
|FILE_SHARE_WRITE
, NULL
,
2334 OPEN_EXISTING
, 0, 0)) == INVALID_HANDLE_VALUE
) {
2335 if (con
->reportscsiioctl
)
2336 pout(" %s: Open failed, Error=%ld\n", b
, GetLastError());
2340 sdip
->h_spt_ioctl
= h
;
2341 return k
+ SPT_FDOFFSET
;
2344 spt_dev_arr
[k
] = NULL
;
2350 static void spt_close(int fd
)
2352 struct spt_dev_info
* sdip
;
2353 int index
= fd
- SPT_FDOFFSET
;
2355 if ((index
< 0) || (index
>= SPT_MAXDEV
)) {
2356 if (con
->reportscsiioctl
)
2357 pout("spt_close: bad fd range\n");
2360 sdip
= spt_dev_arr
[index
];
2362 if (con
->reportscsiioctl
)
2363 pout("spt_close: fd already closed\n");
2367 spt_dev_arr
[index
] = NULL
;
2371 #define IOCTL_SCSI_PASS_THROUGH_DIRECT \
2372 CTL_CODE(IOCTL_SCSI_BASE, 0x0405, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS)
2374 typedef struct _SCSI_PASS_THROUGH_DIRECT
{
2381 UCHAR SenseInfoLength
;
2383 ULONG DataTransferLength
;
2386 ULONG SenseInfoOffset
;
2388 } SCSI_PASS_THROUGH_DIRECT
;
2391 SCSI_PASS_THROUGH_DIRECT spt
;
2393 UCHAR ucSenseBuf
[64];
2394 } SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER
;
2397 // Interface to SPT SCSI devices. See scsicmds.h and os_linux.c
2398 static int do_spt_cmnd_io(int fd
, struct scsi_cmnd_io
* iop
, int report
)
2400 struct spt_dev_info
* sdip
;
2401 int index
= fd
- SPT_FDOFFSET
;
2402 SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER sb
;
2405 if ((index
< 0) || (index
>= SPT_MAXDEV
)) {
2407 pout("do_spt_cmnd_io: bad fd range\n");
2410 sdip
= spt_dev_arr
[index
];
2413 pout("do_spt_cmnd_io: fd already closed\n");
2419 const unsigned char * ucp
= iop
->cmnd
;
2422 const int sz
= (int)sizeof(buff
);
2424 np
= scsi_get_opcode_name(ucp
[0]);
2425 j
= snprintf(buff
, sz
, " [%s: ", np
? np
: "<unknown opcode>");
2426 for (k
= 0; k
< (int)iop
->cmnd_len
; ++k
)
2427 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "%02x ", ucp
[k
]);
2429 (DXFER_TO_DEVICE
== iop
->dxfer_dir
) && (iop
->dxferp
)) {
2430 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
2432 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n Outgoing "
2433 "data, len=%d%s:\n", (int)iop
->dxfer_len
,
2434 (trunc
? " [only first 256 bytes shown]" : ""));
2435 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
2438 j
+= snprintf(&buff
[j
], (sz
> j
? (sz
- j
) : 0), "]\n");
2441 if (iop
->cmnd_len
> (int)sizeof(sb
.spt
.Cdb
)) {
2443 pout("do_spt_cmnd_io: cmnd_len too large\n");
2447 memset(&sb
, 0, sizeof(sb
));
2448 sb
.spt
.Length
= sizeof(SCSI_PASS_THROUGH_DIRECT
);
2449 sb
.spt
.CdbLength
= iop
->cmnd_len
;
2450 memcpy(sb
.spt
.Cdb
, iop
->cmnd
, iop
->cmnd_len
);
2451 sb
.spt
.SenseInfoLength
= sizeof(sb
.ucSenseBuf
);
2452 sb
.spt
.SenseInfoOffset
=
2453 offsetof(SCSI_PASS_THROUGH_DIRECT_WITH_BUFFER
, ucSenseBuf
);
2454 sb
.spt
.TimeOutValue
= (iop
->timeout
? iop
->timeout
: 60);
2455 switch (iop
->dxfer_dir
) {
2457 sb
.spt
.DataIn
= SCSI_IOCTL_DATA_UNSPECIFIED
;
2459 case DXFER_FROM_DEVICE
:
2460 sb
.spt
.DataIn
= SCSI_IOCTL_DATA_IN
;
2461 sb
.spt
.DataTransferLength
= iop
->dxfer_len
;
2462 sb
.spt
.DataBuffer
= iop
->dxferp
;
2464 case DXFER_TO_DEVICE
:
2465 sb
.spt
.DataIn
= SCSI_IOCTL_DATA_OUT
;
2466 sb
.spt
.DataTransferLength
= iop
->dxfer_len
;
2467 sb
.spt
.DataBuffer
= iop
->dxferp
;
2470 pout("do_spt_cmnd_io: bad dxfer_dir\n");
2474 if (! DeviceIoControl(sdip
->h_spt_ioctl
, IOCTL_SCSI_PASS_THROUGH_DIRECT
,
2475 &sb
, sizeof(sb
), &sb
, sizeof(sb
), &num_out
, NULL
)) {
2476 long err
= GetLastError();
2479 pout(" IOCTL_SCSI_PASS_THROUGH_DIRECT failed, Error=%ld\n", err
);
2480 return -(err
== ERROR_INVALID_FUNCTION
? ENOSYS
: EIO
);
2483 iop
->scsi_status
= sb
.spt
.ScsiStatus
;
2484 if (SCSI_STATUS_CHECK_CONDITION
& iop
->scsi_status
) {
2485 int slen
= sb
.ucSenseBuf
[7] + 8;
2487 if (slen
> (int)sizeof(sb
.ucSenseBuf
))
2488 slen
= sizeof(sb
.ucSenseBuf
);
2489 if (slen
> (int)iop
->max_sense_len
)
2490 slen
= iop
->max_sense_len
;
2491 memcpy(iop
->sensep
, sb
.ucSenseBuf
, slen
);
2492 iop
->resp_sense_len
= slen
;
2494 if ((iop
->sensep
[0] & 0x7f) > 0x71)
2495 pout(" status=%x: [desc] sense_key=%x asc=%x ascq=%x\n",
2496 iop
->scsi_status
, iop
->sensep
[1] & 0xf,
2497 iop
->sensep
[2], iop
->sensep
[3]);
2499 pout(" status=%x: sense_key=%x asc=%x ascq=%x\n",
2500 iop
->scsi_status
, iop
->sensep
[2] & 0xf,
2501 iop
->sensep
[12], iop
->sensep
[13]);
2504 iop
->resp_sense_len
= 0;
2506 if ((iop
->dxfer_len
> 0) && (sb
.spt
.DataTransferLength
> 0))
2507 iop
->resid
= iop
->dxfer_len
- sb
.spt
.DataTransferLength
;
2511 if ((iop
->dxfer_dir
== DXFER_FROM_DEVICE
) && (report
> 1)) {
2512 int trunc
= (iop
->dxfer_len
> 256) ? 1 : 0;
2513 pout(" Incoming data, len=%d%s:\n", (int)iop
->dxfer_len
,
2514 (trunc
? " [only first 256 bytes shown]" : ""));
2515 dStrHex(iop
->dxferp
, (trunc
? 256 : iop
->dxfer_len
) , 1);
2521 // Decides which SCSI implementation based on pseudo fd.
2522 // Declaration and explanation in scsicmds.h
2523 int do_scsi_cmnd_io(int fd
, struct scsi_cmnd_io
* iop
, int report
)
2525 if ((fd
& ~0xff) == ASPI_FDOFFSET
)
2526 return do_aspi_cmnd_io(fd
, iop
, report
);
2528 return do_spt_cmnd_io(fd
, iop
, report
);