16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(aspi);
21 * 1) Residual byte length reporting not handled
22 * 2) Make this code re-entrant for multithreading
23 * -- Added CriticalSection to OpenDevices function
24 * 3) Only linux supported so far
25 * 4) Leaves sg devices open. This may or may not be okay. A better solution
26 * would be to close the file descriptors when the thread/process using
27 * them no longer needs them.
32 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
33 static CRITICAL_SECTION ASPI_CritSection = CRITICAL_SECTION_INIT("ASPI_CritSection");
35 #endif /* defined(linux) */
38 BOOL WINAPI WNASPI32_LibMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
43 case DLL_PROCESS_ATTACH:
46 case DLL_PROCESS_DETACH:
47 DeleteCriticalSection( &ASPI_CritSection );
49 case DLL_THREAD_ATTACH:
50 case DLL_THREAD_DETACH:
53 #endif /* defined(linux) */
61 ASPI_OpenDevice(SRB_ExecSCSICmd *prb)
65 ASPI_DEVICE_INFO *curr;
67 /* search list of devices to see if we've opened it already.
68 * There is not an explicit open/close in ASPI land, so hopefully
69 * keeping a device open won't be a problem.
72 EnterCriticalSection(&ASPI_CritSection);
73 for (curr = ASPI_open_devices; curr; curr = curr->next) {
74 if (curr->hostId == prb->SRB_HaId &&
75 curr->target == prb->SRB_Target &&
76 curr->lun == prb->SRB_Lun) {
77 LeaveCriticalSection(&ASPI_CritSection);
81 LeaveCriticalSection(&ASPI_CritSection);
83 if (prb->SRB_HaId > ASPI_GetNumControllers())
86 hc = ASPI_GetHCforController( prb->SRB_HaId );
87 fd = SCSI_OpenDevice( HIWORD(hc), LOWORD(hc), prb->SRB_Target, prb->SRB_Lun);
92 /* device is now open */
93 /* FIXME: Let users specify SCSI timeout in registry */
94 SCSI_LinuxSetTimeout( fd, SCSI_DEFAULT_TIMEOUT );
96 curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
98 curr->hostId = prb->SRB_HaId;
99 curr->target = prb->SRB_Target;
100 curr->lun = prb->SRB_Lun;
102 /* insert new record at beginning of open device list */
103 EnterCriticalSection(&ASPI_CritSection);
104 curr->next = ASPI_open_devices;
105 ASPI_open_devices = curr;
106 LeaveCriticalSection(&ASPI_CritSection);
112 ASPI_DebugPrintCmd(SRB_ExecSCSICmd *prb)
118 switch (prb->CDBByte[0]) {
120 TRACE("INQUIRY {\n");
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: %p\n", prb->SRB_BufPointer);
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];
160 if (TRACE_ON(aspi)) {
161 DPRINTF("CDB buffer[");
162 for (i = 0; i < prb->SRB_CDBLen; i++) {
163 if (i != 0) DPRINTF(",");
164 DPRINTF("%02x", *cdb++);
171 ASPI_PrintCDBArea(SRB_ExecSCSICmd *prb)
177 for (i = 0; i < prb->SRB_CDBLen; i++) {
179 DPRINTF("%02x", prb->CDBByte[i]);
186 ASPI_PrintSenseArea(SRB_ExecSCSICmd *prb)
189 BYTE *rqbuf = prb->SenseArea;
193 DPRINTF("Request Sense reports:\n");
194 if ((rqbuf[0]&0x7f)!=0x70) {
195 DPRINTF("\tInvalid sense header: 0x%02x instead of 0x70\n", rqbuf[0]&0x7f);
198 DPRINTF("\tCurrent command read filemark: %s\n",(rqbuf[2]&0x80)?"yes":"no");
199 DPRINTF("\tEarly warning passed: %s\n",(rqbuf[2]&0x40)?"yes":"no");
200 DPRINTF("\tIncorrect blocklength: %s\n",(rqbuf[2]&0x20)?"yes":"no");
201 DPRINTF("\tSense Key: %d\n",rqbuf[2]&0xf);
203 DPRINTF("\tResidual Length: %d\n",rqbuf[3]*0x1000000+rqbuf[4]*0x10000+rqbuf[5]*0x100+rqbuf[6]);
204 DPRINTF("\tAdditional Sense Length: %d\n",rqbuf[7]);
205 DPRINTF("\tAdditional Sense Code: %d\n",rqbuf[12]);
206 DPRINTF("\tAdditional Sense Code Qualifier: %d\n",rqbuf[13]);
207 if (rqbuf[15]&0x80) {
208 DPRINTF("\tIllegal Param is in %s\n",(rqbuf[15]&0x40)?"the CDB":"the Data Out Phase");
210 DPRINTF("Pointer at %d, bit %d\n",rqbuf[16]*256+rqbuf[17],rqbuf[15]&0x7);
213 DPRINTF("SenseArea[");
214 for (i = 0; i < prb->SRB_SenseLen; i++) {
216 DPRINTF("%02x", *rqbuf++);
223 ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
226 TRACE("SRB_Status: %x\n", prb->SRB_Status);
227 TRACE("SRB_HaStat: %x\n", prb->SRB_HaStat);
228 TRACE("SRB_TargStat: %x\n", prb->SRB_TargStat);
229 switch (prb->CDBByte[0]) {
231 TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
233 case CMD_TEST_UNIT_READY:
234 ASPI_PrintSenseArea(prb);
239 /* Posting must be done in such a way that as soon as the SRB_Status is set
240 * we don't touch the SRB anymore because it could possibly be freed
241 * if the app is doing ASPI polling
244 WNASPI32_DoPosting( SRB_ExecSCSICmd *lpPRB, DWORD status )
246 void (*SRB_PostProc)() = lpPRB->SRB_PostProc;
247 BYTE SRB_Flags = lpPRB->SRB_Flags;
248 if( status == SS_PENDING )
250 WARN("Tried posting SS_PENDING\n");
253 lpPRB->SRB_Status = status;
254 /* lpPRB is NOT safe, it could be freed in another thread */
260 TRACE("Post Routine (%lx) called\n", (DWORD) SRB_PostProc);
261 /* Even though lpPRB could have been freed by
262 * the program.. that's unlikely if it planned
263 * to use it in the PostProc
265 (*SRB_PostProc)(lpPRB);
267 else if (SRB_Flags & SRB_EVENT_NOTIFY) {
268 TRACE("Setting event %04x\n", (HANDLE)SRB_PostProc);
269 SetEvent((HANDLE)SRB_PostProc);
276 ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
278 struct sg_header *sg_hd, *sg_reply_hdr;
286 /* FIXME: hackmode */
287 #define MAKE_TARGET_TO_HOST(lpPRB) \
288 if (!TARGET_TO_HOST(lpPRB)) { \
289 WARN("program was not sending target_to_host for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
290 lpPRB->SRB_Flags |= 0x08; \
292 #define MAKE_HOST_TO_TARGET(lpPRB) \
293 if (!HOST_TO_TARGET(lpPRB)) { \
294 WARN("program was not sending host_to_target for cmd %x (flags=%x),correcting.\n",lpPRB->CDBByte[0],lpPRB->SRB_Flags); \
295 lpPRB->SRB_Flags |= 0x10; \
297 switch (lpPRB->CDBByte[0]) {
298 case 0x12: /* INQUIRY */
299 case 0x5a: /* MODE_SENSE_10 */
300 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
301 case 0xad: /* READ DVD STRUCTURE MMC-2 */
302 MAKE_TARGET_TO_HOST(lpPRB)
304 case 0xa3: /* SEND KEY (DVD) MMC-2 */
305 MAKE_HOST_TO_TARGET(lpPRB)
308 if ((((lpPRB->SRB_Flags & 0x18) == 0x00) ||
309 ((lpPRB->SRB_Flags & 0x18) == 0x18)
310 ) && lpPRB->SRB_BufLen
312 FIXME("command 0x%02x, no data transfer specified, but buflen is %ld!!!\n",lpPRB->CDBByte[0],lpPRB->SRB_BufLen);
316 ASPI_DebugPrintCmd(lpPRB);
317 if (lpPRB->SRB_HaId > ASPI_GetNumControllers()) {
318 ERR("Failed: Wanted hostadapter %d, but we have only %d.\n",
319 lpPRB->SRB_HaId,ASPI_GetNumControllers()
321 return WNASPI32_DoPosting( lpPRB, SS_INVALID_HA );
323 fd = ASPI_OpenDevice(lpPRB);
325 return WNASPI32_DoPosting( lpPRB, SS_NO_DEVICE );
331 lpPRB->SRB_Status = SS_PENDING;
333 if (!lpPRB->SRB_CDBLen) {
334 ERR("Failed: lpPRB->SRB_CDBLen = 0.\n");
335 return WNASPI32_DoPosting( lpPRB, SS_INVALID_SRB );
338 /* build up sg_header + scsi cmd */
339 if (HOST_TO_TARGET(lpPRB)) {
340 /* send header, command, and then data */
341 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
342 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
343 memset(sg_hd, 0, SCSI_OFF);
344 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
345 if (lpPRB->SRB_BufLen) {
346 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
350 /* send header and command - no data */
351 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
352 sg_hd = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, in_len);
353 memset(sg_hd, 0, SCSI_OFF);
354 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
357 if (TARGET_TO_HOST(lpPRB)) {
358 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
359 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
360 memset(sg_reply_hdr, 0, SCSI_OFF);
361 sg_hd->reply_len = out_len;
365 sg_reply_hdr = (struct sg_header *) HeapAlloc(GetProcessHeap(), 0, out_len);
366 memset(sg_reply_hdr, 0, SCSI_OFF);
367 sg_hd->reply_len = out_len;
370 SCSI_Fix_CMD_LEN(fd, lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
372 if(!SCSI_LinuxDeviceIo( fd,
374 sg_reply_hdr, out_len,
380 if (sg_reply_hdr->result != 0) {
381 error_code = sg_reply_hdr->result;
382 WARN("reply header error (%d)\n", sg_reply_hdr->result);
386 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
387 memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
390 /* copy in sense buffer to amount that is available in client */
391 if (lpPRB->SRB_SenseLen) {
392 int sense_len = lpPRB->SRB_SenseLen;
393 if (lpPRB->SRB_SenseLen > 16)
396 /* CDB is fixed in WNASPI32 */
397 memcpy(lpPRB->SenseArea, &sg_reply_hdr->sense_buffer[0], sense_len);
399 ASPI_PrintCDBArea(lpPRB);
400 ASPI_PrintSenseArea(lpPRB);
403 SRB_Status = SS_COMP;
404 lpPRB->SRB_HaStat = HASTAT_OK;
405 lpPRB->SRB_TargStat = sg_reply_hdr->target_status << 1;
407 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
408 HeapFree(GetProcessHeap(), 0, sg_hd);
410 /* FIXME: Should this be != 0 maybe? */
411 if( lpPRB->SRB_TargStat == 2 ) {
413 switch (lpPRB->CDBByte[0]) {
414 case 0xa4: /* REPORT_KEY (DVD) MMC-2 */
415 case 0xa3: /* SEND KEY (DVD) MMC-2 */
416 SRB_Status = SS_COMP;
417 lpPRB->SRB_TargStat = 0;
418 FIXME("Program wants to do DVD Region switching, but fails (non compliant DVD drive). Ignoring....\n");
423 ASPI_DebugPrintResult(lpPRB);
425 ret = WNASPI32_DoPosting( lpPRB, SRB_Status );
427 switch (lpPRB->CDBByte[0]) {
429 if (SRB_Status == SS_COMP)
430 return SS_COMP; /* some junk expects ss_comp here. */
436 /* In real WNASPI32 stuff really is always pending because ASPI does things
437 in the background, but we are not doing that (yet) */
443 if (error_code == EBUSY) {
444 WNASPI32_DoPosting( lpPRB, SS_ASPI_IS_BUSY );
445 TRACE("Device busy\n");
449 /* I'm not sure exactly error codes work here
450 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
452 WARN("error_exit\n");
453 HeapFree(GetProcessHeap(), 0, sg_reply_hdr);
454 HeapFree(GetProcessHeap(), 0, sg_hd);
455 WNASPI32_DoPosting( lpPRB, SRB_Status );
459 #endif /* defined(linux) */
462 /*******************************************************************
463 * GetASPI32SupportInfo [WNASPI32.1]
465 * Checks if the ASPI subsystem is initialized correctly.
469 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
470 * LOBYTE of LOWORD: # of host adapters.
472 DWORD __cdecl GetASPI32SupportInfo(void)
474 DWORD controllers = ASPI_GetNumControllers();
477 return SS_NO_ADAPTERS << 8;
478 return (SS_COMP << 8) | controllers ;
481 /***********************************************************************
482 * SendASPI32Command (WNASPI32.2)
484 DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
487 switch (lpSRB->common.SRB_Cmd) {
489 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
490 lpSRB->inquiry.HA_Count = ASPI_GetNumControllers();
491 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
492 strcpy(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
493 strcpy(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host adapter name */
494 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
495 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
496 lpSRB->inquiry.HA_Unique[3] = 0x08; /* Maximum number of SCSI targets */
497 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
500 case SC_GET_DEV_TYPE: {
501 /* FIXME: We should return SS_NO_DEVICE if the device is not configured */
502 /* FIXME: We should return SS_INVALID_HA if HostAdapter!=0 */
507 memset(&tmpsrb,0,sizeof(tmpsrb));
510 memcpy(&tmpsrb.common,&(lpSRB->common),sizeof(tmpsrb.common));
512 tmpsrb.cmd.SRB_Flags |= 8; /* target to host */
513 tmpsrb.cmd.SRB_Cmd = SC_EXEC_SCSI_CMD;
514 tmpsrb.cmd.SRB_Target = lpSRB->devtype.SRB_Target;
515 tmpsrb.cmd.SRB_Lun = lpSRB->devtype.SRB_Lun;
516 tmpsrb.cmd.SRB_BufLen = sizeof(inqbuf);
517 tmpsrb.cmd.SRB_BufPointer = inqbuf;
518 tmpsrb.cmd.CDBByte[0] = 0x12; /* INQUIRY */
519 /* FIXME: handle lun */
520 tmpsrb.cmd.CDBByte[4] = sizeof(inqbuf);
521 tmpsrb.cmd.SRB_CDBLen = 6;
523 ret = ASPI_ExecScsiCmd(&tmpsrb.cmd);
525 lpSRB->devtype.SRB_Status = tmpsrb.cmd.SRB_Status;
526 lpSRB->devtype.SRB_DeviceType = inqbuf[0]&0x1f;
528 TRACE("returning devicetype %d for target %d\n",inqbuf[0]&0x1f,tmpsrb.cmd.SRB_Target);
529 if (ret!=SS_PENDING) /* Any error is passed down directly */
531 /* FIXME: knows that the command is finished already, pass final Status */
532 return tmpsrb.cmd.SRB_Status;
534 case SC_EXEC_SCSI_CMD:
535 return ASPI_ExecScsiCmd(&lpSRB->cmd);
537 FIXME("Not implemented SC_ABORT_SRB\n");
540 FIXME("Not implemented SC_RESET_DEV\n");
542 case SC_GET_DISK_INFO:
543 /* here we have to find out the int13 / bios association.
544 * We just say we do not have any.
546 FIXME("SC_GET_DISK_INFO always return 'int13 unassociated disk'.\n");
547 lpSRB->diskinfo.SRB_DriveFlags = 0; /* disk is not int13 served */
550 FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
552 return SS_INVALID_SRB;
554 return SS_INVALID_SRB;
559 /***********************************************************************
560 * GetASPI32DLLVersion (WNASPI32.4)
562 DWORD __cdecl GetASPI32DLLVersion(void)
565 TRACE("Returning version 1\n");
568 FIXME("Please add SCSI support for your operating system, returning 0\n");
573 /***********************************************************************
574 * GetASPI32Buffer (WNASPI32.8)
575 * Supposed to return a DMA capable large SCSI buffer.
576 * Our implementation does not use those at all, all buffer stuff is
577 * done in the kernel SG device layer. So we just heapalloc the buffer.
579 BOOL __cdecl GetASPI32Buffer(PASPI32BUFF pab)
581 pab->AB_BufPointer = HeapAlloc(GetProcessHeap(),
582 pab->AB_ZeroFill?HEAP_ZERO_MEMORY:0,
585 if (!pab->AB_BufPointer) return FALSE;
589 /***********************************************************************
590 * FreeASPI32Buffer (WNASPI32.14)
592 BOOL __cdecl FreeASPI32Buffer(PASPI32BUFF pab)
594 HeapFree(GetProcessHeap(),0,pab->AB_BufPointer);
598 /***********************************************************************
599 * TranslateASPI32Address (WNASPI32.7)
601 BOOL __cdecl TranslateASPI32Address(LPDWORD pdwPath, LPDWORD pdwDEVNODE)
603 FIXME("(%p, %p), stub !\n", pdwPath, pdwDEVNODE);