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