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