16 #include "debugtools.h"
17 #include "selectors.h"
20 DEFAULT_DEBUG_CHANNEL(aspi)
24 * 1) Residual byte length reporting not handled
25 * 2) Make this code re-entrant for multithreading
26 * 3) Only linux supported so far
31 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
33 static FARPROC16 ASPIChainFunc = NULL;
34 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
37 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
42 ASPI_DEVICE_INFO *curr;
44 /* search list of devices to see if we've opened it already.
45 * There is not an explicit open/close in ASPI land, so hopefully
46 * keeping a device open won't be a problem.
49 for (curr = ASPI_open_devices; curr; curr = curr->next) {
50 if (curr->hostId == prb->SRB_HaId &&
51 curr->target == prb->SRB_Target &&
52 curr->lun == prb->SRB_Lun) {
57 /* device wasn't cached, go ahead and open it */
58 sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
60 if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
61 TRACE("Trying to open unlisted scsi device %s\n", idstr);
65 TRACE("Opening device %s=%s\n", idstr, device_str);
67 fd = open(device_str, O_RDWR);
69 int save_error = errno;
71 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
73 ERR("Error opening device %s, error %d\n", device_str, save_error);
78 /* device is now open */
79 curr = HeapAlloc( SystemHeap, 0, sizeof(ASPI_DEVICE_INFO) );
81 curr->hostId = prb->SRB_HaId;
82 curr->target = prb->SRB_Target;
83 curr->lun = prb->SRB_Lun;
85 /* insert new record at beginning of open device list */
86 curr->next = ASPI_open_devices;
87 ASPI_open_devices = curr;
93 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
99 dbg_decl_str(aspi, 512);
104 /* translate real mode address */
105 if (prb->SRB_BufPointer)
106 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
109 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
113 switch (prb->CDBByte[0]) {
116 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
117 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
118 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
119 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
120 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
124 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
128 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
129 TRACE("Flags: %d\n", prb->SRB_Flags);
130 if (TARGET_TO_HOST(prb)) {
131 TRACE("\tData transfer: Target to host. Length checked.\n");
133 else if (HOST_TO_TARGET(prb)) {
134 TRACE("\tData transfer: Host to target. Length checked.\n");
136 else if (NO_DATA_TRANSFERED(prb)) {
137 TRACE("\tData transfer: none\n");
140 WARN("\tTransfer by scsi cmd. Length not checked\n");
143 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
144 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
145 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
146 TRACE("Target: %d\n", prb->SRB_Target);
147 TRACE("Lun: %d\n", prb->SRB_Lun);
148 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
149 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
150 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
151 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
152 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
153 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
154 cdb = &prb->CDBByte[0];
155 cmd = prb->CDBByte[0];
156 for (i = 0; i < prb->SRB_CDBLen; i++) {
157 if (i != 0) dsprintf(aspi, ",");
158 dsprintf(aspi, "%02x", *cdb++);
160 TRACE("CDB buffer[%s]\n", dbg_str(aspi));
164 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
168 dbg_decl_str(aspi, 512);
170 cdb = &prb->CDBByte[0];
171 for (i = 0; i < prb->SRB_SenseLen; i++) {
172 if (i) dsprintf(aspi, ",");
173 dsprintf(aspi, "%02x", *cdb++);
175 TRACE("SenseArea[%s]\n", dbg_str(aspi));
179 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
186 /* translate real mode address */
187 if (prb->SRB_BufPointer)
188 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
191 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
195 switch (prb->CDBByte[0]) {
197 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
199 case CMD_TEST_UNIT_READY:
200 ASPI_PrintSenseArea16(prb);
206 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
208 SRB_ExecSCSICmd16 *lpPRB = 0;
209 struct sg_header *sg_hd, *sg_reply_hdr;
220 lpPRB = (SRB_ExecSCSICmd16 *)DOSMEM_MapRealToLinear(ptrPRB);
223 lpPRB = PTR_SEG_TO_LIN(ptrPRB);
227 ASPI_DebugPrintCmd(lpPRB, mode);
229 fd = ASPI_OpenDevice16(lpPRB);
231 WARN("Failed: could not open device. Device permissions !?\n");
232 lpPRB->SRB_Status = SS_ERR;
239 lpPRB->SRB_Status = SS_PENDING;
244 /* translate real mode address */
246 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)lpPRB->SRB_BufPointer);
249 lpBuf = PTR_SEG_TO_LIN(lpPRB->SRB_BufPointer);
253 if (!lpPRB->SRB_CDBLen) {
254 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
255 lpPRB->SRB_Status = SS_ERR;
259 /* build up sg_header + scsi cmd */
260 if (HOST_TO_TARGET(lpPRB)) {
261 /* send header, command, and then data */
262 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
263 sg_hd = (struct sg_header *) malloc(in_len);
264 memset(sg_hd, 0, SCSI_OFF);
265 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
266 if (lpPRB->SRB_BufLen) {
267 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
271 /* send header and command - no data */
272 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
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);
278 if (TARGET_TO_HOST(lpPRB)) {
279 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
280 sg_reply_hdr = (struct sg_header *) malloc(out_len);
281 memset(sg_reply_hdr, 0, SCSI_OFF);
282 sg_hd->reply_len = out_len;
286 sg_reply_hdr = (struct sg_header *) malloc(out_len);
287 memset(sg_reply_hdr, 0, SCSI_OFF);
288 sg_hd->reply_len = out_len;
291 status = write(fd, sg_hd, in_len);
292 if (status < 0 || status != in_len) {
293 int save_error = errno;
295 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
297 if (save_error == ENOMEM) {
298 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");
301 WARN("error:= '%s'\n", strerror(save_error));
303 WARN("error:= %d\n", save_error);
309 status = read(fd, sg_reply_hdr, out_len);
310 if (status < 0 || status != out_len) {
311 WARN("not enough bytes read from scsi device%d\n", status);
315 if (sg_reply_hdr->result != 0) {
316 error_code = sg_reply_hdr->result;
317 WARN("reply header error (%d)\n", sg_reply_hdr->result);
321 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
322 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
325 /* copy in sense buffer to amount that is available in client */
326 if (lpPRB->SRB_SenseLen) {
327 int sense_len = lpPRB->SRB_SenseLen;
328 if (lpPRB->SRB_SenseLen > 16)
330 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
334 lpPRB->SRB_Status = SS_COMP;
335 lpPRB->SRB_HaStat = HASTAT_OK;
336 lpPRB->SRB_TargStat = STATUS_GOOD;
340 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
341 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
346 SEGPTR spPRB = MapLS(lpPRB);
348 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, spPRB);
353 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, ptrPRB);
360 ASPI_DebugPrintResult(lpPRB, mode);
364 if (error_code == EBUSY) {
365 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
366 TRACE("Device busy\n");
370 lpPRB->SRB_Status = SS_ERR;
373 /* I'm not sure exactly error codes work here
374 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
376 WARN("error_exit\n");
379 return lpPRB->SRB_Status;
384 /***********************************************************************
385 * GetASPISupportInfo16 (WINASPI.1)
388 WORD WINAPI GetASPISupportInfo16()
391 TRACE("GETASPISupportInfo16\n");
392 /* high byte SS_COMP - low byte number of host adapters */
393 return ((SS_COMP << 8) | HA_Count);
395 return ((SS_NO_ASPI << 8) | 0);
400 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
409 lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(ptrSRB);
412 lpSRB = PTR_SEG_TO_LIN(ptrSRB);
415 /* This is not the post proc, it's the chain proc this time */
416 DWORD ret = Callbacks->CallASPIPostProc(ASPIChainFunc, ptrSRB);
419 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
426 switch (lpSRB->common.SRB_Cmd) {
428 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
429 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
430 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
432 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
433 lpSRB->inquiry.HA_Count = HA_Count;
434 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
435 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
436 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
438 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
439 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
440 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
442 case SC_GET_DEV_TYPE:
443 FIXME("Not implemented SC_GET_DEV_TYPE\n");
445 case SC_EXEC_SCSI_CMD:
446 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
449 FIXME("Not implemented SC_RESET_DEV\n");
452 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
455 return SS_INVALID_SRB;
459 /***********************************************************************
460 * SendASPICommand16 (WINASPI.2)
462 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
465 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
472 /***********************************************************************
473 * InsertInASPIChain16 (WINASPI.3)
475 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
478 if (remove == TRUE) /* Remove */
480 if (ASPIChainFunc == pASPIChainFunc)
482 ASPIChainFunc = NULL;
487 if (remove == FALSE) /* Insert */
489 if (ASPIChainFunc == NULL)
491 ASPIChainFunc = pASPIChainFunc;
500 /***********************************************************************
501 * GetASPIDLLVersion16 (WINASPI.4)
504 DWORD WINAPI GetASPIDLLVersion16()
514 void WINAPI ASPI_DOS_func(CONTEXT86 *context)
516 WORD *stack = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
517 DWORD ptrSRB = *(DWORD *)&stack[2];
519 ASPI_SendASPICommand(ptrSRB, ASPI_DOS);
521 /* simulate a normal RETF sequence as required by DPMI CallRMProcFar */
522 IP_reg(context) = *(stack++);
523 CS_reg(context) = *(stack++);
524 SP_reg(context) += 2*sizeof(WORD);
528 /* returns the address of a real mode callback to ASPI_DOS_func() */
529 void ASPI_DOS_HandleInt(CONTEXT86 *context)
532 FARPROC16 *p = (FARPROC16 *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
533 if ((CX_reg(context) == 4) || (CX_reg(context) == 5))
535 *p = DPMI_AllocInternalRMCB(ASPI_DOS_func);
536 TRACE("allocated real mode proc %p\n", *p);
537 AX_reg(context) = CX_reg(context);