162306a36Sopenharmony_ci========================
262306a36Sopenharmony_cilibATA Developer's Guide
362306a36Sopenharmony_ci========================
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci:Author: Jeff Garzik
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciIntroduction
862306a36Sopenharmony_ci============
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cilibATA is a library used inside the Linux kernel to support ATA host
1162306a36Sopenharmony_cicontrollers and devices. libATA provides an ATA driver API, class
1262306a36Sopenharmony_citransports for ATA and ATAPI devices, and SCSI<->ATA translation for ATA
1362306a36Sopenharmony_cidevices according to the T10 SAT specification.
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciThis Guide documents the libATA driver API, library functions, library
1662306a36Sopenharmony_ciinternals, and a couple sample ATA low-level drivers.
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cilibata Driver API
1962306a36Sopenharmony_ci=================
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci:c:type:`struct ata_port_operations <ata_port_operations>`
2262306a36Sopenharmony_ciis defined for every low-level libata
2362306a36Sopenharmony_cihardware driver, and it controls how the low-level driver interfaces
2462306a36Sopenharmony_ciwith the ATA and SCSI layers.
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ciFIS-based drivers will hook into the system with ``->qc_prep()`` and
2762306a36Sopenharmony_ci``->qc_issue()`` high-level hooks. Hardware which behaves in a manner
2862306a36Sopenharmony_cisimilar to PCI IDE hardware may utilize several generic helpers,
2962306a36Sopenharmony_cidefining at a bare minimum the bus I/O addresses of the ATA shadow
3062306a36Sopenharmony_ciregister blocks.
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci:c:type:`struct ata_port_operations <ata_port_operations>`
3362306a36Sopenharmony_ci----------------------------------------------------------
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ciPost-IDENTIFY device configuration
3662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci::
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci    void (*dev_config) (struct ata_port *, struct ata_device *);
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ciCalled after IDENTIFY [PACKET] DEVICE is issued to each device found.
4462306a36Sopenharmony_ciTypically used to apply device-specific fixups prior to issue of SET
4562306a36Sopenharmony_ciFEATURES - XFER MODE, and prior to operation.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciThis entry may be specified as NULL in ata_port_operations.
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciSet PIO/DMA mode
5062306a36Sopenharmony_ci~~~~~~~~~~~~~~~~
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci::
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci    void (*set_piomode) (struct ata_port *, struct ata_device *);
5562306a36Sopenharmony_ci    void (*set_dmamode) (struct ata_port *, struct ata_device *);
5662306a36Sopenharmony_ci    void (*post_set_mode) (struct ata_port *);
5762306a36Sopenharmony_ci    unsigned int (*mode_filter) (struct ata_port *, struct ata_device *, unsigned int);
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ciHooks called prior to the issue of SET FEATURES - XFER MODE command. The
6162306a36Sopenharmony_cioptional ``->mode_filter()`` hook is called when libata has built a mask of
6262306a36Sopenharmony_cithe possible modes. This is passed to the ``->mode_filter()`` function
6362306a36Sopenharmony_ciwhich should return a mask of valid modes after filtering those
6462306a36Sopenharmony_ciunsuitable due to hardware limits. It is not valid to use this interface
6562306a36Sopenharmony_cito add modes.
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci``dev->pio_mode`` and ``dev->dma_mode`` are guaranteed to be valid when
6862306a36Sopenharmony_ci``->set_piomode()`` and when ``->set_dmamode()`` is called. The timings for
6962306a36Sopenharmony_ciany other drive sharing the cable will also be valid at this point. That
7062306a36Sopenharmony_ciis the library records the decisions for the modes of each drive on a
7162306a36Sopenharmony_cichannel before it attempts to set any of them.
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci``->post_set_mode()`` is called unconditionally, after the SET FEATURES -
7462306a36Sopenharmony_ciXFER MODE command completes successfully.
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci``->set_piomode()`` is always called (if present), but ``->set_dma_mode()``
7762306a36Sopenharmony_ciis only called if DMA is possible.
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ciTaskfile read/write
8062306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci::
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci    void (*sff_tf_load) (struct ata_port *ap, struct ata_taskfile *tf);
8562306a36Sopenharmony_ci    void (*sff_tf_read) (struct ata_port *ap, struct ata_taskfile *tf);
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci``->tf_load()`` is called to load the given taskfile into hardware
8962306a36Sopenharmony_ciregisters / DMA buffers. ``->tf_read()`` is called to read the hardware
9062306a36Sopenharmony_ciregisters / DMA buffers, to obtain the current set of taskfile register
9162306a36Sopenharmony_civalues. Most drivers for taskfile-based hardware (PIO or MMIO) use
9262306a36Sopenharmony_ci:c:func:`ata_sff_tf_load` and :c:func:`ata_sff_tf_read` for these hooks.
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ciPIO data read/write
9562306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci::
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci    void (*sff_data_xfer) (struct ata_device *, unsigned char *, unsigned int, int);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ciAll bmdma-style drivers must implement this hook. This is the low-level
10362306a36Sopenharmony_cioperation that actually copies the data bytes during a PIO data
10462306a36Sopenharmony_citransfer. Typically the driver will choose one of
10562306a36Sopenharmony_ci:c:func:`ata_sff_data_xfer`, or :c:func:`ata_sff_data_xfer32`.
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ciATA command execute
10862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci::
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci    void (*sff_exec_command)(struct ata_port *ap, struct ata_taskfile *tf);
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_cicauses an ATA command, previously loaded with ``->tf_load()``, to be
11662306a36Sopenharmony_ciinitiated in hardware. Most drivers for taskfile-based hardware use
11762306a36Sopenharmony_ci:c:func:`ata_sff_exec_command` for this hook.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciPer-cmd ATAPI DMA capabilities filter
12062306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci::
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci    int (*check_atapi_dma) (struct ata_queued_cmd *qc);
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciAllow low-level driver to filter ATA PACKET commands, returning a status
12862306a36Sopenharmony_ciindicating whether or not it is OK to use DMA for the supplied PACKET
12962306a36Sopenharmony_cicommand.
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ciThis hook may be specified as NULL, in which case libata will assume
13262306a36Sopenharmony_cithat atapi dma can be supported.
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciRead specific ATA shadow registers
13562306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13662306a36Sopenharmony_ci
13762306a36Sopenharmony_ci::
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci    u8   (*sff_check_status)(struct ata_port *ap);
14062306a36Sopenharmony_ci    u8   (*sff_check_altstatus)(struct ata_port *ap);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciReads the Status/AltStatus ATA shadow register from hardware. On some
14462306a36Sopenharmony_cihardware, reading the Status register has the side effect of clearing
14562306a36Sopenharmony_cithe interrupt condition. Most drivers for taskfile-based hardware use
14662306a36Sopenharmony_ci:c:func:`ata_sff_check_status` for this hook.
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ciWrite specific ATA shadow register
14962306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci::
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci    void (*sff_set_devctl)(struct ata_port *ap, u8 ctl);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ciWrite the device control ATA shadow register to the hardware. Most
15762306a36Sopenharmony_cidrivers don't need to define this.
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_ciSelect ATA device on bus
16062306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci::
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci    void (*sff_dev_select)(struct ata_port *ap, unsigned int device);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ciIssues the low-level hardware command(s) that causes one of N hardware
16862306a36Sopenharmony_cidevices to be considered 'selected' (active and available for use) on
16962306a36Sopenharmony_cithe ATA bus. This generally has no meaning on FIS-based devices.
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ciMost drivers for taskfile-based hardware use :c:func:`ata_sff_dev_select` for
17262306a36Sopenharmony_cithis hook.
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciPrivate tuning method
17562306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci::
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci    void (*set_mode) (struct ata_port *ap);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciBy default libata performs drive and controller tuning in accordance
18362306a36Sopenharmony_ciwith the ATA timing rules and also applies blacklists and cable limits.
18462306a36Sopenharmony_ciSome controllers need special handling and have custom tuning rules,
18562306a36Sopenharmony_citypically raid controllers that use ATA commands but do not actually do
18662306a36Sopenharmony_cidrive timing.
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci    **Warning**
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci    This hook should not be used to replace the standard controller
19162306a36Sopenharmony_ci    tuning logic when a controller has quirks. Replacing the default
19262306a36Sopenharmony_ci    tuning logic in that case would bypass handling for drive and bridge
19362306a36Sopenharmony_ci    quirks that may be important to data reliability. If a controller
19462306a36Sopenharmony_ci    needs to filter the mode selection it should use the mode_filter
19562306a36Sopenharmony_ci    hook instead.
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ciControl PCI IDE BMDMA engine
19862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci::
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci    void (*bmdma_setup) (struct ata_queued_cmd *qc);
20362306a36Sopenharmony_ci    void (*bmdma_start) (struct ata_queued_cmd *qc);
20462306a36Sopenharmony_ci    void (*bmdma_stop) (struct ata_port *ap);
20562306a36Sopenharmony_ci    u8   (*bmdma_status) (struct ata_port *ap);
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciWhen setting up an IDE BMDMA transaction, these hooks arm
20962306a36Sopenharmony_ci(``->bmdma_setup``), fire (``->bmdma_start``), and halt (``->bmdma_stop``) the
21062306a36Sopenharmony_cihardware's DMA engine. ``->bmdma_status`` is used to read the standard PCI
21162306a36Sopenharmony_ciIDE DMA Status register.
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ciThese hooks are typically either no-ops, or simply not implemented, in
21462306a36Sopenharmony_ciFIS-based drivers.
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ciMost legacy IDE drivers use :c:func:`ata_bmdma_setup` for the
21762306a36Sopenharmony_ci:c:func:`bmdma_setup` hook. :c:func:`ata_bmdma_setup` will write the pointer
21862306a36Sopenharmony_cito the PRD table to the IDE PRD Table Address register, enable DMA in the DMA
21962306a36Sopenharmony_ciCommand register, and call :c:func:`exec_command` to begin the transfer.
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ciMost legacy IDE drivers use :c:func:`ata_bmdma_start` for the
22262306a36Sopenharmony_ci:c:func:`bmdma_start` hook. :c:func:`ata_bmdma_start` will write the
22362306a36Sopenharmony_ciATA_DMA_START flag to the DMA Command register.
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ciMany legacy IDE drivers use :c:func:`ata_bmdma_stop` for the
22662306a36Sopenharmony_ci:c:func:`bmdma_stop` hook. :c:func:`ata_bmdma_stop` clears the ATA_DMA_START
22762306a36Sopenharmony_ciflag in the DMA command register.
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ciMany legacy IDE drivers use :c:func:`ata_bmdma_status` as the
23062306a36Sopenharmony_ci:c:func:`bmdma_status` hook.
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ciHigh-level taskfile hooks
23362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_ci::
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ci    enum ata_completion_errors (*qc_prep) (struct ata_queued_cmd *qc);
23862306a36Sopenharmony_ci    int (*qc_issue) (struct ata_queued_cmd *qc);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ciHigher-level hooks, these two hooks can potentially supersede several of
24262306a36Sopenharmony_cithe above taskfile/DMA engine hooks. ``->qc_prep`` is called after the
24362306a36Sopenharmony_cibuffers have been DMA-mapped, and is typically used to populate the
24462306a36Sopenharmony_cihardware's DMA scatter-gather table. Some drivers use the standard
24562306a36Sopenharmony_ci:c:func:`ata_bmdma_qc_prep` and :c:func:`ata_bmdma_dumb_qc_prep` helper
24662306a36Sopenharmony_cifunctions, but more advanced drivers roll their own.
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci``->qc_issue`` is used to make a command active, once the hardware and S/G
24962306a36Sopenharmony_citables have been prepared. IDE BMDMA drivers use the helper function
25062306a36Sopenharmony_ci:c:func:`ata_sff_qc_issue` for taskfile protocol-based dispatch. More
25162306a36Sopenharmony_ciadvanced drivers implement their own ``->qc_issue``.
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci:c:func:`ata_sff_qc_issue` calls ``->sff_tf_load()``, ``->bmdma_setup()``, and
25462306a36Sopenharmony_ci``->bmdma_start()`` as necessary to initiate a transfer.
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ciException and probe handling (EH)
25762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci::
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci    void (*freeze) (struct ata_port *ap);
26262306a36Sopenharmony_ci    void (*thaw) (struct ata_port *ap);
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci:c:func:`ata_port_freeze` is called when HSM violations or some other
26662306a36Sopenharmony_cicondition disrupts normal operation of the port. A frozen port is not
26762306a36Sopenharmony_ciallowed to perform any operation until the port is thawed, which usually
26862306a36Sopenharmony_cifollows a successful reset.
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ciThe optional ``->freeze()`` callback can be used for freezing the port
27162306a36Sopenharmony_cihardware-wise (e.g. mask interrupt and stop DMA engine). If a port
27262306a36Sopenharmony_cicannot be frozen hardware-wise, the interrupt handler must ack and clear
27362306a36Sopenharmony_ciinterrupts unconditionally while the port is frozen.
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ciThe optional ``->thaw()`` callback is called to perform the opposite of
27662306a36Sopenharmony_ci``->freeze()``: prepare the port for normal operation once again. Unmask
27762306a36Sopenharmony_ciinterrupts, start DMA engine, etc.
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ci::
28062306a36Sopenharmony_ci
28162306a36Sopenharmony_ci    void (*error_handler) (struct ata_port *ap);
28262306a36Sopenharmony_ci
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci``->error_handler()`` is a driver's hook into probe, hotplug, and recovery
28562306a36Sopenharmony_ciand other exceptional conditions. The primary responsibility of an
28662306a36Sopenharmony_ciimplementation is to call :c:func:`ata_do_eh` or :c:func:`ata_bmdma_drive_eh`
28762306a36Sopenharmony_ciwith a set of EH hooks as arguments:
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci'prereset' hook (may be NULL) is called during an EH reset, before any
29062306a36Sopenharmony_ciother actions are taken.
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci'postreset' hook (may be NULL) is called after the EH reset is
29362306a36Sopenharmony_ciperformed. Based on existing conditions, severity of the problem, and
29462306a36Sopenharmony_cihardware capabilities,
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ciEither 'softreset' (may be NULL) or 'hardreset' (may be NULL) will be
29762306a36Sopenharmony_cicalled to perform the low-level EH reset.
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci::
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci    void (*post_internal_cmd) (struct ata_queued_cmd *qc);
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ciPerform any hardware-specific actions necessary to finish processing
30562306a36Sopenharmony_ciafter executing a probe-time or EH-time command via
30662306a36Sopenharmony_ci:c:func:`ata_exec_internal`.
30762306a36Sopenharmony_ci
30862306a36Sopenharmony_ciHardware interrupt handling
30962306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci::
31262306a36Sopenharmony_ci
31362306a36Sopenharmony_ci    irqreturn_t (*irq_handler)(int, void *, struct pt_regs *);
31462306a36Sopenharmony_ci    void (*irq_clear) (struct ata_port *);
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci``->irq_handler`` is the interrupt handling routine registered with the
31862306a36Sopenharmony_cisystem, by libata. ``->irq_clear`` is called during probe just before the
31962306a36Sopenharmony_ciinterrupt handler is registered, to be sure hardware is quiet.
32062306a36Sopenharmony_ci
32162306a36Sopenharmony_ciThe second argument, dev_instance, should be cast to a pointer to
32262306a36Sopenharmony_ci:c:type:`struct ata_host_set <ata_host_set>`.
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ciMost legacy IDE drivers use :c:func:`ata_sff_interrupt` for the irq_handler
32562306a36Sopenharmony_cihook, which scans all ports in the host_set, determines which queued
32662306a36Sopenharmony_cicommand was active (if any), and calls ata_sff_host_intr(ap,qc).
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_ciMost legacy IDE drivers use :c:func:`ata_sff_irq_clear` for the
32962306a36Sopenharmony_ci:c:func:`irq_clear` hook, which simply clears the interrupt and error flags
33062306a36Sopenharmony_ciin the DMA status register.
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ciSATA phy read/write
33362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci::
33662306a36Sopenharmony_ci
33762306a36Sopenharmony_ci    int (*scr_read) (struct ata_port *ap, unsigned int sc_reg,
33862306a36Sopenharmony_ci             u32 *val);
33962306a36Sopenharmony_ci    int (*scr_write) (struct ata_port *ap, unsigned int sc_reg,
34062306a36Sopenharmony_ci                       u32 val);
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ciRead and write standard SATA phy registers.
34462306a36Sopenharmony_cisc_reg is one of SCR_STATUS, SCR_CONTROL, SCR_ERROR, or SCR_ACTIVE.
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ciInit and shutdown
34762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci::
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci    int (*port_start) (struct ata_port *ap);
35262306a36Sopenharmony_ci    void (*port_stop) (struct ata_port *ap);
35362306a36Sopenharmony_ci    void (*host_stop) (struct ata_host_set *host_set);
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_ci``->port_start()`` is called just after the data structures for each port
35762306a36Sopenharmony_ciare initialized. Typically this is used to alloc per-port DMA buffers /
35862306a36Sopenharmony_citables / rings, enable DMA engines, and similar tasks. Some drivers also
35962306a36Sopenharmony_ciuse this entry point as a chance to allocate driver-private memory for
36062306a36Sopenharmony_ci``ap->private_data``.
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ciMany drivers use :c:func:`ata_port_start` as this hook or call it from their
36362306a36Sopenharmony_ciown :c:func:`port_start` hooks. :c:func:`ata_port_start` allocates space for
36462306a36Sopenharmony_cia legacy IDE PRD table and returns.
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci``->port_stop()`` is called after ``->host_stop()``. Its sole function is to
36762306a36Sopenharmony_cirelease DMA/memory resources, now that they are no longer actively being
36862306a36Sopenharmony_ciused. Many drivers also free driver-private data from port at this time.
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci``->host_stop()`` is called after all ``->port_stop()`` calls have completed.
37162306a36Sopenharmony_ciThe hook must finalize hardware shutdown, release DMA and other
37262306a36Sopenharmony_ciresources, etc. This hook may be specified as NULL, in which case it is
37362306a36Sopenharmony_cinot called.
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ciError handling
37662306a36Sopenharmony_ci==============
37762306a36Sopenharmony_ci
37862306a36Sopenharmony_ciThis chapter describes how errors are handled under libata. Readers are
37962306a36Sopenharmony_ciadvised to read SCSI EH (Documentation/scsi/scsi_eh.rst) and ATA
38062306a36Sopenharmony_ciexceptions doc first.
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ciOrigins of commands
38362306a36Sopenharmony_ci-------------------
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ciIn libata, a command is represented with
38662306a36Sopenharmony_ci:c:type:`struct ata_queued_cmd <ata_queued_cmd>` or qc.
38762306a36Sopenharmony_ciqc's are preallocated during port initialization and repetitively used
38862306a36Sopenharmony_cifor command executions. Currently only one qc is allocated per port but
38962306a36Sopenharmony_ciyet-to-be-merged NCQ branch allocates one for each tag and maps each qc
39062306a36Sopenharmony_cito NCQ tag 1-to-1.
39162306a36Sopenharmony_ci
39262306a36Sopenharmony_cilibata commands can originate from two sources - libata itself and SCSI
39362306a36Sopenharmony_cimidlayer. libata internal commands are used for initialization and error
39462306a36Sopenharmony_cihandling. All normal blk requests and commands for SCSI emulation are
39562306a36Sopenharmony_cipassed as SCSI commands through queuecommand callback of SCSI host
39662306a36Sopenharmony_citemplate.
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_ciHow commands are issued
39962306a36Sopenharmony_ci-----------------------
40062306a36Sopenharmony_ci
40162306a36Sopenharmony_ciInternal commands
40262306a36Sopenharmony_ci    Once allocated qc's taskfile is initialized for the command to be
40362306a36Sopenharmony_ci    executed. qc currently has two mechanisms to notify completion. One
40462306a36Sopenharmony_ci    is via ``qc->complete_fn()`` callback and the other is completion
40562306a36Sopenharmony_ci    ``qc->waiting``. ``qc->complete_fn()`` callback is the asynchronous path
40662306a36Sopenharmony_ci    used by normal SCSI translated commands and ``qc->waiting`` is the
40762306a36Sopenharmony_ci    synchronous (issuer sleeps in process context) path used by internal
40862306a36Sopenharmony_ci    commands.
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci    Once initialization is complete, host_set lock is acquired and the
41162306a36Sopenharmony_ci    qc is issued.
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_ciSCSI commands
41462306a36Sopenharmony_ci    All libata drivers use :c:func:`ata_scsi_queuecmd` as
41562306a36Sopenharmony_ci    ``hostt->queuecommand`` callback. scmds can either be simulated or
41662306a36Sopenharmony_ci    translated. No qc is involved in processing a simulated scmd. The
41762306a36Sopenharmony_ci    result is computed right away and the scmd is completed.
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci    ``qc->complete_fn()`` callback is used for completion notification. ATA
42062306a36Sopenharmony_ci    commands use :c:func:`ata_scsi_qc_complete` while ATAPI commands use
42162306a36Sopenharmony_ci    :c:func:`atapi_qc_complete`. Both functions end up calling ``qc->scsidone``
42262306a36Sopenharmony_ci    to notify upper layer when the qc is finished. After translation is
42362306a36Sopenharmony_ci    completed, the qc is issued with :c:func:`ata_qc_issue`.
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci    Note that SCSI midlayer invokes hostt->queuecommand while holding
42662306a36Sopenharmony_ci    host_set lock, so all above occur while holding host_set lock.
42762306a36Sopenharmony_ci
42862306a36Sopenharmony_ciHow commands are processed
42962306a36Sopenharmony_ci--------------------------
43062306a36Sopenharmony_ci
43162306a36Sopenharmony_ciDepending on which protocol and which controller are used, commands are
43262306a36Sopenharmony_ciprocessed differently. For the purpose of discussion, a controller which
43362306a36Sopenharmony_ciuses taskfile interface and all standard callbacks is assumed.
43462306a36Sopenharmony_ci
43562306a36Sopenharmony_ciCurrently 6 ATA command protocols are used. They can be sorted into the
43662306a36Sopenharmony_cifollowing four categories according to how they are processed.
43762306a36Sopenharmony_ci
43862306a36Sopenharmony_ciATA NO DATA or DMA
43962306a36Sopenharmony_ci    ATA_PROT_NODATA and ATA_PROT_DMA fall into this category. These
44062306a36Sopenharmony_ci    types of commands don't require any software intervention once
44162306a36Sopenharmony_ci    issued. Device will raise interrupt on completion.
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ciATA PIO
44462306a36Sopenharmony_ci    ATA_PROT_PIO is in this category. libata currently implements PIO
44562306a36Sopenharmony_ci    with polling. ATA_NIEN bit is set to turn off interrupt and
44662306a36Sopenharmony_ci    pio_task on ata_wq performs polling and IO.
44762306a36Sopenharmony_ci
44862306a36Sopenharmony_ciATAPI NODATA or DMA
44962306a36Sopenharmony_ci    ATA_PROT_ATAPI_NODATA and ATA_PROT_ATAPI_DMA are in this
45062306a36Sopenharmony_ci    category. packet_task is used to poll BSY bit after issuing PACKET
45162306a36Sopenharmony_ci    command. Once BSY is turned off by the device, packet_task
45262306a36Sopenharmony_ci    transfers CDB and hands off processing to interrupt handler.
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ciATAPI PIO
45562306a36Sopenharmony_ci    ATA_PROT_ATAPI is in this category. ATA_NIEN bit is set and, as
45662306a36Sopenharmony_ci    in ATAPI NODATA or DMA, packet_task submits cdb. However, after
45762306a36Sopenharmony_ci    submitting cdb, further processing (data transfer) is handed off to
45862306a36Sopenharmony_ci    pio_task.
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ciHow commands are completed
46162306a36Sopenharmony_ci--------------------------
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_ciOnce issued, all qc's are either completed with :c:func:`ata_qc_complete` or
46462306a36Sopenharmony_citime out. For commands which are handled by interrupts,
46562306a36Sopenharmony_ci:c:func:`ata_host_intr` invokes :c:func:`ata_qc_complete`, and, for PIO tasks,
46662306a36Sopenharmony_cipio_task invokes :c:func:`ata_qc_complete`. In error cases, packet_task may
46762306a36Sopenharmony_cialso complete commands.
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci:c:func:`ata_qc_complete` does the following.
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci1. DMA memory is unmapped.
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci2. ATA_QCFLAG_ACTIVE is cleared from qc->flags.
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci3. :c:expr:`qc->complete_fn` callback is invoked. If the return value of the
47662306a36Sopenharmony_ci   callback is not zero. Completion is short circuited and
47762306a36Sopenharmony_ci   :c:func:`ata_qc_complete` returns.
47862306a36Sopenharmony_ci
47962306a36Sopenharmony_ci4. :c:func:`__ata_qc_complete` is called, which does
48062306a36Sopenharmony_ci
48162306a36Sopenharmony_ci   1. ``qc->flags`` is cleared to zero.
48262306a36Sopenharmony_ci
48362306a36Sopenharmony_ci   2. ``ap->active_tag`` and ``qc->tag`` are poisoned.
48462306a36Sopenharmony_ci
48562306a36Sopenharmony_ci   3. ``qc->waiting`` is cleared & completed (in that order).
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci   4. qc is deallocated by clearing appropriate bit in ``ap->qactive``.
48862306a36Sopenharmony_ci
48962306a36Sopenharmony_ciSo, it basically notifies upper layer and deallocates qc. One exception
49062306a36Sopenharmony_ciis short-circuit path in #3 which is used by :c:func:`atapi_qc_complete`.
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ciFor all non-ATAPI commands, whether it fails or not, almost the same
49362306a36Sopenharmony_cicode path is taken and very little error handling takes place. A qc is
49462306a36Sopenharmony_cicompleted with success status if it succeeded, with failed status
49562306a36Sopenharmony_ciotherwise.
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ciHowever, failed ATAPI commands require more handling as REQUEST SENSE is
49862306a36Sopenharmony_cineeded to acquire sense data. If an ATAPI command fails,
49962306a36Sopenharmony_ci:c:func:`ata_qc_complete` is invoked with error status, which in turn invokes
50062306a36Sopenharmony_ci:c:func:`atapi_qc_complete` via ``qc->complete_fn()`` callback.
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_ciThis makes :c:func:`atapi_qc_complete` set ``scmd->result`` to
50362306a36Sopenharmony_ciSAM_STAT_CHECK_CONDITION, complete the scmd and return 1. As the
50462306a36Sopenharmony_cisense data is empty but ``scmd->result`` is CHECK CONDITION, SCSI midlayer
50562306a36Sopenharmony_ciwill invoke EH for the scmd, and returning 1 makes :c:func:`ata_qc_complete`
50662306a36Sopenharmony_cito return without deallocating the qc. This leads us to
50762306a36Sopenharmony_ci:c:func:`ata_scsi_error` with partially completed qc.
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci:c:func:`ata_scsi_error`
51062306a36Sopenharmony_ci------------------------
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci:c:func:`ata_scsi_error` is the current ``transportt->eh_strategy_handler()``
51362306a36Sopenharmony_cifor libata. As discussed above, this will be entered in two cases -
51462306a36Sopenharmony_citimeout and ATAPI error completion. This function will check if a qc is active
51562306a36Sopenharmony_ciand has not failed yet. Such a qc will be marked with AC_ERR_TIMEOUT such that
51662306a36Sopenharmony_ciEH will know to handle it later. Then it calls low level libata driver's
51762306a36Sopenharmony_ci:c:func:`error_handler` callback.
51862306a36Sopenharmony_ci
51962306a36Sopenharmony_ciWhen the :c:func:`error_handler` callback is invoked it stops BMDMA and
52062306a36Sopenharmony_cicompletes the qc. Note that as we're currently in EH, we cannot call
52162306a36Sopenharmony_ciscsi_done. As described in SCSI EH doc, a recovered scmd should be
52262306a36Sopenharmony_cieither retried with :c:func:`scsi_queue_insert` or finished with
52362306a36Sopenharmony_ci:c:func:`scsi_finish_command`. Here, we override ``qc->scsidone`` with
52462306a36Sopenharmony_ci:c:func:`scsi_finish_command` and calls :c:func:`ata_qc_complete`.
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ciIf EH is invoked due to a failed ATAPI qc, the qc here is completed but
52762306a36Sopenharmony_cinot deallocated. The purpose of this half-completion is to use the qc as
52862306a36Sopenharmony_ciplace holder to make EH code reach this place. This is a bit hackish,
52962306a36Sopenharmony_cibut it works.
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ciOnce control reaches here, the qc is deallocated by invoking
53262306a36Sopenharmony_ci:c:func:`__ata_qc_complete` explicitly. Then, internal qc for REQUEST SENSE
53362306a36Sopenharmony_ciis issued. Once sense data is acquired, scmd is finished by directly
53462306a36Sopenharmony_ciinvoking :c:func:`scsi_finish_command` on the scmd. Note that as we already
53562306a36Sopenharmony_cihave completed and deallocated the qc which was associated with the
53662306a36Sopenharmony_ciscmd, we don't need to/cannot call :c:func:`ata_qc_complete` again.
53762306a36Sopenharmony_ci
53862306a36Sopenharmony_ciProblems with the current EH
53962306a36Sopenharmony_ci----------------------------
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci-  Error representation is too crude. Currently any and all error
54262306a36Sopenharmony_ci   conditions are represented with ATA STATUS and ERROR registers.
54362306a36Sopenharmony_ci   Errors which aren't ATA device errors are treated as ATA device
54462306a36Sopenharmony_ci   errors by setting ATA_ERR bit. Better error descriptor which can
54562306a36Sopenharmony_ci   properly represent ATA and other errors/exceptions is needed.
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci-  When handling timeouts, no action is taken to make device forget
54862306a36Sopenharmony_ci   about the timed out command and ready for new commands.
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci-  EH handling via :c:func:`ata_scsi_error` is not properly protected from
55162306a36Sopenharmony_ci   usual command processing. On EH entrance, the device is not in
55262306a36Sopenharmony_ci   quiescent state. Timed out commands may succeed or fail any time.
55362306a36Sopenharmony_ci   pio_task and atapi_task may still be running.
55462306a36Sopenharmony_ci
55562306a36Sopenharmony_ci-  Too weak error recovery. Devices / controllers causing HSM mismatch
55662306a36Sopenharmony_ci   errors and other errors quite often require reset to return to known
55762306a36Sopenharmony_ci   state. Also, advanced error handling is necessary to support features
55862306a36Sopenharmony_ci   like NCQ and hotplug.
55962306a36Sopenharmony_ci
56062306a36Sopenharmony_ci-  ATA errors are directly handled in the interrupt handler and PIO
56162306a36Sopenharmony_ci   errors in pio_task. This is problematic for advanced error handling
56262306a36Sopenharmony_ci   for the following reasons.
56362306a36Sopenharmony_ci
56462306a36Sopenharmony_ci   First, advanced error handling often requires context and internal qc
56562306a36Sopenharmony_ci   execution.
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci   Second, even a simple failure (say, CRC error) needs information
56862306a36Sopenharmony_ci   gathering and could trigger complex error handling (say, resetting &
56962306a36Sopenharmony_ci   reconfiguring). Having multiple code paths to gather information,
57062306a36Sopenharmony_ci   enter EH and trigger actions makes life painful.
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci   Third, scattered EH code makes implementing low level drivers
57362306a36Sopenharmony_ci   difficult. Low level drivers override libata callbacks. If EH is
57462306a36Sopenharmony_ci   scattered over several places, each affected callbacks should perform
57562306a36Sopenharmony_ci   its part of error handling. This can be error prone and painful.
57662306a36Sopenharmony_ci
57762306a36Sopenharmony_cilibata Library
57862306a36Sopenharmony_ci==============
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci.. kernel-doc:: drivers/ata/libata-core.c
58162306a36Sopenharmony_ci   :export:
58262306a36Sopenharmony_ci
58362306a36Sopenharmony_cilibata Core Internals
58462306a36Sopenharmony_ci=====================
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci.. kernel-doc:: drivers/ata/libata-core.c
58762306a36Sopenharmony_ci   :internal:
58862306a36Sopenharmony_ci
58962306a36Sopenharmony_ci.. kernel-doc:: drivers/ata/libata-eh.c
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_cilibata SCSI translation/emulation
59262306a36Sopenharmony_ci=================================
59362306a36Sopenharmony_ci
59462306a36Sopenharmony_ci.. kernel-doc:: drivers/ata/libata-scsi.c
59562306a36Sopenharmony_ci   :export:
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci.. kernel-doc:: drivers/ata/libata-scsi.c
59862306a36Sopenharmony_ci   :internal:
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ciATA errors and exceptions
60162306a36Sopenharmony_ci=========================
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ciThis chapter tries to identify what error/exception conditions exist for
60462306a36Sopenharmony_ciATA/ATAPI devices and describe how they should be handled in
60562306a36Sopenharmony_ciimplementation-neutral way.
60662306a36Sopenharmony_ci
60762306a36Sopenharmony_ciThe term 'error' is used to describe conditions where either an explicit
60862306a36Sopenharmony_cierror condition is reported from device or a command has timed out.
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ciThe term 'exception' is either used to describe exceptional conditions
61162306a36Sopenharmony_ciwhich are not errors (say, power or hotplug events), or to describe both
61262306a36Sopenharmony_cierrors and non-error exceptional conditions. Where explicit distinction
61362306a36Sopenharmony_cibetween error and exception is necessary, the term 'non-error exception'
61462306a36Sopenharmony_ciis used.
61562306a36Sopenharmony_ci
61662306a36Sopenharmony_ciException categories
61762306a36Sopenharmony_ci--------------------
61862306a36Sopenharmony_ci
61962306a36Sopenharmony_ciExceptions are described primarily with respect to legacy taskfile + bus
62062306a36Sopenharmony_cimaster IDE interface. If a controller provides other better mechanism
62162306a36Sopenharmony_cifor error reporting, mapping those into categories described below
62262306a36Sopenharmony_cishouldn't be difficult.
62362306a36Sopenharmony_ci
62462306a36Sopenharmony_ciIn the following sections, two recovery actions - reset and
62562306a36Sopenharmony_cireconfiguring transport - are mentioned. These are described further in
62662306a36Sopenharmony_ci`EH recovery actions <#exrec>`__.
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_ciHSM violation
62962306a36Sopenharmony_ci~~~~~~~~~~~~~
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ciThis error is indicated when STATUS value doesn't match HSM requirement
63262306a36Sopenharmony_ciduring issuing or execution any ATA/ATAPI command.
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci-  ATA_STATUS doesn't contain !BSY && DRDY && !DRQ while trying to
63562306a36Sopenharmony_ci   issue a command.
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci-  !BSY && !DRQ during PIO data transfer.
63862306a36Sopenharmony_ci
63962306a36Sopenharmony_ci-  DRQ on command completion.
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_ci-  !BSY && ERR after CDB transfer starts but before the last byte of CDB
64262306a36Sopenharmony_ci   is transferred. ATA/ATAPI standard states that "The device shall not
64362306a36Sopenharmony_ci   terminate the PACKET command with an error before the last byte of
64462306a36Sopenharmony_ci   the command packet has been written" in the error outputs description
64562306a36Sopenharmony_ci   of PACKET command and the state diagram doesn't include such
64662306a36Sopenharmony_ci   transitions.
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_ciIn these cases, HSM is violated and not much information regarding the
64962306a36Sopenharmony_cierror can be acquired from STATUS or ERROR register. IOW, this error can
65062306a36Sopenharmony_cibe anything - driver bug, faulty device, controller and/or cable.
65162306a36Sopenharmony_ci
65262306a36Sopenharmony_ciAs HSM is violated, reset is necessary to restore known state.
65362306a36Sopenharmony_ciReconfiguring transport for lower speed might be helpful too as
65462306a36Sopenharmony_citransmission errors sometimes cause this kind of errors.
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ciATA/ATAPI device error (non-NCQ / non-CHECK CONDITION)
65762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65862306a36Sopenharmony_ci
65962306a36Sopenharmony_ciThese are errors detected and reported by ATA/ATAPI devices indicating
66062306a36Sopenharmony_cidevice problems. For this type of errors, STATUS and ERROR register
66162306a36Sopenharmony_civalues are valid and describe error condition. Note that some of ATA bus
66262306a36Sopenharmony_cierrors are detected by ATA/ATAPI devices and reported using the same
66362306a36Sopenharmony_cimechanism as device errors. Those cases are described later in this
66462306a36Sopenharmony_cisection.
66562306a36Sopenharmony_ci
66662306a36Sopenharmony_ciFor ATA commands, this type of errors are indicated by !BSY && ERR
66762306a36Sopenharmony_ciduring command execution and on completion.
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ciFor ATAPI commands,
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci-  !BSY && ERR && ABRT right after issuing PACKET indicates that PACKET
67262306a36Sopenharmony_ci   command is not supported and falls in this category.
67362306a36Sopenharmony_ci
67462306a36Sopenharmony_ci-  !BSY && ERR(==CHK) && !ABRT after the last byte of CDB is transferred
67562306a36Sopenharmony_ci   indicates CHECK CONDITION and doesn't fall in this category.
67662306a36Sopenharmony_ci
67762306a36Sopenharmony_ci-  !BSY && ERR(==CHK) && ABRT after the last byte of CDB is transferred
67862306a36Sopenharmony_ci   \*probably\* indicates CHECK CONDITION and doesn't fall in this
67962306a36Sopenharmony_ci   category.
68062306a36Sopenharmony_ci
68162306a36Sopenharmony_ciOf errors detected as above, the following are not ATA/ATAPI device
68262306a36Sopenharmony_cierrors but ATA bus errors and should be handled according to
68362306a36Sopenharmony_ci`ATA bus error <#excatATAbusErr>`__.
68462306a36Sopenharmony_ci
68562306a36Sopenharmony_ciCRC error during data transfer
68662306a36Sopenharmony_ci    This is indicated by ICRC bit in the ERROR register and means that
68762306a36Sopenharmony_ci    corruption occurred during data transfer. Up to ATA/ATAPI-7, the
68862306a36Sopenharmony_ci    standard specifies that this bit is only applicable to UDMA
68962306a36Sopenharmony_ci    transfers but ATA/ATAPI-8 draft revision 1f says that the bit may be
69062306a36Sopenharmony_ci    applicable to multiword DMA and PIO.
69162306a36Sopenharmony_ci
69262306a36Sopenharmony_ciABRT error during data transfer or on completion
69362306a36Sopenharmony_ci    Up to ATA/ATAPI-7, the standard specifies that ABRT could be set on
69462306a36Sopenharmony_ci    ICRC errors and on cases where a device is not able to complete a
69562306a36Sopenharmony_ci    command. Combined with the fact that MWDMA and PIO transfer errors
69662306a36Sopenharmony_ci    aren't allowed to use ICRC bit up to ATA/ATAPI-7, it seems to imply
69762306a36Sopenharmony_ci    that ABRT bit alone could indicate transfer errors.
69862306a36Sopenharmony_ci
69962306a36Sopenharmony_ci    However, ATA/ATAPI-8 draft revision 1f removes the part that ICRC
70062306a36Sopenharmony_ci    errors can turn on ABRT. So, this is kind of gray area. Some
70162306a36Sopenharmony_ci    heuristics are needed here.
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ciATA/ATAPI device errors can be further categorized as follows.
70462306a36Sopenharmony_ci
70562306a36Sopenharmony_ciMedia errors
70662306a36Sopenharmony_ci    This is indicated by UNC bit in the ERROR register. ATA devices
70762306a36Sopenharmony_ci    reports UNC error only after certain number of retries cannot
70862306a36Sopenharmony_ci    recover the data, so there's nothing much else to do other than
70962306a36Sopenharmony_ci    notifying upper layer.
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci    READ and WRITE commands report CHS or LBA of the first failed sector
71262306a36Sopenharmony_ci    but ATA/ATAPI standard specifies that the amount of transferred data
71362306a36Sopenharmony_ci    on error completion is indeterminate, so we cannot assume that
71462306a36Sopenharmony_ci    sectors preceding the failed sector have been transferred and thus
71562306a36Sopenharmony_ci    cannot complete those sectors successfully as SCSI does.
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_ciMedia changed / media change requested error
71862306a36Sopenharmony_ci    <<TODO: fill here>>
71962306a36Sopenharmony_ci
72062306a36Sopenharmony_ciAddress error
72162306a36Sopenharmony_ci    This is indicated by IDNF bit in the ERROR register. Report to upper
72262306a36Sopenharmony_ci    layer.
72362306a36Sopenharmony_ci
72462306a36Sopenharmony_ciOther errors
72562306a36Sopenharmony_ci    This can be invalid command or parameter indicated by ABRT ERROR bit
72662306a36Sopenharmony_ci    or some other error condition. Note that ABRT bit can indicate a lot
72762306a36Sopenharmony_ci    of things including ICRC and Address errors. Heuristics needed.
72862306a36Sopenharmony_ci
72962306a36Sopenharmony_ciDepending on commands, not all STATUS/ERROR bits are applicable. These
73062306a36Sopenharmony_cinon-applicable bits are marked with "na" in the output descriptions but
73162306a36Sopenharmony_ciup to ATA/ATAPI-7 no definition of "na" can be found. However,
73262306a36Sopenharmony_ciATA/ATAPI-8 draft revision 1f describes "N/A" as follows.
73362306a36Sopenharmony_ci
73462306a36Sopenharmony_ci    3.2.3.3a N/A
73562306a36Sopenharmony_ci        A keyword the indicates a field has no defined value in this
73662306a36Sopenharmony_ci        standard and should not be checked by the host or device. N/A
73762306a36Sopenharmony_ci        fields should be cleared to zero.
73862306a36Sopenharmony_ci
73962306a36Sopenharmony_ciSo, it seems reasonable to assume that "na" bits are cleared to zero by
74062306a36Sopenharmony_cidevices and thus need no explicit masking.
74162306a36Sopenharmony_ci
74262306a36Sopenharmony_ciATAPI device CHECK CONDITION
74362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ciATAPI device CHECK CONDITION error is indicated by set CHK bit (ERR bit)
74662306a36Sopenharmony_ciin the STATUS register after the last byte of CDB is transferred for a
74762306a36Sopenharmony_ciPACKET command. For this kind of errors, sense data should be acquired
74862306a36Sopenharmony_cito gather information regarding the errors. REQUEST SENSE packet command
74962306a36Sopenharmony_cishould be used to acquire sense data.
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_ciOnce sense data is acquired, this type of errors can be handled
75262306a36Sopenharmony_cisimilarly to other SCSI errors. Note that sense data may indicate ATA
75362306a36Sopenharmony_cibus error (e.g. Sense Key 04h HARDWARE ERROR && ASC/ASCQ 47h/00h SCSI
75462306a36Sopenharmony_ciPARITY ERROR). In such cases, the error should be considered as an ATA
75562306a36Sopenharmony_cibus error and handled according to `ATA bus error <#excatATAbusErr>`__.
75662306a36Sopenharmony_ci
75762306a36Sopenharmony_ciATA device error (NCQ)
75862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~
75962306a36Sopenharmony_ci
76062306a36Sopenharmony_ciNCQ command error is indicated by cleared BSY and set ERR bit during NCQ
76162306a36Sopenharmony_cicommand phase (one or more NCQ commands outstanding). Although STATUS
76262306a36Sopenharmony_ciand ERROR registers will contain valid values describing the error, READ
76362306a36Sopenharmony_ciLOG EXT is required to clear the error condition, determine which
76462306a36Sopenharmony_cicommand has failed and acquire more information.
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ciREAD LOG EXT Log Page 10h reports which tag has failed and taskfile
76762306a36Sopenharmony_ciregister values describing the error. With this information the failed
76862306a36Sopenharmony_cicommand can be handled as a normal ATA command error as in
76962306a36Sopenharmony_ci`ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__
77062306a36Sopenharmony_ciand all other in-flight commands must be retried. Note that this retry
77162306a36Sopenharmony_cishould not be counted - it's likely that commands retried this way would
77262306a36Sopenharmony_cihave completed normally if it were not for the failed command.
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ciNote that ATA bus errors can be reported as ATA device NCQ errors. This
77562306a36Sopenharmony_cishould be handled as described in `ATA bus error <#excatATAbusErr>`__.
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ciIf READ LOG EXT Log Page 10h fails or reports NQ, we're thoroughly
77862306a36Sopenharmony_ciscrewed. This condition should be treated according to
77962306a36Sopenharmony_ci`HSM violation <#excatHSMviolation>`__.
78062306a36Sopenharmony_ci
78162306a36Sopenharmony_ciATA bus error
78262306a36Sopenharmony_ci~~~~~~~~~~~~~
78362306a36Sopenharmony_ci
78462306a36Sopenharmony_ciATA bus error means that data corruption occurred during transmission
78562306a36Sopenharmony_ciover ATA bus (SATA or PATA). This type of errors can be indicated by
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci-  ICRC or ABRT error as described in
78862306a36Sopenharmony_ci   `ATA/ATAPI device error (non-NCQ / non-CHECK CONDITION) <#excatDevErr>`__.
78962306a36Sopenharmony_ci
79062306a36Sopenharmony_ci-  Controller-specific error completion with error information
79162306a36Sopenharmony_ci   indicating transmission error.
79262306a36Sopenharmony_ci
79362306a36Sopenharmony_ci-  On some controllers, command timeout. In this case, there may be a
79462306a36Sopenharmony_ci   mechanism to determine that the timeout is due to transmission error.
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ci-  Unknown/random errors, timeouts and all sorts of weirdities.
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_ciAs described above, transmission errors can cause wide variety of
79962306a36Sopenharmony_cisymptoms ranging from device ICRC error to random device lockup, and,
80062306a36Sopenharmony_cifor many cases, there is no way to tell if an error condition is due to
80162306a36Sopenharmony_citransmission error or not; therefore, it's necessary to employ some kind
80262306a36Sopenharmony_ciof heuristic when dealing with errors and timeouts. For example,
80362306a36Sopenharmony_ciencountering repetitive ABRT errors for known supported command is
80462306a36Sopenharmony_cilikely to indicate ATA bus error.
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ciOnce it's determined that ATA bus errors have possibly occurred,
80762306a36Sopenharmony_cilowering ATA bus transmission speed is one of actions which may
80862306a36Sopenharmony_cialleviate the problem. See `Reconfigure transport <#exrecReconf>`__ for
80962306a36Sopenharmony_cimore information.
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ciPCI bus error
81262306a36Sopenharmony_ci~~~~~~~~~~~~~
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_ciData corruption or other failures during transmission over PCI (or other
81562306a36Sopenharmony_cisystem bus). For standard BMDMA, this is indicated by Error bit in the
81662306a36Sopenharmony_ciBMDMA Status register. This type of errors must be logged as it
81762306a36Sopenharmony_ciindicates something is very wrong with the system. Resetting host
81862306a36Sopenharmony_cicontroller is recommended.
81962306a36Sopenharmony_ci
82062306a36Sopenharmony_ciLate completion
82162306a36Sopenharmony_ci~~~~~~~~~~~~~~~
82262306a36Sopenharmony_ci
82362306a36Sopenharmony_ciThis occurs when timeout occurs and the timeout handler finds out that
82462306a36Sopenharmony_cithe timed out command has completed successfully or with error. This is
82562306a36Sopenharmony_ciusually caused by lost interrupts. This type of errors must be logged.
82662306a36Sopenharmony_ciResetting host controller is recommended.
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_ciUnknown error (timeout)
82962306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ciThis is when timeout occurs and the command is still processing or the
83262306a36Sopenharmony_cihost and device are in unknown state. When this occurs, HSM could be in
83362306a36Sopenharmony_ciany valid or invalid state. To bring the device to known state and make
83462306a36Sopenharmony_ciit forget about the timed out command, resetting is necessary. The timed
83562306a36Sopenharmony_ciout command may be retried.
83662306a36Sopenharmony_ci
83762306a36Sopenharmony_ciTimeouts can also be caused by transmission errors. Refer to
83862306a36Sopenharmony_ci`ATA bus error <#excatATAbusErr>`__ for more details.
83962306a36Sopenharmony_ci
84062306a36Sopenharmony_ciHotplug and power management exceptions
84162306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
84262306a36Sopenharmony_ci
84362306a36Sopenharmony_ci<<TODO: fill here>>
84462306a36Sopenharmony_ci
84562306a36Sopenharmony_ciEH recovery actions
84662306a36Sopenharmony_ci-------------------
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ciThis section discusses several important recovery actions.
84962306a36Sopenharmony_ci
85062306a36Sopenharmony_ciClearing error condition
85162306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ciMany controllers require its error registers to be cleared by error
85462306a36Sopenharmony_cihandler. Different controllers may have different requirements.
85562306a36Sopenharmony_ci
85662306a36Sopenharmony_ciFor SATA, it's strongly recommended to clear at least SError register
85762306a36Sopenharmony_ciduring error handling.
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ciReset
86062306a36Sopenharmony_ci~~~~~
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ciDuring EH, resetting is necessary in the following cases.
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci-  HSM is in unknown or invalid state
86562306a36Sopenharmony_ci
86662306a36Sopenharmony_ci-  HBA is in unknown or invalid state
86762306a36Sopenharmony_ci
86862306a36Sopenharmony_ci-  EH needs to make HBA/device forget about in-flight commands
86962306a36Sopenharmony_ci
87062306a36Sopenharmony_ci-  HBA/device behaves weirdly
87162306a36Sopenharmony_ci
87262306a36Sopenharmony_ciResetting during EH might be a good idea regardless of error condition
87362306a36Sopenharmony_cito improve EH robustness. Whether to reset both or either one of HBA and
87462306a36Sopenharmony_cidevice depends on situation but the following scheme is recommended.
87562306a36Sopenharmony_ci
87662306a36Sopenharmony_ci-  When it's known that HBA is in ready state but ATA/ATAPI device is in
87762306a36Sopenharmony_ci   unknown state, reset only device.
87862306a36Sopenharmony_ci
87962306a36Sopenharmony_ci-  If HBA is in unknown state, reset both HBA and device.
88062306a36Sopenharmony_ci
88162306a36Sopenharmony_ciHBA resetting is implementation specific. For a controller complying to
88262306a36Sopenharmony_citaskfile/BMDMA PCI IDE, stopping active DMA transaction may be
88362306a36Sopenharmony_cisufficient iff BMDMA state is the only HBA context. But even mostly
88462306a36Sopenharmony_citaskfile/BMDMA PCI IDE complying controllers may have implementation
88562306a36Sopenharmony_cispecific requirements and mechanism to reset themselves. This must be
88662306a36Sopenharmony_ciaddressed by specific drivers.
88762306a36Sopenharmony_ci
88862306a36Sopenharmony_ciOTOH, ATA/ATAPI standard describes in detail ways to reset ATA/ATAPI
88962306a36Sopenharmony_cidevices.
89062306a36Sopenharmony_ci
89162306a36Sopenharmony_ciPATA hardware reset
89262306a36Sopenharmony_ci    This is hardware initiated device reset signalled with asserted PATA
89362306a36Sopenharmony_ci    RESET- signal. There is no standard way to initiate hardware reset
89462306a36Sopenharmony_ci    from software although some hardware provides registers that allow
89562306a36Sopenharmony_ci    driver to directly tweak the RESET- signal.
89662306a36Sopenharmony_ci
89762306a36Sopenharmony_ciSoftware reset
89862306a36Sopenharmony_ci    This is achieved by turning CONTROL SRST bit on for at least 5us.
89962306a36Sopenharmony_ci    Both PATA and SATA support it but, in case of SATA, this may require
90062306a36Sopenharmony_ci    controller-specific support as the second Register FIS to clear SRST
90162306a36Sopenharmony_ci    should be transmitted while BSY bit is still set. Note that on PATA,
90262306a36Sopenharmony_ci    this resets both master and slave devices on a channel.
90362306a36Sopenharmony_ci
90462306a36Sopenharmony_ciEXECUTE DEVICE DIAGNOSTIC command
90562306a36Sopenharmony_ci    Although ATA/ATAPI standard doesn't describe exactly, EDD implies
90662306a36Sopenharmony_ci    some level of resetting, possibly similar level with software reset.
90762306a36Sopenharmony_ci    Host-side EDD protocol can be handled with normal command processing
90862306a36Sopenharmony_ci    and most SATA controllers should be able to handle EDD's just like
90962306a36Sopenharmony_ci    other commands. As in software reset, EDD affects both devices on a
91062306a36Sopenharmony_ci    PATA bus.
91162306a36Sopenharmony_ci
91262306a36Sopenharmony_ci    Although EDD does reset devices, this doesn't suit error handling as
91362306a36Sopenharmony_ci    EDD cannot be issued while BSY is set and it's unclear how it will
91462306a36Sopenharmony_ci    act when device is in unknown/weird state.
91562306a36Sopenharmony_ci
91662306a36Sopenharmony_ciATAPI DEVICE RESET command
91762306a36Sopenharmony_ci    This is very similar to software reset except that reset can be
91862306a36Sopenharmony_ci    restricted to the selected device without affecting the other device
91962306a36Sopenharmony_ci    sharing the cable.
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ciSATA phy reset
92262306a36Sopenharmony_ci    This is the preferred way of resetting a SATA device. In effect,
92362306a36Sopenharmony_ci    it's identical to PATA hardware reset. Note that this can be done
92462306a36Sopenharmony_ci    with the standard SCR Control register. As such, it's usually easier
92562306a36Sopenharmony_ci    to implement than software reset.
92662306a36Sopenharmony_ci
92762306a36Sopenharmony_ciOne more thing to consider when resetting devices is that resetting
92862306a36Sopenharmony_ciclears certain configuration parameters and they need to be set to their
92962306a36Sopenharmony_ciprevious or newly adjusted values after reset.
93062306a36Sopenharmony_ci
93162306a36Sopenharmony_ciParameters affected are.
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci-  CHS set up with INITIALIZE DEVICE PARAMETERS (seldom used)
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_ci-  Parameters set with SET FEATURES including transfer mode setting
93662306a36Sopenharmony_ci
93762306a36Sopenharmony_ci-  Block count set with SET MULTIPLE MODE
93862306a36Sopenharmony_ci
93962306a36Sopenharmony_ci-  Other parameters (SET MAX, MEDIA LOCK...)
94062306a36Sopenharmony_ci
94162306a36Sopenharmony_ciATA/ATAPI standard specifies that some parameters must be maintained
94262306a36Sopenharmony_ciacross hardware or software reset, but doesn't strictly specify all of
94362306a36Sopenharmony_cithem. Always reconfiguring needed parameters after reset is required for
94462306a36Sopenharmony_cirobustness. Note that this also applies when resuming from deep sleep
94562306a36Sopenharmony_ci(power-off).
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ciAlso, ATA/ATAPI standard requires that IDENTIFY DEVICE / IDENTIFY PACKET
94862306a36Sopenharmony_ciDEVICE is issued after any configuration parameter is updated or a
94962306a36Sopenharmony_cihardware reset and the result used for further operation. OS driver is
95062306a36Sopenharmony_cirequired to implement revalidation mechanism to support this.
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ciReconfigure transport
95362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ciFor both PATA and SATA, a lot of corners are cut for cheap connectors,
95662306a36Sopenharmony_cicables or controllers and it's quite common to see high transmission
95762306a36Sopenharmony_cierror rate. This can be mitigated by lowering transmission speed.
95862306a36Sopenharmony_ci
95962306a36Sopenharmony_ciThe following is a possible scheme Jeff Garzik suggested.
96062306a36Sopenharmony_ci
96162306a36Sopenharmony_ci    If more than $N (3?) transmission errors happen in 15 minutes,
96262306a36Sopenharmony_ci
96362306a36Sopenharmony_ci    -  if SATA, decrease SATA PHY speed. if speed cannot be decreased,
96462306a36Sopenharmony_ci
96562306a36Sopenharmony_ci    -  decrease UDMA xfer speed. if at UDMA0, switch to PIO4,
96662306a36Sopenharmony_ci
96762306a36Sopenharmony_ci    -  decrease PIO xfer speed. if at PIO3, complain, but continue
96862306a36Sopenharmony_ci
96962306a36Sopenharmony_ciata_piix Internals
97062306a36Sopenharmony_ci===================
97162306a36Sopenharmony_ci
97262306a36Sopenharmony_ci.. kernel-doc:: drivers/ata/ata_piix.c
97362306a36Sopenharmony_ci   :internal:
97462306a36Sopenharmony_ci
97562306a36Sopenharmony_cisata_sil Internals
97662306a36Sopenharmony_ci===================
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci.. kernel-doc:: drivers/ata/sata_sil.c
97962306a36Sopenharmony_ci   :internal:
98062306a36Sopenharmony_ci
98162306a36Sopenharmony_ciThanks
98262306a36Sopenharmony_ci======
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ciThe bulk of the ATA knowledge comes thanks to long conversations with
98562306a36Sopenharmony_ciAndre Hedrick (www.linux-ide.org), and long hours pondering the ATA and
98662306a36Sopenharmony_ciSCSI specifications.
98762306a36Sopenharmony_ci
98862306a36Sopenharmony_ciThanks to Alan Cox for pointing out similarities between SATA and SCSI,
98962306a36Sopenharmony_ciand in general for motivation to hack on libata.
99062306a36Sopenharmony_ci
99162306a36Sopenharmony_cilibata's device detection method, ata_pio_devchk, and in general all
99262306a36Sopenharmony_cithe early probing was based on extensive study of Hale Landis's
99362306a36Sopenharmony_ciprobe/reset code in his ATADRVR driver (www.ata-atapi.com).
994