1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * scsi_scan.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * Copyright (C) 2000 Eric Youngdale,
61da177e4SLinus Torvalds * Copyright (C) 2002 Patrick Mansfield
71da177e4SLinus Torvalds *
81da177e4SLinus Torvalds * The general scanning/probing algorithm is as follows, exceptions are
91da177e4SLinus Torvalds * made to it depending on device specific flags, compilation options, and
101da177e4SLinus Torvalds * global variable (boot or module load time) settings.
111da177e4SLinus Torvalds *
121da177e4SLinus Torvalds * A specific LUN is scanned via an INQUIRY command; if the LUN has a
13f64a181dSChristoph Hellwig * device attached, a scsi_device is allocated and setup for it.
141da177e4SLinus Torvalds *
151da177e4SLinus Torvalds * For every id of every channel on the given host:
161da177e4SLinus Torvalds *
171da177e4SLinus Torvalds * Scan LUN 0; if the target responds to LUN 0 (even if there is no
181da177e4SLinus Torvalds * device or storage attached to LUN 0):
191da177e4SLinus Torvalds *
201da177e4SLinus Torvalds * If LUN 0 has a device attached, allocate and setup a
21f64a181dSChristoph Hellwig * scsi_device for it.
221da177e4SLinus Torvalds *
231da177e4SLinus Torvalds * If target is SCSI-3 or up, issue a REPORT LUN, and scan
241da177e4SLinus Torvalds * all of the LUNs returned by the REPORT LUN; else,
251da177e4SLinus Torvalds * sequentially scan LUNs up until some maximum is reached,
261da177e4SLinus Torvalds * or a LUN is seen that cannot have a device attached to it.
271da177e4SLinus Torvalds */
281da177e4SLinus Torvalds
291da177e4SLinus Torvalds #include <linux/module.h>
301da177e4SLinus Torvalds #include <linux/moduleparam.h>
311da177e4SLinus Torvalds #include <linux/init.h>
321da177e4SLinus Torvalds #include <linux/blkdev.h>
333e082a91SMatthew Wilcox #include <linux/delay.h>
343e082a91SMatthew Wilcox #include <linux/kthread.h>
353e082a91SMatthew Wilcox #include <linux/spinlock.h>
364ace92fcSArjan van de Ven #include <linux/async.h>
375a0e3ad6STejun Heo #include <linux/slab.h>
382a904e5dSRob Evers #include <asm/unaligned.h>
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds #include <scsi/scsi.h>
41beb40487SChristoph Hellwig #include <scsi/scsi_cmnd.h>
421da177e4SLinus Torvalds #include <scsi/scsi_device.h>
431da177e4SLinus Torvalds #include <scsi/scsi_driver.h>
441da177e4SLinus Torvalds #include <scsi/scsi_devinfo.h>
451da177e4SLinus Torvalds #include <scsi/scsi_host.h>
461da177e4SLinus Torvalds #include <scsi/scsi_transport.h>
47d3d32891SHannes Reinecke #include <scsi/scsi_dh.h>
481da177e4SLinus Torvalds #include <scsi/scsi_eh.h>
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds #include "scsi_priv.h"
511da177e4SLinus Torvalds #include "scsi_logging.h"
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
541da177e4SLinus Torvalds " SCSI scanning, some SCSI devices might not be configured\n"
551da177e4SLinus Torvalds
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds * Default timeout
581da177e4SLinus Torvalds */
591da177e4SLinus Torvalds #define SCSI_TIMEOUT (2*HZ)
60b39c9a66SBrian King #define SCSI_REPORT_LUNS_TIMEOUT (30*HZ)
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds /*
63405ae7d3SRobert P. J. Day * Prefix values for the SCSI id's (stored in sysfs name field)
641da177e4SLinus Torvalds */
651da177e4SLinus Torvalds #define SCSI_UID_SER_NUM 'S'
661da177e4SLinus Torvalds #define SCSI_UID_UNKNOWN 'Z'
671da177e4SLinus Torvalds
681da177e4SLinus Torvalds /*
691da177e4SLinus Torvalds * Return values of some of the scanning functions.
701da177e4SLinus Torvalds *
711da177e4SLinus Torvalds * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
721da177e4SLinus Torvalds * includes allocation or general failures preventing IO from being sent.
731da177e4SLinus Torvalds *
741da177e4SLinus Torvalds * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
751da177e4SLinus Torvalds * on the given LUN.
761da177e4SLinus Torvalds *
771da177e4SLinus Torvalds * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
781da177e4SLinus Torvalds * given LUN.
791da177e4SLinus Torvalds */
801da177e4SLinus Torvalds #define SCSI_SCAN_NO_RESPONSE 0
811da177e4SLinus Torvalds #define SCSI_SCAN_TARGET_PRESENT 1
821da177e4SLinus Torvalds #define SCSI_SCAN_LUN_PRESENT 2
831da177e4SLinus Torvalds
840ad78200SArjan van de Ven static const char *scsi_null_device_strs = "nullnullnullnull";
851da177e4SLinus Torvalds
861da177e4SLinus Torvalds #define MAX_SCSI_LUNS 512
871da177e4SLinus Torvalds
881abf635dSHannes Reinecke static u64 max_scsi_luns = MAX_SCSI_LUNS;
891da177e4SLinus Torvalds
901abf635dSHannes Reinecke module_param_named(max_luns, max_scsi_luns, ullong, S_IRUGO|S_IWUSR);
911da177e4SLinus Torvalds MODULE_PARM_DESC(max_luns,
921abf635dSHannes Reinecke "last scsi LUN (should be between 1 and 2^64-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
1007cc5aad6SBart Van Assche static char scsi_scan_type[7] = SCSI_SCAN_TYPE_DEFAULT;
1013e082a91SMatthew Wilcox
1021d645088SHannes Reinecke module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type),
1031d645088SHannes Reinecke S_IRUGO|S_IWUSR);
1041d645088SHannes Reinecke MODULE_PARM_DESC(scan, "sync, async, manual, or none. "
1051d645088SHannes Reinecke "Setting to 'manual' disables automatic scanning, but allows "
1061d645088SHannes Reinecke "for manual device scan via the 'scan' sysfs attribute.");
1073e082a91SMatthew Wilcox
10814faf12fSAlan Stern static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ + 18;
1091da177e4SLinus Torvalds
11010f4b89aSMasatake YAMATO module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR);
1111da177e4SLinus Torvalds MODULE_PARM_DESC(inq_timeout,
1121da177e4SLinus Torvalds "Timeout (in seconds) waiting for devices to answer INQUIRY."
11314faf12fSAlan Stern " Default is 20. Some devices may need more; most need less.");
1141da177e4SLinus Torvalds
1156b7f123fSMatthew Wilcox /* This lock protects only this list */
1163e082a91SMatthew Wilcox static DEFINE_SPINLOCK(async_scan_lock);
1173e082a91SMatthew Wilcox static LIST_HEAD(scanning_hosts);
1183e082a91SMatthew Wilcox
1193e082a91SMatthew Wilcox struct async_scan_data {
1203e082a91SMatthew Wilcox struct list_head list;
1213e082a91SMatthew Wilcox struct Scsi_Host *shost;
1223e082a91SMatthew Wilcox struct completion prev_finished;
1233e082a91SMatthew Wilcox };
1243e082a91SMatthew Wilcox
125776141ddSBart Van Assche /*
126a19a93e4SBart Van Assche * scsi_enable_async_suspend - Enable async suspend and resume
127a19a93e4SBart Van Assche */
scsi_enable_async_suspend(struct device * dev)128a19a93e4SBart Van Assche void scsi_enable_async_suspend(struct device *dev)
129a19a93e4SBart Van Assche {
130a19a93e4SBart Van Assche /*
131a19a93e4SBart Van Assche * If a user has disabled async probing a likely reason is due to a
132a19a93e4SBart Van Assche * storage enclosure that does not inject staggered spin-ups. For
133a19a93e4SBart Van Assche * safety, make resume synchronous as well in that case.
134a19a93e4SBart Van Assche */
135a19a93e4SBart Van Assche if (strncmp(scsi_scan_type, "async", 5) != 0)
136a19a93e4SBart Van Assche return;
137a19a93e4SBart Van Assche /* Enable asynchronous suspend and resume. */
138a19a93e4SBart Van Assche device_enable_async_suspend(dev);
139a19a93e4SBart Van Assche }
140a19a93e4SBart Van Assche
141a19a93e4SBart Van Assche /**
1423e082a91SMatthew Wilcox * scsi_complete_async_scans - Wait for asynchronous scans to complete
1433e082a91SMatthew Wilcox *
1448bcc2412SMatthew Wilcox * When this function returns, any host which started scanning before
1458bcc2412SMatthew Wilcox * this function was called will have finished its scan. Hosts which
1468bcc2412SMatthew Wilcox * started scanning after this function was called may or may not have
1478bcc2412SMatthew Wilcox * finished.
1483e082a91SMatthew Wilcox */
scsi_complete_async_scans(void)1493e082a91SMatthew Wilcox int scsi_complete_async_scans(void)
1503e082a91SMatthew Wilcox {
1513e082a91SMatthew Wilcox struct async_scan_data *data;
1523e082a91SMatthew Wilcox
1533e082a91SMatthew Wilcox do {
1543e082a91SMatthew Wilcox if (list_empty(&scanning_hosts))
155e96eb23dSDan Williams return 0;
1563e082a91SMatthew Wilcox /* If we can't get memory immediately, that's OK. Just
1573e082a91SMatthew Wilcox * sleep a little. Even if we never get memory, the async
1583e082a91SMatthew Wilcox * scans will finish eventually.
1593e082a91SMatthew Wilcox */
1603e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL);
1613e082a91SMatthew Wilcox if (!data)
1623e082a91SMatthew Wilcox msleep(1);
1633e082a91SMatthew Wilcox } while (!data);
1643e082a91SMatthew Wilcox
1653e082a91SMatthew Wilcox data->shost = NULL;
1663e082a91SMatthew Wilcox init_completion(&data->prev_finished);
1673e082a91SMatthew Wilcox
1683e082a91SMatthew Wilcox spin_lock(&async_scan_lock);
1693e082a91SMatthew Wilcox /* Check that there's still somebody else on the list */
1703e082a91SMatthew Wilcox if (list_empty(&scanning_hosts))
1713e082a91SMatthew Wilcox goto done;
1723e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts);
1733e082a91SMatthew Wilcox spin_unlock(&async_scan_lock);
1743e082a91SMatthew Wilcox
1753e082a91SMatthew Wilcox printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
1763e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished);
1773e082a91SMatthew Wilcox
1783e082a91SMatthew Wilcox spin_lock(&async_scan_lock);
1793e082a91SMatthew Wilcox list_del(&data->list);
1808bcc2412SMatthew Wilcox if (!list_empty(&scanning_hosts)) {
1818bcc2412SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next,
1828bcc2412SMatthew Wilcox struct async_scan_data, list);
1838bcc2412SMatthew Wilcox complete(&next->prev_finished);
1848bcc2412SMatthew Wilcox }
1853e082a91SMatthew Wilcox done:
1863e082a91SMatthew Wilcox spin_unlock(&async_scan_lock);
187e96eb23dSDan Williams
1883e082a91SMatthew Wilcox kfree(data);
1893e082a91SMatthew Wilcox return 0;
1903e082a91SMatthew Wilcox }
1913e082a91SMatthew Wilcox
1921da177e4SLinus Torvalds /**
1931da177e4SLinus Torvalds * scsi_unlock_floptical - unlock device via a special MODE SENSE command
19439216033SJames Bottomley * @sdev: scsi device to send command to
1951da177e4SLinus Torvalds * @result: area to store the result of the MODE SENSE
1961da177e4SLinus Torvalds *
1971da177e4SLinus Torvalds * Description:
19839216033SJames Bottomley * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
1991da177e4SLinus Torvalds * Called for BLIST_KEY devices.
2001da177e4SLinus Torvalds **/
scsi_unlock_floptical(struct scsi_device * sdev,unsigned char * result)20139216033SJames Bottomley static void scsi_unlock_floptical(struct scsi_device *sdev,
2021da177e4SLinus Torvalds unsigned char *result)
2031da177e4SLinus Torvalds {
2041da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE];
2051da177e4SLinus Torvalds
20691921e01SHannes Reinecke sdev_printk(KERN_NOTICE, sdev, "unlocking floptical drive\n");
2071da177e4SLinus Torvalds scsi_cmd[0] = MODE_SENSE;
2081da177e4SLinus Torvalds scsi_cmd[1] = 0;
2091da177e4SLinus Torvalds scsi_cmd[2] = 0x2e;
2101da177e4SLinus Torvalds scsi_cmd[3] = 0;
2111da177e4SLinus Torvalds scsi_cmd[4] = 0x2a; /* size */
2121da177e4SLinus Torvalds scsi_cmd[5] = 0;
2137dfe0b5eSMike Christie scsi_execute_cmd(sdev, scsi_cmd, REQ_OP_DRV_IN, result, 0x2a,
214f4f4e47eSFUJITA Tomonori SCSI_TIMEOUT, 3, NULL);
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds
scsi_realloc_sdev_budget_map(struct scsi_device * sdev,unsigned int depth)217edb854a3SMing Lei static int scsi_realloc_sdev_budget_map(struct scsi_device *sdev,
218edb854a3SMing Lei unsigned int depth)
219edb854a3SMing Lei {
220edb854a3SMing Lei int new_shift = sbitmap_calculate_shift(depth);
221edb854a3SMing Lei bool need_alloc = !sdev->budget_map.map;
222edb854a3SMing Lei bool need_free = false;
223edb854a3SMing Lei int ret;
224edb854a3SMing Lei struct sbitmap sb_backup;
225edb854a3SMing Lei
226eaba83b5SJohn Garry depth = min_t(unsigned int, depth, scsi_device_max_queue_depth(sdev));
227eaba83b5SJohn Garry
228edb854a3SMing Lei /*
229edb854a3SMing Lei * realloc if new shift is calculated, which is caused by setting
230b7eefcf1SChristoph Hellwig * up one new default queue depth after calling ->device_configure
231edb854a3SMing Lei */
232edb854a3SMing Lei if (!need_alloc && new_shift != sdev->budget_map.shift)
233edb854a3SMing Lei need_alloc = need_free = true;
234edb854a3SMing Lei
235edb854a3SMing Lei if (!need_alloc)
236edb854a3SMing Lei return 0;
237edb854a3SMing Lei
238edb854a3SMing Lei /*
239edb854a3SMing Lei * Request queue has to be frozen for reallocating budget map,
240edb854a3SMing Lei * and here disk isn't added yet, so freezing is pretty fast
241edb854a3SMing Lei */
242edb854a3SMing Lei if (need_free) {
243edb854a3SMing Lei blk_mq_freeze_queue(sdev->request_queue);
244edb854a3SMing Lei sb_backup = sdev->budget_map;
245edb854a3SMing Lei }
246edb854a3SMing Lei ret = sbitmap_init_node(&sdev->budget_map,
247edb854a3SMing Lei scsi_device_max_queue_depth(sdev),
248edb854a3SMing Lei new_shift, GFP_KERNEL,
249edb854a3SMing Lei sdev->request_queue->node, false, true);
250eaba83b5SJohn Garry if (!ret)
251eaba83b5SJohn Garry sbitmap_resize(&sdev->budget_map, depth);
252eaba83b5SJohn Garry
253edb854a3SMing Lei if (need_free) {
254edb854a3SMing Lei if (ret)
255edb854a3SMing Lei sdev->budget_map = sb_backup;
256edb854a3SMing Lei else
257edb854a3SMing Lei sbitmap_free(&sb_backup);
258edb854a3SMing Lei ret = 0;
259edb854a3SMing Lei blk_mq_unfreeze_queue(sdev->request_queue);
260edb854a3SMing Lei }
261edb854a3SMing Lei return ret;
262edb854a3SMing Lei }
263edb854a3SMing Lei
2641da177e4SLinus Torvalds /**
2651da177e4SLinus Torvalds * scsi_alloc_sdev - allocate and setup a scsi_Device
266eb44820cSRob Landley * @starget: which target to allocate a &scsi_device for
267eb44820cSRob Landley * @lun: which lun
268eb44820cSRob Landley * @hostdata: usually NULL and set by ->slave_alloc instead
2691da177e4SLinus Torvalds *
2701da177e4SLinus Torvalds * Description:
2711da177e4SLinus Torvalds * Allocate, initialize for io, and return a pointer to a scsi_Device.
2721da177e4SLinus Torvalds * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
2731da177e4SLinus Torvalds * adds scsi_Device to the appropriate list.
2741da177e4SLinus Torvalds *
2751da177e4SLinus Torvalds * Return value:
2761da177e4SLinus Torvalds * scsi_Device pointer, or NULL on failure.
2771da177e4SLinus Torvalds **/
scsi_alloc_sdev(struct scsi_target * starget,u64 lun,void * hostdata)2781da177e4SLinus Torvalds static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
2799cb78c16SHannes Reinecke u64 lun, void *hostdata)
2801da177e4SLinus Torvalds {
281020b0f0aSMing Lei unsigned int depth;
2821da177e4SLinus Torvalds struct scsi_device *sdev;
28359506abeSBart Van Assche struct request_queue *q;
2841da177e4SLinus Torvalds int display_failure_msg = 1, ret;
2851da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
286afd53a3dSChristoph Hellwig struct queue_limits lim;
2871da177e4SLinus Torvalds
28824669f75SJes Sorensen sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
2891749ef00SBenjamin Block GFP_KERNEL);
2901da177e4SLinus Torvalds if (!sdev)
2911da177e4SLinus Torvalds goto out;
2921da177e4SLinus Torvalds
2931da177e4SLinus Torvalds sdev->vendor = scsi_null_device_strs;
2941da177e4SLinus Torvalds sdev->model = scsi_null_device_strs;
2951da177e4SLinus Torvalds sdev->rev = scsi_null_device_strs;
2961da177e4SLinus Torvalds sdev->host = shost;
2974a84067dSVasu Dev sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD;
2981da177e4SLinus Torvalds sdev->id = starget->id;
2991da177e4SLinus Torvalds sdev->lun = lun;
3001da177e4SLinus Torvalds sdev->channel = starget->channel;
3010db6ca8aSBart Van Assche mutex_init(&sdev->state_mutex);
3021da177e4SLinus Torvalds sdev->sdev_state = SDEV_CREATED;
3031da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->siblings);
3041da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->same_target_siblings);
3051da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->starved_entry);
306a341cd0fSJeff Garzik INIT_LIST_HEAD(&sdev->event_list);
3071da177e4SLinus Torvalds spin_lock_init(&sdev->list_lock);
30809e2b0b1SHannes Reinecke mutex_init(&sdev->inquiry_mutex);
309a341cd0fSJeff Garzik INIT_WORK(&sdev->event_work, scsi_evt_thread);
3109937a5e2SJens Axboe INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue);
3111da177e4SLinus Torvalds
3121da177e4SLinus Torvalds sdev->sdev_gendev.parent = get_device(&starget->dev);
3131da177e4SLinus Torvalds sdev->sdev_target = starget;
3141da177e4SLinus Torvalds
3151da177e4SLinus Torvalds /* usually NULL and set by ->slave_alloc instead */
3161da177e4SLinus Torvalds sdev->hostdata = hostdata;
3171da177e4SLinus Torvalds
3181da177e4SLinus Torvalds /* if the device needs this changing, it may do so in the
3191da177e4SLinus Torvalds * slave_configure function */
3201da177e4SLinus Torvalds sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
3211da177e4SLinus Torvalds
3221da177e4SLinus Torvalds /*
3231da177e4SLinus Torvalds * Some low level driver could use device->type
3241da177e4SLinus Torvalds */
3251da177e4SLinus Torvalds sdev->type = -1;
3261da177e4SLinus Torvalds
3271da177e4SLinus Torvalds /*
3281da177e4SLinus Torvalds * Assume that the device will have handshaking problems,
3291da177e4SLinus Torvalds * and then fix this field later if it turns out it
3301da177e4SLinus Torvalds * doesn't
3311da177e4SLinus Torvalds */
3321da177e4SLinus Torvalds sdev->borken = 1;
3331da177e4SLinus Torvalds
3341e61c1a8SChristoph Hellwig sdev->sg_reserved_size = INT_MAX;
3351e61c1a8SChristoph Hellwig
336afd53a3dSChristoph Hellwig scsi_init_limits(shost, &lim);
337*e7c09df1SJohn Garry q = blk_mq_alloc_queue(&sdev->host->tag_set, &lim, sdev);
33859506abeSBart Van Assche if (IS_ERR(q)) {
3391da177e4SLinus Torvalds /* release fn is set up in scsi_sysfs_device_initialise, so
3401da177e4SLinus Torvalds * have to free and put manually here */
3411da177e4SLinus Torvalds put_device(&starget->dev);
34293f56089SDave Jones kfree(sdev);
3431da177e4SLinus Torvalds goto out;
3441da177e4SLinus Torvalds }
3458fe4ce58SBart Van Assche kref_get(&sdev->host->tagset_refcnt);
34659506abeSBart Van Assche sdev->request_queue = q;
3472ecb204dSChristoph Hellwig
348020b0f0aSMing Lei depth = sdev->host->cmd_per_lun ?: 1;
349020b0f0aSMing Lei
350020b0f0aSMing Lei /*
351020b0f0aSMing Lei * Use .can_queue as budget map's depth because we have to
352020b0f0aSMing Lei * support adjusting queue depth from sysfs. Meantime use
353020b0f0aSMing Lei * default device queue depth to figure out sbitmap shift
354020b0f0aSMing Lei * since we use this queue depth most of times.
355020b0f0aSMing Lei */
356edb854a3SMing Lei if (scsi_realloc_sdev_budget_map(sdev, depth)) {
357020b0f0aSMing Lei put_device(&starget->dev);
358020b0f0aSMing Lei kfree(sdev);
359020b0f0aSMing Lei goto out;
360020b0f0aSMing Lei }
361020b0f0aSMing Lei
362020b0f0aSMing Lei scsi_change_queue_depth(sdev, depth);
3631da177e4SLinus Torvalds
3641da177e4SLinus Torvalds scsi_sysfs_device_initialize(sdev);
3651da177e4SLinus Torvalds
3661da177e4SLinus Torvalds if (shost->hostt->slave_alloc) {
3671da177e4SLinus Torvalds ret = shost->hostt->slave_alloc(sdev);
3681da177e4SLinus Torvalds if (ret) {
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds * if LLDD reports slave not present, don't clutter
3711da177e4SLinus Torvalds * console with alloc failure messages
3721da177e4SLinus Torvalds */
3731da177e4SLinus Torvalds if (ret == -ENXIO)
3741da177e4SLinus Torvalds display_failure_msg = 0;
3751da177e4SLinus Torvalds goto out_device_destroy;
3761da177e4SLinus Torvalds }
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds
3791da177e4SLinus Torvalds return sdev;
3801da177e4SLinus Torvalds
3811da177e4SLinus Torvalds out_device_destroy:
3824e6c82b3SJames Bottomley __scsi_remove_device(sdev);
3831da177e4SLinus Torvalds out:
3841da177e4SLinus Torvalds if (display_failure_msg)
385cadbd4a5SHarvey Harrison printk(ALLOC_FAILURE_MSG, __func__);
3861da177e4SLinus Torvalds return NULL;
3871da177e4SLinus Torvalds }
3881da177e4SLinus Torvalds
scsi_target_destroy(struct scsi_target * starget)389643eb2d9SJames Bottomley static void scsi_target_destroy(struct scsi_target *starget)
390643eb2d9SJames Bottomley {
391643eb2d9SJames Bottomley struct device *dev = &starget->dev;
392643eb2d9SJames Bottomley struct Scsi_Host *shost = dev_to_shost(dev->parent);
393643eb2d9SJames Bottomley unsigned long flags;
394643eb2d9SJames Bottomley
395f05795d3SJohannes Thumshirn BUG_ON(starget->state == STARGET_DEL);
396f2495e22SJames Bottomley starget->state = STARGET_DEL;
397643eb2d9SJames Bottomley transport_destroy_device(dev);
398643eb2d9SJames Bottomley spin_lock_irqsave(shost->host_lock, flags);
399643eb2d9SJames Bottomley if (shost->hostt->target_destroy)
400643eb2d9SJames Bottomley shost->hostt->target_destroy(starget);
401643eb2d9SJames Bottomley list_del_init(&starget->siblings);
402643eb2d9SJames Bottomley spin_unlock_irqrestore(shost->host_lock, flags);
403643eb2d9SJames Bottomley put_device(dev);
404643eb2d9SJames Bottomley }
405643eb2d9SJames Bottomley
scsi_target_dev_release(struct device * dev)4061da177e4SLinus Torvalds static void scsi_target_dev_release(struct device *dev)
4071da177e4SLinus Torvalds {
4081da177e4SLinus Torvalds struct device *parent = dev->parent;
4091da177e4SLinus Torvalds struct scsi_target *starget = to_scsi_target(dev);
410a283bd37SJames Bottomley
4111da177e4SLinus Torvalds kfree(starget);
4121da177e4SLinus Torvalds put_device(parent);
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds
415517bcc2bSRicardo B. Marliere static const struct device_type scsi_target_type = {
416b0ed4336SHannes Reinecke .name = "scsi_target",
417b0ed4336SHannes Reinecke .release = scsi_target_dev_release,
418b0ed4336SHannes Reinecke };
419b0ed4336SHannes Reinecke
scsi_is_target_device(const struct device * dev)4201da177e4SLinus Torvalds int scsi_is_target_device(const struct device *dev)
4211da177e4SLinus Torvalds {
422b0ed4336SHannes Reinecke return dev->type == &scsi_target_type;
4231da177e4SLinus Torvalds }
4241da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_target_device);
4251da177e4SLinus Torvalds
__scsi_find_target(struct device * parent,int channel,uint id)4261da177e4SLinus Torvalds static struct scsi_target *__scsi_find_target(struct device *parent,
4271da177e4SLinus Torvalds int channel, uint id)
4281da177e4SLinus Torvalds {
4291da177e4SLinus Torvalds struct scsi_target *starget, *found_starget = NULL;
4301da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent);
4311da177e4SLinus Torvalds /*
4321da177e4SLinus Torvalds * Search for an existing target for this sdev.
4331da177e4SLinus Torvalds */
4341da177e4SLinus Torvalds list_for_each_entry(starget, &shost->__targets, siblings) {
4351da177e4SLinus Torvalds if (starget->id == id &&
4361da177e4SLinus Torvalds starget->channel == channel) {
4371da177e4SLinus Torvalds found_starget = starget;
4381da177e4SLinus Torvalds break;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds }
4411da177e4SLinus Torvalds if (found_starget)
4421da177e4SLinus Torvalds get_device(&found_starget->dev);
4431da177e4SLinus Torvalds
4441da177e4SLinus Torvalds return found_starget;
4451da177e4SLinus Torvalds }
4461da177e4SLinus Torvalds
447884d25ccSJames Bottomley /**
448e63ed0d7SJames Bottomley * scsi_target_reap_ref_release - remove target from visibility
449e63ed0d7SJames Bottomley * @kref: the reap_ref in the target being released
450e63ed0d7SJames Bottomley *
451e63ed0d7SJames Bottomley * Called on last put of reap_ref, which is the indication that no device
452e63ed0d7SJames Bottomley * under this target is visible anymore, so render the target invisible in
453e63ed0d7SJames Bottomley * sysfs. Note: we have to be in user context here because the target reaps
454e63ed0d7SJames Bottomley * should be done in places where the scsi device visibility is being removed.
455e63ed0d7SJames Bottomley */
scsi_target_reap_ref_release(struct kref * kref)456e63ed0d7SJames Bottomley static void scsi_target_reap_ref_release(struct kref *kref)
457e63ed0d7SJames Bottomley {
458e63ed0d7SJames Bottomley struct scsi_target *starget
459e63ed0d7SJames Bottomley = container_of(kref, struct scsi_target, reap_ref);
460e63ed0d7SJames Bottomley
461f2495e22SJames Bottomley /*
462f9279c96SEwan D. Milne * if we get here and the target is still in a CREATED state that
463f2495e22SJames Bottomley * means it was allocated but never made visible (because a scan
464f2495e22SJames Bottomley * turned up no LUNs), so don't call device_del() on it.
465f2495e22SJames Bottomley */
466f9279c96SEwan D. Milne if ((starget->state != STARGET_CREATED) &&
467f9279c96SEwan D. Milne (starget->state != STARGET_CREATED_REMOVE)) {
468e63ed0d7SJames Bottomley transport_remove_device(&starget->dev);
469e63ed0d7SJames Bottomley device_del(&starget->dev);
470f2495e22SJames Bottomley }
471e63ed0d7SJames Bottomley scsi_target_destroy(starget);
472e63ed0d7SJames Bottomley }
473e63ed0d7SJames Bottomley
scsi_target_reap_ref_put(struct scsi_target * starget)474e63ed0d7SJames Bottomley static void scsi_target_reap_ref_put(struct scsi_target *starget)
475e63ed0d7SJames Bottomley {
476e63ed0d7SJames Bottomley kref_put(&starget->reap_ref, scsi_target_reap_ref_release);
477e63ed0d7SJames Bottomley }
478e63ed0d7SJames Bottomley
479e63ed0d7SJames Bottomley /**
480884d25ccSJames Bottomley * scsi_alloc_target - allocate a new or find an existing target
481884d25ccSJames Bottomley * @parent: parent of the target (need not be a scsi host)
482884d25ccSJames Bottomley * @channel: target channel number (zero if no channels)
483884d25ccSJames Bottomley * @id: target id number
484884d25ccSJames Bottomley *
485884d25ccSJames Bottomley * Return an existing target if one exists, provided it hasn't already
486884d25ccSJames Bottomley * gone into STARGET_DEL state, otherwise allocate a new target.
487884d25ccSJames Bottomley *
488884d25ccSJames Bottomley * The target is returned with an incremented reference, so the caller
489884d25ccSJames Bottomley * is responsible for both reaping and doing a last put
490884d25ccSJames Bottomley */
scsi_alloc_target(struct device * parent,int channel,uint id)4911da177e4SLinus Torvalds static struct scsi_target *scsi_alloc_target(struct device *parent,
4921da177e4SLinus Torvalds int channel, uint id)
4931da177e4SLinus Torvalds {
4941da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent);
4951da177e4SLinus Torvalds struct device *dev = NULL;
4961da177e4SLinus Torvalds unsigned long flags;
4971da177e4SLinus Torvalds const int size = sizeof(struct scsi_target)
4981da177e4SLinus Torvalds + shost->transportt->target_size;
4995c44cd2aSJames.Smart@Emulex.Com struct scsi_target *starget;
5001da177e4SLinus Torvalds struct scsi_target *found_target;
501e63ed0d7SJames Bottomley int error, ref_got;
5021da177e4SLinus Torvalds
50324669f75SJes Sorensen starget = kzalloc(size, GFP_KERNEL);
5041da177e4SLinus Torvalds if (!starget) {
505cadbd4a5SHarvey Harrison printk(KERN_ERR "%s: allocation failure\n", __func__);
5061da177e4SLinus Torvalds return NULL;
5071da177e4SLinus Torvalds }
5081da177e4SLinus Torvalds dev = &starget->dev;
5091da177e4SLinus Torvalds device_initialize(dev);
510e63ed0d7SJames Bottomley kref_init(&starget->reap_ref);
5111da177e4SLinus Torvalds dev->parent = get_device(parent);
51271610f55SKay Sievers dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
513b0ed4336SHannes Reinecke dev->bus = &scsi_bus_type;
514b0ed4336SHannes Reinecke dev->type = &scsi_target_type;
515a19a93e4SBart Van Assche scsi_enable_async_suspend(dev);
5161da177e4SLinus Torvalds starget->id = id;
5171da177e4SLinus Torvalds starget->channel = channel;
518f0c0a376SMike Christie starget->can_queue = 0;
5191da177e4SLinus Torvalds INIT_LIST_HEAD(&starget->siblings);
5201da177e4SLinus Torvalds INIT_LIST_HEAD(&starget->devices);
521643eb2d9SJames Bottomley starget->state = STARGET_CREATED;
5227c9d6f16SAlan Stern starget->scsi_level = SCSI_2;
523c53a284fSEdward Goggin starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED;
524ffedb452SJames Bottomley retry:
5251da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags);
5261da177e4SLinus Torvalds
5271da177e4SLinus Torvalds found_target = __scsi_find_target(parent, channel, id);
5281da177e4SLinus Torvalds if (found_target)
5291da177e4SLinus Torvalds goto found;
5301da177e4SLinus Torvalds
5311da177e4SLinus Torvalds list_add_tail(&starget->siblings, &shost->__targets);
5321da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags);
5331da177e4SLinus Torvalds /* allocate and add */
534a283bd37SJames Bottomley transport_setup_device(dev);
535a283bd37SJames Bottomley if (shost->hostt->target_alloc) {
53632f95792SBrian King error = shost->hostt->target_alloc(starget);
537a283bd37SJames Bottomley
538a283bd37SJames Bottomley if(error) {
53970edd2e6SSreekanth Reddy if (error != -ENXIO)
54070edd2e6SSreekanth Reddy dev_err(dev, "target allocation failed, error %d\n", error);
541a283bd37SJames Bottomley /* don't want scsi_target_reap to do the final
542a283bd37SJames Bottomley * put because it will be under the host lock */
543643eb2d9SJames Bottomley scsi_target_destroy(starget);
544a283bd37SJames Bottomley return NULL;
545a283bd37SJames Bottomley }
546a283bd37SJames Bottomley }
547884d25ccSJames Bottomley get_device(dev);
548a283bd37SJames Bottomley
5491da177e4SLinus Torvalds return starget;
5501da177e4SLinus Torvalds
5511da177e4SLinus Torvalds found:
552e63ed0d7SJames Bottomley /*
553e63ed0d7SJames Bottomley * release routine already fired if kref is zero, so if we can still
554e63ed0d7SJames Bottomley * take the reference, the target must be alive. If we can't, it must
555e63ed0d7SJames Bottomley * be dying and we need to wait for a new target
556e63ed0d7SJames Bottomley */
557e63ed0d7SJames Bottomley ref_got = kref_get_unless_zero(&found_target->reap_ref);
558e63ed0d7SJames Bottomley
5591da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags);
560e63ed0d7SJames Bottomley if (ref_got) {
56112fb8c15SAlan Stern put_device(dev);
5621da177e4SLinus Torvalds return found_target;
5631da177e4SLinus Torvalds }
564e63ed0d7SJames Bottomley /*
565e63ed0d7SJames Bottomley * Unfortunately, we found a dying target; need to wait until it's
566e63ed0d7SJames Bottomley * dead before we can get a new one. There is an anomaly here. We
567e63ed0d7SJames Bottomley * *should* call scsi_target_reap() to balance the kref_get() of the
568e63ed0d7SJames Bottomley * reap_ref above. However, since the target being released, it's
569e63ed0d7SJames Bottomley * already invisible and the reap_ref is irrelevant. If we call
570e63ed0d7SJames Bottomley * scsi_target_reap() we might spuriously do another device_del() on
571e63ed0d7SJames Bottomley * an already invisible target.
572e63ed0d7SJames Bottomley */
573ffedb452SJames Bottomley put_device(&found_target->dev);
574e63ed0d7SJames Bottomley /*
575e63ed0d7SJames Bottomley * length of time is irrelevant here, we just want to yield the CPU
576e63ed0d7SJames Bottomley * for a tick to avoid busy waiting for the target to die.
577e63ed0d7SJames Bottomley */
578e63ed0d7SJames Bottomley msleep(1);
579ffedb452SJames Bottomley goto retry;
580ffedb452SJames Bottomley }
5811da177e4SLinus Torvalds
5821da177e4SLinus Torvalds /**
5831da177e4SLinus Torvalds * scsi_target_reap - check to see if target is in use and destroy if not
5841da177e4SLinus Torvalds * @starget: target to be checked
5851da177e4SLinus Torvalds *
5861da177e4SLinus Torvalds * This is used after removing a LUN or doing a last put of the target
5871da177e4SLinus Torvalds * it checks atomically that nothing is using the target and removes
5881da177e4SLinus Torvalds * it if so.
5891da177e4SLinus Torvalds */
scsi_target_reap(struct scsi_target * starget)5901da177e4SLinus Torvalds void scsi_target_reap(struct scsi_target *starget)
5911da177e4SLinus Torvalds {
592f2495e22SJames Bottomley /*
593f2495e22SJames Bottomley * serious problem if this triggers: STARGET_DEL is only set in the if
594f2495e22SJames Bottomley * the reap_ref drops to zero, so we're trying to do another final put
595f2495e22SJames Bottomley * on an already released kref
596f2495e22SJames Bottomley */
597e63ed0d7SJames Bottomley BUG_ON(starget->state == STARGET_DEL);
598e63ed0d7SJames Bottomley scsi_target_reap_ref_put(starget);
5991da177e4SLinus Torvalds }
6001da177e4SLinus Torvalds
6011da177e4SLinus Torvalds /**
6023846470aSDon Brace * scsi_sanitize_inquiry_string - remove non-graphical chars from an
6033846470aSDon Brace * INQUIRY result string
604e5b3cd42SAlan Stern * @s: INQUIRY result string to sanitize
605e5b3cd42SAlan Stern * @len: length of the string
606e5b3cd42SAlan Stern *
607e5b3cd42SAlan Stern * Description:
608e5b3cd42SAlan Stern * The SCSI spec says that INQUIRY vendor, product, and revision
609e5b3cd42SAlan Stern * strings must consist entirely of graphic ASCII characters,
610e5b3cd42SAlan Stern * padded on the right with spaces. Since not all devices obey
611e5b3cd42SAlan Stern * this rule, we will replace non-graphic or non-ASCII characters
612e5b3cd42SAlan Stern * with spaces. Exception: a NUL character is interpreted as a
613e5b3cd42SAlan Stern * string terminator, so all the following characters are set to
614e5b3cd42SAlan Stern * spaces.
615e5b3cd42SAlan Stern **/
scsi_sanitize_inquiry_string(unsigned char * s,int len)6163846470aSDon Brace void scsi_sanitize_inquiry_string(unsigned char *s, int len)
617e5b3cd42SAlan Stern {
618e5b3cd42SAlan Stern int terminated = 0;
619e5b3cd42SAlan Stern
620e5b3cd42SAlan Stern for (; len > 0; (--len, ++s)) {
621e5b3cd42SAlan Stern if (*s == 0)
622e5b3cd42SAlan Stern terminated = 1;
623e5b3cd42SAlan Stern if (terminated || *s < 0x20 || *s > 0x7e)
624e5b3cd42SAlan Stern *s = ' ';
625e5b3cd42SAlan Stern }
626e5b3cd42SAlan Stern }
6273846470aSDon Brace EXPORT_SYMBOL(scsi_sanitize_inquiry_string);
628e5b3cd42SAlan Stern
6292a1f96f6SMike Christie
630e5b3cd42SAlan Stern /**
6311da177e4SLinus Torvalds * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
63239216033SJames Bottomley * @sdev: scsi_device to probe
6331da177e4SLinus Torvalds * @inq_result: area to store the INQUIRY result
63439216033SJames Bottomley * @result_len: len of inq_result
6351da177e4SLinus Torvalds * @bflags: store any bflags found here
6361da177e4SLinus Torvalds *
6371da177e4SLinus Torvalds * Description:
63839216033SJames Bottomley * Probe the lun associated with @req using a standard SCSI INQUIRY;
6391da177e4SLinus Torvalds *
64039216033SJames Bottomley * If the INQUIRY is successful, zero is returned and the
6411da177e4SLinus Torvalds * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
642f64a181dSChristoph Hellwig * are copied to the scsi_device any flags value is stored in *@bflags.
6431da177e4SLinus Torvalds **/
scsi_probe_lun(struct scsi_device * sdev,unsigned char * inq_result,int result_len,blist_flags_t * bflags)644e5b3cd42SAlan Stern static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
6455ebde469SHannes Reinecke int result_len, blist_flags_t *bflags)
6461da177e4SLinus Torvalds {
6471da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE];
6481da177e4SLinus Torvalds int first_inquiry_len, try_inquiry_len, next_inquiry_len;
6491da177e4SLinus Torvalds int response_len = 0;
6507dfe0b5eSMike Christie int pass, count, result, resid;
6512a1f96f6SMike Christie struct scsi_failure failure_defs[] = {
6522a1f96f6SMike Christie /*
6532a1f96f6SMike Christie * not-ready to ready transition [asc/ascq=0x28/0x0] or
6542a1f96f6SMike Christie * power-on, reset [asc/ascq=0x29/0x0], continue. INQUIRY
6552a1f96f6SMike Christie * should not yield UNIT_ATTENTION but many buggy devices do
6562a1f96f6SMike Christie * so anyway.
6572a1f96f6SMike Christie */
6582a1f96f6SMike Christie {
6592a1f96f6SMike Christie .sense = UNIT_ATTENTION,
6602a1f96f6SMike Christie .asc = 0x28,
6612a1f96f6SMike Christie .result = SAM_STAT_CHECK_CONDITION,
6622a1f96f6SMike Christie },
6632a1f96f6SMike Christie {
6642a1f96f6SMike Christie .sense = UNIT_ATTENTION,
6652a1f96f6SMike Christie .asc = 0x29,
6662a1f96f6SMike Christie .result = SAM_STAT_CHECK_CONDITION,
6672a1f96f6SMike Christie },
668987d7d3dSMike Christie {
669987d7d3dSMike Christie .allowed = 1,
670987d7d3dSMike Christie .result = DID_TIME_OUT << 16,
671987d7d3dSMike Christie },
6722a1f96f6SMike Christie {}
6732a1f96f6SMike Christie };
6742a1f96f6SMike Christie struct scsi_failures failures = {
6752a1f96f6SMike Christie .total_allowed = 3,
6762a1f96f6SMike Christie .failure_definitions = failure_defs,
6772a1f96f6SMike Christie };
6787dfe0b5eSMike Christie const struct scsi_exec_args exec_args = {
6797dfe0b5eSMike Christie .resid = &resid,
6802a1f96f6SMike Christie .failures = &failures,
6817dfe0b5eSMike Christie };
6821da177e4SLinus Torvalds
6831da177e4SLinus Torvalds *bflags = 0;
6841da177e4SLinus Torvalds
6851da177e4SLinus Torvalds /* Perform up to 3 passes. The first pass uses a conservative
6861da177e4SLinus Torvalds * transfer length of 36 unless sdev->inquiry_len specifies a
6871da177e4SLinus Torvalds * different value. */
6881da177e4SLinus Torvalds first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
6891da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len;
6901da177e4SLinus Torvalds pass = 1;
6911da177e4SLinus Torvalds
6921da177e4SLinus Torvalds next_pass:
6939ccfc756SJames Bottomley SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
6949ccfc756SJames Bottomley "scsi scan: INQUIRY pass %d length %d\n",
6959ccfc756SJames Bottomley pass, try_inquiry_len));
6961da177e4SLinus Torvalds
6971da177e4SLinus Torvalds /* Each pass gets up to three chances to ignore Unit Attention */
6982a1f96f6SMike Christie scsi_failures_reset_retries(&failures);
6992a1f96f6SMike Christie
7001da177e4SLinus Torvalds for (count = 0; count < 3; ++count) {
7011da177e4SLinus Torvalds memset(scsi_cmd, 0, 6);
7021da177e4SLinus Torvalds scsi_cmd[0] = INQUIRY;
7031da177e4SLinus Torvalds scsi_cmd[4] = (unsigned char) try_inquiry_len;
7041da177e4SLinus Torvalds
7051da177e4SLinus Torvalds memset(inq_result, 0, try_inquiry_len);
70639216033SJames Bottomley
7077dfe0b5eSMike Christie result = scsi_execute_cmd(sdev, scsi_cmd, REQ_OP_DRV_IN,
7087dfe0b5eSMike Christie inq_result, try_inquiry_len,
709f4f4e47eSFUJITA Tomonori HZ / 2 + HZ * scsi_inq_timeout, 3,
7107dfe0b5eSMike Christie &exec_args);
7111da177e4SLinus Torvalds
71291921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
71391921e01SHannes Reinecke "scsi scan: INQUIRY %s with code 0x%x\n",
71439216033SJames Bottomley result ? "failed" : "successful", result));
7151da177e4SLinus Torvalds
7162a1f96f6SMike Christie if (result == 0) {
7175cd3bbfaSFUJITA Tomonori /*
7185cd3bbfaSFUJITA Tomonori * if nothing was transferred, we try
7195cd3bbfaSFUJITA Tomonori * again. It's a workaround for some USB
7205cd3bbfaSFUJITA Tomonori * devices.
7215cd3bbfaSFUJITA Tomonori */
7225cd3bbfaSFUJITA Tomonori if (resid == try_inquiry_len)
7235cd3bbfaSFUJITA Tomonori continue;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds break;
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds
72839216033SJames Bottomley if (result == 0) {
7293846470aSDon Brace scsi_sanitize_inquiry_string(&inq_result[8], 8);
7303846470aSDon Brace scsi_sanitize_inquiry_string(&inq_result[16], 16);
7313846470aSDon Brace scsi_sanitize_inquiry_string(&inq_result[32], 4);
732e5b3cd42SAlan Stern
733e5b3cd42SAlan Stern response_len = inq_result[4] + 5;
7341da177e4SLinus Torvalds if (response_len > 255)
7351da177e4SLinus Torvalds response_len = first_inquiry_len; /* sanity */
7361da177e4SLinus Torvalds
7371da177e4SLinus Torvalds /*
7381da177e4SLinus Torvalds * Get any flags for this device.
7391da177e4SLinus Torvalds *
740f64a181dSChristoph Hellwig * XXX add a bflags to scsi_device, and replace the
741f64a181dSChristoph Hellwig * corresponding bit fields in scsi_device, so bflags
7421da177e4SLinus Torvalds * need not be passed as an argument.
7431da177e4SLinus Torvalds */
7441da177e4SLinus Torvalds *bflags = scsi_get_device_flags(sdev, &inq_result[8],
7451da177e4SLinus Torvalds &inq_result[16]);
7461da177e4SLinus Torvalds
7471da177e4SLinus Torvalds /* When the first pass succeeds we gain information about
7481da177e4SLinus Torvalds * what larger transfer lengths might work. */
7491da177e4SLinus Torvalds if (pass == 1) {
7501da177e4SLinus Torvalds if (BLIST_INQUIRY_36 & *bflags)
7511da177e4SLinus Torvalds next_inquiry_len = 36;
752d657700cSMartin K. Petersen /*
753d657700cSMartin K. Petersen * LLD specified a maximum sdev->inquiry_len
754d657700cSMartin K. Petersen * but device claims it has more data. Capping
755d657700cSMartin K. Petersen * the length only makes sense for legacy
756d657700cSMartin K. Petersen * devices. If a device supports SPC-4 (2014)
757d657700cSMartin K. Petersen * or newer, assume that it is safe to ask for
758d657700cSMartin K. Petersen * as much as the device says it supports.
759d657700cSMartin K. Petersen */
760d657700cSMartin K. Petersen else if (sdev->inquiry_len &&
761d657700cSMartin K. Petersen response_len > sdev->inquiry_len &&
762d657700cSMartin K. Petersen (inq_result[2] & 0x7) < 6) /* SPC-4 */
7631da177e4SLinus Torvalds next_inquiry_len = sdev->inquiry_len;
7641da177e4SLinus Torvalds else
7651da177e4SLinus Torvalds next_inquiry_len = response_len;
7661da177e4SLinus Torvalds
7671da177e4SLinus Torvalds /* If more data is available perform the second pass */
7681da177e4SLinus Torvalds if (next_inquiry_len > try_inquiry_len) {
7691da177e4SLinus Torvalds try_inquiry_len = next_inquiry_len;
7701da177e4SLinus Torvalds pass = 2;
7711da177e4SLinus Torvalds goto next_pass;
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds }
7741da177e4SLinus Torvalds
7751da177e4SLinus Torvalds } else if (pass == 2) {
77691921e01SHannes Reinecke sdev_printk(KERN_INFO, sdev,
77791921e01SHannes Reinecke "scsi scan: %d byte inquiry failed. "
7781da177e4SLinus Torvalds "Consider BLIST_INQUIRY_36 for this device\n",
7791da177e4SLinus Torvalds try_inquiry_len);
7801da177e4SLinus Torvalds
7811da177e4SLinus Torvalds /* If this pass failed, the third pass goes back and transfers
7821da177e4SLinus Torvalds * the same amount as we successfully got in the first pass. */
7831da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len;
7841da177e4SLinus Torvalds pass = 3;
7851da177e4SLinus Torvalds goto next_pass;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds
7881da177e4SLinus Torvalds /* If the last transfer attempt got an error, assume the
7891da177e4SLinus Torvalds * peripheral doesn't exist or is dead. */
79039216033SJames Bottomley if (result)
79139216033SJames Bottomley return -EIO;
7921da177e4SLinus Torvalds
7931da177e4SLinus Torvalds /* Don't report any more data than the device says is valid */
7941da177e4SLinus Torvalds sdev->inquiry_len = min(try_inquiry_len, response_len);
7951da177e4SLinus Torvalds
7961da177e4SLinus Torvalds /*
7971da177e4SLinus Torvalds * XXX Abort if the response length is less than 36? If less than
7981da177e4SLinus Torvalds * 32, the lookup of the device flags (above) could be invalid,
7991da177e4SLinus Torvalds * and it would be possible to take an incorrect action - we do
8001da177e4SLinus Torvalds * not want to hang because of a short INQUIRY. On the flip side,
8011da177e4SLinus Torvalds * if the device is spun down or becoming ready (and so it gives a
8021da177e4SLinus Torvalds * short INQUIRY), an abort here prevents any further use of the
8031da177e4SLinus Torvalds * device, including spin up.
8041da177e4SLinus Torvalds *
805e423ee31SAlan Stern * On the whole, the best approach seems to be to assume the first
806e423ee31SAlan Stern * 36 bytes are valid no matter what the device says. That's
807e423ee31SAlan Stern * better than copying < 36 bytes to the inquiry-result buffer
808e423ee31SAlan Stern * and displaying garbage for the Vendor, Product, or Revision
809e423ee31SAlan Stern * strings.
810e423ee31SAlan Stern */
811e423ee31SAlan Stern if (sdev->inquiry_len < 36) {
812a35bb445SVitaly Kuznetsov if (!sdev->host->short_inquiry) {
813a35bb445SVitaly Kuznetsov shost_printk(KERN_INFO, sdev->host,
81491921e01SHannes Reinecke "scsi scan: INQUIRY result too short (%d),"
815e423ee31SAlan Stern " using 36\n", sdev->inquiry_len);
816a35bb445SVitaly Kuznetsov sdev->host->short_inquiry = 1;
817a35bb445SVitaly Kuznetsov }
818e423ee31SAlan Stern sdev->inquiry_len = 36;
819e423ee31SAlan Stern }
820e423ee31SAlan Stern
821e423ee31SAlan Stern /*
8221da177e4SLinus Torvalds * Related to the above issue:
8231da177e4SLinus Torvalds *
8241da177e4SLinus Torvalds * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
8251da177e4SLinus Torvalds * and if not ready, sent a START_STOP to start (maybe spin up) and
8261da177e4SLinus Torvalds * then send the INQUIRY again, since the INQUIRY can change after
8271da177e4SLinus Torvalds * a device is initialized.
8281da177e4SLinus Torvalds *
8291da177e4SLinus Torvalds * Ideally, start a device if explicitly asked to do so. This
8301da177e4SLinus Torvalds * assumes that a device is spun up on power on, spun down on
8311da177e4SLinus Torvalds * request, and then spun up on request.
8321da177e4SLinus Torvalds */
8331da177e4SLinus Torvalds
8341da177e4SLinus Torvalds /*
8351da177e4SLinus Torvalds * The scanning code needs to know the scsi_level, even if no
8361da177e4SLinus Torvalds * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
8371da177e4SLinus Torvalds * non-zero LUNs can be scanned.
8381da177e4SLinus Torvalds */
8392132df16SDamien Le Moal sdev->scsi_level = inq_result[2] & 0x0f;
8401da177e4SLinus Torvalds if (sdev->scsi_level >= 2 ||
8411da177e4SLinus Torvalds (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
8421da177e4SLinus Torvalds sdev->scsi_level++;
8436f3a2024SJames Bottomley sdev->sdev_target->scsi_level = sdev->scsi_level;
8441da177e4SLinus Torvalds
84550c4e964SAlan Stern /*
84650c4e964SAlan Stern * If SCSI-2 or lower, and if the transport requires it,
84750c4e964SAlan Stern * store the LUN value in CDB[1].
84850c4e964SAlan Stern */
84950c4e964SAlan Stern sdev->lun_in_cdb = 0;
85050c4e964SAlan Stern if (sdev->scsi_level <= SCSI_2 &&
85150c4e964SAlan Stern sdev->scsi_level != SCSI_UNKNOWN &&
85250c4e964SAlan Stern !sdev->host->no_scsi2_lun_in_cdb)
85350c4e964SAlan Stern sdev->lun_in_cdb = 1;
85450c4e964SAlan Stern
85539216033SJames Bottomley return 0;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds
8581da177e4SLinus Torvalds /**
859f64a181dSChristoph Hellwig * scsi_add_lun - allocate and fully initialze a scsi_device
8606d877688SMatthew Wilcox * @sdev: holds information to be stored in the new scsi_device
8611da177e4SLinus Torvalds * @inq_result: holds the result of a previous INQUIRY to the LUN
8621da177e4SLinus Torvalds * @bflags: black/white list flag
8636d877688SMatthew Wilcox * @async: 1 if this device is being scanned asynchronously
8641da177e4SLinus Torvalds *
8651da177e4SLinus Torvalds * Description:
8666d877688SMatthew Wilcox * Initialize the scsi_device @sdev. Optionally set fields based
8676d877688SMatthew Wilcox * on values in *@bflags.
8681da177e4SLinus Torvalds *
8691da177e4SLinus Torvalds * Return:
870f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
871f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
8721da177e4SLinus Torvalds **/
scsi_add_lun(struct scsi_device * sdev,unsigned char * inq_result,blist_flags_t * bflags,int async)873e5b3cd42SAlan Stern static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
874093b8886SBart Van Assche blist_flags_t *bflags, int async)
8751da177e4SLinus Torvalds {
876b7eefcf1SChristoph Hellwig const struct scsi_host_template *hostt = sdev->host->hostt;
877693a1e8cSChristoph Hellwig struct queue_limits lim;
8786f4267e3SJames Bottomley int ret;
8796f4267e3SJames Bottomley
8801da177e4SLinus Torvalds /*
8811da177e4SLinus Torvalds * XXX do not save the inquiry, since it can change underneath us,
8821da177e4SLinus Torvalds * save just vendor/model/rev.
8831da177e4SLinus Torvalds *
8841da177e4SLinus Torvalds * Rather than save it and have an ioctl that retrieves the saved
8851da177e4SLinus Torvalds * value, have an ioctl that executes the same INQUIRY code used
8861da177e4SLinus Torvalds * in scsi_probe_lun, let user level programs doing INQUIRY
8871da177e4SLinus Torvalds * scanning run at their own risk, or supply a user level program
8881da177e4SLinus Torvalds * that can correctly scan.
8891da177e4SLinus Torvalds */
8901da177e4SLinus Torvalds
89109123d23SAlan Stern /*
89209123d23SAlan Stern * Copy at least 36 bytes of INQUIRY data, so that we don't
89309123d23SAlan Stern * dereference unallocated memory when accessing the Vendor,
89409123d23SAlan Stern * Product, and Revision strings. Badly behaved devices may set
89509123d23SAlan Stern * the INQUIRY Additional Length byte to a small value, indicating
89609123d23SAlan Stern * these strings are invalid, but often they contain plausible data
89709123d23SAlan Stern * nonetheless. It doesn't matter if the device sent < 36 bytes
89809123d23SAlan Stern * total, since scsi_probe_lun() initializes inq_result with 0s.
89909123d23SAlan Stern */
90009123d23SAlan Stern sdev->inquiry = kmemdup(inq_result,
90109123d23SAlan Stern max_t(size_t, sdev->inquiry_len, 36),
9021749ef00SBenjamin Block GFP_KERNEL);
90309123d23SAlan Stern if (sdev->inquiry == NULL)
90409123d23SAlan Stern return SCSI_SCAN_NO_RESPONSE;
90509123d23SAlan Stern
9061da177e4SLinus Torvalds sdev->vendor = (char *) (sdev->inquiry + 8);
9071da177e4SLinus Torvalds sdev->model = (char *) (sdev->inquiry + 16);
9081da177e4SLinus Torvalds sdev->rev = (char *) (sdev->inquiry + 32);
9091da177e4SLinus Torvalds
91014216561SJames Bottomley if (strncmp(sdev->vendor, "ATA ", 8) == 0) {
91114216561SJames Bottomley /*
91214216561SJames Bottomley * sata emulation layer device. This is a hack to work around
91314216561SJames Bottomley * the SATL power management specifications which state that
91414216561SJames Bottomley * when the SATL detects the device has gone into standby
91514216561SJames Bottomley * mode, it shall respond with NOT READY.
91614216561SJames Bottomley */
91714216561SJames Bottomley sdev->allow_restart = 1;
91814216561SJames Bottomley }
91914216561SJames Bottomley
9201da177e4SLinus Torvalds if (*bflags & BLIST_ISROM) {
9216d877688SMatthew Wilcox sdev->type = TYPE_ROM;
9226d877688SMatthew Wilcox sdev->removable = 1;
9236d877688SMatthew Wilcox } else {
9246d877688SMatthew Wilcox sdev->type = (inq_result[0] & 0x1f);
9256d877688SMatthew Wilcox sdev->removable = (inq_result[1] & 0x80) >> 7;
92645341ca3SSubhash Jadavani
92745341ca3SSubhash Jadavani /*
92845341ca3SSubhash Jadavani * some devices may respond with wrong type for
92945341ca3SSubhash Jadavani * well-known logical units. Force well-known type
93045341ca3SSubhash Jadavani * to enumerate them correctly.
93145341ca3SSubhash Jadavani */
93245341ca3SSubhash Jadavani if (scsi_is_wlun(sdev->lun) && sdev->type != TYPE_WLUN) {
93345341ca3SSubhash Jadavani sdev_printk(KERN_WARNING, sdev,
93445341ca3SSubhash Jadavani "%s: correcting incorrect peripheral device type 0x%x for W-LUN 0x%16xhN\n",
93545341ca3SSubhash Jadavani __func__, sdev->type, (unsigned int)sdev->lun);
93645341ca3SSubhash Jadavani sdev->type = TYPE_WLUN;
93745341ca3SSubhash Jadavani }
93845341ca3SSubhash Jadavani
9396d877688SMatthew Wilcox }
9401da177e4SLinus Torvalds
9416d877688SMatthew Wilcox if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
9426d877688SMatthew Wilcox /* RBC and MMC devices can return SCSI-3 compliance and yet
9436d877688SMatthew Wilcox * still not support REPORT LUNS, so make them act as
9446d877688SMatthew Wilcox * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
9456d877688SMatthew Wilcox * specifically set */
9466d877688SMatthew Wilcox if ((*bflags & BLIST_REPORTLUN2) == 0)
9476d877688SMatthew Wilcox *bflags |= BLIST_NOREPORTLUN;
9486d877688SMatthew Wilcox }
9496d877688SMatthew Wilcox
9501da177e4SLinus Torvalds /*
9511da177e4SLinus Torvalds * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
9521da177e4SLinus Torvalds * spec says: The device server is capable of supporting the
9531da177e4SLinus Torvalds * specified peripheral device type on this logical unit. However,
9541da177e4SLinus Torvalds * the physical device is not currently connected to this logical
9551da177e4SLinus Torvalds * unit.
9561da177e4SLinus Torvalds *
9571da177e4SLinus Torvalds * The above is vague, as it implies that we could treat 001 and
9581da177e4SLinus Torvalds * 011 the same. Stay compatible with previous code, and create a
959f64a181dSChristoph Hellwig * scsi_device for a PQ of 1
9601da177e4SLinus Torvalds *
9611da177e4SLinus Torvalds * Don't set the device offline here; rather let the upper
9621da177e4SLinus Torvalds * level drivers eval the PQ to decide whether they should
9631da177e4SLinus Torvalds * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
9641da177e4SLinus Torvalds */
9651da177e4SLinus Torvalds
9661da177e4SLinus Torvalds sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
9671da177e4SLinus Torvalds sdev->lockable = sdev->removable;
9681da177e4SLinus Torvalds sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
9691da177e4SLinus Torvalds
9706d877688SMatthew Wilcox if (sdev->scsi_level >= SCSI_3 ||
9716d877688SMatthew Wilcox (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
9721da177e4SLinus Torvalds sdev->ppr = 1;
9731da177e4SLinus Torvalds if (inq_result[7] & 0x60)
9741da177e4SLinus Torvalds sdev->wdtr = 1;
9751da177e4SLinus Torvalds if (inq_result[7] & 0x10)
9761da177e4SLinus Torvalds sdev->sdtr = 1;
9771da177e4SLinus Torvalds
97819ac0db3SJames Bottomley sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
9794ff36718SMatthew Wilcox "ANSI: %d%s\n", scsi_device_type(sdev->type),
9804ff36718SMatthew Wilcox sdev->vendor, sdev->model, sdev->rev,
9814ff36718SMatthew Wilcox sdev->inq_periph_qual, inq_result[2] & 0x07,
9824ff36718SMatthew Wilcox (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
9834ff36718SMatthew Wilcox
9841da177e4SLinus Torvalds if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
985c8b09f6fSChristoph Hellwig !(*bflags & BLIST_NOTQ)) {
9861da177e4SLinus Torvalds sdev->tagged_supported = 1;
987c8b09f6fSChristoph Hellwig sdev->simple_tags = 1;
988c8b09f6fSChristoph Hellwig }
9896d877688SMatthew Wilcox
9901da177e4SLinus Torvalds /*
9911da177e4SLinus Torvalds * Some devices (Texel CD ROM drives) have handshaking problems
9921da177e4SLinus Torvalds * when used with the Seagate controllers. borken is initialized
9931da177e4SLinus Torvalds * to 1, and then set it to 0 here.
9941da177e4SLinus Torvalds */
9951da177e4SLinus Torvalds if ((*bflags & BLIST_BORKEN) == 0)
9961da177e4SLinus Torvalds sdev->borken = 0;
9971da177e4SLinus Torvalds
9986d877688SMatthew Wilcox if (*bflags & BLIST_NO_ULD_ATTACH)
9996d877688SMatthew Wilcox sdev->no_uld_attach = 1;
10006d877688SMatthew Wilcox
10011da177e4SLinus Torvalds /*
10021da177e4SLinus Torvalds * Apparently some really broken devices (contrary to the SCSI
10031da177e4SLinus Torvalds * standards) need to be selected without asserting ATN
10041da177e4SLinus Torvalds */
10051da177e4SLinus Torvalds if (*bflags & BLIST_SELECT_NO_ATN)
10061da177e4SLinus Torvalds sdev->select_no_atn = 1;
10071da177e4SLinus Torvalds
10081da177e4SLinus Torvalds /*
10091da177e4SLinus Torvalds * Some devices may not want to have a start command automatically
10101da177e4SLinus Torvalds * issued when a device is added.
10111da177e4SLinus Torvalds */
10121da177e4SLinus Torvalds if (*bflags & BLIST_NOSTARTONADD)
10131da177e4SLinus Torvalds sdev->no_start_on_add = 1;
10141da177e4SLinus Torvalds
10151da177e4SLinus Torvalds if (*bflags & BLIST_SINGLELUN)
101625d7c363STony Battersby scsi_target(sdev)->single_lun = 1;
10171da177e4SLinus Torvalds
10181da177e4SLinus Torvalds sdev->use_10_for_rw = 1;
10191da177e4SLinus Torvalds
10200213436aSJanusz Dziemidowicz /* some devices don't like REPORT SUPPORTED OPERATION CODES
10210213436aSJanusz Dziemidowicz * and will simply timeout causing sd_mod init to take a very
10220213436aSJanusz Dziemidowicz * very long time */
10230213436aSJanusz Dziemidowicz if (*bflags & BLIST_NO_RSOC)
10240213436aSJanusz Dziemidowicz sdev->no_report_opcodes = 1;
10250213436aSJanusz Dziemidowicz
10261da177e4SLinus Torvalds /* set the device running here so that slave configure
10271da177e4SLinus Torvalds * may do I/O */
10280db6ca8aSBart Van Assche mutex_lock(&sdev->state_mutex);
10296f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_RUNNING);
10300db6ca8aSBart Van Assche if (ret)
10316f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_BLOCK);
10320db6ca8aSBart Van Assche mutex_unlock(&sdev->state_mutex);
10336f4267e3SJames Bottomley
10346f4267e3SJames Bottomley if (ret) {
10356f4267e3SJames Bottomley sdev_printk(KERN_ERR, sdev,
10366f4267e3SJames Bottomley "in wrong state %s to complete scan\n",
10376f4267e3SJames Bottomley scsi_device_state_name(sdev->sdev_state));
10386f4267e3SJames Bottomley return SCSI_SCAN_NO_RESPONSE;
10396f4267e3SJames Bottomley }
10401da177e4SLinus Torvalds
10411da177e4SLinus Torvalds if (*bflags & BLIST_NOT_LOCKABLE)
10421da177e4SLinus Torvalds sdev->lockable = 0;
10431da177e4SLinus Torvalds
10441da177e4SLinus Torvalds if (*bflags & BLIST_RETRY_HWERROR)
10451da177e4SLinus Torvalds sdev->retry_hwerror = 1;
10461da177e4SLinus Torvalds
1047d974e426SMartin K. Petersen if (*bflags & BLIST_NO_DIF)
1048d974e426SMartin K. Petersen sdev->no_dif = 1;
1049d974e426SMartin K. Petersen
105028a0bc41SMartin K. Petersen if (*bflags & BLIST_UNMAP_LIMIT_WS)
105128a0bc41SMartin K. Petersen sdev->unmap_limit_for_ws = 1;
105228a0bc41SMartin K. Petersen
1053f591a2e0SMartin Kepplinger if (*bflags & BLIST_IGN_MEDIA_CHANGE)
1054f591a2e0SMartin Kepplinger sdev->ignore_media_change = 1;
1055f591a2e0SMartin Kepplinger
10560816c925SMartin K. Petersen sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT;
10570816c925SMartin K. Petersen
1058c1d40a52SMartin K. Petersen if (*bflags & BLIST_TRY_VPD_PAGES)
1059c1d40a52SMartin K. Petersen sdev->try_vpd_pages = 1;
1060c1d40a52SMartin K. Petersen else if (*bflags & BLIST_SKIP_VPD_PAGES)
106156f2a801SMartin K. Petersen sdev->skip_vpd_pages = 1;
106256f2a801SMartin K. Petersen
10634b1a2c2aSLee Duncan if (*bflags & BLIST_NO_VPD_SIZE)
10644b1a2c2aSLee Duncan sdev->no_vpd_size = 1;
10654b1a2c2aSLee Duncan
10661da177e4SLinus Torvalds transport_configure_device(&sdev->sdev_gendev);
10671da177e4SLinus Torvalds
106893805091SChristoph Hellwig /*
1069693a1e8cSChristoph Hellwig * No need to freeze the queue as it isn't reachable to anyone else yet.
107093805091SChristoph Hellwig */
1071693a1e8cSChristoph Hellwig lim = queue_limits_start_update(sdev->request_queue);
1072693a1e8cSChristoph Hellwig if (*bflags & BLIST_MAX_512)
1073693a1e8cSChristoph Hellwig lim.max_hw_sectors = 512;
1074693a1e8cSChristoph Hellwig else if (*bflags & BLIST_MAX_1024)
1075693a1e8cSChristoph Hellwig lim.max_hw_sectors = 1024;
1076b7eefcf1SChristoph Hellwig
1077b7eefcf1SChristoph Hellwig if (hostt->device_configure)
1078b7eefcf1SChristoph Hellwig ret = hostt->device_configure(sdev, &lim);
1079b7eefcf1SChristoph Hellwig else if (hostt->slave_configure)
1080b7eefcf1SChristoph Hellwig ret = hostt->slave_configure(sdev);
1081b7eefcf1SChristoph Hellwig if (ret) {
1082b7eefcf1SChristoph Hellwig queue_limits_cancel_update(sdev->request_queue);
1083b7eefcf1SChristoph Hellwig /*
1084b7eefcf1SChristoph Hellwig * If the LLDD reports device not present, don't clutter the
1085b7eefcf1SChristoph Hellwig * console with failure messages.
1086b7eefcf1SChristoph Hellwig */
1087b7eefcf1SChristoph Hellwig if (ret != -ENXIO)
108893805091SChristoph Hellwig sdev_printk(KERN_ERR, sdev,
108993805091SChristoph Hellwig "failed to configure device\n");
1090b7eefcf1SChristoph Hellwig return SCSI_SCAN_NO_RESPONSE;
109193805091SChristoph Hellwig }
1092b7eefcf1SChristoph Hellwig
1093693a1e8cSChristoph Hellwig ret = queue_limits_commit_update(sdev->request_queue, &lim);
1094693a1e8cSChristoph Hellwig if (ret) {
1095693a1e8cSChristoph Hellwig sdev_printk(KERN_ERR, sdev, "failed to apply queue limits.\n");
109693805091SChristoph Hellwig return SCSI_SCAN_NO_RESPONSE;
109793805091SChristoph Hellwig }
1098edb854a3SMing Lei
1099edb854a3SMing Lei /*
1100b7eefcf1SChristoph Hellwig * The queue_depth is often changed in ->device_configure.
1101b7eefcf1SChristoph Hellwig *
1102b7eefcf1SChristoph Hellwig * Set up budget map again since memory consumption of the map depends
1103b7eefcf1SChristoph Hellwig * on actual queue depth.
1104edb854a3SMing Lei */
1105b7eefcf1SChristoph Hellwig if (hostt->device_configure || hostt->slave_configure)
1106edb854a3SMing Lei scsi_realloc_sdev_budget_map(sdev, sdev->queue_depth);
11071da177e4SLinus Torvalds
1108b3ae8780SHannes Reinecke if (sdev->scsi_level >= SCSI_3)
1109b3ae8780SHannes Reinecke scsi_attach_vpd(sdev);
1110b3ae8780SHannes Reinecke
111162488520SDamien Le Moal scsi_cdl_check(sdev);
111262488520SDamien Le Moal
11134a84067dSVasu Dev sdev->max_queue_depth = sdev->queue_depth;
1114020b0f0aSMing Lei WARN_ON_ONCE(sdev->max_queue_depth > sdev->budget_map.depth);
1115345e2960SHannes Reinecke sdev->sdev_bflags = *bflags;
11164a84067dSVasu Dev
11171da177e4SLinus Torvalds /*
11181da177e4SLinus Torvalds * Ok, the device is now all set up, we can
11191da177e4SLinus Torvalds * register it and tell the rest of the kernel
11201da177e4SLinus Torvalds * about it.
11211da177e4SLinus Torvalds */
11223e082a91SMatthew Wilcox if (!async && scsi_sysfs_add_sdev(sdev) != 0)
1123b24b1033SAlan Stern return SCSI_SCAN_NO_RESPONSE;
11241da177e4SLinus Torvalds
11251da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT;
11261da177e4SLinus Torvalds }
11271da177e4SLinus Torvalds
1128c5f2e640Sakpm@osdl.org #ifdef CONFIG_SCSI_LOGGING
11296c7154c9SKurt Garloff /**
1130eb44820cSRob Landley * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace
11316c7154c9SKurt Garloff * @buf: Output buffer with at least end-first+1 bytes of space
11326c7154c9SKurt Garloff * @inq: Inquiry buffer (input)
11336c7154c9SKurt Garloff * @first: Offset of string into inq
11346c7154c9SKurt Garloff * @end: Index after last character in inq
11356c7154c9SKurt Garloff */
scsi_inq_str(unsigned char * buf,unsigned char * inq,unsigned first,unsigned end)11366c7154c9SKurt Garloff static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
11376c7154c9SKurt Garloff unsigned first, unsigned end)
11386c7154c9SKurt Garloff {
11396c7154c9SKurt Garloff unsigned term = 0, idx;
1140c5f2e640Sakpm@osdl.org
1141c5f2e640Sakpm@osdl.org for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
1142c5f2e640Sakpm@osdl.org if (inq[idx+first] > ' ') {
11436c7154c9SKurt Garloff buf[idx] = inq[idx+first];
11446c7154c9SKurt Garloff term = idx+1;
11456c7154c9SKurt Garloff } else {
11466c7154c9SKurt Garloff buf[idx] = ' ';
11476c7154c9SKurt Garloff }
11486c7154c9SKurt Garloff }
11496c7154c9SKurt Garloff buf[term] = 0;
11506c7154c9SKurt Garloff return buf;
11516c7154c9SKurt Garloff }
1152c5f2e640Sakpm@osdl.org #endif
11536f3a2024SJames Bottomley
11541da177e4SLinus Torvalds /**
11551da177e4SLinus Torvalds * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
11561da177e4SLinus Torvalds * @starget: pointer to target device structure
11571da177e4SLinus Torvalds * @lun: LUN of target device
11581da177e4SLinus Torvalds * @bflagsp: store bflags here if not NULL
1159eb44820cSRob Landley * @sdevp: probe the LUN corresponding to this scsi_device
11601d645088SHannes Reinecke * @rescan: if not equal to SCSI_SCAN_INITIAL skip some code only
11611d645088SHannes Reinecke * needed on first scan
1162eb44820cSRob Landley * @hostdata: passed to scsi_alloc_sdev()
11631da177e4SLinus Torvalds *
11641da177e4SLinus Torvalds * Description:
11651da177e4SLinus Torvalds * Call scsi_probe_lun, if a LUN with an attached device is found,
11661da177e4SLinus Torvalds * allocate and set it up by calling scsi_add_lun.
11671da177e4SLinus Torvalds *
11681da177e4SLinus Torvalds * Return:
1169739aca06SMauro Carvalho Chehab *
1170739aca06SMauro Carvalho Chehab * - SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
1171739aca06SMauro Carvalho Chehab * - SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
11721da177e4SLinus Torvalds * attached at the LUN
1173739aca06SMauro Carvalho Chehab * - SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
11741da177e4SLinus Torvalds **/
scsi_probe_and_add_lun(struct scsi_target * starget,u64 lun,blist_flags_t * bflagsp,struct scsi_device ** sdevp,enum scsi_scan_mode rescan,void * hostdata)11751da177e4SLinus Torvalds static int scsi_probe_and_add_lun(struct scsi_target *starget,
1176093b8886SBart Van Assche u64 lun, blist_flags_t *bflagsp,
11771d645088SHannes Reinecke struct scsi_device **sdevp,
11781d645088SHannes Reinecke enum scsi_scan_mode rescan,
11791da177e4SLinus Torvalds void *hostdata)
11801da177e4SLinus Torvalds {
11811da177e4SLinus Torvalds struct scsi_device *sdev;
11821da177e4SLinus Torvalds unsigned char *result;
1183093b8886SBart Van Assche blist_flags_t bflags;
1184093b8886SBart Van Assche int res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
11851da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
11861da177e4SLinus Torvalds
11871da177e4SLinus Torvalds /*
11881da177e4SLinus Torvalds * The rescan flag is used as an optimization, the first scan of a
11891da177e4SLinus Torvalds * host adapter calls into here with rescan == 0.
11901da177e4SLinus Torvalds */
11911da177e4SLinus Torvalds sdev = scsi_device_lookup_by_target(starget, lun);
11921da177e4SLinus Torvalds if (sdev) {
11931d645088SHannes Reinecke if (rescan != SCSI_SCAN_INITIAL || !scsi_device_created(sdev)) {
119491921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
11951da177e4SLinus Torvalds "scsi scan: device exists on %s\n",
119671610f55SKay Sievers dev_name(&sdev->sdev_gendev)));
11971da177e4SLinus Torvalds if (sdevp)
11981da177e4SLinus Torvalds *sdevp = sdev;
11991da177e4SLinus Torvalds else
12001da177e4SLinus Torvalds scsi_device_put(sdev);
12011da177e4SLinus Torvalds
12021da177e4SLinus Torvalds if (bflagsp)
12031da177e4SLinus Torvalds *bflagsp = scsi_get_device_flags(sdev,
12041da177e4SLinus Torvalds sdev->vendor,
12051da177e4SLinus Torvalds sdev->model);
12061da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT;
12071da177e4SLinus Torvalds }
12086f3a2024SJames Bottomley scsi_device_put(sdev);
12096f3a2024SJames Bottomley } else
12101da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, lun, hostdata);
12111da177e4SLinus Torvalds if (!sdev)
12121da177e4SLinus Torvalds goto out;
121339216033SJames Bottomley
1214aaff5ebaSChristoph Hellwig result = kmalloc(result_len, GFP_KERNEL);
12151da177e4SLinus Torvalds if (!result)
121639216033SJames Bottomley goto out_free_sdev;
12171da177e4SLinus Torvalds
121839216033SJames Bottomley if (scsi_probe_lun(sdev, result, result_len, &bflags))
12191da177e4SLinus Torvalds goto out_free_result;
12201da177e4SLinus Torvalds
12214186ab19SKurt Garloff if (bflagsp)
12224186ab19SKurt Garloff *bflagsp = bflags;
12231da177e4SLinus Torvalds /*
12241da177e4SLinus Torvalds * result contains valid SCSI INQUIRY data.
12251da177e4SLinus Torvalds */
1226496c91bbSChristoph Hellwig if ((result[0] >> 5) == 3) {
12271da177e4SLinus Torvalds /*
12281da177e4SLinus Torvalds * For a Peripheral qualifier 3 (011b), the SCSI
12291da177e4SLinus Torvalds * spec says: The device server is not capable of
12301da177e4SLinus Torvalds * supporting a physical device on this logical
12311da177e4SLinus Torvalds * unit.
12321da177e4SLinus Torvalds *
12331da177e4SLinus Torvalds * For disks, this implies that there is no
12341da177e4SLinus Torvalds * logical disk configured at sdev->lun, but there
12351da177e4SLinus Torvalds * is a target id responding.
12361da177e4SLinus Torvalds */
12376c7154c9SKurt Garloff SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
12386c7154c9SKurt Garloff " peripheral qualifier of 3, device not"
12396c7154c9SKurt Garloff " added\n"))
12406c7154c9SKurt Garloff if (lun == 0) {
1241c5f2e640Sakpm@osdl.org SCSI_LOG_SCAN_BUS(1, {
1242c5f2e640Sakpm@osdl.org unsigned char vend[9];
1243c5f2e640Sakpm@osdl.org unsigned char mod[17];
1244c5f2e640Sakpm@osdl.org
1245c5f2e640Sakpm@osdl.org sdev_printk(KERN_INFO, sdev,
12466c7154c9SKurt Garloff "scsi scan: consider passing scsi_mod."
12473424a65dSKurt Garloff "dev_flags=%s:%s:0x240 or 0x1000240\n",
12486c7154c9SKurt Garloff scsi_inq_str(vend, result, 8, 16),
1249c5f2e640Sakpm@osdl.org scsi_inq_str(mod, result, 16, 32));
1250c5f2e640Sakpm@osdl.org });
1251643eb2d9SJames Bottomley
12526c7154c9SKurt Garloff }
12536c7154c9SKurt Garloff
12541da177e4SLinus Torvalds res = SCSI_SCAN_TARGET_PRESENT;
12551da177e4SLinus Torvalds goto out_free_result;
12561da177e4SLinus Torvalds }
12571da177e4SLinus Torvalds
12581bfc5d9dSAlan Stern /*
125984961f28Sdave wysochanski * Some targets may set slight variations of PQ and PDT to signal
126084961f28Sdave wysochanski * that no LUN is present, so don't add sdev in these cases.
126184961f28Sdave wysochanski * Two specific examples are:
126284961f28Sdave wysochanski * 1) NetApp targets: return PQ=1, PDT=0x1f
126315600159SMartin K. Petersen * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
126484961f28Sdave wysochanski * in the UFI 1.0 spec (we cannot rely on reserved bits).
126584961f28Sdave wysochanski *
126684961f28Sdave wysochanski * References:
126784961f28Sdave wysochanski * 1) SCSI SPC-3, pp. 145-146
126884961f28Sdave wysochanski * PQ=1: "A peripheral device having the specified peripheral
126984961f28Sdave wysochanski * device type is not connected to this logical unit. However, the
127084961f28Sdave wysochanski * device server is capable of supporting the specified peripheral
127184961f28Sdave wysochanski * device type on this logical unit."
127284961f28Sdave wysochanski * PDT=0x1f: "Unknown or no device type"
127384961f28Sdave wysochanski * 2) USB UFI 1.0, p. 20
127484961f28Sdave wysochanski * PDT=00h Direct-access device (floppy)
127584961f28Sdave wysochanski * PDT=1Fh none (no FDD connected to the requested logical unit)
12761bfc5d9dSAlan Stern */
127715600159SMartin K. Petersen if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
127815600159SMartin K. Petersen (result[0] & 0x1f) == 0x1f &&
127901b291bdSJames Bottomley !scsi_is_wlun(lun)) {
128091921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
12811bfc5d9dSAlan Stern "scsi scan: peripheral device type"
12821bfc5d9dSAlan Stern " of 31, no device added\n"));
12831bfc5d9dSAlan Stern res = SCSI_SCAN_TARGET_PRESENT;
12841bfc5d9dSAlan Stern goto out_free_result;
12851bfc5d9dSAlan Stern }
12861bfc5d9dSAlan Stern
12873e082a91SMatthew Wilcox res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
12881da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) {
12891da177e4SLinus Torvalds if (bflags & BLIST_KEY) {
12901da177e4SLinus Torvalds sdev->lockable = 0;
129139216033SJames Bottomley scsi_unlock_floptical(sdev, result);
12921da177e4SLinus Torvalds }
12931da177e4SLinus Torvalds }
12941da177e4SLinus Torvalds
12951da177e4SLinus Torvalds out_free_result:
12961da177e4SLinus Torvalds kfree(result);
12971da177e4SLinus Torvalds out_free_sdev:
12981da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) {
12991da177e4SLinus Torvalds if (sdevp) {
1300b70d37bfSAlan Stern if (scsi_device_get(sdev) == 0) {
13011da177e4SLinus Torvalds *sdevp = sdev;
1302b70d37bfSAlan Stern } else {
1303b70d37bfSAlan Stern __scsi_remove_device(sdev);
1304b70d37bfSAlan Stern res = SCSI_SCAN_NO_RESPONSE;
1305b70d37bfSAlan Stern }
13061da177e4SLinus Torvalds }
13076f3a2024SJames Bottomley } else
1308860dc736SJames Bottomley __scsi_remove_device(sdev);
13091da177e4SLinus Torvalds out:
13101da177e4SLinus Torvalds return res;
13111da177e4SLinus Torvalds }
13121da177e4SLinus Torvalds
13131da177e4SLinus Torvalds /**
13141da177e4SLinus Torvalds * scsi_sequential_lun_scan - sequentially scan a SCSI target
13151da177e4SLinus Torvalds * @starget: pointer to target structure to scan
13161da177e4SLinus Torvalds * @bflags: black/white list flag for LUN 0
1317eb44820cSRob Landley * @scsi_level: Which version of the standard does this device adhere to
1318eb44820cSRob Landley * @rescan: passed to scsi_probe_add_lun()
13191da177e4SLinus Torvalds *
13201da177e4SLinus Torvalds * Description:
13211da177e4SLinus Torvalds * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
13221da177e4SLinus Torvalds * scanned) to some maximum lun until a LUN is found with no device
13231da177e4SLinus Torvalds * attached. Use the bflags to figure out any oddities.
13241da177e4SLinus Torvalds *
13251da177e4SLinus Torvalds * Modifies sdevscan->lun.
13261da177e4SLinus Torvalds **/
scsi_sequential_lun_scan(struct scsi_target * starget,blist_flags_t bflags,int scsi_level,enum scsi_scan_mode rescan)13271da177e4SLinus Torvalds static void scsi_sequential_lun_scan(struct scsi_target *starget,
1328093b8886SBart Van Assche blist_flags_t bflags, int scsi_level,
13291d645088SHannes Reinecke enum scsi_scan_mode rescan)
13301da177e4SLinus Torvalds {
13319cb78c16SHannes Reinecke uint max_dev_lun;
13329cb78c16SHannes Reinecke u64 sparse_lun, lun;
13331da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
13341da177e4SLinus Torvalds
133591921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, starget_printk(KERN_INFO, starget,
133691921e01SHannes Reinecke "scsi scan: Sequential scan\n"));
13371da177e4SLinus Torvalds
13381da177e4SLinus Torvalds max_dev_lun = min(max_scsi_luns, shost->max_lun);
13391da177e4SLinus Torvalds /*
13401da177e4SLinus Torvalds * If this device is known to support sparse multiple units,
13411da177e4SLinus Torvalds * override the other settings, and scan all of them. Normally,
13421da177e4SLinus Torvalds * SCSI-3 devices should be scanned via the REPORT LUNS.
13431da177e4SLinus Torvalds */
13441da177e4SLinus Torvalds if (bflags & BLIST_SPARSELUN) {
13451da177e4SLinus Torvalds max_dev_lun = shost->max_lun;
13461da177e4SLinus Torvalds sparse_lun = 1;
13471da177e4SLinus Torvalds } else
13481da177e4SLinus Torvalds sparse_lun = 0;
13491da177e4SLinus Torvalds
13501da177e4SLinus Torvalds /*
1351fb0d82f4SMark Knibbs * If less than SCSI_1_CCS, and no special lun scanning, stop
13521da177e4SLinus Torvalds * scanning; this matches 2.4 behaviour, but could just be a bug
1353fb0d82f4SMark Knibbs * (to continue scanning a SCSI_1_CCS device).
13541da177e4SLinus Torvalds *
13551da177e4SLinus Torvalds * This test is broken. We might not have any device on lun0 for
13561da177e4SLinus Torvalds * a sparselun device, and if that's the case then how would we
13571da177e4SLinus Torvalds * know the real scsi_level, eh? It might make sense to just not
13581da177e4SLinus Torvalds * scan any SCSI_1 device for non-0 luns, but that check would best
13591da177e4SLinus Torvalds * go into scsi_alloc_sdev() and just have it return null when asked
13601da177e4SLinus Torvalds * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
13611da177e4SLinus Torvalds *
13621da177e4SLinus Torvalds if ((sdevscan->scsi_level < SCSI_1_CCS) &&
13631da177e4SLinus Torvalds ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
13641da177e4SLinus Torvalds == 0))
13651da177e4SLinus Torvalds return;
13661da177e4SLinus Torvalds */
13671da177e4SLinus Torvalds /*
13681da177e4SLinus Torvalds * If this device is known to support multiple units, override
13691da177e4SLinus Torvalds * the other settings, and scan all of them.
13701da177e4SLinus Torvalds */
13711da177e4SLinus Torvalds if (bflags & BLIST_FORCELUN)
13721da177e4SLinus Torvalds max_dev_lun = shost->max_lun;
13731da177e4SLinus Torvalds /*
13741da177e4SLinus Torvalds * REGAL CDC-4X: avoid hang after LUN 4
13751da177e4SLinus Torvalds */
13761da177e4SLinus Torvalds if (bflags & BLIST_MAX5LUN)
13771da177e4SLinus Torvalds max_dev_lun = min(5U, max_dev_lun);
13781da177e4SLinus Torvalds /*
13791da177e4SLinus Torvalds * Do not scan SCSI-2 or lower device past LUN 7, unless
13801da177e4SLinus Torvalds * BLIST_LARGELUN.
13811da177e4SLinus Torvalds */
13821da177e4SLinus Torvalds if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
13831da177e4SLinus Torvalds max_dev_lun = min(8U, max_dev_lun);
1384496c91bbSChristoph Hellwig else
138522ffeb48SHannes Reinecke max_dev_lun = min(256U, max_dev_lun);
138622ffeb48SHannes Reinecke
138722ffeb48SHannes Reinecke /*
13881da177e4SLinus Torvalds * We have already scanned LUN 0, so start at LUN 1. Keep scanning
13891da177e4SLinus Torvalds * until we reach the max, or no LUN is found and we are not
13901da177e4SLinus Torvalds * sparse_lun.
13911da177e4SLinus Torvalds */
13921da177e4SLinus Torvalds for (lun = 1; lun < max_dev_lun; ++lun)
13931da177e4SLinus Torvalds if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
13941da177e4SLinus Torvalds NULL) != SCSI_SCAN_LUN_PRESENT) &&
13951da177e4SLinus Torvalds !sparse_lun)
13961da177e4SLinus Torvalds return;
13971da177e4SLinus Torvalds }
13981da177e4SLinus Torvalds
13991da177e4SLinus Torvalds /**
14001da177e4SLinus Torvalds * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1401eb44820cSRob Landley * @starget: which target
1402eb44820cSRob Landley * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN
1403eb44820cSRob Landley * @rescan: nonzero if we can skip code only needed on first scan
14041da177e4SLinus Torvalds *
14051da177e4SLinus Torvalds * Description:
1406eb44820cSRob Landley * Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command.
1407eb44820cSRob Landley * Scan the resulting list of LUNs by calling scsi_probe_and_add_lun.
14081da177e4SLinus Torvalds *
1409eb44820cSRob Landley * If BLINK_REPORTLUN2 is set, scan a target that supports more than 8
1410eb44820cSRob Landley * LUNs even if it's older than SCSI-3.
1411eb44820cSRob Landley * If BLIST_NOREPORTLUN is set, return 1 always.
1412eb44820cSRob Landley * If BLIST_NOLUN is set, return 0 always.
141309b6b51bSAlan Stern * If starget->no_report_luns is set, return 1 always.
14141da177e4SLinus Torvalds *
14151da177e4SLinus Torvalds * Return:
14161da177e4SLinus Torvalds * 0: scan completed (or no memory, so further scanning is futile)
1417eb44820cSRob Landley * 1: could not scan with REPORT LUN
14181da177e4SLinus Torvalds **/
scsi_report_lun_scan(struct scsi_target * starget,blist_flags_t bflags,enum scsi_scan_mode rescan)1419093b8886SBart Van Assche static int scsi_report_lun_scan(struct scsi_target *starget, blist_flags_t bflags,
14201d645088SHannes Reinecke enum scsi_scan_mode rescan)
14211da177e4SLinus Torvalds {
14221da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE];
14231da177e4SLinus Torvalds unsigned int length;
14249cb78c16SHannes Reinecke u64 lun;
14251da177e4SLinus Torvalds unsigned int num_luns;
142639216033SJames Bottomley int result;
14271da177e4SLinus Torvalds struct scsi_lun *lunp, *lun_data;
14286f3a2024SJames Bottomley struct scsi_device *sdev;
14296f3a2024SJames Bottomley struct Scsi_Host *shost = dev_to_shost(&starget->dev);
14308d24677eSMike Christie struct scsi_failure failure_defs[] = {
14318d24677eSMike Christie {
14328d24677eSMike Christie .sense = UNIT_ATTENTION,
14338d24677eSMike Christie .asc = SCMD_FAILURE_ASC_ANY,
14348d24677eSMike Christie .ascq = SCMD_FAILURE_ASCQ_ANY,
14358d24677eSMike Christie .result = SAM_STAT_CHECK_CONDITION,
14368d24677eSMike Christie },
14378d24677eSMike Christie /* Fail all CCs except the UA above */
14388d24677eSMike Christie {
14398d24677eSMike Christie .sense = SCMD_FAILURE_SENSE_ANY,
14408d24677eSMike Christie .result = SAM_STAT_CHECK_CONDITION,
14418d24677eSMike Christie },
14428d24677eSMike Christie /* Retry any other errors not listed above */
14438d24677eSMike Christie {
14448d24677eSMike Christie .result = SCMD_FAILURE_RESULT_ANY,
14458d24677eSMike Christie },
14468d24677eSMike Christie {}
14478d24677eSMike Christie };
14488d24677eSMike Christie struct scsi_failures failures = {
14498d24677eSMike Christie .total_allowed = 3,
14508d24677eSMike Christie .failure_definitions = failure_defs,
14518d24677eSMike Christie };
14527dfe0b5eSMike Christie const struct scsi_exec_args exec_args = {
14538d24677eSMike Christie .failures = &failures,
14547dfe0b5eSMike Christie };
14552ef89198SAlan Stern int ret = 0;
14561da177e4SLinus Torvalds
14571da177e4SLinus Torvalds /*
14581da177e4SLinus Torvalds * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
14591da177e4SLinus Torvalds * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
14601da177e4SLinus Torvalds * support more than 8 LUNs.
146109b6b51bSAlan Stern * Don't attempt if the target doesn't support REPORT LUNS.
14621da177e4SLinus Torvalds */
14634d7db04aSJames Bottomley if (bflags & BLIST_NOREPORTLUN)
14644d7db04aSJames Bottomley return 1;
14654d7db04aSJames Bottomley if (starget->scsi_level < SCSI_2 &&
14664d7db04aSJames Bottomley starget->scsi_level != SCSI_UNKNOWN)
14674d7db04aSJames Bottomley return 1;
14684d7db04aSJames Bottomley if (starget->scsi_level < SCSI_3 &&
14694d7db04aSJames Bottomley (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
14701da177e4SLinus Torvalds return 1;
14711da177e4SLinus Torvalds if (bflags & BLIST_NOLUN)
14721da177e4SLinus Torvalds return 0;
147309b6b51bSAlan Stern if (starget->no_report_luns)
147409b6b51bSAlan Stern return 1;
14751da177e4SLinus Torvalds
14766f3a2024SJames Bottomley if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
14776f3a2024SJames Bottomley sdev = scsi_alloc_sdev(starget, 0, NULL);
14786f3a2024SJames Bottomley if (!sdev)
14796f3a2024SJames Bottomley return 0;
148075f8ee8eSAlan Stern if (scsi_device_get(sdev)) {
148175f8ee8eSAlan Stern __scsi_remove_device(sdev);
14826f3a2024SJames Bottomley return 0;
14836f3a2024SJames Bottomley }
148475f8ee8eSAlan Stern }
14856f3a2024SJames Bottomley
14861da177e4SLinus Torvalds /*
14871da177e4SLinus Torvalds * Allocate enough to hold the header (the same size as one scsi_lun)
1488acd6d738SRob Evers * plus the number of luns we are requesting. 511 was the default
1489acd6d738SRob Evers * value of the now removed max_report_luns parameter.
14901da177e4SLinus Torvalds */
1491acd6d738SRob Evers length = (511 + 1) * sizeof(struct scsi_lun);
1492acd6d738SRob Evers retry:
1493aaff5ebaSChristoph Hellwig lun_data = kmalloc(length, GFP_KERNEL);
14946f3a2024SJames Bottomley if (!lun_data) {
1495cadbd4a5SHarvey Harrison printk(ALLOC_FAILURE_MSG, __func__);
149639216033SJames Bottomley goto out;
14976f3a2024SJames Bottomley }
14981da177e4SLinus Torvalds
14991da177e4SLinus Torvalds scsi_cmd[0] = REPORT_LUNS;
15001da177e4SLinus Torvalds
15011da177e4SLinus Torvalds /*
15021da177e4SLinus Torvalds * bytes 1 - 5: reserved, set to zero.
15031da177e4SLinus Torvalds */
15041da177e4SLinus Torvalds memset(&scsi_cmd[1], 0, 5);
15051da177e4SLinus Torvalds
15061da177e4SLinus Torvalds /*
15071da177e4SLinus Torvalds * bytes 6 - 9: length of the command.
15081da177e4SLinus Torvalds */
15092a904e5dSRob Evers put_unaligned_be32(length, &scsi_cmd[6]);
15101da177e4SLinus Torvalds
15111da177e4SLinus Torvalds scsi_cmd[10] = 0; /* reserved */
15121da177e4SLinus Torvalds scsi_cmd[11] = 0; /* control */
15131da177e4SLinus Torvalds
15141da177e4SLinus Torvalds /*
15151da177e4SLinus Torvalds * We can get a UNIT ATTENTION, for example a power on/reset, so
15161da177e4SLinus Torvalds * retry a few times (like sd.c does for TEST UNIT READY).
15171da177e4SLinus Torvalds * Experience shows some combinations of adapter/devices get at
15181da177e4SLinus Torvalds * least two power on/resets.
15191da177e4SLinus Torvalds *
15201da177e4SLinus Torvalds * Illegal requests (for devices that do not support REPORT LUNS)
15211da177e4SLinus Torvalds * should come through as a check condition, and will not generate
15221da177e4SLinus Torvalds * a retry.
15231da177e4SLinus Torvalds */
15248d24677eSMike Christie scsi_failures_reset_retries(&failures);
152539216033SJames Bottomley
15268d24677eSMike Christie SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
15278d24677eSMike Christie "scsi scan: Sending REPORT LUNS\n"));
15288d24677eSMike Christie
15298d24677eSMike Christie result = scsi_execute_cmd(sdev, scsi_cmd, REQ_OP_DRV_IN, lun_data,
15308d24677eSMike Christie length, SCSI_REPORT_LUNS_TIMEOUT, 3,
15317dfe0b5eSMike Christie &exec_args);
153239216033SJames Bottomley
153391921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
15348d24677eSMike Christie "scsi scan: REPORT LUNS %s result 0x%x\n",
15358d24677eSMike Christie result ? "failed" : "successful", result));
153639216033SJames Bottomley if (result) {
15371da177e4SLinus Torvalds /*
15381da177e4SLinus Torvalds * The device probably does not support a REPORT LUN command
15391da177e4SLinus Torvalds */
15402ef89198SAlan Stern ret = 1;
15412ef89198SAlan Stern goto out_err;
15421da177e4SLinus Torvalds }
15431da177e4SLinus Torvalds
15441da177e4SLinus Torvalds /*
15451da177e4SLinus Torvalds * Get the length from the first four bytes of lun_data.
15461da177e4SLinus Torvalds */
1547acd6d738SRob Evers if (get_unaligned_be32(lun_data->scsi_lun) +
1548acd6d738SRob Evers sizeof(struct scsi_lun) > length) {
1549acd6d738SRob Evers length = get_unaligned_be32(lun_data->scsi_lun) +
1550acd6d738SRob Evers sizeof(struct scsi_lun);
1551acd6d738SRob Evers kfree(lun_data);
1552acd6d738SRob Evers goto retry;
1553acd6d738SRob Evers }
15542a904e5dSRob Evers length = get_unaligned_be32(lun_data->scsi_lun);
15551da177e4SLinus Torvalds
15561da177e4SLinus Torvalds num_luns = (length / sizeof(struct scsi_lun));
15571da177e4SLinus Torvalds
15583bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
15593bf743e7SJeff Garzik "scsi scan: REPORT LUN scan\n"));
15601da177e4SLinus Torvalds
15611da177e4SLinus Torvalds /*
15621da177e4SLinus Torvalds * Scan the luns in lun_data. The entry at offset 0 is really
15631da177e4SLinus Torvalds * the header, so start at 1 and go up to and including num_luns.
15641da177e4SLinus Torvalds */
15651da177e4SLinus Torvalds for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
15661da177e4SLinus Torvalds lun = scsilun_to_int(lunp);
15671da177e4SLinus Torvalds
15689cb78c16SHannes Reinecke if (lun > sdev->host->max_lun) {
156991921e01SHannes Reinecke sdev_printk(KERN_WARNING, sdev,
157091921e01SHannes Reinecke "lun%llu has a LUN larger than"
157191921e01SHannes Reinecke " allowed by the host adapter\n", lun);
15721da177e4SLinus Torvalds } else {
15731da177e4SLinus Torvalds int res;
15741da177e4SLinus Torvalds
15751da177e4SLinus Torvalds res = scsi_probe_and_add_lun(starget,
15761da177e4SLinus Torvalds lun, NULL, NULL, rescan, NULL);
15771da177e4SLinus Torvalds if (res == SCSI_SCAN_NO_RESPONSE) {
15781da177e4SLinus Torvalds /*
15791da177e4SLinus Torvalds * Got some results, but now none, abort.
15801da177e4SLinus Torvalds */
15813bf743e7SJeff Garzik sdev_printk(KERN_ERR, sdev,
15823bf743e7SJeff Garzik "Unexpected response"
15839cb78c16SHannes Reinecke " from lun %llu while scanning, scan"
15849cb78c16SHannes Reinecke " aborted\n", (unsigned long long)lun);
15851da177e4SLinus Torvalds break;
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds }
15881da177e4SLinus Torvalds }
15891da177e4SLinus Torvalds
15902ef89198SAlan Stern out_err:
15911da177e4SLinus Torvalds kfree(lun_data);
15921da177e4SLinus Torvalds out:
15930f1d87a2SJames Bottomley if (scsi_device_created(sdev))
15941da177e4SLinus Torvalds /*
15956f3a2024SJames Bottomley * the sdev we used didn't appear in the report luns scan
15961da177e4SLinus Torvalds */
1597860dc736SJames Bottomley __scsi_remove_device(sdev);
1598bcd8f2e9SMing Lei scsi_device_put(sdev);
15992ef89198SAlan Stern return ret;
16001da177e4SLinus Torvalds }
16011da177e4SLinus Torvalds
__scsi_add_device(struct Scsi_Host * shost,uint channel,uint id,u64 lun,void * hostdata)16021da177e4SLinus Torvalds struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
16039cb78c16SHannes Reinecke uint id, u64 lun, void *hostdata)
16041da177e4SLinus Torvalds {
1605a97a83a0SMatthew Wilcox struct scsi_device *sdev = ERR_PTR(-ENODEV);
16061da177e4SLinus Torvalds struct device *parent = &shost->shost_gendev;
1607e02f3f59SChristoph Hellwig struct scsi_target *starget;
16081da177e4SLinus Torvalds
1609938e2ac0SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0)
1610938e2ac0SMatthew Wilcox return ERR_PTR(-ENODEV);
1611938e2ac0SMatthew Wilcox
1612e02f3f59SChristoph Hellwig starget = scsi_alloc_target(parent, channel, id);
16131da177e4SLinus Torvalds if (!starget)
16141da177e4SLinus Torvalds return ERR_PTR(-ENOMEM);
1615bc4f2401SAlan Stern scsi_autopm_get_target(starget);
16161da177e4SLinus Torvalds
16170b950672SArjan van de Ven mutex_lock(&shost->scan_mutex);
16186b7f123fSMatthew Wilcox if (!shost->async_scan)
16196b7f123fSMatthew Wilcox scsi_complete_async_scans();
16206b7f123fSMatthew Wilcox
1621bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
162235bd6f9fSJohn Garry scsi_probe_and_add_lun(starget, lun, NULL, &sdev,
162335bd6f9fSJohn Garry SCSI_SCAN_RESCAN, hostdata);
1624bc4f2401SAlan Stern scsi_autopm_put_host(shost);
1625bc4f2401SAlan Stern }
16260b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex);
1627bc4f2401SAlan Stern scsi_autopm_put_target(starget);
1628e63ed0d7SJames Bottomley /*
1629e63ed0d7SJames Bottomley * paired with scsi_alloc_target(). Target will be destroyed unless
1630e63ed0d7SJames Bottomley * scsi_probe_and_add_lun made an underlying device visible
1631e63ed0d7SJames Bottomley */
16321da177e4SLinus Torvalds scsi_target_reap(starget);
16331da177e4SLinus Torvalds put_device(&starget->dev);
16341da177e4SLinus Torvalds
16351da177e4SLinus Torvalds return sdev;
16361da177e4SLinus Torvalds }
16371da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_add_device);
16381da177e4SLinus Torvalds
scsi_add_device(struct Scsi_Host * host,uint channel,uint target,u64 lun)1639146f7262SJames Bottomley int scsi_add_device(struct Scsi_Host *host, uint channel,
16409cb78c16SHannes Reinecke uint target, u64 lun)
1641146f7262SJames Bottomley {
1642146f7262SJames Bottomley struct scsi_device *sdev =
1643146f7262SJames Bottomley __scsi_add_device(host, channel, target, lun, NULL);
1644146f7262SJames Bottomley if (IS_ERR(sdev))
1645146f7262SJames Bottomley return PTR_ERR(sdev);
1646146f7262SJames Bottomley
1647146f7262SJames Bottomley scsi_device_put(sdev);
1648146f7262SJames Bottomley return 0;
1649146f7262SJames Bottomley }
1650146f7262SJames Bottomley EXPORT_SYMBOL(scsi_add_device);
1651146f7262SJames Bottomley
scsi_resume_device(struct scsi_device * sdev)16520c76106cSDamien Le Moal int scsi_resume_device(struct scsi_device *sdev)
16530c76106cSDamien Le Moal {
16540c76106cSDamien Le Moal struct device *dev = &sdev->sdev_gendev;
16550c76106cSDamien Le Moal int ret = 0;
16560c76106cSDamien Le Moal
16570c76106cSDamien Le Moal device_lock(dev);
16580c76106cSDamien Le Moal
16590c76106cSDamien Le Moal /*
16600c76106cSDamien Le Moal * Bail out if the device or its queue are not running. Otherwise,
16610c76106cSDamien Le Moal * the rescan may block waiting for commands to be executed, with us
16620c76106cSDamien Le Moal * holding the device lock. This can result in a potential deadlock
16630c76106cSDamien Le Moal * in the power management core code when system resume is on-going.
16640c76106cSDamien Le Moal */
16650c76106cSDamien Le Moal if (sdev->sdev_state != SDEV_RUNNING ||
16660c76106cSDamien Le Moal blk_queue_pm_only(sdev->request_queue)) {
16670c76106cSDamien Le Moal ret = -EWOULDBLOCK;
16680c76106cSDamien Le Moal goto unlock;
16690c76106cSDamien Le Moal }
16700c76106cSDamien Le Moal
16710c76106cSDamien Le Moal if (dev->driver && try_module_get(dev->driver->owner)) {
16720c76106cSDamien Le Moal struct scsi_driver *drv = to_scsi_driver(dev->driver);
16730c76106cSDamien Le Moal
16740c76106cSDamien Le Moal if (drv->resume)
16750c76106cSDamien Le Moal ret = drv->resume(dev);
16760c76106cSDamien Le Moal module_put(dev->driver->owner);
16770c76106cSDamien Le Moal }
16780c76106cSDamien Le Moal
16790c76106cSDamien Le Moal unlock:
16800c76106cSDamien Le Moal device_unlock(dev);
16810c76106cSDamien Le Moal
16820c76106cSDamien Le Moal return ret;
16830c76106cSDamien Le Moal }
16840c76106cSDamien Le Moal EXPORT_SYMBOL(scsi_resume_device);
16850c76106cSDamien Le Moal
scsi_rescan_device(struct scsi_device * sdev)1686ff48b378SDamien Le Moal int scsi_rescan_device(struct scsi_device *sdev)
16871da177e4SLinus Torvalds {
168879519528SBart Van Assche struct device *dev = &sdev->sdev_gendev;
1689ff48b378SDamien Le Moal int ret = 0;
1690d3d32891SHannes Reinecke
1691e27829dcSChristoph Hellwig device_lock(dev);
169209e2b0b1SHannes Reinecke
1693ff48b378SDamien Le Moal /*
1694626b13f0SDamien Le Moal * Bail out if the device or its queue are not running. Otherwise,
1695626b13f0SDamien Le Moal * the rescan may block waiting for commands to be executed, with us
1696626b13f0SDamien Le Moal * holding the device lock. This can result in a potential deadlock
1697626b13f0SDamien Le Moal * in the power management core code when system resume is on-going.
1698ff48b378SDamien Le Moal */
1699626b13f0SDamien Le Moal if (sdev->sdev_state != SDEV_RUNNING ||
1700626b13f0SDamien Le Moal blk_queue_pm_only(sdev->request_queue)) {
1701ff48b378SDamien Le Moal ret = -EWOULDBLOCK;
1702ff48b378SDamien Le Moal goto unlock;
1703ff48b378SDamien Le Moal }
1704ff48b378SDamien Le Moal
1705d3d32891SHannes Reinecke scsi_attach_vpd(sdev);
170662488520SDamien Le Moal scsi_cdl_check(sdev);
1707d3d32891SHannes Reinecke
1708d3d32891SHannes Reinecke if (sdev->handler && sdev->handler->rescan)
1709d3d32891SHannes Reinecke sdev->handler->rescan(sdev);
171009e2b0b1SHannes Reinecke
1711e27829dcSChristoph Hellwig if (dev->driver && try_module_get(dev->driver->owner)) {
17123af6b352SChristoph Hellwig struct scsi_driver *drv = to_scsi_driver(dev->driver);
17133af6b352SChristoph Hellwig
17141da177e4SLinus Torvalds if (drv->rescan)
17151da177e4SLinus Torvalds drv->rescan(dev);
17163af6b352SChristoph Hellwig module_put(dev->driver->owner);
17171da177e4SLinus Torvalds }
1718ff48b378SDamien Le Moal
1719ff48b378SDamien Le Moal unlock:
1720e27829dcSChristoph Hellwig device_unlock(dev);
1721ff48b378SDamien Le Moal
1722ff48b378SDamien Le Moal return ret;
17231da177e4SLinus Torvalds }
17241da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_rescan_device);
17251da177e4SLinus Torvalds
__scsi_scan_target(struct device * parent,unsigned int channel,unsigned int id,u64 lun,enum scsi_scan_mode rescan)1726e517d313SAlan Stern static void __scsi_scan_target(struct device *parent, unsigned int channel,
17271d645088SHannes Reinecke unsigned int id, u64 lun, enum scsi_scan_mode rescan)
17281da177e4SLinus Torvalds {
17291da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent);
1730093b8886SBart Van Assche blist_flags_t bflags = 0;
17311da177e4SLinus Torvalds int res;
17321da177e4SLinus Torvalds struct scsi_target *starget;
17331da177e4SLinus Torvalds
17341da177e4SLinus Torvalds if (shost->this_id == id)
17351da177e4SLinus Torvalds /*
17361da177e4SLinus Torvalds * Don't scan the host adapter
17371da177e4SLinus Torvalds */
17381da177e4SLinus Torvalds return;
17391da177e4SLinus Torvalds
17401da177e4SLinus Torvalds starget = scsi_alloc_target(parent, channel, id);
17411da177e4SLinus Torvalds if (!starget)
17421da177e4SLinus Torvalds return;
1743bc4f2401SAlan Stern scsi_autopm_get_target(starget);
17441da177e4SLinus Torvalds
17451da177e4SLinus Torvalds if (lun != SCAN_WILD_CARD) {
17461da177e4SLinus Torvalds /*
17471da177e4SLinus Torvalds * Scan for a specific host/chan/id/lun.
17481da177e4SLinus Torvalds */
17491da177e4SLinus Torvalds scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
17501da177e4SLinus Torvalds goto out_reap;
17511da177e4SLinus Torvalds }
17521da177e4SLinus Torvalds
17531da177e4SLinus Torvalds /*
17541da177e4SLinus Torvalds * Scan LUN 0, if there is some response, scan further. Ideally, we
17551da177e4SLinus Torvalds * would not configure LUN 0 until all LUNs are scanned.
17561da177e4SLinus Torvalds */
17576f3a2024SJames Bottomley res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
17586f3a2024SJames Bottomley if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
17596f3a2024SJames Bottomley if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
17601da177e4SLinus Torvalds /*
17611da177e4SLinus Torvalds * The REPORT LUN did not scan the target,
17621da177e4SLinus Torvalds * do a sequential scan.
17631da177e4SLinus Torvalds */
17641da177e4SLinus Torvalds scsi_sequential_lun_scan(starget, bflags,
17654186ab19SKurt Garloff starget->scsi_level, rescan);
17661da177e4SLinus Torvalds }
17671da177e4SLinus Torvalds
17681da177e4SLinus Torvalds out_reap:
1769bc4f2401SAlan Stern scsi_autopm_put_target(starget);
1770e63ed0d7SJames Bottomley /*
1771e63ed0d7SJames Bottomley * paired with scsi_alloc_target(): determine if the target has
1772e63ed0d7SJames Bottomley * any children at all and if not, nuke it
1773e63ed0d7SJames Bottomley */
17741da177e4SLinus Torvalds scsi_target_reap(starget);
17751da177e4SLinus Torvalds
17761da177e4SLinus Torvalds put_device(&starget->dev);
17771da177e4SLinus Torvalds }
1778e517d313SAlan Stern
1779e517d313SAlan Stern /**
1780e59e4a09SRandy Dunlap * scsi_scan_target - scan a target id, possibly including all LUNs on the target.
1781e517d313SAlan Stern * @parent: host to scan
1782e517d313SAlan Stern * @channel: channel to scan
1783e517d313SAlan Stern * @id: target id to scan
1784e517d313SAlan Stern * @lun: Specific LUN to scan or SCAN_WILD_CARD
17851d645088SHannes Reinecke * @rescan: passed to LUN scanning routines; SCSI_SCAN_INITIAL for
17861d645088SHannes Reinecke * no rescan, SCSI_SCAN_RESCAN to rescan existing LUNs,
17871d645088SHannes Reinecke * and SCSI_SCAN_MANUAL to force scanning even if
17881d645088SHannes Reinecke * 'scan=manual' is set.
1789e517d313SAlan Stern *
1790e517d313SAlan Stern * Description:
1791e517d313SAlan Stern * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1792e517d313SAlan Stern * and possibly all LUNs on the target id.
1793e517d313SAlan Stern *
1794e517d313SAlan Stern * First try a REPORT LUN scan, if that does not scan the target, do a
1795e517d313SAlan Stern * sequential scan of LUNs on the target id.
1796e517d313SAlan Stern **/
scsi_scan_target(struct device * parent,unsigned int channel,unsigned int id,u64 lun,enum scsi_scan_mode rescan)1797e517d313SAlan Stern void scsi_scan_target(struct device *parent, unsigned int channel,
17981d645088SHannes Reinecke unsigned int id, u64 lun, enum scsi_scan_mode rescan)
1799e517d313SAlan Stern {
1800e517d313SAlan Stern struct Scsi_Host *shost = dev_to_shost(parent);
1801e517d313SAlan Stern
180293b45af5SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0)
180393b45af5SMatthew Wilcox return;
180493b45af5SMatthew Wilcox
18051d645088SHannes Reinecke if (rescan != SCSI_SCAN_MANUAL &&
18061d645088SHannes Reinecke strncmp(scsi_scan_type, "manual", 6) == 0)
18071d645088SHannes Reinecke return;
18081d645088SHannes Reinecke
18096b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex);
18103e082a91SMatthew Wilcox if (!shost->async_scan)
18113e082a91SMatthew Wilcox scsi_complete_async_scans();
18123e082a91SMatthew Wilcox
1813bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1814e517d313SAlan Stern __scsi_scan_target(parent, channel, id, lun, rescan);
1815bc4f2401SAlan Stern scsi_autopm_put_host(shost);
1816bc4f2401SAlan Stern }
18170b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex);
1818e517d313SAlan Stern }
18191da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_target);
18201da177e4SLinus Torvalds
scsi_scan_channel(struct Scsi_Host * shost,unsigned int channel,unsigned int id,u64 lun,enum scsi_scan_mode rescan)18211da177e4SLinus Torvalds static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
18221d645088SHannes Reinecke unsigned int id, u64 lun,
18231d645088SHannes Reinecke enum scsi_scan_mode rescan)
18241da177e4SLinus Torvalds {
18251da177e4SLinus Torvalds uint order_id;
18261da177e4SLinus Torvalds
18271da177e4SLinus Torvalds if (id == SCAN_WILD_CARD)
18281da177e4SLinus Torvalds for (id = 0; id < shost->max_id; ++id) {
18291da177e4SLinus Torvalds /*
18301da177e4SLinus Torvalds * XXX adapter drivers when possible (FCP, iSCSI)
18311da177e4SLinus Torvalds * could modify max_id to match the current max,
18321da177e4SLinus Torvalds * not the absolute max.
18331da177e4SLinus Torvalds *
18341da177e4SLinus Torvalds * XXX add a shost id iterator, so for example,
18351da177e4SLinus Torvalds * the FC ID can be the same as a target id
18361da177e4SLinus Torvalds * without a huge overhead of sparse id's.
18371da177e4SLinus Torvalds */
18381da177e4SLinus Torvalds if (shost->reverse_ordering)
18391da177e4SLinus Torvalds /*
18401da177e4SLinus Torvalds * Scan from high to low id.
18411da177e4SLinus Torvalds */
18421da177e4SLinus Torvalds order_id = shost->max_id - id - 1;
18431da177e4SLinus Torvalds else
18441da177e4SLinus Torvalds order_id = id;
1845e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel,
1846e517d313SAlan Stern order_id, lun, rescan);
18471da177e4SLinus Torvalds }
18481da177e4SLinus Torvalds else
1849e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel,
1850e517d313SAlan Stern id, lun, rescan);
18511da177e4SLinus Torvalds }
18521da177e4SLinus Torvalds
scsi_scan_host_selected(struct Scsi_Host * shost,unsigned int channel,unsigned int id,u64 lun,enum scsi_scan_mode rescan)18531da177e4SLinus Torvalds int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
18541d645088SHannes Reinecke unsigned int id, u64 lun,
18551d645088SHannes Reinecke enum scsi_scan_mode rescan)
18561da177e4SLinus Torvalds {
18573bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
18589cb78c16SHannes Reinecke "%s: <%u:%u:%llu>\n",
1859cadbd4a5SHarvey Harrison __func__, channel, id, lun));
18601da177e4SLinus Torvalds
18611da177e4SLinus Torvalds if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1862091686d3SAmit Arora ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1863605c6dbeSMark Knibbs ((lun != SCAN_WILD_CARD) && (lun >= shost->max_lun)))
18641da177e4SLinus Torvalds return -EINVAL;
18651da177e4SLinus Torvalds
18660b950672SArjan van de Ven mutex_lock(&shost->scan_mutex);
18676b7f123fSMatthew Wilcox if (!shost->async_scan)
18686b7f123fSMatthew Wilcox scsi_complete_async_scans();
18696b7f123fSMatthew Wilcox
1870bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
18711da177e4SLinus Torvalds if (channel == SCAN_WILD_CARD)
187282f29467SMike Anderson for (channel = 0; channel <= shost->max_channel;
187382f29467SMike Anderson channel++)
187482f29467SMike Anderson scsi_scan_channel(shost, channel, id, lun,
187582f29467SMike Anderson rescan);
18761da177e4SLinus Torvalds else
18771da177e4SLinus Torvalds scsi_scan_channel(shost, channel, id, lun, rescan);
1878bc4f2401SAlan Stern scsi_autopm_put_host(shost);
187982f29467SMike Anderson }
18800b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex);
18811da177e4SLinus Torvalds
18821da177e4SLinus Torvalds return 0;
18831da177e4SLinus Torvalds }
18841da177e4SLinus Torvalds
scsi_sysfs_add_devices(struct Scsi_Host * shost)18853e082a91SMatthew Wilcox static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
18863e082a91SMatthew Wilcox {
18873e082a91SMatthew Wilcox struct scsi_device *sdev;
18883e082a91SMatthew Wilcox shost_for_each_device(sdev, shost) {
18893b661a92SDan Williams /* target removed before the device could be added */
18903b661a92SDan Williams if (sdev->sdev_state == SDEV_DEL)
18913b661a92SDan Williams continue;
1892693ad5baSSubhash Jadavani /* If device is already visible, skip adding it to sysfs */
1893693ad5baSSubhash Jadavani if (sdev->is_visible)
1894693ad5baSSubhash Jadavani continue;
18956b7f123fSMatthew Wilcox if (!scsi_host_scan_allowed(shost) ||
18966b7f123fSMatthew Wilcox scsi_sysfs_add_sdev(sdev) != 0)
1897860dc736SJames Bottomley __scsi_remove_device(sdev);
18983e082a91SMatthew Wilcox }
18993e082a91SMatthew Wilcox }
19003e082a91SMatthew Wilcox
19013e082a91SMatthew Wilcox /**
19023e082a91SMatthew Wilcox * scsi_prep_async_scan - prepare for an async scan
19033e082a91SMatthew Wilcox * @shost: the host which will be scanned
19043e082a91SMatthew Wilcox * Returns: a cookie to be passed to scsi_finish_async_scan()
19053e082a91SMatthew Wilcox *
19063e082a91SMatthew Wilcox * Tells the midlayer this host is going to do an asynchronous scan.
19073e082a91SMatthew Wilcox * It reserves the host's position in the scanning list and ensures
19083e082a91SMatthew Wilcox * that other asynchronous scans started after this one won't affect the
19093e082a91SMatthew Wilcox * ordering of the discovered devices.
19103e082a91SMatthew Wilcox */
scsi_prep_async_scan(struct Scsi_Host * shost)19111aa8fab2SMatthew Wilcox static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
19123e082a91SMatthew Wilcox {
1913831e3405SMing Lei struct async_scan_data *data = NULL;
19146b7f123fSMatthew Wilcox unsigned long flags;
19153e082a91SMatthew Wilcox
19163e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "sync", 4) == 0)
19173e082a91SMatthew Wilcox return NULL;
19183e082a91SMatthew Wilcox
1919831e3405SMing Lei mutex_lock(&shost->scan_mutex);
19203e082a91SMatthew Wilcox if (shost->async_scan) {
1921a4cf30e1SVitaly Kuznetsov shost_printk(KERN_DEBUG, shost, "%s called twice\n", __func__);
1922831e3405SMing Lei goto err;
19233e082a91SMatthew Wilcox }
19243e082a91SMatthew Wilcox
19253e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL);
19263e082a91SMatthew Wilcox if (!data)
19273e082a91SMatthew Wilcox goto err;
19283e082a91SMatthew Wilcox data->shost = scsi_host_get(shost);
19293e082a91SMatthew Wilcox if (!data->shost)
19303e082a91SMatthew Wilcox goto err;
19313e082a91SMatthew Wilcox init_completion(&data->prev_finished);
19323e082a91SMatthew Wilcox
19336b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags);
19343e082a91SMatthew Wilcox shost->async_scan = 1;
19356b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags);
19366b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex);
19376b7f123fSMatthew Wilcox
19386b7f123fSMatthew Wilcox spin_lock(&async_scan_lock);
19393e082a91SMatthew Wilcox if (list_empty(&scanning_hosts))
19403e082a91SMatthew Wilcox complete(&data->prev_finished);
19413e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts);
19423e082a91SMatthew Wilcox spin_unlock(&async_scan_lock);
19433e082a91SMatthew Wilcox
19443e082a91SMatthew Wilcox return data;
19453e082a91SMatthew Wilcox
19463e082a91SMatthew Wilcox err:
1947831e3405SMing Lei mutex_unlock(&shost->scan_mutex);
19483e082a91SMatthew Wilcox kfree(data);
19493e082a91SMatthew Wilcox return NULL;
19503e082a91SMatthew Wilcox }
19513e082a91SMatthew Wilcox
19523e082a91SMatthew Wilcox /**
19533e082a91SMatthew Wilcox * scsi_finish_async_scan - asynchronous scan has finished
19543e082a91SMatthew Wilcox * @data: cookie returned from earlier call to scsi_prep_async_scan()
19553e082a91SMatthew Wilcox *
19563e082a91SMatthew Wilcox * All the devices currently attached to this host have been found.
19573e082a91SMatthew Wilcox * This function announces all the devices it has found to the rest
19583e082a91SMatthew Wilcox * of the system.
19593e082a91SMatthew Wilcox */
scsi_finish_async_scan(struct async_scan_data * data)19601aa8fab2SMatthew Wilcox static void scsi_finish_async_scan(struct async_scan_data *data)
19613e082a91SMatthew Wilcox {
19623e082a91SMatthew Wilcox struct Scsi_Host *shost;
19636b7f123fSMatthew Wilcox unsigned long flags;
19643e082a91SMatthew Wilcox
19653e082a91SMatthew Wilcox if (!data)
19663e082a91SMatthew Wilcox return;
19673e082a91SMatthew Wilcox
19683e082a91SMatthew Wilcox shost = data->shost;
19696b7f123fSMatthew Wilcox
19706b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex);
19716b7f123fSMatthew Wilcox
19723e082a91SMatthew Wilcox if (!shost->async_scan) {
197391921e01SHannes Reinecke shost_printk(KERN_INFO, shost, "%s called twice\n", __func__);
19743e082a91SMatthew Wilcox dump_stack();
1975773e82f6SJulia Lawall mutex_unlock(&shost->scan_mutex);
19763e082a91SMatthew Wilcox return;
19773e082a91SMatthew Wilcox }
19783e082a91SMatthew Wilcox
19793e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished);
19803e082a91SMatthew Wilcox
19813e082a91SMatthew Wilcox scsi_sysfs_add_devices(shost);
19823e082a91SMatthew Wilcox
19836b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags);
19843e082a91SMatthew Wilcox shost->async_scan = 0;
19856b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags);
19866b7f123fSMatthew Wilcox
19876b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex);
19886b7f123fSMatthew Wilcox
19896b7f123fSMatthew Wilcox spin_lock(&async_scan_lock);
19903e082a91SMatthew Wilcox list_del(&data->list);
19913e082a91SMatthew Wilcox if (!list_empty(&scanning_hosts)) {
19923e082a91SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next,
19933e082a91SMatthew Wilcox struct async_scan_data, list);
19943e082a91SMatthew Wilcox complete(&next->prev_finished);
19953e082a91SMatthew Wilcox }
19963e082a91SMatthew Wilcox spin_unlock(&async_scan_lock);
19973e082a91SMatthew Wilcox
1998267a6ad4SHuajun Li scsi_autopm_put_host(shost);
19993e082a91SMatthew Wilcox scsi_host_put(shost);
20003e082a91SMatthew Wilcox kfree(data);
20013e082a91SMatthew Wilcox }
20023e082a91SMatthew Wilcox
do_scsi_scan_host(struct Scsi_Host * shost)20031aa8fab2SMatthew Wilcox static void do_scsi_scan_host(struct Scsi_Host *shost)
20041aa8fab2SMatthew Wilcox {
20051aa8fab2SMatthew Wilcox if (shost->hostt->scan_finished) {
20061aa8fab2SMatthew Wilcox unsigned long start = jiffies;
20071aa8fab2SMatthew Wilcox if (shost->hostt->scan_start)
20081aa8fab2SMatthew Wilcox shost->hostt->scan_start(shost);
20091aa8fab2SMatthew Wilcox
20101aa8fab2SMatthew Wilcox while (!shost->hostt->scan_finished(shost, jiffies - start))
20111aa8fab2SMatthew Wilcox msleep(10);
20121aa8fab2SMatthew Wilcox } else {
20131aa8fab2SMatthew Wilcox scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
2014425b27a0SJohn Garry SCAN_WILD_CARD, SCSI_SCAN_INITIAL);
20151aa8fab2SMatthew Wilcox }
20161aa8fab2SMatthew Wilcox }
20171aa8fab2SMatthew Wilcox
do_scan_async(void * _data,async_cookie_t c)20186cdd5520SDan Williams static void do_scan_async(void *_data, async_cookie_t c)
20193e082a91SMatthew Wilcox {
20203e082a91SMatthew Wilcox struct async_scan_data *data = _data;
2021bc4f2401SAlan Stern struct Scsi_Host *shost = data->shost;
2022bc4f2401SAlan Stern
2023bc4f2401SAlan Stern do_scsi_scan_host(shost);
20243e082a91SMatthew Wilcox scsi_finish_async_scan(data);
20253e082a91SMatthew Wilcox }
20263e082a91SMatthew Wilcox
20271da177e4SLinus Torvalds /**
20281da177e4SLinus Torvalds * scsi_scan_host - scan the given adapter
20291da177e4SLinus Torvalds * @shost: adapter to scan
20301da177e4SLinus Torvalds **/
scsi_scan_host(struct Scsi_Host * shost)20311da177e4SLinus Torvalds void scsi_scan_host(struct Scsi_Host *shost)
20321da177e4SLinus Torvalds {
20333e082a91SMatthew Wilcox struct async_scan_data *data;
20343e082a91SMatthew Wilcox
20351d645088SHannes Reinecke if (strncmp(scsi_scan_type, "none", 4) == 0 ||
20361d645088SHannes Reinecke strncmp(scsi_scan_type, "manual", 6) == 0)
20373e082a91SMatthew Wilcox return;
2038bc4f2401SAlan Stern if (scsi_autopm_get_host(shost) < 0)
2039bc4f2401SAlan Stern return;
20403e082a91SMatthew Wilcox
20413e082a91SMatthew Wilcox data = scsi_prep_async_scan(shost);
20423e082a91SMatthew Wilcox if (!data) {
20431aa8fab2SMatthew Wilcox do_scsi_scan_host(shost);
2044bc4f2401SAlan Stern scsi_autopm_put_host(shost);
20453e082a91SMatthew Wilcox return;
20463e082a91SMatthew Wilcox }
20471aa8fab2SMatthew Wilcox
20486cdd5520SDan Williams /* register with the async subsystem so wait_for_device_probe()
20496cdd5520SDan Williams * will flush this work
20506cdd5520SDan Williams */
20516cdd5520SDan Williams async_schedule(do_scan_async, data);
20526cdd5520SDan Williams
2053267a6ad4SHuajun Li /* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */
20541da177e4SLinus Torvalds }
20551da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_host);
20561da177e4SLinus Torvalds
scsi_forget_host(struct Scsi_Host * shost)20571da177e4SLinus Torvalds void scsi_forget_host(struct Scsi_Host *shost)
20581da177e4SLinus Torvalds {
2059a64358dbSAlan Stern struct scsi_device *sdev;
20601da177e4SLinus Torvalds unsigned long flags;
20611da177e4SLinus Torvalds
2062a64358dbSAlan Stern restart:
20631da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags);
2064a64358dbSAlan Stern list_for_each_entry(sdev, &shost->__devices, siblings) {
2065a64358dbSAlan Stern if (sdev->sdev_state == SDEV_DEL)
2066a64358dbSAlan Stern continue;
20671da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags);
2068a64358dbSAlan Stern __scsi_remove_device(sdev);
2069a64358dbSAlan Stern goto restart;
20701da177e4SLinus Torvalds }
20711da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags);
20721da177e4SLinus Torvalds }
20731da177e4SLinus Torvalds
2074