18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(aspi
);
23 * 1) Residual byte length reporting not handled
24 * 2) Make this code re-entrant for multithreading
25 * -- Added CriticalSection to OpenDevices function
26 * 3) Only linux supported so far
27 * 4) Leaves sg devices open. This may or may not be okay. A better solution
28 * would be to close the file descriptors when the thread/process using
29 * them no longer needs them.
34 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
35 static CRITICAL_SECTION ASPI_CritSection
= CRITICAL_SECTION_INIT
;
37 #endif /* defined(linux) */
40 BOOL WINAPI
WNASPI32_LibMain(HINSTANCE hInstDLL
, DWORD fdwReason
, LPVOID fImpLoad
)
45 case DLL_PROCESS_ATTACH
:
48 case DLL_PROCESS_DETACH
:
49 DeleteCriticalSection( &ASPI_CritSection
);
51 case DLL_THREAD_ATTACH
:
52 case DLL_THREAD_DETACH
:
55 #endif /* defined(linux) */
63 ASPI_OpenDevice(SRB_ExecSCSICmd
*prb
)
67 ASPI_DEVICE_INFO
*curr
;
69 /* search list of devices to see if we've opened it already.
70 * There is not an explicit open/close in ASPI land, so hopefully
71 * keeping a device open won't be a problem.
74 EnterCriticalSection(&ASPI_CritSection
);
75 for (curr
= ASPI_open_devices
; curr
; curr
= curr
->next
) {
76 if (curr
->hostId
== prb
->SRB_HaId
&&
77 curr
->target
== prb
->SRB_Target
&&
78 curr
->lun
== prb
->SRB_Lun
) {
79 LeaveCriticalSection(&ASPI_CritSection
);
83 LeaveCriticalSection(&ASPI_CritSection
);
85 if (prb
->SRB_HaId
> ASPI_GetNumControllers())
88 hc
= ASPI_GetHCforController( prb
->SRB_HaId
);
89 fd
= SCSI_OpenDevice( HIWORD(hc
), LOWORD(hc
), prb
->SRB_Target
, prb
->SRB_Lun
);
94 /* device is now open */
95 /* FIXME: Let users specify SCSI timeout in registry */
96 SCSI_LinuxSetTimeout( fd
, SCSI_DEFAULT_TIMEOUT
);
98 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
100 curr
->hostId
= prb
->SRB_HaId
;
101 curr
->target
= prb
->SRB_Target
;
102 curr
->lun
= prb
->SRB_Lun
;
104 /* insert new record at beginning of open device list */
105 EnterCriticalSection(&ASPI_CritSection
);
106 curr
->next
= ASPI_open_devices
;
107 ASPI_open_devices
= curr
;
108 LeaveCriticalSection(&ASPI_CritSection
);
114 ASPI_DebugPrintCmd(SRB_ExecSCSICmd
*prb
)
120 switch (prb
->CDBByte
[0]) {
122 TRACE("INQUIRY {\n");
123 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
124 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
125 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
126 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
127 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
131 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
135 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
136 TRACE("Flags: %d\n", prb
->SRB_Flags
);
137 if (TARGET_TO_HOST(prb
)) {
138 TRACE("\tData transfer: Target to host. Length checked.\n");
140 else if (HOST_TO_TARGET(prb
)) {
141 TRACE("\tData transfer: Host to target. Length checked.\n");
143 else if (NO_DATA_TRANSFERED(prb
)) {
144 TRACE("\tData transfer: none\n");
147 WARN("\tTransfer by scsi cmd. Length not checked.\n");
150 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
151 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
152 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
153 TRACE("Target: %d\n", prb
->SRB_Target
);
154 TRACE("Lun: %d\n", prb
->SRB_Lun
);
155 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
156 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
157 TRACE("BufPtr: %p\n", prb
->SRB_BufPointer
);
158 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
159 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
160 cdb
= &prb
->CDBByte
[0];
161 cmd
= prb
->CDBByte
[0];
162 if (TRACE_ON(aspi
)) {
163 DPRINTF("CDB buffer[");
164 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
165 if (i
!= 0) DPRINTF(",");
166 DPRINTF("%02x", *cdb
++);
173 ASPI_PrintCDBArea(SRB_ExecSCSICmd
*prb
)
179 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
181 DPRINTF("%02x", prb
->CDBByte
[i
]);
188 ASPI_PrintSenseArea(SRB_ExecSCSICmd
*prb
)
191 BYTE
*rqbuf
= prb
->SenseArea
;
195 DPRINTF("Request Sense reports:\n");
196 if ((rqbuf
[0]&0x7f)!=0x70) {
197 DPRINTF("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf
[0]&0x7f);
200 DPRINTF("\tCurrent command read filemark: %s\n",(rqbuf
[2]&0x80)?"yes":"no");
201 DPRINTF("\tEarly warning passed: %s\n",(rqbuf
[2]&0x40)?"yes":"no");
202 DPRINTF("\tIncorrect blocklength: %s\n",(rqbuf
[2]&0x20)?"yes":"no");
203 DPRINTF("\tSense Key: %d\n",rqbuf
[2]&0xf);
205 DPRINTF("\tResidual Length: %d\n",rqbuf
[3]*0x1000000+rqbuf
[4]*0x10000+rqbuf
[5]*0x100+rqbuf
[6]);
206 DPRINTF("\tAdditional Sense Length: %d\n",rqbuf
[7]);
207 DPRINTF("\tAdditional Sense Code: %d\n",rqbuf
[12]);
208 DPRINTF("\tAdditional Sense Code Qualifier: %d\n",rqbuf
[13]);
209 if (rqbuf
[15]&0x80) {
210 DPRINTF("\tIllegal Param is in %s\n",(rqbuf
[15]&0x40)?"the CDB":"the Data Out Phase");
212 DPRINTF("Pointer at %d, bit %d\n",rqbuf
[16]*256+rqbuf
[17],rqbuf
[15]&0x7);
215 DPRINTF("SenseArea[");
216 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
218 DPRINTF("%02x", *rqbuf
++);
225 ASPI_DebugPrintResult(SRB_ExecSCSICmd
*prb
)
228 TRACE("SRB_Status: %x\n", prb
->SRB_Status
);
229 TRACE("SRB_HaStat: %x\n", prb
->SRB_HaStat
);
230 TRACE("SRB_TargStat: %x\n", prb
->SRB_TargStat
);
231 switch (prb
->CDBByte
[0]) {
233 TRACE("Vendor: '%s'\n", prb
->SRB_BufPointer
+ INQUIRY_VENDOR
);
235 case CMD_TEST_UNIT_READY
:
236 ASPI_PrintSenseArea(prb
);
241 /* Posting must be done in such a way that as soon as the SRB_Status is set
242 * we don't touch the SRB anymore because it could possibly be freed
243 * if the app is doing ASPI polling
246 WNASPI32_DoPosting( SRB_ExecSCSICmd
*lpPRB
, DWORD status
)
248 void (*SRB_PostProc
)() = lpPRB
->SRB_PostProc
;
249 BYTE SRB_Flags
= lpPRB
->SRB_Flags
;
250 if( status
== SS_PENDING
)
252 WARN("Tried posting SS_PENDING\n");
255 lpPRB
->SRB_Status
= status
;
256 /* lpPRB is NOT safe, it could be freed in another thread */
262 TRACE("Post Routine (%lx) called\n", (DWORD
) SRB_PostProc
);
263 /* Even though lpPRB could have been freed by
264 * the program.. that's unlikely if it planned
265 * to use it in the PostProc
267 (*SRB_PostProc
)(lpPRB
);
269 else if (SRB_Flags
& SRB_EVENT_NOTIFY
) {
270 TRACE("Setting event %04x\n", (HANDLE
)SRB_PostProc
);
271 SetEvent((HANDLE
)SRB_PostProc
);
278 ASPI_ExecScsiCmd(SRB_ExecSCSICmd
*lpPRB
)
280 struct sg_header
*sg_hd
, *sg_reply_hdr
;
288 /* FIXME: hackmode */
289 #define MAKE_TARGET_TO_HOST(lpPRB) \
290 if (!TARGET_TO_HOST(lpPRB)) { \
291 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
292 lpPRB->SRB_Flags |= 0x08; \
294 #define MAKE_HOST_TO_TARGET(lpPRB) \
295 if (!HOST_TO_TARGET(lpPRB)) { \
296 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
297 lpPRB->SRB_Flags |= 0x10; \
299 switch (lpPRB
->CDBByte
[0]) {
300 case 0x12: /* INQUIRY */
301 case 0x5a: /* MODE_SENSE_10 */
302 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
303 case 0xad: /* READ DVD STRUCTURE MMC-2 */
304 MAKE_TARGET_TO_HOST(lpPRB
)
306 case 0xa3: /* SEND KEY (DVD) MMC-2 */
307 MAKE_HOST_TO_TARGET(lpPRB
)
310 if ((((lpPRB
->SRB_Flags
& 0x18) == 0x00) ||
311 ((lpPRB
->SRB_Flags
& 0x18) == 0x18)
312 ) && lpPRB
->SRB_BufLen
314 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB
->CDBByte
[0],lpPRB
->SRB_BufLen
);
318 ASPI_DebugPrintCmd(lpPRB
);
319 if (lpPRB
->SRB_HaId
> ASPI_GetNumControllers()) {
320 ERR("Failed: Wanted hostadapter %d, but we have only %d.\n",
321 lpPRB
->SRB_HaId
,ASPI_GetNumControllers()
323 return WNASPI32_DoPosting( lpPRB
, SS_INVALID_HA
);
325 fd
= ASPI_OpenDevice(lpPRB
);
327 return WNASPI32_DoPosting( lpPRB
, SS_NO_DEVICE
);
333 lpPRB
->SRB_Status
= SS_PENDING
;
335 if (!lpPRB
->SRB_CDBLen
) {
336 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
337 return WNASPI32_DoPosting( lpPRB
, SS_INVALID_SRB
);
340 /* build up sg_header + scsi cmd */
341 if (HOST_TO_TARGET(lpPRB
)) {
342 /* send header, command, and then data */
343 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
344 sg_hd
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, in_len
);
345 memset(sg_hd
, 0, SCSI_OFF
);
346 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
347 if (lpPRB
->SRB_BufLen
) {
348 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpPRB
->SRB_BufPointer
, lpPRB
->SRB_BufLen
);
352 /* send header and command - no data */
353 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
354 sg_hd
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, in_len
);
355 memset(sg_hd
, 0, SCSI_OFF
);
356 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
359 if (TARGET_TO_HOST(lpPRB
)) {
360 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
361 sg_reply_hdr
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, out_len
);
362 memset(sg_reply_hdr
, 0, SCSI_OFF
);
363 sg_hd
->reply_len
= out_len
;
367 sg_reply_hdr
= (struct sg_header
*) HeapAlloc(GetProcessHeap(), 0, out_len
);
368 memset(sg_reply_hdr
, 0, SCSI_OFF
);
369 sg_hd
->reply_len
= out_len
;
372 SCSI_Fix_CMD_LEN(fd
, lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
374 if(!SCSI_LinuxDeviceIo( fd
,
376 sg_reply_hdr
, out_len
,
382 if (sg_reply_hdr
->result
!= 0) {
383 error_code
= sg_reply_hdr
->result
;
384 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
388 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
389 memcpy(lpPRB
->SRB_BufPointer
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
392 /* copy in sense buffer to amount that is available in client */
393 if (lpPRB
->SRB_SenseLen
) {
394 int sense_len
= lpPRB
->SRB_SenseLen
;
395 if (lpPRB
->SRB_SenseLen
> 16)
398 /* CDB is fixed in WNASPI32 */
399 memcpy(lpPRB
->SenseArea
, &sg_reply_hdr
->sense_buffer
[0], sense_len
);
401 ASPI_PrintCDBArea(lpPRB
);
402 ASPI_PrintSenseArea(lpPRB
);
405 SRB_Status
= SS_COMP
;
406 lpPRB
->SRB_HaStat
= HASTAT_OK
;
407 lpPRB
->SRB_TargStat
= sg_reply_hdr
->target_status
<< 1;
409 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
410 HeapFree(GetProcessHeap(), 0, sg_hd
);
412 /* FIXME: Should this be != 0 maybe? */
413 if( lpPRB
->SRB_TargStat
== 2 ) {
415 switch (lpPRB
->CDBByte
[0]) {
416 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
417 case 0xa3: /* SEND KEY (DVD) MMC-2 */
418 SRB_Status
= SS_COMP
;
419 lpPRB
->SRB_TargStat
= 0;
420 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
425 ASPI_DebugPrintResult(lpPRB
);
427 ret
= WNASPI32_DoPosting( lpPRB
, SRB_Status
);
429 switch (lpPRB
->CDBByte
[0]) {
431 if (SRB_Status
== SS_COMP
)
432 return SS_COMP
; /* some junk expects ss_comp here. */
438 /* In real WNASPI32 stuff really is always pending because ASPI does things
439 in the background, but we are not doing that (yet) */
445 if (error_code
== EBUSY
) {
446 WNASPI32_DoPosting( lpPRB
, SS_ASPI_IS_BUSY
);
447 TRACE("Device busy\n");
451 /* I'm not sure exactly error codes work here
452 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
454 WARN("error_exit\n");
455 HeapFree(GetProcessHeap(), 0, sg_reply_hdr
);
456 HeapFree(GetProcessHeap(), 0, sg_hd
);
457 WNASPI32_DoPosting( lpPRB
, SRB_Status
);
461 #endif /* defined(linux) */
464 /*******************************************************************
465 * GetASPI32SupportInfo [WNASPI32.1]
467 * Checks if the ASPI subsystem is initialized correctly.
471 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
472 * LOBYTE of LOWORD: # of host adapters.
474 DWORD __cdecl
GetASPI32SupportInfo(void)
476 DWORD controllers
= ASPI_GetNumControllers();
479 return SS_NO_ADAPTERS
<< 8;
480 return (SS_COMP
<< 8) | controllers
;
483 /***********************************************************************
484 * SendASPI32Command (WNASPI32.2)
486 DWORD __cdecl
SendASPI32Command(LPSRB lpSRB
)
489 switch (lpSRB
->common
.SRB_Cmd
) {
491 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
492 lpSRB
->inquiry
.HA_Count
= ASPI_GetNumControllers();
493 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
494 strcpy(lpSRB
->inquiry
.HA_ManagerId
, "ASPI for WIN32"); /* max 15 chars, don't change */
495 strcpy(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host adapter name */
496 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
497 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
498 lpSRB
->inquiry
.HA_Unique
[3] = 0x08; /* Maximum number of SCSI targets */
499 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
502 case SC_GET_DEV_TYPE
: {
503 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
504 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
509 memset(&tmpsrb
,0,sizeof(tmpsrb
));
512 memcpy(&tmpsrb
.common
,&(lpSRB
->common
),sizeof(tmpsrb
.common
));
514 tmpsrb
.cmd
.SRB_Flags
|= 8; /* target to host */
515 tmpsrb
.cmd
.SRB_Cmd
= SC_EXEC_SCSI_CMD
;
516 tmpsrb
.cmd
.SRB_Target
= lpSRB
->devtype
.SRB_Target
;
517 tmpsrb
.cmd
.SRB_Lun
= lpSRB
->devtype
.SRB_Lun
;
518 tmpsrb
.cmd
.SRB_BufLen
= sizeof(inqbuf
);
519 tmpsrb
.cmd
.SRB_BufPointer
= inqbuf
;
520 tmpsrb
.cmd
.CDBByte
[0] = 0x12; /* INQUIRY */
521 /* FIXME: handle lun */
522 tmpsrb
.cmd
.CDBByte
[4] = sizeof(inqbuf
);
523 tmpsrb
.cmd
.SRB_CDBLen
= 6;
525 ret
= ASPI_ExecScsiCmd(&tmpsrb
.cmd
);
527 lpSRB
->devtype
.SRB_Status
= tmpsrb
.cmd
.SRB_Status
;
528 lpSRB
->devtype
.SRB_DeviceType
= inqbuf
[0]&0x1f;
530 TRACE("returning devicetype %d for target %d\n",inqbuf
[0]&0x1f,tmpsrb
.cmd
.SRB_Target
);
531 if (ret
!=SS_PENDING
) /* Any error is passed down directly */
533 /* FIXME: knows that the command is finished already, pass final Status */
534 return tmpsrb
.cmd
.SRB_Status
;
536 case SC_EXEC_SCSI_CMD
:
537 return ASPI_ExecScsiCmd(&lpSRB
->cmd
);
539 FIXME("Not implemented SC_ABORT_SRB\n");
542 FIXME("Not implemented SC_RESET_DEV\n");
544 case SC_GET_DISK_INFO
:
545 /* here we have to find out the int13 / bios association.
546 * We just say we do not have any.
548 FIXME("SC_GET_DISK_INFO always return 'int13 unassociated disk'.\n");
549 lpSRB
->diskinfo
.SRB_DriveFlags
= 0; /* disk is not int13 served */
552 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
554 return SS_INVALID_SRB
;
556 return SS_INVALID_SRB
;
561 /***********************************************************************
562 * GetASPI32DLLVersion (WNASPI32.4)
564 DWORD __cdecl
GetASPI32DLLVersion(void)
567 TRACE("Returning version 1\n");
570 FIXME("Please add SCSI support for your operating system, returning 0\n");
575 /***********************************************************************
576 * GetASPI32Buffer (WNASPI32.8)
577 * Supposed to return a DMA capable large SCSI buffer.
578 * Our implementation does not use those at all, all buffer stuff is
579 * done in the kernel SG device layer. So we just heapalloc the buffer.
581 BOOL __cdecl
GetASPI32Buffer(PASPI32BUFF pab
)
583 pab
->AB_BufPointer
= HeapAlloc(GetProcessHeap(),
584 pab
->AB_ZeroFill
?HEAP_ZERO_MEMORY
:0,
587 if (!pab
->AB_BufPointer
) return FALSE
;
591 /***********************************************************************
592 * FreeASPI32Buffer (WNASPI32.14)
594 BOOL __cdecl
FreeASPI32Buffer(PASPI32BUFF pab
)
596 HeapFree(GetProcessHeap(),0,pab
->AB_BufPointer
);
600 /***********************************************************************
601 * TranslateASPI32Address (WNASPI32.7)
603 BOOL __cdecl
TranslateASPI32Address(LPDWORD pdwPath
, LPDWORD pdwDEVNODE
)
605 FIXME("(%p, %p), stub !\n", pdwPath
, pdwDEVNODE
);