libata.tmpl 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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. This function should do whatever needs to be done to take the
  77. port out of use. In most cases, ata_port_disable() can be used
  78. as this hook.
  79. </para>
  80. <para>
  81. Called from ata_bus_probe() on a failed probe.
  82. Called from ata_bus_reset() on a failed bus reset.
  83. Called from ata_scsi_release().
  84. </para>
  85. </sect2>
  86. <sect2><title>Post-IDENTIFY device configuration</title>
  87. <programlisting>
  88. void (*dev_config) (struct ata_port *, struct ata_device *);
  89. </programlisting>
  90. <para>
  91. Called after IDENTIFY [PACKET] DEVICE is issued to each device
  92. found. Typically used to apply device-specific fixups prior to
  93. issue of SET FEATURES - XFER MODE, and prior to operation.
  94. </para>
  95. <para>
  96. Called by ata_device_add() after ata_dev_identify() determines
  97. a device is present.
  98. </para>
  99. <para>
  100. This entry may be specified as NULL in ata_port_operations.
  101. </para>
  102. </sect2>
  103. <sect2><title>Set PIO/DMA mode</title>
  104. <programlisting>
  105. void (*set_piomode) (struct ata_port *, struct ata_device *);
  106. void (*set_dmamode) (struct ata_port *, struct ata_device *);
  107. void (*post_set_mode) (struct ata_port *ap);
  108. </programlisting>
  109. <para>
  110. Hooks called prior to the issue of SET FEATURES - XFER MODE
  111. command. dev->pio_mode is guaranteed to be valid when
  112. ->set_piomode() is called, and dev->dma_mode is guaranteed to be
  113. valid when ->set_dmamode() is called. ->post_set_mode() is
  114. called unconditionally, after the SET FEATURES - XFER MODE
  115. command completes successfully.
  116. </para>
  117. <para>
  118. ->set_piomode() is always called (if present), but
  119. ->set_dma_mode() is only called if DMA is possible.
  120. </para>
  121. </sect2>
  122. <sect2><title>Taskfile read/write</title>
  123. <programlisting>
  124. void (*tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
  125. void (*tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
  126. </programlisting>
  127. <para>
  128. ->tf_load() is called to load the given taskfile into hardware
  129. registers / DMA buffers. ->tf_read() is called to read the
  130. hardware registers / DMA buffers, to obtain the current set of
  131. taskfile register values.
  132. Most drivers for taskfile-based hardware (PIO or MMIO) use
  133. ata_tf_load() and ata_tf_read() for these hooks.
  134. </para>
  135. </sect2>
  136. <sect2><title>ATA command execute</title>
  137. <programlisting>
  138. void (*exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
  139. </programlisting>
  140. <para>
  141. causes an ATA command, previously loaded with
  142. ->tf_load(), to be initiated in hardware.
  143. Most drivers for taskfile-based hardware use ata_exec_command()
  144. for this hook.
  145. </para>
  146. </sect2>
  147. <sect2><title>Per-cmd ATAPI DMA capabilities filter</title>
  148. <programlisting>
  149. int (*check_atapi_dma) (struct ata_queued_cmd *qc);
  150. </programlisting>
  151. <para>
  152. Allow low-level driver to filter ATA PACKET commands, returning a status
  153. indicating whether or not it is OK to use DMA for the supplied PACKET
  154. command.
  155. </para>
  156. <para>
  157. This hook may be specified as NULL, in which case libata will
  158. assume that atapi dma can be supported.
  159. </para>
  160. </sect2>
  161. <sect2><title>Read specific ATA shadow registers</title>
  162. <programlisting>
  163. u8 (*check_status)(struct ata_port *ap);
  164. u8 (*check_altstatus)(struct ata_port *ap);
  165. u8 (*check_err)(struct ata_port *ap);
  166. </programlisting>
  167. <para>
  168. Reads the Status/AltStatus/Error ATA shadow register from
  169. hardware. On some hardware, reading the Status register has
  170. the side effect of clearing the interrupt condition.
  171. Most drivers for taskfile-based hardware use
  172. ata_check_status() for this hook.
  173. </para>
  174. <para>
  175. Note that because this is called from ata_device_add(), at
  176. least a dummy function that clears device interrupts must be
  177. provided for all drivers, even if the controller doesn't
  178. actually have a taskfile status register.
  179. </para>
  180. </sect2>
  181. <sect2><title>Select ATA device on bus</title>
  182. <programlisting>
  183. void (*dev_select)(struct ata_port *ap, unsigned int device);
  184. </programlisting>
  185. <para>
  186. Issues the low-level hardware command(s) that causes one of N
  187. hardware devices to be considered 'selected' (active and
  188. available for use) on the ATA bus. This generally has no
  189. meaning on FIS-based devices.
  190. </para>
  191. <para>
  192. Most drivers for taskfile-based hardware use
  193. ata_std_dev_select() for this hook. Controllers which do not
  194. support second drives on a port (such as SATA contollers) will
  195. use ata_noop_dev_select().
  196. </para>
  197. </sect2>
  198. <sect2><title>Reset ATA bus</title>
  199. <programlisting>
  200. void (*phy_reset) (struct ata_port *ap);
  201. </programlisting>
  202. <para>
  203. The very first step in the probe phase. Actions vary depending
  204. on the bus type, typically. After waking up the device and probing
  205. for device presence (PATA and SATA), typically a soft reset
  206. (SRST) will be performed. Drivers typically use the helper
  207. functions ata_bus_reset() or sata_phy_reset() for this hook.
  208. Many SATA drivers use sata_phy_reset() or call it from within
  209. their own phy_reset() functions.
  210. </para>
  211. </sect2>
  212. <sect2><title>Control PCI IDE BMDMA engine</title>
  213. <programlisting>
  214. void (*bmdma_setup) (struct ata_queued_cmd *qc);
  215. void (*bmdma_start) (struct ata_queued_cmd *qc);
  216. void (*bmdma_stop) (struct ata_port *ap);
  217. u8 (*bmdma_status) (struct ata_port *ap);
  218. </programlisting>
  219. <para>
  220. When setting up an IDE BMDMA transaction, these hooks arm
  221. (->bmdma_setup), fire (->bmdma_start), and halt (->bmdma_stop)
  222. the hardware's DMA engine. ->bmdma_status is used to read the standard
  223. PCI IDE DMA Status register.
  224. </para>
  225. <para>
  226. These hooks are typically either no-ops, or simply not implemented, in
  227. FIS-based drivers.
  228. </para>
  229. <para>
  230. Most legacy IDE drivers use ata_bmdma_setup() for the bmdma_setup()
  231. hook. ata_bmdma_setup() will write the pointer to the PRD table to
  232. the IDE PRD Table Address register, enable DMA in the DMA Command
  233. register, and call exec_command() to begin the transfer.
  234. </para>
  235. <para>
  236. Most legacy IDE drivers use ata_bmdma_start() for the bmdma_start()
  237. hook. ata_bmdma_start() will write the ATA_DMA_START flag to the DMA
  238. Command register.
  239. </para>
  240. <para>
  241. Many legacy IDE drivers use ata_bmdma_stop() for the bmdma_stop()
  242. hook. ata_bmdma_stop() clears the ATA_DMA_START flag in the DMA
  243. command register.
  244. </para>
  245. <para>
  246. Many legacy IDE drivers use ata_bmdma_status() as the bmdma_status() hook.
  247. </para>
  248. </sect2>
  249. <sect2><title>High-level taskfile hooks</title>
  250. <programlisting>
  251. void (*qc_prep) (struct ata_queued_cmd *qc);
  252. int (*qc_issue) (struct ata_queued_cmd *qc);
  253. </programlisting>
  254. <para>
  255. Higher-level hooks, these two hooks can potentially supercede
  256. several of the above taskfile/DMA engine hooks. ->qc_prep is
  257. called after the buffers have been DMA-mapped, and is typically
  258. used to populate the hardware's DMA scatter-gather table.
  259. Most drivers use the standard ata_qc_prep() helper function, but
  260. more advanced drivers roll their own.
  261. </para>
  262. <para>
  263. ->qc_issue is used to make a command active, once the hardware
  264. and S/G tables have been prepared. IDE BMDMA drivers use the
  265. helper function ata_qc_issue_prot() for taskfile protocol-based
  266. dispatch. More advanced drivers implement their own ->qc_issue.
  267. </para>
  268. <para>
  269. ata_qc_issue_prot() calls ->tf_load(), ->bmdma_setup(), and
  270. ->bmdma_start() as necessary to initiate a transfer.
  271. </para>
  272. </sect2>
  273. <sect2><title>Timeout (error) handling</title>
  274. <programlisting>
  275. void (*eng_timeout) (struct ata_port *ap);
  276. </programlisting>
  277. <para>
  278. This is a high level error handling function, called from the
  279. error handling thread, when a command times out. Most newer
  280. hardware will implement its own error handling code here. IDE BMDMA
  281. drivers may use the helper function ata_eng_timeout().
  282. </para>
  283. </sect2>
  284. <sect2><title>Hardware interrupt handling</title>
  285. <programlisting>
  286. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
  287. void (*irq_clear) (struct ata_port *);
  288. </programlisting>
  289. <para>
  290. ->irq_handler is the interrupt handling routine registered with
  291. the system, by libata. ->irq_clear is called during probe just
  292. before the interrupt handler is registered, to be sure hardware
  293. is quiet.
  294. </para>
  295. <para>
  296. The second argument, dev_instance, should be cast to a pointer
  297. to struct ata_host_set.
  298. </para>
  299. <para>
  300. Most legacy IDE drivers use ata_interrupt() for the
  301. irq_handler hook, which scans all ports in the host_set,
  302. determines which queued command was active (if any), and calls
  303. ata_host_intr(ap,qc).
  304. </para>
  305. <para>
  306. Most legacy IDE drivers use ata_bmdma_irq_clear() for the
  307. irq_clear() hook, which simply clears the interrupt and error
  308. flags in the DMA status register.
  309. </para>
  310. </sect2>
  311. <sect2><title>SATA phy read/write</title>
  312. <programlisting>
  313. u32 (*scr_read) (struct ata_port *ap, unsigned int sc_reg);
  314. void (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
  315. u32 val);
  316. </programlisting>
  317. <para>
  318. Read and write standard SATA phy registers. Currently only used
  319. if ->phy_reset hook called the sata_phy_reset() helper function.
  320. sc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
  321. </para>
  322. </sect2>
  323. <sect2><title>Init and shutdown</title>
  324. <programlisting>
  325. int (*port_start) (struct ata_port *ap);
  326. void (*port_stop) (struct ata_port *ap);
  327. void (*host_stop) (struct ata_host_set *host_set);
  328. </programlisting>
  329. <para>
  330. ->port_start() is called just after the data structures for each
  331. port are initialized. Typically this is used to alloc per-port
  332. DMA buffers / tables / rings, enable DMA engines, and similar
  333. tasks. Some drivers also use this entry point as a chance to
  334. allocate driver-private memory for ap->private_data.
  335. </para>
  336. <para>
  337. Many drivers use ata_port_start() as this hook or call
  338. it from their own port_start() hooks. ata_port_start()
  339. allocates space for a legacy IDE PRD table and returns.
  340. </para>
  341. <para>
  342. ->port_stop() is called after ->host_stop(). It's sole function
  343. is to release DMA/memory resources, now that they are no longer
  344. actively being used. Many drivers also free driver-private
  345. data from port at this time.
  346. </para>
  347. <para>
  348. Many drivers use ata_port_stop() as this hook, which frees the
  349. PRD table.
  350. </para>
  351. <para>
  352. ->host_stop() is called after all ->port_stop() calls
  353. have completed. The hook must finalize hardware shutdown, release DMA
  354. and other resources, etc.
  355. This hook may be specified as NULL, in which case it is not called.
  356. </para>
  357. </sect2>
  358. </sect1>
  359. </chapter>
  360. <chapter id="libataEH">
  361. <title>Error handling</title>
  362. <para>
  363. This chapter describes how errors are handled under libata.
  364. Readers are advised to read SCSI EH
  365. (Documentation/scsi/scsi_eh.txt) and ATA exceptions doc first.
  366. </para>
  367. <sect1><title>Origins of commands</title>
  368. <para>
  369. In libata, a command is represented with struct ata_queued_cmd
  370. or qc. qc's are preallocated during port initialization and
  371. repetitively used for command executions. Currently only one
  372. qc is allocated per port but yet-to-be-merged NCQ branch
  373. allocates one for each tag and maps each qc to NCQ tag 1-to-1.
  374. </para>
  375. <para>
  376. libata commands can originate from two sources - libata itself
  377. and SCSI midlayer. libata internal commands are used for
  378. initialization and error handling. All normal blk requests
  379. and commands for SCSI emulation are passed as SCSI commands
  380. through queuecommand callback of SCSI host template.
  381. </para>
  382. </sect1>
  383. <sect1><title>How commands are issued</title>
  384. <variablelist>
  385. <varlistentry><term>Internal commands</term>
  386. <listitem>
  387. <para>
  388. First, qc is allocated and initialized using
  389. ata_qc_new_init(). Although ata_qc_new_init() doesn't
  390. implement any wait or retry mechanism when qc is not
  391. available, internal commands are currently issued only during
  392. initialization and error recovery, so no other command is
  393. active and allocation is guaranteed to succeed.
  394. </para>
  395. <para>
  396. Once allocated qc's taskfile is initialized for the command to
  397. be executed. qc currently has two mechanisms to notify
  398. completion. One is via qc->complete_fn() callback and the
  399. other is completion qc->waiting. qc->complete_fn() callback
  400. is the asynchronous path used by normal SCSI translated
  401. commands and qc->waiting is the synchronous (issuer sleeps in
  402. process context) path used by internal commands.
  403. </para>
  404. <para>
  405. Once initialization is complete, host_set lock is acquired
  406. and the qc is issued.
  407. </para>
  408. </listitem>
  409. </varlistentry>
  410. <varlistentry><term>SCSI commands</term>
  411. <listitem>
  412. <para>
  413. All libata drivers use ata_scsi_queuecmd() as
  414. hostt->queuecommand callback. scmds can either be simulated
  415. or translated. No qc is involved in processing a simulated
  416. scmd. The result is computed right away and the scmd is
  417. completed.
  418. </para>
  419. <para>
  420. For a translated scmd, ata_qc_new_init() is invoked to
  421. allocate a qc and the scmd is translated into the qc. SCSI
  422. midlayer's completion notification function pointer is stored
  423. into qc->scsidone.
  424. </para>
  425. <para>
  426. qc->complete_fn() callback is used for completion
  427. notification. ATA commands use ata_scsi_qc_complete() while
  428. ATAPI commands use atapi_qc_complete(). Both functions end up
  429. calling qc->scsidone to notify upper layer when the qc is
  430. finished. After translation is completed, the qc is issued
  431. with ata_qc_issue().
  432. </para>
  433. <para>
  434. Note that SCSI midlayer invokes hostt->queuecommand while
  435. holding host_set lock, so all above occur while holding
  436. host_set lock.
  437. </para>
  438. </listitem>
  439. </varlistentry>
  440. </variablelist>
  441. </sect1>
  442. <sect1><title>How commands are processed</title>
  443. <para>
  444. Depending on which protocol and which controller are used,
  445. commands are processed differently. For the purpose of
  446. discussion, a controller which uses taskfile interface and all
  447. standard callbacks is assumed.
  448. </para>
  449. <para>
  450. Currently 6 ATA command protocols are used. They can be
  451. sorted into the following four categories according to how
  452. they are processed.
  453. </para>
  454. <variablelist>
  455. <varlistentry><term>ATA NO DATA or DMA</term>
  456. <listitem>
  457. <para>
  458. ATA_PROT_NODATA and ATA_PROT_DMA fall into this category.
  459. These types of commands don't require any software
  460. intervention once issued. Device will raise interrupt on
  461. completion.
  462. </para>
  463. </listitem>
  464. </varlistentry>
  465. <varlistentry><term>ATA PIO</term>
  466. <listitem>
  467. <para>
  468. ATA_PROT_PIO is in this category. libata currently
  469. implements PIO with polling. ATA_NIEN bit is set to turn
  470. off interrupt and pio_task on ata_wq performs polling and
  471. IO.
  472. </para>
  473. </listitem>
  474. </varlistentry>
  475. <varlistentry><term>ATAPI NODATA or DMA</term>
  476. <listitem>
  477. <para>
  478. ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
  479. category. packet_task is used to poll BSY bit after
  480. issuing PACKET command. Once BSY is turned off by the
  481. device, packet_task transfers CDB and hands off processing
  482. to interrupt handler.
  483. </para>
  484. </listitem>
  485. </varlistentry>
  486. <varlistentry><term>ATAPI PIO</term>
  487. <listitem>
  488. <para>
  489. ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set
  490. and, as in ATAPI NODATA or DMA, packet_task submits cdb.
  491. However, after submitting cdb, further processing (data
  492. transfer) is handed off to pio_task.
  493. </para>
  494. </listitem>
  495. </varlistentry>
  496. </variablelist>
  497. </sect1>
  498. <sect1><title>How commands are completed</title>
  499. <para>
  500. Once issued, all qc's are either completed with
  501. ata_qc_complete() or time out. For commands which are handled
  502. by interrupts, ata_host_intr() invokes ata_qc_complete(), and,
  503. for PIO tasks, pio_task invokes ata_qc_complete(). In error
  504. cases, packet_task may also complete commands.
  505. </para>
  506. <para>
  507. ata_qc_complete() does the following.
  508. </para>
  509. <orderedlist>
  510. <listitem>
  511. <para>
  512. DMA memory is unmapped.
  513. </para>
  514. </listitem>
  515. <listitem>
  516. <para>
  517. ATA_QCFLAG_ACTIVE is clared from qc->flags.
  518. </para>
  519. </listitem>
  520. <listitem>
  521. <para>
  522. qc->complete_fn() callback is invoked. If the return value of
  523. the callback is not zero. Completion is short circuited and
  524. ata_qc_complete() returns.
  525. </para>
  526. </listitem>
  527. <listitem>
  528. <para>
  529. __ata_qc_complete() is called, which does
  530. <orderedlist>
  531. <listitem>
  532. <para>
  533. qc->flags is cleared to zero.
  534. </para>
  535. </listitem>
  536. <listitem>
  537. <para>
  538. ap->active_tag and qc->tag are poisoned.
  539. </para>
  540. </listitem>
  541. <listitem>
  542. <para>
  543. qc->waiting is claread &amp; completed (in that order).
  544. </para>
  545. </listitem>
  546. <listitem>
  547. <para>
  548. qc is deallocated by clearing appropriate bit in ap->qactive.
  549. </para>
  550. </listitem>
  551. </orderedlist>
  552. </para>
  553. </listitem>
  554. </orderedlist>
  555. <para>
  556. So, it basically notifies upper layer and deallocates qc. One
  557. exception is short-circuit path in #3 which is used by
  558. atapi_qc_complete().
  559. </para>
  560. <para>
  561. For all non-ATAPI commands, whether it fails or not, almost
  562. the same code path is taken and very little error handling
  563. takes place. A qc is completed with success status if it
  564. succeeded, with failed status otherwise.
  565. </para>
  566. <para>
  567. However, failed ATAPI commands require more handling as
  568. REQUEST SENSE is needed to acquire sense data. If an ATAPI
  569. command fails, ata_qc_complete() is invoked with error status,
  570. which in turn invokes atapi_qc_complete() via
  571. qc->complete_fn() callback.
  572. </para>
  573. <para>
  574. This makes atapi_qc_complete() set scmd->result to
  575. SAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As
  576. the sense data is empty but scmd->result is CHECK CONDITION,
  577. SCSI midlayer will invoke EH for the scmd, and returning 1
  578. makes ata_qc_complete() to return without deallocating the qc.
  579. This leads us to ata_scsi_error() with partially completed qc.
  580. </para>
  581. </sect1>
  582. <sect1><title>ata_scsi_error()</title>
  583. <para>
  584. ata_scsi_error() is the current hostt->eh_strategy_handler()
  585. for libata. As discussed above, this will be entered in two
  586. cases - timeout and ATAPI error completion. This function
  587. calls low level libata driver's eng_timeout() callback, the
  588. standard callback for which is ata_eng_timeout(). It checks
  589. if a qc is active and calls ata_qc_timeout() on the qc if so.
  590. Actual error handling occurs in ata_qc_timeout().
  591. </para>
  592. <para>
  593. If EH is invoked for timeout, ata_qc_timeout() stops BMDMA and
  594. completes the qc. Note that as we're currently in EH, we
  595. cannot call scsi_done. As described in SCSI EH doc, a
  596. recovered scmd should be either retried with
  597. scsi_queue_insert() or finished with scsi_finish_command().
  598. Here, we override qc->scsidone with scsi_finish_command() and
  599. calls ata_qc_complete().
  600. </para>
  601. <para>
  602. If EH is invoked due to a failed ATAPI qc, the qc here is
  603. completed but not deallocated. The purpose of this
  604. half-completion is to use the qc as place holder to make EH
  605. code reach this place. This is a bit hackish, but it works.
  606. </para>
  607. <para>
  608. Once control reaches here, the qc is deallocated by invoking
  609. __ata_qc_complete() explicitly. Then, internal qc for REQUEST
  610. SENSE is issued. Once sense data is acquired, scmd is
  611. finished by directly invoking scsi_finish_command() on the
  612. scmd. Note that as we already have completed and deallocated
  613. the qc which was associated with the scmd, we don't need
  614. to/cannot call ata_qc_complete() again.
  615. </para>
  616. </sect1>
  617. <sect1><title>Problems with the current EH</title>
  618. <itemizedlist>
  619. <listitem>
  620. <para>
  621. Error representation is too crude. Currently any and all
  622. error conditions are represented with ATA STATUS and ERROR
  623. registers. Errors which aren't ATA device errors are treated
  624. as ATA device errors by setting ATA_ERR bit. Better error
  625. descriptor which can properly represent ATA and other
  626. errors/exceptions is needed.
  627. </para>
  628. </listitem>
  629. <listitem>
  630. <para>
  631. When handling timeouts, no action is taken to make device
  632. forget about the timed out command and ready for new commands.
  633. </para>
  634. </listitem>
  635. <listitem>
  636. <para>
  637. EH handling via ata_scsi_error() is not properly protected
  638. from usual command processing. On EH entrance, the device is
  639. not in quiescent state. Timed out commands may succeed or
  640. fail any time. pio_task and atapi_task may still be running.
  641. </para>
  642. </listitem>
  643. <listitem>
  644. <para>
  645. Too weak error recovery. Devices / controllers causing HSM
  646. mismatch errors and other errors quite often require reset to
  647. return to known state. Also, advanced error handling is
  648. necessary to support features like NCQ and hotplug.
  649. </para>
  650. </listitem>
  651. <listitem>
  652. <para>
  653. ATA errors are directly handled in the interrupt handler and
  654. PIO errors in pio_task. This is problematic for advanced
  655. error handling for the following reasons.
  656. </para>
  657. <para>
  658. First, advanced error handling often requires context and
  659. internal qc execution.
  660. </para>
  661. <para>
  662. Second, even a simple failure (say, CRC error) needs
  663. information gathering and could trigger complex error handling
  664. (say, resetting &amp; reconfiguring). Having multiple code
  665. paths to gather information, enter EH and trigger actions
  666. makes life painful.
  667. </para>
  668. <para>
  669. Third, scattered EH code makes implementing low level drivers
  670. difficult. Low level drivers override libata callbacks. If
  671. EH is scattered over several places, each affected callbacks
  672. should perform its part of error handling. This can be error
  673. prone and painful.
  674. </para>
  675. </listitem>
  676. </itemizedlist>
  677. </sect1>
  678. </chapter>
  679. <chapter id="libataExt">
  680. <title>libata Library</title>
  681. !Edrivers/scsi/libata-core.c
  682. </chapter>
  683. <chapter id="libataInt">
  684. <title>libata Core Internals</title>
  685. !Idrivers/scsi/libata-core.c
  686. </chapter>
  687. <chapter id="libataScsiInt">
  688. <title>libata SCSI translation/emulation</title>
  689. !Edrivers/scsi/libata-scsi.c
  690. !Idrivers/scsi/libata-scsi.c
  691. </chapter>
  692. <chapter id="ataExceptions">
  693. <title>ATA errors &amp; exceptions</title>
  694. <para>
  695. This chapter tries to identify what error/exception conditions exist
  696. for ATA/ATAPI devices and describe how they should be handled in
  697. implementation-neutral way.
  698. </para>
  699. <para>
  700. The term 'error' is used to describe conditions where either an
  701. explicit error condition is reported from device or a command has
  702. timed out.
  703. </para>
  704. <para>
  705. The term 'exception' is either used to describe exceptional
  706. conditions which are not errors (say, power or hotplug events), or
  707. to describe both errors and non-error exceptional conditions. Where
  708. explicit distinction between error and exception is necessary, the
  709. term 'non-error exception' is used.
  710. </para>
  711. <sect1 id="excat">
  712. <title>Exception categories</title>
  713. <para>
  714. Exceptions are described primarily with respect to legacy
  715. taskfile + bus master IDE interface. If a controller provides
  716. other better mechanism for error reporting, mapping those into
  717. categories described below shouldn't be difficult.
  718. </para>
  719. <para>
  720. In the following sections, two recovery actions - reset and
  721. reconfiguring transport - are mentioned. These are described
  722. further in <xref linkend="exrec"/>.
  723. </para>
  724. <sect2 id="excatHSMviolation">
  725. <title>HSM violation</title>
  726. <para>
  727. This error is indicated when STATUS value doesn't match HSM
  728. requirement during issuing or excution any ATA/ATAPI command.
  729. </para>
  730. <itemizedlist>
  731. <title>Examples</title>
  732. <listitem>
  733. <para>
  734. ATA_STATUS doesn't contain !BSY &amp;&amp; DRDY &amp;&amp; !DRQ while trying
  735. to issue a command.
  736. </para>
  737. </listitem>
  738. <listitem>
  739. <para>
  740. !BSY &amp;&amp; !DRQ during PIO data transfer.
  741. </para>
  742. </listitem>
  743. <listitem>
  744. <para>
  745. DRQ on command completion.
  746. </para>
  747. </listitem>
  748. <listitem>
  749. <para>
  750. !BSY &amp;&amp; ERR after CDB tranfer starts but before the
  751. last byte of CDB is transferred. ATA/ATAPI standard states
  752. that &quot;The device shall not terminate the PACKET command
  753. with an error before the last byte of the command packet has
  754. been written&quot; in the error outputs description of PACKET
  755. command and the state diagram doesn't include such
  756. transitions.
  757. </para>
  758. </listitem>
  759. </itemizedlist>
  760. <para>
  761. In these cases, HSM is violated and not much information
  762. regarding the error can be acquired from STATUS or ERROR
  763. register. IOW, this error can be anything - driver bug,
  764. faulty device, controller and/or cable.
  765. </para>
  766. <para>
  767. As HSM is violated, reset is necessary to restore known state.
  768. Reconfiguring transport for lower speed might be helpful too
  769. as transmission errors sometimes cause this kind of errors.
  770. </para>
  771. </sect2>
  772. <sect2 id="excatDevErr">
  773. <title>ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION)</title>
  774. <para>
  775. These are errors detected and reported by ATA/ATAPI devices
  776. indicating device problems. For this type of errors, STATUS
  777. and ERROR register values are valid and describe error
  778. condition. Note that some of ATA bus errors are detected by
  779. ATA/ATAPI devices and reported using the same mechanism as
  780. device errors. Those cases are described later in this
  781. section.
  782. </para>
  783. <para>
  784. For ATA commands, this type of errors are indicated by !BSY
  785. &amp;&amp; ERR during command execution and on completion.
  786. </para>
  787. <para>For ATAPI commands,</para>
  788. <itemizedlist>
  789. <listitem>
  790. <para>
  791. !BSY &amp;&amp; ERR &amp;&amp; ABRT right after issuing PACKET
  792. indicates that PACKET command is not supported and falls in
  793. this category.
  794. </para>
  795. </listitem>
  796. <listitem>
  797. <para>
  798. !BSY &amp;&amp; ERR(==CHK) &amp;&amp; !ABRT after the last
  799. byte of CDB is transferred indicates CHECK CONDITION and
  800. doesn't fall in this category.
  801. </para>
  802. </listitem>
  803. <listitem>
  804. <para>
  805. !BSY &amp;&amp; ERR(==CHK) &amp;&amp; ABRT after the last byte
  806. of CDB is transferred *probably* indicates CHECK CONDITION and
  807. doesn't fall in this category.
  808. </para>
  809. </listitem>
  810. </itemizedlist>
  811. <para>
  812. Of errors detected as above, the followings are not ATA/ATAPI
  813. device errors but ATA bus errors and should be handled
  814. according to <xref linkend="excatATAbusErr"/>.
  815. </para>
  816. <variablelist>
  817. <varlistentry>
  818. <term>CRC error during data transfer</term>
  819. <listitem>
  820. <para>
  821. This is indicated by ICRC bit in the ERROR register and
  822. means that corruption occurred during data transfer. Upto
  823. ATA/ATAPI-7, the standard specifies that this bit is only
  824. applicable to UDMA transfers but ATA/ATAPI-8 draft revision
  825. 1f says that the bit may be applicable to multiword DMA and
  826. PIO.
  827. </para>
  828. </listitem>
  829. </varlistentry>
  830. <varlistentry>
  831. <term>ABRT error during data transfer or on completion</term>
  832. <listitem>
  833. <para>
  834. Upto ATA/ATAPI-7, the standard specifies that ABRT could be
  835. set on ICRC errors and on cases where a device is not able
  836. to complete a command. Combined with the fact that MWDMA
  837. and PIO transfer errors aren't allowed to use ICRC bit upto
  838. ATA/ATAPI-7, it seems to imply that ABRT bit alone could
  839. indicate tranfer errors.
  840. </para>
  841. <para>
  842. However, ATA/ATAPI-8 draft revision 1f removes the part
  843. that ICRC errors can turn on ABRT. So, this is kind of
  844. gray area. Some heuristics are needed here.
  845. </para>
  846. </listitem>
  847. </varlistentry>
  848. </variablelist>
  849. <para>
  850. ATA/ATAPI device errors can be further categorized as follows.
  851. </para>
  852. <variablelist>
  853. <varlistentry>
  854. <term>Media errors</term>
  855. <listitem>
  856. <para>
  857. This is indicated by UNC bit in the ERROR register. ATA
  858. devices reports UNC error only after certain number of
  859. retries cannot recover the data, so there's nothing much
  860. else to do other than notifying upper layer.
  861. </para>
  862. <para>
  863. READ and WRITE commands report CHS or LBA of the first
  864. failed sector but ATA/ATAPI standard specifies that the
  865. amount of transferred data on error completion is
  866. indeterminate, so we cannot assume that sectors preceding
  867. the failed sector have been transferred and thus cannot
  868. complete those sectors successfully as SCSI does.
  869. </para>
  870. </listitem>
  871. </varlistentry>
  872. <varlistentry>
  873. <term>Media changed / media change requested error</term>
  874. <listitem>
  875. <para>
  876. &lt;&lt;TODO: fill here&gt;&gt;
  877. </para>
  878. </listitem>
  879. </varlistentry>
  880. <varlistentry><term>Address error</term>
  881. <listitem>
  882. <para>
  883. This is indicated by IDNF bit in the ERROR register.
  884. Report to upper layer.
  885. </para>
  886. </listitem>
  887. </varlistentry>
  888. <varlistentry><term>Other errors</term>
  889. <listitem>
  890. <para>
  891. This can be invalid command or parameter indicated by ABRT
  892. ERROR bit or some other error condition. Note that ABRT
  893. bit can indicate a lot of things including ICRC and Address
  894. errors. Heuristics needed.
  895. </para>
  896. </listitem>
  897. </varlistentry>
  898. </variablelist>
  899. <para>
  900. Depending on commands, not all STATUS/ERROR bits are
  901. applicable. These non-applicable bits are marked with
  902. &quot;na&quot; in the output descriptions but upto ATA/ATAPI-7
  903. no definition of &quot;na&quot; can be found. However,
  904. ATA/ATAPI-8 draft revision 1f describes &quot;N/A&quot; as
  905. follows.
  906. </para>
  907. <blockquote>
  908. <variablelist>
  909. <varlistentry><term>3.2.3.3a N/A</term>
  910. <listitem>
  911. <para>
  912. A keyword the indicates a field has no defined value in
  913. this standard and should not be checked by the host or
  914. device. N/A fields should be cleared to zero.
  915. </para>
  916. </listitem>
  917. </varlistentry>
  918. </variablelist>
  919. </blockquote>
  920. <para>
  921. So, it seems reasonable to assume that &quot;na&quot; bits are
  922. cleared to zero by devices and thus need no explicit masking.
  923. </para>
  924. </sect2>
  925. <sect2 id="excatATAPIcc">
  926. <title>ATAPI device CHECK CONDITION</title>
  927. <para>
  928. ATAPI device CHECK CONDITION error is indicated by set CHK bit
  929. (ERR bit) in the STATUS register after the last byte of CDB is
  930. transferred for a PACKET command. For this kind of errors,
  931. sense data should be acquired to gather information regarding
  932. the errors. REQUEST SENSE packet command should be used to
  933. acquire sense data.
  934. </para>
  935. <para>
  936. Once sense data is acquired, this type of errors can be
  937. handled similary to other SCSI errors. Note that sense data
  938. may indicate ATA bus error (e.g. Sense Key 04h HARDWARE ERROR
  939. &amp;&amp; ASC/ASCQ 47h/00h SCSI PARITY ERROR). In such
  940. cases, the error should be considered as an ATA bus error and
  941. handled according to <xref linkend="excatATAbusErr"/>.
  942. </para>
  943. </sect2>
  944. <sect2 id="excatNCQerr">
  945. <title>ATA device error (NCQ)</title>
  946. <para>
  947. NCQ command error is indicated by cleared BSY and set ERR bit
  948. during NCQ command phase (one or more NCQ commands
  949. outstanding). Although STATUS and ERROR registers will
  950. contain valid values describing the error, READ LOG EXT is
  951. required to clear the error condition, determine which command
  952. has failed and acquire more information.
  953. </para>
  954. <para>
  955. READ LOG EXT Log Page 10h reports which tag has failed and
  956. taskfile register values describing the error. With this
  957. information the failed command can be handled as a normal ATA
  958. command error as in <xref linkend="excatDevErr"/> and all
  959. other in-flight commands must be retried. Note that this
  960. retry should not be counted - it's likely that commands
  961. retried this way would have completed normally if it were not
  962. for the failed command.
  963. </para>
  964. <para>
  965. Note that ATA bus errors can be reported as ATA device NCQ
  966. errors. This should be handled as described in <xref
  967. linkend="excatATAbusErr"/>.
  968. </para>
  969. <para>
  970. If READ LOG EXT Log Page 10h fails or reports NQ, we're
  971. thoroughly screwed. This condition should be treated
  972. according to <xref linkend="excatHSMviolation"/>.
  973. </para>
  974. </sect2>
  975. <sect2 id="excatATAbusErr">
  976. <title>ATA bus error</title>
  977. <para>
  978. ATA bus error means that data corruption occurred during
  979. transmission over ATA bus (SATA or PATA). This type of errors
  980. can be indicated by
  981. </para>
  982. <itemizedlist>
  983. <listitem>
  984. <para>
  985. ICRC or ABRT error as described in <xref linkend="excatDevErr"/>.
  986. </para>
  987. </listitem>
  988. <listitem>
  989. <para>
  990. Controller-specific error completion with error information
  991. indicating transmission error.
  992. </para>
  993. </listitem>
  994. <listitem>
  995. <para>
  996. On some controllers, command timeout. In this case, there may
  997. be a mechanism to determine that the timeout is due to
  998. transmission error.
  999. </para>
  1000. </listitem>
  1001. <listitem>
  1002. <para>
  1003. Unknown/random errors, timeouts and all sorts of weirdities.
  1004. </para>
  1005. </listitem>
  1006. </itemizedlist>
  1007. <para>
  1008. As described above, transmission errors can cause wide variety
  1009. of symptoms ranging from device ICRC error to random device
  1010. lockup, and, for many cases, there is no way to tell if an
  1011. error condition is due to transmission error or not;
  1012. therefore, it's necessary to employ some kind of heuristic
  1013. when dealing with errors and timeouts. For example,
  1014. encountering repetitive ABRT errors for known supported
  1015. command is likely to indicate ATA bus error.
  1016. </para>
  1017. <para>
  1018. Once it's determined that ATA bus errors have possibly
  1019. occurred, lowering ATA bus transmission speed is one of
  1020. actions which may alleviate the problem. See <xref
  1021. linkend="exrecReconf"/> for more information.
  1022. </para>
  1023. </sect2>
  1024. <sect2 id="excatPCIbusErr">
  1025. <title>PCI bus error</title>
  1026. <para>
  1027. Data corruption or other failures during transmission over PCI
  1028. (or other system bus). For standard BMDMA, this is indicated
  1029. by Error bit in the BMDMA Status register. This type of
  1030. errors must be logged as it indicates something is very wrong
  1031. with the system. Resetting host controller is recommended.
  1032. </para>
  1033. </sect2>
  1034. <sect2 id="excatLateCompletion">
  1035. <title>Late completion</title>
  1036. <para>
  1037. This occurs when timeout occurs and the timeout handler finds
  1038. out that the timed out command has completed successfully or
  1039. with error. This is usually caused by lost interrupts. This
  1040. type of errors must be logged. Resetting host controller is
  1041. recommended.
  1042. </para>
  1043. </sect2>
  1044. <sect2 id="excatUnknown">
  1045. <title>Unknown error (timeout)</title>
  1046. <para>
  1047. This is when timeout occurs and the command is still
  1048. processing or the host and device are in unknown state. When
  1049. this occurs, HSM could be in any valid or invalid state. To
  1050. bring the device to known state and make it forget about the
  1051. timed out command, resetting is necessary. The timed out
  1052. command may be retried.
  1053. </para>
  1054. <para>
  1055. Timeouts can also be caused by transmission errors. Refer to
  1056. <xref linkend="excatATAbusErr"/> for more details.
  1057. </para>
  1058. </sect2>
  1059. <sect2 id="excatHoplugPM">
  1060. <title>Hotplug and power management exceptions</title>
  1061. <para>
  1062. &lt;&lt;TODO: fill here&gt;&gt;
  1063. </para>
  1064. </sect2>
  1065. </sect1>
  1066. <sect1 id="exrec">
  1067. <title>EH recovery actions</title>
  1068. <para>
  1069. This section discusses several important recovery actions.
  1070. </para>
  1071. <sect2 id="exrecClr">
  1072. <title>Clearing error condition</title>
  1073. <para>
  1074. Many controllers require its error registers to be cleared by
  1075. error handler. Different controllers may have different
  1076. requirements.
  1077. </para>
  1078. <para>
  1079. For SATA, it's strongly recommended to clear at least SError
  1080. register during error handling.
  1081. </para>
  1082. </sect2>
  1083. <sect2 id="exrecRst">
  1084. <title>Reset</title>
  1085. <para>
  1086. During EH, resetting is necessary in the following cases.
  1087. </para>
  1088. <itemizedlist>
  1089. <listitem>
  1090. <para>
  1091. HSM is in unknown or invalid state
  1092. </para>
  1093. </listitem>
  1094. <listitem>
  1095. <para>
  1096. HBA is in unknown or invalid state
  1097. </para>
  1098. </listitem>
  1099. <listitem>
  1100. <para>
  1101. EH needs to make HBA/device forget about in-flight commands
  1102. </para>
  1103. </listitem>
  1104. <listitem>
  1105. <para>
  1106. HBA/device behaves weirdly
  1107. </para>
  1108. </listitem>
  1109. </itemizedlist>
  1110. <para>
  1111. Resetting during EH might be a good idea regardless of error
  1112. condition to improve EH robustness. Whether to reset both or
  1113. either one of HBA and device depends on situation but the
  1114. following scheme is recommended.
  1115. </para>
  1116. <itemizedlist>
  1117. <listitem>
  1118. <para>
  1119. When it's known that HBA is in ready state but ATA/ATAPI
  1120. device in in unknown state, reset only device.
  1121. </para>
  1122. </listitem>
  1123. <listitem>
  1124. <para>
  1125. If HBA is in unknown state, reset both HBA and device.
  1126. </para>
  1127. </listitem>
  1128. </itemizedlist>
  1129. <para>
  1130. HBA resetting is implementation specific. For a controller
  1131. complying to taskfile/BMDMA PCI IDE, stopping active DMA
  1132. transaction may be sufficient iff BMDMA state is the only HBA
  1133. context. But even mostly taskfile/BMDMA PCI IDE complying
  1134. controllers may have implementation specific requirements and
  1135. mechanism to reset themselves. This must be addressed by
  1136. specific drivers.
  1137. </para>
  1138. <para>
  1139. OTOH, ATA/ATAPI standard describes in detail ways to reset
  1140. ATA/ATAPI devices.
  1141. </para>
  1142. <variablelist>
  1143. <varlistentry><term>PATA hardware reset</term>
  1144. <listitem>
  1145. <para>
  1146. This is hardware initiated device reset signalled with
  1147. asserted PATA RESET- signal. There is no standard way to
  1148. initiate hardware reset from software although some
  1149. hardware provides registers that allow driver to directly
  1150. tweak the RESET- signal.
  1151. </para>
  1152. </listitem>
  1153. </varlistentry>
  1154. <varlistentry><term>Software reset</term>
  1155. <listitem>
  1156. <para>
  1157. This is achieved by turning CONTROL SRST bit on for at
  1158. least 5us. Both PATA and SATA support it but, in case of
  1159. SATA, this may require controller-specific support as the
  1160. second Register FIS to clear SRST should be transmitted
  1161. while BSY bit is still set. Note that on PATA, this resets
  1162. both master and slave devices on a channel.
  1163. </para>
  1164. </listitem>
  1165. </varlistentry>
  1166. <varlistentry><term>EXECUTE DEVICE DIAGNOSTIC command</term>
  1167. <listitem>
  1168. <para>
  1169. Although ATA/ATAPI standard doesn't describe exactly, EDD
  1170. implies some level of resetting, possibly similar level
  1171. with software reset. Host-side EDD protocol can be handled
  1172. with normal command processing and most SATA controllers
  1173. should be able to handle EDD's just like other commands.
  1174. As in software reset, EDD affects both devices on a PATA
  1175. bus.
  1176. </para>
  1177. <para>
  1178. Although EDD does reset devices, this doesn't suit error
  1179. handling as EDD cannot be issued while BSY is set and it's
  1180. unclear how it will act when device is in unknown/weird
  1181. state.
  1182. </para>
  1183. </listitem>
  1184. </varlistentry>
  1185. <varlistentry><term>ATAPI DEVICE RESET command</term>
  1186. <listitem>
  1187. <para>
  1188. This is very similar to software reset except that reset
  1189. can be restricted to the selected device without affecting
  1190. the other device sharing the cable.
  1191. </para>
  1192. </listitem>
  1193. </varlistentry>
  1194. <varlistentry><term>SATA phy reset</term>
  1195. <listitem>
  1196. <para>
  1197. This is the preferred way of resetting a SATA device. In
  1198. effect, it's identical to PATA hardware reset. Note that
  1199. this can be done with the standard SCR Control register.
  1200. As such, it's usually easier to implement than software
  1201. reset.
  1202. </para>
  1203. </listitem>
  1204. </varlistentry>
  1205. </variablelist>
  1206. <para>
  1207. One more thing to consider when resetting devices is that
  1208. resetting clears certain configuration parameters and they
  1209. need to be set to their previous or newly adjusted values
  1210. after reset.
  1211. </para>
  1212. <para>
  1213. Parameters affected are.
  1214. </para>
  1215. <itemizedlist>
  1216. <listitem>
  1217. <para>
  1218. CHS set up with INITIALIZE DEVICE PARAMETERS (seldomly used)
  1219. </para>
  1220. </listitem>
  1221. <listitem>
  1222. <para>
  1223. Parameters set with SET FEATURES including transfer mode setting
  1224. </para>
  1225. </listitem>
  1226. <listitem>
  1227. <para>
  1228. Block count set with SET MULTIPLE MODE
  1229. </para>
  1230. </listitem>
  1231. <listitem>
  1232. <para>
  1233. Other parameters (SET MAX, MEDIA LOCK...)
  1234. </para>
  1235. </listitem>
  1236. </itemizedlist>
  1237. <para>
  1238. ATA/ATAPI standard specifies that some parameters must be
  1239. maintained across hardware or software reset, but doesn't
  1240. strictly specify all of them. Always reconfiguring needed
  1241. parameters after reset is required for robustness. Note that
  1242. this also applies when resuming from deep sleep (power-off).
  1243. </para>
  1244. <para>
  1245. Also, ATA/ATAPI standard requires that IDENTIFY DEVICE /
  1246. IDENTIFY PACKET DEVICE is issued after any configuration
  1247. parameter is updated or a hardware reset and the result used
  1248. for further operation. OS driver is required to implement
  1249. revalidation mechanism to support this.
  1250. </para>
  1251. </sect2>
  1252. <sect2 id="exrecReconf">
  1253. <title>Reconfigure transport</title>
  1254. <para>
  1255. For both PATA and SATA, a lot of corners are cut for cheap
  1256. connectors, cables or controllers and it's quite common to see
  1257. high transmission error rate. This can be mitigated by
  1258. lowering transmission speed.
  1259. </para>
  1260. <para>
  1261. The following is a possible scheme Jeff Garzik suggested.
  1262. </para>
  1263. <blockquote>
  1264. <para>
  1265. If more than $N (3?) transmission errors happen in 15 minutes,
  1266. </para>
  1267. <itemizedlist>
  1268. <listitem>
  1269. <para>
  1270. if SATA, decrease SATA PHY speed. if speed cannot be decreased,
  1271. </para>
  1272. </listitem>
  1273. <listitem>
  1274. <para>
  1275. decrease UDMA xfer speed. if at UDMA0, switch to PIO4,
  1276. </para>
  1277. </listitem>
  1278. <listitem>
  1279. <para>
  1280. decrease PIO xfer speed. if at PIO3, complain, but continue
  1281. </para>
  1282. </listitem>
  1283. </itemizedlist>
  1284. </blockquote>
  1285. </sect2>
  1286. </sect1>
  1287. </chapter>
  1288. <chapter id="PiixInt">
  1289. <title>ata_piix Internals</title>
  1290. !Idrivers/scsi/ata_piix.c
  1291. </chapter>
  1292. <chapter id="SILInt">
  1293. <title>sata_sil Internals</title>
  1294. !Idrivers/scsi/sata_sil.c
  1295. </chapter>
  1296. <chapter id="libataThanks">
  1297. <title>Thanks</title>
  1298. <para>
  1299. The bulk of the ATA knowledge comes thanks to long conversations with
  1300. Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA
  1301. and SCSI specifications.
  1302. </para>
  1303. <para>
  1304. Thanks to Alan Cox for pointing out similarities
  1305. between SATA and SCSI, and in general for motivation to hack on
  1306. libata.
  1307. </para>
  1308. <para>
  1309. libata's device detection
  1310. method, ata_pio_devchk, and in general all the early probing was
  1311. based on extensive study of Hale Landis's probe/reset code in his
  1312. ATADRVR driver (www.ata-atapi.com).
  1313. </para>
  1314. </chapter>
  1315. </book>