Linux Audio

Check our new training course

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