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