0001 ========================
0002 libATA Developer's Guide
0003 ========================
0004
0005 :Author: Jeff Garzik
0006
0007 Introduction
0008 ============
0009
0010 libATA is a library used inside the Linux kernel to support ATA host
0011 controllers and devices. libATA provides an ATA driver API, class
0012 transports for ATA and ATAPI devices, and SCSI<->ATA translation for ATA
0013 devices according to the T10 SAT specification.
0014
0015 This Guide documents the libATA driver API, library functions, library
0016 internals, and a couple sample ATA low-level drivers.
0017
0018 libata Driver API
0019 =================
0020
0021 :c:type:`struct ata_port_operations <ata_port_operations>`
0022 is defined for every low-level libata
0023 hardware driver, and it controls how the low-level driver interfaces
0024 with the ATA and SCSI layers.
0025
0026 FIS-based drivers will hook into the system with ``->qc_prep()`` and
0027 ``->qc_issue()`` high-level hooks. Hardware which behaves in a manner
0028 similar to PCI IDE hardware may utilize several generic helpers,
0029 defining at a bare minimum the bus I/O addresses of the ATA shadow
0030 register blocks.
0031
0032 :c:type:`struct ata_port_operations <ata_port_operations>`
0033 ----------------------------------------------------------
0034
0035 Disable ATA port
0036 ~~~~~~~~~~~~~~~~
0037
0038 ::
0039
0040 void (*port_disable) (struct ata_port *);
0041
0042
0043 Called from :c:func:`ata_bus_probe` error path, as well as when unregistering
0044 from the SCSI module (rmmod, hot unplug). This function should do
0045 whatever needs to be done to take the port out of use. In most cases,
0046 :c:func:`ata_port_disable` can be used as this hook.
0047
0048 Called from :c:func:`ata_bus_probe` on a failed probe. Called from
0049 :c:func:`ata_scsi_release`.
0050
0051 Post-IDENTIFY device configuration
0052 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0053
0054 ::
0055
0056 void (*dev_config) (struct ata_port *, struct ata_device *);
0057
0058
0059 Called after IDENTIFY [PACKET] DEVICE is issued to each device found.
0060 Typically used to apply device-specific fixups prior to issue of SET
0061 FEATURES - XFER MODE, and prior to operation.
0062
0063 This entry may be specified as NULL in ata_port_operations.
0064
0065 Set PIO/DMA mode
0066 ~~~~~~~~~~~~~~~~
0067
0068 ::
0069
0070 void (*set_piomode) (struct ata_port *, struct ata_device *);
0071 void (*set_dmamode) (struct ata_port *, struct ata_device *);
0072 void (*post_set_mode) (struct ata_port *);
0073 unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int);
0074
0075
0076 Hooks called prior to the issue of SET FEATURES - XFER MODE command. The
0077 optional ``->mode_filter()`` hook is called when libata has built a mask of
0078 the possible modes. This is passed to the ``->mode_filter()`` function
0079 which should return a mask of valid modes after filtering those
0080 unsuitable due to hardware limits. It is not valid to use this interface
0081 to add modes.
0082
0083 ``dev->pio_mode`` and ``dev->dma_mode`` are guaranteed to be valid when
0084 ``->set_piomode()`` and when ``->set_dmamode()`` is called. The timings for
0085 any other drive sharing the cable will also be valid at this point. That
0086 is the library records the decisions for the modes of each drive on a
0087 channel before it attempts to set any of them.
0088
0089 ``->post_set_mode()`` is called unconditionally, after the SET FEATURES -
0090 XFER MODE command completes successfully.
0091
0092 ``->set_piomode()`` is always called (if present), but ``->set_dma_mode()``
0093 is only called if DMA is possible.
0094
0095 Taskfile read/write
0096 ~~~~~~~~~~~~~~~~~~~
0097
0098 ::
0099
0100 void (*sff_tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
0101 void (*sff_tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
0102
0103
0104 ``->tf_load()`` is called to load the given taskfile into hardware
0105 registers / DMA buffers. ``->tf_read()`` is called to read the hardware
0106 registers / DMA buffers, to obtain the current set of taskfile register
0107 values. Most drivers for taskfile-based hardware (PIO or MMIO) use
0108 :c:func:`ata_sff_tf_load` and :c:func:`ata_sff_tf_read` for these hooks.
0109
0110 PIO data read/write
0111 ~~~~~~~~~~~~~~~~~~~
0112
0113 ::
0114
0115 void (*sff_data_xfer) (struct ata_device *, unsigned char *, unsigned int, int);
0116
0117
0118 All bmdma-style drivers must implement this hook. This is the low-level
0119 operation that actually copies the data bytes during a PIO data
0120 transfer. Typically the driver will choose one of
0121 :c:func:`ata_sff_data_xfer`, or :c:func:`ata_sff_data_xfer32`.
0122
0123 ATA command execute
0124 ~~~~~~~~~~~~~~~~~~~
0125
0126 ::
0127
0128 void (*sff_exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
0129
0130
0131 causes an ATA command, previously loaded with ``->tf_load()``, to be
0132 initiated in hardware. Most drivers for taskfile-based hardware use
0133 :c:func:`ata_sff_exec_command` for this hook.
0134
0135 Per-cmd ATAPI DMA capabilities filter
0136 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0137
0138 ::
0139
0140 int (*check_atapi_dma) (struct ata_queued_cmd *qc);
0141
0142
0143 Allow low-level driver to filter ATA PACKET commands, returning a status
0144 indicating whether or not it is OK to use DMA for the supplied PACKET
0145 command.
0146
0147 This hook may be specified as NULL, in which case libata will assume
0148 that atapi dma can be supported.
0149
0150 Read specific ATA shadow registers
0151 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0152
0153 ::
0154
0155 u8 (*sff_check_status)(struct ata_port *ap);
0156 u8 (*sff_check_altstatus)(struct ata_port *ap);
0157
0158
0159 Reads the Status/AltStatus ATA shadow register from hardware. On some
0160 hardware, reading the Status register has the side effect of clearing
0161 the interrupt condition. Most drivers for taskfile-based hardware use
0162 :c:func:`ata_sff_check_status` for this hook.
0163
0164 Write specific ATA shadow register
0165 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0166
0167 ::
0168
0169 void (*sff_set_devctl)(struct ata_port *ap, u8 ctl);
0170
0171
0172 Write the device control ATA shadow register to the hardware. Most
0173 drivers don't need to define this.
0174
0175 Select ATA device on bus
0176 ~~~~~~~~~~~~~~~~~~~~~~~~
0177
0178 ::
0179
0180 void (*sff_dev_select)(struct ata_port *ap, unsigned int device);
0181
0182
0183 Issues the low-level hardware command(s) that causes one of N hardware
0184 devices to be considered 'selected' (active and available for use) on
0185 the ATA bus. This generally has no meaning on FIS-based devices.
0186
0187 Most drivers for taskfile-based hardware use :c:func:`ata_sff_dev_select` for
0188 this hook.
0189
0190 Private tuning method
0191 ~~~~~~~~~~~~~~~~~~~~~
0192
0193 ::
0194
0195 void (*set_mode) (struct ata_port *ap);
0196
0197
0198 By default libata performs drive and controller tuning in accordance
0199 with the ATA timing rules and also applies blacklists and cable limits.
0200 Some controllers need special handling and have custom tuning rules,
0201 typically raid controllers that use ATA commands but do not actually do
0202 drive timing.
0203
0204 **Warning**
0205
0206 This hook should not be used to replace the standard controller
0207 tuning logic when a controller has quirks. Replacing the default
0208 tuning logic in that case would bypass handling for drive and bridge
0209 quirks that may be important to data reliability. If a controller
0210 needs to filter the mode selection it should use the mode_filter
0211 hook instead.
0212
0213 Control PCI IDE BMDMA engine
0214 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0215
0216 ::
0217
0218 void (*bmdma_setup) (struct ata_queued_cmd *qc);
0219 void (*bmdma_start) (struct ata_queued_cmd *qc);
0220 void (*bmdma_stop) (struct ata_port *ap);
0221 u8 (*bmdma_status) (struct ata_port *ap);
0222
0223
0224 When setting up an IDE BMDMA transaction, these hooks arm
0225 (``->bmdma_setup``), fire (``->bmdma_start``), and halt (``->bmdma_stop``) the
0226 hardware's DMA engine. ``->bmdma_status`` is used to read the standard PCI
0227 IDE DMA Status register.
0228
0229 These hooks are typically either no-ops, or simply not implemented, in
0230 FIS-based drivers.
0231
0232 Most legacy IDE drivers use :c:func:`ata_bmdma_setup` for the
0233 :c:func:`bmdma_setup` hook. :c:func:`ata_bmdma_setup` will write the pointer
0234 to the PRD table to the IDE PRD Table Address register, enable DMA in the DMA
0235 Command register, and call :c:func:`exec_command` to begin the transfer.
0236
0237 Most legacy IDE drivers use :c:func:`ata_bmdma_start` for the
0238 :c:func:`bmdma_start` hook. :c:func:`ata_bmdma_start` will write the
0239 ATA_DMA_START flag to the DMA Command register.
0240
0241 Many legacy IDE drivers use :c:func:`ata_bmdma_stop` for the
0242 :c:func:`bmdma_stop` hook. :c:func:`ata_bmdma_stop` clears the ATA_DMA_START
0243 flag in the DMA command register.
0244
0245 Many legacy IDE drivers use :c:func:`ata_bmdma_status` as the
0246 :c:func:`bmdma_status` hook.
0247
0248 High-level taskfile hooks
0249 ~~~~~~~~~~~~~~~~~~~~~~~~~
0250
0251 ::
0252
0253 enum ata_completion_errors (*qc_prep) (struct ata_queued_cmd *qc);
0254 int (*qc_issue) (struct ata_queued_cmd *qc);
0255
0256
0257 Higher-level hooks, these two hooks can potentially supersede several of
0258 the above taskfile/DMA engine hooks. ``->qc_prep`` is called after the
0259 buffers have been DMA-mapped, and is typically used to populate the
0260 hardware's DMA scatter-gather table. Some drivers use the standard
0261 :c:func:`ata_bmdma_qc_prep` and :c:func:`ata_bmdma_dumb_qc_prep` helper
0262 functions, but more advanced drivers roll their own.
0263
0264 ``->qc_issue`` is used to make a command active, once the hardware and S/G
0265 tables have been prepared. IDE BMDMA drivers use the helper function
0266 :c:func:`ata_sff_qc_issue` for taskfile protocol-based dispatch. More
0267 advanced drivers implement their own ``->qc_issue``.
0268
0269 :c:func:`ata_sff_qc_issue` calls ``->sff_tf_load()``, ``->bmdma_setup()``, and
0270 ``->bmdma_start()`` as necessary to initiate a transfer.
0271
0272 Exception and probe handling (EH)
0273 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0274
0275 ::
0276
0277 void (*eng_timeout) (struct ata_port *ap);
0278 void (*phy_reset) (struct ata_port *ap);
0279
0280
0281 Deprecated. Use ``->error_handler()`` instead.
0282
0283 ::
0284
0285 void (*freeze) (struct ata_port *ap);
0286 void (*thaw) (struct ata_port *ap);
0287
0288
0289 :c:func:`ata_port_freeze` is called when HSM violations or some other
0290 condition disrupts normal operation of the port. A frozen port is not
0291 allowed to perform any operation until the port is thawed, which usually
0292 follows a successful reset.
0293
0294 The optional ``->freeze()`` callback can be used for freezing the port
0295 hardware-wise (e.g. mask interrupt and stop DMA engine). If a port
0296 cannot be frozen hardware-wise, the interrupt handler must ack and clear
0297 interrupts unconditionally while the port is frozen.
0298
0299 The optional ``->thaw()`` callback is called to perform the opposite of
0300 ``->freeze()``: prepare the port for normal operation once again. Unmask
0301 interrupts, start DMA engine, etc.
0302
0303 ::
0304
0305 void (*error_handler) (struct ata_port *ap);
0306
0307
0308 ``->error_handler()`` is a driver's hook into probe, hotplug, and recovery
0309 and other exceptional conditions. The primary responsibility of an
0310 implementation is to call :c:func:`ata_do_eh` or :c:func:`ata_bmdma_drive_eh`
0311 with a set of EH hooks as arguments:
0312
0313 'prereset' hook (may be NULL) is called during an EH reset, before any
0314 other actions are taken.
0315
0316 'postreset' hook (may be NULL) is called after the EH reset is
0317 performed. Based on existing conditions, severity of the problem, and
0318 hardware capabilities,
0319
0320 Either 'softreset' (may be NULL) or 'hardreset' (may be NULL) will be
0321 called to perform the low-level EH reset.
0322
0323 ::
0324
0325 void (*post_internal_cmd) (struct ata_queued_cmd *qc);
0326
0327
0328 Perform any hardware-specific actions necessary to finish processing
0329 after executing a probe-time or EH-time command via
0330 :c:func:`ata_exec_internal`.
0331
0332 Hardware interrupt handling
0333 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0334
0335 ::
0336
0337 irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
0338 void (*irq_clear) (struct ata_port *);
0339
0340
0341 ``->irq_handler`` is the interrupt handling routine registered with the
0342 system, by libata. ``->irq_clear`` is called during probe just before the
0343 interrupt handler is registered, to be sure hardware is quiet.
0344
0345 The second argument, dev_instance, should be cast to a pointer to
0346 :c:type:`struct ata_host_set <ata_host_set>`.
0347
0348 Most legacy IDE drivers use :c:func:`ata_sff_interrupt` for the irq_handler
0349 hook, which scans all ports in the host_set, determines which queued
0350 command was active (if any), and calls ata_sff_host_intr(ap,qc).
0351
0352 Most legacy IDE drivers use :c:func:`ata_sff_irq_clear` for the
0353 :c:func:`irq_clear` hook, which simply clears the interrupt and error flags
0354 in the DMA status register.
0355
0356 SATA phy read/write
0357 ~~~~~~~~~~~~~~~~~~~
0358
0359 ::
0360
0361 int (*scr_read) (struct ata_port *ap, unsigned int sc_reg,
0362 u32 *val);
0363 int (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
0364 u32 val);
0365
0366
0367 Read and write standard SATA phy registers. Currently only used if
0368 ``->phy_reset`` hook called the :c:func:`sata_phy_reset` helper function.
0369 sc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
0370
0371 Init and shutdown
0372 ~~~~~~~~~~~~~~~~~
0373
0374 ::
0375
0376 int (*port_start) (struct ata_port *ap);
0377 void (*port_stop) (struct ata_port *ap);
0378 void (*host_stop) (struct ata_host_set *host_set);
0379
0380
0381 ``->port_start()`` is called just after the data structures for each port
0382 are initialized. Typically this is used to alloc per-port DMA buffers /
0383 tables / rings, enable DMA engines, and similar tasks. Some drivers also
0384 use this entry point as a chance to allocate driver-private memory for
0385 ``ap->private_data``.
0386
0387 Many drivers use :c:func:`ata_port_start` as this hook or call it from their
0388 own :c:func:`port_start` hooks. :c:func:`ata_port_start` allocates space for
0389 a legacy IDE PRD table and returns.
0390
0391 ``->port_stop()`` is called after ``->host_stop()``. Its sole function is to
0392 release DMA/memory resources, now that they are no longer actively being
0393 used. Many drivers also free driver-private data from port at this time.
0394
0395 ``->host_stop()`` is called after all ``->port_stop()`` calls have completed.
0396 The hook must finalize hardware shutdown, release DMA and other
0397 resources, etc. This hook may be specified as NULL, in which case it is
0398 not called.
0399
0400 Error handling
0401 ==============
0402
0403 This chapter describes how errors are handled under libata. Readers are
0404 advised to read SCSI EH (Documentation/scsi/scsi_eh.rst) and ATA
0405 exceptions doc first.
0406
0407 Origins of commands
0408 -------------------
0409
0410 In libata, a command is represented with
0411 :c:type:`struct ata_queued_cmd <ata_queued_cmd>` or qc.
0412 qc's are preallocated during port initialization and repetitively used
0413 for command executions. Currently only one qc is allocated per port but
0414 yet-to-be-merged NCQ branch allocates one for each tag and maps each qc
0415 to NCQ tag 1-to-1.
0416
0417 libata commands can originate from two sources - libata itself and SCSI
0418 midlayer. libata internal commands are used for initialization and error
0419 handling. All normal blk requests and commands for SCSI emulation are
0420 passed as SCSI commands through queuecommand callback of SCSI host
0421 template.
0422
0423 How commands are issued
0424 -----------------------
0425
0426 Internal commands
0427 Once allocated qc's taskfile is initialized for the command to be
0428 executed. qc currently has two mechanisms to notify completion. One
0429 is via ``qc->complete_fn()`` callback and the other is completion
0430 ``qc->waiting``. ``qc->complete_fn()`` callback is the asynchronous path
0431 used by normal SCSI translated commands and ``qc->waiting`` is the
0432 synchronous (issuer sleeps in process context) path used by internal
0433 commands.
0434
0435 Once initialization is complete, host_set lock is acquired and the
0436 qc is issued.
0437
0438 SCSI commands
0439 All libata drivers use :c:func:`ata_scsi_queuecmd` as
0440 ``hostt->queuecommand`` callback. scmds can either be simulated or
0441 translated. No qc is involved in processing a simulated scmd. The
0442 result is computed right away and the scmd is completed.
0443
0444 ``qc->complete_fn()`` callback is used for completion notification. ATA
0445 commands use :c:func:`ata_scsi_qc_complete` while ATAPI commands use
0446 :c:func:`atapi_qc_complete`. Both functions end up calling ``qc->scsidone``
0447 to notify upper layer when the qc is finished. After translation is
0448 completed, the qc is issued with :c:func:`ata_qc_issue`.
0449
0450 Note that SCSI midlayer invokes hostt->queuecommand while holding
0451 host_set lock, so all above occur while holding host_set lock.
0452
0453 How commands are processed
0454 --------------------------
0455
0456 Depending on which protocol and which controller are used, commands are
0457 processed differently. For the purpose of discussion, a controller which
0458 uses taskfile interface and all standard callbacks is assumed.
0459
0460 Currently 6 ATA command protocols are used. They can be sorted into the
0461 following four categories according to how they are processed.
0462
0463 ATA NO DATA or DMA
0464 ATA_PROT_NODATA and ATA_PROT_DMA fall into this category. These
0465 types of commands don't require any software intervention once
0466 issued. Device will raise interrupt on completion.
0467
0468 ATA PIO
0469 ATA_PROT_PIO is in this category. libata currently implements PIO
0470 with polling. ATA_NIEN bit is set to turn off interrupt and
0471 pio_task on ata_wq performs polling and IO.
0472
0473 ATAPI NODATA or DMA
0474 ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
0475 category. packet_task is used to poll BSY bit after issuing PACKET
0476 command. Once BSY is turned off by the device, packet_task
0477 transfers CDB and hands off processing to interrupt handler.
0478
0479 ATAPI PIO
0480 ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set and, as
0481 in ATAPI NODATA or DMA, packet_task submits cdb. However, after
0482 submitting cdb, further processing (data transfer) is handed off to
0483 pio_task.
0484
0485 How commands are completed
0486 --------------------------
0487
0488 Once issued, all qc's are either completed with :c:func:`ata_qc_complete` or
0489 time out. For commands which are handled by interrupts,
0490 :c:func:`ata_host_intr` invokes :c:func:`ata_qc_complete`, and, for PIO tasks,
0491 pio_task invokes :c:func:`ata_qc_complete`. In error cases, packet_task may
0492 also complete commands.
0493
0494 :c:func:`ata_qc_complete` does the following.
0495
0496 1. DMA memory is unmapped.
0497
0498 2. ATA_QCFLAG_ACTIVE is cleared from qc->flags.
0499
0500 3. :c:expr:`qc->complete_fn` callback is invoked. If the return value of the
0501 callback is not zero. Completion is short circuited and
0502 :c:func:`ata_qc_complete` returns.
0503
0504 4. :c:func:`__ata_qc_complete` is called, which does
0505
0506 1. ``qc->flags`` is cleared to zero.
0507
0508 2. ``ap->active_tag`` and ``qc->tag`` are poisoned.
0509
0510 3. ``qc->waiting`` is cleared & completed (in that order).
0511
0512 4. qc is deallocated by clearing appropriate bit in ``ap->qactive``.
0513
0514 So, it basically notifies upper layer and deallocates qc. One exception
0515 is short-circuit path in #3 which is used by :c:func:`atapi_qc_complete`.
0516
0517 For all non-ATAPI commands, whether it fails or not, almost the same
0518 code path is taken and very little error handling takes place. A qc is
0519 completed with success status if it succeeded, with failed status
0520 otherwise.
0521
0522 However, failed ATAPI commands require more handling as REQUEST SENSE is
0523 needed to acquire sense data. If an ATAPI command fails,
0524 :c:func:`ata_qc_complete` is invoked with error status, which in turn invokes
0525 :c:func:`atapi_qc_complete` via ``qc->complete_fn()`` callback.
0526
0527 This makes :c:func:`atapi_qc_complete` set ``scmd->result`` to
0528 SAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As the
0529 sense data is empty but ``scmd->result`` is CHECK CONDITION, SCSI midlayer
0530 will invoke EH for the scmd, and returning 1 makes :c:func:`ata_qc_complete`
0531 to return without deallocating the qc. This leads us to
0532 :c:func:`ata_scsi_error` with partially completed qc.
0533
0534 :c:func:`ata_scsi_error`
0535 ------------------------
0536
0537 :c:func:`ata_scsi_error` is the current ``transportt->eh_strategy_handler()``
0538 for libata. As discussed above, this will be entered in two cases -
0539 timeout and ATAPI error completion. This function calls low level libata
0540 driver's :c:func:`eng_timeout` callback, the standard callback for which is
0541 :c:func:`ata_eng_timeout`. It checks if a qc is active and calls
0542 :c:func:`ata_qc_timeout` on the qc if so. Actual error handling occurs in
0543 :c:func:`ata_qc_timeout`.
0544
0545 If EH is invoked for timeout, :c:func:`ata_qc_timeout` stops BMDMA and
0546 completes the qc. Note that as we're currently in EH, we cannot call
0547 scsi_done. As described in SCSI EH doc, a recovered scmd should be
0548 either retried with :c:func:`scsi_queue_insert` or finished with
0549 :c:func:`scsi_finish_command`. Here, we override ``qc->scsidone`` with
0550 :c:func:`scsi_finish_command` and calls :c:func:`ata_qc_complete`.
0551
0552 If EH is invoked due to a failed ATAPI qc, the qc here is completed but
0553 not deallocated. The purpose of this half-completion is to use the qc as
0554 place holder to make EH code reach this place. This is a bit hackish,
0555 but it works.
0556
0557 Once control reaches here, the qc is deallocated by invoking
0558 :c:func:`__ata_qc_complete` explicitly. Then, internal qc for REQUEST SENSE
0559 is issued. Once sense data is acquired, scmd is finished by directly
0560 invoking :c:func:`scsi_finish_command` on the scmd. Note that as we already
0561 have completed and deallocated the qc which was associated with the
0562 scmd, we don't need to/cannot call :c:func:`ata_qc_complete` again.
0563
0564 Problems with the current EH
0565 ----------------------------
0566
0567 - Error representation is too crude. Currently any and all error
0568 conditions are represented with ATA STATUS and ERROR registers.
0569 Errors which aren't ATA device errors are treated as ATA device
0570 errors by setting ATA_ERR bit. Better error descriptor which can
0571 properly represent ATA and other errors/exceptions is needed.
0572
0573 - When handling timeouts, no action is taken to make device forget
0574 about the timed out command and ready for new commands.
0575
0576 - EH handling via :c:func:`ata_scsi_error` is not properly protected from
0577 usual command processing. On EH entrance, the device is not in
0578 quiescent state. Timed out commands may succeed or fail any time.
0579 pio_task and atapi_task may still be running.
0580
0581 - Too weak error recovery. Devices / controllers causing HSM mismatch
0582 errors and other errors quite often require reset to return to known
0583 state. Also, advanced error handling is necessary to support features
0584 like NCQ and hotplug.
0585
0586 - ATA errors are directly handled in the interrupt handler and PIO
0587 errors in pio_task. This is problematic for advanced error handling
0588 for the following reasons.
0589
0590 First, advanced error handling often requires context and internal qc
0591 execution.
0592
0593 Second, even a simple failure (say, CRC error) needs information
0594 gathering and could trigger complex error handling (say, resetting &
0595 reconfiguring). Having multiple code paths to gather information,
0596 enter EH and trigger actions makes life painful.
0597
0598 Third, scattered EH code makes implementing low level drivers
0599 difficult. Low level drivers override libata callbacks. If EH is
0600 scattered over several places, each affected callbacks should perform
0601 its part of error handling. This can be error prone and painful.
0602
0603 libata Library
0604 ==============
0605
0606 .. kernel-doc:: drivers/ata/libata-core.c
0607 :export:
0608
0609 libata Core Internals
0610 =====================
0611
0612 .. kernel-doc:: drivers/ata/libata-core.c
0613 :internal:
0614
0615 .. kernel-doc:: drivers/ata/libata-eh.c
0616
0617 libata SCSI translation/emulation
0618 =================================
0619
0620 .. kernel-doc:: drivers/ata/libata-scsi.c
0621 :export:
0622
0623 .. kernel-doc:: drivers/ata/libata-scsi.c
0624 :internal:
0625
0626 ATA errors and exceptions
0627 =========================
0628
0629 This chapter tries to identify what error/exception conditions exist for
0630 ATA/ATAPI devices and describe how they should be handled in
0631 implementation-neutral way.
0632
0633 The term 'error' is used to describe conditions where either an explicit
0634 error condition is reported from device or a command has timed out.
0635
0636 The term 'exception' is either used to describe exceptional conditions
0637 which are not errors (say, power or hotplug events), or to describe both
0638 errors and non-error exceptional conditions. Where explicit distinction
0639 between error and exception is necessary, the term 'non-error exception'
0640 is used.
0641
0642 Exception categories
0643 --------------------
0644
0645 Exceptions are described primarily with respect to legacy taskfile + bus
0646 master IDE interface. If a controller provides other better mechanism
0647 for error reporting, mapping those into categories described below
0648 shouldn't be difficult.
0649
0650 In the following sections, two recovery actions - reset and
0651 reconfiguring transport - are mentioned. These are described further in
0652 `EH recovery actions <#exrec>`__.
0653
0654 HSM violation
0655 ~~~~~~~~~~~~~
0656
0657 This error is indicated when STATUS value doesn't match HSM requirement
0658 during issuing or execution any ATA/ATAPI command.
0659
0660 - ATA_STATUS doesn't contain !BSY && DRDY && !DRQ while trying to
0661 issue a command.
0662
0663 - !BSY && !DRQ during PIO data transfer.
0664
0665 - DRQ on command completion.
0666
0667 - !BSY && ERR after CDB transfer starts but before the last byte of CDB
0668 is transferred. ATA/ATAPI standard states that "The device shall not
0669 terminate the PACKET command with an error before the last byte of
0670 the command packet has been written" in the error outputs description
0671 of PACKET command and the state diagram doesn't include such
0672 transitions.
0673
0674 In these cases, HSM is violated and not much information regarding the
0675 error can be acquired from STATUS or ERROR register. IOW, this error can
0676 be anything - driver bug, faulty device, controller and/or cable.
0677
0678 As HSM is violated, reset is necessary to restore known state.
0679 Reconfiguring transport for lower speed might be helpful too as
0680 transmission errors sometimes cause this kind of errors.
0681
0682 ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION)
0683 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0684
0685 These are errors detected and reported by ATA/ATAPI devices indicating
0686 device problems. For this type of errors, STATUS and ERROR register
0687 values are valid and describe error condition. Note that some of ATA bus
0688 errors are detected by ATA/ATAPI devices and reported using the same
0689 mechanism as device errors. Those cases are described later in this
0690 section.
0691
0692 For ATA commands, this type of errors are indicated by !BSY && ERR
0693 during command execution and on completion.
0694
0695 For ATAPI commands,
0696
0697 - !BSY && ERR && ABRT right after issuing PACKET indicates that PACKET
0698 command is not supported and falls in this category.
0699
0700 - !BSY && ERR(==CHK) && !ABRT after the last byte of CDB is transferred
0701 indicates CHECK CONDITION and doesn't fall in this category.
0702
0703 - !BSY && ERR(==CHK) && ABRT after the last byte of CDB is transferred
0704 \*probably\* indicates CHECK CONDITION and doesn't fall in this
0705 category.
0706
0707 Of errors detected as above, the following are not ATA/ATAPI device
0708 errors but ATA bus errors and should be handled according to
0709 `ATA bus error <#excatATAbusErr>`__.
0710
0711 CRC error during data transfer
0712 This is indicated by ICRC bit in the ERROR register and means that
0713 corruption occurred during data transfer. Up to ATA/ATAPI-7, the
0714 standard specifies that this bit is only applicable to UDMA
0715 transfers but ATA/ATAPI-8 draft revision 1f says that the bit may be
0716 applicable to multiword DMA and PIO.
0717
0718 ABRT error during data transfer or on completion
0719 Up to ATA/ATAPI-7, the standard specifies that ABRT could be set on
0720 ICRC errors and on cases where a device is not able to complete a
0721 command. Combined with the fact that MWDMA and PIO transfer errors
0722 aren't allowed to use ICRC bit up to ATA/ATAPI-7, it seems to imply
0723 that ABRT bit alone could indicate transfer errors.
0724
0725 However, ATA/ATAPI-8 draft revision 1f removes the part that ICRC
0726 errors can turn on ABRT. So, this is kind of gray area. Some
0727 heuristics are needed here.
0728
0729 ATA/ATAPI device errors can be further categorized as follows.
0730
0731 Media errors
0732 This is indicated by UNC bit in the ERROR register. ATA devices
0733 reports UNC error only after certain number of retries cannot
0734 recover the data, so there's nothing much else to do other than
0735 notifying upper layer.
0736
0737 READ and WRITE commands report CHS or LBA of the first failed sector
0738 but ATA/ATAPI standard specifies that the amount of transferred data
0739 on error completion is indeterminate, so we cannot assume that
0740 sectors preceding the failed sector have been transferred and thus
0741 cannot complete those sectors successfully as SCSI does.
0742
0743 Media changed / media change requested error
0744 <<TODO: fill here>>
0745
0746 Address error
0747 This is indicated by IDNF bit in the ERROR register. Report to upper
0748 layer.
0749
0750 Other errors
0751 This can be invalid command or parameter indicated by ABRT ERROR bit
0752 or some other error condition. Note that ABRT bit can indicate a lot
0753 of things including ICRC and Address errors. Heuristics needed.
0754
0755 Depending on commands, not all STATUS/ERROR bits are applicable. These
0756 non-applicable bits are marked with "na" in the output descriptions but
0757 up to ATA/ATAPI-7 no definition of "na" can be found. However,
0758 ATA/ATAPI-8 draft revision 1f describes "N/A" as follows.
0759
0760 3.2.3.3a N/A
0761 A keyword the indicates a field has no defined value in this
0762 standard and should not be checked by the host or device. N/A
0763 fields should be cleared to zero.
0764
0765 So, it seems reasonable to assume that "na" bits are cleared to zero by
0766 devices and thus need no explicit masking.
0767
0768 ATAPI device CHECK CONDITION
0769 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0770
0771 ATAPI device CHECK CONDITION error is indicated by set CHK bit (ERR bit)
0772 in the STATUS register after the last byte of CDB is transferred for a
0773 PACKET command. For this kind of errors, sense data should be acquired
0774 to gather information regarding the errors. REQUEST SENSE packet command
0775 should be used to acquire sense data.
0776
0777 Once sense data is acquired, this type of errors can be handled
0778 similarly to other SCSI errors. Note that sense data may indicate ATA
0779 bus error (e.g. Sense Key 04h HARDWARE ERROR && ASC/ASCQ 47h/00h SCSI
0780 PARITY ERROR). In such cases, the error should be considered as an ATA
0781 bus error and handled according to `ATA bus error <#excatATAbusErr>`__.
0782
0783 ATA device error (NCQ)
0784 ~~~~~~~~~~~~~~~~~~~~~~
0785
0786 NCQ command error is indicated by cleared BSY and set ERR bit during NCQ
0787 command phase (one or more NCQ commands outstanding). Although STATUS
0788 and ERROR registers will contain valid values describing the error, READ
0789 LOG EXT is required to clear the error condition, determine which
0790 command has failed and acquire more information.
0791
0792 READ LOG EXT Log Page 10h reports which tag has failed and taskfile
0793 register values describing the error. With this information the failed
0794 command can be handled as a normal ATA command error as in
0795 `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__
0796 and all other in-flight commands must be retried. Note that this retry
0797 should not be counted - it's likely that commands retried this way would
0798 have completed normally if it were not for the failed command.
0799
0800 Note that ATA bus errors can be reported as ATA device NCQ errors. This
0801 should be handled as described in `ATA bus error <#excatATAbusErr>`__.
0802
0803 If READ LOG EXT Log Page 10h fails or reports NQ, we're thoroughly
0804 screwed. This condition should be treated according to
0805 `HSM violation <#excatHSMviolation>`__.
0806
0807 ATA bus error
0808 ~~~~~~~~~~~~~
0809
0810 ATA bus error means that data corruption occurred during transmission
0811 over ATA bus (SATA or PATA). This type of errors can be indicated by
0812
0813 - ICRC or ABRT error as described in
0814 `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__.
0815
0816 - Controller-specific error completion with error information
0817 indicating transmission error.
0818
0819 - On some controllers, command timeout. In this case, there may be a
0820 mechanism to determine that the timeout is due to transmission error.
0821
0822 - Unknown/random errors, timeouts and all sorts of weirdities.
0823
0824 As described above, transmission errors can cause wide variety of
0825 symptoms ranging from device ICRC error to random device lockup, and,
0826 for many cases, there is no way to tell if an error condition is due to
0827 transmission error or not; therefore, it's necessary to employ some kind
0828 of heuristic when dealing with errors and timeouts. For example,
0829 encountering repetitive ABRT errors for known supported command is
0830 likely to indicate ATA bus error.
0831
0832 Once it's determined that ATA bus errors have possibly occurred,
0833 lowering ATA bus transmission speed is one of actions which may
0834 alleviate the problem. See `Reconfigure transport <#exrecReconf>`__ for
0835 more information.
0836
0837 PCI bus error
0838 ~~~~~~~~~~~~~
0839
0840 Data corruption or other failures during transmission over PCI (or other
0841 system bus). For standard BMDMA, this is indicated by Error bit in the
0842 BMDMA Status register. This type of errors must be logged as it
0843 indicates something is very wrong with the system. Resetting host
0844 controller is recommended.
0845
0846 Late completion
0847 ~~~~~~~~~~~~~~~
0848
0849 This occurs when timeout occurs and the timeout handler finds out that
0850 the timed out command has completed successfully or with error. This is
0851 usually caused by lost interrupts. This type of errors must be logged.
0852 Resetting host controller is recommended.
0853
0854 Unknown error (timeout)
0855 ~~~~~~~~~~~~~~~~~~~~~~~
0856
0857 This is when timeout occurs and the command is still processing or the
0858 host and device are in unknown state. When this occurs, HSM could be in
0859 any valid or invalid state. To bring the device to known state and make
0860 it forget about the timed out command, resetting is necessary. The timed
0861 out command may be retried.
0862
0863 Timeouts can also be caused by transmission errors. Refer to
0864 `ATA bus error <#excatATAbusErr>`__ for more details.
0865
0866 Hotplug and power management exceptions
0867 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0868
0869 <<TODO: fill here>>
0870
0871 EH recovery actions
0872 -------------------
0873
0874 This section discusses several important recovery actions.
0875
0876 Clearing error condition
0877 ~~~~~~~~~~~~~~~~~~~~~~~~
0878
0879 Many controllers require its error registers to be cleared by error
0880 handler. Different controllers may have different requirements.
0881
0882 For SATA, it's strongly recommended to clear at least SError register
0883 during error handling.
0884
0885 Reset
0886 ~~~~~
0887
0888 During EH, resetting is necessary in the following cases.
0889
0890 - HSM is in unknown or invalid state
0891
0892 - HBA is in unknown or invalid state
0893
0894 - EH needs to make HBA/device forget about in-flight commands
0895
0896 - HBA/device behaves weirdly
0897
0898 Resetting during EH might be a good idea regardless of error condition
0899 to improve EH robustness. Whether to reset both or either one of HBA and
0900 device depends on situation but the following scheme is recommended.
0901
0902 - When it's known that HBA is in ready state but ATA/ATAPI device is in
0903 unknown state, reset only device.
0904
0905 - If HBA is in unknown state, reset both HBA and device.
0906
0907 HBA resetting is implementation specific. For a controller complying to
0908 taskfile/BMDMA PCI IDE, stopping active DMA transaction may be
0909 sufficient iff BMDMA state is the only HBA context. But even mostly
0910 taskfile/BMDMA PCI IDE complying controllers may have implementation
0911 specific requirements and mechanism to reset themselves. This must be
0912 addressed by specific drivers.
0913
0914 OTOH, ATA/ATAPI standard describes in detail ways to reset ATA/ATAPI
0915 devices.
0916
0917 PATA hardware reset
0918 This is hardware initiated device reset signalled with asserted PATA
0919 RESET- signal. There is no standard way to initiate hardware reset
0920 from software although some hardware provides registers that allow
0921 driver to directly tweak the RESET- signal.
0922
0923 Software reset
0924 This is achieved by turning CONTROL SRST bit on for at least 5us.
0925 Both PATA and SATA support it but, in case of SATA, this may require
0926 controller-specific support as the second Register FIS to clear SRST
0927 should be transmitted while BSY bit is still set. Note that on PATA,
0928 this resets both master and slave devices on a channel.
0929
0930 EXECUTE DEVICE DIAGNOSTIC command
0931 Although ATA/ATAPI standard doesn't describe exactly, EDD implies
0932 some level of resetting, possibly similar level with software reset.
0933 Host-side EDD protocol can be handled with normal command processing
0934 and most SATA controllers should be able to handle EDD's just like
0935 other commands. As in software reset, EDD affects both devices on a
0936 PATA bus.
0937
0938 Although EDD does reset devices, this doesn't suit error handling as
0939 EDD cannot be issued while BSY is set and it's unclear how it will
0940 act when device is in unknown/weird state.
0941
0942 ATAPI DEVICE RESET command
0943 This is very similar to software reset except that reset can be
0944 restricted to the selected device without affecting the other device
0945 sharing the cable.
0946
0947 SATA phy reset
0948 This is the preferred way of resetting a SATA device. In effect,
0949 it's identical to PATA hardware reset. Note that this can be done
0950 with the standard SCR Control register. As such, it's usually easier
0951 to implement than software reset.
0952
0953 One more thing to consider when resetting devices is that resetting
0954 clears certain configuration parameters and they need to be set to their
0955 previous or newly adjusted values after reset.
0956
0957 Parameters affected are.
0958
0959 - CHS set up with INITIALIZE DEVICE PARAMETERS (seldom used)
0960
0961 - Parameters set with SET FEATURES including transfer mode setting
0962
0963 - Block count set with SET MULTIPLE MODE
0964
0965 - Other parameters (SET MAX, MEDIA LOCK...)
0966
0967 ATA/ATAPI standard specifies that some parameters must be maintained
0968 across hardware or software reset, but doesn't strictly specify all of
0969 them. Always reconfiguring needed parameters after reset is required for
0970 robustness. Note that this also applies when resuming from deep sleep
0971 (power-off).
0972
0973 Also, ATA/ATAPI standard requires that IDENTIFY DEVICE / IDENTIFY PACKET
0974 DEVICE is issued after any configuration parameter is updated or a
0975 hardware reset and the result used for further operation. OS driver is
0976 required to implement revalidation mechanism to support this.
0977
0978 Reconfigure transport
0979 ~~~~~~~~~~~~~~~~~~~~~
0980
0981 For both PATA and SATA, a lot of corners are cut for cheap connectors,
0982 cables or controllers and it's quite common to see high transmission
0983 error rate. This can be mitigated by lowering transmission speed.
0984
0985 The following is a possible scheme Jeff Garzik suggested.
0986
0987 If more than $N (3?) transmission errors happen in 15 minutes,
0988
0989 - if SATA, decrease SATA PHY speed. if speed cannot be decreased,
0990
0991 - decrease UDMA xfer speed. if at UDMA0, switch to PIO4,
0992
0993 - decrease PIO xfer speed. if at PIO3, complain, but continue
0994
0995 ata_piix Internals
0996 ===================
0997
0998 .. kernel-doc:: drivers/ata/ata_piix.c
0999 :internal:
1000
1001 sata_sil Internals
1002 ===================
1003
1004 .. kernel-doc:: drivers/ata/sata_sil.c
1005 :internal:
1006
1007 Thanks
1008 ======
1009
1010 The bulk of the ATA knowledge comes thanks to long conversations with
1011 Andre Hedrick (www.linux-ide.org), and long hours pondering the ATA and
1012 SCSI specifications.
1013
1014 Thanks to Alan Cox for pointing out similarities between SATA and SCSI,
1015 and in general for motivation to hack on libata.
1016
1017 libata's device detection method, ata_pio_devchk, and in general all
1018 the early probing was based on extensive study of Hale Landis's
1019 probe/reset code in his ATADRVR driver (www.ata-atapi.com).