The Linux SCSI subsystem in 2.4 HOWTO
Douglas Gilbert
������
[email protected]
�����
Copyright � 2000, 2001 by Douglas Gilbert
This document describes the SCSI subsystem as the Linux kernel enters the 2.4
production series. An external view of the SCSI subsystem is the main theme.
Material is included to help the system administration of the Linux SCSI
subsystem. There are also brief descriptions of ioctl()s and interfaces that
may be relevant to those writing applications that use this subsystem.
Permission is granted to copy, distribute and/or modify this document under
the terms of the GNU Free Documentation License, Version 1.1 or any later
version published by the Free Software Foundation; with no Invariant
Sections, with no Front-Cover Texts, and with no Back-Cover Texts.
For an online copy of the license see www.fsf.org/copyleft/fdl.html.
-----------------------------------------------------------------------------
Table of Contents
1. Introduction
2. Architectural Overview
3. Names and Addresses
3.1. SCSI Addressing
3.2. Device Names
3.3. Device Names in devfs
3.4. Device Names in scsidev
4. Kernel Configuration
5. Boot Parameters
6. Modules and their Parameters
7. Proc pseudo file system
8. Mid Level, Unifying layer
8.1. boot parameters
8.2. module parameters
8.3. proc interface
9. Upper level drivers
9.1. Disk driver (sd)
9.2. CDROM driver (sr or scd)
9.3. Tape driver (st)
9.4. Generic driver (sg)
10. Lower Level drivers
10.1. Pseudo drivers
11. Raw devices
12. Devfs pseudo file system
A. Common bus types (SCSI and other)
B. Changes between lk 2.2 and 2.4
B.1. Mid level changes
B.2. sd changes
B.3. sr changes
B.4. st changes
B.5. sg changes
C. Performance and Debugging tools
D. Compile options and System calls including ioctls
D.1. Mid level
D.2. sd driver
D.3. sr driver
D.4. st driver
D.5. sg driver
E. References, Credits and Corrections
-----------------------------------------------------------------------------
Chapter 1. Introduction
This document describes the SCSI subsystem as the Linux kernel enters the 2.4
production series.
An external view of the SCSI subsystem is the main theme. Material is
included to help the system administration of the Linux SCSI subsystem. There
are also brief descriptions of ioctl()s and interfaces that may be relevant
to those writing applications that use this subsystem. However internal data
structures and design issues are not addressed [see reference W2]. To
unclutter the presentation, compile options and system calls (including ioctl
()s) have been placed in Appendix D. Although not strictly part of the SCSI
subsystem, there is also a description of raw devices in Chapter 11.
This document follows on from one written four years ago by Drew Eckhardt
called the SCSI-HOWTO [see reference W7]. That document described the SCSI
subsystem in Linux kernel 1.2 and 1.3 series. It is still available from the
Linux Documentation Project [LDP, see reference W8] in its "unmaintained"
section. Both documents have roughly similar structures although Drew's
document has a lot of information on the adapter drivers.
The most up to date version of this document can be found at www.torque.net/
scsi/SCSI-2.4-HOWTO.
This document was built on 22nd January 2001.
-----------------------------------------------------------------------------
Chapter 2. Architectural Overview
The SCSI subsystem has a 3 level architecture with the "upper" level being
closest to the user/kernel interface while the "lower" level is closest to
the hardware. The upper level drivers are commonly known by a terse two
letter abbreviation (e.g. "sd" for SCSI disk driver). The names of the
corresponding module drivers which, for historical reasons, sometimes differ
from the built in driver names are shown in braces in the following diagram.
[scsi-arch]
The 3 level driver architecture of the SCSI subsystem.
The upper level supports the user-kernel interface. In the case of sd and sr
this is a block device interface while for st and sg this is a character
device interface. Any operation using the SCSI subsystem (e.g. reading a
sector from a disk) involves one driver at each of the 3 levels (e.g. sd,
SCSI mid level and aic7xxx drivers).
As can be seen from the diagram, the SCSI mid level is common to all
operations. The SCSI mid level defines internal interfaces and provides
common services to the upper and lower level drivers. Ioctls provided by the
mid level are available to the file descriptors belonging to any of the 4
upper level drivers.
The most common operation on a block device is to "mount" a file system. For
a sd device typically a partition is mounted (e.g. mount -t ext2 /dev/sda6 /
home). For a sr device usually the whole device is mounted (e.g. mount -t
iso9660 /dev/sr0 /mnt/cdrom). The dd command can be used to read or write
from block devices. In this case the block size argument ("bs") needs to be
set to the block size of the device (e.g. 512 bytes for most disks) or an
integral multiple of that device block size (e.g. 8192 bytes). A recent
addition to the block subsystem allows a device (or partition) to be mounted
more than once, at different mount points.
Sd is a member of the generic disk family, as is the hd device from the IDE
subsystem. Apart from mounting sd devices, the fdisk command is available to
view or modify a disk's partition table. Although the hdparm command is
primarily intended for IDE disks, some options work on SCSI disks.
Sr is a member of the CD-ROM subsystem. Apart from mounting file systems
(e.g. iso9660), audio CDs can also be read. The latter action does not
involve mounting a file system but typically by invoking some ioctls. General
purpose Linux commands such as dd cannot be used on audio CDs.
St is a char device for reading and writing tapes. Even though general
purpose command like tar and dd can be used, the mt command is recommended
since it is specially designed for this purpose.
Sg is a SCSI command pass through device that uses a char device interface.
General purpose Linux commands should not be used on sg devices. Applications
such as SANE (for scanners), cdrecord and cdrdao (for cd writers) and
cdparanoia (for reading audio CDs digitally) use sg.
-----------------------------------------------------------------------------
Chapter 3. Names and Addresses
This section covers the various naming schemes that exist in Linux and the
SCSI worlds and how they interact.
-----------------------------------------------------------------------------
3.1. SCSI Addressing
Linux has a four level hierarchical addressing scheme for SCSI devices:
��*�SCSI adapter number {host}
��*�channel number {bus}
��*�id number {target}
��*�lun {lun}
"Lun" is the common SCSI abbreviation of Logical Unit Number. The terms in
braces are the name conventions used by device pseudo file system (devfs).
"Bus" is used in preference to "channel" in the description below.
The SCSI adapter number is typically an arbitrary numbering of the adapter
cards on the internal IO buses (e.g. PCI, PCMCIA, ISA etc) of the computer.
Such adapters are sometimes termed as HBAs (host bus adapters). SCSI adapter
numbers are issued by the kernel in ascending order starting with 0.
Each HBA may control one of more SCSI buses. The various types of SCSI buses
are listed in Appendix A.
Each SCSI bus can have multiple SCSI devices connected to it. In SCSI
parlance the HBA is called the "initiator" and takes up one SCSI id number
(typically 7). The initiator [1] talks to targets which are commonly known as
SCSI devices (e.g. disks). On SCSI parallel buses the number of ids is
related to the width. 8 bit buses (sometimes called "narrow") can have 8 SCSI
ids of which 1 is taken by the HBA leaving 7 for SCSI devices. Wide SCSI
buses are 16 bits wide and can have a maximum of 15 SCSI devices (targets)
attached. The SCSI 3 draft standard allows a large number of ids to be
present on a SCSI bus.
Each SCSI device can contain multiple Logical Unit Numbers (LUNs). These are
typically used by sophisticated tape and cdrom units that support multiple
media.
So Linux's flavour of SCSI addressing is a four level hierarchy:
<scsi(_adapter_number), channel, id, lun>
Using the naming conventions of devfs this becomes:
<host, bus, target, lun>
-----------------------------------------------------------------------------
3.2. Device Names
Device names can be thought of as gateways to a kernel driver that controls a
device rather than the device itself. Hence there can be multiple device
names some of which may offer slightly different characteristics, all mapping
to the same actual device.
The device names of the various SCSI devices are found within the /dev
directory. Traditionally in Linux, SCSI devices have been identified by their
major and minor device number rather than their SCSI bus addresses (e.g. SCSI
target id and LUN). The device pseudo file system (devfs) moves away from the
major and minor device number scheme and for the SCSI subsystem uses device
names based on the SCSI bus addresses [discussed later in Section 3.3 and
ref: W5]. Alternatively, there is a utility called scsidev which addresses
this issue within the scope of the Linux SCSI subsystem and thus does not
have the same system wide impact as devfs. Scsidev is discussed later in
Section 3.4 and ref: W6.
Eight block major numbers are reserved for SCSI disks: 8, 65, 66, 67, 68, 69,
70 and 71. Each major can accommodate 256 minor numbers which, in the case of
SCSI disks, are subdivided as follows:
[b,8,0] /dev/sda
[b,8,1] /dev/sda1
...
[b,8,15] /dev/sda15
[b,8,16] /dev/sdb
[b,8,17] /dev/sdb1
...
[b,8,255] /dev/sdp15
The disk device names without a trailing digit refer to the whole disk (e.g.
/dev/sda) while those with a trailing digit refer to one of the 15 allowable
partitions [2] within that disk.
The remaining 7 SCSI disk block major numbers follow a similar pattern:
[b,65,0] /dev/sdq
[b,65,1] /dev/sdq1
...
[b,65,159] /dev/sdz15
[b,65,160] /dev/sdaa
[b,65,161] /dev/sdaa1
...
[b,65,255] /dev/sdaf15
[b,66,0] /dev/sdag
[b,66,1] /dev/sdag1
...
[b,66,255] /dev/sdav15
...
[b,71,255] /dev/sddx15
So there are 128 possible disks (i.e. /dev/sda to /dev/sddx) each having up
to 15 partitions. By way of contrast, the IDE subsystem allows 20 disks (10
controllers each with 1 master and 1 slave) which can have up to 63
partitions each.
SCSI CD-ROM devices are allocated the block major number of 11. Traditionally
sr has been the device name but scd probably is more recognizable and is
favoured by several recent distributions. 256 SCSI CD-ROM devices are
allowed:
[b,11,0] /dev/scd0 [or /dev/sr0]
[b,11,255] /dev/scd255 [or /dev/sr255]
SCSI tape devices are allocated the char major number of 9. Up to 32 tapes
devices are supported each of which can be accessed in one of four modes (0,
1, 2 and 3), with or without rewind. The devices are allocated as follows:
[c,9,0] /dev/st0 [tape 0, mode 0, rewind]
[c,9,1] /dev/st1 [tape 1, mode 0, rewind]
...
[c,9,31] /dev/st31 [tape 31, mode 0, rewind]
[c,9,32] /dev/st0l [tape 0, mode 1, rewind]
...
[c,9,63] /dev/st31l [tape 31, mode 1, rewind]
[c,9,64] /dev/st0m [tape 0, mode 2, rewind]
...
[c,9,96] /dev/st0a [tape 0, mode 3, rewind]
...
[c,9,127] /dev/st31a [tape 31, mode 3, rewind]
[c,9,128] /dev/nst0 [tape 0, mode 0, no rewind]
...
[c,9,160] /dev/nst0l [tape 0, mode 1, no rewind]
...
[c,9,192] /dev/nst0m [tape 0, mode 2, no rewind]
...
[c,9,224] /dev/nst0a [tape 0, mode 3, no rewind]
...
[c,9,255] /dev/nst31a [tape 31, mode 3, no rewind]
The SCSI generic (sg) devices are allocated the char major number of 21.
There are 256 possible SCSI generic (sg) devices:
[c,21,0] /dev/sg0
[c,21,1] /dev/sg1
...
[c,21,255] /dev/sg255
Note that the SCSI generic device name's use of a trailing letter (e.g. /dev/
sgc) is deprecated.
Each SCSI disk (but not each partition), each SCSI CD-ROM and each SCSI tape
is mapped to an sg device. SCSI devices that don't fit into these three
categories (e.g. scanners) also appear as sg devices.
Pseudo devices [see Section 10.1] can cause devices that are usually not
considered as SCSI to appear as SCSI device names. For example an IDE ATAPI
CD-ROM may be picked up by the ide-scsi pseudo driver and mapped to /dev/scd0