1 /**************************************************************************
3 (C) 2000 David Elliott <dfe@infinite-internet.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>
28 #include <sys/ioctl.h>
35 #include "debugtools.h"
40 DEFAULT_DEBUG_CHANNEL(aspi);
42 /* Internal function prototypes */
48 SCSI_MapHCtoController();
51 static void set_last_error(void)
53 int save_errno = errno; /* errno gets overwritten by printf */
57 SetLastError( ERROR_SHARING_VIOLATION );
60 SetLastError( ERROR_INVALID_HANDLE );
63 SetLastError( ERROR_HANDLE_DISK_FULL );
68 SetLastError( ERROR_ACCESS_DENIED );
71 SetLastError( ERROR_LOCK_VIOLATION );
74 SetLastError( ERROR_FILE_NOT_FOUND );
77 SetLastError( ERROR_CANNOT_MAKE );
81 SetLastError( ERROR_NO_MORE_FILES );
84 SetLastError( ERROR_FILE_EXISTS );
88 SetLastError( ERROR_SEEK );
91 SetLastError( ERROR_DIR_NOT_EMPTY );
94 SetLastError( ERROR_BAD_FORMAT );
97 WARN( "unknown file error: %s\n", strerror(save_errno) );
98 SetLastError( ERROR_GEN_FAILURE );
104 /* Exported functions */
108 /* For now we just call SCSI_GetProcinfo */
111 SCSI_MapHCtoController();
116 ASPI_GetNumControllers()
119 HKEY hkeyControllerMap;
121 DWORD type = REG_DWORD;
123 DWORD cbData = sizeof(num_ha);
125 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
127 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
131 if( (error=RegOpenKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, KEY_ALL_ACCESS, &hkeyControllerMap )) != ERROR_SUCCESS )
133 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
134 RegCloseKey(hkeyScsi);
138 if( RegQueryValueExA(hkeyControllerMap, NULL, NULL, &type, (LPBYTE)&num_ha, &cbData ) != ERROR_SUCCESS )
140 ERR("Could not query value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
143 RegCloseKey(hkeyControllerMap);
144 RegCloseKey(hkeyScsi);
145 TRACE("Returning %ld host adapters\n", num_ha );
150 SCSI_GetDeviceName( int h, int c, int t, int d, LPSTR devstr, LPDWORD lpcbData )
157 if( RegOpenKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, KEY_ALL_ACCESS, &hkeyScsi ) != ERROR_SUCCESS )
159 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
164 sprintf(idstr, "h%02dc%02dt%02dd%02d", h, c, t, d);
166 if( RegQueryValueExA(hkeyScsi, idstr, NULL, &type, devstr, lpcbData) != ERROR_SUCCESS )
168 WARN("Could not query value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
169 RegCloseKey(hkeyScsi);
172 RegCloseKey(hkeyScsi);
174 TRACE("scsi %s: Device name: %s\n",idstr,devstr);
178 /* SCSI_GetHCforController
180 * HIWORD: Host Adapter
184 ASPI_GetHCforController( int controller )
186 DWORD hc = 0xFFFFFFFF;
190 HKEY hkeyControllerMap;
191 DWORD type = REG_DWORD;
192 DWORD cbData = sizeof(DWORD);
195 FIXME("Please fix to map each channel of each host adapter to the proper ASPI controller number!\n");
196 hc = (controller << 16);
199 if( (error=RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition )) != ERROR_SUCCESS )
201 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
205 if( disposition != REG_OPENED_EXISTING_KEY )
207 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
209 if( (error=RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition )) != ERROR_SUCCESS )
211 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
212 RegCloseKey(hkeyScsi);
216 if( disposition != REG_OPENED_EXISTING_KEY )
218 WARN("Created HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
221 sprintf(cstr, "c%02d", controller);
222 if( (error=RegQueryValueExA( hkeyControllerMap, cstr, 0, &type, (LPBYTE)&hc, &cbData)) != ERROR_SUCCESS )
224 ERR("Could not open HKEY_DYN_DATA\\%s\\%s\\%s, error=%lx\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr, error );
225 SetLastError( error );
227 RegCloseKey(hkeyControllerMap);
228 RegCloseKey(hkeyScsi);
234 SCSI_OpenDevice( int h, int c, int t, int d )
239 char dainbread_linux_hack = 1;
241 if(!SCSI_GetDeviceName( h, c, t, d, devstr, &cbData ))
243 WARN("Could not get device name for h%02dc%02dt%02dd%02d\n", h, c, t, d);
248 TRACE("Opening device %s mode O_RDWR\n",devstr);
249 fd = open(devstr, O_RDWR);
253 int len = strlen(devstr);
254 set_last_error(); /* SetLastError() to errno */
255 TRACE("Open failed (%s)\n", strerror(errno));
257 /* in case of "/dev/sgX", convert from sga to sg0
258 * and the other way around.
259 * FIXME: remove it if the distributions
260 * finally manage to agree on something.
261 * The best would probably be a complete
262 * rewrite of the Linux SCSI layer
263 * to use CAM + devfs :-) */
264 if ( (dainbread_linux_hack) &&
266 (devstr[len-3] == 's') && (devstr[len-2] == 'g') )
268 char *p = &devstr[len-1];
269 *p = (*p >= 'a') ? *p - 'a' + '0' : *p - '0' + 'a';
270 dainbread_linux_hack = 0;
271 TRACE("Retry with \"equivalent\" Linux device '%s'\n", devstr);
280 * Checks to make sure the CMD_LEN is correct
283 SCSI_Fix_CMD_LEN(int fd, int cmd, int len)
285 int index=(cmd>>5)&7;
287 if (len!=scsi_command_size[index])
289 TRACE("CDBLen for command %d claims to be %d, expected %d\n",
290 cmd, len, scsi_command_size[index]);
291 ioctl(fd,SG_NEXT_CMD_LEN,&len);
296 SCSI_LinuxSetTimeout( int fd, int timeout )
299 TRACE("Setting timeout to %d jiffies\n", timeout);
300 retval=ioctl(fd,SG_SET_TIMEOUT,&timeout);
303 WARN("Could not set timeout ! (%s)\n", strerror(errno));
309 /* This function takes care of the write/read to the linux sg device.
310 * It returns TRUE or FALSE and uses set_last_error() to convert
311 * UNIX errno to Windows GetLastError(). The reason for that is that
312 * several programs will check that error and we might as well set
313 * it here. We also return the value of the read call in
316 BOOL /* NOTE: This function SHOULD BLOCK */
317 SCSI_LinuxDeviceIo( int fd,
318 struct sg_header * lpInBuffer, DWORD cbInBuffer,
319 struct sg_header * lpOutBuffer, DWORD cbOutBuffer,
320 LPDWORD lpcbBytesReturned )
325 TRACE("Writing to Linux sg device\n");
326 dwBytes = write( fd, lpInBuffer, cbInBuffer );
327 if( dwBytes != cbInBuffer )
330 save_error = GetLastError();
331 WARN("Not enough bytes written to scsi device. bytes=%ld .. %ld\n", cbInBuffer, dwBytes );
332 /* FIXME: set_last_error() never sets error to ERROR_NOT_ENOUGH_MEMORY... */
333 if( save_error == ERROR_NOT_ENOUGH_MEMORY )
334 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)");
335 WARN("error= %ld\n", save_error );
336 *lpcbBytesReturned = 0;
340 TRACE("Reading reply from Linux sg device\n");
341 *lpcbBytesReturned = read( fd, lpOutBuffer, cbOutBuffer );
342 if( *lpcbBytesReturned != cbOutBuffer )
345 save_error = GetLastError();
346 WARN("Not enough bytes read from scsi device. bytes=%ld .. %ld\n", cbOutBuffer, *lpcbBytesReturned);
347 WARN("error= %ld\n", save_error );
353 /* Internal functions */
354 struct LinuxProcScsiDevice
368 * we need to declare white spaces explicitly via %*1[ ],
369 * as there are very dainbread CD-ROM devices out there
370 * which have their manufacturer name blanked out via spaces,
371 * which confuses fscanf's parsing (skips all blank spaces)
374 SCSI_getprocentry( FILE * procfile, struct LinuxProcScsiDevice * dev )
377 result = fscanf( procfile,
378 "Host:%*1[ ]scsi%d%*1[ ]Channel:%*1[ ]%d%*1[ ]Id:%*1[ ]%d%*1[ ]Lun:%*1[ ]%d\n",
385 /* "end of entries" return, so no TRACE() here */
390 ERR("bus id line scan count error\n");
393 result = fscanf( procfile,
394 " Vendor:%*1[ ]%8c%*1[ ]Model:%*1[ ]%16c%*1[ ]Rev:%*1[ ]%4c\n",
400 ERR("model line scan count error\n");
404 result = fscanf( procfile,
405 " Type:%*3[ ]%32c%*1[ ]ANSI%*1[ ]SCSI%*1[ ]revision:%*1[ ]%d\n",
410 ERR("SCSI type line scan count error\n");
413 /* Since we fscanf with %XXc instead of %s.. put a NULL at end */
423 SCSI_printprocentry( const struct LinuxProcScsiDevice * dev )
425 TRACE( "Host: scsi%d Channel: %02d Id: %02d Lun: %02d\n",
430 TRACE( " Vendor: %s Model: %s Rev: %s\n",
434 TRACE( " Type: %s ANSI SCSI revision: %02d\n",
440 SCSI_PutRegControllerMap( HKEY hkeyControllerMap, int num_controller, int ha, int chan)
445 hc = (ha << 16) + chan;
446 sprintf(cstr, "c%02d", num_controller);
447 if( (error=RegSetValueExA( hkeyControllerMap, cstr, 0, REG_DWORD, (LPBYTE)&hc, sizeof(DWORD))) != ERROR_SUCCESS )
449 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP, cstr );
455 SCSI_MapHCtoController()
458 HKEY hkeyControllerMap;
467 DWORD num_controller = 0;
474 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
476 ERR("Could not open HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
479 if( disposition != REG_OPENED_EXISTING_KEY )
481 WARN("Created HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
483 if( RegCreateKeyExA(hkeyScsi, KEYNAME_SCSI_CONTROLLERMAP, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyControllerMap, &disposition ) != ERROR_SUCCESS )
485 ERR("Could not create HKEY_DYN_DATA\\%s\\%s\n", KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
486 RegCloseKey(hkeyScsi);
490 for( i=0; cbIdStr = sizeof(idstr), (error=RegEnumValueA( hkeyScsi, i, idstr, &cbIdStr, NULL, &type, NULL, NULL )) == ERROR_SUCCESS; i++ )
492 if(idstr[0] == '\0') continue; /* skip the default value */
494 if(sscanf(idstr, "h%02dc%02dt%*02dd%*02d", &ha, &chan) != 2) {
495 ERR("incorrect reg. value %s\n", debugstr_a(idstr));
503 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
506 else if( last_ha > ha )
508 FIXME("Expected registry to be sorted\n");
511 else if( last_chan < chan )
514 SCSI_PutRegControllerMap( hkeyControllerMap, num_controller, ha, chan);
517 else if( last_chan > chan )
519 FIXME("Expected registry to be sorted\n");
521 /* else last_ha == ha && last_chan == chan so do nothing */
523 /* Set (default) value to number of controllers */
524 if( RegSetValueExA(hkeyControllerMap, NULL, 0, REG_DWORD, (LPBYTE)&num_controller, sizeof(DWORD) ) != ERROR_SUCCESS )
526 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, KEYNAME_SCSI_CONTROLLERMAP);
528 RegCloseKey(hkeyControllerMap);
529 RegCloseKey(hkeyScsi);
534 int SCSI_Linux_CheckDevices(void)
537 struct dirent *dent = NULL;
539 devdir = opendir("/dev");
540 for (dent=readdir(devdir);dent;dent=readdir(devdir))
542 if (!(strncmp(dent->d_name, "sg", 2)))
549 MESSAGE("WARNING: You don't have any /dev/sgX generic scsi devices ! \"man MAKEDEV\" !\n");
557 /* I'll admit, this function is somewhat of a mess... it was originally
558 * designed to make some sort of linked list then I realized that
559 * HKEY_DYN_DATA would be a lot less messy
563 static const char procname[] = "/proc/scsi/scsi";
564 FILE * procfile = NULL;
566 char read_line[40], read1[10] = "\0", read2[10] = "\0";
569 struct LinuxProcScsiDevice dev;
580 /* Check whether user has generic scsi devices at all */
581 if (!(SCSI_Linux_CheckDevices()))
584 procfile = fopen( procname, "r" );
587 ERR("Could not open %s\n", procname);
591 fgets(read_line, 40, procfile);
592 sscanf( read_line, "Attached %9s %9s", read1, read2);
594 if(strcmp(read1, "devices:"))
596 ERR("Incorrect %s format\n", procname);
600 if(!(strcmp(read2, "none")))
602 ERR("No devices found in %s. Make sure you loaded your SCSI driver or set up ide-scsi emulation for your IDE device if this app needs it !\n", procname);
606 if( RegCreateKeyExA(HKEY_DYN_DATA, KEYNAME_SCSI, 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkeyScsi, &disposition ) != ERROR_SUCCESS )
608 ERR("Could not create HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
612 /* Read info for one device */
613 while( (result = SCSI_getprocentry(procfile, &dev)) > 0 )
615 /* Add to registry */
617 sprintf(idstr, "h%02dc%02dt%02dd%02d", dev.host, dev.channel, dev.target, dev.lun);
618 sprintf(devstr, "/dev/sg%c", 'a'+devnum);
619 if( RegSetValueExA(hkeyScsi, idstr, 0, REG_SZ, devstr, strlen(devstr)+1 ) != ERROR_SUCCESS )
621 ERR("Could not set value HKEY_DYN_DATA\\%s\\%s\n",KEYNAME_SCSI, idstr);
625 SCSI_printprocentry( &dev );
627 /* FIXME: We *REALLY* need number of controllers.. not ha */
628 /* num of hostadapters is highest ha + 1 */
629 if( dev.host >= num_ha )
635 ERR("Sorry, incorrect %s format\n", procname);
638 if( RegSetValueExA(hkeyScsi, NULL, 0, REG_DWORD, (LPBYTE)&num_ha, sizeof(num_ha) ) != ERROR_SUCCESS )
640 ERR("Could not set value HKEY_DYN_DATA\\%s\n",KEYNAME_SCSI);
642 RegCloseKey(hkeyScsi);