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