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 | ATA_TFLAG_LBA48;
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 | ATA_TFLAG_LBA48;
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);
197 /* if we're about to do hardreset, nothing more to do */
198 if (ehc->i.action & ATA_EH_HARDRESET)
202 rc = sata_link_resume(link, timing, deadline);
204 /* phy resume failed */
205 ata_link_printk(link, KERN_WARNING, "failed to resume link "
206 "for reset (errno=%d)\n", rc);
210 /* clear SError bits including .X which blocks the port when set */
211 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
213 ata_link_printk(link, KERN_ERR,
214 "failed to clear SError (errno=%d)\n", rc);
222 * sata_pmp_std_hardreset - standard hardreset method for PMP link
223 * @link: link to be reset
224 * @class: resulting class of attached device
225 * @deadline: deadline jiffies for the operation
227 * Hardreset PMP port @link. Note that this function doesn't
228 * wait for BSY clearance. There simply isn't a generic way to
229 * wait the event. Instead, this function return -EAGAIN thus
230 * telling libata-EH to followup with softreset.
233 * Kernel thread context (may sleep)
236 * 0 on success, -errno otherwise.
238 int sata_pmp_std_hardreset(struct ata_link *link, unsigned int *class,
239 unsigned long deadline)
241 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
248 rc = sata_link_hardreset(link, timing, deadline);
250 ata_link_printk(link, KERN_ERR,
251 "COMRESET failed (errno=%d)\n", rc);
255 /* clear SError bits including .X which blocks the port when set */
256 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
258 ata_link_printk(link, KERN_ERR, "failed to clear SError "
259 "during hardreset (errno=%d)\n", rc);
263 /* if device is present, follow up with srst to wait for !BSY */
264 if (ata_link_online(link))
267 /* if SCR isn't accessible, we need to reset the PMP */
268 if (rc && rc != -EAGAIN && sata_scr_read(link, SCR_STATUS, &tmp))
271 DPRINTK("EXIT, rc=%d\n", rc);
276 * ata_std_postreset - standard postreset method for PMP link
277 * @link: the target ata_link
278 * @classes: classes of attached devices
280 * This function is invoked after a successful reset. Note that
281 * the device might have been reset more than once using
282 * different reset methods before postreset is invoked.
285 * Kernel thread context (may sleep)
287 void sata_pmp_std_postreset(struct ata_link *link, unsigned int *class)
294 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
295 sata_scr_write(link, SCR_ERROR, serror);
297 /* print link status */
298 sata_print_link_status(link);
304 * sata_pmp_read_gscr - read GSCR block of SATA PMP
306 * @gscr: buffer to read GSCR block into
308 * Read selected PMP GSCRs from the PMP at @dev. This will serve
309 * as configuration and identification info for the PMP.
312 * Kernel thread context (may sleep).
315 * 0 on success, -errno on failure.
317 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
319 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
322 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
323 int reg = gscr_to_read[i];
324 unsigned int err_mask;
326 err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
328 ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
329 "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
337 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
339 u32 rev = gscr[SATA_PMP_GSCR_REV];
348 static int sata_pmp_configure(struct ata_device *dev, int print_info)
350 struct ata_port *ap = dev->link->ap;
351 u32 *gscr = dev->gscr;
352 unsigned int err_mask = 0;
356 nr_ports = sata_pmp_gscr_ports(gscr);
358 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
360 reason = "invalid nr_ports";
364 if ((ap->flags & ATA_FLAG_AN) &&
365 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
366 dev->flags |= ATA_DFLAG_AN;
368 /* monitor SERR_PHYRDY_CHG on fan-out ports */
369 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
373 reason = "failed to write GSCR_ERROR_EN";
377 /* turn off notification till fan-out ports are reset and configured */
378 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
379 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
381 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
382 gscr[SATA_PMP_GSCR_FEAT_EN]);
385 reason = "failed to write GSCR_FEAT_EN";
391 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
392 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
393 sata_pmp_spec_rev_str(gscr),
394 sata_pmp_gscr_vendor(gscr),
395 sata_pmp_gscr_devid(gscr),
396 sata_pmp_gscr_rev(gscr),
397 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
398 gscr[SATA_PMP_GSCR_FEAT]);
400 if (!(dev->flags & ATA_DFLAG_AN))
401 ata_dev_printk(dev, KERN_INFO,
402 "Asynchronous notification not supported, "
403 "hotplug won't\n work on fan-out "
404 "ports. Use warm-plug instead.\n");
410 ata_dev_printk(dev, KERN_ERR,
411 "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
416 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
418 struct ata_link *pmp_link = ap->pmp_link;
422 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
427 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
428 ata_link_init(ap, &pmp_link[i], i);
430 ap->pmp_link = pmp_link;
433 for (i = 0; i < nr_ports; i++) {
434 struct ata_link *link = &pmp_link[i];
435 struct ata_eh_context *ehc = &link->eh_context;
438 ehc->i.probe_mask |= 1;
439 ehc->i.action |= ATA_EH_RESET;
440 ehc->i.flags |= ATA_EHI_RESUME_LINK;
446 static void sata_pmp_quirks(struct ata_port *ap)
448 u32 *gscr = ap->link.device->gscr;
449 u16 vendor = sata_pmp_gscr_vendor(gscr);
450 u16 devid = sata_pmp_gscr_devid(gscr);
451 struct ata_link *link;
453 if (vendor == 0x1095 && devid == 0x3726) {
455 ata_port_for_each_link(link, ap) {
456 /* SError.N need a kick in the ass to get working */
457 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
459 /* class code report is unreliable */
461 link->flags |= ATA_LFLAG_ASSUME_ATA;
463 /* port 5 is for SEMB device and it doesn't like SRST */
465 link->flags |= ATA_LFLAG_NO_SRST |
466 ATA_LFLAG_ASSUME_SEMB;
468 } else if (vendor == 0x1095 && devid == 0x4723) {
470 ata_port_for_each_link(link, ap) {
471 /* SError.N need a kick in the ass to get working */
472 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
474 /* class code report is unreliable */
476 link->flags |= ATA_LFLAG_ASSUME_ATA;
478 /* the config device at port 2 locks up on SRST */
480 link->flags |= ATA_LFLAG_NO_SRST |
481 ATA_LFLAG_ASSUME_ATA;
483 } else if (vendor == 0x1095 && devid == 0x4726) {
485 ata_port_for_each_link(link, ap) {
486 /* SError.N need a kick in the ass to get working */
487 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
489 /* Class code report is unreliable and SRST
490 * times out under certain configurations.
491 * Config device can be at port 0 or 5 and
495 link->flags |= ATA_LFLAG_NO_SRST |
496 ATA_LFLAG_ASSUME_ATA;
498 /* Port 6 is for SEMB device which doesn't
502 link->flags |= ATA_LFLAG_NO_SRST |
503 ATA_LFLAG_ASSUME_SEMB;
505 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
506 devid == 0x5734 || devid == 0x5744)) {
507 /* sil5723/5744 quirks */
509 /* sil5723/5744 has either two or three downstream
510 * ports depending on operation mode. The last port
511 * is empty if any actual IO device is available or
512 * occupied by a pseudo configuration device
513 * otherwise. Don't try hard to recover it.
515 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
516 } else if (vendor == 0x11ab && devid == 0x4140) {
517 /* Marvell 88SM4140 quirks. Fan-out ports require PHY
518 * reset to work; other than that, it behaves very
521 ata_port_for_each_link(link, ap)
522 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
527 * sata_pmp_attach - attach a SATA PMP device
528 * @dev: SATA PMP device to attach
530 * Configure and attach SATA PMP device @dev. This function is
531 * also responsible for allocating and initializing PMP links.
534 * Kernel thread context (may sleep).
537 * 0 on success, -errno on failure.
539 int sata_pmp_attach(struct ata_device *dev)
541 struct ata_link *link = dev->link;
542 struct ata_port *ap = link->ap;
544 struct ata_link *tlink;
547 /* is it hanging off the right place? */
548 if (!(ap->flags & ATA_FLAG_PMP)) {
549 ata_dev_printk(dev, KERN_ERR,
550 "host does not support Port Multiplier\n");
554 if (!ata_is_host_link(link)) {
555 ata_dev_printk(dev, KERN_ERR,
556 "Port Multipliers cannot be nested\n");
561 ata_dev_printk(dev, KERN_ERR,
562 "Port Multiplier must be the first device\n");
566 WARN_ON(link->pmp != 0);
567 link->pmp = SATA_PMP_CTRL_PORT;
569 /* read GSCR block */
570 rc = sata_pmp_read_gscr(dev, dev->gscr);
575 rc = sata_pmp_configure(dev, 1);
579 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
581 ata_dev_printk(dev, KERN_INFO,
582 "failed to initialize PMP links\n");
587 spin_lock_irqsave(ap->lock, flags);
588 WARN_ON(ap->nr_pmp_links);
589 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
590 spin_unlock_irqrestore(ap->lock, flags);
594 if (ap->ops->pmp_attach)
595 ap->ops->pmp_attach(ap);
597 ata_port_for_each_link(tlink, ap)
598 sata_link_init_spd(tlink);
600 ata_acpi_associate_sata_port(ap);
610 * sata_pmp_detach - detach a SATA PMP device
611 * @dev: SATA PMP device to detach
613 * Detach SATA PMP device @dev. This function is also
614 * responsible for deconfiguring PMP links.
617 * Kernel thread context (may sleep).
619 static void sata_pmp_detach(struct ata_device *dev)
621 struct ata_link *link = dev->link;
622 struct ata_port *ap = link->ap;
623 struct ata_link *tlink;
626 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
628 WARN_ON(!ata_is_host_link(link) || dev->devno ||
629 link->pmp != SATA_PMP_CTRL_PORT);
631 if (ap->ops->pmp_detach)
632 ap->ops->pmp_detach(ap);
634 ata_port_for_each_link(tlink, ap)
635 ata_eh_detach_dev(tlink->device);
637 spin_lock_irqsave(ap->lock, flags);
638 ap->nr_pmp_links = 0;
640 spin_unlock_irqrestore(ap->lock, flags);
642 ata_acpi_associate_sata_port(ap);
646 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
647 * @dev: PMP device to compare against
648 * @new_gscr: GSCR block of the new device
650 * Compare @new_gscr against @dev and determine whether @dev is
651 * the PMP described by @new_gscr.
657 * 1 if @dev matches @new_gscr, 0 otherwise.
659 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
661 const u32 *old_gscr = dev->gscr;
662 u16 old_vendor, new_vendor, old_devid, new_devid;
663 int old_nr_ports, new_nr_ports;
665 old_vendor = sata_pmp_gscr_vendor(old_gscr);
666 new_vendor = sata_pmp_gscr_vendor(new_gscr);
667 old_devid = sata_pmp_gscr_devid(old_gscr);
668 new_devid = sata_pmp_gscr_devid(new_gscr);
669 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
670 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
672 if (old_vendor != new_vendor) {
673 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
674 "vendor mismatch '0x%x' != '0x%x'\n",
675 old_vendor, new_vendor);
679 if (old_devid != new_devid) {
680 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
681 "device ID mismatch '0x%x' != '0x%x'\n",
682 old_devid, new_devid);
686 if (old_nr_ports != new_nr_ports) {
687 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
688 "nr_ports mismatch '0x%x' != '0x%x'\n",
689 old_nr_ports, new_nr_ports);
697 * sata_pmp_revalidate - revalidate SATA PMP
698 * @dev: PMP device to revalidate
699 * @new_class: new class code
701 * Re-read GSCR block and make sure @dev is still attached to the
702 * port and properly configured.
705 * Kernel thread context (may sleep).
708 * 0 on success, -errno otherwise.
710 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
712 struct ata_link *link = dev->link;
713 struct ata_port *ap = link->ap;
714 u32 *gscr = (void *)ap->sector_buf;
719 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
721 if (!ata_dev_enabled(dev)) {
727 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
733 rc = sata_pmp_read_gscr(dev, gscr);
737 /* is the pmp still there? */
738 if (!sata_pmp_same_pmp(dev, gscr)) {
743 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
745 rc = sata_pmp_configure(dev, 0);
749 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
751 DPRINTK("EXIT, rc=0\n");
755 ata_dev_printk(dev, KERN_ERR,
756 "PMP revalidation failed (errno=%d)\n", rc);
757 DPRINTK("EXIT, rc=%d\n", rc);
762 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
763 * @dev: PMP device to revalidate
765 * Make sure the attached PMP is accessible.
768 * Kernel thread context (may sleep).
771 * 0 on success, -errno otherwise.
773 static int sata_pmp_revalidate_quick(struct ata_device *dev)
775 unsigned int err_mask;
778 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
780 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
781 "(Emask=0x%x)\n", err_mask);
785 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
786 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
787 /* something weird is going on, request full PMP recovery */
795 * sata_pmp_eh_recover_pmp - recover PMP
796 * @ap: ATA port PMP is attached to
797 * @prereset: prereset method (can be NULL)
798 * @softreset: softreset method
799 * @hardreset: hardreset method
800 * @postreset: postreset method (can be NULL)
802 * Recover PMP attached to @ap. Recovery procedure is somewhat
803 * similar to that of ata_eh_recover() except that reset should
804 * always be performed in hard->soft sequence and recovery
805 * failure results in PMP detachment.
808 * Kernel thread context (may sleep).
811 * 0 on success, -errno on failure.
813 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
814 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
815 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
817 struct ata_link *link = &ap->link;
818 struct ata_eh_context *ehc = &link->eh_context;
819 struct ata_device *dev = link->device;
820 int tries = ATA_EH_PMP_TRIES;
821 int detach = 0, rc = 0;
822 int reval_failed = 0;
826 if (dev->flags & ATA_DFLAG_DETACH) {
832 ehc->classes[0] = ATA_DEV_UNKNOWN;
834 if (ehc->i.action & ATA_EH_RESET) {
835 struct ata_link *tlink;
837 ata_eh_freeze_port(ap);
840 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
843 ata_link_printk(link, KERN_ERR,
844 "failed to reset PMP, giving up\n");
848 ata_eh_thaw_port(ap);
850 /* PMP is reset, SErrors cannot be trusted, scan all */
851 ata_port_for_each_link(tlink, ap)
852 ata_ehi_schedule_probe(&tlink->eh_context.i);
855 /* If revalidation is requested, revalidate and reconfigure;
856 * otherwise, do quick revalidation.
858 if (ehc->i.action & ATA_EH_REVALIDATE)
859 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
861 rc = sata_pmp_revalidate_quick(dev);
867 ehc->i.probe_mask |= 1;
869 /* give it just two more chances */
870 tries = min(tries, 2);
874 int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
876 /* consecutive revalidation failures? speed down */
878 sata_down_spd_limit(link);
882 ata_dev_printk(dev, KERN_WARNING,
883 "retrying reset%s\n",
884 sleep ? " in 5 secs" : "");
887 ehc->i.action |= ATA_EH_RESET;
890 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
891 "after %d tries, giving up\n",
897 /* okay, PMP resurrected */
900 DPRINTK("EXIT, rc=0\n");
904 sata_pmp_detach(dev);
906 ata_eh_detach_dev(dev);
908 ata_dev_disable(dev);
910 DPRINTK("EXIT, rc=%d\n", rc);
914 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
916 struct ata_link *link;
920 spin_lock_irqsave(ap->lock, flags);
922 ata_port_for_each_link(link, ap) {
923 if (!(link->flags & ATA_LFLAG_DISABLED))
926 spin_unlock_irqrestore(ap->lock, flags);
928 /* Some PMPs require hardreset sequence to get
931 sata_link_hardreset(link, sata_deb_timing_normal,
932 jiffies + ATA_TMOUT_INTERNAL_QUICK);
934 /* unconditionally clear SError.N */
935 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
937 ata_link_printk(link, KERN_ERR, "failed to clear "
938 "SError.N (errno=%d)\n", rc);
942 spin_lock_irqsave(ap->lock, flags);
945 spin_unlock_irqrestore(ap->lock, flags);
950 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
952 struct ata_port *ap = link->ap;
955 if (link_tries[link->pmp] && --link_tries[link->pmp])
958 /* disable this link */
959 if (!(link->flags & ATA_LFLAG_DISABLED)) {
960 ata_link_printk(link, KERN_WARNING,
961 "failed to recover link after %d tries, disabling\n",
962 ATA_EH_PMP_LINK_TRIES);
964 spin_lock_irqsave(ap->lock, flags);
965 link->flags |= ATA_LFLAG_DISABLED;
966 spin_unlock_irqrestore(ap->lock, flags);
969 ata_dev_disable(link->device);
970 link->eh_context.i.action = 0;
976 * sata_pmp_eh_recover - recover PMP-enabled port
977 * @ap: ATA port to recover
978 * @prereset: prereset method (can be NULL)
979 * @softreset: softreset method
980 * @hardreset: hardreset method
981 * @postreset: postreset method (can be NULL)
982 * @pmp_prereset: PMP prereset method (can be NULL)
983 * @pmp_softreset: PMP softreset method (can be NULL)
984 * @pmp_hardreset: PMP hardreset method (can be NULL)
985 * @pmp_postreset: PMP postreset method (can be NULL)
987 * Drive EH recovery operation for PMP enabled port @ap. This
988 * function recovers host and PMP ports with proper retrials and
989 * fallbacks. Actual recovery operations are performed using
990 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
993 * Kernel thread context (may sleep).
996 * 0 on success, -errno on failure.
998 static int sata_pmp_eh_recover(struct ata_port *ap,
999 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1000 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1001 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1002 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1004 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
1005 struct ata_link *pmp_link = &ap->link;
1006 struct ata_device *pmp_dev = pmp_link->device;
1007 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
1008 struct ata_link *link;
1009 struct ata_device *dev;
1010 unsigned int err_mask;
1011 u32 gscr_error, sntf;
1014 pmp_tries = ATA_EH_PMP_TRIES;
1015 ata_port_for_each_link(link, ap)
1016 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1020 if (!ap->nr_pmp_links) {
1021 rc = ata_eh_recover(ap, prereset, softreset, hardreset,
1024 ata_link_for_each_dev(dev, &ap->link)
1025 ata_dev_disable(dev);
1029 if (pmp_dev->class != ATA_DEV_PMP)
1032 /* new PMP online */
1033 ata_port_for_each_link(link, ap)
1034 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1040 rc = sata_pmp_eh_recover_pmp(ap, prereset, softreset, hardreset,
1045 /* handle disabled links */
1046 rc = sata_pmp_eh_handle_disabled_links(ap);
1051 rc = ata_eh_recover(ap, pmp_prereset, pmp_softreset, pmp_hardreset,
1052 pmp_postreset, &link);
1056 /* Connection status might have changed while resetting other
1057 * links, check SATA_PMP_GSCR_ERROR before returning.
1060 /* clear SNotification */
1061 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1063 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1065 /* enable notification */
1066 if (pmp_dev->flags & ATA_DFLAG_AN) {
1067 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
1069 err_mask = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
1070 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
1072 ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
1073 "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
1079 /* check GSCR_ERROR */
1080 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
1082 ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
1083 "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
1089 ata_port_for_each_link(link, ap) {
1090 if (!(gscr_error & (1 << link->pmp)))
1093 if (sata_pmp_handle_link_fail(link, link_tries)) {
1094 ata_ehi_hotplugged(&link->eh_context.i);
1097 ata_link_printk(link, KERN_WARNING,
1098 "PHY status changed but maxed out on retries, "
1100 ata_link_printk(link, KERN_WARNING,
1101 "Manully issue scan to resume this link\n");
1106 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
1107 "ports, repeating recovery\n");
1114 if (sata_pmp_handle_link_fail(link, link_tries)) {
1115 pmp_ehc->i.action |= ATA_EH_RESET;
1121 /* Control always ends up here after detaching PMP. Shut up
1122 * and return if we're unloading.
1124 if (ap->pflags & ATA_PFLAG_UNLOADING)
1127 if (!ap->nr_pmp_links)
1131 ata_port_printk(ap, KERN_WARNING,
1132 "failed to recover PMP, retrying in 5 secs\n");
1133 pmp_ehc->i.action |= ATA_EH_RESET;
1138 ata_port_printk(ap, KERN_ERR,
1139 "failed to recover PMP after %d tries, giving up\n",
1141 sata_pmp_detach(pmp_dev);
1142 ata_dev_disable(pmp_dev);
1148 * sata_pmp_do_eh - do standard error handling for PMP-enabled host
1149 * @ap: host port to handle error for
1150 * @prereset: prereset method (can be NULL)
1151 * @softreset: softreset method
1152 * @hardreset: hardreset method
1153 * @postreset: postreset method (can be NULL)
1154 * @pmp_prereset: PMP prereset method (can be NULL)
1155 * @pmp_softreset: PMP softreset method (can be NULL)
1156 * @pmp_hardreset: PMP hardreset method (can be NULL)
1157 * @pmp_postreset: PMP postreset method (can be NULL)
1159 * Perform standard error handling sequence for PMP-enabled host
1163 * Kernel thread context (may sleep).
1165 void sata_pmp_do_eh(struct ata_port *ap,
1166 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1167 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1168 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1169 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1173 sata_pmp_eh_recover(ap, prereset, softreset, hardreset, postreset,
1174 pmp_prereset, pmp_softreset, pmp_hardreset,