Define __fastcall.
[wine] / dlls / winaspi / winaspi16.c
1 #include "config.h"
2
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <memory.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <stdio.h>
12
13 #include "winbase.h"
14 #include "aspi.h"
15 #include "winescsi.h"
16 #include "winaspi.h"
17 #include "options.h"
18 #include "heap.h"
19 #include "debugtools.h"
20 #include "selectors.h"
21 #include "miscemu.h"
22 #include "ldt.h"
23 #include "callback.h"
24
25 DEFAULT_DEBUG_CHANNEL(aspi)
26
27
28 /* FIXME!
29  * 1) Residual byte length reporting not handled
30  * 2) Make this code re-entrant for multithreading
31  * 3) Only linux supported so far
32  */
33
34 #ifdef linux
35
36 static ASPI_DEVICE_INFO *ASPI_open_devices = NULL;
37
38 static FARPROC16 ASPIChainFunc = NULL;
39 static WORD HA_Count = 1; /* host adapter count; FIXME: detect it */
40
41 static int
42 ASPI_OpenDevice16(SRB_ExecSCSICmd16 *prb)
43 {
44     int fd;
45     char        idstr[20];
46     char        device_str[50];
47     ASPI_DEVICE_INFO *curr;
48
49     /* search list of devices to see if we've opened it already.
50      * There is not an explicit open/close in ASPI land, so hopefully
51      * keeping a device open won't be a problem.
52      */
53
54     for (curr = ASPI_open_devices; curr; curr = curr->next) {
55         if (curr->hostId == prb->SRB_HaId &&
56             curr->target == prb->SRB_Target &&
57             curr->lun == prb->SRB_Lun) {
58             return curr->fd;
59         }
60     }
61
62     /* device wasn't cached, go ahead and open it */
63     sprintf(idstr, "scsi c%1dt%1dd%1d", prb->SRB_HaId, prb->SRB_Target, prb->SRB_Lun);
64
65     if (!PROFILE_GetWineIniString(idstr, "Device", "", device_str, sizeof(device_str))) {
66         TRACE("Trying to open unlisted scsi device %s\n", idstr);
67         return -1;
68     }
69
70     TRACE("Opening device %s=%s\n", idstr, device_str);
71
72     fd = open(device_str, O_RDWR);
73     if (fd == -1) {
74         int save_error = errno;
75 #ifdef HAVE_STRERROR
76         ERR("Error opening device %s, error '%s'\n", device_str, strerror(save_error));
77 #else
78     ERR("Error opening device %s, error %d\n", device_str, save_error);
79 #endif
80         return -1;
81     }
82
83     /* device is now open */
84     curr = HeapAlloc( GetProcessHeap(), 0, sizeof(ASPI_DEVICE_INFO) );
85     curr->fd = fd;
86     curr->hostId = prb->SRB_HaId;
87     curr->target = prb->SRB_Target;
88     curr->lun = prb->SRB_Lun;
89
90     /* insert new record at beginning of open device list */
91     curr->next = ASPI_open_devices;
92     ASPI_open_devices = curr;
93     return fd;
94 }
95
96
97 static void
98 ASPI_DebugPrintCmd(SRB_ExecSCSICmd16 *prb, UINT16 mode)
99 {
100   BYTE  cmd;
101   int   i;
102   BYTE *cdb;
103   BYTE *lpBuf = 0;
104
105   switch (mode)
106   {
107       case ASPI_DOS:
108         /* translate real mode address */
109         if (prb->SRB_BufPointer)
110             lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
111         break;
112       case ASPI_WIN16:
113         lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
114         break;
115   }
116
117   switch (prb->CDBByte[0]) {
118   case CMD_INQUIRY:
119     TRACE("{\n");
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]);
125     TRACE("}\n");
126     break;
127   case CMD_SCAN_SCAN:
128     TRACE("Transfer Length: %d\n", prb->CDBByte[4]);
129     break;
130   }
131
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");
136   }
137   else if (HOST_TO_TARGET(prb)) {
138     TRACE("\tData transfer: Host to target. Length checked.\n");
139   }
140   else if (NO_DATA_TRANSFERED(prb)) {
141     TRACE("\tData transfer: none\n");
142   }
143   else {
144     WARN("\tTransfer by scsi cmd. Length not checked\n");
145   }
146
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];
160   if (TRACE_ON(aspi))
161   {
162       DPRINTF("CDB buffer[");
163       for (i = 0; i < prb->SRB_CDBLen; i++) {
164           if (i != 0) DPRINTF(",");
165           DPRINTF("%02x", *cdb++);
166       }
167       DPRINTF("]\n");
168   }
169 }
170
171 static void
172 ASPI_PrintSenseArea16(SRB_ExecSCSICmd16 *prb)
173 {
174   int   i;
175   BYTE *cdb;
176
177   if (TRACE_ON(aspi))
178   {
179       cdb = &prb->CDBByte[0];
180       DPRINTF("SenseArea[");
181       for (i = 0; i < prb->SRB_SenseLen; i++) {
182           if (i) DPRINTF(",");
183           DPRINTF("%02x", *cdb++);
184       }
185       DPRINTF("]\n");
186   }
187 }
188
189 static void
190 ASPI_DebugPrintResult(SRB_ExecSCSICmd16 *prb, UINT16 mode)
191 {
192   BYTE *lpBuf = 0;
193
194   switch (mode)
195   {
196       case ASPI_DOS:
197         /* translate real mode address */
198         if (prb->SRB_BufPointer)
199             lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)prb->SRB_BufPointer);
200         break;
201       case ASPI_WIN16:
202         lpBuf = PTR_SEG_TO_LIN(prb->SRB_BufPointer);
203         break;
204   }
205
206   switch (prb->CDBByte[0]) {
207   case CMD_INQUIRY:
208     TRACE("Vendor: '%s'\n", lpBuf + INQUIRY_VENDOR);
209     break;
210   case CMD_TEST_UNIT_READY:
211     ASPI_PrintSenseArea16(prb);
212     break;
213   }
214 }
215
216 static WORD
217 ASPI_ExecScsiCmd(DWORD ptrPRB, UINT16 mode)
218 {
219   SRB_ExecSCSICmd16 *lpPRB = 0;
220   struct sg_header *sg_hd, *sg_reply_hdr;
221   int   status;
222   BYTE *lpBuf = 0;
223   int   in_len, out_len;
224   int   error_code = 0;
225   int   fd;
226
227   switch (mode)
228   {
229       case ASPI_DOS:
230         if (ptrPRB)
231             lpPRB = (SRB_ExecSCSICmd16 *)DOSMEM_MapRealToLinear(ptrPRB);
232         break;
233       case ASPI_WIN16:
234         lpPRB = PTR_SEG_TO_LIN(ptrPRB);
235         break;
236   }
237
238   ASPI_DebugPrintCmd(lpPRB, mode);
239
240   fd = ASPI_OpenDevice16(lpPRB);
241   if (fd == -1) {
242       WARN("Failed: could not open device. Device permissions !?\n");
243       lpPRB->SRB_Status = SS_ERR;
244       return SS_ERR;
245   }
246
247   sg_hd = NULL;
248   sg_reply_hdr = NULL;
249
250   lpPRB->SRB_Status = SS_PENDING;
251
252   switch (mode)
253   {
254       case ASPI_DOS:
255         /* translate real mode address */
256         if (ptrPRB)
257             lpBuf = (BYTE *)DOSMEM_MapRealToLinear((UINT)lpPRB->SRB_BufPointer);
258         break;
259       case ASPI_WIN16:
260         lpBuf = PTR_SEG_TO_LIN(lpPRB->SRB_BufPointer);
261         break;
262   }
263
264   if (!lpPRB->SRB_CDBLen) {
265       WARN("Failed: lpPRB->SRB_CDBLen = 0.\n");
266       lpPRB->SRB_Status = SS_ERR;
267       return SS_ERR;
268   }
269
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);
279     }
280   }
281   else {
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);
287   }
288
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;
294   }
295   else {
296     out_len = SCSI_OFF;
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;
300   }
301
302   status = write(fd, sg_hd, in_len);
303   if (status < 0 || status != in_len) {
304       int save_error = errno;
305
306     WARN("Not enough bytes written to scsi device bytes=%d .. %d\n", in_len, status);
307     if (status < 0) {
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");
310         }
311 #ifdef HAVE_STRERROR
312                 WARN("error:= '%s'\n", strerror(save_error));
313 #else
314                 WARN("error:= %d\n", save_error);
315 #endif
316     }
317     goto error_exit;
318   }
319
320   status = read(fd, sg_reply_hdr, out_len);
321   if (status < 0 || status != out_len) {
322     WARN("not enough bytes read from scsi device%d\n", status);
323     goto error_exit;
324   }
325
326   if (sg_reply_hdr->result != 0) {
327     error_code = sg_reply_hdr->result;
328     WARN("reply header error (%d)\n", sg_reply_hdr->result);
329     goto error_exit;
330   }
331
332   if (TARGET_TO_HOST(lpPRB) && lpPRB->SRB_BufLen) {
333     memcpy(lpBuf, sg_reply_hdr + 1, lpPRB->SRB_BufLen);
334   }
335
336   /* copy in sense buffer to amount that is available in client */
337   if (lpPRB->SRB_SenseLen) {
338     int sense_len = lpPRB->SRB_SenseLen;
339     if (lpPRB->SRB_SenseLen > 16)
340       sense_len = 16;
341     memcpy(SENSE_BUFFER(lpPRB), &sg_reply_hdr->sense_buffer[0], sense_len);
342   }
343
344
345   lpPRB->SRB_Status = SS_COMP;
346   lpPRB->SRB_HaStat = HASTAT_OK;
347   lpPRB->SRB_TargStat = STATUS_GOOD;
348
349   /* now do posting */
350
351   if (ASPI_POSTING(lpPRB) && lpPRB->SRB_PostProc) {
352     TRACE("Post Routine (%lx) called\n", (DWORD) lpPRB->SRB_PostProc);
353     switch (mode)
354     {
355       case ASPI_DOS:
356       {
357         SEGPTR spPRB = MapLS(lpPRB);
358
359         Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, spPRB);        
360         UnMapLS(spPRB);
361         break;
362       }
363       case ASPI_WIN16:
364         Callbacks->CallASPIPostProc(lpPRB->SRB_PostProc, ptrPRB);
365         break;
366     }
367   }
368
369   free(sg_reply_hdr);
370   free(sg_hd);
371   ASPI_DebugPrintResult(lpPRB, mode);
372   return SS_COMP;
373   
374 error_exit:
375   if (error_code == EBUSY) {
376       lpPRB->SRB_Status = SS_ASPI_IS_BUSY;
377       TRACE("Device busy\n");
378   }
379   else {
380       WARN("Failed\n");
381       lpPRB->SRB_Status = SS_ERR;
382   }
383
384   /* I'm not sure exactly error codes work here
385    * We probably should set lpPRB->SRB_TargStat, SRB_HaStat ?
386    */
387   WARN("error_exit\n");
388   free(sg_reply_hdr);
389   free(sg_hd);
390   return lpPRB->SRB_Status;
391 }
392 #endif
393
394
395 /***********************************************************************
396  *             GetASPISupportInfo16   (WINASPI.1)
397  */
398
399 WORD WINAPI GetASPISupportInfo16()
400 {
401 #ifdef linux
402     TRACE("GETASPISupportInfo16\n");
403     /* high byte SS_COMP - low byte number of host adapters */
404     return ((SS_COMP << 8) | HA_Count);
405 #else
406     return ((SS_NO_ASPI << 8) | 0);
407 #endif
408 }
409
410
411 DWORD ASPI_SendASPICommand(DWORD ptrSRB, UINT16 mode)
412 {
413 #ifdef linux
414   LPSRB16 lpSRB = 0;
415
416   switch (mode)
417   {
418       case ASPI_DOS:
419         if (ptrSRB)
420             lpSRB = (LPSRB16)DOSMEM_MapRealToLinear(ptrSRB);
421         break;
422       case ASPI_WIN16:
423         lpSRB = PTR_SEG_TO_LIN(ptrSRB);
424         if (ASPIChainFunc)
425         {
426             /* This is not the post proc, it's the chain proc this time */
427             DWORD ret = Callbacks->CallASPIPostProc(ASPIChainFunc, ptrSRB);
428             if (ret)
429             {
430                 lpSRB->inquiry.SRB_Status = SS_INVALID_SRB;
431                 return ret;
432             }
433         }
434         break;
435   }
436
437   switch (lpSRB->common.SRB_Cmd) {
438   case SC_HA_INQUIRY:
439     lpSRB->inquiry.SRB_Status = SS_COMP;       /* completed successfully */
440     if (lpSRB->inquiry.SRB_55AASignature == 0x55aa) {
441         TRACE("Extended request detected (Adaptec's ASPIxDOS).\nWe don't support it at the moment.\n");
442     }
443     lpSRB->inquiry.SRB_ExtBufferSize = 0x2000; /* bogus value */
444     lpSRB->inquiry.HA_Count = HA_Count;
445     lpSRB->inquiry.HA_SCSI_ID = 7;             /* not always ID 7 */
446     strcat(lpSRB->inquiry.HA_ManagerId, "Wine ASPI16"); /* max 15 chars */
447     strcat(lpSRB->inquiry.HA_Identifier, "Wine host"); /* FIXME: return host
448 adapter name */
449     memset(lpSRB->inquiry.HA_Unique, 0, 16); /* default HA_Unique content */
450     lpSRB->inquiry.HA_Unique[6] = 0x02; /* Maximum Transfer Length (128K, Byte> 4-7) */
451     FIXME("ASPI: Partially implemented SC_HA_INQUIRY for adapter %d.\n", lpSRB->inquiry.SRB_HaId);
452     return SS_COMP;
453   case SC_GET_DEV_TYPE:
454     FIXME("Not implemented SC_GET_DEV_TYPE\n");
455     break;
456   case SC_EXEC_SCSI_CMD:
457     return ASPI_ExecScsiCmd((DWORD)ptrSRB, mode);
458     break;
459   case SC_RESET_DEV:
460     FIXME("Not implemented SC_RESET_DEV\n");
461     break;
462   default:
463     FIXME("Unknown command %d\n", lpSRB->common.SRB_Cmd);
464   }
465 #endif
466   return SS_INVALID_SRB;
467 }
468
469
470 /***********************************************************************
471  *             SendASPICommand16   (WINASPI.2)
472  */
473 WORD WINAPI SendASPICommand16(SEGPTR segptr_srb)
474 {
475 #ifdef linux
476     return ASPI_SendASPICommand(segptr_srb, ASPI_WIN16);
477 #else
478     return 0; 
479 #endif
480 }
481
482
483 /***********************************************************************
484  *             InsertInASPIChain16   (WINASPI.3)
485  */
486 WORD WINAPI InsertInASPIChain16(BOOL16 remove, FARPROC16 pASPIChainFunc)
487 {
488 #ifdef linux
489     if (remove == TRUE) /* Remove */
490     {
491         if (ASPIChainFunc == pASPIChainFunc)
492         {
493             ASPIChainFunc = NULL;
494             return SS_COMP;
495         }
496     }
497     else
498     if (remove == FALSE) /* Insert */
499     {
500         if (ASPIChainFunc == NULL)
501         {
502             ASPIChainFunc = pASPIChainFunc;
503             return SS_COMP;
504         }
505     }
506 #endif
507     return SS_ERR;
508 }
509
510
511 /***********************************************************************
512  *             GetASPIDLLVersion16   (WINASPI.4)
513  */
514
515 DWORD WINAPI GetASPIDLLVersion16()
516 {
517 #ifdef linux
518         return 2;
519 #else
520         return 0;
521 #endif
522 }
523