Linux › Section C · Devices › Topic C.5
In depth: SCSI, drivers, and the kernel
Behind every device file sits a stack of kernel drivers, each doing one part of the job. Following a USB stick or a SATA disk through the kernel's SCSI subsystem shows how Linux reuses one design for very different hardware, and kernel modules show how those drivers are loaded and tuned.
Drivers and subsystems
A device driver is kernel code that turns general requests, such as read these blocks, into the exact commands one kind of hardware understands. Drivers do not work alone: each plugs into a subsystem, such as the block layer, the SCSI subsystem, the USB subsystem, input or networking, which gives user space one uniform interface for many different devices. Hardware that no bus can report is described by firmware ACPI tables on PCs and by a device tree on many ARM boards. Most drivers live in the main kernel source tree. Drivers outside it, such as some vendors' graphics drivers, must be rebuilt for every new kernel, often automatically by DKMS.
SCSI in brief
SCSI, the Small Computer System Interface, began as a hardware bus: a host adapter in the computer connected to a chain of devices, each with an ID, and the computer sent commands through the adapter. Its modern successor, Serial Attached SCSI (SAS), is found in servers. Far more important today is the SCSI command set, which outlived the old cables. USB storage devices carry SCSI commands inside USB messages, optical drives use ATAPI, a form of SCSI commands sent over ATA, and SATA disks are presented to the rest of the kernel as SCSI devices. That is why so many disks are named /dev/sd*.
Reading lsscsi output
lsscsi, often a separate package, lists the devices attached to the SCSI subsystem. Illustrative output: [0:0:0:0] disk ATA Samsung SSD 870 /dev/sda, then [1:0:0:0] cd/dvd HL-DT-ST DVDRAM /dev/sr0, then [4:0:0:0] disk Generic SD Reader /dev/sdb and [4:0:0:1] disk Generic MS Reader /dev/sdc. The four numbers in brackets are host adapter, channel (bus), target ID and logical unit number (LUN). Each SATA port and each USB storage device gets its own host number here. The card reader at host 4 has one target with two logical units, one per card slot, and the kernel gives each unit its own device file.
Three layers of SCSI drivers
The SCSI subsystem has three layers. The upper layer has one driver per class of device: sd for disks, sr for optical drives and sg for generic access. These translate requests from the block layer into SCSI commands for that class. The middle layer routes commands and responses and keeps track of every host, bus and device. The lower layer has drivers for particular host adapters, which pass the commands to real hardware. The split exists because a disk command is the same whatever the connection, while each adapter needs its own handling. For any one device file, the kernel normally uses one upper-layer and one lower-layer driver.
Bridges for USB and SATA
A USB stick understands SCSI commands, but they must travel over USB. The USB subsystem has its own three layers: device-class drivers, a core that manages buses, and host controller drivers. Its storage driver, usb-storage, or uas for newer devices, wraps SCSI commands in USB messages and connects to the SCSI subsystem through a lower-layer bridge. SATA disks are harder. They speak ATA, not SCSI, so the libata library must fully translate each SCSI command into an ATA one and back, rather than just repackaging it. ATAPI optical drives are easier, because their commands are already SCSI inside ATA. Either way, sd or sr above sees an ordinary SCSI device.
Generic SCSI devices
Most programs never send SCSI commands: they read and write block devices such as /dev/sda, and the sd driver does the rest. Some tasks need direct access, and the sg driver provides it through generic devices, /dev/sg0 and onwards, one per SCSI device; lsscsi -g lists them alongside the usual names. Writing optical discs is the classic case: it is complicated and nothing critical depends on it, so Linux leaves it to user-space programs that send the SCSI commands themselves, traditionally through an sg device. One drive can therefore be read through /dev/sr0 and written through /dev/sg1, although current burning tools can also use /dev/sr0.
Kernel modules
Most drivers are built as loadable modules, files ending .ko kept in /lib/modules under a directory for each kernel version; uname -r names the running one. lsmod lists loaded modules with their size and which other modules use them. modinfo name shows a module's description, licence, parameters and the hardware IDs it supports. modprobe name loads a module along with any modules it depends on, using a dependency list built by depmod, and modprobe -r name removes it if nothing is using it. modprobe -n -v name is a dry run that shows what would be loaded. Normally you rarely load modules by hand: when a device appears, udev loads the matching driver automatically.
Module options, blacklists and firmware
Modules can take parameters. Pass one when loading, as in modprobe dummy numdummies=2, or set it permanently with an options line in a file in /etc/modprobe.d, such as options dummy numdummies=2; current values appear under /sys/module/dummy/parameters. A blacklist line, such as blacklist nouveau, stops a module from being loaded automatically for matching hardware, which is how a conflicting driver is kept out of the way, although it can still be loaded explicitly. Many devices, especially Wi-Fi and graphics chips, also need firmware, code that runs on the device itself, which drivers load from /lib/firmware. lspci -k shows which driver each PCI device is using, and lsusb lists USB devices.
Worked example: tracing a USB stick
You insert a USB stick and want to see each layer that serves it. 1. lsusb shows the stick by its USB vendor and product IDs: the USB subsystem has found it. 2. lsmod | grep -E 'usb_storage|uas' shows which USB storage driver loaded; lsmod writes module names with underscores. 3. lsscsi shows a new entry such as [6:0:0:0] disk with /dev/sdb: the stick has joined the SCSI subsystem through the bridge, as host 6. 4. lsmod | grep sd_mod shows the upper-layer disk driver. 5. lsblk shows /dev/sdb and its partitions as block devices. 6. Read upwards: USB host controller, USB core, usb-storage or uas, the SCSI layers, sd, then the block device you use.
Worked example: loading a harmless module
The dummy module creates virtual network interfaces that go nowhere, so it is safe to practise with. 1. Read about it first: modinfo dummy lists a parameter, numdummies, the number of interfaces to create. 2. Do a dry run: sudo modprobe -n -v dummy shows the insmod command it would run, without loading anything. 3. Load it with a parameter: sudo modprobe dummy numdummies=2. 4. Check: lsmod | grep dummy shows the module, and ip link shows dummy0 and dummy1. 5. Remove it: sudo modprobe -r dummy, and the interfaces disappear.
Common mistakes to avoid
First, assuming sd devices must be SCSI hardware: SATA and USB disks use the SCSI subsystem too. Second, reading the lsscsi numbers as partitions: they are host, channel, target and LUN. Third, editing module settings into files under /lib/modules instead of /etc/modprobe.d. Fourth, expecting a blacklist line to stop an explicit modprobe: it only prevents automatic loading. Fifth, blaming the driver when a device is detected but does nothing, when a missing firmware file, reported in journalctl -k, is often the cause. Sixth, unloading modules that a running device depends on.
Key takeaways
Drivers plug into subsystems that give user space uniform interfaces. The SCSI command set lives on in USB storage, ATAPI and, through libata translation, SATA disks. lsscsi shows host, channel, target and LUN. SCSI drivers form three layers: upper (sd, sr, sg), middle, and lower adapter drivers, with bridges from the USB subsystem. Generic sg devices let user space send SCSI commands directly. Modules live in /lib/modules; lsmod, modinfo, modprobe and modprobe -r manage them; /etc/modprobe.d holds options and blacklists; firmware comes from /lib/firmware.
Flashcards
Flashcard
Kernel code that turns general requests into commands for specific hardware
Test your understanding
Question 1 of 5
Why does a SATA disk appear as /dev/sda rather than under an ATA-specific name?
Question 2 of 5
In lsscsi output, a card reader shows [3:0:0:0] and [3:0:0:1]. What do the last numbers mean?
Question 3 of 5
Which driver is in the SCSI subsystem's upper layer?
Question 4 of 5
You add blacklist nouveau to a file in /etc/modprobe.d. What is the effect?
Question 5 of 5
What does sudo modprobe -n -v dummy do?
