11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * scsi_scan.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2000 Eric Youngdale, 51da177e4SLinus Torvalds * Copyright (C) 2002 Patrick Mansfield 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * The general scanning/probing algorithm is as follows, exceptions are 81da177e4SLinus Torvalds * made to it depending on device specific flags, compilation options, and 91da177e4SLinus Torvalds * global variable (boot or module load time) settings. 101da177e4SLinus Torvalds * 111da177e4SLinus Torvalds * A specific LUN is scanned via an INQUIRY command; if the LUN has a 12f64a181dSChristoph Hellwig * device attached, a scsi_device is allocated and setup for it. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * For every id of every channel on the given host: 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * Scan LUN 0; if the target responds to LUN 0 (even if there is no 171da177e4SLinus Torvalds * device or storage attached to LUN 0): 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * If LUN 0 has a device attached, allocate and setup a 20f64a181dSChristoph Hellwig * scsi_device for it. 211da177e4SLinus Torvalds * 221da177e4SLinus Torvalds * If target is SCSI-3 or up, issue a REPORT LUN, and scan 231da177e4SLinus Torvalds * all of the LUNs returned by the REPORT LUN; else, 241da177e4SLinus Torvalds * sequentially scan LUNs up until some maximum is reached, 251da177e4SLinus Torvalds * or a LUN is seen that cannot have a device attached to it. 261da177e4SLinus Torvalds */ 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include <linux/module.h> 291da177e4SLinus Torvalds #include <linux/moduleparam.h> 301da177e4SLinus Torvalds #include <linux/init.h> 311da177e4SLinus Torvalds #include <linux/blkdev.h> 323e082a91SMatthew Wilcox #include <linux/delay.h> 333e082a91SMatthew Wilcox #include <linux/kthread.h> 343e082a91SMatthew Wilcox #include <linux/spinlock.h> 354ace92fcSArjan van de Ven #include <linux/async.h> 365a0e3ad6STejun Heo #include <linux/slab.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <scsi/scsi.h> 39beb40487SChristoph Hellwig #include <scsi/scsi_cmnd.h> 401da177e4SLinus Torvalds #include <scsi/scsi_device.h> 411da177e4SLinus Torvalds #include <scsi/scsi_driver.h> 421da177e4SLinus Torvalds #include <scsi/scsi_devinfo.h> 431da177e4SLinus Torvalds #include <scsi/scsi_host.h> 441da177e4SLinus Torvalds #include <scsi/scsi_transport.h> 451da177e4SLinus Torvalds #include <scsi/scsi_eh.h> 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #include "scsi_priv.h" 481da177e4SLinus Torvalds #include "scsi_logging.h" 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \ 511da177e4SLinus Torvalds " SCSI scanning, some SCSI devices might not be configured\n" 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds * Default timeout 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds #define SCSI_TIMEOUT (2*HZ) 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* 59405ae7d3SRobert P. J. Day * Prefix values for the SCSI id's (stored in sysfs name field) 601da177e4SLinus Torvalds */ 611da177e4SLinus Torvalds #define SCSI_UID_SER_NUM 'S' 621da177e4SLinus Torvalds #define SCSI_UID_UNKNOWN 'Z' 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds /* 651da177e4SLinus Torvalds * Return values of some of the scanning functions. 661da177e4SLinus Torvalds * 671da177e4SLinus Torvalds * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this 681da177e4SLinus Torvalds * includes allocation or general failures preventing IO from being sent. 691da177e4SLinus Torvalds * 701da177e4SLinus Torvalds * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available 711da177e4SLinus Torvalds * on the given LUN. 721da177e4SLinus Torvalds * 731da177e4SLinus Torvalds * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a 741da177e4SLinus Torvalds * given LUN. 751da177e4SLinus Torvalds */ 761da177e4SLinus Torvalds #define SCSI_SCAN_NO_RESPONSE 0 771da177e4SLinus Torvalds #define SCSI_SCAN_TARGET_PRESENT 1 781da177e4SLinus Torvalds #define SCSI_SCAN_LUN_PRESENT 2 791da177e4SLinus Torvalds 800ad78200SArjan van de Ven static const char *scsi_null_device_strs = "nullnullnullnull"; 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds #define MAX_SCSI_LUNS 512 831da177e4SLinus Torvalds 841abf635dSHannes Reinecke static u64 max_scsi_luns = MAX_SCSI_LUNS; 851da177e4SLinus Torvalds 861abf635dSHannes Reinecke module_param_named(max_luns, max_scsi_luns, ullong, S_IRUGO|S_IWUSR); 871da177e4SLinus Torvalds MODULE_PARM_DESC(max_luns, 881abf635dSHannes Reinecke "last scsi LUN (should be between 1 and 2^64-1)"); 891da177e4SLinus Torvalds 9021db1882SMatthew Wilcox #ifdef CONFIG_SCSI_SCAN_ASYNC 9121db1882SMatthew Wilcox #define SCSI_SCAN_TYPE_DEFAULT "async" 9221db1882SMatthew Wilcox #else 9321db1882SMatthew Wilcox #define SCSI_SCAN_TYPE_DEFAULT "sync" 9421db1882SMatthew Wilcox #endif 9521db1882SMatthew Wilcox 963c31b52fSDan Williams char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT; 973e082a91SMatthew Wilcox 983e082a91SMatthew Wilcox module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO); 993e082a91SMatthew Wilcox MODULE_PARM_DESC(scan, "sync, async or none"); 1003e082a91SMatthew Wilcox 1011da177e4SLinus Torvalds /* 1021da177e4SLinus Torvalds * max_scsi_report_luns: the maximum number of LUNS that will be 1031da177e4SLinus Torvalds * returned from the REPORT LUNS command. 8 times this value must 1041da177e4SLinus Torvalds * be allocated. In theory this could be up to an 8 byte value, but 1051da177e4SLinus Torvalds * in practice, the maximum number of LUNs suppored by any device 1061da177e4SLinus Torvalds * is about 16k. 1071da177e4SLinus Torvalds */ 1081da177e4SLinus Torvalds static unsigned int max_scsi_report_luns = 511; 1091da177e4SLinus Torvalds 11010f4b89aSMasatake YAMATO module_param_named(max_report_luns, max_scsi_report_luns, uint, S_IRUGO|S_IWUSR); 1111da177e4SLinus Torvalds MODULE_PARM_DESC(max_report_luns, 1121da177e4SLinus Torvalds "REPORT LUNS maximum number of LUNS received (should be" 1131da177e4SLinus Torvalds " between 1 and 16384)"); 1141da177e4SLinus Torvalds 11514faf12fSAlan Stern static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ + 18; 1161da177e4SLinus Torvalds 11710f4b89aSMasatake YAMATO module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR); 1181da177e4SLinus Torvalds MODULE_PARM_DESC(inq_timeout, 1191da177e4SLinus Torvalds "Timeout (in seconds) waiting for devices to answer INQUIRY." 12014faf12fSAlan Stern " Default is 20. Some devices may need more; most need less."); 1211da177e4SLinus Torvalds 1226b7f123fSMatthew Wilcox /* This lock protects only this list */ 1233e082a91SMatthew Wilcox static DEFINE_SPINLOCK(async_scan_lock); 1243e082a91SMatthew Wilcox static LIST_HEAD(scanning_hosts); 1253e082a91SMatthew Wilcox 1263e082a91SMatthew Wilcox struct async_scan_data { 1273e082a91SMatthew Wilcox struct list_head list; 1283e082a91SMatthew Wilcox struct Scsi_Host *shost; 1293e082a91SMatthew Wilcox struct completion prev_finished; 1303e082a91SMatthew Wilcox }; 1313e082a91SMatthew Wilcox 1323e082a91SMatthew Wilcox /** 1333e082a91SMatthew Wilcox * scsi_complete_async_scans - Wait for asynchronous scans to complete 1343e082a91SMatthew Wilcox * 1358bcc2412SMatthew Wilcox * When this function returns, any host which started scanning before 1368bcc2412SMatthew Wilcox * this function was called will have finished its scan. Hosts which 1378bcc2412SMatthew Wilcox * started scanning after this function was called may or may not have 1388bcc2412SMatthew Wilcox * finished. 1393e082a91SMatthew Wilcox */ 1403e082a91SMatthew Wilcox int scsi_complete_async_scans(void) 1413e082a91SMatthew Wilcox { 1423e082a91SMatthew Wilcox struct async_scan_data *data; 1433e082a91SMatthew Wilcox 1443e082a91SMatthew Wilcox do { 1453e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 146e96eb23dSDan Williams return 0; 1473e082a91SMatthew Wilcox /* If we can't get memory immediately, that's OK. Just 1483e082a91SMatthew Wilcox * sleep a little. Even if we never get memory, the async 1493e082a91SMatthew Wilcox * scans will finish eventually. 1503e082a91SMatthew Wilcox */ 1513e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL); 1523e082a91SMatthew Wilcox if (!data) 1533e082a91SMatthew Wilcox msleep(1); 1543e082a91SMatthew Wilcox } while (!data); 1553e082a91SMatthew Wilcox 1563e082a91SMatthew Wilcox data->shost = NULL; 1573e082a91SMatthew Wilcox init_completion(&data->prev_finished); 1583e082a91SMatthew Wilcox 1593e082a91SMatthew Wilcox spin_lock(&async_scan_lock); 1603e082a91SMatthew Wilcox /* Check that there's still somebody else on the list */ 1613e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 1623e082a91SMatthew Wilcox goto done; 1633e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts); 1643e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 1653e082a91SMatthew Wilcox 1663e082a91SMatthew Wilcox printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n"); 1673e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished); 1683e082a91SMatthew Wilcox 1693e082a91SMatthew Wilcox spin_lock(&async_scan_lock); 1703e082a91SMatthew Wilcox list_del(&data->list); 1718bcc2412SMatthew Wilcox if (!list_empty(&scanning_hosts)) { 1728bcc2412SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next, 1738bcc2412SMatthew Wilcox struct async_scan_data, list); 1748bcc2412SMatthew Wilcox complete(&next->prev_finished); 1758bcc2412SMatthew Wilcox } 1763e082a91SMatthew Wilcox done: 1773e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 178e96eb23dSDan Williams 1793e082a91SMatthew Wilcox kfree(data); 1803e082a91SMatthew Wilcox return 0; 1813e082a91SMatthew Wilcox } 1823e082a91SMatthew Wilcox 1831da177e4SLinus Torvalds /** 1841da177e4SLinus Torvalds * scsi_unlock_floptical - unlock device via a special MODE SENSE command 18539216033SJames Bottomley * @sdev: scsi device to send command to 1861da177e4SLinus Torvalds * @result: area to store the result of the MODE SENSE 1871da177e4SLinus Torvalds * 1881da177e4SLinus Torvalds * Description: 18939216033SJames Bottomley * Send a vendor specific MODE SENSE (not a MODE SELECT) command. 1901da177e4SLinus Torvalds * Called for BLIST_KEY devices. 1911da177e4SLinus Torvalds **/ 19239216033SJames Bottomley static void scsi_unlock_floptical(struct scsi_device *sdev, 1931da177e4SLinus Torvalds unsigned char *result) 1941da177e4SLinus Torvalds { 1951da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 1961da177e4SLinus Torvalds 19791921e01SHannes Reinecke sdev_printk(KERN_NOTICE, sdev, "unlocking floptical drive\n"); 1981da177e4SLinus Torvalds scsi_cmd[0] = MODE_SENSE; 1991da177e4SLinus Torvalds scsi_cmd[1] = 0; 2001da177e4SLinus Torvalds scsi_cmd[2] = 0x2e; 2011da177e4SLinus Torvalds scsi_cmd[3] = 0; 2021da177e4SLinus Torvalds scsi_cmd[4] = 0x2a; /* size */ 2031da177e4SLinus Torvalds scsi_cmd[5] = 0; 20439216033SJames Bottomley scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL, 205f4f4e47eSFUJITA Tomonori SCSI_TIMEOUT, 3, NULL); 2061da177e4SLinus Torvalds } 2071da177e4SLinus Torvalds 2081da177e4SLinus Torvalds /** 2091da177e4SLinus Torvalds * scsi_alloc_sdev - allocate and setup a scsi_Device 210eb44820cSRob Landley * @starget: which target to allocate a &scsi_device for 211eb44820cSRob Landley * @lun: which lun 212eb44820cSRob Landley * @hostdata: usually NULL and set by ->slave_alloc instead 2131da177e4SLinus Torvalds * 2141da177e4SLinus Torvalds * Description: 2151da177e4SLinus Torvalds * Allocate, initialize for io, and return a pointer to a scsi_Device. 2161da177e4SLinus Torvalds * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and 2171da177e4SLinus Torvalds * adds scsi_Device to the appropriate list. 2181da177e4SLinus Torvalds * 2191da177e4SLinus Torvalds * Return value: 2201da177e4SLinus Torvalds * scsi_Device pointer, or NULL on failure. 2211da177e4SLinus Torvalds **/ 2221da177e4SLinus Torvalds static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, 2239cb78c16SHannes Reinecke u64 lun, void *hostdata) 2241da177e4SLinus Torvalds { 2251da177e4SLinus Torvalds struct scsi_device *sdev; 2261da177e4SLinus Torvalds int display_failure_msg = 1, ret; 2271da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 228a341cd0fSJeff Garzik extern void scsi_evt_thread(struct work_struct *work); 2299937a5e2SJens Axboe extern void scsi_requeue_run_queue(struct work_struct *work); 2301da177e4SLinus Torvalds 23124669f75SJes Sorensen sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size, 2321da177e4SLinus Torvalds GFP_ATOMIC); 2331da177e4SLinus Torvalds if (!sdev) 2341da177e4SLinus Torvalds goto out; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds sdev->vendor = scsi_null_device_strs; 2371da177e4SLinus Torvalds sdev->model = scsi_null_device_strs; 2381da177e4SLinus Torvalds sdev->rev = scsi_null_device_strs; 2391da177e4SLinus Torvalds sdev->host = shost; 2404a84067dSVasu Dev sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD; 2411da177e4SLinus Torvalds sdev->id = starget->id; 2421da177e4SLinus Torvalds sdev->lun = lun; 2431da177e4SLinus Torvalds sdev->channel = starget->channel; 2441da177e4SLinus Torvalds sdev->sdev_state = SDEV_CREATED; 2451da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->siblings); 2461da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->same_target_siblings); 2471da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->cmd_list); 2481da177e4SLinus Torvalds INIT_LIST_HEAD(&sdev->starved_entry); 249a341cd0fSJeff Garzik INIT_LIST_HEAD(&sdev->event_list); 2501da177e4SLinus Torvalds spin_lock_init(&sdev->list_lock); 251a341cd0fSJeff Garzik INIT_WORK(&sdev->event_work, scsi_evt_thread); 2529937a5e2SJens Axboe INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue); 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds sdev->sdev_gendev.parent = get_device(&starget->dev); 2551da177e4SLinus Torvalds sdev->sdev_target = starget; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds /* usually NULL and set by ->slave_alloc instead */ 2581da177e4SLinus Torvalds sdev->hostdata = hostdata; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds /* if the device needs this changing, it may do so in the 2611da177e4SLinus Torvalds * slave_configure function */ 2621da177e4SLinus Torvalds sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* 2651da177e4SLinus Torvalds * Some low level driver could use device->type 2661da177e4SLinus Torvalds */ 2671da177e4SLinus Torvalds sdev->type = -1; 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds /* 2701da177e4SLinus Torvalds * Assume that the device will have handshaking problems, 2711da177e4SLinus Torvalds * and then fix this field later if it turns out it 2721da177e4SLinus Torvalds * doesn't 2731da177e4SLinus Torvalds */ 2741da177e4SLinus Torvalds sdev->borken = 1; 2751da177e4SLinus Torvalds 276d285203cSChristoph Hellwig if (shost_use_blk_mq(shost)) 277d285203cSChristoph Hellwig sdev->request_queue = scsi_mq_alloc_queue(sdev); 278d285203cSChristoph Hellwig else 2791da177e4SLinus Torvalds sdev->request_queue = scsi_alloc_queue(sdev); 2801da177e4SLinus Torvalds if (!sdev->request_queue) { 2811da177e4SLinus Torvalds /* release fn is set up in scsi_sysfs_device_initialise, so 2821da177e4SLinus Torvalds * have to free and put manually here */ 2831da177e4SLinus Torvalds put_device(&starget->dev); 28493f56089SDave Jones kfree(sdev); 2851da177e4SLinus Torvalds goto out; 2861da177e4SLinus Torvalds } 28709ac46c4STejun Heo WARN_ON_ONCE(!blk_get_queue(sdev->request_queue)); 2881da177e4SLinus Torvalds sdev->request_queue->queuedata = sdev; 2891da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun); 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds scsi_sysfs_device_initialize(sdev); 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds if (shost->hostt->slave_alloc) { 2941da177e4SLinus Torvalds ret = shost->hostt->slave_alloc(sdev); 2951da177e4SLinus Torvalds if (ret) { 2961da177e4SLinus Torvalds /* 2971da177e4SLinus Torvalds * if LLDD reports slave not present, don't clutter 2981da177e4SLinus Torvalds * console with alloc failure messages 2991da177e4SLinus Torvalds */ 3001da177e4SLinus Torvalds if (ret == -ENXIO) 3011da177e4SLinus Torvalds display_failure_msg = 0; 3021da177e4SLinus Torvalds goto out_device_destroy; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds } 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds return sdev; 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds out_device_destroy: 3094e6c82b3SJames Bottomley __scsi_remove_device(sdev); 3101da177e4SLinus Torvalds out: 3111da177e4SLinus Torvalds if (display_failure_msg) 312cadbd4a5SHarvey Harrison printk(ALLOC_FAILURE_MSG, __func__); 3131da177e4SLinus Torvalds return NULL; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 316643eb2d9SJames Bottomley static void scsi_target_destroy(struct scsi_target *starget) 317643eb2d9SJames Bottomley { 318643eb2d9SJames Bottomley struct device *dev = &starget->dev; 319643eb2d9SJames Bottomley struct Scsi_Host *shost = dev_to_shost(dev->parent); 320643eb2d9SJames Bottomley unsigned long flags; 321643eb2d9SJames Bottomley 322f2495e22SJames Bottomley starget->state = STARGET_DEL; 323643eb2d9SJames Bottomley transport_destroy_device(dev); 324643eb2d9SJames Bottomley spin_lock_irqsave(shost->host_lock, flags); 325643eb2d9SJames Bottomley if (shost->hostt->target_destroy) 326643eb2d9SJames Bottomley shost->hostt->target_destroy(starget); 327643eb2d9SJames Bottomley list_del_init(&starget->siblings); 328643eb2d9SJames Bottomley spin_unlock_irqrestore(shost->host_lock, flags); 329643eb2d9SJames Bottomley put_device(dev); 330643eb2d9SJames Bottomley } 331643eb2d9SJames Bottomley 3321da177e4SLinus Torvalds static void scsi_target_dev_release(struct device *dev) 3331da177e4SLinus Torvalds { 3341da177e4SLinus Torvalds struct device *parent = dev->parent; 3351da177e4SLinus Torvalds struct scsi_target *starget = to_scsi_target(dev); 336a283bd37SJames Bottomley 3371da177e4SLinus Torvalds kfree(starget); 3381da177e4SLinus Torvalds put_device(parent); 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds 341453cd0f3SAdrian Bunk static struct device_type scsi_target_type = { 342b0ed4336SHannes Reinecke .name = "scsi_target", 343b0ed4336SHannes Reinecke .release = scsi_target_dev_release, 344b0ed4336SHannes Reinecke }; 345b0ed4336SHannes Reinecke 3461da177e4SLinus Torvalds int scsi_is_target_device(const struct device *dev) 3471da177e4SLinus Torvalds { 348b0ed4336SHannes Reinecke return dev->type == &scsi_target_type; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_target_device); 3511da177e4SLinus Torvalds 3521da177e4SLinus Torvalds static struct scsi_target *__scsi_find_target(struct device *parent, 3531da177e4SLinus Torvalds int channel, uint id) 3541da177e4SLinus Torvalds { 3551da177e4SLinus Torvalds struct scsi_target *starget, *found_starget = NULL; 3561da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * Search for an existing target for this sdev. 3591da177e4SLinus Torvalds */ 3601da177e4SLinus Torvalds list_for_each_entry(starget, &shost->__targets, siblings) { 3611da177e4SLinus Torvalds if (starget->id == id && 3621da177e4SLinus Torvalds starget->channel == channel) { 3631da177e4SLinus Torvalds found_starget = starget; 3641da177e4SLinus Torvalds break; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds } 3671da177e4SLinus Torvalds if (found_starget) 3681da177e4SLinus Torvalds get_device(&found_starget->dev); 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds return found_starget; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 373884d25ccSJames Bottomley /** 374e63ed0d7SJames Bottomley * scsi_target_reap_ref_release - remove target from visibility 375e63ed0d7SJames Bottomley * @kref: the reap_ref in the target being released 376e63ed0d7SJames Bottomley * 377e63ed0d7SJames Bottomley * Called on last put of reap_ref, which is the indication that no device 378e63ed0d7SJames Bottomley * under this target is visible anymore, so render the target invisible in 379e63ed0d7SJames Bottomley * sysfs. Note: we have to be in user context here because the target reaps 380e63ed0d7SJames Bottomley * should be done in places where the scsi device visibility is being removed. 381e63ed0d7SJames Bottomley */ 382e63ed0d7SJames Bottomley static void scsi_target_reap_ref_release(struct kref *kref) 383e63ed0d7SJames Bottomley { 384e63ed0d7SJames Bottomley struct scsi_target *starget 385e63ed0d7SJames Bottomley = container_of(kref, struct scsi_target, reap_ref); 386e63ed0d7SJames Bottomley 387f2495e22SJames Bottomley /* 388f2495e22SJames Bottomley * if we get here and the target is still in the CREATED state that 389f2495e22SJames Bottomley * means it was allocated but never made visible (because a scan 390f2495e22SJames Bottomley * turned up no LUNs), so don't call device_del() on it. 391f2495e22SJames Bottomley */ 392f2495e22SJames Bottomley if (starget->state != STARGET_CREATED) { 393e63ed0d7SJames Bottomley transport_remove_device(&starget->dev); 394e63ed0d7SJames Bottomley device_del(&starget->dev); 395f2495e22SJames Bottomley } 396e63ed0d7SJames Bottomley scsi_target_destroy(starget); 397e63ed0d7SJames Bottomley } 398e63ed0d7SJames Bottomley 399e63ed0d7SJames Bottomley static void scsi_target_reap_ref_put(struct scsi_target *starget) 400e63ed0d7SJames Bottomley { 401e63ed0d7SJames Bottomley kref_put(&starget->reap_ref, scsi_target_reap_ref_release); 402e63ed0d7SJames Bottomley } 403e63ed0d7SJames Bottomley 404e63ed0d7SJames Bottomley /** 405884d25ccSJames Bottomley * scsi_alloc_target - allocate a new or find an existing target 406884d25ccSJames Bottomley * @parent: parent of the target (need not be a scsi host) 407884d25ccSJames Bottomley * @channel: target channel number (zero if no channels) 408884d25ccSJames Bottomley * @id: target id number 409884d25ccSJames Bottomley * 410884d25ccSJames Bottomley * Return an existing target if one exists, provided it hasn't already 411884d25ccSJames Bottomley * gone into STARGET_DEL state, otherwise allocate a new target. 412884d25ccSJames Bottomley * 413884d25ccSJames Bottomley * The target is returned with an incremented reference, so the caller 414884d25ccSJames Bottomley * is responsible for both reaping and doing a last put 415884d25ccSJames Bottomley */ 4161da177e4SLinus Torvalds static struct scsi_target *scsi_alloc_target(struct device *parent, 4171da177e4SLinus Torvalds int channel, uint id) 4181da177e4SLinus Torvalds { 4191da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 4201da177e4SLinus Torvalds struct device *dev = NULL; 4211da177e4SLinus Torvalds unsigned long flags; 4221da177e4SLinus Torvalds const int size = sizeof(struct scsi_target) 4231da177e4SLinus Torvalds + shost->transportt->target_size; 4245c44cd2aSJames.Smart@Emulex.Com struct scsi_target *starget; 4251da177e4SLinus Torvalds struct scsi_target *found_target; 426e63ed0d7SJames Bottomley int error, ref_got; 4271da177e4SLinus Torvalds 42824669f75SJes Sorensen starget = kzalloc(size, GFP_KERNEL); 4291da177e4SLinus Torvalds if (!starget) { 430cadbd4a5SHarvey Harrison printk(KERN_ERR "%s: allocation failure\n", __func__); 4311da177e4SLinus Torvalds return NULL; 4321da177e4SLinus Torvalds } 4331da177e4SLinus Torvalds dev = &starget->dev; 4341da177e4SLinus Torvalds device_initialize(dev); 435e63ed0d7SJames Bottomley kref_init(&starget->reap_ref); 4361da177e4SLinus Torvalds dev->parent = get_device(parent); 43771610f55SKay Sievers dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id); 438b0ed4336SHannes Reinecke dev->bus = &scsi_bus_type; 439b0ed4336SHannes Reinecke dev->type = &scsi_target_type; 4401da177e4SLinus Torvalds starget->id = id; 4411da177e4SLinus Torvalds starget->channel = channel; 442f0c0a376SMike Christie starget->can_queue = 0; 4431da177e4SLinus Torvalds INIT_LIST_HEAD(&starget->siblings); 4441da177e4SLinus Torvalds INIT_LIST_HEAD(&starget->devices); 445643eb2d9SJames Bottomley starget->state = STARGET_CREATED; 4467c9d6f16SAlan Stern starget->scsi_level = SCSI_2; 447c53a284fSEdward Goggin starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED; 448ffedb452SJames Bottomley retry: 4491da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 4501da177e4SLinus Torvalds 4511da177e4SLinus Torvalds found_target = __scsi_find_target(parent, channel, id); 4521da177e4SLinus Torvalds if (found_target) 4531da177e4SLinus Torvalds goto found; 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds list_add_tail(&starget->siblings, &shost->__targets); 4561da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 4571da177e4SLinus Torvalds /* allocate and add */ 458a283bd37SJames Bottomley transport_setup_device(dev); 459a283bd37SJames Bottomley if (shost->hostt->target_alloc) { 46032f95792SBrian King error = shost->hostt->target_alloc(starget); 461a283bd37SJames Bottomley 462a283bd37SJames Bottomley if(error) { 463a283bd37SJames Bottomley dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error); 464a283bd37SJames Bottomley /* don't want scsi_target_reap to do the final 465a283bd37SJames Bottomley * put because it will be under the host lock */ 466643eb2d9SJames Bottomley scsi_target_destroy(starget); 467a283bd37SJames Bottomley return NULL; 468a283bd37SJames Bottomley } 469a283bd37SJames Bottomley } 470884d25ccSJames Bottomley get_device(dev); 471a283bd37SJames Bottomley 4721da177e4SLinus Torvalds return starget; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds found: 475e63ed0d7SJames Bottomley /* 476e63ed0d7SJames Bottomley * release routine already fired if kref is zero, so if we can still 477e63ed0d7SJames Bottomley * take the reference, the target must be alive. If we can't, it must 478e63ed0d7SJames Bottomley * be dying and we need to wait for a new target 479e63ed0d7SJames Bottomley */ 480e63ed0d7SJames Bottomley ref_got = kref_get_unless_zero(&found_target->reap_ref); 481e63ed0d7SJames Bottomley 4821da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 483e63ed0d7SJames Bottomley if (ref_got) { 48412fb8c15SAlan Stern put_device(dev); 4851da177e4SLinus Torvalds return found_target; 4861da177e4SLinus Torvalds } 487e63ed0d7SJames Bottomley /* 488e63ed0d7SJames Bottomley * Unfortunately, we found a dying target; need to wait until it's 489e63ed0d7SJames Bottomley * dead before we can get a new one. There is an anomaly here. We 490e63ed0d7SJames Bottomley * *should* call scsi_target_reap() to balance the kref_get() of the 491e63ed0d7SJames Bottomley * reap_ref above. However, since the target being released, it's 492e63ed0d7SJames Bottomley * already invisible and the reap_ref is irrelevant. If we call 493e63ed0d7SJames Bottomley * scsi_target_reap() we might spuriously do another device_del() on 494e63ed0d7SJames Bottomley * an already invisible target. 495e63ed0d7SJames Bottomley */ 496ffedb452SJames Bottomley put_device(&found_target->dev); 497e63ed0d7SJames Bottomley /* 498e63ed0d7SJames Bottomley * length of time is irrelevant here, we just want to yield the CPU 499e63ed0d7SJames Bottomley * for a tick to avoid busy waiting for the target to die. 500e63ed0d7SJames Bottomley */ 501e63ed0d7SJames Bottomley msleep(1); 502ffedb452SJames Bottomley goto retry; 503ffedb452SJames Bottomley } 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds /** 5061da177e4SLinus Torvalds * scsi_target_reap - check to see if target is in use and destroy if not 5071da177e4SLinus Torvalds * @starget: target to be checked 5081da177e4SLinus Torvalds * 5091da177e4SLinus Torvalds * This is used after removing a LUN or doing a last put of the target 5101da177e4SLinus Torvalds * it checks atomically that nothing is using the target and removes 5111da177e4SLinus Torvalds * it if so. 5121da177e4SLinus Torvalds */ 5131da177e4SLinus Torvalds void scsi_target_reap(struct scsi_target *starget) 5141da177e4SLinus Torvalds { 515f2495e22SJames Bottomley /* 516f2495e22SJames Bottomley * serious problem if this triggers: STARGET_DEL is only set in the if 517f2495e22SJames Bottomley * the reap_ref drops to zero, so we're trying to do another final put 518f2495e22SJames Bottomley * on an already released kref 519f2495e22SJames Bottomley */ 520e63ed0d7SJames Bottomley BUG_ON(starget->state == STARGET_DEL); 521e63ed0d7SJames Bottomley scsi_target_reap_ref_put(starget); 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds /** 525e5b3cd42SAlan Stern * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string 526e5b3cd42SAlan Stern * @s: INQUIRY result string to sanitize 527e5b3cd42SAlan Stern * @len: length of the string 528e5b3cd42SAlan Stern * 529e5b3cd42SAlan Stern * Description: 530e5b3cd42SAlan Stern * The SCSI spec says that INQUIRY vendor, product, and revision 531e5b3cd42SAlan Stern * strings must consist entirely of graphic ASCII characters, 532e5b3cd42SAlan Stern * padded on the right with spaces. Since not all devices obey 533e5b3cd42SAlan Stern * this rule, we will replace non-graphic or non-ASCII characters 534e5b3cd42SAlan Stern * with spaces. Exception: a NUL character is interpreted as a 535e5b3cd42SAlan Stern * string terminator, so all the following characters are set to 536e5b3cd42SAlan Stern * spaces. 537e5b3cd42SAlan Stern **/ 538e5b3cd42SAlan Stern static void sanitize_inquiry_string(unsigned char *s, int len) 539e5b3cd42SAlan Stern { 540e5b3cd42SAlan Stern int terminated = 0; 541e5b3cd42SAlan Stern 542e5b3cd42SAlan Stern for (; len > 0; (--len, ++s)) { 543e5b3cd42SAlan Stern if (*s == 0) 544e5b3cd42SAlan Stern terminated = 1; 545e5b3cd42SAlan Stern if (terminated || *s < 0x20 || *s > 0x7e) 546e5b3cd42SAlan Stern *s = ' '; 547e5b3cd42SAlan Stern } 548e5b3cd42SAlan Stern } 549e5b3cd42SAlan Stern 550e5b3cd42SAlan Stern /** 5511da177e4SLinus Torvalds * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY 55239216033SJames Bottomley * @sdev: scsi_device to probe 5531da177e4SLinus Torvalds * @inq_result: area to store the INQUIRY result 55439216033SJames Bottomley * @result_len: len of inq_result 5551da177e4SLinus Torvalds * @bflags: store any bflags found here 5561da177e4SLinus Torvalds * 5571da177e4SLinus Torvalds * Description: 55839216033SJames Bottomley * Probe the lun associated with @req using a standard SCSI INQUIRY; 5591da177e4SLinus Torvalds * 56039216033SJames Bottomley * If the INQUIRY is successful, zero is returned and the 5611da177e4SLinus Torvalds * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length 562f64a181dSChristoph Hellwig * are copied to the scsi_device any flags value is stored in *@bflags. 5631da177e4SLinus Torvalds **/ 564e5b3cd42SAlan Stern static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result, 56539216033SJames Bottomley int result_len, int *bflags) 5661da177e4SLinus Torvalds { 5671da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 5681da177e4SLinus Torvalds int first_inquiry_len, try_inquiry_len, next_inquiry_len; 5691da177e4SLinus Torvalds int response_len = 0; 57039216033SJames Bottomley int pass, count, result; 5711da177e4SLinus Torvalds struct scsi_sense_hdr sshdr; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds *bflags = 0; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* Perform up to 3 passes. The first pass uses a conservative 5761da177e4SLinus Torvalds * transfer length of 36 unless sdev->inquiry_len specifies a 5771da177e4SLinus Torvalds * different value. */ 5781da177e4SLinus Torvalds first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36; 5791da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len; 5801da177e4SLinus Torvalds pass = 1; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds next_pass: 5839ccfc756SJames Bottomley SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 5849ccfc756SJames Bottomley "scsi scan: INQUIRY pass %d length %d\n", 5859ccfc756SJames Bottomley pass, try_inquiry_len)); 5861da177e4SLinus Torvalds 5871da177e4SLinus Torvalds /* Each pass gets up to three chances to ignore Unit Attention */ 5881da177e4SLinus Torvalds for (count = 0; count < 3; ++count) { 5895cd3bbfaSFUJITA Tomonori int resid; 5905cd3bbfaSFUJITA Tomonori 5911da177e4SLinus Torvalds memset(scsi_cmd, 0, 6); 5921da177e4SLinus Torvalds scsi_cmd[0] = INQUIRY; 5931da177e4SLinus Torvalds scsi_cmd[4] = (unsigned char) try_inquiry_len; 5941da177e4SLinus Torvalds 5951da177e4SLinus Torvalds memset(inq_result, 0, try_inquiry_len); 59639216033SJames Bottomley 59739216033SJames Bottomley result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, 598ea73a9f2SJames Bottomley inq_result, try_inquiry_len, &sshdr, 599f4f4e47eSFUJITA Tomonori HZ / 2 + HZ * scsi_inq_timeout, 3, 6005cd3bbfaSFUJITA Tomonori &resid); 6011da177e4SLinus Torvalds 60291921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 60391921e01SHannes Reinecke "scsi scan: INQUIRY %s with code 0x%x\n", 60439216033SJames Bottomley result ? "failed" : "successful", result)); 6051da177e4SLinus Torvalds 60639216033SJames Bottomley if (result) { 6071da177e4SLinus Torvalds /* 6081da177e4SLinus Torvalds * not-ready to ready transition [asc/ascq=0x28/0x0] 6091da177e4SLinus Torvalds * or power-on, reset [asc/ascq=0x29/0x0], continue. 6101da177e4SLinus Torvalds * INQUIRY should not yield UNIT_ATTENTION 6111da177e4SLinus Torvalds * but many buggy devices do so anyway. 6121da177e4SLinus Torvalds */ 61339216033SJames Bottomley if ((driver_byte(result) & DRIVER_SENSE) && 614ea73a9f2SJames Bottomley scsi_sense_valid(&sshdr)) { 6151da177e4SLinus Torvalds if ((sshdr.sense_key == UNIT_ATTENTION) && 6161da177e4SLinus Torvalds ((sshdr.asc == 0x28) || 6171da177e4SLinus Torvalds (sshdr.asc == 0x29)) && 6181da177e4SLinus Torvalds (sshdr.ascq == 0)) 6191da177e4SLinus Torvalds continue; 6201da177e4SLinus Torvalds } 6215cd3bbfaSFUJITA Tomonori } else { 6225cd3bbfaSFUJITA Tomonori /* 6235cd3bbfaSFUJITA Tomonori * if nothing was transferred, we try 6245cd3bbfaSFUJITA Tomonori * again. It's a workaround for some USB 6255cd3bbfaSFUJITA Tomonori * devices. 6265cd3bbfaSFUJITA Tomonori */ 6275cd3bbfaSFUJITA Tomonori if (resid == try_inquiry_len) 6285cd3bbfaSFUJITA Tomonori continue; 6291da177e4SLinus Torvalds } 6301da177e4SLinus Torvalds break; 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 63339216033SJames Bottomley if (result == 0) { 634e5b3cd42SAlan Stern sanitize_inquiry_string(&inq_result[8], 8); 635e5b3cd42SAlan Stern sanitize_inquiry_string(&inq_result[16], 16); 636e5b3cd42SAlan Stern sanitize_inquiry_string(&inq_result[32], 4); 637e5b3cd42SAlan Stern 638e5b3cd42SAlan Stern response_len = inq_result[4] + 5; 6391da177e4SLinus Torvalds if (response_len > 255) 6401da177e4SLinus Torvalds response_len = first_inquiry_len; /* sanity */ 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds /* 6431da177e4SLinus Torvalds * Get any flags for this device. 6441da177e4SLinus Torvalds * 645f64a181dSChristoph Hellwig * XXX add a bflags to scsi_device, and replace the 646f64a181dSChristoph Hellwig * corresponding bit fields in scsi_device, so bflags 6471da177e4SLinus Torvalds * need not be passed as an argument. 6481da177e4SLinus Torvalds */ 6491da177e4SLinus Torvalds *bflags = scsi_get_device_flags(sdev, &inq_result[8], 6501da177e4SLinus Torvalds &inq_result[16]); 6511da177e4SLinus Torvalds 6521da177e4SLinus Torvalds /* When the first pass succeeds we gain information about 6531da177e4SLinus Torvalds * what larger transfer lengths might work. */ 6541da177e4SLinus Torvalds if (pass == 1) { 6551da177e4SLinus Torvalds if (BLIST_INQUIRY_36 & *bflags) 6561da177e4SLinus Torvalds next_inquiry_len = 36; 6571da177e4SLinus Torvalds else if (BLIST_INQUIRY_58 & *bflags) 6581da177e4SLinus Torvalds next_inquiry_len = 58; 6591da177e4SLinus Torvalds else if (sdev->inquiry_len) 6601da177e4SLinus Torvalds next_inquiry_len = sdev->inquiry_len; 6611da177e4SLinus Torvalds else 6621da177e4SLinus Torvalds next_inquiry_len = response_len; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* If more data is available perform the second pass */ 6651da177e4SLinus Torvalds if (next_inquiry_len > try_inquiry_len) { 6661da177e4SLinus Torvalds try_inquiry_len = next_inquiry_len; 6671da177e4SLinus Torvalds pass = 2; 6681da177e4SLinus Torvalds goto next_pass; 6691da177e4SLinus Torvalds } 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds } else if (pass == 2) { 67391921e01SHannes Reinecke sdev_printk(KERN_INFO, sdev, 67491921e01SHannes Reinecke "scsi scan: %d byte inquiry failed. " 6751da177e4SLinus Torvalds "Consider BLIST_INQUIRY_36 for this device\n", 6761da177e4SLinus Torvalds try_inquiry_len); 6771da177e4SLinus Torvalds 6781da177e4SLinus Torvalds /* If this pass failed, the third pass goes back and transfers 6791da177e4SLinus Torvalds * the same amount as we successfully got in the first pass. */ 6801da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len; 6811da177e4SLinus Torvalds pass = 3; 6821da177e4SLinus Torvalds goto next_pass; 6831da177e4SLinus Torvalds } 6841da177e4SLinus Torvalds 6851da177e4SLinus Torvalds /* If the last transfer attempt got an error, assume the 6861da177e4SLinus Torvalds * peripheral doesn't exist or is dead. */ 68739216033SJames Bottomley if (result) 68839216033SJames Bottomley return -EIO; 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds /* Don't report any more data than the device says is valid */ 6911da177e4SLinus Torvalds sdev->inquiry_len = min(try_inquiry_len, response_len); 6921da177e4SLinus Torvalds 6931da177e4SLinus Torvalds /* 6941da177e4SLinus Torvalds * XXX Abort if the response length is less than 36? If less than 6951da177e4SLinus Torvalds * 32, the lookup of the device flags (above) could be invalid, 6961da177e4SLinus Torvalds * and it would be possible to take an incorrect action - we do 6971da177e4SLinus Torvalds * not want to hang because of a short INQUIRY. On the flip side, 6981da177e4SLinus Torvalds * if the device is spun down or becoming ready (and so it gives a 6991da177e4SLinus Torvalds * short INQUIRY), an abort here prevents any further use of the 7001da177e4SLinus Torvalds * device, including spin up. 7011da177e4SLinus Torvalds * 702e423ee31SAlan Stern * On the whole, the best approach seems to be to assume the first 703e423ee31SAlan Stern * 36 bytes are valid no matter what the device says. That's 704e423ee31SAlan Stern * better than copying < 36 bytes to the inquiry-result buffer 705e423ee31SAlan Stern * and displaying garbage for the Vendor, Product, or Revision 706e423ee31SAlan Stern * strings. 707e423ee31SAlan Stern */ 708e423ee31SAlan Stern if (sdev->inquiry_len < 36) { 70991921e01SHannes Reinecke sdev_printk(KERN_INFO, sdev, 71091921e01SHannes Reinecke "scsi scan: INQUIRY result too short (%d)," 711e423ee31SAlan Stern " using 36\n", sdev->inquiry_len); 712e423ee31SAlan Stern sdev->inquiry_len = 36; 713e423ee31SAlan Stern } 714e423ee31SAlan Stern 715e423ee31SAlan Stern /* 7161da177e4SLinus Torvalds * Related to the above issue: 7171da177e4SLinus Torvalds * 7181da177e4SLinus Torvalds * XXX Devices (disk or all?) should be sent a TEST UNIT READY, 7191da177e4SLinus Torvalds * and if not ready, sent a START_STOP to start (maybe spin up) and 7201da177e4SLinus Torvalds * then send the INQUIRY again, since the INQUIRY can change after 7211da177e4SLinus Torvalds * a device is initialized. 7221da177e4SLinus Torvalds * 7231da177e4SLinus Torvalds * Ideally, start a device if explicitly asked to do so. This 7241da177e4SLinus Torvalds * assumes that a device is spun up on power on, spun down on 7251da177e4SLinus Torvalds * request, and then spun up on request. 7261da177e4SLinus Torvalds */ 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds /* 7291da177e4SLinus Torvalds * The scanning code needs to know the scsi_level, even if no 7301da177e4SLinus Torvalds * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so 7311da177e4SLinus Torvalds * non-zero LUNs can be scanned. 7321da177e4SLinus Torvalds */ 7331da177e4SLinus Torvalds sdev->scsi_level = inq_result[2] & 0x07; 7341da177e4SLinus Torvalds if (sdev->scsi_level >= 2 || 7351da177e4SLinus Torvalds (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1)) 7361da177e4SLinus Torvalds sdev->scsi_level++; 7376f3a2024SJames Bottomley sdev->sdev_target->scsi_level = sdev->scsi_level; 7381da177e4SLinus Torvalds 73950c4e964SAlan Stern /* 74050c4e964SAlan Stern * If SCSI-2 or lower, and if the transport requires it, 74150c4e964SAlan Stern * store the LUN value in CDB[1]. 74250c4e964SAlan Stern */ 74350c4e964SAlan Stern sdev->lun_in_cdb = 0; 74450c4e964SAlan Stern if (sdev->scsi_level <= SCSI_2 && 74550c4e964SAlan Stern sdev->scsi_level != SCSI_UNKNOWN && 74650c4e964SAlan Stern !sdev->host->no_scsi2_lun_in_cdb) 74750c4e964SAlan Stern sdev->lun_in_cdb = 1; 74850c4e964SAlan Stern 74939216033SJames Bottomley return 0; 7501da177e4SLinus Torvalds } 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds /** 753f64a181dSChristoph Hellwig * scsi_add_lun - allocate and fully initialze a scsi_device 7546d877688SMatthew Wilcox * @sdev: holds information to be stored in the new scsi_device 7551da177e4SLinus Torvalds * @inq_result: holds the result of a previous INQUIRY to the LUN 7561da177e4SLinus Torvalds * @bflags: black/white list flag 7576d877688SMatthew Wilcox * @async: 1 if this device is being scanned asynchronously 7581da177e4SLinus Torvalds * 7591da177e4SLinus Torvalds * Description: 7606d877688SMatthew Wilcox * Initialize the scsi_device @sdev. Optionally set fields based 7616d877688SMatthew Wilcox * on values in *@bflags. 7621da177e4SLinus Torvalds * 7631da177e4SLinus Torvalds * Return: 764f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 765f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 7661da177e4SLinus Torvalds **/ 767e5b3cd42SAlan Stern static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, 7683e082a91SMatthew Wilcox int *bflags, int async) 7691da177e4SLinus Torvalds { 7706f4267e3SJames Bottomley int ret; 7716f4267e3SJames Bottomley 7721da177e4SLinus Torvalds /* 7731da177e4SLinus Torvalds * XXX do not save the inquiry, since it can change underneath us, 7741da177e4SLinus Torvalds * save just vendor/model/rev. 7751da177e4SLinus Torvalds * 7761da177e4SLinus Torvalds * Rather than save it and have an ioctl that retrieves the saved 7771da177e4SLinus Torvalds * value, have an ioctl that executes the same INQUIRY code used 7781da177e4SLinus Torvalds * in scsi_probe_lun, let user level programs doing INQUIRY 7791da177e4SLinus Torvalds * scanning run at their own risk, or supply a user level program 7801da177e4SLinus Torvalds * that can correctly scan. 7811da177e4SLinus Torvalds */ 7821da177e4SLinus Torvalds 78309123d23SAlan Stern /* 78409123d23SAlan Stern * Copy at least 36 bytes of INQUIRY data, so that we don't 78509123d23SAlan Stern * dereference unallocated memory when accessing the Vendor, 78609123d23SAlan Stern * Product, and Revision strings. Badly behaved devices may set 78709123d23SAlan Stern * the INQUIRY Additional Length byte to a small value, indicating 78809123d23SAlan Stern * these strings are invalid, but often they contain plausible data 78909123d23SAlan Stern * nonetheless. It doesn't matter if the device sent < 36 bytes 79009123d23SAlan Stern * total, since scsi_probe_lun() initializes inq_result with 0s. 79109123d23SAlan Stern */ 79209123d23SAlan Stern sdev->inquiry = kmemdup(inq_result, 79309123d23SAlan Stern max_t(size_t, sdev->inquiry_len, 36), 79409123d23SAlan Stern GFP_ATOMIC); 79509123d23SAlan Stern if (sdev->inquiry == NULL) 79609123d23SAlan Stern return SCSI_SCAN_NO_RESPONSE; 79709123d23SAlan Stern 7981da177e4SLinus Torvalds sdev->vendor = (char *) (sdev->inquiry + 8); 7991da177e4SLinus Torvalds sdev->model = (char *) (sdev->inquiry + 16); 8001da177e4SLinus Torvalds sdev->rev = (char *) (sdev->inquiry + 32); 8011da177e4SLinus Torvalds 80214216561SJames Bottomley if (strncmp(sdev->vendor, "ATA ", 8) == 0) { 80314216561SJames Bottomley /* 80414216561SJames Bottomley * sata emulation layer device. This is a hack to work around 80514216561SJames Bottomley * the SATL power management specifications which state that 80614216561SJames Bottomley * when the SATL detects the device has gone into standby 80714216561SJames Bottomley * mode, it shall respond with NOT READY. 80814216561SJames Bottomley */ 80914216561SJames Bottomley sdev->allow_restart = 1; 81014216561SJames Bottomley } 81114216561SJames Bottomley 8121da177e4SLinus Torvalds if (*bflags & BLIST_ISROM) { 8136d877688SMatthew Wilcox sdev->type = TYPE_ROM; 8146d877688SMatthew Wilcox sdev->removable = 1; 8156d877688SMatthew Wilcox } else { 8166d877688SMatthew Wilcox sdev->type = (inq_result[0] & 0x1f); 8176d877688SMatthew Wilcox sdev->removable = (inq_result[1] & 0x80) >> 7; 81845341ca3SSubhash Jadavani 81945341ca3SSubhash Jadavani /* 82045341ca3SSubhash Jadavani * some devices may respond with wrong type for 82145341ca3SSubhash Jadavani * well-known logical units. Force well-known type 82245341ca3SSubhash Jadavani * to enumerate them correctly. 82345341ca3SSubhash Jadavani */ 82445341ca3SSubhash Jadavani if (scsi_is_wlun(sdev->lun) && sdev->type != TYPE_WLUN) { 82545341ca3SSubhash Jadavani sdev_printk(KERN_WARNING, sdev, 82645341ca3SSubhash Jadavani "%s: correcting incorrect peripheral device type 0x%x for W-LUN 0x%16xhN\n", 82745341ca3SSubhash Jadavani __func__, sdev->type, (unsigned int)sdev->lun); 82845341ca3SSubhash Jadavani sdev->type = TYPE_WLUN; 82945341ca3SSubhash Jadavani } 83045341ca3SSubhash Jadavani 8316d877688SMatthew Wilcox } 8321da177e4SLinus Torvalds 8336d877688SMatthew Wilcox if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) { 8346d877688SMatthew Wilcox /* RBC and MMC devices can return SCSI-3 compliance and yet 8356d877688SMatthew Wilcox * still not support REPORT LUNS, so make them act as 8366d877688SMatthew Wilcox * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is 8376d877688SMatthew Wilcox * specifically set */ 8386d877688SMatthew Wilcox if ((*bflags & BLIST_REPORTLUN2) == 0) 8396d877688SMatthew Wilcox *bflags |= BLIST_NOREPORTLUN; 8406d877688SMatthew Wilcox } 8416d877688SMatthew Wilcox 8421da177e4SLinus Torvalds /* 8431da177e4SLinus Torvalds * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI 8441da177e4SLinus Torvalds * spec says: The device server is capable of supporting the 8451da177e4SLinus Torvalds * specified peripheral device type on this logical unit. However, 8461da177e4SLinus Torvalds * the physical device is not currently connected to this logical 8471da177e4SLinus Torvalds * unit. 8481da177e4SLinus Torvalds * 8491da177e4SLinus Torvalds * The above is vague, as it implies that we could treat 001 and 8501da177e4SLinus Torvalds * 011 the same. Stay compatible with previous code, and create a 851f64a181dSChristoph Hellwig * scsi_device for a PQ of 1 8521da177e4SLinus Torvalds * 8531da177e4SLinus Torvalds * Don't set the device offline here; rather let the upper 8541da177e4SLinus Torvalds * level drivers eval the PQ to decide whether they should 8551da177e4SLinus Torvalds * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. 8561da177e4SLinus Torvalds */ 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; 8591da177e4SLinus Torvalds sdev->lockable = sdev->removable; 8601da177e4SLinus Torvalds sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); 8611da177e4SLinus Torvalds 8626d877688SMatthew Wilcox if (sdev->scsi_level >= SCSI_3 || 8636d877688SMatthew Wilcox (sdev->inquiry_len > 56 && inq_result[56] & 0x04)) 8641da177e4SLinus Torvalds sdev->ppr = 1; 8651da177e4SLinus Torvalds if (inq_result[7] & 0x60) 8661da177e4SLinus Torvalds sdev->wdtr = 1; 8671da177e4SLinus Torvalds if (inq_result[7] & 0x10) 8681da177e4SLinus Torvalds sdev->sdtr = 1; 8691da177e4SLinus Torvalds 87019ac0db3SJames Bottomley sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d " 8714ff36718SMatthew Wilcox "ANSI: %d%s\n", scsi_device_type(sdev->type), 8724ff36718SMatthew Wilcox sdev->vendor, sdev->model, sdev->rev, 8734ff36718SMatthew Wilcox sdev->inq_periph_qual, inq_result[2] & 0x07, 8744ff36718SMatthew Wilcox (inq_result[3] & 0x0f) == 1 ? " CCS" : ""); 8754ff36718SMatthew Wilcox 8761da177e4SLinus Torvalds if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && 8771da177e4SLinus Torvalds !(*bflags & BLIST_NOTQ)) 8781da177e4SLinus Torvalds sdev->tagged_supported = 1; 8796d877688SMatthew Wilcox 8801da177e4SLinus Torvalds /* 8811da177e4SLinus Torvalds * Some devices (Texel CD ROM drives) have handshaking problems 8821da177e4SLinus Torvalds * when used with the Seagate controllers. borken is initialized 8831da177e4SLinus Torvalds * to 1, and then set it to 0 here. 8841da177e4SLinus Torvalds */ 8851da177e4SLinus Torvalds if ((*bflags & BLIST_BORKEN) == 0) 8861da177e4SLinus Torvalds sdev->borken = 0; 8871da177e4SLinus Torvalds 8886d877688SMatthew Wilcox if (*bflags & BLIST_NO_ULD_ATTACH) 8896d877688SMatthew Wilcox sdev->no_uld_attach = 1; 8906d877688SMatthew Wilcox 8911da177e4SLinus Torvalds /* 8921da177e4SLinus Torvalds * Apparently some really broken devices (contrary to the SCSI 8931da177e4SLinus Torvalds * standards) need to be selected without asserting ATN 8941da177e4SLinus Torvalds */ 8951da177e4SLinus Torvalds if (*bflags & BLIST_SELECT_NO_ATN) 8961da177e4SLinus Torvalds sdev->select_no_atn = 1; 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds /* 8994d7db04aSJames Bottomley * Maximum 512 sector transfer length 9004d7db04aSJames Bottomley * broken RA4x00 Compaq Disk Array 9014d7db04aSJames Bottomley */ 9024d7db04aSJames Bottomley if (*bflags & BLIST_MAX_512) 903086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(sdev->request_queue, 512); 9044d7db04aSJames Bottomley 9054d7db04aSJames Bottomley /* 9061da177e4SLinus Torvalds * Some devices may not want to have a start command automatically 9071da177e4SLinus Torvalds * issued when a device is added. 9081da177e4SLinus Torvalds */ 9091da177e4SLinus Torvalds if (*bflags & BLIST_NOSTARTONADD) 9101da177e4SLinus Torvalds sdev->no_start_on_add = 1; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds if (*bflags & BLIST_SINGLELUN) 91325d7c363STony Battersby scsi_target(sdev)->single_lun = 1; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds sdev->use_10_for_rw = 1; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds if (*bflags & BLIST_MS_SKIP_PAGE_08) 9181da177e4SLinus Torvalds sdev->skip_ms_page_8 = 1; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds if (*bflags & BLIST_MS_SKIP_PAGE_3F) 9211da177e4SLinus Torvalds sdev->skip_ms_page_3f = 1; 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds if (*bflags & BLIST_USE_10_BYTE_MS) 9241da177e4SLinus Torvalds sdev->use_10_for_ms = 1; 9251da177e4SLinus Torvalds 9260213436aSJanusz Dziemidowicz /* some devices don't like REPORT SUPPORTED OPERATION CODES 9270213436aSJanusz Dziemidowicz * and will simply timeout causing sd_mod init to take a very 9280213436aSJanusz Dziemidowicz * very long time */ 9290213436aSJanusz Dziemidowicz if (*bflags & BLIST_NO_RSOC) 9300213436aSJanusz Dziemidowicz sdev->no_report_opcodes = 1; 9310213436aSJanusz Dziemidowicz 9321da177e4SLinus Torvalds /* set the device running here so that slave configure 9331da177e4SLinus Torvalds * may do I/O */ 9346f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_RUNNING); 9356f4267e3SJames Bottomley if (ret) { 9366f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_BLOCK); 9376f4267e3SJames Bottomley 9386f4267e3SJames Bottomley if (ret) { 9396f4267e3SJames Bottomley sdev_printk(KERN_ERR, sdev, 9406f4267e3SJames Bottomley "in wrong state %s to complete scan\n", 9416f4267e3SJames Bottomley scsi_device_state_name(sdev->sdev_state)); 9426f4267e3SJames Bottomley return SCSI_SCAN_NO_RESPONSE; 9436f4267e3SJames Bottomley } 9446f4267e3SJames Bottomley } 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds if (*bflags & BLIST_MS_192_BYTES_FOR_3F) 9471da177e4SLinus Torvalds sdev->use_192_bytes_for_3f = 1; 9481da177e4SLinus Torvalds 9491da177e4SLinus Torvalds if (*bflags & BLIST_NOT_LOCKABLE) 9501da177e4SLinus Torvalds sdev->lockable = 0; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds if (*bflags & BLIST_RETRY_HWERROR) 9531da177e4SLinus Torvalds sdev->retry_hwerror = 1; 9541da177e4SLinus Torvalds 955d974e426SMartin K. Petersen if (*bflags & BLIST_NO_DIF) 956d974e426SMartin K. Petersen sdev->no_dif = 1; 957d974e426SMartin K. Petersen 9580816c925SMartin K. Petersen sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT; 9590816c925SMartin K. Petersen 960c1d40a52SMartin K. Petersen if (*bflags & BLIST_TRY_VPD_PAGES) 961c1d40a52SMartin K. Petersen sdev->try_vpd_pages = 1; 962c1d40a52SMartin K. Petersen else if (*bflags & BLIST_SKIP_VPD_PAGES) 96356f2a801SMartin K. Petersen sdev->skip_vpd_pages = 1; 96456f2a801SMartin K. Petersen 9651da177e4SLinus Torvalds transport_configure_device(&sdev->sdev_gendev); 9661da177e4SLinus Torvalds 96793805091SChristoph Hellwig if (sdev->host->hostt->slave_configure) { 9686f4267e3SJames Bottomley ret = sdev->host->hostt->slave_configure(sdev); 96993805091SChristoph Hellwig if (ret) { 97093805091SChristoph Hellwig /* 97193805091SChristoph Hellwig * if LLDD reports slave not present, don't clutter 97293805091SChristoph Hellwig * console with alloc failure messages 97393805091SChristoph Hellwig */ 97493805091SChristoph Hellwig if (ret != -ENXIO) { 97593805091SChristoph Hellwig sdev_printk(KERN_ERR, sdev, 97693805091SChristoph Hellwig "failed to configure device\n"); 97793805091SChristoph Hellwig } 97893805091SChristoph Hellwig return SCSI_SCAN_NO_RESPONSE; 97993805091SChristoph Hellwig } 98093805091SChristoph Hellwig } 9811da177e4SLinus Torvalds 982b3ae8780SHannes Reinecke if (sdev->scsi_level >= SCSI_3) 983b3ae8780SHannes Reinecke scsi_attach_vpd(sdev); 984b3ae8780SHannes Reinecke 9854a84067dSVasu Dev sdev->max_queue_depth = sdev->queue_depth; 9864a84067dSVasu Dev 9871da177e4SLinus Torvalds /* 9881da177e4SLinus Torvalds * Ok, the device is now all set up, we can 9891da177e4SLinus Torvalds * register it and tell the rest of the kernel 9901da177e4SLinus Torvalds * about it. 9911da177e4SLinus Torvalds */ 9923e082a91SMatthew Wilcox if (!async && scsi_sysfs_add_sdev(sdev) != 0) 993b24b1033SAlan Stern return SCSI_SCAN_NO_RESPONSE; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 9961da177e4SLinus Torvalds } 9971da177e4SLinus Torvalds 998c5f2e640Sakpm@osdl.org #ifdef CONFIG_SCSI_LOGGING 9996c7154c9SKurt Garloff /** 1000eb44820cSRob Landley * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace 10016c7154c9SKurt Garloff * @buf: Output buffer with at least end-first+1 bytes of space 10026c7154c9SKurt Garloff * @inq: Inquiry buffer (input) 10036c7154c9SKurt Garloff * @first: Offset of string into inq 10046c7154c9SKurt Garloff * @end: Index after last character in inq 10056c7154c9SKurt Garloff */ 10066c7154c9SKurt Garloff static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq, 10076c7154c9SKurt Garloff unsigned first, unsigned end) 10086c7154c9SKurt Garloff { 10096c7154c9SKurt Garloff unsigned term = 0, idx; 1010c5f2e640Sakpm@osdl.org 1011c5f2e640Sakpm@osdl.org for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) { 1012c5f2e640Sakpm@osdl.org if (inq[idx+first] > ' ') { 10136c7154c9SKurt Garloff buf[idx] = inq[idx+first]; 10146c7154c9SKurt Garloff term = idx+1; 10156c7154c9SKurt Garloff } else { 10166c7154c9SKurt Garloff buf[idx] = ' '; 10176c7154c9SKurt Garloff } 10186c7154c9SKurt Garloff } 10196c7154c9SKurt Garloff buf[term] = 0; 10206c7154c9SKurt Garloff return buf; 10216c7154c9SKurt Garloff } 1022c5f2e640Sakpm@osdl.org #endif 10236f3a2024SJames Bottomley 10241da177e4SLinus Torvalds /** 10251da177e4SLinus Torvalds * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 10261da177e4SLinus Torvalds * @starget: pointer to target device structure 10271da177e4SLinus Torvalds * @lun: LUN of target device 10281da177e4SLinus Torvalds * @bflagsp: store bflags here if not NULL 1029eb44820cSRob Landley * @sdevp: probe the LUN corresponding to this scsi_device 1030eb44820cSRob Landley * @rescan: if nonzero skip some code only needed on first scan 1031eb44820cSRob Landley * @hostdata: passed to scsi_alloc_sdev() 10321da177e4SLinus Torvalds * 10331da177e4SLinus Torvalds * Description: 10341da177e4SLinus Torvalds * Call scsi_probe_lun, if a LUN with an attached device is found, 10351da177e4SLinus Torvalds * allocate and set it up by calling scsi_add_lun. 10361da177e4SLinus Torvalds * 10371da177e4SLinus Torvalds * Return: 1038f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 10391da177e4SLinus Torvalds * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is 10401da177e4SLinus Torvalds * attached at the LUN 1041f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 10421da177e4SLinus Torvalds **/ 10431da177e4SLinus Torvalds static int scsi_probe_and_add_lun(struct scsi_target *starget, 10449cb78c16SHannes Reinecke u64 lun, int *bflagsp, 10451da177e4SLinus Torvalds struct scsi_device **sdevp, int rescan, 10461da177e4SLinus Torvalds void *hostdata) 10471da177e4SLinus Torvalds { 10481da177e4SLinus Torvalds struct scsi_device *sdev; 10491da177e4SLinus Torvalds unsigned char *result; 105039216033SJames Bottomley int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256; 10511da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds /* 10541da177e4SLinus Torvalds * The rescan flag is used as an optimization, the first scan of a 10551da177e4SLinus Torvalds * host adapter calls into here with rescan == 0. 10561da177e4SLinus Torvalds */ 10571da177e4SLinus Torvalds sdev = scsi_device_lookup_by_target(starget, lun); 10581da177e4SLinus Torvalds if (sdev) { 10590f1d87a2SJames Bottomley if (rescan || !scsi_device_created(sdev)) { 106091921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 10611da177e4SLinus Torvalds "scsi scan: device exists on %s\n", 106271610f55SKay Sievers dev_name(&sdev->sdev_gendev))); 10631da177e4SLinus Torvalds if (sdevp) 10641da177e4SLinus Torvalds *sdevp = sdev; 10651da177e4SLinus Torvalds else 10661da177e4SLinus Torvalds scsi_device_put(sdev); 10671da177e4SLinus Torvalds 10681da177e4SLinus Torvalds if (bflagsp) 10691da177e4SLinus Torvalds *bflagsp = scsi_get_device_flags(sdev, 10701da177e4SLinus Torvalds sdev->vendor, 10711da177e4SLinus Torvalds sdev->model); 10721da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 10731da177e4SLinus Torvalds } 10746f3a2024SJames Bottomley scsi_device_put(sdev); 10756f3a2024SJames Bottomley } else 10761da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, lun, hostdata); 10771da177e4SLinus Torvalds if (!sdev) 10781da177e4SLinus Torvalds goto out; 107939216033SJames Bottomley 108039216033SJames Bottomley result = kmalloc(result_len, GFP_ATOMIC | 1081bc86120aSAl Viro ((shost->unchecked_isa_dma) ? __GFP_DMA : 0)); 10821da177e4SLinus Torvalds if (!result) 108339216033SJames Bottomley goto out_free_sdev; 10841da177e4SLinus Torvalds 108539216033SJames Bottomley if (scsi_probe_lun(sdev, result, result_len, &bflags)) 10861da177e4SLinus Torvalds goto out_free_result; 10871da177e4SLinus Torvalds 10884186ab19SKurt Garloff if (bflagsp) 10894186ab19SKurt Garloff *bflagsp = bflags; 10901da177e4SLinus Torvalds /* 10911da177e4SLinus Torvalds * result contains valid SCSI INQUIRY data. 10921da177e4SLinus Torvalds */ 109313f7e5acSKurt Garloff if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) { 10941da177e4SLinus Torvalds /* 10951da177e4SLinus Torvalds * For a Peripheral qualifier 3 (011b), the SCSI 10961da177e4SLinus Torvalds * spec says: The device server is not capable of 10971da177e4SLinus Torvalds * supporting a physical device on this logical 10981da177e4SLinus Torvalds * unit. 10991da177e4SLinus Torvalds * 11001da177e4SLinus Torvalds * For disks, this implies that there is no 11011da177e4SLinus Torvalds * logical disk configured at sdev->lun, but there 11021da177e4SLinus Torvalds * is a target id responding. 11031da177e4SLinus Torvalds */ 11046c7154c9SKurt Garloff SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:" 11056c7154c9SKurt Garloff " peripheral qualifier of 3, device not" 11066c7154c9SKurt Garloff " added\n")) 11076c7154c9SKurt Garloff if (lun == 0) { 1108c5f2e640Sakpm@osdl.org SCSI_LOG_SCAN_BUS(1, { 1109c5f2e640Sakpm@osdl.org unsigned char vend[9]; 1110c5f2e640Sakpm@osdl.org unsigned char mod[17]; 1111c5f2e640Sakpm@osdl.org 1112c5f2e640Sakpm@osdl.org sdev_printk(KERN_INFO, sdev, 11136c7154c9SKurt Garloff "scsi scan: consider passing scsi_mod." 11143424a65dSKurt Garloff "dev_flags=%s:%s:0x240 or 0x1000240\n", 11156c7154c9SKurt Garloff scsi_inq_str(vend, result, 8, 16), 1116c5f2e640Sakpm@osdl.org scsi_inq_str(mod, result, 16, 32)); 1117c5f2e640Sakpm@osdl.org }); 1118643eb2d9SJames Bottomley 11196c7154c9SKurt Garloff } 11206c7154c9SKurt Garloff 11211da177e4SLinus Torvalds res = SCSI_SCAN_TARGET_PRESENT; 11221da177e4SLinus Torvalds goto out_free_result; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 11251bfc5d9dSAlan Stern /* 112684961f28Sdave wysochanski * Some targets may set slight variations of PQ and PDT to signal 112784961f28Sdave wysochanski * that no LUN is present, so don't add sdev in these cases. 112884961f28Sdave wysochanski * Two specific examples are: 112984961f28Sdave wysochanski * 1) NetApp targets: return PQ=1, PDT=0x1f 113084961f28Sdave wysochanski * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" 113184961f28Sdave wysochanski * in the UFI 1.0 spec (we cannot rely on reserved bits). 113284961f28Sdave wysochanski * 113384961f28Sdave wysochanski * References: 113484961f28Sdave wysochanski * 1) SCSI SPC-3, pp. 145-146 113584961f28Sdave wysochanski * PQ=1: "A peripheral device having the specified peripheral 113684961f28Sdave wysochanski * device type is not connected to this logical unit. However, the 113784961f28Sdave wysochanski * device server is capable of supporting the specified peripheral 113884961f28Sdave wysochanski * device type on this logical unit." 113984961f28Sdave wysochanski * PDT=0x1f: "Unknown or no device type" 114084961f28Sdave wysochanski * 2) USB UFI 1.0, p. 20 114184961f28Sdave wysochanski * PDT=00h Direct-access device (floppy) 114284961f28Sdave wysochanski * PDT=1Fh none (no FDD connected to the requested logical unit) 11431bfc5d9dSAlan Stern */ 114484961f28Sdave wysochanski if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) && 114501b291bdSJames Bottomley (result[0] & 0x1f) == 0x1f && 114601b291bdSJames Bottomley !scsi_is_wlun(lun)) { 114791921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 11481bfc5d9dSAlan Stern "scsi scan: peripheral device type" 11491bfc5d9dSAlan Stern " of 31, no device added\n")); 11501bfc5d9dSAlan Stern res = SCSI_SCAN_TARGET_PRESENT; 11511bfc5d9dSAlan Stern goto out_free_result; 11521bfc5d9dSAlan Stern } 11531bfc5d9dSAlan Stern 11543e082a91SMatthew Wilcox res = scsi_add_lun(sdev, result, &bflags, shost->async_scan); 11551da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 11561da177e4SLinus Torvalds if (bflags & BLIST_KEY) { 11571da177e4SLinus Torvalds sdev->lockable = 0; 115839216033SJames Bottomley scsi_unlock_floptical(sdev, result); 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds } 11611da177e4SLinus Torvalds 11621da177e4SLinus Torvalds out_free_result: 11631da177e4SLinus Torvalds kfree(result); 11641da177e4SLinus Torvalds out_free_sdev: 11651da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 11661da177e4SLinus Torvalds if (sdevp) { 1167b70d37bfSAlan Stern if (scsi_device_get(sdev) == 0) { 11681da177e4SLinus Torvalds *sdevp = sdev; 1169b70d37bfSAlan Stern } else { 1170b70d37bfSAlan Stern __scsi_remove_device(sdev); 1171b70d37bfSAlan Stern res = SCSI_SCAN_NO_RESPONSE; 1172b70d37bfSAlan Stern } 11731da177e4SLinus Torvalds } 11746f3a2024SJames Bottomley } else 1175860dc736SJames Bottomley __scsi_remove_device(sdev); 11761da177e4SLinus Torvalds out: 11771da177e4SLinus Torvalds return res; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds /** 11811da177e4SLinus Torvalds * scsi_sequential_lun_scan - sequentially scan a SCSI target 11821da177e4SLinus Torvalds * @starget: pointer to target structure to scan 11831da177e4SLinus Torvalds * @bflags: black/white list flag for LUN 0 1184eb44820cSRob Landley * @scsi_level: Which version of the standard does this device adhere to 1185eb44820cSRob Landley * @rescan: passed to scsi_probe_add_lun() 11861da177e4SLinus Torvalds * 11871da177e4SLinus Torvalds * Description: 11881da177e4SLinus Torvalds * Generally, scan from LUN 1 (LUN 0 is assumed to already have been 11891da177e4SLinus Torvalds * scanned) to some maximum lun until a LUN is found with no device 11901da177e4SLinus Torvalds * attached. Use the bflags to figure out any oddities. 11911da177e4SLinus Torvalds * 11921da177e4SLinus Torvalds * Modifies sdevscan->lun. 11931da177e4SLinus Torvalds **/ 11941da177e4SLinus Torvalds static void scsi_sequential_lun_scan(struct scsi_target *starget, 11954186ab19SKurt Garloff int bflags, int scsi_level, int rescan) 11961da177e4SLinus Torvalds { 11979cb78c16SHannes Reinecke uint max_dev_lun; 11989cb78c16SHannes Reinecke u64 sparse_lun, lun; 11991da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 12001da177e4SLinus Torvalds 120191921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, starget_printk(KERN_INFO, starget, 120291921e01SHannes Reinecke "scsi scan: Sequential scan\n")); 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds max_dev_lun = min(max_scsi_luns, shost->max_lun); 12051da177e4SLinus Torvalds /* 12061da177e4SLinus Torvalds * If this device is known to support sparse multiple units, 12071da177e4SLinus Torvalds * override the other settings, and scan all of them. Normally, 12081da177e4SLinus Torvalds * SCSI-3 devices should be scanned via the REPORT LUNS. 12091da177e4SLinus Torvalds */ 12101da177e4SLinus Torvalds if (bflags & BLIST_SPARSELUN) { 12111da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 12121da177e4SLinus Torvalds sparse_lun = 1; 12131da177e4SLinus Torvalds } else 12141da177e4SLinus Torvalds sparse_lun = 0; 12151da177e4SLinus Torvalds 12161da177e4SLinus Torvalds /* 1217fb0d82f4SMark Knibbs * If less than SCSI_1_CCS, and no special lun scanning, stop 12181da177e4SLinus Torvalds * scanning; this matches 2.4 behaviour, but could just be a bug 1219fb0d82f4SMark Knibbs * (to continue scanning a SCSI_1_CCS device). 12201da177e4SLinus Torvalds * 12211da177e4SLinus Torvalds * This test is broken. We might not have any device on lun0 for 12221da177e4SLinus Torvalds * a sparselun device, and if that's the case then how would we 12231da177e4SLinus Torvalds * know the real scsi_level, eh? It might make sense to just not 12241da177e4SLinus Torvalds * scan any SCSI_1 device for non-0 luns, but that check would best 12251da177e4SLinus Torvalds * go into scsi_alloc_sdev() and just have it return null when asked 12261da177e4SLinus Torvalds * to alloc an sdev for lun > 0 on an already found SCSI_1 device. 12271da177e4SLinus Torvalds * 12281da177e4SLinus Torvalds if ((sdevscan->scsi_level < SCSI_1_CCS) && 12291da177e4SLinus Torvalds ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) 12301da177e4SLinus Torvalds == 0)) 12311da177e4SLinus Torvalds return; 12321da177e4SLinus Torvalds */ 12331da177e4SLinus Torvalds /* 12341da177e4SLinus Torvalds * If this device is known to support multiple units, override 12351da177e4SLinus Torvalds * the other settings, and scan all of them. 12361da177e4SLinus Torvalds */ 12371da177e4SLinus Torvalds if (bflags & BLIST_FORCELUN) 12381da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 12391da177e4SLinus Torvalds /* 12401da177e4SLinus Torvalds * REGAL CDC-4X: avoid hang after LUN 4 12411da177e4SLinus Torvalds */ 12421da177e4SLinus Torvalds if (bflags & BLIST_MAX5LUN) 12431da177e4SLinus Torvalds max_dev_lun = min(5U, max_dev_lun); 12441da177e4SLinus Torvalds /* 12451da177e4SLinus Torvalds * Do not scan SCSI-2 or lower device past LUN 7, unless 12461da177e4SLinus Torvalds * BLIST_LARGELUN. 12471da177e4SLinus Torvalds */ 12481da177e4SLinus Torvalds if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) 12491da177e4SLinus Torvalds max_dev_lun = min(8U, max_dev_lun); 12501da177e4SLinus Torvalds 12511da177e4SLinus Torvalds /* 125222ffeb48SHannes Reinecke * Stop scanning at 255 unless BLIST_SCSI3LUN 125322ffeb48SHannes Reinecke */ 125422ffeb48SHannes Reinecke if (!(bflags & BLIST_SCSI3LUN)) 125522ffeb48SHannes Reinecke max_dev_lun = min(256U, max_dev_lun); 125622ffeb48SHannes Reinecke 125722ffeb48SHannes Reinecke /* 12581da177e4SLinus Torvalds * We have already scanned LUN 0, so start at LUN 1. Keep scanning 12591da177e4SLinus Torvalds * until we reach the max, or no LUN is found and we are not 12601da177e4SLinus Torvalds * sparse_lun. 12611da177e4SLinus Torvalds */ 12621da177e4SLinus Torvalds for (lun = 1; lun < max_dev_lun; ++lun) 12631da177e4SLinus Torvalds if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, 12641da177e4SLinus Torvalds NULL) != SCSI_SCAN_LUN_PRESENT) && 12651da177e4SLinus Torvalds !sparse_lun) 12661da177e4SLinus Torvalds return; 12671da177e4SLinus Torvalds } 12681da177e4SLinus Torvalds 12691da177e4SLinus Torvalds /** 12709f6aa575SRandy Dunlap * scsilun_to_int - convert a scsi_lun to an int 12711da177e4SLinus Torvalds * @scsilun: struct scsi_lun to be converted. 12721da177e4SLinus Torvalds * 12731da177e4SLinus Torvalds * Description: 12741da177e4SLinus Torvalds * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered 12751da177e4SLinus Torvalds * integer, and return the result. The caller must check for 12761da177e4SLinus Torvalds * truncation before using this function. 12771da177e4SLinus Torvalds * 12781da177e4SLinus Torvalds * Notes: 12791da177e4SLinus Torvalds * For a description of the LUN format, post SCSI-3 see the SCSI 12801da177e4SLinus Torvalds * Architecture Model, for SCSI-3 see the SCSI Controller Commands. 12811da177e4SLinus Torvalds * 1282d9e5d618SHannes Reinecke * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function 1283d9e5d618SHannes Reinecke * returns the integer: 0x0b03d204 1284d9e5d618SHannes Reinecke * 1285d9e5d618SHannes Reinecke * This encoding will return a standard integer LUN for LUNs smaller 1286d9e5d618SHannes Reinecke * than 256, which typically use a single level LUN structure with 1287d9e5d618SHannes Reinecke * addressing method 0. 12881da177e4SLinus Torvalds **/ 12899cb78c16SHannes Reinecke u64 scsilun_to_int(struct scsi_lun *scsilun) 12901da177e4SLinus Torvalds { 12911da177e4SLinus Torvalds int i; 12929cb78c16SHannes Reinecke u64 lun; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds lun = 0; 12951da177e4SLinus Torvalds for (i = 0; i < sizeof(lun); i += 2) 1296d9e5d618SHannes Reinecke lun = lun | (((u64)scsilun->scsi_lun[i] << ((i + 1) * 8)) | 1297d9e5d618SHannes Reinecke ((u64)scsilun->scsi_lun[i + 1] << (i * 8))); 12981da177e4SLinus Torvalds return lun; 12991da177e4SLinus Torvalds } 1300462b7859SChristof Schmitt EXPORT_SYMBOL(scsilun_to_int); 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds /** 13039f6aa575SRandy Dunlap * int_to_scsilun - reverts an int into a scsi_lun 1304eb44820cSRob Landley * @lun: integer to be reverted 13052f4701d8SJames.Smart@Emulex.Com * @scsilun: struct scsi_lun to be set. 13062f4701d8SJames.Smart@Emulex.Com * 13072f4701d8SJames.Smart@Emulex.Com * Description: 13082f4701d8SJames.Smart@Emulex.Com * Reverts the functionality of the scsilun_to_int, which packed 13092f4701d8SJames.Smart@Emulex.Com * an 8-byte lun value into an int. This routine unpacks the int 13102f4701d8SJames.Smart@Emulex.Com * back into the lun value. 13112f4701d8SJames.Smart@Emulex.Com * 13122f4701d8SJames.Smart@Emulex.Com * Notes: 1313d9e5d618SHannes Reinecke * Given an integer : 0x0b03d204, this function returns a 1314d9e5d618SHannes Reinecke * struct scsi_lun of: d2 04 0b 03 00 00 00 00 13152f4701d8SJames.Smart@Emulex.Com * 13162f4701d8SJames.Smart@Emulex.Com **/ 13179cb78c16SHannes Reinecke void int_to_scsilun(u64 lun, struct scsi_lun *scsilun) 13182f4701d8SJames.Smart@Emulex.Com { 13192f4701d8SJames.Smart@Emulex.Com int i; 13202f4701d8SJames.Smart@Emulex.Com 13212f4701d8SJames.Smart@Emulex.Com memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun)); 13222f4701d8SJames.Smart@Emulex.Com 13232f4701d8SJames.Smart@Emulex.Com for (i = 0; i < sizeof(lun); i += 2) { 13242f4701d8SJames.Smart@Emulex.Com scsilun->scsi_lun[i] = (lun >> 8) & 0xFF; 13252f4701d8SJames.Smart@Emulex.Com scsilun->scsi_lun[i+1] = lun & 0xFF; 13262f4701d8SJames.Smart@Emulex.Com lun = lun >> 16; 13272f4701d8SJames.Smart@Emulex.Com } 13282f4701d8SJames.Smart@Emulex.Com } 13292f4701d8SJames.Smart@Emulex.Com EXPORT_SYMBOL(int_to_scsilun); 13302f4701d8SJames.Smart@Emulex.Com 13312f4701d8SJames.Smart@Emulex.Com /** 13321da177e4SLinus Torvalds * scsi_report_lun_scan - Scan using SCSI REPORT LUN results 1333eb44820cSRob Landley * @starget: which target 1334eb44820cSRob Landley * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN 1335eb44820cSRob Landley * @rescan: nonzero if we can skip code only needed on first scan 13361da177e4SLinus Torvalds * 13371da177e4SLinus Torvalds * Description: 1338eb44820cSRob Landley * Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command. 1339eb44820cSRob Landley * Scan the resulting list of LUNs by calling scsi_probe_and_add_lun. 13401da177e4SLinus Torvalds * 1341eb44820cSRob Landley * If BLINK_REPORTLUN2 is set, scan a target that supports more than 8 1342eb44820cSRob Landley * LUNs even if it's older than SCSI-3. 1343eb44820cSRob Landley * If BLIST_NOREPORTLUN is set, return 1 always. 1344eb44820cSRob Landley * If BLIST_NOLUN is set, return 0 always. 134509b6b51bSAlan Stern * If starget->no_report_luns is set, return 1 always. 13461da177e4SLinus Torvalds * 13471da177e4SLinus Torvalds * Return: 13481da177e4SLinus Torvalds * 0: scan completed (or no memory, so further scanning is futile) 1349eb44820cSRob Landley * 1: could not scan with REPORT LUN 13501da177e4SLinus Torvalds **/ 13516f3a2024SJames Bottomley static int scsi_report_lun_scan(struct scsi_target *starget, int bflags, 13521da177e4SLinus Torvalds int rescan) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds char devname[64]; 13551da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 13561da177e4SLinus Torvalds unsigned int length; 13579cb78c16SHannes Reinecke u64 lun; 13581da177e4SLinus Torvalds unsigned int num_luns; 13591da177e4SLinus Torvalds unsigned int retries; 136039216033SJames Bottomley int result; 13611da177e4SLinus Torvalds struct scsi_lun *lunp, *lun_data; 13621da177e4SLinus Torvalds u8 *data; 13631da177e4SLinus Torvalds struct scsi_sense_hdr sshdr; 13646f3a2024SJames Bottomley struct scsi_device *sdev; 13656f3a2024SJames Bottomley struct Scsi_Host *shost = dev_to_shost(&starget->dev); 13662ef89198SAlan Stern int ret = 0; 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /* 13691da177e4SLinus Torvalds * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set. 13701da177e4SLinus Torvalds * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does 13711da177e4SLinus Torvalds * support more than 8 LUNs. 137209b6b51bSAlan Stern * Don't attempt if the target doesn't support REPORT LUNS. 13731da177e4SLinus Torvalds */ 13744d7db04aSJames Bottomley if (bflags & BLIST_NOREPORTLUN) 13754d7db04aSJames Bottomley return 1; 13764d7db04aSJames Bottomley if (starget->scsi_level < SCSI_2 && 13774d7db04aSJames Bottomley starget->scsi_level != SCSI_UNKNOWN) 13784d7db04aSJames Bottomley return 1; 13794d7db04aSJames Bottomley if (starget->scsi_level < SCSI_3 && 13804d7db04aSJames Bottomley (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) 13811da177e4SLinus Torvalds return 1; 13821da177e4SLinus Torvalds if (bflags & BLIST_NOLUN) 13831da177e4SLinus Torvalds return 0; 138409b6b51bSAlan Stern if (starget->no_report_luns) 138509b6b51bSAlan Stern return 1; 13861da177e4SLinus Torvalds 13876f3a2024SJames Bottomley if (!(sdev = scsi_device_lookup_by_target(starget, 0))) { 13886f3a2024SJames Bottomley sdev = scsi_alloc_sdev(starget, 0, NULL); 13896f3a2024SJames Bottomley if (!sdev) 13906f3a2024SJames Bottomley return 0; 139175f8ee8eSAlan Stern if (scsi_device_get(sdev)) { 139275f8ee8eSAlan Stern __scsi_remove_device(sdev); 13936f3a2024SJames Bottomley return 0; 13946f3a2024SJames Bottomley } 139575f8ee8eSAlan Stern } 13966f3a2024SJames Bottomley 13971da177e4SLinus Torvalds sprintf(devname, "host %d channel %d id %d", 13986f3a2024SJames Bottomley shost->host_no, sdev->channel, sdev->id); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds /* 14011da177e4SLinus Torvalds * Allocate enough to hold the header (the same size as one scsi_lun) 14021da177e4SLinus Torvalds * plus the max number of luns we are requesting. 14031da177e4SLinus Torvalds * 14041da177e4SLinus Torvalds * Reallocating and trying again (with the exact amount we need) 14051da177e4SLinus Torvalds * would be nice, but then we need to somehow limit the size 14061da177e4SLinus Torvalds * allocated based on the available memory and the limits of 14071da177e4SLinus Torvalds * kmalloc - we don't want a kmalloc() failure of a huge value to 14081da177e4SLinus Torvalds * prevent us from finding any LUNs on this target. 14091da177e4SLinus Torvalds */ 14101da177e4SLinus Torvalds length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun); 14111da177e4SLinus Torvalds lun_data = kmalloc(length, GFP_ATOMIC | 14121da177e4SLinus Torvalds (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0)); 14136f3a2024SJames Bottomley if (!lun_data) { 1414cadbd4a5SHarvey Harrison printk(ALLOC_FAILURE_MSG, __func__); 141539216033SJames Bottomley goto out; 14166f3a2024SJames Bottomley } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds scsi_cmd[0] = REPORT_LUNS; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds /* 14211da177e4SLinus Torvalds * bytes 1 - 5: reserved, set to zero. 14221da177e4SLinus Torvalds */ 14231da177e4SLinus Torvalds memset(&scsi_cmd[1], 0, 5); 14241da177e4SLinus Torvalds 14251da177e4SLinus Torvalds /* 14261da177e4SLinus Torvalds * bytes 6 - 9: length of the command. 14271da177e4SLinus Torvalds */ 14281da177e4SLinus Torvalds scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff; 14291da177e4SLinus Torvalds scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff; 14301da177e4SLinus Torvalds scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff; 14311da177e4SLinus Torvalds scsi_cmd[9] = (unsigned char) length & 0xff; 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds scsi_cmd[10] = 0; /* reserved */ 14341da177e4SLinus Torvalds scsi_cmd[11] = 0; /* control */ 14351da177e4SLinus Torvalds 14361da177e4SLinus Torvalds /* 14371da177e4SLinus Torvalds * We can get a UNIT ATTENTION, for example a power on/reset, so 14381da177e4SLinus Torvalds * retry a few times (like sd.c does for TEST UNIT READY). 14391da177e4SLinus Torvalds * Experience shows some combinations of adapter/devices get at 14401da177e4SLinus Torvalds * least two power on/resets. 14411da177e4SLinus Torvalds * 14421da177e4SLinus Torvalds * Illegal requests (for devices that do not support REPORT LUNS) 14431da177e4SLinus Torvalds * should come through as a check condition, and will not generate 14441da177e4SLinus Torvalds * a retry. 14451da177e4SLinus Torvalds */ 14461da177e4SLinus Torvalds for (retries = 0; retries < 3; retries++) { 144791921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 144891921e01SHannes Reinecke "scsi scan: Sending REPORT LUNS to (try %d)\n", 14491da177e4SLinus Torvalds retries)); 145039216033SJames Bottomley 145139216033SJames Bottomley result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, 1452ea73a9f2SJames Bottomley lun_data, length, &sshdr, 1453f4f4e47eSFUJITA Tomonori SCSI_TIMEOUT + 4 * HZ, 3, NULL); 145439216033SJames Bottomley 145591921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 145691921e01SHannes Reinecke "scsi scan: REPORT LUNS" 145791921e01SHannes Reinecke " %s (try %d) result 0x%x\n", 145891921e01SHannes Reinecke result ? "failed" : "successful", 145991921e01SHannes Reinecke retries, result)); 146039216033SJames Bottomley if (result == 0) 14611da177e4SLinus Torvalds break; 1462ea73a9f2SJames Bottomley else if (scsi_sense_valid(&sshdr)) { 14631da177e4SLinus Torvalds if (sshdr.sense_key != UNIT_ATTENTION) 14641da177e4SLinus Torvalds break; 14651da177e4SLinus Torvalds } 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 146839216033SJames Bottomley if (result) { 14691da177e4SLinus Torvalds /* 14701da177e4SLinus Torvalds * The device probably does not support a REPORT LUN command 14711da177e4SLinus Torvalds */ 14722ef89198SAlan Stern ret = 1; 14732ef89198SAlan Stern goto out_err; 14741da177e4SLinus Torvalds } 14751da177e4SLinus Torvalds 14761da177e4SLinus Torvalds /* 14771da177e4SLinus Torvalds * Get the length from the first four bytes of lun_data. 14781da177e4SLinus Torvalds */ 14791da177e4SLinus Torvalds data = (u8 *) lun_data->scsi_lun; 14801da177e4SLinus Torvalds length = ((data[0] << 24) | (data[1] << 16) | 14811da177e4SLinus Torvalds (data[2] << 8) | (data[3] << 0)); 14821da177e4SLinus Torvalds 14831da177e4SLinus Torvalds num_luns = (length / sizeof(struct scsi_lun)); 14841da177e4SLinus Torvalds if (num_luns > max_scsi_report_luns) { 148591921e01SHannes Reinecke sdev_printk(KERN_WARNING, sdev, 148691921e01SHannes Reinecke "Only %d (max_scsi_report_luns)" 14871da177e4SLinus Torvalds " of %d luns reported, try increasing" 148891921e01SHannes Reinecke " max_scsi_report_luns.\n", 14891da177e4SLinus Torvalds max_scsi_report_luns, num_luns); 14901da177e4SLinus Torvalds num_luns = max_scsi_report_luns; 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds 14933bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 14943bf743e7SJeff Garzik "scsi scan: REPORT LUN scan\n")); 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds /* 14971da177e4SLinus Torvalds * Scan the luns in lun_data. The entry at offset 0 is really 14981da177e4SLinus Torvalds * the header, so start at 1 and go up to and including num_luns. 14991da177e4SLinus Torvalds */ 15001da177e4SLinus Torvalds for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) { 15011da177e4SLinus Torvalds lun = scsilun_to_int(lunp); 15021da177e4SLinus Torvalds 15039cb78c16SHannes Reinecke if (lun > sdev->host->max_lun) { 150491921e01SHannes Reinecke sdev_printk(KERN_WARNING, sdev, 150591921e01SHannes Reinecke "lun%llu has a LUN larger than" 150691921e01SHannes Reinecke " allowed by the host adapter\n", lun); 15071da177e4SLinus Torvalds } else { 15081da177e4SLinus Torvalds int res; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds res = scsi_probe_and_add_lun(starget, 15111da177e4SLinus Torvalds lun, NULL, NULL, rescan, NULL); 15121da177e4SLinus Torvalds if (res == SCSI_SCAN_NO_RESPONSE) { 15131da177e4SLinus Torvalds /* 15141da177e4SLinus Torvalds * Got some results, but now none, abort. 15151da177e4SLinus Torvalds */ 15163bf743e7SJeff Garzik sdev_printk(KERN_ERR, sdev, 15173bf743e7SJeff Garzik "Unexpected response" 15189cb78c16SHannes Reinecke " from lun %llu while scanning, scan" 15199cb78c16SHannes Reinecke " aborted\n", (unsigned long long)lun); 15201da177e4SLinus Torvalds break; 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds } 15231da177e4SLinus Torvalds } 15241da177e4SLinus Torvalds 15252ef89198SAlan Stern out_err: 15261da177e4SLinus Torvalds kfree(lun_data); 15271da177e4SLinus Torvalds out: 15286f3a2024SJames Bottomley scsi_device_put(sdev); 15290f1d87a2SJames Bottomley if (scsi_device_created(sdev)) 15301da177e4SLinus Torvalds /* 15316f3a2024SJames Bottomley * the sdev we used didn't appear in the report luns scan 15321da177e4SLinus Torvalds */ 1533860dc736SJames Bottomley __scsi_remove_device(sdev); 15342ef89198SAlan Stern return ret; 15351da177e4SLinus Torvalds } 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel, 15389cb78c16SHannes Reinecke uint id, u64 lun, void *hostdata) 15391da177e4SLinus Torvalds { 1540a97a83a0SMatthew Wilcox struct scsi_device *sdev = ERR_PTR(-ENODEV); 15411da177e4SLinus Torvalds struct device *parent = &shost->shost_gendev; 1542e02f3f59SChristoph Hellwig struct scsi_target *starget; 15431da177e4SLinus Torvalds 1544938e2ac0SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 1545938e2ac0SMatthew Wilcox return ERR_PTR(-ENODEV); 1546938e2ac0SMatthew Wilcox 1547e02f3f59SChristoph Hellwig starget = scsi_alloc_target(parent, channel, id); 15481da177e4SLinus Torvalds if (!starget) 15491da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 1550bc4f2401SAlan Stern scsi_autopm_get_target(starget); 15511da177e4SLinus Torvalds 15520b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 15536b7f123fSMatthew Wilcox if (!shost->async_scan) 15546b7f123fSMatthew Wilcox scsi_complete_async_scans(); 15556b7f123fSMatthew Wilcox 1556bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1557a97a83a0SMatthew Wilcox scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata); 1558bc4f2401SAlan Stern scsi_autopm_put_host(shost); 1559bc4f2401SAlan Stern } 15600b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 1561bc4f2401SAlan Stern scsi_autopm_put_target(starget); 1562e63ed0d7SJames Bottomley /* 1563e63ed0d7SJames Bottomley * paired with scsi_alloc_target(). Target will be destroyed unless 1564e63ed0d7SJames Bottomley * scsi_probe_and_add_lun made an underlying device visible 1565e63ed0d7SJames Bottomley */ 15661da177e4SLinus Torvalds scsi_target_reap(starget); 15671da177e4SLinus Torvalds put_device(&starget->dev); 15681da177e4SLinus Torvalds 15691da177e4SLinus Torvalds return sdev; 15701da177e4SLinus Torvalds } 15711da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_add_device); 15721da177e4SLinus Torvalds 1573146f7262SJames Bottomley int scsi_add_device(struct Scsi_Host *host, uint channel, 15749cb78c16SHannes Reinecke uint target, u64 lun) 1575146f7262SJames Bottomley { 1576146f7262SJames Bottomley struct scsi_device *sdev = 1577146f7262SJames Bottomley __scsi_add_device(host, channel, target, lun, NULL); 1578146f7262SJames Bottomley if (IS_ERR(sdev)) 1579146f7262SJames Bottomley return PTR_ERR(sdev); 1580146f7262SJames Bottomley 1581146f7262SJames Bottomley scsi_device_put(sdev); 1582146f7262SJames Bottomley return 0; 1583146f7262SJames Bottomley } 1584146f7262SJames Bottomley EXPORT_SYMBOL(scsi_add_device); 1585146f7262SJames Bottomley 15861da177e4SLinus Torvalds void scsi_rescan_device(struct device *dev) 15871da177e4SLinus Torvalds { 15881da177e4SLinus Torvalds struct scsi_driver *drv; 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds if (!dev->driver) 15911da177e4SLinus Torvalds return; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds drv = to_scsi_driver(dev->driver); 15941da177e4SLinus Torvalds if (try_module_get(drv->owner)) { 15951da177e4SLinus Torvalds if (drv->rescan) 15961da177e4SLinus Torvalds drv->rescan(dev); 15971da177e4SLinus Torvalds module_put(drv->owner); 15981da177e4SLinus Torvalds } 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_rescan_device); 16011da177e4SLinus Torvalds 1602e517d313SAlan Stern static void __scsi_scan_target(struct device *parent, unsigned int channel, 16039cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 16061da177e4SLinus Torvalds int bflags = 0; 16071da177e4SLinus Torvalds int res; 16081da177e4SLinus Torvalds struct scsi_target *starget; 16091da177e4SLinus Torvalds 16101da177e4SLinus Torvalds if (shost->this_id == id) 16111da177e4SLinus Torvalds /* 16121da177e4SLinus Torvalds * Don't scan the host adapter 16131da177e4SLinus Torvalds */ 16141da177e4SLinus Torvalds return; 16151da177e4SLinus Torvalds 16161da177e4SLinus Torvalds starget = scsi_alloc_target(parent, channel, id); 16171da177e4SLinus Torvalds if (!starget) 16181da177e4SLinus Torvalds return; 1619bc4f2401SAlan Stern scsi_autopm_get_target(starget); 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds if (lun != SCAN_WILD_CARD) { 16221da177e4SLinus Torvalds /* 16231da177e4SLinus Torvalds * Scan for a specific host/chan/id/lun. 16241da177e4SLinus Torvalds */ 16251da177e4SLinus Torvalds scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL); 16261da177e4SLinus Torvalds goto out_reap; 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds /* 16301da177e4SLinus Torvalds * Scan LUN 0, if there is some response, scan further. Ideally, we 16311da177e4SLinus Torvalds * would not configure LUN 0 until all LUNs are scanned. 16321da177e4SLinus Torvalds */ 16336f3a2024SJames Bottomley res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL); 16346f3a2024SJames Bottomley if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) { 16356f3a2024SJames Bottomley if (scsi_report_lun_scan(starget, bflags, rescan) != 0) 16361da177e4SLinus Torvalds /* 16371da177e4SLinus Torvalds * The REPORT LUN did not scan the target, 16381da177e4SLinus Torvalds * do a sequential scan. 16391da177e4SLinus Torvalds */ 16401da177e4SLinus Torvalds scsi_sequential_lun_scan(starget, bflags, 16414186ab19SKurt Garloff starget->scsi_level, rescan); 16421da177e4SLinus Torvalds } 16431da177e4SLinus Torvalds 16441da177e4SLinus Torvalds out_reap: 1645bc4f2401SAlan Stern scsi_autopm_put_target(starget); 1646e63ed0d7SJames Bottomley /* 1647e63ed0d7SJames Bottomley * paired with scsi_alloc_target(): determine if the target has 1648e63ed0d7SJames Bottomley * any children at all and if not, nuke it 1649e63ed0d7SJames Bottomley */ 16501da177e4SLinus Torvalds scsi_target_reap(starget); 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds put_device(&starget->dev); 16531da177e4SLinus Torvalds } 1654e517d313SAlan Stern 1655e517d313SAlan Stern /** 1656e59e4a09SRandy Dunlap * scsi_scan_target - scan a target id, possibly including all LUNs on the target. 1657e517d313SAlan Stern * @parent: host to scan 1658e517d313SAlan Stern * @channel: channel to scan 1659e517d313SAlan Stern * @id: target id to scan 1660e517d313SAlan Stern * @lun: Specific LUN to scan or SCAN_WILD_CARD 1661e517d313SAlan Stern * @rescan: passed to LUN scanning routines 1662e517d313SAlan Stern * 1663e517d313SAlan Stern * Description: 1664e517d313SAlan Stern * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0, 1665e517d313SAlan Stern * and possibly all LUNs on the target id. 1666e517d313SAlan Stern * 1667e517d313SAlan Stern * First try a REPORT LUN scan, if that does not scan the target, do a 1668e517d313SAlan Stern * sequential scan of LUNs on the target id. 1669e517d313SAlan Stern **/ 1670e517d313SAlan Stern void scsi_scan_target(struct device *parent, unsigned int channel, 16719cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 1672e517d313SAlan Stern { 1673e517d313SAlan Stern struct Scsi_Host *shost = dev_to_shost(parent); 1674e517d313SAlan Stern 167593b45af5SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 167693b45af5SMatthew Wilcox return; 167793b45af5SMatthew Wilcox 16786b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 16793e082a91SMatthew Wilcox if (!shost->async_scan) 16803e082a91SMatthew Wilcox scsi_complete_async_scans(); 16813e082a91SMatthew Wilcox 1682bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1683e517d313SAlan Stern __scsi_scan_target(parent, channel, id, lun, rescan); 1684bc4f2401SAlan Stern scsi_autopm_put_host(shost); 1685bc4f2401SAlan Stern } 16860b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 1687e517d313SAlan Stern } 16881da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_target); 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel, 16919cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 16921da177e4SLinus Torvalds { 16931da177e4SLinus Torvalds uint order_id; 16941da177e4SLinus Torvalds 16951da177e4SLinus Torvalds if (id == SCAN_WILD_CARD) 16961da177e4SLinus Torvalds for (id = 0; id < shost->max_id; ++id) { 16971da177e4SLinus Torvalds /* 16981da177e4SLinus Torvalds * XXX adapter drivers when possible (FCP, iSCSI) 16991da177e4SLinus Torvalds * could modify max_id to match the current max, 17001da177e4SLinus Torvalds * not the absolute max. 17011da177e4SLinus Torvalds * 17021da177e4SLinus Torvalds * XXX add a shost id iterator, so for example, 17031da177e4SLinus Torvalds * the FC ID can be the same as a target id 17041da177e4SLinus Torvalds * without a huge overhead of sparse id's. 17051da177e4SLinus Torvalds */ 17061da177e4SLinus Torvalds if (shost->reverse_ordering) 17071da177e4SLinus Torvalds /* 17081da177e4SLinus Torvalds * Scan from high to low id. 17091da177e4SLinus Torvalds */ 17101da177e4SLinus Torvalds order_id = shost->max_id - id - 1; 17111da177e4SLinus Torvalds else 17121da177e4SLinus Torvalds order_id = id; 1713e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1714e517d313SAlan Stern order_id, lun, rescan); 17151da177e4SLinus Torvalds } 17161da177e4SLinus Torvalds else 1717e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1718e517d313SAlan Stern id, lun, rescan); 17191da177e4SLinus Torvalds } 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel, 17229cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 17231da177e4SLinus Torvalds { 17243bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost, 17259cb78c16SHannes Reinecke "%s: <%u:%u:%llu>\n", 1726cadbd4a5SHarvey Harrison __func__, channel, id, lun)); 17271da177e4SLinus Torvalds 17281da177e4SLinus Torvalds if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || 1729091686d3SAmit Arora ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || 1730*605c6dbeSMark Knibbs ((lun != SCAN_WILD_CARD) && (lun >= shost->max_lun))) 17311da177e4SLinus Torvalds return -EINVAL; 17321da177e4SLinus Torvalds 17330b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 17346b7f123fSMatthew Wilcox if (!shost->async_scan) 17356b7f123fSMatthew Wilcox scsi_complete_async_scans(); 17366b7f123fSMatthew Wilcox 1737bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 17381da177e4SLinus Torvalds if (channel == SCAN_WILD_CARD) 173982f29467SMike Anderson for (channel = 0; channel <= shost->max_channel; 174082f29467SMike Anderson channel++) 174182f29467SMike Anderson scsi_scan_channel(shost, channel, id, lun, 174282f29467SMike Anderson rescan); 17431da177e4SLinus Torvalds else 17441da177e4SLinus Torvalds scsi_scan_channel(shost, channel, id, lun, rescan); 1745bc4f2401SAlan Stern scsi_autopm_put_host(shost); 174682f29467SMike Anderson } 17470b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds return 0; 17501da177e4SLinus Torvalds } 17511da177e4SLinus Torvalds 17523e082a91SMatthew Wilcox static void scsi_sysfs_add_devices(struct Scsi_Host *shost) 17533e082a91SMatthew Wilcox { 17543e082a91SMatthew Wilcox struct scsi_device *sdev; 17553e082a91SMatthew Wilcox shost_for_each_device(sdev, shost) { 17563b661a92SDan Williams /* target removed before the device could be added */ 17573b661a92SDan Williams if (sdev->sdev_state == SDEV_DEL) 17583b661a92SDan Williams continue; 1759693ad5baSSubhash Jadavani /* If device is already visible, skip adding it to sysfs */ 1760693ad5baSSubhash Jadavani if (sdev->is_visible) 1761693ad5baSSubhash Jadavani continue; 17626b7f123fSMatthew Wilcox if (!scsi_host_scan_allowed(shost) || 17636b7f123fSMatthew Wilcox scsi_sysfs_add_sdev(sdev) != 0) 1764860dc736SJames Bottomley __scsi_remove_device(sdev); 17653e082a91SMatthew Wilcox } 17663e082a91SMatthew Wilcox } 17673e082a91SMatthew Wilcox 17683e082a91SMatthew Wilcox /** 17693e082a91SMatthew Wilcox * scsi_prep_async_scan - prepare for an async scan 17703e082a91SMatthew Wilcox * @shost: the host which will be scanned 17713e082a91SMatthew Wilcox * Returns: a cookie to be passed to scsi_finish_async_scan() 17723e082a91SMatthew Wilcox * 17733e082a91SMatthew Wilcox * Tells the midlayer this host is going to do an asynchronous scan. 17743e082a91SMatthew Wilcox * It reserves the host's position in the scanning list and ensures 17753e082a91SMatthew Wilcox * that other asynchronous scans started after this one won't affect the 17763e082a91SMatthew Wilcox * ordering of the discovered devices. 17773e082a91SMatthew Wilcox */ 17781aa8fab2SMatthew Wilcox static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) 17793e082a91SMatthew Wilcox { 17803e082a91SMatthew Wilcox struct async_scan_data *data; 17816b7f123fSMatthew Wilcox unsigned long flags; 17823e082a91SMatthew Wilcox 17833e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "sync", 4) == 0) 17843e082a91SMatthew Wilcox return NULL; 17853e082a91SMatthew Wilcox 17863e082a91SMatthew Wilcox if (shost->async_scan) { 178791921e01SHannes Reinecke shost_printk(KERN_INFO, shost, "%s called twice\n", __func__); 17883e082a91SMatthew Wilcox dump_stack(); 17893e082a91SMatthew Wilcox return NULL; 17903e082a91SMatthew Wilcox } 17913e082a91SMatthew Wilcox 17923e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL); 17933e082a91SMatthew Wilcox if (!data) 17943e082a91SMatthew Wilcox goto err; 17953e082a91SMatthew Wilcox data->shost = scsi_host_get(shost); 17963e082a91SMatthew Wilcox if (!data->shost) 17973e082a91SMatthew Wilcox goto err; 17983e082a91SMatthew Wilcox init_completion(&data->prev_finished); 17993e082a91SMatthew Wilcox 18006b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 18016b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags); 18023e082a91SMatthew Wilcox shost->async_scan = 1; 18036b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags); 18046b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex); 18056b7f123fSMatthew Wilcox 18066b7f123fSMatthew Wilcox spin_lock(&async_scan_lock); 18073e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 18083e082a91SMatthew Wilcox complete(&data->prev_finished); 18093e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts); 18103e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 18113e082a91SMatthew Wilcox 18123e082a91SMatthew Wilcox return data; 18133e082a91SMatthew Wilcox 18143e082a91SMatthew Wilcox err: 18153e082a91SMatthew Wilcox kfree(data); 18163e082a91SMatthew Wilcox return NULL; 18173e082a91SMatthew Wilcox } 18183e082a91SMatthew Wilcox 18193e082a91SMatthew Wilcox /** 18203e082a91SMatthew Wilcox * scsi_finish_async_scan - asynchronous scan has finished 18213e082a91SMatthew Wilcox * @data: cookie returned from earlier call to scsi_prep_async_scan() 18223e082a91SMatthew Wilcox * 18233e082a91SMatthew Wilcox * All the devices currently attached to this host have been found. 18243e082a91SMatthew Wilcox * This function announces all the devices it has found to the rest 18253e082a91SMatthew Wilcox * of the system. 18263e082a91SMatthew Wilcox */ 18271aa8fab2SMatthew Wilcox static void scsi_finish_async_scan(struct async_scan_data *data) 18283e082a91SMatthew Wilcox { 18293e082a91SMatthew Wilcox struct Scsi_Host *shost; 18306b7f123fSMatthew Wilcox unsigned long flags; 18313e082a91SMatthew Wilcox 18323e082a91SMatthew Wilcox if (!data) 18333e082a91SMatthew Wilcox return; 18343e082a91SMatthew Wilcox 18353e082a91SMatthew Wilcox shost = data->shost; 18366b7f123fSMatthew Wilcox 18376b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 18386b7f123fSMatthew Wilcox 18393e082a91SMatthew Wilcox if (!shost->async_scan) { 184091921e01SHannes Reinecke shost_printk(KERN_INFO, shost, "%s called twice\n", __func__); 18413e082a91SMatthew Wilcox dump_stack(); 1842773e82f6SJulia Lawall mutex_unlock(&shost->scan_mutex); 18433e082a91SMatthew Wilcox return; 18443e082a91SMatthew Wilcox } 18453e082a91SMatthew Wilcox 18463e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished); 18473e082a91SMatthew Wilcox 18483e082a91SMatthew Wilcox scsi_sysfs_add_devices(shost); 18493e082a91SMatthew Wilcox 18506b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags); 18513e082a91SMatthew Wilcox shost->async_scan = 0; 18526b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags); 18536b7f123fSMatthew Wilcox 18546b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex); 18556b7f123fSMatthew Wilcox 18566b7f123fSMatthew Wilcox spin_lock(&async_scan_lock); 18573e082a91SMatthew Wilcox list_del(&data->list); 18583e082a91SMatthew Wilcox if (!list_empty(&scanning_hosts)) { 18593e082a91SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next, 18603e082a91SMatthew Wilcox struct async_scan_data, list); 18613e082a91SMatthew Wilcox complete(&next->prev_finished); 18623e082a91SMatthew Wilcox } 18633e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 18643e082a91SMatthew Wilcox 1865267a6ad4SHuajun Li scsi_autopm_put_host(shost); 18663e082a91SMatthew Wilcox scsi_host_put(shost); 18673e082a91SMatthew Wilcox kfree(data); 18683e082a91SMatthew Wilcox } 18693e082a91SMatthew Wilcox 18701aa8fab2SMatthew Wilcox static void do_scsi_scan_host(struct Scsi_Host *shost) 18711aa8fab2SMatthew Wilcox { 18721aa8fab2SMatthew Wilcox if (shost->hostt->scan_finished) { 18731aa8fab2SMatthew Wilcox unsigned long start = jiffies; 18741aa8fab2SMatthew Wilcox if (shost->hostt->scan_start) 18751aa8fab2SMatthew Wilcox shost->hostt->scan_start(shost); 18761aa8fab2SMatthew Wilcox 18771aa8fab2SMatthew Wilcox while (!shost->hostt->scan_finished(shost, jiffies - start)) 18781aa8fab2SMatthew Wilcox msleep(10); 18791aa8fab2SMatthew Wilcox } else { 18801aa8fab2SMatthew Wilcox scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD, 18811aa8fab2SMatthew Wilcox SCAN_WILD_CARD, 0); 18821aa8fab2SMatthew Wilcox } 18831aa8fab2SMatthew Wilcox } 18841aa8fab2SMatthew Wilcox 18856cdd5520SDan Williams static void do_scan_async(void *_data, async_cookie_t c) 18863e082a91SMatthew Wilcox { 18873e082a91SMatthew Wilcox struct async_scan_data *data = _data; 1888bc4f2401SAlan Stern struct Scsi_Host *shost = data->shost; 1889bc4f2401SAlan Stern 1890bc4f2401SAlan Stern do_scsi_scan_host(shost); 18913e082a91SMatthew Wilcox scsi_finish_async_scan(data); 18923e082a91SMatthew Wilcox } 18933e082a91SMatthew Wilcox 18941da177e4SLinus Torvalds /** 18951da177e4SLinus Torvalds * scsi_scan_host - scan the given adapter 18961da177e4SLinus Torvalds * @shost: adapter to scan 18971da177e4SLinus Torvalds **/ 18981da177e4SLinus Torvalds void scsi_scan_host(struct Scsi_Host *shost) 18991da177e4SLinus Torvalds { 19003e082a91SMatthew Wilcox struct async_scan_data *data; 19013e082a91SMatthew Wilcox 19023e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 19033e082a91SMatthew Wilcox return; 1904bc4f2401SAlan Stern if (scsi_autopm_get_host(shost) < 0) 1905bc4f2401SAlan Stern return; 19063e082a91SMatthew Wilcox 19073e082a91SMatthew Wilcox data = scsi_prep_async_scan(shost); 19083e082a91SMatthew Wilcox if (!data) { 19091aa8fab2SMatthew Wilcox do_scsi_scan_host(shost); 1910bc4f2401SAlan Stern scsi_autopm_put_host(shost); 19113e082a91SMatthew Wilcox return; 19123e082a91SMatthew Wilcox } 19131aa8fab2SMatthew Wilcox 19146cdd5520SDan Williams /* register with the async subsystem so wait_for_device_probe() 19156cdd5520SDan Williams * will flush this work 19166cdd5520SDan Williams */ 19176cdd5520SDan Williams async_schedule(do_scan_async, data); 19186cdd5520SDan Williams 1919267a6ad4SHuajun Li /* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */ 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_host); 19221da177e4SLinus Torvalds 19231da177e4SLinus Torvalds void scsi_forget_host(struct Scsi_Host *shost) 19241da177e4SLinus Torvalds { 1925a64358dbSAlan Stern struct scsi_device *sdev; 19261da177e4SLinus Torvalds unsigned long flags; 19271da177e4SLinus Torvalds 1928a64358dbSAlan Stern restart: 19291da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 1930a64358dbSAlan Stern list_for_each_entry(sdev, &shost->__devices, siblings) { 1931a64358dbSAlan Stern if (sdev->sdev_state == SDEV_DEL) 1932a64358dbSAlan Stern continue; 19331da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 1934a64358dbSAlan Stern __scsi_remove_device(sdev); 1935a64358dbSAlan Stern goto restart; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 19381da177e4SLinus Torvalds } 19391da177e4SLinus Torvalds 19409f6aa575SRandy Dunlap /** 19419f6aa575SRandy Dunlap * scsi_get_host_dev - Create a scsi_device that points to the host adapter itself 19429f6aa575SRandy Dunlap * @shost: Host that needs a scsi_device 19431da177e4SLinus Torvalds * 19441da177e4SLinus Torvalds * Lock status: None assumed. 19451da177e4SLinus Torvalds * 1946f64a181dSChristoph Hellwig * Returns: The scsi_device or NULL 19471da177e4SLinus Torvalds * 19481da177e4SLinus Torvalds * Notes: 1949f64a181dSChristoph Hellwig * Attach a single scsi_device to the Scsi_Host - this should 19501da177e4SLinus Torvalds * be made to look like a "pseudo-device" that points to the 19511da177e4SLinus Torvalds * HA itself. 19521da177e4SLinus Torvalds * 19531da177e4SLinus Torvalds * Note - this device is not accessible from any high-level 19541da177e4SLinus Torvalds * drivers (including generics), which is probably not 19559f6aa575SRandy Dunlap * optimal. We can add hooks later to attach. 19561da177e4SLinus Torvalds */ 19571da177e4SLinus Torvalds struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost) 19581da177e4SLinus Torvalds { 1959e517d313SAlan Stern struct scsi_device *sdev = NULL; 19601da177e4SLinus Torvalds struct scsi_target *starget; 19611da177e4SLinus Torvalds 19620b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 1963e517d313SAlan Stern if (!scsi_host_scan_allowed(shost)) 1964e517d313SAlan Stern goto out; 19651da177e4SLinus Torvalds starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id); 19661da177e4SLinus Torvalds if (!starget) 1967e517d313SAlan Stern goto out; 19681da177e4SLinus Torvalds 19691da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, 0, NULL); 1970d5469119SAlan Stern if (sdev) 19711da177e4SLinus Torvalds sdev->borken = 0; 1972d5469119SAlan Stern else 1973884d25ccSJames Bottomley scsi_target_reap(starget); 19741da177e4SLinus Torvalds put_device(&starget->dev); 1975e517d313SAlan Stern out: 19760b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 19771da177e4SLinus Torvalds return sdev; 19781da177e4SLinus Torvalds } 19791da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_host_dev); 19801da177e4SLinus Torvalds 19819f6aa575SRandy Dunlap /** 19829f6aa575SRandy Dunlap * scsi_free_host_dev - Free a scsi_device that points to the host adapter itself 19839f6aa575SRandy Dunlap * @sdev: Host device to be freed 19841da177e4SLinus Torvalds * 19851da177e4SLinus Torvalds * Lock status: None assumed. 19861da177e4SLinus Torvalds * 19871da177e4SLinus Torvalds * Returns: Nothing 19881da177e4SLinus Torvalds */ 19891da177e4SLinus Torvalds void scsi_free_host_dev(struct scsi_device *sdev) 19901da177e4SLinus Torvalds { 19911da177e4SLinus Torvalds BUG_ON(sdev->id != sdev->host->this_id); 19921da177e4SLinus Torvalds 1993860dc736SJames Bottomley __scsi_remove_device(sdev); 19941da177e4SLinus Torvalds } 19951da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_free_host_dev); 19961da177e4SLinus Torvalds 1997