14 #include "wine/windef16.h"
18 #include "wine/winaspi.h"
20 #include "debugtools.h"
23 DEFAULT_DEBUG_CHANNEL(aspi
);
27 * 1) Residual byte length reporting not handled
28 * 2) Make this code re-entrant for multithreading
29 * 3) Only linux supported so far
32 /* ### start build ### */
33 extern LONG CALLBACK
ASPI_CallTo16_long_l(FARPROC16
,SEGPTR
);
34 /* ### stop build ### */
38 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
40 static FARPROC16 ASPIChainFunc
= NULL
;
41 static WORD HA_Count
= 1; /* host adapter count; FIXME: detect it */
44 ASPI_OpenDevice16(SRB_ExecSCSICmd16
*prb
)
50 ASPI_DEVICE_INFO
*curr
;
52 /* search list of devices to see if we've opened it already.
53 * There is not an explicit open/close in ASPI land, so hopefully
54 * keeping a device open won't be a problem.
57 for (curr
= ASPI_open_devices
; curr
; curr
= curr
->next
) {
58 if (curr
->hostId
== prb
->SRB_HaId
&&
59 curr
->target
== prb
->SRB_Target
&&
60 curr
->lun
== prb
->SRB_Lun
) {
65 /* device wasn't cached, go ahead and open it */
66 sprintf( idstr
, "Software\\Wine\\Wine\\Config\\scsi c%1dt%1dd%1d",
67 prb
->SRB_HaId
, prb
->SRB_Target
, prb
->SRB_Lun
);
70 if (!RegOpenKeyExA( HKEY_LOCAL_MACHINE
, idstr
, 0, KEY_ALL_ACCESS
, &hkey
))
72 DWORD type
, count
= sizeof(device_str
);
73 if (RegQueryValueExA( hkey
, "Device", 0, &type
, device_str
, &count
)) device_str
[0] = 0;
79 TRACE("Trying to open unlisted scsi device %s\n", idstr
);
83 TRACE("Opening device %s=%s\n", idstr
, device_str
);
85 fd
= open(device_str
, O_RDWR
);
87 int save_error
= errno
;
88 ERR("Error opening device %s, error '%s'\n", device_str
, strerror(save_error
));
92 /* device is now open */
93 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
95 curr
->hostId
= prb
->SRB_HaId
;
96 curr
->target
= prb
->SRB_Target
;
97 curr
->lun
= prb
->SRB_Lun
;
99 /* insert new record at beginning of open device list */
100 curr
->next
= ASPI_open_devices
;
101 ASPI_open_devices
= curr
;
107 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
117 /* translate real mode address */
118 if (prb
->SRB_BufPointer
)
119 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(prb
->SRB_BufPointer
), OFFSETOF(prb
->SRB_BufPointer
));
122 lpBuf
= MapSL(prb
->SRB_BufPointer
);
126 switch (prb
->CDBByte
[0]) {
129 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
130 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
131 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
132 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
133 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
137 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
141 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
142 TRACE("Flags: %d\n", prb
->SRB_Flags
);
143 if (TARGET_TO_HOST(prb
)) {
144 TRACE("\tData transfer: Target to host. Length checked.\n");
146 else if (HOST_TO_TARGET(prb
)) {
147 TRACE("\tData transfer: Host to target. Length checked.\n");
149 else if (NO_DATA_TRANSFERED(prb
)) {
150 TRACE("\tData transfer: none\n");
153 WARN("\tTransfer by scsi cmd. Length not checked\n");
156 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
157 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
158 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
159 TRACE("Target: %d\n", prb
->SRB_Target
);
160 TRACE("Lun: %d\n", prb
->SRB_Lun
);
161 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
162 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
163 TRACE("BufPtr: %lx (%p)\n", prb
->SRB_BufPointer
, lpBuf
);
164 TRACE("LinkPointer %lx\n", prb
->SRB_Rsvd1
);
165 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
166 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
167 cdb
= &prb
->CDBByte
[0];
168 cmd
= prb
->CDBByte
[0];
171 DPRINTF("CDB buffer[");
172 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
173 if (i
!= 0) DPRINTF(",");
174 DPRINTF("%02x", *cdb
++);
181 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16
*prb
)
188 cdb
= &prb
->CDBByte
[0];
189 DPRINTF("SenseArea[");
190 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
192 DPRINTF("%02x", *cdb
++);
199 ASPI_DebugPrintResult(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
206 /* translate real mode address */
207 if (prb
->SRB_BufPointer
)
208 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(prb
->SRB_BufPointer
), OFFSETOF(prb
->SRB_BufPointer
));
211 lpBuf
= MapSL(prb
->SRB_BufPointer
);
215 switch (prb
->CDBByte
[0]) {
217 TRACE("Vendor: '%s'\n", lpBuf
+ INQUIRY_VENDOR
);
219 case CMD_TEST_UNIT_READY
:
220 ASPI_PrintSenseArea16(prb
);
226 ASPI_ExecScsiCmd(DWORD ptrPRB
, UINT16 mode
)
228 SRB_ExecSCSICmd16
*lpPRB
= 0;
229 struct sg_header
*sg_hd
, *sg_reply_hdr
;
240 lpPRB
= PTR_REAL_TO_LIN( SELECTOROF(ptrPRB
), OFFSETOF(ptrPRB
));
243 lpPRB
= MapSL(ptrPRB
);
247 ASPI_DebugPrintCmd(lpPRB
, mode
);
249 fd
= ASPI_OpenDevice16(lpPRB
);
251 WARN("Failed: could not open device. Device permissions !?\n");
252 lpPRB
->SRB_Status
= SS_ERR
;
259 lpPRB
->SRB_Status
= SS_PENDING
;
264 /* translate real mode address */
266 lpBuf
= PTR_REAL_TO_LIN( SELECTOROF(lpPRB
->SRB_BufPointer
),
267 OFFSETOF(lpPRB
->SRB_BufPointer
));
270 lpBuf
= MapSL(lpPRB
->SRB_BufPointer
);
274 if (!lpPRB
->SRB_CDBLen
) {
275 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
276 lpPRB
->SRB_Status
= SS_ERR
;
280 /* build up sg_header + scsi cmd */
281 if (HOST_TO_TARGET(lpPRB
)) {
282 /* send header, command, and then data */
283 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
284 sg_hd
= (struct sg_header
*) malloc(in_len
);
285 memset(sg_hd
, 0, SCSI_OFF
);
286 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
287 if (lpPRB
->SRB_BufLen
) {
288 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpBuf
, lpPRB
->SRB_BufLen
);
292 /* send header and command - no data */
293 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
294 sg_hd
= (struct sg_header
*) malloc(in_len
);
295 memset(sg_hd
, 0, SCSI_OFF
);
296 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
299 if (TARGET_TO_HOST(lpPRB
)) {
300 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
301 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
302 memset(sg_reply_hdr
, 0, SCSI_OFF
);
303 sg_hd
->reply_len
= out_len
;
307 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
308 memset(sg_reply_hdr
, 0, SCSI_OFF
);
309 sg_hd
->reply_len
= out_len
;
312 status
= write(fd
, sg_hd
, in_len
);
313 if (status
< 0 || status
!= in_len
) {
314 int save_error
= errno
;
316 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len
, status
);
318 if (save_error
== ENOMEM
) {
319 MESSAGE("ASPI: Linux generic scsi driver\n You probably need to re-compile your kernel with a larger SG_BIG_BUFF value (sg.h)\n Suggest 130560\n");
321 WARN("error:= '%s'\n", strerror(save_error
));
326 status
= read(fd
, sg_reply_hdr
, out_len
);
327 if (status
< 0 || status
!= out_len
) {
328 WARN("not enough bytes read from scsi device%d\n", status
);
332 if (sg_reply_hdr
->result
!= 0) {
333 error_code
= sg_reply_hdr
->result
;
334 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
338 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
339 memcpy(lpBuf
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
342 /* copy in sense buffer to amount that is available in client */
343 if (lpPRB
->SRB_SenseLen
) {
344 int sense_len
= lpPRB
->SRB_SenseLen
;
345 if (lpPRB
->SRB_SenseLen
> 16)
347 memcpy(SENSE_BUFFER(lpPRB
), &sg_reply_hdr
->sense_buffer
[0], sense_len
);
351 lpPRB
->SRB_Status
= SS_COMP
;
352 lpPRB
->SRB_HaStat
= HASTAT_OK
;
353 lpPRB
->SRB_TargStat
= STATUS_GOOD
;
357 if (ASPI_POSTING(lpPRB
) && lpPRB
->SRB_PostProc
) {
358 TRACE("Post Routine (%lx) called\n", (DWORD
) lpPRB
->SRB_PostProc
);
363 SEGPTR spPRB
= MapLS(lpPRB
);
365 ASPI_CallTo16_long_l(lpPRB
->SRB_PostProc
, spPRB
);
370 ASPI_CallTo16_long_l(lpPRB
->SRB_PostProc
, ptrPRB
);
377 ASPI_DebugPrintResult(lpPRB
, mode
);
381 if (error_code
== EBUSY
) {
382 lpPRB
->SRB_Status
= SS_ASPI_IS_BUSY
;
383 TRACE("Device busy\n");
387 lpPRB
->SRB_Status
= SS_ERR
;
390 /* I'm not sure exactly error codes work here
391 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
393 WARN("error_exit\n");
396 return lpPRB
->SRB_Status
;
401 /***********************************************************************
402 * GetASPISupportInfo16 (WINASPI.1)
405 WORD WINAPI
GetASPISupportInfo16(void)
408 TRACE("GETASPISupportInfo16\n");
409 /* high byte SS_COMP - low byte number of host adapters */
410 return ((SS_COMP
<< 8) | HA_Count
);
412 return ((SS_NO_ASPI
<< 8) | 0);
417 DWORD
ASPI_SendASPICommand(DWORD ptrSRB
, UINT16 mode
)
426 lpSRB
= PTR_REAL_TO_LIN( SELECTOROF(ptrSRB
), OFFSETOF(ptrSRB
));
429 lpSRB
= MapSL(ptrSRB
);
432 /* This is not the post proc, it's the chain proc this time */
433 DWORD ret
= ASPI_CallTo16_long_l(ASPIChainFunc
, ptrSRB
);
436 lpSRB
->inquiry
.SRB_Status
= SS_INVALID_SRB
;
443 switch (lpSRB
->common
.SRB_Cmd
) {
445 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
446 if (lpSRB
->inquiry
.SRB_55AASignature
== 0x55aa) {
447 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
449 lpSRB
->inquiry
.SRB_ExtBufferSize
= 0x2000; /* bogus value */
450 lpSRB
->inquiry
.HA_Count
= HA_Count
;
451 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
452 strcat(lpSRB
->inquiry
.HA_ManagerId
, "Wine ASPI16"); /* max 15 chars */
453 strcat(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host
455 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
456 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
457 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
459 case SC_GET_DEV_TYPE
:
460 FIXME("Not implemented SC_GET_DEV_TYPE\n");
462 case SC_EXEC_SCSI_CMD
:
463 return ASPI_ExecScsiCmd((DWORD
)ptrSRB
, mode
);
466 FIXME("Not implemented SC_RESET_DEV\n");
469 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
472 return SS_INVALID_SRB
;
476 /***********************************************************************
477 * SendASPICommand16 (WINASPI.2)
479 WORD WINAPI
SendASPICommand16(SEGPTR segptr_srb
)
482 return ASPI_SendASPICommand(segptr_srb
, ASPI_WIN16
);
489 /***********************************************************************
490 * InsertInASPIChain16 (WINASPI.3)
492 WORD WINAPI
InsertInASPIChain16(BOOL16 remove
, FARPROC16 pASPIChainFunc
)
495 if (remove
== TRUE
) /* Remove */
497 if (ASPIChainFunc
== pASPIChainFunc
)
499 ASPIChainFunc
= NULL
;
504 if (remove
== FALSE
) /* Insert */
506 if (ASPIChainFunc
== NULL
)
508 ASPIChainFunc
= pASPIChainFunc
;
517 /***********************************************************************
518 * GetASPIDLLVersion16 (WINASPI.4)
521 DWORD WINAPI
GetASPIDLLVersion16()