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 73939216033SJames Bottomley return 0; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds /** 743f64a181dSChristoph Hellwig * scsi_add_lun - allocate and fully initialze a scsi_device 7446d877688SMatthew Wilcox * @sdev: holds information to be stored in the new scsi_device 7451da177e4SLinus Torvalds * @inq_result: holds the result of a previous INQUIRY to the LUN 7461da177e4SLinus Torvalds * @bflags: black/white list flag 7476d877688SMatthew Wilcox * @async: 1 if this device is being scanned asynchronously 7481da177e4SLinus Torvalds * 7491da177e4SLinus Torvalds * Description: 7506d877688SMatthew Wilcox * Initialize the scsi_device @sdev. Optionally set fields based 7516d877688SMatthew Wilcox * on values in *@bflags. 7521da177e4SLinus Torvalds * 7531da177e4SLinus Torvalds * Return: 754f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 755f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 7561da177e4SLinus Torvalds **/ 757e5b3cd42SAlan Stern static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, 7583e082a91SMatthew Wilcox int *bflags, int async) 7591da177e4SLinus Torvalds { 7606f4267e3SJames Bottomley int ret; 7616f4267e3SJames Bottomley 7621da177e4SLinus Torvalds /* 7631da177e4SLinus Torvalds * XXX do not save the inquiry, since it can change underneath us, 7641da177e4SLinus Torvalds * save just vendor/model/rev. 7651da177e4SLinus Torvalds * 7661da177e4SLinus Torvalds * Rather than save it and have an ioctl that retrieves the saved 7671da177e4SLinus Torvalds * value, have an ioctl that executes the same INQUIRY code used 7681da177e4SLinus Torvalds * in scsi_probe_lun, let user level programs doing INQUIRY 7691da177e4SLinus Torvalds * scanning run at their own risk, or supply a user level program 7701da177e4SLinus Torvalds * that can correctly scan. 7711da177e4SLinus Torvalds */ 7721da177e4SLinus Torvalds 77309123d23SAlan Stern /* 77409123d23SAlan Stern * Copy at least 36 bytes of INQUIRY data, so that we don't 77509123d23SAlan Stern * dereference unallocated memory when accessing the Vendor, 77609123d23SAlan Stern * Product, and Revision strings. Badly behaved devices may set 77709123d23SAlan Stern * the INQUIRY Additional Length byte to a small value, indicating 77809123d23SAlan Stern * these strings are invalid, but often they contain plausible data 77909123d23SAlan Stern * nonetheless. It doesn't matter if the device sent < 36 bytes 78009123d23SAlan Stern * total, since scsi_probe_lun() initializes inq_result with 0s. 78109123d23SAlan Stern */ 78209123d23SAlan Stern sdev->inquiry = kmemdup(inq_result, 78309123d23SAlan Stern max_t(size_t, sdev->inquiry_len, 36), 78409123d23SAlan Stern GFP_ATOMIC); 78509123d23SAlan Stern if (sdev->inquiry == NULL) 78609123d23SAlan Stern return SCSI_SCAN_NO_RESPONSE; 78709123d23SAlan Stern 7881da177e4SLinus Torvalds sdev->vendor = (char *) (sdev->inquiry + 8); 7891da177e4SLinus Torvalds sdev->model = (char *) (sdev->inquiry + 16); 7901da177e4SLinus Torvalds sdev->rev = (char *) (sdev->inquiry + 32); 7911da177e4SLinus Torvalds 79214216561SJames Bottomley if (strncmp(sdev->vendor, "ATA ", 8) == 0) { 79314216561SJames Bottomley /* 79414216561SJames Bottomley * sata emulation layer device. This is a hack to work around 79514216561SJames Bottomley * the SATL power management specifications which state that 79614216561SJames Bottomley * when the SATL detects the device has gone into standby 79714216561SJames Bottomley * mode, it shall respond with NOT READY. 79814216561SJames Bottomley */ 79914216561SJames Bottomley sdev->allow_restart = 1; 80014216561SJames Bottomley } 80114216561SJames Bottomley 8021da177e4SLinus Torvalds if (*bflags & BLIST_ISROM) { 8036d877688SMatthew Wilcox sdev->type = TYPE_ROM; 8046d877688SMatthew Wilcox sdev->removable = 1; 8056d877688SMatthew Wilcox } else { 8066d877688SMatthew Wilcox sdev->type = (inq_result[0] & 0x1f); 8076d877688SMatthew Wilcox sdev->removable = (inq_result[1] & 0x80) >> 7; 8086d877688SMatthew Wilcox } 8091da177e4SLinus Torvalds 8106d877688SMatthew Wilcox if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) { 8116d877688SMatthew Wilcox /* RBC and MMC devices can return SCSI-3 compliance and yet 8126d877688SMatthew Wilcox * still not support REPORT LUNS, so make them act as 8136d877688SMatthew Wilcox * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is 8146d877688SMatthew Wilcox * specifically set */ 8156d877688SMatthew Wilcox if ((*bflags & BLIST_REPORTLUN2) == 0) 8166d877688SMatthew Wilcox *bflags |= BLIST_NOREPORTLUN; 8176d877688SMatthew Wilcox } 8186d877688SMatthew Wilcox 8191da177e4SLinus Torvalds /* 8201da177e4SLinus Torvalds * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI 8211da177e4SLinus Torvalds * spec says: The device server is capable of supporting the 8221da177e4SLinus Torvalds * specified peripheral device type on this logical unit. However, 8231da177e4SLinus Torvalds * the physical device is not currently connected to this logical 8241da177e4SLinus Torvalds * unit. 8251da177e4SLinus Torvalds * 8261da177e4SLinus Torvalds * The above is vague, as it implies that we could treat 001 and 8271da177e4SLinus Torvalds * 011 the same. Stay compatible with previous code, and create a 828f64a181dSChristoph Hellwig * scsi_device for a PQ of 1 8291da177e4SLinus Torvalds * 8301da177e4SLinus Torvalds * Don't set the device offline here; rather let the upper 8311da177e4SLinus Torvalds * level drivers eval the PQ to decide whether they should 8321da177e4SLinus Torvalds * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. 8331da177e4SLinus Torvalds */ 8341da177e4SLinus Torvalds 8351da177e4SLinus Torvalds sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; 8361da177e4SLinus Torvalds sdev->lockable = sdev->removable; 8371da177e4SLinus Torvalds sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); 8381da177e4SLinus Torvalds 8396d877688SMatthew Wilcox if (sdev->scsi_level >= SCSI_3 || 8406d877688SMatthew Wilcox (sdev->inquiry_len > 56 && inq_result[56] & 0x04)) 8411da177e4SLinus Torvalds sdev->ppr = 1; 8421da177e4SLinus Torvalds if (inq_result[7] & 0x60) 8431da177e4SLinus Torvalds sdev->wdtr = 1; 8441da177e4SLinus Torvalds if (inq_result[7] & 0x10) 8451da177e4SLinus Torvalds sdev->sdtr = 1; 8461da177e4SLinus Torvalds 84719ac0db3SJames Bottomley sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d " 8484ff36718SMatthew Wilcox "ANSI: %d%s\n", scsi_device_type(sdev->type), 8494ff36718SMatthew Wilcox sdev->vendor, sdev->model, sdev->rev, 8504ff36718SMatthew Wilcox sdev->inq_periph_qual, inq_result[2] & 0x07, 8514ff36718SMatthew Wilcox (inq_result[3] & 0x0f) == 1 ? " CCS" : ""); 8524ff36718SMatthew Wilcox 8531da177e4SLinus Torvalds if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && 8541da177e4SLinus Torvalds !(*bflags & BLIST_NOTQ)) 8551da177e4SLinus Torvalds sdev->tagged_supported = 1; 8566d877688SMatthew Wilcox 8571da177e4SLinus Torvalds /* 8581da177e4SLinus Torvalds * Some devices (Texel CD ROM drives) have handshaking problems 8591da177e4SLinus Torvalds * when used with the Seagate controllers. borken is initialized 8601da177e4SLinus Torvalds * to 1, and then set it to 0 here. 8611da177e4SLinus Torvalds */ 8621da177e4SLinus Torvalds if ((*bflags & BLIST_BORKEN) == 0) 8631da177e4SLinus Torvalds sdev->borken = 0; 8641da177e4SLinus Torvalds 8656d877688SMatthew Wilcox if (*bflags & BLIST_NO_ULD_ATTACH) 8666d877688SMatthew Wilcox sdev->no_uld_attach = 1; 8676d877688SMatthew Wilcox 8681da177e4SLinus Torvalds /* 8691da177e4SLinus Torvalds * Apparently some really broken devices (contrary to the SCSI 8701da177e4SLinus Torvalds * standards) need to be selected without asserting ATN 8711da177e4SLinus Torvalds */ 8721da177e4SLinus Torvalds if (*bflags & BLIST_SELECT_NO_ATN) 8731da177e4SLinus Torvalds sdev->select_no_atn = 1; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* 8764d7db04aSJames Bottomley * Maximum 512 sector transfer length 8774d7db04aSJames Bottomley * broken RA4x00 Compaq Disk Array 8784d7db04aSJames Bottomley */ 8794d7db04aSJames Bottomley if (*bflags & BLIST_MAX_512) 880086fa5ffSMartin K. Petersen blk_queue_max_hw_sectors(sdev->request_queue, 512); 8814d7db04aSJames Bottomley 8824d7db04aSJames Bottomley /* 8831da177e4SLinus Torvalds * Some devices may not want to have a start command automatically 8841da177e4SLinus Torvalds * issued when a device is added. 8851da177e4SLinus Torvalds */ 8861da177e4SLinus Torvalds if (*bflags & BLIST_NOSTARTONADD) 8871da177e4SLinus Torvalds sdev->no_start_on_add = 1; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds if (*bflags & BLIST_SINGLELUN) 89025d7c363STony Battersby scsi_target(sdev)->single_lun = 1; 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds sdev->use_10_for_rw = 1; 8931da177e4SLinus Torvalds 8941da177e4SLinus Torvalds if (*bflags & BLIST_MS_SKIP_PAGE_08) 8951da177e4SLinus Torvalds sdev->skip_ms_page_8 = 1; 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds if (*bflags & BLIST_MS_SKIP_PAGE_3F) 8981da177e4SLinus Torvalds sdev->skip_ms_page_3f = 1; 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds if (*bflags & BLIST_USE_10_BYTE_MS) 9011da177e4SLinus Torvalds sdev->use_10_for_ms = 1; 9021da177e4SLinus Torvalds 903*0213436aSJanusz Dziemidowicz /* some devices don't like REPORT SUPPORTED OPERATION CODES 904*0213436aSJanusz Dziemidowicz * and will simply timeout causing sd_mod init to take a very 905*0213436aSJanusz Dziemidowicz * very long time */ 906*0213436aSJanusz Dziemidowicz if (*bflags & BLIST_NO_RSOC) 907*0213436aSJanusz Dziemidowicz sdev->no_report_opcodes = 1; 908*0213436aSJanusz Dziemidowicz 9091da177e4SLinus Torvalds /* set the device running here so that slave configure 9101da177e4SLinus Torvalds * may do I/O */ 9116f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_RUNNING); 9126f4267e3SJames Bottomley if (ret) { 9136f4267e3SJames Bottomley ret = scsi_device_set_state(sdev, SDEV_BLOCK); 9146f4267e3SJames Bottomley 9156f4267e3SJames Bottomley if (ret) { 9166f4267e3SJames Bottomley sdev_printk(KERN_ERR, sdev, 9176f4267e3SJames Bottomley "in wrong state %s to complete scan\n", 9186f4267e3SJames Bottomley scsi_device_state_name(sdev->sdev_state)); 9196f4267e3SJames Bottomley return SCSI_SCAN_NO_RESPONSE; 9206f4267e3SJames Bottomley } 9216f4267e3SJames Bottomley } 9221da177e4SLinus Torvalds 9231da177e4SLinus Torvalds if (*bflags & BLIST_MS_192_BYTES_FOR_3F) 9241da177e4SLinus Torvalds sdev->use_192_bytes_for_3f = 1; 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds if (*bflags & BLIST_NOT_LOCKABLE) 9271da177e4SLinus Torvalds sdev->lockable = 0; 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds if (*bflags & BLIST_RETRY_HWERROR) 9301da177e4SLinus Torvalds sdev->retry_hwerror = 1; 9311da177e4SLinus Torvalds 932d974e426SMartin K. Petersen if (*bflags & BLIST_NO_DIF) 933d974e426SMartin K. Petersen sdev->no_dif = 1; 934d974e426SMartin K. Petersen 9350816c925SMartin K. Petersen sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT; 9360816c925SMartin K. Petersen 937c1d40a52SMartin K. Petersen if (*bflags & BLIST_TRY_VPD_PAGES) 938c1d40a52SMartin K. Petersen sdev->try_vpd_pages = 1; 939c1d40a52SMartin K. Petersen else if (*bflags & BLIST_SKIP_VPD_PAGES) 94056f2a801SMartin K. Petersen sdev->skip_vpd_pages = 1; 94156f2a801SMartin K. Petersen 9421da177e4SLinus Torvalds transport_configure_device(&sdev->sdev_gendev); 9431da177e4SLinus Torvalds 94493805091SChristoph Hellwig if (sdev->host->hostt->slave_configure) { 9456f4267e3SJames Bottomley ret = sdev->host->hostt->slave_configure(sdev); 94693805091SChristoph Hellwig if (ret) { 94793805091SChristoph Hellwig /* 94893805091SChristoph Hellwig * if LLDD reports slave not present, don't clutter 94993805091SChristoph Hellwig * console with alloc failure messages 95093805091SChristoph Hellwig */ 95193805091SChristoph Hellwig if (ret != -ENXIO) { 95293805091SChristoph Hellwig sdev_printk(KERN_ERR, sdev, 95393805091SChristoph Hellwig "failed to configure device\n"); 95493805091SChristoph Hellwig } 95593805091SChristoph Hellwig return SCSI_SCAN_NO_RESPONSE; 95693805091SChristoph Hellwig } 95793805091SChristoph Hellwig } 9581da177e4SLinus Torvalds 959b3ae8780SHannes Reinecke if (sdev->scsi_level >= SCSI_3) 960b3ae8780SHannes Reinecke scsi_attach_vpd(sdev); 961b3ae8780SHannes Reinecke 9624a84067dSVasu Dev sdev->max_queue_depth = sdev->queue_depth; 9634a84067dSVasu Dev 9641da177e4SLinus Torvalds /* 9651da177e4SLinus Torvalds * Ok, the device is now all set up, we can 9661da177e4SLinus Torvalds * register it and tell the rest of the kernel 9671da177e4SLinus Torvalds * about it. 9681da177e4SLinus Torvalds */ 9693e082a91SMatthew Wilcox if (!async && scsi_sysfs_add_sdev(sdev) != 0) 970b24b1033SAlan Stern return SCSI_SCAN_NO_RESPONSE; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 975c5f2e640Sakpm@osdl.org #ifdef CONFIG_SCSI_LOGGING 9766c7154c9SKurt Garloff /** 977eb44820cSRob Landley * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace 9786c7154c9SKurt Garloff * @buf: Output buffer with at least end-first+1 bytes of space 9796c7154c9SKurt Garloff * @inq: Inquiry buffer (input) 9806c7154c9SKurt Garloff * @first: Offset of string into inq 9816c7154c9SKurt Garloff * @end: Index after last character in inq 9826c7154c9SKurt Garloff */ 9836c7154c9SKurt Garloff static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq, 9846c7154c9SKurt Garloff unsigned first, unsigned end) 9856c7154c9SKurt Garloff { 9866c7154c9SKurt Garloff unsigned term = 0, idx; 987c5f2e640Sakpm@osdl.org 988c5f2e640Sakpm@osdl.org for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) { 989c5f2e640Sakpm@osdl.org if (inq[idx+first] > ' ') { 9906c7154c9SKurt Garloff buf[idx] = inq[idx+first]; 9916c7154c9SKurt Garloff term = idx+1; 9926c7154c9SKurt Garloff } else { 9936c7154c9SKurt Garloff buf[idx] = ' '; 9946c7154c9SKurt Garloff } 9956c7154c9SKurt Garloff } 9966c7154c9SKurt Garloff buf[term] = 0; 9976c7154c9SKurt Garloff return buf; 9986c7154c9SKurt Garloff } 999c5f2e640Sakpm@osdl.org #endif 10006f3a2024SJames Bottomley 10011da177e4SLinus Torvalds /** 10021da177e4SLinus Torvalds * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 10031da177e4SLinus Torvalds * @starget: pointer to target device structure 10041da177e4SLinus Torvalds * @lun: LUN of target device 10051da177e4SLinus Torvalds * @bflagsp: store bflags here if not NULL 1006eb44820cSRob Landley * @sdevp: probe the LUN corresponding to this scsi_device 1007eb44820cSRob Landley * @rescan: if nonzero skip some code only needed on first scan 1008eb44820cSRob Landley * @hostdata: passed to scsi_alloc_sdev() 10091da177e4SLinus Torvalds * 10101da177e4SLinus Torvalds * Description: 10111da177e4SLinus Torvalds * Call scsi_probe_lun, if a LUN with an attached device is found, 10121da177e4SLinus Torvalds * allocate and set it up by calling scsi_add_lun. 10131da177e4SLinus Torvalds * 10141da177e4SLinus Torvalds * Return: 1015f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 10161da177e4SLinus Torvalds * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is 10171da177e4SLinus Torvalds * attached at the LUN 1018f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 10191da177e4SLinus Torvalds **/ 10201da177e4SLinus Torvalds static int scsi_probe_and_add_lun(struct scsi_target *starget, 10219cb78c16SHannes Reinecke u64 lun, int *bflagsp, 10221da177e4SLinus Torvalds struct scsi_device **sdevp, int rescan, 10231da177e4SLinus Torvalds void *hostdata) 10241da177e4SLinus Torvalds { 10251da177e4SLinus Torvalds struct scsi_device *sdev; 10261da177e4SLinus Torvalds unsigned char *result; 102739216033SJames Bottomley int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256; 10281da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds /* 10311da177e4SLinus Torvalds * The rescan flag is used as an optimization, the first scan of a 10321da177e4SLinus Torvalds * host adapter calls into here with rescan == 0. 10331da177e4SLinus Torvalds */ 10341da177e4SLinus Torvalds sdev = scsi_device_lookup_by_target(starget, lun); 10351da177e4SLinus Torvalds if (sdev) { 10360f1d87a2SJames Bottomley if (rescan || !scsi_device_created(sdev)) { 103791921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 10381da177e4SLinus Torvalds "scsi scan: device exists on %s\n", 103971610f55SKay Sievers dev_name(&sdev->sdev_gendev))); 10401da177e4SLinus Torvalds if (sdevp) 10411da177e4SLinus Torvalds *sdevp = sdev; 10421da177e4SLinus Torvalds else 10431da177e4SLinus Torvalds scsi_device_put(sdev); 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds if (bflagsp) 10461da177e4SLinus Torvalds *bflagsp = scsi_get_device_flags(sdev, 10471da177e4SLinus Torvalds sdev->vendor, 10481da177e4SLinus Torvalds sdev->model); 10491da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 10501da177e4SLinus Torvalds } 10516f3a2024SJames Bottomley scsi_device_put(sdev); 10526f3a2024SJames Bottomley } else 10531da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, lun, hostdata); 10541da177e4SLinus Torvalds if (!sdev) 10551da177e4SLinus Torvalds goto out; 105639216033SJames Bottomley 105739216033SJames Bottomley result = kmalloc(result_len, GFP_ATOMIC | 1058bc86120aSAl Viro ((shost->unchecked_isa_dma) ? __GFP_DMA : 0)); 10591da177e4SLinus Torvalds if (!result) 106039216033SJames Bottomley goto out_free_sdev; 10611da177e4SLinus Torvalds 106239216033SJames Bottomley if (scsi_probe_lun(sdev, result, result_len, &bflags)) 10631da177e4SLinus Torvalds goto out_free_result; 10641da177e4SLinus Torvalds 10654186ab19SKurt Garloff if (bflagsp) 10664186ab19SKurt Garloff *bflagsp = bflags; 10671da177e4SLinus Torvalds /* 10681da177e4SLinus Torvalds * result contains valid SCSI INQUIRY data. 10691da177e4SLinus Torvalds */ 107013f7e5acSKurt Garloff if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) { 10711da177e4SLinus Torvalds /* 10721da177e4SLinus Torvalds * For a Peripheral qualifier 3 (011b), the SCSI 10731da177e4SLinus Torvalds * spec says: The device server is not capable of 10741da177e4SLinus Torvalds * supporting a physical device on this logical 10751da177e4SLinus Torvalds * unit. 10761da177e4SLinus Torvalds * 10771da177e4SLinus Torvalds * For disks, this implies that there is no 10781da177e4SLinus Torvalds * logical disk configured at sdev->lun, but there 10791da177e4SLinus Torvalds * is a target id responding. 10801da177e4SLinus Torvalds */ 10816c7154c9SKurt Garloff SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:" 10826c7154c9SKurt Garloff " peripheral qualifier of 3, device not" 10836c7154c9SKurt Garloff " added\n")) 10846c7154c9SKurt Garloff if (lun == 0) { 1085c5f2e640Sakpm@osdl.org SCSI_LOG_SCAN_BUS(1, { 1086c5f2e640Sakpm@osdl.org unsigned char vend[9]; 1087c5f2e640Sakpm@osdl.org unsigned char mod[17]; 1088c5f2e640Sakpm@osdl.org 1089c5f2e640Sakpm@osdl.org sdev_printk(KERN_INFO, sdev, 10906c7154c9SKurt Garloff "scsi scan: consider passing scsi_mod." 10913424a65dSKurt Garloff "dev_flags=%s:%s:0x240 or 0x1000240\n", 10926c7154c9SKurt Garloff scsi_inq_str(vend, result, 8, 16), 1093c5f2e640Sakpm@osdl.org scsi_inq_str(mod, result, 16, 32)); 1094c5f2e640Sakpm@osdl.org }); 1095643eb2d9SJames Bottomley 10966c7154c9SKurt Garloff } 10976c7154c9SKurt Garloff 10981da177e4SLinus Torvalds res = SCSI_SCAN_TARGET_PRESENT; 10991da177e4SLinus Torvalds goto out_free_result; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11021bfc5d9dSAlan Stern /* 110384961f28Sdave wysochanski * Some targets may set slight variations of PQ and PDT to signal 110484961f28Sdave wysochanski * that no LUN is present, so don't add sdev in these cases. 110584961f28Sdave wysochanski * Two specific examples are: 110684961f28Sdave wysochanski * 1) NetApp targets: return PQ=1, PDT=0x1f 110784961f28Sdave wysochanski * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" 110884961f28Sdave wysochanski * in the UFI 1.0 spec (we cannot rely on reserved bits). 110984961f28Sdave wysochanski * 111084961f28Sdave wysochanski * References: 111184961f28Sdave wysochanski * 1) SCSI SPC-3, pp. 145-146 111284961f28Sdave wysochanski * PQ=1: "A peripheral device having the specified peripheral 111384961f28Sdave wysochanski * device type is not connected to this logical unit. However, the 111484961f28Sdave wysochanski * device server is capable of supporting the specified peripheral 111584961f28Sdave wysochanski * device type on this logical unit." 111684961f28Sdave wysochanski * PDT=0x1f: "Unknown or no device type" 111784961f28Sdave wysochanski * 2) USB UFI 1.0, p. 20 111884961f28Sdave wysochanski * PDT=00h Direct-access device (floppy) 111984961f28Sdave wysochanski * PDT=1Fh none (no FDD connected to the requested logical unit) 11201bfc5d9dSAlan Stern */ 112184961f28Sdave wysochanski if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) && 112201b291bdSJames Bottomley (result[0] & 0x1f) == 0x1f && 112301b291bdSJames Bottomley !scsi_is_wlun(lun)) { 112491921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 11251bfc5d9dSAlan Stern "scsi scan: peripheral device type" 11261bfc5d9dSAlan Stern " of 31, no device added\n")); 11271bfc5d9dSAlan Stern res = SCSI_SCAN_TARGET_PRESENT; 11281bfc5d9dSAlan Stern goto out_free_result; 11291bfc5d9dSAlan Stern } 11301bfc5d9dSAlan Stern 11313e082a91SMatthew Wilcox res = scsi_add_lun(sdev, result, &bflags, shost->async_scan); 11321da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 11331da177e4SLinus Torvalds if (bflags & BLIST_KEY) { 11341da177e4SLinus Torvalds sdev->lockable = 0; 113539216033SJames Bottomley scsi_unlock_floptical(sdev, result); 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds out_free_result: 11401da177e4SLinus Torvalds kfree(result); 11411da177e4SLinus Torvalds out_free_sdev: 11421da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 11431da177e4SLinus Torvalds if (sdevp) { 1144b70d37bfSAlan Stern if (scsi_device_get(sdev) == 0) { 11451da177e4SLinus Torvalds *sdevp = sdev; 1146b70d37bfSAlan Stern } else { 1147b70d37bfSAlan Stern __scsi_remove_device(sdev); 1148b70d37bfSAlan Stern res = SCSI_SCAN_NO_RESPONSE; 1149b70d37bfSAlan Stern } 11501da177e4SLinus Torvalds } 11516f3a2024SJames Bottomley } else 1152860dc736SJames Bottomley __scsi_remove_device(sdev); 11531da177e4SLinus Torvalds out: 11541da177e4SLinus Torvalds return res; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds /** 11581da177e4SLinus Torvalds * scsi_sequential_lun_scan - sequentially scan a SCSI target 11591da177e4SLinus Torvalds * @starget: pointer to target structure to scan 11601da177e4SLinus Torvalds * @bflags: black/white list flag for LUN 0 1161eb44820cSRob Landley * @scsi_level: Which version of the standard does this device adhere to 1162eb44820cSRob Landley * @rescan: passed to scsi_probe_add_lun() 11631da177e4SLinus Torvalds * 11641da177e4SLinus Torvalds * Description: 11651da177e4SLinus Torvalds * Generally, scan from LUN 1 (LUN 0 is assumed to already have been 11661da177e4SLinus Torvalds * scanned) to some maximum lun until a LUN is found with no device 11671da177e4SLinus Torvalds * attached. Use the bflags to figure out any oddities. 11681da177e4SLinus Torvalds * 11691da177e4SLinus Torvalds * Modifies sdevscan->lun. 11701da177e4SLinus Torvalds **/ 11711da177e4SLinus Torvalds static void scsi_sequential_lun_scan(struct scsi_target *starget, 11724186ab19SKurt Garloff int bflags, int scsi_level, int rescan) 11731da177e4SLinus Torvalds { 11749cb78c16SHannes Reinecke uint max_dev_lun; 11759cb78c16SHannes Reinecke u64 sparse_lun, lun; 11761da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 11771da177e4SLinus Torvalds 117891921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, starget_printk(KERN_INFO, starget, 117991921e01SHannes Reinecke "scsi scan: Sequential scan\n")); 11801da177e4SLinus Torvalds 11811da177e4SLinus Torvalds max_dev_lun = min(max_scsi_luns, shost->max_lun); 11821da177e4SLinus Torvalds /* 11831da177e4SLinus Torvalds * If this device is known to support sparse multiple units, 11841da177e4SLinus Torvalds * override the other settings, and scan all of them. Normally, 11851da177e4SLinus Torvalds * SCSI-3 devices should be scanned via the REPORT LUNS. 11861da177e4SLinus Torvalds */ 11871da177e4SLinus Torvalds if (bflags & BLIST_SPARSELUN) { 11881da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 11891da177e4SLinus Torvalds sparse_lun = 1; 11901da177e4SLinus Torvalds } else 11911da177e4SLinus Torvalds sparse_lun = 0; 11921da177e4SLinus Torvalds 11931da177e4SLinus Torvalds /* 11941da177e4SLinus Torvalds * If less than SCSI_1_CSS, and no special lun scaning, stop 11951da177e4SLinus Torvalds * scanning; this matches 2.4 behaviour, but could just be a bug 11961da177e4SLinus Torvalds * (to continue scanning a SCSI_1_CSS device). 11971da177e4SLinus Torvalds * 11981da177e4SLinus Torvalds * This test is broken. We might not have any device on lun0 for 11991da177e4SLinus Torvalds * a sparselun device, and if that's the case then how would we 12001da177e4SLinus Torvalds * know the real scsi_level, eh? It might make sense to just not 12011da177e4SLinus Torvalds * scan any SCSI_1 device for non-0 luns, but that check would best 12021da177e4SLinus Torvalds * go into scsi_alloc_sdev() and just have it return null when asked 12031da177e4SLinus Torvalds * to alloc an sdev for lun > 0 on an already found SCSI_1 device. 12041da177e4SLinus Torvalds * 12051da177e4SLinus Torvalds if ((sdevscan->scsi_level < SCSI_1_CCS) && 12061da177e4SLinus Torvalds ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) 12071da177e4SLinus Torvalds == 0)) 12081da177e4SLinus Torvalds return; 12091da177e4SLinus Torvalds */ 12101da177e4SLinus Torvalds /* 12111da177e4SLinus Torvalds * If this device is known to support multiple units, override 12121da177e4SLinus Torvalds * the other settings, and scan all of them. 12131da177e4SLinus Torvalds */ 12141da177e4SLinus Torvalds if (bflags & BLIST_FORCELUN) 12151da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 12161da177e4SLinus Torvalds /* 12171da177e4SLinus Torvalds * REGAL CDC-4X: avoid hang after LUN 4 12181da177e4SLinus Torvalds */ 12191da177e4SLinus Torvalds if (bflags & BLIST_MAX5LUN) 12201da177e4SLinus Torvalds max_dev_lun = min(5U, max_dev_lun); 12211da177e4SLinus Torvalds /* 12221da177e4SLinus Torvalds * Do not scan SCSI-2 or lower device past LUN 7, unless 12231da177e4SLinus Torvalds * BLIST_LARGELUN. 12241da177e4SLinus Torvalds */ 12251da177e4SLinus Torvalds if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) 12261da177e4SLinus Torvalds max_dev_lun = min(8U, max_dev_lun); 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds /* 122922ffeb48SHannes Reinecke * Stop scanning at 255 unless BLIST_SCSI3LUN 123022ffeb48SHannes Reinecke */ 123122ffeb48SHannes Reinecke if (!(bflags & BLIST_SCSI3LUN)) 123222ffeb48SHannes Reinecke max_dev_lun = min(256U, max_dev_lun); 123322ffeb48SHannes Reinecke 123422ffeb48SHannes Reinecke /* 12351da177e4SLinus Torvalds * We have already scanned LUN 0, so start at LUN 1. Keep scanning 12361da177e4SLinus Torvalds * until we reach the max, or no LUN is found and we are not 12371da177e4SLinus Torvalds * sparse_lun. 12381da177e4SLinus Torvalds */ 12391da177e4SLinus Torvalds for (lun = 1; lun < max_dev_lun; ++lun) 12401da177e4SLinus Torvalds if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, 12411da177e4SLinus Torvalds NULL) != SCSI_SCAN_LUN_PRESENT) && 12421da177e4SLinus Torvalds !sparse_lun) 12431da177e4SLinus Torvalds return; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds /** 12479f6aa575SRandy Dunlap * scsilun_to_int - convert a scsi_lun to an int 12481da177e4SLinus Torvalds * @scsilun: struct scsi_lun to be converted. 12491da177e4SLinus Torvalds * 12501da177e4SLinus Torvalds * Description: 12511da177e4SLinus Torvalds * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered 12521da177e4SLinus Torvalds * integer, and return the result. The caller must check for 12531da177e4SLinus Torvalds * truncation before using this function. 12541da177e4SLinus Torvalds * 12551da177e4SLinus Torvalds * Notes: 12561da177e4SLinus Torvalds * For a description of the LUN format, post SCSI-3 see the SCSI 12571da177e4SLinus Torvalds * Architecture Model, for SCSI-3 see the SCSI Controller Commands. 12581da177e4SLinus Torvalds * 1259d9e5d618SHannes Reinecke * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function 1260d9e5d618SHannes Reinecke * returns the integer: 0x0b03d204 1261d9e5d618SHannes Reinecke * 1262d9e5d618SHannes Reinecke * This encoding will return a standard integer LUN for LUNs smaller 1263d9e5d618SHannes Reinecke * than 256, which typically use a single level LUN structure with 1264d9e5d618SHannes Reinecke * addressing method 0. 12651da177e4SLinus Torvalds **/ 12669cb78c16SHannes Reinecke u64 scsilun_to_int(struct scsi_lun *scsilun) 12671da177e4SLinus Torvalds { 12681da177e4SLinus Torvalds int i; 12699cb78c16SHannes Reinecke u64 lun; 12701da177e4SLinus Torvalds 12711da177e4SLinus Torvalds lun = 0; 12721da177e4SLinus Torvalds for (i = 0; i < sizeof(lun); i += 2) 1273d9e5d618SHannes Reinecke lun = lun | (((u64)scsilun->scsi_lun[i] << ((i + 1) * 8)) | 1274d9e5d618SHannes Reinecke ((u64)scsilun->scsi_lun[i + 1] << (i * 8))); 12751da177e4SLinus Torvalds return lun; 12761da177e4SLinus Torvalds } 1277462b7859SChristof Schmitt EXPORT_SYMBOL(scsilun_to_int); 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds /** 12809f6aa575SRandy Dunlap * int_to_scsilun - reverts an int into a scsi_lun 1281eb44820cSRob Landley * @lun: integer to be reverted 12822f4701d8SJames.Smart@Emulex.Com * @scsilun: struct scsi_lun to be set. 12832f4701d8SJames.Smart@Emulex.Com * 12842f4701d8SJames.Smart@Emulex.Com * Description: 12852f4701d8SJames.Smart@Emulex.Com * Reverts the functionality of the scsilun_to_int, which packed 12862f4701d8SJames.Smart@Emulex.Com * an 8-byte lun value into an int. This routine unpacks the int 12872f4701d8SJames.Smart@Emulex.Com * back into the lun value. 12882f4701d8SJames.Smart@Emulex.Com * 12892f4701d8SJames.Smart@Emulex.Com * Notes: 1290d9e5d618SHannes Reinecke * Given an integer : 0x0b03d204, this function returns a 1291d9e5d618SHannes Reinecke * struct scsi_lun of: d2 04 0b 03 00 00 00 00 12922f4701d8SJames.Smart@Emulex.Com * 12932f4701d8SJames.Smart@Emulex.Com **/ 12949cb78c16SHannes Reinecke void int_to_scsilun(u64 lun, struct scsi_lun *scsilun) 12952f4701d8SJames.Smart@Emulex.Com { 12962f4701d8SJames.Smart@Emulex.Com int i; 12972f4701d8SJames.Smart@Emulex.Com 12982f4701d8SJames.Smart@Emulex.Com memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun)); 12992f4701d8SJames.Smart@Emulex.Com 13002f4701d8SJames.Smart@Emulex.Com for (i = 0; i < sizeof(lun); i += 2) { 13012f4701d8SJames.Smart@Emulex.Com scsilun->scsi_lun[i] = (lun >> 8) & 0xFF; 13022f4701d8SJames.Smart@Emulex.Com scsilun->scsi_lun[i+1] = lun & 0xFF; 13032f4701d8SJames.Smart@Emulex.Com lun = lun >> 16; 13042f4701d8SJames.Smart@Emulex.Com } 13052f4701d8SJames.Smart@Emulex.Com } 13062f4701d8SJames.Smart@Emulex.Com EXPORT_SYMBOL(int_to_scsilun); 13072f4701d8SJames.Smart@Emulex.Com 13082f4701d8SJames.Smart@Emulex.Com /** 13091da177e4SLinus Torvalds * scsi_report_lun_scan - Scan using SCSI REPORT LUN results 1310eb44820cSRob Landley * @starget: which target 1311eb44820cSRob Landley * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN 1312eb44820cSRob Landley * @rescan: nonzero if we can skip code only needed on first scan 13131da177e4SLinus Torvalds * 13141da177e4SLinus Torvalds * Description: 1315eb44820cSRob Landley * Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command. 1316eb44820cSRob Landley * Scan the resulting list of LUNs by calling scsi_probe_and_add_lun. 13171da177e4SLinus Torvalds * 1318eb44820cSRob Landley * If BLINK_REPORTLUN2 is set, scan a target that supports more than 8 1319eb44820cSRob Landley * LUNs even if it's older than SCSI-3. 1320eb44820cSRob Landley * If BLIST_NOREPORTLUN is set, return 1 always. 1321eb44820cSRob Landley * If BLIST_NOLUN is set, return 0 always. 132209b6b51bSAlan Stern * If starget->no_report_luns is set, return 1 always. 13231da177e4SLinus Torvalds * 13241da177e4SLinus Torvalds * Return: 13251da177e4SLinus Torvalds * 0: scan completed (or no memory, so further scanning is futile) 1326eb44820cSRob Landley * 1: could not scan with REPORT LUN 13271da177e4SLinus Torvalds **/ 13286f3a2024SJames Bottomley static int scsi_report_lun_scan(struct scsi_target *starget, int bflags, 13291da177e4SLinus Torvalds int rescan) 13301da177e4SLinus Torvalds { 13311da177e4SLinus Torvalds char devname[64]; 13321da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 13331da177e4SLinus Torvalds unsigned int length; 13349cb78c16SHannes Reinecke u64 lun; 13351da177e4SLinus Torvalds unsigned int num_luns; 13361da177e4SLinus Torvalds unsigned int retries; 133739216033SJames Bottomley int result; 13381da177e4SLinus Torvalds struct scsi_lun *lunp, *lun_data; 13391da177e4SLinus Torvalds u8 *data; 13401da177e4SLinus Torvalds struct scsi_sense_hdr sshdr; 13416f3a2024SJames Bottomley struct scsi_device *sdev; 13426f3a2024SJames Bottomley struct Scsi_Host *shost = dev_to_shost(&starget->dev); 13432ef89198SAlan Stern int ret = 0; 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds /* 13461da177e4SLinus Torvalds * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set. 13471da177e4SLinus Torvalds * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does 13481da177e4SLinus Torvalds * support more than 8 LUNs. 134909b6b51bSAlan Stern * Don't attempt if the target doesn't support REPORT LUNS. 13501da177e4SLinus Torvalds */ 13514d7db04aSJames Bottomley if (bflags & BLIST_NOREPORTLUN) 13524d7db04aSJames Bottomley return 1; 13534d7db04aSJames Bottomley if (starget->scsi_level < SCSI_2 && 13544d7db04aSJames Bottomley starget->scsi_level != SCSI_UNKNOWN) 13554d7db04aSJames Bottomley return 1; 13564d7db04aSJames Bottomley if (starget->scsi_level < SCSI_3 && 13574d7db04aSJames Bottomley (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) 13581da177e4SLinus Torvalds return 1; 13591da177e4SLinus Torvalds if (bflags & BLIST_NOLUN) 13601da177e4SLinus Torvalds return 0; 136109b6b51bSAlan Stern if (starget->no_report_luns) 136209b6b51bSAlan Stern return 1; 13631da177e4SLinus Torvalds 13646f3a2024SJames Bottomley if (!(sdev = scsi_device_lookup_by_target(starget, 0))) { 13656f3a2024SJames Bottomley sdev = scsi_alloc_sdev(starget, 0, NULL); 13666f3a2024SJames Bottomley if (!sdev) 13676f3a2024SJames Bottomley return 0; 136875f8ee8eSAlan Stern if (scsi_device_get(sdev)) { 136975f8ee8eSAlan Stern __scsi_remove_device(sdev); 13706f3a2024SJames Bottomley return 0; 13716f3a2024SJames Bottomley } 137275f8ee8eSAlan Stern } 13736f3a2024SJames Bottomley 13741da177e4SLinus Torvalds sprintf(devname, "host %d channel %d id %d", 13756f3a2024SJames Bottomley shost->host_no, sdev->channel, sdev->id); 13761da177e4SLinus Torvalds 13771da177e4SLinus Torvalds /* 13781da177e4SLinus Torvalds * Allocate enough to hold the header (the same size as one scsi_lun) 13791da177e4SLinus Torvalds * plus the max number of luns we are requesting. 13801da177e4SLinus Torvalds * 13811da177e4SLinus Torvalds * Reallocating and trying again (with the exact amount we need) 13821da177e4SLinus Torvalds * would be nice, but then we need to somehow limit the size 13831da177e4SLinus Torvalds * allocated based on the available memory and the limits of 13841da177e4SLinus Torvalds * kmalloc - we don't want a kmalloc() failure of a huge value to 13851da177e4SLinus Torvalds * prevent us from finding any LUNs on this target. 13861da177e4SLinus Torvalds */ 13871da177e4SLinus Torvalds length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun); 13881da177e4SLinus Torvalds lun_data = kmalloc(length, GFP_ATOMIC | 13891da177e4SLinus Torvalds (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0)); 13906f3a2024SJames Bottomley if (!lun_data) { 1391cadbd4a5SHarvey Harrison printk(ALLOC_FAILURE_MSG, __func__); 139239216033SJames Bottomley goto out; 13936f3a2024SJames Bottomley } 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds scsi_cmd[0] = REPORT_LUNS; 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds /* 13981da177e4SLinus Torvalds * bytes 1 - 5: reserved, set to zero. 13991da177e4SLinus Torvalds */ 14001da177e4SLinus Torvalds memset(&scsi_cmd[1], 0, 5); 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds /* 14031da177e4SLinus Torvalds * bytes 6 - 9: length of the command. 14041da177e4SLinus Torvalds */ 14051da177e4SLinus Torvalds scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff; 14061da177e4SLinus Torvalds scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff; 14071da177e4SLinus Torvalds scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff; 14081da177e4SLinus Torvalds scsi_cmd[9] = (unsigned char) length & 0xff; 14091da177e4SLinus Torvalds 14101da177e4SLinus Torvalds scsi_cmd[10] = 0; /* reserved */ 14111da177e4SLinus Torvalds scsi_cmd[11] = 0; /* control */ 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14141da177e4SLinus Torvalds * We can get a UNIT ATTENTION, for example a power on/reset, so 14151da177e4SLinus Torvalds * retry a few times (like sd.c does for TEST UNIT READY). 14161da177e4SLinus Torvalds * Experience shows some combinations of adapter/devices get at 14171da177e4SLinus Torvalds * least two power on/resets. 14181da177e4SLinus Torvalds * 14191da177e4SLinus Torvalds * Illegal requests (for devices that do not support REPORT LUNS) 14201da177e4SLinus Torvalds * should come through as a check condition, and will not generate 14211da177e4SLinus Torvalds * a retry. 14221da177e4SLinus Torvalds */ 14231da177e4SLinus Torvalds for (retries = 0; retries < 3; retries++) { 142491921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 142591921e01SHannes Reinecke "scsi scan: Sending REPORT LUNS to (try %d)\n", 14261da177e4SLinus Torvalds retries)); 142739216033SJames Bottomley 142839216033SJames Bottomley result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, 1429ea73a9f2SJames Bottomley lun_data, length, &sshdr, 1430f4f4e47eSFUJITA Tomonori SCSI_TIMEOUT + 4 * HZ, 3, NULL); 143139216033SJames Bottomley 143291921e01SHannes Reinecke SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 143391921e01SHannes Reinecke "scsi scan: REPORT LUNS" 143491921e01SHannes Reinecke " %s (try %d) result 0x%x\n", 143591921e01SHannes Reinecke result ? "failed" : "successful", 143691921e01SHannes Reinecke retries, result)); 143739216033SJames Bottomley if (result == 0) 14381da177e4SLinus Torvalds break; 1439ea73a9f2SJames Bottomley else if (scsi_sense_valid(&sshdr)) { 14401da177e4SLinus Torvalds if (sshdr.sense_key != UNIT_ATTENTION) 14411da177e4SLinus Torvalds break; 14421da177e4SLinus Torvalds } 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 144539216033SJames Bottomley if (result) { 14461da177e4SLinus Torvalds /* 14471da177e4SLinus Torvalds * The device probably does not support a REPORT LUN command 14481da177e4SLinus Torvalds */ 14492ef89198SAlan Stern ret = 1; 14502ef89198SAlan Stern goto out_err; 14511da177e4SLinus Torvalds } 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds /* 14541da177e4SLinus Torvalds * Get the length from the first four bytes of lun_data. 14551da177e4SLinus Torvalds */ 14561da177e4SLinus Torvalds data = (u8 *) lun_data->scsi_lun; 14571da177e4SLinus Torvalds length = ((data[0] << 24) | (data[1] << 16) | 14581da177e4SLinus Torvalds (data[2] << 8) | (data[3] << 0)); 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds num_luns = (length / sizeof(struct scsi_lun)); 14611da177e4SLinus Torvalds if (num_luns > max_scsi_report_luns) { 146291921e01SHannes Reinecke sdev_printk(KERN_WARNING, sdev, 146391921e01SHannes Reinecke "Only %d (max_scsi_report_luns)" 14641da177e4SLinus Torvalds " of %d luns reported, try increasing" 146591921e01SHannes Reinecke " max_scsi_report_luns.\n", 14661da177e4SLinus Torvalds max_scsi_report_luns, num_luns); 14671da177e4SLinus Torvalds num_luns = max_scsi_report_luns; 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds 14703bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 14713bf743e7SJeff Garzik "scsi scan: REPORT LUN scan\n")); 14721da177e4SLinus Torvalds 14731da177e4SLinus Torvalds /* 14741da177e4SLinus Torvalds * Scan the luns in lun_data. The entry at offset 0 is really 14751da177e4SLinus Torvalds * the header, so start at 1 and go up to and including num_luns. 14761da177e4SLinus Torvalds */ 14771da177e4SLinus Torvalds for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) { 14781da177e4SLinus Torvalds lun = scsilun_to_int(lunp); 14791da177e4SLinus Torvalds 14809cb78c16SHannes Reinecke if (lun > sdev->host->max_lun) { 148191921e01SHannes Reinecke sdev_printk(KERN_WARNING, sdev, 148291921e01SHannes Reinecke "lun%llu has a LUN larger than" 148391921e01SHannes Reinecke " allowed by the host adapter\n", lun); 14841da177e4SLinus Torvalds } else { 14851da177e4SLinus Torvalds int res; 14861da177e4SLinus Torvalds 14871da177e4SLinus Torvalds res = scsi_probe_and_add_lun(starget, 14881da177e4SLinus Torvalds lun, NULL, NULL, rescan, NULL); 14891da177e4SLinus Torvalds if (res == SCSI_SCAN_NO_RESPONSE) { 14901da177e4SLinus Torvalds /* 14911da177e4SLinus Torvalds * Got some results, but now none, abort. 14921da177e4SLinus Torvalds */ 14933bf743e7SJeff Garzik sdev_printk(KERN_ERR, sdev, 14943bf743e7SJeff Garzik "Unexpected response" 14959cb78c16SHannes Reinecke " from lun %llu while scanning, scan" 14969cb78c16SHannes Reinecke " aborted\n", (unsigned long long)lun); 14971da177e4SLinus Torvalds break; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds } 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 15022ef89198SAlan Stern out_err: 15031da177e4SLinus Torvalds kfree(lun_data); 15041da177e4SLinus Torvalds out: 15056f3a2024SJames Bottomley scsi_device_put(sdev); 15060f1d87a2SJames Bottomley if (scsi_device_created(sdev)) 15071da177e4SLinus Torvalds /* 15086f3a2024SJames Bottomley * the sdev we used didn't appear in the report luns scan 15091da177e4SLinus Torvalds */ 1510860dc736SJames Bottomley __scsi_remove_device(sdev); 15112ef89198SAlan Stern return ret; 15121da177e4SLinus Torvalds } 15131da177e4SLinus Torvalds 15141da177e4SLinus Torvalds struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel, 15159cb78c16SHannes Reinecke uint id, u64 lun, void *hostdata) 15161da177e4SLinus Torvalds { 1517a97a83a0SMatthew Wilcox struct scsi_device *sdev = ERR_PTR(-ENODEV); 15181da177e4SLinus Torvalds struct device *parent = &shost->shost_gendev; 1519e02f3f59SChristoph Hellwig struct scsi_target *starget; 15201da177e4SLinus Torvalds 1521938e2ac0SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 1522938e2ac0SMatthew Wilcox return ERR_PTR(-ENODEV); 1523938e2ac0SMatthew Wilcox 1524e02f3f59SChristoph Hellwig starget = scsi_alloc_target(parent, channel, id); 15251da177e4SLinus Torvalds if (!starget) 15261da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 1527bc4f2401SAlan Stern scsi_autopm_get_target(starget); 15281da177e4SLinus Torvalds 15290b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 15306b7f123fSMatthew Wilcox if (!shost->async_scan) 15316b7f123fSMatthew Wilcox scsi_complete_async_scans(); 15326b7f123fSMatthew Wilcox 1533bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1534a97a83a0SMatthew Wilcox scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata); 1535bc4f2401SAlan Stern scsi_autopm_put_host(shost); 1536bc4f2401SAlan Stern } 15370b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 1538bc4f2401SAlan Stern scsi_autopm_put_target(starget); 1539e63ed0d7SJames Bottomley /* 1540e63ed0d7SJames Bottomley * paired with scsi_alloc_target(). Target will be destroyed unless 1541e63ed0d7SJames Bottomley * scsi_probe_and_add_lun made an underlying device visible 1542e63ed0d7SJames Bottomley */ 15431da177e4SLinus Torvalds scsi_target_reap(starget); 15441da177e4SLinus Torvalds put_device(&starget->dev); 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds return sdev; 15471da177e4SLinus Torvalds } 15481da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_add_device); 15491da177e4SLinus Torvalds 1550146f7262SJames Bottomley int scsi_add_device(struct Scsi_Host *host, uint channel, 15519cb78c16SHannes Reinecke uint target, u64 lun) 1552146f7262SJames Bottomley { 1553146f7262SJames Bottomley struct scsi_device *sdev = 1554146f7262SJames Bottomley __scsi_add_device(host, channel, target, lun, NULL); 1555146f7262SJames Bottomley if (IS_ERR(sdev)) 1556146f7262SJames Bottomley return PTR_ERR(sdev); 1557146f7262SJames Bottomley 1558146f7262SJames Bottomley scsi_device_put(sdev); 1559146f7262SJames Bottomley return 0; 1560146f7262SJames Bottomley } 1561146f7262SJames Bottomley EXPORT_SYMBOL(scsi_add_device); 1562146f7262SJames Bottomley 15631da177e4SLinus Torvalds void scsi_rescan_device(struct device *dev) 15641da177e4SLinus Torvalds { 15651da177e4SLinus Torvalds struct scsi_driver *drv; 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds if (!dev->driver) 15681da177e4SLinus Torvalds return; 15691da177e4SLinus Torvalds 15701da177e4SLinus Torvalds drv = to_scsi_driver(dev->driver); 15711da177e4SLinus Torvalds if (try_module_get(drv->owner)) { 15721da177e4SLinus Torvalds if (drv->rescan) 15731da177e4SLinus Torvalds drv->rescan(dev); 15741da177e4SLinus Torvalds module_put(drv->owner); 15751da177e4SLinus Torvalds } 15761da177e4SLinus Torvalds } 15771da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_rescan_device); 15781da177e4SLinus Torvalds 1579e517d313SAlan Stern static void __scsi_scan_target(struct device *parent, unsigned int channel, 15809cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 15811da177e4SLinus Torvalds { 15821da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 15831da177e4SLinus Torvalds int bflags = 0; 15841da177e4SLinus Torvalds int res; 15851da177e4SLinus Torvalds struct scsi_target *starget; 15861da177e4SLinus Torvalds 15871da177e4SLinus Torvalds if (shost->this_id == id) 15881da177e4SLinus Torvalds /* 15891da177e4SLinus Torvalds * Don't scan the host adapter 15901da177e4SLinus Torvalds */ 15911da177e4SLinus Torvalds return; 15921da177e4SLinus Torvalds 15931da177e4SLinus Torvalds starget = scsi_alloc_target(parent, channel, id); 15941da177e4SLinus Torvalds if (!starget) 15951da177e4SLinus Torvalds return; 1596bc4f2401SAlan Stern scsi_autopm_get_target(starget); 15971da177e4SLinus Torvalds 15981da177e4SLinus Torvalds if (lun != SCAN_WILD_CARD) { 15991da177e4SLinus Torvalds /* 16001da177e4SLinus Torvalds * Scan for a specific host/chan/id/lun. 16011da177e4SLinus Torvalds */ 16021da177e4SLinus Torvalds scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL); 16031da177e4SLinus Torvalds goto out_reap; 16041da177e4SLinus Torvalds } 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds /* 16071da177e4SLinus Torvalds * Scan LUN 0, if there is some response, scan further. Ideally, we 16081da177e4SLinus Torvalds * would not configure LUN 0 until all LUNs are scanned. 16091da177e4SLinus Torvalds */ 16106f3a2024SJames Bottomley res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL); 16116f3a2024SJames Bottomley if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) { 16126f3a2024SJames Bottomley if (scsi_report_lun_scan(starget, bflags, rescan) != 0) 16131da177e4SLinus Torvalds /* 16141da177e4SLinus Torvalds * The REPORT LUN did not scan the target, 16151da177e4SLinus Torvalds * do a sequential scan. 16161da177e4SLinus Torvalds */ 16171da177e4SLinus Torvalds scsi_sequential_lun_scan(starget, bflags, 16184186ab19SKurt Garloff starget->scsi_level, rescan); 16191da177e4SLinus Torvalds } 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds out_reap: 1622bc4f2401SAlan Stern scsi_autopm_put_target(starget); 1623e63ed0d7SJames Bottomley /* 1624e63ed0d7SJames Bottomley * paired with scsi_alloc_target(): determine if the target has 1625e63ed0d7SJames Bottomley * any children at all and if not, nuke it 1626e63ed0d7SJames Bottomley */ 16271da177e4SLinus Torvalds scsi_target_reap(starget); 16281da177e4SLinus Torvalds 16291da177e4SLinus Torvalds put_device(&starget->dev); 16301da177e4SLinus Torvalds } 1631e517d313SAlan Stern 1632e517d313SAlan Stern /** 1633e59e4a09SRandy Dunlap * scsi_scan_target - scan a target id, possibly including all LUNs on the target. 1634e517d313SAlan Stern * @parent: host to scan 1635e517d313SAlan Stern * @channel: channel to scan 1636e517d313SAlan Stern * @id: target id to scan 1637e517d313SAlan Stern * @lun: Specific LUN to scan or SCAN_WILD_CARD 1638e517d313SAlan Stern * @rescan: passed to LUN scanning routines 1639e517d313SAlan Stern * 1640e517d313SAlan Stern * Description: 1641e517d313SAlan Stern * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0, 1642e517d313SAlan Stern * and possibly all LUNs on the target id. 1643e517d313SAlan Stern * 1644e517d313SAlan Stern * First try a REPORT LUN scan, if that does not scan the target, do a 1645e517d313SAlan Stern * sequential scan of LUNs on the target id. 1646e517d313SAlan Stern **/ 1647e517d313SAlan Stern void scsi_scan_target(struct device *parent, unsigned int channel, 16489cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 1649e517d313SAlan Stern { 1650e517d313SAlan Stern struct Scsi_Host *shost = dev_to_shost(parent); 1651e517d313SAlan Stern 165293b45af5SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 165393b45af5SMatthew Wilcox return; 165493b45af5SMatthew Wilcox 16556b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 16563e082a91SMatthew Wilcox if (!shost->async_scan) 16573e082a91SMatthew Wilcox scsi_complete_async_scans(); 16583e082a91SMatthew Wilcox 1659bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 1660e517d313SAlan Stern __scsi_scan_target(parent, channel, id, lun, rescan); 1661bc4f2401SAlan Stern scsi_autopm_put_host(shost); 1662bc4f2401SAlan Stern } 16630b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 1664e517d313SAlan Stern } 16651da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_target); 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel, 16689cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 16691da177e4SLinus Torvalds { 16701da177e4SLinus Torvalds uint order_id; 16711da177e4SLinus Torvalds 16721da177e4SLinus Torvalds if (id == SCAN_WILD_CARD) 16731da177e4SLinus Torvalds for (id = 0; id < shost->max_id; ++id) { 16741da177e4SLinus Torvalds /* 16751da177e4SLinus Torvalds * XXX adapter drivers when possible (FCP, iSCSI) 16761da177e4SLinus Torvalds * could modify max_id to match the current max, 16771da177e4SLinus Torvalds * not the absolute max. 16781da177e4SLinus Torvalds * 16791da177e4SLinus Torvalds * XXX add a shost id iterator, so for example, 16801da177e4SLinus Torvalds * the FC ID can be the same as a target id 16811da177e4SLinus Torvalds * without a huge overhead of sparse id's. 16821da177e4SLinus Torvalds */ 16831da177e4SLinus Torvalds if (shost->reverse_ordering) 16841da177e4SLinus Torvalds /* 16851da177e4SLinus Torvalds * Scan from high to low id. 16861da177e4SLinus Torvalds */ 16871da177e4SLinus Torvalds order_id = shost->max_id - id - 1; 16881da177e4SLinus Torvalds else 16891da177e4SLinus Torvalds order_id = id; 1690e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1691e517d313SAlan Stern order_id, lun, rescan); 16921da177e4SLinus Torvalds } 16931da177e4SLinus Torvalds else 1694e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1695e517d313SAlan Stern id, lun, rescan); 16961da177e4SLinus Torvalds } 16971da177e4SLinus Torvalds 16981da177e4SLinus Torvalds int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel, 16999cb78c16SHannes Reinecke unsigned int id, u64 lun, int rescan) 17001da177e4SLinus Torvalds { 17013bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost, 17029cb78c16SHannes Reinecke "%s: <%u:%u:%llu>\n", 1703cadbd4a5SHarvey Harrison __func__, channel, id, lun)); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || 1706091686d3SAmit Arora ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || 17071da177e4SLinus Torvalds ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun))) 17081da177e4SLinus Torvalds return -EINVAL; 17091da177e4SLinus Torvalds 17100b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 17116b7f123fSMatthew Wilcox if (!shost->async_scan) 17126b7f123fSMatthew Wilcox scsi_complete_async_scans(); 17136b7f123fSMatthew Wilcox 1714bc4f2401SAlan Stern if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) { 17151da177e4SLinus Torvalds if (channel == SCAN_WILD_CARD) 171682f29467SMike Anderson for (channel = 0; channel <= shost->max_channel; 171782f29467SMike Anderson channel++) 171882f29467SMike Anderson scsi_scan_channel(shost, channel, id, lun, 171982f29467SMike Anderson rescan); 17201da177e4SLinus Torvalds else 17211da177e4SLinus Torvalds scsi_scan_channel(shost, channel, id, lun, rescan); 1722bc4f2401SAlan Stern scsi_autopm_put_host(shost); 172382f29467SMike Anderson } 17240b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds return 0; 17271da177e4SLinus Torvalds } 17281da177e4SLinus Torvalds 17293e082a91SMatthew Wilcox static void scsi_sysfs_add_devices(struct Scsi_Host *shost) 17303e082a91SMatthew Wilcox { 17313e082a91SMatthew Wilcox struct scsi_device *sdev; 17323e082a91SMatthew Wilcox shost_for_each_device(sdev, shost) { 17333b661a92SDan Williams /* target removed before the device could be added */ 17343b661a92SDan Williams if (sdev->sdev_state == SDEV_DEL) 17353b661a92SDan Williams continue; 17366b7f123fSMatthew Wilcox if (!scsi_host_scan_allowed(shost) || 17376b7f123fSMatthew Wilcox scsi_sysfs_add_sdev(sdev) != 0) 1738860dc736SJames Bottomley __scsi_remove_device(sdev); 17393e082a91SMatthew Wilcox } 17403e082a91SMatthew Wilcox } 17413e082a91SMatthew Wilcox 17423e082a91SMatthew Wilcox /** 17433e082a91SMatthew Wilcox * scsi_prep_async_scan - prepare for an async scan 17443e082a91SMatthew Wilcox * @shost: the host which will be scanned 17453e082a91SMatthew Wilcox * Returns: a cookie to be passed to scsi_finish_async_scan() 17463e082a91SMatthew Wilcox * 17473e082a91SMatthew Wilcox * Tells the midlayer this host is going to do an asynchronous scan. 17483e082a91SMatthew Wilcox * It reserves the host's position in the scanning list and ensures 17493e082a91SMatthew Wilcox * that other asynchronous scans started after this one won't affect the 17503e082a91SMatthew Wilcox * ordering of the discovered devices. 17513e082a91SMatthew Wilcox */ 17521aa8fab2SMatthew Wilcox static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) 17533e082a91SMatthew Wilcox { 17543e082a91SMatthew Wilcox struct async_scan_data *data; 17556b7f123fSMatthew Wilcox unsigned long flags; 17563e082a91SMatthew Wilcox 17573e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "sync", 4) == 0) 17583e082a91SMatthew Wilcox return NULL; 17593e082a91SMatthew Wilcox 17603e082a91SMatthew Wilcox if (shost->async_scan) { 176191921e01SHannes Reinecke shost_printk(KERN_INFO, shost, "%s called twice\n", __func__); 17623e082a91SMatthew Wilcox dump_stack(); 17633e082a91SMatthew Wilcox return NULL; 17643e082a91SMatthew Wilcox } 17653e082a91SMatthew Wilcox 17663e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL); 17673e082a91SMatthew Wilcox if (!data) 17683e082a91SMatthew Wilcox goto err; 17693e082a91SMatthew Wilcox data->shost = scsi_host_get(shost); 17703e082a91SMatthew Wilcox if (!data->shost) 17713e082a91SMatthew Wilcox goto err; 17723e082a91SMatthew Wilcox init_completion(&data->prev_finished); 17733e082a91SMatthew Wilcox 17746b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 17756b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags); 17763e082a91SMatthew Wilcox shost->async_scan = 1; 17776b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags); 17786b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex); 17796b7f123fSMatthew Wilcox 17806b7f123fSMatthew Wilcox spin_lock(&async_scan_lock); 17813e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 17823e082a91SMatthew Wilcox complete(&data->prev_finished); 17833e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts); 17843e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 17853e082a91SMatthew Wilcox 17863e082a91SMatthew Wilcox return data; 17873e082a91SMatthew Wilcox 17883e082a91SMatthew Wilcox err: 17893e082a91SMatthew Wilcox kfree(data); 17903e082a91SMatthew Wilcox return NULL; 17913e082a91SMatthew Wilcox } 17923e082a91SMatthew Wilcox 17933e082a91SMatthew Wilcox /** 17943e082a91SMatthew Wilcox * scsi_finish_async_scan - asynchronous scan has finished 17953e082a91SMatthew Wilcox * @data: cookie returned from earlier call to scsi_prep_async_scan() 17963e082a91SMatthew Wilcox * 17973e082a91SMatthew Wilcox * All the devices currently attached to this host have been found. 17983e082a91SMatthew Wilcox * This function announces all the devices it has found to the rest 17993e082a91SMatthew Wilcox * of the system. 18003e082a91SMatthew Wilcox */ 18011aa8fab2SMatthew Wilcox static void scsi_finish_async_scan(struct async_scan_data *data) 18023e082a91SMatthew Wilcox { 18033e082a91SMatthew Wilcox struct Scsi_Host *shost; 18046b7f123fSMatthew Wilcox unsigned long flags; 18053e082a91SMatthew Wilcox 18063e082a91SMatthew Wilcox if (!data) 18073e082a91SMatthew Wilcox return; 18083e082a91SMatthew Wilcox 18093e082a91SMatthew Wilcox shost = data->shost; 18106b7f123fSMatthew Wilcox 18116b7f123fSMatthew Wilcox mutex_lock(&shost->scan_mutex); 18126b7f123fSMatthew Wilcox 18133e082a91SMatthew Wilcox if (!shost->async_scan) { 181491921e01SHannes Reinecke shost_printk(KERN_INFO, shost, "%s called twice\n", __func__); 18153e082a91SMatthew Wilcox dump_stack(); 1816773e82f6SJulia Lawall mutex_unlock(&shost->scan_mutex); 18173e082a91SMatthew Wilcox return; 18183e082a91SMatthew Wilcox } 18193e082a91SMatthew Wilcox 18203e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished); 18213e082a91SMatthew Wilcox 18223e082a91SMatthew Wilcox scsi_sysfs_add_devices(shost); 18233e082a91SMatthew Wilcox 18246b7f123fSMatthew Wilcox spin_lock_irqsave(shost->host_lock, flags); 18253e082a91SMatthew Wilcox shost->async_scan = 0; 18266b7f123fSMatthew Wilcox spin_unlock_irqrestore(shost->host_lock, flags); 18276b7f123fSMatthew Wilcox 18286b7f123fSMatthew Wilcox mutex_unlock(&shost->scan_mutex); 18296b7f123fSMatthew Wilcox 18306b7f123fSMatthew Wilcox spin_lock(&async_scan_lock); 18313e082a91SMatthew Wilcox list_del(&data->list); 18323e082a91SMatthew Wilcox if (!list_empty(&scanning_hosts)) { 18333e082a91SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next, 18343e082a91SMatthew Wilcox struct async_scan_data, list); 18353e082a91SMatthew Wilcox complete(&next->prev_finished); 18363e082a91SMatthew Wilcox } 18373e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 18383e082a91SMatthew Wilcox 1839267a6ad4SHuajun Li scsi_autopm_put_host(shost); 18403e082a91SMatthew Wilcox scsi_host_put(shost); 18413e082a91SMatthew Wilcox kfree(data); 18423e082a91SMatthew Wilcox } 18433e082a91SMatthew Wilcox 18441aa8fab2SMatthew Wilcox static void do_scsi_scan_host(struct Scsi_Host *shost) 18451aa8fab2SMatthew Wilcox { 18461aa8fab2SMatthew Wilcox if (shost->hostt->scan_finished) { 18471aa8fab2SMatthew Wilcox unsigned long start = jiffies; 18481aa8fab2SMatthew Wilcox if (shost->hostt->scan_start) 18491aa8fab2SMatthew Wilcox shost->hostt->scan_start(shost); 18501aa8fab2SMatthew Wilcox 18511aa8fab2SMatthew Wilcox while (!shost->hostt->scan_finished(shost, jiffies - start)) 18521aa8fab2SMatthew Wilcox msleep(10); 18531aa8fab2SMatthew Wilcox } else { 18541aa8fab2SMatthew Wilcox scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD, 18551aa8fab2SMatthew Wilcox SCAN_WILD_CARD, 0); 18561aa8fab2SMatthew Wilcox } 18571aa8fab2SMatthew Wilcox } 18581aa8fab2SMatthew Wilcox 18596cdd5520SDan Williams static void do_scan_async(void *_data, async_cookie_t c) 18603e082a91SMatthew Wilcox { 18613e082a91SMatthew Wilcox struct async_scan_data *data = _data; 1862bc4f2401SAlan Stern struct Scsi_Host *shost = data->shost; 1863bc4f2401SAlan Stern 1864bc4f2401SAlan Stern do_scsi_scan_host(shost); 18653e082a91SMatthew Wilcox scsi_finish_async_scan(data); 18663e082a91SMatthew Wilcox } 18673e082a91SMatthew Wilcox 18681da177e4SLinus Torvalds /** 18691da177e4SLinus Torvalds * scsi_scan_host - scan the given adapter 18701da177e4SLinus Torvalds * @shost: adapter to scan 18711da177e4SLinus Torvalds **/ 18721da177e4SLinus Torvalds void scsi_scan_host(struct Scsi_Host *shost) 18731da177e4SLinus Torvalds { 18743e082a91SMatthew Wilcox struct async_scan_data *data; 18753e082a91SMatthew Wilcox 18763e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 18773e082a91SMatthew Wilcox return; 1878bc4f2401SAlan Stern if (scsi_autopm_get_host(shost) < 0) 1879bc4f2401SAlan Stern return; 18803e082a91SMatthew Wilcox 18813e082a91SMatthew Wilcox data = scsi_prep_async_scan(shost); 18823e082a91SMatthew Wilcox if (!data) { 18831aa8fab2SMatthew Wilcox do_scsi_scan_host(shost); 1884bc4f2401SAlan Stern scsi_autopm_put_host(shost); 18853e082a91SMatthew Wilcox return; 18863e082a91SMatthew Wilcox } 18871aa8fab2SMatthew Wilcox 18886cdd5520SDan Williams /* register with the async subsystem so wait_for_device_probe() 18896cdd5520SDan Williams * will flush this work 18906cdd5520SDan Williams */ 18916cdd5520SDan Williams async_schedule(do_scan_async, data); 18926cdd5520SDan Williams 1893267a6ad4SHuajun Li /* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */ 18941da177e4SLinus Torvalds } 18951da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_host); 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds void scsi_forget_host(struct Scsi_Host *shost) 18981da177e4SLinus Torvalds { 1899a64358dbSAlan Stern struct scsi_device *sdev; 19001da177e4SLinus Torvalds unsigned long flags; 19011da177e4SLinus Torvalds 1902a64358dbSAlan Stern restart: 19031da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 1904a64358dbSAlan Stern list_for_each_entry(sdev, &shost->__devices, siblings) { 1905a64358dbSAlan Stern if (sdev->sdev_state == SDEV_DEL) 1906a64358dbSAlan Stern continue; 19071da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 1908a64358dbSAlan Stern __scsi_remove_device(sdev); 1909a64358dbSAlan Stern goto restart; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 19121da177e4SLinus Torvalds } 19131da177e4SLinus Torvalds 19149f6aa575SRandy Dunlap /** 19159f6aa575SRandy Dunlap * scsi_get_host_dev - Create a scsi_device that points to the host adapter itself 19169f6aa575SRandy Dunlap * @shost: Host that needs a scsi_device 19171da177e4SLinus Torvalds * 19181da177e4SLinus Torvalds * Lock status: None assumed. 19191da177e4SLinus Torvalds * 1920f64a181dSChristoph Hellwig * Returns: The scsi_device or NULL 19211da177e4SLinus Torvalds * 19221da177e4SLinus Torvalds * Notes: 1923f64a181dSChristoph Hellwig * Attach a single scsi_device to the Scsi_Host - this should 19241da177e4SLinus Torvalds * be made to look like a "pseudo-device" that points to the 19251da177e4SLinus Torvalds * HA itself. 19261da177e4SLinus Torvalds * 19271da177e4SLinus Torvalds * Note - this device is not accessible from any high-level 19281da177e4SLinus Torvalds * drivers (including generics), which is probably not 19299f6aa575SRandy Dunlap * optimal. We can add hooks later to attach. 19301da177e4SLinus Torvalds */ 19311da177e4SLinus Torvalds struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost) 19321da177e4SLinus Torvalds { 1933e517d313SAlan Stern struct scsi_device *sdev = NULL; 19341da177e4SLinus Torvalds struct scsi_target *starget; 19351da177e4SLinus Torvalds 19360b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 1937e517d313SAlan Stern if (!scsi_host_scan_allowed(shost)) 1938e517d313SAlan Stern goto out; 19391da177e4SLinus Torvalds starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id); 19401da177e4SLinus Torvalds if (!starget) 1941e517d313SAlan Stern goto out; 19421da177e4SLinus Torvalds 19431da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, 0, NULL); 1944d5469119SAlan Stern if (sdev) 19451da177e4SLinus Torvalds sdev->borken = 0; 1946d5469119SAlan Stern else 1947884d25ccSJames Bottomley scsi_target_reap(starget); 19481da177e4SLinus Torvalds put_device(&starget->dev); 1949e517d313SAlan Stern out: 19500b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 19511da177e4SLinus Torvalds return sdev; 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_host_dev); 19541da177e4SLinus Torvalds 19559f6aa575SRandy Dunlap /** 19569f6aa575SRandy Dunlap * scsi_free_host_dev - Free a scsi_device that points to the host adapter itself 19579f6aa575SRandy Dunlap * @sdev: Host device to be freed 19581da177e4SLinus Torvalds * 19591da177e4SLinus Torvalds * Lock status: None assumed. 19601da177e4SLinus Torvalds * 19611da177e4SLinus Torvalds * Returns: Nothing 19621da177e4SLinus Torvalds */ 19631da177e4SLinus Torvalds void scsi_free_host_dev(struct scsi_device *sdev) 19641da177e4SLinus Torvalds { 19651da177e4SLinus Torvalds BUG_ON(sdev->id != sdev->host->this_id); 19661da177e4SLinus Torvalds 1967860dc736SJames Bottomley __scsi_remove_device(sdev); 19681da177e4SLinus Torvalds } 19691da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_free_host_dev); 19701da177e4SLinus Torvalds 1971