libata: move PMP SCR access failure during reset to ata_eh_reset()
[linux-2.6/kvm.git] / drivers / ata / libata-pmp.c
blob9c998611b6440665e5c11f8880ece0704a7e012d
1 /*
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.
8 */
10 #include <linux/kernel.h>
11 #include <linux/libata.h>
12 #include "libata.h"
14 /**
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 * Read PMP register.
22 * LOCKING:
23 * Kernel thread context (may sleep).
25 * RETURNS:
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;
39 tf.feature = reg;
40 tf.device = link->pmp;
42 err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
43 SATA_PMP_SCR_TIMEOUT);
44 if (err_mask)
45 return err_mask;
47 *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
48 return 0;
51 /**
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
57 * Write PMP register.
59 * LOCKING:
60 * Kernel thread context (may sleep).
62 * RETURNS:
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;
75 tf.feature = reg;
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);
86 /**
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.
93 * LOCKING:
94 * spin_lock_irqsave(host lock)
96 * RETURNS:
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
119 * @reg: PSCR to read
120 * @r_val: resulting value
122 * Read PSCR @reg into @r_val for @link, to be called from
123 * ata_scr_read().
125 * LOCKING:
126 * Kernel thread context (may sleep).
128 * RETURNS:
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)
136 return -EINVAL;
138 err_mask = sata_pmp_read(link, reg, r_val);
139 if (err_mask) {
140 ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
141 "(Emask=0x%x)\n", reg, err_mask);
142 return -EIO;
144 return 0;
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().
156 * LOCKING:
157 * Kernel thread context (may sleep).
159 * RETURNS:
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)
167 return -EINVAL;
169 err_mask = sata_pmp_write(link, reg, val);
170 if (err_mask) {
171 ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
172 "(Emask=0x%x)\n", reg, err_mask);
173 return -EIO;
175 return 0;
179 * sata_pmp_read_gscr - read GSCR block of SATA PMP
180 * @dev: PMP device
181 * @gscr: buffer to read GSCR block into
183 * Read selected PMP GSCRs from the PMP at @dev. This will serve
184 * as configuration and identification info for the PMP.
186 * LOCKING:
187 * Kernel thread context (may sleep).
189 * RETURNS:
190 * 0 on success, -errno on failure.
192 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
194 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
195 int i;
197 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
198 int reg = gscr_to_read[i];
199 unsigned int err_mask;
201 err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
202 if (err_mask) {
203 ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
204 "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
205 return -EIO;
209 return 0;
212 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
214 u32 rev = gscr[SATA_PMP_GSCR_REV];
216 if (rev & (1 << 2))
217 return "1.1";
218 if (rev & (1 << 1))
219 return "1.0";
220 return "<unknown>";
223 static int sata_pmp_configure(struct ata_device *dev, int print_info)
225 struct ata_port *ap = dev->link->ap;
226 u32 *gscr = dev->gscr;
227 unsigned int err_mask = 0;
228 const char *reason;
229 int nr_ports, rc;
231 nr_ports = sata_pmp_gscr_ports(gscr);
233 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
234 rc = -EINVAL;
235 reason = "invalid nr_ports";
236 goto fail;
239 if ((ap->flags & ATA_FLAG_AN) &&
240 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
241 dev->flags |= ATA_DFLAG_AN;
243 /* monitor SERR_PHYRDY_CHG on fan-out ports */
244 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
245 SERR_PHYRDY_CHG);
246 if (err_mask) {
247 rc = -EIO;
248 reason = "failed to write GSCR_ERROR_EN";
249 goto fail;
252 /* turn off notification till fan-out ports are reset and configured */
253 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
254 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
256 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
257 gscr[SATA_PMP_GSCR_FEAT_EN]);
258 if (err_mask) {
259 rc = -EIO;
260 reason = "failed to write GSCR_FEAT_EN";
261 goto fail;
265 if (print_info) {
266 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
267 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
268 sata_pmp_spec_rev_str(gscr),
269 sata_pmp_gscr_vendor(gscr),
270 sata_pmp_gscr_devid(gscr),
271 sata_pmp_gscr_rev(gscr),
272 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
273 gscr[SATA_PMP_GSCR_FEAT]);
275 if (!(dev->flags & ATA_DFLAG_AN))
276 ata_dev_printk(dev, KERN_INFO,
277 "Asynchronous notification not supported, "
278 "hotplug won't\n work on fan-out "
279 "ports. Use warm-plug instead.\n");
282 return 0;
284 fail:
285 ata_dev_printk(dev, KERN_ERR,
286 "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
287 reason, err_mask);
288 return rc;
291 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
293 struct ata_link *pmp_link = ap->pmp_link;
294 int i;
296 if (!pmp_link) {
297 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
298 GFP_NOIO);
299 if (!pmp_link)
300 return -ENOMEM;
302 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
303 ata_link_init(ap, &pmp_link[i], i);
305 ap->pmp_link = pmp_link;
308 for (i = 0; i < nr_ports; i++) {
309 struct ata_link *link = &pmp_link[i];
310 struct ata_eh_context *ehc = &link->eh_context;
312 link->flags = 0;
313 ehc->i.probe_mask |= ATA_ALL_DEVICES;
314 ehc->i.action |= ATA_EH_RESET;
317 return 0;
320 static void sata_pmp_quirks(struct ata_port *ap)
322 u32 *gscr = ap->link.device->gscr;
323 u16 vendor = sata_pmp_gscr_vendor(gscr);
324 u16 devid = sata_pmp_gscr_devid(gscr);
325 struct ata_link *link;
327 if (vendor == 0x1095 && devid == 0x3726) {
328 /* sil3726 quirks */
329 ata_port_for_each_link(link, ap) {
330 /* class code report is unreliable */
331 if (link->pmp < 5)
332 link->flags |= ATA_LFLAG_ASSUME_ATA;
334 /* port 5 is for SEMB device and it doesn't like SRST */
335 if (link->pmp == 5)
336 link->flags |= ATA_LFLAG_NO_SRST |
337 ATA_LFLAG_ASSUME_SEMB;
339 } else if (vendor == 0x1095 && devid == 0x4723) {
340 /* sil4723 quirks */
341 ata_port_for_each_link(link, ap) {
342 /* class code report is unreliable */
343 if (link->pmp < 2)
344 link->flags |= ATA_LFLAG_ASSUME_ATA;
346 /* the config device at port 2 locks up on SRST */
347 if (link->pmp == 2)
348 link->flags |= ATA_LFLAG_NO_SRST |
349 ATA_LFLAG_ASSUME_ATA;
351 } else if (vendor == 0x1095 && devid == 0x4726) {
352 /* sil4726 quirks */
353 ata_port_for_each_link(link, ap) {
354 /* Class code report is unreliable and SRST
355 * times out under certain configurations.
356 * Config device can be at port 0 or 5 and
357 * locks up on SRST.
359 if (link->pmp <= 5)
360 link->flags |= ATA_LFLAG_NO_SRST |
361 ATA_LFLAG_ASSUME_ATA;
363 /* Port 6 is for SEMB device which doesn't
364 * like SRST either.
366 if (link->pmp == 6)
367 link->flags |= ATA_LFLAG_NO_SRST |
368 ATA_LFLAG_ASSUME_SEMB;
370 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
371 devid == 0x5734 || devid == 0x5744)) {
372 /* sil5723/5744 quirks */
374 /* sil5723/5744 has either two or three downstream
375 * ports depending on operation mode. The last port
376 * is empty if any actual IO device is available or
377 * occupied by a pseudo configuration device
378 * otherwise. Don't try hard to recover it.
380 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
385 * sata_pmp_attach - attach a SATA PMP device
386 * @dev: SATA PMP device to attach
388 * Configure and attach SATA PMP device @dev. This function is
389 * also responsible for allocating and initializing PMP links.
391 * LOCKING:
392 * Kernel thread context (may sleep).
394 * RETURNS:
395 * 0 on success, -errno on failure.
397 int sata_pmp_attach(struct ata_device *dev)
399 struct ata_link *link = dev->link;
400 struct ata_port *ap = link->ap;
401 unsigned long flags;
402 struct ata_link *tlink;
403 int rc;
405 /* is it hanging off the right place? */
406 if (!(ap->flags & ATA_FLAG_PMP)) {
407 ata_dev_printk(dev, KERN_ERR,
408 "host does not support Port Multiplier\n");
409 return -EINVAL;
412 if (!ata_is_host_link(link)) {
413 ata_dev_printk(dev, KERN_ERR,
414 "Port Multipliers cannot be nested\n");
415 return -EINVAL;
418 if (dev->devno) {
419 ata_dev_printk(dev, KERN_ERR,
420 "Port Multiplier must be the first device\n");
421 return -EINVAL;
424 WARN_ON(link->pmp != 0);
425 link->pmp = SATA_PMP_CTRL_PORT;
427 /* read GSCR block */
428 rc = sata_pmp_read_gscr(dev, dev->gscr);
429 if (rc)
430 goto fail;
432 /* config PMP */
433 rc = sata_pmp_configure(dev, 1);
434 if (rc)
435 goto fail;
437 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
438 if (rc) {
439 ata_dev_printk(dev, KERN_INFO,
440 "failed to initialize PMP links\n");
441 goto fail;
444 /* attach it */
445 spin_lock_irqsave(ap->lock, flags);
446 WARN_ON(ap->nr_pmp_links);
447 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
448 spin_unlock_irqrestore(ap->lock, flags);
450 sata_pmp_quirks(ap);
452 if (ap->ops->pmp_attach)
453 ap->ops->pmp_attach(ap);
455 ata_port_for_each_link(tlink, ap)
456 sata_link_init_spd(tlink);
458 ata_acpi_associate_sata_port(ap);
460 return 0;
462 fail:
463 link->pmp = 0;
464 return rc;
468 * sata_pmp_detach - detach a SATA PMP device
469 * @dev: SATA PMP device to detach
471 * Detach SATA PMP device @dev. This function is also
472 * responsible for deconfiguring PMP links.
474 * LOCKING:
475 * Kernel thread context (may sleep).
477 static void sata_pmp_detach(struct ata_device *dev)
479 struct ata_link *link = dev->link;
480 struct ata_port *ap = link->ap;
481 struct ata_link *tlink;
482 unsigned long flags;
484 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
486 WARN_ON(!ata_is_host_link(link) || dev->devno ||
487 link->pmp != SATA_PMP_CTRL_PORT);
489 if (ap->ops->pmp_detach)
490 ap->ops->pmp_detach(ap);
492 ata_port_for_each_link(tlink, ap)
493 ata_eh_detach_dev(tlink->device);
495 spin_lock_irqsave(ap->lock, flags);
496 ap->nr_pmp_links = 0;
497 link->pmp = 0;
498 spin_unlock_irqrestore(ap->lock, flags);
500 ata_acpi_associate_sata_port(ap);
504 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
505 * @dev: PMP device to compare against
506 * @new_gscr: GSCR block of the new device
508 * Compare @new_gscr against @dev and determine whether @dev is
509 * the PMP described by @new_gscr.
511 * LOCKING:
512 * None.
514 * RETURNS:
515 * 1 if @dev matches @new_gscr, 0 otherwise.
517 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
519 const u32 *old_gscr = dev->gscr;
520 u16 old_vendor, new_vendor, old_devid, new_devid;
521 int old_nr_ports, new_nr_ports;
523 old_vendor = sata_pmp_gscr_vendor(old_gscr);
524 new_vendor = sata_pmp_gscr_vendor(new_gscr);
525 old_devid = sata_pmp_gscr_devid(old_gscr);
526 new_devid = sata_pmp_gscr_devid(new_gscr);
527 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
528 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
530 if (old_vendor != new_vendor) {
531 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
532 "vendor mismatch '0x%x' != '0x%x'\n",
533 old_vendor, new_vendor);
534 return 0;
537 if (old_devid != new_devid) {
538 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
539 "device ID mismatch '0x%x' != '0x%x'\n",
540 old_devid, new_devid);
541 return 0;
544 if (old_nr_ports != new_nr_ports) {
545 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
546 "nr_ports mismatch '0x%x' != '0x%x'\n",
547 old_nr_ports, new_nr_ports);
548 return 0;
551 return 1;
555 * sata_pmp_revalidate - revalidate SATA PMP
556 * @dev: PMP device to revalidate
557 * @new_class: new class code
559 * Re-read GSCR block and make sure @dev is still attached to the
560 * port and properly configured.
562 * LOCKING:
563 * Kernel thread context (may sleep).
565 * RETURNS:
566 * 0 on success, -errno otherwise.
568 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
570 struct ata_link *link = dev->link;
571 struct ata_port *ap = link->ap;
572 u32 *gscr = (void *)ap->sector_buf;
573 int rc;
575 DPRINTK("ENTER\n");
577 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
579 if (!ata_dev_enabled(dev)) {
580 rc = -ENODEV;
581 goto fail;
584 /* wrong class? */
585 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
586 rc = -ENODEV;
587 goto fail;
590 /* read GSCR */
591 rc = sata_pmp_read_gscr(dev, gscr);
592 if (rc)
593 goto fail;
595 /* is the pmp still there? */
596 if (!sata_pmp_same_pmp(dev, gscr)) {
597 rc = -ENODEV;
598 goto fail;
601 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
603 rc = sata_pmp_configure(dev, 0);
604 if (rc)
605 goto fail;
607 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
609 DPRINTK("EXIT, rc=0\n");
610 return 0;
612 fail:
613 ata_dev_printk(dev, KERN_ERR,
614 "PMP revalidation failed (errno=%d)\n", rc);
615 DPRINTK("EXIT, rc=%d\n", rc);
616 return rc;
620 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
621 * @dev: PMP device to revalidate
623 * Make sure the attached PMP is accessible.
625 * LOCKING:
626 * Kernel thread context (may sleep).
628 * RETURNS:
629 * 0 on success, -errno otherwise.
631 static int sata_pmp_revalidate_quick(struct ata_device *dev)
633 unsigned int err_mask;
634 u32 prod_id;
636 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
637 if (err_mask) {
638 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
639 "(Emask=0x%x)\n", err_mask);
640 return -EIO;
643 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
644 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
645 /* something weird is going on, request full PMP recovery */
646 return -EIO;
649 return 0;
653 * sata_pmp_eh_recover_pmp - recover PMP
654 * @ap: ATA port PMP is attached to
655 * @prereset: prereset method (can be NULL)
656 * @softreset: softreset method
657 * @hardreset: hardreset method
658 * @postreset: postreset method (can be NULL)
660 * Recover PMP attached to @ap. Recovery procedure is somewhat
661 * similar to that of ata_eh_recover() except that reset should
662 * always be performed in hard->soft sequence and recovery
663 * failure results in PMP detachment.
665 * LOCKING:
666 * Kernel thread context (may sleep).
668 * RETURNS:
669 * 0 on success, -errno on failure.
671 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
672 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
673 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
675 struct ata_link *link = &ap->link;
676 struct ata_eh_context *ehc = &link->eh_context;
677 struct ata_device *dev = link->device;
678 int tries = ATA_EH_PMP_TRIES;
679 int detach = 0, rc = 0;
680 int reval_failed = 0;
682 DPRINTK("ENTER\n");
684 if (dev->flags & ATA_DFLAG_DETACH) {
685 detach = 1;
686 goto fail;
689 retry:
690 ehc->classes[0] = ATA_DEV_UNKNOWN;
692 if (ehc->i.action & ATA_EH_RESET) {
693 struct ata_link *tlink;
695 ata_eh_freeze_port(ap);
697 /* reset */
698 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
699 postreset);
700 if (rc) {
701 ata_link_printk(link, KERN_ERR,
702 "failed to reset PMP, giving up\n");
703 goto fail;
706 ata_eh_thaw_port(ap);
708 /* PMP is reset, SErrors cannot be trusted, scan all */
709 ata_port_for_each_link(tlink, ap) {
710 struct ata_eh_context *ehc = &tlink->eh_context;
712 ehc->i.probe_mask |= ATA_ALL_DEVICES;
713 ehc->i.action |= ATA_EH_RESET;
717 /* If revalidation is requested, revalidate and reconfigure;
718 * otherwise, do quick revalidation.
720 if (ehc->i.action & ATA_EH_REVALIDATE)
721 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
722 else
723 rc = sata_pmp_revalidate_quick(dev);
725 if (rc) {
726 tries--;
728 if (rc == -ENODEV) {
729 ehc->i.probe_mask |= ATA_ALL_DEVICES;
730 detach = 1;
731 /* give it just two more chances */
732 tries = min(tries, 2);
735 if (tries) {
736 int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
738 /* consecutive revalidation failures? speed down */
739 if (reval_failed)
740 sata_down_spd_limit(link);
741 else
742 reval_failed = 1;
744 ata_dev_printk(dev, KERN_WARNING,
745 "retrying reset%s\n",
746 sleep ? " in 5 secs" : "");
747 if (sleep)
748 ssleep(5);
749 ehc->i.action |= ATA_EH_RESET;
750 goto retry;
751 } else {
752 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
753 "after %d tries, giving up\n",
754 ATA_EH_PMP_TRIES);
755 goto fail;
759 /* okay, PMP resurrected */
760 ehc->i.flags = 0;
762 DPRINTK("EXIT, rc=0\n");
763 return 0;
765 fail:
766 sata_pmp_detach(dev);
767 if (detach)
768 ata_eh_detach_dev(dev);
769 else
770 ata_dev_disable(dev);
772 DPRINTK("EXIT, rc=%d\n", rc);
773 return rc;
776 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
778 struct ata_link *link;
779 unsigned long flags;
780 int rc;
782 spin_lock_irqsave(ap->lock, flags);
784 ata_port_for_each_link(link, ap) {
785 if (!(link->flags & ATA_LFLAG_DISABLED))
786 continue;
788 spin_unlock_irqrestore(ap->lock, flags);
790 /* Some PMPs require hardreset sequence to get
791 * SError.N working.
793 sata_link_hardreset(link, sata_deb_timing_normal,
794 jiffies + ATA_TMOUT_INTERNAL_QUICK, NULL, NULL);
796 /* unconditionally clear SError.N */
797 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
798 if (rc) {
799 ata_link_printk(link, KERN_ERR, "failed to clear "
800 "SError.N (errno=%d)\n", rc);
801 return rc;
804 spin_lock_irqsave(ap->lock, flags);
807 spin_unlock_irqrestore(ap->lock, flags);
809 return 0;
812 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
814 struct ata_port *ap = link->ap;
815 unsigned long flags;
817 if (link_tries[link->pmp] && --link_tries[link->pmp])
818 return 1;
820 /* disable this link */
821 if (!(link->flags & ATA_LFLAG_DISABLED)) {
822 ata_link_printk(link, KERN_WARNING,
823 "failed to recover link after %d tries, disabling\n",
824 ATA_EH_PMP_LINK_TRIES);
826 spin_lock_irqsave(ap->lock, flags);
827 link->flags |= ATA_LFLAG_DISABLED;
828 spin_unlock_irqrestore(ap->lock, flags);
831 ata_dev_disable(link->device);
832 link->eh_context.i.action = 0;
834 return 0;
838 * sata_pmp_eh_recover - recover PMP-enabled port
839 * @ap: ATA port to recover
841 * Drive EH recovery operation for PMP enabled port @ap. This
842 * function recovers host and PMP ports with proper retrials and
843 * fallbacks. Actual recovery operations are performed using
844 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
846 * LOCKING:
847 * Kernel thread context (may sleep).
849 * RETURNS:
850 * 0 on success, -errno on failure.
852 static int sata_pmp_eh_recover(struct ata_port *ap)
854 struct ata_port_operations *ops = ap->ops;
855 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
856 struct ata_link *pmp_link = &ap->link;
857 struct ata_device *pmp_dev = pmp_link->device;
858 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
859 struct ata_link *link;
860 struct ata_device *dev;
861 unsigned int err_mask;
862 u32 gscr_error, sntf;
863 int cnt, rc;
865 pmp_tries = ATA_EH_PMP_TRIES;
866 ata_port_for_each_link(link, ap)
867 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
869 retry:
870 /* PMP attached? */
871 if (!ap->nr_pmp_links) {
872 rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
873 ops->hardreset, ops->postreset, NULL);
874 if (rc) {
875 ata_link_for_each_dev(dev, &ap->link)
876 ata_dev_disable(dev);
877 return rc;
880 if (pmp_dev->class != ATA_DEV_PMP)
881 return 0;
883 /* new PMP online */
884 ata_port_for_each_link(link, ap)
885 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
887 /* fall through */
890 /* recover pmp */
891 rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
892 ops->hardreset, ops->postreset);
893 if (rc)
894 goto pmp_fail;
896 /* handle disabled links */
897 rc = sata_pmp_eh_handle_disabled_links(ap);
898 if (rc)
899 goto pmp_fail;
901 /* recover links */
902 rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
903 ops->pmp_hardreset, ops->pmp_postreset, &link);
904 if (rc)
905 goto link_fail;
907 /* Connection status might have changed while resetting other
908 * links, check SATA_PMP_GSCR_ERROR before returning.
911 /* clear SNotification */
912 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
913 if (rc == 0)
914 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
916 /* enable notification */
917 if (pmp_dev->flags & ATA_DFLAG_AN) {
918 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
920 err_mask = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
921 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
922 if (err_mask) {
923 ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
924 "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
925 rc = -EIO;
926 goto pmp_fail;
930 /* check GSCR_ERROR */
931 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
932 if (err_mask) {
933 ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
934 "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
935 rc = -EIO;
936 goto pmp_fail;
939 cnt = 0;
940 ata_port_for_each_link(link, ap) {
941 if (!(gscr_error & (1 << link->pmp)))
942 continue;
944 if (sata_pmp_handle_link_fail(link, link_tries)) {
945 ata_ehi_hotplugged(&link->eh_context.i);
946 cnt++;
947 } else {
948 ata_link_printk(link, KERN_WARNING,
949 "PHY status changed but maxed out on retries, "
950 "giving up\n");
951 ata_link_printk(link, KERN_WARNING,
952 "Manully issue scan to resume this link\n");
956 if (cnt) {
957 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
958 "ports, repeating recovery\n");
959 goto retry;
962 return 0;
964 link_fail:
965 if (sata_pmp_handle_link_fail(link, link_tries)) {
966 pmp_ehc->i.action |= ATA_EH_RESET;
967 goto retry;
970 /* fall through */
971 pmp_fail:
972 /* Control always ends up here after detaching PMP. Shut up
973 * and return if we're unloading.
975 if (ap->pflags & ATA_PFLAG_UNLOADING)
976 return rc;
978 if (!ap->nr_pmp_links)
979 goto retry;
981 if (--pmp_tries) {
982 ata_port_printk(ap, KERN_WARNING,
983 "failed to recover PMP, retrying in 5 secs\n");
984 pmp_ehc->i.action |= ATA_EH_RESET;
985 ssleep(5);
986 goto retry;
989 ata_port_printk(ap, KERN_ERR,
990 "failed to recover PMP after %d tries, giving up\n",
991 ATA_EH_PMP_TRIES);
992 sata_pmp_detach(pmp_dev);
993 ata_dev_disable(pmp_dev);
995 return rc;
999 * sata_pmp_error_handler - do standard error handling for PMP-enabled host
1000 * @ap: host port to handle error for
1002 * Perform standard error handling sequence for PMP-enabled host
1003 * @ap.
1005 * LOCKING:
1006 * Kernel thread context (may sleep).
1008 void sata_pmp_error_handler(struct ata_port *ap)
1010 ata_eh_autopsy(ap);
1011 ata_eh_report(ap);
1012 sata_pmp_eh_recover(ap);
1013 ata_eh_finish(ap);