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