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 * Checks to make sure the CMD_LEN is correct
206 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
208 int index=(cmd>>5)&7;
210 if (len!=scsi_command_size[index])
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);
219 SCSI_LinuxSetTimeout( int fd, int timeout )
222 TRACE("Setting timeout to %d jiffies\n", timeout);
223 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
226 WARN("Could not set timeout errno=%d!\n",errno);
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
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 )
248 TRACE("Writing to Liunx sg device\n");
249 dwBytes = write( fd, lpInBuffer, cbInBuffer );
250 if( dwBytes != cbInBuffer )
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;
262 TRACE("Reading reply from Linux sg device\n");
263 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
264 if( *lpcbBytesReturned != cbOutBuffer )
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 );
275 /* Internal functions */
276 struct LinuxProcScsiDevice
290 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
293 result = fscanf( procfile,
294 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
303 result = fscanf( procfile,
304 " Vendor: %8c Model: %16c Rev: %4c\n",
311 result = fscanf( procfile,
312 " Type: %32c ANSI SCSI revision: %d\n",
317 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
327 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
329 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
334 TRACE( " Vendor: %s Model: %s Rev: %s\n",
338 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
344 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
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 )
353 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
359 SCSI_MapHCtoController()
362 HKEY hkeyControllerMap;
371 DWORD num_controller = 0;
378 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
380 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
383 if( disposition != REG_OPENED_EXISTING_KEY )
385 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
387 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
389 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
390 RegCloseKey(hkeyScsi);
394 for( i=0; (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
396 sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan);
401 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
404 else if( last_ha > ha )
406 FIXME("Expected registry to be sorted\n");
409 else if( last_chan < chan )
412 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
415 else if( last_chan > chan )
417 FIXME("Expected registry to be sorted\n");
419 /* else last_ha == ha && last_chan == chan so do nothing */
421 /* Set (default) value to number of controllers */
422 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
424 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
426 RegCloseKey(hkeyControllerMap);
427 RegCloseKey(hkeyScsi);
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
440 FILE * procfile = NULL;
444 struct LinuxProcScsiDevice dev;
455 procfile = fopen( "/proc/scsi/scsi", "r" );
458 ERR("Could not open /proc/scsi/scsi\n");
462 result = fscanf( procfile, "Attached devices: \n");
465 ERR("Incorrect /proc/scsi/scsi format");
469 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
471 ERR("Could not create HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
475 /* Read info for one device */
476 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
478 /* Add to registry */
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 )
484 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
488 SCSI_printprocentry( &dev );
490 /* FIXME: We *REALLY* need number of controllers.. not ha */
491 /* num of hostadapters is highest ha + 1 */
492 if( dev.host >= num_ha )
498 ERR("Incorrect /proc/scsi/scsi format");
501 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
503 ERR("Could not set value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
505 RegCloseKey(hkeyScsi);