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-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *  hosts.c Copyright (C) 1992 Drew Eckhardt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *          Copyright (C) 1993, 1994, 1995 Eric Youngdale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *          Copyright (C) 2002-2003 Christoph Hellwig
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *  mid to lowlevel SCSI driver interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *      Initial versions: Drew Eckhardt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *      Subsequent revisions: Eric Youngdale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  *  <drew@colorado.edu>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  *  Jiffies wrap fixes (host->resetting), 3 Dec 1998 Andrea Arcangeli
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  *  Added QLOGIC QLA1280 SCSI controller kernel host support. 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  *     August 4, 1999 Fred Lewis, Intel DuPont
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  *  Updated to reflect the new initialization scheme for the higher 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  *  level of scsi drivers (sd/sr/st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  *  September 17, 2000 Torben Mathiasen <tmm@image.dk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  *  Restructured scsi_host lists and associated functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  *  September 04, 2002 Mike Anderson (andmike@us.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/transport_class.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include <scsi/scsi_transport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <scsi/scsi_cmnd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #include "scsi_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) #include "scsi_logging.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) static int shost_eh_deadline = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) module_param_named(eh_deadline, shost_eh_deadline, int, S_IRUGO|S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) MODULE_PARM_DESC(eh_deadline,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 		 "SCSI EH timeout in seconds (should be between 0 and 2^31-1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) static DEFINE_IDA(host_index_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) static void scsi_host_cls_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	put_device(&class_to_shost(dev)->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static struct class shost_class = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	.name		= "scsi_host",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	.dev_release	= scsi_host_cls_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  *	scsi_host_set_state - Take the given host through the host state model.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *	@shost:	scsi host to change the state of.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  *	@state:	state to change to.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  *	Returns zero if unsuccessful or an error if the requested
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  *	transition is illegal.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) int scsi_host_set_state(struct Scsi_Host *shost, enum scsi_host_state state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	enum scsi_host_state oldstate = shost->shost_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (state == oldstate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	case SHOST_CREATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		/* There are no legal states that come back to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		 * created.  This is the manually initialised start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		 * state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		goto illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	case SHOST_RUNNING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		switch (oldstate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		case SHOST_CREATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		case SHOST_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			goto illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	case SHOST_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		switch (oldstate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		case SHOST_RUNNING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 			goto illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	case SHOST_CANCEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		switch (oldstate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 		case SHOST_CREATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		case SHOST_RUNNING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		case SHOST_CANCEL_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 			goto illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	case SHOST_DEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		switch (oldstate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		case SHOST_CANCEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		case SHOST_DEL_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			goto illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	case SHOST_CANCEL_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		switch (oldstate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		case SHOST_CANCEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		case SHOST_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			goto illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	case SHOST_DEL_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		switch (oldstate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		case SHOST_CANCEL_RECOVERY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			goto illegal;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	shost->shost_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)  illegal:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	SCSI_LOG_ERROR_RECOVERY(1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				shost_printk(KERN_ERR, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 					     "Illegal host state transition"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 					     "%s->%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 					     scsi_host_state_name(oldstate),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 					     scsi_host_state_name(state)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * scsi_remove_host - remove a scsi host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * @shost:	a pointer to a scsi host to remove
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) void scsi_remove_host(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 	mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (scsi_host_set_state(shost, SHOST_CANCEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 			spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 			mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	scsi_autopm_get_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	flush_workqueue(shost->tmf_work_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	scsi_forget_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	scsi_proc_host_rm(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (scsi_host_set_state(shost, SHOST_DEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		BUG_ON(scsi_host_set_state(shost, SHOST_DEL_RECOVERY));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	transport_unregister_device(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	device_unregister(&shost->shost_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	device_del(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) EXPORT_SYMBOL(scsi_remove_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * scsi_add_host_with_dma - add a scsi host with dma device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  * @shost:	scsi host pointer to add
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * @dev:	a struct device of type scsi class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  * @dma_dev:	dma device for the host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  * Note: You rarely need to worry about this unless you're in a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)  * virtualised host environments, so use the simpler scsi_add_host()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)  * function instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)  * Return value: 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)  * 	0 on success / != 0 for error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) int scsi_add_host_with_dma(struct Scsi_Host *shost, struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 			   struct device *dma_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	struct scsi_host_template *sht = shost->hostt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	int error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	shost_printk(KERN_INFO, shost, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 			sht->info ? sht->info(shost) : sht->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	if (!shost->can_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 		shost_printk(KERN_ERR, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 			     "can_queue = 0 no longer supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	/* Use min_t(int, ...) in case shost->can_queue exceeds SHRT_MAX */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	shost->cmd_per_lun = min_t(int, shost->cmd_per_lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 				   shost->can_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	error = scsi_init_sense_cache(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	error = scsi_mq_setup_tags(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (!shost->shost_gendev.parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		shost->shost_gendev.parent = dev ? dev : &platform_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (!dma_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		dma_dev = shost->shost_gendev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	shost->dma_dev = dma_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 * Increase usage count temporarily here so that calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 * scsi_autopm_put_host() will trigger runtime idle if there is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	 * nothing else preventing suspending the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	pm_runtime_get_noresume(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	pm_runtime_set_active(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	pm_runtime_enable(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	device_enable_async_suspend(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	error = device_add(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		goto out_disable_runtime_pm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	scsi_host_set_state(shost, SHOST_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	get_device(shost->shost_gendev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	device_enable_async_suspend(&shost->shost_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	get_device(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 	error = device_add(&shost->shost_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		goto out_del_gendev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (shost->transportt->host_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		shost->shost_data = kzalloc(shost->transportt->host_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 					 GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		if (shost->shost_data == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 			goto out_del_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	if (shost->transportt->create_work_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		snprintf(shost->work_q_name, sizeof(shost->work_q_name),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 			 "scsi_wq_%d", shost->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		shost->work_q = alloc_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			WQ_SYSFS | __WQ_LEGACY | WQ_MEM_RECLAIM | WQ_UNBOUND,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 			1, shost->work_q_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		if (!shost->work_q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 			goto out_del_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	error = scsi_sysfs_add_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		goto out_del_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	scsi_proc_host_add(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	scsi_autopm_put_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	 * Any host allocation in this function will be freed in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	 * scsi_host_dev_release().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  out_del_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	device_del(&shost->shost_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  out_del_gendev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	 * Host state is SHOST_RUNNING so we have to explicitly release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	 * ->shost_dev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	put_device(&shost->shost_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	device_del(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  out_disable_runtime_pm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	device_disable_async_suspend(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	pm_runtime_disable(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	pm_runtime_set_suspended(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	pm_runtime_put_noidle(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)  fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) EXPORT_SYMBOL(scsi_add_host_with_dma);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) static void scsi_host_dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	struct Scsi_Host *shost = dev_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	struct device *parent = dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	scsi_proc_hostdir_rm(shost->hostt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	/* Wait for functions invoked through call_rcu(&shost->rcu, ...) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	if (shost->tmf_work_q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 		destroy_workqueue(shost->tmf_work_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	if (shost->ehandler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		kthread_stop(shost->ehandler);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (shost->work_q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		destroy_workqueue(shost->work_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (shost->shost_state == SHOST_CREATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		 * Free the shost_dev device name here if scsi_host_alloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		 * and scsi_host_put() have been called but neither
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		 * scsi_host_add() nor scsi_host_remove() has been called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		 * This avoids that the memory allocated for the shost_dev
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		 * name is leaked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 		kfree(dev_name(&shost->shost_dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	if (shost->tag_set.tags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 		scsi_mq_destroy_tags(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	kfree(shost->shost_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	ida_simple_remove(&host_index_ida, shost->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	if (shost->shost_state != SHOST_CREATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 		put_device(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	kfree(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) static struct device_type scsi_host_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	.name =		"scsi_host",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	.release =	scsi_host_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)  * scsi_host_alloc - register a scsi host adapter instance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)  * @sht:	pointer to scsi host template
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * @privsize:	extra bytes to allocate for driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  * Note:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  * 	Allocate a new Scsi_Host and perform basic initialization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  * 	The host is not published to the scsi midlayer until scsi_add_host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * 	is called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  * 	Pointer to a new Scsi_Host
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) struct Scsi_Host *scsi_host_alloc(struct scsi_host_template *sht, int privsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct Scsi_Host *shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	gfp_t gfp_mask = GFP_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	if (sht->unchecked_isa_dma && privsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 		gfp_mask |= __GFP_DMA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	shost = kzalloc(sizeof(struct Scsi_Host) + privsize, gfp_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (!shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	shost->host_lock = &shost->default_lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	spin_lock_init(shost->host_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	shost->shost_state = SHOST_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	INIT_LIST_HEAD(&shost->__devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	INIT_LIST_HEAD(&shost->__targets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	INIT_LIST_HEAD(&shost->eh_cmd_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	INIT_LIST_HEAD(&shost->starved_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	init_waitqueue_head(&shost->host_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	mutex_init(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	index = ida_simple_get(&host_index_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	if (index < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		kfree(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	shost->host_no = index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	shost->dma_channel = 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	/* These three are default values which can be overridden */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	shost->max_channel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	shost->max_id = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	shost->max_lun = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	/* Give each shost a default transportt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	shost->transportt = &blank_transport_template;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	 * All drivers right now should be able to handle 12 byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	 * commands.  Every so often there are requests for 16 byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	 * commands, but individual low-level drivers need to certify that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	 * they actually do something sensible with such commands.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	shost->max_cmd_len = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	shost->hostt = sht;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	shost->this_id = sht->this_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	shost->can_queue = sht->can_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	shost->sg_tablesize = sht->sg_tablesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	shost->sg_prot_tablesize = sht->sg_prot_tablesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	shost->cmd_per_lun = sht->cmd_per_lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	shost->unchecked_isa_dma = sht->unchecked_isa_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	shost->no_write_same = sht->no_write_same;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	shost->host_tagset = sht->host_tagset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	if (shost_eh_deadline == -1 || !sht->eh_host_reset_handler)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 		shost->eh_deadline = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	else if ((ulong) shost_eh_deadline * HZ > INT_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		shost_printk(KERN_WARNING, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 			     "eh_deadline %u too large, setting to %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 			     shost_eh_deadline, INT_MAX / HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		shost->eh_deadline = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		shost->eh_deadline = shost_eh_deadline * HZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	if (sht->supported_mode == MODE_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 		/* means we didn't set it ... default to INITIATOR */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		shost->active_mode = MODE_INITIATOR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		shost->active_mode = sht->supported_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (sht->max_host_blocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		shost->max_host_blocked = sht->max_host_blocked;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		shost->max_host_blocked = SCSI_DEFAULT_HOST_BLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	 * If the driver imposes no hard sector transfer limit, start at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	 * machine infinity initially.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (sht->max_sectors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		shost->max_sectors = sht->max_sectors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		shost->max_sectors = SCSI_DEFAULT_MAX_SECTORS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (sht->max_segment_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		shost->max_segment_size = sht->max_segment_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		shost->max_segment_size = BLK_MAX_SEGMENT_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	 * assume a 4GB boundary, if not set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	if (sht->dma_boundary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		shost->dma_boundary = sht->dma_boundary;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		shost->dma_boundary = 0xffffffff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	if (sht->virt_boundary_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		shost->virt_boundary_mask = sht->virt_boundary_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	device_initialize(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	dev_set_name(&shost->shost_gendev, "host%d", shost->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	shost->shost_gendev.bus = &scsi_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	shost->shost_gendev.type = &scsi_host_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	device_initialize(&shost->shost_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	shost->shost_dev.parent = &shost->shost_gendev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	shost->shost_dev.class = &shost_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	dev_set_name(&shost->shost_dev, "host%d", shost->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 	shost->shost_dev.groups = scsi_sysfs_shost_attr_groups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	shost->ehandler = kthread_run(scsi_error_handler, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 			"scsi_eh_%d", shost->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	if (IS_ERR(shost->ehandler)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		shost_printk(KERN_WARNING, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 			"error handler thread failed to spawn, error = %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 			PTR_ERR(shost->ehandler));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 		shost->ehandler = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	shost->tmf_work_q = alloc_workqueue("scsi_tmf_%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 					WQ_UNBOUND | WQ_MEM_RECLAIM | WQ_SYSFS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 					   1, shost->host_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	if (!shost->tmf_work_q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 		shost_printk(KERN_WARNING, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			     "failed to create tmf workq\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	scsi_proc_hostdir_add(shost->hostt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	return shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	 * Host state is still SHOST_CREATED and that is enough to release
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	 * ->shost_gendev. scsi_host_dev_release() will free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	 * dev_name(&shost->shost_dev).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	put_device(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) EXPORT_SYMBOL(scsi_host_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static int __scsi_host_match(struct device *dev, const void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	struct Scsi_Host *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	const unsigned short *hostnum = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	p = class_to_shost(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	return p->host_no == *hostnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)  * scsi_host_lookup - get a reference to a Scsi_Host by host no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  * @hostnum:	host number to locate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537)  *	A pointer to located Scsi_Host or NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)  *	The caller must do a scsi_host_put() to drop the reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)  *	that scsi_host_get() took. The put_device() below dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)  *	the reference from class_find_device().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) struct Scsi_Host *scsi_host_lookup(unsigned short hostnum)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	struct device *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	struct Scsi_Host *shost = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	cdev = class_find_device(&shost_class, NULL, &hostnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 				 __scsi_host_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 	if (cdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 		shost = scsi_host_get(class_to_shost(cdev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		put_device(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	return shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) EXPORT_SYMBOL(scsi_host_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)  * scsi_host_get - inc a Scsi_Host ref count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)  * @shost:	Pointer to Scsi_Host to inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) struct Scsi_Host *scsi_host_get(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	if ((shost->shost_state == SHOST_DEL) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		!get_device(&shost->shost_gendev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	return shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) EXPORT_SYMBOL(scsi_host_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) static bool scsi_host_check_in_flight(struct request *rq, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 				      bool reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	int *count = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	if (test_bit(SCMD_STATE_INFLIGHT, &cmd->state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		(*count)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)  * scsi_host_busy - Return the host busy counter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)  * @shost:	Pointer to Scsi_Host to inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) int scsi_host_busy(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	int cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	blk_mq_tagset_busy_iter(&shost->tag_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 				scsi_host_check_in_flight, &cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) EXPORT_SYMBOL(scsi_host_busy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598)  * scsi_host_put - dec a Scsi_Host ref count
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)  * @shost:	Pointer to Scsi_Host to dec.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) void scsi_host_put(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	put_device(&shost->shost_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) EXPORT_SYMBOL(scsi_host_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) int scsi_init_hosts(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	return class_register(&shost_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) void scsi_exit_hosts(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	class_unregister(&shost_class);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	ida_destroy(&host_index_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) int scsi_is_host_device(const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 	return dev->type == &scsi_host_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) EXPORT_SYMBOL(scsi_is_host_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625)  * scsi_queue_work - Queue work to the Scsi_Host workqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)  * @shost:	Pointer to Scsi_Host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)  * @work:	Work to queue for execution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * 	1 - work queued for execution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  *	0 - work is already queued
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  *	-EINVAL - work queue doesn't exist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) int scsi_queue_work(struct Scsi_Host *shost, struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	if (unlikely(!shost->work_q)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		shost_printk(KERN_ERR, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			"ERROR: Scsi host '%s' attempted to queue scsi-work, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 			"when no workqueue created.\n", shost->hostt->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	return queue_work(shost->work_q, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) EXPORT_SYMBOL_GPL(scsi_queue_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)  * scsi_flush_work - Flush a Scsi_Host's workqueue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)  * @shost:	Pointer to Scsi_Host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) void scsi_flush_work(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	if (!shost->work_q) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 		shost_printk(KERN_ERR, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 			"ERROR: Scsi host '%s' attempted to flush scsi-work, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 			"when no workqueue created.\n", shost->hostt->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	flush_workqueue(shost->work_q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) EXPORT_SYMBOL_GPL(scsi_flush_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) static bool complete_all_cmds_iter(struct request *rq, void *data, bool rsvd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	int status = *(int *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	scsi_dma_unmap(scmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	scmd->result = status << 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 	scmd->scsi_done(scmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	return true;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679)  * scsi_host_complete_all_commands - Terminate all running commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680)  * @shost:	Scsi Host on which commands should be terminated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681)  * @status:	Status to be set for the terminated commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)  * There is no protection against modification of the number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684)  * of outstanding commands. It is the responsibility of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685)  * caller to ensure that concurrent I/O submission and/or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)  * completion is stopped when calling this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) void scsi_host_complete_all_commands(struct Scsi_Host *shost, int status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	blk_mq_tagset_busy_iter(&shost->tag_set, complete_all_cmds_iter,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 				&status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) EXPORT_SYMBOL_GPL(scsi_host_complete_all_commands);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) struct scsi_host_busy_iter_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	bool (*fn)(struct scsi_cmnd *, void *, bool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	void *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) static bool __scsi_host_busy_iter_fn(struct request *req, void *priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 				   bool reserved)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	struct scsi_host_busy_iter_data *iter_data = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	struct scsi_cmnd *sc = blk_mq_rq_to_pdu(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	return iter_data->fn(sc, iter_data->priv, reserved);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)  * scsi_host_busy_iter - Iterate over all busy commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711)  * @shost:	Pointer to Scsi_Host.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712)  * @fn:		Function to call on each busy command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)  * @priv:	Data pointer passed to @fn
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715)  * If locking against concurrent command completions is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)  * ithas to be provided by the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) void scsi_host_busy_iter(struct Scsi_Host *shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 			 bool (*fn)(struct scsi_cmnd *, void *, bool),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 			 void *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	struct scsi_host_busy_iter_data iter_data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 		.fn = fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 		.priv = priv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 	blk_mq_tagset_busy_iter(&shost->tag_set, __scsi_host_busy_iter_fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 				&iter_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) EXPORT_SYMBOL_GPL(scsi_host_busy_iter);