16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(aspi)
22 * 1) Residual byte length reporting not handled
23 * 2) Make this code re-entrant for multithreading
24 * 3) Only linux supported so far
29 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
32 ASPI_OpenDevice(SRB_ExecSCSICmd *prb)
37 ASPI_DEVICE_INFO *curr;
39 /* search list of devices to see if we've opened it already.
40 * There is not an explicit open/close in ASPI land, so hopefully
41 * keeping a device open won't be a problem.
44 for (curr = ASPI_open_devices; curr; curr = curr->next) {
45 if (curr->hostId == prb->SRB_HaId &&
46 curr->target == prb->SRB_Target &&
47 curr->lun == prb->SRB_Lun) {
52 /* device wasn't cached, go ahead and open it */
53 sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
55 if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
56 TRACE("Trying to open unlisted scsi device %s\n", idstr);
60 TRACE("Opening device %s=%s\n", idstr, device_str);
62 fd = open(device_str, O_RDWR);
64 int save_error = errno;
66 ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
68 ERR("Error opening device %s, error %d\n", device_str, save_error);
73 /* device is now open */
74 curr = HeapAlloc( SystemHeap, 0, sizeof(ASPI_DEVICE_INFO) );
76 curr->hostId = prb->SRB_HaId;
77 curr->target = prb->SRB_Target;
78 curr->lun = prb->SRB_Lun;
80 /* insert new record at beginning of open device list */
81 curr->next = ASPI_open_devices;
82 ASPI_open_devices = curr;
88 ASPI_DebugPrintCmd(SRB_ExecSCSICmd *prb)
93 dbg_decl_str(aspi, 512);
95 switch (prb->CDBByte[0]) {
98 TRACE("\tEVPD: %d\n", prb->CDBByte[1] & 1);
99 TRACE("\tLUN: %d\n", (prb->CDBByte[1] & 0xc) >> 1);
100 TRACE("\tPAGE CODE: %d\n", prb->CDBByte[2]);
101 TRACE("\tALLOCATION LENGTH: %d\n", prb->CDBByte[4]);
102 TRACE("\tCONTROL: %d\n", prb->CDBByte[5]);
106 TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
110 TRACE("Host Adapter: %d\n", prb->SRB_HaId);
111 TRACE("Flags: %d\n", prb->SRB_Flags);
112 if (TARGET_TO_HOST(prb)) {
113 TRACE("\tData transfer: Target to host. Length checked.\n");
115 else if (HOST_TO_TARGET(prb)) {
116 TRACE("\tData transfer: Host to target. Length checked.\n");
118 else if (NO_DATA_TRANSFERED(prb)) {
119 TRACE("\tData transfer: none\n");
122 WARN("\tTransfer by scsi cmd. Length not checked.\n");
125 TRACE("\tResidual byte length reporting %s\n", prb->SRB_Flags & 0x4 ? "enabled" : "disabled");
126 TRACE("\tLinking %s\n", prb->SRB_Flags & 0x2 ? "enabled" : "disabled");
127 TRACE("\tPosting %s\n", prb->SRB_Flags & 0x1 ? "enabled" : "disabled");
128 TRACE("Target: %d\n", prb->SRB_Target);
129 TRACE("Lun: %d\n", prb->SRB_Lun);
130 TRACE("BufLen: %ld\n", prb->SRB_BufLen);
131 TRACE("SenseLen: %d\n", prb->SRB_SenseLen);
132 TRACE("BufPtr: %p\n", prb->SRB_BufPointer);
133 TRACE("CDB Length: %d\n", prb->SRB_CDBLen);
134 TRACE("POST Proc: %lx\n", (DWORD) prb->SRB_PostProc);
135 cdb = &prb->CDBByte[0];
136 cmd = prb->CDBByte[0];
137 for (i = 0; i < prb->SRB_CDBLen; i++) {
138 if (i != 0) dsprintf(aspi, ",");
139 dsprintf(aspi, "%02x", *cdb++);
141 TRACE("CDB buffer[%s]\n", dbg_str(aspi));
145 ASPI_PrintSenseArea(SRB_ExecSCSICmd *prb)
149 dbg_decl_str(aspi, 512);
151 cdb = &prb->CDBByte[0];
152 for (i = 0; i < prb->SRB_SenseLen; i++) {
153 if (i) dsprintf(aspi, ",");
154 dsprintf(aspi, "%02x", *cdb++);
156 TRACE("SenseArea[%s]\n", dbg_str(aspi));
160 ASPI_DebugPrintResult(SRB_ExecSCSICmd *prb)
163 switch (prb->CDBByte[0]) {
165 TRACE("Vendor: '%s'\n", prb->SRB_BufPointer + INQUIRY_VENDOR);
167 case CMD_TEST_UNIT_READY:
168 ASPI_PrintSenseArea(prb);
174 ASPI_ExecScsiCmd(SRB_ExecSCSICmd *lpPRB)
176 struct sg_header *sg_hd, *sg_reply_hdr;
182 ASPI_DebugPrintCmd(lpPRB);
184 fd = ASPI_OpenDevice(lpPRB);
186 ERR("Failed: could not open device c%01dt%01dd%01d. Device permissions !?\n",
187 lpPRB->SRB_HaId,lpPRB->SRB_Target,lpPRB->SRB_Lun);
188 lpPRB->SRB_Status = SS_NO_DEVICE;
195 lpPRB->SRB_Status = SS_PENDING;
197 if (!lpPRB->SRB_CDBLen) {
198 WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
199 lpPRB->SRB_Status = SS_ERR;
203 /* build up sg_header + scsi cmd */
204 if (HOST_TO_TARGET(lpPRB)) {
205 /* send header, command, and then data */
206 in_len = SCSI_OFF + lpPRB->SRB_CDBLen + lpPRB->SRB_BufLen;
207 sg_hd = (struct sg_header *) malloc(in_len);
208 memset(sg_hd, 0, SCSI_OFF);
209 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
210 if (lpPRB->SRB_BufLen) {
211 memcpy(((BYTE *) sg_hd) + SCSI_OFF + lpPRB->SRB_CDBLen, lpPRB->SRB_BufPointer, lpPRB->SRB_BufLen);
215 /* send header and command - no data */
216 in_len = SCSI_OFF + lpPRB->SRB_CDBLen;
217 sg_hd = (struct sg_header *) malloc(in_len);
218 memset(sg_hd, 0, SCSI_OFF);
219 memcpy(sg_hd + 1, &lpPRB->CDBByte[0], lpPRB->SRB_CDBLen);
222 if (TARGET_TO_HOST(lpPRB)) {
223 out_len = SCSI_OFF + lpPRB->SRB_BufLen;
224 sg_reply_hdr = (struct sg_header *) malloc(out_len);
225 memset(sg_reply_hdr, 0, SCSI_OFF);
226 sg_hd->reply_len = out_len;
230 sg_reply_hdr = (struct sg_header *) malloc(out_len);
231 memset(sg_reply_hdr, 0, SCSI_OFF);
232 sg_hd->reply_len = out_len;
235 status = write(fd, sg_hd, in_len);
236 if (status < 0 || status != in_len) {
237 int save_error = errno;
239 WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
241 if (save_error == ENOMEM) {
242 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");
245 WARN("error:= '%s'\n", strerror(save_error));
247 WARN("error:= %d\n", save_error);
253 status = read(fd, sg_reply_hdr, out_len);
254 if (status < 0 || status != out_len) {
255 WARN("not enough bytes read from scsi device%d\n", status);
259 if (sg_reply_hdr->result != 0) {
260 error_code = sg_reply_hdr->result;
261 WARN("reply header error (%d)\n", sg_reply_hdr->result);
265 if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
266 memcpy(lpPRB->SRB_BufPointer, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
269 /* copy in sense buffer to amount that is available in client */
270 if (lpPRB->SRB_SenseLen) {
271 int sense_len = lpPRB->SRB_SenseLen;
272 if (lpPRB->SRB_SenseLen > 16)
274 memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
278 lpPRB->SRB_Status = SS_COMP;
279 lpPRB->SRB_HaStat = HASTAT_OK;
280 lpPRB->SRB_TargStat = STATUS_GOOD;
284 if (lpPRB->SRB_PostProc) {
285 if (ASPI_POSTING(lpPRB)) {
286 TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
287 (*lpPRB->SRB_PostProc)(lpPRB);
290 if (lpPRB->SRB_Flags & SRB_EVENT_NOTIFY) {
291 TRACE("Setting event %04x\n", (HANDLE)lpPRB->SRB_PostProc);
292 SetEvent((HANDLE)lpPRB->SRB_PostProc); /* FIXME: correct ? */
297 ASPI_DebugPrintResult(lpPRB);
301 if (error_code == EBUSY) {
302 lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
303 TRACE("Device busy\n");
307 lpPRB->SRB_Status = SS_ERR;
310 /* I'm not sure exactly error codes work here
311 * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
313 WARN("error_exit\n");
316 return lpPRB->SRB_Status;
321 /*******************************************************************
322 * GetASPI32SupportInfo32 [WNASPI32.0]
324 * Checks if the ASPI subsystem is initialized correctly.
328 * HIBYTE of LOWORD: status (SS_COMP or SS_FAILED_INIT)
329 * LOBYTE of LOWORD: # of host adapters.
331 DWORD WINAPI GetASPI32SupportInfo()
333 return ((SS_COMP << 8) | 1); /* FIXME: get # of host adapters installed */
337 /***********************************************************************
338 * SendASPI32Command32 (WNASPI32.1)
340 DWORD __cdecl SendASPI32Command(LPSRB lpSRB)
343 switch (lpSRB->common.SRB_Cmd) {
345 lpSRB->inquiry.SRB_Status = SS_COMP; /* completed successfully */
346 lpSRB->inquiry.HA_Count = 1; /* not always */
347 lpSRB->inquiry.HA_SCSI_ID = 7; /* not always ID 7 */
348 strcat(lpSRB->inquiry.HA_ManagerId, "ASPI for WIN32"); /* max 15 chars, don't change */
349 strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host adapter name */
350 memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
351 lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
352 FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
354 case SC_GET_DEV_TYPE:
355 FIXME("Not implemented SC_GET_DEV_TYPE\n");
357 case SC_EXEC_SCSI_CMD:
358 return ASPI_ExecScsiCmd(&lpSRB->cmd);
361 FIXME("Not implemented SC_RESET_DEV\n");
364 WARN("Unknown command %d\n", lpSRB->common.SRB_Cmd);
366 return SS_INVALID_SRB;
368 return SS_INVALID_SRB;
373 /***********************************************************************
374 * GetASPI32DLLVersion32 (WNASPI32.3)
377 DWORD WINAPI GetASPI32DLLVersion()