Merge branch 'fix/hda' into for-linus
[pandora-kernel.git] / drivers / ata / libata-pmp.c
1 /*
2  * libata-pmp.c - libata port multiplier support
3  *
4  * Copyright (c) 2007  SUSE Linux Products GmbH
5  * Copyright (c) 2007  Tejun Heo <teheo@suse.de>
6  *
7  * This file is released under the GPLv2.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/libata.h>
12 #include <linux/slab.h>
13 #include "libata.h"
14
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,
21 };
22
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
28  *
29  *      Read PMP register.
30  *
31  *      LOCKING:
32  *      Kernel thread context (may sleep).
33  *
34  *      RETURNS:
35  *      0 on success, AC_ERR_* mask on failure.
36  */
37 static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
38 {
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;
43
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;
50
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;
55
56         *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
57         return 0;
58 }
59
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
65  *
66  *      Write PMP register.
67  *
68  *      LOCKING:
69  *      Kernel thread context (may sleep).
70  *
71  *      RETURNS:
72  *      0 on success, AC_ERR_* mask on failure.
73  */
74 static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
75 {
76         struct ata_port *ap = link->ap;
77         struct ata_device *pmp_dev = ap->link.device;
78         struct ata_taskfile tf;
79
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;
90
91         return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
92                                  SATA_PMP_RW_TIMEOUT);
93 }
94
95 /**
96  *      sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
97  *      @qc: ATA command in question
98  *
99  *      A host which has command switching PMP support cannot issue
100  *      commands to multiple links simultaneously.
101  *
102  *      LOCKING:
103  *      spin_lock_irqsave(host lock)
104  *
105  *      RETURNS:
106  *      ATA_DEFER_* if deferring is needed, 0 otherwise.
107  */
108 int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
109 {
110         struct ata_link *link = qc->dev->link;
111         struct ata_port *ap = link->ap;
112
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);
117                 }
118
119                 ap->excl_link = link;
120         }
121
122         return ATA_DEFER_PORT;
123 }
124
125 /**
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
130  *
131  *      Read PSCR @reg into @r_val for @link, to be called from
132  *      ata_scr_read().
133  *
134  *      LOCKING:
135  *      Kernel thread context (may sleep).
136  *
137  *      RETURNS:
138  *      0 on success, -errno on failure.
139  */
140 int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
141 {
142         unsigned int err_mask;
143
144         if (reg > SATA_PMP_PSCR_CONTROL)
145                 return -EINVAL;
146
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;
152         }
153         return 0;
154 }
155
156 /**
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
161  *
162  *      Write @val to PSCR @reg for @link, to be called from
163  *      ata_scr_write() and ata_scr_write_flush().
164  *
165  *      LOCKING:
166  *      Kernel thread context (may sleep).
167  *
168  *      RETURNS:
169  *      0 on success, -errno on failure.
170  */
171 int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
172 {
173         unsigned int err_mask;
174
175         if (reg > SATA_PMP_PSCR_CONTROL)
176                 return -EINVAL;
177
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;
183         }
184         return 0;
185 }
186
187 /**
188  *      sata_pmp_read_gscr - read GSCR block of SATA PMP
189  *      @dev: PMP device
190  *      @gscr: buffer to read GSCR block into
191  *
192  *      Read selected PMP GSCRs from the PMP at @dev.  This will serve
193  *      as configuration and identification info for the PMP.
194  *
195  *      LOCKING:
196  *      Kernel thread context (may sleep).
197  *
198  *      RETURNS:
199  *      0 on success, -errno on failure.
200  */
201 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
202 {
203         static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
204         int i;
205
206         for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
207                 int reg = gscr_to_read[i];
208                 unsigned int err_mask;
209
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;
215                 }
216         }
217
218         return 0;
219 }
220
221 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
222 {
223         u32 rev = gscr[SATA_PMP_GSCR_REV];
224
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>";
232 }
233
234 static int sata_pmp_configure(struct ata_device *dev, int print_info)
235 {
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;
241
242         nr_ports = sata_pmp_gscr_ports(gscr);
243
244         if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
245                 rc = -EINVAL;
246                 reason = "invalid nr_ports";
247                 goto fail;
248         }
249
250         if ((ap->flags & ATA_FLAG_AN) &&
251             (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
252                 dev->flags |= ATA_DFLAG_AN;
253
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;
261         }
262
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]);
272
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");
278         }
279
280         return 0;
281
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;
287 }
288
289 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
290 {
291         struct ata_link *pmp_link = ap->pmp_link;
292         int i;
293
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;
299
300                 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
301                         ata_link_init(ap, &pmp_link[i], i);
302
303                 ap->pmp_link = pmp_link;
304         }
305
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;
309
310                 link->flags = 0;
311                 ehc->i.probe_mask |= ATA_ALL_DEVICES;
312                 ehc->i.action |= ATA_EH_RESET;
313         }
314
315         return 0;
316 }
317
318 static void sata_pmp_quirks(struct ata_port *ap)
319 {
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;
324
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.
330                          */
331                         if (link->pmp < 5)
332                                 link->flags |= ATA_LFLAG_NO_SRST |
333                                                ATA_LFLAG_ASSUME_ATA;
334
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;
339                 }
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;
346
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;
351                 }
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.
359                          */
360                         if (link->pmp <= 5)
361                                 link->flags |= ATA_LFLAG_NO_SRST |
362                                                ATA_LFLAG_ASSUME_ATA;
363
364                         /* Port 6 is for SEMB device which doesn't
365                          * like SRST either.
366                          */
367                         if (link->pmp == 6)
368                                 link->flags |= ATA_LFLAG_NO_SRST |
369                                                ATA_LFLAG_ASSUME_SEMB;
370                 }
371         } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
372                                         devid == 0x5734 || devid == 0x5744)) {
373                 /* sil5723/5744 quirks */
374
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.
380                  */
381                 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
382         }
383 }
384
385 /**
386  *      sata_pmp_attach - attach a SATA PMP device
387  *      @dev: SATA PMP device to attach
388  *
389  *      Configure and attach SATA PMP device @dev.  This function is
390  *      also responsible for allocating and initializing PMP links.
391  *
392  *      LOCKING:
393  *      Kernel thread context (may sleep).
394  *
395  *      RETURNS:
396  *      0 on success, -errno on failure.
397  */
398 int sata_pmp_attach(struct ata_device *dev)
399 {
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;
405
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;
411         }
412
413         if (!ata_is_host_link(link)) {
414                 ata_dev_printk(dev, KERN_ERR,
415                                "Port Multipliers cannot be nested\n");
416                 return -EINVAL;
417         }
418
419         if (dev->devno) {
420                 ata_dev_printk(dev, KERN_ERR,
421                                "Port Multiplier must be the first device\n");
422                 return -EINVAL;
423         }
424
425         WARN_ON(link->pmp != 0);
426         link->pmp = SATA_PMP_CTRL_PORT;
427
428         /* read GSCR block */
429         rc = sata_pmp_read_gscr(dev, dev->gscr);
430         if (rc)
431                 goto fail;
432
433         /* config PMP */
434         rc = sata_pmp_configure(dev, 1);
435         if (rc)
436                 goto fail;
437
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;
443         }
444
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);
450
451         sata_pmp_quirks(ap);
452
453         if (ap->ops->pmp_attach)
454                 ap->ops->pmp_attach(ap);
455
456         ata_for_each_link(tlink, ap, EDGE)
457                 sata_link_init_spd(tlink);
458
459         ata_acpi_associate_sata_port(ap);
460
461         return 0;
462
463  fail:
464         link->pmp = 0;
465         return rc;
466 }
467
468 /**
469  *      sata_pmp_detach - detach a SATA PMP device
470  *      @dev: SATA PMP device to detach
471  *
472  *      Detach SATA PMP device @dev.  This function is also
473  *      responsible for deconfiguring PMP links.
474  *
475  *      LOCKING:
476  *      Kernel thread context (may sleep).
477  */
478 static void sata_pmp_detach(struct ata_device *dev)
479 {
480         struct ata_link *link = dev->link;
481         struct ata_port *ap = link->ap;
482         struct ata_link *tlink;
483         unsigned long flags;
484
485         ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
486
487         WARN_ON(!ata_is_host_link(link) || dev->devno ||
488                 link->pmp != SATA_PMP_CTRL_PORT);
489
490         if (ap->ops->pmp_detach)
491                 ap->ops->pmp_detach(ap);
492
493         ata_for_each_link(tlink, ap, EDGE)
494                 ata_eh_detach_dev(tlink->device);
495
496         spin_lock_irqsave(ap->lock, flags);
497         ap->nr_pmp_links = 0;
498         link->pmp = 0;
499         spin_unlock_irqrestore(ap->lock, flags);
500
501         ata_acpi_associate_sata_port(ap);
502 }
503
504 /**
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
508  *
509  *      Compare @new_gscr against @dev and determine whether @dev is
510  *      the PMP described by @new_gscr.
511  *
512  *      LOCKING:
513  *      None.
514  *
515  *      RETURNS:
516  *      1 if @dev matches @new_gscr, 0 otherwise.
517  */
518 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
519 {
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;
523
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);
530
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;
536         }
537
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;
543         }
544
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;
550         }
551
552         return 1;
553 }
554
555 /**
556  *      sata_pmp_revalidate - revalidate SATA PMP
557  *      @dev: PMP device to revalidate
558  *      @new_class: new class code
559  *
560  *      Re-read GSCR block and make sure @dev is still attached to the
561  *      port and properly configured.
562  *
563  *      LOCKING:
564  *      Kernel thread context (may sleep).
565  *
566  *      RETURNS:
567  *      0 on success, -errno otherwise.
568  */
569 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
570 {
571         struct ata_link *link = dev->link;
572         struct ata_port *ap = link->ap;
573         u32 *gscr = (void *)ap->sector_buf;
574         int rc;
575
576         DPRINTK("ENTER\n");
577
578         ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
579
580         if (!ata_dev_enabled(dev)) {
581                 rc = -ENODEV;
582                 goto fail;
583         }
584
585         /* wrong class? */
586         if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
587                 rc = -ENODEV;
588                 goto fail;
589         }
590
591         /* read GSCR */
592         rc = sata_pmp_read_gscr(dev, gscr);
593         if (rc)
594                 goto fail;
595
596         /* is the pmp still there? */
597         if (!sata_pmp_same_pmp(dev, gscr)) {
598                 rc = -ENODEV;
599                 goto fail;
600         }
601
602         memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
603
604         rc = sata_pmp_configure(dev, 0);
605         if (rc)
606                 goto fail;
607
608         ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
609
610         DPRINTK("EXIT, rc=0\n");
611         return 0;
612
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;
618 }
619
620 /**
621  *      sata_pmp_revalidate_quick - revalidate SATA PMP quickly
622  *      @dev: PMP device to revalidate
623  *
624  *      Make sure the attached PMP is accessible.
625  *
626  *      LOCKING:
627  *      Kernel thread context (may sleep).
628  *
629  *      RETURNS:
630  *      0 on success, -errno otherwise.
631  */
632 static int sata_pmp_revalidate_quick(struct ata_device *dev)
633 {
634         unsigned int err_mask;
635         u32 prod_id;
636
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;
642         }
643
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;
648         }
649
650         return 0;
651 }
652
653 /**
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)
660  *
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.
665  *
666  *      LOCKING:
667  *      Kernel thread context (may sleep).
668  *
669  *      RETURNS:
670  *      0 on success, -errno on failure.
671  */
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)
675 {
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;
682
683         DPRINTK("ENTER\n");
684
685         if (dev->flags & ATA_DFLAG_DETACH) {
686                 detach = 1;
687                 goto fail;
688         }
689
690  retry:
691         ehc->classes[0] = ATA_DEV_UNKNOWN;
692
693         if (ehc->i.action & ATA_EH_RESET) {
694                 struct ata_link *tlink;
695
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;
703                 }
704
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;
708
709                         ehc->i.probe_mask |= ATA_ALL_DEVICES;
710                         ehc->i.action |= ATA_EH_RESET;
711                 }
712         }
713
714         /* If revalidation is requested, revalidate and reconfigure;
715          * otherwise, do quick revalidation.
716          */
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);
721
722         if (rc) {
723                 tries--;
724
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);
730                 }
731
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;
738
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;
746                 }
747         }
748
749         /* okay, PMP resurrected */
750         ehc->i.flags = 0;
751
752         DPRINTK("EXIT, rc=0\n");
753         return 0;
754
755  fail:
756         sata_pmp_detach(dev);
757         if (detach)
758                 ata_eh_detach_dev(dev);
759         else
760                 ata_dev_disable(dev);
761
762         DPRINTK("EXIT, rc=%d\n", rc);
763         return rc;
764 }
765
766 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
767 {
768         struct ata_link *link;
769         unsigned long flags;
770         int rc;
771
772         spin_lock_irqsave(ap->lock, flags);
773
774         ata_for_each_link(link, ap, EDGE) {
775                 if (!(link->flags & ATA_LFLAG_DISABLED))
776                         continue;
777
778                 spin_unlock_irqrestore(ap->lock, flags);
779
780                 /* Some PMPs require hardreset sequence to get
781                  * SError.N working.
782                  */
783                 sata_link_hardreset(link, sata_deb_timing_normal,
784                                 ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK),
785                                 NULL, NULL);
786
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;
793                 }
794
795                 spin_lock_irqsave(ap->lock, flags);
796         }
797
798         spin_unlock_irqrestore(ap->lock, flags);
799
800         return 0;
801 }
802
803 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
804 {
805         struct ata_port *ap = link->ap;
806         unsigned long flags;
807
808         if (link_tries[link->pmp] && --link_tries[link->pmp])
809                 return 1;
810
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);
816
817                 spin_lock_irqsave(ap->lock, flags);
818                 link->flags |= ATA_LFLAG_DISABLED;
819                 spin_unlock_irqrestore(ap->lock, flags);
820         }
821
822         ata_dev_disable(link->device);
823         link->eh_context.i.action = 0;
824
825         return 0;
826 }
827
828 /**
829  *      sata_pmp_eh_recover - recover PMP-enabled port
830  *      @ap: ATA port to recover
831  *
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().
836  *
837  *      LOCKING:
838  *      Kernel thread context (may sleep).
839  *
840  *      RETURNS:
841  *      0 on success, -errno on failure.
842  */
843 static int sata_pmp_eh_recover(struct ata_port *ap)
844 {
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;
856
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;
860
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;
870                 }
871
872                 if (pmp_dev->class != ATA_DEV_PMP)
873                         return 0;
874
875                 /* new PMP online */
876                 ata_for_each_link(link, ap, EDGE)
877                         link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
878
879                 /* fall through */
880         }
881
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;
887
888         /* PHY event notification can disturb reset and other recovery
889          * operations.  Turn it off.
890          */
891         if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
892                 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
893
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;
901                 }
902         }
903
904         /* handle disabled links */
905         rc = sata_pmp_eh_handle_disabled_links(ap);
906         if (rc)
907                 goto pmp_fail;
908
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;
914
915         /* Connection status might have changed while resetting other
916          * links, check SATA_PMP_GSCR_ERROR before returning.
917          */
918
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);
923
924         /* enable notification */
925         if (pmp_dev->flags & ATA_DFLAG_AN) {
926                 gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
927
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;
935                 }
936         }
937
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;
945         }
946
947         cnt = 0;
948         ata_for_each_link(link, ap, EDGE) {
949                 if (!(gscr_error & (1 << link->pmp)))
950                         continue;
951
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");
961                 }
962         }
963
964         if (cnt) {
965                 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
966                                 "ports, repeating recovery\n");
967                 goto retry;
968         }
969
970         return 0;
971
972  link_fail:
973         if (sata_pmp_handle_link_fail(link, link_tries)) {
974                 pmp_ehc->i.action |= ATA_EH_RESET;
975                 goto retry;
976         }
977
978         /* fall through */
979  pmp_fail:
980         /* Control always ends up here after detaching PMP.  Shut up
981          * and return if we're unloading.
982          */
983         if (ap->pflags & ATA_PFLAG_UNLOADING)
984                 return rc;
985
986         if (!sata_pmp_attached(ap))
987                 goto retry;
988
989         if (--pmp_tries) {
990                 pmp_ehc->i.action |= ATA_EH_RESET;
991                 goto retry;
992         }
993
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);
999
1000         return rc;
1001 }
1002
1003 /**
1004  *      sata_pmp_error_handler - do standard error handling for PMP-enabled host
1005  *      @ap: host port to handle error for
1006  *
1007  *      Perform standard error handling sequence for PMP-enabled host
1008  *      @ap.
1009  *
1010  *      LOCKING:
1011  *      Kernel thread context (may sleep).
1012  */
1013 void sata_pmp_error_handler(struct ata_port *ap)
1014 {
1015         ata_eh_autopsy(ap);
1016         ata_eh_report(ap);
1017         sata_pmp_eh_recover(ap);
1018         ata_eh_finish(ap);
1019 }
1020
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);