20 #include "debugtools.h"
21 #include "selectors.h"
26 DEFAULT_DEBUG_CHANNEL(aspi
)
30 * 1) Residual byte length reporting not handled
31 * 2) Make this code re-entrant for multithreading
32 * 3) Only linux supported so far
37 static ASPI_DEVICE_INFO
*ASPI_open_devices
= NULL
;
39 static FARPROC16 ASPIChainFunc
= NULL
;
40 static WORD HA_Count
= 1; /* host adapter count; FIXME: detect it */
43 ASPI_OpenDevice16(SRB_ExecSCSICmd16
*prb
)
48 ASPI_DEVICE_INFO
*curr
;
50 /* search list of devices to see if we've opened it already.
51 * There is not an explicit open/close in ASPI land, so hopefully
52 * keeping a device open won't be a problem.
55 for (curr
= ASPI_open_devices
; curr
; curr
= curr
->next
) {
56 if (curr
->hostId
== prb
->SRB_HaId
&&
57 curr
->target
== prb
->SRB_Target
&&
58 curr
->lun
== prb
->SRB_Lun
) {
63 /* device wasn't cached, go ahead and open it */
64 sprintf(idstr
, "scsi c%1dt%1dd%1d", prb
->SRB_HaId
, prb
->SRB_Target
, prb
->SRB_Lun
);
66 if (!PROFILE_GetWineIniString(idstr
, "Device", "", device_str
, sizeof(device_str
))) {
67 TRACE("Trying to open unlisted scsi device %s\n", idstr
);
71 TRACE("Opening device %s=%s\n", idstr
, device_str
);
73 fd
= open(device_str
, O_RDWR
);
75 int save_error
= errno
;
77 ERR("Error opening device %s, error '%s'\n", device_str
, strerror(save_error
));
79 ERR("Error opening device %s, error %d\n", device_str
, save_error
);
84 /* device is now open */
85 curr
= HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO
) );
87 curr
->hostId
= prb
->SRB_HaId
;
88 curr
->target
= prb
->SRB_Target
;
89 curr
->lun
= prb
->SRB_Lun
;
91 /* insert new record at beginning of open device list */
92 curr
->next
= ASPI_open_devices
;
93 ASPI_open_devices
= curr
;
99 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
109 /* translate real mode address */
110 if (prb
->SRB_BufPointer
)
111 lpBuf
= (BYTE
*)DOSMEM_MapRealToLinear((UINT
)prb
->SRB_BufPointer
);
114 lpBuf
= PTR_SEG_TO_LIN(prb
->SRB_BufPointer
);
118 switch (prb
->CDBByte
[0]) {
121 TRACE("\tEVPD: %d\n", prb
->CDBByte
[1] & 1);
122 TRACE("\tLUN: %d\n", (prb
->CDBByte
[1] & 0xc) >> 1);
123 TRACE("\tPAGE CODE: %d\n", prb
->CDBByte
[2]);
124 TRACE("\tALLOCATION LENGTH: %d\n", prb
->CDBByte
[4]);
125 TRACE("\tCONTROL: %d\n", prb
->CDBByte
[5]);
129 TRACE("Transfer Length: %d\n", prb
->CDBByte
[4]);
133 TRACE("Host Adapter: %d\n", prb
->SRB_HaId
);
134 TRACE("Flags: %d\n", prb
->SRB_Flags
);
135 if (TARGET_TO_HOST(prb
)) {
136 TRACE("\tData transfer: Target to host. Length checked.\n");
138 else if (HOST_TO_TARGET(prb
)) {
139 TRACE("\tData transfer: Host to target. Length checked.\n");
141 else if (NO_DATA_TRANSFERED(prb
)) {
142 TRACE("\tData transfer: none\n");
145 WARN("\tTransfer by scsi cmd. Length not checked\n");
148 TRACE("\tResidual byte length reporting %s\n", prb
->SRB_Flags
& 0x4 ? "enabled" : "disabled");
149 TRACE("\tLinking %s\n", prb
->SRB_Flags
& 0x2 ? "enabled" : "disabled");
150 TRACE("\tPosting %s\n", prb
->SRB_Flags
& 0x1 ? "enabled" : "disabled");
151 TRACE("Target: %d\n", prb
->SRB_Target
);
152 TRACE("Lun: %d\n", prb
->SRB_Lun
);
153 TRACE("BufLen: %ld\n", prb
->SRB_BufLen
);
154 TRACE("SenseLen: %d\n", prb
->SRB_SenseLen
);
155 TRACE("BufPtr: %lx (%p)\n", prb
->SRB_BufPointer
, lpBuf
);
156 TRACE("LinkPointer %lx\n", prb
->SRB_Rsvd1
);
157 TRACE("CDB Length: %d\n", prb
->SRB_CDBLen
);
158 TRACE("POST Proc: %lx\n", (DWORD
) prb
->SRB_PostProc
);
159 cdb
= &prb
->CDBByte
[0];
160 cmd
= prb
->CDBByte
[0];
163 DPRINTF("CDB buffer[");
164 for (i
= 0; i
< prb
->SRB_CDBLen
; i
++) {
165 if (i
!= 0) DPRINTF(",");
166 DPRINTF("%02x", *cdb
++);
173 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16
*prb
)
180 cdb
= &prb
->CDBByte
[0];
181 DPRINTF("SenseArea[");
182 for (i
= 0; i
< prb
->SRB_SenseLen
; i
++) {
184 DPRINTF("%02x", *cdb
++);
191 ASPI_DebugPrintResult(SRB_ExecSCSICmd16
*prb
, UINT16 mode
)
198 /* translate real mode address */
199 if (prb
->SRB_BufPointer
)
200 lpBuf
= (BYTE
*)DOSMEM_MapRealToLinear((UINT
)prb
->SRB_BufPointer
);
203 lpBuf
= PTR_SEG_TO_LIN(prb
->SRB_BufPointer
);
207 switch (prb
->CDBByte
[0]) {
209 TRACE("Vendor: '%s'\n", lpBuf
+ INQUIRY_VENDOR
);
211 case CMD_TEST_UNIT_READY
:
212 ASPI_PrintSenseArea16(prb
);
218 ASPI_ExecScsiCmd(DWORD ptrPRB
, UINT16 mode
)
220 SRB_ExecSCSICmd16
*lpPRB
= 0;
221 struct sg_header
*sg_hd
, *sg_reply_hdr
;
232 lpPRB
= (SRB_ExecSCSICmd16
*)DOSMEM_MapRealToLinear(ptrPRB
);
235 lpPRB
= PTR_SEG_TO_LIN(ptrPRB
);
239 ASPI_DebugPrintCmd(lpPRB
, mode
);
241 fd
= ASPI_OpenDevice16(lpPRB
);
243 WARN("Failed: could not open device. Device permissions !?\n");
244 lpPRB
->SRB_Status
= SS_ERR
;
251 lpPRB
->SRB_Status
= SS_PENDING
;
256 /* translate real mode address */
258 lpBuf
= (BYTE
*)DOSMEM_MapRealToLinear((UINT
)lpPRB
->SRB_BufPointer
);
261 lpBuf
= PTR_SEG_TO_LIN(lpPRB
->SRB_BufPointer
);
265 if (!lpPRB
->SRB_CDBLen
) {
266 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
267 lpPRB
->SRB_Status
= SS_ERR
;
271 /* build up sg_header + scsi cmd */
272 if (HOST_TO_TARGET(lpPRB
)) {
273 /* send header, command, and then data */
274 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
+ lpPRB
->SRB_BufLen
;
275 sg_hd
= (struct sg_header
*) malloc(in_len
);
276 memset(sg_hd
, 0, SCSI_OFF
);
277 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
278 if (lpPRB
->SRB_BufLen
) {
279 memcpy(((BYTE
*) sg_hd
) + SCSI_OFF
+ lpPRB
->SRB_CDBLen
, lpBuf
, lpPRB
->SRB_BufLen
);
283 /* send header and command - no data */
284 in_len
= SCSI_OFF
+ lpPRB
->SRB_CDBLen
;
285 sg_hd
= (struct sg_header
*) malloc(in_len
);
286 memset(sg_hd
, 0, SCSI_OFF
);
287 memcpy(sg_hd
+ 1, &lpPRB
->CDBByte
[0], lpPRB
->SRB_CDBLen
);
290 if (TARGET_TO_HOST(lpPRB
)) {
291 out_len
= SCSI_OFF
+ lpPRB
->SRB_BufLen
;
292 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
293 memset(sg_reply_hdr
, 0, SCSI_OFF
);
294 sg_hd
->reply_len
= out_len
;
298 sg_reply_hdr
= (struct sg_header
*) malloc(out_len
);
299 memset(sg_reply_hdr
, 0, SCSI_OFF
);
300 sg_hd
->reply_len
= out_len
;
303 status
= write(fd
, sg_hd
, in_len
);
304 if (status
< 0 || status
!= in_len
) {
305 int save_error
= errno
;
307 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len
, status
);
309 if (save_error
== ENOMEM
) {
310 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");
313 WARN("error:= '%s'\n", strerror(save_error
));
315 WARN("error:= %d\n", save_error
);
321 status
= read(fd
, sg_reply_hdr
, out_len
);
322 if (status
< 0 || status
!= out_len
) {
323 WARN("not enough bytes read from scsi device%d\n", status
);
327 if (sg_reply_hdr
->result
!= 0) {
328 error_code
= sg_reply_hdr
->result
;
329 WARN("reply header error (%d)\n", sg_reply_hdr
->result
);
333 if (TARGET_TO_HOST(lpPRB
) && lpPRB
->SRB_BufLen
) {
334 memcpy(lpBuf
, sg_reply_hdr
+ 1, lpPRB
->SRB_BufLen
);
337 /* copy in sense buffer to amount that is available in client */
338 if (lpPRB
->SRB_SenseLen
) {
339 int sense_len
= lpPRB
->SRB_SenseLen
;
340 if (lpPRB
->SRB_SenseLen
> 16)
342 memcpy(SENSE_BUFFER(lpPRB
), &sg_reply_hdr
->sense_buffer
[0], sense_len
);
346 lpPRB
->SRB_Status
= SS_COMP
;
347 lpPRB
->SRB_HaStat
= HASTAT_OK
;
348 lpPRB
->SRB_TargStat
= STATUS_GOOD
;
352 if (ASPI_POSTING(lpPRB
) && lpPRB
->SRB_PostProc
) {
353 TRACE("Post Routine (%lx) called\n", (DWORD
) lpPRB
->SRB_PostProc
);
358 SEGPTR spPRB
= MapLS(lpPRB
);
360 Callbacks
->CallASPIPostProc(lpPRB
->SRB_PostProc
, spPRB
);
365 Callbacks
->CallASPIPostProc(lpPRB
->SRB_PostProc
, ptrPRB
);
372 ASPI_DebugPrintResult(lpPRB
, mode
);
376 if (error_code
== EBUSY
) {
377 lpPRB
->SRB_Status
= SS_ASPI_IS_BUSY
;
378 TRACE("Device busy\n");
382 lpPRB
->SRB_Status
= SS_ERR
;
385 /* I'm not sure exactly error codes work here
386 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
388 WARN("error_exit\n");
391 return lpPRB
->SRB_Status
;
396 /***********************************************************************
397 * GetASPISupportInfo16 (WINASPI.1)
400 WORD WINAPI
GetASPISupportInfo16()
403 TRACE("GETASPISupportInfo16\n");
404 /* high byte SS_COMP - low byte number of host adapters */
405 return ((SS_COMP
<< 8) | HA_Count
);
407 return ((SS_NO_ASPI
<< 8) | 0);
412 DWORD
ASPI_SendASPICommand(DWORD ptrSRB
, UINT16 mode
)
421 lpSRB
= (LPSRB16
)DOSMEM_MapRealToLinear(ptrSRB
);
424 lpSRB
= PTR_SEG_TO_LIN(ptrSRB
);
427 /* This is not the post proc, it's the chain proc this time */
428 DWORD ret
= Callbacks
->CallASPIPostProc(ASPIChainFunc
, ptrSRB
);
431 lpSRB
->inquiry
.SRB_Status
= SS_INVALID_SRB
;
438 switch (lpSRB
->common
.SRB_Cmd
) {
440 lpSRB
->inquiry
.SRB_Status
= SS_COMP
; /* completed successfully */
441 if (lpSRB
->inquiry
.SRB_55AASignature
== 0x55aa) {
442 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
444 lpSRB
->inquiry
.SRB_ExtBufferSize
= 0x2000; /* bogus value */
445 lpSRB
->inquiry
.HA_Count
= HA_Count
;
446 lpSRB
->inquiry
.HA_SCSI_ID
= 7; /* not always ID 7 */
447 strcat(lpSRB
->inquiry
.HA_ManagerId
, "Wine ASPI16"); /* max 15 chars */
448 strcat(lpSRB
->inquiry
.HA_Identifier
, "Wine host"); /* FIXME: return host
450 memset(lpSRB
->inquiry
.HA_Unique
, 0, 16); /* default HA_Unique content */
451 lpSRB
->inquiry
.HA_Unique
[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
452 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB
->inquiry
.SRB_HaId
);
454 case SC_GET_DEV_TYPE
:
455 FIXME("Not implemented SC_GET_DEV_TYPE\n");
457 case SC_EXEC_SCSI_CMD
:
458 return ASPI_ExecScsiCmd((DWORD
)ptrSRB
, mode
);
461 FIXME("Not implemented SC_RESET_DEV\n");
464 FIXME("Unknown command %d\n", lpSRB
->common
.SRB_Cmd
);
467 return SS_INVALID_SRB
;
471 /***********************************************************************
472 * SendASPICommand16 (WINASPI.2)
474 WORD WINAPI
SendASPICommand16(SEGPTR segptr_srb
)
477 return ASPI_SendASPICommand(segptr_srb
, ASPI_WIN16
);
484 /***********************************************************************
485 * InsertInASPIChain16 (WINASPI.3)
487 WORD WINAPI
InsertInASPIChain16(BOOL16 remove
, FARPROC16 pASPIChainFunc
)
490 if (remove
== TRUE
) /* Remove */
492 if (ASPIChainFunc
== pASPIChainFunc
)
494 ASPIChainFunc
= NULL
;
499 if (remove
== FALSE
) /* Insert */
501 if (ASPIChainFunc
== NULL
)
503 ASPIChainFunc
= pASPIChainFunc
;
512 /***********************************************************************
513 * GetASPIDLLVersion16 (WINASPI.4)
516 DWORD WINAPI
GetASPIDLLVersion16()