1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  SATA specific part of ATA helper library
4  *
5  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
6  *  Copyright 2003-2004 Jeff Garzik
7  *  Copyright 2006 Tejun Heo <htejun@gmail.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <scsi/scsi_cmnd.h>
13 #include <scsi/scsi_device.h>
14 #include <scsi/scsi_eh.h>
15 #include <linux/libata.h>
16 #include <asm/unaligned.h>
17 
18 #include "libata.h"
19 #include "libata-transport.h"
20 
21 /* debounce timing parameters in msecs { interval, duration, timeout } */
22 const unsigned int sata_deb_timing_normal[]		= {   5,  100, 2000 };
23 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
24 const unsigned int sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
25 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
26 const unsigned int sata_deb_timing_long[]		= { 100, 2000, 5000 };
27 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
28 
29 /**
30  *	sata_scr_valid - test whether SCRs are accessible
31  *	@link: ATA link to test SCR accessibility for
32  *
33  *	Test whether SCRs are accessible for @link.
34  *
35  *	LOCKING:
36  *	None.
37  *
38  *	RETURNS:
39  *	1 if SCRs are accessible, 0 otherwise.
40  */
sata_scr_valid(struct ata_link * link)41 int sata_scr_valid(struct ata_link *link)
42 {
43 	struct ata_port *ap = link->ap;
44 
45 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
46 }
47 EXPORT_SYMBOL_GPL(sata_scr_valid);
48 
49 /**
50  *	sata_scr_read - read SCR register of the specified port
51  *	@link: ATA link to read SCR for
52  *	@reg: SCR to read
53  *	@val: Place to store read value
54  *
55  *	Read SCR register @reg of @link into *@val.  This function is
56  *	guaranteed to succeed if @link is ap->link, the cable type of
57  *	the port is SATA and the port implements ->scr_read.
58  *
59  *	LOCKING:
60  *	None if @link is ap->link.  Kernel thread context otherwise.
61  *
62  *	RETURNS:
63  *	0 on success, negative errno on failure.
64  */
sata_scr_read(struct ata_link * link,int reg,u32 * val)65 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
66 {
67 	if (ata_is_host_link(link)) {
68 		if (sata_scr_valid(link))
69 			return link->ap->ops->scr_read(link, reg, val);
70 		return -EOPNOTSUPP;
71 	}
72 
73 	return sata_pmp_scr_read(link, reg, val);
74 }
75 EXPORT_SYMBOL_GPL(sata_scr_read);
76 
77 /**
78  *	sata_scr_write - write SCR register of the specified port
79  *	@link: ATA link to write SCR for
80  *	@reg: SCR to write
81  *	@val: value to write
82  *
83  *	Write @val to SCR register @reg of @link.  This function is
84  *	guaranteed to succeed if @link is ap->link, the cable type of
85  *	the port is SATA and the port implements ->scr_read.
86  *
87  *	LOCKING:
88  *	None if @link is ap->link.  Kernel thread context otherwise.
89  *
90  *	RETURNS:
91  *	0 on success, negative errno on failure.
92  */
sata_scr_write(struct ata_link * link,int reg,u32 val)93 int sata_scr_write(struct ata_link *link, int reg, u32 val)
94 {
95 	if (ata_is_host_link(link)) {
96 		if (sata_scr_valid(link))
97 			return link->ap->ops->scr_write(link, reg, val);
98 		return -EOPNOTSUPP;
99 	}
100 
101 	return sata_pmp_scr_write(link, reg, val);
102 }
103 EXPORT_SYMBOL_GPL(sata_scr_write);
104 
105 /**
106  *	sata_scr_write_flush - write SCR register of the specified port and flush
107  *	@link: ATA link to write SCR for
108  *	@reg: SCR to write
109  *	@val: value to write
110  *
111  *	This function is identical to sata_scr_write() except that this
112  *	function performs flush after writing to the register.
113  *
114  *	LOCKING:
115  *	None if @link is ap->link.  Kernel thread context otherwise.
116  *
117  *	RETURNS:
118  *	0 on success, negative errno on failure.
119  */
sata_scr_write_flush(struct ata_link * link,int reg,u32 val)120 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
121 {
122 	if (ata_is_host_link(link)) {
123 		int rc;
124 
125 		if (sata_scr_valid(link)) {
126 			rc = link->ap->ops->scr_write(link, reg, val);
127 			if (rc == 0)
128 				rc = link->ap->ops->scr_read(link, reg, &val);
129 			return rc;
130 		}
131 		return -EOPNOTSUPP;
132 	}
133 
134 	return sata_pmp_scr_write(link, reg, val);
135 }
136 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
137 
138 /**
139  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
140  *	@tf: Taskfile to convert
141  *	@pmp: Port multiplier port
142  *	@is_cmd: This FIS is for command
143  *	@fis: Buffer into which data will output
144  *
145  *	Converts a standard ATA taskfile to a Serial ATA
146  *	FIS structure (Register - Host to Device).
147  *
148  *	LOCKING:
149  *	Inherited from caller.
150  */
ata_tf_to_fis(const struct ata_taskfile * tf,u8 pmp,int is_cmd,u8 * fis)151 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
152 {
153 	fis[0] = 0x27;			/* Register - Host to Device FIS */
154 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
155 	if (is_cmd)
156 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
157 
158 	fis[2] = tf->command;
159 	fis[3] = tf->feature;
160 
161 	fis[4] = tf->lbal;
162 	fis[5] = tf->lbam;
163 	fis[6] = tf->lbah;
164 	fis[7] = tf->device;
165 
166 	fis[8] = tf->hob_lbal;
167 	fis[9] = tf->hob_lbam;
168 	fis[10] = tf->hob_lbah;
169 	fis[11] = tf->hob_feature;
170 
171 	fis[12] = tf->nsect;
172 	fis[13] = tf->hob_nsect;
173 	fis[14] = 0;
174 	fis[15] = tf->ctl;
175 
176 	fis[16] = tf->auxiliary & 0xff;
177 	fis[17] = (tf->auxiliary >> 8) & 0xff;
178 	fis[18] = (tf->auxiliary >> 16) & 0xff;
179 	fis[19] = (tf->auxiliary >> 24) & 0xff;
180 }
181 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
182 
183 /**
184  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
185  *	@fis: Buffer from which data will be input
186  *	@tf: Taskfile to output
187  *
188  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
189  *
190  *	LOCKING:
191  *	Inherited from caller.
192  */
193 
ata_tf_from_fis(const u8 * fis,struct ata_taskfile * tf)194 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
195 {
196 	tf->status	= fis[2];
197 	tf->error	= fis[3];
198 
199 	tf->lbal	= fis[4];
200 	tf->lbam	= fis[5];
201 	tf->lbah	= fis[6];
202 	tf->device	= fis[7];
203 
204 	tf->hob_lbal	= fis[8];
205 	tf->hob_lbam	= fis[9];
206 	tf->hob_lbah	= fis[10];
207 
208 	tf->nsect	= fis[12];
209 	tf->hob_nsect	= fis[13];
210 }
211 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
212 
213 /**
214  *	sata_link_debounce - debounce SATA phy status
215  *	@link: ATA link to debounce SATA phy status for
216  *	@params: timing parameters { interval, duration, timeout } in msec
217  *	@deadline: deadline jiffies for the operation
218  *
219  *	Make sure SStatus of @link reaches stable state, determined by
220  *	holding the same value where DET is not 1 for @duration polled
221  *	every @interval, before @timeout.  Timeout constraints the
222  *	beginning of the stable state.  Because DET gets stuck at 1 on
223  *	some controllers after hot unplugging, this functions waits
224  *	until timeout then returns 0 if DET is stable at 1.
225  *
226  *	@timeout is further limited by @deadline.  The sooner of the
227  *	two is used.
228  *
229  *	LOCKING:
230  *	Kernel thread context (may sleep)
231  *
232  *	RETURNS:
233  *	0 on success, -errno on failure.
234  */
sata_link_debounce(struct ata_link * link,const unsigned int * params,unsigned long deadline)235 int sata_link_debounce(struct ata_link *link, const unsigned int *params,
236 		       unsigned long deadline)
237 {
238 	unsigned int interval = params[0];
239 	unsigned int duration = params[1];
240 	unsigned long last_jiffies, t;
241 	u32 last, cur;
242 	int rc;
243 
244 	t = ata_deadline(jiffies, params[2]);
245 	if (time_before(t, deadline))
246 		deadline = t;
247 
248 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
249 		return rc;
250 	cur &= 0xf;
251 
252 	last = cur;
253 	last_jiffies = jiffies;
254 
255 	while (1) {
256 		ata_msleep(link->ap, interval);
257 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
258 			return rc;
259 		cur &= 0xf;
260 
261 		/* DET stable? */
262 		if (cur == last) {
263 			if (cur == 1 && time_before(jiffies, deadline))
264 				continue;
265 			if (time_after(jiffies,
266 				       ata_deadline(last_jiffies, duration)))
267 				return 0;
268 			continue;
269 		}
270 
271 		/* unstable, start over */
272 		last = cur;
273 		last_jiffies = jiffies;
274 
275 		/* Check deadline.  If debouncing failed, return
276 		 * -EPIPE to tell upper layer to lower link speed.
277 		 */
278 		if (time_after(jiffies, deadline))
279 			return -EPIPE;
280 	}
281 }
282 EXPORT_SYMBOL_GPL(sata_link_debounce);
283 
284 /**
285  *	sata_link_resume - resume SATA link
286  *	@link: ATA link to resume SATA
287  *	@params: timing parameters { interval, duration, timeout } in msec
288  *	@deadline: deadline jiffies for the operation
289  *
290  *	Resume SATA phy @link and debounce it.
291  *
292  *	LOCKING:
293  *	Kernel thread context (may sleep)
294  *
295  *	RETURNS:
296  *	0 on success, -errno on failure.
297  */
sata_link_resume(struct ata_link * link,const unsigned int * params,unsigned long deadline)298 int sata_link_resume(struct ata_link *link, const unsigned int *params,
299 		     unsigned long deadline)
300 {
301 	int tries = ATA_LINK_RESUME_TRIES;
302 	u32 scontrol, serror;
303 	int rc;
304 
305 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
306 		return rc;
307 
308 	/*
309 	 * Writes to SControl sometimes get ignored under certain
310 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
311 	 * cleared.
312 	 */
313 	do {
314 		scontrol = (scontrol & 0x0f0) | 0x300;
315 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
316 			return rc;
317 		/*
318 		 * Some PHYs react badly if SStatus is pounded
319 		 * immediately after resuming.  Delay 200ms before
320 		 * debouncing.
321 		 */
322 		if (!(link->flags & ATA_LFLAG_NO_DEBOUNCE_DELAY))
323 			ata_msleep(link->ap, 200);
324 
325 		/* is SControl restored correctly? */
326 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
327 			return rc;
328 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
329 
330 	if ((scontrol & 0xf0f) != 0x300) {
331 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
332 			     scontrol);
333 		return 0;
334 	}
335 
336 	if (tries < ATA_LINK_RESUME_TRIES)
337 		ata_link_warn(link, "link resume succeeded after %d retries\n",
338 			      ATA_LINK_RESUME_TRIES - tries);
339 
340 	if ((rc = sata_link_debounce(link, params, deadline)))
341 		return rc;
342 
343 	/* clear SError, some PHYs require this even for SRST to work */
344 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
345 		rc = sata_scr_write(link, SCR_ERROR, serror);
346 
347 	return rc != -EINVAL ? rc : 0;
348 }
349 EXPORT_SYMBOL_GPL(sata_link_resume);
350 
351 /**
352  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
353  *	@link: ATA link to manipulate SControl for
354  *	@policy: LPM policy to configure
355  *	@spm_wakeup: initiate LPM transition to active state
356  *
357  *	Manipulate the IPM field of the SControl register of @link
358  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
359  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
360  *	the link.  This function also clears PHYRDY_CHG before
361  *	returning.
362  *
363  *	LOCKING:
364  *	EH context.
365  *
366  *	RETURNS:
367  *	0 on success, -errno otherwise.
368  */
sata_link_scr_lpm(struct ata_link * link,enum ata_lpm_policy policy,bool spm_wakeup)369 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
370 		      bool spm_wakeup)
371 {
372 	struct ata_eh_context *ehc = &link->eh_context;
373 	bool woken_up = false;
374 	u32 scontrol;
375 	int rc;
376 
377 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
378 	if (rc)
379 		return rc;
380 
381 	switch (policy) {
382 	case ATA_LPM_MAX_POWER:
383 		/* disable all LPM transitions */
384 		scontrol |= (0x7 << 8);
385 		/* initiate transition to active state */
386 		if (spm_wakeup) {
387 			scontrol |= (0x4 << 12);
388 			woken_up = true;
389 		}
390 		break;
391 	case ATA_LPM_MED_POWER:
392 		/* allow LPM to PARTIAL */
393 		scontrol &= ~(0x1 << 8);
394 		scontrol |= (0x6 << 8);
395 		break;
396 	case ATA_LPM_MED_POWER_WITH_DIPM:
397 	case ATA_LPM_MIN_POWER_WITH_PARTIAL:
398 	case ATA_LPM_MIN_POWER:
399 		if (ata_link_nr_enabled(link) > 0) {
400 			/* assume no restrictions on LPM transitions */
401 			scontrol &= ~(0x7 << 8);
402 
403 			/*
404 			 * If the controller does not support partial, slumber,
405 			 * or devsleep, then disallow these transitions.
406 			 */
407 			if (link->ap->host->flags & ATA_HOST_NO_PART)
408 				scontrol |= (0x1 << 8);
409 
410 			if (link->ap->host->flags & ATA_HOST_NO_SSC)
411 				scontrol |= (0x2 << 8);
412 
413 			if (link->ap->host->flags & ATA_HOST_NO_DEVSLP)
414 				scontrol |= (0x4 << 8);
415 		} else {
416 			/* empty port, power off */
417 			scontrol &= ~0xf;
418 			scontrol |= (0x1 << 2);
419 		}
420 		break;
421 	default:
422 		WARN_ON(1);
423 	}
424 
425 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
426 	if (rc)
427 		return rc;
428 
429 	/* give the link time to transit out of LPM state */
430 	if (woken_up)
431 		msleep(10);
432 
433 	/* clear PHYRDY_CHG from SError */
434 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
435 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
436 }
437 EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
438 
__sata_set_spd_needed(struct ata_link * link,u32 * scontrol)439 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
440 {
441 	struct ata_link *host_link = &link->ap->link;
442 	u32 limit, target, spd;
443 
444 	limit = link->sata_spd_limit;
445 
446 	/* Don't configure downstream link faster than upstream link.
447 	 * It doesn't speed up anything and some PMPs choke on such
448 	 * configuration.
449 	 */
450 	if (!ata_is_host_link(link) && host_link->sata_spd)
451 		limit &= (1 << host_link->sata_spd) - 1;
452 
453 	if (limit == UINT_MAX)
454 		target = 0;
455 	else
456 		target = fls(limit);
457 
458 	spd = (*scontrol >> 4) & 0xf;
459 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
460 
461 	return spd != target;
462 }
463 
464 /**
465  *	sata_set_spd_needed - is SATA spd configuration needed
466  *	@link: Link in question
467  *
468  *	Test whether the spd limit in SControl matches
469  *	@link->sata_spd_limit.  This function is used to determine
470  *	whether hardreset is necessary to apply SATA spd
471  *	configuration.
472  *
473  *	LOCKING:
474  *	Inherited from caller.
475  *
476  *	RETURNS:
477  *	1 if SATA spd configuration is needed, 0 otherwise.
478  */
sata_set_spd_needed(struct ata_link * link)479 static int sata_set_spd_needed(struct ata_link *link)
480 {
481 	u32 scontrol;
482 
483 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
484 		return 1;
485 
486 	return __sata_set_spd_needed(link, &scontrol);
487 }
488 
489 /**
490  *	sata_set_spd - set SATA spd according to spd limit
491  *	@link: Link to set SATA spd for
492  *
493  *	Set SATA spd of @link according to sata_spd_limit.
494  *
495  *	LOCKING:
496  *	Inherited from caller.
497  *
498  *	RETURNS:
499  *	0 if spd doesn't need to be changed, 1 if spd has been
500  *	changed.  Negative errno if SCR registers are inaccessible.
501  */
sata_set_spd(struct ata_link * link)502 int sata_set_spd(struct ata_link *link)
503 {
504 	u32 scontrol;
505 	int rc;
506 
507 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
508 		return rc;
509 
510 	if (!__sata_set_spd_needed(link, &scontrol))
511 		return 0;
512 
513 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
514 		return rc;
515 
516 	return 1;
517 }
518 EXPORT_SYMBOL_GPL(sata_set_spd);
519 
520 /**
521  *	sata_link_hardreset - reset link via SATA phy reset
522  *	@link: link to reset
523  *	@timing: timing parameters { interval, duration, timeout } in msec
524  *	@deadline: deadline jiffies for the operation
525  *	@online: optional out parameter indicating link onlineness
526  *	@check_ready: optional callback to check link readiness
527  *
528  *	SATA phy-reset @link using DET bits of SControl register.
529  *	After hardreset, link readiness is waited upon using
530  *	ata_wait_ready() if @check_ready is specified.  LLDs are
531  *	allowed to not specify @check_ready and wait itself after this
532  *	function returns.  Device classification is LLD's
533  *	responsibility.
534  *
535  *	*@online is set to one iff reset succeeded and @link is online
536  *	after reset.
537  *
538  *	LOCKING:
539  *	Kernel thread context (may sleep)
540  *
541  *	RETURNS:
542  *	0 on success, -errno otherwise.
543  */
sata_link_hardreset(struct ata_link * link,const unsigned int * timing,unsigned long deadline,bool * online,int (* check_ready)(struct ata_link *))544 int sata_link_hardreset(struct ata_link *link, const unsigned int *timing,
545 			unsigned long deadline,
546 			bool *online, int (*check_ready)(struct ata_link *))
547 {
548 	u32 scontrol;
549 	int rc;
550 
551 	if (online)
552 		*online = false;
553 
554 	if (sata_set_spd_needed(link)) {
555 		/* SATA spec says nothing about how to reconfigure
556 		 * spd.  To be on the safe side, turn off phy during
557 		 * reconfiguration.  This works for at least ICH7 AHCI
558 		 * and Sil3124.
559 		 */
560 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
561 			goto out;
562 
563 		scontrol = (scontrol & 0x0f0) | 0x304;
564 
565 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
566 			goto out;
567 
568 		sata_set_spd(link);
569 	}
570 
571 	/* issue phy wake/reset */
572 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
573 		goto out;
574 
575 	scontrol = (scontrol & 0x0f0) | 0x301;
576 
577 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
578 		goto out;
579 
580 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
581 	 * 10.4.2 says at least 1 ms.
582 	 */
583 	ata_msleep(link->ap, 1);
584 
585 	/* bring link back */
586 	rc = sata_link_resume(link, timing, deadline);
587 	if (rc)
588 		goto out;
589 	/* if link is offline nothing more to do */
590 	if (ata_phys_link_offline(link))
591 		goto out;
592 
593 	/* Link is online.  From this point, -ENODEV too is an error. */
594 	if (online)
595 		*online = true;
596 
597 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
598 		/* If PMP is supported, we have to do follow-up SRST.
599 		 * Some PMPs don't send D2H Reg FIS after hardreset if
600 		 * the first port is empty.  Wait only for
601 		 * ATA_TMOUT_PMP_SRST_WAIT.
602 		 */
603 		if (check_ready) {
604 			unsigned long pmp_deadline;
605 
606 			pmp_deadline = ata_deadline(jiffies,
607 						    ATA_TMOUT_PMP_SRST_WAIT);
608 			if (time_after(pmp_deadline, deadline))
609 				pmp_deadline = deadline;
610 			ata_wait_ready(link, pmp_deadline, check_ready);
611 		}
612 		rc = -EAGAIN;
613 		goto out;
614 	}
615 
616 	rc = 0;
617 	if (check_ready)
618 		rc = ata_wait_ready(link, deadline, check_ready);
619  out:
620 	if (rc && rc != -EAGAIN) {
621 		/* online is set iff link is online && reset succeeded */
622 		if (online)
623 			*online = false;
624 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
625 	}
626 	return rc;
627 }
628 EXPORT_SYMBOL_GPL(sata_link_hardreset);
629 
630 /**
631  *	ata_qc_complete_multiple - Complete multiple qcs successfully
632  *	@ap: port in question
633  *	@qc_active: new qc_active mask
634  *
635  *	Complete in-flight commands.  This functions is meant to be
636  *	called from low-level driver's interrupt routine to complete
637  *	requests normally.  ap->qc_active and @qc_active is compared
638  *	and commands are completed accordingly.
639  *
640  *	Always use this function when completing multiple NCQ commands
641  *	from IRQ handlers instead of calling ata_qc_complete()
642  *	multiple times to keep IRQ expect status properly in sync.
643  *
644  *	LOCKING:
645  *	spin_lock_irqsave(host lock)
646  *
647  *	RETURNS:
648  *	Number of completed commands on success, -errno otherwise.
649  */
ata_qc_complete_multiple(struct ata_port * ap,u64 qc_active)650 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
651 {
652 	u64 done_mask, ap_qc_active = ap->qc_active;
653 	int nr_done = 0;
654 
655 	/*
656 	 * If the internal tag is set on ap->qc_active, then we care about
657 	 * bit0 on the passed in qc_active mask. Move that bit up to match
658 	 * the internal tag.
659 	 */
660 	if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
661 		qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
662 		qc_active ^= qc_active & 0x01;
663 	}
664 
665 	done_mask = ap_qc_active ^ qc_active;
666 
667 	if (unlikely(done_mask & qc_active)) {
668 		ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
669 			     ap->qc_active, qc_active);
670 		return -EINVAL;
671 	}
672 
673 	if (ap->ops->qc_ncq_fill_rtf)
674 		ap->ops->qc_ncq_fill_rtf(ap, done_mask);
675 
676 	while (done_mask) {
677 		struct ata_queued_cmd *qc;
678 		unsigned int tag = __ffs64(done_mask);
679 
680 		qc = ata_qc_from_tag(ap, tag);
681 		if (qc) {
682 			ata_qc_complete(qc);
683 			nr_done++;
684 		}
685 		done_mask &= ~(1ULL << tag);
686 	}
687 
688 	return nr_done;
689 }
690 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
691 
692 /**
693  *	ata_slave_link_init - initialize slave link
694  *	@ap: port to initialize slave link for
695  *
696  *	Create and initialize slave link for @ap.  This enables slave
697  *	link handling on the port.
698  *
699  *	In libata, a port contains links and a link contains devices.
700  *	There is single host link but if a PMP is attached to it,
701  *	there can be multiple fan-out links.  On SATA, there's usually
702  *	a single device connected to a link but PATA and SATA
703  *	controllers emulating TF based interface can have two - master
704  *	and slave.
705  *
706  *	However, there are a few controllers which don't fit into this
707  *	abstraction too well - SATA controllers which emulate TF
708  *	interface with both master and slave devices but also have
709  *	separate SCR register sets for each device.  These controllers
710  *	need separate links for physical link handling
711  *	(e.g. onlineness, link speed) but should be treated like a
712  *	traditional M/S controller for everything else (e.g. command
713  *	issue, softreset).
714  *
715  *	slave_link is libata's way of handling this class of
716  *	controllers without impacting core layer too much.  For
717  *	anything other than physical link handling, the default host
718  *	link is used for both master and slave.  For physical link
719  *	handling, separate @ap->slave_link is used.  All dirty details
720  *	are implemented inside libata core layer.  From LLD's POV, the
721  *	only difference is that prereset, hardreset and postreset are
722  *	called once more for the slave link, so the reset sequence
723  *	looks like the following.
724  *
725  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
726  *	softreset(M) -> postreset(M) -> postreset(S)
727  *
728  *	Note that softreset is called only for the master.  Softreset
729  *	resets both M/S by definition, so SRST on master should handle
730  *	both (the standard method will work just fine).
731  *
732  *	LOCKING:
733  *	Should be called before host is registered.
734  *
735  *	RETURNS:
736  *	0 on success, -errno on failure.
737  */
ata_slave_link_init(struct ata_port * ap)738 int ata_slave_link_init(struct ata_port *ap)
739 {
740 	struct ata_link *link;
741 
742 	WARN_ON(ap->slave_link);
743 	WARN_ON(ap->flags & ATA_FLAG_PMP);
744 
745 	link = kzalloc(sizeof(*link), GFP_KERNEL);
746 	if (!link)
747 		return -ENOMEM;
748 
749 	ata_link_init(ap, link, 1);
750 	ap->slave_link = link;
751 	return 0;
752 }
753 EXPORT_SYMBOL_GPL(ata_slave_link_init);
754 
755 /**
756  *	sata_lpm_ignore_phy_events - test if PHY event should be ignored
757  *	@link: Link receiving the event
758  *
759  *	Test whether the received PHY event has to be ignored or not.
760  *
761  *	LOCKING:
762  *	None:
763  *
764  *	RETURNS:
765  *	True if the event has to be ignored.
766  */
sata_lpm_ignore_phy_events(struct ata_link * link)767 bool sata_lpm_ignore_phy_events(struct ata_link *link)
768 {
769 	unsigned long lpm_timeout = link->last_lpm_change +
770 				    msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
771 
772 	/* if LPM is enabled, PHYRDY doesn't mean anything */
773 	if (link->lpm_policy > ATA_LPM_MAX_POWER)
774 		return true;
775 
776 	/* ignore the first PHY event after the LPM policy changed
777 	 * as it is might be spurious
778 	 */
779 	if ((link->flags & ATA_LFLAG_CHANGED) &&
780 	    time_before(jiffies, lpm_timeout))
781 		return true;
782 
783 	return false;
784 }
785 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
786 
787 static const char *ata_lpm_policy_names[] = {
788 	[ATA_LPM_UNKNOWN]		= "max_performance",
789 	[ATA_LPM_MAX_POWER]		= "max_performance",
790 	[ATA_LPM_MED_POWER]		= "medium_power",
791 	[ATA_LPM_MED_POWER_WITH_DIPM]	= "med_power_with_dipm",
792 	[ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
793 	[ATA_LPM_MIN_POWER]		= "min_power",
794 };
795 
ata_scsi_lpm_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)796 static ssize_t ata_scsi_lpm_store(struct device *device,
797 				  struct device_attribute *attr,
798 				  const char *buf, size_t count)
799 {
800 	struct Scsi_Host *shost = class_to_shost(device);
801 	struct ata_port *ap = ata_shost_to_port(shost);
802 	struct ata_link *link;
803 	struct ata_device *dev;
804 	enum ata_lpm_policy policy;
805 	unsigned long flags;
806 
807 	/* UNKNOWN is internal state, iterate from MAX_POWER */
808 	for (policy = ATA_LPM_MAX_POWER;
809 	     policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
810 		const char *name = ata_lpm_policy_names[policy];
811 
812 		if (strncmp(name, buf, strlen(name)) == 0)
813 			break;
814 	}
815 	if (policy == ARRAY_SIZE(ata_lpm_policy_names))
816 		return -EINVAL;
817 
818 	spin_lock_irqsave(ap->lock, flags);
819 
820 	ata_for_each_link(link, ap, EDGE) {
821 		ata_for_each_dev(dev, &ap->link, ENABLED) {
822 			if (dev->horkage & ATA_HORKAGE_NOLPM) {
823 				count = -EOPNOTSUPP;
824 				goto out_unlock;
825 			}
826 		}
827 	}
828 
829 	ap->target_lpm_policy = policy;
830 	ata_port_schedule_eh(ap);
831 out_unlock:
832 	spin_unlock_irqrestore(ap->lock, flags);
833 	return count;
834 }
835 
ata_scsi_lpm_show(struct device * dev,struct device_attribute * attr,char * buf)836 static ssize_t ata_scsi_lpm_show(struct device *dev,
837 				 struct device_attribute *attr, char *buf)
838 {
839 	struct Scsi_Host *shost = class_to_shost(dev);
840 	struct ata_port *ap = ata_shost_to_port(shost);
841 
842 	if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
843 		return -EINVAL;
844 
845 	return sysfs_emit(buf, "%s\n",
846 			ata_lpm_policy_names[ap->target_lpm_policy]);
847 }
848 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
849 	    ata_scsi_lpm_show, ata_scsi_lpm_store);
850 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
851 
ata_ncq_prio_supported_show(struct device * device,struct device_attribute * attr,char * buf)852 static ssize_t ata_ncq_prio_supported_show(struct device *device,
853 					   struct device_attribute *attr,
854 					   char *buf)
855 {
856 	struct scsi_device *sdev = to_scsi_device(device);
857 	struct ata_port *ap = ata_shost_to_port(sdev->host);
858 	struct ata_device *dev;
859 	bool ncq_prio_supported;
860 	int rc = 0;
861 
862 	spin_lock_irq(ap->lock);
863 	dev = ata_scsi_find_dev(ap, sdev);
864 	if (!dev)
865 		rc = -ENODEV;
866 	else
867 		ncq_prio_supported = dev->flags & ATA_DFLAG_NCQ_PRIO;
868 	spin_unlock_irq(ap->lock);
869 
870 	return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_supported);
871 }
872 
873 DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL);
874 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported);
875 
ata_ncq_prio_enable_show(struct device * device,struct device_attribute * attr,char * buf)876 static ssize_t ata_ncq_prio_enable_show(struct device *device,
877 					struct device_attribute *attr,
878 					char *buf)
879 {
880 	struct scsi_device *sdev = to_scsi_device(device);
881 	struct ata_port *ap = ata_shost_to_port(sdev->host);
882 	struct ata_device *dev;
883 	bool ncq_prio_enable;
884 	int rc = 0;
885 
886 	spin_lock_irq(ap->lock);
887 	dev = ata_scsi_find_dev(ap, sdev);
888 	if (!dev)
889 		rc = -ENODEV;
890 	else
891 		ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED;
892 	spin_unlock_irq(ap->lock);
893 
894 	return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_enable);
895 }
896 
ata_ncq_prio_enable_store(struct device * device,struct device_attribute * attr,const char * buf,size_t len)897 static ssize_t ata_ncq_prio_enable_store(struct device *device,
898 					 struct device_attribute *attr,
899 					 const char *buf, size_t len)
900 {
901 	struct scsi_device *sdev = to_scsi_device(device);
902 	struct ata_port *ap;
903 	struct ata_device *dev;
904 	long int input;
905 	int rc = 0;
906 
907 	rc = kstrtol(buf, 10, &input);
908 	if (rc)
909 		return rc;
910 	if ((input < 0) || (input > 1))
911 		return -EINVAL;
912 
913 	ap = ata_shost_to_port(sdev->host);
914 	dev = ata_scsi_find_dev(ap, sdev);
915 	if (unlikely(!dev))
916 		return  -ENODEV;
917 
918 	spin_lock_irq(ap->lock);
919 
920 	if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
921 		rc = -EINVAL;
922 		goto unlock;
923 	}
924 
925 	if (input) {
926 		if (dev->flags & ATA_DFLAG_CDL_ENABLED) {
927 			ata_dev_err(dev,
928 				"CDL must be disabled to enable NCQ priority\n");
929 			rc = -EINVAL;
930 			goto unlock;
931 		}
932 		dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLED;
933 	} else {
934 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
935 	}
936 
937 unlock:
938 	spin_unlock_irq(ap->lock);
939 
940 	return rc ? rc : len;
941 }
942 
943 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
944 	    ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
945 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
946 
947 static struct attribute *ata_ncq_sdev_attrs[] = {
948 	&dev_attr_unload_heads.attr,
949 	&dev_attr_ncq_prio_enable.attr,
950 	&dev_attr_ncq_prio_supported.attr,
951 	NULL
952 };
953 
954 static const struct attribute_group ata_ncq_sdev_attr_group = {
955 	.attrs = ata_ncq_sdev_attrs
956 };
957 
958 const struct attribute_group *ata_ncq_sdev_groups[] = {
959 	&ata_ncq_sdev_attr_group,
960 	NULL
961 };
962 EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups);
963 
964 static ssize_t
ata_scsi_em_message_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)965 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
966 			  const char *buf, size_t count)
967 {
968 	struct Scsi_Host *shost = class_to_shost(dev);
969 	struct ata_port *ap = ata_shost_to_port(shost);
970 	if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
971 		return ap->ops->em_store(ap, buf, count);
972 	return -EINVAL;
973 }
974 
975 static ssize_t
ata_scsi_em_message_show(struct device * dev,struct device_attribute * attr,char * buf)976 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
977 			 char *buf)
978 {
979 	struct Scsi_Host *shost = class_to_shost(dev);
980 	struct ata_port *ap = ata_shost_to_port(shost);
981 
982 	if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
983 		return ap->ops->em_show(ap, buf);
984 	return -EINVAL;
985 }
986 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
987 		ata_scsi_em_message_show, ata_scsi_em_message_store);
988 EXPORT_SYMBOL_GPL(dev_attr_em_message);
989 
990 static ssize_t
ata_scsi_em_message_type_show(struct device * dev,struct device_attribute * attr,char * buf)991 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
992 			      char *buf)
993 {
994 	struct Scsi_Host *shost = class_to_shost(dev);
995 	struct ata_port *ap = ata_shost_to_port(shost);
996 
997 	return sysfs_emit(buf, "%d\n", ap->em_message_type);
998 }
999 DEVICE_ATTR(em_message_type, S_IRUGO,
1000 		  ata_scsi_em_message_type_show, NULL);
1001 EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
1002 
1003 static ssize_t
ata_scsi_activity_show(struct device * dev,struct device_attribute * attr,char * buf)1004 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
1005 		char *buf)
1006 {
1007 	struct scsi_device *sdev = to_scsi_device(dev);
1008 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1009 	struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
1010 
1011 	if (atadev && ap->ops->sw_activity_show &&
1012 	    (ap->flags & ATA_FLAG_SW_ACTIVITY))
1013 		return ap->ops->sw_activity_show(atadev, buf);
1014 	return -EINVAL;
1015 }
1016 
1017 static ssize_t
ata_scsi_activity_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1018 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
1019 	const char *buf, size_t count)
1020 {
1021 	struct scsi_device *sdev = to_scsi_device(dev);
1022 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1023 	struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
1024 	enum sw_activity val;
1025 	int rc;
1026 
1027 	if (atadev && ap->ops->sw_activity_store &&
1028 	    (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
1029 		val = simple_strtoul(buf, NULL, 0);
1030 		switch (val) {
1031 		case OFF: case BLINK_ON: case BLINK_OFF:
1032 			rc = ap->ops->sw_activity_store(atadev, val);
1033 			if (!rc)
1034 				return count;
1035 			else
1036 				return rc;
1037 		}
1038 	}
1039 	return -EINVAL;
1040 }
1041 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
1042 			ata_scsi_activity_store);
1043 EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
1044 
1045 /**
1046  *	ata_change_queue_depth - Set a device maximum queue depth
1047  *	@ap: ATA port of the target device
1048  *	@sdev: SCSI device to configure queue depth for
1049  *	@queue_depth: new queue depth
1050  *
1051  *	Helper to set a device maximum queue depth, usable with both libsas
1052  *	and libata.
1053  *
1054  */
ata_change_queue_depth(struct ata_port * ap,struct scsi_device * sdev,int queue_depth)1055 int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1056 			   int queue_depth)
1057 {
1058 	struct ata_device *dev;
1059 	unsigned long flags;
1060 	int max_queue_depth;
1061 
1062 	spin_lock_irqsave(ap->lock, flags);
1063 
1064 	dev = ata_scsi_find_dev(ap, sdev);
1065 	if (!dev || queue_depth < 1 || queue_depth == sdev->queue_depth) {
1066 		spin_unlock_irqrestore(ap->lock, flags);
1067 		return sdev->queue_depth;
1068 	}
1069 
1070 	/*
1071 	 * Make sure that the queue depth requested does not exceed the device
1072 	 * capabilities.
1073 	 */
1074 	max_queue_depth = min(ATA_MAX_QUEUE, sdev->host->can_queue);
1075 	max_queue_depth = min(max_queue_depth, ata_id_queue_depth(dev->id));
1076 	if (queue_depth > max_queue_depth) {
1077 		spin_unlock_irqrestore(ap->lock, flags);
1078 		return -EINVAL;
1079 	}
1080 
1081 	/*
1082 	 * If NCQ is not supported by the device or if the target queue depth
1083 	 * is 1 (to disable drive side command queueing), turn off NCQ.
1084 	 */
1085 	if (queue_depth == 1 || !ata_ncq_supported(dev)) {
1086 		dev->flags |= ATA_DFLAG_NCQ_OFF;
1087 		queue_depth = 1;
1088 	} else {
1089 		dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1090 	}
1091 
1092 	spin_unlock_irqrestore(ap->lock, flags);
1093 
1094 	if (queue_depth == sdev->queue_depth)
1095 		return sdev->queue_depth;
1096 
1097 	return scsi_change_queue_depth(sdev, queue_depth);
1098 }
1099 EXPORT_SYMBOL_GPL(ata_change_queue_depth);
1100 
1101 /**
1102  *	ata_scsi_change_queue_depth - SCSI callback for queue depth config
1103  *	@sdev: SCSI device to configure queue depth for
1104  *	@queue_depth: new queue depth
1105  *
1106  *	This is libata standard hostt->change_queue_depth callback.
1107  *	SCSI will call into this callback when user tries to set queue
1108  *	depth via sysfs.
1109  *
1110  *	LOCKING:
1111  *	SCSI layer (we don't care)
1112  *
1113  *	RETURNS:
1114  *	Newly configured queue depth.
1115  */
ata_scsi_change_queue_depth(struct scsi_device * sdev,int queue_depth)1116 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1117 {
1118 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1119 
1120 	return ata_change_queue_depth(ap, sdev, queue_depth);
1121 }
1122 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
1123 
1124 /**
1125  *	ata_sas_port_alloc - Allocate port for a SAS attached SATA device
1126  *	@host: ATA host container for all SAS ports
1127  *	@port_info: Information from low-level host driver
1128  *	@shost: SCSI host that the scsi device is attached to
1129  *
1130  *	LOCKING:
1131  *	PCI/etc. bus probe sem.
1132  *
1133  *	RETURNS:
1134  *	ata_port pointer on success / NULL on failure.
1135  */
1136 
ata_sas_port_alloc(struct ata_host * host,struct ata_port_info * port_info,struct Scsi_Host * shost)1137 struct ata_port *ata_sas_port_alloc(struct ata_host *host,
1138 				    struct ata_port_info *port_info,
1139 				    struct Scsi_Host *shost)
1140 {
1141 	struct ata_port *ap;
1142 
1143 	ap = ata_port_alloc(host);
1144 	if (!ap)
1145 		return NULL;
1146 
1147 	ap->port_no = 0;
1148 	ap->lock = &host->lock;
1149 	ap->pio_mask = port_info->pio_mask;
1150 	ap->mwdma_mask = port_info->mwdma_mask;
1151 	ap->udma_mask = port_info->udma_mask;
1152 	ap->flags |= port_info->flags;
1153 	ap->ops = port_info->port_ops;
1154 	ap->cbl = ATA_CBL_SATA;
1155 	ap->print_id = atomic_inc_return(&ata_print_id);
1156 
1157 	return ap;
1158 }
1159 EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
1160 
ata_sas_tport_add(struct device * parent,struct ata_port * ap)1161 int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
1162 {
1163 	return ata_tport_add(parent, ap);
1164 }
1165 EXPORT_SYMBOL_GPL(ata_sas_tport_add);
1166 
ata_sas_tport_delete(struct ata_port * ap)1167 void ata_sas_tport_delete(struct ata_port *ap)
1168 {
1169 	ata_tport_delete(ap);
1170 }
1171 EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
1172 
1173 /**
1174  *	ata_sas_slave_configure - Default slave_config routine for libata devices
1175  *	@sdev: SCSI device to configure
1176  *	@ap: ATA port to which SCSI device is attached
1177  *
1178  *	RETURNS:
1179  *	Zero.
1180  */
1181 
ata_sas_slave_configure(struct scsi_device * sdev,struct ata_port * ap)1182 int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
1183 {
1184 	ata_scsi_sdev_config(sdev);
1185 	ata_scsi_dev_config(sdev, ap->link.device);
1186 	return 0;
1187 }
1188 EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
1189 
1190 /**
1191  *	ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
1192  *	@cmd: SCSI command to be sent
1193  *	@ap:	ATA port to which the command is being sent
1194  *
1195  *	RETURNS:
1196  *	Return value from __ata_scsi_queuecmd() if @cmd can be queued,
1197  *	0 otherwise.
1198  */
1199 
ata_sas_queuecmd(struct scsi_cmnd * cmd,struct ata_port * ap)1200 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1201 {
1202 	int rc = 0;
1203 
1204 	if (likely(ata_dev_enabled(ap->link.device)))
1205 		rc = __ata_scsi_queuecmd(cmd, ap->link.device);
1206 	else {
1207 		cmd->result = (DID_BAD_TARGET << 16);
1208 		scsi_done(cmd);
1209 	}
1210 	return rc;
1211 }
1212 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
1213 
1214 /**
1215  *	sata_async_notification - SATA async notification handler
1216  *	@ap: ATA port where async notification is received
1217  *
1218  *	Handler to be called when async notification via SDB FIS is
1219  *	received.  This function schedules EH if necessary.
1220  *
1221  *	LOCKING:
1222  *	spin_lock_irqsave(host lock)
1223  *
1224  *	RETURNS:
1225  *	1 if EH is scheduled, 0 otherwise.
1226  */
sata_async_notification(struct ata_port * ap)1227 int sata_async_notification(struct ata_port *ap)
1228 {
1229 	u32 sntf;
1230 	int rc;
1231 
1232 	if (!(ap->flags & ATA_FLAG_AN))
1233 		return 0;
1234 
1235 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1236 	if (rc == 0)
1237 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1238 
1239 	if (!sata_pmp_attached(ap) || rc) {
1240 		/* PMP is not attached or SNTF is not available */
1241 		if (!sata_pmp_attached(ap)) {
1242 			/* PMP is not attached.  Check whether ATAPI
1243 			 * AN is configured.  If so, notify media
1244 			 * change.
1245 			 */
1246 			struct ata_device *dev = ap->link.device;
1247 
1248 			if ((dev->class == ATA_DEV_ATAPI) &&
1249 			    (dev->flags & ATA_DFLAG_AN))
1250 				ata_scsi_media_change_notify(dev);
1251 			return 0;
1252 		} else {
1253 			/* PMP is attached but SNTF is not available.
1254 			 * ATAPI async media change notification is
1255 			 * not used.  The PMP must be reporting PHY
1256 			 * status change, schedule EH.
1257 			 */
1258 			ata_port_schedule_eh(ap);
1259 			return 1;
1260 		}
1261 	} else {
1262 		/* PMP is attached and SNTF is available */
1263 		struct ata_link *link;
1264 
1265 		/* check and notify ATAPI AN */
1266 		ata_for_each_link(link, ap, EDGE) {
1267 			if (!(sntf & (1 << link->pmp)))
1268 				continue;
1269 
1270 			if ((link->device->class == ATA_DEV_ATAPI) &&
1271 			    (link->device->flags & ATA_DFLAG_AN))
1272 				ata_scsi_media_change_notify(link->device);
1273 		}
1274 
1275 		/* If PMP is reporting that PHY status of some
1276 		 * downstream ports has changed, schedule EH.
1277 		 */
1278 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1279 			ata_port_schedule_eh(ap);
1280 			return 1;
1281 		}
1282 
1283 		return 0;
1284 	}
1285 }
1286 EXPORT_SYMBOL_GPL(sata_async_notification);
1287 
1288 /**
1289  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1290  *	@dev: Device to read log page 10h from
1291  *	@tag: Resulting tag of the failed command
1292  *	@tf: Resulting taskfile registers of the failed command
1293  *
1294  *	Read log page 10h to obtain NCQ error details and clear error
1295  *	condition.
1296  *
1297  *	LOCKING:
1298  *	Kernel thread context (may sleep).
1299  *
1300  *	RETURNS:
1301  *	0 on success, -errno otherwise.
1302  */
ata_eh_read_log_10h(struct ata_device * dev,int * tag,struct ata_taskfile * tf)1303 static int ata_eh_read_log_10h(struct ata_device *dev,
1304 			       int *tag, struct ata_taskfile *tf)
1305 {
1306 	u8 *buf = dev->link->ap->sector_buf;
1307 	unsigned int err_mask;
1308 	u8 csum;
1309 	int i;
1310 
1311 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1312 	if (err_mask)
1313 		return -EIO;
1314 
1315 	csum = 0;
1316 	for (i = 0; i < ATA_SECT_SIZE; i++)
1317 		csum += buf[i];
1318 	if (csum)
1319 		ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1320 			     csum);
1321 
1322 	if (buf[0] & 0x80)
1323 		return -ENOENT;
1324 
1325 	*tag = buf[0] & 0x1f;
1326 
1327 	tf->status = buf[2];
1328 	tf->error = buf[3];
1329 	tf->lbal = buf[4];
1330 	tf->lbam = buf[5];
1331 	tf->lbah = buf[6];
1332 	tf->device = buf[7];
1333 	tf->hob_lbal = buf[8];
1334 	tf->hob_lbam = buf[9];
1335 	tf->hob_lbah = buf[10];
1336 	tf->nsect = buf[12];
1337 	tf->hob_nsect = buf[13];
1338 	if (ata_id_has_ncq_autosense(dev->id) && (tf->status & ATA_SENSE))
1339 		tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1340 
1341 	return 0;
1342 }
1343 
1344 /**
1345  *	ata_eh_read_sense_success_ncq_log - Read the sense data for successful
1346  *					    NCQ commands log
1347  *	@link: ATA link to get sense data for
1348  *
1349  *	Read the sense data for successful NCQ commands log page to obtain
1350  *	sense data for all NCQ commands that completed successfully with
1351  *	the sense data available bit set.
1352  *
1353  *	LOCKING:
1354  *	Kernel thread context (may sleep).
1355  *
1356  *	RETURNS:
1357  *	0 on success, -errno otherwise.
1358  */
ata_eh_read_sense_success_ncq_log(struct ata_link * link)1359 int ata_eh_read_sense_success_ncq_log(struct ata_link *link)
1360 {
1361 	struct ata_device *dev = link->device;
1362 	struct ata_port *ap = dev->link->ap;
1363 	u8 *buf = ap->ncq_sense_buf;
1364 	struct ata_queued_cmd *qc;
1365 	unsigned int err_mask, tag;
1366 	u8 *sense, sk = 0, asc = 0, ascq = 0;
1367 	u64 sense_valid, val;
1368 	int ret = 0;
1369 
1370 	err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2);
1371 	if (err_mask) {
1372 		ata_dev_err(dev,
1373 			"Failed to read Sense Data for Successful NCQ Commands log\n");
1374 		return -EIO;
1375 	}
1376 
1377 	/* Check the log header */
1378 	val = get_unaligned_le64(&buf[0]);
1379 	if ((val & 0xffff) != 1 || ((val >> 16) & 0xff) != 0x0f) {
1380 		ata_dev_err(dev,
1381 			"Invalid Sense Data for Successful NCQ Commands log\n");
1382 		return -EIO;
1383 	}
1384 
1385 	sense_valid = (u64)buf[8] | ((u64)buf[9] << 8) |
1386 		((u64)buf[10] << 16) | ((u64)buf[11] << 24);
1387 
1388 	ata_qc_for_each_raw(ap, qc, tag) {
1389 		if (!(qc->flags & ATA_QCFLAG_EH) ||
1390 		    !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) ||
1391 		    qc->err_mask ||
1392 		    ata_dev_phys_link(qc->dev) != link)
1393 			continue;
1394 
1395 		/*
1396 		 * If the command does not have any sense data, clear ATA_SENSE.
1397 		 * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished.
1398 		 */
1399 		if (!(sense_valid & (1ULL << tag))) {
1400 			qc->result_tf.status &= ~ATA_SENSE;
1401 			continue;
1402 		}
1403 
1404 		sense = &buf[32 + 24 * tag];
1405 		sk = sense[0];
1406 		asc = sense[1];
1407 		ascq = sense[2];
1408 
1409 		if (!ata_scsi_sense_is_valid(sk, asc, ascq)) {
1410 			ret = -EIO;
1411 			continue;
1412 		}
1413 
1414 		/* Set sense without also setting scsicmd->result */
1415 		scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE,
1416 					qc->scsicmd->sense_buffer, sk,
1417 					asc, ascq);
1418 		qc->flags |= ATA_QCFLAG_SENSE_VALID;
1419 
1420 		/*
1421 		 * If we have sense data, call scsi_check_sense() in order to
1422 		 * set the correct SCSI ML byte (if any). No point in checking
1423 		 * the return value, since the command has already completed
1424 		 * successfully.
1425 		 */
1426 		scsi_check_sense(qc->scsicmd);
1427 	}
1428 
1429 	return ret;
1430 }
1431 EXPORT_SYMBOL_GPL(ata_eh_read_sense_success_ncq_log);
1432 
1433 /**
1434  *	ata_eh_analyze_ncq_error - analyze NCQ error
1435  *	@link: ATA link to analyze NCQ error for
1436  *
1437  *	Read log page 10h, determine the offending qc and acquire
1438  *	error status TF.  For NCQ device errors, all LLDDs have to do
1439  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1440  *	care of the rest.
1441  *
1442  *	LOCKING:
1443  *	Kernel thread context (may sleep).
1444  */
ata_eh_analyze_ncq_error(struct ata_link * link)1445 void ata_eh_analyze_ncq_error(struct ata_link *link)
1446 {
1447 	struct ata_port *ap = link->ap;
1448 	struct ata_eh_context *ehc = &link->eh_context;
1449 	struct ata_device *dev = link->device;
1450 	struct ata_queued_cmd *qc;
1451 	struct ata_taskfile tf;
1452 	int tag, rc;
1453 
1454 	/* if frozen, we can't do much */
1455 	if (ata_port_is_frozen(ap))
1456 		return;
1457 
1458 	/* is it NCQ device error? */
1459 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1460 		return;
1461 
1462 	/* has LLDD analyzed already? */
1463 	ata_qc_for_each_raw(ap, qc, tag) {
1464 		if (!(qc->flags & ATA_QCFLAG_EH))
1465 			continue;
1466 
1467 		if (qc->err_mask)
1468 			return;
1469 	}
1470 
1471 	/* okay, this error is ours */
1472 	memset(&tf, 0, sizeof(tf));
1473 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1474 	if (rc) {
1475 		ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1476 			     rc);
1477 		return;
1478 	}
1479 
1480 	if (!(link->sactive & (1 << tag))) {
1481 		ata_link_err(link, "log page 10h reported inactive tag %d\n",
1482 			     tag);
1483 		return;
1484 	}
1485 
1486 	/* we've got the perpetrator, condemn it */
1487 	qc = __ata_qc_from_tag(ap, tag);
1488 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1489 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1490 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1491 
1492 	/*
1493 	 * If the device supports NCQ autosense, ata_eh_read_log_10h() will have
1494 	 * stored the sense data in qc->result_tf.auxiliary.
1495 	 */
1496 	if (qc->result_tf.auxiliary) {
1497 		char sense_key, asc, ascq;
1498 
1499 		sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1500 		asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1501 		ascq = qc->result_tf.auxiliary & 0xff;
1502 		if (ata_scsi_sense_is_valid(sense_key, asc, ascq)) {
1503 			ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc,
1504 					   ascq);
1505 			ata_scsi_set_sense_information(dev, qc->scsicmd,
1506 						       &qc->result_tf);
1507 			qc->flags |= ATA_QCFLAG_SENSE_VALID;
1508 		}
1509 	}
1510 
1511 	ata_qc_for_each_raw(ap, qc, tag) {
1512 		if (!(qc->flags & ATA_QCFLAG_EH) ||
1513 		    qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD ||
1514 		    ata_dev_phys_link(qc->dev) != link)
1515 			continue;
1516 
1517 		/* Skip the single QC which caused the NCQ error. */
1518 		if (qc->err_mask)
1519 			continue;
1520 
1521 		/*
1522 		 * For SATA, the STATUS and ERROR fields are shared for all NCQ
1523 		 * commands that were completed with the same SDB FIS.
1524 		 * Therefore, we have to clear the ATA_ERR bit for all QCs
1525 		 * except the one that caused the NCQ error.
1526 		 */
1527 		qc->result_tf.status &= ~ATA_ERR;
1528 		qc->result_tf.error = 0;
1529 
1530 		/*
1531 		 * If we get a NCQ error, that means that a single command was
1532 		 * aborted. All other failed commands for our link should be
1533 		 * retried and has no business of going though further scrutiny
1534 		 * by ata_eh_link_autopsy().
1535 		 */
1536 		qc->flags |= ATA_QCFLAG_RETRY;
1537 	}
1538 
1539 	ehc->i.err_mask &= ~AC_ERR_DEV;
1540 }
1541 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
1542