1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SATA specific part of ATA helper library
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 * Copyright 2006 Tejun Heo <htejun@gmail.com>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <scsi/scsi_cmnd.h>
13 #include <scsi/scsi_device.h>
14 #include <scsi/scsi_eh.h>
15 #include <linux/libata.h>
16 #include <asm/unaligned.h>
19 #include "libata-transport.h"
21 /* debounce timing parameters in msecs { interval, duration, timeout } */
22 const unsigned int sata_deb_timing_normal[] = { 5, 100, 2000 };
23 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
24 const unsigned int sata_deb_timing_hotplug[] = { 25, 500, 2000 };
25 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
26 const unsigned int sata_deb_timing_long[] = { 100, 2000, 5000 };
27 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
30 * sata_scr_valid - test whether SCRs are accessible
31 * @link: ATA link to test SCR accessibility for
33 * Test whether SCRs are accessible for @link.
39 * 1 if SCRs are accessible, 0 otherwise.
41 int sata_scr_valid(struct ata_link *link)
43 struct ata_port *ap = link->ap;
45 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
47 EXPORT_SYMBOL_GPL(sata_scr_valid);
50 * sata_scr_read - read SCR register of the specified port
51 * @link: ATA link to read SCR for
53 * @val: Place to store read value
55 * Read SCR register @reg of @link into *@val. This function is
56 * guaranteed to succeed if @link is ap->link, the cable type of
57 * the port is SATA and the port implements ->scr_read.
60 * None if @link is ap->link. Kernel thread context otherwise.
63 * 0 on success, negative errno on failure.
65 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
67 if (ata_is_host_link(link)) {
68 if (sata_scr_valid(link))
69 return link->ap->ops->scr_read(link, reg, val);
73 return sata_pmp_scr_read(link, reg, val);
75 EXPORT_SYMBOL_GPL(sata_scr_read);
78 * sata_scr_write - write SCR register of the specified port
79 * @link: ATA link to write SCR for
81 * @val: value to write
83 * Write @val to SCR register @reg of @link. This function is
84 * guaranteed to succeed if @link is ap->link, the cable type of
85 * the port is SATA and the port implements ->scr_read.
88 * None if @link is ap->link. Kernel thread context otherwise.
91 * 0 on success, negative errno on failure.
93 int sata_scr_write(struct ata_link *link, int reg, u32 val)
95 if (ata_is_host_link(link)) {
96 if (sata_scr_valid(link))
97 return link->ap->ops->scr_write(link, reg, val);
101 return sata_pmp_scr_write(link, reg, val);
103 EXPORT_SYMBOL_GPL(sata_scr_write);
106 * sata_scr_write_flush - write SCR register of the specified port and flush
107 * @link: ATA link to write SCR for
109 * @val: value to write
111 * This function is identical to sata_scr_write() except that this
112 * function performs flush after writing to the register.
115 * None if @link is ap->link. Kernel thread context otherwise.
118 * 0 on success, negative errno on failure.
120 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
122 if (ata_is_host_link(link)) {
125 if (sata_scr_valid(link)) {
126 rc = link->ap->ops->scr_write(link, reg, val);
128 rc = link->ap->ops->scr_read(link, reg, &val);
134 return sata_pmp_scr_write(link, reg, val);
136 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
139 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
140 * @tf: Taskfile to convert
141 * @pmp: Port multiplier port
142 * @is_cmd: This FIS is for command
143 * @fis: Buffer into which data will output
145 * Converts a standard ATA taskfile to a Serial ATA
146 * FIS structure (Register - Host to Device).
149 * Inherited from caller.
151 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
153 fis[0] = 0x27; /* Register - Host to Device FIS */
154 fis[1] = pmp & 0xf; /* Port multiplier number*/
156 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
158 fis[2] = tf->command;
159 fis[3] = tf->feature;
166 fis[8] = tf->hob_lbal;
167 fis[9] = tf->hob_lbam;
168 fis[10] = tf->hob_lbah;
169 fis[11] = tf->hob_feature;
172 fis[13] = tf->hob_nsect;
176 fis[16] = tf->auxiliary & 0xff;
177 fis[17] = (tf->auxiliary >> 8) & 0xff;
178 fis[18] = (tf->auxiliary >> 16) & 0xff;
179 fis[19] = (tf->auxiliary >> 24) & 0xff;
181 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
184 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
185 * @fis: Buffer from which data will be input
186 * @tf: Taskfile to output
188 * Converts a serial ATA FIS structure to a standard ATA taskfile.
191 * Inherited from caller.
194 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
204 tf->hob_lbal = fis[8];
205 tf->hob_lbam = fis[9];
206 tf->hob_lbah = fis[10];
209 tf->hob_nsect = fis[13];
211 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
214 * sata_link_debounce - debounce SATA phy status
215 * @link: ATA link to debounce SATA phy status for
216 * @params: timing parameters { interval, duration, timeout } in msec
217 * @deadline: deadline jiffies for the operation
219 * Make sure SStatus of @link reaches stable state, determined by
220 * holding the same value where DET is not 1 for @duration polled
221 * every @interval, before @timeout. Timeout constraints the
222 * beginning of the stable state. Because DET gets stuck at 1 on
223 * some controllers after hot unplugging, this functions waits
224 * until timeout then returns 0 if DET is stable at 1.
226 * @timeout is further limited by @deadline. The sooner of the
230 * Kernel thread context (may sleep)
233 * 0 on success, -errno on failure.
235 int sata_link_debounce(struct ata_link *link, const unsigned int *params,
236 unsigned long deadline)
238 unsigned int interval = params[0];
239 unsigned int duration = params[1];
240 unsigned long last_jiffies, t;
244 t = ata_deadline(jiffies, params[2]);
245 if (time_before(t, deadline))
248 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
253 last_jiffies = jiffies;
256 ata_msleep(link->ap, interval);
257 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
263 if (cur == 1 && time_before(jiffies, deadline))
265 if (time_after(jiffies,
266 ata_deadline(last_jiffies, duration)))
271 /* unstable, start over */
273 last_jiffies = jiffies;
275 /* Check deadline. If debouncing failed, return
276 * -EPIPE to tell upper layer to lower link speed.
278 if (time_after(jiffies, deadline))
282 EXPORT_SYMBOL_GPL(sata_link_debounce);
285 * sata_link_resume - resume SATA link
286 * @link: ATA link to resume SATA
287 * @params: timing parameters { interval, duration, timeout } in msec
288 * @deadline: deadline jiffies for the operation
290 * Resume SATA phy @link and debounce it.
293 * Kernel thread context (may sleep)
296 * 0 on success, -errno on failure.
298 int sata_link_resume(struct ata_link *link, const unsigned int *params,
299 unsigned long deadline)
301 int tries = ATA_LINK_RESUME_TRIES;
302 u32 scontrol, serror;
305 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
309 * Writes to SControl sometimes get ignored under certain
310 * controllers (ata_piix SIDPR). Make sure DET actually is
314 scontrol = (scontrol & 0x0f0) | 0x300;
315 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
318 * Some PHYs react badly if SStatus is pounded
319 * immediately after resuming. Delay 200ms before
322 if (!(link->flags & ATA_LFLAG_NO_DEBOUNCE_DELAY))
323 ata_msleep(link->ap, 200);
325 /* is SControl restored correctly? */
326 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
328 } while ((scontrol & 0xf0f) != 0x300 && --tries);
330 if ((scontrol & 0xf0f) != 0x300) {
331 ata_link_warn(link, "failed to resume link (SControl %X)\n",
336 if (tries < ATA_LINK_RESUME_TRIES)
337 ata_link_warn(link, "link resume succeeded after %d retries\n",
338 ATA_LINK_RESUME_TRIES - tries);
340 if ((rc = sata_link_debounce(link, params, deadline)))
343 /* clear SError, some PHYs require this even for SRST to work */
344 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
345 rc = sata_scr_write(link, SCR_ERROR, serror);
347 return rc != -EINVAL ? rc : 0;
349 EXPORT_SYMBOL_GPL(sata_link_resume);
352 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
353 * @link: ATA link to manipulate SControl for
354 * @policy: LPM policy to configure
355 * @spm_wakeup: initiate LPM transition to active state
357 * Manipulate the IPM field of the SControl register of @link
358 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
359 * @spm_wakeup is %true, the SPM field is manipulated to wake up
360 * the link. This function also clears PHYRDY_CHG before
367 * 0 on success, -errno otherwise.
369 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
372 struct ata_eh_context *ehc = &link->eh_context;
373 bool woken_up = false;
377 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
382 case ATA_LPM_MAX_POWER:
383 /* disable all LPM transitions */
384 scontrol |= (0x7 << 8);
385 /* initiate transition to active state */
387 scontrol |= (0x4 << 12);
391 case ATA_LPM_MED_POWER:
392 /* allow LPM to PARTIAL */
393 scontrol &= ~(0x1 << 8);
394 scontrol |= (0x6 << 8);
396 case ATA_LPM_MED_POWER_WITH_DIPM:
397 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
398 case ATA_LPM_MIN_POWER:
399 if (ata_link_nr_enabled(link) > 0)
400 /* no restrictions on LPM transitions */
401 scontrol &= ~(0x7 << 8);
403 /* empty port, power off */
405 scontrol |= (0x1 << 2);
412 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
416 /* give the link time to transit out of LPM state */
420 /* clear PHYRDY_CHG from SError */
421 ehc->i.serror &= ~SERR_PHYRDY_CHG;
422 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
424 EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
426 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
428 struct ata_link *host_link = &link->ap->link;
429 u32 limit, target, spd;
431 limit = link->sata_spd_limit;
433 /* Don't configure downstream link faster than upstream link.
434 * It doesn't speed up anything and some PMPs choke on such
437 if (!ata_is_host_link(link) && host_link->sata_spd)
438 limit &= (1 << host_link->sata_spd) - 1;
440 if (limit == UINT_MAX)
445 spd = (*scontrol >> 4) & 0xf;
446 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
448 return spd != target;
452 * sata_set_spd_needed - is SATA spd configuration needed
453 * @link: Link in question
455 * Test whether the spd limit in SControl matches
456 * @link->sata_spd_limit. This function is used to determine
457 * whether hardreset is necessary to apply SATA spd
461 * Inherited from caller.
464 * 1 if SATA spd configuration is needed, 0 otherwise.
466 static int sata_set_spd_needed(struct ata_link *link)
470 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
473 return __sata_set_spd_needed(link, &scontrol);
477 * sata_set_spd - set SATA spd according to spd limit
478 * @link: Link to set SATA spd for
480 * Set SATA spd of @link according to sata_spd_limit.
483 * Inherited from caller.
486 * 0 if spd doesn't need to be changed, 1 if spd has been
487 * changed. Negative errno if SCR registers are inaccessible.
489 int sata_set_spd(struct ata_link *link)
494 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
497 if (!__sata_set_spd_needed(link, &scontrol))
500 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
505 EXPORT_SYMBOL_GPL(sata_set_spd);
508 * sata_link_hardreset - reset link via SATA phy reset
509 * @link: link to reset
510 * @timing: timing parameters { interval, duration, timeout } in msec
511 * @deadline: deadline jiffies for the operation
512 * @online: optional out parameter indicating link onlineness
513 * @check_ready: optional callback to check link readiness
515 * SATA phy-reset @link using DET bits of SControl register.
516 * After hardreset, link readiness is waited upon using
517 * ata_wait_ready() if @check_ready is specified. LLDs are
518 * allowed to not specify @check_ready and wait itself after this
519 * function returns. Device classification is LLD's
522 * *@online is set to one iff reset succeeded and @link is online
526 * Kernel thread context (may sleep)
529 * 0 on success, -errno otherwise.
531 int sata_link_hardreset(struct ata_link *link, const unsigned int *timing,
532 unsigned long deadline,
533 bool *online, int (*check_ready)(struct ata_link *))
541 if (sata_set_spd_needed(link)) {
542 /* SATA spec says nothing about how to reconfigure
543 * spd. To be on the safe side, turn off phy during
544 * reconfiguration. This works for at least ICH7 AHCI
547 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
550 scontrol = (scontrol & 0x0f0) | 0x304;
552 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
558 /* issue phy wake/reset */
559 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
562 scontrol = (scontrol & 0x0f0) | 0x301;
564 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
567 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
568 * 10.4.2 says at least 1 ms.
570 ata_msleep(link->ap, 1);
572 /* bring link back */
573 rc = sata_link_resume(link, timing, deadline);
576 /* if link is offline nothing more to do */
577 if (ata_phys_link_offline(link))
580 /* Link is online. From this point, -ENODEV too is an error. */
584 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
585 /* If PMP is supported, we have to do follow-up SRST.
586 * Some PMPs don't send D2H Reg FIS after hardreset if
587 * the first port is empty. Wait only for
588 * ATA_TMOUT_PMP_SRST_WAIT.
591 unsigned long pmp_deadline;
593 pmp_deadline = ata_deadline(jiffies,
594 ATA_TMOUT_PMP_SRST_WAIT);
595 if (time_after(pmp_deadline, deadline))
596 pmp_deadline = deadline;
597 ata_wait_ready(link, pmp_deadline, check_ready);
605 rc = ata_wait_ready(link, deadline, check_ready);
607 if (rc && rc != -EAGAIN) {
608 /* online is set iff link is online && reset succeeded */
611 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
615 EXPORT_SYMBOL_GPL(sata_link_hardreset);
618 * ata_qc_complete_multiple - Complete multiple qcs successfully
619 * @ap: port in question
620 * @qc_active: new qc_active mask
622 * Complete in-flight commands. This functions is meant to be
623 * called from low-level driver's interrupt routine to complete
624 * requests normally. ap->qc_active and @qc_active is compared
625 * and commands are completed accordingly.
627 * Always use this function when completing multiple NCQ commands
628 * from IRQ handlers instead of calling ata_qc_complete()
629 * multiple times to keep IRQ expect status properly in sync.
632 * spin_lock_irqsave(host lock)
635 * Number of completed commands on success, -errno otherwise.
637 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
639 u64 done_mask, ap_qc_active = ap->qc_active;
643 * If the internal tag is set on ap->qc_active, then we care about
644 * bit0 on the passed in qc_active mask. Move that bit up to match
647 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
648 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
649 qc_active ^= qc_active & 0x01;
652 done_mask = ap_qc_active ^ qc_active;
654 if (unlikely(done_mask & qc_active)) {
655 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
656 ap->qc_active, qc_active);
660 if (ap->ops->qc_ncq_fill_rtf)
661 ap->ops->qc_ncq_fill_rtf(ap, done_mask);
664 struct ata_queued_cmd *qc;
665 unsigned int tag = __ffs64(done_mask);
667 qc = ata_qc_from_tag(ap, tag);
672 done_mask &= ~(1ULL << tag);
677 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
680 * ata_slave_link_init - initialize slave link
681 * @ap: port to initialize slave link for
683 * Create and initialize slave link for @ap. This enables slave
684 * link handling on the port.
686 * In libata, a port contains links and a link contains devices.
687 * There is single host link but if a PMP is attached to it,
688 * there can be multiple fan-out links. On SATA, there's usually
689 * a single device connected to a link but PATA and SATA
690 * controllers emulating TF based interface can have two - master
693 * However, there are a few controllers which don't fit into this
694 * abstraction too well - SATA controllers which emulate TF
695 * interface with both master and slave devices but also have
696 * separate SCR register sets for each device. These controllers
697 * need separate links for physical link handling
698 * (e.g. onlineness, link speed) but should be treated like a
699 * traditional M/S controller for everything else (e.g. command
702 * slave_link is libata's way of handling this class of
703 * controllers without impacting core layer too much. For
704 * anything other than physical link handling, the default host
705 * link is used for both master and slave. For physical link
706 * handling, separate @ap->slave_link is used. All dirty details
707 * are implemented inside libata core layer. From LLD's POV, the
708 * only difference is that prereset, hardreset and postreset are
709 * called once more for the slave link, so the reset sequence
710 * looks like the following.
712 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
713 * softreset(M) -> postreset(M) -> postreset(S)
715 * Note that softreset is called only for the master. Softreset
716 * resets both M/S by definition, so SRST on master should handle
717 * both (the standard method will work just fine).
720 * Should be called before host is registered.
723 * 0 on success, -errno on failure.
725 int ata_slave_link_init(struct ata_port *ap)
727 struct ata_link *link;
729 WARN_ON(ap->slave_link);
730 WARN_ON(ap->flags & ATA_FLAG_PMP);
732 link = kzalloc(sizeof(*link), GFP_KERNEL);
736 ata_link_init(ap, link, 1);
737 ap->slave_link = link;
740 EXPORT_SYMBOL_GPL(ata_slave_link_init);
743 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
744 * @link: Link receiving the event
746 * Test whether the received PHY event has to be ignored or not.
752 * True if the event has to be ignored.
754 bool sata_lpm_ignore_phy_events(struct ata_link *link)
756 unsigned long lpm_timeout = link->last_lpm_change +
757 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
759 /* if LPM is enabled, PHYRDY doesn't mean anything */
760 if (link->lpm_policy > ATA_LPM_MAX_POWER)
763 /* ignore the first PHY event after the LPM policy changed
764 * as it is might be spurious
766 if ((link->flags & ATA_LFLAG_CHANGED) &&
767 time_before(jiffies, lpm_timeout))
772 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
774 static const char *ata_lpm_policy_names[] = {
775 [ATA_LPM_UNKNOWN] = "max_performance",
776 [ATA_LPM_MAX_POWER] = "max_performance",
777 [ATA_LPM_MED_POWER] = "medium_power",
778 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
779 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
780 [ATA_LPM_MIN_POWER] = "min_power",
783 static ssize_t ata_scsi_lpm_store(struct device *device,
784 struct device_attribute *attr,
785 const char *buf, size_t count)
787 struct Scsi_Host *shost = class_to_shost(device);
788 struct ata_port *ap = ata_shost_to_port(shost);
789 struct ata_link *link;
790 struct ata_device *dev;
791 enum ata_lpm_policy policy;
794 /* UNKNOWN is internal state, iterate from MAX_POWER */
795 for (policy = ATA_LPM_MAX_POWER;
796 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
797 const char *name = ata_lpm_policy_names[policy];
799 if (strncmp(name, buf, strlen(name)) == 0)
802 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
805 spin_lock_irqsave(ap->lock, flags);
807 ata_for_each_link(link, ap, EDGE) {
808 ata_for_each_dev(dev, &ap->link, ENABLED) {
809 if (dev->horkage & ATA_HORKAGE_NOLPM) {
816 ap->target_lpm_policy = policy;
817 ata_port_schedule_eh(ap);
819 spin_unlock_irqrestore(ap->lock, flags);
823 static ssize_t ata_scsi_lpm_show(struct device *dev,
824 struct device_attribute *attr, char *buf)
826 struct Scsi_Host *shost = class_to_shost(dev);
827 struct ata_port *ap = ata_shost_to_port(shost);
829 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
832 return sysfs_emit(buf, "%s\n",
833 ata_lpm_policy_names[ap->target_lpm_policy]);
835 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
836 ata_scsi_lpm_show, ata_scsi_lpm_store);
837 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
839 static ssize_t ata_ncq_prio_supported_show(struct device *device,
840 struct device_attribute *attr,
843 struct scsi_device *sdev = to_scsi_device(device);
844 struct ata_port *ap = ata_shost_to_port(sdev->host);
845 struct ata_device *dev;
846 bool ncq_prio_supported;
849 spin_lock_irq(ap->lock);
850 dev = ata_scsi_find_dev(ap, sdev);
854 ncq_prio_supported = dev->flags & ATA_DFLAG_NCQ_PRIO;
855 spin_unlock_irq(ap->lock);
857 return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_supported);
860 DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL);
861 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported);
863 static ssize_t ata_ncq_prio_enable_show(struct device *device,
864 struct device_attribute *attr,
867 struct scsi_device *sdev = to_scsi_device(device);
868 struct ata_port *ap = ata_shost_to_port(sdev->host);
869 struct ata_device *dev;
870 bool ncq_prio_enable;
873 spin_lock_irq(ap->lock);
874 dev = ata_scsi_find_dev(ap, sdev);
878 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED;
879 spin_unlock_irq(ap->lock);
881 return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_enable);
884 static ssize_t ata_ncq_prio_enable_store(struct device *device,
885 struct device_attribute *attr,
886 const char *buf, size_t len)
888 struct scsi_device *sdev = to_scsi_device(device);
890 struct ata_device *dev;
894 rc = kstrtol(buf, 10, &input);
897 if ((input < 0) || (input > 1))
900 ap = ata_shost_to_port(sdev->host);
901 dev = ata_scsi_find_dev(ap, sdev);
905 spin_lock_irq(ap->lock);
907 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
913 if (dev->flags & ATA_DFLAG_CDL_ENABLED) {
915 "CDL must be disabled to enable NCQ priority\n");
919 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLED;
921 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
925 spin_unlock_irq(ap->lock);
927 return rc ? rc : len;
930 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
931 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
932 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
934 static struct attribute *ata_ncq_sdev_attrs[] = {
935 &dev_attr_unload_heads.attr,
936 &dev_attr_ncq_prio_enable.attr,
937 &dev_attr_ncq_prio_supported.attr,
941 static const struct attribute_group ata_ncq_sdev_attr_group = {
942 .attrs = ata_ncq_sdev_attrs
945 const struct attribute_group *ata_ncq_sdev_groups[] = {
946 &ata_ncq_sdev_attr_group,
949 EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups);
952 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
953 const char *buf, size_t count)
955 struct Scsi_Host *shost = class_to_shost(dev);
956 struct ata_port *ap = ata_shost_to_port(shost);
957 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
958 return ap->ops->em_store(ap, buf, count);
963 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
966 struct Scsi_Host *shost = class_to_shost(dev);
967 struct ata_port *ap = ata_shost_to_port(shost);
969 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
970 return ap->ops->em_show(ap, buf);
973 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
974 ata_scsi_em_message_show, ata_scsi_em_message_store);
975 EXPORT_SYMBOL_GPL(dev_attr_em_message);
978 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
981 struct Scsi_Host *shost = class_to_shost(dev);
982 struct ata_port *ap = ata_shost_to_port(shost);
984 return sysfs_emit(buf, "%d\n", ap->em_message_type);
986 DEVICE_ATTR(em_message_type, S_IRUGO,
987 ata_scsi_em_message_type_show, NULL);
988 EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
991 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
994 struct scsi_device *sdev = to_scsi_device(dev);
995 struct ata_port *ap = ata_shost_to_port(sdev->host);
996 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
998 if (atadev && ap->ops->sw_activity_show &&
999 (ap->flags & ATA_FLAG_SW_ACTIVITY))
1000 return ap->ops->sw_activity_show(atadev, buf);
1005 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
1006 const char *buf, size_t count)
1008 struct scsi_device *sdev = to_scsi_device(dev);
1009 struct ata_port *ap = ata_shost_to_port(sdev->host);
1010 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
1011 enum sw_activity val;
1014 if (atadev && ap->ops->sw_activity_store &&
1015 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
1016 val = simple_strtoul(buf, NULL, 0);
1018 case OFF: case BLINK_ON: case BLINK_OFF:
1019 rc = ap->ops->sw_activity_store(atadev, val);
1028 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
1029 ata_scsi_activity_store);
1030 EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
1033 * ata_change_queue_depth - Set a device maximum queue depth
1034 * @ap: ATA port of the target device
1035 * @sdev: SCSI device to configure queue depth for
1036 * @queue_depth: new queue depth
1038 * Helper to set a device maximum queue depth, usable with both libsas
1042 int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1045 struct ata_device *dev;
1046 unsigned long flags;
1047 int max_queue_depth;
1049 spin_lock_irqsave(ap->lock, flags);
1051 dev = ata_scsi_find_dev(ap, sdev);
1052 if (!dev || queue_depth < 1 || queue_depth == sdev->queue_depth) {
1053 spin_unlock_irqrestore(ap->lock, flags);
1054 return sdev->queue_depth;
1058 * Make sure that the queue depth requested does not exceed the device
1061 max_queue_depth = min(ATA_MAX_QUEUE, sdev->host->can_queue);
1062 max_queue_depth = min(max_queue_depth, ata_id_queue_depth(dev->id));
1063 if (queue_depth > max_queue_depth) {
1064 spin_unlock_irqrestore(ap->lock, flags);
1069 * If NCQ is not supported by the device or if the target queue depth
1070 * is 1 (to disable drive side command queueing), turn off NCQ.
1072 if (queue_depth == 1 || !ata_ncq_supported(dev)) {
1073 dev->flags |= ATA_DFLAG_NCQ_OFF;
1076 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1079 spin_unlock_irqrestore(ap->lock, flags);
1081 if (queue_depth == sdev->queue_depth)
1082 return sdev->queue_depth;
1084 return scsi_change_queue_depth(sdev, queue_depth);
1086 EXPORT_SYMBOL_GPL(ata_change_queue_depth);
1089 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1090 * @sdev: SCSI device to configure queue depth for
1091 * @queue_depth: new queue depth
1093 * This is libata standard hostt->change_queue_depth callback.
1094 * SCSI will call into this callback when user tries to set queue
1098 * SCSI layer (we don't care)
1101 * Newly configured queue depth.
1103 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1105 struct ata_port *ap = ata_shost_to_port(sdev->host);
1107 return ata_change_queue_depth(ap, sdev, queue_depth);
1109 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
1112 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
1113 * @host: ATA host container for all SAS ports
1114 * @port_info: Information from low-level host driver
1115 * @shost: SCSI host that the scsi device is attached to
1118 * PCI/etc. bus probe sem.
1121 * ata_port pointer on success / NULL on failure.
1124 struct ata_port *ata_sas_port_alloc(struct ata_host *host,
1125 struct ata_port_info *port_info,
1126 struct Scsi_Host *shost)
1128 struct ata_port *ap;
1130 ap = ata_port_alloc(host);
1135 ap->lock = &host->lock;
1136 ap->pio_mask = port_info->pio_mask;
1137 ap->mwdma_mask = port_info->mwdma_mask;
1138 ap->udma_mask = port_info->udma_mask;
1139 ap->flags |= port_info->flags;
1140 ap->ops = port_info->port_ops;
1141 ap->cbl = ATA_CBL_SATA;
1142 ap->print_id = atomic_inc_return(&ata_print_id);
1146 EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
1148 int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
1150 return ata_tport_add(parent, ap);
1152 EXPORT_SYMBOL_GPL(ata_sas_tport_add);
1154 void ata_sas_tport_delete(struct ata_port *ap)
1156 ata_tport_delete(ap);
1158 EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
1161 * ata_sas_slave_configure - Default slave_config routine for libata devices
1162 * @sdev: SCSI device to configure
1163 * @ap: ATA port to which SCSI device is attached
1169 int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
1171 ata_scsi_sdev_config(sdev);
1172 ata_scsi_dev_config(sdev, ap->link.device);
1175 EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
1178 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
1179 * @cmd: SCSI command to be sent
1180 * @ap: ATA port to which the command is being sent
1183 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
1187 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1191 if (likely(ata_dev_enabled(ap->link.device)))
1192 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
1194 cmd->result = (DID_BAD_TARGET << 16);
1199 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
1202 * sata_async_notification - SATA async notification handler
1203 * @ap: ATA port where async notification is received
1205 * Handler to be called when async notification via SDB FIS is
1206 * received. This function schedules EH if necessary.
1209 * spin_lock_irqsave(host lock)
1212 * 1 if EH is scheduled, 0 otherwise.
1214 int sata_async_notification(struct ata_port *ap)
1219 if (!(ap->flags & ATA_FLAG_AN))
1222 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1224 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1226 if (!sata_pmp_attached(ap) || rc) {
1227 /* PMP is not attached or SNTF is not available */
1228 if (!sata_pmp_attached(ap)) {
1229 /* PMP is not attached. Check whether ATAPI
1230 * AN is configured. If so, notify media
1233 struct ata_device *dev = ap->link.device;
1235 if ((dev->class == ATA_DEV_ATAPI) &&
1236 (dev->flags & ATA_DFLAG_AN))
1237 ata_scsi_media_change_notify(dev);
1240 /* PMP is attached but SNTF is not available.
1241 * ATAPI async media change notification is
1242 * not used. The PMP must be reporting PHY
1243 * status change, schedule EH.
1245 ata_port_schedule_eh(ap);
1249 /* PMP is attached and SNTF is available */
1250 struct ata_link *link;
1252 /* check and notify ATAPI AN */
1253 ata_for_each_link(link, ap, EDGE) {
1254 if (!(sntf & (1 << link->pmp)))
1257 if ((link->device->class == ATA_DEV_ATAPI) &&
1258 (link->device->flags & ATA_DFLAG_AN))
1259 ata_scsi_media_change_notify(link->device);
1262 /* If PMP is reporting that PHY status of some
1263 * downstream ports has changed, schedule EH.
1265 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1266 ata_port_schedule_eh(ap);
1273 EXPORT_SYMBOL_GPL(sata_async_notification);
1276 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1277 * @dev: Device to read log page 10h from
1278 * @tag: Resulting tag of the failed command
1279 * @tf: Resulting taskfile registers of the failed command
1281 * Read log page 10h to obtain NCQ error details and clear error
1285 * Kernel thread context (may sleep).
1288 * 0 on success, -errno otherwise.
1290 static int ata_eh_read_log_10h(struct ata_device *dev,
1291 int *tag, struct ata_taskfile *tf)
1293 u8 *buf = dev->link->ap->sector_buf;
1294 unsigned int err_mask;
1298 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1303 for (i = 0; i < ATA_SECT_SIZE; i++)
1306 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1312 *tag = buf[0] & 0x1f;
1314 tf->status = buf[2];
1319 tf->device = buf[7];
1320 tf->hob_lbal = buf[8];
1321 tf->hob_lbam = buf[9];
1322 tf->hob_lbah = buf[10];
1323 tf->nsect = buf[12];
1324 tf->hob_nsect = buf[13];
1325 if (ata_id_has_ncq_autosense(dev->id) && (tf->status & ATA_SENSE))
1326 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1332 * ata_eh_read_sense_success_ncq_log - Read the sense data for successful
1334 * @link: ATA link to get sense data for
1336 * Read the sense data for successful NCQ commands log page to obtain
1337 * sense data for all NCQ commands that completed successfully with
1338 * the sense data available bit set.
1341 * Kernel thread context (may sleep).
1344 * 0 on success, -errno otherwise.
1346 int ata_eh_read_sense_success_ncq_log(struct ata_link *link)
1348 struct ata_device *dev = link->device;
1349 struct ata_port *ap = dev->link->ap;
1350 u8 *buf = ap->ncq_sense_buf;
1351 struct ata_queued_cmd *qc;
1352 unsigned int err_mask, tag;
1353 u8 *sense, sk = 0, asc = 0, ascq = 0;
1354 u64 sense_valid, val;
1357 err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2);
1360 "Failed to read Sense Data for Successful NCQ Commands log\n");
1364 /* Check the log header */
1365 val = get_unaligned_le64(&buf[0]);
1366 if ((val & 0xffff) != 1 || ((val >> 16) & 0xff) != 0x0f) {
1368 "Invalid Sense Data for Successful NCQ Commands log\n");
1372 sense_valid = (u64)buf[8] | ((u64)buf[9] << 8) |
1373 ((u64)buf[10] << 16) | ((u64)buf[11] << 24);
1375 ata_qc_for_each_raw(ap, qc, tag) {
1376 if (!(qc->flags & ATA_QCFLAG_EH) ||
1377 !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) ||
1379 ata_dev_phys_link(qc->dev) != link)
1383 * If the command does not have any sense data, clear ATA_SENSE.
1384 * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished.
1386 if (!(sense_valid & (1ULL << tag))) {
1387 qc->result_tf.status &= ~ATA_SENSE;
1391 sense = &buf[32 + 24 * tag];
1396 if (!ata_scsi_sense_is_valid(sk, asc, ascq)) {
1401 /* Set sense without also setting scsicmd->result */
1402 scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE,
1403 qc->scsicmd->sense_buffer, sk,
1405 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1408 * If we have sense data, call scsi_check_sense() in order to
1409 * set the correct SCSI ML byte (if any). No point in checking
1410 * the return value, since the command has already completed
1413 scsi_check_sense(qc->scsicmd);
1418 EXPORT_SYMBOL_GPL(ata_eh_read_sense_success_ncq_log);
1421 * ata_eh_analyze_ncq_error - analyze NCQ error
1422 * @link: ATA link to analyze NCQ error for
1424 * Read log page 10h, determine the offending qc and acquire
1425 * error status TF. For NCQ device errors, all LLDDs have to do
1426 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1430 * Kernel thread context (may sleep).
1432 void ata_eh_analyze_ncq_error(struct ata_link *link)
1434 struct ata_port *ap = link->ap;
1435 struct ata_eh_context *ehc = &link->eh_context;
1436 struct ata_device *dev = link->device;
1437 struct ata_queued_cmd *qc;
1438 struct ata_taskfile tf;
1441 /* if frozen, we can't do much */
1442 if (ata_port_is_frozen(ap))
1445 /* is it NCQ device error? */
1446 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1449 /* has LLDD analyzed already? */
1450 ata_qc_for_each_raw(ap, qc, tag) {
1451 if (!(qc->flags & ATA_QCFLAG_EH))
1458 /* okay, this error is ours */
1459 memset(&tf, 0, sizeof(tf));
1460 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1462 ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1467 if (!(link->sactive & (1 << tag))) {
1468 ata_link_err(link, "log page 10h reported inactive tag %d\n",
1473 /* we've got the perpetrator, condemn it */
1474 qc = __ata_qc_from_tag(ap, tag);
1475 memcpy(&qc->result_tf, &tf, sizeof(tf));
1476 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1477 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1480 * If the device supports NCQ autosense, ata_eh_read_log_10h() will have
1481 * stored the sense data in qc->result_tf.auxiliary.
1483 if (qc->result_tf.auxiliary) {
1484 char sense_key, asc, ascq;
1486 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1487 asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1488 ascq = qc->result_tf.auxiliary & 0xff;
1489 if (ata_scsi_sense_is_valid(sense_key, asc, ascq)) {
1490 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc,
1492 ata_scsi_set_sense_information(dev, qc->scsicmd,
1494 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1498 ata_qc_for_each_raw(ap, qc, tag) {
1499 if (!(qc->flags & ATA_QCFLAG_EH) ||
1500 qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD ||
1501 ata_dev_phys_link(qc->dev) != link)
1504 /* Skip the single QC which caused the NCQ error. */
1509 * For SATA, the STATUS and ERROR fields are shared for all NCQ
1510 * commands that were completed with the same SDB FIS.
1511 * Therefore, we have to clear the ATA_ERR bit for all QCs
1512 * except the one that caused the NCQ error.
1514 qc->result_tf.status &= ~ATA_ERR;
1515 qc->result_tf.error = 0;
1518 * If we get a NCQ error, that means that a single command was
1519 * aborted. All other failed commands for our link should be
1520 * retried and has no business of going though further scrutiny
1521 * by ata_eh_link_autopsy().
1523 qc->flags |= ATA_QCFLAG_RETRY;
1526 ehc->i.err_mask &= ~AC_ERR_DEV;
1528 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);