6

For one of my applications, I have to understand the the SATA device driver flow. As per my understanding, SATA device driver should be there in the Linux kernel tree. I have referred how to find the driver module associated with a device on Linux to find the device driver for the SATA device.

$ readlink /sys/block/sda/device/driver ../../../../../../../bus/scsi/d

I have go to the above location but haven't found a device driver file.

usr@usr:/sys/bus/scsi/drivers/sd$ ll
total 0
drwxr-xr-x 2 root root    0 Dec  9 17:00 ./
drwxr-xr-x 4 root root    0 Dec  9 17:00 ../
lrwxrwxrwx 1 root root    0 Dec  9 17:47 2:0:1:0 -> ../../../../devices/pci0000:00/0000:00:1f.2/ata3/host2/target2:0:1/2:0:1:0/
--w------- 1 root root 4096 Dec  9 17:47 bind
--w------- 1 root root 4096 Dec  9 17:47 uevent
--w------- 1 root root 4096 Dec  9 17:47 unbind
usr@usr:/sys/bus/scsi/drivers/sd$ 

Please suggest how can I see the device driver which was actually load for SATA hard drive on my system.

Hemant
  • 113
  • First take a look at lsmod. What distribution and kernel, please? – Faheem Mitha Dec 18 '15 at 09:48
  • I am using "Linux version 3.13.0-32-generic (buildd@phianna) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #57~precise1-Ubuntu SMP Tue Jul 15 03:51:20 UTC 2014" ubuntu 12.04 . Can you please elaborate what information I need to check in lsmod? – Hemant Dec 18 '15 at 09:53
  • Hemant, if you are using a stock Ubuntu kernel, and it sounds like you are, much of the functionality is in modules, to make the stock kernel as flexible as possible. So, take a look at the listed module names to see which one is implementing SATA. I seem to remember it is sg (http://www.tldp.org/HOWTO/SCSI-2.4-HOWTO/sg.html), but just do a search for "moulestring linux kernel module" for possible likely strings. – Faheem Mitha Dec 18 '15 at 10:00
  • See http://unix.stackexchange.com/a/6968/4671. This question might be a dupe of that. – Faheem Mitha Dec 18 '15 at 10:02
  • Hey,Thanks for the help, my final goal is to understand the software flow between the SATA Host(HBA) and a SATA device driver(SATA Hard Disk).To understand the flow I need to go through the SATA Host and Device driver code .I know AHCI can be used as a HBA in linux kernel which is at "linux-xlnx-xilinx_v2.6.30/drivers/ata/" but I am not sure about the SATA device driver.Can you point out which device driver code should I look in kernel tree to understand the device code flow or Can you point me somewhere I can get the sample SATA device driver code to undestand Host and Device communication? – Hemant Dec 18 '15 at 10:33

2 Answers2

7

Use udevadm info as described in the other answer to the link you mentioned. Here's an example with a different grep-variant to reduce the output, while walking the /sys tree backwards:

$ udevadm info -a -n /dev/sda | egrep 'looking|DRIVER'
  looking at device '/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda':
    DRIVER==""
  looking at parent device '/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0':
    DRIVERS=="sd"
  looking at parent device '/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0':
    DRIVERS==""
  looking at parent device '/devices/pci0000:00/0000:00:1f.2/ata1/host0':
    DRIVERS==""
  looking at parent device '/devices/pci0000:00/0000:00:1f.2/ata1':
    DRIVERS==""
  looking at parent device '/devices/pci0000:00/0000:00:1f.2':
    DRIVERS=="ahci"
  looking at parent device '/devices/pci0000:00':
    DRIVERS==""

So you can see that on my system, /dev/sda maps to SCSI device 0:0:0:0, which is attached to ata1 on PCI device 0:00:1f.2 (which is the host adapter)

The driver sd is responsible for handling the block device, while the driver ahci is responsible for handling the SATA host adapter.

dirkt
  • 32,309
  • Thank for Answer. This helps a lot and I can find any device driver associate with any device on system with this method of course with a little change in the command argument. – Hemant Feb 09 '17 at 06:18
0

This only attempts to answer part of the question, namely how to find kernel driver code. I'd add this as a comment, but it's too long for one.

First install the Debian kernel sources package.

apt-get install linux-source-3.16

or whatever the corresponding version of your kernel is. Then the source is installed to /usr/src/linux-source-3.16.tar.xz.

Now create the /usr/local/src/linux directory and copy your source there. Namely:

mkdir /usr/local/src/linux 

followed by

cp -ar /usr/src/linux-source-3.16.tar.xz /usr/local/src/linux

Now you can unpack it with tar.

tar xvf linux-source-3.16.tar.xz 
cd linux-source-3.16

Then run find for your desired device driver, say:

/usr/local/src/linux/linux-source-3.16$ find . -name 'sg.c'
./drivers/scsi/sg.c

So the driver in question is in

/usr/local/src/linux/linux-source-3.16/drivers/scsi/sg.c

All the modules listed in lsmod should be in the source tree, in the form modname.c, where modname is the name that appears in the lsmod listing.

Faheem Mitha
  • 35,108