2 * libata-pmp.c - libata port multiplier support
4 * Copyright (c) 2007 SUSE Linux Products GmbH
5 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
7 * This file is released under the GPLv2.
10 #include <linux/kernel.h>
11 #include <linux/libata.h>
15 * sata_pmp_read - read PMP register
16 * @link: link to read PMP register for
17 * @reg: register to read
18 * @r_val: resulting value
20 * Wrapper around ap->ops->pmp_read to make it easier to call and
21 * nomarlize error return value.
24 * Kernel thread context (may sleep).
27 * 0 on success, -errno on failure.
29 static int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
31 struct ata_port *ap = link->ap;
32 struct ata_device *pmp_dev = ap->link.device;
37 rc = ap->ops->pmp_read(pmp_dev, link->pmp, reg, r_val);
44 * sata_pmp_write - write PMP register
45 * @link: link to write PMP register for
46 * @reg: register to write
47 * @r_val: value to write
49 * Wrapper around ap->ops->pmp_write to make it easier to call
50 * and nomarlize error return value.
53 * Kernel thread context (may sleep).
56 * 0 on success, -errno on failure.
58 static int sata_pmp_write(struct ata_link *link, int reg, u32 val)
60 struct ata_port *ap = link->ap;
61 struct ata_device *pmp_dev = ap->link.device;
66 rc = ap->ops->pmp_write(pmp_dev, link->pmp, reg, val);
73 * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
74 * @qc: ATA command in question
76 * A host which has command switching PMP support cannot issue
77 * commands to multiple links simultaneously.
80 * spin_lock_irqsave(host lock)
83 * ATA_DEFER_* if deferring is needed, 0 otherwise.
85 int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
87 struct ata_link *link = qc->dev->link;
88 struct ata_port *ap = link->ap;
90 if (ap->excl_link == NULL || ap->excl_link == link) {
91 if (ap->nr_active_links == 0 || ata_link_active(link)) {
92 qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
93 return ata_std_qc_defer(qc);
99 return ATA_DEFER_PORT;
103 * sata_pmp_read_init_tf - initialize TF for PMP read
104 * @tf: taskfile to initialize
106 * @pmp: port multiplier port number
107 * @reg: register to read
109 * Initialize @tf for PMP read command.
114 void sata_pmp_read_init_tf(struct ata_taskfile *tf,
115 struct ata_device *dev, int pmp, int reg)
117 ata_tf_init(dev, tf);
118 tf->command = ATA_CMD_PMP_READ;
119 tf->protocol = ATA_PROT_NODATA;
120 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
126 * sata_pmp_read_val - extract PMP read result from TF
129 * Determine PMP read result from @tf.
134 u32 sata_pmp_read_val(const struct ata_taskfile *tf)
136 return tf->nsect | tf->lbal << 8 | tf->lbam << 16 | tf->lbah << 24;
140 * sata_pmp_read_init_tf - initialize TF for PMP write
141 * @tf: taskfile to initialize
143 * @pmp: port multiplier port number
144 * @reg: register to read
145 * @val: value to write
147 * Initialize @tf for PMP write command.
152 void sata_pmp_write_init_tf(struct ata_taskfile *tf,
153 struct ata_device *dev, int pmp, int reg, u32 val)
155 ata_tf_init(dev, tf);
156 tf->command = ATA_CMD_PMP_WRITE;
157 tf->protocol = ATA_PROT_NODATA;
158 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
161 tf->nsect = val & 0xff;
162 tf->lbal = (val >> 8) & 0xff;
163 tf->lbam = (val >> 16) & 0xff;
164 tf->lbah = (val >> 24) & 0xff;
168 * sata_pmp_scr_read - read PSCR
169 * @link: ATA link to read PSCR for
171 * @r_val: resulting value
173 * Read PSCR @reg into @r_val for @link, to be called from
177 * Kernel thread context (may sleep).
180 * 0 on success, -errno on failure.
182 int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
184 if (reg > SATA_PMP_PSCR_CONTROL)
187 return sata_pmp_read(link, reg, r_val);
191 * sata_pmp_scr_write - write PSCR
192 * @link: ATA link to write PSCR for
193 * @reg: PSCR to write
194 * @val: value to be written
196 * Write @val to PSCR @reg for @link, to be called from
197 * ata_scr_write() and ata_scr_write_flush().
200 * Kernel thread context (may sleep).
203 * 0 on success, -errno on failure.
205 int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
207 if (reg > SATA_PMP_PSCR_CONTROL)
210 return sata_pmp_write(link, reg, val);
214 * sata_pmp_std_prereset - prepare PMP link for reset
215 * @link: link to be reset
216 * @deadline: deadline jiffies for the operation
218 * @link is about to be reset. Initialize it.
221 * Kernel thread context (may sleep)
224 * 0 on success, -errno otherwise.
226 int sata_pmp_std_prereset(struct ata_link *link, unsigned long deadline)
228 struct ata_eh_context *ehc = &link->eh_context;
229 const unsigned long *timing = sata_ehc_deb_timing(ehc);
233 if (link->flags & ATA_LFLAG_NO_SRST)
234 ehc->i.action |= ATA_EH_HARDRESET;
236 /* handle link resume */
237 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
238 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
239 ehc->i.action |= ATA_EH_HARDRESET;
241 /* if we're about to do hardreset, nothing more to do */
242 if (ehc->i.action & ATA_EH_HARDRESET)
246 rc = sata_link_resume(link, timing, deadline);
248 /* phy resume failed */
249 ata_link_printk(link, KERN_WARNING, "failed to resume link "
250 "for reset (errno=%d)\n", rc);
254 /* clear SError bits including .X which blocks the port when set */
255 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
257 ata_link_printk(link, KERN_ERR,
258 "failed to clear SError (errno=%d)\n", rc);
266 * sata_pmp_std_hardreset - standard hardreset method for PMP link
267 * @link: link to be reset
268 * @class: resulting class of attached device
269 * @deadline: deadline jiffies for the operation
271 * Hardreset PMP port @link. Note that this function doesn't
272 * wait for BSY clearance. There simply isn't a generic way to
273 * wait the event. Instead, this function return -EAGAIN thus
274 * telling libata-EH to followup with softreset.
277 * Kernel thread context (may sleep)
280 * 0 on success, -errno otherwise.
282 int sata_pmp_std_hardreset(struct ata_link *link, unsigned int *class,
283 unsigned long deadline)
285 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
292 rc = sata_link_hardreset(link, timing, deadline);
294 ata_link_printk(link, KERN_ERR,
295 "COMRESET failed (errno=%d)\n", rc);
299 /* clear SError bits including .X which blocks the port when set */
300 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
302 ata_link_printk(link, KERN_ERR, "failed to clear SError "
303 "during hardreset (errno=%d)\n", rc);
307 /* if device is present, follow up with srst to wait for !BSY */
308 if (ata_link_online(link))
311 /* if SCR isn't accessible, we need to reset the PMP */
312 if (rc && rc != -EAGAIN && sata_scr_read(link, SCR_STATUS, &tmp))
315 DPRINTK("EXIT, rc=%d\n", rc);
320 * ata_std_postreset - standard postreset method for PMP link
321 * @link: the target ata_link
322 * @classes: classes of attached devices
324 * This function is invoked after a successful reset. Note that
325 * the device might have been reset more than once using
326 * different reset methods before postreset is invoked.
329 * Kernel thread context (may sleep)
331 void sata_pmp_std_postreset(struct ata_link *link, unsigned int *class)
338 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
339 sata_scr_write(link, SCR_ERROR, serror);
341 /* print link status */
342 sata_print_link_status(link);
348 * sata_pmp_read_gscr - read GSCR block of SATA PMP
350 * @gscr: buffer to read GSCR block into
352 * Read selected PMP GSCRs from the PMP at @dev. This will serve
353 * as configuration and identification info for the PMP.
356 * Kernel thread context (may sleep).
359 * 0 on success, -errno on failure.
361 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
363 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
366 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
367 int reg = gscr_to_read[i];
369 rc = sata_pmp_read(dev->link, reg, &gscr[reg]);
371 ata_dev_printk(dev, KERN_ERR, "failed to read "
372 "PMP GSCR[%d] (errno=%d)\n", reg, rc);
380 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
382 u32 rev = gscr[SATA_PMP_GSCR_REV];
391 static int sata_pmp_configure(struct ata_device *dev, int print_info)
393 struct ata_port *ap = dev->link->ap;
394 u32 *gscr = dev->gscr;
398 nr_ports = sata_pmp_gscr_ports(gscr);
400 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
402 reason = "invalid nr_ports";
406 if ((ap->flags & ATA_FLAG_AN) &&
407 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
408 dev->flags |= ATA_DFLAG_AN;
410 /* monitor SERR_PHYRDY_CHG on fan-out ports */
411 rc = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN, SERR_PHYRDY_CHG);
413 reason = "failed to write GSCR_ERROR_EN";
417 /* turn off notification till fan-out ports are reset and configured */
418 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
419 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
421 rc = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
422 gscr[SATA_PMP_GSCR_FEAT_EN]);
424 reason = "failed to write GSCR_FEAT_EN";
430 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
431 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
432 sata_pmp_spec_rev_str(gscr),
433 sata_pmp_gscr_vendor(gscr),
434 sata_pmp_gscr_devid(gscr),
435 sata_pmp_gscr_rev(gscr),
436 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
437 gscr[SATA_PMP_GSCR_FEAT]);
439 if (!(dev->flags & ATA_DFLAG_AN))
440 ata_dev_printk(dev, KERN_INFO,
441 "Asynchronous notification not supported, "
442 "hotplug won't\n work on fan-out "
443 "ports. Use warm-plug instead.\n");
449 ata_dev_printk(dev, KERN_ERR,
450 "failed to configure Port Multiplier (%s)\n", reason);
454 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
456 struct ata_link *pmp_link = ap->pmp_link;
460 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
465 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
466 ata_link_init(ap, &pmp_link[i], i);
468 ap->pmp_link = pmp_link;
471 for (i = 0; i < nr_ports; i++) {
472 struct ata_link *link = &pmp_link[i];
473 struct ata_eh_context *ehc = &link->eh_context;
476 ehc->i.probe_mask |= 1;
477 ehc->i.action |= ATA_EH_SOFTRESET;
478 ehc->i.flags |= ATA_EHI_RESUME_LINK;
484 static void sata_pmp_quirks(struct ata_port *ap)
486 u32 *gscr = ap->link.device->gscr;
487 u16 vendor = sata_pmp_gscr_vendor(gscr);
488 u16 devid = sata_pmp_gscr_devid(gscr);
489 struct ata_link *link;
491 if (vendor == 0x1095 && devid == 0x3726) {
493 ata_port_for_each_link(link, ap) {
494 /* SError.N need a kick in the ass to get working */
495 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
497 /* class code report is unreliable */
499 link->flags |= ATA_LFLAG_ASSUME_ATA;
501 /* port 5 is for SEMB device and it doesn't like SRST */
503 link->flags |= ATA_LFLAG_NO_SRST |
504 ATA_LFLAG_ASSUME_SEMB;
506 } else if (vendor == 0x1095 && devid == 0x4723) {
508 ata_port_for_each_link(link, ap) {
509 /* SError.N need a kick in the ass to get working */
510 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
512 /* class code report is unreliable */
514 link->flags |= ATA_LFLAG_ASSUME_ATA;
516 /* the config device at port 2 locks up on SRST */
518 link->flags |= ATA_LFLAG_NO_SRST |
519 ATA_LFLAG_ASSUME_ATA;
521 } else if (vendor == 0x1095 && devid == 0x4726) {
523 ata_port_for_each_link(link, ap) {
524 /* SError.N need a kick in the ass to get working */
525 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
527 /* class code report is unreliable */
529 link->flags |= ATA_LFLAG_ASSUME_ATA;
531 /* The config device, which can be either at
532 * port 0 or 5, locks up on SRST.
534 if (link->pmp == 0 || link->pmp == 5)
535 link->flags |= ATA_LFLAG_NO_SRST |
536 ATA_LFLAG_ASSUME_ATA;
538 /* Port 6 is for SEMB device which doesn't
542 link->flags |= ATA_LFLAG_NO_SRST |
543 ATA_LFLAG_ASSUME_SEMB;
545 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
546 devid == 0x5734 || devid == 0x5744)) {
547 /* sil5723/5744 quirks */
549 /* sil5723/5744 has either two or three downstream
550 * ports depending on operation mode. The last port
551 * is empty if any actual IO device is available or
552 * occupied by a pseudo configuration device
553 * otherwise. Don't try hard to recover it.
555 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
556 } else if (vendor == 0x11ab && devid == 0x4140) {
557 /* Marvell 88SM4140 quirks. Fan-out ports require PHY
558 * reset to work; other than that, it behaves very
561 ata_port_for_each_link(link, ap)
562 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
567 * sata_pmp_attach - attach a SATA PMP device
568 * @dev: SATA PMP device to attach
570 * Configure and attach SATA PMP device @dev. This function is
571 * also responsible for allocating and initializing PMP links.
574 * Kernel thread context (may sleep).
577 * 0 on success, -errno on failure.
579 int sata_pmp_attach(struct ata_device *dev)
581 struct ata_link *link = dev->link;
582 struct ata_port *ap = link->ap;
584 struct ata_link *tlink;
587 /* is it hanging off the right place? */
588 if (!(ap->flags & ATA_FLAG_PMP)) {
589 ata_dev_printk(dev, KERN_ERR,
590 "host does not support Port Multiplier\n");
594 if (!ata_is_host_link(link)) {
595 ata_dev_printk(dev, KERN_ERR,
596 "Port Multipliers cannot be nested\n");
601 ata_dev_printk(dev, KERN_ERR,
602 "Port Multiplier must be the first device\n");
606 WARN_ON(link->pmp != 0);
607 link->pmp = SATA_PMP_CTRL_PORT;
609 /* read GSCR block */
610 rc = sata_pmp_read_gscr(dev, dev->gscr);
615 rc = sata_pmp_configure(dev, 1);
619 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
621 ata_dev_printk(dev, KERN_INFO,
622 "failed to initialize PMP links\n");
627 spin_lock_irqsave(ap->lock, flags);
628 WARN_ON(ap->nr_pmp_links);
629 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
630 spin_unlock_irqrestore(ap->lock, flags);
634 if (ap->ops->pmp_attach)
635 ap->ops->pmp_attach(ap);
637 ata_port_for_each_link(tlink, ap)
638 sata_link_init_spd(tlink);
640 ata_acpi_associate_sata_port(ap);
650 * sata_pmp_detach - detach a SATA PMP device
651 * @dev: SATA PMP device to detach
653 * Detach SATA PMP device @dev. This function is also
654 * responsible for deconfiguring PMP links.
657 * Kernel thread context (may sleep).
659 static void sata_pmp_detach(struct ata_device *dev)
661 struct ata_link *link = dev->link;
662 struct ata_port *ap = link->ap;
663 struct ata_link *tlink;
666 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
668 WARN_ON(!ata_is_host_link(link) || dev->devno ||
669 link->pmp != SATA_PMP_CTRL_PORT);
671 if (ap->ops->pmp_detach)
672 ap->ops->pmp_detach(ap);
674 ata_port_for_each_link(tlink, ap)
675 ata_eh_detach_dev(tlink->device);
677 spin_lock_irqsave(ap->lock, flags);
678 ap->nr_pmp_links = 0;
680 spin_unlock_irqrestore(ap->lock, flags);
682 ata_acpi_associate_sata_port(ap);
686 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
687 * @dev: PMP device to compare against
688 * @new_gscr: GSCR block of the new device
690 * Compare @new_gscr against @dev and determine whether @dev is
691 * the PMP described by @new_gscr.
697 * 1 if @dev matches @new_gscr, 0 otherwise.
699 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
701 const u32 *old_gscr = dev->gscr;
702 u16 old_vendor, new_vendor, old_devid, new_devid;
703 int old_nr_ports, new_nr_ports;
705 old_vendor = sata_pmp_gscr_vendor(old_gscr);
706 new_vendor = sata_pmp_gscr_vendor(new_gscr);
707 old_devid = sata_pmp_gscr_devid(old_gscr);
708 new_devid = sata_pmp_gscr_devid(new_gscr);
709 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
710 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
712 if (old_vendor != new_vendor) {
713 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
714 "vendor mismatch '0x%x' != '0x%x'\n",
715 old_vendor, new_vendor);
719 if (old_devid != new_devid) {
720 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
721 "device ID mismatch '0x%x' != '0x%x'\n",
722 old_devid, new_devid);
726 if (old_nr_ports != new_nr_ports) {
727 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
728 "nr_ports mismatch '0x%x' != '0x%x'\n",
729 old_nr_ports, new_nr_ports);
737 * sata_pmp_revalidate - revalidate SATA PMP
738 * @dev: PMP device to revalidate
739 * @new_class: new class code
741 * Re-read GSCR block and make sure @dev is still attached to the
742 * port and properly configured.
745 * Kernel thread context (may sleep).
748 * 0 on success, -errno otherwise.
750 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
752 struct ata_link *link = dev->link;
753 struct ata_port *ap = link->ap;
754 u32 *gscr = (void *)ap->sector_buf;
759 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
761 if (!ata_dev_enabled(dev)) {
767 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
773 rc = sata_pmp_read_gscr(dev, gscr);
777 /* is the pmp still there? */
778 if (!sata_pmp_same_pmp(dev, gscr)) {
783 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
785 rc = sata_pmp_configure(dev, 0);
789 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
791 DPRINTK("EXIT, rc=0\n");
795 ata_dev_printk(dev, KERN_ERR,
796 "PMP revalidation failed (errno=%d)\n", rc);
797 DPRINTK("EXIT, rc=%d\n", rc);
802 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
803 * @dev: PMP device to revalidate
805 * Make sure the attached PMP is accessible.
808 * Kernel thread context (may sleep).
811 * 0 on success, -errno otherwise.
813 static int sata_pmp_revalidate_quick(struct ata_device *dev)
818 rc = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
820 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID\n");
824 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
825 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
826 /* something weird is going on, request full PMP recovery */
834 * sata_pmp_eh_recover_pmp - recover PMP
835 * @ap: ATA port PMP is attached to
836 * @prereset: prereset method (can be NULL)
837 * @softreset: softreset method
838 * @hardreset: hardreset method
839 * @postreset: postreset method (can be NULL)
841 * Recover PMP attached to @ap. Recovery procedure is somewhat
842 * similar to that of ata_eh_recover() except that reset should
843 * always be performed in hard->soft sequence and recovery
844 * failure results in PMP detachment.
847 * Kernel thread context (may sleep).
850 * 0 on success, -errno on failure.
852 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
853 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
854 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
856 struct ata_link *link = &ap->link;
857 struct ata_eh_context *ehc = &link->eh_context;
858 struct ata_device *dev = link->device;
859 int tries = ATA_EH_PMP_TRIES;
860 int detach = 0, rc = 0;
861 int reval_failed = 0;
865 if (dev->flags & ATA_DFLAG_DETACH) {
871 ehc->classes[0] = ATA_DEV_UNKNOWN;
873 if (ehc->i.action & ATA_EH_RESET_MASK) {
874 struct ata_link *tlink;
876 ata_eh_freeze_port(ap);
879 ehc->i.action = ATA_EH_HARDRESET;
880 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
883 ata_link_printk(link, KERN_ERR,
884 "failed to reset PMP, giving up\n");
888 ata_eh_thaw_port(ap);
890 /* PMP is reset, SErrors cannot be trusted, scan all */
891 ata_port_for_each_link(tlink, ap)
892 ata_ehi_schedule_probe(&tlink->eh_context.i);
895 /* If revalidation is requested, revalidate and reconfigure;
896 * otherwise, do quick revalidation.
898 if (ehc->i.action & ATA_EH_REVALIDATE)
899 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
901 rc = sata_pmp_revalidate_quick(dev);
907 ehc->i.probe_mask |= 1;
909 /* give it just two more chances */
910 tries = min(tries, 2);
914 int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
916 /* consecutive revalidation failures? speed down */
918 sata_down_spd_limit(link);
922 ata_dev_printk(dev, KERN_WARNING,
923 "retrying hardreset%s\n",
924 sleep ? " in 5 secs" : "");
927 ehc->i.action |= ATA_EH_HARDRESET;
930 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
931 "after %d tries, giving up\n",
937 /* okay, PMP resurrected */
940 DPRINTK("EXIT, rc=0\n");
944 sata_pmp_detach(dev);
946 ata_eh_detach_dev(dev);
948 ata_dev_disable(dev);
950 DPRINTK("EXIT, rc=%d\n", rc);
954 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
956 struct ata_link *link;
960 spin_lock_irqsave(ap->lock, flags);
962 ata_port_for_each_link(link, ap) {
963 if (!(link->flags & ATA_LFLAG_DISABLED))
966 spin_unlock_irqrestore(ap->lock, flags);
968 /* Some PMPs require hardreset sequence to get
971 if ((link->flags & ATA_LFLAG_HRST_TO_RESUME) &&
972 (link->eh_context.i.flags & ATA_EHI_RESUME_LINK))
973 sata_link_hardreset(link, sata_deb_timing_normal,
974 jiffies + ATA_TMOUT_INTERNAL_QUICK);
976 /* unconditionally clear SError.N */
977 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
979 ata_link_printk(link, KERN_ERR, "failed to clear "
980 "SError.N (errno=%d)\n", rc);
984 spin_lock_irqsave(ap->lock, flags);
987 spin_unlock_irqrestore(ap->lock, flags);
992 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
994 struct ata_port *ap = link->ap;
997 if (link_tries[link->pmp] && --link_tries[link->pmp])
1000 /* disable this link */
1001 if (!(link->flags & ATA_LFLAG_DISABLED)) {
1002 ata_link_printk(link, KERN_WARNING,
1003 "failed to recover link after %d tries, disabling\n",
1004 ATA_EH_PMP_LINK_TRIES);
1006 spin_lock_irqsave(ap->lock, flags);
1007 link->flags |= ATA_LFLAG_DISABLED;
1008 spin_unlock_irqrestore(ap->lock, flags);
1011 ata_dev_disable(link->device);
1012 link->eh_context.i.action = 0;
1018 * sata_pmp_eh_recover - recover PMP-enabled port
1019 * @ap: ATA port to recover
1020 * @prereset: prereset method (can be NULL)
1021 * @softreset: softreset method
1022 * @hardreset: hardreset method
1023 * @postreset: postreset method (can be NULL)
1024 * @pmp_prereset: PMP prereset method (can be NULL)
1025 * @pmp_softreset: PMP softreset method (can be NULL)
1026 * @pmp_hardreset: PMP hardreset method (can be NULL)
1027 * @pmp_postreset: PMP postreset method (can be NULL)
1029 * Drive EH recovery operation for PMP enabled port @ap. This
1030 * function recovers host and PMP ports with proper retrials and
1031 * fallbacks. Actual recovery operations are performed using
1032 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
1035 * Kernel thread context (may sleep).
1038 * 0 on success, -errno on failure.
1040 static int sata_pmp_eh_recover(struct ata_port *ap,
1041 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1042 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1043 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1044 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1046 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
1047 struct ata_link *pmp_link = &ap->link;
1048 struct ata_device *pmp_dev = pmp_link->device;
1049 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
1050 struct ata_link *link;
1051 struct ata_device *dev;
1052 u32 gscr_error, sntf;
1055 pmp_tries = ATA_EH_PMP_TRIES;
1056 ata_port_for_each_link(link, ap)
1057 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1061 if (!ap->nr_pmp_links) {
1062 rc = ata_eh_recover(ap, prereset, softreset, hardreset,
1065 ata_link_for_each_dev(dev, &ap->link)
1066 ata_dev_disable(dev);
1070 if (pmp_dev->class != ATA_DEV_PMP)
1073 /* new PMP online */
1074 ata_port_for_each_link(link, ap)
1075 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1081 rc = sata_pmp_eh_recover_pmp(ap, prereset, softreset, hardreset,
1086 /* handle disabled links */
1087 rc = sata_pmp_eh_handle_disabled_links(ap);
1092 rc = ata_eh_recover(ap, pmp_prereset, pmp_softreset, pmp_hardreset,
1093 pmp_postreset, &link);
1097 /* Connection status might have changed while resetting other
1098 * links, check SATA_PMP_GSCR_ERROR before returning.
1101 /* clear SNotification */
1102 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1104 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1106 /* enable notification */
1107 if (pmp_dev->flags & ATA_DFLAG_AN) {
1108 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
1110 rc = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
1111 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
1113 ata_dev_printk(pmp_dev, KERN_ERR,
1114 "failed to write PMP_FEAT_EN\n");
1119 /* check GSCR_ERROR */
1120 rc = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
1122 ata_dev_printk(pmp_dev, KERN_ERR,
1123 "failed to read PMP_GSCR_ERROR\n");
1128 ata_port_for_each_link(link, ap) {
1129 if (!(gscr_error & (1 << link->pmp)))
1132 if (sata_pmp_handle_link_fail(link, link_tries)) {
1133 ata_ehi_hotplugged(&link->eh_context.i);
1136 ata_link_printk(link, KERN_WARNING,
1137 "PHY status changed but maxed out on retries, "
1139 ata_link_printk(link, KERN_WARNING,
1140 "Manully issue scan to resume this link\n");
1145 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
1146 "ports, repeating recovery\n");
1153 if (sata_pmp_handle_link_fail(link, link_tries)) {
1154 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1160 /* Control always ends up here after detaching PMP. Shut up
1161 * and return if we're unloading.
1163 if (ap->pflags & ATA_PFLAG_UNLOADING)
1166 if (!ap->nr_pmp_links)
1170 ata_port_printk(ap, KERN_WARNING,
1171 "failed to recover PMP, retrying in 5 secs\n");
1172 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1177 ata_port_printk(ap, KERN_ERR,
1178 "failed to recover PMP after %d tries, giving up\n",
1180 sata_pmp_detach(pmp_dev);
1181 ata_dev_disable(pmp_dev);
1187 * sata_pmp_do_eh - do standard error handling for PMP-enabled host
1188 * @ap: host port to handle error for
1189 * @prereset: prereset method (can be NULL)
1190 * @softreset: softreset method
1191 * @hardreset: hardreset method
1192 * @postreset: postreset method (can be NULL)
1193 * @pmp_prereset: PMP prereset method (can be NULL)
1194 * @pmp_softreset: PMP softreset method (can be NULL)
1195 * @pmp_hardreset: PMP hardreset method (can be NULL)
1196 * @pmp_postreset: PMP postreset method (can be NULL)
1198 * Perform standard error handling sequence for PMP-enabled host
1202 * Kernel thread context (may sleep).
1204 void sata_pmp_do_eh(struct ata_port *ap,
1205 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1206 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1207 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1208 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1212 sata_pmp_eh_recover(ap, prereset, softreset, hardreset, postreset,
1213 pmp_prereset, pmp_softreset, pmp_hardreset,