libata.rst 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. ========================
  2. libATA Developer's Guide
  3. ========================
  4. :Author: Jeff Garzik
  5. Introduction
  6. ============
  7. libATA is a library used inside the Linux kernel to support ATA host
  8. controllers and devices. libATA provides an ATA driver API, class
  9. transports for ATA and ATAPI devices, and SCSI<->ATA translation for ATA
  10. devices according to the T10 SAT specification.
  11. This Guide documents the libATA driver API, library functions, library
  12. internals, and a couple sample ATA low-level drivers.
  13. libata Driver API
  14. =================
  15. :c:type:`struct ata_port_operations <ata_port_operations>`
  16. is defined for every low-level libata
  17. hardware driver, and it controls how the low-level driver interfaces
  18. with the ATA and SCSI layers.
  19. FIS-based drivers will hook into the system with ``->qc_prep()`` and
  20. ``->qc_issue()`` high-level hooks. Hardware which behaves in a manner
  21. similar to PCI IDE hardware may utilize several generic helpers,
  22. defining at a bare minimum the bus I/O addresses of the ATA shadow
  23. register blocks.
  24. :c:type:`struct ata_port_operations <ata_port_operations>`
  25. ----------------------------------------------------------
  26. Post-IDENTIFY device configuration
  27. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  28. ::
  29. void (*dev_config) (struct ata_port *, struct ata_device *);
  30. Called after IDENTIFY [PACKET] DEVICE is issued to each device found.
  31. Typically used to apply device-specific fixups prior to issue of SET
  32. FEATURES - XFER MODE, and prior to operation.
  33. This entry may be specified as NULL in ata_port_operations.
  34. Set PIO/DMA mode
  35. ~~~~~~~~~~~~~~~~
  36. ::
  37. void (*set_piomode) (struct ata_port *, struct ata_device *);
  38. void (*set_dmamode) (struct ata_port *, struct ata_device *);
  39. void (*post_set_mode) (struct ata_port *);
  40. unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int);
  41. Hooks called prior to the issue of SET FEATURES - XFER MODE command. The
  42. optional ``->mode_filter()`` hook is called when libata has built a mask of
  43. the possible modes. This is passed to the ``->mode_filter()`` function
  44. which should return a mask of valid modes after filtering those
  45. unsuitable due to hardware limits. It is not valid to use this interface
  46. to add modes.
  47. ``dev->pio_mode`` and ``dev->dma_mode`` are guaranteed to be valid when
  48. ``->set_piomode()`` and when ``->set_dmamode()`` is called. The timings for
  49. any other drive sharing the cable will also be valid at this point. That
  50. is the library records the decisions for the modes of each drive on a
  51. channel before it attempts to set any of them.
  52. ``->post_set_mode()`` is called unconditionally, after the SET FEATURES -
  53. XFER MODE command completes successfully.
  54. ``->set_piomode()`` is always called (if present), but ``->set_dma_mode()``
  55. is only called if DMA is possible.
  56. Taskfile read/write
  57. ~~~~~~~~~~~~~~~~~~~
  58. ::
  59. void (*sff_tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
  60. void (*sff_tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
  61. ``->tf_load()`` is called to load the given taskfile into hardware
  62. registers / DMA buffers. ``->tf_read()`` is called to read the hardware
  63. registers / DMA buffers, to obtain the current set of taskfile register
  64. values. Most drivers for taskfile-based hardware (PIO or MMIO) use
  65. :c:func:`ata_sff_tf_load` and :c:func:`ata_sff_tf_read` for these hooks.
  66. PIO data read/write
  67. ~~~~~~~~~~~~~~~~~~~
  68. ::
  69. void (*sff_data_xfer) (struct ata_device *, unsigned char *, unsigned int, int);
  70. All bmdma-style drivers must implement this hook. This is the low-level
  71. operation that actually copies the data bytes during a PIO data
  72. transfer. Typically the driver will choose one of
  73. :c:func:`ata_sff_data_xfer`, or :c:func:`ata_sff_data_xfer32`.
  74. ATA command execute
  75. ~~~~~~~~~~~~~~~~~~~
  76. ::
  77. void (*sff_exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
  78. causes an ATA command, previously loaded with ``->tf_load()``, to be
  79. initiated in hardware. Most drivers for taskfile-based hardware use
  80. :c:func:`ata_sff_exec_command` for this hook.
  81. Per-cmd ATAPI DMA capabilities filter
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. ::
  84. int (*check_atapi_dma) (struct ata_queued_cmd *qc);
  85. Allow low-level driver to filter ATA PACKET commands, returning a status
  86. indicating whether or not it is OK to use DMA for the supplied PACKET
  87. command.
  88. This hook may be specified as NULL, in which case libata will assume
  89. that atapi dma can be supported.
  90. Read specific ATA shadow registers
  91. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. ::
  93. u8 (*sff_check_status)(struct ata_port *ap);
  94. u8 (*sff_check_altstatus)(struct ata_port *ap);
  95. Reads the Status/AltStatus ATA shadow register from hardware. On some
  96. hardware, reading the Status register has the side effect of clearing
  97. the interrupt condition. Most drivers for taskfile-based hardware use
  98. :c:func:`ata_sff_check_status` for this hook.
  99. Write specific ATA shadow register
  100. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  101. ::
  102. void (*sff_set_devctl)(struct ata_port *ap, u8 ctl);
  103. Write the device control ATA shadow register to the hardware. Most
  104. drivers don't need to define this.
  105. Select ATA device on bus
  106. ~~~~~~~~~~~~~~~~~~~~~~~~
  107. ::
  108. void (*sff_dev_select)(struct ata_port *ap, unsigned int device);
  109. Issues the low-level hardware command(s) that causes one of N hardware
  110. devices to be considered 'selected' (active and available for use) on
  111. the ATA bus. This generally has no meaning on FIS-based devices.
  112. Most drivers for taskfile-based hardware use :c:func:`ata_sff_dev_select` for
  113. this hook.
  114. Private tuning method
  115. ~~~~~~~~~~~~~~~~~~~~~
  116. ::
  117. void (*set_mode) (struct ata_port *ap);
  118. By default libata performs drive and controller tuning in accordance
  119. with the ATA timing rules and also applies blacklists and cable limits.
  120. Some controllers need special handling and have custom tuning rules,
  121. typically raid controllers that use ATA commands but do not actually do
  122. drive timing.
  123. **Warning**
  124. This hook should not be used to replace the standard controller
  125. tuning logic when a controller has quirks. Replacing the default
  126. tuning logic in that case would bypass handling for drive and bridge
  127. quirks that may be important to data reliability. If a controller
  128. needs to filter the mode selection it should use the mode_filter
  129. hook instead.
  130. Control PCI IDE BMDMA engine
  131. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  132. ::
  133. void (*bmdma_setup) (struct ata_queued_cmd *qc);
  134. void (*bmdma_start) (struct ata_queued_cmd *qc);
  135. void (*bmdma_stop) (struct ata_port *ap);
  136. u8 (*bmdma_status) (struct ata_port *ap);
  137. When setting up an IDE BMDMA transaction, these hooks arm
  138. (``->bmdma_setup``), fire (``->bmdma_start``), and halt (``->bmdma_stop``) the
  139. hardware's DMA engine. ``->bmdma_status`` is used to read the standard PCI
  140. IDE DMA Status register.
  141. These hooks are typically either no-ops, or simply not implemented, in
  142. FIS-based drivers.
  143. Most legacy IDE drivers use :c:func:`ata_bmdma_setup` for the
  144. :c:func:`bmdma_setup` hook. :c:func:`ata_bmdma_setup` will write the pointer
  145. to the PRD table to the IDE PRD Table Address register, enable DMA in the DMA
  146. Command register, and call :c:func:`exec_command` to begin the transfer.
  147. Most legacy IDE drivers use :c:func:`ata_bmdma_start` for the
  148. :c:func:`bmdma_start` hook. :c:func:`ata_bmdma_start` will write the
  149. ATA_DMA_START flag to the DMA Command register.
  150. Many legacy IDE drivers use :c:func:`ata_bmdma_stop` for the
  151. :c:func:`bmdma_stop` hook. :c:func:`ata_bmdma_stop` clears the ATA_DMA_START
  152. flag in the DMA command register.
  153. Many legacy IDE drivers use :c:func:`ata_bmdma_status` as the
  154. :c:func:`bmdma_status` hook.
  155. High-level taskfile hooks
  156. ~~~~~~~~~~~~~~~~~~~~~~~~~
  157. ::
  158. enum ata_completion_errors (*qc_prep) (struct ata_queued_cmd *qc);
  159. int (*qc_issue) (struct ata_queued_cmd *qc);
  160. Higher-level hooks, these two hooks can potentially supersede several of
  161. the above taskfile/DMA engine hooks. ``->qc_prep`` is called after the
  162. buffers have been DMA-mapped, and is typically used to populate the
  163. hardware's DMA scatter-gather table. Some drivers use the standard
  164. :c:func:`ata_bmdma_qc_prep` and :c:func:`ata_bmdma_dumb_qc_prep` helper
  165. functions, but more advanced drivers roll their own.
  166. ``->qc_issue`` is used to make a command active, once the hardware and S/G
  167. tables have been prepared. IDE BMDMA drivers use the helper function
  168. :c:func:`ata_sff_qc_issue` for taskfile protocol-based dispatch. More
  169. advanced drivers implement their own ``->qc_issue``.
  170. :c:func:`ata_sff_qc_issue` calls ``->sff_tf_load()``, ``->bmdma_setup()``, and
  171. ``->bmdma_start()`` as necessary to initiate a transfer.
  172. Exception and probe handling (EH)
  173. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  174. ::
  175. void (*freeze) (struct ata_port *ap);
  176. void (*thaw) (struct ata_port *ap);
  177. :c:func:`ata_port_freeze` is called when HSM violations or some other
  178. condition disrupts normal operation of the port. A frozen port is not
  179. allowed to perform any operation until the port is thawed, which usually
  180. follows a successful reset.
  181. The optional ``->freeze()`` callback can be used for freezing the port
  182. hardware-wise (e.g. mask interrupt and stop DMA engine). If a port
  183. cannot be frozen hardware-wise, the interrupt handler must ack and clear
  184. interrupts unconditionally while the port is frozen.
  185. The optional ``->thaw()`` callback is called to perform the opposite of
  186. ``->freeze()``: prepare the port for normal operation once again. Unmask
  187. interrupts, start DMA engine, etc.
  188. ::
  189. void (*error_handler) (struct ata_port *ap);
  190. ``->error_handler()`` is a driver's hook into probe, hotplug, and recovery
  191. and other exceptional conditions. The primary responsibility of an
  192. implementation is to call :c:func:`ata_std_error_handler`.
  193. :c:func:`ata_std_error_handler` will perform a standard error handling sequence
  194. to resurect failed devices, detach lost devices and add new devices (if any).
  195. This function will call the various reset operations for a port, as needed.
  196. These operations are as follows.
  197. * The 'prereset' operation (which may be NULL) is called during an EH reset,
  198. before any other action is taken.
  199. * The 'postreset' hook (which may be NULL) is called after the EH reset is
  200. performed. Based on existing conditions, severity of the problem, and hardware
  201. capabilities,
  202. * Either the 'softreset' operation or the 'hardreset' operation will be called
  203. to perform the low-level EH reset. If both operations are defined,
  204. 'hardreset' is preferred and used. If both are not defined, no low-level reset
  205. is performed and EH assumes that an ATA class device is connected through the
  206. link.
  207. ::
  208. void (*post_internal_cmd) (struct ata_queued_cmd *qc);
  209. Perform any hardware-specific actions necessary to finish processing
  210. after executing a probe-time or EH-time command via
  211. :c:func:`ata_exec_internal`.
  212. Hardware interrupt handling
  213. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  214. ::
  215. irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
  216. void (*irq_clear) (struct ata_port *);
  217. ``->irq_handler`` is the interrupt handling routine registered with the
  218. system, by libata. ``->irq_clear`` is called during probe just before the
  219. interrupt handler is registered, to be sure hardware is quiet.
  220. The second argument, dev_instance, should be cast to a pointer to
  221. :c:type:`struct ata_host_set <ata_host_set>`.
  222. Most legacy IDE drivers use :c:func:`ata_sff_interrupt` for the irq_handler
  223. hook, which scans all ports in the host_set, determines which queued
  224. command was active (if any), and calls ata_sff_host_intr(ap,qc).
  225. Most legacy IDE drivers use :c:func:`ata_sff_irq_clear` for the
  226. :c:func:`irq_clear` hook, which simply clears the interrupt and error flags
  227. in the DMA status register.
  228. SATA phy read/write
  229. ~~~~~~~~~~~~~~~~~~~
  230. ::
  231. int (*scr_read) (struct ata_port *ap, unsigned int sc_reg,
  232. u32 *val);
  233. int (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
  234. u32 val);
  235. Read and write standard SATA phy registers.
  236. sc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
  237. Init and shutdown
  238. ~~~~~~~~~~~~~~~~~
  239. ::
  240. int (*port_start) (struct ata_port *ap);
  241. void (*port_stop) (struct ata_port *ap);
  242. void (*host_stop) (struct ata_host_set *host_set);
  243. ``->port_start()`` is called just after the data structures for each port
  244. are initialized. Typically this is used to alloc per-port DMA buffers /
  245. tables / rings, enable DMA engines, and similar tasks. Some drivers also
  246. use this entry point as a chance to allocate driver-private memory for
  247. ``ap->private_data``.
  248. Many drivers use :c:func:`ata_port_start` as this hook or call it from their
  249. own :c:func:`port_start` hooks. :c:func:`ata_port_start` allocates space for
  250. a legacy IDE PRD table and returns.
  251. ``->port_stop()`` is called after ``->host_stop()``. Its sole function is to
  252. release DMA/memory resources, now that they are no longer actively being
  253. used. Many drivers also free driver-private data from port at this time.
  254. ``->host_stop()`` is called after all ``->port_stop()`` calls have completed.
  255. The hook must finalize hardware shutdown, release DMA and other
  256. resources, etc. This hook may be specified as NULL, in which case it is
  257. not called.
  258. Error handling
  259. ==============
  260. This chapter describes how errors are handled under libata. Readers are
  261. advised to read SCSI EH (Documentation/scsi/scsi_eh.rst) and ATA
  262. exceptions doc first.
  263. Origins of commands
  264. -------------------
  265. In libata, a command is represented with
  266. :c:type:`struct ata_queued_cmd <ata_queued_cmd>` or qc.
  267. qc's are preallocated during port initialization and repetitively used
  268. for command executions. Currently only one qc is allocated per port but
  269. yet-to-be-merged NCQ branch allocates one for each tag and maps each qc
  270. to NCQ tag 1-to-1.
  271. libata commands can originate from two sources - libata itself and SCSI
  272. midlayer. libata internal commands are used for initialization and error
  273. handling. All normal blk requests and commands for SCSI emulation are
  274. passed as SCSI commands through queuecommand callback of SCSI host
  275. template.
  276. How commands are issued
  277. -----------------------
  278. Internal commands
  279. Once allocated qc's taskfile is initialized for the command to be
  280. executed. qc currently has two mechanisms to notify completion. One
  281. is via ``qc->complete_fn()`` callback and the other is completion
  282. ``qc->waiting``. ``qc->complete_fn()`` callback is the asynchronous path
  283. used by normal SCSI translated commands and ``qc->waiting`` is the
  284. synchronous (issuer sleeps in process context) path used by internal
  285. commands.
  286. Once initialization is complete, host_set lock is acquired and the
  287. qc is issued.
  288. SCSI commands
  289. All libata drivers use :c:func:`ata_scsi_queuecmd` as
  290. ``hostt->queuecommand`` callback. scmds can either be simulated or
  291. translated. No qc is involved in processing a simulated scmd. The
  292. result is computed right away and the scmd is completed.
  293. ``qc->complete_fn()`` callback is used for completion notification. ATA
  294. commands use :c:func:`ata_scsi_qc_complete` while ATAPI commands use
  295. :c:func:`atapi_qc_complete`. Both functions end up calling ``qc->scsidone``
  296. to notify upper layer when the qc is finished. After translation is
  297. completed, the qc is issued with :c:func:`ata_qc_issue`.
  298. Note that SCSI midlayer invokes hostt->queuecommand while holding
  299. host_set lock, so all above occur while holding host_set lock.
  300. How commands are processed
  301. --------------------------
  302. Depending on which protocol and which controller are used, commands are
  303. processed differently. For the purpose of discussion, a controller which
  304. uses taskfile interface and all standard callbacks is assumed.
  305. Currently 6 ATA command protocols are used. They can be sorted into the
  306. following four categories according to how they are processed.
  307. ATA NO DATA or DMA
  308. ATA_PROT_NODATA and ATA_PROT_DMA fall into this category. These
  309. types of commands don't require any software intervention once
  310. issued. Device will raise interrupt on completion.
  311. ATA PIO
  312. ATA_PROT_PIO is in this category. libata currently implements PIO
  313. with polling. ATA_NIEN bit is set to turn off interrupt and
  314. pio_task on ata_wq performs polling and IO.
  315. ATAPI NODATA or DMA
  316. ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
  317. category. packet_task is used to poll BSY bit after issuing PACKET
  318. command. Once BSY is turned off by the device, packet_task
  319. transfers CDB and hands off processing to interrupt handler.
  320. ATAPI PIO
  321. ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set and, as
  322. in ATAPI NODATA or DMA, packet_task submits cdb. However, after
  323. submitting cdb, further processing (data transfer) is handed off to
  324. pio_task.
  325. How commands are completed
  326. --------------------------
  327. Once issued, all qc's are either completed with :c:func:`ata_qc_complete` or
  328. time out. For commands which are handled by interrupts,
  329. :c:func:`ata_host_intr` invokes :c:func:`ata_qc_complete`, and, for PIO tasks,
  330. pio_task invokes :c:func:`ata_qc_complete`. In error cases, packet_task may
  331. also complete commands.
  332. :c:func:`ata_qc_complete` does the following.
  333. 1. DMA memory is unmapped.
  334. 2. ATA_QCFLAG_ACTIVE is cleared from qc->flags.
  335. 3. :c:expr:`qc->complete_fn` callback is invoked. If the return value of the
  336. callback is not zero. Completion is short circuited and
  337. :c:func:`ata_qc_complete` returns.
  338. 4. :c:func:`__ata_qc_complete` is called, which does
  339. 1. ``qc->flags`` is cleared to zero.
  340. 2. ``ap->active_tag`` and ``qc->tag`` are poisoned.
  341. 3. ``qc->waiting`` is cleared & completed (in that order).
  342. 4. qc is deallocated by clearing appropriate bit in ``ap->qactive``.
  343. So, it basically notifies upper layer and deallocates qc. One exception
  344. is short-circuit path in #3 which is used by :c:func:`atapi_qc_complete`.
  345. For all non-ATAPI commands, whether it fails or not, almost the same
  346. code path is taken and very little error handling takes place. A qc is
  347. completed with success status if it succeeded, with failed status
  348. otherwise.
  349. However, failed ATAPI commands require more handling as REQUEST SENSE is
  350. needed to acquire sense data. If an ATAPI command fails,
  351. :c:func:`ata_qc_complete` is invoked with error status, which in turn invokes
  352. :c:func:`atapi_qc_complete` via ``qc->complete_fn()`` callback.
  353. This makes :c:func:`atapi_qc_complete` set ``scmd->result`` to
  354. SAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As the
  355. sense data is empty but ``scmd->result`` is CHECK CONDITION, SCSI midlayer
  356. will invoke EH for the scmd, and returning 1 makes :c:func:`ata_qc_complete`
  357. to return without deallocating the qc. This leads us to
  358. :c:func:`ata_scsi_error` with partially completed qc.
  359. :c:func:`ata_scsi_error`
  360. ------------------------
  361. :c:func:`ata_scsi_error` is the current ``transportt->eh_strategy_handler()``
  362. for libata. As discussed above, this will be entered in two cases -
  363. timeout and ATAPI error completion. This function will check if a qc is active
  364. and has not failed yet. Such a qc will be marked with AC_ERR_TIMEOUT such that
  365. EH will know to handle it later. Then it calls low level libata driver's
  366. :c:func:`error_handler` callback.
  367. When the :c:func:`error_handler` callback is invoked it stops BMDMA and
  368. completes the qc. Note that as we're currently in EH, we cannot call
  369. scsi_done. As described in SCSI EH doc, a recovered scmd should be
  370. either retried with :c:func:`scsi_queue_insert` or finished with
  371. :c:func:`scsi_finish_command`. Here, we override ``qc->scsidone`` with
  372. :c:func:`scsi_finish_command` and calls :c:func:`ata_qc_complete`.
  373. If EH is invoked due to a failed ATAPI qc, the qc here is completed but
  374. not deallocated. The purpose of this half-completion is to use the qc as
  375. place holder to make EH code reach this place. This is a bit hackish,
  376. but it works.
  377. Once control reaches here, the qc is deallocated by invoking
  378. :c:func:`__ata_qc_complete` explicitly. Then, internal qc for REQUEST SENSE
  379. is issued. Once sense data is acquired, scmd is finished by directly
  380. invoking :c:func:`scsi_finish_command` on the scmd. Note that as we already
  381. have completed and deallocated the qc which was associated with the
  382. scmd, we don't need to/cannot call :c:func:`ata_qc_complete` again.
  383. Problems with the current EH
  384. ----------------------------
  385. - Error representation is too crude. Currently any and all error
  386. conditions are represented with ATA STATUS and ERROR registers.
  387. Errors which aren't ATA device errors are treated as ATA device
  388. errors by setting ATA_ERR bit. Better error descriptor which can
  389. properly represent ATA and other errors/exceptions is needed.
  390. - When handling timeouts, no action is taken to make device forget
  391. about the timed out command and ready for new commands.
  392. - EH handling via :c:func:`ata_scsi_error` is not properly protected from
  393. usual command processing. On EH entrance, the device is not in
  394. quiescent state. Timed out commands may succeed or fail any time.
  395. pio_task and atapi_task may still be running.
  396. - Too weak error recovery. Devices / controllers causing HSM mismatch
  397. errors and other errors quite often require reset to return to known
  398. state. Also, advanced error handling is necessary to support features
  399. like NCQ and hotplug.
  400. - ATA errors are directly handled in the interrupt handler and PIO
  401. errors in pio_task. This is problematic for advanced error handling
  402. for the following reasons.
  403. First, advanced error handling often requires context and internal qc
  404. execution.
  405. Second, even a simple failure (say, CRC error) needs information
  406. gathering and could trigger complex error handling (say, resetting &
  407. reconfiguring). Having multiple code paths to gather information,
  408. enter EH and trigger actions makes life painful.
  409. Third, scattered EH code makes implementing low level drivers
  410. difficult. Low level drivers override libata callbacks. If EH is
  411. scattered over several places, each affected callbacks should perform
  412. its part of error handling. This can be error prone and painful.
  413. libata Library
  414. ==============
  415. .. kernel-doc:: drivers/ata/libata-core.c
  416. :export:
  417. libata Core Internals
  418. =====================
  419. .. kernel-doc:: drivers/ata/libata-core.c
  420. :internal:
  421. .. kernel-doc:: drivers/ata/libata-eh.c
  422. libata SCSI translation/emulation
  423. =================================
  424. .. kernel-doc:: drivers/ata/libata-scsi.c
  425. :export:
  426. .. kernel-doc:: drivers/ata/libata-scsi.c
  427. :internal:
  428. ATA errors and exceptions
  429. =========================
  430. This chapter tries to identify what error/exception conditions exist for
  431. ATA/ATAPI devices and describe how they should be handled in
  432. implementation-neutral way.
  433. The term 'error' is used to describe conditions where either an explicit
  434. error condition is reported from device or a command has timed out.
  435. The term 'exception' is either used to describe exceptional conditions
  436. which are not errors (say, power or hotplug events), or to describe both
  437. errors and non-error exceptional conditions. Where explicit distinction
  438. between error and exception is necessary, the term 'non-error exception'
  439. is used.
  440. Exception categories
  441. --------------------
  442. Exceptions are described primarily with respect to legacy taskfile + bus
  443. master IDE interface. If a controller provides other better mechanism
  444. for error reporting, mapping those into categories described below
  445. shouldn't be difficult.
  446. In the following sections, two recovery actions - reset and
  447. reconfiguring transport - are mentioned. These are described further in
  448. `EH recovery actions <#exrec>`__.
  449. HSM violation
  450. ~~~~~~~~~~~~~
  451. This error is indicated when STATUS value doesn't match HSM requirement
  452. during issuing or execution any ATA/ATAPI command.
  453. - ATA_STATUS doesn't contain !BSY && DRDY && !DRQ while trying to
  454. issue a command.
  455. - !BSY && !DRQ during PIO data transfer.
  456. - DRQ on command completion.
  457. - !BSY && ERR after CDB transfer starts but before the last byte of CDB
  458. is transferred. ATA/ATAPI standard states that "The device shall not
  459. terminate the PACKET command with an error before the last byte of
  460. the command packet has been written" in the error outputs description
  461. of PACKET command and the state diagram doesn't include such
  462. transitions.
  463. In these cases, HSM is violated and not much information regarding the
  464. error can be acquired from STATUS or ERROR register. IOW, this error can
  465. be anything - driver bug, faulty device, controller and/or cable.
  466. As HSM is violated, reset is necessary to restore known state.
  467. Reconfiguring transport for lower speed might be helpful too as
  468. transmission errors sometimes cause this kind of errors.
  469. ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION)
  470. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  471. These are errors detected and reported by ATA/ATAPI devices indicating
  472. device problems. For this type of errors, STATUS and ERROR register
  473. values are valid and describe error condition. Note that some of ATA bus
  474. errors are detected by ATA/ATAPI devices and reported using the same
  475. mechanism as device errors. Those cases are described later in this
  476. section.
  477. For ATA commands, this type of errors are indicated by !BSY && ERR
  478. during command execution and on completion.
  479. For ATAPI commands,
  480. - !BSY && ERR && ABRT right after issuing PACKET indicates that PACKET
  481. command is not supported and falls in this category.
  482. - !BSY && ERR(==CHK) && !ABRT after the last byte of CDB is transferred
  483. indicates CHECK CONDITION and doesn't fall in this category.
  484. - !BSY && ERR(==CHK) && ABRT after the last byte of CDB is transferred
  485. \*probably\* indicates CHECK CONDITION and doesn't fall in this
  486. category.
  487. Of errors detected as above, the following are not ATA/ATAPI device
  488. errors but ATA bus errors and should be handled according to
  489. `ATA bus error <#excatATAbusErr>`__.
  490. CRC error during data transfer
  491. This is indicated by ICRC bit in the ERROR register and means that
  492. corruption occurred during data transfer. Up to ATA/ATAPI-7, the
  493. standard specifies that this bit is only applicable to UDMA
  494. transfers but ATA/ATAPI-8 draft revision 1f says that the bit may be
  495. applicable to multiword DMA and PIO.
  496. ABRT error during data transfer or on completion
  497. Up to ATA/ATAPI-7, the standard specifies that ABRT could be set on
  498. ICRC errors and on cases where a device is not able to complete a
  499. command. Combined with the fact that MWDMA and PIO transfer errors
  500. aren't allowed to use ICRC bit up to ATA/ATAPI-7, it seems to imply
  501. that ABRT bit alone could indicate transfer errors.
  502. However, ATA/ATAPI-8 draft revision 1f removes the part that ICRC
  503. errors can turn on ABRT. So, this is kind of gray area. Some
  504. heuristics are needed here.
  505. ATA/ATAPI device errors can be further categorized as follows.
  506. Media errors
  507. This is indicated by UNC bit in the ERROR register. ATA devices
  508. reports UNC error only after certain number of retries cannot
  509. recover the data, so there's nothing much else to do other than
  510. notifying upper layer.
  511. READ and WRITE commands report CHS or LBA of the first failed sector
  512. but ATA/ATAPI standard specifies that the amount of transferred data
  513. on error completion is indeterminate, so we cannot assume that
  514. sectors preceding the failed sector have been transferred and thus
  515. cannot complete those sectors successfully as SCSI does.
  516. Media changed / media change requested error
  517. <<TODO: fill here>>
  518. Address error
  519. This is indicated by IDNF bit in the ERROR register. Report to upper
  520. layer.
  521. Other errors
  522. This can be invalid command or parameter indicated by ABRT ERROR bit
  523. or some other error condition. Note that ABRT bit can indicate a lot
  524. of things including ICRC and Address errors. Heuristics needed.
  525. Depending on commands, not all STATUS/ERROR bits are applicable. These
  526. non-applicable bits are marked with "na" in the output descriptions but
  527. up to ATA/ATAPI-7 no definition of "na" can be found. However,
  528. ATA/ATAPI-8 draft revision 1f describes "N/A" as follows.
  529. 3.2.3.3a N/A
  530. A keyword the indicates a field has no defined value in this
  531. standard and should not be checked by the host or device. N/A
  532. fields should be cleared to zero.
  533. So, it seems reasonable to assume that "na" bits are cleared to zero by
  534. devices and thus need no explicit masking.
  535. ATAPI device CHECK CONDITION
  536. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  537. ATAPI device CHECK CONDITION error is indicated by set CHK bit (ERR bit)
  538. in the STATUS register after the last byte of CDB is transferred for a
  539. PACKET command. For this kind of errors, sense data should be acquired
  540. to gather information regarding the errors. REQUEST SENSE packet command
  541. should be used to acquire sense data.
  542. Once sense data is acquired, this type of errors can be handled
  543. similarly to other SCSI errors. Note that sense data may indicate ATA
  544. bus error (e.g. Sense Key 04h HARDWARE ERROR && ASC/ASCQ 47h/00h SCSI
  545. PARITY ERROR). In such cases, the error should be considered as an ATA
  546. bus error and handled according to `ATA bus error <#excatATAbusErr>`__.
  547. ATA device error (NCQ)
  548. ~~~~~~~~~~~~~~~~~~~~~~
  549. NCQ command error is indicated by cleared BSY and set ERR bit during NCQ
  550. command phase (one or more NCQ commands outstanding). Although STATUS
  551. and ERROR registers will contain valid values describing the error, READ
  552. LOG EXT is required to clear the error condition, determine which
  553. command has failed and acquire more information.
  554. READ LOG EXT Log Page 10h reports which tag has failed and taskfile
  555. register values describing the error. With this information the failed
  556. command can be handled as a normal ATA command error as in
  557. `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__
  558. and all other in-flight commands must be retried. Note that this retry
  559. should not be counted - it's likely that commands retried this way would
  560. have completed normally if it were not for the failed command.
  561. Note that ATA bus errors can be reported as ATA device NCQ errors. This
  562. should be handled as described in `ATA bus error <#excatATAbusErr>`__.
  563. If READ LOG EXT Log Page 10h fails or reports NQ, we're thoroughly
  564. screwed. This condition should be treated according to
  565. `HSM violation <#excatHSMviolation>`__.
  566. ATA bus error
  567. ~~~~~~~~~~~~~
  568. ATA bus error means that data corruption occurred during transmission
  569. over ATA bus (SATA or PATA). This type of errors can be indicated by
  570. - ICRC or ABRT error as described in
  571. `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__.
  572. - Controller-specific error completion with error information
  573. indicating transmission error.
  574. - On some controllers, command timeout. In this case, there may be a
  575. mechanism to determine that the timeout is due to transmission error.
  576. - Unknown/random errors, timeouts and all sorts of weirdities.
  577. As described above, transmission errors can cause wide variety of
  578. symptoms ranging from device ICRC error to random device lockup, and,
  579. for many cases, there is no way to tell if an error condition is due to
  580. transmission error or not; therefore, it's necessary to employ some kind
  581. of heuristic when dealing with errors and timeouts. For example,
  582. encountering repetitive ABRT errors for known supported command is
  583. likely to indicate ATA bus error.
  584. Once it's determined that ATA bus errors have possibly occurred,
  585. lowering ATA bus transmission speed is one of actions which may
  586. alleviate the problem. See `Reconfigure transport <#exrecReconf>`__ for
  587. more information.
  588. PCI bus error
  589. ~~~~~~~~~~~~~
  590. Data corruption or other failures during transmission over PCI (or other
  591. system bus). For standard BMDMA, this is indicated by Error bit in the
  592. BMDMA Status register. This type of errors must be logged as it
  593. indicates something is very wrong with the system. Resetting host
  594. controller is recommended.
  595. Late completion
  596. ~~~~~~~~~~~~~~~
  597. This occurs when timeout occurs and the timeout handler finds out that
  598. the timed out command has completed successfully or with error. This is
  599. usually caused by lost interrupts. This type of errors must be logged.
  600. Resetting host controller is recommended.
  601. Unknown error (timeout)
  602. ~~~~~~~~~~~~~~~~~~~~~~~
  603. This is when timeout occurs and the command is still processing or the
  604. host and device are in unknown state. When this occurs, HSM could be in
  605. any valid or invalid state. To bring the device to known state and make
  606. it forget about the timed out command, resetting is necessary. The timed
  607. out command may be retried.
  608. Timeouts can also be caused by transmission errors. Refer to
  609. `ATA bus error <#excatATAbusErr>`__ for more details.
  610. Hotplug and power management exceptions
  611. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  612. <<TODO: fill here>>
  613. EH recovery actions
  614. -------------------
  615. This section discusses several important recovery actions.
  616. Clearing error condition
  617. ~~~~~~~~~~~~~~~~~~~~~~~~
  618. Many controllers require its error registers to be cleared by error
  619. handler. Different controllers may have different requirements.
  620. For SATA, it's strongly recommended to clear at least SError register
  621. during error handling.
  622. Reset
  623. ~~~~~
  624. During EH, resetting is necessary in the following cases.
  625. - HSM is in unknown or invalid state
  626. - HBA is in unknown or invalid state
  627. - EH needs to make HBA/device forget about in-flight commands
  628. - HBA/device behaves weirdly
  629. Resetting during EH might be a good idea regardless of error condition
  630. to improve EH robustness. Whether to reset both or either one of HBA and
  631. device depends on situation but the following scheme is recommended.
  632. - When it's known that HBA is in ready state but ATA/ATAPI device is in
  633. unknown state, reset only device.
  634. - If HBA is in unknown state, reset both HBA and device.
  635. HBA resetting is implementation specific. For a controller complying to
  636. taskfile/BMDMA PCI IDE, stopping active DMA transaction may be
  637. sufficient iff BMDMA state is the only HBA context. But even mostly
  638. taskfile/BMDMA PCI IDE complying controllers may have implementation
  639. specific requirements and mechanism to reset themselves. This must be
  640. addressed by specific drivers.
  641. OTOH, ATA/ATAPI standard describes in detail ways to reset ATA/ATAPI
  642. devices.
  643. PATA hardware reset
  644. This is hardware initiated device reset signalled with asserted PATA
  645. RESET- signal. There is no standard way to initiate hardware reset
  646. from software although some hardware provides registers that allow
  647. driver to directly tweak the RESET- signal.
  648. Software reset
  649. This is achieved by turning CONTROL SRST bit on for at least 5us.
  650. Both PATA and SATA support it but, in case of SATA, this may require
  651. controller-specific support as the second Register FIS to clear SRST
  652. should be transmitted while BSY bit is still set. Note that on PATA,
  653. this resets both master and slave devices on a channel.
  654. EXECUTE DEVICE DIAGNOSTIC command
  655. Although ATA/ATAPI standard doesn't describe exactly, EDD implies
  656. some level of resetting, possibly similar level with software reset.
  657. Host-side EDD protocol can be handled with normal command processing
  658. and most SATA controllers should be able to handle EDD's just like
  659. other commands. As in software reset, EDD affects both devices on a
  660. PATA bus.
  661. Although EDD does reset devices, this doesn't suit error handling as
  662. EDD cannot be issued while BSY is set and it's unclear how it will
  663. act when device is in unknown/weird state.
  664. ATAPI DEVICE RESET command
  665. This is very similar to software reset except that reset can be
  666. restricted to the selected device without affecting the other device
  667. sharing the cable.
  668. SATA phy reset
  669. This is the preferred way of resetting a SATA device. In effect,
  670. it's identical to PATA hardware reset. Note that this can be done
  671. with the standard SCR Control register. As such, it's usually easier
  672. to implement than software reset.
  673. One more thing to consider when resetting devices is that resetting
  674. clears certain configuration parameters and they need to be set to their
  675. previous or newly adjusted values after reset.
  676. Parameters affected are.
  677. - CHS set up with INITIALIZE DEVICE PARAMETERS (seldom used)
  678. - Parameters set with SET FEATURES including transfer mode setting
  679. - Block count set with SET MULTIPLE MODE
  680. - Other parameters (SET MAX, MEDIA LOCK...)
  681. ATA/ATAPI standard specifies that some parameters must be maintained
  682. across hardware or software reset, but doesn't strictly specify all of
  683. them. Always reconfiguring needed parameters after reset is required for
  684. robustness. Note that this also applies when resuming from deep sleep
  685. (power-off).
  686. Also, ATA/ATAPI standard requires that IDENTIFY DEVICE / IDENTIFY PACKET
  687. DEVICE is issued after any configuration parameter is updated or a
  688. hardware reset and the result used for further operation. OS driver is
  689. required to implement revalidation mechanism to support this.
  690. Reconfigure transport
  691. ~~~~~~~~~~~~~~~~~~~~~
  692. For both PATA and SATA, a lot of corners are cut for cheap connectors,
  693. cables or controllers and it's quite common to see high transmission
  694. error rate. This can be mitigated by lowering transmission speed.
  695. The following is a possible scheme Jeff Garzik suggested.
  696. If more than $N (3?) transmission errors happen in 15 minutes,
  697. - if SATA, decrease SATA PHY speed. if speed cannot be decreased,
  698. - decrease UDMA xfer speed. if at UDMA0, switch to PIO4,
  699. - decrease PIO xfer speed. if at PIO3, complain, but continue
  700. ata_piix Internals
  701. ===================
  702. .. kernel-doc:: drivers/ata/ata_piix.c
  703. :internal:
  704. sata_sil Internals
  705. ===================
  706. .. kernel-doc:: drivers/ata/sata_sil.c
  707. :internal:
  708. Thanks
  709. ======
  710. The bulk of the ATA knowledge comes thanks to long conversations with
  711. Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA and
  712. SCSI specifications.
  713. Thanks to Alan Cox for pointing out similarities between SATA and SCSI,
  714. and in general for motivation to hack on libata.
  715. libata's device detection method, ata_pio_devchk, and in general all
  716. the early probing was based on extensive study of Hale Landis's
  717. probe/reset code in his ATADRVR driver (www.ata-atapi.com).