18 #include "debugtools.h"
19 #include "selectors.h"
24 DEFAULT_DEBUG_CHANNEL(aspi)
28 * 1) Residual byte length reporting not handled
29 * 2) Make this code re-entrant for multithreading
30 * 3) Only linux supported so far
35 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
37 static FARPROC16 ASPIChainFunc = NULL;
38 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
41 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
46 ASPI_DEVICE_INFO *curr;
48 /* search list of devices to see if we've opened it already.
49 * There is not an explicit open/close in ASPI land, so hopefully
50 * keeping a device open won't be a problem.
53 for (curr = ASPI_open_devices; curr; curr = curr->next) {
54 if (curr->hostId == prb->SRB_HaId &&
55 curr->target == prb->SRB_Target &&
56 curr->lun == prb->SRB_Lun) {
61 /* device wasn't cached, go ahead and open it */
62 sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
64 if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
65 TRACE("Trying to open unlisted scsi device %s\n", idstr);
69 TRACE("Opening device %s=%s\n", idstr, device_str);
71 fd = open(device_str, O_RDWR);
73 int save_error = errno;
75 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
77 ERR("Error opening device %s, error %d\n", device_str, save_error);
82 /* device is now open */
83 curr = HeapAlloc( SystemHeap, 0, sizeof(ASPI_DEVICE_INFO) );
85 curr->hostId = prb->SRB_HaId;
86 curr->target = prb->SRB_Target;
87 curr->lun = prb->SRB_Lun;
89 /* insert new record at beginning of open device list */
90 curr->next = ASPI_open_devices;
91 ASPI_open_devices = curr;
97 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
107 /* translate real mode address */
108 if (prb->SRB_BufPointer)
109 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
112 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
116 switch (prb->CDBByte[0]) {
119 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
120 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
121 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
122 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
123 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
127 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
131 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
132 TRACE("Flags: %d\n", prb->SRB_Flags);
133 if (TARGET_TO_HOST(prb)) {
134 TRACE("\tData transfer: Target to host. Length checked.\n");
136 else if (HOST_TO_TARGET(prb)) {
137 TRACE("\tData transfer: Host to target. Length checked.\n");
139 else if (NO_DATA_TRANSFERED(prb)) {
140 TRACE("\tData transfer: none\n");
143 WARN("\tTransfer by scsi cmd. Length not checked\n");
146 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
147 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
148 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
149 TRACE("Target: %d\n", prb->SRB_Target);
150 TRACE("Lun: %d\n", prb->SRB_Lun);
151 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
152 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
153 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
154 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
155 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
156 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
157 cdb = &prb->CDBByte[0];
158 cmd = prb->CDBByte[0];
161 DPRINTF("CDB buffer[");
162 for (i = 0; i < prb->SRB_CDBLen; i++) {
163 if (i != 0) DPRINTF(",");
164 DPRINTF("%02x", *cdb++);
171 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
178 cdb = &prb->CDBByte[0];
179 DPRINTF("SenseArea[");
180 for (i = 0; i < prb->SRB_SenseLen; i++) {
182 DPRINTF("%02x", *cdb++);
189 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
196 /* translate real mode address */
197 if (prb->SRB_BufPointer)
198 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
201 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
205 switch (prb->CDBByte[0]) {
207 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
209 case CMD_TEST_UNIT_READY:
210 ASPI_PrintSenseArea16(prb);
216 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
218 SRB_ExecSCSICmd16 *lpPRB = 0;
219 struct sg_header *sg_hd, *sg_reply_hdr;
230 lpPRB = (SRB_ExecSCSICmd16 *)DOSMEM_MapRealToLinear(ptrPRB);
233 lpPRB = PTR_SEG_TO_LIN(ptrPRB);
237 ASPI_DebugPrintCmd(lpPRB, mode);
239 fd = ASPI_OpenDevice16(lpPRB);
241 WARN("Failed: could not open device. Device permissions !?\n");
242 lpPRB->SRB_Status = SS_ERR;
249 lpPRB->SRB_Status = SS_PENDING;
254 /* translate real mode address */
256 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)lpPRB->SRB_BufPointer);
259 lpBuf = PTR_SEG_TO_LIN(lpPRB->SRB_BufPointer);
263 if (!lpPRB->SRB_CDBLen) {
264 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
265 lpPRB->SRB_Status = SS_ERR;
269 /* build up sg_header + scsi cmd */
270 if (HOST_TO_TARGET(lpPRB)) {
271 /* send header, command, and then data */
272 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
273 sg_hd = (struct sg_header *) malloc(in_len);
274 memset(sg_hd, 0, SCSI_OFF);
275 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
276 if (lpPRB->SRB_BufLen) {
277 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
281 /* send header and command - no data */
282 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
283 sg_hd = (struct sg_header *) malloc(in_len);
284 memset(sg_hd, 0, SCSI_OFF);
285 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
288 if (TARGET_TO_HOST(lpPRB)) {
289 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
290 sg_reply_hdr = (struct sg_header *) malloc(out_len);
291 memset(sg_reply_hdr, 0, SCSI_OFF);
292 sg_hd->reply_len = out_len;
296 sg_reply_hdr = (struct sg_header *) malloc(out_len);
297 memset(sg_reply_hdr, 0, SCSI_OFF);
298 sg_hd->reply_len = out_len;
301 status = write(fd, sg_hd, in_len);
302 if (status < 0 || status != in_len) {
303 int save_error = errno;
305 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
307 if (save_error == ENOMEM) {
308 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");
311 WARN("error:= '%s'\n", strerror(save_error));
313 WARN("error:= %d\n", save_error);
319 status = read(fd, sg_reply_hdr, out_len);
320 if (status < 0 || status != out_len) {
321 WARN("not enough bytes read from scsi device%d\n", status);
325 if (sg_reply_hdr->result != 0) {
326 error_code = sg_reply_hdr->result;
327 WARN("reply header error (%d)\n", sg_reply_hdr->result);
331 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
332 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
335 /* copy in sense buffer to amount that is available in client */
336 if (lpPRB->SRB_SenseLen) {
337 int sense_len = lpPRB->SRB_SenseLen;
338 if (lpPRB->SRB_SenseLen > 16)
340 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
344 lpPRB->SRB_Status = SS_COMP;
345 lpPRB->SRB_HaStat = HASTAT_OK;
346 lpPRB->SRB_TargStat = STATUS_GOOD;
350 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
351 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
356 SEGPTR spPRB = MapLS(lpPRB);
358 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, spPRB);
363 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, ptrPRB);
370 ASPI_DebugPrintResult(lpPRB, mode);
374 if (error_code == EBUSY) {
375 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
376 TRACE("Device busy\n");
380 lpPRB->SRB_Status = SS_ERR;
383 /* I'm not sure exactly error codes work here
384 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
386 WARN("error_exit\n");
389 return lpPRB->SRB_Status;
394 /***********************************************************************
395 * GetASPISupportInfo16 (WINASPI.1)
398 WORD WINAPI GetASPISupportInfo16()
401 TRACE("GETASPISupportInfo16\n");
402 /* high byte SS_COMP - low byte number of host adapters */
403 return ((SS_COMP << 8) | HA_Count);
405 return ((SS_NO_ASPI << 8) | 0);
410 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
419 lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(ptrSRB);
422 lpSRB = PTR_SEG_TO_LIN(ptrSRB);
425 /* This is not the post proc, it's the chain proc this time */
426 DWORD ret = Callbacks->CallASPIPostProc(ASPIChainFunc, ptrSRB);
429 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
436 switch (lpSRB->common.SRB_Cmd) {
438 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
439 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
440 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
442 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
443 lpSRB->inquiry.HA_Count = HA_Count;
444 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
445 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
446 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
448 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
449 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
450 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
452 case SC_GET_DEV_TYPE:
453 FIXME("Not implemented SC_GET_DEV_TYPE\n");
455 case SC_EXEC_SCSI_CMD:
456 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
459 FIXME("Not implemented SC_RESET_DEV\n");
462 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
465 return SS_INVALID_SRB;
469 /***********************************************************************
470 * SendASPICommand16 (WINASPI.2)
472 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
475 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
482 /***********************************************************************
483 * InsertInASPIChain16 (WINASPI.3)
485 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
488 if (remove == TRUE) /* Remove */
490 if (ASPIChainFunc == pASPIChainFunc)
492 ASPIChainFunc = NULL;
497 if (remove == FALSE) /* Insert */
499 if (ASPIChainFunc == NULL)
501 ASPIChainFunc = pASPIChainFunc;
510 /***********************************************************************
511 * GetASPIDLLVersion16 (WINASPI.4)
514 DWORD WINAPI GetASPIDLLVersion16()
524 void WINAPI ASPI_DOS_func(CONTEXT86 *context)
526 WORD *stack = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
527 DWORD ptrSRB = *(DWORD *)&stack[2];
529 ASPI_SendASPICommand(ptrSRB, ASPI_DOS);
531 /* simulate a normal RETF sequence as required by DPMI CallRMProcFar */
532 EIP_reg(context) = *(stack++);
533 CS_reg(context) = *(stack++);
534 ESP_reg(context) += 2*sizeof(WORD);
538 /* returns the address of a real mode callback to ASPI_DOS_func() */
539 void ASPI_DOS_HandleInt(CONTEXT86 *context)
542 FARPROC16 *p = (FARPROC16 *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
543 if ((CX_reg(context) == 4) || (CX_reg(context) == 5))
545 *p = DPMI_AllocInternalRMCB(ASPI_DOS_func);
546 TRACE("allocated real mode proc %p\n", *p);
547 AX_reg(context) = CX_reg(context);