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
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * scsi_scan.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 2000 Eric Youngdale,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 2002 Patrick Mansfield
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  * The general scanning/probing algorithm is as follows, exceptions are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * made to it depending on device specific flags, compilation options, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * global variable (boot or module load time) settings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * A specific LUN is scanned via an INQUIRY command; if the LUN has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * device attached, a scsi_device is allocated and setup for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  * For every id of every channel on the given host:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * 	Scan LUN 0; if the target responds to LUN 0 (even if there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  * 	device or storage attached to LUN 0):
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * 		If LUN 0 has a device attached, allocate and setup a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * 		scsi_device for it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * 		If target is SCSI-3 or up, issue a REPORT LUN, and scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * 		all of the LUNs returned by the REPORT LUN; else,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * 		sequentially scan LUNs up until some maximum is reached,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * 		or a LUN is seen that cannot have a device attached to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/blkdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <asm/unaligned.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <scsi/scsi.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) #include <scsi/scsi_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #include <scsi/scsi_driver.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #include <scsi/scsi_devinfo.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #include <scsi/scsi_host.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #include <scsi/scsi_transport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #include <scsi/scsi_dh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #include <scsi/scsi_eh.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) #include "scsi_priv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #include "scsi_logging.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) #define ALLOC_FAILURE_MSG	KERN_ERR "%s: Allocation failure during" \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	" SCSI scanning, some SCSI devices might not be configured\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57)  * Default timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define SCSI_TIMEOUT (2*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) #define SCSI_REPORT_LUNS_TIMEOUT (30*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63)  * Prefix values for the SCSI id's (stored in sysfs name field)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define SCSI_UID_SER_NUM 'S'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define SCSI_UID_UNKNOWN 'Z'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69)  * Return values of some of the scanning functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  * includes allocation or general failures preventing IO from being sent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75)  * on the given LUN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * given LUN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define SCSI_SCAN_NO_RESPONSE		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define SCSI_SCAN_TARGET_PRESENT	1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) #define SCSI_SCAN_LUN_PRESENT		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static const char *scsi_null_device_strs = "nullnullnullnull";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) #define MAX_SCSI_LUNS	512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) static u64 max_scsi_luns = MAX_SCSI_LUNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) module_param_named(max_luns, max_scsi_luns, ullong, S_IRUGO|S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) MODULE_PARM_DESC(max_luns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 		 "last scsi LUN (should be between 1 and 2^64-1)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) #ifdef CONFIG_SCSI_SCAN_ASYNC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define SCSI_SCAN_TYPE_DEFAULT "async"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) #define SCSI_SCAN_TYPE_DEFAULT "sync"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) char scsi_scan_type[7] = SCSI_SCAN_TYPE_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		    S_IRUGO|S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) MODULE_PARM_DESC(scan, "sync, async, manual, or none. "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 		 "Setting to 'manual' disables automatic scanning, but allows "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 		 "for manual device scan via the 'scan' sysfs attribute.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ + 18;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) MODULE_PARM_DESC(inq_timeout, 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 		 "Timeout (in seconds) waiting for devices to answer INQUIRY."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		 " Default is 20. Some devices may need more; most need less.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) /* This lock protects only this list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) static DEFINE_SPINLOCK(async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) static LIST_HEAD(scanning_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) struct async_scan_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	struct Scsi_Host *shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	struct completion prev_finished;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126)  * scsi_complete_async_scans - Wait for asynchronous scans to complete
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128)  * When this function returns, any host which started scanning before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  * this function was called will have finished its scan.  Hosts which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  * started scanning after this function was called may or may not have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131)  * finished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) int scsi_complete_async_scans(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	struct async_scan_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		if (list_empty(&scanning_hosts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		/* If we can't get memory immediately, that's OK.  Just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		 * sleep a little.  Even if we never get memory, the async
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		 * scans will finish eventually.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		data = kmalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 		if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 			msleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	} while (!data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	data->shost = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	init_completion(&data->prev_finished);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	spin_lock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	/* Check that there's still somebody else on the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	if (list_empty(&scanning_hosts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	list_add_tail(&data->list, &scanning_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	spin_unlock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 	printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	wait_for_completion(&data->prev_finished);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	spin_lock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	list_del(&data->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (!list_empty(&scanning_hosts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		struct async_scan_data *next = list_entry(scanning_hosts.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 				struct async_scan_data, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 		complete(&next->prev_finished);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	spin_unlock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177)  * scsi_unlock_floptical - unlock device via a special MODE SENSE command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178)  * @sdev:	scsi device to send command to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179)  * @result:	area to store the result of the MODE SENSE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  *     Send a vendor specific MODE SENSE (not a MODE SELECT) command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183)  *     Called for BLIST_KEY devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) static void scsi_unlock_floptical(struct scsi_device *sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 				  unsigned char *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	sdev_printk(KERN_NOTICE, sdev, "unlocking floptical drive\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	scsi_cmd[0] = MODE_SENSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	scsi_cmd[1] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	scsi_cmd[2] = 0x2e;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	scsi_cmd[3] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	scsi_cmd[4] = 0x2a;     /* size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	scsi_cmd[5] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 			 SCSI_TIMEOUT, 3, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202)  * scsi_alloc_sdev - allocate and setup a scsi_Device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203)  * @starget: which target to allocate a &scsi_device for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204)  * @lun: which lun
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205)  * @hostdata: usually NULL and set by ->slave_alloc instead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208)  *     Allocate, initialize for io, and return a pointer to a scsi_Device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209)  *     Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  *     adds scsi_Device to the appropriate list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  * Return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  *     scsi_Device pointer, or NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 					   u64 lun, void *hostdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	int display_failure_msg = 1, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	if (!sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	sdev->vendor = scsi_null_device_strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	sdev->model = scsi_null_device_strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	sdev->rev = scsi_null_device_strs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	sdev->host = shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	sdev->id = starget->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	sdev->lun = lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	sdev->channel = starget->channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	mutex_init(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	sdev->sdev_state = SDEV_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	INIT_LIST_HEAD(&sdev->siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	INIT_LIST_HEAD(&sdev->same_target_siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	INIT_LIST_HEAD(&sdev->starved_entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	INIT_LIST_HEAD(&sdev->event_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	spin_lock_init(&sdev->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	mutex_init(&sdev->inquiry_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	INIT_WORK(&sdev->event_work, scsi_evt_thread);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	sdev->sdev_gendev.parent = get_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	sdev->sdev_target = starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	/* usually NULL and set by ->slave_alloc instead */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	sdev->hostdata = hostdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	/* if the device needs this changing, it may do so in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	 * slave_configure function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	 * Some low level driver could use device->type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	sdev->type = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	 * Assume that the device will have handshaking problems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	 * and then fix this field later if it turns out it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 	 * doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	sdev->borken = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	sdev->request_queue = scsi_mq_alloc_queue(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	if (!sdev->request_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		/* release fn is set up in scsi_sysfs_device_initialise, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		 * have to free and put manually here */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		put_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		kfree(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	WARN_ON_ONCE(!blk_get_queue(sdev->request_queue));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 	sdev->request_queue->queuedata = sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	scsi_change_queue_depth(sdev, sdev->host->cmd_per_lun ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 					sdev->host->cmd_per_lun : 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	scsi_sysfs_device_initialize(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	if (shost->hostt->slave_alloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		ret = shost->hostt->slave_alloc(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 			 * if LLDD reports slave not present, don't clutter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 			 * console with alloc failure messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 			if (ret == -ENXIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 				display_failure_msg = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 			goto out_device_destroy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 		}
^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) 	return sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) out_device_destroy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (display_failure_msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		printk(ALLOC_FAILURE_MSG, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	return NULL;
^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) static void scsi_target_destroy(struct scsi_target *starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	struct device *dev = &starget->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	struct Scsi_Host *shost = dev_to_shost(dev->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	BUG_ON(starget->state == STARGET_DEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	starget->state = STARGET_DEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	transport_destroy_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	if (shost->hostt->target_destroy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		shost->hostt->target_destroy(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	list_del_init(&starget->siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 	put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) static void scsi_target_dev_release(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	struct device *parent = dev->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	struct scsi_target *starget = to_scsi_target(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	kfree(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	put_device(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) static struct device_type scsi_target_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	.name =		"scsi_target",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	.release =	scsi_target_dev_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) int scsi_is_target_device(const struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	return dev->type == &scsi_target_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) EXPORT_SYMBOL(scsi_is_target_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) static struct scsi_target *__scsi_find_target(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 					      int channel, uint id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	struct scsi_target *starget, *found_starget = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	struct Scsi_Host *shost = dev_to_shost(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	 * Search for an existing target for this sdev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 	list_for_each_entry(starget, &shost->__targets, siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		if (starget->id == id &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		    starget->channel == channel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 			found_starget = starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			break;
^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) 	if (found_starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		get_device(&found_starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	return found_starget;
^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) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366)  * scsi_target_reap_ref_release - remove target from visibility
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367)  * @kref: the reap_ref in the target being released
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369)  * Called on last put of reap_ref, which is the indication that no device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370)  * under this target is visible anymore, so render the target invisible in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371)  * sysfs.  Note: we have to be in user context here because the target reaps
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372)  * should be done in places where the scsi device visibility is being removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) static void scsi_target_reap_ref_release(struct kref *kref)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct scsi_target *starget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		= container_of(kref, struct scsi_target, reap_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	 * if we get here and the target is still in a CREATED state that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	 * means it was allocated but never made visible (because a scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	 * turned up no LUNs), so don't call device_del() on it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if ((starget->state != STARGET_CREATED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	    (starget->state != STARGET_CREATED_REMOVE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 		transport_remove_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		device_del(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	scsi_target_destroy(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) static void scsi_target_reap_ref_put(struct scsi_target *starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	kref_put(&starget->reap_ref, scsi_target_reap_ref_release);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398)  * scsi_alloc_target - allocate a new or find an existing target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399)  * @parent:	parent of the target (need not be a scsi host)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400)  * @channel:	target channel number (zero if no channels)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401)  * @id:		target id number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403)  * Return an existing target if one exists, provided it hasn't already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404)  * gone into STARGET_DEL state, otherwise allocate a new target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406)  * The target is returned with an incremented reference, so the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407)  * is responsible for both reaping and doing a last put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) static struct scsi_target *scsi_alloc_target(struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 					     int channel, uint id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	struct Scsi_Host *shost = dev_to_shost(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	struct device *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	const int size = sizeof(struct scsi_target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		+ shost->transportt->target_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	struct scsi_target *starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	struct scsi_target *found_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	int error, ref_got;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	starget = kzalloc(size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	if (!starget) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		printk(KERN_ERR "%s: allocation failure\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	dev = &starget->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	device_initialize(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	kref_init(&starget->reap_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	dev->parent = get_device(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	dev->bus = &scsi_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 	dev->type = &scsi_target_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 	starget->id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	starget->channel = channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	starget->can_queue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	INIT_LIST_HEAD(&starget->siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	INIT_LIST_HEAD(&starget->devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	starget->state = STARGET_CREATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	starget->scsi_level = SCSI_2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	found_target = __scsi_find_target(parent, channel, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	if (found_target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		goto found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	list_add_tail(&starget->siblings, &shost->__targets);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	/* allocate and add */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	transport_setup_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	if (shost->hostt->target_alloc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		error = shost->hostt->target_alloc(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		if(error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			if (error != -ENXIO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 				dev_err(dev, "target allocation failed, error %d\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 			/* don't want scsi_target_reap to do the final
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 			 * put because it will be under the host lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 			scsi_target_destroy(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	get_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	return starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468)  found:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	 * release routine already fired if kref is zero, so if we can still
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	 * take the reference, the target must be alive.  If we can't, it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	 * be dying and we need to wait for a new target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	ref_got = kref_get_unless_zero(&found_target->reap_ref);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	if (ref_got) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		put_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		return found_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	 * Unfortunately, we found a dying target; need to wait until it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	 * dead before we can get a new one.  There is an anomaly here.  We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 	 * *should* call scsi_target_reap() to balance the kref_get() of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	 * reap_ref above.  However, since the target being released, it's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	 * already invisible and the reap_ref is irrelevant.  If we call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	 * scsi_target_reap() we might spuriously do another device_del() on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	 * an already invisible target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	put_device(&found_target->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	 * length of time is irrelevant here, we just want to yield the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	 * for a tick to avoid busy waiting for the target to die.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	msleep(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500)  * scsi_target_reap - check to see if target is in use and destroy if not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501)  * @starget: target to be checked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503)  * This is used after removing a LUN or doing a last put of the target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504)  * it checks atomically that nothing is using the target and removes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505)  * it if so.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) void scsi_target_reap(struct scsi_target *starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 	 * serious problem if this triggers: STARGET_DEL is only set in the if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	 * the reap_ref drops to zero, so we're trying to do another final put
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	 * on an already released kref
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	BUG_ON(starget->state == STARGET_DEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	scsi_target_reap_ref_put(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  * scsi_sanitize_inquiry_string - remove non-graphical chars from an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520)  *                                INQUIRY result string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521)  * @s: INQUIRY result string to sanitize
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522)  * @len: length of the string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525)  *	The SCSI spec says that INQUIRY vendor, product, and revision
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526)  *	strings must consist entirely of graphic ASCII characters,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527)  *	padded on the right with spaces.  Since not all devices obey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528)  *	this rule, we will replace non-graphic or non-ASCII characters
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529)  *	with spaces.  Exception: a NUL character is interpreted as a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530)  *	string terminator, so all the following characters are set to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531)  *	spaces.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) void scsi_sanitize_inquiry_string(unsigned char *s, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	int terminated = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	for (; len > 0; (--len, ++s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		if (*s == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 			terminated = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 		if (terminated || *s < 0x20 || *s > 0x7e)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 			*s = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) EXPORT_SYMBOL(scsi_sanitize_inquiry_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  * @sdev:	scsi_device to probe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  * @inq_result:	area to store the INQUIRY result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  * @result_len: len of inq_result
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  * @bflags:	store any bflags found here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554)  *     Probe the lun associated with @req using a standard SCSI INQUIRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556)  *     If the INQUIRY is successful, zero is returned and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557)  *     INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558)  *     are copied to the scsi_device any flags value is stored in *@bflags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 			  int result_len, blist_flags_t *bflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	int first_inquiry_len, try_inquiry_len, next_inquiry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	int response_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	int pass, count, result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	struct scsi_sense_hdr sshdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	*bflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	/* Perform up to 3 passes.  The first pass uses a conservative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	 * transfer length of 36 unless sdev->inquiry_len specifies a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	 * different value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	try_inquiry_len = first_inquiry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	pass = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  next_pass:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 				"scsi scan: INQUIRY pass %d length %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 				pass, try_inquiry_len));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	/* Each pass gets up to three chances to ignore Unit Attention */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	for (count = 0; count < 3; ++count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		int resid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		memset(scsi_cmd, 0, 6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		scsi_cmd[0] = INQUIRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		scsi_cmd[4] = (unsigned char) try_inquiry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 		memset(inq_result, 0, try_inquiry_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		result = scsi_execute_req(sdev,  scsi_cmd, DMA_FROM_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 					  inq_result, try_inquiry_len, &sshdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 					  HZ / 2 + HZ * scsi_inq_timeout, 3,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 					  &resid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 				"scsi scan: INQUIRY %s with code 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 				result ? "failed" : "successful", result));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 		if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 			 * not-ready to ready transition [asc/ascq=0x28/0x0]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 			 * or power-on, reset [asc/ascq=0x29/0x0], continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 			 * INQUIRY should not yield UNIT_ATTENTION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 			 * but many buggy devices do so anyway. 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			if (driver_byte(result) == DRIVER_SENSE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 			    scsi_sense_valid(&sshdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 				if ((sshdr.sense_key == UNIT_ATTENTION) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 				    ((sshdr.asc == 0x28) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 				     (sshdr.asc == 0x29)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 				    (sshdr.ascq == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 			 * if nothing was transferred, we try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 			 * again. It's a workaround for some USB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 			 * devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 			if (resid == try_inquiry_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	if (result == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		scsi_sanitize_inquiry_string(&inq_result[8], 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		scsi_sanitize_inquiry_string(&inq_result[16], 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 		scsi_sanitize_inquiry_string(&inq_result[32], 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		response_len = inq_result[4] + 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		if (response_len > 255)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			response_len = first_inquiry_len;	/* sanity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		 * Get any flags for this device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		 * XXX add a bflags to scsi_device, and replace the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		 * corresponding bit fields in scsi_device, so bflags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		 * need not be passed as an argument.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		*bflags = scsi_get_device_flags(sdev, &inq_result[8],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 				&inq_result[16]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		/* When the first pass succeeds we gain information about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		 * what larger transfer lengths might work. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		if (pass == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			if (BLIST_INQUIRY_36 & *bflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				next_inquiry_len = 36;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 			else if (sdev->inquiry_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 				next_inquiry_len = sdev->inquiry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 				next_inquiry_len = response_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			/* If more data is available perform the second pass */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 			if (next_inquiry_len > try_inquiry_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 				try_inquiry_len = next_inquiry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 				pass = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 				goto next_pass;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	} else if (pass == 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 			    "scsi scan: %d byte inquiry failed.  "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 			    "Consider BLIST_INQUIRY_36 for this device\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			    try_inquiry_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 		/* If this pass failed, the third pass goes back and transfers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		 * the same amount as we successfully got in the first pass. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 		try_inquiry_len = first_inquiry_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		pass = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		goto next_pass;
^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) 	/* If the last transfer attempt got an error, assume the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	 * peripheral doesn't exist or is dead. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	/* Don't report any more data than the device says is valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	sdev->inquiry_len = min(try_inquiry_len, response_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	 * XXX Abort if the response length is less than 36? If less than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	 * 32, the lookup of the device flags (above) could be invalid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	 * and it would be possible to take an incorrect action - we do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	 * not want to hang because of a short INQUIRY. On the flip side,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	 * if the device is spun down or becoming ready (and so it gives a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	 * short INQUIRY), an abort here prevents any further use of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	 * device, including spin up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	 * On the whole, the best approach seems to be to assume the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	 * 36 bytes are valid no matter what the device says.  That's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	 * better than copying < 36 bytes to the inquiry-result buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	 * and displaying garbage for the Vendor, Product, or Revision
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	 * strings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	if (sdev->inquiry_len < 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		if (!sdev->host->short_inquiry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 			shost_printk(KERN_INFO, sdev->host,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 				    "scsi scan: INQUIRY result too short (%d),"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 				    " using 36\n", sdev->inquiry_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			sdev->host->short_inquiry = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		sdev->inquiry_len = 36;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	 * Related to the above issue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	 * and if not ready, sent a START_STOP to start (maybe spin up) and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	 * then send the INQUIRY again, since the INQUIRY can change after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	 * a device is initialized.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	 * Ideally, start a device if explicitly asked to do so.  This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	 * assumes that a device is spun up on power on, spun down on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	 * request, and then spun up on request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	 * The scanning code needs to know the scsi_level, even if no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	 * non-zero LUNs can be scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	sdev->scsi_level = inq_result[2] & 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	if (sdev->scsi_level >= 2 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	    (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		sdev->scsi_level++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	sdev->sdev_target->scsi_level = sdev->scsi_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	 * If SCSI-2 or lower, and if the transport requires it,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	 * store the LUN value in CDB[1].
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	sdev->lun_in_cdb = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (sdev->scsi_level <= SCSI_2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	    sdev->scsi_level != SCSI_UNKNOWN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	    !sdev->host->no_scsi2_lun_in_cdb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		sdev->lun_in_cdb = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750)  * scsi_add_lun - allocate and fully initialze a scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751)  * @sdev:	holds information to be stored in the new scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * @inq_result:	holds the result of a previous INQUIRY to the LUN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  * @bflags:	black/white list flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * @async:	1 if this device is being scanned asynchronously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757)  *     Initialize the scsi_device @sdev.  Optionally set fields based
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758)  *     on values in *@bflags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761)  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 		blist_flags_t *bflags, int async)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	 * XXX do not save the inquiry, since it can change underneath us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	 * save just vendor/model/rev.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	 * Rather than save it and have an ioctl that retrieves the saved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	 * value, have an ioctl that executes the same INQUIRY code used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	 * in scsi_probe_lun, let user level programs doing INQUIRY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	 * scanning run at their own risk, or supply a user level program
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	 * that can correctly scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 	 */
^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) 	 * Copy at least 36 bytes of INQUIRY data, so that we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	 * dereference unallocated memory when accessing the Vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	 * Product, and Revision strings.  Badly behaved devices may set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	 * the INQUIRY Additional Length byte to a small value, indicating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 	 * these strings are invalid, but often they contain plausible data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	 * nonetheless.  It doesn't matter if the device sent < 36 bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	 * total, since scsi_probe_lun() initializes inq_result with 0s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	sdev->inquiry = kmemdup(inq_result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 				max_t(size_t, sdev->inquiry_len, 36),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (sdev->inquiry == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		return SCSI_SCAN_NO_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	sdev->vendor = (char *) (sdev->inquiry + 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	sdev->model = (char *) (sdev->inquiry + 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	sdev->rev = (char *) (sdev->inquiry + 32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	if (strncmp(sdev->vendor, "ATA     ", 8) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		 * sata emulation layer device.  This is a hack to work around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		 * the SATL power management specifications which state that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		 * when the SATL detects the device has gone into standby
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		 * mode, it shall respond with NOT READY.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		sdev->allow_restart = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	if (*bflags & BLIST_ISROM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		sdev->type = TYPE_ROM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		sdev->removable = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		sdev->type = (inq_result[0] & 0x1f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		sdev->removable = (inq_result[1] & 0x80) >> 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 		 * some devices may respond with wrong type for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		 * well-known logical units. Force well-known type
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		 * to enumerate them correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		if (scsi_is_wlun(sdev->lun) && sdev->type != TYPE_WLUN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			sdev_printk(KERN_WARNING, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 				"%s: correcting incorrect peripheral device type 0x%x for W-LUN 0x%16xhN\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 				__func__, sdev->type, (unsigned int)sdev->lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			sdev->type = TYPE_WLUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		/* RBC and MMC devices can return SCSI-3 compliance and yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		 * still not support REPORT LUNS, so make them act as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		 * specifically set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		if ((*bflags & BLIST_REPORTLUN2) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			*bflags |= BLIST_NOREPORTLUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	 * spec says: The device server is capable of supporting the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	 * specified peripheral device type on this logical unit. However,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	 * the physical device is not currently connected to this logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 	 * unit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	 * The above is vague, as it implies that we could treat 001 and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	 * 011 the same. Stay compatible with previous code, and create a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	 * scsi_device for a PQ of 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	 * Don't set the device offline here; rather let the upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	 * level drivers eval the PQ to decide whether they should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	 */ 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	sdev->lockable = sdev->removable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	if (sdev->scsi_level >= SCSI_3 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			(sdev->inquiry_len > 56 && inq_result[56] & 0x04))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		sdev->ppr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	if (inq_result[7] & 0x60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		sdev->wdtr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 	if (inq_result[7] & 0x10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 		sdev->sdtr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			"ANSI: %d%s\n", scsi_device_type(sdev->type),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			sdev->vendor, sdev->model, sdev->rev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			sdev->inq_periph_qual, inq_result[2] & 0x07,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			(inq_result[3] & 0x0f) == 1 ? " CCS" : "");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	    !(*bflags & BLIST_NOTQ)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		sdev->tagged_supported = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 		sdev->simple_tags = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	 * Some devices (Texel CD ROM drives) have handshaking problems
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	 * when used with the Seagate controllers. borken is initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	 * to 1, and then set it to 0 here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if ((*bflags & BLIST_BORKEN) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		sdev->borken = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	if (*bflags & BLIST_NO_ULD_ATTACH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		sdev->no_uld_attach = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	 * Apparently some really broken devices (contrary to the SCSI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	 * standards) need to be selected without asserting ATN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	if (*bflags & BLIST_SELECT_NO_ATN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		sdev->select_no_atn = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	 * Maximum 512 sector transfer length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	 * broken RA4x00 Compaq Disk Array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	if (*bflags & BLIST_MAX_512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		blk_queue_max_hw_sectors(sdev->request_queue, 512);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	 * Max 1024 sector transfer length for targets that report incorrect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	 * max/optimal lengths and relied on the old block layer safe default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 	else if (*bflags & BLIST_MAX_1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		blk_queue_max_hw_sectors(sdev->request_queue, 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	 * Some devices may not want to have a start command automatically
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	 * issued when a device is added.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	if (*bflags & BLIST_NOSTARTONADD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		sdev->no_start_on_add = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	if (*bflags & BLIST_SINGLELUN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		scsi_target(sdev)->single_lun = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	sdev->use_10_for_rw = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	/* some devices don't like REPORT SUPPORTED OPERATION CODES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	 * and will simply timeout causing sd_mod init to take a very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	 * very long time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	if (*bflags & BLIST_NO_RSOC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		sdev->no_report_opcodes = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	/* set the device running here so that slave configure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	 * may do I/O */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	mutex_lock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	ret = scsi_device_set_state(sdev, SDEV_RUNNING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		ret = scsi_device_set_state(sdev, SDEV_BLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	mutex_unlock(&sdev->state_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		sdev_printk(KERN_ERR, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			    "in wrong state %s to complete scan\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			    scsi_device_state_name(sdev->sdev_state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		return SCSI_SCAN_NO_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	if (*bflags & BLIST_NOT_LOCKABLE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		sdev->lockable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	if (*bflags & BLIST_RETRY_HWERROR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		sdev->retry_hwerror = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (*bflags & BLIST_NO_DIF)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		sdev->no_dif = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	if (*bflags & BLIST_UNMAP_LIMIT_WS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		sdev->unmap_limit_for_ws = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 	sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	if (*bflags & BLIST_TRY_VPD_PAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		sdev->try_vpd_pages = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 	else if (*bflags & BLIST_SKIP_VPD_PAGES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		sdev->skip_vpd_pages = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	transport_configure_device(&sdev->sdev_gendev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	if (sdev->host->hostt->slave_configure) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		ret = sdev->host->hostt->slave_configure(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			 * if LLDD reports slave not present, don't clutter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			 * console with alloc failure messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			if (ret != -ENXIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 				sdev_printk(KERN_ERR, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 					"failed to configure device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			return SCSI_SCAN_NO_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	if (sdev->scsi_level >= SCSI_3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		scsi_attach_vpd(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	sdev->max_queue_depth = sdev->queue_depth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	sdev->sdev_bflags = *bflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	 * Ok, the device is now all set up, we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * register it and tell the rest of the kernel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 * about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (!async && scsi_sysfs_add_sdev(sdev) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		return SCSI_SCAN_NO_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	return SCSI_SCAN_LUN_PRESENT;
^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) #ifdef CONFIG_SCSI_LOGGING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) /** 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * @buf:   Output buffer with at least end-first+1 bytes of space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * @inq:   Inquiry buffer (input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  * @first: Offset of string into inq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  * @end:   Index after last character in inq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 				   unsigned first, unsigned end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	unsigned term = 0, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		if (inq[idx+first] > ' ') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 			buf[idx] = inq[idx+first];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			term = idx+1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 			buf[idx] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	buf[term] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023)  * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)  * @starget:	pointer to target device structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025)  * @lun:	LUN of target device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)  * @bflagsp:	store bflags here if not NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)  * @sdevp:	probe the LUN corresponding to this scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * @rescan:     if not equal to SCSI_SCAN_INITIAL skip some code only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  *              needed on first scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030)  * @hostdata:	passed to scsi_alloc_sdev()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033)  *     Call scsi_probe_lun, if a LUN with an attached device is found,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)  *     allocate and set it up by calling scsi_add_lun.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038)  *   - SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039)  *   - SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040)  *         attached at the LUN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041)  *   - SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) static int scsi_probe_and_add_lun(struct scsi_target *starget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 				  u64 lun, blist_flags_t *bflagsp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 				  struct scsi_device **sdevp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 				  enum scsi_scan_mode rescan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 				  void *hostdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	unsigned char *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	blist_flags_t bflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	int res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	 * The rescan flag is used as an optimization, the first scan of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 	 * host adapter calls into here with rescan == 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	sdev = scsi_device_lookup_by_target(starget, lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	if (sdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		if (rescan != SCSI_SCAN_INITIAL || !scsi_device_created(sdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 				"scsi scan: device exists on %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 				dev_name(&sdev->sdev_gendev)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			if (sdevp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 				*sdevp = sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 				scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			if (bflagsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 				*bflagsp = scsi_get_device_flags(sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 								 sdev->vendor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 								 sdev->model);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 			return SCSI_SCAN_LUN_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		sdev = scsi_alloc_sdev(starget, lun, hostdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	if (!sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	result = kmalloc(result_len, GFP_KERNEL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (!result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		goto out_free_sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	if (scsi_probe_lun(sdev, result, result_len, &bflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		goto out_free_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (bflagsp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		*bflagsp = bflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 	 * result contains valid SCSI INQUIRY data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	if ((result[0] >> 5) == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		 * For a Peripheral qualifier 3 (011b), the SCSI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		 * spec says: The device server is not capable of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		 * supporting a physical device on this logical
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		 * unit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 		 * For disks, this implies that there is no
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		 * logical disk configured at sdev->lun, but there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		 * is a target id responding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 		SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 				   " peripheral qualifier of 3, device not"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 				   " added\n"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		if (lun == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 			SCSI_LOG_SCAN_BUS(1, {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 				unsigned char vend[9];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 				unsigned char mod[17];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 				sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 					"scsi scan: consider passing scsi_mod."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 					"dev_flags=%s:%s:0x240 or 0x1000240\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 					scsi_inq_str(vend, result, 8, 16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 					scsi_inq_str(mod, result, 16, 32));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 			});
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		res = SCSI_SCAN_TARGET_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		goto out_free_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	 * Some targets may set slight variations of PQ and PDT to signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	 * that no LUN is present, so don't add sdev in these cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	 * Two specific examples are:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	 * 1) NetApp targets: return PQ=1, PDT=0x1f
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	 * 2) IBM/2145 targets: return PQ=1, PDT=0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	 * 3) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	 *    in the UFI 1.0 spec (we cannot rely on reserved bits).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	 * References:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	 * 1) SCSI SPC-3, pp. 145-146
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	 * PQ=1: "A peripheral device having the specified peripheral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	 * device type is not connected to this logical unit. However, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	 * device server is capable of supporting the specified peripheral
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	 * device type on this logical unit."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	 * PDT=0x1f: "Unknown or no device type"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	 * 2) USB UFI 1.0, p. 20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	 * PDT=00h Direct-access device (floppy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	 * PDT=1Fh none (no FDD connected to the requested logical unit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	if (((result[0] >> 5) == 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	    (starget->pdt_1f_for_no_lun && (result[0] & 0x1f) == 0x1f)) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	    !scsi_is_wlun(lun)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 		SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 					"scsi scan: peripheral device type"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 					" of 31, no device added\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		res = SCSI_SCAN_TARGET_PRESENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 		goto out_free_result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	if (res == SCSI_SCAN_LUN_PRESENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		if (bflags & BLIST_KEY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 			sdev->lockable = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			scsi_unlock_floptical(sdev, result);
^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)  out_free_result:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	kfree(result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167)  out_free_sdev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (res == SCSI_SCAN_LUN_PRESENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		if (sdevp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 			if (scsi_device_get(sdev) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 				*sdevp = sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 				__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 				res = SCSI_SCAN_NO_RESPONSE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184)  * scsi_sequential_lun_scan - sequentially scan a SCSI target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185)  * @starget:	pointer to target structure to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186)  * @bflags:	black/white list flag for LUN 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187)  * @scsi_level: Which version of the standard does this device adhere to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188)  * @rescan:     passed to scsi_probe_add_lun()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191)  *     Generally, scan from LUN 1 (LUN 0 is assumed to already have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192)  *     scanned) to some maximum lun until a LUN is found with no device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193)  *     attached. Use the bflags to figure out any oddities.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195)  *     Modifies sdevscan->lun.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) static void scsi_sequential_lun_scan(struct scsi_target *starget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 				     blist_flags_t bflags, int scsi_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 				     enum scsi_scan_mode rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	uint max_dev_lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	u64 sparse_lun, lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	SCSI_LOG_SCAN_BUS(3, starget_printk(KERN_INFO, starget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		"scsi scan: Sequential scan\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	max_dev_lun = min(max_scsi_luns, shost->max_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	 * If this device is known to support sparse multiple units,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	 * override the other settings, and scan all of them. Normally,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	 * SCSI-3 devices should be scanned via the REPORT LUNS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	if (bflags & BLIST_SPARSELUN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		max_dev_lun = shost->max_lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 		sparse_lun = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		sparse_lun = 0;
^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) 	 * If less than SCSI_1_CCS, and no special lun scanning, stop
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	 * scanning; this matches 2.4 behaviour, but could just be a bug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	 * (to continue scanning a SCSI_1_CCS device).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	 * This test is broken.  We might not have any device on lun0 for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	 * a sparselun device, and if that's the case then how would we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	 * know the real scsi_level, eh?  It might make sense to just not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	 * scan any SCSI_1 device for non-0 luns, but that check would best
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	 * go into scsi_alloc_sdev() and just have it return null when asked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	if ((sdevscan->scsi_level < SCSI_1_CCS) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	    ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	     == 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	 * If this device is known to support multiple units, override
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	 * the other settings, and scan all of them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	if (bflags & BLIST_FORCELUN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		max_dev_lun = shost->max_lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	 * REGAL CDC-4X: avoid hang after LUN 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	if (bflags & BLIST_MAX5LUN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		max_dev_lun = min(5U, max_dev_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	 * Do not scan SCSI-2 or lower device past LUN 7, unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	 * BLIST_LARGELUN.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		max_dev_lun = min(8U, max_dev_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		max_dev_lun = min(256U, max_dev_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	 * until we reach the max, or no LUN is found and we are not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	 * sparse_lun.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	for (lun = 1; lun < max_dev_lun; ++lun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 		if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 					    NULL) != SCSI_SCAN_LUN_PRESENT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		    !sparse_lun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270)  * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271)  * @starget: which target
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)  * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273)  * @rescan: nonzero if we can skip code only needed on first scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276)  *   Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277)  *   Scan the resulting list of LUNs by calling scsi_probe_and_add_lun.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279)  *   If BLINK_REPORTLUN2 is set, scan a target that supports more than 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)  *   LUNs even if it's older than SCSI-3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281)  *   If BLIST_NOREPORTLUN is set, return 1 always.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282)  *   If BLIST_NOLUN is set, return 0 always.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)  *   If starget->no_report_luns is set, return 1 always.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285)  * Return:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286)  *     0: scan completed (or no memory, so further scanning is futile)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)  *     1: could not scan with REPORT LUN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) static int scsi_report_lun_scan(struct scsi_target *starget, blist_flags_t bflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 				enum scsi_scan_mode rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	unsigned int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	u64 lun;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	unsigned int num_luns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	unsigned int retries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	struct scsi_lun *lunp, *lun_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	struct scsi_sense_hdr sshdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	 * support more than 8 LUNs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	 * Don't attempt if the target doesn't support REPORT LUNS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 	if (bflags & BLIST_NOREPORTLUN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 	if (starget->scsi_level < SCSI_2 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	    starget->scsi_level != SCSI_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	if (starget->scsi_level < SCSI_3 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 	    (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 	if (bflags & BLIST_NOLUN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	if (starget->no_report_luns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		sdev = scsi_alloc_sdev(starget, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		if (!sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 		if (scsi_device_get(sdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	 * Allocate enough to hold the header (the same size as one scsi_lun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	 * plus the number of luns we are requesting.  511 was the default
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	 * value of the now removed max_report_luns parameter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	length = (511 + 1) * sizeof(struct scsi_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 	lun_data = kmalloc(length, GFP_KERNEL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			   (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	if (!lun_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		printk(ALLOC_FAILURE_MSG, __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 	scsi_cmd[0] = REPORT_LUNS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 	 * bytes 1 - 5: reserved, set to zero.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 	memset(&scsi_cmd[1], 0, 5);
^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) 	 * bytes 6 - 9: length of the command.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	put_unaligned_be32(length, &scsi_cmd[6]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	scsi_cmd[10] = 0;	/* reserved */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	scsi_cmd[11] = 0;	/* control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	 * We can get a UNIT ATTENTION, for example a power on/reset, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	 * retry a few times (like sd.c does for TEST UNIT READY).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	 * Experience shows some combinations of adapter/devices get at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	 * least two power on/resets.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	 * Illegal requests (for devices that do not support REPORT LUNS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	 * should come through as a check condition, and will not generate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	 * a retry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	for (retries = 0; retries < 3; retries++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 				"scsi scan: Sending REPORT LUNS to (try %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 				retries));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 					  lun_data, length, &sshdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 					  SCSI_REPORT_LUNS_TIMEOUT, 3, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 		SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 				"scsi scan: REPORT LUNS"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 				" %s (try %d) result 0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 				result ?  "failed" : "successful",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 				retries, result));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		if (result == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		else if (scsi_sense_valid(&sshdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 			if (sshdr.sense_key != UNIT_ATTENTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	if (result) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 		 * The device probably does not support a REPORT LUN command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		ret = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	 * Get the length from the first four bytes of lun_data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	if (get_unaligned_be32(lun_data->scsi_lun) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 	    sizeof(struct scsi_lun) > length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		length = get_unaligned_be32(lun_data->scsi_lun) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			 sizeof(struct scsi_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		kfree(lun_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	length = get_unaligned_be32(lun_data->scsi_lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	num_luns = (length / sizeof(struct scsi_lun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		"scsi scan: REPORT LUN scan\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 	 * Scan the luns in lun_data. The entry at offset 0 is really
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 	 * the header, so start at 1 and go up to and including num_luns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		lun = scsilun_to_int(lunp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		if (lun > sdev->host->max_lun) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 			sdev_printk(KERN_WARNING, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 				    "lun%llu has a LUN larger than"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 				    " allowed by the host adapter\n", lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 			int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 			res = scsi_probe_and_add_lun(starget,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 				lun, NULL, NULL, rescan, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 			if (res == SCSI_SCAN_NO_RESPONSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 				 * Got some results, but now none, abort.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 				sdev_printk(KERN_ERR, sdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 					"Unexpected response"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 					" from lun %llu while scanning, scan"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 					" aborted\n", (unsigned long long)lun);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 		}
^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)  out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	kfree(lun_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 	if (scsi_device_created(sdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		 * the sdev we used didn't appear in the report luns scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 		__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 				      uint id, u64 lun, void *hostdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	struct scsi_device *sdev = ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	struct device *parent = &shost->shost_gendev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	struct scsi_target *starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	if (strncmp(scsi_scan_type, "none", 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 		return ERR_PTR(-ENODEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	starget = scsi_alloc_target(parent, channel, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	if (!starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	scsi_autopm_get_target(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	if (!shost->async_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 		scsi_complete_async_scans();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 	if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 		scsi_autopm_put_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 	scsi_autopm_put_target(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	 * paired with scsi_alloc_target().  Target will be destroyed unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 	 * scsi_probe_and_add_lun made an underlying device visible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	scsi_target_reap(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	put_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 	return sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) EXPORT_SYMBOL(__scsi_add_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) int scsi_add_device(struct Scsi_Host *host, uint channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		    uint target, u64 lun)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 	struct scsi_device *sdev = 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		__scsi_add_device(host, channel, target, lun, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	if (IS_ERR(sdev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 		return PTR_ERR(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 	scsi_device_put(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) EXPORT_SYMBOL(scsi_add_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) void scsi_rescan_device(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	struct scsi_device *sdev = to_scsi_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	device_lock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	scsi_attach_vpd(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	if (sdev->handler && sdev->handler->rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 		sdev->handler->rescan(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	if (dev->driver && try_module_get(dev->driver->owner)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		struct scsi_driver *drv = to_scsi_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 		if (drv->rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 			drv->rescan(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		module_put(dev->driver->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	device_unlock(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) EXPORT_SYMBOL(scsi_rescan_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) static void __scsi_scan_target(struct device *parent, unsigned int channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		unsigned int id, u64 lun, enum scsi_scan_mode rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	struct Scsi_Host *shost = dev_to_shost(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 	blist_flags_t bflags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	struct scsi_target *starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	if (shost->this_id == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 		 * Don't scan the host adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	starget = scsi_alloc_target(parent, channel, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	if (!starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	scsi_autopm_get_target(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	if (lun != SCAN_WILD_CARD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		 * Scan for a specific host/chan/id/lun.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 		scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		goto out_reap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	 * Scan LUN 0, if there is some response, scan further. Ideally, we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	 * would not configure LUN 0 until all LUNs are scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 	if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 			 * The REPORT LUN did not scan the target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 			 * do a sequential scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 			scsi_sequential_lun_scan(starget, bflags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 						 starget->scsi_level, rescan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573)  out_reap:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 	scsi_autopm_put_target(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 	 * paired with scsi_alloc_target(): determine if the target has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	 * any children at all and if not, nuke it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	scsi_target_reap(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	put_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)  * scsi_scan_target - scan a target id, possibly including all LUNs on the target.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586)  * @parent:	host to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587)  * @channel:	channel to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)  * @id:		target id to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589)  * @lun:	Specific LUN to scan or SCAN_WILD_CARD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590)  * @rescan:	passed to LUN scanning routines; SCSI_SCAN_INITIAL for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591)  *              no rescan, SCSI_SCAN_RESCAN to rescan existing LUNs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592)  *              and SCSI_SCAN_MANUAL to force scanning even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593)  *              'scan=manual' is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595)  * Description:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596)  *     Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597)  *     and possibly all LUNs on the target id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599)  *     First try a REPORT LUN scan, if that does not scan the target, do a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600)  *     sequential scan of LUNs on the target id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) void scsi_scan_target(struct device *parent, unsigned int channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		      unsigned int id, u64 lun, enum scsi_scan_mode rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	struct Scsi_Host *shost = dev_to_shost(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (strncmp(scsi_scan_type, "none", 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 	if (rescan != SCSI_SCAN_MANUAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 	    strncmp(scsi_scan_type, "manual", 6) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	if (!shost->async_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 		scsi_complete_async_scans();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		__scsi_scan_target(parent, channel, id, lun, rescan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 		scsi_autopm_put_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) EXPORT_SYMBOL(scsi_scan_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 			      unsigned int id, u64 lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 			      enum scsi_scan_mode rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	uint order_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (id == SCAN_WILD_CARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		for (id = 0; id < shost->max_id; ++id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 			 * XXX adapter drivers when possible (FCP, iSCSI)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 			 * could modify max_id to match the current max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 			 * not the absolute max.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 			 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 			 * XXX add a shost id iterator, so for example,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 			 * the FC ID can be the same as a target id
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 			 * without a huge overhead of sparse id's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 			if (shost->reverse_ordering)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 				 * Scan from high to low id.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 				order_id = shost->max_id - id - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 				order_id = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 			__scsi_scan_target(&shost->shost_gendev, channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 					order_id, lun, rescan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 		__scsi_scan_target(&shost->shost_gendev, channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 				id, lun, rescan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 			    unsigned int id, u64 lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 			    enum scsi_scan_mode rescan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 		"%s: <%u:%u:%llu>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		__func__, channel, id, lun));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	    ((lun != SCAN_WILD_CARD) && (lun >= shost->max_lun)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 	mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	if (!shost->async_scan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 		scsi_complete_async_scans();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 		if (channel == SCAN_WILD_CARD)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 			for (channel = 0; channel <= shost->max_channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 			     channel++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 				scsi_scan_channel(shost, channel, id, lun,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 						  rescan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 			scsi_scan_channel(shost, channel, id, lun, rescan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 		scsi_autopm_put_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	shost_for_each_device(sdev, shost) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 		/* target removed before the device could be added */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 		if (sdev->sdev_state == SDEV_DEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 		/* If device is already visible, skip adding it to sysfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 		if (sdev->is_visible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 		if (!scsi_host_scan_allowed(shost) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 		    scsi_sysfs_add_sdev(sdev) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 			__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)  * scsi_prep_async_scan - prepare for an async scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708)  * @shost: the host which will be scanned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709)  * Returns: a cookie to be passed to scsi_finish_async_scan()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711)  * Tells the midlayer this host is going to do an asynchronous scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712)  * It reserves the host's position in the scanning list and ensures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713)  * that other asynchronous scans started after this one won't affect the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714)  * ordering of the discovered devices.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	struct async_scan_data *data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	if (strncmp(scsi_scan_type, "sync", 4) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 	mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) 	if (shost->async_scan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 		shost_printk(KERN_DEBUG, shost, "%s called twice\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	data = kmalloc(sizeof(*data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	data->shost = scsi_host_get(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	if (!data->shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 		goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 	init_completion(&data->prev_finished);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	shost->async_scan = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	spin_lock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	if (list_empty(&scanning_hosts))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		complete(&data->prev_finished);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 	list_add_tail(&data->list, &scanning_hosts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	spin_unlock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	return data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)  err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758)  * scsi_finish_async_scan - asynchronous scan has finished
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759)  * @data: cookie returned from earlier call to scsi_prep_async_scan()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)  * All the devices currently attached to this host have been found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762)  * This function announces all the devices it has found to the rest
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763)  * of the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) static void scsi_finish_async_scan(struct async_scan_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	struct Scsi_Host *shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 	shost = data->shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 	mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 	if (!shost->async_scan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		shost_printk(KERN_INFO, shost, "%s called twice\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	wait_for_completion(&data->prev_finished);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 	scsi_sysfs_add_devices(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 	spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	shost->async_scan = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 	spin_lock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	list_del(&data->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 	if (!list_empty(&scanning_hosts)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 		struct async_scan_data *next = list_entry(scanning_hosts.next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 				struct async_scan_data, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 		complete(&next->prev_finished);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 	spin_unlock(&async_scan_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 	scsi_autopm_put_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 	scsi_host_put(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 	kfree(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) static void do_scsi_scan_host(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	if (shost->hostt->scan_finished) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 		unsigned long start = jiffies;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 		if (shost->hostt->scan_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 			shost->hostt->scan_start(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 		while (!shost->hostt->scan_finished(shost, jiffies - start))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 				SCAN_WILD_CARD, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) static void do_scan_async(void *_data, async_cookie_t c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 	struct async_scan_data *data = _data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	struct Scsi_Host *shost = data->shost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	do_scsi_scan_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	scsi_finish_async_scan(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)  * scsi_scan_host - scan the given adapter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)  * @shost:	adapter to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)  **/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) void scsi_scan_host(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	struct async_scan_data *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	if (strncmp(scsi_scan_type, "none", 4) == 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 	    strncmp(scsi_scan_type, "manual", 6) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	if (scsi_autopm_get_host(shost) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 	data = scsi_prep_async_scan(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 	if (!data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 		do_scsi_scan_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 		scsi_autopm_put_host(shost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 	/* register with the async subsystem so wait_for_device_probe()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	 * will flush this work
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 	async_schedule(do_scan_async, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 	/* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) EXPORT_SYMBOL(scsi_scan_host);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) void scsi_forget_host(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) 	struct scsi_device *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867)  restart:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	spin_lock_irqsave(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	list_for_each_entry(sdev, &shost->__devices, siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		if (sdev->sdev_state == SDEV_DEL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 		spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 		__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		goto restart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	spin_unlock_irqrestore(shost->host_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880)  * scsi_get_host_dev - Create a scsi_device that points to the host adapter itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881)  * @shost: Host that needs a scsi_device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883)  * Lock status: None assumed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885)  * Returns:     The scsi_device or NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)  * Notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)  *	Attach a single scsi_device to the Scsi_Host - this should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)  *	be made to look like a "pseudo-device" that points to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890)  *	HA itself.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)  *	Note - this device is not accessible from any high-level
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893)  *	drivers (including generics), which is probably not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)  *	optimal.  We can add hooks later to attach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 	struct scsi_device *sdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	struct scsi_target *starget;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 	mutex_lock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	if (!scsi_host_scan_allowed(shost))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	if (!starget)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	sdev = scsi_alloc_sdev(starget, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	if (sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 		sdev->borken = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		scsi_target_reap(starget);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	put_device(&starget->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914)  out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	mutex_unlock(&shost->scan_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	return sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) EXPORT_SYMBOL(scsi_get_host_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  * scsi_free_host_dev - Free a scsi_device that points to the host adapter itself
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)  * @sdev: Host device to be freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)  * Lock status: None assumed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926)  * Returns:     Nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) void scsi_free_host_dev(struct scsi_device *sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	BUG_ON(sdev->id != sdev->host->this_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	__scsi_remove_device(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) EXPORT_SYMBOL(scsi_free_host_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)