xref: /linux/drivers/scsi/scsi_scan.c (revision 12fb8c1574d7d0c262d2f4c667047889c4f27ebe)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * scsi_scan.c
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2000 Eric Youngdale,
51da177e4SLinus Torvalds  * Copyright (C) 2002 Patrick Mansfield
61da177e4SLinus Torvalds  *
71da177e4SLinus Torvalds  * The general scanning/probing algorithm is as follows, exceptions are
81da177e4SLinus Torvalds  * made to it depending on device specific flags, compilation options, and
91da177e4SLinus Torvalds  * global variable (boot or module load time) settings.
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * A specific LUN is scanned via an INQUIRY command; if the LUN has a
12f64a181dSChristoph Hellwig  * device attached, a scsi_device is allocated and setup for it.
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  * For every id of every channel on the given host:
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * 	Scan LUN 0; if the target responds to LUN 0 (even if there is no
171da177e4SLinus Torvalds  * 	device or storage attached to LUN 0):
181da177e4SLinus Torvalds  *
191da177e4SLinus Torvalds  * 		If LUN 0 has a device attached, allocate and setup a
20f64a181dSChristoph Hellwig  * 		scsi_device for it.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * 		If target is SCSI-3 or up, issue a REPORT LUN, and scan
231da177e4SLinus Torvalds  * 		all of the LUNs returned by the REPORT LUN; else,
241da177e4SLinus Torvalds  * 		sequentially scan LUNs up until some maximum is reached,
251da177e4SLinus Torvalds  * 		or a LUN is seen that cannot have a device attached to it.
261da177e4SLinus Torvalds  */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <linux/module.h>
291da177e4SLinus Torvalds #include <linux/moduleparam.h>
301da177e4SLinus Torvalds #include <linux/init.h>
311da177e4SLinus Torvalds #include <linux/blkdev.h>
323e082a91SMatthew Wilcox #include <linux/delay.h>
333e082a91SMatthew Wilcox #include <linux/kthread.h>
343e082a91SMatthew Wilcox #include <linux/spinlock.h>
354ace92fcSArjan van de Ven #include <linux/async.h>
365a0e3ad6STejun Heo #include <linux/slab.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <scsi/scsi.h>
39beb40487SChristoph Hellwig #include <scsi/scsi_cmnd.h>
401da177e4SLinus Torvalds #include <scsi/scsi_device.h>
411da177e4SLinus Torvalds #include <scsi/scsi_driver.h>
421da177e4SLinus Torvalds #include <scsi/scsi_devinfo.h>
431da177e4SLinus Torvalds #include <scsi/scsi_host.h>
441da177e4SLinus Torvalds #include <scsi/scsi_transport.h>
451da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #include "scsi_priv.h"
481da177e4SLinus Torvalds #include "scsi_logging.h"
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define ALLOC_FAILURE_MSG	KERN_ERR "%s: Allocation failure during" \
511da177e4SLinus Torvalds 	" SCSI scanning, some SCSI devices might not be configured\n"
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds /*
541da177e4SLinus Torvalds  * Default timeout
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds #define SCSI_TIMEOUT (2*HZ)
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds /*
59405ae7d3SRobert P. J. Day  * Prefix values for the SCSI id's (stored in sysfs name field)
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds #define SCSI_UID_SER_NUM 'S'
621da177e4SLinus Torvalds #define SCSI_UID_UNKNOWN 'Z'
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * Return values of some of the scanning functions.
661da177e4SLinus Torvalds  *
671da177e4SLinus Torvalds  * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
681da177e4SLinus Torvalds  * includes allocation or general failures preventing IO from being sent.
691da177e4SLinus Torvalds  *
701da177e4SLinus Torvalds  * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
711da177e4SLinus Torvalds  * on the given LUN.
721da177e4SLinus Torvalds  *
731da177e4SLinus Torvalds  * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
741da177e4SLinus Torvalds  * given LUN.
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds #define SCSI_SCAN_NO_RESPONSE		0
771da177e4SLinus Torvalds #define SCSI_SCAN_TARGET_PRESENT	1
781da177e4SLinus Torvalds #define SCSI_SCAN_LUN_PRESENT		2
791da177e4SLinus Torvalds 
800ad78200SArjan van de Ven static const char *scsi_null_device_strs = "nullnullnullnull";
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds #define MAX_SCSI_LUNS	512
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds #ifdef CONFIG_SCSI_MULTI_LUN
851da177e4SLinus Torvalds static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
861da177e4SLinus Torvalds #else
871da177e4SLinus Torvalds static unsigned int max_scsi_luns = 1;
881da177e4SLinus Torvalds #endif
891da177e4SLinus Torvalds 
9010f4b89aSMasatake YAMATO module_param_named(max_luns, max_scsi_luns, uint, S_IRUGO|S_IWUSR);
911da177e4SLinus Torvalds MODULE_PARM_DESC(max_luns,
921da177e4SLinus Torvalds 		 "last scsi LUN (should be between 1 and 2^32-1)");
931da177e4SLinus Torvalds 
9421db1882SMatthew Wilcox #ifdef CONFIG_SCSI_SCAN_ASYNC
9521db1882SMatthew Wilcox #define SCSI_SCAN_TYPE_DEFAULT "async"
9621db1882SMatthew Wilcox #else
9721db1882SMatthew Wilcox #define SCSI_SCAN_TYPE_DEFAULT "sync"
9821db1882SMatthew Wilcox #endif
9921db1882SMatthew Wilcox 
10021db1882SMatthew Wilcox static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT;
1013e082a91SMatthew Wilcox 
1023e082a91SMatthew Wilcox module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
1033e082a91SMatthew Wilcox MODULE_PARM_DESC(scan, "sync, async or none");
1043e082a91SMatthew Wilcox 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * max_scsi_report_luns: the maximum number of LUNS that will be
1071da177e4SLinus Torvalds  * returned from the REPORT LUNS command. 8 times this value must
1081da177e4SLinus Torvalds  * be allocated. In theory this could be up to an 8 byte value, but
1091da177e4SLinus Torvalds  * in practice, the maximum number of LUNs suppored by any device
1101da177e4SLinus Torvalds  * is about 16k.
1111da177e4SLinus Torvalds  */
1121da177e4SLinus Torvalds static unsigned int max_scsi_report_luns = 511;
1131da177e4SLinus Torvalds 
11410f4b89aSMasatake YAMATO module_param_named(max_report_luns, max_scsi_report_luns, uint, S_IRUGO|S_IWUSR);
1151da177e4SLinus Torvalds MODULE_PARM_DESC(max_report_luns,
1161da177e4SLinus Torvalds 		 "REPORT LUNS maximum number of LUNS received (should be"
1171da177e4SLinus Torvalds 		 " between 1 and 16384)");
1181da177e4SLinus Torvalds 
11914faf12fSAlan Stern static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ + 18;
1201da177e4SLinus Torvalds 
12110f4b89aSMasatake YAMATO module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR);
1221da177e4SLinus Torvalds MODULE_PARM_DESC(inq_timeout,
1231da177e4SLinus Torvalds 		 "Timeout (in seconds) waiting for devices to answer INQUIRY."
12414faf12fSAlan Stern 		 " Default is 20. Some devices may need more; most need less.");
1251da177e4SLinus Torvalds 
1266b7f123fSMatthew Wilcox /* This lock protects only this list */
1273e082a91SMatthew Wilcox static DEFINE_SPINLOCK(async_scan_lock);
1283e082a91SMatthew Wilcox static LIST_HEAD(scanning_hosts);
1293e082a91SMatthew Wilcox 
1303e082a91SMatthew Wilcox struct async_scan_data {
1313e082a91SMatthew Wilcox 	struct list_head list;
1323e082a91SMatthew Wilcox 	struct Scsi_Host *shost;
1333e082a91SMatthew Wilcox 	struct completion prev_finished;
1343e082a91SMatthew Wilcox };
1353e082a91SMatthew Wilcox 
1363e082a91SMatthew Wilcox /**
1373e082a91SMatthew Wilcox  * scsi_complete_async_scans - Wait for asynchronous scans to complete
1383e082a91SMatthew Wilcox  *
1398bcc2412SMatthew Wilcox  * When this function returns, any host which started scanning before
1408bcc2412SMatthew Wilcox  * this function was called will have finished its scan.  Hosts which
1418bcc2412SMatthew Wilcox  * started scanning after this function was called may or may not have
1428bcc2412SMatthew Wilcox  * finished.
1433e082a91SMatthew Wilcox  */
1443e082a91SMatthew Wilcox int scsi_complete_async_scans(void)
1453e082a91SMatthew Wilcox {
1463e082a91SMatthew Wilcox 	struct async_scan_data *data;
1473e082a91SMatthew Wilcox 
1483e082a91SMatthew Wilcox 	do {
1493e082a91SMatthew Wilcox 		if (list_empty(&scanning_hosts))
1503e082a91SMatthew Wilcox 			return 0;
1513e082a91SMatthew Wilcox 		/* If we can't get memory immediately, that's OK.  Just
1523e082a91SMatthew Wilcox 		 * sleep a little.  Even if we never get memory, the async
1533e082a91SMatthew Wilcox 		 * scans will finish eventually.
1543e082a91SMatthew Wilcox 		 */
1553e082a91SMatthew Wilcox 		data = kmalloc(sizeof(*data), GFP_KERNEL);
1563e082a91SMatthew Wilcox 		if (!data)
1573e082a91SMatthew Wilcox 			msleep(1);
1583e082a91SMatthew Wilcox 	} while (!data);
1593e082a91SMatthew Wilcox 
1603e082a91SMatthew Wilcox 	data->shost = NULL;
1613e082a91SMatthew Wilcox 	init_completion(&data->prev_finished);
1623e082a91SMatthew Wilcox 
1633e082a91SMatthew Wilcox 	spin_lock(&async_scan_lock);
1643e082a91SMatthew Wilcox 	/* Check that there's still somebody else on the list */
1653e082a91SMatthew Wilcox 	if (list_empty(&scanning_hosts))
1663e082a91SMatthew Wilcox 		goto done;
1673e082a91SMatthew Wilcox 	list_add_tail(&data->list, &scanning_hosts);
1683e082a91SMatthew Wilcox 	spin_unlock(&async_scan_lock);
1693e082a91SMatthew Wilcox 
1703e082a91SMatthew Wilcox 	printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
1713e082a91SMatthew Wilcox 	wait_for_completion(&data->prev_finished);
1723e082a91SMatthew Wilcox 
1733e082a91SMatthew Wilcox 	spin_lock(&async_scan_lock);
1743e082a91SMatthew Wilcox 	list_del(&data->list);
1758bcc2412SMatthew Wilcox 	if (!list_empty(&scanning_hosts)) {
1768bcc2412SMatthew Wilcox 		struct async_scan_data *next = list_entry(scanning_hosts.next,
1778bcc2412SMatthew Wilcox 				struct async_scan_data, list);
1788bcc2412SMatthew Wilcox 		complete(&next->prev_finished);
1798bcc2412SMatthew Wilcox 	}
1803e082a91SMatthew Wilcox  done:
1813e082a91SMatthew Wilcox 	spin_unlock(&async_scan_lock);
1823e082a91SMatthew Wilcox 
1833e082a91SMatthew Wilcox 	kfree(data);
1843e082a91SMatthew Wilcox 	return 0;
1853e082a91SMatthew Wilcox }
1863e082a91SMatthew Wilcox 
1873e082a91SMatthew Wilcox /* Only exported for the benefit of scsi_wait_scan */
1883e082a91SMatthew Wilcox EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
1893e082a91SMatthew Wilcox 
190f2f027c6SHugh Dickins #ifndef MODULE
191f2f027c6SHugh Dickins /*
192f2f027c6SHugh Dickins  * For async scanning we need to wait for all the scans to complete before
193f2f027c6SHugh Dickins  * trying to mount the root fs.  Otherwise non-modular drivers may not be ready
194f2f027c6SHugh Dickins  * yet.
195f2f027c6SHugh Dickins  */
196f2f027c6SHugh Dickins late_initcall(scsi_complete_async_scans);
197f2f027c6SHugh Dickins #endif
198f2f027c6SHugh Dickins 
1991da177e4SLinus Torvalds /**
2001da177e4SLinus Torvalds  * scsi_unlock_floptical - unlock device via a special MODE SENSE command
20139216033SJames Bottomley  * @sdev:	scsi device to send command to
2021da177e4SLinus Torvalds  * @result:	area to store the result of the MODE SENSE
2031da177e4SLinus Torvalds  *
2041da177e4SLinus Torvalds  * Description:
20539216033SJames Bottomley  *     Send a vendor specific MODE SENSE (not a MODE SELECT) command.
2061da177e4SLinus Torvalds  *     Called for BLIST_KEY devices.
2071da177e4SLinus Torvalds  **/
20839216033SJames Bottomley static void scsi_unlock_floptical(struct scsi_device *sdev,
2091da177e4SLinus Torvalds 				  unsigned char *result)
2101da177e4SLinus Torvalds {
2111da177e4SLinus Torvalds 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 	printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
2141da177e4SLinus Torvalds 	scsi_cmd[0] = MODE_SENSE;
2151da177e4SLinus Torvalds 	scsi_cmd[1] = 0;
2161da177e4SLinus Torvalds 	scsi_cmd[2] = 0x2e;
2171da177e4SLinus Torvalds 	scsi_cmd[3] = 0;
2181da177e4SLinus Torvalds 	scsi_cmd[4] = 0x2a;     /* size */
2191da177e4SLinus Torvalds 	scsi_cmd[5] = 0;
22039216033SJames Bottomley 	scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
221f4f4e47eSFUJITA Tomonori 			 SCSI_TIMEOUT, 3, NULL);
2221da177e4SLinus Torvalds }
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds /**
2251da177e4SLinus Torvalds  * scsi_alloc_sdev - allocate and setup a scsi_Device
226eb44820cSRob Landley  * @starget: which target to allocate a &scsi_device for
227eb44820cSRob Landley  * @lun: which lun
228eb44820cSRob Landley  * @hostdata: usually NULL and set by ->slave_alloc instead
2291da177e4SLinus Torvalds  *
2301da177e4SLinus Torvalds  * Description:
2311da177e4SLinus Torvalds  *     Allocate, initialize for io, and return a pointer to a scsi_Device.
2321da177e4SLinus Torvalds  *     Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
2331da177e4SLinus Torvalds  *     adds scsi_Device to the appropriate list.
2341da177e4SLinus Torvalds  *
2351da177e4SLinus Torvalds  * Return value:
2361da177e4SLinus Torvalds  *     scsi_Device pointer, or NULL on failure.
2371da177e4SLinus Torvalds  **/
2381da177e4SLinus Torvalds static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
2391da177e4SLinus Torvalds 					   unsigned int lun, void *hostdata)
2401da177e4SLinus Torvalds {
2411da177e4SLinus Torvalds 	struct scsi_device *sdev;
2421da177e4SLinus Torvalds 	int display_failure_msg = 1, ret;
2431da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
244a341cd0fSJeff Garzik 	extern void scsi_evt_thread(struct work_struct *work);
2451da177e4SLinus Torvalds 
24624669f75SJes Sorensen 	sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
2471da177e4SLinus Torvalds 		       GFP_ATOMIC);
2481da177e4SLinus Torvalds 	if (!sdev)
2491da177e4SLinus Torvalds 		goto out;
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	sdev->vendor = scsi_null_device_strs;
2521da177e4SLinus Torvalds 	sdev->model = scsi_null_device_strs;
2531da177e4SLinus Torvalds 	sdev->rev = scsi_null_device_strs;
2541da177e4SLinus Torvalds 	sdev->host = shost;
2554a84067dSVasu Dev 	sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD;
2561da177e4SLinus Torvalds 	sdev->id = starget->id;
2571da177e4SLinus Torvalds 	sdev->lun = lun;
2581da177e4SLinus Torvalds 	sdev->channel = starget->channel;
2591da177e4SLinus Torvalds 	sdev->sdev_state = SDEV_CREATED;
2601da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->siblings);
2611da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->same_target_siblings);
2621da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->cmd_list);
2631da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->starved_entry);
264a341cd0fSJeff Garzik 	INIT_LIST_HEAD(&sdev->event_list);
2651da177e4SLinus Torvalds 	spin_lock_init(&sdev->list_lock);
266a341cd0fSJeff Garzik 	INIT_WORK(&sdev->event_work, scsi_evt_thread);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	sdev->sdev_gendev.parent = get_device(&starget->dev);
2691da177e4SLinus Torvalds 	sdev->sdev_target = starget;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	/* usually NULL and set by ->slave_alloc instead */
2721da177e4SLinus Torvalds 	sdev->hostdata = hostdata;
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/* if the device needs this changing, it may do so in the
2751da177e4SLinus Torvalds 	 * slave_configure function */
2761da177e4SLinus Torvalds 	sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds 	/*
2791da177e4SLinus Torvalds 	 * Some low level driver could use device->type
2801da177e4SLinus Torvalds 	 */
2811da177e4SLinus Torvalds 	sdev->type = -1;
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	/*
2841da177e4SLinus Torvalds 	 * Assume that the device will have handshaking problems,
2851da177e4SLinus Torvalds 	 * and then fix this field later if it turns out it
2861da177e4SLinus Torvalds 	 * doesn't
2871da177e4SLinus Torvalds 	 */
2881da177e4SLinus Torvalds 	sdev->borken = 1;
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds 	sdev->request_queue = scsi_alloc_queue(sdev);
2911da177e4SLinus Torvalds 	if (!sdev->request_queue) {
2921da177e4SLinus Torvalds 		/* release fn is set up in scsi_sysfs_device_initialise, so
2931da177e4SLinus Torvalds 		 * have to free and put manually here */
2941da177e4SLinus Torvalds 		put_device(&starget->dev);
29593f56089SDave Jones 		kfree(sdev);
2961da177e4SLinus Torvalds 		goto out;
2971da177e4SLinus Torvalds 	}
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	sdev->request_queue->queuedata = sdev;
3001da177e4SLinus Torvalds 	scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	scsi_sysfs_device_initialize(sdev);
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (shost->hostt->slave_alloc) {
3051da177e4SLinus Torvalds 		ret = shost->hostt->slave_alloc(sdev);
3061da177e4SLinus Torvalds 		if (ret) {
3071da177e4SLinus Torvalds 			/*
3081da177e4SLinus Torvalds 			 * if LLDD reports slave not present, don't clutter
3091da177e4SLinus Torvalds 			 * console with alloc failure messages
3101da177e4SLinus Torvalds 			 */
3111da177e4SLinus Torvalds 			if (ret == -ENXIO)
3121da177e4SLinus Torvalds 				display_failure_msg = 0;
3131da177e4SLinus Torvalds 			goto out_device_destroy;
3141da177e4SLinus Torvalds 		}
3151da177e4SLinus Torvalds 	}
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	return sdev;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds out_device_destroy:
320c2f9e49fSJames Smart 	scsi_device_set_state(sdev, SDEV_DEL);
3211da177e4SLinus Torvalds 	transport_destroy_device(&sdev->sdev_gendev);
32237e6ba00SJames Bottomley 	put_device(&sdev->sdev_dev);
3231da177e4SLinus Torvalds 	put_device(&sdev->sdev_gendev);
3241da177e4SLinus Torvalds out:
3251da177e4SLinus Torvalds 	if (display_failure_msg)
326cadbd4a5SHarvey Harrison 		printk(ALLOC_FAILURE_MSG, __func__);
3271da177e4SLinus Torvalds 	return NULL;
3281da177e4SLinus Torvalds }
3291da177e4SLinus Torvalds 
330643eb2d9SJames Bottomley static void scsi_target_destroy(struct scsi_target *starget)
331643eb2d9SJames Bottomley {
332643eb2d9SJames Bottomley 	struct device *dev = &starget->dev;
333643eb2d9SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(dev->parent);
334643eb2d9SJames Bottomley 	unsigned long flags;
335643eb2d9SJames Bottomley 
336643eb2d9SJames Bottomley 	transport_destroy_device(dev);
337643eb2d9SJames Bottomley 	spin_lock_irqsave(shost->host_lock, flags);
338643eb2d9SJames Bottomley 	if (shost->hostt->target_destroy)
339643eb2d9SJames Bottomley 		shost->hostt->target_destroy(starget);
340643eb2d9SJames Bottomley 	list_del_init(&starget->siblings);
341643eb2d9SJames Bottomley 	spin_unlock_irqrestore(shost->host_lock, flags);
342643eb2d9SJames Bottomley 	put_device(dev);
343643eb2d9SJames Bottomley }
344643eb2d9SJames Bottomley 
3451da177e4SLinus Torvalds static void scsi_target_dev_release(struct device *dev)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	struct device *parent = dev->parent;
3481da177e4SLinus Torvalds 	struct scsi_target *starget = to_scsi_target(dev);
349a283bd37SJames Bottomley 
3501da177e4SLinus Torvalds 	kfree(starget);
3511da177e4SLinus Torvalds 	put_device(parent);
3521da177e4SLinus Torvalds }
3531da177e4SLinus Torvalds 
354453cd0f3SAdrian Bunk static struct device_type scsi_target_type = {
355b0ed4336SHannes Reinecke 	.name =		"scsi_target",
356b0ed4336SHannes Reinecke 	.release =	scsi_target_dev_release,
357b0ed4336SHannes Reinecke };
358b0ed4336SHannes Reinecke 
3591da177e4SLinus Torvalds int scsi_is_target_device(const struct device *dev)
3601da177e4SLinus Torvalds {
361b0ed4336SHannes Reinecke 	return dev->type == &scsi_target_type;
3621da177e4SLinus Torvalds }
3631da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_target_device);
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds static struct scsi_target *__scsi_find_target(struct device *parent,
3661da177e4SLinus Torvalds 					      int channel, uint id)
3671da177e4SLinus Torvalds {
3681da177e4SLinus Torvalds 	struct scsi_target *starget, *found_starget = NULL;
3691da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(parent);
3701da177e4SLinus Torvalds 	/*
3711da177e4SLinus Torvalds 	 * Search for an existing target for this sdev.
3721da177e4SLinus Torvalds 	 */
3731da177e4SLinus Torvalds 	list_for_each_entry(starget, &shost->__targets, siblings) {
3741da177e4SLinus Torvalds 		if (starget->id == id &&
3751da177e4SLinus Torvalds 		    starget->channel == channel) {
3761da177e4SLinus Torvalds 			found_starget = starget;
3771da177e4SLinus Torvalds 			break;
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 	}
3801da177e4SLinus Torvalds 	if (found_starget)
3811da177e4SLinus Torvalds 		get_device(&found_starget->dev);
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 	return found_starget;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
386884d25ccSJames Bottomley /**
387884d25ccSJames Bottomley  * scsi_alloc_target - allocate a new or find an existing target
388884d25ccSJames Bottomley  * @parent:	parent of the target (need not be a scsi host)
389884d25ccSJames Bottomley  * @channel:	target channel number (zero if no channels)
390884d25ccSJames Bottomley  * @id:		target id number
391884d25ccSJames Bottomley  *
392884d25ccSJames Bottomley  * Return an existing target if one exists, provided it hasn't already
393884d25ccSJames Bottomley  * gone into STARGET_DEL state, otherwise allocate a new target.
394884d25ccSJames Bottomley  *
395884d25ccSJames Bottomley  * The target is returned with an incremented reference, so the caller
396884d25ccSJames Bottomley  * is responsible for both reaping and doing a last put
397884d25ccSJames Bottomley  */
3981da177e4SLinus Torvalds static struct scsi_target *scsi_alloc_target(struct device *parent,
3991da177e4SLinus Torvalds 					     int channel, uint id)
4001da177e4SLinus Torvalds {
4011da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(parent);
4021da177e4SLinus Torvalds 	struct device *dev = NULL;
4031da177e4SLinus Torvalds 	unsigned long flags;
4041da177e4SLinus Torvalds 	const int size = sizeof(struct scsi_target)
4051da177e4SLinus Torvalds 		+ shost->transportt->target_size;
4065c44cd2aSJames.Smart@Emulex.Com 	struct scsi_target *starget;
4071da177e4SLinus Torvalds 	struct scsi_target *found_target;
40832f95792SBrian King 	int error;
4091da177e4SLinus Torvalds 
41024669f75SJes Sorensen 	starget = kzalloc(size, GFP_KERNEL);
4111da177e4SLinus Torvalds 	if (!starget) {
412cadbd4a5SHarvey Harrison 		printk(KERN_ERR "%s: allocation failure\n", __func__);
4131da177e4SLinus Torvalds 		return NULL;
4141da177e4SLinus Torvalds 	}
4151da177e4SLinus Torvalds 	dev = &starget->dev;
4161da177e4SLinus Torvalds 	device_initialize(dev);
4171da177e4SLinus Torvalds 	starget->reap_ref = 1;
4181da177e4SLinus Torvalds 	dev->parent = get_device(parent);
41971610f55SKay Sievers 	dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
420b0ed4336SHannes Reinecke #ifndef CONFIG_SYSFS_DEPRECATED
421b0ed4336SHannes Reinecke 	dev->bus = &scsi_bus_type;
422b0ed4336SHannes Reinecke #endif
423b0ed4336SHannes Reinecke 	dev->type = &scsi_target_type;
4241da177e4SLinus Torvalds 	starget->id = id;
4251da177e4SLinus Torvalds 	starget->channel = channel;
426f0c0a376SMike Christie 	starget->can_queue = 0;
4271da177e4SLinus Torvalds 	INIT_LIST_HEAD(&starget->siblings);
4281da177e4SLinus Torvalds 	INIT_LIST_HEAD(&starget->devices);
429643eb2d9SJames Bottomley 	starget->state = STARGET_CREATED;
4307c9d6f16SAlan Stern 	starget->scsi_level = SCSI_2;
431c53a284fSEdward Goggin 	starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED;
432ffedb452SJames Bottomley  retry:
4331da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds 	found_target = __scsi_find_target(parent, channel, id);
4361da177e4SLinus Torvalds 	if (found_target)
4371da177e4SLinus Torvalds 		goto found;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	list_add_tail(&starget->siblings, &shost->__targets);
4401da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
4411da177e4SLinus Torvalds 	/* allocate and add */
442a283bd37SJames Bottomley 	transport_setup_device(dev);
443a283bd37SJames Bottomley 	if (shost->hostt->target_alloc) {
44432f95792SBrian King 		error = shost->hostt->target_alloc(starget);
445a283bd37SJames Bottomley 
446a283bd37SJames Bottomley 		if(error) {
447a283bd37SJames Bottomley 			dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
448a283bd37SJames Bottomley 			/* don't want scsi_target_reap to do the final
449a283bd37SJames Bottomley 			 * put because it will be under the host lock */
450643eb2d9SJames Bottomley 			scsi_target_destroy(starget);
451a283bd37SJames Bottomley 			return NULL;
452a283bd37SJames Bottomley 		}
453a283bd37SJames Bottomley 	}
454884d25ccSJames Bottomley 	get_device(dev);
455a283bd37SJames Bottomley 
4561da177e4SLinus Torvalds 	return starget;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds  found:
4591da177e4SLinus Torvalds 	found_target->reap_ref++;
4601da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
461ffedb452SJames Bottomley 	if (found_target->state != STARGET_DEL) {
462*12fb8c15SAlan Stern 		put_device(dev);
4631da177e4SLinus Torvalds 		return found_target;
4641da177e4SLinus Torvalds 	}
465ffedb452SJames Bottomley 	/* Unfortunately, we found a dying target; need to
466ffedb452SJames Bottomley 	 * wait until it's dead before we can get a new one */
467ffedb452SJames Bottomley 	put_device(&found_target->dev);
468ffedb452SJames Bottomley 	flush_scheduled_work();
469ffedb452SJames Bottomley 	goto retry;
470ffedb452SJames Bottomley }
4711da177e4SLinus Torvalds 
47265f27f38SDavid Howells static void scsi_target_reap_usercontext(struct work_struct *work)
47365110b21SJames Bottomley {
47465f27f38SDavid Howells 	struct scsi_target *starget =
47565f27f38SDavid Howells 		container_of(work, struct scsi_target, ew.work);
476863a930aSJames Bottomley 
47704333393SJames Bottomley 	transport_remove_device(&starget->dev);
478863a930aSJames Bottomley 	device_del(&starget->dev);
479643eb2d9SJames Bottomley 	scsi_target_destroy(starget);
480863a930aSJames Bottomley }
481863a930aSJames Bottomley 
4821da177e4SLinus Torvalds /**
4831da177e4SLinus Torvalds  * scsi_target_reap - check to see if target is in use and destroy if not
4841da177e4SLinus Torvalds  * @starget: target to be checked
4851da177e4SLinus Torvalds  *
4861da177e4SLinus Torvalds  * This is used after removing a LUN or doing a last put of the target
4871da177e4SLinus Torvalds  * it checks atomically that nothing is using the target and removes
4881da177e4SLinus Torvalds  * it if so.
4891da177e4SLinus Torvalds  */
4901da177e4SLinus Torvalds void scsi_target_reap(struct scsi_target *starget)
4911da177e4SLinus Torvalds {
492ffedb452SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
493ffedb452SJames Bottomley 	unsigned long flags;
494643eb2d9SJames Bottomley 	enum scsi_target_state state;
495643eb2d9SJames Bottomley 	int empty;
496ffedb452SJames Bottomley 
497ffedb452SJames Bottomley 	spin_lock_irqsave(shost->host_lock, flags);
498643eb2d9SJames Bottomley 	state = starget->state;
499643eb2d9SJames Bottomley 	empty = --starget->reap_ref == 0 &&
500643eb2d9SJames Bottomley 		list_empty(&starget->devices) ? 1 : 0;
501ffedb452SJames Bottomley 	spin_unlock_irqrestore(shost->host_lock, flags);
502643eb2d9SJames Bottomley 
503643eb2d9SJames Bottomley 	if (!empty)
504643eb2d9SJames Bottomley 		return;
505643eb2d9SJames Bottomley 
506643eb2d9SJames Bottomley 	BUG_ON(state == STARGET_DEL);
507643eb2d9SJames Bottomley 	starget->state = STARGET_DEL;
508643eb2d9SJames Bottomley 	if (state == STARGET_CREATED)
509643eb2d9SJames Bottomley 		scsi_target_destroy(starget);
510643eb2d9SJames Bottomley 	else
511ffedb452SJames Bottomley 		execute_in_process_context(scsi_target_reap_usercontext,
51265f27f38SDavid Howells 					   &starget->ew);
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds /**
516e5b3cd42SAlan Stern  * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
517e5b3cd42SAlan Stern  * @s: INQUIRY result string to sanitize
518e5b3cd42SAlan Stern  * @len: length of the string
519e5b3cd42SAlan Stern  *
520e5b3cd42SAlan Stern  * Description:
521e5b3cd42SAlan Stern  *	The SCSI spec says that INQUIRY vendor, product, and revision
522e5b3cd42SAlan Stern  *	strings must consist entirely of graphic ASCII characters,
523e5b3cd42SAlan Stern  *	padded on the right with spaces.  Since not all devices obey
524e5b3cd42SAlan Stern  *	this rule, we will replace non-graphic or non-ASCII characters
525e5b3cd42SAlan Stern  *	with spaces.  Exception: a NUL character is interpreted as a
526e5b3cd42SAlan Stern  *	string terminator, so all the following characters are set to
527e5b3cd42SAlan Stern  *	spaces.
528e5b3cd42SAlan Stern  **/
529e5b3cd42SAlan Stern static void sanitize_inquiry_string(unsigned char *s, int len)
530e5b3cd42SAlan Stern {
531e5b3cd42SAlan Stern 	int terminated = 0;
532e5b3cd42SAlan Stern 
533e5b3cd42SAlan Stern 	for (; len > 0; (--len, ++s)) {
534e5b3cd42SAlan Stern 		if (*s == 0)
535e5b3cd42SAlan Stern 			terminated = 1;
536e5b3cd42SAlan Stern 		if (terminated || *s < 0x20 || *s > 0x7e)
537e5b3cd42SAlan Stern 			*s = ' ';
538e5b3cd42SAlan Stern 	}
539e5b3cd42SAlan Stern }
540e5b3cd42SAlan Stern 
541e5b3cd42SAlan Stern /**
5421da177e4SLinus Torvalds  * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
54339216033SJames Bottomley  * @sdev:	scsi_device to probe
5441da177e4SLinus Torvalds  * @inq_result:	area to store the INQUIRY result
54539216033SJames Bottomley  * @result_len: len of inq_result
5461da177e4SLinus Torvalds  * @bflags:	store any bflags found here
5471da177e4SLinus Torvalds  *
5481da177e4SLinus Torvalds  * Description:
54939216033SJames Bottomley  *     Probe the lun associated with @req using a standard SCSI INQUIRY;
5501da177e4SLinus Torvalds  *
55139216033SJames Bottomley  *     If the INQUIRY is successful, zero is returned and the
5521da177e4SLinus Torvalds  *     INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
553f64a181dSChristoph Hellwig  *     are copied to the scsi_device any flags value is stored in *@bflags.
5541da177e4SLinus Torvalds  **/
555e5b3cd42SAlan Stern static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
55639216033SJames Bottomley 			  int result_len, int *bflags)
5571da177e4SLinus Torvalds {
5581da177e4SLinus Torvalds 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
5591da177e4SLinus Torvalds 	int first_inquiry_len, try_inquiry_len, next_inquiry_len;
5601da177e4SLinus Torvalds 	int response_len = 0;
56139216033SJames Bottomley 	int pass, count, result;
5621da177e4SLinus Torvalds 	struct scsi_sense_hdr sshdr;
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	*bflags = 0;
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	/* Perform up to 3 passes.  The first pass uses a conservative
5671da177e4SLinus Torvalds 	 * transfer length of 36 unless sdev->inquiry_len specifies a
5681da177e4SLinus Torvalds 	 * different value. */
5691da177e4SLinus Torvalds 	first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
5701da177e4SLinus Torvalds 	try_inquiry_len = first_inquiry_len;
5711da177e4SLinus Torvalds 	pass = 1;
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds  next_pass:
5749ccfc756SJames Bottomley 	SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
5759ccfc756SJames Bottomley 				"scsi scan: INQUIRY pass %d length %d\n",
5769ccfc756SJames Bottomley 				pass, try_inquiry_len));
5771da177e4SLinus Torvalds 
5781da177e4SLinus Torvalds 	/* Each pass gets up to three chances to ignore Unit Attention */
5791da177e4SLinus Torvalds 	for (count = 0; count < 3; ++count) {
5805cd3bbfaSFUJITA Tomonori 		int resid;
5815cd3bbfaSFUJITA Tomonori 
5821da177e4SLinus Torvalds 		memset(scsi_cmd, 0, 6);
5831da177e4SLinus Torvalds 		scsi_cmd[0] = INQUIRY;
5841da177e4SLinus Torvalds 		scsi_cmd[4] = (unsigned char) try_inquiry_len;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 		memset(inq_result, 0, try_inquiry_len);
58739216033SJames Bottomley 
58839216033SJames Bottomley 		result = scsi_execute_req(sdev,  scsi_cmd, DMA_FROM_DEVICE,
589ea73a9f2SJames Bottomley 					  inq_result, try_inquiry_len, &sshdr,
590f4f4e47eSFUJITA Tomonori 					  HZ / 2 + HZ * scsi_inq_timeout, 3,
5915cd3bbfaSFUJITA Tomonori 					  &resid);
5921da177e4SLinus Torvalds 
5931da177e4SLinus Torvalds 		SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
5941da177e4SLinus Torvalds 				"with code 0x%x\n",
59539216033SJames Bottomley 				result ? "failed" : "successful", result));
5961da177e4SLinus Torvalds 
59739216033SJames Bottomley 		if (result) {
5981da177e4SLinus Torvalds 			/*
5991da177e4SLinus Torvalds 			 * not-ready to ready transition [asc/ascq=0x28/0x0]
6001da177e4SLinus Torvalds 			 * or power-on, reset [asc/ascq=0x29/0x0], continue.
6011da177e4SLinus Torvalds 			 * INQUIRY should not yield UNIT_ATTENTION
6021da177e4SLinus Torvalds 			 * but many buggy devices do so anyway.
6031da177e4SLinus Torvalds 			 */
60439216033SJames Bottomley 			if ((driver_byte(result) & DRIVER_SENSE) &&
605ea73a9f2SJames Bottomley 			    scsi_sense_valid(&sshdr)) {
6061da177e4SLinus Torvalds 				if ((sshdr.sense_key == UNIT_ATTENTION) &&
6071da177e4SLinus Torvalds 				    ((sshdr.asc == 0x28) ||
6081da177e4SLinus Torvalds 				     (sshdr.asc == 0x29)) &&
6091da177e4SLinus Torvalds 				    (sshdr.ascq == 0))
6101da177e4SLinus Torvalds 					continue;
6111da177e4SLinus Torvalds 			}
6125cd3bbfaSFUJITA Tomonori 		} else {
6135cd3bbfaSFUJITA Tomonori 			/*
6145cd3bbfaSFUJITA Tomonori 			 * if nothing was transferred, we try
6155cd3bbfaSFUJITA Tomonori 			 * again. It's a workaround for some USB
6165cd3bbfaSFUJITA Tomonori 			 * devices.
6175cd3bbfaSFUJITA Tomonori 			 */
6185cd3bbfaSFUJITA Tomonori 			if (resid == try_inquiry_len)
6195cd3bbfaSFUJITA Tomonori 				continue;
6201da177e4SLinus Torvalds 		}
6211da177e4SLinus Torvalds 		break;
6221da177e4SLinus Torvalds 	}
6231da177e4SLinus Torvalds 
62439216033SJames Bottomley 	if (result == 0) {
625e5b3cd42SAlan Stern 		sanitize_inquiry_string(&inq_result[8], 8);
626e5b3cd42SAlan Stern 		sanitize_inquiry_string(&inq_result[16], 16);
627e5b3cd42SAlan Stern 		sanitize_inquiry_string(&inq_result[32], 4);
628e5b3cd42SAlan Stern 
629e5b3cd42SAlan Stern 		response_len = inq_result[4] + 5;
6301da177e4SLinus Torvalds 		if (response_len > 255)
6311da177e4SLinus Torvalds 			response_len = first_inquiry_len;	/* sanity */
6321da177e4SLinus Torvalds 
6331da177e4SLinus Torvalds 		/*
6341da177e4SLinus Torvalds 		 * Get any flags for this device.
6351da177e4SLinus Torvalds 		 *
636f64a181dSChristoph Hellwig 		 * XXX add a bflags to scsi_device, and replace the
637f64a181dSChristoph Hellwig 		 * corresponding bit fields in scsi_device, so bflags
6381da177e4SLinus Torvalds 		 * need not be passed as an argument.
6391da177e4SLinus Torvalds 		 */
6401da177e4SLinus Torvalds 		*bflags = scsi_get_device_flags(sdev, &inq_result[8],
6411da177e4SLinus Torvalds 				&inq_result[16]);
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 		/* When the first pass succeeds we gain information about
6441da177e4SLinus Torvalds 		 * what larger transfer lengths might work. */
6451da177e4SLinus Torvalds 		if (pass == 1) {
6461da177e4SLinus Torvalds 			if (BLIST_INQUIRY_36 & *bflags)
6471da177e4SLinus Torvalds 				next_inquiry_len = 36;
6481da177e4SLinus Torvalds 			else if (BLIST_INQUIRY_58 & *bflags)
6491da177e4SLinus Torvalds 				next_inquiry_len = 58;
6501da177e4SLinus Torvalds 			else if (sdev->inquiry_len)
6511da177e4SLinus Torvalds 				next_inquiry_len = sdev->inquiry_len;
6521da177e4SLinus Torvalds 			else
6531da177e4SLinus Torvalds 				next_inquiry_len = response_len;
6541da177e4SLinus Torvalds 
6551da177e4SLinus Torvalds 			/* If more data is available perform the second pass */
6561da177e4SLinus Torvalds 			if (next_inquiry_len > try_inquiry_len) {
6571da177e4SLinus Torvalds 				try_inquiry_len = next_inquiry_len;
6581da177e4SLinus Torvalds 				pass = 2;
6591da177e4SLinus Torvalds 				goto next_pass;
6601da177e4SLinus Torvalds 			}
6611da177e4SLinus Torvalds 		}
6621da177e4SLinus Torvalds 
6631da177e4SLinus Torvalds 	} else if (pass == 2) {
6641da177e4SLinus Torvalds 		printk(KERN_INFO "scsi scan: %d byte inquiry failed.  "
6651da177e4SLinus Torvalds 				"Consider BLIST_INQUIRY_36 for this device\n",
6661da177e4SLinus Torvalds 				try_inquiry_len);
6671da177e4SLinus Torvalds 
6681da177e4SLinus Torvalds 		/* If this pass failed, the third pass goes back and transfers
6691da177e4SLinus Torvalds 		 * the same amount as we successfully got in the first pass. */
6701da177e4SLinus Torvalds 		try_inquiry_len = first_inquiry_len;
6711da177e4SLinus Torvalds 		pass = 3;
6721da177e4SLinus Torvalds 		goto next_pass;
6731da177e4SLinus Torvalds 	}
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	/* If the last transfer attempt got an error, assume the
6761da177e4SLinus Torvalds 	 * peripheral doesn't exist or is dead. */
67739216033SJames Bottomley 	if (result)
67839216033SJames Bottomley 		return -EIO;
6791da177e4SLinus Torvalds 
6801da177e4SLinus Torvalds 	/* Don't report any more data than the device says is valid */
6811da177e4SLinus Torvalds 	sdev->inquiry_len = min(try_inquiry_len, response_len);
6821da177e4SLinus Torvalds 
6831da177e4SLinus Torvalds 	/*
6841da177e4SLinus Torvalds 	 * XXX Abort if the response length is less than 36? If less than
6851da177e4SLinus Torvalds 	 * 32, the lookup of the device flags (above) could be invalid,
6861da177e4SLinus Torvalds 	 * and it would be possible to take an incorrect action - we do
6871da177e4SLinus Torvalds 	 * not want to hang because of a short INQUIRY. On the flip side,
6881da177e4SLinus Torvalds 	 * if the device is spun down or becoming ready (and so it gives a
6891da177e4SLinus Torvalds 	 * short INQUIRY), an abort here prevents any further use of the
6901da177e4SLinus Torvalds 	 * device, including spin up.
6911da177e4SLinus Torvalds 	 *
692e423ee31SAlan Stern 	 * On the whole, the best approach seems to be to assume the first
693e423ee31SAlan Stern 	 * 36 bytes are valid no matter what the device says.  That's
694e423ee31SAlan Stern 	 * better than copying < 36 bytes to the inquiry-result buffer
695e423ee31SAlan Stern 	 * and displaying garbage for the Vendor, Product, or Revision
696e423ee31SAlan Stern 	 * strings.
697e423ee31SAlan Stern 	 */
698e423ee31SAlan Stern 	if (sdev->inquiry_len < 36) {
699e423ee31SAlan Stern 		printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
700e423ee31SAlan Stern 				" using 36\n", sdev->inquiry_len);
701e423ee31SAlan Stern 		sdev->inquiry_len = 36;
702e423ee31SAlan Stern 	}
703e423ee31SAlan Stern 
704e423ee31SAlan Stern 	/*
7051da177e4SLinus Torvalds 	 * Related to the above issue:
7061da177e4SLinus Torvalds 	 *
7071da177e4SLinus Torvalds 	 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
7081da177e4SLinus Torvalds 	 * and if not ready, sent a START_STOP to start (maybe spin up) and
7091da177e4SLinus Torvalds 	 * then send the INQUIRY again, since the INQUIRY can change after
7101da177e4SLinus Torvalds 	 * a device is initialized.
7111da177e4SLinus Torvalds 	 *
7121da177e4SLinus Torvalds 	 * Ideally, start a device if explicitly asked to do so.  This
7131da177e4SLinus Torvalds 	 * assumes that a device is spun up on power on, spun down on
7141da177e4SLinus Torvalds 	 * request, and then spun up on request.
7151da177e4SLinus Torvalds 	 */
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds 	/*
7181da177e4SLinus Torvalds 	 * The scanning code needs to know the scsi_level, even if no
7191da177e4SLinus Torvalds 	 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
7201da177e4SLinus Torvalds 	 * non-zero LUNs can be scanned.
7211da177e4SLinus Torvalds 	 */
7221da177e4SLinus Torvalds 	sdev->scsi_level = inq_result[2] & 0x07;
7231da177e4SLinus Torvalds 	if (sdev->scsi_level >= 2 ||
7241da177e4SLinus Torvalds 	    (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
7251da177e4SLinus Torvalds 		sdev->scsi_level++;
7266f3a2024SJames Bottomley 	sdev->sdev_target->scsi_level = sdev->scsi_level;
7271da177e4SLinus Torvalds 
72839216033SJames Bottomley 	return 0;
7291da177e4SLinus Torvalds }
7301da177e4SLinus Torvalds 
7311da177e4SLinus Torvalds /**
732f64a181dSChristoph Hellwig  * scsi_add_lun - allocate and fully initialze a scsi_device
7336d877688SMatthew Wilcox  * @sdev:	holds information to be stored in the new scsi_device
7341da177e4SLinus Torvalds  * @inq_result:	holds the result of a previous INQUIRY to the LUN
7351da177e4SLinus Torvalds  * @bflags:	black/white list flag
7366d877688SMatthew Wilcox  * @async:	1 if this device is being scanned asynchronously
7371da177e4SLinus Torvalds  *
7381da177e4SLinus Torvalds  * Description:
7396d877688SMatthew Wilcox  *     Initialize the scsi_device @sdev.  Optionally set fields based
7406d877688SMatthew Wilcox  *     on values in *@bflags.
7411da177e4SLinus Torvalds  *
7421da177e4SLinus Torvalds  * Return:
743f64a181dSChristoph Hellwig  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
744f64a181dSChristoph Hellwig  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
7451da177e4SLinus Torvalds  **/
746e5b3cd42SAlan Stern static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
7473e082a91SMatthew Wilcox 		int *bflags, int async)
7481da177e4SLinus Torvalds {
7496f4267e3SJames Bottomley 	int ret;
7506f4267e3SJames Bottomley 
7511da177e4SLinus Torvalds 	/*
7521da177e4SLinus Torvalds 	 * XXX do not save the inquiry, since it can change underneath us,
7531da177e4SLinus Torvalds 	 * save just vendor/model/rev.
7541da177e4SLinus Torvalds 	 *
7551da177e4SLinus Torvalds 	 * Rather than save it and have an ioctl that retrieves the saved
7561da177e4SLinus Torvalds 	 * value, have an ioctl that executes the same INQUIRY code used
7571da177e4SLinus Torvalds 	 * in scsi_probe_lun, let user level programs doing INQUIRY
7581da177e4SLinus Torvalds 	 * scanning run at their own risk, or supply a user level program
7591da177e4SLinus Torvalds 	 * that can correctly scan.
7601da177e4SLinus Torvalds 	 */
7611da177e4SLinus Torvalds 
76209123d23SAlan Stern 	/*
76309123d23SAlan Stern 	 * Copy at least 36 bytes of INQUIRY data, so that we don't
76409123d23SAlan Stern 	 * dereference unallocated memory when accessing the Vendor,
76509123d23SAlan Stern 	 * Product, and Revision strings.  Badly behaved devices may set
76609123d23SAlan Stern 	 * the INQUIRY Additional Length byte to a small value, indicating
76709123d23SAlan Stern 	 * these strings are invalid, but often they contain plausible data
76809123d23SAlan Stern 	 * nonetheless.  It doesn't matter if the device sent < 36 bytes
76909123d23SAlan Stern 	 * total, since scsi_probe_lun() initializes inq_result with 0s.
77009123d23SAlan Stern 	 */
77109123d23SAlan Stern 	sdev->inquiry = kmemdup(inq_result,
77209123d23SAlan Stern 				max_t(size_t, sdev->inquiry_len, 36),
77309123d23SAlan Stern 				GFP_ATOMIC);
77409123d23SAlan Stern 	if (sdev->inquiry == NULL)
77509123d23SAlan Stern 		return SCSI_SCAN_NO_RESPONSE;
77609123d23SAlan Stern 
7771da177e4SLinus Torvalds 	sdev->vendor = (char *) (sdev->inquiry + 8);
7781da177e4SLinus Torvalds 	sdev->model = (char *) (sdev->inquiry + 16);
7791da177e4SLinus Torvalds 	sdev->rev = (char *) (sdev->inquiry + 32);
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds 	if (*bflags & BLIST_ISROM) {
7826d877688SMatthew Wilcox 		sdev->type = TYPE_ROM;
7836d877688SMatthew Wilcox 		sdev->removable = 1;
7846d877688SMatthew Wilcox 	} else {
7856d877688SMatthew Wilcox 		sdev->type = (inq_result[0] & 0x1f);
7866d877688SMatthew Wilcox 		sdev->removable = (inq_result[1] & 0x80) >> 7;
7876d877688SMatthew Wilcox 	}
7881da177e4SLinus Torvalds 
7896d877688SMatthew Wilcox 	switch (sdev->type) {
790ddaf6fc8SJames Bottomley 	case TYPE_RBC:
7911da177e4SLinus Torvalds 	case TYPE_TAPE:
7921da177e4SLinus Torvalds 	case TYPE_DISK:
7931da177e4SLinus Torvalds 	case TYPE_PRINTER:
7941da177e4SLinus Torvalds 	case TYPE_MOD:
7951da177e4SLinus Torvalds 	case TYPE_PROCESSOR:
7961da177e4SLinus Torvalds 	case TYPE_SCANNER:
7971da177e4SLinus Torvalds 	case TYPE_MEDIUM_CHANGER:
7981da177e4SLinus Torvalds 	case TYPE_ENCLOSURE:
7991da177e4SLinus Torvalds 	case TYPE_COMM:
8004d7db04aSJames Bottomley 	case TYPE_RAID:
80182443a58SBoaz Harrosh 	case TYPE_OSD:
8021da177e4SLinus Torvalds 		sdev->writeable = 1;
8031da177e4SLinus Torvalds 		break;
8041da177e4SLinus Torvalds 	case TYPE_ROM:
805ddaf6fc8SJames Bottomley 	case TYPE_WORM:
8061da177e4SLinus Torvalds 		sdev->writeable = 0;
8071da177e4SLinus Torvalds 		break;
8081da177e4SLinus Torvalds 	default:
8091da177e4SLinus Torvalds 		printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
8101da177e4SLinus Torvalds 	}
8111da177e4SLinus Torvalds 
8126d877688SMatthew Wilcox 	if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
8136d877688SMatthew Wilcox 		/* RBC and MMC devices can return SCSI-3 compliance and yet
8146d877688SMatthew Wilcox 		 * still not support REPORT LUNS, so make them act as
8156d877688SMatthew Wilcox 		 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
8166d877688SMatthew Wilcox 		 * specifically set */
8176d877688SMatthew Wilcox 		if ((*bflags & BLIST_REPORTLUN2) == 0)
8186d877688SMatthew Wilcox 			*bflags |= BLIST_NOREPORTLUN;
8196d877688SMatthew Wilcox 	}
8206d877688SMatthew Wilcox 
8211da177e4SLinus Torvalds 	/*
8221da177e4SLinus Torvalds 	 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
8231da177e4SLinus Torvalds 	 * spec says: The device server is capable of supporting the
8241da177e4SLinus Torvalds 	 * specified peripheral device type on this logical unit. However,
8251da177e4SLinus Torvalds 	 * the physical device is not currently connected to this logical
8261da177e4SLinus Torvalds 	 * unit.
8271da177e4SLinus Torvalds 	 *
8281da177e4SLinus Torvalds 	 * The above is vague, as it implies that we could treat 001 and
8291da177e4SLinus Torvalds 	 * 011 the same. Stay compatible with previous code, and create a
830f64a181dSChristoph Hellwig 	 * scsi_device for a PQ of 1
8311da177e4SLinus Torvalds 	 *
8321da177e4SLinus Torvalds 	 * Don't set the device offline here; rather let the upper
8331da177e4SLinus Torvalds 	 * level drivers eval the PQ to decide whether they should
8341da177e4SLinus Torvalds 	 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
8351da177e4SLinus Torvalds 	 */
8361da177e4SLinus Torvalds 
8371da177e4SLinus Torvalds 	sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
8381da177e4SLinus Torvalds 	sdev->lockable = sdev->removable;
8391da177e4SLinus Torvalds 	sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
8401da177e4SLinus Torvalds 
8416d877688SMatthew Wilcox 	if (sdev->scsi_level >= SCSI_3 ||
8426d877688SMatthew Wilcox 			(sdev->inquiry_len > 56 && inq_result[56] & 0x04))
8431da177e4SLinus Torvalds 		sdev->ppr = 1;
8441da177e4SLinus Torvalds 	if (inq_result[7] & 0x60)
8451da177e4SLinus Torvalds 		sdev->wdtr = 1;
8461da177e4SLinus Torvalds 	if (inq_result[7] & 0x10)
8471da177e4SLinus Torvalds 		sdev->sdtr = 1;
8481da177e4SLinus Torvalds 
84919ac0db3SJames Bottomley 	sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
8504ff36718SMatthew Wilcox 			"ANSI: %d%s\n", scsi_device_type(sdev->type),
8514ff36718SMatthew Wilcox 			sdev->vendor, sdev->model, sdev->rev,
8524ff36718SMatthew Wilcox 			sdev->inq_periph_qual, inq_result[2] & 0x07,
8534ff36718SMatthew Wilcox 			(inq_result[3] & 0x0f) == 1 ? " CCS" : "");
8544ff36718SMatthew Wilcox 
8551da177e4SLinus Torvalds 	if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
8561da177e4SLinus Torvalds 	    !(*bflags & BLIST_NOTQ))
8571da177e4SLinus Torvalds 		sdev->tagged_supported = 1;
8586d877688SMatthew Wilcox 
8591da177e4SLinus Torvalds 	/*
8601da177e4SLinus Torvalds 	 * Some devices (Texel CD ROM drives) have handshaking problems
8611da177e4SLinus Torvalds 	 * when used with the Seagate controllers. borken is initialized
8621da177e4SLinus Torvalds 	 * to 1, and then set it to 0 here.
8631da177e4SLinus Torvalds 	 */
8641da177e4SLinus Torvalds 	if ((*bflags & BLIST_BORKEN) == 0)
8651da177e4SLinus Torvalds 		sdev->borken = 0;
8661da177e4SLinus Torvalds 
8676d877688SMatthew Wilcox 	if (*bflags & BLIST_NO_ULD_ATTACH)
8686d877688SMatthew Wilcox 		sdev->no_uld_attach = 1;
8696d877688SMatthew Wilcox 
8701da177e4SLinus Torvalds 	/*
8711da177e4SLinus Torvalds 	 * Apparently some really broken devices (contrary to the SCSI
8721da177e4SLinus Torvalds 	 * standards) need to be selected without asserting ATN
8731da177e4SLinus Torvalds 	 */
8741da177e4SLinus Torvalds 	if (*bflags & BLIST_SELECT_NO_ATN)
8751da177e4SLinus Torvalds 		sdev->select_no_atn = 1;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	/*
8784d7db04aSJames Bottomley 	 * Maximum 512 sector transfer length
8794d7db04aSJames Bottomley 	 * broken RA4x00 Compaq Disk Array
8804d7db04aSJames Bottomley 	 */
8814d7db04aSJames Bottomley 	if (*bflags & BLIST_MAX_512)
882086fa5ffSMartin K. Petersen 		blk_queue_max_hw_sectors(sdev->request_queue, 512);
8834d7db04aSJames Bottomley 
8844d7db04aSJames Bottomley 	/*
8851da177e4SLinus Torvalds 	 * Some devices may not want to have a start command automatically
8861da177e4SLinus Torvalds 	 * issued when a device is added.
8871da177e4SLinus Torvalds 	 */
8881da177e4SLinus Torvalds 	if (*bflags & BLIST_NOSTARTONADD)
8891da177e4SLinus Torvalds 		sdev->no_start_on_add = 1;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (*bflags & BLIST_SINGLELUN)
89225d7c363STony Battersby 		scsi_target(sdev)->single_lun = 1;
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	sdev->use_10_for_rw = 1;
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds 	if (*bflags & BLIST_MS_SKIP_PAGE_08)
8971da177e4SLinus Torvalds 		sdev->skip_ms_page_8 = 1;
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 	if (*bflags & BLIST_MS_SKIP_PAGE_3F)
9001da177e4SLinus Torvalds 		sdev->skip_ms_page_3f = 1;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if (*bflags & BLIST_USE_10_BYTE_MS)
9031da177e4SLinus Torvalds 		sdev->use_10_for_ms = 1;
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	/* set the device running here so that slave configure
9061da177e4SLinus Torvalds 	 * may do I/O */
9076f4267e3SJames Bottomley 	ret = scsi_device_set_state(sdev, SDEV_RUNNING);
9086f4267e3SJames Bottomley 	if (ret) {
9096f4267e3SJames Bottomley 		ret = scsi_device_set_state(sdev, SDEV_BLOCK);
9106f4267e3SJames Bottomley 
9116f4267e3SJames Bottomley 		if (ret) {
9126f4267e3SJames Bottomley 			sdev_printk(KERN_ERR, sdev,
9136f4267e3SJames Bottomley 				    "in wrong state %s to complete scan\n",
9146f4267e3SJames Bottomley 				    scsi_device_state_name(sdev->sdev_state));
9156f4267e3SJames Bottomley 			return SCSI_SCAN_NO_RESPONSE;
9166f4267e3SJames Bottomley 		}
9176f4267e3SJames Bottomley 	}
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
9201da177e4SLinus Torvalds 		sdev->use_192_bytes_for_3f = 1;
9211da177e4SLinus Torvalds 
9221da177e4SLinus Torvalds 	if (*bflags & BLIST_NOT_LOCKABLE)
9231da177e4SLinus Torvalds 		sdev->lockable = 0;
9241da177e4SLinus Torvalds 
9251da177e4SLinus Torvalds 	if (*bflags & BLIST_RETRY_HWERROR)
9261da177e4SLinus Torvalds 		sdev->retry_hwerror = 1;
9271da177e4SLinus Torvalds 
9281da177e4SLinus Torvalds 	transport_configure_device(&sdev->sdev_gendev);
9291da177e4SLinus Torvalds 
93093805091SChristoph Hellwig 	if (sdev->host->hostt->slave_configure) {
9316f4267e3SJames Bottomley 		ret = sdev->host->hostt->slave_configure(sdev);
93293805091SChristoph Hellwig 		if (ret) {
93393805091SChristoph Hellwig 			/*
93493805091SChristoph Hellwig 			 * if LLDD reports slave not present, don't clutter
93593805091SChristoph Hellwig 			 * console with alloc failure messages
93693805091SChristoph Hellwig 			 */
93793805091SChristoph Hellwig 			if (ret != -ENXIO) {
93893805091SChristoph Hellwig 				sdev_printk(KERN_ERR, sdev,
93993805091SChristoph Hellwig 					"failed to configure device\n");
94093805091SChristoph Hellwig 			}
94193805091SChristoph Hellwig 			return SCSI_SCAN_NO_RESPONSE;
94293805091SChristoph Hellwig 		}
94393805091SChristoph Hellwig 	}
9441da177e4SLinus Torvalds 
9454a84067dSVasu Dev 	sdev->max_queue_depth = sdev->queue_depth;
9464a84067dSVasu Dev 
9471da177e4SLinus Torvalds 	/*
9481da177e4SLinus Torvalds 	 * Ok, the device is now all set up, we can
9491da177e4SLinus Torvalds 	 * register it and tell the rest of the kernel
9501da177e4SLinus Torvalds 	 * about it.
9511da177e4SLinus Torvalds 	 */
9523e082a91SMatthew Wilcox 	if (!async && scsi_sysfs_add_sdev(sdev) != 0)
953b24b1033SAlan Stern 		return SCSI_SCAN_NO_RESPONSE;
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	return SCSI_SCAN_LUN_PRESENT;
9561da177e4SLinus Torvalds }
9571da177e4SLinus Torvalds 
958c5f2e640Sakpm@osdl.org #ifdef CONFIG_SCSI_LOGGING
9596c7154c9SKurt Garloff /**
960eb44820cSRob Landley  * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace
9616c7154c9SKurt Garloff  * @buf:   Output buffer with at least end-first+1 bytes of space
9626c7154c9SKurt Garloff  * @inq:   Inquiry buffer (input)
9636c7154c9SKurt Garloff  * @first: Offset of string into inq
9646c7154c9SKurt Garloff  * @end:   Index after last character in inq
9656c7154c9SKurt Garloff  */
9666c7154c9SKurt Garloff static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
9676c7154c9SKurt Garloff 				   unsigned first, unsigned end)
9686c7154c9SKurt Garloff {
9696c7154c9SKurt Garloff 	unsigned term = 0, idx;
970c5f2e640Sakpm@osdl.org 
971c5f2e640Sakpm@osdl.org 	for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
972c5f2e640Sakpm@osdl.org 		if (inq[idx+first] > ' ') {
9736c7154c9SKurt Garloff 			buf[idx] = inq[idx+first];
9746c7154c9SKurt Garloff 			term = idx+1;
9756c7154c9SKurt Garloff 		} else {
9766c7154c9SKurt Garloff 			buf[idx] = ' ';
9776c7154c9SKurt Garloff 		}
9786c7154c9SKurt Garloff 	}
9796c7154c9SKurt Garloff 	buf[term] = 0;
9806c7154c9SKurt Garloff 	return buf;
9816c7154c9SKurt Garloff }
982c5f2e640Sakpm@osdl.org #endif
9836f3a2024SJames Bottomley 
9841da177e4SLinus Torvalds /**
9851da177e4SLinus Torvalds  * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
9861da177e4SLinus Torvalds  * @starget:	pointer to target device structure
9871da177e4SLinus Torvalds  * @lun:	LUN of target device
9881da177e4SLinus Torvalds  * @bflagsp:	store bflags here if not NULL
989eb44820cSRob Landley  * @sdevp:	probe the LUN corresponding to this scsi_device
990eb44820cSRob Landley  * @rescan:     if nonzero skip some code only needed on first scan
991eb44820cSRob Landley  * @hostdata:	passed to scsi_alloc_sdev()
9921da177e4SLinus Torvalds  *
9931da177e4SLinus Torvalds  * Description:
9941da177e4SLinus Torvalds  *     Call scsi_probe_lun, if a LUN with an attached device is found,
9951da177e4SLinus Torvalds  *     allocate and set it up by calling scsi_add_lun.
9961da177e4SLinus Torvalds  *
9971da177e4SLinus Torvalds  * Return:
998f64a181dSChristoph Hellwig  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
9991da177e4SLinus Torvalds  *     SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
10001da177e4SLinus Torvalds  *         attached at the LUN
1001f64a181dSChristoph Hellwig  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
10021da177e4SLinus Torvalds  **/
10031da177e4SLinus Torvalds static int scsi_probe_and_add_lun(struct scsi_target *starget,
10041da177e4SLinus Torvalds 				  uint lun, int *bflagsp,
10051da177e4SLinus Torvalds 				  struct scsi_device **sdevp, int rescan,
10061da177e4SLinus Torvalds 				  void *hostdata)
10071da177e4SLinus Torvalds {
10081da177e4SLinus Torvalds 	struct scsi_device *sdev;
10091da177e4SLinus Torvalds 	unsigned char *result;
101039216033SJames Bottomley 	int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
10111da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	/*
10141da177e4SLinus Torvalds 	 * The rescan flag is used as an optimization, the first scan of a
10151da177e4SLinus Torvalds 	 * host adapter calls into here with rescan == 0.
10161da177e4SLinus Torvalds 	 */
10171da177e4SLinus Torvalds 	sdev = scsi_device_lookup_by_target(starget, lun);
10181da177e4SLinus Torvalds 	if (sdev) {
10190f1d87a2SJames Bottomley 		if (rescan || !scsi_device_created(sdev)) {
10201da177e4SLinus Torvalds 			SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
10211da177e4SLinus Torvalds 				"scsi scan: device exists on %s\n",
102271610f55SKay Sievers 				dev_name(&sdev->sdev_gendev)));
10231da177e4SLinus Torvalds 			if (sdevp)
10241da177e4SLinus Torvalds 				*sdevp = sdev;
10251da177e4SLinus Torvalds 			else
10261da177e4SLinus Torvalds 				scsi_device_put(sdev);
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 			if (bflagsp)
10291da177e4SLinus Torvalds 				*bflagsp = scsi_get_device_flags(sdev,
10301da177e4SLinus Torvalds 								 sdev->vendor,
10311da177e4SLinus Torvalds 								 sdev->model);
10321da177e4SLinus Torvalds 			return SCSI_SCAN_LUN_PRESENT;
10331da177e4SLinus Torvalds 		}
10346f3a2024SJames Bottomley 		scsi_device_put(sdev);
10356f3a2024SJames Bottomley 	} else
10361da177e4SLinus Torvalds 		sdev = scsi_alloc_sdev(starget, lun, hostdata);
10371da177e4SLinus Torvalds 	if (!sdev)
10381da177e4SLinus Torvalds 		goto out;
103939216033SJames Bottomley 
104039216033SJames Bottomley 	result = kmalloc(result_len, GFP_ATOMIC |
1041bc86120aSAl Viro 			((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
10421da177e4SLinus Torvalds 	if (!result)
104339216033SJames Bottomley 		goto out_free_sdev;
10441da177e4SLinus Torvalds 
104539216033SJames Bottomley 	if (scsi_probe_lun(sdev, result, result_len, &bflags))
10461da177e4SLinus Torvalds 		goto out_free_result;
10471da177e4SLinus Torvalds 
10484186ab19SKurt Garloff 	if (bflagsp)
10494186ab19SKurt Garloff 		*bflagsp = bflags;
10501da177e4SLinus Torvalds 	/*
10511da177e4SLinus Torvalds 	 * result contains valid SCSI INQUIRY data.
10521da177e4SLinus Torvalds 	 */
105313f7e5acSKurt Garloff 	if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
10541da177e4SLinus Torvalds 		/*
10551da177e4SLinus Torvalds 		 * For a Peripheral qualifier 3 (011b), the SCSI
10561da177e4SLinus Torvalds 		 * spec says: The device server is not capable of
10571da177e4SLinus Torvalds 		 * supporting a physical device on this logical
10581da177e4SLinus Torvalds 		 * unit.
10591da177e4SLinus Torvalds 		 *
10601da177e4SLinus Torvalds 		 * For disks, this implies that there is no
10611da177e4SLinus Torvalds 		 * logical disk configured at sdev->lun, but there
10621da177e4SLinus Torvalds 		 * is a target id responding.
10631da177e4SLinus Torvalds 		 */
10646c7154c9SKurt Garloff 		SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
10656c7154c9SKurt Garloff 				   " peripheral qualifier of 3, device not"
10666c7154c9SKurt Garloff 				   " added\n"))
10676c7154c9SKurt Garloff 		if (lun == 0) {
1068c5f2e640Sakpm@osdl.org 			SCSI_LOG_SCAN_BUS(1, {
1069c5f2e640Sakpm@osdl.org 				unsigned char vend[9];
1070c5f2e640Sakpm@osdl.org 				unsigned char mod[17];
1071c5f2e640Sakpm@osdl.org 
1072c5f2e640Sakpm@osdl.org 				sdev_printk(KERN_INFO, sdev,
10736c7154c9SKurt Garloff 					"scsi scan: consider passing scsi_mod."
10743424a65dSKurt Garloff 					"dev_flags=%s:%s:0x240 or 0x1000240\n",
10756c7154c9SKurt Garloff 					scsi_inq_str(vend, result, 8, 16),
1076c5f2e640Sakpm@osdl.org 					scsi_inq_str(mod, result, 16, 32));
1077c5f2e640Sakpm@osdl.org 			});
1078643eb2d9SJames Bottomley 
10796c7154c9SKurt Garloff 		}
10806c7154c9SKurt Garloff 
10811da177e4SLinus Torvalds 		res = SCSI_SCAN_TARGET_PRESENT;
10821da177e4SLinus Torvalds 		goto out_free_result;
10831da177e4SLinus Torvalds 	}
10841da177e4SLinus Torvalds 
10851bfc5d9dSAlan Stern 	/*
108684961f28Sdave wysochanski 	 * Some targets may set slight variations of PQ and PDT to signal
108784961f28Sdave wysochanski 	 * that no LUN is present, so don't add sdev in these cases.
108884961f28Sdave wysochanski 	 * Two specific examples are:
108984961f28Sdave wysochanski 	 * 1) NetApp targets: return PQ=1, PDT=0x1f
109084961f28Sdave wysochanski 	 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
109184961f28Sdave wysochanski 	 *    in the UFI 1.0 spec (we cannot rely on reserved bits).
109284961f28Sdave wysochanski 	 *
109384961f28Sdave wysochanski 	 * References:
109484961f28Sdave wysochanski 	 * 1) SCSI SPC-3, pp. 145-146
109584961f28Sdave wysochanski 	 * PQ=1: "A peripheral device having the specified peripheral
109684961f28Sdave wysochanski 	 * device type is not connected to this logical unit. However, the
109784961f28Sdave wysochanski 	 * device server is capable of supporting the specified peripheral
109884961f28Sdave wysochanski 	 * device type on this logical unit."
109984961f28Sdave wysochanski 	 * PDT=0x1f: "Unknown or no device type"
110084961f28Sdave wysochanski 	 * 2) USB UFI 1.0, p. 20
110184961f28Sdave wysochanski 	 * PDT=00h Direct-access device (floppy)
110284961f28Sdave wysochanski 	 * PDT=1Fh none (no FDD connected to the requested logical unit)
11031bfc5d9dSAlan Stern 	 */
110484961f28Sdave wysochanski 	if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
110501b291bdSJames Bottomley 	    (result[0] & 0x1f) == 0x1f &&
110601b291bdSJames Bottomley 	    !scsi_is_wlun(lun)) {
11071bfc5d9dSAlan Stern 		SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
11081bfc5d9dSAlan Stern 					"scsi scan: peripheral device type"
11091bfc5d9dSAlan Stern 					" of 31, no device added\n"));
11101bfc5d9dSAlan Stern 		res = SCSI_SCAN_TARGET_PRESENT;
11111bfc5d9dSAlan Stern 		goto out_free_result;
11121bfc5d9dSAlan Stern 	}
11131bfc5d9dSAlan Stern 
11143e082a91SMatthew Wilcox 	res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
11151da177e4SLinus Torvalds 	if (res == SCSI_SCAN_LUN_PRESENT) {
11161da177e4SLinus Torvalds 		if (bflags & BLIST_KEY) {
11171da177e4SLinus Torvalds 			sdev->lockable = 0;
111839216033SJames Bottomley 			scsi_unlock_floptical(sdev, result);
11191da177e4SLinus Torvalds 		}
11201da177e4SLinus Torvalds 	}
11211da177e4SLinus Torvalds 
11221da177e4SLinus Torvalds  out_free_result:
11231da177e4SLinus Torvalds 	kfree(result);
11241da177e4SLinus Torvalds  out_free_sdev:
11251da177e4SLinus Torvalds 	if (res == SCSI_SCAN_LUN_PRESENT) {
11261da177e4SLinus Torvalds 		if (sdevp) {
1127b70d37bfSAlan Stern 			if (scsi_device_get(sdev) == 0) {
11281da177e4SLinus Torvalds 				*sdevp = sdev;
1129b70d37bfSAlan Stern 			} else {
1130b70d37bfSAlan Stern 				__scsi_remove_device(sdev);
1131b70d37bfSAlan Stern 				res = SCSI_SCAN_NO_RESPONSE;
1132b70d37bfSAlan Stern 			}
11331da177e4SLinus Torvalds 		}
11346f3a2024SJames Bottomley 	} else
1135860dc736SJames Bottomley 		__scsi_remove_device(sdev);
11361da177e4SLinus Torvalds  out:
11371da177e4SLinus Torvalds 	return res;
11381da177e4SLinus Torvalds }
11391da177e4SLinus Torvalds 
11401da177e4SLinus Torvalds /**
11411da177e4SLinus Torvalds  * scsi_sequential_lun_scan - sequentially scan a SCSI target
11421da177e4SLinus Torvalds  * @starget:	pointer to target structure to scan
11431da177e4SLinus Torvalds  * @bflags:	black/white list flag for LUN 0
1144eb44820cSRob Landley  * @scsi_level: Which version of the standard does this device adhere to
1145eb44820cSRob Landley  * @rescan:     passed to scsi_probe_add_lun()
11461da177e4SLinus Torvalds  *
11471da177e4SLinus Torvalds  * Description:
11481da177e4SLinus Torvalds  *     Generally, scan from LUN 1 (LUN 0 is assumed to already have been
11491da177e4SLinus Torvalds  *     scanned) to some maximum lun until a LUN is found with no device
11501da177e4SLinus Torvalds  *     attached. Use the bflags to figure out any oddities.
11511da177e4SLinus Torvalds  *
11521da177e4SLinus Torvalds  *     Modifies sdevscan->lun.
11531da177e4SLinus Torvalds  **/
11541da177e4SLinus Torvalds static void scsi_sequential_lun_scan(struct scsi_target *starget,
11554186ab19SKurt Garloff 				     int bflags, int scsi_level, int rescan)
11561da177e4SLinus Torvalds {
11571da177e4SLinus Torvalds 	unsigned int sparse_lun, lun, max_dev_lun;
11581da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
11591da177e4SLinus Torvalds 
11601da177e4SLinus Torvalds 	SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
116171610f55SKay Sievers 				    "%s\n", dev_name(&starget->dev)));
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	max_dev_lun = min(max_scsi_luns, shost->max_lun);
11641da177e4SLinus Torvalds 	/*
11651da177e4SLinus Torvalds 	 * If this device is known to support sparse multiple units,
11661da177e4SLinus Torvalds 	 * override the other settings, and scan all of them. Normally,
11671da177e4SLinus Torvalds 	 * SCSI-3 devices should be scanned via the REPORT LUNS.
11681da177e4SLinus Torvalds 	 */
11691da177e4SLinus Torvalds 	if (bflags & BLIST_SPARSELUN) {
11701da177e4SLinus Torvalds 		max_dev_lun = shost->max_lun;
11711da177e4SLinus Torvalds 		sparse_lun = 1;
11721da177e4SLinus Torvalds 	} else
11731da177e4SLinus Torvalds 		sparse_lun = 0;
11741da177e4SLinus Torvalds 
11751da177e4SLinus Torvalds 	/*
11761da177e4SLinus Torvalds 	 * If less than SCSI_1_CSS, and no special lun scaning, stop
11771da177e4SLinus Torvalds 	 * scanning; this matches 2.4 behaviour, but could just be a bug
11781da177e4SLinus Torvalds 	 * (to continue scanning a SCSI_1_CSS device).
11791da177e4SLinus Torvalds 	 *
11801da177e4SLinus Torvalds 	 * This test is broken.  We might not have any device on lun0 for
11811da177e4SLinus Torvalds 	 * a sparselun device, and if that's the case then how would we
11821da177e4SLinus Torvalds 	 * know the real scsi_level, eh?  It might make sense to just not
11831da177e4SLinus Torvalds 	 * scan any SCSI_1 device for non-0 luns, but that check would best
11841da177e4SLinus Torvalds 	 * go into scsi_alloc_sdev() and just have it return null when asked
11851da177e4SLinus Torvalds 	 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
11861da177e4SLinus Torvalds 	 *
11871da177e4SLinus Torvalds 	if ((sdevscan->scsi_level < SCSI_1_CCS) &&
11881da177e4SLinus Torvalds 	    ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
11891da177e4SLinus Torvalds 	     == 0))
11901da177e4SLinus Torvalds 		return;
11911da177e4SLinus Torvalds 	 */
11921da177e4SLinus Torvalds 	/*
11931da177e4SLinus Torvalds 	 * If this device is known to support multiple units, override
11941da177e4SLinus Torvalds 	 * the other settings, and scan all of them.
11951da177e4SLinus Torvalds 	 */
11961da177e4SLinus Torvalds 	if (bflags & BLIST_FORCELUN)
11971da177e4SLinus Torvalds 		max_dev_lun = shost->max_lun;
11981da177e4SLinus Torvalds 	/*
11991da177e4SLinus Torvalds 	 * REGAL CDC-4X: avoid hang after LUN 4
12001da177e4SLinus Torvalds 	 */
12011da177e4SLinus Torvalds 	if (bflags & BLIST_MAX5LUN)
12021da177e4SLinus Torvalds 		max_dev_lun = min(5U, max_dev_lun);
12031da177e4SLinus Torvalds 	/*
12041da177e4SLinus Torvalds 	 * Do not scan SCSI-2 or lower device past LUN 7, unless
12051da177e4SLinus Torvalds 	 * BLIST_LARGELUN.
12061da177e4SLinus Torvalds 	 */
12071da177e4SLinus Torvalds 	if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
12081da177e4SLinus Torvalds 		max_dev_lun = min(8U, max_dev_lun);
12091da177e4SLinus Torvalds 
12101da177e4SLinus Torvalds 	/*
12111da177e4SLinus Torvalds 	 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
12121da177e4SLinus Torvalds 	 * until we reach the max, or no LUN is found and we are not
12131da177e4SLinus Torvalds 	 * sparse_lun.
12141da177e4SLinus Torvalds 	 */
12151da177e4SLinus Torvalds 	for (lun = 1; lun < max_dev_lun; ++lun)
12161da177e4SLinus Torvalds 		if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
12171da177e4SLinus Torvalds 					    NULL) != SCSI_SCAN_LUN_PRESENT) &&
12181da177e4SLinus Torvalds 		    !sparse_lun)
12191da177e4SLinus Torvalds 			return;
12201da177e4SLinus Torvalds }
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds /**
12231da177e4SLinus Torvalds  * scsilun_to_int: convert a scsi_lun to an int
12241da177e4SLinus Torvalds  * @scsilun:	struct scsi_lun to be converted.
12251da177e4SLinus Torvalds  *
12261da177e4SLinus Torvalds  * Description:
12271da177e4SLinus Torvalds  *     Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
12281da177e4SLinus Torvalds  *     integer, and return the result. The caller must check for
12291da177e4SLinus Torvalds  *     truncation before using this function.
12301da177e4SLinus Torvalds  *
12311da177e4SLinus Torvalds  * Notes:
12321da177e4SLinus Torvalds  *     The struct scsi_lun is assumed to be four levels, with each level
12331da177e4SLinus Torvalds  *     effectively containing a SCSI byte-ordered (big endian) short; the
12341da177e4SLinus Torvalds  *     addressing bits of each level are ignored (the highest two bits).
12351da177e4SLinus Torvalds  *     For a description of the LUN format, post SCSI-3 see the SCSI
12361da177e4SLinus Torvalds  *     Architecture Model, for SCSI-3 see the SCSI Controller Commands.
12371da177e4SLinus Torvalds  *
12381da177e4SLinus Torvalds  *     Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
12391da177e4SLinus Torvalds  *     the integer: 0x0b030a04
12401da177e4SLinus Torvalds  **/
1241462b7859SChristof Schmitt int scsilun_to_int(struct scsi_lun *scsilun)
12421da177e4SLinus Torvalds {
12431da177e4SLinus Torvalds 	int i;
12441da177e4SLinus Torvalds 	unsigned int lun;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	lun = 0;
12471da177e4SLinus Torvalds 	for (i = 0; i < sizeof(lun); i += 2)
12481da177e4SLinus Torvalds 		lun = lun | (((scsilun->scsi_lun[i] << 8) |
12491da177e4SLinus Torvalds 			      scsilun->scsi_lun[i + 1]) << (i * 8));
12501da177e4SLinus Torvalds 	return lun;
12511da177e4SLinus Torvalds }
1252462b7859SChristof Schmitt EXPORT_SYMBOL(scsilun_to_int);
12531da177e4SLinus Torvalds 
12541da177e4SLinus Torvalds /**
12552f4701d8SJames.Smart@Emulex.Com  * int_to_scsilun: reverts an int into a scsi_lun
1256eb44820cSRob Landley  * @lun:        integer to be reverted
12572f4701d8SJames.Smart@Emulex.Com  * @scsilun:	struct scsi_lun to be set.
12582f4701d8SJames.Smart@Emulex.Com  *
12592f4701d8SJames.Smart@Emulex.Com  * Description:
12602f4701d8SJames.Smart@Emulex.Com  *     Reverts the functionality of the scsilun_to_int, which packed
12612f4701d8SJames.Smart@Emulex.Com  *     an 8-byte lun value into an int. This routine unpacks the int
12622f4701d8SJames.Smart@Emulex.Com  *     back into the lun value.
12632f4701d8SJames.Smart@Emulex.Com  *     Note: the scsilun_to_int() routine does not truly handle all
12642f4701d8SJames.Smart@Emulex.Com  *     8bytes of the lun value. This functions restores only as much
12652f4701d8SJames.Smart@Emulex.Com  *     as was set by the routine.
12662f4701d8SJames.Smart@Emulex.Com  *
12672f4701d8SJames.Smart@Emulex.Com  * Notes:
12682f4701d8SJames.Smart@Emulex.Com  *     Given an integer : 0x0b030a04,  this function returns a
12692f4701d8SJames.Smart@Emulex.Com  *     scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
12702f4701d8SJames.Smart@Emulex.Com  *
12712f4701d8SJames.Smart@Emulex.Com  **/
12722f4701d8SJames.Smart@Emulex.Com void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
12732f4701d8SJames.Smart@Emulex.Com {
12742f4701d8SJames.Smart@Emulex.Com 	int i;
12752f4701d8SJames.Smart@Emulex.Com 
12762f4701d8SJames.Smart@Emulex.Com 	memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
12772f4701d8SJames.Smart@Emulex.Com 
12782f4701d8SJames.Smart@Emulex.Com 	for (i = 0; i < sizeof(lun); i += 2) {
12792f4701d8SJames.Smart@Emulex.Com 		scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
12802f4701d8SJames.Smart@Emulex.Com 		scsilun->scsi_lun[i+1] = lun & 0xFF;
12812f4701d8SJames.Smart@Emulex.Com 		lun = lun >> 16;
12822f4701d8SJames.Smart@Emulex.Com 	}
12832f4701d8SJames.Smart@Emulex.Com }
12842f4701d8SJames.Smart@Emulex.Com EXPORT_SYMBOL(int_to_scsilun);
12852f4701d8SJames.Smart@Emulex.Com 
12862f4701d8SJames.Smart@Emulex.Com /**
12871da177e4SLinus Torvalds  * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1288eb44820cSRob Landley  * @starget: which target
1289eb44820cSRob Landley  * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN
1290eb44820cSRob Landley  * @rescan: nonzero if we can skip code only needed on first scan
12911da177e4SLinus Torvalds  *
12921da177e4SLinus Torvalds  * Description:
1293eb44820cSRob Landley  *   Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command.
1294eb44820cSRob Landley  *   Scan the resulting list of LUNs by calling scsi_probe_and_add_lun.
12951da177e4SLinus Torvalds  *
1296eb44820cSRob Landley  *   If BLINK_REPORTLUN2 is set, scan a target that supports more than 8
1297eb44820cSRob Landley  *   LUNs even if it's older than SCSI-3.
1298eb44820cSRob Landley  *   If BLIST_NOREPORTLUN is set, return 1 always.
1299eb44820cSRob Landley  *   If BLIST_NOLUN is set, return 0 always.
13001da177e4SLinus Torvalds  *
13011da177e4SLinus Torvalds  * Return:
13021da177e4SLinus Torvalds  *     0: scan completed (or no memory, so further scanning is futile)
1303eb44820cSRob Landley  *     1: could not scan with REPORT LUN
13041da177e4SLinus Torvalds  **/
13056f3a2024SJames Bottomley static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
13061da177e4SLinus Torvalds 				int rescan)
13071da177e4SLinus Torvalds {
13081da177e4SLinus Torvalds 	char devname[64];
13091da177e4SLinus Torvalds 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
13101da177e4SLinus Torvalds 	unsigned int length;
13111da177e4SLinus Torvalds 	unsigned int lun;
13121da177e4SLinus Torvalds 	unsigned int num_luns;
13131da177e4SLinus Torvalds 	unsigned int retries;
131439216033SJames Bottomley 	int result;
13151da177e4SLinus Torvalds 	struct scsi_lun *lunp, *lun_data;
13161da177e4SLinus Torvalds 	u8 *data;
13171da177e4SLinus Torvalds 	struct scsi_sense_hdr sshdr;
13186f3a2024SJames Bottomley 	struct scsi_device *sdev;
13196f3a2024SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
13202ef89198SAlan Stern 	int ret = 0;
13211da177e4SLinus Torvalds 
13221da177e4SLinus Torvalds 	/*
13231da177e4SLinus Torvalds 	 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
13241da177e4SLinus Torvalds 	 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
13251da177e4SLinus Torvalds 	 * support more than 8 LUNs.
13261da177e4SLinus Torvalds 	 */
13274d7db04aSJames Bottomley 	if (bflags & BLIST_NOREPORTLUN)
13284d7db04aSJames Bottomley 		return 1;
13294d7db04aSJames Bottomley 	if (starget->scsi_level < SCSI_2 &&
13304d7db04aSJames Bottomley 	    starget->scsi_level != SCSI_UNKNOWN)
13314d7db04aSJames Bottomley 		return 1;
13324d7db04aSJames Bottomley 	if (starget->scsi_level < SCSI_3 &&
13334d7db04aSJames Bottomley 	    (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
13341da177e4SLinus Torvalds 		return 1;
13351da177e4SLinus Torvalds 	if (bflags & BLIST_NOLUN)
13361da177e4SLinus Torvalds 		return 0;
13371da177e4SLinus Torvalds 
13386f3a2024SJames Bottomley 	if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
13396f3a2024SJames Bottomley 		sdev = scsi_alloc_sdev(starget, 0, NULL);
13406f3a2024SJames Bottomley 		if (!sdev)
13416f3a2024SJames Bottomley 			return 0;
134275f8ee8eSAlan Stern 		if (scsi_device_get(sdev)) {
134375f8ee8eSAlan Stern 			__scsi_remove_device(sdev);
13446f3a2024SJames Bottomley 			return 0;
13456f3a2024SJames Bottomley 		}
134675f8ee8eSAlan Stern 	}
13476f3a2024SJames Bottomley 
13481da177e4SLinus Torvalds 	sprintf(devname, "host %d channel %d id %d",
13496f3a2024SJames Bottomley 		shost->host_no, sdev->channel, sdev->id);
13501da177e4SLinus Torvalds 
13511da177e4SLinus Torvalds 	/*
13521da177e4SLinus Torvalds 	 * Allocate enough to hold the header (the same size as one scsi_lun)
13531da177e4SLinus Torvalds 	 * plus the max number of luns we are requesting.
13541da177e4SLinus Torvalds 	 *
13551da177e4SLinus Torvalds 	 * Reallocating and trying again (with the exact amount we need)
13561da177e4SLinus Torvalds 	 * would be nice, but then we need to somehow limit the size
13571da177e4SLinus Torvalds 	 * allocated based on the available memory and the limits of
13581da177e4SLinus Torvalds 	 * kmalloc - we don't want a kmalloc() failure of a huge value to
13591da177e4SLinus Torvalds 	 * prevent us from finding any LUNs on this target.
13601da177e4SLinus Torvalds 	 */
13611da177e4SLinus Torvalds 	length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
13621da177e4SLinus Torvalds 	lun_data = kmalloc(length, GFP_ATOMIC |
13631da177e4SLinus Torvalds 			   (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
13646f3a2024SJames Bottomley 	if (!lun_data) {
1365cadbd4a5SHarvey Harrison 		printk(ALLOC_FAILURE_MSG, __func__);
136639216033SJames Bottomley 		goto out;
13676f3a2024SJames Bottomley 	}
13681da177e4SLinus Torvalds 
13691da177e4SLinus Torvalds 	scsi_cmd[0] = REPORT_LUNS;
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds 	/*
13721da177e4SLinus Torvalds 	 * bytes 1 - 5: reserved, set to zero.
13731da177e4SLinus Torvalds 	 */
13741da177e4SLinus Torvalds 	memset(&scsi_cmd[1], 0, 5);
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	/*
13771da177e4SLinus Torvalds 	 * bytes 6 - 9: length of the command.
13781da177e4SLinus Torvalds 	 */
13791da177e4SLinus Torvalds 	scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
13801da177e4SLinus Torvalds 	scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
13811da177e4SLinus Torvalds 	scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
13821da177e4SLinus Torvalds 	scsi_cmd[9] = (unsigned char) length & 0xff;
13831da177e4SLinus Torvalds 
13841da177e4SLinus Torvalds 	scsi_cmd[10] = 0;	/* reserved */
13851da177e4SLinus Torvalds 	scsi_cmd[11] = 0;	/* control */
13861da177e4SLinus Torvalds 
13871da177e4SLinus Torvalds 	/*
13881da177e4SLinus Torvalds 	 * We can get a UNIT ATTENTION, for example a power on/reset, so
13891da177e4SLinus Torvalds 	 * retry a few times (like sd.c does for TEST UNIT READY).
13901da177e4SLinus Torvalds 	 * Experience shows some combinations of adapter/devices get at
13911da177e4SLinus Torvalds 	 * least two power on/resets.
13921da177e4SLinus Torvalds 	 *
13931da177e4SLinus Torvalds 	 * Illegal requests (for devices that do not support REPORT LUNS)
13941da177e4SLinus Torvalds 	 * should come through as a check condition, and will not generate
13951da177e4SLinus Torvalds 	 * a retry.
13961da177e4SLinus Torvalds 	 */
13971da177e4SLinus Torvalds 	for (retries = 0; retries < 3; retries++) {
13981da177e4SLinus Torvalds 		SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
13991da177e4SLinus Torvalds 				" REPORT LUNS to %s (try %d)\n", devname,
14001da177e4SLinus Torvalds 				retries));
140139216033SJames Bottomley 
140239216033SJames Bottomley 		result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1403ea73a9f2SJames Bottomley 					  lun_data, length, &sshdr,
1404f4f4e47eSFUJITA Tomonori 					  SCSI_TIMEOUT + 4 * HZ, 3, NULL);
140539216033SJames Bottomley 
14061da177e4SLinus Torvalds 		SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
140739216033SJames Bottomley 				" %s (try %d) result 0x%x\n", result
140839216033SJames Bottomley 				?  "failed" : "successful", retries, result));
140939216033SJames Bottomley 		if (result == 0)
14101da177e4SLinus Torvalds 			break;
1411ea73a9f2SJames Bottomley 		else if (scsi_sense_valid(&sshdr)) {
14121da177e4SLinus Torvalds 			if (sshdr.sense_key != UNIT_ATTENTION)
14131da177e4SLinus Torvalds 				break;
14141da177e4SLinus Torvalds 		}
14151da177e4SLinus Torvalds 	}
14161da177e4SLinus Torvalds 
141739216033SJames Bottomley 	if (result) {
14181da177e4SLinus Torvalds 		/*
14191da177e4SLinus Torvalds 		 * The device probably does not support a REPORT LUN command
14201da177e4SLinus Torvalds 		 */
14212ef89198SAlan Stern 		ret = 1;
14222ef89198SAlan Stern 		goto out_err;
14231da177e4SLinus Torvalds 	}
14241da177e4SLinus Torvalds 
14251da177e4SLinus Torvalds 	/*
14261da177e4SLinus Torvalds 	 * Get the length from the first four bytes of lun_data.
14271da177e4SLinus Torvalds 	 */
14281da177e4SLinus Torvalds 	data = (u8 *) lun_data->scsi_lun;
14291da177e4SLinus Torvalds 	length = ((data[0] << 24) | (data[1] << 16) |
14301da177e4SLinus Torvalds 		  (data[2] << 8) | (data[3] << 0));
14311da177e4SLinus Torvalds 
14321da177e4SLinus Torvalds 	num_luns = (length / sizeof(struct scsi_lun));
14331da177e4SLinus Torvalds 	if (num_luns > max_scsi_report_luns) {
14341da177e4SLinus Torvalds 		printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
14351da177e4SLinus Torvalds 		       " of %d luns reported, try increasing"
14361da177e4SLinus Torvalds 		       " max_scsi_report_luns.\n", devname,
14371da177e4SLinus Torvalds 		       max_scsi_report_luns, num_luns);
14381da177e4SLinus Torvalds 		num_luns = max_scsi_report_luns;
14391da177e4SLinus Torvalds 	}
14401da177e4SLinus Torvalds 
14413bf743e7SJeff Garzik 	SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
14423bf743e7SJeff Garzik 		"scsi scan: REPORT LUN scan\n"));
14431da177e4SLinus Torvalds 
14441da177e4SLinus Torvalds 	/*
14451da177e4SLinus Torvalds 	 * Scan the luns in lun_data. The entry at offset 0 is really
14461da177e4SLinus Torvalds 	 * the header, so start at 1 and go up to and including num_luns.
14471da177e4SLinus Torvalds 	 */
14481da177e4SLinus Torvalds 	for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
14491da177e4SLinus Torvalds 		lun = scsilun_to_int(lunp);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds 		/*
14521da177e4SLinus Torvalds 		 * Check if the unused part of lunp is non-zero, and so
14531da177e4SLinus Torvalds 		 * does not fit in lun.
14541da177e4SLinus Torvalds 		 */
14551da177e4SLinus Torvalds 		if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
14561da177e4SLinus Torvalds 			int i;
14571da177e4SLinus Torvalds 
14581da177e4SLinus Torvalds 			/*
14591da177e4SLinus Torvalds 			 * Output an error displaying the LUN in byte order,
14601da177e4SLinus Torvalds 			 * this differs from what linux would print for the
14611da177e4SLinus Torvalds 			 * integer LUN value.
14621da177e4SLinus Torvalds 			 */
14631da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi: %s lun 0x", devname);
14641da177e4SLinus Torvalds 			data = (char *)lunp->scsi_lun;
14651da177e4SLinus Torvalds 			for (i = 0; i < sizeof(struct scsi_lun); i++)
14661da177e4SLinus Torvalds 				printk("%02x", data[i]);
14671da177e4SLinus Torvalds 			printk(" has a LUN larger than currently supported.\n");
14681da177e4SLinus Torvalds 		} else if (lun > sdev->host->max_lun) {
14691da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
14701da177e4SLinus Torvalds 			       " than allowed by the host adapter\n",
14711da177e4SLinus Torvalds 			       devname, lun);
14721da177e4SLinus Torvalds 		} else {
14731da177e4SLinus Torvalds 			int res;
14741da177e4SLinus Torvalds 
14751da177e4SLinus Torvalds 			res = scsi_probe_and_add_lun(starget,
14761da177e4SLinus Torvalds 				lun, NULL, NULL, rescan, NULL);
14771da177e4SLinus Torvalds 			if (res == SCSI_SCAN_NO_RESPONSE) {
14781da177e4SLinus Torvalds 				/*
14791da177e4SLinus Torvalds 				 * Got some results, but now none, abort.
14801da177e4SLinus Torvalds 				 */
14813bf743e7SJeff Garzik 				sdev_printk(KERN_ERR, sdev,
14823bf743e7SJeff Garzik 					"Unexpected response"
14833bf743e7SJeff Garzik 				        " from lun %d while scanning, scan"
14843bf743e7SJeff Garzik 				        " aborted\n", lun);
14851da177e4SLinus Torvalds 				break;
14861da177e4SLinus Torvalds 			}
14871da177e4SLinus Torvalds 		}
14881da177e4SLinus Torvalds 	}
14891da177e4SLinus Torvalds 
14902ef89198SAlan Stern  out_err:
14911da177e4SLinus Torvalds 	kfree(lun_data);
14921da177e4SLinus Torvalds  out:
14936f3a2024SJames Bottomley 	scsi_device_put(sdev);
14940f1d87a2SJames Bottomley 	if (scsi_device_created(sdev))
14951da177e4SLinus Torvalds 		/*
14966f3a2024SJames Bottomley 		 * the sdev we used didn't appear in the report luns scan
14971da177e4SLinus Torvalds 		 */
1498860dc736SJames Bottomley 		__scsi_remove_device(sdev);
14992ef89198SAlan Stern 	return ret;
15001da177e4SLinus Torvalds }
15011da177e4SLinus Torvalds 
15021da177e4SLinus Torvalds struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
15031da177e4SLinus Torvalds 				      uint id, uint lun, void *hostdata)
15041da177e4SLinus Torvalds {
1505a97a83a0SMatthew Wilcox 	struct scsi_device *sdev = ERR_PTR(-ENODEV);
15061da177e4SLinus Torvalds 	struct device *parent = &shost->shost_gendev;
1507e02f3f59SChristoph Hellwig 	struct scsi_target *starget;
15081da177e4SLinus Torvalds 
1509938e2ac0SMatthew Wilcox 	if (strncmp(scsi_scan_type, "none", 4) == 0)
1510938e2ac0SMatthew Wilcox 		return ERR_PTR(-ENODEV);
1511938e2ac0SMatthew Wilcox 
1512e02f3f59SChristoph Hellwig 	starget = scsi_alloc_target(parent, channel, id);
15131da177e4SLinus Torvalds 	if (!starget)
15141da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
15151da177e4SLinus Torvalds 
15160b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
15176b7f123fSMatthew Wilcox 	if (!shost->async_scan)
15186b7f123fSMatthew Wilcox 		scsi_complete_async_scans();
15196b7f123fSMatthew Wilcox 
1520a97a83a0SMatthew Wilcox 	if (scsi_host_scan_allowed(shost))
1521a97a83a0SMatthew Wilcox 		scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
15220b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
15231da177e4SLinus Torvalds 	scsi_target_reap(starget);
15241da177e4SLinus Torvalds 	put_device(&starget->dev);
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 	return sdev;
15271da177e4SLinus Torvalds }
15281da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_add_device);
15291da177e4SLinus Torvalds 
1530146f7262SJames Bottomley int scsi_add_device(struct Scsi_Host *host, uint channel,
1531146f7262SJames Bottomley 		    uint target, uint lun)
1532146f7262SJames Bottomley {
1533146f7262SJames Bottomley 	struct scsi_device *sdev =
1534146f7262SJames Bottomley 		__scsi_add_device(host, channel, target, lun, NULL);
1535146f7262SJames Bottomley 	if (IS_ERR(sdev))
1536146f7262SJames Bottomley 		return PTR_ERR(sdev);
1537146f7262SJames Bottomley 
1538146f7262SJames Bottomley 	scsi_device_put(sdev);
1539146f7262SJames Bottomley 	return 0;
1540146f7262SJames Bottomley }
1541146f7262SJames Bottomley EXPORT_SYMBOL(scsi_add_device);
1542146f7262SJames Bottomley 
15431da177e4SLinus Torvalds void scsi_rescan_device(struct device *dev)
15441da177e4SLinus Torvalds {
15451da177e4SLinus Torvalds 	struct scsi_driver *drv;
15461da177e4SLinus Torvalds 
15471da177e4SLinus Torvalds 	if (!dev->driver)
15481da177e4SLinus Torvalds 		return;
15491da177e4SLinus Torvalds 
15501da177e4SLinus Torvalds 	drv = to_scsi_driver(dev->driver);
15511da177e4SLinus Torvalds 	if (try_module_get(drv->owner)) {
15521da177e4SLinus Torvalds 		if (drv->rescan)
15531da177e4SLinus Torvalds 			drv->rescan(dev);
15541da177e4SLinus Torvalds 		module_put(drv->owner);
15551da177e4SLinus Torvalds 	}
15561da177e4SLinus Torvalds }
15571da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_rescan_device);
15581da177e4SLinus Torvalds 
1559e517d313SAlan Stern static void __scsi_scan_target(struct device *parent, unsigned int channel,
15601da177e4SLinus Torvalds 		unsigned int id, unsigned int lun, int rescan)
15611da177e4SLinus Torvalds {
15621da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(parent);
15631da177e4SLinus Torvalds 	int bflags = 0;
15641da177e4SLinus Torvalds 	int res;
15651da177e4SLinus Torvalds 	struct scsi_target *starget;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	if (shost->this_id == id)
15681da177e4SLinus Torvalds 		/*
15691da177e4SLinus Torvalds 		 * Don't scan the host adapter
15701da177e4SLinus Torvalds 		 */
15711da177e4SLinus Torvalds 		return;
15721da177e4SLinus Torvalds 
15731da177e4SLinus Torvalds 	starget = scsi_alloc_target(parent, channel, id);
15741da177e4SLinus Torvalds 	if (!starget)
15751da177e4SLinus Torvalds 		return;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	if (lun != SCAN_WILD_CARD) {
15781da177e4SLinus Torvalds 		/*
15791da177e4SLinus Torvalds 		 * Scan for a specific host/chan/id/lun.
15801da177e4SLinus Torvalds 		 */
15811da177e4SLinus Torvalds 		scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
15821da177e4SLinus Torvalds 		goto out_reap;
15831da177e4SLinus Torvalds 	}
15841da177e4SLinus Torvalds 
15851da177e4SLinus Torvalds 	/*
15861da177e4SLinus Torvalds 	 * Scan LUN 0, if there is some response, scan further. Ideally, we
15871da177e4SLinus Torvalds 	 * would not configure LUN 0 until all LUNs are scanned.
15881da177e4SLinus Torvalds 	 */
15896f3a2024SJames Bottomley 	res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
15906f3a2024SJames Bottomley 	if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
15916f3a2024SJames Bottomley 		if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
15921da177e4SLinus Torvalds 			/*
15931da177e4SLinus Torvalds 			 * The REPORT LUN did not scan the target,
15941da177e4SLinus Torvalds 			 * do a sequential scan.
15951da177e4SLinus Torvalds 			 */
15961da177e4SLinus Torvalds 			scsi_sequential_lun_scan(starget, bflags,
15974186ab19SKurt Garloff 						 starget->scsi_level, rescan);
15981da177e4SLinus Torvalds 	}
15991da177e4SLinus Torvalds 
16001da177e4SLinus Torvalds  out_reap:
16011da177e4SLinus Torvalds 	/* now determine if the target has any children at all
16021da177e4SLinus Torvalds 	 * and if not, nuke it */
16031da177e4SLinus Torvalds 	scsi_target_reap(starget);
16041da177e4SLinus Torvalds 
16051da177e4SLinus Torvalds 	put_device(&starget->dev);
16061da177e4SLinus Torvalds }
1607e517d313SAlan Stern 
1608e517d313SAlan Stern /**
1609e59e4a09SRandy Dunlap  * scsi_scan_target - scan a target id, possibly including all LUNs on the target.
1610e517d313SAlan Stern  * @parent:	host to scan
1611e517d313SAlan Stern  * @channel:	channel to scan
1612e517d313SAlan Stern  * @id:		target id to scan
1613e517d313SAlan Stern  * @lun:	Specific LUN to scan or SCAN_WILD_CARD
1614e517d313SAlan Stern  * @rescan:	passed to LUN scanning routines
1615e517d313SAlan Stern  *
1616e517d313SAlan Stern  * Description:
1617e517d313SAlan Stern  *     Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1618e517d313SAlan Stern  *     and possibly all LUNs on the target id.
1619e517d313SAlan Stern  *
1620e517d313SAlan Stern  *     First try a REPORT LUN scan, if that does not scan the target, do a
1621e517d313SAlan Stern  *     sequential scan of LUNs on the target id.
1622e517d313SAlan Stern  **/
1623e517d313SAlan Stern void scsi_scan_target(struct device *parent, unsigned int channel,
1624e517d313SAlan Stern 		      unsigned int id, unsigned int lun, int rescan)
1625e517d313SAlan Stern {
1626e517d313SAlan Stern 	struct Scsi_Host *shost = dev_to_shost(parent);
1627e517d313SAlan Stern 
162893b45af5SMatthew Wilcox 	if (strncmp(scsi_scan_type, "none", 4) == 0)
162993b45af5SMatthew Wilcox 		return;
163093b45af5SMatthew Wilcox 
16316b7f123fSMatthew Wilcox 	mutex_lock(&shost->scan_mutex);
16323e082a91SMatthew Wilcox 	if (!shost->async_scan)
16333e082a91SMatthew Wilcox 		scsi_complete_async_scans();
16343e082a91SMatthew Wilcox 
1635e517d313SAlan Stern 	if (scsi_host_scan_allowed(shost))
1636e517d313SAlan Stern 		__scsi_scan_target(parent, channel, id, lun, rescan);
16370b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
1638e517d313SAlan Stern }
16391da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_target);
16401da177e4SLinus Torvalds 
16411da177e4SLinus Torvalds static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
16421da177e4SLinus Torvalds 			      unsigned int id, unsigned int lun, int rescan)
16431da177e4SLinus Torvalds {
16441da177e4SLinus Torvalds 	uint order_id;
16451da177e4SLinus Torvalds 
16461da177e4SLinus Torvalds 	if (id == SCAN_WILD_CARD)
16471da177e4SLinus Torvalds 		for (id = 0; id < shost->max_id; ++id) {
16481da177e4SLinus Torvalds 			/*
16491da177e4SLinus Torvalds 			 * XXX adapter drivers when possible (FCP, iSCSI)
16501da177e4SLinus Torvalds 			 * could modify max_id to match the current max,
16511da177e4SLinus Torvalds 			 * not the absolute max.
16521da177e4SLinus Torvalds 			 *
16531da177e4SLinus Torvalds 			 * XXX add a shost id iterator, so for example,
16541da177e4SLinus Torvalds 			 * the FC ID can be the same as a target id
16551da177e4SLinus Torvalds 			 * without a huge overhead of sparse id's.
16561da177e4SLinus Torvalds 			 */
16571da177e4SLinus Torvalds 			if (shost->reverse_ordering)
16581da177e4SLinus Torvalds 				/*
16591da177e4SLinus Torvalds 				 * Scan from high to low id.
16601da177e4SLinus Torvalds 				 */
16611da177e4SLinus Torvalds 				order_id = shost->max_id - id - 1;
16621da177e4SLinus Torvalds 			else
16631da177e4SLinus Torvalds 				order_id = id;
1664e517d313SAlan Stern 			__scsi_scan_target(&shost->shost_gendev, channel,
1665e517d313SAlan Stern 					order_id, lun, rescan);
16661da177e4SLinus Torvalds 		}
16671da177e4SLinus Torvalds 	else
1668e517d313SAlan Stern 		__scsi_scan_target(&shost->shost_gendev, channel,
1669e517d313SAlan Stern 				id, lun, rescan);
16701da177e4SLinus Torvalds }
16711da177e4SLinus Torvalds 
16721da177e4SLinus Torvalds int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
16731da177e4SLinus Torvalds 			    unsigned int id, unsigned int lun, int rescan)
16741da177e4SLinus Torvalds {
16753bf743e7SJeff Garzik 	SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
16763bf743e7SJeff Garzik 		"%s: <%u:%u:%u>\n",
1677cadbd4a5SHarvey Harrison 		__func__, channel, id, lun));
16781da177e4SLinus Torvalds 
16791da177e4SLinus Torvalds 	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1680091686d3SAmit Arora 	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
16811da177e4SLinus Torvalds 	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
16821da177e4SLinus Torvalds 		return -EINVAL;
16831da177e4SLinus Torvalds 
16840b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
16856b7f123fSMatthew Wilcox 	if (!shost->async_scan)
16866b7f123fSMatthew Wilcox 		scsi_complete_async_scans();
16876b7f123fSMatthew Wilcox 
168882f29467SMike Anderson 	if (scsi_host_scan_allowed(shost)) {
16891da177e4SLinus Torvalds 		if (channel == SCAN_WILD_CARD)
169082f29467SMike Anderson 			for (channel = 0; channel <= shost->max_channel;
169182f29467SMike Anderson 			     channel++)
169282f29467SMike Anderson 				scsi_scan_channel(shost, channel, id, lun,
169382f29467SMike Anderson 						  rescan);
16941da177e4SLinus Torvalds 		else
16951da177e4SLinus Torvalds 			scsi_scan_channel(shost, channel, id, lun, rescan);
169682f29467SMike Anderson 	}
16970b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
16981da177e4SLinus Torvalds 
16991da177e4SLinus Torvalds 	return 0;
17001da177e4SLinus Torvalds }
17011da177e4SLinus Torvalds 
17023e082a91SMatthew Wilcox static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
17033e082a91SMatthew Wilcox {
17043e082a91SMatthew Wilcox 	struct scsi_device *sdev;
17053e082a91SMatthew Wilcox 	shost_for_each_device(sdev, shost) {
17066b7f123fSMatthew Wilcox 		if (!scsi_host_scan_allowed(shost) ||
17076b7f123fSMatthew Wilcox 		    scsi_sysfs_add_sdev(sdev) != 0)
1708860dc736SJames Bottomley 			__scsi_remove_device(sdev);
17093e082a91SMatthew Wilcox 	}
17103e082a91SMatthew Wilcox }
17113e082a91SMatthew Wilcox 
17123e082a91SMatthew Wilcox /**
17133e082a91SMatthew Wilcox  * scsi_prep_async_scan - prepare for an async scan
17143e082a91SMatthew Wilcox  * @shost: the host which will be scanned
17153e082a91SMatthew Wilcox  * Returns: a cookie to be passed to scsi_finish_async_scan()
17163e082a91SMatthew Wilcox  *
17173e082a91SMatthew Wilcox  * Tells the midlayer this host is going to do an asynchronous scan.
17183e082a91SMatthew Wilcox  * It reserves the host's position in the scanning list and ensures
17193e082a91SMatthew Wilcox  * that other asynchronous scans started after this one won't affect the
17203e082a91SMatthew Wilcox  * ordering of the discovered devices.
17213e082a91SMatthew Wilcox  */
17221aa8fab2SMatthew Wilcox static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
17233e082a91SMatthew Wilcox {
17243e082a91SMatthew Wilcox 	struct async_scan_data *data;
17256b7f123fSMatthew Wilcox 	unsigned long flags;
17263e082a91SMatthew Wilcox 
17273e082a91SMatthew Wilcox 	if (strncmp(scsi_scan_type, "sync", 4) == 0)
17283e082a91SMatthew Wilcox 		return NULL;
17293e082a91SMatthew Wilcox 
17303e082a91SMatthew Wilcox 	if (shost->async_scan) {
1731cadbd4a5SHarvey Harrison 		printk("%s called twice for host %d", __func__,
17323e082a91SMatthew Wilcox 				shost->host_no);
17333e082a91SMatthew Wilcox 		dump_stack();
17343e082a91SMatthew Wilcox 		return NULL;
17353e082a91SMatthew Wilcox 	}
17363e082a91SMatthew Wilcox 
17373e082a91SMatthew Wilcox 	data = kmalloc(sizeof(*data), GFP_KERNEL);
17383e082a91SMatthew Wilcox 	if (!data)
17393e082a91SMatthew Wilcox 		goto err;
17403e082a91SMatthew Wilcox 	data->shost = scsi_host_get(shost);
17413e082a91SMatthew Wilcox 	if (!data->shost)
17423e082a91SMatthew Wilcox 		goto err;
17433e082a91SMatthew Wilcox 	init_completion(&data->prev_finished);
17443e082a91SMatthew Wilcox 
17456b7f123fSMatthew Wilcox 	mutex_lock(&shost->scan_mutex);
17466b7f123fSMatthew Wilcox 	spin_lock_irqsave(shost->host_lock, flags);
17473e082a91SMatthew Wilcox 	shost->async_scan = 1;
17486b7f123fSMatthew Wilcox 	spin_unlock_irqrestore(shost->host_lock, flags);
17496b7f123fSMatthew Wilcox 	mutex_unlock(&shost->scan_mutex);
17506b7f123fSMatthew Wilcox 
17516b7f123fSMatthew Wilcox 	spin_lock(&async_scan_lock);
17523e082a91SMatthew Wilcox 	if (list_empty(&scanning_hosts))
17533e082a91SMatthew Wilcox 		complete(&data->prev_finished);
17543e082a91SMatthew Wilcox 	list_add_tail(&data->list, &scanning_hosts);
17553e082a91SMatthew Wilcox 	spin_unlock(&async_scan_lock);
17563e082a91SMatthew Wilcox 
17573e082a91SMatthew Wilcox 	return data;
17583e082a91SMatthew Wilcox 
17593e082a91SMatthew Wilcox  err:
17603e082a91SMatthew Wilcox 	kfree(data);
17613e082a91SMatthew Wilcox 	return NULL;
17623e082a91SMatthew Wilcox }
17633e082a91SMatthew Wilcox 
17643e082a91SMatthew Wilcox /**
17653e082a91SMatthew Wilcox  * scsi_finish_async_scan - asynchronous scan has finished
17663e082a91SMatthew Wilcox  * @data: cookie returned from earlier call to scsi_prep_async_scan()
17673e082a91SMatthew Wilcox  *
17683e082a91SMatthew Wilcox  * All the devices currently attached to this host have been found.
17693e082a91SMatthew Wilcox  * This function announces all the devices it has found to the rest
17703e082a91SMatthew Wilcox  * of the system.
17713e082a91SMatthew Wilcox  */
17721aa8fab2SMatthew Wilcox static void scsi_finish_async_scan(struct async_scan_data *data)
17733e082a91SMatthew Wilcox {
17743e082a91SMatthew Wilcox 	struct Scsi_Host *shost;
17756b7f123fSMatthew Wilcox 	unsigned long flags;
17763e082a91SMatthew Wilcox 
17773e082a91SMatthew Wilcox 	if (!data)
17783e082a91SMatthew Wilcox 		return;
17793e082a91SMatthew Wilcox 
17803e082a91SMatthew Wilcox 	shost = data->shost;
17816b7f123fSMatthew Wilcox 
17826b7f123fSMatthew Wilcox 	mutex_lock(&shost->scan_mutex);
17836b7f123fSMatthew Wilcox 
17843e082a91SMatthew Wilcox 	if (!shost->async_scan) {
1785cadbd4a5SHarvey Harrison 		printk("%s called twice for host %d", __func__,
17863e082a91SMatthew Wilcox 				shost->host_no);
17873e082a91SMatthew Wilcox 		dump_stack();
1788773e82f6SJulia Lawall 		mutex_unlock(&shost->scan_mutex);
17893e082a91SMatthew Wilcox 		return;
17903e082a91SMatthew Wilcox 	}
17913e082a91SMatthew Wilcox 
17923e082a91SMatthew Wilcox 	wait_for_completion(&data->prev_finished);
17933e082a91SMatthew Wilcox 
17943e082a91SMatthew Wilcox 	scsi_sysfs_add_devices(shost);
17953e082a91SMatthew Wilcox 
17966b7f123fSMatthew Wilcox 	spin_lock_irqsave(shost->host_lock, flags);
17973e082a91SMatthew Wilcox 	shost->async_scan = 0;
17986b7f123fSMatthew Wilcox 	spin_unlock_irqrestore(shost->host_lock, flags);
17996b7f123fSMatthew Wilcox 
18006b7f123fSMatthew Wilcox 	mutex_unlock(&shost->scan_mutex);
18016b7f123fSMatthew Wilcox 
18026b7f123fSMatthew Wilcox 	spin_lock(&async_scan_lock);
18033e082a91SMatthew Wilcox 	list_del(&data->list);
18043e082a91SMatthew Wilcox 	if (!list_empty(&scanning_hosts)) {
18053e082a91SMatthew Wilcox 		struct async_scan_data *next = list_entry(scanning_hosts.next,
18063e082a91SMatthew Wilcox 				struct async_scan_data, list);
18073e082a91SMatthew Wilcox 		complete(&next->prev_finished);
18083e082a91SMatthew Wilcox 	}
18093e082a91SMatthew Wilcox 	spin_unlock(&async_scan_lock);
18103e082a91SMatthew Wilcox 
18113e082a91SMatthew Wilcox 	scsi_host_put(shost);
18123e082a91SMatthew Wilcox 	kfree(data);
18133e082a91SMatthew Wilcox }
18143e082a91SMatthew Wilcox 
18151aa8fab2SMatthew Wilcox static void do_scsi_scan_host(struct Scsi_Host *shost)
18161aa8fab2SMatthew Wilcox {
18171aa8fab2SMatthew Wilcox 	if (shost->hostt->scan_finished) {
18181aa8fab2SMatthew Wilcox 		unsigned long start = jiffies;
18191aa8fab2SMatthew Wilcox 		if (shost->hostt->scan_start)
18201aa8fab2SMatthew Wilcox 			shost->hostt->scan_start(shost);
18211aa8fab2SMatthew Wilcox 
18221aa8fab2SMatthew Wilcox 		while (!shost->hostt->scan_finished(shost, jiffies - start))
18231aa8fab2SMatthew Wilcox 			msleep(10);
18241aa8fab2SMatthew Wilcox 	} else {
18251aa8fab2SMatthew Wilcox 		scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
18261aa8fab2SMatthew Wilcox 				SCAN_WILD_CARD, 0);
18271aa8fab2SMatthew Wilcox 	}
18281aa8fab2SMatthew Wilcox }
18291aa8fab2SMatthew Wilcox 
18303e082a91SMatthew Wilcox static int do_scan_async(void *_data)
18313e082a91SMatthew Wilcox {
18323e082a91SMatthew Wilcox 	struct async_scan_data *data = _data;
18331aa8fab2SMatthew Wilcox 	do_scsi_scan_host(data->shost);
18343e082a91SMatthew Wilcox 	scsi_finish_async_scan(data);
18353e082a91SMatthew Wilcox 	return 0;
18363e082a91SMatthew Wilcox }
18373e082a91SMatthew Wilcox 
18381da177e4SLinus Torvalds /**
18391da177e4SLinus Torvalds  * scsi_scan_host - scan the given adapter
18401da177e4SLinus Torvalds  * @shost:	adapter to scan
18411da177e4SLinus Torvalds  **/
18421da177e4SLinus Torvalds void scsi_scan_host(struct Scsi_Host *shost)
18431da177e4SLinus Torvalds {
1844a57b1fccSMatthew Wilcox 	struct task_struct *p;
18453e082a91SMatthew Wilcox 	struct async_scan_data *data;
18463e082a91SMatthew Wilcox 
18473e082a91SMatthew Wilcox 	if (strncmp(scsi_scan_type, "none", 4) == 0)
18483e082a91SMatthew Wilcox 		return;
18493e082a91SMatthew Wilcox 
18503e082a91SMatthew Wilcox 	data = scsi_prep_async_scan(shost);
18513e082a91SMatthew Wilcox 	if (!data) {
18521aa8fab2SMatthew Wilcox 		do_scsi_scan_host(shost);
18533e082a91SMatthew Wilcox 		return;
18543e082a91SMatthew Wilcox 	}
18551aa8fab2SMatthew Wilcox 
1856a57b1fccSMatthew Wilcox 	p = kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1857801678c5SHirofumi Nakagawa 	if (IS_ERR(p))
1858a57b1fccSMatthew Wilcox 		do_scan_async(data);
18591da177e4SLinus Torvalds }
18601da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_host);
18611da177e4SLinus Torvalds 
18621da177e4SLinus Torvalds void scsi_forget_host(struct Scsi_Host *shost)
18631da177e4SLinus Torvalds {
1864a64358dbSAlan Stern 	struct scsi_device *sdev;
18651da177e4SLinus Torvalds 	unsigned long flags;
18661da177e4SLinus Torvalds 
1867a64358dbSAlan Stern  restart:
18681da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
1869a64358dbSAlan Stern 	list_for_each_entry(sdev, &shost->__devices, siblings) {
1870a64358dbSAlan Stern 		if (sdev->sdev_state == SDEV_DEL)
1871a64358dbSAlan Stern 			continue;
18721da177e4SLinus Torvalds 		spin_unlock_irqrestore(shost->host_lock, flags);
1873a64358dbSAlan Stern 		__scsi_remove_device(sdev);
1874a64358dbSAlan Stern 		goto restart;
18751da177e4SLinus Torvalds 	}
18761da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
18771da177e4SLinus Torvalds }
18781da177e4SLinus Torvalds 
18791da177e4SLinus Torvalds /*
18801da177e4SLinus Torvalds  * Function:    scsi_get_host_dev()
18811da177e4SLinus Torvalds  *
1882f64a181dSChristoph Hellwig  * Purpose:     Create a scsi_device that points to the host adapter itself.
18831da177e4SLinus Torvalds  *
1884f64a181dSChristoph Hellwig  * Arguments:   SHpnt   - Host that needs a scsi_device
18851da177e4SLinus Torvalds  *
18861da177e4SLinus Torvalds  * Lock status: None assumed.
18871da177e4SLinus Torvalds  *
1888f64a181dSChristoph Hellwig  * Returns:     The scsi_device or NULL
18891da177e4SLinus Torvalds  *
18901da177e4SLinus Torvalds  * Notes:
1891f64a181dSChristoph Hellwig  *	Attach a single scsi_device to the Scsi_Host - this should
18921da177e4SLinus Torvalds  *	be made to look like a "pseudo-device" that points to the
18931da177e4SLinus Torvalds  *	HA itself.
18941da177e4SLinus Torvalds  *
18951da177e4SLinus Torvalds  *	Note - this device is not accessible from any high-level
18961da177e4SLinus Torvalds  *	drivers (including generics), which is probably not
18971da177e4SLinus Torvalds  *	optimal.  We can add hooks later to attach
18981da177e4SLinus Torvalds  */
18991da177e4SLinus Torvalds struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
19001da177e4SLinus Torvalds {
1901e517d313SAlan Stern 	struct scsi_device *sdev = NULL;
19021da177e4SLinus Torvalds 	struct scsi_target *starget;
19031da177e4SLinus Torvalds 
19040b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
1905e517d313SAlan Stern 	if (!scsi_host_scan_allowed(shost))
1906e517d313SAlan Stern 		goto out;
19071da177e4SLinus Torvalds 	starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
19081da177e4SLinus Torvalds 	if (!starget)
1909e517d313SAlan Stern 		goto out;
19101da177e4SLinus Torvalds 
19111da177e4SLinus Torvalds 	sdev = scsi_alloc_sdev(starget, 0, NULL);
1912d5469119SAlan Stern 	if (sdev)
19131da177e4SLinus Torvalds 		sdev->borken = 0;
1914d5469119SAlan Stern 	else
1915884d25ccSJames Bottomley 		scsi_target_reap(starget);
19161da177e4SLinus Torvalds 	put_device(&starget->dev);
1917e517d313SAlan Stern  out:
19180b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
19191da177e4SLinus Torvalds 	return sdev;
19201da177e4SLinus Torvalds }
19211da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_host_dev);
19221da177e4SLinus Torvalds 
19231da177e4SLinus Torvalds /*
19241da177e4SLinus Torvalds  * Function:    scsi_free_host_dev()
19251da177e4SLinus Torvalds  *
19261da177e4SLinus Torvalds  * Purpose:     Free a scsi_device that points to the host adapter itself.
19271da177e4SLinus Torvalds  *
1928f64a181dSChristoph Hellwig  * Arguments:   SHpnt   - Host that needs a scsi_device
19291da177e4SLinus Torvalds  *
19301da177e4SLinus Torvalds  * Lock status: None assumed.
19311da177e4SLinus Torvalds  *
19321da177e4SLinus Torvalds  * Returns:     Nothing
19331da177e4SLinus Torvalds  *
19341da177e4SLinus Torvalds  * Notes:
19351da177e4SLinus Torvalds  */
19361da177e4SLinus Torvalds void scsi_free_host_dev(struct scsi_device *sdev)
19371da177e4SLinus Torvalds {
19381da177e4SLinus Torvalds 	BUG_ON(sdev->id != sdev->host->this_id);
19391da177e4SLinus Torvalds 
1940860dc736SJames Bottomley 	__scsi_remove_device(sdev);
19411da177e4SLinus Torvalds }
19421da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_free_host_dev);
19431da177e4SLinus Torvalds 
1944