libata.tmpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="libataDevGuide">
  5. <bookinfo>
  6. <title>libATA Developer's Guide</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Jeff</firstname>
  10. <surname>Garzik</surname>
  11. </author>
  12. </authorgroup>
  13. <copyright>
  14. <year>2003-2005</year>
  15. <holder>Jeff Garzik</holder>
  16. </copyright>
  17. <legalnotice>
  18. <para>
  19. The contents of this file are subject to the Open
  20. Software License version 1.1 that can be found at
  21. <ulink url="http://www.opensource.org/licenses/osl-1.1.txt">http://www.opensource.org/licenses/osl-1.1.txt</ulink> and is included herein
  22. by reference.
  23. </para>
  24. <para>
  25. Alternatively, the contents of this file may be used under the terms
  26. of the GNU General Public License version 2 (the "GPL") as distributed
  27. in the kernel source COPYING file, in which case the provisions of
  28. the GPL are applicable instead of the above. If you wish to allow
  29. the use of your version of this file only under the terms of the
  30. GPL and not to allow others to use your version of this file under
  31. the OSL, indicate your decision by deleting the provisions above and
  32. replace them with the notice and other provisions required by the GPL.
  33. If you do not delete the provisions above, a recipient may use your
  34. version of this file under either the OSL or the GPL.
  35. </para>
  36. </legalnotice>
  37. </bookinfo>
  38. <toc></toc>
  39. <chapter id="libataIntroduction">
  40. <title>Introduction</title>
  41. <para>
  42. libATA is a library used inside the Linux kernel to support ATA host
  43. controllers and devices. libATA provides an ATA driver API, class
  44. transports for ATA and ATAPI devices, and SCSI&lt;-&gt;ATA translation
  45. for ATA devices according to the T10 SAT specification.
  46. </para>
  47. <para>
  48. This Guide documents the libATA driver API, library functions, library
  49. internals, and a couple sample ATA low-level drivers.
  50. </para>
  51. </chapter>
  52. <chapter id="libataDriverApi">
  53. <title>libata Driver API</title>
  54. <para>
  55. struct ata_port_operations is defined for every low-level libata
  56. hardware driver, and it controls how the low-level driver
  57. interfaces with the ATA and SCSI layers.
  58. </para>
  59. <para>
  60. FIS-based drivers will hook into the system with ->qc_prep() and
  61. ->qc_issue() high-level hooks. Hardware which behaves in a manner
  62. similar to PCI IDE hardware may utilize several generic helpers,
  63. defining at a bare minimum the bus I/O addresses of the ATA shadow
  64. register blocks.
  65. </para>
  66. <sect1>
  67. <title>struct ata_port_operations</title>
  68. <sect2><title>Disable ATA port</title>
  69. <programlisting>
  70. void (*port_disable) (struct ata_port *);
  71. </programlisting>
  72. <para>
  73. Called from ata_bus_probe() and ata_bus_reset() error paths,
  74. as well as when unregistering from the SCSI module (rmmod, hot
  75. unplug).
  76. </para>
  77. </sect2>
  78. <sect2><title>Post-IDENTIFY device configuration</title>
  79. <programlisting>
  80. void (*dev_config) (struct ata_port *, struct ata_device *);
  81. </programlisting>
  82. <para>
  83. Called after IDENTIFY [PACKET] DEVICE is issued to each device
  84. found. Typically used to apply device-specific fixups prior to
  85. issue of SET FEATURES - XFER MODE, and prior to operation.
  86. </para>
  87. </sect2>
  88. <sect2><title>Set PIO/DMA mode</title>
  89. <programlisting>
  90. void (*set_piomode) (struct ata_port *, struct ata_device *);
  91. void (*set_dmamode) (struct ata_port *, struct ata_device *);
  92. void (*post_set_mode) (struct ata_port *ap);
  93. </programlisting>
  94. <para>
  95. Hooks called prior to the issue of SET FEATURES - XFER MODE
  96. command. dev->pio_mode is guaranteed to be valid when
  97. ->set_piomode() is called, and dev->dma_mode is guaranteed to be
  98. valid when ->set_dmamode() is called. ->post_set_mode() is
  99. called unconditionally, after the SET FEATURES - XFER MODE
  100. command completes successfully.
  101. </para>
  102. <para>
  103. ->set_piomode() is always called (if present), but
  104. ->set_dma_mode() is only called if DMA is possible.
  105. </para>
  106. </sect2>
  107. <sect2><title>Taskfile read/write</title>
  108. <programlisting>
  109. void (*tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
  110. void (*tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
  111. </programlisting>
  112. <para>
  113. ->tf_load() is called to load the given taskfile into hardware
  114. registers / DMA buffers. ->tf_read() is called to read the
  115. hardware registers / DMA buffers, to obtain the current set of
  116. taskfile register values.
  117. </para>
  118. </sect2>
  119. <sect2><title>ATA command execute</title>
  120. <programlisting>
  121. void (*exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
  122. </programlisting>
  123. <para>
  124. causes an ATA command, previously loaded with
  125. ->tf_load(), to be initiated in hardware.
  126. </para>
  127. </sect2>
  128. <sect2><title>Per-cmd ATAPI DMA capabilities filter</title>
  129. <programlisting>
  130. int (*check_atapi_dma) (struct ata_queued_cmd *qc);
  131. </programlisting>
  132. <para>
  133. Allow low-level driver to filter ATA PACKET commands, returning a status
  134. indicating whether or not it is OK to use DMA for the supplied PACKET
  135. command.
  136. </para>
  137. </sect2>
  138. <sect2><title>Read specific ATA shadow registers</title>
  139. <programlisting>
  140. u8 (*check_status)(struct ata_port *ap);
  141. u8 (*check_altstatus)(struct ata_port *ap);
  142. u8 (*check_err)(struct ata_port *ap);
  143. </programlisting>
  144. <para>
  145. Reads the Status/AltStatus/Error ATA shadow register from
  146. hardware. On some hardware, reading the Status register has
  147. the side effect of clearing the interrupt condition.
  148. </para>
  149. </sect2>
  150. <sect2><title>Select ATA device on bus</title>
  151. <programlisting>
  152. void (*dev_select)(struct ata_port *ap, unsigned int device);
  153. </programlisting>
  154. <para>
  155. Issues the low-level hardware command(s) that causes one of N
  156. hardware devices to be considered 'selected' (active and
  157. available for use) on the ATA bus. This generally has no
  158. meaning on FIS-based devices.
  159. </para>
  160. </sect2>
  161. <sect2><title>Reset ATA bus</title>
  162. <programlisting>
  163. void (*phy_reset) (struct ata_port *ap);
  164. </programlisting>
  165. <para>
  166. The very first step in the probe phase. Actions vary depending
  167. on the bus type, typically. After waking up the device and probing
  168. for device presence (PATA and SATA), typically a soft reset
  169. (SRST) will be performed. Drivers typically use the helper
  170. functions ata_bus_reset() or sata_phy_reset() for this hook.
  171. </para>
  172. </sect2>
  173. <sect2><title>Control PCI IDE BMDMA engine</title>
  174. <programlisting>
  175. void (*bmdma_setup) (struct ata_queued_cmd *qc);
  176. void (*bmdma_start) (struct ata_queued_cmd *qc);
  177. void (*bmdma_stop) (struct ata_port *ap);
  178. u8 (*bmdma_status) (struct ata_port *ap);
  179. </programlisting>
  180. <para>
  181. When setting up an IDE BMDMA transaction, these hooks arm
  182. (->bmdma_setup), fire (->bmdma_start), and halt (->bmdma_stop)
  183. the hardware's DMA engine. ->bmdma_status is used to read the standard
  184. PCI IDE DMA Status register.
  185. </para>
  186. <para>
  187. These hooks are typically either no-ops, or simply not implemented, in
  188. FIS-based drivers.
  189. </para>
  190. </sect2>
  191. <sect2><title>High-level taskfile hooks</title>
  192. <programlisting>
  193. void (*qc_prep) (struct ata_queued_cmd *qc);
  194. int (*qc_issue) (struct ata_queued_cmd *qc);
  195. </programlisting>
  196. <para>
  197. Higher-level hooks, these two hooks can potentially supercede
  198. several of the above taskfile/DMA engine hooks. ->qc_prep is
  199. called after the buffers have been DMA-mapped, and is typically
  200. used to populate the hardware's DMA scatter-gather table.
  201. Most drivers use the standard ata_qc_prep() helper function, but
  202. more advanced drivers roll their own.
  203. </para>
  204. <para>
  205. ->qc_issue is used to make a command active, once the hardware
  206. and S/G tables have been prepared. IDE BMDMA drivers use the
  207. helper function ata_qc_issue_prot() for taskfile protocol-based
  208. dispatch. More advanced drivers implement their own ->qc_issue.
  209. </para>
  210. </sect2>
  211. <sect2><title>Timeout (error) handling</title>
  212. <programlisting>
  213. void (*eng_timeout) (struct ata_port *ap);
  214. </programlisting>
  215. <para>
  216. This is a high level error handling function, called from the
  217. error handling thread, when a command times out. Most newer
  218. hardware will implement its own error handling code here. IDE BMDMA
  219. drivers may use the helper function ata_eng_timeout().
  220. </para>
  221. </sect2>
  222. <sect2><title>Hardware interrupt handling</title>
  223. <programlisting>
  224. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
  225. void (*irq_clear) (struct ata_port *);
  226. </programlisting>
  227. <para>
  228. ->irq_handler is the interrupt handling routine registered with
  229. the system, by libata. ->irq_clear is called during probe just
  230. before the interrupt handler is registered, to be sure hardware
  231. is quiet.
  232. </para>
  233. </sect2>
  234. <sect2><title>SATA phy read/write</title>
  235. <programlisting>
  236. u32 (*scr_read) (struct ata_port *ap, unsigned int sc_reg);
  237. void (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
  238. u32 val);
  239. </programlisting>
  240. <para>
  241. Read and write standard SATA phy registers. Currently only used
  242. if ->phy_reset hook called the sata_phy_reset() helper function.
  243. </para>
  244. </sect2>
  245. <sect2><title>Init and shutdown</title>
  246. <programlisting>
  247. int (*port_start) (struct ata_port *ap);
  248. void (*port_stop) (struct ata_port *ap);
  249. void (*host_stop) (struct ata_host_set *host_set);
  250. </programlisting>
  251. <para>
  252. ->port_start() is called just after the data structures for each
  253. port are initialized. Typically this is used to alloc per-port
  254. DMA buffers / tables / rings, enable DMA engines, and similar
  255. tasks.
  256. </para>
  257. <para>
  258. ->port_stop() is called after ->host_stop(). It's sole function
  259. is to release DMA/memory resources, now that they are no longer
  260. actively being used.
  261. </para>
  262. <para>
  263. ->host_stop() is called after all ->port_stop() calls
  264. have completed. The hook must finalize hardware shutdown, release DMA
  265. and other resources, etc.
  266. </para>
  267. </sect2>
  268. </sect1>
  269. </chapter>
  270. <chapter id="libataExt">
  271. <title>libata Library</title>
  272. !Edrivers/scsi/libata-core.c
  273. </chapter>
  274. <chapter id="libataInt">
  275. <title>libata Core Internals</title>
  276. !Idrivers/scsi/libata-core.c
  277. </chapter>
  278. <chapter id="libataScsiInt">
  279. <title>libata SCSI translation/emulation</title>
  280. !Edrivers/scsi/libata-scsi.c
  281. !Idrivers/scsi/libata-scsi.c
  282. </chapter>
  283. <chapter id="PiixInt">
  284. <title>ata_piix Internals</title>
  285. !Idrivers/scsi/ata_piix.c
  286. </chapter>
  287. <chapter id="SILInt">
  288. <title>sata_sil Internals</title>
  289. !Idrivers/scsi/sata_sil.c
  290. </chapter>
  291. <chapter id="libataThanks">
  292. <title>Thanks</title>
  293. <para>
  294. The bulk of the ATA knowledge comes thanks to long conversations with
  295. Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA
  296. and SCSI specifications.
  297. </para>
  298. <para>
  299. Thanks to Alan Cox for pointing out similarities
  300. between SATA and SCSI, and in general for motivation to hack on
  301. libata.
  302. </para>
  303. <para>
  304. libata's device detection
  305. method, ata_pio_devchk, and in general all the early probing was
  306. based on extensive study of Hale Landis's probe/reset code in his
  307. ATADRVR driver (www.ata-atapi.com).
  308. </para>
  309. </chapter>
  310. </book>