14 #include "wine/windef16.h"
17 #include "wine/winaspi.h"
20 #include "debugtools.h"
21 #include "selectors.h"
25 DEFAULT_DEBUG_CHANNEL(aspi);
29 * 1) Residual byte length reporting not handled
30 * 2) Make this code re-entrant for multithreading
31 * 3) Only linux supported so far
34 /* ### start build ### */
35 extern LONG CALLBACK ASPI_CallTo16_long_l(FARPROC16,SEGPTR);
36 /* ### stop build ### */
40 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
42 static FARPROC16 ASPIChainFunc = NULL;
43 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
46 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
51 ASPI_DEVICE_INFO *curr;
53 /* search list of devices to see if we've opened it already.
54 * There is not an explicit open/close in ASPI land, so hopefully
55 * keeping a device open won't be a problem.
58 for (curr = ASPI_open_devices; curr; curr = curr->next) {
59 if (curr->hostId == prb->SRB_HaId &&
60 curr->target == prb->SRB_Target &&
61 curr->lun == prb->SRB_Lun) {
66 /* device wasn't cached, go ahead and open it */
67 sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
69 if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
70 TRACE("Trying to open unlisted scsi device %s\n", idstr);
74 TRACE("Opening device %s=%s\n", idstr, device_str);
76 fd = open(device_str, O_RDWR);
78 int save_error = errno;
79 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
83 /* device is now open */
84 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
86 curr->hostId = prb->SRB_HaId;
87 curr->target = prb->SRB_Target;
88 curr->lun = prb->SRB_Lun;
90 /* insert new record at beginning of open device list */
91 curr->next = ASPI_open_devices;
92 ASPI_open_devices = curr;
98 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
108 /* translate real mode address */
109 if (prb->SRB_BufPointer)
110 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
113 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
117 switch (prb->CDBByte[0]) {
120 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
121 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
122 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
123 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
124 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
128 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
132 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
133 TRACE("Flags: %d\n", prb->SRB_Flags);
134 if (TARGET_TO_HOST(prb)) {
135 TRACE("\tData transfer: Target to host. Length checked.\n");
137 else if (HOST_TO_TARGET(prb)) {
138 TRACE("\tData transfer: Host to target. Length checked.\n");
140 else if (NO_DATA_TRANSFERED(prb)) {
141 TRACE("\tData transfer: none\n");
144 WARN("\tTransfer by scsi cmd. Length not checked\n");
147 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
148 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
149 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
150 TRACE("Target: %d\n", prb->SRB_Target);
151 TRACE("Lun: %d\n", prb->SRB_Lun);
152 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
153 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
154 TRACE("BufPtr: %lx (%p)\n", prb->SRB_BufPointer, lpBuf);
155 TRACE("LinkPointer %lx\n", prb->SRB_Rsvd1);
156 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
157 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
158 cdb = &prb->CDBByte[0];
159 cmd = prb->CDBByte[0];
162 DPRINTF("CDB buffer[");
163 for (i = 0; i < prb->SRB_CDBLen; i++) {
164 if (i != 0) DPRINTF(",");
165 DPRINTF("%02x", *cdb++);
172 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
179 cdb = &prb->CDBByte[0];
180 DPRINTF("SenseArea[");
181 for (i = 0; i < prb->SRB_SenseLen; i++) {
183 DPRINTF("%02x", *cdb++);
190 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
197 /* translate real mode address */
198 if (prb->SRB_BufPointer)
199 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
202 lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
206 switch (prb->CDBByte[0]) {
208 TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
210 case CMD_TEST_UNIT_READY:
211 ASPI_PrintSenseArea16(prb);
217 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
219 SRB_ExecSCSICmd16 *lpPRB = 0;
220 struct sg_header *sg_hd, *sg_reply_hdr;
231 lpPRB = (SRB_ExecSCSICmd16 *)DOSMEM_MapRealToLinear(ptrPRB);
234 lpPRB = PTR_SEG_TO_LIN(ptrPRB);
238 ASPI_DebugPrintCmd(lpPRB, mode);
240 fd = ASPI_OpenDevice16(lpPRB);
242 WARN("Failed: could not open device. Device permissions !?\n");
243 lpPRB->SRB_Status = SS_ERR;
250 lpPRB->SRB_Status = SS_PENDING;
255 /* translate real mode address */
257 lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)lpPRB->SRB_BufPointer);
260 lpBuf = PTR_SEG_TO_LIN(lpPRB->SRB_BufPointer);
264 if (!lpPRB->SRB_CDBLen) {
265 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
266 lpPRB->SRB_Status = SS_ERR;
270 /* build up sg_header + scsi cmd */
271 if (HOST_TO_TARGET(lpPRB)) {
272 /* send header, command, and then data */
273 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
274 sg_hd = (struct sg_header *) malloc(in_len);
275 memset(sg_hd, 0, SCSI_OFF);
276 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
277 if (lpPRB->SRB_BufLen) {
278 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpBuf, lpPRB->SRB_BufLen);
282 /* send header and command - no data */
283 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
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);
289 if (TARGET_TO_HOST(lpPRB)) {
290 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
291 sg_reply_hdr = (struct sg_header *) malloc(out_len);
292 memset(sg_reply_hdr, 0, SCSI_OFF);
293 sg_hd->reply_len = out_len;
297 sg_reply_hdr = (struct sg_header *) malloc(out_len);
298 memset(sg_reply_hdr, 0, SCSI_OFF);
299 sg_hd->reply_len = out_len;
302 status = write(fd, sg_hd, in_len);
303 if (status < 0 || status != in_len) {
304 int save_error = errno;
306 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
308 if (save_error == ENOMEM) {
309 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));
316 status = read(fd, sg_reply_hdr, out_len);
317 if (status < 0 || status != out_len) {
318 WARN("not enough bytes read from scsi device%d\n", status);
322 if (sg_reply_hdr->result != 0) {
323 error_code = sg_reply_hdr->result;
324 WARN("reply header error (%d)\n", sg_reply_hdr->result);
328 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
329 memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
332 /* copy in sense buffer to amount that is available in client */
333 if (lpPRB->SRB_SenseLen) {
334 int sense_len = lpPRB->SRB_SenseLen;
335 if (lpPRB->SRB_SenseLen > 16)
337 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
341 lpPRB->SRB_Status = SS_COMP;
342 lpPRB->SRB_HaStat = HASTAT_OK;
343 lpPRB->SRB_TargStat = STATUS_GOOD;
347 if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
348 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
353 SEGPTR spPRB = MapLS(lpPRB);
355 ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, spPRB);
360 ASPI_CallTo16_long_l(lpPRB->SRB_PostProc, ptrPRB);
367 ASPI_DebugPrintResult(lpPRB, mode);
371 if (error_code == EBUSY) {
372 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
373 TRACE("Device busy\n");
377 lpPRB->SRB_Status = SS_ERR;
380 /* I'm not sure exactly error codes work here
381 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
383 WARN("error_exit\n");
386 return lpPRB->SRB_Status;
391 /***********************************************************************
392 * GetASPISupportInfo16 (WINASPI.1)
395 WORD WINAPI GetASPISupportInfo16(void)
398 TRACE("GETASPISupportInfo16\n");
399 /* high byte SS_COMP - low byte number of host adapters */
400 return ((SS_COMP << 8) | HA_Count);
402 return ((SS_NO_ASPI << 8) | 0);
407 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
416 lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(ptrSRB);
419 lpSRB = PTR_SEG_TO_LIN(ptrSRB);
422 /* This is not the post proc, it's the chain proc this time */
423 DWORD ret = ASPI_CallTo16_long_l(ASPIChainFunc, ptrSRB);
426 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
433 switch (lpSRB->common.SRB_Cmd) {
435 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
436 if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
437 TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
439 lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
440 lpSRB->inquiry.HA_Count = HA_Count;
441 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
442 strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
443 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
445 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
446 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
447 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
449 case SC_GET_DEV_TYPE:
450 FIXME("Not implemented SC_GET_DEV_TYPE\n");
452 case SC_EXEC_SCSI_CMD:
453 return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
456 FIXME("Not implemented SC_RESET_DEV\n");
459 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
462 return SS_INVALID_SRB;
466 /***********************************************************************
467 * SendASPICommand16 (WINASPI.2)
469 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
472 return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
479 /***********************************************************************
480 * InsertInASPIChain16 (WINASPI.3)
482 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
485 if (remove == TRUE) /* Remove */
487 if (ASPIChainFunc == pASPIChainFunc)
489 ASPIChainFunc = NULL;
494 if (remove == FALSE) /* Insert */
496 if (ASPIChainFunc == NULL)
498 ASPIChainFunc = pASPIChainFunc;
507 /***********************************************************************
508 * GetASPIDLLVersion16 (WINASPI.4)
511 DWORD WINAPI GetASPIDLLVersion16()