1 /**************************************************************************
3 (C) 2000 David Elliott <dfe@netnitco.net>
4 Licensed under the WINE (X11) license
7 /* These routines are to be called from either WNASPI32 or WINASPI */
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.
20 Registry format is currently:
23 (default)=number of host adapters
24 hHHcCCtTTdDD=linux device name
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
34 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(aspi);
42 /* Internal function prototypes */
47 SCSI_MapHCtoController();
49 /* Exported functions */
53 /* For now we just call SCSI_GetProcinfo */
55 SCSI_MapHCtoController();
59 ASPI_GetNumControllers()
62 HKEY hkeyControllerMap;
64 DWORD type = REG_DWORD;
66 DWORD cbData = sizeof(num_ha);
68 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
70 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
74 if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
76 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
77 RegCloseKey(hkeyScsi);
81 if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
83 ERR("Could not query value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
86 RegCloseKey(hkeyControllerMap);
87 RegCloseKey(hkeyScsi);
88 TRACE("Returning %ld host adapters\n", num_ha );
93 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
100 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
102 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
107 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
109 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
111 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
112 RegCloseKey(hkeyScsi);
115 RegCloseKey(hkeyScsi);
117 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
121 /* SCSI_GetHCforController
123 * HIWORD: Host Adapter
127 ASPI_GetHCforController( int controller )
129 DWORD hc = 0xFFFFFFFF;
133 HKEY hkeyControllerMap;
134 DWORD type = REG_DWORD;
135 DWORD cbData = sizeof(DWORD);
138 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
139 hc = (controller << 16);
142 if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
144 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
148 if( disposition != REG_OPENED_EXISTING_KEY )
150 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
152 if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
154 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
155 RegCloseKey(hkeyScsi);
159 if( disposition != REG_OPENED_EXISTING_KEY )
161 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
164 sprintf(cstr, "c%02d", controller);
165 if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
167 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
168 SetLastError( error );
170 RegCloseKey(hkeyControllerMap);
171 RegCloseKey(hkeyScsi);
177 SCSI_OpenDevice( int h, int c, int t, int d )
183 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
185 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
189 TRACE("Opening device %s mode O_RDWR\n",devstr);
190 fd = open(devstr, O_RDWR);
194 TRACE("open failed\n");
195 FILE_SetDosError(); /* SetLastError() to errno */
196 TRACE("GetLastError: %ld\n", GetLastError());
203 SCSI_LinuxSetTimeout( int fd, int timeout )
206 TRACE("Setting timeout to %d jiffies\n", timeout);
207 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
210 WARN("Could not set timeout errno=%d!\n",errno);
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
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 )
232 TRACE("Writing to Liunx sg device\n");
233 dwBytes = write( fd, lpInBuffer, cbInBuffer );
234 if( dwBytes != cbInBuffer )
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;
246 TRACE("Reading reply from Linux sg device\n");
247 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
248 if( *lpcbBytesReturned != cbOutBuffer )
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 );
259 /* Internal functions */
260 struct LinuxProcScsiDevice
274 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
277 result = fscanf( procfile,
278 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
287 result = fscanf( procfile,
288 " Vendor: %8c Model: %16c Rev: %4c\n",
295 result = fscanf( procfile,
296 " Type: %32c ANSI SCSI revision: %d\n",
301 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
311 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
313 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
318 TRACE( " Vendor: %s Model: %s Rev: %s\n",
322 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
328 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
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 )
337 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
343 SCSI_MapHCtoController()
346 HKEY hkeyControllerMap;
355 DWORD num_controller = 0;
362 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
364 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
367 if( disposition != REG_OPENED_EXISTING_KEY )
369 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
371 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
373 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
374 RegCloseKey(hkeyScsi);
378 for( i=0; (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
380 sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan);
385 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
388 else if( last_ha > ha )
390 FIXME("Expected registry to be sorted\n");
393 else if( last_chan < chan )
396 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
399 else if( last_chan > chan )
401 FIXME("Expected registry to be sorted\n");
403 /* else last_ha == ha && last_chan == chan so do nothing */
405 /* Set (default) value to number of controllers */
406 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
408 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
410 RegCloseKey(hkeyControllerMap);
411 RegCloseKey(hkeyScsi);
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
424 FILE * procfile = NULL;
428 struct LinuxProcScsiDevice dev;
439 procfile = fopen( "/proc/scsi/scsi", "r" );
442 ERR("Could not open /proc/scsi/scsi\n");
446 result = fscanf( procfile, "Attached devices: \n");
449 ERR("Incorrect /proc/scsi/scsi format");
453 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
455 ERR("Could not create HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
459 /* Read info for one device */
460 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
462 /* Add to registry */
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 )
468 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
472 SCSI_printprocentry( &dev );
474 /* FIXME: We *REALLY* need number of controllers.. not ha */
475 /* num of hostadapters is highest ha + 1 */
476 if( dev.host >= num_ha )
482 ERR("Incorrect /proc/scsi/scsi format");
485 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
487 ERR("Could not set value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
489 RegCloseKey(hkeyScsi);