General
[1.1] Why change the SG device
driver in Linux kernel 2.2.6?
[1.2]
But I have my own mods to sg.c ...
[1.3]
Is there a version of this sg driver for 2.0.x ?
[1.4]
Scsi device, bus and driver resets
[1.5]
How to make an ATAPI cdrom appear as a SCSI device
Problems with SG and other kernel and hardware issues
[2.1]
Kernel build dies in sg.c complaining about scsi_logging
[2.2]
When I change SG_BIG_BUFF it is not reflected in
/proc/sys/kernel/sg-big-buff
[2.3]
Foooooooood fight panic from aha1542
[2.4] DMA
problems with sym53c416
[2.5] aha152x
[2.6]
Agfa SnapScan (scanner under SANE)
[2.7] aic7xxx
[2.8]
SCSI_IOCTL_SEND_COMMAND
[2.9] Canon
backend in SANE
[2.10]
Sg freezes in early lk 2.4 series
[2.11]
Ide-scsi driver continually resets in lk 2.4
[2.12] Sg in Redhat 6.2, 7.0 and Mandrake
7.1
[2.13] Sg in Redhat 7.3
Application related issues
[3.1]
The compiler is picking up the wrong sg.h ?
[3.2] Cdrecord
[3.3] Cdrdao
[3.4] SANE
[3.5]
Cdparanoia [a.k.a. cdda paranoia or just paranoia]
[3.6]
Finding the target id of a scsi device
The memory starvation problem has improved since a new kernel memory
allocator was included in kernel 2.2.10 .
The RedHat 6.2 upgrade kernel, RedHat 7.0 and Mandrake 7.1 contain the SCSI mid level patch that activates the sg driver reset capability. As of lk 2.4.0 the required SCSI mid level patch is not in the main kernel tree. However a patch has been posted on the linux-scsi mailing list.
Here is the SCSI mid level patch
for lk 2.2.16. This mid level patch
for lk 2.2.18 was updated on 2001/3/5. This mid level patch for lk 2.4.2 was updated on
2001/03/21. Here is a more recent 2.4.7
patch.
This works if both ide-cd and ide-scsi are modules. If not what follows is a more detailed approach.
Stage 1 can be accomplished by rebuilding the kernel with
CONFIG_BLK_DEV_IDECD deselected. Perhaps a simpler technique (that only
affects 1 device) is to add the following line in the appropriate place
of the file /etc/lilo.conf :
append="hdd=ide-scsi" .
Stage 2 can be accomplished by rebuilding the kernel to include a
SCSI subsytem and with CONFIG_BLK_DEV_SR built in or a module. Most
distributions come with the SCSI subsystem (and its cdrom driver) built
in. Further, the ide-scsi driver (called CONFIG_BLK_DEV_IDESCSI needs to
be built in or be a module. Most distributions come with the ide-scsi
module available. The simplest way to find out is to write:
modprobe ide-scsi
[The /etc/modules.conf file can be tweaked to make this module load
automatically as required.] If this has worked then the following line
should output data describing your cdrom device:
cat /proc/scsi/scsi
If so, then try cdrecord (xcdroast or whatever) again and it should
be happy.
If you can't use the recommended ioctls() (e.g. not visible) then increase the value of the SG_DEF_RESERVED_SIZE define to get a similar effect.
As for /proc/sys/kernel/sg-big-buff, it is only visible
when sg is built into the kernel (i.e. not when sg is a module). If
available, it yields the size of the reserved buffer of the most
recently opened sg device. It is only readable. If the lk 2.4 series is
being used then /proc/scsi/sg/def_reserved_size contains the same
information, is visible whenever sg is built in or loaded as a module
and is writeable.
Problems seem to have re-emerged with this driver (or the way sg
uses it) in lk 2.4.0-test9 (or kernel around that time) and they are now fixed
in lk 2.4.0-test11
There is a new aic7xxx driver which is sponsored by adaptec. It is
currently in beta test and there have been favourable reports. It can be
found at people.FreeBSD.org/~gibbs/linux
.
* struct sdata {
* unsigned int inlen; [i]
Length of data to be written to device
* unsigned int outlen; [i]
Length of data to be read from device
* unsigned char cmd[x]; [i] SCSI
command (6 <= x <= 12).
*
[o] Data read from device starts here.
*
[o] On error, sense buffer starts here.
* unsigned char wdata[y]; [i] Data written to
device starts here.
* };
* Notes:
* - The SCSI command length is determined
by examining the 1st byte
* of the given command. There
is no way to override this.
* - Data transfers are limited to PAGE_SIZE
(4K on i386, 8K on alpha).
* - The length (x + y) must be at least
OMAX_SB_LEN bytes long to
* accommodate the sense buffer
when an error occurs.
* The sense buffer is truncated
to OMAX_SB_LEN (16) bytes so that
* old code will not be
surprised.
* - If a Unix error occurs (e.g. ENOMEM)
then the user will receive
* a negative return and the
Unix error code in 'errno'.
* If the SCSI command succeeds
then 0 is returned.
* Positive numbers returned are
the compacted SCSI error codes (4
* bytes in one int) where the
lowest byte is the SCSI status.
* See the drivers/scsi/scsi.h
file for more information on this.
This problem only impacts sg 2.1.x versions of sg (and sg version
3.0.17).
It is regrettable that such issues arise, but from a technical
viewpoint it is a wise change.
glibc comes with it's own headers for these files in /usr/include/scsi - but they're not currently synched. When "#include <scsi/sg.h>" is written in an application then this refers to the file /usr/include/scsi/sg.h . Glibc 2.1.3 and later versions should get it right (but will probably be out of sync again when lk 2.4 is released).
The fact the /usr/include/linux is a symbolic link opens up the following solution proposed by the author of cdparanoia (Monty): #include <linux/../scsi/sg.h> .
[I would like to thank Andreas Jaeger <aj@suse.de> for his
contributions on this subject.]
There is a mailing list that covers cdrecord, cdrdao and cdparanoia issues at other-cdwrite@lists.debian.org . You can subscribe by sending mail to other-cdwrite-request@lists.debian.org containing only the word subscribe in the body. Please do not send subscription requests directly to the main list.
Cdrecord uses the sg interface to access IDE ATAPI cd writers. See FAQ 1.5 .
In the linux 2.4 kernel series there has been an increase in
problems when the ide-scsi driver is used so that cdrecord can control
ATAPI (IDE) cd writers. The problem may be related to the aggressive
manner in which the IDE subsystem attempts to optimize the speed of
transfers to devices it controls. Some people experiencing timeouts and
machine lockups have found that reducing the DMA setting via the hdparm
command has fixed the problem. If the cd writer is connected to /dev/hdd
then users have reported success with these 2 commands:
hdparm -d0 -c1 /dev/hdd
hdparm -d 1 -X 34 /dev/hdd
The first one turns off DMA completely while the second one sets it
"multiword DMA mode 2". Cd writers do not need the types of speeds that
modern disks utilize. Even burning at "x16" implies a sustained transfer
rate of 16 times 150 KB/sec which is approximately 2.4 MB/sec, not
really that fast. There has also been a report that moving a cd writer
off a high speed IDE controller (Promise) and back to the motherboard's
lower speed IDE controllers has fixed a random IDE bus reset problem.
Another report suggests reducing (or turning off) the DMA on the IDE
hard disk can also stop lockups. Jörg Schilling states that DMA
should be used for burn speeds greater tha "x12". The lowest IDE DMA
speed of 33 MB/sec should be sufficient for the foreseeable future.
If things are failing with SANE then defining various environment variables may help. For Bourne derived shells 'export SANE_DEBUG_SANEI_SCSI=127' and 'export SANE_DEBUG_<BACKEND_NAME>=127' will provide output that may help those trying to trace the problem.
There is a problem with SANE 1.0.3 and 1.0.4 when used with the
version 3 sg driver found in the lk 2.4 series. The default timeout of
10 seconds is too short and causes SCSI bus resets on many scanners. In
the SANE source this change from Abel Deuring is required:
Try to change the following line in sanei/sanei_scsi.c
req->sgdata.sg3.hdr.timeout =
10000;
(line 1892 in Sane 1.0.4) to:
req->sgdata.sg3.hdr.timeout =
1000 * 10 * 60;
The SANE CVS has been updated so SANE version 1.0.5 won't have this
problem. Also major Linux distributions that will be based on early
versions of lk 2.4 will most likely include this patch in their SANE
packages.
The above problem is fixed in SANE 1.0.5 . Some SCSI (pseudo) adapters (e.g. usb/microtek currently but may soon be fixed) do not support scatter gather at the device level. In such cases set the environment variable SANE_SG_BUFFERSIZE to 32768. When using the bash shell this can be done with "export SANE_SG_BUFFERSIZE=32768".
> How can I translate between BUS,ID,LUN <-> sg <-> sr (hd)?
The answer depends on what size of "hammer" you wish to use on this problem.
At one extreme is the device pseudo file system (devfs) which is
included in 2.4 kernels (but not usually the default). It makes both the
host/channel/target/lun hierarchy and (when devfsd is used) the linux
traditional "sda"-like device names available. To get your mapping may
require a fair amount of directory scanning.
Next there is scsidev ( see http://www.garloff.de/kurt/linux/scsidev ). Once this utility is run it adds a /dev/scsi directory that looks like this on my system:
$ ls -l /dev/scsi
brw------- 1 root
root 8, 0 Aug 12 11:04
sdh1-0c0i0l0
crw------- 1 root
root 21, 0 Aug 12 10:48
sgh1-0c0i0l0
crw------- 1 root
root 21, 1 Aug 12 10:48
sgh2-0c0i2l0
crw------- 1 root
root 21, 3 Aug 12 11:04
sgh2-0c0i5l0
crw------- 1 root
root 21, 2 Aug 12 10:48
sgh2-0c0i6l0
br-------- 1 root
root 11, 0 Aug 12 10:48
srh2-0c0i2l0
br-------- 1 root
root 11, 1 Aug 12 10:48
srh2-0c0i6l0
This time a relatively simple directory scan should answer your question. [Even though devfs introduces a /dev/scsi directory, it and scsidev do seem to happily co-exist.]
Another approach is to parse the output of 'cat /proc/scsi/scsi'. It
is not pretty but some applications do it:
$ cat /proc/scsi/scsi
Attached devices:
Host: scsi1 Channel: 00 Id: 00 Lun: 00
Vendor: IBM Model:
DNES-309170W Rev: SA30
Type:
Direct-Access
ANSI SCSI revision: 03
Host: scsi2 Channel: 00 Id: 02 Lun: 00
Vendor: PIONEER Model: DVD-ROM DVD-303 Rev: 1.10
Type:
CD-ROM
ANSI SCSI revision: 02
Host: scsi2 Channel: 00 Id: 06 Lun: 00
Vendor: YAMAHA Model:
CRW4416S Rev: 1.0g
Type:
CD-ROM
ANSI SCSI revision: 02
Host: scsi2 Channel: 00 Id: 05 Lun: 00
Vendor: UMAX Model: Astra
1220S Rev: V1.2
Type:
Scanner
ANSI SCSI revision: 02
Then there is the SCSI_IOCTL_GET_IDLUN ioctl(). It does some awkward
bit stuffing and be careful with the hostnumber (because in lk 2.2 and
earlier you will need to call the additional SCSI_IOCTL_GET_BUS_NUMBER
ioctl() to get that number). It can applied to all SCSI device names.
Finally the sg driver tries to make this a bit simpler with the
SG_GET_SCSI_ID ioctl(). See sg's documentation. It should be simple
enough to see what it does from /usr/src/linux/include/scsi/sg.h :
typedef struct sg_scsi_id {
int
host_no; /* as in
"scsi<n>" where 'n' is one of 0, 1, 2 etc */
int channel;
int
scsi_id; /* scsi id of target
device */
int lun;
int scsi_type; /*
TYPE_... defined in scsi/scsi.h */
/* ...... */
} Sg_scsi_id;
For examples of this see the sg web site and the sg utilities. The best example is a program called sg_map (another is sg_scan). The output of sg_map for my system looks like:
$ sg_map
/dev/sg0 /dev/sda
/dev/sg1 /dev/sr0
/dev/sg2 /dev/sr1
/dev/sg3
Note that /dev/sg3 doesn't "map" because it is a scanner. With this extra "x" option this becomes:
$ sg_map -x
/dev/sg0 1 0 0 0 0 /dev/sda
/dev/sg1 2 0 2 0 5 /dev/sr0
/dev/sg2 2 0 6 0 5 /dev/sr1
/dev/sg3 2 0 5 0 6
The numbers are host_number, channel, id, lun and scsi device type (where "6" is a scanner). With the sg driver in lk 2.4 similar information is provided via the "proc" file system. After the header line each row corresponds to an sg device starting at /dev/sg0.
$ cd /proc/scsi/sg ; cat device_hdr devices
host chan
id lun
type bopens qdepth busy
1
0
0
0
0
4 63 3
2
0
2
0
5
0
4 0
2
0
6
0
5
0
4 0
2
0
5
0
6
0
4 0
If a device has been removed the a row of "-1"s is output. A device
can be removed with a line like 'echo "scsi remove-single-device 2 0 6
0" > /proc/scsi/scsi'. Also "-1"s are only output (indicating a
"hole" in the mapping) if there is a following device.
If the ide-scsi pseudo driver is being used I know of no way to
establish the sr <-> hd mapping after the fact. Suggestions?
Return to main page.
Douglas Gilbert can be
emailed at this address.
Last updated: 14th June 2002, 23:00