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 <linux/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_down_spd_limit - adjust SATA spd limit downward
522  *	@link: Link to adjust SATA spd limit for
523  *	@spd_limit: Additional limit
524  *
525  *	Adjust SATA spd limit of @link downward.  Note that this
526  *	function only adjusts the limit.  The change must be applied
527  *	using sata_set_spd().
528  *
529  *	If @spd_limit is non-zero, the speed is limited to equal to or
530  *	lower than @spd_limit if such speed is supported.  If
531  *	@spd_limit is slower than any supported speed, only the lowest
532  *	supported speed is allowed.
533  *
534  *	LOCKING:
535  *	Inherited from caller.
536  *
537  *	RETURNS:
538  *	0 on success, negative errno on failure
539  */
sata_down_spd_limit(struct ata_link * link,u32 spd_limit)540 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
541 {
542 	u32 sstatus, spd, mask;
543 	int rc, bit;
544 
545 	if (!sata_scr_valid(link))
546 		return -EOPNOTSUPP;
547 
548 	/* If SCR can be read, use it to determine the current SPD.
549 	 * If not, use cached value in link->sata_spd.
550 	 */
551 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
552 	if (rc == 0 && ata_sstatus_online(sstatus))
553 		spd = (sstatus >> 4) & 0xf;
554 	else
555 		spd = link->sata_spd;
556 
557 	mask = link->sata_spd_limit;
558 	if (mask <= 1)
559 		return -EINVAL;
560 
561 	/* unconditionally mask off the highest bit */
562 	bit = fls(mask) - 1;
563 	mask &= ~(1 << bit);
564 
565 	/*
566 	 * Mask off all speeds higher than or equal to the current one.  At
567 	 * this point, if current SPD is not available and we previously
568 	 * recorded the link speed from SStatus, the driver has already
569 	 * masked off the highest bit so mask should already be 1 or 0.
570 	 * Otherwise, we should not force 1.5Gbps on a link where we have
571 	 * not previously recorded speed from SStatus.  Just return in this
572 	 * case.
573 	 */
574 	if (spd > 1)
575 		mask &= (1 << (spd - 1)) - 1;
576 	else if (link->sata_spd)
577 		return -EINVAL;
578 
579 	/* were we already at the bottom? */
580 	if (!mask)
581 		return -EINVAL;
582 
583 	if (spd_limit) {
584 		if (mask & ((1 << spd_limit) - 1))
585 			mask &= (1 << spd_limit) - 1;
586 		else {
587 			bit = ffs(mask) - 1;
588 			mask = 1 << bit;
589 		}
590 	}
591 
592 	link->sata_spd_limit = mask;
593 
594 	ata_link_warn(link, "limiting SATA link speed to %s\n",
595 		      sata_spd_string(fls(mask)));
596 
597 	return 0;
598 }
599 
600 /**
601  *	sata_link_hardreset - reset link via SATA phy reset
602  *	@link: link to reset
603  *	@timing: timing parameters { interval, duration, timeout } in msec
604  *	@deadline: deadline jiffies for the operation
605  *	@online: optional out parameter indicating link onlineness
606  *	@check_ready: optional callback to check link readiness
607  *
608  *	SATA phy-reset @link using DET bits of SControl register.
609  *	After hardreset, link readiness is waited upon using
610  *	ata_wait_ready() if @check_ready is specified.  LLDs are
611  *	allowed to not specify @check_ready and wait itself after this
612  *	function returns.  Device classification is LLD's
613  *	responsibility.
614  *
615  *	*@online is set to one iff reset succeeded and @link is online
616  *	after reset.
617  *
618  *	LOCKING:
619  *	Kernel thread context (may sleep)
620  *
621  *	RETURNS:
622  *	0 on success, -errno otherwise.
623  */
sata_link_hardreset(struct ata_link * link,const unsigned int * timing,unsigned long deadline,bool * online,int (* check_ready)(struct ata_link *))624 int sata_link_hardreset(struct ata_link *link, const unsigned int *timing,
625 			unsigned long deadline,
626 			bool *online, int (*check_ready)(struct ata_link *))
627 {
628 	u32 scontrol;
629 	int rc;
630 
631 	if (online)
632 		*online = false;
633 
634 	if (sata_set_spd_needed(link)) {
635 		/* SATA spec says nothing about how to reconfigure
636 		 * spd.  To be on the safe side, turn off phy during
637 		 * reconfiguration.  This works for at least ICH7 AHCI
638 		 * and Sil3124.
639 		 */
640 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
641 			goto out;
642 
643 		scontrol = (scontrol & 0x0f0) | 0x304;
644 
645 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
646 			goto out;
647 
648 		sata_set_spd(link);
649 	}
650 
651 	/* issue phy wake/reset */
652 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
653 		goto out;
654 
655 	scontrol = (scontrol & 0x0f0) | 0x301;
656 
657 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
658 		goto out;
659 
660 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
661 	 * 10.4.2 says at least 1 ms.
662 	 */
663 	ata_msleep(link->ap, 1);
664 
665 	/* bring link back */
666 	rc = sata_link_resume(link, timing, deadline);
667 	if (rc)
668 		goto out;
669 	/* if link is offline nothing more to do */
670 	if (ata_phys_link_offline(link))
671 		goto out;
672 
673 	/* Link is online.  From this point, -ENODEV too is an error. */
674 	if (online)
675 		*online = true;
676 
677 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
678 		/* If PMP is supported, we have to do follow-up SRST.
679 		 * Some PMPs don't send D2H Reg FIS after hardreset if
680 		 * the first port is empty.  Wait only for
681 		 * ATA_TMOUT_PMP_SRST_WAIT.
682 		 */
683 		if (check_ready) {
684 			unsigned long pmp_deadline;
685 
686 			pmp_deadline = ata_deadline(jiffies,
687 						    ATA_TMOUT_PMP_SRST_WAIT);
688 			if (time_after(pmp_deadline, deadline))
689 				pmp_deadline = deadline;
690 			ata_wait_ready(link, pmp_deadline, check_ready);
691 		}
692 		rc = -EAGAIN;
693 		goto out;
694 	}
695 
696 	rc = 0;
697 	if (check_ready)
698 		rc = ata_wait_ready(link, deadline, check_ready);
699  out:
700 	if (rc && rc != -EAGAIN) {
701 		/* online is set iff link is online && reset succeeded */
702 		if (online)
703 			*online = false;
704 	}
705 	return rc;
706 }
707 EXPORT_SYMBOL_GPL(sata_link_hardreset);
708 
709 /**
710  *	sata_std_hardreset - COMRESET w/o waiting or classification
711  *	@link: link to reset
712  *	@class: resulting class of attached device
713  *	@deadline: deadline jiffies for the operation
714  *
715  *	Standard SATA COMRESET w/o waiting or classification.
716  *
717  *	LOCKING:
718  *	Kernel thread context (may sleep)
719  *
720  *	RETURNS:
721  *	0 if link offline, -EAGAIN if link online, -errno on errors.
722  */
sata_std_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)723 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
724 		       unsigned long deadline)
725 {
726 	const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context);
727 	bool online;
728 	int rc;
729 
730 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
731 	if (online)
732 		return -EAGAIN;
733 	return rc;
734 }
735 EXPORT_SYMBOL_GPL(sata_std_hardreset);
736 
737 /**
738  *	ata_qc_complete_multiple - Complete multiple qcs successfully
739  *	@ap: port in question
740  *	@qc_active: new qc_active mask
741  *
742  *	Complete in-flight commands.  This functions is meant to be
743  *	called from low-level driver's interrupt routine to complete
744  *	requests normally.  ap->qc_active and @qc_active is compared
745  *	and commands are completed accordingly.
746  *
747  *	Always use this function when completing multiple NCQ commands
748  *	from IRQ handlers instead of calling ata_qc_complete()
749  *	multiple times to keep IRQ expect status properly in sync.
750  *
751  *	LOCKING:
752  *	spin_lock_irqsave(host lock)
753  *
754  *	RETURNS:
755  *	Number of completed commands on success, -errno otherwise.
756  */
ata_qc_complete_multiple(struct ata_port * ap,u64 qc_active)757 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
758 {
759 	u64 done_mask, ap_qc_active = ap->qc_active;
760 	int nr_done = 0;
761 
762 	/*
763 	 * If the internal tag is set on ap->qc_active, then we care about
764 	 * bit0 on the passed in qc_active mask. Move that bit up to match
765 	 * the internal tag.
766 	 */
767 	if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
768 		qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
769 		qc_active ^= qc_active & 0x01;
770 	}
771 
772 	done_mask = ap_qc_active ^ qc_active;
773 
774 	if (unlikely(done_mask & qc_active)) {
775 		ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
776 			     ap->qc_active, qc_active);
777 		return -EINVAL;
778 	}
779 
780 	if (ap->ops->qc_ncq_fill_rtf)
781 		ap->ops->qc_ncq_fill_rtf(ap, done_mask);
782 
783 	while (done_mask) {
784 		struct ata_queued_cmd *qc;
785 		unsigned int tag = __ffs64(done_mask);
786 
787 		qc = ata_qc_from_tag(ap, tag);
788 		if (qc) {
789 			ata_qc_complete(qc);
790 			nr_done++;
791 		}
792 		done_mask &= ~(1ULL << tag);
793 	}
794 
795 	return nr_done;
796 }
797 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
798 
799 /**
800  *	ata_slave_link_init - initialize slave link
801  *	@ap: port to initialize slave link for
802  *
803  *	Create and initialize slave link for @ap.  This enables slave
804  *	link handling on the port.
805  *
806  *	In libata, a port contains links and a link contains devices.
807  *	There is single host link but if a PMP is attached to it,
808  *	there can be multiple fan-out links.  On SATA, there's usually
809  *	a single device connected to a link but PATA and SATA
810  *	controllers emulating TF based interface can have two - master
811  *	and slave.
812  *
813  *	However, there are a few controllers which don't fit into this
814  *	abstraction too well - SATA controllers which emulate TF
815  *	interface with both master and slave devices but also have
816  *	separate SCR register sets for each device.  These controllers
817  *	need separate links for physical link handling
818  *	(e.g. onlineness, link speed) but should be treated like a
819  *	traditional M/S controller for everything else (e.g. command
820  *	issue, softreset).
821  *
822  *	slave_link is libata's way of handling this class of
823  *	controllers without impacting core layer too much.  For
824  *	anything other than physical link handling, the default host
825  *	link is used for both master and slave.  For physical link
826  *	handling, separate @ap->slave_link is used.  All dirty details
827  *	are implemented inside libata core layer.  From LLD's POV, the
828  *	only difference is that prereset, hardreset and postreset are
829  *	called once more for the slave link, so the reset sequence
830  *	looks like the following.
831  *
832  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
833  *	softreset(M) -> postreset(M) -> postreset(S)
834  *
835  *	Note that softreset is called only for the master.  Softreset
836  *	resets both M/S by definition, so SRST on master should handle
837  *	both (the standard method will work just fine).
838  *
839  *	LOCKING:
840  *	Should be called before host is registered.
841  *
842  *	RETURNS:
843  *	0 on success, -errno on failure.
844  */
ata_slave_link_init(struct ata_port * ap)845 int ata_slave_link_init(struct ata_port *ap)
846 {
847 	struct ata_link *link;
848 
849 	WARN_ON(ap->slave_link);
850 	WARN_ON(ap->flags & ATA_FLAG_PMP);
851 
852 	link = kzalloc(sizeof(*link), GFP_KERNEL);
853 	if (!link)
854 		return -ENOMEM;
855 
856 	ata_link_init(ap, link, 1);
857 	ap->slave_link = link;
858 	return 0;
859 }
860 EXPORT_SYMBOL_GPL(ata_slave_link_init);
861 
862 /**
863  *	sata_lpm_ignore_phy_events - test if PHY event should be ignored
864  *	@link: Link receiving the event
865  *
866  *	Test whether the received PHY event has to be ignored or not.
867  *
868  *	LOCKING:
869  *	None:
870  *
871  *	RETURNS:
872  *	True if the event has to be ignored.
873  */
sata_lpm_ignore_phy_events(struct ata_link * link)874 bool sata_lpm_ignore_phy_events(struct ata_link *link)
875 {
876 	unsigned long lpm_timeout = link->last_lpm_change +
877 				    msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
878 
879 	/* if LPM is enabled, PHYRDY doesn't mean anything */
880 	if (link->lpm_policy > ATA_LPM_MAX_POWER)
881 		return true;
882 
883 	/* ignore the first PHY event after the LPM policy changed
884 	 * as it is might be spurious
885 	 */
886 	if ((link->flags & ATA_LFLAG_CHANGED) &&
887 	    time_before(jiffies, lpm_timeout))
888 		return true;
889 
890 	return false;
891 }
892 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
893 
894 static const char *ata_lpm_policy_names[] = {
895 	[ATA_LPM_UNKNOWN]		= "keep_firmware_settings",
896 	[ATA_LPM_MAX_POWER]		= "max_performance",
897 	[ATA_LPM_MED_POWER]		= "medium_power",
898 	[ATA_LPM_MED_POWER_WITH_DIPM]	= "med_power_with_dipm",
899 	[ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
900 	[ATA_LPM_MIN_POWER]		= "min_power",
901 };
902 
ata_scsi_lpm_store(struct device * device,struct device_attribute * attr,const char * buf,size_t count)903 static ssize_t ata_scsi_lpm_store(struct device *device,
904 				  struct device_attribute *attr,
905 				  const char *buf, size_t count)
906 {
907 	struct Scsi_Host *shost = class_to_shost(device);
908 	struct ata_port *ap = ata_shost_to_port(shost);
909 	struct ata_link *link;
910 	struct ata_device *dev;
911 	enum ata_lpm_policy policy;
912 	unsigned long flags;
913 
914 	/* UNKNOWN is internal state, iterate from MAX_POWER */
915 	for (policy = ATA_LPM_MAX_POWER;
916 	     policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
917 		const char *name = ata_lpm_policy_names[policy];
918 
919 		if (strncmp(name, buf, strlen(name)) == 0)
920 			break;
921 	}
922 	if (policy == ARRAY_SIZE(ata_lpm_policy_names))
923 		return -EINVAL;
924 
925 	spin_lock_irqsave(ap->lock, flags);
926 
927 	ata_for_each_link(link, ap, EDGE) {
928 		ata_for_each_dev(dev, &ap->link, ENABLED) {
929 			if (dev->quirks & ATA_QUIRK_NOLPM) {
930 				count = -EOPNOTSUPP;
931 				goto out_unlock;
932 			}
933 		}
934 	}
935 
936 	ap->target_lpm_policy = policy;
937 	ata_port_schedule_eh(ap);
938 out_unlock:
939 	spin_unlock_irqrestore(ap->lock, flags);
940 	return count;
941 }
942 
ata_scsi_lpm_show(struct device * dev,struct device_attribute * attr,char * buf)943 static ssize_t ata_scsi_lpm_show(struct device *dev,
944 				 struct device_attribute *attr, char *buf)
945 {
946 	struct Scsi_Host *shost = class_to_shost(dev);
947 	struct ata_port *ap = ata_shost_to_port(shost);
948 
949 	if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
950 		return -EINVAL;
951 
952 	return sysfs_emit(buf, "%s\n",
953 			ata_lpm_policy_names[ap->target_lpm_policy]);
954 }
955 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
956 	    ata_scsi_lpm_show, ata_scsi_lpm_store);
957 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
958 
959 /**
960  *	ata_ncq_prio_supported - Check if device supports NCQ Priority
961  *	@ap: ATA port of the target device
962  *	@sdev: SCSI device
963  *	@supported: Address of a boolean to store the result
964  *
965  *	Helper to check if device supports NCQ Priority feature.
966  *
967  *	Context: Any context. Takes and releases @ap->lock.
968  *
969  *	Return:
970  *	* %0		- OK. Status is stored into @supported
971  *	* %-ENODEV	- Failed to find the ATA device
972  */
ata_ncq_prio_supported(struct ata_port * ap,struct scsi_device * sdev,bool * supported)973 int ata_ncq_prio_supported(struct ata_port *ap, struct scsi_device *sdev,
974 			   bool *supported)
975 {
976 	struct ata_device *dev;
977 	unsigned long flags;
978 	int rc = 0;
979 
980 	spin_lock_irqsave(ap->lock, flags);
981 	dev = ata_scsi_find_dev(ap, sdev);
982 	if (!dev)
983 		rc = -ENODEV;
984 	else
985 		*supported = dev->flags & ATA_DFLAG_NCQ_PRIO;
986 	spin_unlock_irqrestore(ap->lock, flags);
987 
988 	return rc;
989 }
990 EXPORT_SYMBOL_GPL(ata_ncq_prio_supported);
991 
ata_ncq_prio_supported_show(struct device * device,struct device_attribute * attr,char * buf)992 static ssize_t ata_ncq_prio_supported_show(struct device *device,
993 					   struct device_attribute *attr,
994 					   char *buf)
995 {
996 	struct scsi_device *sdev = to_scsi_device(device);
997 	struct ata_port *ap = ata_shost_to_port(sdev->host);
998 	bool supported;
999 	int rc;
1000 
1001 	rc = ata_ncq_prio_supported(ap, sdev, &supported);
1002 	if (rc)
1003 		return rc;
1004 
1005 	return sysfs_emit(buf, "%d\n", supported);
1006 }
1007 
1008 DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL);
1009 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported);
1010 
1011 /**
1012  *	ata_ncq_prio_enabled - Check if NCQ Priority is enabled
1013  *	@ap: ATA port of the target device
1014  *	@sdev: SCSI device
1015  *	@enabled: Address of a boolean to store the result
1016  *
1017  *	Helper to check if NCQ Priority feature is enabled.
1018  *
1019  *	Context: Any context. Takes and releases @ap->lock.
1020  *
1021  *	Return:
1022  *	* %0		- OK. Status is stored into @enabled
1023  *	* %-ENODEV	- Failed to find the ATA device
1024  */
ata_ncq_prio_enabled(struct ata_port * ap,struct scsi_device * sdev,bool * enabled)1025 int ata_ncq_prio_enabled(struct ata_port *ap, struct scsi_device *sdev,
1026 			 bool *enabled)
1027 {
1028 	struct ata_device *dev;
1029 	unsigned long flags;
1030 	int rc = 0;
1031 
1032 	spin_lock_irqsave(ap->lock, flags);
1033 	dev = ata_scsi_find_dev(ap, sdev);
1034 	if (!dev)
1035 		rc = -ENODEV;
1036 	else
1037 		*enabled = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED;
1038 	spin_unlock_irqrestore(ap->lock, flags);
1039 
1040 	return rc;
1041 }
1042 EXPORT_SYMBOL_GPL(ata_ncq_prio_enabled);
1043 
ata_ncq_prio_enable_show(struct device * device,struct device_attribute * attr,char * buf)1044 static ssize_t ata_ncq_prio_enable_show(struct device *device,
1045 					struct device_attribute *attr,
1046 					char *buf)
1047 {
1048 	struct scsi_device *sdev = to_scsi_device(device);
1049 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1050 	bool enabled;
1051 	int rc;
1052 
1053 	rc = ata_ncq_prio_enabled(ap, sdev, &enabled);
1054 	if (rc)
1055 		return rc;
1056 
1057 	return sysfs_emit(buf, "%d\n", enabled);
1058 }
1059 
1060 /**
1061  *	ata_ncq_prio_enable - Enable/disable NCQ Priority
1062  *	@ap: ATA port of the target device
1063  *	@sdev: SCSI device
1064  *	@enable: true - enable NCQ Priority, false - disable NCQ Priority
1065  *
1066  *	Helper to enable/disable NCQ Priority feature.
1067  *
1068  *	Context: Any context. Takes and releases @ap->lock.
1069  *
1070  *	Return:
1071  *	* %0		- OK. Status is stored into @enabled
1072  *	* %-ENODEV	- Failed to find the ATA device
1073  *	* %-EINVAL	- NCQ Priority is not supported or CDL is enabled
1074  */
ata_ncq_prio_enable(struct ata_port * ap,struct scsi_device * sdev,bool enable)1075 int ata_ncq_prio_enable(struct ata_port *ap, struct scsi_device *sdev,
1076 			bool enable)
1077 {
1078 	struct ata_device *dev;
1079 	unsigned long flags;
1080 	int rc = 0;
1081 
1082 	spin_lock_irqsave(ap->lock, flags);
1083 
1084 	dev = ata_scsi_find_dev(ap, sdev);
1085 	if (!dev) {
1086 		rc = -ENODEV;
1087 		goto unlock;
1088 	}
1089 
1090 	if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
1091 		rc = -EINVAL;
1092 		goto unlock;
1093 	}
1094 
1095 	if (enable) {
1096 		if (dev->flags & ATA_DFLAG_CDL_ENABLED) {
1097 			ata_dev_err(dev,
1098 				"CDL must be disabled to enable NCQ priority\n");
1099 			rc = -EINVAL;
1100 			goto unlock;
1101 		}
1102 		dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLED;
1103 	} else {
1104 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
1105 	}
1106 
1107 unlock:
1108 	spin_unlock_irqrestore(ap->lock, flags);
1109 
1110 	return rc;
1111 }
1112 EXPORT_SYMBOL_GPL(ata_ncq_prio_enable);
1113 
ata_ncq_prio_enable_store(struct device * device,struct device_attribute * attr,const char * buf,size_t len)1114 static ssize_t ata_ncq_prio_enable_store(struct device *device,
1115 					 struct device_attribute *attr,
1116 					 const char *buf, size_t len)
1117 {
1118 	struct scsi_device *sdev = to_scsi_device(device);
1119 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1120 	bool enable;
1121 	int rc;
1122 
1123 	rc = kstrtobool(buf, &enable);
1124 	if (rc)
1125 		return rc;
1126 
1127 	rc = ata_ncq_prio_enable(ap, sdev, enable);
1128 	if (rc)
1129 		return rc;
1130 
1131 	return len;
1132 }
1133 
1134 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
1135 	    ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
1136 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
1137 
1138 static struct attribute *ata_ncq_sdev_attrs[] = {
1139 	&dev_attr_unload_heads.attr,
1140 	&dev_attr_ncq_prio_enable.attr,
1141 	&dev_attr_ncq_prio_supported.attr,
1142 	NULL
1143 };
1144 
1145 static const struct attribute_group ata_ncq_sdev_attr_group = {
1146 	.attrs = ata_ncq_sdev_attrs
1147 };
1148 
1149 const struct attribute_group *ata_ncq_sdev_groups[] = {
1150 	&ata_ncq_sdev_attr_group,
1151 	NULL
1152 };
1153 EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups);
1154 
1155 static ssize_t
ata_scsi_em_message_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1156 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
1157 			  const char *buf, size_t count)
1158 {
1159 	struct Scsi_Host *shost = class_to_shost(dev);
1160 	struct ata_port *ap = ata_shost_to_port(shost);
1161 	if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
1162 		return ap->ops->em_store(ap, buf, count);
1163 	return -EINVAL;
1164 }
1165 
1166 static ssize_t
ata_scsi_em_message_show(struct device * dev,struct device_attribute * attr,char * buf)1167 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
1168 			 char *buf)
1169 {
1170 	struct Scsi_Host *shost = class_to_shost(dev);
1171 	struct ata_port *ap = ata_shost_to_port(shost);
1172 
1173 	if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
1174 		return ap->ops->em_show(ap, buf);
1175 	return -EINVAL;
1176 }
1177 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
1178 		ata_scsi_em_message_show, ata_scsi_em_message_store);
1179 EXPORT_SYMBOL_GPL(dev_attr_em_message);
1180 
1181 static ssize_t
ata_scsi_em_message_type_show(struct device * dev,struct device_attribute * attr,char * buf)1182 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
1183 			      char *buf)
1184 {
1185 	struct Scsi_Host *shost = class_to_shost(dev);
1186 	struct ata_port *ap = ata_shost_to_port(shost);
1187 
1188 	return sysfs_emit(buf, "%d\n", ap->em_message_type);
1189 }
1190 DEVICE_ATTR(em_message_type, S_IRUGO,
1191 		  ata_scsi_em_message_type_show, NULL);
1192 EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
1193 
1194 static ssize_t
ata_scsi_activity_show(struct device * dev,struct device_attribute * attr,char * buf)1195 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
1196 		char *buf)
1197 {
1198 	struct scsi_device *sdev = to_scsi_device(dev);
1199 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1200 	struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
1201 
1202 	if (atadev && ap->ops->sw_activity_show &&
1203 	    (ap->flags & ATA_FLAG_SW_ACTIVITY))
1204 		return ap->ops->sw_activity_show(atadev, buf);
1205 	return -EINVAL;
1206 }
1207 
1208 static ssize_t
ata_scsi_activity_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1209 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
1210 	const char *buf, size_t count)
1211 {
1212 	struct scsi_device *sdev = to_scsi_device(dev);
1213 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1214 	struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
1215 	enum sw_activity val;
1216 	int rc;
1217 
1218 	if (atadev && ap->ops->sw_activity_store &&
1219 	    (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
1220 		val = simple_strtoul(buf, NULL, 0);
1221 		switch (val) {
1222 		case OFF: case BLINK_ON: case BLINK_OFF:
1223 			rc = ap->ops->sw_activity_store(atadev, val);
1224 			if (!rc)
1225 				return count;
1226 			else
1227 				return rc;
1228 		}
1229 	}
1230 	return -EINVAL;
1231 }
1232 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
1233 			ata_scsi_activity_store);
1234 EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
1235 
1236 /**
1237  *	ata_change_queue_depth - Set a device maximum queue depth
1238  *	@ap: ATA port of the target device
1239  *	@sdev: SCSI device to configure queue depth for
1240  *	@queue_depth: new queue depth
1241  *
1242  *	Helper to set a device maximum queue depth, usable with both libsas
1243  *	and libata.
1244  *
1245  */
ata_change_queue_depth(struct ata_port * ap,struct scsi_device * sdev,int queue_depth)1246 int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1247 			   int queue_depth)
1248 {
1249 	struct ata_device *dev;
1250 	unsigned long flags;
1251 	int max_queue_depth;
1252 
1253 	spin_lock_irqsave(ap->lock, flags);
1254 
1255 	dev = ata_scsi_find_dev(ap, sdev);
1256 	if (!dev || queue_depth < 1 || queue_depth == sdev->queue_depth) {
1257 		spin_unlock_irqrestore(ap->lock, flags);
1258 		return sdev->queue_depth;
1259 	}
1260 
1261 	/*
1262 	 * Make sure that the queue depth requested does not exceed the device
1263 	 * capabilities.
1264 	 */
1265 	max_queue_depth = min(ATA_MAX_QUEUE, sdev->host->can_queue);
1266 	max_queue_depth = min(max_queue_depth, ata_id_queue_depth(dev->id));
1267 	if (queue_depth > max_queue_depth) {
1268 		spin_unlock_irqrestore(ap->lock, flags);
1269 		return -EINVAL;
1270 	}
1271 
1272 	/*
1273 	 * If NCQ is not supported by the device or if the target queue depth
1274 	 * is 1 (to disable drive side command queueing), turn off NCQ.
1275 	 */
1276 	if (queue_depth == 1 || !ata_ncq_supported(dev)) {
1277 		dev->flags |= ATA_DFLAG_NCQ_OFF;
1278 		queue_depth = 1;
1279 	} else {
1280 		dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1281 	}
1282 
1283 	spin_unlock_irqrestore(ap->lock, flags);
1284 
1285 	if (queue_depth == sdev->queue_depth)
1286 		return sdev->queue_depth;
1287 
1288 	return scsi_change_queue_depth(sdev, queue_depth);
1289 }
1290 EXPORT_SYMBOL_GPL(ata_change_queue_depth);
1291 
1292 /**
1293  *	ata_scsi_change_queue_depth - SCSI callback for queue depth config
1294  *	@sdev: SCSI device to configure queue depth for
1295  *	@queue_depth: new queue depth
1296  *
1297  *	This is libata standard hostt->change_queue_depth callback.
1298  *	SCSI will call into this callback when user tries to set queue
1299  *	depth via sysfs.
1300  *
1301  *	LOCKING:
1302  *	SCSI layer (we don't care)
1303  *
1304  *	RETURNS:
1305  *	Newly configured queue depth.
1306  */
ata_scsi_change_queue_depth(struct scsi_device * sdev,int queue_depth)1307 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1308 {
1309 	struct ata_port *ap = ata_shost_to_port(sdev->host);
1310 
1311 	return ata_change_queue_depth(ap, sdev, queue_depth);
1312 }
1313 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
1314 
1315 /**
1316  *	ata_sas_device_configure - Default device_configure routine for libata
1317  *				   devices
1318  *	@sdev: SCSI device to configure
1319  *	@lim: queue limits
1320  *	@ap: ATA port to which SCSI device is attached
1321  *
1322  *	RETURNS:
1323  *	Zero.
1324  */
1325 
ata_sas_device_configure(struct scsi_device * sdev,struct queue_limits * lim,struct ata_port * ap)1326 int ata_sas_device_configure(struct scsi_device *sdev, struct queue_limits *lim,
1327 		struct ata_port *ap)
1328 {
1329 	ata_scsi_sdev_config(sdev);
1330 
1331 	return ata_scsi_dev_config(sdev, lim, ap->link.device);
1332 }
1333 EXPORT_SYMBOL_GPL(ata_sas_device_configure);
1334 
1335 /**
1336  *	ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
1337  *	@cmd: SCSI command to be sent
1338  *	@ap:	ATA port to which the command is being sent
1339  *
1340  *	RETURNS:
1341  *	Return value from __ata_scsi_queuecmd() if @cmd can be queued,
1342  *	0 otherwise.
1343  */
1344 
ata_sas_queuecmd(struct scsi_cmnd * cmd,struct ata_port * ap)1345 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1346 {
1347 	int rc = 0;
1348 
1349 	if (likely(ata_dev_enabled(ap->link.device)))
1350 		rc = __ata_scsi_queuecmd(cmd, ap->link.device);
1351 	else {
1352 		cmd->result = (DID_BAD_TARGET << 16);
1353 		scsi_done(cmd);
1354 	}
1355 	return rc;
1356 }
1357 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
1358 
1359 /**
1360  *	sata_async_notification - SATA async notification handler
1361  *	@ap: ATA port where async notification is received
1362  *
1363  *	Handler to be called when async notification via SDB FIS is
1364  *	received.  This function schedules EH if necessary.
1365  *
1366  *	LOCKING:
1367  *	spin_lock_irqsave(host lock)
1368  *
1369  *	RETURNS:
1370  *	1 if EH is scheduled, 0 otherwise.
1371  */
sata_async_notification(struct ata_port * ap)1372 int sata_async_notification(struct ata_port *ap)
1373 {
1374 	u32 sntf;
1375 	int rc;
1376 
1377 	if (!(ap->flags & ATA_FLAG_AN))
1378 		return 0;
1379 
1380 	rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1381 	if (rc == 0)
1382 		sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1383 
1384 	if (!sata_pmp_attached(ap) || rc) {
1385 		/* PMP is not attached or SNTF is not available */
1386 		if (!sata_pmp_attached(ap)) {
1387 			/* PMP is not attached.  Check whether ATAPI
1388 			 * AN is configured.  If so, notify media
1389 			 * change.
1390 			 */
1391 			struct ata_device *dev = ap->link.device;
1392 
1393 			if ((dev->class == ATA_DEV_ATAPI) &&
1394 			    (dev->flags & ATA_DFLAG_AN))
1395 				ata_scsi_media_change_notify(dev);
1396 			return 0;
1397 		} else {
1398 			/* PMP is attached but SNTF is not available.
1399 			 * ATAPI async media change notification is
1400 			 * not used.  The PMP must be reporting PHY
1401 			 * status change, schedule EH.
1402 			 */
1403 			ata_port_schedule_eh(ap);
1404 			return 1;
1405 		}
1406 	} else {
1407 		/* PMP is attached and SNTF is available */
1408 		struct ata_link *link;
1409 
1410 		/* check and notify ATAPI AN */
1411 		ata_for_each_link(link, ap, EDGE) {
1412 			if (!(sntf & (1 << link->pmp)))
1413 				continue;
1414 
1415 			if ((link->device->class == ATA_DEV_ATAPI) &&
1416 			    (link->device->flags & ATA_DFLAG_AN))
1417 				ata_scsi_media_change_notify(link->device);
1418 		}
1419 
1420 		/* If PMP is reporting that PHY status of some
1421 		 * downstream ports has changed, schedule EH.
1422 		 */
1423 		if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1424 			ata_port_schedule_eh(ap);
1425 			return 1;
1426 		}
1427 
1428 		return 0;
1429 	}
1430 }
1431 EXPORT_SYMBOL_GPL(sata_async_notification);
1432 
1433 /**
1434  *	ata_eh_read_log_10h - Read log page 10h for NCQ error details
1435  *	@dev: Device to read log page 10h from
1436  *	@tag: Resulting tag of the failed command
1437  *	@tf: Resulting taskfile registers of the failed command
1438  *
1439  *	Read log page 10h to obtain NCQ error details and clear error
1440  *	condition.
1441  *
1442  *	LOCKING:
1443  *	Kernel thread context (may sleep).
1444  *
1445  *	RETURNS:
1446  *	0 on success, -errno otherwise.
1447  */
ata_eh_read_log_10h(struct ata_device * dev,int * tag,struct ata_taskfile * tf)1448 static int ata_eh_read_log_10h(struct ata_device *dev,
1449 			       int *tag, struct ata_taskfile *tf)
1450 {
1451 	u8 *buf = dev->sector_buf;
1452 	unsigned int err_mask;
1453 	u8 csum;
1454 	int i;
1455 
1456 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1457 	if (err_mask)
1458 		return -EIO;
1459 
1460 	csum = 0;
1461 	for (i = 0; i < ATA_SECT_SIZE; i++)
1462 		csum += buf[i];
1463 	if (csum)
1464 		ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1465 			     csum);
1466 
1467 	if (buf[0] & 0x80)
1468 		return -ENOENT;
1469 
1470 	*tag = buf[0] & 0x1f;
1471 
1472 	tf->status = buf[2];
1473 	tf->error = buf[3];
1474 	tf->lbal = buf[4];
1475 	tf->lbam = buf[5];
1476 	tf->lbah = buf[6];
1477 	tf->device = buf[7];
1478 	tf->hob_lbal = buf[8];
1479 	tf->hob_lbam = buf[9];
1480 	tf->hob_lbah = buf[10];
1481 	tf->nsect = buf[12];
1482 	tf->hob_nsect = buf[13];
1483 	if (ata_id_has_ncq_autosense(dev->id) && (tf->status & ATA_SENSE))
1484 		tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1485 
1486 	return 0;
1487 }
1488 
1489 /**
1490  *	ata_eh_get_ncq_success_sense - Read and process the sense data for
1491  *				       successful NCQ commands log page
1492  *	@link: ATA link to get sense data for
1493  *
1494  *	Read the sense data for successful NCQ commands log page to obtain
1495  *	sense data for all NCQ commands that completed successfully with
1496  *	the sense data available bit set.
1497  *
1498  *	LOCKING:
1499  *	Kernel thread context (may sleep).
1500  *
1501  *	RETURNS:
1502  *	0 on success, -errno otherwise.
1503  */
ata_eh_get_ncq_success_sense(struct ata_link * link)1504 int ata_eh_get_ncq_success_sense(struct ata_link *link)
1505 {
1506 	struct ata_device *dev = link->device;
1507 	struct ata_port *ap = dev->link->ap;
1508 	u8 *buf = dev->cdl->ncq_sense_log_buf;
1509 	struct ata_queued_cmd *qc;
1510 	unsigned int err_mask, tag;
1511 	u8 *sense, sk = 0, asc = 0, ascq = 0;
1512 	u64 sense_valid, val;
1513 	int ret = 0;
1514 
1515 	err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2);
1516 	if (err_mask) {
1517 		ata_dev_err(dev,
1518 			"Failed to read Sense Data for Successful NCQ Commands log\n");
1519 		return -EIO;
1520 	}
1521 
1522 	/* Check the log header */
1523 	val = get_unaligned_le64(&buf[0]);
1524 	if ((val & 0xffff) != 1 || ((val >> 16) & 0xff) != 0x0f) {
1525 		ata_dev_err(dev,
1526 			"Invalid Sense Data for Successful NCQ Commands log\n");
1527 		return -EIO;
1528 	}
1529 
1530 	sense_valid = (u64)buf[8] | ((u64)buf[9] << 8) |
1531 		((u64)buf[10] << 16) | ((u64)buf[11] << 24);
1532 
1533 	ata_qc_for_each_raw(ap, qc, tag) {
1534 		if (!(qc->flags & ATA_QCFLAG_EH) ||
1535 		    !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) ||
1536 		    qc->err_mask ||
1537 		    ata_dev_phys_link(qc->dev) != link)
1538 			continue;
1539 
1540 		/*
1541 		 * If the command does not have any sense data, clear ATA_SENSE.
1542 		 * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished.
1543 		 */
1544 		if (!(sense_valid & (1ULL << tag))) {
1545 			qc->result_tf.status &= ~ATA_SENSE;
1546 			continue;
1547 		}
1548 
1549 		sense = &buf[32 + 24 * tag];
1550 		sk = sense[0];
1551 		asc = sense[1];
1552 		ascq = sense[2];
1553 
1554 		if (!ata_scsi_sense_is_valid(sk, asc, ascq)) {
1555 			ret = -EIO;
1556 			continue;
1557 		}
1558 
1559 		/* Set sense without also setting scsicmd->result */
1560 		scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE,
1561 					qc->scsicmd->sense_buffer, sk,
1562 					asc, ascq);
1563 		qc->flags |= ATA_QCFLAG_SENSE_VALID;
1564 
1565 		/*
1566 		 * No point in checking the return value, since the command has
1567 		 * already completed successfully.
1568 		 */
1569 		ata_eh_decide_disposition(qc);
1570 	}
1571 
1572 	return ret;
1573 }
1574 
1575 /**
1576  *	ata_eh_analyze_ncq_error - analyze NCQ error
1577  *	@link: ATA link to analyze NCQ error for
1578  *
1579  *	Read log page 10h, determine the offending qc and acquire
1580  *	error status TF.  For NCQ device errors, all LLDDs have to do
1581  *	is setting AC_ERR_DEV in ehi->err_mask.  This function takes
1582  *	care of the rest.
1583  *
1584  *	LOCKING:
1585  *	Kernel thread context (may sleep).
1586  */
ata_eh_analyze_ncq_error(struct ata_link * link)1587 void ata_eh_analyze_ncq_error(struct ata_link *link)
1588 {
1589 	struct ata_port *ap = link->ap;
1590 	struct ata_eh_context *ehc = &link->eh_context;
1591 	struct ata_device *dev = link->device;
1592 	struct ata_queued_cmd *qc;
1593 	struct ata_taskfile tf;
1594 	int tag, rc;
1595 
1596 	/* if frozen, we can't do much */
1597 	if (ata_port_is_frozen(ap))
1598 		return;
1599 
1600 	/* is it NCQ device error? */
1601 	if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1602 		return;
1603 
1604 	/* has LLDD analyzed already? */
1605 	ata_qc_for_each_raw(ap, qc, tag) {
1606 		if (!(qc->flags & ATA_QCFLAG_EH))
1607 			continue;
1608 
1609 		if (qc->err_mask)
1610 			return;
1611 	}
1612 
1613 	/* okay, this error is ours */
1614 	memset(&tf, 0, sizeof(tf));
1615 	rc = ata_eh_read_log_10h(dev, &tag, &tf);
1616 	if (rc) {
1617 		ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1618 			     rc);
1619 		return;
1620 	}
1621 
1622 	if (!(link->sactive & (1 << tag))) {
1623 		ata_link_err(link, "log page 10h reported inactive tag %d\n",
1624 			     tag);
1625 		return;
1626 	}
1627 
1628 	/* we've got the perpetrator, condemn it */
1629 	qc = __ata_qc_from_tag(ap, tag);
1630 	memcpy(&qc->result_tf, &tf, sizeof(tf));
1631 	qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1632 	qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1633 
1634 	/*
1635 	 * If the device supports NCQ autosense, ata_eh_read_log_10h() will have
1636 	 * stored the sense data in qc->result_tf.auxiliary.
1637 	 */
1638 	if (qc->result_tf.auxiliary) {
1639 		char sense_key, asc, ascq;
1640 
1641 		sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1642 		asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1643 		ascq = qc->result_tf.auxiliary & 0xff;
1644 		if (ata_scsi_sense_is_valid(sense_key, asc, ascq)) {
1645 			ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc,
1646 					   ascq);
1647 			ata_scsi_set_sense_information(dev, qc->scsicmd,
1648 						       &qc->result_tf);
1649 			qc->flags |= ATA_QCFLAG_SENSE_VALID;
1650 		}
1651 	}
1652 
1653 	ata_qc_for_each_raw(ap, qc, tag) {
1654 		if (!(qc->flags & ATA_QCFLAG_EH) ||
1655 		    qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD ||
1656 		    ata_dev_phys_link(qc->dev) != link)
1657 			continue;
1658 
1659 		/* Skip the single QC which caused the NCQ error. */
1660 		if (qc->err_mask)
1661 			continue;
1662 
1663 		/*
1664 		 * For SATA, the STATUS and ERROR fields are shared for all NCQ
1665 		 * commands that were completed with the same SDB FIS.
1666 		 * Therefore, we have to clear the ATA_ERR bit for all QCs
1667 		 * except the one that caused the NCQ error.
1668 		 */
1669 		qc->result_tf.status &= ~ATA_ERR;
1670 		qc->result_tf.error = 0;
1671 
1672 		/*
1673 		 * If we get a NCQ error, that means that a single command was
1674 		 * aborted. All other failed commands for our link should be
1675 		 * retried and has no business of going though further scrutiny
1676 		 * by ata_eh_link_autopsy().
1677 		 */
1678 		qc->flags |= ATA_QCFLAG_RETRY;
1679 	}
1680 
1681 	ehc->i.err_mask &= ~AC_ERR_DEV;
1682 }
1683 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
1684 
1685 const struct ata_port_operations sata_port_ops = {
1686 	.inherits		= &ata_base_port_ops,
1687 
1688 	.qc_defer		= ata_std_qc_defer,
1689 	.hardreset		= sata_std_hardreset,
1690 };
1691 EXPORT_SYMBOL_GPL(sata_port_ops);
1692