ALSA: hda - Add quirk for Dell Vostro 1220
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / ata / libata-pmp.c
blob00305f41ed86397a0af8746cccd03160020a2881
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 <linux/slab.h>
13 #include "libata.h"
15 const struct ata_port_operations sata_pmp_port_ops = {
16 .inherits = &sata_port_ops,
17 .pmp_prereset = ata_std_prereset,
18 .pmp_hardreset = sata_std_hardreset,
19 .pmp_postreset = ata_std_postreset,
20 .error_handler = sata_pmp_error_handler,
23 /**
24 * sata_pmp_read - read PMP register
25 * @link: link to read PMP register for
26 * @reg: register to read
27 * @r_val: resulting value
29 * Read PMP register.
31 * LOCKING:
32 * Kernel thread context (may sleep).
34 * RETURNS:
35 * 0 on success, AC_ERR_* mask on failure.
37 static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
39 struct ata_port *ap = link->ap;
40 struct ata_device *pmp_dev = ap->link.device;
41 struct ata_taskfile tf;
42 unsigned int err_mask;
44 ata_tf_init(pmp_dev, &tf);
45 tf.command = ATA_CMD_PMP_READ;
46 tf.protocol = ATA_PROT_NODATA;
47 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
48 tf.feature = reg;
49 tf.device = link->pmp;
51 err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
52 SATA_PMP_RW_TIMEOUT);
53 if (err_mask)
54 return err_mask;
56 *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
57 return 0;
60 /**
61 * sata_pmp_write - write PMP register
62 * @link: link to write PMP register for
63 * @reg: register to write
64 * @r_val: value to write
66 * Write PMP register.
68 * LOCKING:
69 * Kernel thread context (may sleep).
71 * RETURNS:
72 * 0 on success, AC_ERR_* mask on failure.
74 static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
76 struct ata_port *ap = link->ap;
77 struct ata_device *pmp_dev = ap->link.device;
78 struct ata_taskfile tf;
80 ata_tf_init(pmp_dev, &tf);
81 tf.command = ATA_CMD_PMP_WRITE;
82 tf.protocol = ATA_PROT_NODATA;
83 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
84 tf.feature = reg;
85 tf.device = link->pmp;
86 tf.nsect = val & 0xff;
87 tf.lbal = (val >> 8) & 0xff;
88 tf.lbam = (val >> 16) & 0xff;
89 tf.lbah = (val >> 24) & 0xff;
91 return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
92 SATA_PMP_RW_TIMEOUT);
95 /**
96 * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
97 * @qc: ATA command in question
99 * A host which has command switching PMP support cannot issue
100 * commands to multiple links simultaneously.
102 * LOCKING:
103 * spin_lock_irqsave(host lock)
105 * RETURNS:
106 * ATA_DEFER_* if deferring is needed, 0 otherwise.
108 int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
110 struct ata_link *link = qc->dev->link;
111 struct ata_port *ap = link->ap;
113 if (ap->excl_link == NULL || ap->excl_link == link) {
114 if (ap->nr_active_links == 0 || ata_link_active(link)) {
115 qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
116 return ata_std_qc_defer(qc);
119 ap->excl_link = link;
122 return ATA_DEFER_PORT;
126 * sata_pmp_scr_read - read PSCR
127 * @link: ATA link to read PSCR for
128 * @reg: PSCR to read
129 * @r_val: resulting value
131 * Read PSCR @reg into @r_val for @link, to be called from
132 * ata_scr_read().
134 * LOCKING:
135 * Kernel thread context (may sleep).
137 * RETURNS:
138 * 0 on success, -errno on failure.
140 int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
142 unsigned int err_mask;
144 if (reg > SATA_PMP_PSCR_CONTROL)
145 return -EINVAL;
147 err_mask = sata_pmp_read(link, reg, r_val);
148 if (err_mask) {
149 ata_link_printk(link, KERN_WARNING, "failed to read SCR %d "
150 "(Emask=0x%x)\n", reg, err_mask);
151 return -EIO;
153 return 0;
157 * sata_pmp_scr_write - write PSCR
158 * @link: ATA link to write PSCR for
159 * @reg: PSCR to write
160 * @val: value to be written
162 * Write @val to PSCR @reg for @link, to be called from
163 * ata_scr_write() and ata_scr_write_flush().
165 * LOCKING:
166 * Kernel thread context (may sleep).
168 * RETURNS:
169 * 0 on success, -errno on failure.
171 int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
173 unsigned int err_mask;
175 if (reg > SATA_PMP_PSCR_CONTROL)
176 return -EINVAL;
178 err_mask = sata_pmp_write(link, reg, val);
179 if (err_mask) {
180 ata_link_printk(link, KERN_WARNING, "failed to write SCR %d "
181 "(Emask=0x%x)\n", reg, err_mask);
182 return -EIO;
184 return 0;
188 * sata_pmp_read_gscr - read GSCR block of SATA PMP
189 * @dev: PMP device
190 * @gscr: buffer to read GSCR block into
192 * Read selected PMP GSCRs from the PMP at @dev. This will serve
193 * as configuration and identification info for the PMP.
195 * LOCKING:
196 * Kernel thread context (may sleep).
198 * RETURNS:
199 * 0 on success, -errno on failure.
201 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
203 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
204 int i;
206 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
207 int reg = gscr_to_read[i];
208 unsigned int err_mask;
210 err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
211 if (err_mask) {
212 ata_dev_printk(dev, KERN_ERR, "failed to read PMP "
213 "GSCR[%d] (Emask=0x%x)\n", reg, err_mask);
214 return -EIO;
218 return 0;
221 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
223 u32 rev = gscr[SATA_PMP_GSCR_REV];
225 if (rev & (1 << 3))
226 return "1.2";
227 if (rev & (1 << 2))
228 return "1.1";
229 if (rev & (1 << 1))
230 return "1.0";
231 return "<unknown>";
234 static int sata_pmp_configure(struct ata_device *dev, int print_info)
236 struct ata_port *ap = dev->link->ap;
237 u32 *gscr = dev->gscr;
238 unsigned int err_mask = 0;
239 const char *reason;
240 int nr_ports, rc;
242 nr_ports = sata_pmp_gscr_ports(gscr);
244 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
245 rc = -EINVAL;
246 reason = "invalid nr_ports";
247 goto fail;
250 if ((ap->flags & ATA_FLAG_AN) &&
251 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
252 dev->flags |= ATA_DFLAG_AN;
254 /* monitor SERR_PHYRDY_CHG on fan-out ports */
255 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
256 SERR_PHYRDY_CHG);
257 if (err_mask) {
258 rc = -EIO;
259 reason = "failed to write GSCR_ERROR_EN";
260 goto fail;
263 if (print_info) {
264 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
265 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
266 sata_pmp_spec_rev_str(gscr),
267 sata_pmp_gscr_vendor(gscr),
268 sata_pmp_gscr_devid(gscr),
269 sata_pmp_gscr_rev(gscr),
270 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
271 gscr[SATA_PMP_GSCR_FEAT]);
273 if (!(dev->flags & ATA_DFLAG_AN))
274 ata_dev_printk(dev, KERN_INFO,
275 "Asynchronous notification not supported, "
276 "hotplug won't\n work on fan-out "
277 "ports. Use warm-plug instead.\n");
280 return 0;
282 fail:
283 ata_dev_printk(dev, KERN_ERR,
284 "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
285 reason, err_mask);
286 return rc;
289 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
291 struct ata_link *pmp_link = ap->pmp_link;
292 int i;
294 if (!pmp_link) {
295 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
296 GFP_NOIO);
297 if (!pmp_link)
298 return -ENOMEM;
300 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
301 ata_link_init(ap, &pmp_link[i], i);
303 ap->pmp_link = pmp_link;
306 for (i = 0; i < nr_ports; i++) {
307 struct ata_link *link = &pmp_link[i];
308 struct ata_eh_context *ehc = &link->eh_context;
310 link->flags = 0;
311 ehc->i.probe_mask |= ATA_ALL_DEVICES;
312 ehc->i.action |= ATA_EH_RESET;
315 return 0;
318 static void sata_pmp_quirks(struct ata_port *ap)
320 u32 *gscr = ap->link.device->gscr;
321 u16 vendor = sata_pmp_gscr_vendor(gscr);
322 u16 devid = sata_pmp_gscr_devid(gscr);
323 struct ata_link *link;
325 if (vendor == 0x1095 && devid == 0x3726) {
326 /* sil3726 quirks */
327 ata_for_each_link(link, ap, EDGE) {
328 /* Class code report is unreliable and SRST
329 * times out under certain configurations.
331 if (link->pmp < 5)
332 link->flags |= ATA_LFLAG_NO_SRST |
333 ATA_LFLAG_ASSUME_ATA;
335 /* port 5 is for SEMB device and it doesn't like SRST */
336 if (link->pmp == 5)
337 link->flags |= ATA_LFLAG_NO_SRST |
338 ATA_LFLAG_ASSUME_SEMB;
340 } else if (vendor == 0x1095 && devid == 0x4723) {
341 /* sil4723 quirks */
342 ata_for_each_link(link, ap, EDGE) {
343 /* class code report is unreliable */
344 if (link->pmp < 2)
345 link->flags |= ATA_LFLAG_ASSUME_ATA;
347 /* the config device at port 2 locks up on SRST */
348 if (link->pmp == 2)
349 link->flags |= ATA_LFLAG_NO_SRST |
350 ATA_LFLAG_ASSUME_ATA;
352 } else if (vendor == 0x1095 && devid == 0x4726) {
353 /* sil4726 quirks */
354 ata_for_each_link(link, ap, EDGE) {
355 /* Class code report is unreliable and SRST
356 * times out under certain configurations.
357 * Config device can be at port 0 or 5 and
358 * locks up on SRST.
360 if (link->pmp <= 5)
361 link->flags |= ATA_LFLAG_NO_SRST |
362 ATA_LFLAG_ASSUME_ATA;
364 /* Port 6 is for SEMB device which doesn't
365 * like SRST either.
367 if (link->pmp == 6)
368 link->flags |= ATA_LFLAG_NO_SRST |
369 ATA_LFLAG_ASSUME_SEMB;
371 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
372 devid == 0x5734 || devid == 0x5744)) {
373 /* sil5723/5744 quirks */
375 /* sil5723/5744 has either two or three downstream
376 * ports depending on operation mode. The last port
377 * is empty if any actual IO device is available or
378 * occupied by a pseudo configuration device
379 * otherwise. Don't try hard to recover it.
381 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
386 * sata_pmp_attach - attach a SATA PMP device
387 * @dev: SATA PMP device to attach
389 * Configure and attach SATA PMP device @dev. This function is
390 * also responsible for allocating and initializing PMP links.
392 * LOCKING:
393 * Kernel thread context (may sleep).
395 * RETURNS:
396 * 0 on success, -errno on failure.
398 int sata_pmp_attach(struct ata_device *dev)
400 struct ata_link *link = dev->link;
401 struct ata_port *ap = link->ap;
402 unsigned long flags;
403 struct ata_link *tlink;
404 int rc;
406 /* is it hanging off the right place? */
407 if (!sata_pmp_supported(ap)) {
408 ata_dev_printk(dev, KERN_ERR,
409 "host does not support Port Multiplier\n");
410 return -EINVAL;
413 if (!ata_is_host_link(link)) {
414 ata_dev_printk(dev, KERN_ERR,
415 "Port Multipliers cannot be nested\n");
416 return -EINVAL;
419 if (dev->devno) {
420 ata_dev_printk(dev, KERN_ERR,
421 "Port Multiplier must be the first device\n");
422 return -EINVAL;
425 WARN_ON(link->pmp != 0);
426 link->pmp = SATA_PMP_CTRL_PORT;
428 /* read GSCR block */
429 rc = sata_pmp_read_gscr(dev, dev->gscr);
430 if (rc)
431 goto fail;
433 /* config PMP */
434 rc = sata_pmp_configure(dev, 1);
435 if (rc)
436 goto fail;
438 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
439 if (rc) {
440 ata_dev_printk(dev, KERN_INFO,
441 "failed to initialize PMP links\n");
442 goto fail;
445 /* attach it */
446 spin_lock_irqsave(ap->lock, flags);
447 WARN_ON(ap->nr_pmp_links);
448 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
449 spin_unlock_irqrestore(ap->lock, flags);
451 sata_pmp_quirks(ap);
453 if (ap->ops->pmp_attach)
454 ap->ops->pmp_attach(ap);
456 ata_for_each_link(tlink, ap, EDGE)
457 sata_link_init_spd(tlink);
459 ata_acpi_associate_sata_port(ap);
461 return 0;
463 fail:
464 link->pmp = 0;
465 return rc;
469 * sata_pmp_detach - detach a SATA PMP device
470 * @dev: SATA PMP device to detach
472 * Detach SATA PMP device @dev. This function is also
473 * responsible for deconfiguring PMP links.
475 * LOCKING:
476 * Kernel thread context (may sleep).
478 static void sata_pmp_detach(struct ata_device *dev)
480 struct ata_link *link = dev->link;
481 struct ata_port *ap = link->ap;
482 struct ata_link *tlink;
483 unsigned long flags;
485 ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
487 WARN_ON(!ata_is_host_link(link) || dev->devno ||
488 link->pmp != SATA_PMP_CTRL_PORT);
490 if (ap->ops->pmp_detach)
491 ap->ops->pmp_detach(ap);
493 ata_for_each_link(tlink, ap, EDGE)
494 ata_eh_detach_dev(tlink->device);
496 spin_lock_irqsave(ap->lock, flags);
497 ap->nr_pmp_links = 0;
498 link->pmp = 0;
499 spin_unlock_irqrestore(ap->lock, flags);
501 ata_acpi_associate_sata_port(ap);
505 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
506 * @dev: PMP device to compare against
507 * @new_gscr: GSCR block of the new device
509 * Compare @new_gscr against @dev and determine whether @dev is
510 * the PMP described by @new_gscr.
512 * LOCKING:
513 * None.
515 * RETURNS:
516 * 1 if @dev matches @new_gscr, 0 otherwise.
518 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
520 const u32 *old_gscr = dev->gscr;
521 u16 old_vendor, new_vendor, old_devid, new_devid;
522 int old_nr_ports, new_nr_ports;
524 old_vendor = sata_pmp_gscr_vendor(old_gscr);
525 new_vendor = sata_pmp_gscr_vendor(new_gscr);
526 old_devid = sata_pmp_gscr_devid(old_gscr);
527 new_devid = sata_pmp_gscr_devid(new_gscr);
528 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
529 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
531 if (old_vendor != new_vendor) {
532 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
533 "vendor mismatch '0x%x' != '0x%x'\n",
534 old_vendor, new_vendor);
535 return 0;
538 if (old_devid != new_devid) {
539 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
540 "device ID mismatch '0x%x' != '0x%x'\n",
541 old_devid, new_devid);
542 return 0;
545 if (old_nr_ports != new_nr_ports) {
546 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
547 "nr_ports mismatch '0x%x' != '0x%x'\n",
548 old_nr_ports, new_nr_ports);
549 return 0;
552 return 1;
556 * sata_pmp_revalidate - revalidate SATA PMP
557 * @dev: PMP device to revalidate
558 * @new_class: new class code
560 * Re-read GSCR block and make sure @dev is still attached to the
561 * port and properly configured.
563 * LOCKING:
564 * Kernel thread context (may sleep).
566 * RETURNS:
567 * 0 on success, -errno otherwise.
569 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
571 struct ata_link *link = dev->link;
572 struct ata_port *ap = link->ap;
573 u32 *gscr = (void *)ap->sector_buf;
574 int rc;
576 DPRINTK("ENTER\n");
578 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
580 if (!ata_dev_enabled(dev)) {
581 rc = -ENODEV;
582 goto fail;
585 /* wrong class? */
586 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
587 rc = -ENODEV;
588 goto fail;
591 /* read GSCR */
592 rc = sata_pmp_read_gscr(dev, gscr);
593 if (rc)
594 goto fail;
596 /* is the pmp still there? */
597 if (!sata_pmp_same_pmp(dev, gscr)) {
598 rc = -ENODEV;
599 goto fail;
602 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
604 rc = sata_pmp_configure(dev, 0);
605 if (rc)
606 goto fail;
608 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
610 DPRINTK("EXIT, rc=0\n");
611 return 0;
613 fail:
614 ata_dev_printk(dev, KERN_ERR,
615 "PMP revalidation failed (errno=%d)\n", rc);
616 DPRINTK("EXIT, rc=%d\n", rc);
617 return rc;
621 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
622 * @dev: PMP device to revalidate
624 * Make sure the attached PMP is accessible.
626 * LOCKING:
627 * Kernel thread context (may sleep).
629 * RETURNS:
630 * 0 on success, -errno otherwise.
632 static int sata_pmp_revalidate_quick(struct ata_device *dev)
634 unsigned int err_mask;
635 u32 prod_id;
637 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
638 if (err_mask) {
639 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID "
640 "(Emask=0x%x)\n", err_mask);
641 return -EIO;
644 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
645 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
646 /* something weird is going on, request full PMP recovery */
647 return -EIO;
650 return 0;
654 * sata_pmp_eh_recover_pmp - recover PMP
655 * @ap: ATA port PMP is attached to
656 * @prereset: prereset method (can be NULL)
657 * @softreset: softreset method
658 * @hardreset: hardreset method
659 * @postreset: postreset method (can be NULL)
661 * Recover PMP attached to @ap. Recovery procedure is somewhat
662 * similar to that of ata_eh_recover() except that reset should
663 * always be performed in hard->soft sequence and recovery
664 * failure results in PMP detachment.
666 * LOCKING:
667 * Kernel thread context (may sleep).
669 * RETURNS:
670 * 0 on success, -errno on failure.
672 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
673 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
674 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
676 struct ata_link *link = &ap->link;
677 struct ata_eh_context *ehc = &link->eh_context;
678 struct ata_device *dev = link->device;
679 int tries = ATA_EH_PMP_TRIES;
680 int detach = 0, rc = 0;
681 int reval_failed = 0;
683 DPRINTK("ENTER\n");
685 if (dev->flags & ATA_DFLAG_DETACH) {
686 detach = 1;
687 goto fail;
690 retry:
691 ehc->classes[0] = ATA_DEV_UNKNOWN;
693 if (ehc->i.action & ATA_EH_RESET) {
694 struct ata_link *tlink;
696 /* reset */
697 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
698 postreset);
699 if (rc) {
700 ata_link_printk(link, KERN_ERR,
701 "failed to reset PMP, giving up\n");
702 goto fail;
705 /* PMP is reset, SErrors cannot be trusted, scan all */
706 ata_for_each_link(tlink, ap, EDGE) {
707 struct ata_eh_context *ehc = &tlink->eh_context;
709 ehc->i.probe_mask |= ATA_ALL_DEVICES;
710 ehc->i.action |= ATA_EH_RESET;
714 /* If revalidation is requested, revalidate and reconfigure;
715 * otherwise, do quick revalidation.
717 if (ehc->i.action & ATA_EH_REVALIDATE)
718 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
719 else
720 rc = sata_pmp_revalidate_quick(dev);
722 if (rc) {
723 tries--;
725 if (rc == -ENODEV) {
726 ehc->i.probe_mask |= ATA_ALL_DEVICES;
727 detach = 1;
728 /* give it just two more chances */
729 tries = min(tries, 2);
732 if (tries) {
733 /* consecutive revalidation failures? speed down */
734 if (reval_failed)
735 sata_down_spd_limit(link, 0);
736 else
737 reval_failed = 1;
739 ehc->i.action |= ATA_EH_RESET;
740 goto retry;
741 } else {
742 ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
743 "after %d tries, giving up\n",
744 ATA_EH_PMP_TRIES);
745 goto fail;
749 /* okay, PMP resurrected */
750 ehc->i.flags = 0;
752 DPRINTK("EXIT, rc=0\n");
753 return 0;
755 fail:
756 sata_pmp_detach(dev);
757 if (detach)
758 ata_eh_detach_dev(dev);
759 else
760 ata_dev_disable(dev);
762 DPRINTK("EXIT, rc=%d\n", rc);
763 return rc;
766 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
768 struct ata_link *link;
769 unsigned long flags;
770 int rc;
772 spin_lock_irqsave(ap->lock, flags);
774 ata_for_each_link(link, ap, EDGE) {
775 if (!(link->flags & ATA_LFLAG_DISABLED))
776 continue;
778 spin_unlock_irqrestore(ap->lock, flags);
780 /* Some PMPs require hardreset sequence to get
781 * SError.N working.
783 sata_link_hardreset(link, sata_deb_timing_normal,
784 ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK),
785 NULL, NULL);
787 /* unconditionally clear SError.N */
788 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
789 if (rc) {
790 ata_link_printk(link, KERN_ERR, "failed to clear "
791 "SError.N (errno=%d)\n", rc);
792 return rc;
795 spin_lock_irqsave(ap->lock, flags);
798 spin_unlock_irqrestore(ap->lock, flags);
800 return 0;
803 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
805 struct ata_port *ap = link->ap;
806 unsigned long flags;
808 if (link_tries[link->pmp] && --link_tries[link->pmp])
809 return 1;
811 /* disable this link */
812 if (!(link->flags & ATA_LFLAG_DISABLED)) {
813 ata_link_printk(link, KERN_WARNING,
814 "failed to recover link after %d tries, disabling\n",
815 ATA_EH_PMP_LINK_TRIES);
817 spin_lock_irqsave(ap->lock, flags);
818 link->flags |= ATA_LFLAG_DISABLED;
819 spin_unlock_irqrestore(ap->lock, flags);
822 ata_dev_disable(link->device);
823 link->eh_context.i.action = 0;
825 return 0;
829 * sata_pmp_eh_recover - recover PMP-enabled port
830 * @ap: ATA port to recover
832 * Drive EH recovery operation for PMP enabled port @ap. This
833 * function recovers host and PMP ports with proper retrials and
834 * fallbacks. Actual recovery operations are performed using
835 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
837 * LOCKING:
838 * Kernel thread context (may sleep).
840 * RETURNS:
841 * 0 on success, -errno on failure.
843 static int sata_pmp_eh_recover(struct ata_port *ap)
845 struct ata_port_operations *ops = ap->ops;
846 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
847 struct ata_link *pmp_link = &ap->link;
848 struct ata_device *pmp_dev = pmp_link->device;
849 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
850 u32 *gscr = pmp_dev->gscr;
851 struct ata_link *link;
852 struct ata_device *dev;
853 unsigned int err_mask;
854 u32 gscr_error, sntf;
855 int cnt, rc;
857 pmp_tries = ATA_EH_PMP_TRIES;
858 ata_for_each_link(link, ap, EDGE)
859 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
861 retry:
862 /* PMP attached? */
863 if (!sata_pmp_attached(ap)) {
864 rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
865 ops->hardreset, ops->postreset, NULL);
866 if (rc) {
867 ata_for_each_dev(dev, &ap->link, ALL)
868 ata_dev_disable(dev);
869 return rc;
872 if (pmp_dev->class != ATA_DEV_PMP)
873 return 0;
875 /* new PMP online */
876 ata_for_each_link(link, ap, EDGE)
877 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
879 /* fall through */
882 /* recover pmp */
883 rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
884 ops->hardreset, ops->postreset);
885 if (rc)
886 goto pmp_fail;
888 /* PHY event notification can disturb reset and other recovery
889 * operations. Turn it off.
891 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
892 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
894 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
895 gscr[SATA_PMP_GSCR_FEAT_EN]);
896 if (err_mask) {
897 ata_link_printk(pmp_link, KERN_WARNING,
898 "failed to disable NOTIFY (err_mask=0x%x)\n",
899 err_mask);
900 goto pmp_fail;
904 /* handle disabled links */
905 rc = sata_pmp_eh_handle_disabled_links(ap);
906 if (rc)
907 goto pmp_fail;
909 /* recover links */
910 rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
911 ops->pmp_hardreset, ops->pmp_postreset, &link);
912 if (rc)
913 goto link_fail;
915 /* Connection status might have changed while resetting other
916 * links, check SATA_PMP_GSCR_ERROR before returning.
919 /* clear SNotification */
920 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
921 if (rc == 0)
922 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
924 /* enable notification */
925 if (pmp_dev->flags & ATA_DFLAG_AN) {
926 gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
928 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
929 gscr[SATA_PMP_GSCR_FEAT_EN]);
930 if (err_mask) {
931 ata_dev_printk(pmp_dev, KERN_ERR, "failed to write "
932 "PMP_FEAT_EN (Emask=0x%x)\n", err_mask);
933 rc = -EIO;
934 goto pmp_fail;
938 /* check GSCR_ERROR */
939 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
940 if (err_mask) {
941 ata_dev_printk(pmp_dev, KERN_ERR, "failed to read "
942 "PMP_GSCR_ERROR (Emask=0x%x)\n", err_mask);
943 rc = -EIO;
944 goto pmp_fail;
947 cnt = 0;
948 ata_for_each_link(link, ap, EDGE) {
949 if (!(gscr_error & (1 << link->pmp)))
950 continue;
952 if (sata_pmp_handle_link_fail(link, link_tries)) {
953 ata_ehi_hotplugged(&link->eh_context.i);
954 cnt++;
955 } else {
956 ata_link_printk(link, KERN_WARNING,
957 "PHY status changed but maxed out on retries, "
958 "giving up\n");
959 ata_link_printk(link, KERN_WARNING,
960 "Manully issue scan to resume this link\n");
964 if (cnt) {
965 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
966 "ports, repeating recovery\n");
967 goto retry;
970 return 0;
972 link_fail:
973 if (sata_pmp_handle_link_fail(link, link_tries)) {
974 pmp_ehc->i.action |= ATA_EH_RESET;
975 goto retry;
978 /* fall through */
979 pmp_fail:
980 /* Control always ends up here after detaching PMP. Shut up
981 * and return if we're unloading.
983 if (ap->pflags & ATA_PFLAG_UNLOADING)
984 return rc;
986 if (!sata_pmp_attached(ap))
987 goto retry;
989 if (--pmp_tries) {
990 pmp_ehc->i.action |= ATA_EH_RESET;
991 goto retry;
994 ata_port_printk(ap, KERN_ERR,
995 "failed to recover PMP after %d tries, giving up\n",
996 ATA_EH_PMP_TRIES);
997 sata_pmp_detach(pmp_dev);
998 ata_dev_disable(pmp_dev);
1000 return rc;
1004 * sata_pmp_error_handler - do standard error handling for PMP-enabled host
1005 * @ap: host port to handle error for
1007 * Perform standard error handling sequence for PMP-enabled host
1008 * @ap.
1010 * LOCKING:
1011 * Kernel thread context (may sleep).
1013 void sata_pmp_error_handler(struct ata_port *ap)
1015 ata_eh_autopsy(ap);
1016 ata_eh_report(ap);
1017 sata_pmp_eh_recover(ap);
1018 ata_eh_finish(ap);
1021 EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
1022 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
1023 EXPORT_SYMBOL_GPL(sata_pmp_error_handler);