Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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