Map each channel on every host to an ASPI controller number.
[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 int
203 SCSI_LinuxSetTimeout( int fd, int timeout )
204 {
205         int retval;
206         TRACE("Setting timeout to %d jiffies\n", timeout);
207         retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
208         if(retval)
209         {
210                 WARN("Could not set timeout errno=%d!\n",errno);
211         }
212         return retval;
213         
214 }
215
216 /* This function takes care of the write/read to the linux sg device.
217  * It returns TRUE or FALSE and uses FILE_SetDosError() to convert
218  * UNIX errno to Windows GetLastError().  The reason for that is that
219  * several programs will check that error and we might as well set
220  * it here.  We also return the value of the read call in
221  * lpcbBytesReturned.
222  */
223 BOOL /* NOTE: This function SHOULD BLOCK */
224 SCSI_LinuxDeviceIo( int fd,
225                 struct sg_header * lpInBuffer, DWORD cbInBuffer,
226                 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
227                 LPDWORD lpcbBytesReturned )
228 {
229         DWORD dwBytes;
230         DWORD save_error;
231
232         TRACE("Writing to Liunx sg device\n");
233         dwBytes = write( fd, lpInBuffer, cbInBuffer );
234         if( dwBytes != cbInBuffer )
235         {
236                 FILE_SetDosError();
237                 save_error = GetLastError();
238                 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
239                 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
240                         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");
241                 WARN("error= %ld\n", save_error );
242                 *lpcbBytesReturned = 0;
243                 return FALSE;
244         }
245         
246         TRACE("Reading reply from Linux sg device\n");
247         *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
248         if( *lpcbBytesReturned != cbOutBuffer )
249         {
250                 FILE_SetDosError();
251                 save_error = GetLastError();
252                 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
253                 WARN("error= %ld\n", save_error );
254                 return FALSE;
255         }
256         return TRUE;
257 }
258
259 /* Internal functions */
260 struct LinuxProcScsiDevice
261 {
262         int host;
263         int channel;
264         int target;
265         int lun;
266         char vendor[9];
267         char model[17];
268         char rev[5];
269         char type[33];
270         int ansirev;
271 };
272
273 static int
274 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
275 {
276         int result;
277         result = fscanf( procfile,
278                 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
279                 &dev->host,
280                 &dev->channel,
281                 &dev->target,
282                 &dev->lun );
283         if( result == EOF )
284                 return EOF;
285         if( result != 4 )
286                 return 0;
287         result = fscanf( procfile,
288                 "  Vendor: %8c Model: %16c Rev: %4c\n",
289                 dev->vendor,
290                 dev->model,
291                 dev->rev );
292         if( result != 3 )
293                 return 0;
294
295         result = fscanf( procfile,
296                 "  Type:   %32c ANSI SCSI revision: %d\n",
297                 dev->type,
298                 &dev->ansirev );
299         if( result != 2 )
300                 return 0;
301         /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
302         dev->vendor[8] = 0;
303         dev->model[16] = 0;
304         dev->rev[4] = 0;
305         dev->type[32] = 0;
306
307         return 1;
308 }
309
310 static void
311 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
312 {
313         TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
314                 dev->host,
315                 dev->channel,
316                 dev->target,
317                 dev->lun );
318         TRACE( "  Vendor: %s Model: %s Rev: %s\n",
319                 dev->vendor,
320                 dev->model,
321                 dev->rev );
322         TRACE( "  Type:   %s ANSI SCSI revision: %02d\n",
323                 dev->type,
324                 dev->ansirev );
325 }
326
327 static BOOL
328 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
329 {
330         DWORD error;
331         char cstr[20];
332         DWORD hc;
333         hc = (ha << 16) + chan;
334         sprintf(cstr, "c%02d", num_controller);
335         if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
336         {
337                 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
338         }
339         return error;
340 }
341
342 static void
343 SCSI_MapHCtoController()
344 {
345         HKEY hkeyScsi;
346         HKEY hkeyControllerMap;
347         DWORD disposition;
348
349         char idstr[20];
350         DWORD cbIdStr = 20;
351         int i = 0;
352         DWORD type = 0;
353         DWORD error;
354
355         DWORD num_controller = 0;
356         int last_ha = -1;
357         int last_chan = -1;
358
359         int ha = 0;
360         int chan = 0;
361
362         if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
363         {
364                 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
365                 return;
366         }
367         if( disposition != REG_OPENED_EXISTING_KEY )
368         {
369                 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
370         }
371         if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
372         {
373                 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
374                 RegCloseKey(hkeyScsi);
375                 return;
376         }
377         
378         for( i=0; (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
379         {
380                 sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan);
381                 if( last_ha < ha )
382                 {       /* Next HA */
383                         last_ha = ha;
384                         last_chan = chan;
385                         SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
386                         num_controller++;
387                 }
388                 else if( last_ha > ha )
389                 {
390                         FIXME("Expected registry to be sorted\n");
391                 }
392                 /* last_ha == ha */
393                 else if( last_chan < chan )
394                 {
395                         last_chan = chan;
396                         SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
397                         num_controller++;
398                 }
399                 else if( last_chan > chan )
400                 {
401                         FIXME("Expected registry to be sorted\n");
402                 }
403                 /* else last_ha == ha && last_chan == chan so do nothing */
404         }
405         /* Set (default) value to number of controllers */
406         if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
407         {
408                 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
409         }
410         RegCloseKey(hkeyControllerMap);
411         RegCloseKey(hkeyScsi);
412         return;
413 }
414 #endif
415
416 static void
417 SCSI_GetProcinfo()
418 /* I'll admit, this function is somewhat of a mess... it was originally
419  * designed to make some sort of linked list then I realized that
420  * HKEY_DYN_DATA would be a lot less messy
421  */
422 {
423 #ifdef linux
424         FILE * procfile = NULL;
425
426         int result = 0;
427
428         struct LinuxProcScsiDevice dev;
429
430         char idstr[20];
431         char devstr[20];
432
433         int devnum=0;
434         int num_ha = 0;
435
436         HKEY hkeyScsi;
437         DWORD disposition;
438
439         procfile = fopen( "/proc/scsi/scsi", "r" );
440         if( !procfile )
441         {
442                 ERR("Could not open /proc/scsi/scsi\n");
443                 return;
444         }
445
446         result = fscanf( procfile, "Attached devices: \n");
447         if( result != 0 )
448         {
449                 ERR("Incorrect /proc/scsi/scsi format");
450                 return;
451         }
452
453         if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
454         {
455                 ERR("Could not create HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
456                 return;
457         }
458
459         /* Read info for one device */
460         while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
461         {
462                 /* Add to registry */
463
464                 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
465                 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
466                 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
467                 {
468                         ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
469                 }
470
471                 /* Debug output */
472                 SCSI_printprocentry( &dev );
473
474                 /* FIXME: We *REALLY* need number of controllers.. not ha */
475                 /* num of hostadapters is highest ha + 1 */
476                 if( dev.host >= num_ha )
477                         num_ha = dev.host+1;
478                 devnum++;
479         } /* while(1) */
480         if( result != EOF )
481         {
482                 ERR("Incorrect /proc/scsi/scsi format");
483         }
484         fclose( procfile );
485         if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
486         {
487                 ERR("Could not set value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
488         }
489         RegCloseKey(hkeyScsi);
490         return;
491 #endif
492 }