libata.tmpl 47 KB

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