Beginner's guide to writing a SATA device driver?
Ozgur Karatas
ozgurk at ieee.org
Sat May 7 18:40:11 EDT 2022
On Sun, May 8, 2022 at 12:34 AM Gabriel L. Somlo <gsomlo at gmail.com> wrote:
>
> Hi,
>
Hello Gabriel,
> 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.
>
I think, first of all look at how the drivers on SATA hardware are
coded under Linux kernel at low level
(you are expected to know a little about Linux C standards).
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/ata/
and if it were me, I would start with ata_generic.c first:
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/ata/ata_generic.c
For another example, you can take a look at Bootlin source code:
https://elixir.bootlin.com/linux/latest/source/drivers/ata/sata_mv.c
For a working logic, take a look at this document:
https://github.com/digi-embedded/linux/blob/v4.1/dey-2.2/maint/Documentation/devicetree/bindings/ata/ahci-platform.txt
Right after these, you should have an idea about "Block Drivers"
because you will need to contact kernel.
Here is a great guide for you:
http://www.embeddedlinux.org.cn/essentiallinuxdevicedrivers/final/ch14.html
I think all these will be enough for you at first.
Salut,
Ozgur
> 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
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
More information about the Kernelnewbies
mailing list