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