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_read_init_tf - initialize TF for PMP read
74 * @tf: taskfile to initialize
76 * @pmp: port multiplier port number
77 * @reg: register to read
79 * Initialize @tf for PMP read command.
84 void sata_pmp_read_init_tf(struct ata_taskfile *tf,
85 struct ata_device *dev, int pmp, int reg)
88 tf->command = ATA_CMD_PMP_READ;
89 tf->protocol = ATA_PROT_NODATA;
90 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
96 * sata_pmp_read_val - extract PMP read result from TF
99 * Determine PMP read result from @tf.
104 u32 sata_pmp_read_val(const struct ata_taskfile *tf)
106 return tf->nsect | tf->lbal << 8 | tf->lbam << 16 | tf->lbah << 24;
110 * sata_pmp_read_init_tf - initialize TF for PMP write
111 * @tf: taskfile to initialize
113 * @pmp: port multiplier port number
114 * @reg: register to read
115 * @val: value to write
117 * Initialize @tf for PMP write command.
122 void sata_pmp_write_init_tf(struct ata_taskfile *tf,
123 struct ata_device *dev, int pmp, int reg, u32 val)
125 ata_tf_init(dev, tf);
126 tf->command = ATA_CMD_PMP_WRITE;
127 tf->protocol = ATA_PROT_NODATA;
128 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
131 tf->nsect = val & 0xff;
132 tf->lbal = (val >> 8) & 0xff;
133 tf->lbam = (val >> 16) & 0xff;
134 tf->lbah = (val >> 24) & 0xff;
138 * sata_pmp_scr_read - read PSCR
139 * @link: ATA link to read PSCR for
141 * @r_val: resulting value
143 * Read PSCR @reg into @r_val for @link, to be called from
147 * Kernel thread context (may sleep).
150 * 0 on success, -errno on failure.
152 int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
154 if (reg > SATA_PMP_PSCR_CONTROL)
157 return sata_pmp_read(link, reg, r_val);
161 * sata_pmp_scr_write - write PSCR
162 * @link: ATA link to write PSCR for
163 * @reg: PSCR to write
164 * @val: value to be written
166 * Write @val to PSCR @reg for @link, to be called from
167 * ata_scr_write() and ata_scr_write_flush().
170 * Kernel thread context (may sleep).
173 * 0 on success, -errno on failure.
175 int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
177 if (reg > SATA_PMP_PSCR_CONTROL)
180 return sata_pmp_write(link, reg, val);
184 * sata_pmp_std_prereset - prepare PMP link for reset
185 * @link: link to be reset
186 * @deadline: deadline jiffies for the operation
188 * @link is about to be reset. Initialize it.
191 * Kernel thread context (may sleep)
194 * 0 on success, -errno otherwise.
196 int sata_pmp_std_prereset(struct ata_link *link, unsigned long deadline)
198 struct ata_eh_context *ehc = &link->eh_context;
199 const unsigned long *timing = sata_ehc_deb_timing(ehc);
203 if (link->flags & ATA_LFLAG_NO_SRST)
204 ehc->i.action |= ATA_EH_HARDRESET;
206 /* handle link resume */
207 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
208 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
209 ehc->i.action |= ATA_EH_HARDRESET;
211 /* if we're about to do hardreset, nothing more to do */
212 if (ehc->i.action & ATA_EH_HARDRESET)
216 rc = sata_link_resume(link, timing, deadline);
218 /* phy resume failed */
219 ata_link_printk(link, KERN_WARNING, "failed to resume link "
220 "for reset (errno=%d)\n", rc);
224 /* clear SError bits including .X which blocks the port when set */
225 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
227 ata_link_printk(link, KERN_ERR,
228 "failed to clear SError (errno=%d)\n", rc);
236 * sata_pmp_std_hardreset - standard hardreset method for PMP link
237 * @link: link to be reset
238 * @class: resulting class of attached device
239 * @deadline: deadline jiffies for the operation
241 * Hardreset PMP port @link. Note that this function doesn't
242 * wait for BSY clearance. There simply isn't a generic way to
243 * wait the event. Instead, this function return -EAGAIN thus
244 * telling libata-EH to followup with softreset.
247 * Kernel thread context (may sleep)
250 * 0 on success, -errno otherwise.
252 int sata_pmp_std_hardreset(struct ata_link *link, unsigned int *class,
253 unsigned long deadline)
255 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
262 rc = sata_link_hardreset(link, timing, deadline);
264 ata_link_printk(link, KERN_ERR,
265 "COMRESET failed (errno=%d)\n", rc);
269 /* clear SError bits including .X which blocks the port when set */
270 rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
272 ata_link_printk(link, KERN_ERR, "failed to clear SError "
273 "during hardreset (errno=%d)\n", rc);
277 /* if device is present, follow up with srst to wait for !BSY */
278 if (ata_link_online(link))
281 /* if SCR isn't accessible, we need to reset the PMP */
282 if (rc && rc != -EAGAIN && sata_scr_read(link, SCR_STATUS, &tmp))
285 DPRINTK("EXIT, rc=%d\n", rc);
290 * ata_std_postreset - standard postreset method for PMP link
291 * @link: the target ata_link
292 * @classes: classes of attached devices
294 * This function is invoked after a successful reset. Note that
295 * the device might have been reset more than once using
296 * different reset methods before postreset is invoked.
299 * Kernel thread context (may sleep)
301 void sata_pmp_std_postreset(struct ata_link *link, unsigned int *class)
308 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
309 sata_scr_write(link, SCR_ERROR, serror);
311 /* print link status */
312 sata_print_link_status(link);
318 * sata_pmp_read_gscr - read GSCR block of SATA PMP
320 * @gscr: buffer to read GSCR block into
322 * Read selected PMP GSCRs from the PMP at @dev. This will serve
323 * as configuration and identification info for the PMP.
326 * Kernel thread context (may sleep).
329 * 0 on success, -errno on failure.
331 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
333 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
336 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
337 int reg = gscr_to_read[i];
339 rc = sata_pmp_read(dev->link, reg, &gscr[reg]);
341 ata_dev_printk(dev, KERN_ERR, "failed to read "
342 "PMP GSCR[%d] (errno=%d)\n", reg, rc);
350 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
352 u32 rev = gscr[SATA_PMP_GSCR_REV];
361 static int sata_pmp_configure(struct ata_device *dev, int print_info)
363 struct ata_port *ap = dev->link->ap;
364 u32 *gscr = dev->gscr;
368 nr_ports = sata_pmp_gscr_ports(gscr);
370 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
372 reason = "invalid nr_ports";
376 if ((ap->flags & ATA_FLAG_AN) &&
377 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
378 dev->flags |= ATA_DFLAG_AN;
380 /* monitor SERR_PHYRDY_CHG on fan-out ports */
381 rc = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN, SERR_PHYRDY_CHG);
383 reason = "failed to write GSCR_ERROR_EN";
387 /* turn off notification till fan-out ports are reset and configured */
388 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
389 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
391 rc = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
392 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)\n", reason);
424 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
426 struct ata_link *pmp_link = ap->pmp_link;
430 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
435 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
436 ata_link_init(ap, &pmp_link[i], i);
438 ap->pmp_link = pmp_link;
441 for (i = 0; i < nr_ports; i++) {
442 struct ata_link *link = &pmp_link[i];
443 struct ata_eh_context *ehc = &link->eh_context;
446 ehc->i.probe_mask |= 1;
447 ehc->i.action |= ATA_EH_SOFTRESET;
448 ehc->i.flags |= ATA_EHI_RESUME_LINK;
454 static void sata_pmp_quirks(struct ata_port *ap)
456 u32 *gscr = ap->link.device->gscr;
457 u16 vendor = sata_pmp_gscr_vendor(gscr);
458 u16 devid = sata_pmp_gscr_devid(gscr);
459 struct ata_link *link;
461 if (vendor == 0x1095 && devid == 0x3726) {
463 ata_port_for_each_link(link, ap) {
464 /* SError.N need a kick in the ass to get working */
465 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
467 /* class code report is unreliable */
469 link->flags |= ATA_LFLAG_ASSUME_ATA;
471 /* port 5 is for SEMB device and it doesn't like SRST */
473 link->flags |= ATA_LFLAG_NO_SRST |
474 ATA_LFLAG_ASSUME_SEMB;
476 } else if (vendor == 0x1095 && devid == 0x4723) {
478 ata_port_for_each_link(link, ap) {
479 /* SError.N need a kick in the ass to get working */
480 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
482 /* class code report is unreliable */
484 link->flags |= ATA_LFLAG_ASSUME_ATA;
486 /* the config device at port 2 locks up on SRST */
488 link->flags |= ATA_LFLAG_NO_SRST |
489 ATA_LFLAG_ASSUME_ATA;
491 } else if (vendor == 0x1095 && devid == 0x4726) {
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 /* The config device, which can be either at
502 * port 0 or 5, locks up on SRST.
504 if (link->pmp == 0 || link->pmp == 5)
505 link->flags |= ATA_LFLAG_NO_SRST |
506 ATA_LFLAG_ASSUME_ATA;
508 /* Port 6 is for SEMB device which doesn't
512 link->flags |= ATA_LFLAG_NO_SRST |
513 ATA_LFLAG_ASSUME_SEMB;
515 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
516 devid == 0x5734 || devid == 0x5744)) {
517 /* sil5723/5744 quirks */
519 /* sil5723/5744 has either two or three downstream
520 * ports depending on operation mode. The last port
521 * is empty if any actual IO device is available or
522 * occupied by a pseudo configuration device
523 * otherwise. Don't try hard to recover it.
525 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
526 } else if (vendor == 0x11ab && devid == 0x4140) {
527 /* Marvell 88SM4140 quirks. Fan-out ports require PHY
528 * reset to work; other than that, it behaves very
531 ata_port_for_each_link(link, ap)
532 link->flags |= ATA_LFLAG_HRST_TO_RESUME;
537 * sata_pmp_attach - attach a SATA PMP device
538 * @dev: SATA PMP device to attach
540 * Configure and attach SATA PMP device @dev. This function is
541 * also responsible for allocating and initializing PMP links.
544 * Kernel thread context (may sleep).
547 * 0 on success, -errno on failure.
549 int sata_pmp_attach(struct ata_device *dev)
551 struct ata_link *link = dev->link;
552 struct ata_port *ap = link->ap;
554 struct ata_link *tlink;
557 /* is it hanging off the right place? */
558 if (!(ap->flags & ATA_FLAG_PMP)) {
559 ata_dev_printk(dev, KERN_ERR,
560 "host does not support Port Multiplier\n");
564 if (!ata_is_host_link(link)) {
565 ata_dev_printk(dev, KERN_ERR,
566 "Port Multipliers cannot be nested\n");
571 ata_dev_printk(dev, KERN_ERR,
572 "Port Multiplier must be the first device\n");
576 WARN_ON(link->pmp != 0);
577 link->pmp = SATA_PMP_CTRL_PORT;
579 /* read GSCR block */
580 rc = sata_pmp_read_gscr(dev, dev->gscr);
585 rc = sata_pmp_configure(dev, 1);
589 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
591 ata_dev_printk(dev, KERN_INFO,
592 "failed to initialize PMP links\n");
597 spin_lock_irqsave(ap->lock, flags);
598 WARN_ON(ap->nr_pmp_links);
599 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
600 spin_unlock_irqrestore(ap->lock, flags);
604 if (ap->ops->pmp_attach)
605 ap->ops->pmp_attach(ap);
607 ata_port_for_each_link(tlink, ap)
608 sata_link_init_spd(tlink);
618 * sata_pmp_detach - detach a SATA PMP device
619 * @dev: SATA PMP device to detach
621 * Detach SATA PMP device @dev. This function is also
622 * responsible for deconfiguring PMP links.
625 * Kernel thread context (may sleep).
627 static void sata_pmp_detach(struct ata_device *dev)
629 struct ata_link *link = dev->link;
630 struct ata_port *ap = link->ap;
631 struct ata_link *tlink;
634 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
636 WARN_ON(!ata_is_host_link(link) || dev->devno ||
637 link->pmp != SATA_PMP_CTRL_PORT);
639 if (ap->ops->pmp_detach)
640 ap->ops->pmp_detach(ap);
642 ata_port_for_each_link(tlink, ap)
643 ata_eh_detach_dev(tlink->device);
645 spin_lock_irqsave(ap->lock, flags);
646 ap->nr_pmp_links = 0;
648 spin_unlock_irqrestore(ap->lock, flags);
652 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
653 * @dev: PMP device to compare against
654 * @new_gscr: GSCR block of the new device
656 * Compare @new_gscr against @dev and determine whether @dev is
657 * the PMP described by @new_gscr.
663 * 1 if @dev matches @new_gscr, 0 otherwise.
665 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
667 const u32 *old_gscr = dev->gscr;
668 u16 old_vendor, new_vendor, old_devid, new_devid;
669 int old_nr_ports, new_nr_ports;
671 old_vendor = sata_pmp_gscr_vendor(old_gscr);
672 new_vendor = sata_pmp_gscr_vendor(new_gscr);
673 old_devid = sata_pmp_gscr_devid(old_gscr);
674 new_devid = sata_pmp_gscr_devid(new_gscr);
675 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
676 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
678 if (old_vendor != new_vendor) {
679 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
680 "vendor mismatch '0x%x' != '0x%x'\n",
681 old_vendor, new_vendor);
685 if (old_devid != new_devid) {
686 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
687 "device ID mismatch '0x%x' != '0x%x'\n",
688 old_devid, new_devid);
692 if (old_nr_ports != new_nr_ports) {
693 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
694 "nr_ports mismatch '0x%x' != '0x%x'\n",
695 old_nr_ports, new_nr_ports);
703 * sata_pmp_revalidate - revalidate SATA PMP
704 * @dev: PMP device to revalidate
705 * @new_class: new class code
707 * Re-read GSCR block and make sure @dev is still attached to the
708 * port and properly configured.
711 * Kernel thread context (may sleep).
714 * 0 on success, -errno otherwise.
716 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
718 struct ata_link *link = dev->link;
719 struct ata_port *ap = link->ap;
720 u32 *gscr = (void *)ap->sector_buf;
725 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
727 if (!ata_dev_enabled(dev)) {
733 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
739 rc = sata_pmp_read_gscr(dev, gscr);
743 /* is the pmp still there? */
744 if (!sata_pmp_same_pmp(dev, gscr)) {
749 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
751 rc = sata_pmp_configure(dev, 0);
755 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
757 DPRINTK("EXIT, rc=0\n");
761 ata_dev_printk(dev, KERN_ERR,
762 "PMP revalidation failed (errno=%d)\n", rc);
763 DPRINTK("EXIT, rc=%d\n", rc);
768 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
769 * @dev: PMP device to revalidate
771 * Make sure the attached PMP is accessible.
774 * Kernel thread context (may sleep).
777 * 0 on success, -errno otherwise.
779 static int sata_pmp_revalidate_quick(struct ata_device *dev)
784 rc = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
786 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID\n");
790 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
791 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
792 /* something weird is going on, request full PMP recovery */
800 * sata_pmp_eh_recover_pmp - recover PMP
801 * @ap: ATA port PMP is attached to
802 * @prereset: prereset method (can be NULL)
803 * @softreset: softreset method
804 * @hardreset: hardreset method
805 * @postreset: postreset method (can be NULL)
807 * Recover PMP attached to @ap. Recovery procedure is somewhat
808 * similar to that of ata_eh_recover() except that reset should
809 * always be performed in hard->soft sequence and recovery
810 * failure results in PMP detachment.
813 * Kernel thread context (may sleep).
816 * 0 on success, -errno on failure.
818 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
819 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
820 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
822 struct ata_link *link = &ap->link;
823 struct ata_eh_context *ehc = &link->eh_context;
824 struct ata_device *dev = link->device;
825 int tries = ATA_EH_PMP_TRIES;
826 int detach = 0, rc = 0;
827 int reval_failed = 0;
831 if (dev->flags & ATA_DFLAG_DETACH) {
837 ehc->classes[0] = ATA_DEV_UNKNOWN;
839 if (ehc->i.action & ATA_EH_RESET_MASK) {
840 struct ata_link *tlink;
842 ata_eh_freeze_port(ap);
845 ehc->i.action = ATA_EH_HARDRESET;
846 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
849 ata_link_printk(link, KERN_ERR,
850 "failed to reset PMP, giving up\n");
854 ata_eh_thaw_port(ap);
856 /* PMP is reset, SErrors cannot be trusted, scan all */
857 ata_port_for_each_link(tlink, ap)
858 ata_ehi_schedule_probe(&tlink->eh_context.i);
861 /* If revalidation is requested, revalidate and reconfigure;
862 * otherwise, do quick revalidation.
864 if (ehc->i.action & ATA_EH_REVALIDATE)
865 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
867 rc = sata_pmp_revalidate_quick(dev);
873 ehc->i.probe_mask |= 1;
875 /* give it just two more chances */
876 tries = min(tries, 2);
880 int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
882 /* consecutive revalidation failures? speed down */
884 sata_down_spd_limit(link);
888 ata_dev_printk(dev, KERN_WARNING,
889 "retrying hardreset%s\n",
890 sleep ? " in 5 secs" : "");
893 ehc->i.action |= ATA_EH_HARDRESET;
896 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
897 "after %d tries, giving up\n",
903 /* okay, PMP resurrected */
906 DPRINTK("EXIT, rc=0\n");
910 sata_pmp_detach(dev);
912 ata_eh_detach_dev(dev);
914 ata_dev_disable(dev);
916 DPRINTK("EXIT, rc=%d\n", rc);
920 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
922 struct ata_link *link;
926 spin_lock_irqsave(ap->lock, flags);
928 ata_port_for_each_link(link, ap) {
929 if (!(link->flags & ATA_LFLAG_DISABLED))
932 spin_unlock_irqrestore(ap->lock, flags);
934 /* Some PMPs require hardreset sequence to get
937 if ((link->flags & ATA_LFLAG_HRST_TO_RESUME) &&
938 (link->eh_context.i.flags & ATA_EHI_RESUME_LINK))
939 sata_link_hardreset(link, sata_deb_timing_normal,
940 jiffies + ATA_TMOUT_INTERNAL_QUICK);
942 /* unconditionally clear SError.N */
943 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
945 ata_link_printk(link, KERN_ERR, "failed to clear "
946 "SError.N (errno=%d)\n", rc);
950 spin_lock_irqsave(ap->lock, flags);
953 spin_unlock_irqrestore(ap->lock, flags);
958 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
960 struct ata_port *ap = link->ap;
963 if (link_tries[link->pmp] && --link_tries[link->pmp])
966 /* disable this link */
967 if (!(link->flags & ATA_LFLAG_DISABLED)) {
968 ata_link_printk(link, KERN_WARNING,
969 "failed to recover link after %d tries, disabling\n",
970 ATA_EH_PMP_LINK_TRIES);
972 spin_lock_irqsave(ap->lock, flags);
973 link->flags |= ATA_LFLAG_DISABLED;
974 spin_unlock_irqrestore(ap->lock, flags);
977 ata_dev_disable(link->device);
978 link->eh_context.i.action = 0;
984 * sata_pmp_eh_recover - recover PMP-enabled port
985 * @ap: ATA port to recover
986 * @prereset: prereset method (can be NULL)
987 * @softreset: softreset method
988 * @hardreset: hardreset method
989 * @postreset: postreset method (can be NULL)
990 * @pmp_prereset: PMP prereset method (can be NULL)
991 * @pmp_softreset: PMP softreset method (can be NULL)
992 * @pmp_hardreset: PMP hardreset method (can be NULL)
993 * @pmp_postreset: PMP postreset method (can be NULL)
995 * Drive EH recovery operation for PMP enabled port @ap. This
996 * function recovers host and PMP ports with proper retrials and
997 * fallbacks. Actual recovery operations are performed using
998 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
1001 * Kernel thread context (may sleep).
1004 * 0 on success, -errno on failure.
1006 static int sata_pmp_eh_recover(struct ata_port *ap,
1007 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1008 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1009 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1010 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1012 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
1013 struct ata_link *pmp_link = &ap->link;
1014 struct ata_device *pmp_dev = pmp_link->device;
1015 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
1016 struct ata_link *link;
1017 struct ata_device *dev;
1018 u32 gscr_error, sntf;
1021 pmp_tries = ATA_EH_PMP_TRIES;
1022 ata_port_for_each_link(link, ap)
1023 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1027 if (!ap->nr_pmp_links) {
1028 rc = ata_eh_recover(ap, prereset, softreset, hardreset,
1031 ata_link_for_each_dev(dev, &ap->link)
1032 ata_dev_disable(dev);
1036 if (pmp_dev->class != ATA_DEV_PMP)
1039 /* new PMP online */
1040 ata_port_for_each_link(link, ap)
1041 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1047 rc = sata_pmp_eh_recover_pmp(ap, prereset, softreset, hardreset,
1052 /* handle disabled links */
1053 rc = sata_pmp_eh_handle_disabled_links(ap);
1058 rc = ata_eh_recover(ap, pmp_prereset, pmp_softreset, pmp_hardreset,
1059 pmp_postreset, &link);
1063 /* Connection status might have changed while resetting other
1064 * links, check SATA_PMP_GSCR_ERROR before returning.
1067 /* clear SNotification */
1068 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1070 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1072 /* enable notification */
1073 if (pmp_dev->flags & ATA_DFLAG_AN) {
1074 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
1076 rc = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
1077 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
1079 ata_dev_printk(pmp_dev, KERN_ERR,
1080 "failed to write PMP_FEAT_EN\n");
1085 /* check GSCR_ERROR */
1086 rc = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
1088 ata_dev_printk(pmp_dev, KERN_ERR,
1089 "failed to read PMP_GSCR_ERROR\n");
1094 ata_port_for_each_link(link, ap) {
1095 if (!(gscr_error & (1 << link->pmp)))
1098 if (sata_pmp_handle_link_fail(link, link_tries)) {
1099 ata_ehi_hotplugged(&link->eh_context.i);
1102 ata_link_printk(link, KERN_WARNING,
1103 "PHY status changed but maxed out on retries, "
1105 ata_link_printk(link, KERN_WARNING,
1106 "Manully issue scan to resume this link\n");
1111 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
1112 "ports, repeating recovery\n");
1119 if (sata_pmp_handle_link_fail(link, link_tries)) {
1120 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1126 /* Control always ends up here after detaching PMP. Shut up
1127 * and return if we're unloading.
1129 if (ap->pflags & ATA_PFLAG_UNLOADING)
1132 if (!ap->nr_pmp_links)
1136 ata_port_printk(ap, KERN_WARNING,
1137 "failed to recover PMP, retrying in 5 secs\n");
1138 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1143 ata_port_printk(ap, KERN_ERR,
1144 "failed to recover PMP after %d tries, giving up\n",
1146 sata_pmp_detach(pmp_dev);
1147 ata_dev_disable(pmp_dev);
1153 * sata_pmp_do_eh - do standard error handling for PMP-enabled host
1154 * @ap: host port to handle error for
1155 * @prereset: prereset method (can be NULL)
1156 * @softreset: softreset method
1157 * @hardreset: hardreset method
1158 * @postreset: postreset method (can be NULL)
1159 * @pmp_prereset: PMP prereset method (can be NULL)
1160 * @pmp_softreset: PMP softreset method (can be NULL)
1161 * @pmp_hardreset: PMP hardreset method (can be NULL)
1162 * @pmp_postreset: PMP postreset method (can be NULL)
1164 * Perform standard error handling sequence for PMP-enabled host
1168 * Kernel thread context (may sleep).
1170 void sata_pmp_do_eh(struct ata_port *ap,
1171 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1172 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1173 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1174 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1178 sata_pmp_eh_recover(ap, prereset, softreset, hardreset, postreset,
1179 pmp_prereset, pmp_softreset, pmp_hardreset,