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