Beginner's guide to writing a SATA device driver?

Gabriel L. Somlo gsomlo at gmail.com
Sat May 7 16:33:08 EDT 2022


Hi,

I have a (soft-IP, FPGA based, single-port) very simple SATA device
(https://github.com/enjoy-digital/litesata) that provides me (r/w)
access to a single physical SATA hard drive. I can access one sector
at a time from bare-metal C code, using DMA and the following three
MMIO register sets:

	- phy:
		u8 enable;	// write 1 to bit 0 to enable phy
				// bits 1..7 reserved
		u8 status_ro ;	// bit 0 = 1 if overall phy ready
				// bit 1 = 1 if phy tx ready
				// bit 2 = 1 if phy rx ready
				// bit 3 = 1 if phy ctrl ready

	- dma_writer:		// write one 512-bit sector to disk
		u64 sector;	// 48-bit disk sector number
		u64 base;	// 64-bit dma base address (source buffer)
		u8  start;	// write 1 to bit 0 to start DMA transfer
		u8  done_ro;	// read 1 from bit 0 when transfer complete
		u8  error_ro;	// read 1 from bit 0 if transfer failed

	- dma_reader:		// read one 512-bit sector from disk
		u64 sector;	// 48-bit disk sector number
		u64 base;	// 64-bit dma base address (dest. buffer)
		u8  start;	// write 1 to bit 0 to start DMA transfer
		u8  done_ro;	// read 1 from bit 0 when transfer complete
		u8  error_ro;	// read 1 from bit 0 if transfer failed

For practice, I'm trying to write a low level SATA driver to access
the hard drive from Linux using this device.

I've tried studying existing drivers under `drivers/ata/*` to get an
idea how to "connect" my device into the Linux SATA subsystem, and
also tried studying the "libATA Developer’s Guide" at
https://www.kernel.org/doc/html/v5.18-rc5/driver-api/libata.html

It turns out there's a sizable "impedance mismatch" between my
understanding of my device's simple register interface and most of
the "production" devices for which existing drivers have been
written in `drivers/ata/*`...

I'm not even sure if I should be looking at `drivers/ata/ahci_*.c` or
rather `drivers/ata/sata_*.c` :)

I'd like some help finding an existing driver that's "closest"
conceptually to the behavior of my device (outlined above), that I can
use as a source of inspiration and as a starting point in learning about
the linux SATA subsystem.

Alternatively, is there some "guided tour to the Linux SATA subsystem"
that's a bit less of a firehose than the "LibATA Developer's Guide"?

Many thanks for any clues or pointers,
--Gabriel



More information about the Kernelnewbies mailing list