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
23 * Kernel thread context (may sleep).
26 * 0 on success, AC_ERR_* mask on failure.
28 static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
30 struct ata_port *ap = link->ap;
31 struct ata_device *pmp_dev = ap->link.device;
32 struct ata_taskfile tf;
33 unsigned int err_mask;
35 ata_tf_init(pmp_dev, &tf);
36 tf.command = ATA_CMD_PMP_READ;
37 tf.protocol = ATA_PROT_NODATA;
38 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
40 tf.device = link->pmp;
42 err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
43 SATA_PMP_SCR_TIMEOUT);
47 *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
52 * sata_pmp_write - write PMP register
53 * @link: link to write PMP register for
54 * @reg: register to write
55 * @r_val: value to write
60 * Kernel thread context (may sleep).
63 * 0 on success, AC_ERR_* mask on failure.
65 static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
67 struct ata_port *ap = link->ap;
68 struct ata_device *pmp_dev = ap->link.device;
69 struct ata_taskfile tf;
71 ata_tf_init(pmp_dev, &tf);
72 tf.command = ATA_CMD_PMP_WRITE;
73 tf.protocol = ATA_PROT_NODATA;
74 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
76 tf.device = link->pmp;
77 tf.nsect = val & 0xff;
78 tf.lbal = (val >> 8) & 0xff;
79 tf.lbam = (val >> 16) & 0xff;
80 tf.lbah = (val >> 24) & 0xff;
82 return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
83 SATA_PMP_SCR_TIMEOUT);
87 * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
88 * @qc: ATA command in question
90 * A host which has command switching PMP support cannot issue
91 * commands to multiple links simultaneously.
94 * spin_lock_irqsave(host lock)
97 * ATA_DEFER_* if deferring is needed, 0 otherwise.
99 int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
101 struct ata_link *link = qc->dev->link;
102 struct ata_port *ap = link->ap;
104 if (ap->excl_link == NULL || ap->excl_link == link) {
105 if (ap->nr_active_links == 0 || ata_link_active(link)) {
106 qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
107 return ata_std_qc_defer(qc);
110 ap->excl_link = link;
113 return ATA_DEFER_PORT;
117 * sata_pmp_scr_read - read PSCR
118 * @link: ATA link to read PSCR for
120 * @r_val: resulting value
122 * Read PSCR @reg into @r_val for @link, to be called from
126 * Kernel thread context (may sleep).
129 * 0 on success, -errno on failure.
131 int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
133 unsigned int err_mask;
135 if (reg > SATA_PMP_PSCR_CONTROL)
138 err_mask = sata_pmp_read(link, reg, r_val);
140 ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
141 "(Emask=0x%x)\n", reg, err_mask);
148 * sata_pmp_scr_write - write PSCR
149 * @link: ATA link to write PSCR for
150 * @reg: PSCR to write
151 * @val: value to be written
153 * Write @val to PSCR @reg for @link, to be called from
154 * ata_scr_write() and ata_scr_write_flush().
157 * Kernel thread context (may sleep).
160 * 0 on success, -errno on failure.
162 int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
164 unsigned int err_mask;
166 if (reg > SATA_PMP_PSCR_CONTROL)
169 err_mask = sata_pmp_write(link, reg, val);
171 ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
172 "(Emask=0x%x)\n", reg, err_mask);
179 * sata_pmp_std_prereset - prepare PMP link for reset
180 * @link: link to be reset
181 * @deadline: deadline jiffies for the operation
183 * @link is about to be reset. Initialize it.
186 * Kernel thread context (may sleep)
189 * 0 on success, -errno otherwise.
191 int sata_pmp_std_prereset(struct ata_link *link, unsigned long deadline)
193 struct ata_eh_context *ehc = &link->eh_context;
194 const unsigned long *timing = sata_ehc_deb_timing(ehc);
198 if (link->flags & ATA_LFLAG_NO_SRST)
199 ehc->i.action |= ATA_EH_HARDRESET;
201 /* handle link resume */
202 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
203 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
204 ehc->i.action |= ATA_EH_HARDRESET;
206 /* if we're about to do hardreset, nothing more to do */
207 if (ehc->i.action & ATA_EH_HARDRESET)
211 rc = sata_link_resume(link, timing, deadline);
213 /* phy resume failed */
214 ata_link_printk(link, KERN_WARNING, "failed to resume link "
215 "for reset (errno=%d)\n", rc);
219 /* clear SError bits including .X which blocks the port when set */
220 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
222 ata_link_printk(link, KERN_ERR,
223 "failed to clear SError (errno=%d)\n", rc);
231 * sata_pmp_std_hardreset - standard hardreset method for PMP link
232 * @link: link to be reset
233 * @class: resulting class of attached device
234 * @deadline: deadline jiffies for the operation
236 * Hardreset PMP port @link. Note that this function doesn't
237 * wait for BSY clearance. There simply isn't a generic way to
238 * wait the event. Instead, this function return -EAGAIN thus
239 * telling libata-EH to followup with softreset.
242 * Kernel thread context (may sleep)
245 * 0 on success, -errno otherwise.
247 int sata_pmp_std_hardreset(struct ata_link *link, unsigned int *class,
248 unsigned long deadline)
250 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
257 rc = sata_link_hardreset(link, timing, deadline);
259 ata_link_printk(link, KERN_ERR,
260 "COMRESET failed (errno=%d)\n", rc);
264 /* clear SError bits including .X which blocks the port when set */
265 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
267 ata_link_printk(link, KERN_ERR, "failed to clear SError "
268 "during hardreset (errno=%d)\n", rc);
272 /* if device is present, follow up with srst to wait for !BSY */
273 if (ata_link_online(link))
276 /* if SCR isn't accessible, we need to reset the PMP */
277 if (rc && rc != -EAGAIN && sata_scr_read(link, SCR_STATUS, &tmp))
280 DPRINTK("EXIT, rc=%d\n", rc);
285 * ata_std_postreset - standard postreset method for PMP link
286 * @link: the target ata_link
287 * @classes: classes of attached devices
289 * This function is invoked after a successful reset. Note that
290 * the device might have been reset more than once using
291 * different reset methods before postreset is invoked.
294 * Kernel thread context (may sleep)
296 void sata_pmp_std_postreset(struct ata_link *link, unsigned int *class)
303 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
304 sata_scr_write(link, SCR_ERROR, serror);
306 /* print link status */
307 sata_print_link_status(link);
313 * sata_pmp_read_gscr - read GSCR block of SATA PMP
315 * @gscr: buffer to read GSCR block into
317 * Read selected PMP GSCRs from the PMP at @dev. This will serve
318 * as configuration and identification info for the PMP.
321 * Kernel thread context (may sleep).
324 * 0 on success, -errno on failure.
326 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
328 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
331 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
332 int reg = gscr_to_read[i];
333 unsigned int err_mask;
335 err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
337 ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
338 "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
346 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
348 u32 rev = gscr[SATA_PMP_GSCR_REV];
357 static int sata_pmp_configure(struct ata_device *dev, int print_info)
359 struct ata_port *ap = dev->link->ap;
360 u32 *gscr = dev->gscr;
361 unsigned int err_mask = 0;
365 nr_ports = sata_pmp_gscr_ports(gscr);
367 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
369 reason = "invalid nr_ports";
373 if ((ap->flags & ATA_FLAG_AN) &&
374 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
375 dev->flags |= ATA_DFLAG_AN;
377 /* monitor SERR_PHYRDY_CHG on fan-out ports */
378 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
382 reason = "failed to write GSCR_ERROR_EN";
386 /* turn off notification till fan-out ports are reset and configured */
387 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
388 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
390 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
391 gscr[SATA_PMP_GSCR_FEAT_EN]);
394 reason = "failed to write GSCR_FEAT_EN";
400 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
401 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
402 sata_pmp_spec_rev_str(gscr),
403 sata_pmp_gscr_vendor(gscr),
404 sata_pmp_gscr_devid(gscr),
405 sata_pmp_gscr_rev(gscr),
406 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
407 gscr[SATA_PMP_GSCR_FEAT]);
409 if (!(dev->flags & ATA_DFLAG_AN))
410 ata_dev_printk(dev, KERN_INFO,
411 "Asynchronous notification not supported, "
412 "hotplug won't\n work on fan-out "
413 "ports. Use warm-plug instead.\n");
419 ata_dev_printk(dev, KERN_ERR,
420 "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
425 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
427 struct ata_link *pmp_link = ap->pmp_link;
431 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
436 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
437 ata_link_init(ap, &pmp_link[i], i);
439 ap->pmp_link = pmp_link;
442 for (i = 0; i < nr_ports; i++) {
443 struct ata_link *link = &pmp_link[i];
444 struct ata_eh_context *ehc = &link->eh_context;
447 ehc->i.probe_mask |= 1;
448 ehc->i.action |= ATA_EH_SOFTRESET;
449 ehc->i.flags |= ATA_EHI_RESUME_LINK;
455 static void sata_pmp_quirks(struct ata_port *ap)
457 u32 *gscr = ap->link.device->gscr;
458 u16 vendor = sata_pmp_gscr_vendor(gscr);
459 u16 devid = sata_pmp_gscr_devid(gscr);
460 struct ata_link *link;
462 if (vendor == 0x1095 && devid == 0x3726) {
464 ata_port_for_each_link(link, ap) {
465 /* SError.N need a kick in the ass to get working */
466 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
468 /* class code report is unreliable */
470 link->flags |= ATA_LFLAG_ASSUME_ATA;
472 /* port 5 is for SEMB device and it doesn't like SRST */
474 link->flags |= ATA_LFLAG_NO_SRST |
475 ATA_LFLAG_ASSUME_SEMB;
477 } else if (vendor == 0x1095 && devid == 0x4723) {
479 ata_port_for_each_link(link, ap) {
480 /* SError.N need a kick in the ass to get working */
481 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
483 /* class code report is unreliable */
485 link->flags |= ATA_LFLAG_ASSUME_ATA;
487 /* the config device at port 2 locks up on SRST */
489 link->flags |= ATA_LFLAG_NO_SRST |
490 ATA_LFLAG_ASSUME_ATA;
492 } else if (vendor == 0x1095 && devid == 0x4726) {
494 ata_port_for_each_link(link, ap) {
495 /* SError.N need a kick in the ass to get working */
496 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
498 /* Class code report is unreliable and SRST
499 * times out under certain configurations.
500 * Config device can be at port 0 or 5 and
504 link->flags |= ATA_LFLAG_NO_SRST |
505 ATA_LFLAG_ASSUME_ATA;
507 /* Port 6 is for SEMB device which doesn't
511 link->flags |= ATA_LFLAG_NO_SRST |
512 ATA_LFLAG_ASSUME_SEMB;
514 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
515 devid == 0x5734 || devid == 0x5744)) {
516 /* sil5723/5744 quirks */
518 /* sil5723/5744 has either two or three downstream
519 * ports depending on operation mode. The last port
520 * is empty if any actual IO device is available or
521 * occupied by a pseudo configuration device
522 * otherwise. Don't try hard to recover it.
524 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
525 } else if (vendor == 0x11ab && devid == 0x4140) {
526 /* Marvell 88SM4140 quirks. Fan-out ports require PHY
527 * reset to work; other than that, it behaves very
530 ata_port_for_each_link(link, ap)
531 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
536 * sata_pmp_attach - attach a SATA PMP device
537 * @dev: SATA PMP device to attach
539 * Configure and attach SATA PMP device @dev. This function is
540 * also responsible for allocating and initializing PMP links.
543 * Kernel thread context (may sleep).
546 * 0 on success, -errno on failure.
548 int sata_pmp_attach(struct ata_device *dev)
550 struct ata_link *link = dev->link;
551 struct ata_port *ap = link->ap;
553 struct ata_link *tlink;
556 /* is it hanging off the right place? */
557 if (!(ap->flags & ATA_FLAG_PMP)) {
558 ata_dev_printk(dev, KERN_ERR,
559 "host does not support Port Multiplier\n");
563 if (!ata_is_host_link(link)) {
564 ata_dev_printk(dev, KERN_ERR,
565 "Port Multipliers cannot be nested\n");
570 ata_dev_printk(dev, KERN_ERR,
571 "Port Multiplier must be the first device\n");
575 WARN_ON(link->pmp != 0);
576 link->pmp = SATA_PMP_CTRL_PORT;
578 /* read GSCR block */
579 rc = sata_pmp_read_gscr(dev, dev->gscr);
584 rc = sata_pmp_configure(dev, 1);
588 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
590 ata_dev_printk(dev, KERN_INFO,
591 "failed to initialize PMP links\n");
596 spin_lock_irqsave(ap->lock, flags);
597 WARN_ON(ap->nr_pmp_links);
598 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
599 spin_unlock_irqrestore(ap->lock, flags);
603 if (ap->ops->pmp_attach)
604 ap->ops->pmp_attach(ap);
606 ata_port_for_each_link(tlink, ap)
607 sata_link_init_spd(tlink);
609 ata_acpi_associate_sata_port(ap);
619 * sata_pmp_detach - detach a SATA PMP device
620 * @dev: SATA PMP device to detach
622 * Detach SATA PMP device @dev. This function is also
623 * responsible for deconfiguring PMP links.
626 * Kernel thread context (may sleep).
628 static void sata_pmp_detach(struct ata_device *dev)
630 struct ata_link *link = dev->link;
631 struct ata_port *ap = link->ap;
632 struct ata_link *tlink;
635 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
637 WARN_ON(!ata_is_host_link(link) || dev->devno ||
638 link->pmp != SATA_PMP_CTRL_PORT);
640 if (ap->ops->pmp_detach)
641 ap->ops->pmp_detach(ap);
643 ata_port_for_each_link(tlink, ap)
644 ata_eh_detach_dev(tlink->device);
646 spin_lock_irqsave(ap->lock, flags);
647 ap->nr_pmp_links = 0;
649 spin_unlock_irqrestore(ap->lock, flags);
651 ata_acpi_associate_sata_port(ap);
655 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
656 * @dev: PMP device to compare against
657 * @new_gscr: GSCR block of the new device
659 * Compare @new_gscr against @dev and determine whether @dev is
660 * the PMP described by @new_gscr.
666 * 1 if @dev matches @new_gscr, 0 otherwise.
668 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
670 const u32 *old_gscr = dev->gscr;
671 u16 old_vendor, new_vendor, old_devid, new_devid;
672 int old_nr_ports, new_nr_ports;
674 old_vendor = sata_pmp_gscr_vendor(old_gscr);
675 new_vendor = sata_pmp_gscr_vendor(new_gscr);
676 old_devid = sata_pmp_gscr_devid(old_gscr);
677 new_devid = sata_pmp_gscr_devid(new_gscr);
678 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
679 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
681 if (old_vendor != new_vendor) {
682 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
683 "vendor mismatch '0x%x' != '0x%x'\n",
684 old_vendor, new_vendor);
688 if (old_devid != new_devid) {
689 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
690 "device ID mismatch '0x%x' != '0x%x'\n",
691 old_devid, new_devid);
695 if (old_nr_ports != new_nr_ports) {
696 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
697 "nr_ports mismatch '0x%x' != '0x%x'\n",
698 old_nr_ports, new_nr_ports);
706 * sata_pmp_revalidate - revalidate SATA PMP
707 * @dev: PMP device to revalidate
708 * @new_class: new class code
710 * Re-read GSCR block and make sure @dev is still attached to the
711 * port and properly configured.
714 * Kernel thread context (may sleep).
717 * 0 on success, -errno otherwise.
719 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
721 struct ata_link *link = dev->link;
722 struct ata_port *ap = link->ap;
723 u32 *gscr = (void *)ap->sector_buf;
728 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
730 if (!ata_dev_enabled(dev)) {
736 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
742 rc = sata_pmp_read_gscr(dev, gscr);
746 /* is the pmp still there? */
747 if (!sata_pmp_same_pmp(dev, gscr)) {
752 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
754 rc = sata_pmp_configure(dev, 0);
758 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
760 DPRINTK("EXIT, rc=0\n");
764 ata_dev_printk(dev, KERN_ERR,
765 "PMP revalidation failed (errno=%d)\n", rc);
766 DPRINTK("EXIT, rc=%d\n", rc);
771 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
772 * @dev: PMP device to revalidate
774 * Make sure the attached PMP is accessible.
777 * Kernel thread context (may sleep).
780 * 0 on success, -errno otherwise.
782 static int sata_pmp_revalidate_quick(struct ata_device *dev)
784 unsigned int err_mask;
787 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
789 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
790 "(Emask=0x%x)\n", err_mask);
794 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
795 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
796 /* something weird is going on, request full PMP recovery */
804 * sata_pmp_eh_recover_pmp - recover PMP
805 * @ap: ATA port PMP is attached to
806 * @prereset: prereset method (can be NULL)
807 * @softreset: softreset method
808 * @hardreset: hardreset method
809 * @postreset: postreset method (can be NULL)
811 * Recover PMP attached to @ap. Recovery procedure is somewhat
812 * similar to that of ata_eh_recover() except that reset should
813 * always be performed in hard->soft sequence and recovery
814 * failure results in PMP detachment.
817 * Kernel thread context (may sleep).
820 * 0 on success, -errno on failure.
822 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
823 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
824 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
826 struct ata_link *link = &ap->link;
827 struct ata_eh_context *ehc = &link->eh_context;
828 struct ata_device *dev = link->device;
829 int tries = ATA_EH_PMP_TRIES;
830 int detach = 0, rc = 0;
831 int reval_failed = 0;
835 if (dev->flags & ATA_DFLAG_DETACH) {
841 ehc->classes[0] = ATA_DEV_UNKNOWN;
843 if (ehc->i.action & ATA_EH_RESET_MASK) {
844 struct ata_link *tlink;
846 ata_eh_freeze_port(ap);
849 ehc->i.action = ATA_EH_HARDRESET;
850 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
853 ata_link_printk(link, KERN_ERR,
854 "failed to reset PMP, giving up\n");
858 ata_eh_thaw_port(ap);
860 /* PMP is reset, SErrors cannot be trusted, scan all */
861 ata_port_for_each_link(tlink, ap)
862 ata_ehi_schedule_probe(&tlink->eh_context.i);
865 /* If revalidation is requested, revalidate and reconfigure;
866 * otherwise, do quick revalidation.
868 if (ehc->i.action & ATA_EH_REVALIDATE)
869 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
871 rc = sata_pmp_revalidate_quick(dev);
877 ehc->i.probe_mask |= 1;
879 /* give it just two more chances */
880 tries = min(tries, 2);
884 int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
886 /* consecutive revalidation failures? speed down */
888 sata_down_spd_limit(link);
892 ata_dev_printk(dev, KERN_WARNING,
893 "retrying hardreset%s\n",
894 sleep ? " in 5 secs" : "");
897 ehc->i.action |= ATA_EH_HARDRESET;
900 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
901 "after %d tries, giving up\n",
907 /* okay, PMP resurrected */
910 DPRINTK("EXIT, rc=0\n");
914 sata_pmp_detach(dev);
916 ata_eh_detach_dev(dev);
918 ata_dev_disable(dev);
920 DPRINTK("EXIT, rc=%d\n", rc);
924 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
926 struct ata_link *link;
930 spin_lock_irqsave(ap->lock, flags);
932 ata_port_for_each_link(link, ap) {
933 if (!(link->flags & ATA_LFLAG_DISABLED))
936 spin_unlock_irqrestore(ap->lock, flags);
938 /* Some PMPs require hardreset sequence to get
941 if ((link->flags & ATA_LFLAG_HRST_TO_RESUME) &&
942 (link->eh_context.i.flags & ATA_EHI_RESUME_LINK))
943 sata_link_hardreset(link, sata_deb_timing_normal,
944 jiffies + ATA_TMOUT_INTERNAL_QUICK);
946 /* unconditionally clear SError.N */
947 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
949 ata_link_printk(link, KERN_ERR, "failed to clear "
950 "SError.N (errno=%d)\n", rc);
954 spin_lock_irqsave(ap->lock, flags);
957 spin_unlock_irqrestore(ap->lock, flags);
962 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
964 struct ata_port *ap = link->ap;
967 if (link_tries[link->pmp] && --link_tries[link->pmp])
970 /* disable this link */
971 if (!(link->flags & ATA_LFLAG_DISABLED)) {
972 ata_link_printk(link, KERN_WARNING,
973 "failed to recover link after %d tries, disabling\n",
974 ATA_EH_PMP_LINK_TRIES);
976 spin_lock_irqsave(ap->lock, flags);
977 link->flags |= ATA_LFLAG_DISABLED;
978 spin_unlock_irqrestore(ap->lock, flags);
981 ata_dev_disable(link->device);
982 link->eh_context.i.action = 0;
988 * sata_pmp_eh_recover - recover PMP-enabled port
989 * @ap: ATA port to recover
990 * @prereset: prereset method (can be NULL)
991 * @softreset: softreset method
992 * @hardreset: hardreset method
993 * @postreset: postreset method (can be NULL)
994 * @pmp_prereset: PMP prereset method (can be NULL)
995 * @pmp_softreset: PMP softreset method (can be NULL)
996 * @pmp_hardreset: PMP hardreset method (can be NULL)
997 * @pmp_postreset: PMP postreset method (can be NULL)
999 * Drive EH recovery operation for PMP enabled port @ap. This
1000 * function recovers host and PMP ports with proper retrials and
1001 * fallbacks. Actual recovery operations are performed using
1002 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
1005 * Kernel thread context (may sleep).
1008 * 0 on success, -errno on failure.
1010 static int sata_pmp_eh_recover(struct ata_port *ap,
1011 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1012 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1013 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1014 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1016 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
1017 struct ata_link *pmp_link = &ap->link;
1018 struct ata_device *pmp_dev = pmp_link->device;
1019 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
1020 struct ata_link *link;
1021 struct ata_device *dev;
1022 unsigned int err_mask;
1023 u32 gscr_error, sntf;
1026 pmp_tries = ATA_EH_PMP_TRIES;
1027 ata_port_for_each_link(link, ap)
1028 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1032 if (!ap->nr_pmp_links) {
1033 rc = ata_eh_recover(ap, prereset, softreset, hardreset,
1036 ata_link_for_each_dev(dev, &ap->link)
1037 ata_dev_disable(dev);
1041 if (pmp_dev->class != ATA_DEV_PMP)
1044 /* new PMP online */
1045 ata_port_for_each_link(link, ap)
1046 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1052 rc = sata_pmp_eh_recover_pmp(ap, prereset, softreset, hardreset,
1057 /* handle disabled links */
1058 rc = sata_pmp_eh_handle_disabled_links(ap);
1063 rc = ata_eh_recover(ap, pmp_prereset, pmp_softreset, pmp_hardreset,
1064 pmp_postreset, &link);
1068 /* Connection status might have changed while resetting other
1069 * links, check SATA_PMP_GSCR_ERROR before returning.
1072 /* clear SNotification */
1073 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1075 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1077 /* enable notification */
1078 if (pmp_dev->flags & ATA_DFLAG_AN) {
1079 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
1081 err_mask = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
1082 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
1084 ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
1085 "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
1091 /* check GSCR_ERROR */
1092 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
1094 ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
1095 "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
1101 ata_port_for_each_link(link, ap) {
1102 if (!(gscr_error & (1 << link->pmp)))
1105 if (sata_pmp_handle_link_fail(link, link_tries)) {
1106 ata_ehi_hotplugged(&link->eh_context.i);
1109 ata_link_printk(link, KERN_WARNING,
1110 "PHY status changed but maxed out on retries, "
1112 ata_link_printk(link, KERN_WARNING,
1113 "Manully issue scan to resume this link\n");
1118 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
1119 "ports, repeating recovery\n");
1126 if (sata_pmp_handle_link_fail(link, link_tries)) {
1127 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1133 /* Control always ends up here after detaching PMP. Shut up
1134 * and return if we're unloading.
1136 if (ap->pflags & ATA_PFLAG_UNLOADING)
1139 if (!ap->nr_pmp_links)
1143 ata_port_printk(ap, KERN_WARNING,
1144 "failed to recover PMP, retrying in 5 secs\n");
1145 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1150 ata_port_printk(ap, KERN_ERR,
1151 "failed to recover PMP after %d tries, giving up\n",
1153 sata_pmp_detach(pmp_dev);
1154 ata_dev_disable(pmp_dev);
1160 * sata_pmp_do_eh - do standard error handling for PMP-enabled host
1161 * @ap: host port to handle error for
1162 * @prereset: prereset method (can be NULL)
1163 * @softreset: softreset method
1164 * @hardreset: hardreset method
1165 * @postreset: postreset method (can be NULL)
1166 * @pmp_prereset: PMP prereset method (can be NULL)
1167 * @pmp_softreset: PMP softreset method (can be NULL)
1168 * @pmp_hardreset: PMP hardreset method (can be NULL)
1169 * @pmp_postreset: PMP postreset method (can be NULL)
1171 * Perform standard error handling sequence for PMP-enabled host
1175 * Kernel thread context (may sleep).
1177 void sata_pmp_do_eh(struct ata_port *ap,
1178 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1179 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1180 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1181 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1185 sata_pmp_eh_recover(ap, prereset, softreset, hardreset, postreset,
1186 pmp_prereset, pmp_softreset, pmp_hardreset,