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 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
36 ASPI_DEVICE_INFO *curr;
38 /* search list of devices to see if we've opened it already.
39 * There is not an explicit open/close in ASPI land, so hopefully
40 * keeping a device open won't be a problem.
43 for (curr = ASPI_open_devices; curr; curr = curr->next) {
44 if (curr->hostId == prb->SRB_HaId &&
45 curr->target == prb->SRB_Target &&
46 curr->lun == prb->SRB_Lun) {
51 /* device wasn't cached, go ahead and open it */
52 sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
54 if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
55 TRACE(aspi, "Trying to open unlisted scsi device %s\n", idstr);
59 TRACE(aspi, "Opening device %s=%s\n", idstr, device_str);
61 fd = open(device_str, O_RDWR);
63 int save_error = errno;
65 ERR(aspi, "Error opening device %s, error '%s'\n", device_str, strerror(save_error));
67 ERR(aspi, "Error opening device %s, error %d\n", device_str, save_error);
72 /* device is now open */
73 curr = HeapAlloc( SystemHeap, 0, sizeof(ASPI_DEVICE_INFO) );
75 curr->hostId = prb->SRB_HaId;
76 curr->target = prb->SRB_Target;
77 curr->lun = prb->SRB_Lun;
79 /* insert new record at beginning of open device list */
80 curr->next = ASPI_open_devices;
81 ASPI_open_devices = curr;
87 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
93 dbg_decl_str(aspi, 512);
98 /* translate real mode address */
99 if (prb->SRB_BufPointer)
100 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
103 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
107 switch (prb->CDBByte[0]) {
110 TRACE(aspi, "\tEVPD: %d\n", prb->CDBByte[1] & 1);
111 TRACE(aspi, "\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
112 TRACE(aspi, "\tPAGE CODE: %d\n", prb->CDBByte[2]);
113 TRACE(aspi, "\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
114 TRACE(aspi, "\tCONTROL: %d\n", prb->CDBByte[5]);
118 TRACE(aspi, "Transfer Length: %d\n", prb->CDBByte[4]);
122 TRACE(aspi, "Host Adapter: %d\n", prb->SRB_HaId);
123 TRACE(aspi, "Flags: %d\n", prb->SRB_Flags);
124 if (TARGET_TO_HOST(prb)) {
125 TRACE(aspi, "\tData transfer: Target to host. Length checked.\n");
127 else if (HOST_TO_TARGET(prb)) {
128 TRACE(aspi, "\tData transfer: Host to target. Length checked.\n");
130 else if (NO_DATA_TRANSFERED(prb)) {
131 TRACE(aspi, "\tData transfer: none\n");
134 WARN(aspi, "\tTransfer by scsi cmd. Length not checked\n");
137 TRACE(aspi, "\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
138 TRACE(aspi, "\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
139 TRACE(aspi, "\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
140 TRACE(aspi, "Target: %d\n", prb->SRB_Target);
141 TRACE(aspi, "Lun: %d\n", prb->SRB_Lun);
142 TRACE(aspi, "BufLen: %ld\n", prb->SRB_BufLen);
143 TRACE(aspi, "SenseLen: %d\n", prb->SRB_SenseLen);
144 TRACE(aspi, "BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
145 TRACE(aspi, "LinkPointer %lx\n", prb->SRB_Rsvd1);
146 TRACE(aspi, "CDB Length: %d\n", prb->SRB_CDBLen);
147 TRACE(aspi, "POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
148 cdb = &prb->CDBByte[0];
149 cmd = prb->CDBByte[0];
150 for (i = 0; i < prb->SRB_CDBLen; i++) {
151 if (i != 0) dsprintf(aspi, ",");
152 dsprintf(aspi, "%02x", *cdb++);
154 TRACE(aspi, "CDB buffer[%s]\n", dbg_str(aspi));
158 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
162 dbg_decl_str(aspi, 512);
164 cdb = &prb->CDBByte[0];
165 for (i = 0; i < prb->SRB_SenseLen; i++) {
166 if (i) dsprintf(aspi, ",");
167 dsprintf(aspi, "%02x", *cdb++);
169 TRACE(aspi, "SenseArea[%s]\n", dbg_str(aspi));
173 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
180 /* translate real mode address */
181 if (prb->SRB_BufPointer)
182 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
185 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
189 switch (prb->CDBByte[0]) {
191 TRACE(aspi, "Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
193 case CMD_TEST_UNIT_READY:
194 ASPI_PrintSenseArea16(prb);
200 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
202 SRB_ExecSCSICmd16 *lpPRB = 0;
203 struct sg_header *sg_hd, *sg_reply_hdr;
214 lpPRB = (SRB_ExecSCSICmd16 *)DOSMEM_MapRealToLinear(ptrPRB);
217 lpPRB = PTR_SEG_TO_LIN(ptrPRB);
221 ASPI_DebugPrintCmd(lpPRB, mode);
223 fd = ASPI_OpenDevice16(lpPRB);
225 WARN(aspi, "Failed: could not open device. Device permissions !?\n");
226 lpPRB->SRB_Status = SS_ERR;
233 lpPRB->SRB_Status = SS_PENDING;
238 /* translate real mode address */
240 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)lpPRB->SRB_BufPointer);
243 lpBuf = PTR_SEG_TO_LIN(lpPRB->SRB_BufPointer);
247 if (!lpPRB->SRB_CDBLen) {
248 WARN(aspi, "Failed: lpPRB->SRB_CDBLen = 0.\n");
249 lpPRB->SRB_Status = SS_ERR;
253 /* build up sg_header + scsi cmd */
254 if (HOST_TO_TARGET(lpPRB)) {
255 /* send header, command, and then data */
256 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
257 sg_hd = (struct sg_header *) malloc(in_len);
258 memset(sg_hd, 0, SCSI_OFF);
259 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
260 if (lpPRB->SRB_BufLen) {
261 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
265 /* send header and command - no data */
266 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
267 sg_hd = (struct sg_header *) malloc(in_len);
268 memset(sg_hd, 0, SCSI_OFF);
269 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
272 if (TARGET_TO_HOST(lpPRB)) {
273 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
274 sg_reply_hdr = (struct sg_header *) malloc(out_len);
275 memset(sg_reply_hdr, 0, SCSI_OFF);
276 sg_hd->reply_len = out_len;
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;
285 status = write(fd, sg_hd, in_len);
286 if (status < 0 || status != in_len) {
287 int save_error = errno;
289 WARN(aspi, "Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
291 if (save_error == ENOMEM) {
292 MSG("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");
295 WARN(aspi, "error:= '%s'\n", strerror(save_error));
297 WARN(aspi, "error:= %d\n", save_error);
303 status = read(fd, sg_reply_hdr, out_len);
304 if (status < 0 || status != out_len) {
305 WARN(aspi, "not enough bytes read from scsi device%d\n", status);
309 if (sg_reply_hdr->result != 0) {
310 error_code = sg_reply_hdr->result;
311 WARN(aspi, "reply header error (%d)\n", sg_reply_hdr->result);
315 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
316 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
319 /* copy in sense buffer to amount that is available in client */
320 if (lpPRB->SRB_SenseLen) {
321 int sense_len = lpPRB->SRB_SenseLen;
322 if (lpPRB->SRB_SenseLen > 16)
324 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
328 lpPRB->SRB_Status = SS_COMP;
329 lpPRB->SRB_HaStat = HASTAT_OK;
330 lpPRB->SRB_TargStat = STATUS_GOOD;
334 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
335 TRACE(aspi, "Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
340 SEGPTR spPRB = MapLS(lpPRB);
342 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, spPRB);
347 Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, ptrPRB);
354 ASPI_DebugPrintResult(lpPRB, mode);
358 if (error_code == EBUSY) {
359 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
360 TRACE(aspi, "Device busy\n");
363 WARN(aspi, "Failed\n");
364 lpPRB->SRB_Status = SS_ERR;
367 /* I'm not sure exactly error codes work here
368 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
370 WARN(aspi, "error_exit\n");
373 return lpPRB->SRB_Status;
378 /***********************************************************************
379 * GetASPISupportInfo16 (WINASPI.1)
382 WORD WINAPI GetASPISupportInfo16()
385 TRACE(aspi, "GETASPISupportInfo16\n");
386 /* high byte SS_COMP - low byte number of host adapters.
387 * FIXME!!! The number of host adapters is incorrect.
388 * I'm not sure how to determine this under linux etc.
390 return ((SS_COMP << 8) | 1);
392 return ((SS_COMP << 8) | 0);
397 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
406 lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(ptrSRB);
409 lpSRB = PTR_SEG_TO_LIN(ptrSRB);
413 switch (lpSRB->common.SRB_Cmd) {
415 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
416 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
417 TRACE(aspi, "Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
419 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
420 lpSRB->inquiry.HA_Count = 1; /* not always */
421 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
422 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
423 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
425 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
426 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
427 FIXME(aspi, "ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
429 case SC_GET_DEV_TYPE:
430 FIXME(aspi, "Not implemented SC_GET_DEV_TYPE\n");
432 case SC_EXEC_SCSI_CMD:
433 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
436 FIXME(aspi, "Not implemented SC_RESET_DEV\n");
439 WARN(aspi, "Unknown command %d\n", lpSRB->common.SRB_Cmd);
441 return SS_INVALID_SRB;
443 return SS_INVALID_SRB;
448 /***********************************************************************
449 * SendASPICommand16 (WINASPI.2)
451 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
454 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
461 /***********************************************************************
462 * GetASPIDLLVersion16 (WINASPI.4)
465 DWORD WINAPI GetASPIDLLVersion16()
475 void WINAPI ASPI_DOS_func(CONTEXT *context)
477 WORD *stack = CTX_SEG_OFF_TO_LIN(context, SS_reg(context), ESP_reg(context));
478 DWORD ptrSRB = *(DWORD *)&stack[2];
480 ASPI_SendASPICommand(ptrSRB, ASPI_DOS);
482 /* simulate a normal RETF sequence as required by DPMI CallRMProcFar */
483 IP_reg(context) = *(stack++);
484 CS_reg(context) = *(stack++);
485 SP_reg(context) += 2*sizeof(WORD);
489 /* returns the address of a real mode callback to ASPI_DOS_func() */
490 void ASPI_DOS_HandleInt(CONTEXT *context)
493 FARPROC16 *p = (FARPROC16 *)CTX_SEG_OFF_TO_LIN(context, DS_reg(context), EDX_reg(context));
494 if ((CX_reg(context) == 4) || (CX_reg(context) == 5))
496 *p = DPMI_AllocInternalRMCB(ASPI_DOS_func);
497 TRACE(aspi, "allocated real mode proc %p\n", *p);
498 AX_reg(context) = CX_reg(context);