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