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 */
48 SCSI_MapHCtoController();
51 /* Exported functions */
55 /* For now we just call SCSI_GetProcinfo */
58 SCSI_MapHCtoController();
63 ASPI_GetNumControllers()
66 HKEY hkeyControllerMap;
68 DWORD type = REG_DWORD;
70 DWORD cbData = sizeof(num_ha);
72 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
74 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
78 if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
80 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
81 RegCloseKey(hkeyScsi);
85 if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
87 ERR("Could not query value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
90 RegCloseKey(hkeyControllerMap);
91 RegCloseKey(hkeyScsi);
92 TRACE("Returning %ld host adapters\n", num_ha );
97 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
104 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
106 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
111 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
113 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
115 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
116 RegCloseKey(hkeyScsi);
119 RegCloseKey(hkeyScsi);
121 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
125 /* SCSI_GetHCforController
127 * HIWORD: Host Adapter
131 ASPI_GetHCforController( int controller )
133 DWORD hc = 0xFFFFFFFF;
137 HKEY hkeyControllerMap;
138 DWORD type = REG_DWORD;
139 DWORD cbData = sizeof(DWORD);
142 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
143 hc = (controller << 16);
146 if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
148 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
152 if( disposition != REG_OPENED_EXISTING_KEY )
154 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
156 if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
158 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
159 RegCloseKey(hkeyScsi);
163 if( disposition != REG_OPENED_EXISTING_KEY )
165 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
168 sprintf(cstr, "c%02d", controller);
169 if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
171 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
172 SetLastError( error );
174 RegCloseKey(hkeyControllerMap);
175 RegCloseKey(hkeyScsi);
181 SCSI_OpenDevice( int h, int c, int t, int d )
187 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
189 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
193 TRACE("Opening device %s mode O_RDWR\n",devstr);
194 fd = open(devstr, O_RDWR);
198 TRACE("open failed\n");
199 FILE_SetDosError(); /* SetLastError() to errno */
200 TRACE("GetLastError: %ld\n", GetLastError());
207 * Checks to make sure the CMD_LEN is correct
210 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
212 int index=(cmd>>5)&7;
214 if (len!=scsi_command_size[index])
216 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
217 cmd, len, scsi_command_size[index]);
218 ioctl(fd,SG_NEXT_CMD_LEN,&len);
223 SCSI_LinuxSetTimeout( int fd, int timeout )
226 TRACE("Setting timeout to %d jiffies\n", timeout);
227 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
230 WARN("Could not set timeout errno=%d!\n",errno);
236 /* This function takes care of the write/read to the linux sg device.
237 * It returns TRUE or FALSE and uses FILE_SetDosError() to convert
238 * UNIX errno to Windows GetLastError(). The reason for that is that
239 * several programs will check that error and we might as well set
240 * it here. We also return the value of the read call in
243 BOOL /* NOTE: This function SHOULD BLOCK */
244 SCSI_LinuxDeviceIo( int fd,
245 struct sg_header * lpInBuffer, DWORD cbInBuffer,
246 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
247 LPDWORD lpcbBytesReturned )
252 TRACE("Writing to Liunx sg device\n");
253 dwBytes = write( fd, lpInBuffer, cbInBuffer );
254 if( dwBytes != cbInBuffer )
257 save_error = GetLastError();
258 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
259 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
260 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");
261 WARN("error= %ld\n", save_error );
262 *lpcbBytesReturned = 0;
266 TRACE("Reading reply from Linux sg device\n");
267 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
268 if( *lpcbBytesReturned != cbOutBuffer )
271 save_error = GetLastError();
272 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
273 WARN("error= %ld\n", save_error );
279 /* Internal functions */
280 struct LinuxProcScsiDevice
294 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
297 result = fscanf( procfile,
298 "Host: scsi%d Channel: %d Id: %d Lun: %d\n",
307 result = fscanf( procfile,
308 " Vendor: %8c Model: %16c Rev: %4c\n",
315 result = fscanf( procfile,
316 " Type: %32c ANSI SCSI revision: %d\n",
321 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
331 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
333 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
338 TRACE( " Vendor: %s Model: %s Rev: %s\n",
342 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
348 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
353 hc = (ha << 16) + chan;
354 sprintf(cstr, "c%02d", num_controller);
355 if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
357 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
363 SCSI_MapHCtoController()
366 HKEY hkeyControllerMap;
375 DWORD num_controller = 0;
382 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
384 ERR("Could not open HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
387 if( disposition != REG_OPENED_EXISTING_KEY )
389 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
391 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
393 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
394 RegCloseKey(hkeyScsi);
398 for( i=0; (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
400 sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan);
405 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
408 else if( last_ha > ha )
410 FIXME("Expected registry to be sorted\n");
413 else if( last_chan < chan )
416 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
419 else if( last_chan > chan )
421 FIXME("Expected registry to be sorted\n");
423 /* else last_ha == ha && last_chan == chan so do nothing */
425 /* Set (default) value to number of controllers */
426 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
428 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
430 RegCloseKey(hkeyControllerMap);
431 RegCloseKey(hkeyScsi);
438 /* I'll admit, this function is somewhat of a mess... it was originally
439 * designed to make some sort of linked list then I realized that
440 * HKEY_DYN_DATA would be a lot less messy
444 FILE * procfile = NULL;
448 struct LinuxProcScsiDevice dev;
459 procfile = fopen( "/proc/scsi/scsi", "r" );
462 ERR("Could not open /proc/scsi/scsi\n");
466 result = fscanf( procfile, "Attached devices: \n");
469 ERR("Incorrect /proc/scsi/scsi format");
473 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
475 ERR("Could not create HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
479 /* Read info for one device */
480 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
482 /* Add to registry */
484 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
485 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
486 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
488 ERR("Could not set value HEKY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
492 SCSI_printprocentry( &dev );
494 /* FIXME: We *REALLY* need number of controllers.. not ha */
495 /* num of hostadapters is highest ha + 1 */
496 if( dev.host >= num_ha )
502 ERR("Incorrect /proc/scsi/scsi format");
505 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
507 ERR("Could not set value HEKY_DYN_DATA\\%s\n",KEYNAME_SCSI);
509 RegCloseKey(hkeyScsi);