- New function in aspi.c SCSI_Fix_CMD_LEN which determines if Linux is
[wine] / dlls / winaspi / aspi.c
1 /**************************************************************************
2 ASPI routines
3 (C) 2000 David Elliott <dfe@netnitco.net>
4 Licensed under the WINE (X11) license
5 */
6
7 /* These routines are to be called from either WNASPI32 or WINASPI */
8
9 /* FIXME:
10  * - Registry format is stupid for now.. fix that later
11  * - No way to override automatic /proc detection, maybe provide an
12  *   HKEY_LOCAL_MACHINE\Software\Wine\Wine\Scsi regkey
13  * - Somewhat debating an #ifdef linux... technically all this code will
14  *   run on another UNIX.. it will fail nicely.
15  * - Please add support for mapping multiple channels on host adapters to
16  *   aspi controllers, e-mail me if you need help.
17  */
18
19 /*
20 Registry format is currently:
21 HKEY_DYN_DATA
22         WineScsi
23                 (default)=number of host adapters
24                 hHHcCCtTTdDD=linux device name
25 */
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/ioctl.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33
34 #include "debugtools.h"
35 #include "winreg.h"
36 #include "winerror.h"
37 #include "winescsi.h"
38 #include "file.h"
39
40 DEFAULT_DEBUG_CHANNEL(aspi);
41
42 /* Internal function prototypes */
43 static void
44 SCSI_GetProcinfo();
45
46 static void
47 SCSI_MapHCtoController();
48
49 /* Exported functions */
50 void
51 SCSI_Init()
52 {
53         /* For now we just call SCSI_GetProcinfo */
54         SCSI_GetProcinfo();
55         SCSI_MapHCtoController();
56 }
57
58 int
59 ASPI_GetNumControllers()
60 {
61         HKEY hkeyScsi;
62         HKEY hkeyControllerMap;
63         DWORD error;
64         DWORD type = REG_DWORD;
65         DWORD num_ha = 0;
66         DWORD cbData = sizeof(num_ha);
67
68         if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
69         {
70                 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
71                 return 0;
72         }
73
74         if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
75         {
76                 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
77                 RegCloseKey(hkeyScsi);
78                 SetLastError(error);
79                 return 0;
80         }
81         if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
82         {
83                 ERR("Could not query value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
84                 num_ha=0;
85         }
86         RegCloseKey(hkeyControllerMap);
87         RegCloseKey(hkeyScsi);
88         TRACE("Returning %ld host adapters\n", num_ha );
89         return num_ha;
90 }
91
92 BOOL
93 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
94 {
95
96         char idstr[20];
97         HKEY hkeyScsi;
98         DWORD type;
99
100         if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
101         {
102                 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
103                 return FALSE;
104         }
105
106
107         sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
108
109         if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
110         {
111                 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
112                 RegCloseKey(hkeyScsi);
113                 return FALSE;
114         }
115         RegCloseKey(hkeyScsi);
116
117         TRACE("scsi %s: Device name: %s\n",idstr,devstr);
118         return TRUE;
119 }
120
121 /* SCSI_GetHCforController
122  * RETURNS
123  *      HIWORD: Host Adapter
124  *      LOWORD: Channel
125  */
126 DWORD
127 ASPI_GetHCforController( int controller )
128 {
129         DWORD hc = 0xFFFFFFFF;
130         char cstr[20];
131         DWORD error;
132         HKEY hkeyScsi;
133         HKEY hkeyControllerMap;
134         DWORD type = REG_DWORD;
135         DWORD cbData = sizeof(DWORD);
136         DWORD disposition;
137 #if 0
138         FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
139         hc = (controller << 16);
140         return hc;
141 #endif
142         if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
143         {
144                 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
145                 SetLastError(error);
146                 return hc;
147         }
148         if( disposition != REG_OPENED_EXISTING_KEY )
149         {
150                 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
151         }
152         if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
153         {
154                 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
155                 RegCloseKey(hkeyScsi);
156                 SetLastError(error);
157                 return hc;
158         }
159         if( disposition != REG_OPENED_EXISTING_KEY )
160         {
161                 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
162         }
163
164         sprintf(cstr, "c%02d", controller);
165         if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
166         {
167                 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
168                 SetLastError( error );
169         }
170         RegCloseKey(hkeyControllerMap);
171         RegCloseKey(hkeyScsi);
172         return hc;
173
174 };
175
176 int
177 SCSI_OpenDevice( int h, int c, int t, int d )
178 {
179         char devstr[20];
180         DWORD cbData = 20;
181         int fd = -1;
182
183         if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
184         {
185                 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
186                 return -1;
187         }
188
189         TRACE("Opening device %s mode O_RDWR\n",devstr);
190         fd = open(devstr, O_RDWR);
191
192         if( fd < 0 )
193         {
194                 TRACE("open failed\n");
195                 FILE_SetDosError(); /* SetLastError() to errno */
196                 TRACE("GetLastError: %ld\n", GetLastError());
197         }
198         return fd;
199 }
200
201 #ifdef linux
202 /* SCSI_Fix_CMD_LEN
203  *      Checks to make sure the CMD_LEN is correct
204  */
205 void
206 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
207 {
208         int index=(cmd>>5)&7;
209
210         if (len!=scsi_command_size[index])
211         {
212                 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
213                                 cmd, len, scsi_command_size[index]);
214                 ioctl(fd,SG_NEXT_CMD_LEN,&len);
215         }
216 }
217
218 int
219 SCSI_LinuxSetTimeout( int fd, int timeout )
220 {
221         int retval;
222         TRACE("Setting timeout to %d jiffies\n", timeout);
223         retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
224         if(retval)
225         {
226                 WARN("Could not set timeout errno=%d!\n",errno);
227         }
228         return retval;
229         
230 }
231
232 /* This function takes care of the write/read to the linux sg device.
233  * It returns TRUE or FALSE and uses FILE_SetDosError() to convert
234  * UNIX errno to Windows GetLastError().  The reason for that is that
235  * several programs will check that error and we might as well set
236  * it here.  We also return the value of the read call in
237  * lpcbBytesReturned.
238  */
239 BOOL /* NOTE: This function SHOULD BLOCK */
240 SCSI_LinuxDeviceIo( int fd,
241                 struct sg_header * lpInBuffer, DWORD cbInBuffer,
242                 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
243                 LPDWORD lpcbBytesReturned )
244 {
245         DWORD dwBytes;
246         DWORD save_error;
247
248         TRACE("Writing to Liunx sg device\n");
249         dwBytes = write( fd, lpInBuffer, cbInBuffer );
250         if( dwBytes != cbInBuffer )
251         {
252                 FILE_SetDosError();
253                 save_error = GetLastError();
254                 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
255                 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
256                         MESSAGE("Your Linux kernel was not able to handle the amount of data sent to the scsi device.  Try recompiling with a larger SG_BIG_BUFF value (kernel 2.0.x sg.h");
257                 WARN("error= %ld\n", save_error );
258                 *lpcbBytesReturned = 0;
259                 return FALSE;
260         }
261         
262         TRACE("Reading reply from Linux sg device\n");
263         *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
264         if( *lpcbBytesReturned != cbOutBuffer )
265         {
266                 FILE_SetDosError();
267                 save_error = GetLastError();
268                 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
269                 WARN("error= %ld\n", save_error );
270                 return FALSE;
271         }
272         return TRUE;
273 }
274
275 /* Internal functions */
276 struct LinuxProcScsiDevice
277 {
278         int host;
279         int channel;
280         int target;
281         int lun;
282         char vendor[9];
283         char model[17];
284         char rev[5];
285         char type[33];
286         int ansirev;
287 };
288
289 static int
290 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
291 {
292         int result;
293         result = fscanf( procfile,
294                 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
295                 &dev->host,
296                 &dev->channel,
297                 &dev->target,
298                 &dev->lun );
299         if( result == EOF )
300                 return EOF;
301         if( result != 4 )
302                 return 0;
303         result = fscanf( procfile,
304                 "  Vendor: %8c Model: %16c Rev: %4c\n",
305                 dev->vendor,
306                 dev->model,
307                 dev->rev );
308         if( result != 3 )
309                 return 0;
310
311         result = fscanf( procfile,
312                 "  Type:   %32c ANSI SCSI revision: %d\n",
313                 dev->type,
314                 &dev->ansirev );
315         if( result != 2 )
316                 return 0;
317         /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
318         dev->vendor[8] = 0;
319         dev->model[16] = 0;
320         dev->rev[4] = 0;
321         dev->type[32] = 0;
322
323         return 1;
324 }
325
326 static void
327 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
328 {
329         TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
330                 dev->host,
331                 dev->channel,
332                 dev->target,
333                 dev->lun );
334         TRACE( "  Vendor: %s Model: %s Rev: %s\n",
335                 dev->vendor,
336                 dev->model,
337                 dev->rev );
338         TRACE( "  Type:   %s ANSI SCSI revision: %02d\n",
339                 dev->type,
340                 dev->ansirev );
341 }
342
343 static BOOL
344 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
345 {
346         DWORD error;
347         char cstr[20];
348         DWORD hc;
349         hc = (ha << 16) + chan;
350         sprintf(cstr, "c%02d", num_controller);
351         if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
352         {
353                 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
354         }
355         return error;
356 }
357
358 static void
359 SCSI_MapHCtoController()
360 {
361         HKEY hkeyScsi;
362         HKEY hkeyControllerMap;
363         DWORD disposition;
364
365         char idstr[20];
366         DWORD cbIdStr = 20;
367         int i = 0;
368         DWORD type = 0;
369         DWORD error;
370
371         DWORD num_controller = 0;
372         int last_ha = -1;
373         int last_chan = -1;
374
375         int ha = 0;
376         int chan = 0;
377
378         if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
379         {
380                 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
381                 return;
382         }
383         if( disposition != REG_OPENED_EXISTING_KEY )
384         {
385                 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
386         }
387         if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
388         {
389                 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
390                 RegCloseKey(hkeyScsi);
391                 return;
392         }
393         
394         for( i=0; (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
395         {
396                 sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan);
397                 if( last_ha < ha )
398                 {       /* Next HA */
399                         last_ha = ha;
400                         last_chan = chan;
401                         SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
402                         num_controller++;
403                 }
404                 else if( last_ha > ha )
405                 {
406                         FIXME("Expected registry to be sorted\n");
407                 }
408                 /* last_ha == ha */
409                 else if( last_chan < chan )
410                 {
411                         last_chan = chan;
412                         SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
413                         num_controller++;
414                 }
415                 else if( last_chan > chan )
416                 {
417                         FIXME("Expected registry to be sorted\n");
418                 }
419                 /* else last_ha == ha && last_chan == chan so do nothing */
420         }
421         /* Set (default) value to number of controllers */
422         if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
423         {
424                 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
425         }
426         RegCloseKey(hkeyControllerMap);
427         RegCloseKey(hkeyScsi);
428         return;
429 }
430 #endif
431
432 static void
433 SCSI_GetProcinfo()
434 /* I'll admit, this function is somewhat of a mess... it was originally
435  * designed to make some sort of linked list then I realized that
436  * HKEY_DYN_DATA would be a lot less messy
437  */
438 {
439 #ifdef linux
440         FILE * procfile = NULL;
441
442         int result = 0;
443
444         struct LinuxProcScsiDevice dev;
445
446         char idstr[20];
447         char devstr[20];
448
449         int devnum=0;
450         int num_ha = 0;
451
452         HKEY hkeyScsi;
453         DWORD disposition;
454
455         procfile = fopen( "/proc/scsi/scsi", "r" );
456         if( !procfile )
457         {
458                 ERR("Could not open /proc/scsi/scsi\n");
459                 return;
460         }
461
462         result = fscanf( procfile, "Attached devices: \n");
463         if( result != 0 )
464         {
465                 ERR("Incorrect /proc/scsi/scsi format");
466                 return;
467         }
468
469         if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
470         {
471                 ERR("Could not create HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
472                 return;
473         }
474
475         /* Read info for one device */
476         while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
477         {
478                 /* Add to registry */
479
480                 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
481                 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
482                 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
483                 {
484                         ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
485                 }
486
487                 /* Debug output */
488                 SCSI_printprocentry( &dev );
489
490                 /* FIXME: We *REALLY* need number of controllers.. not ha */
491                 /* num of hostadapters is highest ha + 1 */
492                 if( dev.host >= num_ha )
493                         num_ha = dev.host+1;
494                 devnum++;
495         } /* while(1) */
496         if( result != EOF )
497         {
498                 ERR("Incorrect /proc/scsi/scsi format");
499         }
500         fclose( procfile );
501         if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
502         {
503                 ERR("Could not set value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
504         }
505         RegCloseKey(hkeyScsi);
506         return;
507 #endif
508 }