?? scsi-wnt.c
字號:
case SS_ILLEGAL_MODE: /* 0xE2 Unsupported Windows mode */ case SS_NO_ASPI: /* 0xE3 No ASPI managers */ case SS_FAILED_INIT: /* 0xE4 ASPI for windows failed init */ case SS_MISMATCHED_COMPONENTS: /* 0xE7 The DLLs/EXEs of ASPI don't */ /* version check */ case SS_NO_ADAPTERS: /* 0xE8 No host adapters to manager */ case SS_ASPI_IS_SHUTDOWN: /* 0xEA Call came to ASPI after */ /* PROCESS_DETACH */ case SS_BAD_INSTALL: /* 0xEB The DLL or other components */ /* are installed wrong */ sp->error = SCG_FATAL; sp->ux_errno = EINVAL; break;#ifdef XXX case SS_OLD_MANAGER: /* 0xE1 ASPI manager doesn't support */ /* windows */#endif case SS_BUFFER_ALIGN: /* 0xE1 Buffer not aligned (replaces */ /* SS_OLD_MANAGER in Win32) */ sp->error = SCG_FATAL; sp->ux_errno = EFAULT; break; case SS_ASPI_IS_BUSY: /* 0xE5 No resources available to */ /* execute command */ sp->error = SCG_RETRYABLE; sp->ux_errno = EBUSY; break;#ifdef XXX case SS_BUFFER_TO_BIG: /* 0xE6 Buffer size too big to handle*/#endif case SS_BUFFER_TOO_BIG: /* 0xE6 Correct spelling of 'too' */ case SS_INSUFFICIENT_RESOURCES: /* 0xE9 Couldn't allocate resources */ /* needed to init */ sp->error = SCG_RETRYABLE; sp->ux_errno = ENOMEM; break; }}LOCAL intscsi_send(scgp, f, sp) SCSI *scgp; int f; struct scg_cmd *sp;{ DWORD Status = 0; DWORD EventStatus = WAIT_OBJECT_0; HANDLE Event = NULL; SRB_ExecSCSICmd s; /* * Check if ASPI library is loaded */ if (AspiLoaded == FALSE) { errmsgno(EX_BAD, "error in scsi_send: ASPI driver not loaded.\n"); sp->error = SCG_FATAL; return (-1); } if (f < 0) { sp->error = SCG_FATAL; return (-1); } /* * Initialize variables */ sp->error = SCG_NO_ERROR; sp->sense_count = 0; sp->u_scb.cmd_scb[0] = 0; sp->resid = 0; memset(&s, 0, sizeof(s)); /* Clear SRB structure */ /* * Check cbd_len > the maximum command pakket that can be handled by ASPI */ if (sp->cdb_len > 16) { sp->error = SCG_FATAL; sp->ux_errno = EINVAL; printf("sp->cdb_len > sizeof(SRB_ExecSCSICmd.CDBByte). Fatal error in scsi_send, exiting...\n"); return (-1); } /* * copy cdrecord command into SRB */ movebytes(&sp->cdb, &(s.CDBByte), sp->cdb_len); Event = CreateEvent(NULL, TRUE, FALSE, NULL); /* * Fill ASPI structure */ s.SRB_Cmd = SC_EXEC_SCSI_CMD; /* SCSI Command */ s.SRB_HaId = scgp->scsibus; /* Host adapter number */ s.SRB_Flags = SRB_EVENT_NOTIFY; /* Flags */ s.SRB_Target = scgp->target; /* Target SCSI ID */ s.SRB_Lun = scgp->lun; /* Target SCSI LUN */ s.SRB_BufLen = sp->size; /* # of bytes transferred */ s.SRB_BufPointer= sp->addr; /* pointer to data buffer */ s.SRB_CDBLen = sp->cdb_len; /* SCSI command length */ s.SRB_PostProc = Event; /* Post proc event */ s.SRB_SenseLen = SENSE_LEN; /* Lenght of sense buffer */ /* * Do we receive data from this ASPI command? */ if (sp->flags & SCG_RECV_DATA) { s.SRB_Flags |= SRB_DIR_IN; } else { /* * Set direction to output */ if (sp->size > 0) { s.SRB_Flags |= SRB_DIR_OUT; } }#ifdef DEBUG_WNTASPI /* * Dump some debug information when enabled */ DebugScsiSend(s, TRUE);/* DebugScsiSend(s, (s.SRB_Flags&SRB_DIR_OUT) == SRB_DIR_OUT);*/#endif /* * ------------ Send SCSI command -------------------------- */ ResetEvent(Event); /* Clear event handle */ Status = pfnSendASPI32Command((LPSRB)&s);/* Initiate SCSI command */ if (Status == SS_PENDING) { /* If in progress */ /* * Wait untill command completes, or times out. */ EventStatus = WaitForSingleObject(Event, sp->timeout*1000L);/* EventStatus = WaitForSingleObject(Event, 10L);*/ if (EventStatus == WAIT_OBJECT_0) ResetEvent(Event); /* Clear event, time out */ if (s.SRB_Status == SS_PENDING) {/* Check if we got a timeout*/ if (scgp->debug) printf("Timeout....\n"); scsiabort(scgp, &s); ResetEvent(Event); /* Clear event, time out */ CloseHandle(Event); /* Close the event handle */ sp->error = SCG_TIMEOUT; return (1); /* Return error */ } } CloseHandle(Event); /* Close the event handle */ /* * Check ASPI command status */ if (s.SRB_Status != SS_COMP) { if (scgp->debug) printf("Error in scsi_send: s.SRB_Status is 0x%x\n", s.SRB_Status); set_error(&s, sp); /* Set error flags */ copy_sensedata(&s, sp); /* Copy sense and status */ if (scgp->debug) printf("Mapped to: error %d errno: %d\n", sp->error, sp->ux_errno); return (1); } /* * Return success */ return (0);}/*************************************************************************** * * * BOOL open_driver() * * * * Opens the ASPI Router device driver and sets device_handle. * * Returns: * * TRUE - Success * * FALSE - Unsuccessful opening of device driver * * * * Preconditions: ASPI Router driver has be loaded * * * ***************************************************************************/LOCAL BOOLopen_driver(scgp) SCSI *scgp;{ DWORD astatus; BYTE HACount; BYTE ASPIStatus; int i;#ifdef DEBUG_WNTASPI printf("enter open_driver\n");#endif /* * Check if ASPI library is already loaded yet */ if (AspiLoaded == TRUE) return (TRUE); /* * Load the ASPI library */#ifdef __CYGWIN32__ hAspiLib = dlopen("WNASPI32", RTLD_NOW);#else hAspiLib = LoadLibrary("WNASPI32");#endif /* * Check if ASPI library is loaded correctly */ if (hAspiLib == NULL) { printf("Can not load ASPI driver! "); return (FALSE); } /* * Get a pointer to GetASPI32SupportInfo function * and a pointer to SendASPI32Command function */#ifdef __CYGWIN32__ pfnGetASPI32SupportInfo = (DWORD(*)(void))dlsym(hAspiLib, "GetASPI32SupportInfo"); pfnSendASPI32Command = (DWORD(*)(LPSRB))dlsym(hAspiLib, "SendASPI32Command");#else pfnGetASPI32SupportInfo = (DWORD(*)(void))GetProcAddress(hAspiLib, "GetASPI32SupportInfo"); pfnSendASPI32Command = (DWORD(*)(LPSRB))GetProcAddress(hAspiLib, "SendASPI32Command");#endif if ((pfnGetASPI32SupportInfo == NULL) || (pfnSendASPI32Command == NULL)) { printf("ASPI function not found in library!"); return (FALSE); }#ifdef __CYGWIN32__ pfnGetASPI32Buffer = (BOOL(*)(PASPI32BUFF))dlsym(hAspiLib, "GetASPI32Buffer"); pfnFreeASPI32Buffer = (BOOL(*)(PASPI32BUFF))dlsym(hAspiLib, "FreeASPI32Buffer"); pfnTranslateASPI32Address = (BOOL(*)(PDWORD, PDWORD))dlsym(hAspiLib, "TranslateASPI32Address");#else pfnGetASPI32Buffer = (BOOL(*)(PASPI32BUFF))GetProcAddress(hAspiLib, "GetASPI32Buffer"); pfnFreeASPI32Buffer = (BOOL(*)(PASPI32BUFF))GetProcAddress(hAspiLib, "FreeASPI32Buffer"); pfnTranslateASPI32Address = (BOOL(*)(PDWORD, PDWORD))GetProcAddress(hAspiLib, "TranslateASPI32Address");#endif /* * Set AspiLoaded variable */ AspiLoaded = TRUE; astatus = pfnGetASPI32SupportInfo(); ASPIStatus = HIBYTE(LOWORD(astatus)); HACount = LOBYTE(LOWORD(astatus)); if (scgp->debug) printf("open_driver %X HostASPIStatus=0x%x HACount=0x%x\n", astatus, ASPIStatus, HACount); if (ASPIStatus != SS_COMP && ASPIStatus != SS_NO_ADAPTERS) { printf("Could not find any host adapters\n"); printf("ASPIStatus == 0x%02X", ASPIStatus); return (FALSE); } busses = HACount;#ifdef DEBUG_WNTASPI printf("open_driver HostASPIStatus=0x%x HACount=0x%x\n", ASPIStatus, HACount); printf("leaving open_driver\n");#endif for (i=0; i < busses; i++) { SRB_HAInquiry s; ha_inquiry(scgp, i, &s); } /* * Indicate that library loaded/initialized properly */ return (TRUE);}/*************************************************************************** * * * BOOL close_driver() * * * * Closes the device driver * * Returns: * * TRUE - Success * * FALSE - Unsuccessful closing of device driver * * * * Preconditions: ASPI Router driver has be opened with open_driver * * * ***************************************************************************/LOCAL BOOLclose_driver(){ /* * If library is loaded */ if (hAspiLib) { /* * Clear all variables */ AspiLoaded = FALSE; pfnGetASPI32SupportInfo = NULL; pfnSendASPI32Command = NULL; pfnGetASPI32Buffer = NULL; pfnFreeASPI32Buffer = NULL; pfnTranslateASPI32Address = NULL; /* * Free ASPI library, we do not need it any longer */#ifdef __CYGWIN32__ dlclose(hAspiLib);#else FreeLibrary(hAspiLib);#endif hAspiLib = NULL; } /* * Indicate that shutdown has been finished properly */ return (TRUE);}LOCAL intha_inquiry(scgp, id, ip) SCSI *scgp; int id; SRB_HAInquiry *ip;{ DWORD Status; ip->SRB_Cmd = SC_HA_INQUIRY; ip->SRB_HaId = id; ip->SRB_Flags = 0; ip->SRB_Hdr_Rsvd= 0; Status = pfnSendASPI32Command((LPSRB)ip); if (scgp->debug) { printf("Status : %d\n", Status); printf("hacount: %d\n", ip->HA_Count); printf("SCSI id: %d\n", ip->HA_SCSI_ID); printf("Manager: '%.16s'\n", ip->HA_ManagerId); printf("Identif: '%.16s'\n", ip->HA_Identifier); scsiprbytes("Unique:", ip->HA_Unique, 16); } if (ip->SRB_Status != SS_COMP) return (-1); return (0);}LOCAL intresetSCSIBus(void){ DWORD Status; HANDLE Event; SRB_BusDeviceReset s; printf("Attempting to reset SCSI bus\n"); Event = CreateEvent(NULL, TRUE, FALSE, NULL); memset(&s, 0, sizeof(s)); /* Clear SRB_BesDeviceReset structure */ /* * Set structure variables */ s.SRB_Cmd = SC_RESET_DEV; s.SRB_PostProc = (LPVOID)Event; /* * Clear event */ ResetEvent(Event); /* * Initiate SCSI command */ Status = pfnSendASPI32Command((LPSRB)&s); /* * Check status */ if (Status == SS_PENDING) { /* * Wait till command completes */ WaitForSingleObject(Event, INFINITE); } /* * Close the event handle */ CloseHandle(Event); /* * Check condition */ if (s.SRB_Status != SS_COMP) { printf("ERROR 0x%08X\n", s.SRB_Status); /* * Indicate that error has occured */ return (FALSE); } /* * Everything went OK */ return (TRUE);}LOCAL intscsiabort(scgp, sp) SCSI *scgp; SRB_ExecSCSICmd *sp;{ DWORD Status = 0; SRB_Abort s; if (scgp->debug) printf("Attempting to abort SCSI command\n"); /* * Check if ASPI library is loaded */ if (AspiLoaded == FALSE) { printf("error in scsiabort: ASPI driver not loaded !\n"); return (FALSE); } /* * Set structure variables */ s.SRB_Cmd = SC_ABORT_SRB; /* ASPI command code = SC_ABORT_SRB */ s.SRB_HaId = scgp->scsibus; /* ASPI host adapter number */ s.SRB_Flags = 0; /* Flags */ s.SRB_ToAbort = (LPSRB)&sp; /* sp */ /* * Initiate SCSI abort */ Status = pfnSendASPI32Command((LPSRB)&s); /* * Check condition */ if (s.SRB_Status != SS_COMP) { printf("Abort ERROR! 0x%08X\n", s.SRB_Status); /* * Indicate that error has occured */ return (FALSE); } if (scgp->debug) printf("Abort SCSI command completed\n"); /* * Everything went OK */ return (TRUE);}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -