f6c4b11336e99b6c2acc900e354175c32279497e
[safe/jmp/linux-2.6] / 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 "libata.h"
13
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
19  *
20  *      Wrapper around ap->ops->pmp_read to make it easier to call and
21  *      nomarlize error return value.
22  *
23  *      LOCKING:
24  *      Kernel thread context (may sleep).
25  *
26  *      RETURNS:
27  *      0 on success, -errno on failure.
28  */
29 static int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
30 {
31         struct ata_port *ap = link->ap;
32         struct ata_device *pmp_dev = ap->link.device;
33         int rc;
34
35         might_sleep();
36
37         rc = ap->ops->pmp_read(pmp_dev, link->pmp, reg, r_val);
38         if (rc)
39                 rc = -EIO;
40         return rc;
41 }
42
43 /**
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
48  *
49  *      Wrapper around ap->ops->pmp_write to make it easier to call
50  *      and nomarlize error return value.
51  *
52  *      LOCKING:
53  *      Kernel thread context (may sleep).
54  *
55  *      RETURNS:
56  *      0 on success, -errno on failure.
57  */
58 static int sata_pmp_write(struct ata_link *link, int reg, u32 val)
59 {
60         struct ata_port *ap = link->ap;
61         struct ata_device *pmp_dev = ap->link.device;
62         int rc;
63
64         might_sleep();
65
66         rc = ap->ops->pmp_write(pmp_dev, link->pmp, reg, val);
67         if (rc)
68                 rc = -EIO;
69         return rc;
70 }
71
72 /**
73  *      sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
74  *      @qc: ATA command in question
75  *
76  *      A host which has command switching PMP support cannot issue
77  *      commands to multiple links simultaneously.
78  *
79  *      LOCKING:
80  *      spin_lock_irqsave(host lock)
81  *
82  *      RETURNS:
83  *      ATA_DEFER_* if deferring is needed, 0 otherwise.
84  */
85 int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
86 {
87         struct ata_link *link = qc->dev->link;
88         struct ata_port *ap = link->ap;
89
90         if (ap->excl_link == NULL || ap->excl_link == link) {
91                 if (ap->nr_active_links == 0 || ata_link_active(link)) {
92                         qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
93                         return ata_std_qc_defer(qc);
94                 }
95
96                 ap->excl_link = link;
97         }
98
99         return ATA_DEFER_PORT;
100 }
101
102 /**
103  *      sata_pmp_read_init_tf - initialize TF for PMP read
104  *      @tf: taskfile to initialize
105  *      @dev: PMP dev
106  *      @pmp: port multiplier port number
107  *      @reg: register to read
108  *
109  *      Initialize @tf for PMP read command.
110  *
111  *      LOCKING:
112  *      None.
113  */
114 void sata_pmp_read_init_tf(struct ata_taskfile *tf,
115                            struct ata_device *dev, int pmp, int reg)
116 {
117         ata_tf_init(dev, tf);
118         tf->command = ATA_CMD_PMP_READ;
119         tf->protocol = ATA_PROT_NODATA;
120         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
121         tf->feature = reg;
122         tf->device = pmp;
123 }
124
125 /**
126  *      sata_pmp_read_val - extract PMP read result from TF
127  *      @tf: target TF
128  *
129  *      Determine PMP read result from @tf.
130  *
131  *      LOCKING:
132  *      None.
133  */
134 u32 sata_pmp_read_val(const struct ata_taskfile *tf)
135 {
136         return tf->nsect | tf->lbal << 8 | tf->lbam << 16 | tf->lbah << 24;
137 }
138
139 /**
140  *      sata_pmp_read_init_tf - initialize TF for PMP write
141  *      @tf: taskfile to initialize
142  *      @dev: PMP dev
143  *      @pmp: port multiplier port number
144  *      @reg: register to read
145  *      @val: value to write
146  *
147  *      Initialize @tf for PMP write command.
148  *
149  *      LOCKING:
150  *      None.
151  */
152 void sata_pmp_write_init_tf(struct ata_taskfile *tf,
153                             struct ata_device *dev, int pmp, int reg, u32 val)
154 {
155         ata_tf_init(dev, tf);
156         tf->command = ATA_CMD_PMP_WRITE;
157         tf->protocol = ATA_PROT_NODATA;
158         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
159         tf->feature = reg;
160         tf->device = pmp;
161         tf->nsect = val & 0xff;
162         tf->lbal = (val >> 8) & 0xff;
163         tf->lbam = (val >> 16) & 0xff;
164         tf->lbah = (val >> 24) & 0xff;
165 }
166
167 /**
168  *      sata_pmp_scr_read - read PSCR
169  *      @link: ATA link to read PSCR for
170  *      @reg: PSCR to read
171  *      @r_val: resulting value
172  *
173  *      Read PSCR @reg into @r_val for @link, to be called from
174  *      ata_scr_read().
175  *
176  *      LOCKING:
177  *      Kernel thread context (may sleep).
178  *
179  *      RETURNS:
180  *      0 on success, -errno on failure.
181  */
182 int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
183 {
184         if (reg > SATA_PMP_PSCR_CONTROL)
185                 return -EINVAL;
186
187         return sata_pmp_read(link, reg, r_val);
188 }
189
190 /**
191  *      sata_pmp_scr_write - write PSCR
192  *      @link: ATA link to write PSCR for
193  *      @reg: PSCR to write
194  *      @val: value to be written
195  *
196  *      Write @val to PSCR @reg for @link, to be called from
197  *      ata_scr_write() and ata_scr_write_flush().
198  *
199  *      LOCKING:
200  *      Kernel thread context (may sleep).
201  *
202  *      RETURNS:
203  *      0 on success, -errno on failure.
204  */
205 int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
206 {
207         if (reg > SATA_PMP_PSCR_CONTROL)
208                 return -EINVAL;
209
210         return sata_pmp_write(link, reg, val);
211 }
212
213 /**
214  *      sata_pmp_std_prereset - prepare PMP link for reset
215  *      @link: link to be reset
216  *      @deadline: deadline jiffies for the operation
217  *
218  *      @link is about to be reset.  Initialize it.
219  *
220  *      LOCKING:
221  *      Kernel thread context (may sleep)
222  *
223  *      RETURNS:
224  *      0 on success, -errno otherwise.
225  */
226 int sata_pmp_std_prereset(struct ata_link *link, unsigned long deadline)
227 {
228         struct ata_eh_context *ehc = &link->eh_context;
229         const unsigned long *timing = sata_ehc_deb_timing(ehc);
230         int rc;
231
232         /* force HRST? */
233         if (link->flags & ATA_LFLAG_NO_SRST)
234                 ehc->i.action |= ATA_EH_HARDRESET;
235
236         /* handle link resume */
237         if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
238             (link->flags & ATA_LFLAG_HRST_TO_RESUME))
239                 ehc->i.action |= ATA_EH_HARDRESET;
240
241         /* if we're about to do hardreset, nothing more to do */
242         if (ehc->i.action & ATA_EH_HARDRESET)
243                 return 0;
244
245         /* resume link */
246         rc = sata_link_resume(link, timing, deadline);
247         if (rc) {
248                 /* phy resume failed */
249                 ata_link_printk(link, KERN_WARNING, "failed to resume link "
250                                 "for reset (errno=%d)\n", rc);
251                 return rc;
252         }
253
254         /* clear SError bits including .X which blocks the port when set */
255         rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
256         if (rc) {
257                 ata_link_printk(link, KERN_ERR,
258                                 "failed to clear SError (errno=%d)\n", rc);
259                 return rc;
260         }
261
262         return 0;
263 }
264
265 /**
266  *      sata_pmp_std_hardreset - standard hardreset method for PMP link
267  *      @link: link to be reset
268  *      @class: resulting class of attached device
269  *      @deadline: deadline jiffies for the operation
270  *
271  *      Hardreset PMP port @link.  Note that this function doesn't
272  *      wait for BSY clearance.  There simply isn't a generic way to
273  *      wait the event.  Instead, this function return -EAGAIN thus
274  *      telling libata-EH to followup with softreset.
275  *
276  *      LOCKING:
277  *      Kernel thread context (may sleep)
278  *
279  *      RETURNS:
280  *      0 on success, -errno otherwise.
281  */
282 int sata_pmp_std_hardreset(struct ata_link *link, unsigned int *class,
283                            unsigned long deadline)
284 {
285         const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
286         u32 tmp;
287         int rc;
288
289         DPRINTK("ENTER\n");
290
291         /* do hardreset */
292         rc = sata_link_hardreset(link, timing, deadline);
293         if (rc) {
294                 ata_link_printk(link, KERN_ERR,
295                                 "COMRESET failed (errno=%d)\n", rc);
296                 goto out;
297         }
298
299         /* clear SError bits including .X which blocks the port when set */
300         rc = sata_scr_write(link, SCR_ERROR, 0xffffffff);
301         if (rc) {
302                 ata_link_printk(link, KERN_ERR, "failed to clear SError "
303                                 "during hardreset (errno=%d)\n", rc);
304                 goto out;
305         }
306
307         /* if device is present, follow up with srst to wait for !BSY */
308         if (ata_link_online(link))
309                 rc = -EAGAIN;
310  out:
311         /* if SCR isn't accessible, we need to reset the PMP */
312         if (rc && rc != -EAGAIN && sata_scr_read(link, SCR_STATUS, &tmp))
313                 rc = -ERESTART;
314
315         DPRINTK("EXIT, rc=%d\n", rc);
316         return rc;
317 }
318
319 /**
320  *      ata_std_postreset - standard postreset method for PMP link
321  *      @link: the target ata_link
322  *      @classes: classes of attached devices
323  *
324  *      This function is invoked after a successful reset.  Note that
325  *      the device might have been reset more than once using
326  *      different reset methods before postreset is invoked.
327  *
328  *      LOCKING:
329  *      Kernel thread context (may sleep)
330  */
331 void sata_pmp_std_postreset(struct ata_link *link, unsigned int *class)
332 {
333         u32 serror;
334
335         DPRINTK("ENTER\n");
336
337         /* clear SError */
338         if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
339                 sata_scr_write(link, SCR_ERROR, serror);
340
341         /* print link status */
342         sata_print_link_status(link);
343
344         DPRINTK("EXIT\n");
345 }
346
347 /**
348  *      sata_pmp_read_gscr - read GSCR block of SATA PMP
349  *      @dev: PMP device
350  *      @gscr: buffer to read GSCR block into
351  *
352  *      Read selected PMP GSCRs from the PMP at @dev.  This will serve
353  *      as configuration and identification info for the PMP.
354  *
355  *      LOCKING:
356  *      Kernel thread context (may sleep).
357  *
358  *      RETURNS:
359  *      0 on success, -errno on failure.
360  */
361 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
362 {
363         static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
364         int i, rc;
365
366         for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
367                 int reg = gscr_to_read[i];
368
369                 rc = sata_pmp_read(dev->link, reg, &gscr[reg]);
370                 if (rc) {
371                         ata_dev_printk(dev, KERN_ERR, "failed to read "
372                                        "PMP GSCR[%d] (errno=%d)\n", reg, rc);
373                         return rc;
374                 }
375         }
376
377         return 0;
378 }
379
380 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
381 {
382         u32 rev = gscr[SATA_PMP_GSCR_REV];
383
384         if (rev & (1 << 2))
385                 return "1.1";
386         if (rev & (1 << 1))
387                 return "1.0";
388         return "<unknown>";
389 }
390
391 static int sata_pmp_configure(struct ata_device *dev, int print_info)
392 {
393         struct ata_port *ap = dev->link->ap;
394         u32 *gscr = dev->gscr;
395         const char *reason;
396         int nr_ports, rc;
397
398         nr_ports = sata_pmp_gscr_ports(gscr);
399
400         if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
401                 rc = -EINVAL;
402                 reason = "invalid nr_ports";
403                 goto fail;
404         }
405
406         if ((ap->flags & ATA_FLAG_AN) &&
407             (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
408                 dev->flags |= ATA_DFLAG_AN;
409
410         /* monitor SERR_PHYRDY_CHG on fan-out ports */
411         rc = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN, SERR_PHYRDY_CHG);
412         if (rc) {
413                 reason = "failed to write GSCR_ERROR_EN";
414                 goto fail;
415         }
416
417         /* turn off notification till fan-out ports are reset and configured */
418         if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
419                 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
420
421                 rc = sata_pmp_write(dev->link, SATA_PMP_GSCR_FEAT_EN,
422                                     gscr[SATA_PMP_GSCR_FEAT_EN]);
423                 if (rc) {
424                         reason = "failed to write GSCR_FEAT_EN";
425                         goto fail;
426                 }
427         }
428
429         if (print_info) {
430                 ata_dev_printk(dev, KERN_INFO, "Port Multiplier %s, "
431                                "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
432                                sata_pmp_spec_rev_str(gscr),
433                                sata_pmp_gscr_vendor(gscr),
434                                sata_pmp_gscr_devid(gscr),
435                                sata_pmp_gscr_rev(gscr),
436                                nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
437                                gscr[SATA_PMP_GSCR_FEAT]);
438
439                 if (!(dev->flags & ATA_DFLAG_AN))
440                         ata_dev_printk(dev, KERN_INFO,
441                                 "Asynchronous notification not supported, "
442                                 "hotplug won't\n         work on fan-out "
443                                 "ports. Use warm-plug instead.\n");
444         }
445
446         return 0;
447
448  fail:
449         ata_dev_printk(dev, KERN_ERR,
450                        "failed to configure Port Multiplier (%s)\n", reason);
451         return rc;
452 }
453
454 static int sata_pmp_init_links(struct ata_port *ap, int nr_ports)
455 {
456         struct ata_link *pmp_link = ap->pmp_link;
457         int i;
458
459         if (!pmp_link) {
460                 pmp_link = kzalloc(sizeof(pmp_link[0]) * SATA_PMP_MAX_PORTS,
461                                    GFP_NOIO);
462                 if (!pmp_link)
463                         return -ENOMEM;
464
465                 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
466                         ata_link_init(ap, &pmp_link[i], i);
467
468                 ap->pmp_link = pmp_link;
469         }
470
471         for (i = 0; i < nr_ports; i++) {
472                 struct ata_link *link = &pmp_link[i];
473                 struct ata_eh_context *ehc = &link->eh_context;
474
475                 link->flags = 0;
476                 ehc->i.probe_mask |= 1;
477                 ehc->i.action |= ATA_EH_SOFTRESET;
478                 ehc->i.flags |= ATA_EHI_RESUME_LINK;
479         }
480
481         return 0;
482 }
483
484 static void sata_pmp_quirks(struct ata_port *ap)
485 {
486         u32 *gscr = ap->link.device->gscr;
487         u16 vendor = sata_pmp_gscr_vendor(gscr);
488         u16 devid = sata_pmp_gscr_devid(gscr);
489         struct ata_link *link;
490
491         if (vendor == 0x1095 && devid == 0x3726) {
492                 /* sil3726 quirks */
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;
496
497                         /* class code report is unreliable */
498                         if (link->pmp < 5)
499                                 link->flags |= ATA_LFLAG_ASSUME_ATA;
500
501                         /* port 5 is for SEMB device and it doesn't like SRST */
502                         if (link->pmp == 5)
503                                 link->flags |= ATA_LFLAG_NO_SRST |
504                                                ATA_LFLAG_ASSUME_SEMB;
505                 }
506         } else if (vendor == 0x1095 && devid == 0x4723) {
507                 /* sil4723 quirks */
508                 ata_port_for_each_link(link, ap) {
509                         /* SError.N need a kick in the ass to get working */
510                         link->flags |= ATA_LFLAG_HRST_TO_RESUME;
511
512                         /* class code report is unreliable */
513                         if (link->pmp < 2)
514                                 link->flags |= ATA_LFLAG_ASSUME_ATA;
515
516                         /* the config device at port 2 locks up on SRST */
517                         if (link->pmp == 2)
518                                 link->flags |= ATA_LFLAG_NO_SRST |
519                                                ATA_LFLAG_ASSUME_ATA;
520                 }
521         } else if (vendor == 0x1095 && devid == 0x4726) {
522                 /* sil4726 quirks */
523                 ata_port_for_each_link(link, ap) {
524                         /* SError.N need a kick in the ass to get working */
525                         link->flags |= ATA_LFLAG_HRST_TO_RESUME;
526
527                         /* class code report is unreliable */
528                         if (link->pmp < 5)
529                                 link->flags |= ATA_LFLAG_ASSUME_ATA;
530
531                         /* The config device, which can be either at
532                          * port 0 or 5, locks up on SRST.
533                          */
534                         if (link->pmp == 0 || link->pmp == 5)
535                                 link->flags |= ATA_LFLAG_NO_SRST |
536                                                ATA_LFLAG_ASSUME_ATA;
537
538                         /* Port 6 is for SEMB device which doesn't
539                          * like SRST either.
540                          */
541                         if (link->pmp == 6)
542                                 link->flags |= ATA_LFLAG_NO_SRST |
543                                                ATA_LFLAG_ASSUME_SEMB;
544                 }
545         } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
546                                         devid == 0x5734 || devid == 0x5744)) {
547                 /* sil5723/5744 quirks */
548
549                 /* sil5723/5744 has either two or three downstream
550                  * ports depending on operation mode.  The last port
551                  * is empty if any actual IO device is available or
552                  * occupied by a pseudo configuration device
553                  * otherwise.  Don't try hard to recover it.
554                  */
555                 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
556         } else if (vendor == 0x11ab && devid == 0x4140) {
557                 /* Marvell 88SM4140 quirks.  Fan-out ports require PHY
558                  * reset to work; other than that, it behaves very
559                  * nicely.
560                  */
561                 ata_port_for_each_link(link, ap)
562                         link->flags |= ATA_LFLAG_HRST_TO_RESUME;
563         }
564 }
565
566 /**
567  *      sata_pmp_attach - attach a SATA PMP device
568  *      @dev: SATA PMP device to attach
569  *
570  *      Configure and attach SATA PMP device @dev.  This function is
571  *      also responsible for allocating and initializing PMP links.
572  *
573  *      LOCKING:
574  *      Kernel thread context (may sleep).
575  *
576  *      RETURNS:
577  *      0 on success, -errno on failure.
578  */
579 int sata_pmp_attach(struct ata_device *dev)
580 {
581         struct ata_link *link = dev->link;
582         struct ata_port *ap = link->ap;
583         unsigned long flags;
584         struct ata_link *tlink;
585         int rc;
586
587         /* is it hanging off the right place? */
588         if (!(ap->flags & ATA_FLAG_PMP)) {
589                 ata_dev_printk(dev, KERN_ERR,
590                                "host does not support Port Multiplier\n");
591                 return -EINVAL;
592         }
593
594         if (!ata_is_host_link(link)) {
595                 ata_dev_printk(dev, KERN_ERR,
596                                "Port Multipliers cannot be nested\n");
597                 return -EINVAL;
598         }
599
600         if (dev->devno) {
601                 ata_dev_printk(dev, KERN_ERR,
602                                "Port Multiplier must be the first device\n");
603                 return -EINVAL;
604         }
605
606         WARN_ON(link->pmp != 0);
607         link->pmp = SATA_PMP_CTRL_PORT;
608
609         /* read GSCR block */
610         rc = sata_pmp_read_gscr(dev, dev->gscr);
611         if (rc)
612                 goto fail;
613
614         /* config PMP */
615         rc = sata_pmp_configure(dev, 1);
616         if (rc)
617                 goto fail;
618
619         rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
620         if (rc) {
621                 ata_dev_printk(dev, KERN_INFO,
622                                "failed to initialize PMP links\n");
623                 goto fail;
624         }
625
626         /* attach it */
627         spin_lock_irqsave(ap->lock, flags);
628         WARN_ON(ap->nr_pmp_links);
629         ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
630         spin_unlock_irqrestore(ap->lock, flags);
631
632         sata_pmp_quirks(ap);
633
634         if (ap->ops->pmp_attach)
635                 ap->ops->pmp_attach(ap);
636
637         ata_port_for_each_link(tlink, ap)
638                 sata_link_init_spd(tlink);
639
640         ata_acpi_associate_sata_port(ap);
641
642         return 0;
643
644  fail:
645         link->pmp = 0;
646         return rc;
647 }
648
649 /**
650  *      sata_pmp_detach - detach a SATA PMP device
651  *      @dev: SATA PMP device to detach
652  *
653  *      Detach SATA PMP device @dev.  This function is also
654  *      responsible for deconfiguring PMP links.
655  *
656  *      LOCKING:
657  *      Kernel thread context (may sleep).
658  */
659 static void sata_pmp_detach(struct ata_device *dev)
660 {
661         struct ata_link *link = dev->link;
662         struct ata_port *ap = link->ap;
663         struct ata_link *tlink;
664         unsigned long flags;
665
666         ata_dev_printk(dev, KERN_INFO, "Port Multiplier detaching\n");
667
668         WARN_ON(!ata_is_host_link(link) || dev->devno ||
669                 link->pmp != SATA_PMP_CTRL_PORT);
670
671         if (ap->ops->pmp_detach)
672                 ap->ops->pmp_detach(ap);
673
674         ata_port_for_each_link(tlink, ap)
675                 ata_eh_detach_dev(tlink->device);
676
677         spin_lock_irqsave(ap->lock, flags);
678         ap->nr_pmp_links = 0;
679         link->pmp = 0;
680         spin_unlock_irqrestore(ap->lock, flags);
681
682         ata_acpi_associate_sata_port(ap);
683 }
684
685 /**
686  *      sata_pmp_same_pmp - does new GSCR matches the configured PMP?
687  *      @dev: PMP device to compare against
688  *      @new_gscr: GSCR block of the new device
689  *
690  *      Compare @new_gscr against @dev and determine whether @dev is
691  *      the PMP described by @new_gscr.
692  *
693  *      LOCKING:
694  *      None.
695  *
696  *      RETURNS:
697  *      1 if @dev matches @new_gscr, 0 otherwise.
698  */
699 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
700 {
701         const u32 *old_gscr = dev->gscr;
702         u16 old_vendor, new_vendor, old_devid, new_devid;
703         int old_nr_ports, new_nr_ports;
704
705         old_vendor = sata_pmp_gscr_vendor(old_gscr);
706         new_vendor = sata_pmp_gscr_vendor(new_gscr);
707         old_devid = sata_pmp_gscr_devid(old_gscr);
708         new_devid = sata_pmp_gscr_devid(new_gscr);
709         old_nr_ports = sata_pmp_gscr_ports(old_gscr);
710         new_nr_ports = sata_pmp_gscr_ports(new_gscr);
711
712         if (old_vendor != new_vendor) {
713                 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
714                                "vendor mismatch '0x%x' != '0x%x'\n",
715                                old_vendor, new_vendor);
716                 return 0;
717         }
718
719         if (old_devid != new_devid) {
720                 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
721                                "device ID mismatch '0x%x' != '0x%x'\n",
722                                old_devid, new_devid);
723                 return 0;
724         }
725
726         if (old_nr_ports != new_nr_ports) {
727                 ata_dev_printk(dev, KERN_INFO, "Port Multiplier "
728                                "nr_ports mismatch '0x%x' != '0x%x'\n",
729                                old_nr_ports, new_nr_ports);
730                 return 0;
731         }
732
733         return 1;
734 }
735
736 /**
737  *      sata_pmp_revalidate - revalidate SATA PMP
738  *      @dev: PMP device to revalidate
739  *      @new_class: new class code
740  *
741  *      Re-read GSCR block and make sure @dev is still attached to the
742  *      port and properly configured.
743  *
744  *      LOCKING:
745  *      Kernel thread context (may sleep).
746  *
747  *      RETURNS:
748  *      0 on success, -errno otherwise.
749  */
750 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
751 {
752         struct ata_link *link = dev->link;
753         struct ata_port *ap = link->ap;
754         u32 *gscr = (void *)ap->sector_buf;
755         int rc;
756
757         DPRINTK("ENTER\n");
758
759         ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
760
761         if (!ata_dev_enabled(dev)) {
762                 rc = -ENODEV;
763                 goto fail;
764         }
765
766         /* wrong class? */
767         if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
768                 rc = -ENODEV;
769                 goto fail;
770         }
771
772         /* read GSCR */
773         rc = sata_pmp_read_gscr(dev, gscr);
774         if (rc)
775                 goto fail;
776
777         /* is the pmp still there? */
778         if (!sata_pmp_same_pmp(dev, gscr)) {
779                 rc = -ENODEV;
780                 goto fail;
781         }
782
783         memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
784
785         rc = sata_pmp_configure(dev, 0);
786         if (rc)
787                 goto fail;
788
789         ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
790
791         DPRINTK("EXIT, rc=0\n");
792         return 0;
793
794  fail:
795         ata_dev_printk(dev, KERN_ERR,
796                        "PMP revalidation failed (errno=%d)\n", rc);
797         DPRINTK("EXIT, rc=%d\n", rc);
798         return rc;
799 }
800
801 /**
802  *      sata_pmp_revalidate_quick - revalidate SATA PMP quickly
803  *      @dev: PMP device to revalidate
804  *
805  *      Make sure the attached PMP is accessible.
806  *
807  *      LOCKING:
808  *      Kernel thread context (may sleep).
809  *
810  *      RETURNS:
811  *      0 on success, -errno otherwise.
812  */
813 static int sata_pmp_revalidate_quick(struct ata_device *dev)
814 {
815         u32 prod_id;
816         int rc;
817
818         rc = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
819         if (rc) {
820                 ata_dev_printk(dev, KERN_ERR, "failed to read PMP product ID\n");
821                 return rc;
822         }
823
824         if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
825                 ata_dev_printk(dev, KERN_ERR, "PMP product ID mismatch\n");
826                 /* something weird is going on, request full PMP recovery */
827                 return -EIO;
828         }
829
830         return 0;
831 }
832
833 /**
834  *      sata_pmp_eh_recover_pmp - recover PMP
835  *      @ap: ATA port PMP is attached to
836  *      @prereset: prereset method (can be NULL)
837  *      @softreset: softreset method
838  *      @hardreset: hardreset method
839  *      @postreset: postreset method (can be NULL)
840  *
841  *      Recover PMP attached to @ap.  Recovery procedure is somewhat
842  *      similar to that of ata_eh_recover() except that reset should
843  *      always be performed in hard->soft sequence and recovery
844  *      failure results in PMP detachment.
845  *
846  *      LOCKING:
847  *      Kernel thread context (may sleep).
848  *
849  *      RETURNS:
850  *      0 on success, -errno on failure.
851  */
852 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
853                 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
854                 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
855 {
856         struct ata_link *link = &ap->link;
857         struct ata_eh_context *ehc = &link->eh_context;
858         struct ata_device *dev = link->device;
859         int tries = ATA_EH_PMP_TRIES;
860         int detach = 0, rc = 0;
861         int reval_failed = 0;
862
863         DPRINTK("ENTER\n");
864
865         if (dev->flags & ATA_DFLAG_DETACH) {
866                 detach = 1;
867                 goto fail;
868         }
869
870  retry:
871         ehc->classes[0] = ATA_DEV_UNKNOWN;
872
873         if (ehc->i.action & ATA_EH_RESET_MASK) {
874                 struct ata_link *tlink;
875
876                 ata_eh_freeze_port(ap);
877
878                 /* reset */
879                 ehc->i.action = ATA_EH_HARDRESET;
880                 rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
881                                   postreset);
882                 if (rc) {
883                         ata_link_printk(link, KERN_ERR,
884                                         "failed to reset PMP, giving up\n");
885                         goto fail;
886                 }
887
888                 ata_eh_thaw_port(ap);
889
890                 /* PMP is reset, SErrors cannot be trusted, scan all */
891                 ata_port_for_each_link(tlink, ap)
892                         ata_ehi_schedule_probe(&tlink->eh_context.i);
893         }
894
895         /* If revalidation is requested, revalidate and reconfigure;
896          * otherwise, do quick revalidation.
897          */
898         if (ehc->i.action & ATA_EH_REVALIDATE)
899                 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
900         else
901                 rc = sata_pmp_revalidate_quick(dev);
902
903         if (rc) {
904                 tries--;
905
906                 if (rc == -ENODEV) {
907                         ehc->i.probe_mask |= 1;
908                         detach = 1;
909                         /* give it just two more chances */
910                         tries = min(tries, 2);
911                 }
912
913                 if (tries) {
914                         int sleep = ehc->i.flags & ATA_EHI_DID_RESET;
915
916                         /* consecutive revalidation failures? speed down */
917                         if (reval_failed)
918                                 sata_down_spd_limit(link);
919                         else
920                                 reval_failed = 1;
921
922                         ata_dev_printk(dev, KERN_WARNING,
923                                        "retrying hardreset%s\n",
924                                        sleep ? " in 5 secs" : "");
925                         if (sleep)
926                                 ssleep(5);
927                         ehc->i.action |= ATA_EH_HARDRESET;
928                         goto retry;
929                 } else {
930                         ata_dev_printk(dev, KERN_ERR, "failed to recover PMP "
931                                        "after %d tries, giving up\n",
932                                        ATA_EH_PMP_TRIES);
933                         goto fail;
934                 }
935         }
936
937         /* okay, PMP resurrected */
938         ehc->i.flags = 0;
939
940         DPRINTK("EXIT, rc=0\n");
941         return 0;
942
943  fail:
944         sata_pmp_detach(dev);
945         if (detach)
946                 ata_eh_detach_dev(dev);
947         else
948                 ata_dev_disable(dev);
949
950         DPRINTK("EXIT, rc=%d\n", rc);
951         return rc;
952 }
953
954 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
955 {
956         struct ata_link *link;
957         unsigned long flags;
958         int rc;
959
960         spin_lock_irqsave(ap->lock, flags);
961
962         ata_port_for_each_link(link, ap) {
963                 if (!(link->flags & ATA_LFLAG_DISABLED))
964                         continue;
965
966                 spin_unlock_irqrestore(ap->lock, flags);
967
968                 /* Some PMPs require hardreset sequence to get
969                  * SError.N working.
970                  */
971                 if ((link->flags & ATA_LFLAG_HRST_TO_RESUME) &&
972                     (link->eh_context.i.flags & ATA_EHI_RESUME_LINK))
973                         sata_link_hardreset(link, sata_deb_timing_normal,
974                                             jiffies + ATA_TMOUT_INTERNAL_QUICK);
975
976                 /* unconditionally clear SError.N */
977                 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
978                 if (rc) {
979                         ata_link_printk(link, KERN_ERR, "failed to clear "
980                                         "SError.N (errno=%d)\n", rc);
981                         return rc;
982                 }
983
984                 spin_lock_irqsave(ap->lock, flags);
985         }
986
987         spin_unlock_irqrestore(ap->lock, flags);
988
989         return 0;
990 }
991
992 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
993 {
994         struct ata_port *ap = link->ap;
995         unsigned long flags;
996
997         if (link_tries[link->pmp] && --link_tries[link->pmp])
998                 return 1;
999
1000         /* disable this link */
1001         if (!(link->flags & ATA_LFLAG_DISABLED)) {
1002                 ata_link_printk(link, KERN_WARNING,
1003                         "failed to recover link after %d tries, disabling\n",
1004                         ATA_EH_PMP_LINK_TRIES);
1005
1006                 spin_lock_irqsave(ap->lock, flags);
1007                 link->flags |= ATA_LFLAG_DISABLED;
1008                 spin_unlock_irqrestore(ap->lock, flags);
1009         }
1010
1011         ata_dev_disable(link->device);
1012         link->eh_context.i.action = 0;
1013
1014         return 0;
1015 }
1016
1017 /**
1018  *      sata_pmp_eh_recover - recover PMP-enabled port
1019  *      @ap: ATA port to recover
1020  *      @prereset: prereset method (can be NULL)
1021  *      @softreset: softreset method
1022  *      @hardreset: hardreset method
1023  *      @postreset: postreset method (can be NULL)
1024  *      @pmp_prereset: PMP prereset method (can be NULL)
1025  *      @pmp_softreset: PMP softreset method (can be NULL)
1026  *      @pmp_hardreset: PMP hardreset method (can be NULL)
1027  *      @pmp_postreset: PMP postreset method (can be NULL)
1028  *
1029  *      Drive EH recovery operation for PMP enabled port @ap.  This
1030  *      function recovers host and PMP ports with proper retrials and
1031  *      fallbacks.  Actual recovery operations are performed using
1032  *      ata_eh_recover() and sata_pmp_eh_recover_pmp().
1033  *
1034  *      LOCKING:
1035  *      Kernel thread context (may sleep).
1036  *
1037  *      RETURNS:
1038  *      0 on success, -errno on failure.
1039  */
1040 static int sata_pmp_eh_recover(struct ata_port *ap,
1041                 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1042                 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1043                 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1044                 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1045 {
1046         int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
1047         struct ata_link *pmp_link = &ap->link;
1048         struct ata_device *pmp_dev = pmp_link->device;
1049         struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
1050         struct ata_link *link;
1051         struct ata_device *dev;
1052         u32 gscr_error, sntf;
1053         int cnt, rc;
1054
1055         pmp_tries = ATA_EH_PMP_TRIES;
1056         ata_port_for_each_link(link, ap)
1057                 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1058
1059  retry:
1060         /* PMP attached? */
1061         if (!ap->nr_pmp_links) {
1062                 rc = ata_eh_recover(ap, prereset, softreset, hardreset,
1063                                     postreset, NULL);
1064                 if (rc) {
1065                         ata_link_for_each_dev(dev, &ap->link)
1066                                 ata_dev_disable(dev);
1067                         return rc;
1068                 }
1069
1070                 if (pmp_dev->class != ATA_DEV_PMP)
1071                         return 0;
1072
1073                 /* new PMP online */
1074                 ata_port_for_each_link(link, ap)
1075                         link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
1076
1077                 /* fall through */
1078         }
1079
1080         /* recover pmp */
1081         rc = sata_pmp_eh_recover_pmp(ap, prereset, softreset, hardreset,
1082                                      postreset);
1083         if (rc)
1084                 goto pmp_fail;
1085
1086         /* handle disabled links */
1087         rc = sata_pmp_eh_handle_disabled_links(ap);
1088         if (rc)
1089                 goto pmp_fail;
1090
1091         /* recover links */
1092         rc = ata_eh_recover(ap, pmp_prereset, pmp_softreset, pmp_hardreset,
1093                             pmp_postreset, &link);
1094         if (rc)
1095                 goto link_fail;
1096
1097         /* Connection status might have changed while resetting other
1098          * links, check SATA_PMP_GSCR_ERROR before returning.
1099          */
1100
1101         /* clear SNotification */
1102         rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1103         if (rc == 0)
1104                 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1105
1106         /* enable notification */
1107         if (pmp_dev->flags & ATA_DFLAG_AN) {
1108                 pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
1109
1110                 rc = sata_pmp_write(pmp_dev->link, SATA_PMP_GSCR_FEAT_EN,
1111                                     pmp_dev->gscr[SATA_PMP_GSCR_FEAT_EN]);
1112                 if (rc) {
1113                         ata_dev_printk(pmp_dev, KERN_ERR,
1114                                        "failed to write PMP_FEAT_EN\n");
1115                         goto pmp_fail;
1116                 }
1117         }
1118
1119         /* check GSCR_ERROR */
1120         rc = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
1121         if (rc) {
1122                 ata_dev_printk(pmp_dev, KERN_ERR,
1123                                "failed to read PMP_GSCR_ERROR\n");
1124                 goto pmp_fail;
1125         }
1126
1127         cnt = 0;
1128         ata_port_for_each_link(link, ap) {
1129                 if (!(gscr_error & (1 << link->pmp)))
1130                         continue;
1131
1132                 if (sata_pmp_handle_link_fail(link, link_tries)) {
1133                         ata_ehi_hotplugged(&link->eh_context.i);
1134                         cnt++;
1135                 } else {
1136                         ata_link_printk(link, KERN_WARNING,
1137                                 "PHY status changed but maxed out on retries, "
1138                                 "giving up\n");
1139                         ata_link_printk(link, KERN_WARNING,
1140                                 "Manully issue scan to resume this link\n");
1141                 }
1142         }
1143
1144         if (cnt) {
1145                 ata_port_printk(ap, KERN_INFO, "PMP SError.N set for some "
1146                                 "ports, repeating recovery\n");
1147                 goto retry;
1148         }
1149
1150         return 0;
1151
1152  link_fail:
1153         if (sata_pmp_handle_link_fail(link, link_tries)) {
1154                 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1155                 goto retry;
1156         }
1157
1158         /* fall through */
1159  pmp_fail:
1160         /* Control always ends up here after detaching PMP.  Shut up
1161          * and return if we're unloading.
1162          */
1163         if (ap->pflags & ATA_PFLAG_UNLOADING)
1164                 return rc;
1165
1166         if (!ap->nr_pmp_links)
1167                 goto retry;
1168
1169         if (--pmp_tries) {
1170                 ata_port_printk(ap, KERN_WARNING,
1171                                 "failed to recover PMP, retrying in 5 secs\n");
1172                 pmp_ehc->i.action |= ATA_EH_HARDRESET;
1173                 ssleep(5);
1174                 goto retry;
1175         }
1176
1177         ata_port_printk(ap, KERN_ERR,
1178                         "failed to recover PMP after %d tries, giving up\n",
1179                         ATA_EH_PMP_TRIES);
1180         sata_pmp_detach(pmp_dev);
1181         ata_dev_disable(pmp_dev);
1182
1183         return rc;
1184 }
1185
1186 /**
1187  *      sata_pmp_do_eh - do standard error handling for PMP-enabled host
1188  *      @ap: host port to handle error for
1189  *      @prereset: prereset method (can be NULL)
1190  *      @softreset: softreset method
1191  *      @hardreset: hardreset method
1192  *      @postreset: postreset method (can be NULL)
1193  *      @pmp_prereset: PMP prereset method (can be NULL)
1194  *      @pmp_softreset: PMP softreset method (can be NULL)
1195  *      @pmp_hardreset: PMP hardreset method (can be NULL)
1196  *      @pmp_postreset: PMP postreset method (can be NULL)
1197  *
1198  *      Perform standard error handling sequence for PMP-enabled host
1199  *      @ap.
1200  *
1201  *      LOCKING:
1202  *      Kernel thread context (may sleep).
1203  */
1204 void sata_pmp_do_eh(struct ata_port *ap,
1205                 ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
1206                 ata_reset_fn_t hardreset, ata_postreset_fn_t postreset,
1207                 ata_prereset_fn_t pmp_prereset, ata_reset_fn_t pmp_softreset,
1208                 ata_reset_fn_t pmp_hardreset, ata_postreset_fn_t pmp_postreset)
1209 {
1210         ata_eh_autopsy(ap);
1211         ata_eh_report(ap);
1212         sata_pmp_eh_recover(ap, prereset, softreset, hardreset, postreset,
1213                             pmp_prereset, pmp_softreset, pmp_hardreset,
1214                             pmp_postreset);
1215         ata_eh_finish(ap);
1216 }