xref: /linux/drivers/scsi/scsi_scan.c (revision e96eb23d82b4246cce4eeb14a7eedbbdcf37b3d4)
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))
150*e96eb23dSDan Williams 			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);
182*e96eb23dSDan Williams 
1833e082a91SMatthew Wilcox 	kfree(data);
1843e082a91SMatthew Wilcox 	return 0;
1853e082a91SMatthew Wilcox }
1863e082a91SMatthew Wilcox 
1871da177e4SLinus Torvalds /**
1881da177e4SLinus Torvalds  * scsi_unlock_floptical - unlock device via a special MODE SENSE command
18939216033SJames Bottomley  * @sdev:	scsi device to send command to
1901da177e4SLinus Torvalds  * @result:	area to store the result of the MODE SENSE
1911da177e4SLinus Torvalds  *
1921da177e4SLinus Torvalds  * Description:
19339216033SJames Bottomley  *     Send a vendor specific MODE SENSE (not a MODE SELECT) command.
1941da177e4SLinus Torvalds  *     Called for BLIST_KEY devices.
1951da177e4SLinus Torvalds  **/
19639216033SJames Bottomley static void scsi_unlock_floptical(struct scsi_device *sdev,
1971da177e4SLinus Torvalds 				  unsigned char *result)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
2021da177e4SLinus Torvalds 	scsi_cmd[0] = MODE_SENSE;
2031da177e4SLinus Torvalds 	scsi_cmd[1] = 0;
2041da177e4SLinus Torvalds 	scsi_cmd[2] = 0x2e;
2051da177e4SLinus Torvalds 	scsi_cmd[3] = 0;
2061da177e4SLinus Torvalds 	scsi_cmd[4] = 0x2a;     /* size */
2071da177e4SLinus Torvalds 	scsi_cmd[5] = 0;
20839216033SJames Bottomley 	scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
209f4f4e47eSFUJITA Tomonori 			 SCSI_TIMEOUT, 3, NULL);
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds /**
2131da177e4SLinus Torvalds  * scsi_alloc_sdev - allocate and setup a scsi_Device
214eb44820cSRob Landley  * @starget: which target to allocate a &scsi_device for
215eb44820cSRob Landley  * @lun: which lun
216eb44820cSRob Landley  * @hostdata: usually NULL and set by ->slave_alloc instead
2171da177e4SLinus Torvalds  *
2181da177e4SLinus Torvalds  * Description:
2191da177e4SLinus Torvalds  *     Allocate, initialize for io, and return a pointer to a scsi_Device.
2201da177e4SLinus Torvalds  *     Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
2211da177e4SLinus Torvalds  *     adds scsi_Device to the appropriate list.
2221da177e4SLinus Torvalds  *
2231da177e4SLinus Torvalds  * Return value:
2241da177e4SLinus Torvalds  *     scsi_Device pointer, or NULL on failure.
2251da177e4SLinus Torvalds  **/
2261da177e4SLinus Torvalds static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
2271da177e4SLinus Torvalds 					   unsigned int lun, void *hostdata)
2281da177e4SLinus Torvalds {
2291da177e4SLinus Torvalds 	struct scsi_device *sdev;
2301da177e4SLinus Torvalds 	int display_failure_msg = 1, ret;
2311da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
232a341cd0fSJeff Garzik 	extern void scsi_evt_thread(struct work_struct *work);
2339937a5e2SJens Axboe 	extern void scsi_requeue_run_queue(struct work_struct *work);
2341da177e4SLinus Torvalds 
23524669f75SJes Sorensen 	sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
2361da177e4SLinus Torvalds 		       GFP_ATOMIC);
2371da177e4SLinus Torvalds 	if (!sdev)
2381da177e4SLinus Torvalds 		goto out;
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	sdev->vendor = scsi_null_device_strs;
2411da177e4SLinus Torvalds 	sdev->model = scsi_null_device_strs;
2421da177e4SLinus Torvalds 	sdev->rev = scsi_null_device_strs;
2431da177e4SLinus Torvalds 	sdev->host = shost;
2444a84067dSVasu Dev 	sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD;
2451da177e4SLinus Torvalds 	sdev->id = starget->id;
2461da177e4SLinus Torvalds 	sdev->lun = lun;
2471da177e4SLinus Torvalds 	sdev->channel = starget->channel;
2481da177e4SLinus Torvalds 	sdev->sdev_state = SDEV_CREATED;
2491da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->siblings);
2501da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->same_target_siblings);
2511da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->cmd_list);
2521da177e4SLinus Torvalds 	INIT_LIST_HEAD(&sdev->starved_entry);
253a341cd0fSJeff Garzik 	INIT_LIST_HEAD(&sdev->event_list);
2541da177e4SLinus Torvalds 	spin_lock_init(&sdev->list_lock);
255a341cd0fSJeff Garzik 	INIT_WORK(&sdev->event_work, scsi_evt_thread);
2569937a5e2SJens Axboe 	INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue);
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	sdev->sdev_gendev.parent = get_device(&starget->dev);
2591da177e4SLinus Torvalds 	sdev->sdev_target = starget;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds 	/* usually NULL and set by ->slave_alloc instead */
2621da177e4SLinus Torvalds 	sdev->hostdata = hostdata;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	/* if the device needs this changing, it may do so in the
2651da177e4SLinus Torvalds 	 * slave_configure function */
2661da177e4SLinus Torvalds 	sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	/*
2691da177e4SLinus Torvalds 	 * Some low level driver could use device->type
2701da177e4SLinus Torvalds 	 */
2711da177e4SLinus Torvalds 	sdev->type = -1;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	/*
2741da177e4SLinus Torvalds 	 * Assume that the device will have handshaking problems,
2751da177e4SLinus Torvalds 	 * and then fix this field later if it turns out it
2761da177e4SLinus Torvalds 	 * doesn't
2771da177e4SLinus Torvalds 	 */
2781da177e4SLinus Torvalds 	sdev->borken = 1;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	sdev->request_queue = scsi_alloc_queue(sdev);
2811da177e4SLinus Torvalds 	if (!sdev->request_queue) {
2821da177e4SLinus Torvalds 		/* release fn is set up in scsi_sysfs_device_initialise, so
2831da177e4SLinus Torvalds 		 * have to free and put manually here */
2841da177e4SLinus Torvalds 		put_device(&starget->dev);
28593f56089SDave Jones 		kfree(sdev);
2861da177e4SLinus Torvalds 		goto out;
2871da177e4SLinus Torvalds 	}
28809ac46c4STejun Heo 	WARN_ON_ONCE(!blk_get_queue(sdev->request_queue));
2891da177e4SLinus Torvalds 	sdev->request_queue->queuedata = sdev;
2901da177e4SLinus Torvalds 	scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
2911da177e4SLinus Torvalds 
2921da177e4SLinus Torvalds 	scsi_sysfs_device_initialize(sdev);
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds 	if (shost->hostt->slave_alloc) {
2951da177e4SLinus Torvalds 		ret = shost->hostt->slave_alloc(sdev);
2961da177e4SLinus Torvalds 		if (ret) {
2971da177e4SLinus Torvalds 			/*
2981da177e4SLinus Torvalds 			 * if LLDD reports slave not present, don't clutter
2991da177e4SLinus Torvalds 			 * console with alloc failure messages
3001da177e4SLinus Torvalds 			 */
3011da177e4SLinus Torvalds 			if (ret == -ENXIO)
3021da177e4SLinus Torvalds 				display_failure_msg = 0;
3031da177e4SLinus Torvalds 			goto out_device_destroy;
3041da177e4SLinus Torvalds 		}
3051da177e4SLinus Torvalds 	}
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	return sdev;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds out_device_destroy:
3104e6c82b3SJames Bottomley 	__scsi_remove_device(sdev);
3111da177e4SLinus Torvalds out:
3121da177e4SLinus Torvalds 	if (display_failure_msg)
313cadbd4a5SHarvey Harrison 		printk(ALLOC_FAILURE_MSG, __func__);
3141da177e4SLinus Torvalds 	return NULL;
3151da177e4SLinus Torvalds }
3161da177e4SLinus Torvalds 
317643eb2d9SJames Bottomley static void scsi_target_destroy(struct scsi_target *starget)
318643eb2d9SJames Bottomley {
319643eb2d9SJames Bottomley 	struct device *dev = &starget->dev;
320643eb2d9SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(dev->parent);
321643eb2d9SJames Bottomley 	unsigned long flags;
322643eb2d9SJames Bottomley 
323643eb2d9SJames Bottomley 	transport_destroy_device(dev);
324643eb2d9SJames Bottomley 	spin_lock_irqsave(shost->host_lock, flags);
325643eb2d9SJames Bottomley 	if (shost->hostt->target_destroy)
326643eb2d9SJames Bottomley 		shost->hostt->target_destroy(starget);
327643eb2d9SJames Bottomley 	list_del_init(&starget->siblings);
328643eb2d9SJames Bottomley 	spin_unlock_irqrestore(shost->host_lock, flags);
329643eb2d9SJames Bottomley 	put_device(dev);
330643eb2d9SJames Bottomley }
331643eb2d9SJames Bottomley 
3321da177e4SLinus Torvalds static void scsi_target_dev_release(struct device *dev)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct device *parent = dev->parent;
3351da177e4SLinus Torvalds 	struct scsi_target *starget = to_scsi_target(dev);
336a283bd37SJames Bottomley 
3371da177e4SLinus Torvalds 	kfree(starget);
3381da177e4SLinus Torvalds 	put_device(parent);
3391da177e4SLinus Torvalds }
3401da177e4SLinus Torvalds 
341453cd0f3SAdrian Bunk static struct device_type scsi_target_type = {
342b0ed4336SHannes Reinecke 	.name =		"scsi_target",
343b0ed4336SHannes Reinecke 	.release =	scsi_target_dev_release,
344b0ed4336SHannes Reinecke };
345b0ed4336SHannes Reinecke 
3461da177e4SLinus Torvalds int scsi_is_target_device(const struct device *dev)
3471da177e4SLinus Torvalds {
348b0ed4336SHannes Reinecke 	return dev->type == &scsi_target_type;
3491da177e4SLinus Torvalds }
3501da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_target_device);
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds static struct scsi_target *__scsi_find_target(struct device *parent,
3531da177e4SLinus Torvalds 					      int channel, uint id)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct scsi_target *starget, *found_starget = NULL;
3561da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(parent);
3571da177e4SLinus Torvalds 	/*
3581da177e4SLinus Torvalds 	 * Search for an existing target for this sdev.
3591da177e4SLinus Torvalds 	 */
3601da177e4SLinus Torvalds 	list_for_each_entry(starget, &shost->__targets, siblings) {
3611da177e4SLinus Torvalds 		if (starget->id == id &&
3621da177e4SLinus Torvalds 		    starget->channel == channel) {
3631da177e4SLinus Torvalds 			found_starget = starget;
3641da177e4SLinus Torvalds 			break;
3651da177e4SLinus Torvalds 		}
3661da177e4SLinus Torvalds 	}
3671da177e4SLinus Torvalds 	if (found_starget)
3681da177e4SLinus Torvalds 		get_device(&found_starget->dev);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	return found_starget;
3711da177e4SLinus Torvalds }
3721da177e4SLinus Torvalds 
373884d25ccSJames Bottomley /**
374884d25ccSJames Bottomley  * scsi_alloc_target - allocate a new or find an existing target
375884d25ccSJames Bottomley  * @parent:	parent of the target (need not be a scsi host)
376884d25ccSJames Bottomley  * @channel:	target channel number (zero if no channels)
377884d25ccSJames Bottomley  * @id:		target id number
378884d25ccSJames Bottomley  *
379884d25ccSJames Bottomley  * Return an existing target if one exists, provided it hasn't already
380884d25ccSJames Bottomley  * gone into STARGET_DEL state, otherwise allocate a new target.
381884d25ccSJames Bottomley  *
382884d25ccSJames Bottomley  * The target is returned with an incremented reference, so the caller
383884d25ccSJames Bottomley  * is responsible for both reaping and doing a last put
384884d25ccSJames Bottomley  */
3851da177e4SLinus Torvalds static struct scsi_target *scsi_alloc_target(struct device *parent,
3861da177e4SLinus Torvalds 					     int channel, uint id)
3871da177e4SLinus Torvalds {
3881da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(parent);
3891da177e4SLinus Torvalds 	struct device *dev = NULL;
3901da177e4SLinus Torvalds 	unsigned long flags;
3911da177e4SLinus Torvalds 	const int size = sizeof(struct scsi_target)
3921da177e4SLinus Torvalds 		+ shost->transportt->target_size;
3935c44cd2aSJames.Smart@Emulex.Com 	struct scsi_target *starget;
3941da177e4SLinus Torvalds 	struct scsi_target *found_target;
39532f95792SBrian King 	int error;
3961da177e4SLinus Torvalds 
39724669f75SJes Sorensen 	starget = kzalloc(size, GFP_KERNEL);
3981da177e4SLinus Torvalds 	if (!starget) {
399cadbd4a5SHarvey Harrison 		printk(KERN_ERR "%s: allocation failure\n", __func__);
4001da177e4SLinus Torvalds 		return NULL;
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 	dev = &starget->dev;
4031da177e4SLinus Torvalds 	device_initialize(dev);
4041da177e4SLinus Torvalds 	starget->reap_ref = 1;
4051da177e4SLinus Torvalds 	dev->parent = get_device(parent);
40671610f55SKay Sievers 	dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
407b0ed4336SHannes Reinecke 	dev->bus = &scsi_bus_type;
408b0ed4336SHannes Reinecke 	dev->type = &scsi_target_type;
4091da177e4SLinus Torvalds 	starget->id = id;
4101da177e4SLinus Torvalds 	starget->channel = channel;
411f0c0a376SMike Christie 	starget->can_queue = 0;
4121da177e4SLinus Torvalds 	INIT_LIST_HEAD(&starget->siblings);
4131da177e4SLinus Torvalds 	INIT_LIST_HEAD(&starget->devices);
414643eb2d9SJames Bottomley 	starget->state = STARGET_CREATED;
4157c9d6f16SAlan Stern 	starget->scsi_level = SCSI_2;
416c53a284fSEdward Goggin 	starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED;
417ffedb452SJames Bottomley  retry:
4181da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	found_target = __scsi_find_target(parent, channel, id);
4211da177e4SLinus Torvalds 	if (found_target)
4221da177e4SLinus Torvalds 		goto found;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	list_add_tail(&starget->siblings, &shost->__targets);
4251da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
4261da177e4SLinus Torvalds 	/* allocate and add */
427a283bd37SJames Bottomley 	transport_setup_device(dev);
428a283bd37SJames Bottomley 	if (shost->hostt->target_alloc) {
42932f95792SBrian King 		error = shost->hostt->target_alloc(starget);
430a283bd37SJames Bottomley 
431a283bd37SJames Bottomley 		if(error) {
432a283bd37SJames Bottomley 			dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
433a283bd37SJames Bottomley 			/* don't want scsi_target_reap to do the final
434a283bd37SJames Bottomley 			 * put because it will be under the host lock */
435643eb2d9SJames Bottomley 			scsi_target_destroy(starget);
436a283bd37SJames Bottomley 			return NULL;
437a283bd37SJames Bottomley 		}
438a283bd37SJames Bottomley 	}
439884d25ccSJames Bottomley 	get_device(dev);
440a283bd37SJames Bottomley 
4411da177e4SLinus Torvalds 	return starget;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds  found:
4441da177e4SLinus Torvalds 	found_target->reap_ref++;
4451da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
446ffedb452SJames Bottomley 	if (found_target->state != STARGET_DEL) {
44712fb8c15SAlan Stern 		put_device(dev);
4481da177e4SLinus Torvalds 		return found_target;
4491da177e4SLinus Torvalds 	}
450ffedb452SJames Bottomley 	/* Unfortunately, we found a dying target; need to
451ffedb452SJames Bottomley 	 * wait until it's dead before we can get a new one */
452ffedb452SJames Bottomley 	put_device(&found_target->dev);
453ffedb452SJames Bottomley 	flush_scheduled_work();
454ffedb452SJames Bottomley 	goto retry;
455ffedb452SJames Bottomley }
4561da177e4SLinus Torvalds 
45765f27f38SDavid Howells static void scsi_target_reap_usercontext(struct work_struct *work)
45865110b21SJames Bottomley {
45965f27f38SDavid Howells 	struct scsi_target *starget =
46065f27f38SDavid Howells 		container_of(work, struct scsi_target, ew.work);
461863a930aSJames Bottomley 
46204333393SJames Bottomley 	transport_remove_device(&starget->dev);
463863a930aSJames Bottomley 	device_del(&starget->dev);
464643eb2d9SJames Bottomley 	scsi_target_destroy(starget);
465863a930aSJames Bottomley }
466863a930aSJames Bottomley 
4671da177e4SLinus Torvalds /**
4681da177e4SLinus Torvalds  * scsi_target_reap - check to see if target is in use and destroy if not
4691da177e4SLinus Torvalds  * @starget: target to be checked
4701da177e4SLinus Torvalds  *
4711da177e4SLinus Torvalds  * This is used after removing a LUN or doing a last put of the target
4721da177e4SLinus Torvalds  * it checks atomically that nothing is using the target and removes
4731da177e4SLinus Torvalds  * it if so.
4741da177e4SLinus Torvalds  */
4751da177e4SLinus Torvalds void scsi_target_reap(struct scsi_target *starget)
4761da177e4SLinus Torvalds {
477ffedb452SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
478ffedb452SJames Bottomley 	unsigned long flags;
479643eb2d9SJames Bottomley 	enum scsi_target_state state;
480f9e8894aSAlan Stern 	int empty = 0;
481ffedb452SJames Bottomley 
482ffedb452SJames Bottomley 	spin_lock_irqsave(shost->host_lock, flags);
483643eb2d9SJames Bottomley 	state = starget->state;
484f9e8894aSAlan Stern 	if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
485f9e8894aSAlan Stern 		empty = 1;
486f9e8894aSAlan Stern 		starget->state = STARGET_DEL;
487f9e8894aSAlan Stern 	}
488ffedb452SJames Bottomley 	spin_unlock_irqrestore(shost->host_lock, flags);
489643eb2d9SJames Bottomley 
490643eb2d9SJames Bottomley 	if (!empty)
491643eb2d9SJames Bottomley 		return;
492643eb2d9SJames Bottomley 
493643eb2d9SJames Bottomley 	BUG_ON(state == STARGET_DEL);
494643eb2d9SJames Bottomley 	if (state == STARGET_CREATED)
495643eb2d9SJames Bottomley 		scsi_target_destroy(starget);
496643eb2d9SJames Bottomley 	else
497ffedb452SJames Bottomley 		execute_in_process_context(scsi_target_reap_usercontext,
49865f27f38SDavid Howells 					   &starget->ew);
4991da177e4SLinus Torvalds }
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds /**
502e5b3cd42SAlan Stern  * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
503e5b3cd42SAlan Stern  * @s: INQUIRY result string to sanitize
504e5b3cd42SAlan Stern  * @len: length of the string
505e5b3cd42SAlan Stern  *
506e5b3cd42SAlan Stern  * Description:
507e5b3cd42SAlan Stern  *	The SCSI spec says that INQUIRY vendor, product, and revision
508e5b3cd42SAlan Stern  *	strings must consist entirely of graphic ASCII characters,
509e5b3cd42SAlan Stern  *	padded on the right with spaces.  Since not all devices obey
510e5b3cd42SAlan Stern  *	this rule, we will replace non-graphic or non-ASCII characters
511e5b3cd42SAlan Stern  *	with spaces.  Exception: a NUL character is interpreted as a
512e5b3cd42SAlan Stern  *	string terminator, so all the following characters are set to
513e5b3cd42SAlan Stern  *	spaces.
514e5b3cd42SAlan Stern  **/
515e5b3cd42SAlan Stern static void sanitize_inquiry_string(unsigned char *s, int len)
516e5b3cd42SAlan Stern {
517e5b3cd42SAlan Stern 	int terminated = 0;
518e5b3cd42SAlan Stern 
519e5b3cd42SAlan Stern 	for (; len > 0; (--len, ++s)) {
520e5b3cd42SAlan Stern 		if (*s == 0)
521e5b3cd42SAlan Stern 			terminated = 1;
522e5b3cd42SAlan Stern 		if (terminated || *s < 0x20 || *s > 0x7e)
523e5b3cd42SAlan Stern 			*s = ' ';
524e5b3cd42SAlan Stern 	}
525e5b3cd42SAlan Stern }
526e5b3cd42SAlan Stern 
527e5b3cd42SAlan Stern /**
5281da177e4SLinus Torvalds  * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
52939216033SJames Bottomley  * @sdev:	scsi_device to probe
5301da177e4SLinus Torvalds  * @inq_result:	area to store the INQUIRY result
53139216033SJames Bottomley  * @result_len: len of inq_result
5321da177e4SLinus Torvalds  * @bflags:	store any bflags found here
5331da177e4SLinus Torvalds  *
5341da177e4SLinus Torvalds  * Description:
53539216033SJames Bottomley  *     Probe the lun associated with @req using a standard SCSI INQUIRY;
5361da177e4SLinus Torvalds  *
53739216033SJames Bottomley  *     If the INQUIRY is successful, zero is returned and the
5381da177e4SLinus Torvalds  *     INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
539f64a181dSChristoph Hellwig  *     are copied to the scsi_device any flags value is stored in *@bflags.
5401da177e4SLinus Torvalds  **/
541e5b3cd42SAlan Stern static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
54239216033SJames Bottomley 			  int result_len, int *bflags)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
5451da177e4SLinus Torvalds 	int first_inquiry_len, try_inquiry_len, next_inquiry_len;
5461da177e4SLinus Torvalds 	int response_len = 0;
54739216033SJames Bottomley 	int pass, count, result;
5481da177e4SLinus Torvalds 	struct scsi_sense_hdr sshdr;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	*bflags = 0;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	/* Perform up to 3 passes.  The first pass uses a conservative
5531da177e4SLinus Torvalds 	 * transfer length of 36 unless sdev->inquiry_len specifies a
5541da177e4SLinus Torvalds 	 * different value. */
5551da177e4SLinus Torvalds 	first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
5561da177e4SLinus Torvalds 	try_inquiry_len = first_inquiry_len;
5571da177e4SLinus Torvalds 	pass = 1;
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds  next_pass:
5609ccfc756SJames Bottomley 	SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
5619ccfc756SJames Bottomley 				"scsi scan: INQUIRY pass %d length %d\n",
5629ccfc756SJames Bottomley 				pass, try_inquiry_len));
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	/* Each pass gets up to three chances to ignore Unit Attention */
5651da177e4SLinus Torvalds 	for (count = 0; count < 3; ++count) {
5665cd3bbfaSFUJITA Tomonori 		int resid;
5675cd3bbfaSFUJITA Tomonori 
5681da177e4SLinus Torvalds 		memset(scsi_cmd, 0, 6);
5691da177e4SLinus Torvalds 		scsi_cmd[0] = INQUIRY;
5701da177e4SLinus Torvalds 		scsi_cmd[4] = (unsigned char) try_inquiry_len;
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 		memset(inq_result, 0, try_inquiry_len);
57339216033SJames Bottomley 
57439216033SJames Bottomley 		result = scsi_execute_req(sdev,  scsi_cmd, DMA_FROM_DEVICE,
575ea73a9f2SJames Bottomley 					  inq_result, try_inquiry_len, &sshdr,
576f4f4e47eSFUJITA Tomonori 					  HZ / 2 + HZ * scsi_inq_timeout, 3,
5775cd3bbfaSFUJITA Tomonori 					  &resid);
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 		SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
5801da177e4SLinus Torvalds 				"with code 0x%x\n",
58139216033SJames Bottomley 				result ? "failed" : "successful", result));
5821da177e4SLinus Torvalds 
58339216033SJames Bottomley 		if (result) {
5841da177e4SLinus Torvalds 			/*
5851da177e4SLinus Torvalds 			 * not-ready to ready transition [asc/ascq=0x28/0x0]
5861da177e4SLinus Torvalds 			 * or power-on, reset [asc/ascq=0x29/0x0], continue.
5871da177e4SLinus Torvalds 			 * INQUIRY should not yield UNIT_ATTENTION
5881da177e4SLinus Torvalds 			 * but many buggy devices do so anyway.
5891da177e4SLinus Torvalds 			 */
59039216033SJames Bottomley 			if ((driver_byte(result) & DRIVER_SENSE) &&
591ea73a9f2SJames Bottomley 			    scsi_sense_valid(&sshdr)) {
5921da177e4SLinus Torvalds 				if ((sshdr.sense_key == UNIT_ATTENTION) &&
5931da177e4SLinus Torvalds 				    ((sshdr.asc == 0x28) ||
5941da177e4SLinus Torvalds 				     (sshdr.asc == 0x29)) &&
5951da177e4SLinus Torvalds 				    (sshdr.ascq == 0))
5961da177e4SLinus Torvalds 					continue;
5971da177e4SLinus Torvalds 			}
5985cd3bbfaSFUJITA Tomonori 		} else {
5995cd3bbfaSFUJITA Tomonori 			/*
6005cd3bbfaSFUJITA Tomonori 			 * if nothing was transferred, we try
6015cd3bbfaSFUJITA Tomonori 			 * again. It's a workaround for some USB
6025cd3bbfaSFUJITA Tomonori 			 * devices.
6035cd3bbfaSFUJITA Tomonori 			 */
6045cd3bbfaSFUJITA Tomonori 			if (resid == try_inquiry_len)
6055cd3bbfaSFUJITA Tomonori 				continue;
6061da177e4SLinus Torvalds 		}
6071da177e4SLinus Torvalds 		break;
6081da177e4SLinus Torvalds 	}
6091da177e4SLinus Torvalds 
61039216033SJames Bottomley 	if (result == 0) {
611e5b3cd42SAlan Stern 		sanitize_inquiry_string(&inq_result[8], 8);
612e5b3cd42SAlan Stern 		sanitize_inquiry_string(&inq_result[16], 16);
613e5b3cd42SAlan Stern 		sanitize_inquiry_string(&inq_result[32], 4);
614e5b3cd42SAlan Stern 
615e5b3cd42SAlan Stern 		response_len = inq_result[4] + 5;
6161da177e4SLinus Torvalds 		if (response_len > 255)
6171da177e4SLinus Torvalds 			response_len = first_inquiry_len;	/* sanity */
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds 		/*
6201da177e4SLinus Torvalds 		 * Get any flags for this device.
6211da177e4SLinus Torvalds 		 *
622f64a181dSChristoph Hellwig 		 * XXX add a bflags to scsi_device, and replace the
623f64a181dSChristoph Hellwig 		 * corresponding bit fields in scsi_device, so bflags
6241da177e4SLinus Torvalds 		 * need not be passed as an argument.
6251da177e4SLinus Torvalds 		 */
6261da177e4SLinus Torvalds 		*bflags = scsi_get_device_flags(sdev, &inq_result[8],
6271da177e4SLinus Torvalds 				&inq_result[16]);
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 		/* When the first pass succeeds we gain information about
6301da177e4SLinus Torvalds 		 * what larger transfer lengths might work. */
6311da177e4SLinus Torvalds 		if (pass == 1) {
6321da177e4SLinus Torvalds 			if (BLIST_INQUIRY_36 & *bflags)
6331da177e4SLinus Torvalds 				next_inquiry_len = 36;
6341da177e4SLinus Torvalds 			else if (BLIST_INQUIRY_58 & *bflags)
6351da177e4SLinus Torvalds 				next_inquiry_len = 58;
6361da177e4SLinus Torvalds 			else if (sdev->inquiry_len)
6371da177e4SLinus Torvalds 				next_inquiry_len = sdev->inquiry_len;
6381da177e4SLinus Torvalds 			else
6391da177e4SLinus Torvalds 				next_inquiry_len = response_len;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 			/* If more data is available perform the second pass */
6421da177e4SLinus Torvalds 			if (next_inquiry_len > try_inquiry_len) {
6431da177e4SLinus Torvalds 				try_inquiry_len = next_inquiry_len;
6441da177e4SLinus Torvalds 				pass = 2;
6451da177e4SLinus Torvalds 				goto next_pass;
6461da177e4SLinus Torvalds 			}
6471da177e4SLinus Torvalds 		}
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	} else if (pass == 2) {
6501da177e4SLinus Torvalds 		printk(KERN_INFO "scsi scan: %d byte inquiry failed.  "
6511da177e4SLinus Torvalds 				"Consider BLIST_INQUIRY_36 for this device\n",
6521da177e4SLinus Torvalds 				try_inquiry_len);
6531da177e4SLinus Torvalds 
6541da177e4SLinus Torvalds 		/* If this pass failed, the third pass goes back and transfers
6551da177e4SLinus Torvalds 		 * the same amount as we successfully got in the first pass. */
6561da177e4SLinus Torvalds 		try_inquiry_len = first_inquiry_len;
6571da177e4SLinus Torvalds 		pass = 3;
6581da177e4SLinus Torvalds 		goto next_pass;
6591da177e4SLinus Torvalds 	}
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	/* If the last transfer attempt got an error, assume the
6621da177e4SLinus Torvalds 	 * peripheral doesn't exist or is dead. */
66339216033SJames Bottomley 	if (result)
66439216033SJames Bottomley 		return -EIO;
6651da177e4SLinus Torvalds 
6661da177e4SLinus Torvalds 	/* Don't report any more data than the device says is valid */
6671da177e4SLinus Torvalds 	sdev->inquiry_len = min(try_inquiry_len, response_len);
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/*
6701da177e4SLinus Torvalds 	 * XXX Abort if the response length is less than 36? If less than
6711da177e4SLinus Torvalds 	 * 32, the lookup of the device flags (above) could be invalid,
6721da177e4SLinus Torvalds 	 * and it would be possible to take an incorrect action - we do
6731da177e4SLinus Torvalds 	 * not want to hang because of a short INQUIRY. On the flip side,
6741da177e4SLinus Torvalds 	 * if the device is spun down or becoming ready (and so it gives a
6751da177e4SLinus Torvalds 	 * short INQUIRY), an abort here prevents any further use of the
6761da177e4SLinus Torvalds 	 * device, including spin up.
6771da177e4SLinus Torvalds 	 *
678e423ee31SAlan Stern 	 * On the whole, the best approach seems to be to assume the first
679e423ee31SAlan Stern 	 * 36 bytes are valid no matter what the device says.  That's
680e423ee31SAlan Stern 	 * better than copying < 36 bytes to the inquiry-result buffer
681e423ee31SAlan Stern 	 * and displaying garbage for the Vendor, Product, or Revision
682e423ee31SAlan Stern 	 * strings.
683e423ee31SAlan Stern 	 */
684e423ee31SAlan Stern 	if (sdev->inquiry_len < 36) {
685e423ee31SAlan Stern 		printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
686e423ee31SAlan Stern 				" using 36\n", sdev->inquiry_len);
687e423ee31SAlan Stern 		sdev->inquiry_len = 36;
688e423ee31SAlan Stern 	}
689e423ee31SAlan Stern 
690e423ee31SAlan Stern 	/*
6911da177e4SLinus Torvalds 	 * Related to the above issue:
6921da177e4SLinus Torvalds 	 *
6931da177e4SLinus Torvalds 	 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
6941da177e4SLinus Torvalds 	 * and if not ready, sent a START_STOP to start (maybe spin up) and
6951da177e4SLinus Torvalds 	 * then send the INQUIRY again, since the INQUIRY can change after
6961da177e4SLinus Torvalds 	 * a device is initialized.
6971da177e4SLinus Torvalds 	 *
6981da177e4SLinus Torvalds 	 * Ideally, start a device if explicitly asked to do so.  This
6991da177e4SLinus Torvalds 	 * assumes that a device is spun up on power on, spun down on
7001da177e4SLinus Torvalds 	 * request, and then spun up on request.
7011da177e4SLinus Torvalds 	 */
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	/*
7041da177e4SLinus Torvalds 	 * The scanning code needs to know the scsi_level, even if no
7051da177e4SLinus Torvalds 	 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
7061da177e4SLinus Torvalds 	 * non-zero LUNs can be scanned.
7071da177e4SLinus Torvalds 	 */
7081da177e4SLinus Torvalds 	sdev->scsi_level = inq_result[2] & 0x07;
7091da177e4SLinus Torvalds 	if (sdev->scsi_level >= 2 ||
7101da177e4SLinus Torvalds 	    (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
7111da177e4SLinus Torvalds 		sdev->scsi_level++;
7126f3a2024SJames Bottomley 	sdev->sdev_target->scsi_level = sdev->scsi_level;
7131da177e4SLinus Torvalds 
71439216033SJames Bottomley 	return 0;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds /**
718f64a181dSChristoph Hellwig  * scsi_add_lun - allocate and fully initialze a scsi_device
7196d877688SMatthew Wilcox  * @sdev:	holds information to be stored in the new scsi_device
7201da177e4SLinus Torvalds  * @inq_result:	holds the result of a previous INQUIRY to the LUN
7211da177e4SLinus Torvalds  * @bflags:	black/white list flag
7226d877688SMatthew Wilcox  * @async:	1 if this device is being scanned asynchronously
7231da177e4SLinus Torvalds  *
7241da177e4SLinus Torvalds  * Description:
7256d877688SMatthew Wilcox  *     Initialize the scsi_device @sdev.  Optionally set fields based
7266d877688SMatthew Wilcox  *     on values in *@bflags.
7271da177e4SLinus Torvalds  *
7281da177e4SLinus Torvalds  * Return:
729f64a181dSChristoph Hellwig  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
730f64a181dSChristoph Hellwig  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
7311da177e4SLinus Torvalds  **/
732e5b3cd42SAlan Stern static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
7333e082a91SMatthew Wilcox 		int *bflags, int async)
7341da177e4SLinus Torvalds {
7356f4267e3SJames Bottomley 	int ret;
7366f4267e3SJames Bottomley 
7371da177e4SLinus Torvalds 	/*
7381da177e4SLinus Torvalds 	 * XXX do not save the inquiry, since it can change underneath us,
7391da177e4SLinus Torvalds 	 * save just vendor/model/rev.
7401da177e4SLinus Torvalds 	 *
7411da177e4SLinus Torvalds 	 * Rather than save it and have an ioctl that retrieves the saved
7421da177e4SLinus Torvalds 	 * value, have an ioctl that executes the same INQUIRY code used
7431da177e4SLinus Torvalds 	 * in scsi_probe_lun, let user level programs doing INQUIRY
7441da177e4SLinus Torvalds 	 * scanning run at their own risk, or supply a user level program
7451da177e4SLinus Torvalds 	 * that can correctly scan.
7461da177e4SLinus Torvalds 	 */
7471da177e4SLinus Torvalds 
74809123d23SAlan Stern 	/*
74909123d23SAlan Stern 	 * Copy at least 36 bytes of INQUIRY data, so that we don't
75009123d23SAlan Stern 	 * dereference unallocated memory when accessing the Vendor,
75109123d23SAlan Stern 	 * Product, and Revision strings.  Badly behaved devices may set
75209123d23SAlan Stern 	 * the INQUIRY Additional Length byte to a small value, indicating
75309123d23SAlan Stern 	 * these strings are invalid, but often they contain plausible data
75409123d23SAlan Stern 	 * nonetheless.  It doesn't matter if the device sent < 36 bytes
75509123d23SAlan Stern 	 * total, since scsi_probe_lun() initializes inq_result with 0s.
75609123d23SAlan Stern 	 */
75709123d23SAlan Stern 	sdev->inquiry = kmemdup(inq_result,
75809123d23SAlan Stern 				max_t(size_t, sdev->inquiry_len, 36),
75909123d23SAlan Stern 				GFP_ATOMIC);
76009123d23SAlan Stern 	if (sdev->inquiry == NULL)
76109123d23SAlan Stern 		return SCSI_SCAN_NO_RESPONSE;
76209123d23SAlan Stern 
7631da177e4SLinus Torvalds 	sdev->vendor = (char *) (sdev->inquiry + 8);
7641da177e4SLinus Torvalds 	sdev->model = (char *) (sdev->inquiry + 16);
7651da177e4SLinus Torvalds 	sdev->rev = (char *) (sdev->inquiry + 32);
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	if (*bflags & BLIST_ISROM) {
7686d877688SMatthew Wilcox 		sdev->type = TYPE_ROM;
7696d877688SMatthew Wilcox 		sdev->removable = 1;
7706d877688SMatthew Wilcox 	} else {
7716d877688SMatthew Wilcox 		sdev->type = (inq_result[0] & 0x1f);
7726d877688SMatthew Wilcox 		sdev->removable = (inq_result[1] & 0x80) >> 7;
7736d877688SMatthew Wilcox 	}
7741da177e4SLinus Torvalds 
7756d877688SMatthew Wilcox 	switch (sdev->type) {
776ddaf6fc8SJames Bottomley 	case TYPE_RBC:
7771da177e4SLinus Torvalds 	case TYPE_TAPE:
7781da177e4SLinus Torvalds 	case TYPE_DISK:
7791da177e4SLinus Torvalds 	case TYPE_PRINTER:
7801da177e4SLinus Torvalds 	case TYPE_MOD:
7811da177e4SLinus Torvalds 	case TYPE_PROCESSOR:
7821da177e4SLinus Torvalds 	case TYPE_SCANNER:
7831da177e4SLinus Torvalds 	case TYPE_MEDIUM_CHANGER:
7841da177e4SLinus Torvalds 	case TYPE_ENCLOSURE:
7851da177e4SLinus Torvalds 	case TYPE_COMM:
7864d7db04aSJames Bottomley 	case TYPE_RAID:
78782443a58SBoaz Harrosh 	case TYPE_OSD:
7881da177e4SLinus Torvalds 		sdev->writeable = 1;
7891da177e4SLinus Torvalds 		break;
7901da177e4SLinus Torvalds 	case TYPE_ROM:
791ddaf6fc8SJames Bottomley 	case TYPE_WORM:
7921da177e4SLinus Torvalds 		sdev->writeable = 0;
7931da177e4SLinus Torvalds 		break;
7941da177e4SLinus Torvalds 	default:
7951da177e4SLinus Torvalds 		printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 
7986d877688SMatthew Wilcox 	if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
7996d877688SMatthew Wilcox 		/* RBC and MMC devices can return SCSI-3 compliance and yet
8006d877688SMatthew Wilcox 		 * still not support REPORT LUNS, so make them act as
8016d877688SMatthew Wilcox 		 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
8026d877688SMatthew Wilcox 		 * specifically set */
8036d877688SMatthew Wilcox 		if ((*bflags & BLIST_REPORTLUN2) == 0)
8046d877688SMatthew Wilcox 			*bflags |= BLIST_NOREPORTLUN;
8056d877688SMatthew Wilcox 	}
8066d877688SMatthew Wilcox 
8071da177e4SLinus Torvalds 	/*
8081da177e4SLinus Torvalds 	 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
8091da177e4SLinus Torvalds 	 * spec says: The device server is capable of supporting the
8101da177e4SLinus Torvalds 	 * specified peripheral device type on this logical unit. However,
8111da177e4SLinus Torvalds 	 * the physical device is not currently connected to this logical
8121da177e4SLinus Torvalds 	 * unit.
8131da177e4SLinus Torvalds 	 *
8141da177e4SLinus Torvalds 	 * The above is vague, as it implies that we could treat 001 and
8151da177e4SLinus Torvalds 	 * 011 the same. Stay compatible with previous code, and create a
816f64a181dSChristoph Hellwig 	 * scsi_device for a PQ of 1
8171da177e4SLinus Torvalds 	 *
8181da177e4SLinus Torvalds 	 * Don't set the device offline here; rather let the upper
8191da177e4SLinus Torvalds 	 * level drivers eval the PQ to decide whether they should
8201da177e4SLinus Torvalds 	 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
8211da177e4SLinus Torvalds 	 */
8221da177e4SLinus Torvalds 
8231da177e4SLinus Torvalds 	sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
8241da177e4SLinus Torvalds 	sdev->lockable = sdev->removable;
8251da177e4SLinus Torvalds 	sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
8261da177e4SLinus Torvalds 
8276d877688SMatthew Wilcox 	if (sdev->scsi_level >= SCSI_3 ||
8286d877688SMatthew Wilcox 			(sdev->inquiry_len > 56 && inq_result[56] & 0x04))
8291da177e4SLinus Torvalds 		sdev->ppr = 1;
8301da177e4SLinus Torvalds 	if (inq_result[7] & 0x60)
8311da177e4SLinus Torvalds 		sdev->wdtr = 1;
8321da177e4SLinus Torvalds 	if (inq_result[7] & 0x10)
8331da177e4SLinus Torvalds 		sdev->sdtr = 1;
8341da177e4SLinus Torvalds 
83519ac0db3SJames Bottomley 	sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
8364ff36718SMatthew Wilcox 			"ANSI: %d%s\n", scsi_device_type(sdev->type),
8374ff36718SMatthew Wilcox 			sdev->vendor, sdev->model, sdev->rev,
8384ff36718SMatthew Wilcox 			sdev->inq_periph_qual, inq_result[2] & 0x07,
8394ff36718SMatthew Wilcox 			(inq_result[3] & 0x0f) == 1 ? " CCS" : "");
8404ff36718SMatthew Wilcox 
8411da177e4SLinus Torvalds 	if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
8421da177e4SLinus Torvalds 	    !(*bflags & BLIST_NOTQ))
8431da177e4SLinus Torvalds 		sdev->tagged_supported = 1;
8446d877688SMatthew Wilcox 
8451da177e4SLinus Torvalds 	/*
8461da177e4SLinus Torvalds 	 * Some devices (Texel CD ROM drives) have handshaking problems
8471da177e4SLinus Torvalds 	 * when used with the Seagate controllers. borken is initialized
8481da177e4SLinus Torvalds 	 * to 1, and then set it to 0 here.
8491da177e4SLinus Torvalds 	 */
8501da177e4SLinus Torvalds 	if ((*bflags & BLIST_BORKEN) == 0)
8511da177e4SLinus Torvalds 		sdev->borken = 0;
8521da177e4SLinus Torvalds 
8536d877688SMatthew Wilcox 	if (*bflags & BLIST_NO_ULD_ATTACH)
8546d877688SMatthew Wilcox 		sdev->no_uld_attach = 1;
8556d877688SMatthew Wilcox 
8561da177e4SLinus Torvalds 	/*
8571da177e4SLinus Torvalds 	 * Apparently some really broken devices (contrary to the SCSI
8581da177e4SLinus Torvalds 	 * standards) need to be selected without asserting ATN
8591da177e4SLinus Torvalds 	 */
8601da177e4SLinus Torvalds 	if (*bflags & BLIST_SELECT_NO_ATN)
8611da177e4SLinus Torvalds 		sdev->select_no_atn = 1;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	/*
8644d7db04aSJames Bottomley 	 * Maximum 512 sector transfer length
8654d7db04aSJames Bottomley 	 * broken RA4x00 Compaq Disk Array
8664d7db04aSJames Bottomley 	 */
8674d7db04aSJames Bottomley 	if (*bflags & BLIST_MAX_512)
868086fa5ffSMartin K. Petersen 		blk_queue_max_hw_sectors(sdev->request_queue, 512);
8694d7db04aSJames Bottomley 
8704d7db04aSJames Bottomley 	/*
8711da177e4SLinus Torvalds 	 * Some devices may not want to have a start command automatically
8721da177e4SLinus Torvalds 	 * issued when a device is added.
8731da177e4SLinus Torvalds 	 */
8741da177e4SLinus Torvalds 	if (*bflags & BLIST_NOSTARTONADD)
8751da177e4SLinus Torvalds 		sdev->no_start_on_add = 1;
8761da177e4SLinus Torvalds 
8771da177e4SLinus Torvalds 	if (*bflags & BLIST_SINGLELUN)
87825d7c363STony Battersby 		scsi_target(sdev)->single_lun = 1;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	sdev->use_10_for_rw = 1;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	if (*bflags & BLIST_MS_SKIP_PAGE_08)
8831da177e4SLinus Torvalds 		sdev->skip_ms_page_8 = 1;
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	if (*bflags & BLIST_MS_SKIP_PAGE_3F)
8861da177e4SLinus Torvalds 		sdev->skip_ms_page_3f = 1;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	if (*bflags & BLIST_USE_10_BYTE_MS)
8891da177e4SLinus Torvalds 		sdev->use_10_for_ms = 1;
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	/* set the device running here so that slave configure
8921da177e4SLinus Torvalds 	 * may do I/O */
8936f4267e3SJames Bottomley 	ret = scsi_device_set_state(sdev, SDEV_RUNNING);
8946f4267e3SJames Bottomley 	if (ret) {
8956f4267e3SJames Bottomley 		ret = scsi_device_set_state(sdev, SDEV_BLOCK);
8966f4267e3SJames Bottomley 
8976f4267e3SJames Bottomley 		if (ret) {
8986f4267e3SJames Bottomley 			sdev_printk(KERN_ERR, sdev,
8996f4267e3SJames Bottomley 				    "in wrong state %s to complete scan\n",
9006f4267e3SJames Bottomley 				    scsi_device_state_name(sdev->sdev_state));
9016f4267e3SJames Bottomley 			return SCSI_SCAN_NO_RESPONSE;
9026f4267e3SJames Bottomley 		}
9036f4267e3SJames Bottomley 	}
9041da177e4SLinus Torvalds 
9051da177e4SLinus Torvalds 	if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
9061da177e4SLinus Torvalds 		sdev->use_192_bytes_for_3f = 1;
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds 	if (*bflags & BLIST_NOT_LOCKABLE)
9091da177e4SLinus Torvalds 		sdev->lockable = 0;
9101da177e4SLinus Torvalds 
9111da177e4SLinus Torvalds 	if (*bflags & BLIST_RETRY_HWERROR)
9121da177e4SLinus Torvalds 		sdev->retry_hwerror = 1;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	transport_configure_device(&sdev->sdev_gendev);
9151da177e4SLinus Torvalds 
91693805091SChristoph Hellwig 	if (sdev->host->hostt->slave_configure) {
9176f4267e3SJames Bottomley 		ret = sdev->host->hostt->slave_configure(sdev);
91893805091SChristoph Hellwig 		if (ret) {
91993805091SChristoph Hellwig 			/*
92093805091SChristoph Hellwig 			 * if LLDD reports slave not present, don't clutter
92193805091SChristoph Hellwig 			 * console with alloc failure messages
92293805091SChristoph Hellwig 			 */
92393805091SChristoph Hellwig 			if (ret != -ENXIO) {
92493805091SChristoph Hellwig 				sdev_printk(KERN_ERR, sdev,
92593805091SChristoph Hellwig 					"failed to configure device\n");
92693805091SChristoph Hellwig 			}
92793805091SChristoph Hellwig 			return SCSI_SCAN_NO_RESPONSE;
92893805091SChristoph Hellwig 		}
92993805091SChristoph Hellwig 	}
9301da177e4SLinus Torvalds 
9314a84067dSVasu Dev 	sdev->max_queue_depth = sdev->queue_depth;
9324a84067dSVasu Dev 
9331da177e4SLinus Torvalds 	/*
9341da177e4SLinus Torvalds 	 * Ok, the device is now all set up, we can
9351da177e4SLinus Torvalds 	 * register it and tell the rest of the kernel
9361da177e4SLinus Torvalds 	 * about it.
9371da177e4SLinus Torvalds 	 */
9383e082a91SMatthew Wilcox 	if (!async && scsi_sysfs_add_sdev(sdev) != 0)
939b24b1033SAlan Stern 		return SCSI_SCAN_NO_RESPONSE;
9401da177e4SLinus Torvalds 
9411da177e4SLinus Torvalds 	return SCSI_SCAN_LUN_PRESENT;
9421da177e4SLinus Torvalds }
9431da177e4SLinus Torvalds 
944c5f2e640Sakpm@osdl.org #ifdef CONFIG_SCSI_LOGGING
9456c7154c9SKurt Garloff /**
946eb44820cSRob Landley  * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace
9476c7154c9SKurt Garloff  * @buf:   Output buffer with at least end-first+1 bytes of space
9486c7154c9SKurt Garloff  * @inq:   Inquiry buffer (input)
9496c7154c9SKurt Garloff  * @first: Offset of string into inq
9506c7154c9SKurt Garloff  * @end:   Index after last character in inq
9516c7154c9SKurt Garloff  */
9526c7154c9SKurt Garloff static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
9536c7154c9SKurt Garloff 				   unsigned first, unsigned end)
9546c7154c9SKurt Garloff {
9556c7154c9SKurt Garloff 	unsigned term = 0, idx;
956c5f2e640Sakpm@osdl.org 
957c5f2e640Sakpm@osdl.org 	for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
958c5f2e640Sakpm@osdl.org 		if (inq[idx+first] > ' ') {
9596c7154c9SKurt Garloff 			buf[idx] = inq[idx+first];
9606c7154c9SKurt Garloff 			term = idx+1;
9616c7154c9SKurt Garloff 		} else {
9626c7154c9SKurt Garloff 			buf[idx] = ' ';
9636c7154c9SKurt Garloff 		}
9646c7154c9SKurt Garloff 	}
9656c7154c9SKurt Garloff 	buf[term] = 0;
9666c7154c9SKurt Garloff 	return buf;
9676c7154c9SKurt Garloff }
968c5f2e640Sakpm@osdl.org #endif
9696f3a2024SJames Bottomley 
9701da177e4SLinus Torvalds /**
9711da177e4SLinus Torvalds  * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
9721da177e4SLinus Torvalds  * @starget:	pointer to target device structure
9731da177e4SLinus Torvalds  * @lun:	LUN of target device
9741da177e4SLinus Torvalds  * @bflagsp:	store bflags here if not NULL
975eb44820cSRob Landley  * @sdevp:	probe the LUN corresponding to this scsi_device
976eb44820cSRob Landley  * @rescan:     if nonzero skip some code only needed on first scan
977eb44820cSRob Landley  * @hostdata:	passed to scsi_alloc_sdev()
9781da177e4SLinus Torvalds  *
9791da177e4SLinus Torvalds  * Description:
9801da177e4SLinus Torvalds  *     Call scsi_probe_lun, if a LUN with an attached device is found,
9811da177e4SLinus Torvalds  *     allocate and set it up by calling scsi_add_lun.
9821da177e4SLinus Torvalds  *
9831da177e4SLinus Torvalds  * Return:
984f64a181dSChristoph Hellwig  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
9851da177e4SLinus Torvalds  *     SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
9861da177e4SLinus Torvalds  *         attached at the LUN
987f64a181dSChristoph Hellwig  *     SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
9881da177e4SLinus Torvalds  **/
9891da177e4SLinus Torvalds static int scsi_probe_and_add_lun(struct scsi_target *starget,
9901da177e4SLinus Torvalds 				  uint lun, int *bflagsp,
9911da177e4SLinus Torvalds 				  struct scsi_device **sdevp, int rescan,
9921da177e4SLinus Torvalds 				  void *hostdata)
9931da177e4SLinus Torvalds {
9941da177e4SLinus Torvalds 	struct scsi_device *sdev;
9951da177e4SLinus Torvalds 	unsigned char *result;
99639216033SJames Bottomley 	int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
9971da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
9981da177e4SLinus Torvalds 
9991da177e4SLinus Torvalds 	/*
10001da177e4SLinus Torvalds 	 * The rescan flag is used as an optimization, the first scan of a
10011da177e4SLinus Torvalds 	 * host adapter calls into here with rescan == 0.
10021da177e4SLinus Torvalds 	 */
10031da177e4SLinus Torvalds 	sdev = scsi_device_lookup_by_target(starget, lun);
10041da177e4SLinus Torvalds 	if (sdev) {
10050f1d87a2SJames Bottomley 		if (rescan || !scsi_device_created(sdev)) {
10061da177e4SLinus Torvalds 			SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
10071da177e4SLinus Torvalds 				"scsi scan: device exists on %s\n",
100871610f55SKay Sievers 				dev_name(&sdev->sdev_gendev)));
10091da177e4SLinus Torvalds 			if (sdevp)
10101da177e4SLinus Torvalds 				*sdevp = sdev;
10111da177e4SLinus Torvalds 			else
10121da177e4SLinus Torvalds 				scsi_device_put(sdev);
10131da177e4SLinus Torvalds 
10141da177e4SLinus Torvalds 			if (bflagsp)
10151da177e4SLinus Torvalds 				*bflagsp = scsi_get_device_flags(sdev,
10161da177e4SLinus Torvalds 								 sdev->vendor,
10171da177e4SLinus Torvalds 								 sdev->model);
10181da177e4SLinus Torvalds 			return SCSI_SCAN_LUN_PRESENT;
10191da177e4SLinus Torvalds 		}
10206f3a2024SJames Bottomley 		scsi_device_put(sdev);
10216f3a2024SJames Bottomley 	} else
10221da177e4SLinus Torvalds 		sdev = scsi_alloc_sdev(starget, lun, hostdata);
10231da177e4SLinus Torvalds 	if (!sdev)
10241da177e4SLinus Torvalds 		goto out;
102539216033SJames Bottomley 
102639216033SJames Bottomley 	result = kmalloc(result_len, GFP_ATOMIC |
1027bc86120aSAl Viro 			((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
10281da177e4SLinus Torvalds 	if (!result)
102939216033SJames Bottomley 		goto out_free_sdev;
10301da177e4SLinus Torvalds 
103139216033SJames Bottomley 	if (scsi_probe_lun(sdev, result, result_len, &bflags))
10321da177e4SLinus Torvalds 		goto out_free_result;
10331da177e4SLinus Torvalds 
10344186ab19SKurt Garloff 	if (bflagsp)
10354186ab19SKurt Garloff 		*bflagsp = bflags;
10361da177e4SLinus Torvalds 	/*
10371da177e4SLinus Torvalds 	 * result contains valid SCSI INQUIRY data.
10381da177e4SLinus Torvalds 	 */
103913f7e5acSKurt Garloff 	if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
10401da177e4SLinus Torvalds 		/*
10411da177e4SLinus Torvalds 		 * For a Peripheral qualifier 3 (011b), the SCSI
10421da177e4SLinus Torvalds 		 * spec says: The device server is not capable of
10431da177e4SLinus Torvalds 		 * supporting a physical device on this logical
10441da177e4SLinus Torvalds 		 * unit.
10451da177e4SLinus Torvalds 		 *
10461da177e4SLinus Torvalds 		 * For disks, this implies that there is no
10471da177e4SLinus Torvalds 		 * logical disk configured at sdev->lun, but there
10481da177e4SLinus Torvalds 		 * is a target id responding.
10491da177e4SLinus Torvalds 		 */
10506c7154c9SKurt Garloff 		SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
10516c7154c9SKurt Garloff 				   " peripheral qualifier of 3, device not"
10526c7154c9SKurt Garloff 				   " added\n"))
10536c7154c9SKurt Garloff 		if (lun == 0) {
1054c5f2e640Sakpm@osdl.org 			SCSI_LOG_SCAN_BUS(1, {
1055c5f2e640Sakpm@osdl.org 				unsigned char vend[9];
1056c5f2e640Sakpm@osdl.org 				unsigned char mod[17];
1057c5f2e640Sakpm@osdl.org 
1058c5f2e640Sakpm@osdl.org 				sdev_printk(KERN_INFO, sdev,
10596c7154c9SKurt Garloff 					"scsi scan: consider passing scsi_mod."
10603424a65dSKurt Garloff 					"dev_flags=%s:%s:0x240 or 0x1000240\n",
10616c7154c9SKurt Garloff 					scsi_inq_str(vend, result, 8, 16),
1062c5f2e640Sakpm@osdl.org 					scsi_inq_str(mod, result, 16, 32));
1063c5f2e640Sakpm@osdl.org 			});
1064643eb2d9SJames Bottomley 
10656c7154c9SKurt Garloff 		}
10666c7154c9SKurt Garloff 
10671da177e4SLinus Torvalds 		res = SCSI_SCAN_TARGET_PRESENT;
10681da177e4SLinus Torvalds 		goto out_free_result;
10691da177e4SLinus Torvalds 	}
10701da177e4SLinus Torvalds 
10711bfc5d9dSAlan Stern 	/*
107284961f28Sdave wysochanski 	 * Some targets may set slight variations of PQ and PDT to signal
107384961f28Sdave wysochanski 	 * that no LUN is present, so don't add sdev in these cases.
107484961f28Sdave wysochanski 	 * Two specific examples are:
107584961f28Sdave wysochanski 	 * 1) NetApp targets: return PQ=1, PDT=0x1f
107684961f28Sdave wysochanski 	 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
107784961f28Sdave wysochanski 	 *    in the UFI 1.0 spec (we cannot rely on reserved bits).
107884961f28Sdave wysochanski 	 *
107984961f28Sdave wysochanski 	 * References:
108084961f28Sdave wysochanski 	 * 1) SCSI SPC-3, pp. 145-146
108184961f28Sdave wysochanski 	 * PQ=1: "A peripheral device having the specified peripheral
108284961f28Sdave wysochanski 	 * device type is not connected to this logical unit. However, the
108384961f28Sdave wysochanski 	 * device server is capable of supporting the specified peripheral
108484961f28Sdave wysochanski 	 * device type on this logical unit."
108584961f28Sdave wysochanski 	 * PDT=0x1f: "Unknown or no device type"
108684961f28Sdave wysochanski 	 * 2) USB UFI 1.0, p. 20
108784961f28Sdave wysochanski 	 * PDT=00h Direct-access device (floppy)
108884961f28Sdave wysochanski 	 * PDT=1Fh none (no FDD connected to the requested logical unit)
10891bfc5d9dSAlan Stern 	 */
109084961f28Sdave wysochanski 	if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
109101b291bdSJames Bottomley 	    (result[0] & 0x1f) == 0x1f &&
109201b291bdSJames Bottomley 	    !scsi_is_wlun(lun)) {
10931bfc5d9dSAlan Stern 		SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
10941bfc5d9dSAlan Stern 					"scsi scan: peripheral device type"
10951bfc5d9dSAlan Stern 					" of 31, no device added\n"));
10961bfc5d9dSAlan Stern 		res = SCSI_SCAN_TARGET_PRESENT;
10971bfc5d9dSAlan Stern 		goto out_free_result;
10981bfc5d9dSAlan Stern 	}
10991bfc5d9dSAlan Stern 
11003e082a91SMatthew Wilcox 	res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
11011da177e4SLinus Torvalds 	if (res == SCSI_SCAN_LUN_PRESENT) {
11021da177e4SLinus Torvalds 		if (bflags & BLIST_KEY) {
11031da177e4SLinus Torvalds 			sdev->lockable = 0;
110439216033SJames Bottomley 			scsi_unlock_floptical(sdev, result);
11051da177e4SLinus Torvalds 		}
11061da177e4SLinus Torvalds 	}
11071da177e4SLinus Torvalds 
11081da177e4SLinus Torvalds  out_free_result:
11091da177e4SLinus Torvalds 	kfree(result);
11101da177e4SLinus Torvalds  out_free_sdev:
11111da177e4SLinus Torvalds 	if (res == SCSI_SCAN_LUN_PRESENT) {
11121da177e4SLinus Torvalds 		if (sdevp) {
1113b70d37bfSAlan Stern 			if (scsi_device_get(sdev) == 0) {
11141da177e4SLinus Torvalds 				*sdevp = sdev;
1115b70d37bfSAlan Stern 			} else {
1116b70d37bfSAlan Stern 				__scsi_remove_device(sdev);
1117b70d37bfSAlan Stern 				res = SCSI_SCAN_NO_RESPONSE;
1118b70d37bfSAlan Stern 			}
11191da177e4SLinus Torvalds 		}
11206f3a2024SJames Bottomley 	} else
1121860dc736SJames Bottomley 		__scsi_remove_device(sdev);
11221da177e4SLinus Torvalds  out:
11231da177e4SLinus Torvalds 	return res;
11241da177e4SLinus Torvalds }
11251da177e4SLinus Torvalds 
11261da177e4SLinus Torvalds /**
11271da177e4SLinus Torvalds  * scsi_sequential_lun_scan - sequentially scan a SCSI target
11281da177e4SLinus Torvalds  * @starget:	pointer to target structure to scan
11291da177e4SLinus Torvalds  * @bflags:	black/white list flag for LUN 0
1130eb44820cSRob Landley  * @scsi_level: Which version of the standard does this device adhere to
1131eb44820cSRob Landley  * @rescan:     passed to scsi_probe_add_lun()
11321da177e4SLinus Torvalds  *
11331da177e4SLinus Torvalds  * Description:
11341da177e4SLinus Torvalds  *     Generally, scan from LUN 1 (LUN 0 is assumed to already have been
11351da177e4SLinus Torvalds  *     scanned) to some maximum lun until a LUN is found with no device
11361da177e4SLinus Torvalds  *     attached. Use the bflags to figure out any oddities.
11371da177e4SLinus Torvalds  *
11381da177e4SLinus Torvalds  *     Modifies sdevscan->lun.
11391da177e4SLinus Torvalds  **/
11401da177e4SLinus Torvalds static void scsi_sequential_lun_scan(struct scsi_target *starget,
11414186ab19SKurt Garloff 				     int bflags, int scsi_level, int rescan)
11421da177e4SLinus Torvalds {
11431da177e4SLinus Torvalds 	unsigned int sparse_lun, lun, max_dev_lun;
11441da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
11451da177e4SLinus Torvalds 
11461da177e4SLinus Torvalds 	SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
114771610f55SKay Sievers 				    "%s\n", dev_name(&starget->dev)));
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	max_dev_lun = min(max_scsi_luns, shost->max_lun);
11501da177e4SLinus Torvalds 	/*
11511da177e4SLinus Torvalds 	 * If this device is known to support sparse multiple units,
11521da177e4SLinus Torvalds 	 * override the other settings, and scan all of them. Normally,
11531da177e4SLinus Torvalds 	 * SCSI-3 devices should be scanned via the REPORT LUNS.
11541da177e4SLinus Torvalds 	 */
11551da177e4SLinus Torvalds 	if (bflags & BLIST_SPARSELUN) {
11561da177e4SLinus Torvalds 		max_dev_lun = shost->max_lun;
11571da177e4SLinus Torvalds 		sparse_lun = 1;
11581da177e4SLinus Torvalds 	} else
11591da177e4SLinus Torvalds 		sparse_lun = 0;
11601da177e4SLinus Torvalds 
11611da177e4SLinus Torvalds 	/*
11621da177e4SLinus Torvalds 	 * If less than SCSI_1_CSS, and no special lun scaning, stop
11631da177e4SLinus Torvalds 	 * scanning; this matches 2.4 behaviour, but could just be a bug
11641da177e4SLinus Torvalds 	 * (to continue scanning a SCSI_1_CSS device).
11651da177e4SLinus Torvalds 	 *
11661da177e4SLinus Torvalds 	 * This test is broken.  We might not have any device on lun0 for
11671da177e4SLinus Torvalds 	 * a sparselun device, and if that's the case then how would we
11681da177e4SLinus Torvalds 	 * know the real scsi_level, eh?  It might make sense to just not
11691da177e4SLinus Torvalds 	 * scan any SCSI_1 device for non-0 luns, but that check would best
11701da177e4SLinus Torvalds 	 * go into scsi_alloc_sdev() and just have it return null when asked
11711da177e4SLinus Torvalds 	 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
11721da177e4SLinus Torvalds 	 *
11731da177e4SLinus Torvalds 	if ((sdevscan->scsi_level < SCSI_1_CCS) &&
11741da177e4SLinus Torvalds 	    ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
11751da177e4SLinus Torvalds 	     == 0))
11761da177e4SLinus Torvalds 		return;
11771da177e4SLinus Torvalds 	 */
11781da177e4SLinus Torvalds 	/*
11791da177e4SLinus Torvalds 	 * If this device is known to support multiple units, override
11801da177e4SLinus Torvalds 	 * the other settings, and scan all of them.
11811da177e4SLinus Torvalds 	 */
11821da177e4SLinus Torvalds 	if (bflags & BLIST_FORCELUN)
11831da177e4SLinus Torvalds 		max_dev_lun = shost->max_lun;
11841da177e4SLinus Torvalds 	/*
11851da177e4SLinus Torvalds 	 * REGAL CDC-4X: avoid hang after LUN 4
11861da177e4SLinus Torvalds 	 */
11871da177e4SLinus Torvalds 	if (bflags & BLIST_MAX5LUN)
11881da177e4SLinus Torvalds 		max_dev_lun = min(5U, max_dev_lun);
11891da177e4SLinus Torvalds 	/*
11901da177e4SLinus Torvalds 	 * Do not scan SCSI-2 or lower device past LUN 7, unless
11911da177e4SLinus Torvalds 	 * BLIST_LARGELUN.
11921da177e4SLinus Torvalds 	 */
11931da177e4SLinus Torvalds 	if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
11941da177e4SLinus Torvalds 		max_dev_lun = min(8U, max_dev_lun);
11951da177e4SLinus Torvalds 
11961da177e4SLinus Torvalds 	/*
11971da177e4SLinus Torvalds 	 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
11981da177e4SLinus Torvalds 	 * until we reach the max, or no LUN is found and we are not
11991da177e4SLinus Torvalds 	 * sparse_lun.
12001da177e4SLinus Torvalds 	 */
12011da177e4SLinus Torvalds 	for (lun = 1; lun < max_dev_lun; ++lun)
12021da177e4SLinus Torvalds 		if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
12031da177e4SLinus Torvalds 					    NULL) != SCSI_SCAN_LUN_PRESENT) &&
12041da177e4SLinus Torvalds 		    !sparse_lun)
12051da177e4SLinus Torvalds 			return;
12061da177e4SLinus Torvalds }
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds /**
12099f6aa575SRandy Dunlap  * scsilun_to_int - convert a scsi_lun to an int
12101da177e4SLinus Torvalds  * @scsilun:	struct scsi_lun to be converted.
12111da177e4SLinus Torvalds  *
12121da177e4SLinus Torvalds  * Description:
12131da177e4SLinus Torvalds  *     Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
12141da177e4SLinus Torvalds  *     integer, and return the result. The caller must check for
12151da177e4SLinus Torvalds  *     truncation before using this function.
12161da177e4SLinus Torvalds  *
12171da177e4SLinus Torvalds  * Notes:
12181da177e4SLinus Torvalds  *     The struct scsi_lun is assumed to be four levels, with each level
12191da177e4SLinus Torvalds  *     effectively containing a SCSI byte-ordered (big endian) short; the
12201da177e4SLinus Torvalds  *     addressing bits of each level are ignored (the highest two bits).
12211da177e4SLinus Torvalds  *     For a description of the LUN format, post SCSI-3 see the SCSI
12221da177e4SLinus Torvalds  *     Architecture Model, for SCSI-3 see the SCSI Controller Commands.
12231da177e4SLinus Torvalds  *
12241da177e4SLinus Torvalds  *     Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
12251da177e4SLinus Torvalds  *     the integer: 0x0b030a04
12261da177e4SLinus Torvalds  **/
1227462b7859SChristof Schmitt int scsilun_to_int(struct scsi_lun *scsilun)
12281da177e4SLinus Torvalds {
12291da177e4SLinus Torvalds 	int i;
12301da177e4SLinus Torvalds 	unsigned int lun;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 	lun = 0;
12331da177e4SLinus Torvalds 	for (i = 0; i < sizeof(lun); i += 2)
12341da177e4SLinus Torvalds 		lun = lun | (((scsilun->scsi_lun[i] << 8) |
12351da177e4SLinus Torvalds 			      scsilun->scsi_lun[i + 1]) << (i * 8));
12361da177e4SLinus Torvalds 	return lun;
12371da177e4SLinus Torvalds }
1238462b7859SChristof Schmitt EXPORT_SYMBOL(scsilun_to_int);
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds /**
12419f6aa575SRandy Dunlap  * int_to_scsilun - reverts an int into a scsi_lun
1242eb44820cSRob Landley  * @lun:        integer to be reverted
12432f4701d8SJames.Smart@Emulex.Com  * @scsilun:	struct scsi_lun to be set.
12442f4701d8SJames.Smart@Emulex.Com  *
12452f4701d8SJames.Smart@Emulex.Com  * Description:
12462f4701d8SJames.Smart@Emulex.Com  *     Reverts the functionality of the scsilun_to_int, which packed
12472f4701d8SJames.Smart@Emulex.Com  *     an 8-byte lun value into an int. This routine unpacks the int
12482f4701d8SJames.Smart@Emulex.Com  *     back into the lun value.
12492f4701d8SJames.Smart@Emulex.Com  *     Note: the scsilun_to_int() routine does not truly handle all
12502f4701d8SJames.Smart@Emulex.Com  *     8bytes of the lun value. This functions restores only as much
12512f4701d8SJames.Smart@Emulex.Com  *     as was set by the routine.
12522f4701d8SJames.Smart@Emulex.Com  *
12532f4701d8SJames.Smart@Emulex.Com  * Notes:
12542f4701d8SJames.Smart@Emulex.Com  *     Given an integer : 0x0b030a04,  this function returns a
12552f4701d8SJames.Smart@Emulex.Com  *     scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
12562f4701d8SJames.Smart@Emulex.Com  *
12572f4701d8SJames.Smart@Emulex.Com  **/
12582f4701d8SJames.Smart@Emulex.Com void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
12592f4701d8SJames.Smart@Emulex.Com {
12602f4701d8SJames.Smart@Emulex.Com 	int i;
12612f4701d8SJames.Smart@Emulex.Com 
12622f4701d8SJames.Smart@Emulex.Com 	memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
12632f4701d8SJames.Smart@Emulex.Com 
12642f4701d8SJames.Smart@Emulex.Com 	for (i = 0; i < sizeof(lun); i += 2) {
12652f4701d8SJames.Smart@Emulex.Com 		scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
12662f4701d8SJames.Smart@Emulex.Com 		scsilun->scsi_lun[i+1] = lun & 0xFF;
12672f4701d8SJames.Smart@Emulex.Com 		lun = lun >> 16;
12682f4701d8SJames.Smart@Emulex.Com 	}
12692f4701d8SJames.Smart@Emulex.Com }
12702f4701d8SJames.Smart@Emulex.Com EXPORT_SYMBOL(int_to_scsilun);
12712f4701d8SJames.Smart@Emulex.Com 
12722f4701d8SJames.Smart@Emulex.Com /**
12731da177e4SLinus Torvalds  * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1274eb44820cSRob Landley  * @starget: which target
1275eb44820cSRob Landley  * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN
1276eb44820cSRob Landley  * @rescan: nonzero if we can skip code only needed on first scan
12771da177e4SLinus Torvalds  *
12781da177e4SLinus Torvalds  * Description:
1279eb44820cSRob Landley  *   Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command.
1280eb44820cSRob Landley  *   Scan the resulting list of LUNs by calling scsi_probe_and_add_lun.
12811da177e4SLinus Torvalds  *
1282eb44820cSRob Landley  *   If BLINK_REPORTLUN2 is set, scan a target that supports more than 8
1283eb44820cSRob Landley  *   LUNs even if it's older than SCSI-3.
1284eb44820cSRob Landley  *   If BLIST_NOREPORTLUN is set, return 1 always.
1285eb44820cSRob Landley  *   If BLIST_NOLUN is set, return 0 always.
128609b6b51bSAlan Stern  *   If starget->no_report_luns is set, return 1 always.
12871da177e4SLinus Torvalds  *
12881da177e4SLinus Torvalds  * Return:
12891da177e4SLinus Torvalds  *     0: scan completed (or no memory, so further scanning is futile)
1290eb44820cSRob Landley  *     1: could not scan with REPORT LUN
12911da177e4SLinus Torvalds  **/
12926f3a2024SJames Bottomley static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
12931da177e4SLinus Torvalds 				int rescan)
12941da177e4SLinus Torvalds {
12951da177e4SLinus Torvalds 	char devname[64];
12961da177e4SLinus Torvalds 	unsigned char scsi_cmd[MAX_COMMAND_SIZE];
12971da177e4SLinus Torvalds 	unsigned int length;
12981da177e4SLinus Torvalds 	unsigned int lun;
12991da177e4SLinus Torvalds 	unsigned int num_luns;
13001da177e4SLinus Torvalds 	unsigned int retries;
130139216033SJames Bottomley 	int result;
13021da177e4SLinus Torvalds 	struct scsi_lun *lunp, *lun_data;
13031da177e4SLinus Torvalds 	u8 *data;
13041da177e4SLinus Torvalds 	struct scsi_sense_hdr sshdr;
13056f3a2024SJames Bottomley 	struct scsi_device *sdev;
13066f3a2024SJames Bottomley 	struct Scsi_Host *shost = dev_to_shost(&starget->dev);
13072ef89198SAlan Stern 	int ret = 0;
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 	/*
13101da177e4SLinus Torvalds 	 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
13111da177e4SLinus Torvalds 	 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
13121da177e4SLinus Torvalds 	 * support more than 8 LUNs.
131309b6b51bSAlan Stern 	 * Don't attempt if the target doesn't support REPORT LUNS.
13141da177e4SLinus Torvalds 	 */
13154d7db04aSJames Bottomley 	if (bflags & BLIST_NOREPORTLUN)
13164d7db04aSJames Bottomley 		return 1;
13174d7db04aSJames Bottomley 	if (starget->scsi_level < SCSI_2 &&
13184d7db04aSJames Bottomley 	    starget->scsi_level != SCSI_UNKNOWN)
13194d7db04aSJames Bottomley 		return 1;
13204d7db04aSJames Bottomley 	if (starget->scsi_level < SCSI_3 &&
13214d7db04aSJames Bottomley 	    (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
13221da177e4SLinus Torvalds 		return 1;
13231da177e4SLinus Torvalds 	if (bflags & BLIST_NOLUN)
13241da177e4SLinus Torvalds 		return 0;
132509b6b51bSAlan Stern 	if (starget->no_report_luns)
132609b6b51bSAlan Stern 		return 1;
13271da177e4SLinus Torvalds 
13286f3a2024SJames Bottomley 	if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
13296f3a2024SJames Bottomley 		sdev = scsi_alloc_sdev(starget, 0, NULL);
13306f3a2024SJames Bottomley 		if (!sdev)
13316f3a2024SJames Bottomley 			return 0;
133275f8ee8eSAlan Stern 		if (scsi_device_get(sdev)) {
133375f8ee8eSAlan Stern 			__scsi_remove_device(sdev);
13346f3a2024SJames Bottomley 			return 0;
13356f3a2024SJames Bottomley 		}
133675f8ee8eSAlan Stern 	}
13376f3a2024SJames Bottomley 
13381da177e4SLinus Torvalds 	sprintf(devname, "host %d channel %d id %d",
13396f3a2024SJames Bottomley 		shost->host_no, sdev->channel, sdev->id);
13401da177e4SLinus Torvalds 
13411da177e4SLinus Torvalds 	/*
13421da177e4SLinus Torvalds 	 * Allocate enough to hold the header (the same size as one scsi_lun)
13431da177e4SLinus Torvalds 	 * plus the max number of luns we are requesting.
13441da177e4SLinus Torvalds 	 *
13451da177e4SLinus Torvalds 	 * Reallocating and trying again (with the exact amount we need)
13461da177e4SLinus Torvalds 	 * would be nice, but then we need to somehow limit the size
13471da177e4SLinus Torvalds 	 * allocated based on the available memory and the limits of
13481da177e4SLinus Torvalds 	 * kmalloc - we don't want a kmalloc() failure of a huge value to
13491da177e4SLinus Torvalds 	 * prevent us from finding any LUNs on this target.
13501da177e4SLinus Torvalds 	 */
13511da177e4SLinus Torvalds 	length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
13521da177e4SLinus Torvalds 	lun_data = kmalloc(length, GFP_ATOMIC |
13531da177e4SLinus Torvalds 			   (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
13546f3a2024SJames Bottomley 	if (!lun_data) {
1355cadbd4a5SHarvey Harrison 		printk(ALLOC_FAILURE_MSG, __func__);
135639216033SJames Bottomley 		goto out;
13576f3a2024SJames Bottomley 	}
13581da177e4SLinus Torvalds 
13591da177e4SLinus Torvalds 	scsi_cmd[0] = REPORT_LUNS;
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	/*
13621da177e4SLinus Torvalds 	 * bytes 1 - 5: reserved, set to zero.
13631da177e4SLinus Torvalds 	 */
13641da177e4SLinus Torvalds 	memset(&scsi_cmd[1], 0, 5);
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds 	/*
13671da177e4SLinus Torvalds 	 * bytes 6 - 9: length of the command.
13681da177e4SLinus Torvalds 	 */
13691da177e4SLinus Torvalds 	scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
13701da177e4SLinus Torvalds 	scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
13711da177e4SLinus Torvalds 	scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
13721da177e4SLinus Torvalds 	scsi_cmd[9] = (unsigned char) length & 0xff;
13731da177e4SLinus Torvalds 
13741da177e4SLinus Torvalds 	scsi_cmd[10] = 0;	/* reserved */
13751da177e4SLinus Torvalds 	scsi_cmd[11] = 0;	/* control */
13761da177e4SLinus Torvalds 
13771da177e4SLinus Torvalds 	/*
13781da177e4SLinus Torvalds 	 * We can get a UNIT ATTENTION, for example a power on/reset, so
13791da177e4SLinus Torvalds 	 * retry a few times (like sd.c does for TEST UNIT READY).
13801da177e4SLinus Torvalds 	 * Experience shows some combinations of adapter/devices get at
13811da177e4SLinus Torvalds 	 * least two power on/resets.
13821da177e4SLinus Torvalds 	 *
13831da177e4SLinus Torvalds 	 * Illegal requests (for devices that do not support REPORT LUNS)
13841da177e4SLinus Torvalds 	 * should come through as a check condition, and will not generate
13851da177e4SLinus Torvalds 	 * a retry.
13861da177e4SLinus Torvalds 	 */
13871da177e4SLinus Torvalds 	for (retries = 0; retries < 3; retries++) {
13881da177e4SLinus Torvalds 		SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
13891da177e4SLinus Torvalds 				" REPORT LUNS to %s (try %d)\n", devname,
13901da177e4SLinus Torvalds 				retries));
139139216033SJames Bottomley 
139239216033SJames Bottomley 		result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1393ea73a9f2SJames Bottomley 					  lun_data, length, &sshdr,
1394f4f4e47eSFUJITA Tomonori 					  SCSI_TIMEOUT + 4 * HZ, 3, NULL);
139539216033SJames Bottomley 
13961da177e4SLinus Torvalds 		SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
139739216033SJames Bottomley 				" %s (try %d) result 0x%x\n", result
139839216033SJames Bottomley 				?  "failed" : "successful", retries, result));
139939216033SJames Bottomley 		if (result == 0)
14001da177e4SLinus Torvalds 			break;
1401ea73a9f2SJames Bottomley 		else if (scsi_sense_valid(&sshdr)) {
14021da177e4SLinus Torvalds 			if (sshdr.sense_key != UNIT_ATTENTION)
14031da177e4SLinus Torvalds 				break;
14041da177e4SLinus Torvalds 		}
14051da177e4SLinus Torvalds 	}
14061da177e4SLinus Torvalds 
140739216033SJames Bottomley 	if (result) {
14081da177e4SLinus Torvalds 		/*
14091da177e4SLinus Torvalds 		 * The device probably does not support a REPORT LUN command
14101da177e4SLinus Torvalds 		 */
14112ef89198SAlan Stern 		ret = 1;
14122ef89198SAlan Stern 		goto out_err;
14131da177e4SLinus Torvalds 	}
14141da177e4SLinus Torvalds 
14151da177e4SLinus Torvalds 	/*
14161da177e4SLinus Torvalds 	 * Get the length from the first four bytes of lun_data.
14171da177e4SLinus Torvalds 	 */
14181da177e4SLinus Torvalds 	data = (u8 *) lun_data->scsi_lun;
14191da177e4SLinus Torvalds 	length = ((data[0] << 24) | (data[1] << 16) |
14201da177e4SLinus Torvalds 		  (data[2] << 8) | (data[3] << 0));
14211da177e4SLinus Torvalds 
14221da177e4SLinus Torvalds 	num_luns = (length / sizeof(struct scsi_lun));
14231da177e4SLinus Torvalds 	if (num_luns > max_scsi_report_luns) {
14241da177e4SLinus Torvalds 		printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
14251da177e4SLinus Torvalds 		       " of %d luns reported, try increasing"
14261da177e4SLinus Torvalds 		       " max_scsi_report_luns.\n", devname,
14271da177e4SLinus Torvalds 		       max_scsi_report_luns, num_luns);
14281da177e4SLinus Torvalds 		num_luns = max_scsi_report_luns;
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 
14313bf743e7SJeff Garzik 	SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
14323bf743e7SJeff Garzik 		"scsi scan: REPORT LUN scan\n"));
14331da177e4SLinus Torvalds 
14341da177e4SLinus Torvalds 	/*
14351da177e4SLinus Torvalds 	 * Scan the luns in lun_data. The entry at offset 0 is really
14361da177e4SLinus Torvalds 	 * the header, so start at 1 and go up to and including num_luns.
14371da177e4SLinus Torvalds 	 */
14381da177e4SLinus Torvalds 	for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
14391da177e4SLinus Torvalds 		lun = scsilun_to_int(lunp);
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 		/*
14421da177e4SLinus Torvalds 		 * Check if the unused part of lunp is non-zero, and so
14431da177e4SLinus Torvalds 		 * does not fit in lun.
14441da177e4SLinus Torvalds 		 */
14451da177e4SLinus Torvalds 		if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
14461da177e4SLinus Torvalds 			int i;
14471da177e4SLinus Torvalds 
14481da177e4SLinus Torvalds 			/*
14491da177e4SLinus Torvalds 			 * Output an error displaying the LUN in byte order,
14501da177e4SLinus Torvalds 			 * this differs from what linux would print for the
14511da177e4SLinus Torvalds 			 * integer LUN value.
14521da177e4SLinus Torvalds 			 */
14531da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi: %s lun 0x", devname);
14541da177e4SLinus Torvalds 			data = (char *)lunp->scsi_lun;
14551da177e4SLinus Torvalds 			for (i = 0; i < sizeof(struct scsi_lun); i++)
14561da177e4SLinus Torvalds 				printk("%02x", data[i]);
14571da177e4SLinus Torvalds 			printk(" has a LUN larger than currently supported.\n");
14581da177e4SLinus Torvalds 		} else if (lun > sdev->host->max_lun) {
14591da177e4SLinus Torvalds 			printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
14601da177e4SLinus Torvalds 			       " than allowed by the host adapter\n",
14611da177e4SLinus Torvalds 			       devname, lun);
14621da177e4SLinus Torvalds 		} else {
14631da177e4SLinus Torvalds 			int res;
14641da177e4SLinus Torvalds 
14651da177e4SLinus Torvalds 			res = scsi_probe_and_add_lun(starget,
14661da177e4SLinus Torvalds 				lun, NULL, NULL, rescan, NULL);
14671da177e4SLinus Torvalds 			if (res == SCSI_SCAN_NO_RESPONSE) {
14681da177e4SLinus Torvalds 				/*
14691da177e4SLinus Torvalds 				 * Got some results, but now none, abort.
14701da177e4SLinus Torvalds 				 */
14713bf743e7SJeff Garzik 				sdev_printk(KERN_ERR, sdev,
14723bf743e7SJeff Garzik 					"Unexpected response"
14733bf743e7SJeff Garzik 				        " from lun %d while scanning, scan"
14743bf743e7SJeff Garzik 				        " aborted\n", lun);
14751da177e4SLinus Torvalds 				break;
14761da177e4SLinus Torvalds 			}
14771da177e4SLinus Torvalds 		}
14781da177e4SLinus Torvalds 	}
14791da177e4SLinus Torvalds 
14802ef89198SAlan Stern  out_err:
14811da177e4SLinus Torvalds 	kfree(lun_data);
14821da177e4SLinus Torvalds  out:
14836f3a2024SJames Bottomley 	scsi_device_put(sdev);
14840f1d87a2SJames Bottomley 	if (scsi_device_created(sdev))
14851da177e4SLinus Torvalds 		/*
14866f3a2024SJames Bottomley 		 * the sdev we used didn't appear in the report luns scan
14871da177e4SLinus Torvalds 		 */
1488860dc736SJames Bottomley 		__scsi_remove_device(sdev);
14892ef89198SAlan Stern 	return ret;
14901da177e4SLinus Torvalds }
14911da177e4SLinus Torvalds 
14921da177e4SLinus Torvalds struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
14931da177e4SLinus Torvalds 				      uint id, uint lun, void *hostdata)
14941da177e4SLinus Torvalds {
1495a97a83a0SMatthew Wilcox 	struct scsi_device *sdev = ERR_PTR(-ENODEV);
14961da177e4SLinus Torvalds 	struct device *parent = &shost->shost_gendev;
1497e02f3f59SChristoph Hellwig 	struct scsi_target *starget;
14981da177e4SLinus Torvalds 
1499938e2ac0SMatthew Wilcox 	if (strncmp(scsi_scan_type, "none", 4) == 0)
1500938e2ac0SMatthew Wilcox 		return ERR_PTR(-ENODEV);
1501938e2ac0SMatthew Wilcox 
1502e02f3f59SChristoph Hellwig 	starget = scsi_alloc_target(parent, channel, id);
15031da177e4SLinus Torvalds 	if (!starget)
15041da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
1505bc4f2401SAlan Stern 	scsi_autopm_get_target(starget);
15061da177e4SLinus Torvalds 
15070b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
15086b7f123fSMatthew Wilcox 	if (!shost->async_scan)
15096b7f123fSMatthew Wilcox 		scsi_complete_async_scans();
15106b7f123fSMatthew Wilcox 
1511bc4f2401SAlan Stern 	if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1512a97a83a0SMatthew Wilcox 		scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
1513bc4f2401SAlan Stern 		scsi_autopm_put_host(shost);
1514bc4f2401SAlan Stern 	}
15150b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
1516bc4f2401SAlan Stern 	scsi_autopm_put_target(starget);
15171da177e4SLinus Torvalds 	scsi_target_reap(starget);
15181da177e4SLinus Torvalds 	put_device(&starget->dev);
15191da177e4SLinus Torvalds 
15201da177e4SLinus Torvalds 	return sdev;
15211da177e4SLinus Torvalds }
15221da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_add_device);
15231da177e4SLinus Torvalds 
1524146f7262SJames Bottomley int scsi_add_device(struct Scsi_Host *host, uint channel,
1525146f7262SJames Bottomley 		    uint target, uint lun)
1526146f7262SJames Bottomley {
1527146f7262SJames Bottomley 	struct scsi_device *sdev =
1528146f7262SJames Bottomley 		__scsi_add_device(host, channel, target, lun, NULL);
1529146f7262SJames Bottomley 	if (IS_ERR(sdev))
1530146f7262SJames Bottomley 		return PTR_ERR(sdev);
1531146f7262SJames Bottomley 
1532146f7262SJames Bottomley 	scsi_device_put(sdev);
1533146f7262SJames Bottomley 	return 0;
1534146f7262SJames Bottomley }
1535146f7262SJames Bottomley EXPORT_SYMBOL(scsi_add_device);
1536146f7262SJames Bottomley 
15371da177e4SLinus Torvalds void scsi_rescan_device(struct device *dev)
15381da177e4SLinus Torvalds {
15391da177e4SLinus Torvalds 	struct scsi_driver *drv;
15401da177e4SLinus Torvalds 
15411da177e4SLinus Torvalds 	if (!dev->driver)
15421da177e4SLinus Torvalds 		return;
15431da177e4SLinus Torvalds 
15441da177e4SLinus Torvalds 	drv = to_scsi_driver(dev->driver);
15451da177e4SLinus Torvalds 	if (try_module_get(drv->owner)) {
15461da177e4SLinus Torvalds 		if (drv->rescan)
15471da177e4SLinus Torvalds 			drv->rescan(dev);
15481da177e4SLinus Torvalds 		module_put(drv->owner);
15491da177e4SLinus Torvalds 	}
15501da177e4SLinus Torvalds }
15511da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_rescan_device);
15521da177e4SLinus Torvalds 
1553e517d313SAlan Stern static void __scsi_scan_target(struct device *parent, unsigned int channel,
15541da177e4SLinus Torvalds 		unsigned int id, unsigned int lun, int rescan)
15551da177e4SLinus Torvalds {
15561da177e4SLinus Torvalds 	struct Scsi_Host *shost = dev_to_shost(parent);
15571da177e4SLinus Torvalds 	int bflags = 0;
15581da177e4SLinus Torvalds 	int res;
15591da177e4SLinus Torvalds 	struct scsi_target *starget;
15601da177e4SLinus Torvalds 
15611da177e4SLinus Torvalds 	if (shost->this_id == id)
15621da177e4SLinus Torvalds 		/*
15631da177e4SLinus Torvalds 		 * Don't scan the host adapter
15641da177e4SLinus Torvalds 		 */
15651da177e4SLinus Torvalds 		return;
15661da177e4SLinus Torvalds 
15671da177e4SLinus Torvalds 	starget = scsi_alloc_target(parent, channel, id);
15681da177e4SLinus Torvalds 	if (!starget)
15691da177e4SLinus Torvalds 		return;
1570bc4f2401SAlan Stern 	scsi_autopm_get_target(starget);
15711da177e4SLinus Torvalds 
15721da177e4SLinus Torvalds 	if (lun != SCAN_WILD_CARD) {
15731da177e4SLinus Torvalds 		/*
15741da177e4SLinus Torvalds 		 * Scan for a specific host/chan/id/lun.
15751da177e4SLinus Torvalds 		 */
15761da177e4SLinus Torvalds 		scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
15771da177e4SLinus Torvalds 		goto out_reap;
15781da177e4SLinus Torvalds 	}
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	/*
15811da177e4SLinus Torvalds 	 * Scan LUN 0, if there is some response, scan further. Ideally, we
15821da177e4SLinus Torvalds 	 * would not configure LUN 0 until all LUNs are scanned.
15831da177e4SLinus Torvalds 	 */
15846f3a2024SJames Bottomley 	res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
15856f3a2024SJames Bottomley 	if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
15866f3a2024SJames Bottomley 		if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
15871da177e4SLinus Torvalds 			/*
15881da177e4SLinus Torvalds 			 * The REPORT LUN did not scan the target,
15891da177e4SLinus Torvalds 			 * do a sequential scan.
15901da177e4SLinus Torvalds 			 */
15911da177e4SLinus Torvalds 			scsi_sequential_lun_scan(starget, bflags,
15924186ab19SKurt Garloff 						 starget->scsi_level, rescan);
15931da177e4SLinus Torvalds 	}
15941da177e4SLinus Torvalds 
15951da177e4SLinus Torvalds  out_reap:
1596bc4f2401SAlan Stern 	scsi_autopm_put_target(starget);
15971da177e4SLinus Torvalds 	/* now determine if the target has any children at all
15981da177e4SLinus Torvalds 	 * and if not, nuke it */
15991da177e4SLinus Torvalds 	scsi_target_reap(starget);
16001da177e4SLinus Torvalds 
16011da177e4SLinus Torvalds 	put_device(&starget->dev);
16021da177e4SLinus Torvalds }
1603e517d313SAlan Stern 
1604e517d313SAlan Stern /**
1605e59e4a09SRandy Dunlap  * scsi_scan_target - scan a target id, possibly including all LUNs on the target.
1606e517d313SAlan Stern  * @parent:	host to scan
1607e517d313SAlan Stern  * @channel:	channel to scan
1608e517d313SAlan Stern  * @id:		target id to scan
1609e517d313SAlan Stern  * @lun:	Specific LUN to scan or SCAN_WILD_CARD
1610e517d313SAlan Stern  * @rescan:	passed to LUN scanning routines
1611e517d313SAlan Stern  *
1612e517d313SAlan Stern  * Description:
1613e517d313SAlan Stern  *     Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1614e517d313SAlan Stern  *     and possibly all LUNs on the target id.
1615e517d313SAlan Stern  *
1616e517d313SAlan Stern  *     First try a REPORT LUN scan, if that does not scan the target, do a
1617e517d313SAlan Stern  *     sequential scan of LUNs on the target id.
1618e517d313SAlan Stern  **/
1619e517d313SAlan Stern void scsi_scan_target(struct device *parent, unsigned int channel,
1620e517d313SAlan Stern 		      unsigned int id, unsigned int lun, int rescan)
1621e517d313SAlan Stern {
1622e517d313SAlan Stern 	struct Scsi_Host *shost = dev_to_shost(parent);
1623e517d313SAlan Stern 
162493b45af5SMatthew Wilcox 	if (strncmp(scsi_scan_type, "none", 4) == 0)
162593b45af5SMatthew Wilcox 		return;
162693b45af5SMatthew Wilcox 
16276b7f123fSMatthew Wilcox 	mutex_lock(&shost->scan_mutex);
16283e082a91SMatthew Wilcox 	if (!shost->async_scan)
16293e082a91SMatthew Wilcox 		scsi_complete_async_scans();
16303e082a91SMatthew Wilcox 
1631bc4f2401SAlan Stern 	if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1632e517d313SAlan Stern 		__scsi_scan_target(parent, channel, id, lun, rescan);
1633bc4f2401SAlan Stern 		scsi_autopm_put_host(shost);
1634bc4f2401SAlan Stern 	}
16350b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
1636e517d313SAlan Stern }
16371da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_target);
16381da177e4SLinus Torvalds 
16391da177e4SLinus Torvalds static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
16401da177e4SLinus Torvalds 			      unsigned int id, unsigned int lun, int rescan)
16411da177e4SLinus Torvalds {
16421da177e4SLinus Torvalds 	uint order_id;
16431da177e4SLinus Torvalds 
16441da177e4SLinus Torvalds 	if (id == SCAN_WILD_CARD)
16451da177e4SLinus Torvalds 		for (id = 0; id < shost->max_id; ++id) {
16461da177e4SLinus Torvalds 			/*
16471da177e4SLinus Torvalds 			 * XXX adapter drivers when possible (FCP, iSCSI)
16481da177e4SLinus Torvalds 			 * could modify max_id to match the current max,
16491da177e4SLinus Torvalds 			 * not the absolute max.
16501da177e4SLinus Torvalds 			 *
16511da177e4SLinus Torvalds 			 * XXX add a shost id iterator, so for example,
16521da177e4SLinus Torvalds 			 * the FC ID can be the same as a target id
16531da177e4SLinus Torvalds 			 * without a huge overhead of sparse id's.
16541da177e4SLinus Torvalds 			 */
16551da177e4SLinus Torvalds 			if (shost->reverse_ordering)
16561da177e4SLinus Torvalds 				/*
16571da177e4SLinus Torvalds 				 * Scan from high to low id.
16581da177e4SLinus Torvalds 				 */
16591da177e4SLinus Torvalds 				order_id = shost->max_id - id - 1;
16601da177e4SLinus Torvalds 			else
16611da177e4SLinus Torvalds 				order_id = id;
1662e517d313SAlan Stern 			__scsi_scan_target(&shost->shost_gendev, channel,
1663e517d313SAlan Stern 					order_id, lun, rescan);
16641da177e4SLinus Torvalds 		}
16651da177e4SLinus Torvalds 	else
1666e517d313SAlan Stern 		__scsi_scan_target(&shost->shost_gendev, channel,
1667e517d313SAlan Stern 				id, lun, rescan);
16681da177e4SLinus Torvalds }
16691da177e4SLinus Torvalds 
16701da177e4SLinus Torvalds int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
16711da177e4SLinus Torvalds 			    unsigned int id, unsigned int lun, int rescan)
16721da177e4SLinus Torvalds {
16733bf743e7SJeff Garzik 	SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
16743bf743e7SJeff Garzik 		"%s: <%u:%u:%u>\n",
1675cadbd4a5SHarvey Harrison 		__func__, channel, id, lun));
16761da177e4SLinus Torvalds 
16771da177e4SLinus Torvalds 	if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1678091686d3SAmit Arora 	    ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
16791da177e4SLinus Torvalds 	    ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
16801da177e4SLinus Torvalds 		return -EINVAL;
16811da177e4SLinus Torvalds 
16820b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
16836b7f123fSMatthew Wilcox 	if (!shost->async_scan)
16846b7f123fSMatthew Wilcox 		scsi_complete_async_scans();
16856b7f123fSMatthew Wilcox 
1686bc4f2401SAlan Stern 	if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
16871da177e4SLinus Torvalds 		if (channel == SCAN_WILD_CARD)
168882f29467SMike Anderson 			for (channel = 0; channel <= shost->max_channel;
168982f29467SMike Anderson 			     channel++)
169082f29467SMike Anderson 				scsi_scan_channel(shost, channel, id, lun,
169182f29467SMike Anderson 						  rescan);
16921da177e4SLinus Torvalds 		else
16931da177e4SLinus Torvalds 			scsi_scan_channel(shost, channel, id, lun, rescan);
1694bc4f2401SAlan Stern 		scsi_autopm_put_host(shost);
169582f29467SMike Anderson 	}
16960b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
16971da177e4SLinus Torvalds 
16981da177e4SLinus Torvalds 	return 0;
16991da177e4SLinus Torvalds }
17001da177e4SLinus Torvalds 
17013e082a91SMatthew Wilcox static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
17023e082a91SMatthew Wilcox {
17033e082a91SMatthew Wilcox 	struct scsi_device *sdev;
17043e082a91SMatthew Wilcox 	shost_for_each_device(sdev, shost) {
17053b661a92SDan Williams 		/* target removed before the device could be added */
17063b661a92SDan Williams 		if (sdev->sdev_state == SDEV_DEL)
17073b661a92SDan Williams 			continue;
17086b7f123fSMatthew Wilcox 		if (!scsi_host_scan_allowed(shost) ||
17096b7f123fSMatthew Wilcox 		    scsi_sysfs_add_sdev(sdev) != 0)
1710860dc736SJames Bottomley 			__scsi_remove_device(sdev);
17113e082a91SMatthew Wilcox 	}
17123e082a91SMatthew Wilcox }
17133e082a91SMatthew Wilcox 
17143e082a91SMatthew Wilcox /**
17153e082a91SMatthew Wilcox  * scsi_prep_async_scan - prepare for an async scan
17163e082a91SMatthew Wilcox  * @shost: the host which will be scanned
17173e082a91SMatthew Wilcox  * Returns: a cookie to be passed to scsi_finish_async_scan()
17183e082a91SMatthew Wilcox  *
17193e082a91SMatthew Wilcox  * Tells the midlayer this host is going to do an asynchronous scan.
17203e082a91SMatthew Wilcox  * It reserves the host's position in the scanning list and ensures
17213e082a91SMatthew Wilcox  * that other asynchronous scans started after this one won't affect the
17223e082a91SMatthew Wilcox  * ordering of the discovered devices.
17233e082a91SMatthew Wilcox  */
17241aa8fab2SMatthew Wilcox static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
17253e082a91SMatthew Wilcox {
17263e082a91SMatthew Wilcox 	struct async_scan_data *data;
17276b7f123fSMatthew Wilcox 	unsigned long flags;
17283e082a91SMatthew Wilcox 
17293e082a91SMatthew Wilcox 	if (strncmp(scsi_scan_type, "sync", 4) == 0)
17303e082a91SMatthew Wilcox 		return NULL;
17313e082a91SMatthew Wilcox 
17323e082a91SMatthew Wilcox 	if (shost->async_scan) {
1733cadbd4a5SHarvey Harrison 		printk("%s called twice for host %d", __func__,
17343e082a91SMatthew Wilcox 				shost->host_no);
17353e082a91SMatthew Wilcox 		dump_stack();
17363e082a91SMatthew Wilcox 		return NULL;
17373e082a91SMatthew Wilcox 	}
17383e082a91SMatthew Wilcox 
17393e082a91SMatthew Wilcox 	data = kmalloc(sizeof(*data), GFP_KERNEL);
17403e082a91SMatthew Wilcox 	if (!data)
17413e082a91SMatthew Wilcox 		goto err;
17423e082a91SMatthew Wilcox 	data->shost = scsi_host_get(shost);
17433e082a91SMatthew Wilcox 	if (!data->shost)
17443e082a91SMatthew Wilcox 		goto err;
17453e082a91SMatthew Wilcox 	init_completion(&data->prev_finished);
17463e082a91SMatthew Wilcox 
17476b7f123fSMatthew Wilcox 	mutex_lock(&shost->scan_mutex);
17486b7f123fSMatthew Wilcox 	spin_lock_irqsave(shost->host_lock, flags);
17493e082a91SMatthew Wilcox 	shost->async_scan = 1;
17506b7f123fSMatthew Wilcox 	spin_unlock_irqrestore(shost->host_lock, flags);
17516b7f123fSMatthew Wilcox 	mutex_unlock(&shost->scan_mutex);
17526b7f123fSMatthew Wilcox 
17536b7f123fSMatthew Wilcox 	spin_lock(&async_scan_lock);
17543e082a91SMatthew Wilcox 	if (list_empty(&scanning_hosts))
17553e082a91SMatthew Wilcox 		complete(&data->prev_finished);
17563e082a91SMatthew Wilcox 	list_add_tail(&data->list, &scanning_hosts);
17573e082a91SMatthew Wilcox 	spin_unlock(&async_scan_lock);
17583e082a91SMatthew Wilcox 
17593e082a91SMatthew Wilcox 	return data;
17603e082a91SMatthew Wilcox 
17613e082a91SMatthew Wilcox  err:
17623e082a91SMatthew Wilcox 	kfree(data);
17633e082a91SMatthew Wilcox 	return NULL;
17643e082a91SMatthew Wilcox }
17653e082a91SMatthew Wilcox 
17663e082a91SMatthew Wilcox /**
17673e082a91SMatthew Wilcox  * scsi_finish_async_scan - asynchronous scan has finished
17683e082a91SMatthew Wilcox  * @data: cookie returned from earlier call to scsi_prep_async_scan()
17693e082a91SMatthew Wilcox  *
17703e082a91SMatthew Wilcox  * All the devices currently attached to this host have been found.
17713e082a91SMatthew Wilcox  * This function announces all the devices it has found to the rest
17723e082a91SMatthew Wilcox  * of the system.
17733e082a91SMatthew Wilcox  */
17741aa8fab2SMatthew Wilcox static void scsi_finish_async_scan(struct async_scan_data *data)
17753e082a91SMatthew Wilcox {
17763e082a91SMatthew Wilcox 	struct Scsi_Host *shost;
17776b7f123fSMatthew Wilcox 	unsigned long flags;
17783e082a91SMatthew Wilcox 
17793e082a91SMatthew Wilcox 	if (!data)
17803e082a91SMatthew Wilcox 		return;
17813e082a91SMatthew Wilcox 
17823e082a91SMatthew Wilcox 	shost = data->shost;
17836b7f123fSMatthew Wilcox 
17846b7f123fSMatthew Wilcox 	mutex_lock(&shost->scan_mutex);
17856b7f123fSMatthew Wilcox 
17863e082a91SMatthew Wilcox 	if (!shost->async_scan) {
1787cadbd4a5SHarvey Harrison 		printk("%s called twice for host %d", __func__,
17883e082a91SMatthew Wilcox 				shost->host_no);
17893e082a91SMatthew Wilcox 		dump_stack();
1790773e82f6SJulia Lawall 		mutex_unlock(&shost->scan_mutex);
17913e082a91SMatthew Wilcox 		return;
17923e082a91SMatthew Wilcox 	}
17933e082a91SMatthew Wilcox 
17943e082a91SMatthew Wilcox 	wait_for_completion(&data->prev_finished);
17953e082a91SMatthew Wilcox 
17963e082a91SMatthew Wilcox 	scsi_sysfs_add_devices(shost);
17973e082a91SMatthew Wilcox 
17986b7f123fSMatthew Wilcox 	spin_lock_irqsave(shost->host_lock, flags);
17993e082a91SMatthew Wilcox 	shost->async_scan = 0;
18006b7f123fSMatthew Wilcox 	spin_unlock_irqrestore(shost->host_lock, flags);
18016b7f123fSMatthew Wilcox 
18026b7f123fSMatthew Wilcox 	mutex_unlock(&shost->scan_mutex);
18036b7f123fSMatthew Wilcox 
18046b7f123fSMatthew Wilcox 	spin_lock(&async_scan_lock);
18053e082a91SMatthew Wilcox 	list_del(&data->list);
18063e082a91SMatthew Wilcox 	if (!list_empty(&scanning_hosts)) {
18073e082a91SMatthew Wilcox 		struct async_scan_data *next = list_entry(scanning_hosts.next,
18083e082a91SMatthew Wilcox 				struct async_scan_data, list);
18093e082a91SMatthew Wilcox 		complete(&next->prev_finished);
18103e082a91SMatthew Wilcox 	}
18113e082a91SMatthew Wilcox 	spin_unlock(&async_scan_lock);
18123e082a91SMatthew Wilcox 
1813267a6ad4SHuajun Li 	scsi_autopm_put_host(shost);
18143e082a91SMatthew Wilcox 	scsi_host_put(shost);
18153e082a91SMatthew Wilcox 	kfree(data);
18163e082a91SMatthew Wilcox }
18173e082a91SMatthew Wilcox 
18181aa8fab2SMatthew Wilcox static void do_scsi_scan_host(struct Scsi_Host *shost)
18191aa8fab2SMatthew Wilcox {
18201aa8fab2SMatthew Wilcox 	if (shost->hostt->scan_finished) {
18211aa8fab2SMatthew Wilcox 		unsigned long start = jiffies;
18221aa8fab2SMatthew Wilcox 		if (shost->hostt->scan_start)
18231aa8fab2SMatthew Wilcox 			shost->hostt->scan_start(shost);
18241aa8fab2SMatthew Wilcox 
18251aa8fab2SMatthew Wilcox 		while (!shost->hostt->scan_finished(shost, jiffies - start))
18261aa8fab2SMatthew Wilcox 			msleep(10);
18271aa8fab2SMatthew Wilcox 	} else {
18281aa8fab2SMatthew Wilcox 		scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
18291aa8fab2SMatthew Wilcox 				SCAN_WILD_CARD, 0);
18301aa8fab2SMatthew Wilcox 	}
18311aa8fab2SMatthew Wilcox }
18321aa8fab2SMatthew Wilcox 
18336cdd5520SDan Williams static void do_scan_async(void *_data, async_cookie_t c)
18343e082a91SMatthew Wilcox {
18353e082a91SMatthew Wilcox 	struct async_scan_data *data = _data;
1836bc4f2401SAlan Stern 	struct Scsi_Host *shost = data->shost;
1837bc4f2401SAlan Stern 
1838bc4f2401SAlan Stern 	do_scsi_scan_host(shost);
18393e082a91SMatthew Wilcox 	scsi_finish_async_scan(data);
18403e082a91SMatthew Wilcox }
18413e082a91SMatthew Wilcox 
18421da177e4SLinus Torvalds /**
18431da177e4SLinus Torvalds  * scsi_scan_host - scan the given adapter
18441da177e4SLinus Torvalds  * @shost:	adapter to scan
18451da177e4SLinus Torvalds  **/
18461da177e4SLinus Torvalds void scsi_scan_host(struct Scsi_Host *shost)
18471da177e4SLinus Torvalds {
18483e082a91SMatthew Wilcox 	struct async_scan_data *data;
18493e082a91SMatthew Wilcox 
18503e082a91SMatthew Wilcox 	if (strncmp(scsi_scan_type, "none", 4) == 0)
18513e082a91SMatthew Wilcox 		return;
1852bc4f2401SAlan Stern 	if (scsi_autopm_get_host(shost) < 0)
1853bc4f2401SAlan Stern 		return;
18543e082a91SMatthew Wilcox 
18553e082a91SMatthew Wilcox 	data = scsi_prep_async_scan(shost);
18563e082a91SMatthew Wilcox 	if (!data) {
18571aa8fab2SMatthew Wilcox 		do_scsi_scan_host(shost);
1858bc4f2401SAlan Stern 		scsi_autopm_put_host(shost);
18593e082a91SMatthew Wilcox 		return;
18603e082a91SMatthew Wilcox 	}
18611aa8fab2SMatthew Wilcox 
18626cdd5520SDan Williams 	/* register with the async subsystem so wait_for_device_probe()
18636cdd5520SDan Williams 	 * will flush this work
18646cdd5520SDan Williams 	 */
18656cdd5520SDan Williams 	async_schedule(do_scan_async, data);
18666cdd5520SDan Williams 
1867267a6ad4SHuajun Li 	/* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */
18681da177e4SLinus Torvalds }
18691da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_host);
18701da177e4SLinus Torvalds 
18711da177e4SLinus Torvalds void scsi_forget_host(struct Scsi_Host *shost)
18721da177e4SLinus Torvalds {
1873a64358dbSAlan Stern 	struct scsi_device *sdev;
18741da177e4SLinus Torvalds 	unsigned long flags;
18751da177e4SLinus Torvalds 
1876a64358dbSAlan Stern  restart:
18771da177e4SLinus Torvalds 	spin_lock_irqsave(shost->host_lock, flags);
1878a64358dbSAlan Stern 	list_for_each_entry(sdev, &shost->__devices, siblings) {
1879a64358dbSAlan Stern 		if (sdev->sdev_state == SDEV_DEL)
1880a64358dbSAlan Stern 			continue;
18811da177e4SLinus Torvalds 		spin_unlock_irqrestore(shost->host_lock, flags);
1882a64358dbSAlan Stern 		__scsi_remove_device(sdev);
1883a64358dbSAlan Stern 		goto restart;
18841da177e4SLinus Torvalds 	}
18851da177e4SLinus Torvalds 	spin_unlock_irqrestore(shost->host_lock, flags);
18861da177e4SLinus Torvalds }
18871da177e4SLinus Torvalds 
18889f6aa575SRandy Dunlap /**
18899f6aa575SRandy Dunlap  * scsi_get_host_dev - Create a scsi_device that points to the host adapter itself
18909f6aa575SRandy Dunlap  * @shost: Host that needs a scsi_device
18911da177e4SLinus Torvalds  *
18921da177e4SLinus Torvalds  * Lock status: None assumed.
18931da177e4SLinus Torvalds  *
1894f64a181dSChristoph Hellwig  * Returns:     The scsi_device or NULL
18951da177e4SLinus Torvalds  *
18961da177e4SLinus Torvalds  * Notes:
1897f64a181dSChristoph Hellwig  *	Attach a single scsi_device to the Scsi_Host - this should
18981da177e4SLinus Torvalds  *	be made to look like a "pseudo-device" that points to the
18991da177e4SLinus Torvalds  *	HA itself.
19001da177e4SLinus Torvalds  *
19011da177e4SLinus Torvalds  *	Note - this device is not accessible from any high-level
19021da177e4SLinus Torvalds  *	drivers (including generics), which is probably not
19039f6aa575SRandy Dunlap  *	optimal.  We can add hooks later to attach.
19041da177e4SLinus Torvalds  */
19051da177e4SLinus Torvalds struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
19061da177e4SLinus Torvalds {
1907e517d313SAlan Stern 	struct scsi_device *sdev = NULL;
19081da177e4SLinus Torvalds 	struct scsi_target *starget;
19091da177e4SLinus Torvalds 
19100b950672SArjan van de Ven 	mutex_lock(&shost->scan_mutex);
1911e517d313SAlan Stern 	if (!scsi_host_scan_allowed(shost))
1912e517d313SAlan Stern 		goto out;
19131da177e4SLinus Torvalds 	starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
19141da177e4SLinus Torvalds 	if (!starget)
1915e517d313SAlan Stern 		goto out;
19161da177e4SLinus Torvalds 
19171da177e4SLinus Torvalds 	sdev = scsi_alloc_sdev(starget, 0, NULL);
1918d5469119SAlan Stern 	if (sdev)
19191da177e4SLinus Torvalds 		sdev->borken = 0;
1920d5469119SAlan Stern 	else
1921884d25ccSJames Bottomley 		scsi_target_reap(starget);
19221da177e4SLinus Torvalds 	put_device(&starget->dev);
1923e517d313SAlan Stern  out:
19240b950672SArjan van de Ven 	mutex_unlock(&shost->scan_mutex);
19251da177e4SLinus Torvalds 	return sdev;
19261da177e4SLinus Torvalds }
19271da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_host_dev);
19281da177e4SLinus Torvalds 
19299f6aa575SRandy Dunlap /**
19309f6aa575SRandy Dunlap  * scsi_free_host_dev - Free a scsi_device that points to the host adapter itself
19319f6aa575SRandy Dunlap  * @sdev: Host device to be freed
19321da177e4SLinus Torvalds  *
19331da177e4SLinus Torvalds  * Lock status: None assumed.
19341da177e4SLinus Torvalds  *
19351da177e4SLinus Torvalds  * Returns:     Nothing
19361da177e4SLinus Torvalds  */
19371da177e4SLinus Torvalds void scsi_free_host_dev(struct scsi_device *sdev)
19381da177e4SLinus Torvalds {
19391da177e4SLinus Torvalds 	BUG_ON(sdev->id != sdev->host->this_id);
19401da177e4SLinus Torvalds 
1941860dc736SJames Bottomley 	__scsi_remove_device(sdev);
19421da177e4SLinus Torvalds }
19431da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_free_host_dev);
19441da177e4SLinus Torvalds 
1945