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 */ 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 */ 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 **/ 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 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 230edb854a3SMing Lei * up one new default queue depth after calling ->slave_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 **/ 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); 2861da177e4SLinus Torvalds 28724669f75SJes Sorensen sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size, 2881749ef00SBenjamin Block GFP_KERNEL); 2891da177e4SLinus Torvalds if (!sdev) 2901da177e4SLinus Torvalds goto out; 2911da177e4SLinus Torvalds 2921da177e4SLinus Torvalds sdev->vendor = scsi_null_device_strs; 2931da177e4SLinus Torvalds sdev->model = scsi_null_device_strs; 2941da177e4SLinus Torvalds sdev->rev = scsi_null_device_strs; 2951da177e4SLinus Torvalds sdev->host = shost; 2964a84067dSVasu Dev sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD; 2971da177e4SLinus Torvalds sdev->id = starget->id; 2981da177e4SLinus Torvalds sdev->lun = lun; 2991da177e4SLinus Torvalds sdev->channel = starget->channel; 3000db6ca8aSBart Van Assche mutex_init(&sdev->state_mutex); 3011da177e4SLinus Torvalds sdev->sdev_state = SDEV_CREATED; 3021da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->siblings); 3031da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->same_target_siblings); 3041da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->starved_entry); 305a341cd0fSJeff Garzik INIT_LIST_HEAD(&sdev->event_list); 3061da177e4SLinus Torvalds spin_lock_init(&sdev->list_lock); 30709e2b0b1SHannes Reinecke mutex_init(&sdev->inquiry_mutex); 308a341cd0fSJeff Garzik INIT_WORK(&sdev->event_work, scsi_evt_thread); 3099937a5e2SJens Axboe INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue); 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds sdev->sdev_gendev.parent = get_device(&starget->dev); 3121da177e4SLinus Torvalds sdev->sdev_target = starget; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds /* usually NULL and set by ->slave_alloc instead */ 3151da177e4SLinus Torvalds sdev->hostdata = hostdata; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* if the device needs this changing, it may do so in the 3181da177e4SLinus Torvalds * slave_configure function */ 3191da177e4SLinus Torvalds sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds /* 3221da177e4SLinus Torvalds * Some low level driver could use device->type 3231da177e4SLinus Torvalds */ 3241da177e4SLinus Torvalds sdev->type = -1; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* 3271da177e4SLinus Torvalds * Assume that the device will have handshaking problems, 3281da177e4SLinus Torvalds * and then fix this field later if it turns out it 3291da177e4SLinus Torvalds * doesn't 3301da177e4SLinus Torvalds */ 3311da177e4SLinus Torvalds sdev->borken = 1; 3321da177e4SLinus Torvalds 3331e61c1a8SChristoph Hellwig sdev->sg_reserved_size = INT_MAX; 3341e61c1a8SChristoph Hellwig 33559506abeSBart Van Assche q = blk_mq_init_queue(&sdev->host->tag_set); 33659506abeSBart Van Assche if (IS_ERR(q)) { 3371da177e4SLinus Torvalds /* release fn is set up in scsi_sysfs_device_initialise, so 3381da177e4SLinus Torvalds * have to free and put manually here */ 3391da177e4SLinus Torvalds put_device(&starget->dev); 34093f56089SDave Jones kfree(sdev); 3411da177e4SLinus Torvalds goto out; 3421da177e4SLinus Torvalds } 3438fe4ce58SBart Van Assche kref_get(&sdev->host->tagset_refcnt); 34459506abeSBart Van Assche sdev->request_queue = q; 34559506abeSBart Van Assche q->queuedata = sdev; 34659506abeSBart Van Assche __scsi_init_queue(sdev->host, 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 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 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 415453cd0f3SAdrian Bunk static struct device_type scsi_target_type = { 416b0ed4336SHannes Reinecke .name = "scsi_target", 417b0ed4336SHannes Reinecke .release = scsi_target_dev_release, 418b0ed4336SHannes Reinecke }; 419b0ed4336SHannes Reinecke 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 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 */ 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 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 */ 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 */ 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 **/ 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 629e5b3cd42SAlan Stern /** 6301da177e4SLinus Torvalds * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY 63139216033SJames Bottomley * @sdev: scsi_device to probe 6321da177e4SLinus Torvalds * @inq_result: area to store the INQUIRY result 63339216033SJames Bottomley * @result_len: len of inq_result 6341da177e4SLinus Torvalds * @bflags: store any bflags found here 6351da177e4SLinus Torvalds * 6361da177e4SLinus Torvalds * Description: 63739216033SJames Bottomley * Probe the lun associated with @req using a standard SCSI INQUIRY; 6381da177e4SLinus Torvalds * 63939216033SJames Bottomley * If the INQUIRY is successful, zero is returned and the 6401da177e4SLinus Torvalds * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length 641f64a181dSChristoph Hellwig * are copied to the scsi_device any flags value is stored in *@bflags. 6421da177e4SLinus Torvalds **/ 643e5b3cd42SAlan Stern static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result, 6445ebde469SHannes Reinecke int result_len, blist_flags_t *bflags) 6451da177e4SLinus Torvalds { 6461da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 6471da177e4SLinus Torvalds int first_inquiry_len, try_inquiry_len, next_inquiry_len; 6481da177e4SLinus Torvalds int response_len = 0; 6497dfe0b5eSMike Christie int pass, count, result, resid; 6501da177e4SLinus Torvalds struct scsi_sense_hdr sshdr; 6517dfe0b5eSMike Christie const struct scsi_exec_args exec_args = { 6527dfe0b5eSMike Christie .sshdr = &sshdr, 6537dfe0b5eSMike Christie .resid = &resid, 6547dfe0b5eSMike Christie }; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds *bflags = 0; 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds /* Perform up to 3 passes. The first pass uses a conservative 6591da177e4SLinus Torvalds * transfer length of 36 unless sdev->inquiry_len specifies a 6601da177e4SLinus Torvalds * different value. */ 6611da177e4SLinus Torvalds first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36; 6621da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len; 6631da177e4SLinus Torvalds pass = 1; 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds next_pass: 6669ccfc756SJames Bottomley SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 6679ccfc756SJames Bottomley "scsi scan: INQUIRY pass %d length %d\n", 6689ccfc756SJames Bottomley pass, try_inquiry_len)); 6691da177e4SLinus Torvalds 6701da177e4SLinus Torvalds /* Each pass gets up to three chances to ignore Unit Attention */ 6711da177e4SLinus Torvalds for (count = 0; count < 3; ++count) { 6721da177e4SLinus Torvalds memset(scsi_cmd, 0, 6); 6731da177e4SLinus Torvalds scsi_cmd[0] = INQUIRY; 6741da177e4SLinus Torvalds scsi_cmd[4] = (unsigned char) try_inquiry_len; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds memset(inq_result, 0, try_inquiry_len); 67739216033SJames Bottomley 6787dfe0b5eSMike Christie result = scsi_execute_cmd(sdev, scsi_cmd, REQ_OP_DRV_IN, 6797dfe0b5eSMike Christie inq_result, try_inquiry_len, 680f4f4e47eSFUJITA Tomonori HZ / 2 + HZ * scsi_inq_timeout, 3, 6817dfe0b5eSMike Christie &exec_args); 6821da177e4SLinus Torvalds 68391921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 68491921e01SHannes Reinecke "scsi scan: INQUIRY %s with code 0x%x\n", 68539216033SJames Bottomley result ? "failed" : "successful", result)); 6861da177e4SLinus Torvalds 687ced202f7SHannes Reinecke if (result > 0) { 6881da177e4SLinus Torvalds /* 6891da177e4SLinus Torvalds * not-ready to ready transition [asc/ascq=0x28/0x0] 6901da177e4SLinus Torvalds * or power-on, reset [asc/ascq=0x29/0x0], continue. 6911da177e4SLinus Torvalds * INQUIRY should not yield UNIT_ATTENTION 6921da177e4SLinus Torvalds * but many buggy devices do so anyway. 6931da177e4SLinus Torvalds */ 694464a00c9SHannes Reinecke if (scsi_status_is_check_condition(result) && 695ea73a9f2SJames Bottomley scsi_sense_valid(&sshdr)) { 6961da177e4SLinus Torvalds if ((sshdr.sense_key == UNIT_ATTENTION) && 6971da177e4SLinus Torvalds ((sshdr.asc == 0x28) || 6981da177e4SLinus Torvalds (sshdr.asc == 0x29)) && 6991da177e4SLinus Torvalds (sshdr.ascq == 0)) 7001da177e4SLinus Torvalds continue; 7011da177e4SLinus Torvalds } 702ced202f7SHannes Reinecke } else if (result == 0) { 7035cd3bbfaSFUJITA Tomonori /* 7045cd3bbfaSFUJITA Tomonori * if nothing was transferred, we try 7055cd3bbfaSFUJITA Tomonori * again. It's a workaround for some USB 7065cd3bbfaSFUJITA Tomonori * devices. 7075cd3bbfaSFUJITA Tomonori */ 7085cd3bbfaSFUJITA Tomonori if (resid == try_inquiry_len) 7095cd3bbfaSFUJITA Tomonori continue; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds break; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 71439216033SJames Bottomley if (result == 0) { 7153846470aSDon Brace scsi_sanitize_inquiry_string(&inq_result[8], 8); 7163846470aSDon Brace scsi_sanitize_inquiry_string(&inq_result[16], 16); 7173846470aSDon Brace scsi_sanitize_inquiry_string(&inq_result[32], 4); 718e5b3cd42SAlan Stern 719e5b3cd42SAlan Stern response_len = inq_result[4] + 5; 7201da177e4SLinus Torvalds if (response_len > 255) 7211da177e4SLinus Torvalds response_len = first_inquiry_len; /* sanity */ 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* 7241da177e4SLinus Torvalds * Get any flags for this device. 7251da177e4SLinus Torvalds * 726f64a181dSChristoph Hellwig * XXX add a bflags to scsi_device, and replace the 727f64a181dSChristoph Hellwig * corresponding bit fields in scsi_device, so bflags 7281da177e4SLinus Torvalds * need not be passed as an argument. 7291da177e4SLinus Torvalds */ 7301da177e4SLinus Torvalds *bflags = scsi_get_device_flags(sdev, &inq_result[8], 7311da177e4SLinus Torvalds &inq_result[16]); 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds /* When the first pass succeeds we gain information about 7341da177e4SLinus Torvalds * what larger transfer lengths might work. */ 7351da177e4SLinus Torvalds if (pass == 1) { 7361da177e4SLinus Torvalds if (BLIST_INQUIRY_36 & *bflags) 7371da177e4SLinus Torvalds next_inquiry_len = 36; 738d657700cSMartin K. Petersen /* 739d657700cSMartin K. Petersen * LLD specified a maximum sdev->inquiry_len 740d657700cSMartin K. Petersen * but device claims it has more data. Capping 741d657700cSMartin K. Petersen * the length only makes sense for legacy 742d657700cSMartin K. Petersen * devices. If a device supports SPC-4 (2014) 743d657700cSMartin K. Petersen * or newer, assume that it is safe to ask for 744d657700cSMartin K. Petersen * as much as the device says it supports. 745d657700cSMartin K. Petersen */ 746d657700cSMartin K. Petersen else if (sdev->inquiry_len && 747d657700cSMartin K. Petersen response_len > sdev->inquiry_len && 748d657700cSMartin K. Petersen (inq_result[2] & 0x7) < 6) /* SPC-4 */ 7491da177e4SLinus Torvalds next_inquiry_len = sdev->inquiry_len; 7501da177e4SLinus Torvalds else 7511da177e4SLinus Torvalds next_inquiry_len = response_len; 7521da177e4SLinus Torvalds 7531da177e4SLinus Torvalds /* If more data is available perform the second pass */ 7541da177e4SLinus Torvalds if (next_inquiry_len > try_inquiry_len) { 7551da177e4SLinus Torvalds try_inquiry_len = next_inquiry_len; 7561da177e4SLinus Torvalds pass = 2; 7571da177e4SLinus Torvalds goto next_pass; 7581da177e4SLinus Torvalds } 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds } else if (pass == 2) { 76291921e01SHannes Reinecke sdev_printk(KERN_INFO, sdev, 76391921e01SHannes Reinecke "scsi scan: %d byte inquiry failed. " 7641da177e4SLinus Torvalds "Consider BLIST_INQUIRY_36 for this device\n", 7651da177e4SLinus Torvalds try_inquiry_len); 7661da177e4SLinus Torvalds 7671da177e4SLinus Torvalds /* If this pass failed, the third pass goes back and transfers 7681da177e4SLinus Torvalds * the same amount as we successfully got in the first pass. */ 7691da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len; 7701da177e4SLinus Torvalds pass = 3; 7711da177e4SLinus Torvalds goto next_pass; 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 7741da177e4SLinus Torvalds /* If the last transfer attempt got an error, assume the 7751da177e4SLinus Torvalds * peripheral doesn't exist or is dead. */ 77639216033SJames Bottomley if (result) 77739216033SJames Bottomley return -EIO; 7781da177e4SLinus Torvalds 7791da177e4SLinus Torvalds /* Don't report any more data than the device says is valid */ 7801da177e4SLinus Torvalds sdev->inquiry_len = min(try_inquiry_len, response_len); 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds /* 7831da177e4SLinus Torvalds * XXX Abort if the response length is less than 36? If less than 7841da177e4SLinus Torvalds * 32, the lookup of the device flags (above) could be invalid, 7851da177e4SLinus Torvalds * and it would be possible to take an incorrect action - we do 7861da177e4SLinus Torvalds * not want to hang because of a short INQUIRY. On the flip side, 7871da177e4SLinus Torvalds * if the device is spun down or becoming ready (and so it gives a 7881da177e4SLinus Torvalds * short INQUIRY), an abort here prevents any further use of the 7891da177e4SLinus Torvalds * device, including spin up. 7901da177e4SLinus Torvalds * 791e423ee31SAlan Stern * On the whole, the best approach seems to be to assume the first 792e423ee31SAlan Stern * 36 bytes are valid no matter what the device says. That's 793e423ee31SAlan Stern * better than copying < 36 bytes to the inquiry-result buffer 794e423ee31SAlan Stern * and displaying garbage for the Vendor, Product, or Revision 795e423ee31SAlan Stern * strings. 796e423ee31SAlan Stern */ 797e423ee31SAlan Stern if (sdev->inquiry_len < 36) { 798a35bb445SVitaly Kuznetsov if (!sdev->host->short_inquiry) { 799a35bb445SVitaly Kuznetsov shost_printk(KERN_INFO, sdev->host, 80091921e01SHannes Reinecke "scsi scan: INQUIRY result too short (%d)," 801e423ee31SAlan Stern " using 36\n", sdev->inquiry_len); 802a35bb445SVitaly Kuznetsov sdev->host->short_inquiry = 1; 803a35bb445SVitaly Kuznetsov } 804e423ee31SAlan Stern sdev->inquiry_len = 36; 805e423ee31SAlan Stern } 806e423ee31SAlan Stern 807e423ee31SAlan Stern /* 8081da177e4SLinus Torvalds * Related to the above issue: 8091da177e4SLinus Torvalds * 8101da177e4SLinus Torvalds * XXX Devices (disk or all?) should be sent a TEST UNIT READY, 8111da177e4SLinus Torvalds * and if not ready, sent a START_STOP to start (maybe spin up) and 8121da177e4SLinus Torvalds * then send the INQUIRY again, since the INQUIRY can change after 8131da177e4SLinus Torvalds * a device is initialized. 8141da177e4SLinus Torvalds * 8151da177e4SLinus Torvalds * Ideally, start a device if explicitly asked to do so. This 8161da177e4SLinus Torvalds * assumes that a device is spun up on power on, spun down on 8171da177e4SLinus Torvalds * request, and then spun up on request. 8181da177e4SLinus Torvalds */ 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds /* 8211da177e4SLinus Torvalds * The scanning code needs to know the scsi_level, even if no 8221da177e4SLinus Torvalds * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so 8231da177e4SLinus Torvalds * non-zero LUNs can be scanned. 8241da177e4SLinus Torvalds */ 825*2132df16SDamien Le Moal sdev->scsi_level = inq_result[2] & 0x0f; 8261da177e4SLinus Torvalds if (sdev->scsi_level >= 2 || 8271da177e4SLinus Torvalds (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1)) 8281da177e4SLinus Torvalds sdev->scsi_level++; 8296f3a2024SJames Bottomley sdev->sdev_target->scsi_level = sdev->scsi_level; 8301da177e4SLinus Torvalds 83150c4e964SAlan Stern /* 83250c4e964SAlan Stern * If SCSI-2 or lower, and if the transport requires it, 83350c4e964SAlan Stern * store the LUN value in CDB[1]. 83450c4e964SAlan Stern */ 83550c4e964SAlan Stern sdev->lun_in_cdb = 0; 83650c4e964SAlan Stern if (sdev->scsi_level <= SCSI_2 && 83750c4e964SAlan Stern sdev->scsi_level != SCSI_UNKNOWN && 83850c4e964SAlan Stern !sdev->host->no_scsi2_lun_in_cdb) 83950c4e964SAlan Stern sdev->lun_in_cdb = 1; 84050c4e964SAlan Stern 84139216033SJames Bottomley return 0; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /** 845f64a181dSChristoph Hellwig * scsi_add_lun - allocate and fully initialze a scsi_device 8466d877688SMatthew Wilcox * @sdev: holds information to be stored in the new scsi_device 8471da177e4SLinus Torvalds * @inq_result: holds the result of a previous INQUIRY to the LUN 8481da177e4SLinus Torvalds * @bflags: black/white list flag 8496d877688SMatthew Wilcox * @async: 1 if this device is being scanned asynchronously 8501da177e4SLinus Torvalds * 8511da177e4SLinus Torvalds * Description: 8526d877688SMatthew Wilcox * Initialize the scsi_device @sdev. Optionally set fields based 8536d877688SMatthew Wilcox * on values in *@bflags. 8541da177e4SLinus Torvalds * 8551da177e4SLinus Torvalds * Return: 856f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 857f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 8581da177e4SLinus Torvalds **/ 859e5b3cd42SAlan Stern static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, 860093b8886SBart Van Assche blist_flags_t *bflags, int async) 8611da177e4SLinus Torvalds { 8626f4267e3SJames Bottomley int ret; 8636f4267e3SJames Bottomley 8641da177e4SLinus Torvalds /* 8651da177e4SLinus Torvalds * XXX do not save the inquiry, since it can change underneath us, 8661da177e4SLinus Torvalds * save just vendor/model/rev. 8671da177e4SLinus Torvalds * 8681da177e4SLinus Torvalds * Rather than save it and have an ioctl that retrieves the saved 8691da177e4SLinus Torvalds * value, have an ioctl that executes the same INQUIRY code used 8701da177e4SLinus Torvalds * in scsi_probe_lun, let user level programs doing INQUIRY 8711da177e4SLinus Torvalds * scanning run at their own risk, or supply a user level program 8721da177e4SLinus Torvalds * that can correctly scan. 8731da177e4SLinus Torvalds */ 8741da177e4SLinus Torvalds 87509123d23SAlan Stern /* 87609123d23SAlan Stern * Copy at least 36 bytes of INQUIRY data, so that we don't 87709123d23SAlan Stern * dereference unallocated memory when accessing the Vendor, 87809123d23SAlan Stern * Product, and Revision strings. Badly behaved devices may set 87909123d23SAlan Stern * the INQUIRY Additional Length byte to a small value, indicating 88009123d23SAlan Stern * these strings are invalid, but often they contain plausible data 88109123d23SAlan Stern * nonetheless. It doesn't matter if the device sent < 36 bytes 88209123d23SAlan Stern * total, since scsi_probe_lun() initializes inq_result with 0s. 88309123d23SAlan Stern */ 88409123d23SAlan Stern sdev->inquiry = kmemdup(inq_result, 88509123d23SAlan Stern max_t(size_t, sdev->inquiry_len, 36), 8861749ef00SBenjamin Block GFP_KERNEL); 88709123d23SAlan Stern if (sdev->inquiry == NULL) 88809123d23SAlan Stern return SCSI_SCAN_NO_RESPONSE; 88909123d23SAlan Stern 8901da177e4SLinus Torvalds sdev->vendor = (char *) (sdev->inquiry + 8); 8911da177e4SLinus Torvalds sdev->model = (char *) (sdev->inquiry + 16); 8921da177e4SLinus Torvalds sdev->rev = (char *) (sdev->inquiry + 32); 8931da177e4SLinus Torvalds 89414216561SJames Bottomley if (strncmp(sdev->vendor, "ATA ", 8) == 0) { 89514216561SJames Bottomley /* 89614216561SJames Bottomley * sata emulation layer device. This is a hack to work around 89714216561SJames Bottomley * the SATL power management specifications which state that 89814216561SJames Bottomley * when the SATL detects the device has gone into standby 89914216561SJames Bottomley * mode, it shall respond with NOT READY. 90014216561SJames Bottomley */ 90114216561SJames Bottomley sdev->allow_restart = 1; 90214216561SJames Bottomley } 90314216561SJames Bottomley 9041da177e4SLinus Torvalds if (*bflags & BLIST_ISROM) { 9056d877688SMatthew Wilcox sdev->type = TYPE_ROM; 9066d877688SMatthew Wilcox sdev->removable = 1; 9076d877688SMatthew Wilcox } else { 9086d877688SMatthew Wilcox sdev->type = (inq_result[0] & 0x1f); 9096d877688SMatthew Wilcox sdev->removable = (inq_result[1] & 0x80) >> 7; 91045341ca3SSubhash Jadavani 91145341ca3SSubhash Jadavani /* 91245341ca3SSubhash Jadavani * some devices may respond with wrong type for 91345341ca3SSubhash Jadavani * well-known logical units. Force well-known type 91445341ca3SSubhash Jadavani * to enumerate them correctly. 91545341ca3SSubhash Jadavani */ 91645341ca3SSubhash Jadavani if (scsi_is_wlun(sdev->lun) && sdev->type != TYPE_WLUN) { 91745341ca3SSubhash Jadavani sdev_printk(KERN_WARNING, sdev, 91845341ca3SSubhash Jadavani "%s: correcting incorrect peripheral device type 0x%x for W-LUN 0x%16xhN\n", 91945341ca3SSubhash Jadavani __func__, sdev->type, (unsigned int)sdev->lun); 92045341ca3SSubhash Jadavani sdev->type = TYPE_WLUN; 92145341ca3SSubhash Jadavani } 92245341ca3SSubhash Jadavani 9236d877688SMatthew Wilcox } 9241da177e4SLinus Torvalds 9256d877688SMatthew Wilcox if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) { 9266d877688SMatthew Wilcox /* RBC and MMC devices can return SCSI-3 compliance and yet 9276d877688SMatthew Wilcox * still not support REPORT LUNS, so make them act as 9286d877688SMatthew Wilcox * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is 9296d877688SMatthew Wilcox * specifically set */ 9306d877688SMatthew Wilcox if ((*bflags & BLIST_REPORTLUN2) == 0) 9316d877688SMatthew Wilcox *bflags |= BLIST_NOREPORTLUN; 9326d877688SMatthew Wilcox } 9336d877688SMatthew Wilcox 9341da177e4SLinus Torvalds /* 9351da177e4SLinus Torvalds * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI 9361da177e4SLinus Torvalds * spec says: The device server is capable of supporting the 9371da177e4SLinus Torvalds * specified peripheral device type on this logical unit. However, 9381da177e4SLinus Torvalds * the physical device is not currently connected to this logical 9391da177e4SLinus Torvalds * unit. 9401da177e4SLinus Torvalds * 9411da177e4SLinus Torvalds * The above is vague, as it implies that we could treat 001 and 9421da177e4SLinus Torvalds * 011 the same. Stay compatible with previous code, and create a 943f64a181dSChristoph Hellwig * scsi_device for a PQ of 1 9441da177e4SLinus Torvalds * 9451da177e4SLinus Torvalds * Don't set the device offline here; rather let the upper 9461da177e4SLinus Torvalds * level drivers eval the PQ to decide whether they should 9471da177e4SLinus Torvalds * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. 9481da177e4SLinus Torvalds */ 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; 9511da177e4SLinus Torvalds sdev->lockable = sdev->removable; 9521da177e4SLinus Torvalds sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); 9531da177e4SLinus Torvalds 9546d877688SMatthew Wilcox if (sdev->scsi_level >= SCSI_3 || 9556d877688SMatthew Wilcox (sdev->inquiry_len > 56 && inq_result[56] & 0x04)) 9561da177e4SLinus Torvalds sdev->ppr = 1; 9571da177e4SLinus Torvalds if (inq_result[7] & 0x60) 9581da177e4SLinus Torvalds sdev->wdtr = 1; 9591da177e4SLinus Torvalds if (inq_result[7] & 0x10) 9601da177e4SLinus Torvalds sdev->sdtr = 1; 9611da177e4SLinus Torvalds 96219ac0db3SJames Bottomley sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d " 9634ff36718SMatthew Wilcox "ANSI: %d%s\n", scsi_device_type(sdev->type), 9644ff36718SMatthew Wilcox sdev->vendor, sdev->model, sdev->rev, 9654ff36718SMatthew Wilcox sdev->inq_periph_qual, inq_result[2] & 0x07, 9664ff36718SMatthew Wilcox (inq_result[3] & 0x0f) == 1 ? " CCS" : ""); 9674ff36718SMatthew Wilcox 9681da177e4SLinus Torvalds if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && 969c8b09f6fSChristoph Hellwig !(*bflags & BLIST_NOTQ)) { 9701da177e4SLinus Torvalds sdev->tagged_supported = 1; 971c8b09f6fSChristoph Hellwig sdev->simple_tags = 1; 972c8b09f6fSChristoph Hellwig } 9736d877688SMatthew Wilcox 9741da177e4SLinus Torvalds /* 9751da177e4SLinus Torvalds * Some devices (Texel CD ROM drives) have handshaking problems 9761da177e4SLinus Torvalds * when used with the Seagate controllers. borken is initialized 9771da177e4SLinus Torvalds * to 1, and then set it to 0 here. 9781da177e4SLinus Torvalds */ 9791da177e4SLinus Torvalds if ((*bflags & BLIST_BORKEN) == 0) 9801da177e4SLinus Torvalds sdev->borken = 0; 9811da177e4SLinus Torvalds 9826d877688SMatthew Wilcox if (*bflags & BLIST_NO_ULD_ATTACH) 9836d877688SMatthew Wilcox sdev->no_uld_attach = 1; 9846d877688SMatthew Wilcox 9851da177e4SLinus Torvalds /* 9861da177e4SLinus Torvalds * Apparently some really broken devices (contrary to the SCSI 9871da177e4SLinus Torvalds * standards) need to be selected without asserting ATN 9881da177e4SLinus Torvalds */ 9891da177e4SLinus Torvalds if (*bflags & BLIST_SELECT_NO_ATN) 9901da177e4SLinus Torvalds sdev->select_no_atn = 1; 9911da177e4SLinus Torvalds 9921da177e4SLinus Torvalds /* 9934d7db04aSJames Bottomley * Maximum 512 sector transfer length 9944d7db04aSJames Bottomley * broken RA4x00 Compaq Disk Array 9954d7db04aSJames Bottomley */ 9964d7db04aSJames Bottomley if (*bflags & BLIST_MAX_512) 997086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(sdev->request_queue, 512); 99835e9a9f9SMike Christie /* 99935e9a9f9SMike Christie * Max 1024 sector transfer length for targets that report incorrect 100035e9a9f9SMike Christie * max/optimal lengths and relied on the old block layer safe default 100135e9a9f9SMike Christie */ 100235e9a9f9SMike Christie else if (*bflags & BLIST_MAX_1024) 100335e9a9f9SMike Christie blk_queue_max_hw_sectors(sdev->request_queue, 1024); 10044d7db04aSJames Bottomley 10054d7db04aSJames Bottomley /* 10061da177e4SLinus Torvalds * Some devices may not want to have a start command automatically 10071da177e4SLinus Torvalds * issued when a device is added. 10081da177e4SLinus Torvalds */ 10091da177e4SLinus Torvalds if (*bflags & BLIST_NOSTARTONADD) 10101da177e4SLinus Torvalds sdev->no_start_on_add = 1; 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds if (*bflags & BLIST_SINGLELUN) 101325d7c363STony Battersby scsi_target(sdev)->single_lun = 1; 10141da177e4SLinus Torvalds 10151da177e4SLinus Torvalds sdev->use_10_for_rw = 1; 10161da177e4SLinus Torvalds 10170213436aSJanusz Dziemidowicz /* some devices don't like REPORT SUPPORTED OPERATION CODES 10180213436aSJanusz Dziemidowicz * and will simply timeout causing sd_mod init to take a very 10190213436aSJanusz Dziemidowicz * very long time */ 10200213436aSJanusz Dziemidowicz if (*bflags & BLIST_NO_RSOC) 10210213436aSJanusz Dziemidowicz sdev->no_report_opcodes = 1; 10220213436aSJanusz Dziemidowicz 10231da177e4SLinus Torvalds /* set the device running here so that slave configure 10241da177e4SLinus Torvalds * may do I/O */ 10250db6ca8aSBart Van Assche mutex_lock(&sdev->state_mutex); 10266f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_RUNNING); 10270db6ca8aSBart Van Assche if (ret) 10286f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_BLOCK); 10290db6ca8aSBart Van Assche mutex_unlock(&sdev->state_mutex); 10306f4267e3SJames Bottomley 10316f4267e3SJames Bottomley if (ret) { 10326f4267e3SJames Bottomley sdev_printk(KERN_ERR, sdev, 10336f4267e3SJames Bottomley "in wrong state %s to complete scan\n", 10346f4267e3SJames Bottomley scsi_device_state_name(sdev->sdev_state)); 10356f4267e3SJames Bottomley return SCSI_SCAN_NO_RESPONSE; 10366f4267e3SJames Bottomley } 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds if (*bflags & BLIST_NOT_LOCKABLE) 10391da177e4SLinus Torvalds sdev->lockable = 0; 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds if (*bflags & BLIST_RETRY_HWERROR) 10421da177e4SLinus Torvalds sdev->retry_hwerror = 1; 10431da177e4SLinus Torvalds 1044d974e426SMartin K. Petersen if (*bflags & BLIST_NO_DIF) 1045d974e426SMartin K. Petersen sdev->no_dif = 1; 1046d974e426SMartin K. Petersen 104728a0bc41SMartin K. Petersen if (*bflags & BLIST_UNMAP_LIMIT_WS) 104828a0bc41SMartin K. Petersen sdev->unmap_limit_for_ws = 1; 104928a0bc41SMartin K. Petersen 1050f591a2e0SMartin Kepplinger if (*bflags & BLIST_IGN_MEDIA_CHANGE) 1051f591a2e0SMartin Kepplinger sdev->ignore_media_change = 1; 1052f591a2e0SMartin Kepplinger 10530816c925SMartin K. Petersen sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT; 10540816c925SMartin K. Petersen 1055c1d40a52SMartin K. Petersen if (*bflags & BLIST_TRY_VPD_PAGES) 1056c1d40a52SMartin K. Petersen sdev->try_vpd_pages = 1; 1057c1d40a52SMartin K. Petersen else if (*bflags & BLIST_SKIP_VPD_PAGES) 105856f2a801SMartin K. Petersen sdev->skip_vpd_pages = 1; 105956f2a801SMartin K. Petersen 10604b1a2c2aSLee Duncan if (*bflags & BLIST_NO_VPD_SIZE) 10614b1a2c2aSLee Duncan sdev->no_vpd_size = 1; 10624b1a2c2aSLee Duncan 10631da177e4SLinus Torvalds transport_configure_device(&sdev->sdev_gendev); 10641da177e4SLinus Torvalds 106593805091SChristoph Hellwig if (sdev->host->hostt->slave_configure) { 10666f4267e3SJames Bottomley ret = sdev->host->hostt->slave_configure(sdev); 106793805091SChristoph Hellwig if (ret) { 106893805091SChristoph Hellwig /* 106993805091SChristoph Hellwig * if LLDD reports slave not present, don't clutter 107093805091SChristoph Hellwig * console with alloc failure messages 107193805091SChristoph Hellwig */ 107293805091SChristoph Hellwig if (ret != -ENXIO) { 107393805091SChristoph Hellwig sdev_printk(KERN_ERR, sdev, 107493805091SChristoph Hellwig "failed to configure device\n"); 107593805091SChristoph Hellwig } 107693805091SChristoph Hellwig return SCSI_SCAN_NO_RESPONSE; 107793805091SChristoph Hellwig } 1078edb854a3SMing Lei 1079edb854a3SMing Lei /* 1080edb854a3SMing Lei * The queue_depth is often changed in ->slave_configure. 1081edb854a3SMing Lei * Set up budget map again since memory consumption of 1082edb854a3SMing Lei * the map depends on actual queue depth. 1083edb854a3SMing Lei */ 1084edb854a3SMing Lei scsi_realloc_sdev_budget_map(sdev, sdev->queue_depth); 108593805091SChristoph Hellwig } 10861da177e4SLinus Torvalds 1087b3ae8780SHannes Reinecke if (sdev->scsi_level >= SCSI_3) 1088b3ae8780SHannes Reinecke scsi_attach_vpd(sdev); 1089b3ae8780SHannes Reinecke 109062488520SDamien Le Moal scsi_cdl_check(sdev); 109162488520SDamien Le Moal 10924a84067dSVasu Dev sdev->max_queue_depth = sdev->queue_depth; 1093020b0f0aSMing Lei WARN_ON_ONCE(sdev->max_queue_depth > sdev->budget_map.depth); 1094345e2960SHannes Reinecke sdev->sdev_bflags = *bflags; 10954a84067dSVasu Dev 10961da177e4SLinus Torvalds /* 10971da177e4SLinus Torvalds * Ok, the device is now all set up, we can 10981da177e4SLinus Torvalds * register it and tell the rest of the kernel 10991da177e4SLinus Torvalds * about it. 11001da177e4SLinus Torvalds */ 11013e082a91SMatthew Wilcox if (!async && scsi_sysfs_add_sdev(sdev) != 0) 1102b24b1033SAlan Stern return SCSI_SCAN_NO_RESPONSE; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 11051da177e4SLinus Torvalds } 11061da177e4SLinus Torvalds 1107c5f2e640Sakpm@osdl.org #ifdef CONFIG_SCSI_LOGGING 11086c7154c9SKurt Garloff /** 1109eb44820cSRob Landley * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace 11106c7154c9SKurt Garloff * @buf: Output buffer with at least end-first+1 bytes of space 11116c7154c9SKurt Garloff * @inq: Inquiry buffer (input) 11126c7154c9SKurt Garloff * @first: Offset of string into inq 11136c7154c9SKurt Garloff * @end: Index after last character in inq 11146c7154c9SKurt Garloff */ 11156c7154c9SKurt Garloff static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq, 11166c7154c9SKurt Garloff unsigned first, unsigned end) 11176c7154c9SKurt Garloff { 11186c7154c9SKurt Garloff unsigned term = 0, idx; 1119c5f2e640Sakpm@osdl.org 1120c5f2e640Sakpm@osdl.org for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) { 1121c5f2e640Sakpm@osdl.org if (inq[idx+first] > ' ') { 11226c7154c9SKurt Garloff buf[idx] = inq[idx+first]; 11236c7154c9SKurt Garloff term = idx+1; 11246c7154c9SKurt Garloff } else { 11256c7154c9SKurt Garloff buf[idx] = ' '; 11266c7154c9SKurt Garloff } 11276c7154c9SKurt Garloff } 11286c7154c9SKurt Garloff buf[term] = 0; 11296c7154c9SKurt Garloff return buf; 11306c7154c9SKurt Garloff } 1131c5f2e640Sakpm@osdl.org #endif 11326f3a2024SJames Bottomley 11331da177e4SLinus Torvalds /** 11341da177e4SLinus Torvalds * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 11351da177e4SLinus Torvalds * @starget: pointer to target device structure 11361da177e4SLinus Torvalds * @lun: LUN of target device 11371da177e4SLinus Torvalds * @bflagsp: store bflags here if not NULL 1138eb44820cSRob Landley * @sdevp: probe the LUN corresponding to this scsi_device 11391d645088SHannes Reinecke * @rescan: if not equal to SCSI_SCAN_INITIAL skip some code only 11401d645088SHannes Reinecke * needed on first scan 1141eb44820cSRob Landley * @hostdata: passed to scsi_alloc_sdev() 11421da177e4SLinus Torvalds * 11431da177e4SLinus Torvalds * Description: 11441da177e4SLinus Torvalds * Call scsi_probe_lun, if a LUN with an attached device is found, 11451da177e4SLinus Torvalds * allocate and set it up by calling scsi_add_lun. 11461da177e4SLinus Torvalds * 11471da177e4SLinus Torvalds * Return: 1148739aca06SMauro Carvalho Chehab * 1149739aca06SMauro Carvalho Chehab * - SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 1150739aca06SMauro Carvalho Chehab * - SCSI_SCAN_TARGET_PRESENT: target responded, but no device is 11511da177e4SLinus Torvalds * attached at the LUN 1152739aca06SMauro Carvalho Chehab * - SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 11531da177e4SLinus Torvalds **/ 11541da177e4SLinus Torvalds static int scsi_probe_and_add_lun(struct scsi_target *starget, 1155093b8886SBart Van Assche u64 lun, blist_flags_t *bflagsp, 11561d645088SHannes Reinecke struct scsi_device **sdevp, 11571d645088SHannes Reinecke enum scsi_scan_mode rescan, 11581da177e4SLinus Torvalds void *hostdata) 11591da177e4SLinus Torvalds { 11601da177e4SLinus Torvalds struct scsi_device *sdev; 11611da177e4SLinus Torvalds unsigned char *result; 1162093b8886SBart Van Assche blist_flags_t bflags; 1163093b8886SBart Van Assche int res = SCSI_SCAN_NO_RESPONSE, result_len = 256; 11641da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds /* 11671da177e4SLinus Torvalds * The rescan flag is used as an optimization, the first scan of a 11681da177e4SLinus Torvalds * host adapter calls into here with rescan == 0. 11691da177e4SLinus Torvalds */ 11701da177e4SLinus Torvalds sdev = scsi_device_lookup_by_target(starget, lun); 11711da177e4SLinus Torvalds if (sdev) { 11721d645088SHannes Reinecke if (rescan != SCSI_SCAN_INITIAL || !scsi_device_created(sdev)) { 117391921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 11741da177e4SLinus Torvalds "scsi scan: device exists on %s\n", 117571610f55SKay Sievers dev_name(&sdev->sdev_gendev))); 11761da177e4SLinus Torvalds if (sdevp) 11771da177e4SLinus Torvalds *sdevp = sdev; 11781da177e4SLinus Torvalds else 11791da177e4SLinus Torvalds scsi_device_put(sdev); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds if (bflagsp) 11821da177e4SLinus Torvalds *bflagsp = scsi_get_device_flags(sdev, 11831da177e4SLinus Torvalds sdev->vendor, 11841da177e4SLinus Torvalds sdev->model); 11851da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 11861da177e4SLinus Torvalds } 11876f3a2024SJames Bottomley scsi_device_put(sdev); 11886f3a2024SJames Bottomley } else 11891da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, lun, hostdata); 11901da177e4SLinus Torvalds if (!sdev) 11911da177e4SLinus Torvalds goto out; 119239216033SJames Bottomley 1193aaff5ebaSChristoph Hellwig result = kmalloc(result_len, GFP_KERNEL); 11941da177e4SLinus Torvalds if (!result) 119539216033SJames Bottomley goto out_free_sdev; 11961da177e4SLinus Torvalds 119739216033SJames Bottomley if (scsi_probe_lun(sdev, result, result_len, &bflags)) 11981da177e4SLinus Torvalds goto out_free_result; 11991da177e4SLinus Torvalds 12004186ab19SKurt Garloff if (bflagsp) 12014186ab19SKurt Garloff *bflagsp = bflags; 12021da177e4SLinus Torvalds /* 12031da177e4SLinus Torvalds * result contains valid SCSI INQUIRY data. 12041da177e4SLinus Torvalds */ 1205496c91bbSChristoph Hellwig if ((result[0] >> 5) == 3) { 12061da177e4SLinus Torvalds /* 12071da177e4SLinus Torvalds * For a Peripheral qualifier 3 (011b), the SCSI 12081da177e4SLinus Torvalds * spec says: The device server is not capable of 12091da177e4SLinus Torvalds * supporting a physical device on this logical 12101da177e4SLinus Torvalds * unit. 12111da177e4SLinus Torvalds * 12121da177e4SLinus Torvalds * For disks, this implies that there is no 12131da177e4SLinus Torvalds * logical disk configured at sdev->lun, but there 12141da177e4SLinus Torvalds * is a target id responding. 12151da177e4SLinus Torvalds */ 12166c7154c9SKurt Garloff SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:" 12176c7154c9SKurt Garloff " peripheral qualifier of 3, device not" 12186c7154c9SKurt Garloff " added\n")) 12196c7154c9SKurt Garloff if (lun == 0) { 1220c5f2e640Sakpm@osdl.org SCSI_LOG_SCAN_BUS(1, { 1221c5f2e640Sakpm@osdl.org unsigned char vend[9]; 1222c5f2e640Sakpm@osdl.org unsigned char mod[17]; 1223c5f2e640Sakpm@osdl.org 1224c5f2e640Sakpm@osdl.org sdev_printk(KERN_INFO, sdev, 12256c7154c9SKurt Garloff "scsi scan: consider passing scsi_mod." 12263424a65dSKurt Garloff "dev_flags=%s:%s:0x240 or 0x1000240\n", 12276c7154c9SKurt Garloff scsi_inq_str(vend, result, 8, 16), 1228c5f2e640Sakpm@osdl.org scsi_inq_str(mod, result, 16, 32)); 1229c5f2e640Sakpm@osdl.org }); 1230643eb2d9SJames Bottomley 12316c7154c9SKurt Garloff } 12326c7154c9SKurt Garloff 12331da177e4SLinus Torvalds res = SCSI_SCAN_TARGET_PRESENT; 12341da177e4SLinus Torvalds goto out_free_result; 12351da177e4SLinus Torvalds } 12361da177e4SLinus Torvalds 12371bfc5d9dSAlan Stern /* 123884961f28Sdave wysochanski * Some targets may set slight variations of PQ and PDT to signal 123984961f28Sdave wysochanski * that no LUN is present, so don't add sdev in these cases. 124084961f28Sdave wysochanski * Two specific examples are: 124184961f28Sdave wysochanski * 1) NetApp targets: return PQ=1, PDT=0x1f 124215600159SMartin K. Petersen * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" 124384961f28Sdave wysochanski * in the UFI 1.0 spec (we cannot rely on reserved bits). 124484961f28Sdave wysochanski * 124584961f28Sdave wysochanski * References: 124684961f28Sdave wysochanski * 1) SCSI SPC-3, pp. 145-146 124784961f28Sdave wysochanski * PQ=1: "A peripheral device having the specified peripheral 124884961f28Sdave wysochanski * device type is not connected to this logical unit. However, the 124984961f28Sdave wysochanski * device server is capable of supporting the specified peripheral 125084961f28Sdave wysochanski * device type on this logical unit." 125184961f28Sdave wysochanski * PDT=0x1f: "Unknown or no device type" 125284961f28Sdave wysochanski * 2) USB UFI 1.0, p. 20 125384961f28Sdave wysochanski * PDT=00h Direct-access device (floppy) 125484961f28Sdave wysochanski * PDT=1Fh none (no FDD connected to the requested logical unit) 12551bfc5d9dSAlan Stern */ 125615600159SMartin K. Petersen if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) && 125715600159SMartin K. Petersen (result[0] & 0x1f) == 0x1f && 125801b291bdSJames Bottomley !scsi_is_wlun(lun)) { 125991921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 12601bfc5d9dSAlan Stern "scsi scan: peripheral device type" 12611bfc5d9dSAlan Stern " of 31, no device added\n")); 12621bfc5d9dSAlan Stern res = SCSI_SCAN_TARGET_PRESENT; 12631bfc5d9dSAlan Stern goto out_free_result; 12641bfc5d9dSAlan Stern } 12651bfc5d9dSAlan Stern 12663e082a91SMatthew Wilcox res = scsi_add_lun(sdev, result, &bflags, shost->async_scan); 12671da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 12681da177e4SLinus Torvalds if (bflags & BLIST_KEY) { 12691da177e4SLinus Torvalds sdev->lockable = 0; 127039216033SJames Bottomley scsi_unlock_floptical(sdev, result); 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds out_free_result: 12751da177e4SLinus Torvalds kfree(result); 12761da177e4SLinus Torvalds out_free_sdev: 12771da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 12781da177e4SLinus Torvalds if (sdevp) { 1279b70d37bfSAlan Stern if (scsi_device_get(sdev) == 0) { 12801da177e4SLinus Torvalds *sdevp = sdev; 1281b70d37bfSAlan Stern } else { 1282b70d37bfSAlan Stern __scsi_remove_device(sdev); 1283b70d37bfSAlan Stern res = SCSI_SCAN_NO_RESPONSE; 1284b70d37bfSAlan Stern } 12851da177e4SLinus Torvalds } 12866f3a2024SJames Bottomley } else 1287860dc736SJames Bottomley __scsi_remove_device(sdev); 12881da177e4SLinus Torvalds out: 12891da177e4SLinus Torvalds return res; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds 12921da177e4SLinus Torvalds /** 12931da177e4SLinus Torvalds * scsi_sequential_lun_scan - sequentially scan a SCSI target 12941da177e4SLinus Torvalds * @starget: pointer to target structure to scan 12951da177e4SLinus Torvalds * @bflags: black/white list flag for LUN 0 1296eb44820cSRob Landley * @scsi_level: Which version of the standard does this device adhere to 1297eb44820cSRob Landley * @rescan: passed to scsi_probe_add_lun() 12981da177e4SLinus Torvalds * 12991da177e4SLinus Torvalds * Description: 13001da177e4SLinus Torvalds * Generally, scan from LUN 1 (LUN 0 is assumed to already have been 13011da177e4SLinus Torvalds * scanned) to some maximum lun until a LUN is found with no device 13021da177e4SLinus Torvalds * attached. Use the bflags to figure out any oddities. 13031da177e4SLinus Torvalds * 13041da177e4SLinus Torvalds * Modifies sdevscan->lun. 13051da177e4SLinus Torvalds **/ 13061da177e4SLinus Torvalds static void scsi_sequential_lun_scan(struct scsi_target *starget, 1307093b8886SBart Van Assche blist_flags_t bflags, int scsi_level, 13081d645088SHannes Reinecke enum scsi_scan_mode rescan) 13091da177e4SLinus Torvalds { 13109cb78c16SHannes Reinecke uint max_dev_lun; 13119cb78c16SHannes Reinecke u64 sparse_lun, lun; 13121da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 13131da177e4SLinus Torvalds 131491921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, starget_printk(KERN_INFO, starget, 131591921e01SHannes Reinecke "scsi scan: Sequential scan\n")); 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds max_dev_lun = min(max_scsi_luns, shost->max_lun); 13181da177e4SLinus Torvalds /* 13191da177e4SLinus Torvalds * If this device is known to support sparse multiple units, 13201da177e4SLinus Torvalds * override the other settings, and scan all of them. Normally, 13211da177e4SLinus Torvalds * SCSI-3 devices should be scanned via the REPORT LUNS. 13221da177e4SLinus Torvalds */ 13231da177e4SLinus Torvalds if (bflags & BLIST_SPARSELUN) { 13241da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 13251da177e4SLinus Torvalds sparse_lun = 1; 13261da177e4SLinus Torvalds } else 13271da177e4SLinus Torvalds sparse_lun = 0; 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds /* 1330fb0d82f4SMark Knibbs * If less than SCSI_1_CCS, and no special lun scanning, stop 13311da177e4SLinus Torvalds * scanning; this matches 2.4 behaviour, but could just be a bug 1332fb0d82f4SMark Knibbs * (to continue scanning a SCSI_1_CCS device). 13331da177e4SLinus Torvalds * 13341da177e4SLinus Torvalds * This test is broken. We might not have any device on lun0 for 13351da177e4SLinus Torvalds * a sparselun device, and if that's the case then how would we 13361da177e4SLinus Torvalds * know the real scsi_level, eh? It might make sense to just not 13371da177e4SLinus Torvalds * scan any SCSI_1 device for non-0 luns, but that check would best 13381da177e4SLinus Torvalds * go into scsi_alloc_sdev() and just have it return null when asked 13391da177e4SLinus Torvalds * to alloc an sdev for lun > 0 on an already found SCSI_1 device. 13401da177e4SLinus Torvalds * 13411da177e4SLinus Torvalds if ((sdevscan->scsi_level < SCSI_1_CCS) && 13421da177e4SLinus Torvalds ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) 13431da177e4SLinus Torvalds == 0)) 13441da177e4SLinus Torvalds return; 13451da177e4SLinus Torvalds */ 13461da177e4SLinus Torvalds /* 13471da177e4SLinus Torvalds * If this device is known to support multiple units, override 13481da177e4SLinus Torvalds * the other settings, and scan all of them. 13491da177e4SLinus Torvalds */ 13501da177e4SLinus Torvalds if (bflags & BLIST_FORCELUN) 13511da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 13521da177e4SLinus Torvalds /* 13531da177e4SLinus Torvalds * REGAL CDC-4X: avoid hang after LUN 4 13541da177e4SLinus Torvalds */ 13551da177e4SLinus Torvalds if (bflags & BLIST_MAX5LUN) 13561da177e4SLinus Torvalds max_dev_lun = min(5U, max_dev_lun); 13571da177e4SLinus Torvalds /* 13581da177e4SLinus Torvalds * Do not scan SCSI-2 or lower device past LUN 7, unless 13591da177e4SLinus Torvalds * BLIST_LARGELUN. 13601da177e4SLinus Torvalds */ 13611da177e4SLinus Torvalds if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) 13621da177e4SLinus Torvalds max_dev_lun = min(8U, max_dev_lun); 1363496c91bbSChristoph Hellwig else 136422ffeb48SHannes Reinecke max_dev_lun = min(256U, max_dev_lun); 136522ffeb48SHannes Reinecke 136622ffeb48SHannes Reinecke /* 13671da177e4SLinus Torvalds * We have already scanned LUN 0, so start at LUN 1. Keep scanning 13681da177e4SLinus Torvalds * until we reach the max, or no LUN is found and we are not 13691da177e4SLinus Torvalds * sparse_lun. 13701da177e4SLinus Torvalds */ 13711da177e4SLinus Torvalds for (lun = 1; lun < max_dev_lun; ++lun) 13721da177e4SLinus Torvalds if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, 13731da177e4SLinus Torvalds NULL) != SCSI_SCAN_LUN_PRESENT) && 13741da177e4SLinus Torvalds !sparse_lun) 13751da177e4SLinus Torvalds return; 13761da177e4SLinus Torvalds } 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds /** 13791da177e4SLinus Torvalds * scsi_report_lun_scan - Scan using SCSI REPORT LUN results 1380eb44820cSRob Landley * @starget: which target 1381eb44820cSRob Landley * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN 1382eb44820cSRob Landley * @rescan: nonzero if we can skip code only needed on first scan 13831da177e4SLinus Torvalds * 13841da177e4SLinus Torvalds * Description: 1385eb44820cSRob Landley * Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command. 1386eb44820cSRob Landley * Scan the resulting list of LUNs by calling scsi_probe_and_add_lun. 13871da177e4SLinus Torvalds * 1388eb44820cSRob Landley * If BLINK_REPORTLUN2 is set, scan a target that supports more than 8 1389eb44820cSRob Landley * LUNs even if it's older than SCSI-3. 1390eb44820cSRob Landley * If BLIST_NOREPORTLUN is set, return 1 always. 1391eb44820cSRob Landley * If BLIST_NOLUN is set, return 0 always. 139209b6b51bSAlan Stern * If starget->no_report_luns is set, return 1 always. 13931da177e4SLinus Torvalds * 13941da177e4SLinus Torvalds * Return: 13951da177e4SLinus Torvalds * 0: scan completed (or no memory, so further scanning is futile) 1396eb44820cSRob Landley * 1: could not scan with REPORT LUN 13971da177e4SLinus Torvalds **/ 1398093b8886SBart Van Assche static int scsi_report_lun_scan(struct scsi_target *starget, blist_flags_t bflags, 13991d645088SHannes Reinecke enum scsi_scan_mode rescan) 14001da177e4SLinus Torvalds { 14011da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 14021da177e4SLinus Torvalds unsigned int length; 14039cb78c16SHannes Reinecke u64 lun; 14041da177e4SLinus Torvalds unsigned int num_luns; 14051da177e4SLinus Torvalds unsigned int retries; 140639216033SJames Bottomley int result; 14071da177e4SLinus Torvalds struct scsi_lun *lunp, *lun_data; 14081da177e4SLinus Torvalds struct scsi_sense_hdr sshdr; 14096f3a2024SJames Bottomley struct scsi_device *sdev; 14106f3a2024SJames Bottomley struct Scsi_Host *shost = dev_to_shost(&starget->dev); 14117dfe0b5eSMike Christie const struct scsi_exec_args exec_args = { 14127dfe0b5eSMike Christie .sshdr = &sshdr, 14137dfe0b5eSMike Christie }; 14142ef89198SAlan Stern int ret = 0; 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds /* 14171da177e4SLinus Torvalds * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set. 14181da177e4SLinus Torvalds * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does 14191da177e4SLinus Torvalds * support more than 8 LUNs. 142009b6b51bSAlan Stern * Don't attempt if the target doesn't support REPORT LUNS. 14211da177e4SLinus Torvalds */ 14224d7db04aSJames Bottomley if (bflags & BLIST_NOREPORTLUN) 14234d7db04aSJames Bottomley return 1; 14244d7db04aSJames Bottomley if (starget->scsi_level < SCSI_2 && 14254d7db04aSJames Bottomley starget->scsi_level != SCSI_UNKNOWN) 14264d7db04aSJames Bottomley return 1; 14274d7db04aSJames Bottomley if (starget->scsi_level < SCSI_3 && 14284d7db04aSJames Bottomley (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) 14291da177e4SLinus Torvalds return 1; 14301da177e4SLinus Torvalds if (bflags & BLIST_NOLUN) 14311da177e4SLinus Torvalds return 0; 143209b6b51bSAlan Stern if (starget->no_report_luns) 143309b6b51bSAlan Stern return 1; 14341da177e4SLinus Torvalds 14356f3a2024SJames Bottomley if (!(sdev = scsi_device_lookup_by_target(starget, 0))) { 14366f3a2024SJames Bottomley sdev = scsi_alloc_sdev(starget, 0, NULL); 14376f3a2024SJames Bottomley if (!sdev) 14386f3a2024SJames Bottomley return 0; 143975f8ee8eSAlan Stern if (scsi_device_get(sdev)) { 144075f8ee8eSAlan Stern __scsi_remove_device(sdev); 14416f3a2024SJames Bottomley return 0; 14426f3a2024SJames Bottomley } 144375f8ee8eSAlan Stern } 14446f3a2024SJames Bottomley 14451da177e4SLinus Torvalds /* 14461da177e4SLinus Torvalds * Allocate enough to hold the header (the same size as one scsi_lun) 1447acd6d738SRob Evers * plus the number of luns we are requesting. 511 was the default 1448acd6d738SRob Evers * value of the now removed max_report_luns parameter. 14491da177e4SLinus Torvalds */ 1450acd6d738SRob Evers length = (511 + 1) * sizeof(struct scsi_lun); 1451acd6d738SRob Evers retry: 1452aaff5ebaSChristoph Hellwig lun_data = kmalloc(length, GFP_KERNEL); 14536f3a2024SJames Bottomley if (!lun_data) { 1454cadbd4a5SHarvey Harrison printk(ALLOC_FAILURE_MSG, __func__); 145539216033SJames Bottomley goto out; 14566f3a2024SJames Bottomley } 14571da177e4SLinus Torvalds 14581da177e4SLinus Torvalds scsi_cmd[0] = REPORT_LUNS; 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds /* 14611da177e4SLinus Torvalds * bytes 1 - 5: reserved, set to zero. 14621da177e4SLinus Torvalds */ 14631da177e4SLinus Torvalds memset(&scsi_cmd[1], 0, 5); 14641da177e4SLinus Torvalds 14651da177e4SLinus Torvalds /* 14661da177e4SLinus Torvalds * bytes 6 - 9: length of the command. 14671da177e4SLinus Torvalds */ 14682a904e5dSRob Evers put_unaligned_be32(length, &scsi_cmd[6]); 14691da177e4SLinus Torvalds 14701da177e4SLinus Torvalds scsi_cmd[10] = 0; /* reserved */ 14711da177e4SLinus Torvalds scsi_cmd[11] = 0; /* control */ 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds /* 14741da177e4SLinus Torvalds * We can get a UNIT ATTENTION, for example a power on/reset, so 14751da177e4SLinus Torvalds * retry a few times (like sd.c does for TEST UNIT READY). 14761da177e4SLinus Torvalds * Experience shows some combinations of adapter/devices get at 14771da177e4SLinus Torvalds * least two power on/resets. 14781da177e4SLinus Torvalds * 14791da177e4SLinus Torvalds * Illegal requests (for devices that do not support REPORT LUNS) 14801da177e4SLinus Torvalds * should come through as a check condition, and will not generate 14811da177e4SLinus Torvalds * a retry. 14821da177e4SLinus Torvalds */ 14831da177e4SLinus Torvalds for (retries = 0; retries < 3; retries++) { 148491921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 148591921e01SHannes Reinecke "scsi scan: Sending REPORT LUNS to (try %d)\n", 14861da177e4SLinus Torvalds retries)); 148739216033SJames Bottomley 14887dfe0b5eSMike Christie result = scsi_execute_cmd(sdev, scsi_cmd, REQ_OP_DRV_IN, 14897dfe0b5eSMike Christie lun_data, length, 14907dfe0b5eSMike Christie SCSI_REPORT_LUNS_TIMEOUT, 3, 14917dfe0b5eSMike Christie &exec_args); 149239216033SJames Bottomley 149391921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 149491921e01SHannes Reinecke "scsi scan: REPORT LUNS" 149591921e01SHannes Reinecke " %s (try %d) result 0x%x\n", 149691921e01SHannes Reinecke result ? "failed" : "successful", 149791921e01SHannes Reinecke retries, result)); 149839216033SJames Bottomley if (result == 0) 14991da177e4SLinus Torvalds break; 1500ea73a9f2SJames Bottomley else if (scsi_sense_valid(&sshdr)) { 15011da177e4SLinus Torvalds if (sshdr.sense_key != UNIT_ATTENTION) 15021da177e4SLinus Torvalds break; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds } 15051da177e4SLinus Torvalds 150639216033SJames Bottomley if (result) { 15071da177e4SLinus Torvalds /* 15081da177e4SLinus Torvalds * The device probably does not support a REPORT LUN command 15091da177e4SLinus Torvalds */ 15102ef89198SAlan Stern ret = 1; 15112ef89198SAlan Stern goto out_err; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds /* 15151da177e4SLinus Torvalds * Get the length from the first four bytes of lun_data. 15161da177e4SLinus Torvalds */ 1517acd6d738SRob Evers if (get_unaligned_be32(lun_data->scsi_lun) + 1518acd6d738SRob Evers sizeof(struct scsi_lun) > length) { 1519acd6d738SRob Evers length = get_unaligned_be32(lun_data->scsi_lun) + 1520acd6d738SRob Evers sizeof(struct scsi_lun); 1521acd6d738SRob Evers kfree(lun_data); 1522acd6d738SRob Evers goto retry; 1523acd6d738SRob Evers } 15242a904e5dSRob Evers length = get_unaligned_be32(lun_data->scsi_lun); 15251da177e4SLinus Torvalds 15261da177e4SLinus Torvalds num_luns = (length / sizeof(struct scsi_lun)); 15271da177e4SLinus Torvalds 15283bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 15293bf743e7SJeff Garzik "scsi scan: REPORT LUN scan\n")); 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds /* 15321da177e4SLinus Torvalds * Scan the luns in lun_data. The entry at offset 0 is really 15331da177e4SLinus Torvalds * the header, so start at 1 and go up to and including num_luns. 15341da177e4SLinus Torvalds */ 15351da177e4SLinus Torvalds for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) { 15361da177e4SLinus Torvalds lun = scsilun_to_int(lunp); 15371da177e4SLinus Torvalds 15389cb78c16SHannes Reinecke if (lun > sdev->host->max_lun) { 153991921e01SHannes Reinecke sdev_printk(KERN_WARNING, sdev, 154091921e01SHannes Reinecke "lun%llu has a LUN larger than" 154191921e01SHannes Reinecke " allowed by the host adapter\n", lun); 15421da177e4SLinus Torvalds } else { 15431da177e4SLinus Torvalds int res; 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds res = scsi_probe_and_add_lun(starget, 15461da177e4SLinus Torvalds lun, NULL, NULL, rescan, NULL); 15471da177e4SLinus Torvalds if (res == SCSI_SCAN_NO_RESPONSE) { 15481da177e4SLinus Torvalds /* 15491da177e4SLinus Torvalds * Got some results, but now none, abort. 15501da177e4SLinus Torvalds */ 15513bf743e7SJeff Garzik sdev_printk(KERN_ERR, sdev, 15523bf743e7SJeff Garzik "Unexpected response" 15539cb78c16SHannes Reinecke " from lun %llu while scanning, scan" 15549cb78c16SHannes Reinecke " aborted\n", (unsigned long long)lun); 15551da177e4SLinus Torvalds break; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds } 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds 15602ef89198SAlan Stern out_err: 15611da177e4SLinus Torvalds kfree(lun_data); 15621da177e4SLinus Torvalds out: 15630f1d87a2SJames Bottomley if (scsi_device_created(sdev)) 15641da177e4SLinus Torvalds /* 15656f3a2024SJames Bottomley * the sdev we used didn't appear in the report luns scan 15661da177e4SLinus Torvalds */ 1567860dc736SJames Bottomley __scsi_remove_device(sdev); 1568bcd8f2e9SMing Lei scsi_device_put(sdev); 15692ef89198SAlan Stern return ret; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds 15721da177e4SLinus Torvalds struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel, 15739cb78c16SHannes Reinecke uint id, u64 lun, void *hostdata) 15741da177e4SLinus Torvalds { 1575a97a83a0SMatthew Wilcox struct scsi_device *sdev = ERR_PTR(-ENODEV); 15761da177e4SLinus Torvalds struct device *parent = &shost->shost_gendev; 1577e02f3f59SChristoph Hellwig struct scsi_target *starget; 15781da177e4SLinus Torvalds 1579938e2ac0SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 1580938e2ac0SMatthew Wilcox return ERR_PTR(-ENODEV); 1581938e2ac0SMatthew Wilcox 1582e02f3f59SChristoph Hellwig starget = scsi_alloc_target(parent, channel, id); 15831da177e4SLinus Torvalds if (!starget) 15841da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 1585bc4f2401SAlan Stern scsi_autopm_get_target(starget); 15861da177e4SLinus Torvalds 15870b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 15886b7f123fSMatthew Wilcox if (!shost->async_scan) 15896b7f123fSMatthew Wilcox scsi_complete_async_scans(); 15906b7f123fSMatthew Wilcox 1591bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 159235bd6f9fSJohn Garry scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 159335bd6f9fSJohn Garry SCSI_SCAN_RESCAN, hostdata); 1594bc4f2401SAlan Stern scsi_autopm_put_host(shost); 1595bc4f2401SAlan Stern } 15960b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 1597bc4f2401SAlan Stern scsi_autopm_put_target(starget); 1598e63ed0d7SJames Bottomley /* 1599e63ed0d7SJames Bottomley * paired with scsi_alloc_target(). Target will be destroyed unless 1600e63ed0d7SJames Bottomley * scsi_probe_and_add_lun made an underlying device visible 1601e63ed0d7SJames Bottomley */ 16021da177e4SLinus Torvalds scsi_target_reap(starget); 16031da177e4SLinus Torvalds put_device(&starget->dev); 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds return sdev; 16061da177e4SLinus Torvalds } 16071da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_add_device); 16081da177e4SLinus Torvalds 1609146f7262SJames Bottomley int scsi_add_device(struct Scsi_Host *host, uint channel, 16109cb78c16SHannes Reinecke uint target, u64 lun) 1611146f7262SJames Bottomley { 1612146f7262SJames Bottomley struct scsi_device *sdev = 1613146f7262SJames Bottomley __scsi_add_device(host, channel, target, lun, NULL); 1614146f7262SJames Bottomley if (IS_ERR(sdev)) 1615146f7262SJames Bottomley return PTR_ERR(sdev); 1616146f7262SJames Bottomley 1617146f7262SJames Bottomley scsi_device_put(sdev); 1618146f7262SJames Bottomley return 0; 1619146f7262SJames Bottomley } 1620146f7262SJames Bottomley EXPORT_SYMBOL(scsi_add_device); 1621146f7262SJames Bottomley 162279519528SBart Van Assche void scsi_rescan_device(struct scsi_device *sdev) 16231da177e4SLinus Torvalds { 162479519528SBart Van Assche struct device *dev = &sdev->sdev_gendev; 1625d3d32891SHannes Reinecke 1626e27829dcSChristoph Hellwig device_lock(dev); 162709e2b0b1SHannes Reinecke 1628d3d32891SHannes Reinecke scsi_attach_vpd(sdev); 162962488520SDamien Le Moal scsi_cdl_check(sdev); 1630d3d32891SHannes Reinecke 1631d3d32891SHannes Reinecke if (sdev->handler && sdev->handler->rescan) 1632d3d32891SHannes Reinecke sdev->handler->rescan(sdev); 163309e2b0b1SHannes Reinecke 1634e27829dcSChristoph Hellwig if (dev->driver && try_module_get(dev->driver->owner)) { 16353af6b352SChristoph Hellwig struct scsi_driver *drv = to_scsi_driver(dev->driver); 16363af6b352SChristoph Hellwig 16371da177e4SLinus Torvalds if (drv->rescan) 16381da177e4SLinus Torvalds drv->rescan(dev); 16393af6b352SChristoph Hellwig module_put(dev->driver->owner); 16401da177e4SLinus Torvalds } 1641e27829dcSChristoph Hellwig device_unlock(dev); 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_rescan_device); 16441da177e4SLinus Torvalds 1645e517d313SAlan Stern static void __scsi_scan_target(struct device *parent, unsigned int channel, 16461d645088SHannes Reinecke unsigned int id, u64 lun, enum scsi_scan_mode rescan) 16471da177e4SLinus Torvalds { 16481da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 1649093b8886SBart Van Assche blist_flags_t bflags = 0; 16501da177e4SLinus Torvalds int res; 16511da177e4SLinus Torvalds struct scsi_target *starget; 16521da177e4SLinus Torvalds 16531da177e4SLinus Torvalds if (shost->this_id == id) 16541da177e4SLinus Torvalds /* 16551da177e4SLinus Torvalds * Don't scan the host adapter 16561da177e4SLinus Torvalds */ 16571da177e4SLinus Torvalds return; 16581da177e4SLinus Torvalds 16591da177e4SLinus Torvalds starget = scsi_alloc_target(parent, channel, id); 16601da177e4SLinus Torvalds if (!starget) 16611da177e4SLinus Torvalds return; 1662bc4f2401SAlan Stern scsi_autopm_get_target(starget); 16631da177e4SLinus Torvalds 16641da177e4SLinus Torvalds if (lun != SCAN_WILD_CARD) { 16651da177e4SLinus Torvalds /* 16661da177e4SLinus Torvalds * Scan for a specific host/chan/id/lun. 16671da177e4SLinus Torvalds */ 16681da177e4SLinus Torvalds scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL); 16691da177e4SLinus Torvalds goto out_reap; 16701da177e4SLinus Torvalds } 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds /* 16731da177e4SLinus Torvalds * Scan LUN 0, if there is some response, scan further. Ideally, we 16741da177e4SLinus Torvalds * would not configure LUN 0 until all LUNs are scanned. 16751da177e4SLinus Torvalds */ 16766f3a2024SJames Bottomley res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL); 16776f3a2024SJames Bottomley if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) { 16786f3a2024SJames Bottomley if (scsi_report_lun_scan(starget, bflags, rescan) != 0) 16791da177e4SLinus Torvalds /* 16801da177e4SLinus Torvalds * The REPORT LUN did not scan the target, 16811da177e4SLinus Torvalds * do a sequential scan. 16821da177e4SLinus Torvalds */ 16831da177e4SLinus Torvalds scsi_sequential_lun_scan(starget, bflags, 16844186ab19SKurt Garloff starget->scsi_level, rescan); 16851da177e4SLinus Torvalds } 16861da177e4SLinus Torvalds 16871da177e4SLinus Torvalds out_reap: 1688bc4f2401SAlan Stern scsi_autopm_put_target(starget); 1689e63ed0d7SJames Bottomley /* 1690e63ed0d7SJames Bottomley * paired with scsi_alloc_target(): determine if the target has 1691e63ed0d7SJames Bottomley * any children at all and if not, nuke it 1692e63ed0d7SJames Bottomley */ 16931da177e4SLinus Torvalds scsi_target_reap(starget); 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds put_device(&starget->dev); 16961da177e4SLinus Torvalds } 1697e517d313SAlan Stern 1698e517d313SAlan Stern /** 1699e59e4a09SRandy Dunlap * scsi_scan_target - scan a target id, possibly including all LUNs on the target. 1700e517d313SAlan Stern * @parent: host to scan 1701e517d313SAlan Stern * @channel: channel to scan 1702e517d313SAlan Stern * @id: target id to scan 1703e517d313SAlan Stern * @lun: Specific LUN to scan or SCAN_WILD_CARD 17041d645088SHannes Reinecke * @rescan: passed to LUN scanning routines; SCSI_SCAN_INITIAL for 17051d645088SHannes Reinecke * no rescan, SCSI_SCAN_RESCAN to rescan existing LUNs, 17061d645088SHannes Reinecke * and SCSI_SCAN_MANUAL to force scanning even if 17071d645088SHannes Reinecke * 'scan=manual' is set. 1708e517d313SAlan Stern * 1709e517d313SAlan Stern * Description: 1710e517d313SAlan Stern * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0, 1711e517d313SAlan Stern * and possibly all LUNs on the target id. 1712e517d313SAlan Stern * 1713e517d313SAlan Stern * First try a REPORT LUN scan, if that does not scan the target, do a 1714e517d313SAlan Stern * sequential scan of LUNs on the target id. 1715e517d313SAlan Stern **/ 1716e517d313SAlan Stern void scsi_scan_target(struct device *parent, unsigned int channel, 17171d645088SHannes Reinecke unsigned int id, u64 lun, enum scsi_scan_mode rescan) 1718e517d313SAlan Stern { 1719e517d313SAlan Stern struct Scsi_Host *shost = dev_to_shost(parent); 1720e517d313SAlan Stern 172193b45af5SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 172293b45af5SMatthew Wilcox return; 172393b45af5SMatthew Wilcox 17241d645088SHannes Reinecke if (rescan != SCSI_SCAN_MANUAL && 17251d645088SHannes Reinecke strncmp(scsi_scan_type, "manual", 6) == 0) 17261d645088SHannes Reinecke return; 17271d645088SHannes Reinecke 17286b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 17293e082a91SMatthew Wilcox if (!shost->async_scan) 17303e082a91SMatthew Wilcox scsi_complete_async_scans(); 17313e082a91SMatthew Wilcox 1732bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1733e517d313SAlan Stern __scsi_scan_target(parent, channel, id, lun, rescan); 1734bc4f2401SAlan Stern scsi_autopm_put_host(shost); 1735bc4f2401SAlan Stern } 17360b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 1737e517d313SAlan Stern } 17381da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_target); 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel, 17411d645088SHannes Reinecke unsigned int id, u64 lun, 17421d645088SHannes Reinecke enum scsi_scan_mode rescan) 17431da177e4SLinus Torvalds { 17441da177e4SLinus Torvalds uint order_id; 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds if (id == SCAN_WILD_CARD) 17471da177e4SLinus Torvalds for (id = 0; id < shost->max_id; ++id) { 17481da177e4SLinus Torvalds /* 17491da177e4SLinus Torvalds * XXX adapter drivers when possible (FCP, iSCSI) 17501da177e4SLinus Torvalds * could modify max_id to match the current max, 17511da177e4SLinus Torvalds * not the absolute max. 17521da177e4SLinus Torvalds * 17531da177e4SLinus Torvalds * XXX add a shost id iterator, so for example, 17541da177e4SLinus Torvalds * the FC ID can be the same as a target id 17551da177e4SLinus Torvalds * without a huge overhead of sparse id's. 17561da177e4SLinus Torvalds */ 17571da177e4SLinus Torvalds if (shost->reverse_ordering) 17581da177e4SLinus Torvalds /* 17591da177e4SLinus Torvalds * Scan from high to low id. 17601da177e4SLinus Torvalds */ 17611da177e4SLinus Torvalds order_id = shost->max_id - id - 1; 17621da177e4SLinus Torvalds else 17631da177e4SLinus Torvalds order_id = id; 1764e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1765e517d313SAlan Stern order_id, lun, rescan); 17661da177e4SLinus Torvalds } 17671da177e4SLinus Torvalds else 1768e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1769e517d313SAlan Stern id, lun, rescan); 17701da177e4SLinus Torvalds } 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel, 17731d645088SHannes Reinecke unsigned int id, u64 lun, 17741d645088SHannes Reinecke enum scsi_scan_mode rescan) 17751da177e4SLinus Torvalds { 17763bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost, 17779cb78c16SHannes Reinecke "%s: <%u:%u:%llu>\n", 1778cadbd4a5SHarvey Harrison __func__, channel, id, lun)); 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || 1781091686d3SAmit Arora ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || 1782605c6dbeSMark Knibbs ((lun != SCAN_WILD_CARD) && (lun >= shost->max_lun))) 17831da177e4SLinus Torvalds return -EINVAL; 17841da177e4SLinus Torvalds 17850b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 17866b7f123fSMatthew Wilcox if (!shost->async_scan) 17876b7f123fSMatthew Wilcox scsi_complete_async_scans(); 17886b7f123fSMatthew Wilcox 1789bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 17901da177e4SLinus Torvalds if (channel == SCAN_WILD_CARD) 179182f29467SMike Anderson for (channel = 0; channel <= shost->max_channel; 179282f29467SMike Anderson channel++) 179382f29467SMike Anderson scsi_scan_channel(shost, channel, id, lun, 179482f29467SMike Anderson rescan); 17951da177e4SLinus Torvalds else 17961da177e4SLinus Torvalds scsi_scan_channel(shost, channel, id, lun, rescan); 1797bc4f2401SAlan Stern scsi_autopm_put_host(shost); 179882f29467SMike Anderson } 17990b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 18001da177e4SLinus Torvalds 18011da177e4SLinus Torvalds return 0; 18021da177e4SLinus Torvalds } 18031da177e4SLinus Torvalds 18043e082a91SMatthew Wilcox static void scsi_sysfs_add_devices(struct Scsi_Host *shost) 18053e082a91SMatthew Wilcox { 18063e082a91SMatthew Wilcox struct scsi_device *sdev; 18073e082a91SMatthew Wilcox shost_for_each_device(sdev, shost) { 18083b661a92SDan Williams /* target removed before the device could be added */ 18093b661a92SDan Williams if (sdev->sdev_state == SDEV_DEL) 18103b661a92SDan Williams continue; 1811693ad5baSSubhash Jadavani /* If device is already visible, skip adding it to sysfs */ 1812693ad5baSSubhash Jadavani if (sdev->is_visible) 1813693ad5baSSubhash Jadavani continue; 18146b7f123fSMatthew Wilcox if (!scsi_host_scan_allowed(shost) || 18156b7f123fSMatthew Wilcox scsi_sysfs_add_sdev(sdev) != 0) 1816860dc736SJames Bottomley __scsi_remove_device(sdev); 18173e082a91SMatthew Wilcox } 18183e082a91SMatthew Wilcox } 18193e082a91SMatthew Wilcox 18203e082a91SMatthew Wilcox /** 18213e082a91SMatthew Wilcox * scsi_prep_async_scan - prepare for an async scan 18223e082a91SMatthew Wilcox * @shost: the host which will be scanned 18233e082a91SMatthew Wilcox * Returns: a cookie to be passed to scsi_finish_async_scan() 18243e082a91SMatthew Wilcox * 18253e082a91SMatthew Wilcox * Tells the midlayer this host is going to do an asynchronous scan. 18263e082a91SMatthew Wilcox * It reserves the host's position in the scanning list and ensures 18273e082a91SMatthew Wilcox * that other asynchronous scans started after this one won't affect the 18283e082a91SMatthew Wilcox * ordering of the discovered devices. 18293e082a91SMatthew Wilcox */ 18301aa8fab2SMatthew Wilcox static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) 18313e082a91SMatthew Wilcox { 1832831e3405SMing Lei struct async_scan_data *data = NULL; 18336b7f123fSMatthew Wilcox unsigned long flags; 18343e082a91SMatthew Wilcox 18353e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "sync", 4) == 0) 18363e082a91SMatthew Wilcox return NULL; 18373e082a91SMatthew Wilcox 1838831e3405SMing Lei mutex_lock(&shost->scan_mutex); 18393e082a91SMatthew Wilcox if (shost->async_scan) { 1840a4cf30e1SVitaly Kuznetsov shost_printk(KERN_DEBUG, shost, "%s called twice\n", __func__); 1841831e3405SMing Lei goto err; 18423e082a91SMatthew Wilcox } 18433e082a91SMatthew Wilcox 18443e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL); 18453e082a91SMatthew Wilcox if (!data) 18463e082a91SMatthew Wilcox goto err; 18473e082a91SMatthew Wilcox data->shost = scsi_host_get(shost); 18483e082a91SMatthew Wilcox if (!data->shost) 18493e082a91SMatthew Wilcox goto err; 18503e082a91SMatthew Wilcox init_completion(&data->prev_finished); 18513e082a91SMatthew Wilcox 18526b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags); 18533e082a91SMatthew Wilcox shost->async_scan = 1; 18546b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags); 18556b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex); 18566b7f123fSMatthew Wilcox 18576b7f123fSMatthew Wilcox spin_lock(&async_scan_lock); 18583e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 18593e082a91SMatthew Wilcox complete(&data->prev_finished); 18603e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts); 18613e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 18623e082a91SMatthew Wilcox 18633e082a91SMatthew Wilcox return data; 18643e082a91SMatthew Wilcox 18653e082a91SMatthew Wilcox err: 1866831e3405SMing Lei mutex_unlock(&shost->scan_mutex); 18673e082a91SMatthew Wilcox kfree(data); 18683e082a91SMatthew Wilcox return NULL; 18693e082a91SMatthew Wilcox } 18703e082a91SMatthew Wilcox 18713e082a91SMatthew Wilcox /** 18723e082a91SMatthew Wilcox * scsi_finish_async_scan - asynchronous scan has finished 18733e082a91SMatthew Wilcox * @data: cookie returned from earlier call to scsi_prep_async_scan() 18743e082a91SMatthew Wilcox * 18753e082a91SMatthew Wilcox * All the devices currently attached to this host have been found. 18763e082a91SMatthew Wilcox * This function announces all the devices it has found to the rest 18773e082a91SMatthew Wilcox * of the system. 18783e082a91SMatthew Wilcox */ 18791aa8fab2SMatthew Wilcox static void scsi_finish_async_scan(struct async_scan_data *data) 18803e082a91SMatthew Wilcox { 18813e082a91SMatthew Wilcox struct Scsi_Host *shost; 18826b7f123fSMatthew Wilcox unsigned long flags; 18833e082a91SMatthew Wilcox 18843e082a91SMatthew Wilcox if (!data) 18853e082a91SMatthew Wilcox return; 18863e082a91SMatthew Wilcox 18873e082a91SMatthew Wilcox shost = data->shost; 18886b7f123fSMatthew Wilcox 18896b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 18906b7f123fSMatthew Wilcox 18913e082a91SMatthew Wilcox if (!shost->async_scan) { 189291921e01SHannes Reinecke shost_printk(KERN_INFO, shost, "%s called twice\n", __func__); 18933e082a91SMatthew Wilcox dump_stack(); 1894773e82f6SJulia Lawall mutex_unlock(&shost->scan_mutex); 18953e082a91SMatthew Wilcox return; 18963e082a91SMatthew Wilcox } 18973e082a91SMatthew Wilcox 18983e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished); 18993e082a91SMatthew Wilcox 19003e082a91SMatthew Wilcox scsi_sysfs_add_devices(shost); 19013e082a91SMatthew Wilcox 19026b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags); 19033e082a91SMatthew Wilcox shost->async_scan = 0; 19046b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags); 19056b7f123fSMatthew Wilcox 19066b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex); 19076b7f123fSMatthew Wilcox 19086b7f123fSMatthew Wilcox spin_lock(&async_scan_lock); 19093e082a91SMatthew Wilcox list_del(&data->list); 19103e082a91SMatthew Wilcox if (!list_empty(&scanning_hosts)) { 19113e082a91SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next, 19123e082a91SMatthew Wilcox struct async_scan_data, list); 19133e082a91SMatthew Wilcox complete(&next->prev_finished); 19143e082a91SMatthew Wilcox } 19153e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 19163e082a91SMatthew Wilcox 1917267a6ad4SHuajun Li scsi_autopm_put_host(shost); 19183e082a91SMatthew Wilcox scsi_host_put(shost); 19193e082a91SMatthew Wilcox kfree(data); 19203e082a91SMatthew Wilcox } 19213e082a91SMatthew Wilcox 19221aa8fab2SMatthew Wilcox static void do_scsi_scan_host(struct Scsi_Host *shost) 19231aa8fab2SMatthew Wilcox { 19241aa8fab2SMatthew Wilcox if (shost->hostt->scan_finished) { 19251aa8fab2SMatthew Wilcox unsigned long start = jiffies; 19261aa8fab2SMatthew Wilcox if (shost->hostt->scan_start) 19271aa8fab2SMatthew Wilcox shost->hostt->scan_start(shost); 19281aa8fab2SMatthew Wilcox 19291aa8fab2SMatthew Wilcox while (!shost->hostt->scan_finished(shost, jiffies - start)) 19301aa8fab2SMatthew Wilcox msleep(10); 19311aa8fab2SMatthew Wilcox } else { 19321aa8fab2SMatthew Wilcox scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD, 1933425b27a0SJohn Garry SCAN_WILD_CARD, SCSI_SCAN_INITIAL); 19341aa8fab2SMatthew Wilcox } 19351aa8fab2SMatthew Wilcox } 19361aa8fab2SMatthew Wilcox 19376cdd5520SDan Williams static void do_scan_async(void *_data, async_cookie_t c) 19383e082a91SMatthew Wilcox { 19393e082a91SMatthew Wilcox struct async_scan_data *data = _data; 1940bc4f2401SAlan Stern struct Scsi_Host *shost = data->shost; 1941bc4f2401SAlan Stern 1942bc4f2401SAlan Stern do_scsi_scan_host(shost); 19433e082a91SMatthew Wilcox scsi_finish_async_scan(data); 19443e082a91SMatthew Wilcox } 19453e082a91SMatthew Wilcox 19461da177e4SLinus Torvalds /** 19471da177e4SLinus Torvalds * scsi_scan_host - scan the given adapter 19481da177e4SLinus Torvalds * @shost: adapter to scan 19491da177e4SLinus Torvalds **/ 19501da177e4SLinus Torvalds void scsi_scan_host(struct Scsi_Host *shost) 19511da177e4SLinus Torvalds { 19523e082a91SMatthew Wilcox struct async_scan_data *data; 19533e082a91SMatthew Wilcox 19541d645088SHannes Reinecke if (strncmp(scsi_scan_type, "none", 4) == 0 || 19551d645088SHannes Reinecke strncmp(scsi_scan_type, "manual", 6) == 0) 19563e082a91SMatthew Wilcox return; 1957bc4f2401SAlan Stern if (scsi_autopm_get_host(shost) < 0) 1958bc4f2401SAlan Stern return; 19593e082a91SMatthew Wilcox 19603e082a91SMatthew Wilcox data = scsi_prep_async_scan(shost); 19613e082a91SMatthew Wilcox if (!data) { 19621aa8fab2SMatthew Wilcox do_scsi_scan_host(shost); 1963bc4f2401SAlan Stern scsi_autopm_put_host(shost); 19643e082a91SMatthew Wilcox return; 19653e082a91SMatthew Wilcox } 19661aa8fab2SMatthew Wilcox 19676cdd5520SDan Williams /* register with the async subsystem so wait_for_device_probe() 19686cdd5520SDan Williams * will flush this work 19696cdd5520SDan Williams */ 19706cdd5520SDan Williams async_schedule(do_scan_async, data); 19716cdd5520SDan Williams 1972267a6ad4SHuajun Li /* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */ 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_host); 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds void scsi_forget_host(struct Scsi_Host *shost) 19771da177e4SLinus Torvalds { 1978a64358dbSAlan Stern struct scsi_device *sdev; 19791da177e4SLinus Torvalds unsigned long flags; 19801da177e4SLinus Torvalds 1981a64358dbSAlan Stern restart: 19821da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 1983a64358dbSAlan Stern list_for_each_entry(sdev, &shost->__devices, siblings) { 1984a64358dbSAlan Stern if (sdev->sdev_state == SDEV_DEL) 1985a64358dbSAlan Stern continue; 19861da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 1987a64358dbSAlan Stern __scsi_remove_device(sdev); 1988a64358dbSAlan Stern goto restart; 19891da177e4SLinus Torvalds } 19901da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 19911da177e4SLinus Torvalds } 19921da177e4SLinus Torvalds 1993