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 <linux/libata.h>
17 #include "libata-transport.h"
19 /* debounce timing parameters in msecs { interval, duration, timeout } */
20 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
21 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
22 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
23 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
24 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
25 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
28 * sata_scr_valid - test whether SCRs are accessible
29 * @link: ATA link to test SCR accessibility for
31 * Test whether SCRs are accessible for @link.
37 * 1 if SCRs are accessible, 0 otherwise.
39 int sata_scr_valid(struct ata_link *link)
41 struct ata_port *ap = link->ap;
43 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
45 EXPORT_SYMBOL_GPL(sata_scr_valid);
48 * sata_scr_read - read SCR register of the specified port
49 * @link: ATA link to read SCR for
51 * @val: Place to store read value
53 * Read SCR register @reg of @link into *@val. This function is
54 * guaranteed to succeed if @link is ap->link, the cable type of
55 * the port is SATA and the port implements ->scr_read.
58 * None if @link is ap->link. Kernel thread context otherwise.
61 * 0 on success, negative errno on failure.
63 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
65 if (ata_is_host_link(link)) {
66 if (sata_scr_valid(link))
67 return link->ap->ops->scr_read(link, reg, val);
71 return sata_pmp_scr_read(link, reg, val);
73 EXPORT_SYMBOL_GPL(sata_scr_read);
76 * sata_scr_write - write SCR register of the specified port
77 * @link: ATA link to write SCR for
79 * @val: value to write
81 * Write @val to SCR register @reg of @link. This function is
82 * guaranteed to succeed if @link is ap->link, the cable type of
83 * the port is SATA and the port implements ->scr_read.
86 * None if @link is ap->link. Kernel thread context otherwise.
89 * 0 on success, negative errno on failure.
91 int sata_scr_write(struct ata_link *link, int reg, u32 val)
93 if (ata_is_host_link(link)) {
94 if (sata_scr_valid(link))
95 return link->ap->ops->scr_write(link, reg, val);
99 return sata_pmp_scr_write(link, reg, val);
101 EXPORT_SYMBOL_GPL(sata_scr_write);
104 * sata_scr_write_flush - write SCR register of the specified port and flush
105 * @link: ATA link to write SCR for
107 * @val: value to write
109 * This function is identical to sata_scr_write() except that this
110 * function performs flush after writing to the register.
113 * None if @link is ap->link. Kernel thread context otherwise.
116 * 0 on success, negative errno on failure.
118 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
120 if (ata_is_host_link(link)) {
123 if (sata_scr_valid(link)) {
124 rc = link->ap->ops->scr_write(link, reg, val);
126 rc = link->ap->ops->scr_read(link, reg, &val);
132 return sata_pmp_scr_write(link, reg, val);
134 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
137 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
138 * @tf: Taskfile to convert
139 * @pmp: Port multiplier port
140 * @is_cmd: This FIS is for command
141 * @fis: Buffer into which data will output
143 * Converts a standard ATA taskfile to a Serial ATA
144 * FIS structure (Register - Host to Device).
147 * Inherited from caller.
149 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
151 fis[0] = 0x27; /* Register - Host to Device FIS */
152 fis[1] = pmp & 0xf; /* Port multiplier number*/
154 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
156 fis[2] = tf->command;
157 fis[3] = tf->feature;
164 fis[8] = tf->hob_lbal;
165 fis[9] = tf->hob_lbam;
166 fis[10] = tf->hob_lbah;
167 fis[11] = tf->hob_feature;
170 fis[13] = tf->hob_nsect;
174 fis[16] = tf->auxiliary & 0xff;
175 fis[17] = (tf->auxiliary >> 8) & 0xff;
176 fis[18] = (tf->auxiliary >> 16) & 0xff;
177 fis[19] = (tf->auxiliary >> 24) & 0xff;
179 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
182 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
183 * @fis: Buffer from which data will be input
184 * @tf: Taskfile to output
186 * Converts a serial ATA FIS structure to a standard ATA taskfile.
189 * Inherited from caller.
192 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
194 tf->command = fis[2]; /* status */
195 tf->feature = fis[3]; /* error */
202 tf->hob_lbal = fis[8];
203 tf->hob_lbam = fis[9];
204 tf->hob_lbah = fis[10];
207 tf->hob_nsect = fis[13];
209 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
212 * sata_link_debounce - debounce SATA phy status
213 * @link: ATA link to debounce SATA phy status for
214 * @params: timing parameters { interval, duration, timeout } in msec
215 * @deadline: deadline jiffies for the operation
217 * Make sure SStatus of @link reaches stable state, determined by
218 * holding the same value where DET is not 1 for @duration polled
219 * every @interval, before @timeout. Timeout constraints the
220 * beginning of the stable state. Because DET gets stuck at 1 on
221 * some controllers after hot unplugging, this functions waits
222 * until timeout then returns 0 if DET is stable at 1.
224 * @timeout is further limited by @deadline. The sooner of the
228 * Kernel thread context (may sleep)
231 * 0 on success, -errno on failure.
233 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
234 unsigned long deadline)
236 unsigned long interval = params[0];
237 unsigned long duration = params[1];
238 unsigned long last_jiffies, t;
242 t = ata_deadline(jiffies, params[2]);
243 if (time_before(t, deadline))
246 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
251 last_jiffies = jiffies;
254 ata_msleep(link->ap, interval);
255 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
261 if (cur == 1 && time_before(jiffies, deadline))
263 if (time_after(jiffies,
264 ata_deadline(last_jiffies, duration)))
269 /* unstable, start over */
271 last_jiffies = jiffies;
273 /* Check deadline. If debouncing failed, return
274 * -EPIPE to tell upper layer to lower link speed.
276 if (time_after(jiffies, deadline))
280 EXPORT_SYMBOL_GPL(sata_link_debounce);
283 * sata_link_resume - resume SATA link
284 * @link: ATA link to resume SATA
285 * @params: timing parameters { interval, duration, timeout } in msec
286 * @deadline: deadline jiffies for the operation
288 * Resume SATA phy @link and debounce it.
291 * Kernel thread context (may sleep)
294 * 0 on success, -errno on failure.
296 int sata_link_resume(struct ata_link *link, const unsigned long *params,
297 unsigned long deadline)
299 int tries = ATA_LINK_RESUME_TRIES;
300 u32 scontrol, serror;
303 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
307 * Writes to SControl sometimes get ignored under certain
308 * controllers (ata_piix SIDPR). Make sure DET actually is
312 scontrol = (scontrol & 0x0f0) | 0x300;
313 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
316 * Some PHYs react badly if SStatus is pounded
317 * immediately after resuming. Delay 200ms before
320 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
321 ata_msleep(link->ap, 200);
323 /* is SControl restored correctly? */
324 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
326 } while ((scontrol & 0xf0f) != 0x300 && --tries);
328 if ((scontrol & 0xf0f) != 0x300) {
329 ata_link_warn(link, "failed to resume link (SControl %X)\n",
334 if (tries < ATA_LINK_RESUME_TRIES)
335 ata_link_warn(link, "link resume succeeded after %d retries\n",
336 ATA_LINK_RESUME_TRIES - tries);
338 if ((rc = sata_link_debounce(link, params, deadline)))
341 /* clear SError, some PHYs require this even for SRST to work */
342 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
343 rc = sata_scr_write(link, SCR_ERROR, serror);
345 return rc != -EINVAL ? rc : 0;
347 EXPORT_SYMBOL_GPL(sata_link_resume);
350 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
351 * @link: ATA link to manipulate SControl for
352 * @policy: LPM policy to configure
353 * @spm_wakeup: initiate LPM transition to active state
355 * Manipulate the IPM field of the SControl register of @link
356 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
357 * @spm_wakeup is %true, the SPM field is manipulated to wake up
358 * the link. This function also clears PHYRDY_CHG before
365 * 0 on success, -errno otherwise.
367 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
370 struct ata_eh_context *ehc = &link->eh_context;
371 bool woken_up = false;
375 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
380 case ATA_LPM_MAX_POWER:
381 /* disable all LPM transitions */
382 scontrol |= (0x7 << 8);
383 /* initiate transition to active state */
385 scontrol |= (0x4 << 12);
389 case ATA_LPM_MED_POWER:
390 /* allow LPM to PARTIAL */
391 scontrol &= ~(0x1 << 8);
392 scontrol |= (0x6 << 8);
394 case ATA_LPM_MED_POWER_WITH_DIPM:
395 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
396 case ATA_LPM_MIN_POWER:
397 if (ata_link_nr_enabled(link) > 0)
398 /* no restrictions on LPM transitions */
399 scontrol &= ~(0x7 << 8);
401 /* empty port, power off */
403 scontrol |= (0x1 << 2);
410 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
414 /* give the link time to transit out of LPM state */
418 /* clear PHYRDY_CHG from SError */
419 ehc->i.serror &= ~SERR_PHYRDY_CHG;
420 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
422 EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
424 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
426 struct ata_link *host_link = &link->ap->link;
427 u32 limit, target, spd;
429 limit = link->sata_spd_limit;
431 /* Don't configure downstream link faster than upstream link.
432 * It doesn't speed up anything and some PMPs choke on such
435 if (!ata_is_host_link(link) && host_link->sata_spd)
436 limit &= (1 << host_link->sata_spd) - 1;
438 if (limit == UINT_MAX)
443 spd = (*scontrol >> 4) & 0xf;
444 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
446 return spd != target;
450 * sata_set_spd_needed - is SATA spd configuration needed
451 * @link: Link in question
453 * Test whether the spd limit in SControl matches
454 * @link->sata_spd_limit. This function is used to determine
455 * whether hardreset is necessary to apply SATA spd
459 * Inherited from caller.
462 * 1 if SATA spd configuration is needed, 0 otherwise.
464 static int sata_set_spd_needed(struct ata_link *link)
468 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
471 return __sata_set_spd_needed(link, &scontrol);
475 * sata_set_spd - set SATA spd according to spd limit
476 * @link: Link to set SATA spd for
478 * Set SATA spd of @link according to sata_spd_limit.
481 * Inherited from caller.
484 * 0 if spd doesn't need to be changed, 1 if spd has been
485 * changed. Negative errno if SCR registers are inaccessible.
487 int sata_set_spd(struct ata_link *link)
492 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
495 if (!__sata_set_spd_needed(link, &scontrol))
498 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
503 EXPORT_SYMBOL_GPL(sata_set_spd);
506 * sata_link_hardreset - reset link via SATA phy reset
507 * @link: link to reset
508 * @timing: timing parameters { interval, duration, timeout } in msec
509 * @deadline: deadline jiffies for the operation
510 * @online: optional out parameter indicating link onlineness
511 * @check_ready: optional callback to check link readiness
513 * SATA phy-reset @link using DET bits of SControl register.
514 * After hardreset, link readiness is waited upon using
515 * ata_wait_ready() if @check_ready is specified. LLDs are
516 * allowed to not specify @check_ready and wait itself after this
517 * function returns. Device classification is LLD's
520 * *@online is set to one iff reset succeeded and @link is online
524 * Kernel thread context (may sleep)
527 * 0 on success, -errno otherwise.
529 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
530 unsigned long deadline,
531 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);
613 DPRINTK("EXIT, rc=%d\n", rc);
616 EXPORT_SYMBOL_GPL(sata_link_hardreset);
619 * ata_qc_complete_multiple - Complete multiple qcs successfully
620 * @ap: port in question
621 * @qc_active: new qc_active mask
623 * Complete in-flight commands. This functions is meant to be
624 * called from low-level driver's interrupt routine to complete
625 * requests normally. ap->qc_active and @qc_active is compared
626 * and commands are completed accordingly.
628 * Always use this function when completing multiple NCQ commands
629 * from IRQ handlers instead of calling ata_qc_complete()
630 * multiple times to keep IRQ expect status properly in sync.
633 * spin_lock_irqsave(host lock)
636 * Number of completed commands on success, -errno otherwise.
638 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
640 u64 done_mask, ap_qc_active = ap->qc_active;
644 * If the internal tag is set on ap->qc_active, then we care about
645 * bit0 on the passed in qc_active mask. Move that bit up to match
648 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
649 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
650 qc_active ^= qc_active & 0x01;
653 done_mask = ap_qc_active ^ qc_active;
655 if (unlikely(done_mask & qc_active)) {
656 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
657 ap->qc_active, qc_active);
662 struct ata_queued_cmd *qc;
663 unsigned int tag = __ffs64(done_mask);
665 qc = ata_qc_from_tag(ap, tag);
670 done_mask &= ~(1ULL << tag);
675 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
678 * ata_slave_link_init - initialize slave link
679 * @ap: port to initialize slave link for
681 * Create and initialize slave link for @ap. This enables slave
682 * link handling on the port.
684 * In libata, a port contains links and a link contains devices.
685 * There is single host link but if a PMP is attached to it,
686 * there can be multiple fan-out links. On SATA, there's usually
687 * a single device connected to a link but PATA and SATA
688 * controllers emulating TF based interface can have two - master
691 * However, there are a few controllers which don't fit into this
692 * abstraction too well - SATA controllers which emulate TF
693 * interface with both master and slave devices but also have
694 * separate SCR register sets for each device. These controllers
695 * need separate links for physical link handling
696 * (e.g. onlineness, link speed) but should be treated like a
697 * traditional M/S controller for everything else (e.g. command
700 * slave_link is libata's way of handling this class of
701 * controllers without impacting core layer too much. For
702 * anything other than physical link handling, the default host
703 * link is used for both master and slave. For physical link
704 * handling, separate @ap->slave_link is used. All dirty details
705 * are implemented inside libata core layer. From LLD's POV, the
706 * only difference is that prereset, hardreset and postreset are
707 * called once more for the slave link, so the reset sequence
708 * looks like the following.
710 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
711 * softreset(M) -> postreset(M) -> postreset(S)
713 * Note that softreset is called only for the master. Softreset
714 * resets both M/S by definition, so SRST on master should handle
715 * both (the standard method will work just fine).
718 * Should be called before host is registered.
721 * 0 on success, -errno on failure.
723 int ata_slave_link_init(struct ata_port *ap)
725 struct ata_link *link;
727 WARN_ON(ap->slave_link);
728 WARN_ON(ap->flags & ATA_FLAG_PMP);
730 link = kzalloc(sizeof(*link), GFP_KERNEL);
734 ata_link_init(ap, link, 1);
735 ap->slave_link = link;
738 EXPORT_SYMBOL_GPL(ata_slave_link_init);
741 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
742 * @link: Link receiving the event
744 * Test whether the received PHY event has to be ignored or not.
750 * True if the event has to be ignored.
752 bool sata_lpm_ignore_phy_events(struct ata_link *link)
754 unsigned long lpm_timeout = link->last_lpm_change +
755 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
757 /* if LPM is enabled, PHYRDY doesn't mean anything */
758 if (link->lpm_policy > ATA_LPM_MAX_POWER)
761 /* ignore the first PHY event after the LPM policy changed
762 * as it is might be spurious
764 if ((link->flags & ATA_LFLAG_CHANGED) &&
765 time_before(jiffies, lpm_timeout))
770 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
772 static const char *ata_lpm_policy_names[] = {
773 [ATA_LPM_UNKNOWN] = "max_performance",
774 [ATA_LPM_MAX_POWER] = "max_performance",
775 [ATA_LPM_MED_POWER] = "medium_power",
776 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
777 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
778 [ATA_LPM_MIN_POWER] = "min_power",
781 static ssize_t ata_scsi_lpm_store(struct device *device,
782 struct device_attribute *attr,
783 const char *buf, size_t count)
785 struct Scsi_Host *shost = class_to_shost(device);
786 struct ata_port *ap = ata_shost_to_port(shost);
787 struct ata_link *link;
788 struct ata_device *dev;
789 enum ata_lpm_policy policy;
792 /* UNKNOWN is internal state, iterate from MAX_POWER */
793 for (policy = ATA_LPM_MAX_POWER;
794 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
795 const char *name = ata_lpm_policy_names[policy];
797 if (strncmp(name, buf, strlen(name)) == 0)
800 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
803 spin_lock_irqsave(ap->lock, flags);
805 ata_for_each_link(link, ap, EDGE) {
806 ata_for_each_dev(dev, &ap->link, ENABLED) {
807 if (dev->horkage & ATA_HORKAGE_NOLPM) {
814 ap->target_lpm_policy = policy;
815 ata_port_schedule_eh(ap);
817 spin_unlock_irqrestore(ap->lock, flags);
821 static ssize_t ata_scsi_lpm_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
824 struct Scsi_Host *shost = class_to_shost(dev);
825 struct ata_port *ap = ata_shost_to_port(shost);
827 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
830 return snprintf(buf, PAGE_SIZE, "%s\n",
831 ata_lpm_policy_names[ap->target_lpm_policy]);
833 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
834 ata_scsi_lpm_show, ata_scsi_lpm_store);
835 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
837 static ssize_t ata_ncq_prio_enable_show(struct device *device,
838 struct device_attribute *attr,
841 struct scsi_device *sdev = to_scsi_device(device);
842 struct ata_port *ap = ata_shost_to_port(sdev->host);
843 struct ata_device *dev;
844 bool ncq_prio_enable;
847 spin_lock_irq(ap->lock);
848 dev = ata_scsi_find_dev(ap, sdev);
852 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
853 spin_unlock_irq(ap->lock);
855 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
858 static ssize_t ata_ncq_prio_enable_store(struct device *device,
859 struct device_attribute *attr,
860 const char *buf, size_t len)
862 struct scsi_device *sdev = to_scsi_device(device);
864 struct ata_device *dev;
868 rc = kstrtol(buf, 10, &input);
871 if ((input < 0) || (input > 1))
874 ap = ata_shost_to_port(sdev->host);
875 dev = ata_scsi_find_dev(ap, sdev);
879 spin_lock_irq(ap->lock);
881 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
887 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
889 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
892 spin_unlock_irq(ap->lock);
894 return rc ? rc : len;
897 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
898 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
899 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
901 struct device_attribute *ata_ncq_sdev_attrs[] = {
902 &dev_attr_unload_heads,
903 &dev_attr_ncq_prio_enable,
906 EXPORT_SYMBOL_GPL(ata_ncq_sdev_attrs);
909 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
910 const char *buf, size_t count)
912 struct Scsi_Host *shost = class_to_shost(dev);
913 struct ata_port *ap = ata_shost_to_port(shost);
914 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
915 return ap->ops->em_store(ap, buf, count);
920 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
923 struct Scsi_Host *shost = class_to_shost(dev);
924 struct ata_port *ap = ata_shost_to_port(shost);
926 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
927 return ap->ops->em_show(ap, buf);
930 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
931 ata_scsi_em_message_show, ata_scsi_em_message_store);
932 EXPORT_SYMBOL_GPL(dev_attr_em_message);
935 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
938 struct Scsi_Host *shost = class_to_shost(dev);
939 struct ata_port *ap = ata_shost_to_port(shost);
941 return snprintf(buf, 23, "%d\n", ap->em_message_type);
943 DEVICE_ATTR(em_message_type, S_IRUGO,
944 ata_scsi_em_message_type_show, NULL);
945 EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
948 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
951 struct scsi_device *sdev = to_scsi_device(dev);
952 struct ata_port *ap = ata_shost_to_port(sdev->host);
953 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
955 if (atadev && ap->ops->sw_activity_show &&
956 (ap->flags & ATA_FLAG_SW_ACTIVITY))
957 return ap->ops->sw_activity_show(atadev, buf);
962 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
963 const char *buf, size_t count)
965 struct scsi_device *sdev = to_scsi_device(dev);
966 struct ata_port *ap = ata_shost_to_port(sdev->host);
967 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
968 enum sw_activity val;
971 if (atadev && ap->ops->sw_activity_store &&
972 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
973 val = simple_strtoul(buf, NULL, 0);
975 case OFF: case BLINK_ON: case BLINK_OFF:
976 rc = ap->ops->sw_activity_store(atadev, val);
985 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
986 ata_scsi_activity_store);
987 EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
990 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
991 * @ap: ATA port to which the device change the queue depth
992 * @sdev: SCSI device to configure queue depth for
993 * @queue_depth: new queue depth
995 * libsas and libata have different approaches for associating a sdev to
999 int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1002 struct ata_device *dev;
1003 unsigned long flags;
1005 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1006 return sdev->queue_depth;
1008 dev = ata_scsi_find_dev(ap, sdev);
1009 if (!dev || !ata_dev_enabled(dev))
1010 return sdev->queue_depth;
1013 spin_lock_irqsave(ap->lock, flags);
1014 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1015 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1016 dev->flags |= ATA_DFLAG_NCQ_OFF;
1019 spin_unlock_irqrestore(ap->lock, flags);
1021 /* limit and apply queue depth */
1022 queue_depth = min(queue_depth, sdev->host->can_queue);
1023 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1024 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1026 if (sdev->queue_depth == queue_depth)
1029 return scsi_change_queue_depth(sdev, queue_depth);
1031 EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
1034 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1035 * @sdev: SCSI device to configure queue depth for
1036 * @queue_depth: new queue depth
1038 * This is libata standard hostt->change_queue_depth callback.
1039 * SCSI will call into this callback when user tries to set queue
1043 * SCSI layer (we don't care)
1046 * Newly configured queue depth.
1048 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1050 struct ata_port *ap = ata_shost_to_port(sdev->host);
1052 return __ata_change_queue_depth(ap, sdev, queue_depth);
1054 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
1057 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
1058 * @host: ATA host container for all SAS ports
1059 * @port_info: Information from low-level host driver
1060 * @shost: SCSI host that the scsi device is attached to
1063 * PCI/etc. bus probe sem.
1066 * ata_port pointer on success / NULL on failure.
1069 struct ata_port *ata_sas_port_alloc(struct ata_host *host,
1070 struct ata_port_info *port_info,
1071 struct Scsi_Host *shost)
1073 struct ata_port *ap;
1075 ap = ata_port_alloc(host);
1080 ap->lock = &host->lock;
1081 ap->pio_mask = port_info->pio_mask;
1082 ap->mwdma_mask = port_info->mwdma_mask;
1083 ap->udma_mask = port_info->udma_mask;
1084 ap->flags |= port_info->flags;
1085 ap->ops = port_info->port_ops;
1086 ap->cbl = ATA_CBL_SATA;
1090 EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
1093 * ata_sas_port_start - Set port up for dma.
1094 * @ap: Port to initialize
1096 * Called just after data structures for each port are
1099 * May be used as the port_start() entry in ata_port_operations.
1102 * Inherited from caller.
1104 int ata_sas_port_start(struct ata_port *ap)
1107 * the port is marked as frozen at allocation time, but if we don't
1108 * have new eh, we won't thaw it
1110 if (!ap->ops->error_handler)
1111 ap->pflags &= ~ATA_PFLAG_FROZEN;
1114 EXPORT_SYMBOL_GPL(ata_sas_port_start);
1117 * ata_sas_port_stop - Undo ata_sas_port_start()
1118 * @ap: Port to shut down
1120 * May be used as the port_stop() entry in ata_port_operations.
1123 * Inherited from caller.
1126 void ata_sas_port_stop(struct ata_port *ap)
1129 EXPORT_SYMBOL_GPL(ata_sas_port_stop);
1132 * ata_sas_async_probe - simply schedule probing and return
1133 * @ap: Port to probe
1135 * For batch scheduling of probe for sas attached ata devices, assumes
1136 * the port has already been through ata_sas_port_init()
1138 void ata_sas_async_probe(struct ata_port *ap)
1140 __ata_port_probe(ap);
1142 EXPORT_SYMBOL_GPL(ata_sas_async_probe);
1144 int ata_sas_sync_probe(struct ata_port *ap)
1146 return ata_port_probe(ap);
1148 EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
1152 * ata_sas_port_init - Initialize a SATA device
1153 * @ap: SATA port to initialize
1156 * PCI/etc. bus probe sem.
1159 * Zero on success, non-zero on error.
1162 int ata_sas_port_init(struct ata_port *ap)
1164 int rc = ap->ops->port_start(ap);
1168 ap->print_id = atomic_inc_return(&ata_print_id);
1171 EXPORT_SYMBOL_GPL(ata_sas_port_init);
1173 int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
1175 return ata_tport_add(parent, ap);
1177 EXPORT_SYMBOL_GPL(ata_sas_tport_add);
1179 void ata_sas_tport_delete(struct ata_port *ap)
1181 ata_tport_delete(ap);
1183 EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
1186 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
1187 * @ap: SATA port to destroy
1191 void ata_sas_port_destroy(struct ata_port *ap)
1193 if (ap->ops->port_stop)
1194 ap->ops->port_stop(ap);
1197 EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
1200 * ata_sas_slave_configure - Default slave_config routine for libata devices
1201 * @sdev: SCSI device to configure
1202 * @ap: ATA port to which SCSI device is attached
1208 int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
1210 ata_scsi_sdev_config(sdev);
1211 ata_scsi_dev_config(sdev, ap->link.device);
1214 EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
1217 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
1218 * @cmd: SCSI command to be sent
1219 * @ap: ATA port to which the command is being sent
1222 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
1226 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1230 ata_scsi_dump_cdb(ap, cmd);
1232 if (likely(ata_dev_enabled(ap->link.device)))
1233 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
1235 cmd->result = (DID_BAD_TARGET << 16);
1236 cmd->scsi_done(cmd);
1240 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
1242 int ata_sas_allocate_tag(struct ata_port *ap)
1244 unsigned int max_queue = ap->host->n_tags;
1245 unsigned int i, tag;
1247 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
1248 tag = tag < max_queue ? tag : 0;
1250 /* the last tag is reserved for internal command. */
1251 if (ata_tag_internal(tag))
1254 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
1255 ap->sas_last_tag = tag;
1262 void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
1264 clear_bit(tag, &ap->sas_tag_allocated);
1268 * sata_async_notification - SATA async notification handler
1269 * @ap: ATA port where async notification is received
1271 * Handler to be called when async notification via SDB FIS is
1272 * received. This function schedules EH if necessary.
1275 * spin_lock_irqsave(host lock)
1278 * 1 if EH is scheduled, 0 otherwise.
1280 int sata_async_notification(struct ata_port *ap)
1285 if (!(ap->flags & ATA_FLAG_AN))
1288 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1290 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1292 if (!sata_pmp_attached(ap) || rc) {
1293 /* PMP is not attached or SNTF is not available */
1294 if (!sata_pmp_attached(ap)) {
1295 /* PMP is not attached. Check whether ATAPI
1296 * AN is configured. If so, notify media
1299 struct ata_device *dev = ap->link.device;
1301 if ((dev->class == ATA_DEV_ATAPI) &&
1302 (dev->flags & ATA_DFLAG_AN))
1303 ata_scsi_media_change_notify(dev);
1306 /* PMP is attached but SNTF is not available.
1307 * ATAPI async media change notification is
1308 * not used. The PMP must be reporting PHY
1309 * status change, schedule EH.
1311 ata_port_schedule_eh(ap);
1315 /* PMP is attached and SNTF is available */
1316 struct ata_link *link;
1318 /* check and notify ATAPI AN */
1319 ata_for_each_link(link, ap, EDGE) {
1320 if (!(sntf & (1 << link->pmp)))
1323 if ((link->device->class == ATA_DEV_ATAPI) &&
1324 (link->device->flags & ATA_DFLAG_AN))
1325 ata_scsi_media_change_notify(link->device);
1328 /* If PMP is reporting that PHY status of some
1329 * downstream ports has changed, schedule EH.
1331 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1332 ata_port_schedule_eh(ap);
1339 EXPORT_SYMBOL_GPL(sata_async_notification);
1342 * ata_eh_read_log_10h - Read log page 10h for NCQ error details
1343 * @dev: Device to read log page 10h from
1344 * @tag: Resulting tag of the failed command
1345 * @tf: Resulting taskfile registers of the failed command
1347 * Read log page 10h to obtain NCQ error details and clear error
1351 * Kernel thread context (may sleep).
1354 * 0 on success, -errno otherwise.
1356 static int ata_eh_read_log_10h(struct ata_device *dev,
1357 int *tag, struct ata_taskfile *tf)
1359 u8 *buf = dev->link->ap->sector_buf;
1360 unsigned int err_mask;
1364 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1369 for (i = 0; i < ATA_SECT_SIZE; i++)
1372 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1378 *tag = buf[0] & 0x1f;
1380 tf->command = buf[2];
1381 tf->feature = buf[3];
1385 tf->device = buf[7];
1386 tf->hob_lbal = buf[8];
1387 tf->hob_lbam = buf[9];
1388 tf->hob_lbah = buf[10];
1389 tf->nsect = buf[12];
1390 tf->hob_nsect = buf[13];
1391 if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
1392 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1398 * ata_eh_analyze_ncq_error - analyze NCQ error
1399 * @link: ATA link to analyze NCQ error for
1401 * Read log page 10h, determine the offending qc and acquire
1402 * error status TF. For NCQ device errors, all LLDDs have to do
1403 * is setting AC_ERR_DEV in ehi->err_mask. This function takes
1407 * Kernel thread context (may sleep).
1409 void ata_eh_analyze_ncq_error(struct ata_link *link)
1411 struct ata_port *ap = link->ap;
1412 struct ata_eh_context *ehc = &link->eh_context;
1413 struct ata_device *dev = link->device;
1414 struct ata_queued_cmd *qc;
1415 struct ata_taskfile tf;
1418 /* if frozen, we can't do much */
1419 if (ap->pflags & ATA_PFLAG_FROZEN)
1422 /* is it NCQ device error? */
1423 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1426 /* has LLDD analyzed already? */
1427 ata_qc_for_each_raw(ap, qc, tag) {
1428 if (!(qc->flags & ATA_QCFLAG_FAILED))
1435 /* okay, this error is ours */
1436 memset(&tf, 0, sizeof(tf));
1437 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1439 ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1444 if (!(link->sactive & (1 << tag))) {
1445 ata_link_err(link, "log page 10h reported inactive tag %d\n",
1450 /* we've got the perpetrator, condemn it */
1451 qc = __ata_qc_from_tag(ap, tag);
1452 memcpy(&qc->result_tf, &tf, sizeof(tf));
1453 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1454 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1455 if (dev->class == ATA_DEV_ZAC &&
1456 ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) {
1457 char sense_key, asc, ascq;
1459 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1460 asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1461 ascq = qc->result_tf.auxiliary & 0xff;
1462 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1463 ata_scsi_set_sense_information(dev, qc->scsicmd,
1465 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1468 ehc->i.err_mask &= ~AC_ERR_DEV;
1470 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);