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> 351da177e4SLinus Torvalds 361da177e4SLinus Torvalds #include <scsi/scsi.h> 37beb40487SChristoph Hellwig #include <scsi/scsi_cmnd.h> 381da177e4SLinus Torvalds #include <scsi/scsi_device.h> 391da177e4SLinus Torvalds #include <scsi/scsi_driver.h> 401da177e4SLinus Torvalds #include <scsi/scsi_devinfo.h> 411da177e4SLinus Torvalds #include <scsi/scsi_host.h> 421da177e4SLinus Torvalds #include <scsi/scsi_transport.h> 431da177e4SLinus Torvalds #include <scsi/scsi_eh.h> 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds #include "scsi_priv.h" 461da177e4SLinus Torvalds #include "scsi_logging.h" 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \ 491da177e4SLinus Torvalds " SCSI scanning, some SCSI devices might not be configured\n" 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds /* 521da177e4SLinus Torvalds * Default timeout 531da177e4SLinus Torvalds */ 541da177e4SLinus Torvalds #define SCSI_TIMEOUT (2*HZ) 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* 571da177e4SLinus Torvalds * Prefix values for the SCSI id's (stored in driverfs name field) 581da177e4SLinus Torvalds */ 591da177e4SLinus Torvalds #define SCSI_UID_SER_NUM 'S' 601da177e4SLinus Torvalds #define SCSI_UID_UNKNOWN 'Z' 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* 631da177e4SLinus Torvalds * Return values of some of the scanning functions. 641da177e4SLinus Torvalds * 651da177e4SLinus Torvalds * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this 661da177e4SLinus Torvalds * includes allocation or general failures preventing IO from being sent. 671da177e4SLinus Torvalds * 681da177e4SLinus Torvalds * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available 691da177e4SLinus Torvalds * on the given LUN. 701da177e4SLinus Torvalds * 711da177e4SLinus Torvalds * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a 721da177e4SLinus Torvalds * given LUN. 731da177e4SLinus Torvalds */ 741da177e4SLinus Torvalds #define SCSI_SCAN_NO_RESPONSE 0 751da177e4SLinus Torvalds #define SCSI_SCAN_TARGET_PRESENT 1 761da177e4SLinus Torvalds #define SCSI_SCAN_LUN_PRESENT 2 771da177e4SLinus Torvalds 780ad78200SArjan van de Ven static const char *scsi_null_device_strs = "nullnullnullnull"; 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds #define MAX_SCSI_LUNS 512 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds #ifdef CONFIG_SCSI_MULTI_LUN 831da177e4SLinus Torvalds static unsigned int max_scsi_luns = MAX_SCSI_LUNS; 841da177e4SLinus Torvalds #else 851da177e4SLinus Torvalds static unsigned int max_scsi_luns = 1; 861da177e4SLinus Torvalds #endif 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR); 891da177e4SLinus Torvalds MODULE_PARM_DESC(max_luns, 901da177e4SLinus Torvalds "last scsi LUN (should be between 1 and 2^32-1)"); 911da177e4SLinus Torvalds 9221db1882SMatthew Wilcox #ifdef CONFIG_SCSI_SCAN_ASYNC 9321db1882SMatthew Wilcox #define SCSI_SCAN_TYPE_DEFAULT "async" 9421db1882SMatthew Wilcox #else 9521db1882SMatthew Wilcox #define SCSI_SCAN_TYPE_DEFAULT "sync" 9621db1882SMatthew Wilcox #endif 9721db1882SMatthew Wilcox 9821db1882SMatthew Wilcox static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT; 993e082a91SMatthew Wilcox 1003e082a91SMatthew Wilcox module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO); 1013e082a91SMatthew Wilcox MODULE_PARM_DESC(scan, "sync, async or none"); 1023e082a91SMatthew Wilcox 1031da177e4SLinus Torvalds /* 1041da177e4SLinus Torvalds * max_scsi_report_luns: the maximum number of LUNS that will be 1051da177e4SLinus Torvalds * returned from the REPORT LUNS command. 8 times this value must 1061da177e4SLinus Torvalds * be allocated. In theory this could be up to an 8 byte value, but 1071da177e4SLinus Torvalds * in practice, the maximum number of LUNs suppored by any device 1081da177e4SLinus Torvalds * is about 16k. 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds static unsigned int max_scsi_report_luns = 511; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR); 1131da177e4SLinus Torvalds MODULE_PARM_DESC(max_report_luns, 1141da177e4SLinus Torvalds "REPORT LUNS maximum number of LUNS received (should be" 1151da177e4SLinus Torvalds " between 1 and 16384)"); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR); 1201da177e4SLinus Torvalds MODULE_PARM_DESC(inq_timeout, 1211da177e4SLinus Torvalds "Timeout (in seconds) waiting for devices to answer INQUIRY." 1221da177e4SLinus Torvalds " Default is 5. Some non-compliant devices need more."); 1231da177e4SLinus Torvalds 1243e082a91SMatthew Wilcox static DEFINE_SPINLOCK(async_scan_lock); 1253e082a91SMatthew Wilcox static LIST_HEAD(scanning_hosts); 1263e082a91SMatthew Wilcox 1273e082a91SMatthew Wilcox struct async_scan_data { 1283e082a91SMatthew Wilcox struct list_head list; 1293e082a91SMatthew Wilcox struct Scsi_Host *shost; 1303e082a91SMatthew Wilcox struct completion prev_finished; 1313e082a91SMatthew Wilcox }; 1323e082a91SMatthew Wilcox 1333e082a91SMatthew Wilcox /** 1343e082a91SMatthew Wilcox * scsi_complete_async_scans - Wait for asynchronous scans to complete 1353e082a91SMatthew Wilcox * 1368bcc2412SMatthew Wilcox * When this function returns, any host which started scanning before 1378bcc2412SMatthew Wilcox * this function was called will have finished its scan. Hosts which 1388bcc2412SMatthew Wilcox * started scanning after this function was called may or may not have 1398bcc2412SMatthew Wilcox * finished. 1403e082a91SMatthew Wilcox */ 1413e082a91SMatthew Wilcox int scsi_complete_async_scans(void) 1423e082a91SMatthew Wilcox { 1433e082a91SMatthew Wilcox struct async_scan_data *data; 1443e082a91SMatthew Wilcox 1453e082a91SMatthew Wilcox do { 1463e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 1473e082a91SMatthew Wilcox return 0; 1483e082a91SMatthew Wilcox /* If we can't get memory immediately, that's OK. Just 1493e082a91SMatthew Wilcox * sleep a little. Even if we never get memory, the async 1503e082a91SMatthew Wilcox * scans will finish eventually. 1513e082a91SMatthew Wilcox */ 1523e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL); 1533e082a91SMatthew Wilcox if (!data) 1543e082a91SMatthew Wilcox msleep(1); 1553e082a91SMatthew Wilcox } while (!data); 1563e082a91SMatthew Wilcox 1573e082a91SMatthew Wilcox data->shost = NULL; 1583e082a91SMatthew Wilcox init_completion(&data->prev_finished); 1593e082a91SMatthew Wilcox 1603e082a91SMatthew Wilcox spin_lock(&async_scan_lock); 1613e082a91SMatthew Wilcox /* Check that there's still somebody else on the list */ 1623e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 1633e082a91SMatthew Wilcox goto done; 1643e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts); 1653e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 1663e082a91SMatthew Wilcox 1673e082a91SMatthew Wilcox printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n"); 1683e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished); 1693e082a91SMatthew Wilcox 1703e082a91SMatthew Wilcox spin_lock(&async_scan_lock); 1713e082a91SMatthew Wilcox list_del(&data->list); 1728bcc2412SMatthew Wilcox if (!list_empty(&scanning_hosts)) { 1738bcc2412SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next, 1748bcc2412SMatthew Wilcox struct async_scan_data, list); 1758bcc2412SMatthew Wilcox complete(&next->prev_finished); 1768bcc2412SMatthew Wilcox } 1773e082a91SMatthew Wilcox done: 1783e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 1793e082a91SMatthew Wilcox 1803e082a91SMatthew Wilcox kfree(data); 1813e082a91SMatthew Wilcox return 0; 1823e082a91SMatthew Wilcox } 1833e082a91SMatthew Wilcox 1843e082a91SMatthew Wilcox #ifdef MODULE 1853e082a91SMatthew Wilcox /* Only exported for the benefit of scsi_wait_scan */ 1863e082a91SMatthew Wilcox EXPORT_SYMBOL_GPL(scsi_complete_async_scans); 1873e082a91SMatthew Wilcox #endif 1883e082a91SMatthew Wilcox 1891da177e4SLinus Torvalds /** 1901da177e4SLinus Torvalds * scsi_unlock_floptical - unlock device via a special MODE SENSE command 19139216033SJames Bottomley * @sdev: scsi device to send command to 1921da177e4SLinus Torvalds * @result: area to store the result of the MODE SENSE 1931da177e4SLinus Torvalds * 1941da177e4SLinus Torvalds * Description: 19539216033SJames Bottomley * Send a vendor specific MODE SENSE (not a MODE SELECT) command. 1961da177e4SLinus Torvalds * Called for BLIST_KEY devices. 1971da177e4SLinus Torvalds **/ 19839216033SJames Bottomley static void scsi_unlock_floptical(struct scsi_device *sdev, 1991da177e4SLinus Torvalds unsigned char *result) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 2021da177e4SLinus Torvalds 2031da177e4SLinus Torvalds printk(KERN_NOTICE "scsi: unlocking floptical drive\n"); 2041da177e4SLinus Torvalds scsi_cmd[0] = MODE_SENSE; 2051da177e4SLinus Torvalds scsi_cmd[1] = 0; 2061da177e4SLinus Torvalds scsi_cmd[2] = 0x2e; 2071da177e4SLinus Torvalds scsi_cmd[3] = 0; 2081da177e4SLinus Torvalds scsi_cmd[4] = 0x2a; /* size */ 2091da177e4SLinus Torvalds scsi_cmd[5] = 0; 21039216033SJames Bottomley scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL, 21139216033SJames Bottomley SCSI_TIMEOUT, 3); 2121da177e4SLinus Torvalds } 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds /** 2151da177e4SLinus Torvalds * scsi_alloc_sdev - allocate and setup a scsi_Device 2161da177e4SLinus Torvalds * 2171da177e4SLinus Torvalds * Description: 2181da177e4SLinus Torvalds * Allocate, initialize for io, and return a pointer to a scsi_Device. 2191da177e4SLinus Torvalds * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and 2201da177e4SLinus Torvalds * adds scsi_Device to the appropriate list. 2211da177e4SLinus Torvalds * 2221da177e4SLinus Torvalds * Return value: 2231da177e4SLinus Torvalds * scsi_Device pointer, or NULL on failure. 2241da177e4SLinus Torvalds **/ 2251da177e4SLinus Torvalds static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, 2261da177e4SLinus Torvalds unsigned int lun, void *hostdata) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds struct scsi_device *sdev; 2291da177e4SLinus Torvalds int display_failure_msg = 1, ret; 2301da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 2311da177e4SLinus Torvalds 23224669f75SJes Sorensen sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size, 2331da177e4SLinus Torvalds GFP_ATOMIC); 2341da177e4SLinus Torvalds if (!sdev) 2351da177e4SLinus Torvalds goto out; 2361da177e4SLinus Torvalds 2371da177e4SLinus Torvalds sdev->vendor = scsi_null_device_strs; 2381da177e4SLinus Torvalds sdev->model = scsi_null_device_strs; 2391da177e4SLinus Torvalds sdev->rev = scsi_null_device_strs; 2401da177e4SLinus Torvalds sdev->host = shost; 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); 2491da177e4SLinus Torvalds spin_lock_init(&sdev->list_lock); 2501da177e4SLinus Torvalds 2511da177e4SLinus Torvalds sdev->sdev_gendev.parent = get_device(&starget->dev); 2521da177e4SLinus Torvalds sdev->sdev_target = starget; 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds /* usually NULL and set by ->slave_alloc instead */ 2551da177e4SLinus Torvalds sdev->hostdata = hostdata; 2561da177e4SLinus Torvalds 2571da177e4SLinus Torvalds /* if the device needs this changing, it may do so in the 2581da177e4SLinus Torvalds * slave_configure function */ 2591da177e4SLinus Torvalds sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /* 2621da177e4SLinus Torvalds * Some low level driver could use device->type 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds sdev->type = -1; 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds /* 2671da177e4SLinus Torvalds * Assume that the device will have handshaking problems, 2681da177e4SLinus Torvalds * and then fix this field later if it turns out it 2691da177e4SLinus Torvalds * doesn't 2701da177e4SLinus Torvalds */ 2711da177e4SLinus Torvalds sdev->borken = 1; 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds sdev->request_queue = scsi_alloc_queue(sdev); 2741da177e4SLinus Torvalds if (!sdev->request_queue) { 2751da177e4SLinus Torvalds /* release fn is set up in scsi_sysfs_device_initialise, so 2761da177e4SLinus Torvalds * have to free and put manually here */ 2771da177e4SLinus Torvalds put_device(&starget->dev); 27893f56089SDave Jones kfree(sdev); 2791da177e4SLinus Torvalds goto out; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds sdev->request_queue->queuedata = sdev; 2831da177e4SLinus Torvalds scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun); 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds scsi_sysfs_device_initialize(sdev); 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds if (shost->hostt->slave_alloc) { 2881da177e4SLinus Torvalds ret = shost->hostt->slave_alloc(sdev); 2891da177e4SLinus Torvalds if (ret) { 2901da177e4SLinus Torvalds /* 2911da177e4SLinus Torvalds * if LLDD reports slave not present, don't clutter 2921da177e4SLinus Torvalds * console with alloc failure messages 2931da177e4SLinus Torvalds */ 2941da177e4SLinus Torvalds if (ret == -ENXIO) 2951da177e4SLinus Torvalds display_failure_msg = 0; 2961da177e4SLinus Torvalds goto out_device_destroy; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds return sdev; 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds out_device_destroy: 3031da177e4SLinus Torvalds transport_destroy_device(&sdev->sdev_gendev); 3041da177e4SLinus Torvalds put_device(&sdev->sdev_gendev); 3051da177e4SLinus Torvalds out: 3061da177e4SLinus Torvalds if (display_failure_msg) 3071da177e4SLinus Torvalds printk(ALLOC_FAILURE_MSG, __FUNCTION__); 3081da177e4SLinus Torvalds return NULL; 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static void scsi_target_dev_release(struct device *dev) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds struct device *parent = dev->parent; 3141da177e4SLinus Torvalds struct scsi_target *starget = to_scsi_target(dev); 315a283bd37SJames Bottomley 3161da177e4SLinus Torvalds kfree(starget); 3171da177e4SLinus Torvalds put_device(parent); 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds int scsi_is_target_device(const struct device *dev) 3211da177e4SLinus Torvalds { 3221da177e4SLinus Torvalds return dev->release == scsi_target_dev_release; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_is_target_device); 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static struct scsi_target *__scsi_find_target(struct device *parent, 3271da177e4SLinus Torvalds int channel, uint id) 3281da177e4SLinus Torvalds { 3291da177e4SLinus Torvalds struct scsi_target *starget, *found_starget = NULL; 3301da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 3311da177e4SLinus Torvalds /* 3321da177e4SLinus Torvalds * Search for an existing target for this sdev. 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds list_for_each_entry(starget, &shost->__targets, siblings) { 3351da177e4SLinus Torvalds if (starget->id == id && 3361da177e4SLinus Torvalds starget->channel == channel) { 3371da177e4SLinus Torvalds found_starget = starget; 3381da177e4SLinus Torvalds break; 3391da177e4SLinus Torvalds } 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds if (found_starget) 3421da177e4SLinus Torvalds get_device(&found_starget->dev); 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds return found_starget; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 347884d25ccSJames Bottomley /** 348884d25ccSJames Bottomley * scsi_alloc_target - allocate a new or find an existing target 349884d25ccSJames Bottomley * @parent: parent of the target (need not be a scsi host) 350884d25ccSJames Bottomley * @channel: target channel number (zero if no channels) 351884d25ccSJames Bottomley * @id: target id number 352884d25ccSJames Bottomley * 353884d25ccSJames Bottomley * Return an existing target if one exists, provided it hasn't already 354884d25ccSJames Bottomley * gone into STARGET_DEL state, otherwise allocate a new target. 355884d25ccSJames Bottomley * 356884d25ccSJames Bottomley * The target is returned with an incremented reference, so the caller 357884d25ccSJames Bottomley * is responsible for both reaping and doing a last put 358884d25ccSJames Bottomley */ 3591da177e4SLinus Torvalds static struct scsi_target *scsi_alloc_target(struct device *parent, 3601da177e4SLinus Torvalds int channel, uint id) 3611da177e4SLinus Torvalds { 3621da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 3631da177e4SLinus Torvalds struct device *dev = NULL; 3641da177e4SLinus Torvalds unsigned long flags; 3651da177e4SLinus Torvalds const int size = sizeof(struct scsi_target) 3661da177e4SLinus Torvalds + shost->transportt->target_size; 3675c44cd2aSJames.Smart@Emulex.Com struct scsi_target *starget; 3681da177e4SLinus Torvalds struct scsi_target *found_target; 36932f95792SBrian King int error; 3701da177e4SLinus Torvalds 37124669f75SJes Sorensen starget = kzalloc(size, GFP_KERNEL); 3721da177e4SLinus Torvalds if (!starget) { 3731da177e4SLinus Torvalds printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__); 3741da177e4SLinus Torvalds return NULL; 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds dev = &starget->dev; 3771da177e4SLinus Torvalds device_initialize(dev); 3781da177e4SLinus Torvalds starget->reap_ref = 1; 3791da177e4SLinus Torvalds dev->parent = get_device(parent); 3801da177e4SLinus Torvalds dev->release = scsi_target_dev_release; 3811da177e4SLinus Torvalds sprintf(dev->bus_id, "target%d:%d:%d", 3821da177e4SLinus Torvalds shost->host_no, channel, id); 3831da177e4SLinus Torvalds starget->id = id; 3841da177e4SLinus Torvalds starget->channel = channel; 3851da177e4SLinus Torvalds INIT_LIST_HEAD(&starget->siblings); 3861da177e4SLinus Torvalds INIT_LIST_HEAD(&starget->devices); 387ffedb452SJames Bottomley starget->state = STARGET_RUNNING; 388ffedb452SJames Bottomley retry: 3891da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds found_target = __scsi_find_target(parent, channel, id); 3921da177e4SLinus Torvalds if (found_target) 3931da177e4SLinus Torvalds goto found; 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds list_add_tail(&starget->siblings, &shost->__targets); 3961da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 3971da177e4SLinus Torvalds /* allocate and add */ 398a283bd37SJames Bottomley transport_setup_device(dev); 39932f95792SBrian King error = device_add(dev); 40032f95792SBrian King if (error) { 40132f95792SBrian King dev_err(dev, "target device_add failed, error %d\n", error); 40232f95792SBrian King spin_lock_irqsave(shost->host_lock, flags); 40332f95792SBrian King list_del_init(&starget->siblings); 40432f95792SBrian King spin_unlock_irqrestore(shost->host_lock, flags); 40532f95792SBrian King transport_destroy_device(dev); 40632f95792SBrian King put_device(parent); 40732f95792SBrian King kfree(starget); 40832f95792SBrian King return NULL; 40932f95792SBrian King } 410a283bd37SJames Bottomley transport_add_device(dev); 411a283bd37SJames Bottomley if (shost->hostt->target_alloc) { 41232f95792SBrian King error = shost->hostt->target_alloc(starget); 413a283bd37SJames Bottomley 414a283bd37SJames Bottomley if(error) { 415a283bd37SJames Bottomley dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error); 416a283bd37SJames Bottomley /* don't want scsi_target_reap to do the final 417a283bd37SJames Bottomley * put because it will be under the host lock */ 418a283bd37SJames Bottomley get_device(dev); 419a283bd37SJames Bottomley scsi_target_reap(starget); 420a283bd37SJames Bottomley put_device(dev); 421a283bd37SJames Bottomley return NULL; 422a283bd37SJames Bottomley } 423a283bd37SJames Bottomley } 424884d25ccSJames Bottomley get_device(dev); 425a283bd37SJames Bottomley 4261da177e4SLinus Torvalds return starget; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds found: 4291da177e4SLinus Torvalds found_target->reap_ref++; 4301da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 431ffedb452SJames Bottomley if (found_target->state != STARGET_DEL) { 432884d25ccSJames Bottomley put_device(parent); 4331da177e4SLinus Torvalds kfree(starget); 4341da177e4SLinus Torvalds return found_target; 4351da177e4SLinus Torvalds } 436ffedb452SJames Bottomley /* Unfortunately, we found a dying target; need to 437ffedb452SJames Bottomley * wait until it's dead before we can get a new one */ 438ffedb452SJames Bottomley put_device(&found_target->dev); 439ffedb452SJames Bottomley flush_scheduled_work(); 440ffedb452SJames Bottomley goto retry; 441ffedb452SJames Bottomley } 4421da177e4SLinus Torvalds 44365f27f38SDavid Howells static void scsi_target_reap_usercontext(struct work_struct *work) 44465110b21SJames Bottomley { 44565f27f38SDavid Howells struct scsi_target *starget = 44665f27f38SDavid Howells container_of(work, struct scsi_target, ew.work); 447863a930aSJames Bottomley struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 448863a930aSJames Bottomley unsigned long flags; 449863a930aSJames Bottomley 45004333393SJames Bottomley transport_remove_device(&starget->dev); 451863a930aSJames Bottomley device_del(&starget->dev); 45204333393SJames Bottomley transport_destroy_device(&starget->dev); 453ffedb452SJames Bottomley spin_lock_irqsave(shost->host_lock, flags); 454a50a5e37SMike Anderson if (shost->hostt->target_destroy) 455a50a5e37SMike Anderson shost->hostt->target_destroy(starget); 456ffedb452SJames Bottomley list_del_init(&starget->siblings); 457863a930aSJames Bottomley spin_unlock_irqrestore(shost->host_lock, flags); 458ffedb452SJames Bottomley put_device(&starget->dev); 459863a930aSJames Bottomley } 460863a930aSJames Bottomley 4611da177e4SLinus Torvalds /** 4621da177e4SLinus Torvalds * scsi_target_reap - check to see if target is in use and destroy if not 4631da177e4SLinus Torvalds * 4641da177e4SLinus Torvalds * @starget: target to be checked 4651da177e4SLinus Torvalds * 4661da177e4SLinus Torvalds * This is used after removing a LUN or doing a last put of the target 4671da177e4SLinus Torvalds * it checks atomically that nothing is using the target and removes 4681da177e4SLinus Torvalds * it if so. 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds void scsi_target_reap(struct scsi_target *starget) 4711da177e4SLinus Torvalds { 472ffedb452SJames Bottomley struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 473ffedb452SJames Bottomley unsigned long flags; 474ffedb452SJames Bottomley 475ffedb452SJames Bottomley spin_lock_irqsave(shost->host_lock, flags); 476ffedb452SJames Bottomley 477ffedb452SJames Bottomley if (--starget->reap_ref == 0 && list_empty(&starget->devices)) { 478ffedb452SJames Bottomley BUG_ON(starget->state == STARGET_DEL); 479ffedb452SJames Bottomley starget->state = STARGET_DEL; 480ffedb452SJames Bottomley spin_unlock_irqrestore(shost->host_lock, flags); 481ffedb452SJames Bottomley execute_in_process_context(scsi_target_reap_usercontext, 48265f27f38SDavid Howells &starget->ew); 483ffedb452SJames Bottomley return; 484ffedb452SJames Bottomley 485ffedb452SJames Bottomley } 486ffedb452SJames Bottomley spin_unlock_irqrestore(shost->host_lock, flags); 487ffedb452SJames Bottomley 488ffedb452SJames Bottomley return; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds 4911da177e4SLinus Torvalds /** 492e5b3cd42SAlan Stern * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string 493e5b3cd42SAlan Stern * @s: INQUIRY result string to sanitize 494e5b3cd42SAlan Stern * @len: length of the string 495e5b3cd42SAlan Stern * 496e5b3cd42SAlan Stern * Description: 497e5b3cd42SAlan Stern * The SCSI spec says that INQUIRY vendor, product, and revision 498e5b3cd42SAlan Stern * strings must consist entirely of graphic ASCII characters, 499e5b3cd42SAlan Stern * padded on the right with spaces. Since not all devices obey 500e5b3cd42SAlan Stern * this rule, we will replace non-graphic or non-ASCII characters 501e5b3cd42SAlan Stern * with spaces. Exception: a NUL character is interpreted as a 502e5b3cd42SAlan Stern * string terminator, so all the following characters are set to 503e5b3cd42SAlan Stern * spaces. 504e5b3cd42SAlan Stern **/ 505e5b3cd42SAlan Stern static void sanitize_inquiry_string(unsigned char *s, int len) 506e5b3cd42SAlan Stern { 507e5b3cd42SAlan Stern int terminated = 0; 508e5b3cd42SAlan Stern 509e5b3cd42SAlan Stern for (; len > 0; (--len, ++s)) { 510e5b3cd42SAlan Stern if (*s == 0) 511e5b3cd42SAlan Stern terminated = 1; 512e5b3cd42SAlan Stern if (terminated || *s < 0x20 || *s > 0x7e) 513e5b3cd42SAlan Stern *s = ' '; 514e5b3cd42SAlan Stern } 515e5b3cd42SAlan Stern } 516e5b3cd42SAlan Stern 517e5b3cd42SAlan Stern /** 5181da177e4SLinus Torvalds * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY 51939216033SJames Bottomley * @sdev: scsi_device to probe 5201da177e4SLinus Torvalds * @inq_result: area to store the INQUIRY result 52139216033SJames Bottomley * @result_len: len of inq_result 5221da177e4SLinus Torvalds * @bflags: store any bflags found here 5231da177e4SLinus Torvalds * 5241da177e4SLinus Torvalds * Description: 52539216033SJames Bottomley * Probe the lun associated with @req using a standard SCSI INQUIRY; 5261da177e4SLinus Torvalds * 52739216033SJames Bottomley * If the INQUIRY is successful, zero is returned and the 5281da177e4SLinus Torvalds * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length 529f64a181dSChristoph Hellwig * are copied to the scsi_device any flags value is stored in *@bflags. 5301da177e4SLinus Torvalds **/ 531e5b3cd42SAlan Stern static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result, 53239216033SJames Bottomley int result_len, int *bflags) 5331da177e4SLinus Torvalds { 5341da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 5351da177e4SLinus Torvalds int first_inquiry_len, try_inquiry_len, next_inquiry_len; 5361da177e4SLinus Torvalds int response_len = 0; 53739216033SJames Bottomley int pass, count, result; 5381da177e4SLinus Torvalds struct scsi_sense_hdr sshdr; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds *bflags = 0; 5411da177e4SLinus Torvalds 5421da177e4SLinus Torvalds /* Perform up to 3 passes. The first pass uses a conservative 5431da177e4SLinus Torvalds * transfer length of 36 unless sdev->inquiry_len specifies a 5441da177e4SLinus Torvalds * different value. */ 5451da177e4SLinus Torvalds first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36; 5461da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len; 5471da177e4SLinus Torvalds pass = 1; 5481da177e4SLinus Torvalds 5491da177e4SLinus Torvalds next_pass: 5509ccfc756SJames Bottomley SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, 5519ccfc756SJames Bottomley "scsi scan: INQUIRY pass %d length %d\n", 5529ccfc756SJames Bottomley pass, try_inquiry_len)); 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /* Each pass gets up to three chances to ignore Unit Attention */ 5551da177e4SLinus Torvalds for (count = 0; count < 3; ++count) { 5561da177e4SLinus Torvalds memset(scsi_cmd, 0, 6); 5571da177e4SLinus Torvalds scsi_cmd[0] = INQUIRY; 5581da177e4SLinus Torvalds scsi_cmd[4] = (unsigned char) try_inquiry_len; 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds memset(inq_result, 0, try_inquiry_len); 56139216033SJames Bottomley 56239216033SJames Bottomley result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, 563ea73a9f2SJames Bottomley inq_result, try_inquiry_len, &sshdr, 5641da177e4SLinus Torvalds HZ / 2 + HZ * scsi_inq_timeout, 3); 5651da177e4SLinus Torvalds 5661da177e4SLinus Torvalds SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s " 5671da177e4SLinus Torvalds "with code 0x%x\n", 56839216033SJames Bottomley result ? "failed" : "successful", result)); 5691da177e4SLinus Torvalds 57039216033SJames Bottomley if (result) { 5711da177e4SLinus Torvalds /* 5721da177e4SLinus Torvalds * not-ready to ready transition [asc/ascq=0x28/0x0] 5731da177e4SLinus Torvalds * or power-on, reset [asc/ascq=0x29/0x0], continue. 5741da177e4SLinus Torvalds * INQUIRY should not yield UNIT_ATTENTION 5751da177e4SLinus Torvalds * but many buggy devices do so anyway. 5761da177e4SLinus Torvalds */ 57739216033SJames Bottomley if ((driver_byte(result) & DRIVER_SENSE) && 578ea73a9f2SJames Bottomley scsi_sense_valid(&sshdr)) { 5791da177e4SLinus Torvalds if ((sshdr.sense_key == UNIT_ATTENTION) && 5801da177e4SLinus Torvalds ((sshdr.asc == 0x28) || 5811da177e4SLinus Torvalds (sshdr.asc == 0x29)) && 5821da177e4SLinus Torvalds (sshdr.ascq == 0)) 5831da177e4SLinus Torvalds continue; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds } 5861da177e4SLinus Torvalds break; 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 58939216033SJames Bottomley if (result == 0) { 590e5b3cd42SAlan Stern sanitize_inquiry_string(&inq_result[8], 8); 591e5b3cd42SAlan Stern sanitize_inquiry_string(&inq_result[16], 16); 592e5b3cd42SAlan Stern sanitize_inquiry_string(&inq_result[32], 4); 593e5b3cd42SAlan Stern 594e5b3cd42SAlan Stern response_len = inq_result[4] + 5; 5951da177e4SLinus Torvalds if (response_len > 255) 5961da177e4SLinus Torvalds response_len = first_inquiry_len; /* sanity */ 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds /* 5991da177e4SLinus Torvalds * Get any flags for this device. 6001da177e4SLinus Torvalds * 601f64a181dSChristoph Hellwig * XXX add a bflags to scsi_device, and replace the 602f64a181dSChristoph Hellwig * corresponding bit fields in scsi_device, so bflags 6031da177e4SLinus Torvalds * need not be passed as an argument. 6041da177e4SLinus Torvalds */ 6051da177e4SLinus Torvalds *bflags = scsi_get_device_flags(sdev, &inq_result[8], 6061da177e4SLinus Torvalds &inq_result[16]); 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /* When the first pass succeeds we gain information about 6091da177e4SLinus Torvalds * what larger transfer lengths might work. */ 6101da177e4SLinus Torvalds if (pass == 1) { 6111da177e4SLinus Torvalds if (BLIST_INQUIRY_36 & *bflags) 6121da177e4SLinus Torvalds next_inquiry_len = 36; 6131da177e4SLinus Torvalds else if (BLIST_INQUIRY_58 & *bflags) 6141da177e4SLinus Torvalds next_inquiry_len = 58; 6151da177e4SLinus Torvalds else if (sdev->inquiry_len) 6161da177e4SLinus Torvalds next_inquiry_len = sdev->inquiry_len; 6171da177e4SLinus Torvalds else 6181da177e4SLinus Torvalds next_inquiry_len = response_len; 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds /* If more data is available perform the second pass */ 6211da177e4SLinus Torvalds if (next_inquiry_len > try_inquiry_len) { 6221da177e4SLinus Torvalds try_inquiry_len = next_inquiry_len; 6231da177e4SLinus Torvalds pass = 2; 6241da177e4SLinus Torvalds goto next_pass; 6251da177e4SLinus Torvalds } 6261da177e4SLinus Torvalds } 6271da177e4SLinus Torvalds 6281da177e4SLinus Torvalds } else if (pass == 2) { 6291da177e4SLinus Torvalds printk(KERN_INFO "scsi scan: %d byte inquiry failed. " 6301da177e4SLinus Torvalds "Consider BLIST_INQUIRY_36 for this device\n", 6311da177e4SLinus Torvalds try_inquiry_len); 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds /* If this pass failed, the third pass goes back and transfers 6341da177e4SLinus Torvalds * the same amount as we successfully got in the first pass. */ 6351da177e4SLinus Torvalds try_inquiry_len = first_inquiry_len; 6361da177e4SLinus Torvalds pass = 3; 6371da177e4SLinus Torvalds goto next_pass; 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /* If the last transfer attempt got an error, assume the 6411da177e4SLinus Torvalds * peripheral doesn't exist or is dead. */ 64239216033SJames Bottomley if (result) 64339216033SJames Bottomley return -EIO; 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds /* Don't report any more data than the device says is valid */ 6461da177e4SLinus Torvalds sdev->inquiry_len = min(try_inquiry_len, response_len); 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds /* 6491da177e4SLinus Torvalds * XXX Abort if the response length is less than 36? If less than 6501da177e4SLinus Torvalds * 32, the lookup of the device flags (above) could be invalid, 6511da177e4SLinus Torvalds * and it would be possible to take an incorrect action - we do 6521da177e4SLinus Torvalds * not want to hang because of a short INQUIRY. On the flip side, 6531da177e4SLinus Torvalds * if the device is spun down or becoming ready (and so it gives a 6541da177e4SLinus Torvalds * short INQUIRY), an abort here prevents any further use of the 6551da177e4SLinus Torvalds * device, including spin up. 6561da177e4SLinus Torvalds * 657*e423ee31SAlan Stern * On the whole, the best approach seems to be to assume the first 658*e423ee31SAlan Stern * 36 bytes are valid no matter what the device says. That's 659*e423ee31SAlan Stern * better than copying < 36 bytes to the inquiry-result buffer 660*e423ee31SAlan Stern * and displaying garbage for the Vendor, Product, or Revision 661*e423ee31SAlan Stern * strings. 662*e423ee31SAlan Stern */ 663*e423ee31SAlan Stern if (sdev->inquiry_len < 36) { 664*e423ee31SAlan Stern printk(KERN_INFO "scsi scan: INQUIRY result too short (%d)," 665*e423ee31SAlan Stern " using 36\n", sdev->inquiry_len); 666*e423ee31SAlan Stern sdev->inquiry_len = 36; 667*e423ee31SAlan Stern } 668*e423ee31SAlan Stern 669*e423ee31SAlan Stern /* 6701da177e4SLinus Torvalds * Related to the above issue: 6711da177e4SLinus Torvalds * 6721da177e4SLinus Torvalds * XXX Devices (disk or all?) should be sent a TEST UNIT READY, 6731da177e4SLinus Torvalds * and if not ready, sent a START_STOP to start (maybe spin up) and 6741da177e4SLinus Torvalds * then send the INQUIRY again, since the INQUIRY can change after 6751da177e4SLinus Torvalds * a device is initialized. 6761da177e4SLinus Torvalds * 6771da177e4SLinus Torvalds * Ideally, start a device if explicitly asked to do so. This 6781da177e4SLinus Torvalds * assumes that a device is spun up on power on, spun down on 6791da177e4SLinus Torvalds * request, and then spun up on request. 6801da177e4SLinus Torvalds */ 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds /* 6831da177e4SLinus Torvalds * The scanning code needs to know the scsi_level, even if no 6841da177e4SLinus Torvalds * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so 6851da177e4SLinus Torvalds * non-zero LUNs can be scanned. 6861da177e4SLinus Torvalds */ 6871da177e4SLinus Torvalds sdev->scsi_level = inq_result[2] & 0x07; 6881da177e4SLinus Torvalds if (sdev->scsi_level >= 2 || 6891da177e4SLinus Torvalds (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1)) 6901da177e4SLinus Torvalds sdev->scsi_level++; 6916f3a2024SJames Bottomley sdev->sdev_target->scsi_level = sdev->scsi_level; 6921da177e4SLinus Torvalds 69339216033SJames Bottomley return 0; 6941da177e4SLinus Torvalds } 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds /** 697f64a181dSChristoph Hellwig * scsi_add_lun - allocate and fully initialze a scsi_device 698f64a181dSChristoph Hellwig * @sdevscan: holds information to be stored in the new scsi_device 699f64a181dSChristoph Hellwig * @sdevnew: store the address of the newly allocated scsi_device 7001da177e4SLinus Torvalds * @inq_result: holds the result of a previous INQUIRY to the LUN 7011da177e4SLinus Torvalds * @bflags: black/white list flag 7021da177e4SLinus Torvalds * 7031da177e4SLinus Torvalds * Description: 704f64a181dSChristoph Hellwig * Allocate and initialize a scsi_device matching sdevscan. Optionally 7051da177e4SLinus Torvalds * set fields based on values in *@bflags. If @sdevnew is not 706f64a181dSChristoph Hellwig * NULL, store the address of the new scsi_device in *@sdevnew (needed 7071da177e4SLinus Torvalds * when scanning a particular LUN). 7081da177e4SLinus Torvalds * 7091da177e4SLinus Torvalds * Return: 710f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 711f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 7121da177e4SLinus Torvalds **/ 713e5b3cd42SAlan Stern static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, 7143e082a91SMatthew Wilcox int *bflags, int async) 7151da177e4SLinus Torvalds { 7161da177e4SLinus Torvalds /* 7171da177e4SLinus Torvalds * XXX do not save the inquiry, since it can change underneath us, 7181da177e4SLinus Torvalds * save just vendor/model/rev. 7191da177e4SLinus Torvalds * 7201da177e4SLinus Torvalds * Rather than save it and have an ioctl that retrieves the saved 7211da177e4SLinus Torvalds * value, have an ioctl that executes the same INQUIRY code used 7221da177e4SLinus Torvalds * in scsi_probe_lun, let user level programs doing INQUIRY 7231da177e4SLinus Torvalds * scanning run at their own risk, or supply a user level program 7241da177e4SLinus Torvalds * that can correctly scan. 7251da177e4SLinus Torvalds */ 7261da177e4SLinus Torvalds 72709123d23SAlan Stern /* 72809123d23SAlan Stern * Copy at least 36 bytes of INQUIRY data, so that we don't 72909123d23SAlan Stern * dereference unallocated memory when accessing the Vendor, 73009123d23SAlan Stern * Product, and Revision strings. Badly behaved devices may set 73109123d23SAlan Stern * the INQUIRY Additional Length byte to a small value, indicating 73209123d23SAlan Stern * these strings are invalid, but often they contain plausible data 73309123d23SAlan Stern * nonetheless. It doesn't matter if the device sent < 36 bytes 73409123d23SAlan Stern * total, since scsi_probe_lun() initializes inq_result with 0s. 73509123d23SAlan Stern */ 73609123d23SAlan Stern sdev->inquiry = kmemdup(inq_result, 73709123d23SAlan Stern max_t(size_t, sdev->inquiry_len, 36), 73809123d23SAlan Stern GFP_ATOMIC); 73909123d23SAlan Stern if (sdev->inquiry == NULL) 74009123d23SAlan Stern return SCSI_SCAN_NO_RESPONSE; 74109123d23SAlan Stern 7421da177e4SLinus Torvalds sdev->vendor = (char *) (sdev->inquiry + 8); 7431da177e4SLinus Torvalds sdev->model = (char *) (sdev->inquiry + 16); 7441da177e4SLinus Torvalds sdev->rev = (char *) (sdev->inquiry + 32); 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds if (*bflags & BLIST_ISROM) { 7471da177e4SLinus Torvalds /* 7481da177e4SLinus Torvalds * It would be better to modify sdev->type, and set 7494ff36718SMatthew Wilcox * sdev->removable; this can now be done since 7504ff36718SMatthew Wilcox * print_inquiry has gone away. 7511da177e4SLinus Torvalds */ 7521da177e4SLinus Torvalds inq_result[0] = TYPE_ROM; 7531da177e4SLinus Torvalds inq_result[1] |= 0x80; /* removable */ 7541da177e4SLinus Torvalds } else if (*bflags & BLIST_NO_ULD_ATTACH) 7551da177e4SLinus Torvalds sdev->no_uld_attach = 1; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds switch (sdev->type = (inq_result[0] & 0x1f)) { 758ddaf6fc8SJames Bottomley case TYPE_RBC: 759ddaf6fc8SJames Bottomley /* RBC devices can return SCSI-3 compliance and yet 760ddaf6fc8SJames Bottomley * still not support REPORT LUNS, so make them act as 761ddaf6fc8SJames Bottomley * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is 762ddaf6fc8SJames Bottomley * specifically set */ 763ddaf6fc8SJames Bottomley if ((*bflags & BLIST_REPORTLUN2) == 0) 764ddaf6fc8SJames Bottomley *bflags |= BLIST_NOREPORTLUN; 765ddaf6fc8SJames Bottomley /* fall through */ 7661da177e4SLinus Torvalds case TYPE_TAPE: 7671da177e4SLinus Torvalds case TYPE_DISK: 7681da177e4SLinus Torvalds case TYPE_PRINTER: 7691da177e4SLinus Torvalds case TYPE_MOD: 7701da177e4SLinus Torvalds case TYPE_PROCESSOR: 7711da177e4SLinus Torvalds case TYPE_SCANNER: 7721da177e4SLinus Torvalds case TYPE_MEDIUM_CHANGER: 7731da177e4SLinus Torvalds case TYPE_ENCLOSURE: 7741da177e4SLinus Torvalds case TYPE_COMM: 7754d7db04aSJames Bottomley case TYPE_RAID: 7761da177e4SLinus Torvalds sdev->writeable = 1; 7771da177e4SLinus Torvalds break; 7781da177e4SLinus Torvalds case TYPE_ROM: 779ddaf6fc8SJames Bottomley /* MMC devices can return SCSI-3 compliance and yet 780ddaf6fc8SJames Bottomley * still not support REPORT LUNS, so make them act as 781ddaf6fc8SJames Bottomley * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is 782ddaf6fc8SJames Bottomley * specifically set */ 783ddaf6fc8SJames Bottomley if ((*bflags & BLIST_REPORTLUN2) == 0) 784ddaf6fc8SJames Bottomley *bflags |= BLIST_NOREPORTLUN; 785ddaf6fc8SJames Bottomley /* fall through */ 786ddaf6fc8SJames Bottomley case TYPE_WORM: 7871da177e4SLinus Torvalds sdev->writeable = 0; 7881da177e4SLinus Torvalds break; 7891da177e4SLinus Torvalds default: 7901da177e4SLinus Torvalds printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type); 7911da177e4SLinus Torvalds } 7921da177e4SLinus Torvalds 7931da177e4SLinus Torvalds /* 7941da177e4SLinus Torvalds * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI 7951da177e4SLinus Torvalds * spec says: The device server is capable of supporting the 7961da177e4SLinus Torvalds * specified peripheral device type on this logical unit. However, 7971da177e4SLinus Torvalds * the physical device is not currently connected to this logical 7981da177e4SLinus Torvalds * unit. 7991da177e4SLinus Torvalds * 8001da177e4SLinus Torvalds * The above is vague, as it implies that we could treat 001 and 8011da177e4SLinus Torvalds * 011 the same. Stay compatible with previous code, and create a 802f64a181dSChristoph Hellwig * scsi_device for a PQ of 1 8031da177e4SLinus Torvalds * 8041da177e4SLinus Torvalds * Don't set the device offline here; rather let the upper 8051da177e4SLinus Torvalds * level drivers eval the PQ to decide whether they should 8061da177e4SLinus Torvalds * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. 8071da177e4SLinus Torvalds */ 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; 8101da177e4SLinus Torvalds sdev->removable = (0x80 & inq_result[1]) >> 7; 8111da177e4SLinus Torvalds sdev->lockable = sdev->removable; 8121da177e4SLinus Torvalds sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 && 8151da177e4SLinus Torvalds inq_result[56] & 0x04)) 8161da177e4SLinus Torvalds sdev->ppr = 1; 8171da177e4SLinus Torvalds if (inq_result[7] & 0x60) 8181da177e4SLinus Torvalds sdev->wdtr = 1; 8191da177e4SLinus Torvalds if (inq_result[7] & 0x10) 8201da177e4SLinus Torvalds sdev->sdtr = 1; 8211da177e4SLinus Torvalds 82219ac0db3SJames Bottomley sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d " 8234ff36718SMatthew Wilcox "ANSI: %d%s\n", scsi_device_type(sdev->type), 8244ff36718SMatthew Wilcox sdev->vendor, sdev->model, sdev->rev, 8254ff36718SMatthew Wilcox sdev->inq_periph_qual, inq_result[2] & 0x07, 8264ff36718SMatthew Wilcox (inq_result[3] & 0x0f) == 1 ? " CCS" : ""); 8274ff36718SMatthew Wilcox 8281da177e4SLinus Torvalds /* 8295e3c34c1SGreg KH * End sysfs code. 8301da177e4SLinus Torvalds */ 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && 8331da177e4SLinus Torvalds !(*bflags & BLIST_NOTQ)) 8341da177e4SLinus Torvalds sdev->tagged_supported = 1; 8351da177e4SLinus Torvalds /* 8361da177e4SLinus Torvalds * Some devices (Texel CD ROM drives) have handshaking problems 8371da177e4SLinus Torvalds * when used with the Seagate controllers. borken is initialized 8381da177e4SLinus Torvalds * to 1, and then set it to 0 here. 8391da177e4SLinus Torvalds */ 8401da177e4SLinus Torvalds if ((*bflags & BLIST_BORKEN) == 0) 8411da177e4SLinus Torvalds sdev->borken = 0; 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds /* 8441da177e4SLinus Torvalds * Apparently some really broken devices (contrary to the SCSI 8451da177e4SLinus Torvalds * standards) need to be selected without asserting ATN 8461da177e4SLinus Torvalds */ 8471da177e4SLinus Torvalds if (*bflags & BLIST_SELECT_NO_ATN) 8481da177e4SLinus Torvalds sdev->select_no_atn = 1; 8491da177e4SLinus Torvalds 8501da177e4SLinus Torvalds /* 8514d7db04aSJames Bottomley * Maximum 512 sector transfer length 8524d7db04aSJames Bottomley * broken RA4x00 Compaq Disk Array 8534d7db04aSJames Bottomley */ 8544d7db04aSJames Bottomley if (*bflags & BLIST_MAX_512) 8554d7db04aSJames Bottomley blk_queue_max_sectors(sdev->request_queue, 512); 8564d7db04aSJames Bottomley 8574d7db04aSJames Bottomley /* 8581da177e4SLinus Torvalds * Some devices may not want to have a start command automatically 8591da177e4SLinus Torvalds * issued when a device is added. 8601da177e4SLinus Torvalds */ 8611da177e4SLinus Torvalds if (*bflags & BLIST_NOSTARTONADD) 8621da177e4SLinus Torvalds sdev->no_start_on_add = 1; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds if (*bflags & BLIST_SINGLELUN) 8651da177e4SLinus Torvalds sdev->single_lun = 1; 8661da177e4SLinus Torvalds 8671da177e4SLinus Torvalds 8681da177e4SLinus Torvalds sdev->use_10_for_rw = 1; 8691da177e4SLinus Torvalds 8701da177e4SLinus Torvalds if (*bflags & BLIST_MS_SKIP_PAGE_08) 8711da177e4SLinus Torvalds sdev->skip_ms_page_8 = 1; 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (*bflags & BLIST_MS_SKIP_PAGE_3F) 8741da177e4SLinus Torvalds sdev->skip_ms_page_3f = 1; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds if (*bflags & BLIST_USE_10_BYTE_MS) 8771da177e4SLinus Torvalds sdev->use_10_for_ms = 1; 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* set the device running here so that slave configure 8801da177e4SLinus Torvalds * may do I/O */ 8811da177e4SLinus Torvalds scsi_device_set_state(sdev, SDEV_RUNNING); 8821da177e4SLinus Torvalds 8831da177e4SLinus Torvalds if (*bflags & BLIST_MS_192_BYTES_FOR_3F) 8841da177e4SLinus Torvalds sdev->use_192_bytes_for_3f = 1; 8851da177e4SLinus Torvalds 8861da177e4SLinus Torvalds if (*bflags & BLIST_NOT_LOCKABLE) 8871da177e4SLinus Torvalds sdev->lockable = 0; 8881da177e4SLinus Torvalds 8891da177e4SLinus Torvalds if (*bflags & BLIST_RETRY_HWERROR) 8901da177e4SLinus Torvalds sdev->retry_hwerror = 1; 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds transport_configure_device(&sdev->sdev_gendev); 8931da177e4SLinus Torvalds 89493805091SChristoph Hellwig if (sdev->host->hostt->slave_configure) { 89593805091SChristoph Hellwig int ret = sdev->host->hostt->slave_configure(sdev); 89693805091SChristoph Hellwig if (ret) { 89793805091SChristoph Hellwig /* 89893805091SChristoph Hellwig * if LLDD reports slave not present, don't clutter 89993805091SChristoph Hellwig * console with alloc failure messages 90093805091SChristoph Hellwig */ 90193805091SChristoph Hellwig if (ret != -ENXIO) { 90293805091SChristoph Hellwig sdev_printk(KERN_ERR, sdev, 90393805091SChristoph Hellwig "failed to configure device\n"); 90493805091SChristoph Hellwig } 90593805091SChristoph Hellwig return SCSI_SCAN_NO_RESPONSE; 90693805091SChristoph Hellwig } 90793805091SChristoph Hellwig } 9081da177e4SLinus Torvalds 9091da177e4SLinus Torvalds /* 9101da177e4SLinus Torvalds * Ok, the device is now all set up, we can 9111da177e4SLinus Torvalds * register it and tell the rest of the kernel 9121da177e4SLinus Torvalds * about it. 9131da177e4SLinus Torvalds */ 9143e082a91SMatthew Wilcox if (!async && scsi_sysfs_add_sdev(sdev) != 0) 915b24b1033SAlan Stern return SCSI_SCAN_NO_RESPONSE; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 9206f3a2024SJames Bottomley static inline void scsi_destroy_sdev(struct scsi_device *sdev) 9216f3a2024SJames Bottomley { 922309bd271SBrian King scsi_device_set_state(sdev, SDEV_DEL); 9236f3a2024SJames Bottomley if (sdev->host->hostt->slave_destroy) 9246f3a2024SJames Bottomley sdev->host->hostt->slave_destroy(sdev); 9256f3a2024SJames Bottomley transport_destroy_device(&sdev->sdev_gendev); 9266f3a2024SJames Bottomley put_device(&sdev->sdev_gendev); 9276f3a2024SJames Bottomley } 9286f3a2024SJames Bottomley 929c5f2e640Sakpm@osdl.org #ifdef CONFIG_SCSI_LOGGING 9306c7154c9SKurt Garloff /** 9316c7154c9SKurt Garloff * scsi_inq_str - print INQUIRY data from min to max index, 9326c7154c9SKurt Garloff * strip trailing whitespace 9336c7154c9SKurt Garloff * @buf: Output buffer with at least end-first+1 bytes of space 9346c7154c9SKurt Garloff * @inq: Inquiry buffer (input) 9356c7154c9SKurt Garloff * @first: Offset of string into inq 9366c7154c9SKurt Garloff * @end: Index after last character in inq 9376c7154c9SKurt Garloff */ 9386c7154c9SKurt Garloff static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq, 9396c7154c9SKurt Garloff unsigned first, unsigned end) 9406c7154c9SKurt Garloff { 9416c7154c9SKurt Garloff unsigned term = 0, idx; 942c5f2e640Sakpm@osdl.org 943c5f2e640Sakpm@osdl.org for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) { 944c5f2e640Sakpm@osdl.org if (inq[idx+first] > ' ') { 9456c7154c9SKurt Garloff buf[idx] = inq[idx+first]; 9466c7154c9SKurt Garloff term = idx+1; 9476c7154c9SKurt Garloff } else { 9486c7154c9SKurt Garloff buf[idx] = ' '; 9496c7154c9SKurt Garloff } 9506c7154c9SKurt Garloff } 9516c7154c9SKurt Garloff buf[term] = 0; 9526c7154c9SKurt Garloff return buf; 9536c7154c9SKurt Garloff } 954c5f2e640Sakpm@osdl.org #endif 9556f3a2024SJames Bottomley 9561da177e4SLinus Torvalds /** 9571da177e4SLinus Torvalds * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it 9581da177e4SLinus Torvalds * @starget: pointer to target device structure 9591da177e4SLinus Torvalds * @lun: LUN of target device 960f64a181dSChristoph Hellwig * @sdevscan: probe the LUN corresponding to this scsi_device 961f64a181dSChristoph Hellwig * @sdevnew: store the value of any new scsi_device allocated 9621da177e4SLinus Torvalds * @bflagsp: store bflags here if not NULL 9631da177e4SLinus Torvalds * 9641da177e4SLinus Torvalds * Description: 9651da177e4SLinus Torvalds * Call scsi_probe_lun, if a LUN with an attached device is found, 9661da177e4SLinus Torvalds * allocate and set it up by calling scsi_add_lun. 9671da177e4SLinus Torvalds * 9681da177e4SLinus Torvalds * Return: 969f64a181dSChristoph Hellwig * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device 9701da177e4SLinus Torvalds * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is 9711da177e4SLinus Torvalds * attached at the LUN 972f64a181dSChristoph Hellwig * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized 9731da177e4SLinus Torvalds **/ 9741da177e4SLinus Torvalds static int scsi_probe_and_add_lun(struct scsi_target *starget, 9751da177e4SLinus Torvalds uint lun, int *bflagsp, 9761da177e4SLinus Torvalds struct scsi_device **sdevp, int rescan, 9771da177e4SLinus Torvalds void *hostdata) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds struct scsi_device *sdev; 9801da177e4SLinus Torvalds unsigned char *result; 98139216033SJames Bottomley int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256; 9821da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds /* 9851da177e4SLinus Torvalds * The rescan flag is used as an optimization, the first scan of a 9861da177e4SLinus Torvalds * host adapter calls into here with rescan == 0. 9871da177e4SLinus Torvalds */ 9881da177e4SLinus Torvalds sdev = scsi_device_lookup_by_target(starget, lun); 9891da177e4SLinus Torvalds if (sdev) { 9906f3a2024SJames Bottomley if (rescan || sdev->sdev_state != SDEV_CREATED) { 9911da177e4SLinus Torvalds SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO 9921da177e4SLinus Torvalds "scsi scan: device exists on %s\n", 9931da177e4SLinus Torvalds sdev->sdev_gendev.bus_id)); 9941da177e4SLinus Torvalds if (sdevp) 9951da177e4SLinus Torvalds *sdevp = sdev; 9961da177e4SLinus Torvalds else 9971da177e4SLinus Torvalds scsi_device_put(sdev); 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds if (bflagsp) 10001da177e4SLinus Torvalds *bflagsp = scsi_get_device_flags(sdev, 10011da177e4SLinus Torvalds sdev->vendor, 10021da177e4SLinus Torvalds sdev->model); 10031da177e4SLinus Torvalds return SCSI_SCAN_LUN_PRESENT; 10041da177e4SLinus Torvalds } 10056f3a2024SJames Bottomley scsi_device_put(sdev); 10066f3a2024SJames Bottomley } else 10071da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, lun, hostdata); 10081da177e4SLinus Torvalds if (!sdev) 10091da177e4SLinus Torvalds goto out; 101039216033SJames Bottomley 101139216033SJames Bottomley result = kmalloc(result_len, GFP_ATOMIC | 1012bc86120aSAl Viro ((shost->unchecked_isa_dma) ? __GFP_DMA : 0)); 10131da177e4SLinus Torvalds if (!result) 101439216033SJames Bottomley goto out_free_sdev; 10151da177e4SLinus Torvalds 101639216033SJames Bottomley if (scsi_probe_lun(sdev, result, result_len, &bflags)) 10171da177e4SLinus Torvalds goto out_free_result; 10181da177e4SLinus Torvalds 10194186ab19SKurt Garloff if (bflagsp) 10204186ab19SKurt Garloff *bflagsp = bflags; 10211da177e4SLinus Torvalds /* 10221da177e4SLinus Torvalds * result contains valid SCSI INQUIRY data. 10231da177e4SLinus Torvalds */ 102413f7e5acSKurt Garloff if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) { 10251da177e4SLinus Torvalds /* 10261da177e4SLinus Torvalds * For a Peripheral qualifier 3 (011b), the SCSI 10271da177e4SLinus Torvalds * spec says: The device server is not capable of 10281da177e4SLinus Torvalds * supporting a physical device on this logical 10291da177e4SLinus Torvalds * unit. 10301da177e4SLinus Torvalds * 10311da177e4SLinus Torvalds * For disks, this implies that there is no 10321da177e4SLinus Torvalds * logical disk configured at sdev->lun, but there 10331da177e4SLinus Torvalds * is a target id responding. 10341da177e4SLinus Torvalds */ 10356c7154c9SKurt Garloff SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:" 10366c7154c9SKurt Garloff " peripheral qualifier of 3, device not" 10376c7154c9SKurt Garloff " added\n")) 10386c7154c9SKurt Garloff if (lun == 0) { 1039c5f2e640Sakpm@osdl.org SCSI_LOG_SCAN_BUS(1, { 1040c5f2e640Sakpm@osdl.org unsigned char vend[9]; 1041c5f2e640Sakpm@osdl.org unsigned char mod[17]; 1042c5f2e640Sakpm@osdl.org 1043c5f2e640Sakpm@osdl.org sdev_printk(KERN_INFO, sdev, 10446c7154c9SKurt Garloff "scsi scan: consider passing scsi_mod." 10453424a65dSKurt Garloff "dev_flags=%s:%s:0x240 or 0x1000240\n", 10466c7154c9SKurt Garloff scsi_inq_str(vend, result, 8, 16), 1047c5f2e640Sakpm@osdl.org scsi_inq_str(mod, result, 16, 32)); 1048c5f2e640Sakpm@osdl.org }); 10496c7154c9SKurt Garloff } 10506c7154c9SKurt Garloff 10511da177e4SLinus Torvalds res = SCSI_SCAN_TARGET_PRESENT; 10521da177e4SLinus Torvalds goto out_free_result; 10531da177e4SLinus Torvalds } 10541da177e4SLinus Torvalds 10551bfc5d9dSAlan Stern /* 105684961f28Sdave wysochanski * Some targets may set slight variations of PQ and PDT to signal 105784961f28Sdave wysochanski * that no LUN is present, so don't add sdev in these cases. 105884961f28Sdave wysochanski * Two specific examples are: 105984961f28Sdave wysochanski * 1) NetApp targets: return PQ=1, PDT=0x1f 106084961f28Sdave wysochanski * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" 106184961f28Sdave wysochanski * in the UFI 1.0 spec (we cannot rely on reserved bits). 106284961f28Sdave wysochanski * 106384961f28Sdave wysochanski * References: 106484961f28Sdave wysochanski * 1) SCSI SPC-3, pp. 145-146 106584961f28Sdave wysochanski * PQ=1: "A peripheral device having the specified peripheral 106684961f28Sdave wysochanski * device type is not connected to this logical unit. However, the 106784961f28Sdave wysochanski * device server is capable of supporting the specified peripheral 106884961f28Sdave wysochanski * device type on this logical unit." 106984961f28Sdave wysochanski * PDT=0x1f: "Unknown or no device type" 107084961f28Sdave wysochanski * 2) USB UFI 1.0, p. 20 107184961f28Sdave wysochanski * PDT=00h Direct-access device (floppy) 107284961f28Sdave wysochanski * PDT=1Fh none (no FDD connected to the requested logical unit) 10731bfc5d9dSAlan Stern */ 107484961f28Sdave wysochanski if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) && 107584961f28Sdave wysochanski (result[0] & 0x1f) == 0x1f) { 10761bfc5d9dSAlan Stern SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO 10771bfc5d9dSAlan Stern "scsi scan: peripheral device type" 10781bfc5d9dSAlan Stern " of 31, no device added\n")); 10791bfc5d9dSAlan Stern res = SCSI_SCAN_TARGET_PRESENT; 10801bfc5d9dSAlan Stern goto out_free_result; 10811bfc5d9dSAlan Stern } 10821bfc5d9dSAlan Stern 10833e082a91SMatthew Wilcox res = scsi_add_lun(sdev, result, &bflags, shost->async_scan); 10841da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 10851da177e4SLinus Torvalds if (bflags & BLIST_KEY) { 10861da177e4SLinus Torvalds sdev->lockable = 0; 108739216033SJames Bottomley scsi_unlock_floptical(sdev, result); 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds } 10901da177e4SLinus Torvalds 10911da177e4SLinus Torvalds out_free_result: 10921da177e4SLinus Torvalds kfree(result); 10931da177e4SLinus Torvalds out_free_sdev: 10941da177e4SLinus Torvalds if (res == SCSI_SCAN_LUN_PRESENT) { 10951da177e4SLinus Torvalds if (sdevp) { 1096b70d37bfSAlan Stern if (scsi_device_get(sdev) == 0) { 10971da177e4SLinus Torvalds *sdevp = sdev; 1098b70d37bfSAlan Stern } else { 1099b70d37bfSAlan Stern __scsi_remove_device(sdev); 1100b70d37bfSAlan Stern res = SCSI_SCAN_NO_RESPONSE; 1101b70d37bfSAlan Stern } 11021da177e4SLinus Torvalds } 11036f3a2024SJames Bottomley } else 11046f3a2024SJames Bottomley scsi_destroy_sdev(sdev); 11051da177e4SLinus Torvalds out: 11061da177e4SLinus Torvalds return res; 11071da177e4SLinus Torvalds } 11081da177e4SLinus Torvalds 11091da177e4SLinus Torvalds /** 11101da177e4SLinus Torvalds * scsi_sequential_lun_scan - sequentially scan a SCSI target 11111da177e4SLinus Torvalds * @starget: pointer to target structure to scan 11121da177e4SLinus Torvalds * @bflags: black/white list flag for LUN 0 11131da177e4SLinus Torvalds * 11141da177e4SLinus Torvalds * Description: 11151da177e4SLinus Torvalds * Generally, scan from LUN 1 (LUN 0 is assumed to already have been 11161da177e4SLinus Torvalds * scanned) to some maximum lun until a LUN is found with no device 11171da177e4SLinus Torvalds * attached. Use the bflags to figure out any oddities. 11181da177e4SLinus Torvalds * 11191da177e4SLinus Torvalds * Modifies sdevscan->lun. 11201da177e4SLinus Torvalds **/ 11211da177e4SLinus Torvalds static void scsi_sequential_lun_scan(struct scsi_target *starget, 11224186ab19SKurt Garloff int bflags, int scsi_level, int rescan) 11231da177e4SLinus Torvalds { 11241da177e4SLinus Torvalds unsigned int sparse_lun, lun, max_dev_lun; 11251da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of" 11281da177e4SLinus Torvalds "%s\n", starget->dev.bus_id)); 11291da177e4SLinus Torvalds 11301da177e4SLinus Torvalds max_dev_lun = min(max_scsi_luns, shost->max_lun); 11311da177e4SLinus Torvalds /* 11321da177e4SLinus Torvalds * If this device is known to support sparse multiple units, 11331da177e4SLinus Torvalds * override the other settings, and scan all of them. Normally, 11341da177e4SLinus Torvalds * SCSI-3 devices should be scanned via the REPORT LUNS. 11351da177e4SLinus Torvalds */ 11361da177e4SLinus Torvalds if (bflags & BLIST_SPARSELUN) { 11371da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 11381da177e4SLinus Torvalds sparse_lun = 1; 11391da177e4SLinus Torvalds } else 11401da177e4SLinus Torvalds sparse_lun = 0; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds /* 11431da177e4SLinus Torvalds * If less than SCSI_1_CSS, and no special lun scaning, stop 11441da177e4SLinus Torvalds * scanning; this matches 2.4 behaviour, but could just be a bug 11451da177e4SLinus Torvalds * (to continue scanning a SCSI_1_CSS device). 11461da177e4SLinus Torvalds * 11471da177e4SLinus Torvalds * This test is broken. We might not have any device on lun0 for 11481da177e4SLinus Torvalds * a sparselun device, and if that's the case then how would we 11491da177e4SLinus Torvalds * know the real scsi_level, eh? It might make sense to just not 11501da177e4SLinus Torvalds * scan any SCSI_1 device for non-0 luns, but that check would best 11511da177e4SLinus Torvalds * go into scsi_alloc_sdev() and just have it return null when asked 11521da177e4SLinus Torvalds * to alloc an sdev for lun > 0 on an already found SCSI_1 device. 11531da177e4SLinus Torvalds * 11541da177e4SLinus Torvalds if ((sdevscan->scsi_level < SCSI_1_CCS) && 11551da177e4SLinus Torvalds ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) 11561da177e4SLinus Torvalds == 0)) 11571da177e4SLinus Torvalds return; 11581da177e4SLinus Torvalds */ 11591da177e4SLinus Torvalds /* 11601da177e4SLinus Torvalds * If this device is known to support multiple units, override 11611da177e4SLinus Torvalds * the other settings, and scan all of them. 11621da177e4SLinus Torvalds */ 11631da177e4SLinus Torvalds if (bflags & BLIST_FORCELUN) 11641da177e4SLinus Torvalds max_dev_lun = shost->max_lun; 11651da177e4SLinus Torvalds /* 11661da177e4SLinus Torvalds * REGAL CDC-4X: avoid hang after LUN 4 11671da177e4SLinus Torvalds */ 11681da177e4SLinus Torvalds if (bflags & BLIST_MAX5LUN) 11691da177e4SLinus Torvalds max_dev_lun = min(5U, max_dev_lun); 11701da177e4SLinus Torvalds /* 11711da177e4SLinus Torvalds * Do not scan SCSI-2 or lower device past LUN 7, unless 11721da177e4SLinus Torvalds * BLIST_LARGELUN. 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) 11751da177e4SLinus Torvalds max_dev_lun = min(8U, max_dev_lun); 11761da177e4SLinus Torvalds 11771da177e4SLinus Torvalds /* 11781da177e4SLinus Torvalds * We have already scanned LUN 0, so start at LUN 1. Keep scanning 11791da177e4SLinus Torvalds * until we reach the max, or no LUN is found and we are not 11801da177e4SLinus Torvalds * sparse_lun. 11811da177e4SLinus Torvalds */ 11821da177e4SLinus Torvalds for (lun = 1; lun < max_dev_lun; ++lun) 11831da177e4SLinus Torvalds if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, 11841da177e4SLinus Torvalds NULL) != SCSI_SCAN_LUN_PRESENT) && 11851da177e4SLinus Torvalds !sparse_lun) 11861da177e4SLinus Torvalds return; 11871da177e4SLinus Torvalds } 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds /** 11901da177e4SLinus Torvalds * scsilun_to_int: convert a scsi_lun to an int 11911da177e4SLinus Torvalds * @scsilun: struct scsi_lun to be converted. 11921da177e4SLinus Torvalds * 11931da177e4SLinus Torvalds * Description: 11941da177e4SLinus Torvalds * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered 11951da177e4SLinus Torvalds * integer, and return the result. The caller must check for 11961da177e4SLinus Torvalds * truncation before using this function. 11971da177e4SLinus Torvalds * 11981da177e4SLinus Torvalds * Notes: 11991da177e4SLinus Torvalds * The struct scsi_lun is assumed to be four levels, with each level 12001da177e4SLinus Torvalds * effectively containing a SCSI byte-ordered (big endian) short; the 12011da177e4SLinus Torvalds * addressing bits of each level are ignored (the highest two bits). 12021da177e4SLinus Torvalds * For a description of the LUN format, post SCSI-3 see the SCSI 12031da177e4SLinus Torvalds * Architecture Model, for SCSI-3 see the SCSI Controller Commands. 12041da177e4SLinus Torvalds * 12051da177e4SLinus Torvalds * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns 12061da177e4SLinus Torvalds * the integer: 0x0b030a04 12071da177e4SLinus Torvalds **/ 12081da177e4SLinus Torvalds static int scsilun_to_int(struct scsi_lun *scsilun) 12091da177e4SLinus Torvalds { 12101da177e4SLinus Torvalds int i; 12111da177e4SLinus Torvalds unsigned int lun; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds lun = 0; 12141da177e4SLinus Torvalds for (i = 0; i < sizeof(lun); i += 2) 12151da177e4SLinus Torvalds lun = lun | (((scsilun->scsi_lun[i] << 8) | 12161da177e4SLinus Torvalds scsilun->scsi_lun[i + 1]) << (i * 8)); 12171da177e4SLinus Torvalds return lun; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds /** 12212f4701d8SJames.Smart@Emulex.Com * int_to_scsilun: reverts an int into a scsi_lun 12222f4701d8SJames.Smart@Emulex.Com * @int: integer to be reverted 12232f4701d8SJames.Smart@Emulex.Com * @scsilun: struct scsi_lun to be set. 12242f4701d8SJames.Smart@Emulex.Com * 12252f4701d8SJames.Smart@Emulex.Com * Description: 12262f4701d8SJames.Smart@Emulex.Com * Reverts the functionality of the scsilun_to_int, which packed 12272f4701d8SJames.Smart@Emulex.Com * an 8-byte lun value into an int. This routine unpacks the int 12282f4701d8SJames.Smart@Emulex.Com * back into the lun value. 12292f4701d8SJames.Smart@Emulex.Com * Note: the scsilun_to_int() routine does not truly handle all 12302f4701d8SJames.Smart@Emulex.Com * 8bytes of the lun value. This functions restores only as much 12312f4701d8SJames.Smart@Emulex.Com * as was set by the routine. 12322f4701d8SJames.Smart@Emulex.Com * 12332f4701d8SJames.Smart@Emulex.Com * Notes: 12342f4701d8SJames.Smart@Emulex.Com * Given an integer : 0x0b030a04, this function returns a 12352f4701d8SJames.Smart@Emulex.Com * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00 12362f4701d8SJames.Smart@Emulex.Com * 12372f4701d8SJames.Smart@Emulex.Com **/ 12382f4701d8SJames.Smart@Emulex.Com void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun) 12392f4701d8SJames.Smart@Emulex.Com { 12402f4701d8SJames.Smart@Emulex.Com int i; 12412f4701d8SJames.Smart@Emulex.Com 12422f4701d8SJames.Smart@Emulex.Com memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun)); 12432f4701d8SJames.Smart@Emulex.Com 12442f4701d8SJames.Smart@Emulex.Com for (i = 0; i < sizeof(lun); i += 2) { 12452f4701d8SJames.Smart@Emulex.Com scsilun->scsi_lun[i] = (lun >> 8) & 0xFF; 12462f4701d8SJames.Smart@Emulex.Com scsilun->scsi_lun[i+1] = lun & 0xFF; 12472f4701d8SJames.Smart@Emulex.Com lun = lun >> 16; 12482f4701d8SJames.Smart@Emulex.Com } 12492f4701d8SJames.Smart@Emulex.Com } 12502f4701d8SJames.Smart@Emulex.Com EXPORT_SYMBOL(int_to_scsilun); 12512f4701d8SJames.Smart@Emulex.Com 12522f4701d8SJames.Smart@Emulex.Com /** 12531da177e4SLinus Torvalds * scsi_report_lun_scan - Scan using SCSI REPORT LUN results 1254f64a181dSChristoph Hellwig * @sdevscan: scan the host, channel, and id of this scsi_device 12551da177e4SLinus Torvalds * 12561da177e4SLinus Torvalds * Description: 12571da177e4SLinus Torvalds * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN 12581da177e4SLinus Torvalds * command, and scan the resulting list of LUNs by calling 12591da177e4SLinus Torvalds * scsi_probe_and_add_lun. 12601da177e4SLinus Torvalds * 12611da177e4SLinus Torvalds * Modifies sdevscan->lun. 12621da177e4SLinus Torvalds * 12631da177e4SLinus Torvalds * Return: 12641da177e4SLinus Torvalds * 0: scan completed (or no memory, so further scanning is futile) 12651da177e4SLinus Torvalds * 1: no report lun scan, or not configured 12661da177e4SLinus Torvalds **/ 12676f3a2024SJames Bottomley static int scsi_report_lun_scan(struct scsi_target *starget, int bflags, 12681da177e4SLinus Torvalds int rescan) 12691da177e4SLinus Torvalds { 12701da177e4SLinus Torvalds char devname[64]; 12711da177e4SLinus Torvalds unsigned char scsi_cmd[MAX_COMMAND_SIZE]; 12721da177e4SLinus Torvalds unsigned int length; 12731da177e4SLinus Torvalds unsigned int lun; 12741da177e4SLinus Torvalds unsigned int num_luns; 12751da177e4SLinus Torvalds unsigned int retries; 127639216033SJames Bottomley int result; 12771da177e4SLinus Torvalds struct scsi_lun *lunp, *lun_data; 12781da177e4SLinus Torvalds u8 *data; 12791da177e4SLinus Torvalds struct scsi_sense_hdr sshdr; 12806f3a2024SJames Bottomley struct scsi_device *sdev; 12816f3a2024SJames Bottomley struct Scsi_Host *shost = dev_to_shost(&starget->dev); 12822ef89198SAlan Stern int ret = 0; 12831da177e4SLinus Torvalds 12841da177e4SLinus Torvalds /* 12851da177e4SLinus Torvalds * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set. 12861da177e4SLinus Torvalds * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does 12871da177e4SLinus Torvalds * support more than 8 LUNs. 12881da177e4SLinus Torvalds */ 12894d7db04aSJames Bottomley if (bflags & BLIST_NOREPORTLUN) 12904d7db04aSJames Bottomley return 1; 12914d7db04aSJames Bottomley if (starget->scsi_level < SCSI_2 && 12924d7db04aSJames Bottomley starget->scsi_level != SCSI_UNKNOWN) 12934d7db04aSJames Bottomley return 1; 12944d7db04aSJames Bottomley if (starget->scsi_level < SCSI_3 && 12954d7db04aSJames Bottomley (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) 12961da177e4SLinus Torvalds return 1; 12971da177e4SLinus Torvalds if (bflags & BLIST_NOLUN) 12981da177e4SLinus Torvalds return 0; 12991da177e4SLinus Torvalds 13006f3a2024SJames Bottomley if (!(sdev = scsi_device_lookup_by_target(starget, 0))) { 13016f3a2024SJames Bottomley sdev = scsi_alloc_sdev(starget, 0, NULL); 13026f3a2024SJames Bottomley if (!sdev) 13036f3a2024SJames Bottomley return 0; 13046f3a2024SJames Bottomley if (scsi_device_get(sdev)) 13056f3a2024SJames Bottomley return 0; 13066f3a2024SJames Bottomley } 13076f3a2024SJames Bottomley 13081da177e4SLinus Torvalds sprintf(devname, "host %d channel %d id %d", 13096f3a2024SJames Bottomley shost->host_no, sdev->channel, sdev->id); 13101da177e4SLinus Torvalds 13111da177e4SLinus Torvalds /* 13121da177e4SLinus Torvalds * Allocate enough to hold the header (the same size as one scsi_lun) 13131da177e4SLinus Torvalds * plus the max number of luns we are requesting. 13141da177e4SLinus Torvalds * 13151da177e4SLinus Torvalds * Reallocating and trying again (with the exact amount we need) 13161da177e4SLinus Torvalds * would be nice, but then we need to somehow limit the size 13171da177e4SLinus Torvalds * allocated based on the available memory and the limits of 13181da177e4SLinus Torvalds * kmalloc - we don't want a kmalloc() failure of a huge value to 13191da177e4SLinus Torvalds * prevent us from finding any LUNs on this target. 13201da177e4SLinus Torvalds */ 13211da177e4SLinus Torvalds length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun); 13221da177e4SLinus Torvalds lun_data = kmalloc(length, GFP_ATOMIC | 13231da177e4SLinus Torvalds (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0)); 13246f3a2024SJames Bottomley if (!lun_data) { 13256f3a2024SJames Bottomley printk(ALLOC_FAILURE_MSG, __FUNCTION__); 132639216033SJames Bottomley goto out; 13276f3a2024SJames Bottomley } 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds scsi_cmd[0] = REPORT_LUNS; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds /* 13321da177e4SLinus Torvalds * bytes 1 - 5: reserved, set to zero. 13331da177e4SLinus Torvalds */ 13341da177e4SLinus Torvalds memset(&scsi_cmd[1], 0, 5); 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /* 13371da177e4SLinus Torvalds * bytes 6 - 9: length of the command. 13381da177e4SLinus Torvalds */ 13391da177e4SLinus Torvalds scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff; 13401da177e4SLinus Torvalds scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff; 13411da177e4SLinus Torvalds scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff; 13421da177e4SLinus Torvalds scsi_cmd[9] = (unsigned char) length & 0xff; 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds scsi_cmd[10] = 0; /* reserved */ 13451da177e4SLinus Torvalds scsi_cmd[11] = 0; /* control */ 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds /* 13481da177e4SLinus Torvalds * We can get a UNIT ATTENTION, for example a power on/reset, so 13491da177e4SLinus Torvalds * retry a few times (like sd.c does for TEST UNIT READY). 13501da177e4SLinus Torvalds * Experience shows some combinations of adapter/devices get at 13511da177e4SLinus Torvalds * least two power on/resets. 13521da177e4SLinus Torvalds * 13531da177e4SLinus Torvalds * Illegal requests (for devices that do not support REPORT LUNS) 13541da177e4SLinus Torvalds * should come through as a check condition, and will not generate 13551da177e4SLinus Torvalds * a retry. 13561da177e4SLinus Torvalds */ 13571da177e4SLinus Torvalds for (retries = 0; retries < 3; retries++) { 13581da177e4SLinus Torvalds SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending" 13591da177e4SLinus Torvalds " REPORT LUNS to %s (try %d)\n", devname, 13601da177e4SLinus Torvalds retries)); 136139216033SJames Bottomley 136239216033SJames Bottomley result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, 1363ea73a9f2SJames Bottomley lun_data, length, &sshdr, 13641da177e4SLinus Torvalds SCSI_TIMEOUT + 4 * HZ, 3); 136539216033SJames Bottomley 13661da177e4SLinus Torvalds SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS" 136739216033SJames Bottomley " %s (try %d) result 0x%x\n", result 136839216033SJames Bottomley ? "failed" : "successful", retries, result)); 136939216033SJames Bottomley if (result == 0) 13701da177e4SLinus Torvalds break; 1371ea73a9f2SJames Bottomley else if (scsi_sense_valid(&sshdr)) { 13721da177e4SLinus Torvalds if (sshdr.sense_key != UNIT_ATTENTION) 13731da177e4SLinus Torvalds break; 13741da177e4SLinus Torvalds } 13751da177e4SLinus Torvalds } 13761da177e4SLinus Torvalds 137739216033SJames Bottomley if (result) { 13781da177e4SLinus Torvalds /* 13791da177e4SLinus Torvalds * The device probably does not support a REPORT LUN command 13801da177e4SLinus Torvalds */ 13812ef89198SAlan Stern ret = 1; 13822ef89198SAlan Stern goto out_err; 13831da177e4SLinus Torvalds } 13841da177e4SLinus Torvalds 13851da177e4SLinus Torvalds /* 13861da177e4SLinus Torvalds * Get the length from the first four bytes of lun_data. 13871da177e4SLinus Torvalds */ 13881da177e4SLinus Torvalds data = (u8 *) lun_data->scsi_lun; 13891da177e4SLinus Torvalds length = ((data[0] << 24) | (data[1] << 16) | 13901da177e4SLinus Torvalds (data[2] << 8) | (data[3] << 0)); 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds num_luns = (length / sizeof(struct scsi_lun)); 13931da177e4SLinus Torvalds if (num_luns > max_scsi_report_luns) { 13941da177e4SLinus Torvalds printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)" 13951da177e4SLinus Torvalds " of %d luns reported, try increasing" 13961da177e4SLinus Torvalds " max_scsi_report_luns.\n", devname, 13971da177e4SLinus Torvalds max_scsi_report_luns, num_luns); 13981da177e4SLinus Torvalds num_luns = max_scsi_report_luns; 13991da177e4SLinus Torvalds } 14001da177e4SLinus Torvalds 14013bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, 14023bf743e7SJeff Garzik "scsi scan: REPORT LUN scan\n")); 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds /* 14051da177e4SLinus Torvalds * Scan the luns in lun_data. The entry at offset 0 is really 14061da177e4SLinus Torvalds * the header, so start at 1 and go up to and including num_luns. 14071da177e4SLinus Torvalds */ 14081da177e4SLinus Torvalds for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) { 14091da177e4SLinus Torvalds lun = scsilun_to_int(lunp); 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds /* 14121da177e4SLinus Torvalds * Check if the unused part of lunp is non-zero, and so 14131da177e4SLinus Torvalds * does not fit in lun. 14141da177e4SLinus Torvalds */ 14151da177e4SLinus Torvalds if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) { 14161da177e4SLinus Torvalds int i; 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds /* 14191da177e4SLinus Torvalds * Output an error displaying the LUN in byte order, 14201da177e4SLinus Torvalds * this differs from what linux would print for the 14211da177e4SLinus Torvalds * integer LUN value. 14221da177e4SLinus Torvalds */ 14231da177e4SLinus Torvalds printk(KERN_WARNING "scsi: %s lun 0x", devname); 14241da177e4SLinus Torvalds data = (char *)lunp->scsi_lun; 14251da177e4SLinus Torvalds for (i = 0; i < sizeof(struct scsi_lun); i++) 14261da177e4SLinus Torvalds printk("%02x", data[i]); 14271da177e4SLinus Torvalds printk(" has a LUN larger than currently supported.\n"); 14281da177e4SLinus Torvalds } else if (lun > sdev->host->max_lun) { 14291da177e4SLinus Torvalds printk(KERN_WARNING "scsi: %s lun%d has a LUN larger" 14301da177e4SLinus Torvalds " than allowed by the host adapter\n", 14311da177e4SLinus Torvalds devname, lun); 14321da177e4SLinus Torvalds } else { 14331da177e4SLinus Torvalds int res; 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds res = scsi_probe_and_add_lun(starget, 14361da177e4SLinus Torvalds lun, NULL, NULL, rescan, NULL); 14371da177e4SLinus Torvalds if (res == SCSI_SCAN_NO_RESPONSE) { 14381da177e4SLinus Torvalds /* 14391da177e4SLinus Torvalds * Got some results, but now none, abort. 14401da177e4SLinus Torvalds */ 14413bf743e7SJeff Garzik sdev_printk(KERN_ERR, sdev, 14423bf743e7SJeff Garzik "Unexpected response" 14433bf743e7SJeff Garzik " from lun %d while scanning, scan" 14443bf743e7SJeff Garzik " aborted\n", lun); 14451da177e4SLinus Torvalds break; 14461da177e4SLinus Torvalds } 14471da177e4SLinus Torvalds } 14481da177e4SLinus Torvalds } 14491da177e4SLinus Torvalds 14502ef89198SAlan Stern out_err: 14511da177e4SLinus Torvalds kfree(lun_data); 14521da177e4SLinus Torvalds out: 14536f3a2024SJames Bottomley scsi_device_put(sdev); 14546f3a2024SJames Bottomley if (sdev->sdev_state == SDEV_CREATED) 14551da177e4SLinus Torvalds /* 14566f3a2024SJames Bottomley * the sdev we used didn't appear in the report luns scan 14571da177e4SLinus Torvalds */ 14586f3a2024SJames Bottomley scsi_destroy_sdev(sdev); 14592ef89198SAlan Stern return ret; 14601da177e4SLinus Torvalds } 14611da177e4SLinus Torvalds 14621da177e4SLinus Torvalds struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel, 14631da177e4SLinus Torvalds uint id, uint lun, void *hostdata) 14641da177e4SLinus Torvalds { 1465a97a83a0SMatthew Wilcox struct scsi_device *sdev = ERR_PTR(-ENODEV); 14661da177e4SLinus Torvalds struct device *parent = &shost->shost_gendev; 1467e02f3f59SChristoph Hellwig struct scsi_target *starget; 14681da177e4SLinus Torvalds 1469938e2ac0SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 1470938e2ac0SMatthew Wilcox return ERR_PTR(-ENODEV); 1471938e2ac0SMatthew Wilcox 1472938e2ac0SMatthew Wilcox if (!shost->async_scan) 1473938e2ac0SMatthew Wilcox scsi_complete_async_scans(); 1474938e2ac0SMatthew Wilcox 1475e02f3f59SChristoph Hellwig starget = scsi_alloc_target(parent, channel, id); 14761da177e4SLinus Torvalds if (!starget) 14771da177e4SLinus Torvalds return ERR_PTR(-ENOMEM); 14781da177e4SLinus Torvalds 14790b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 1480a97a83a0SMatthew Wilcox if (scsi_host_scan_allowed(shost)) 1481a97a83a0SMatthew Wilcox scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata); 14820b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 14831da177e4SLinus Torvalds scsi_target_reap(starget); 14841da177e4SLinus Torvalds put_device(&starget->dev); 14851da177e4SLinus Torvalds 14861da177e4SLinus Torvalds return sdev; 14871da177e4SLinus Torvalds } 14881da177e4SLinus Torvalds EXPORT_SYMBOL(__scsi_add_device); 14891da177e4SLinus Torvalds 1490146f7262SJames Bottomley int scsi_add_device(struct Scsi_Host *host, uint channel, 1491146f7262SJames Bottomley uint target, uint lun) 1492146f7262SJames Bottomley { 1493146f7262SJames Bottomley struct scsi_device *sdev = 1494146f7262SJames Bottomley __scsi_add_device(host, channel, target, lun, NULL); 1495146f7262SJames Bottomley if (IS_ERR(sdev)) 1496146f7262SJames Bottomley return PTR_ERR(sdev); 1497146f7262SJames Bottomley 1498146f7262SJames Bottomley scsi_device_put(sdev); 1499146f7262SJames Bottomley return 0; 1500146f7262SJames Bottomley } 1501146f7262SJames Bottomley EXPORT_SYMBOL(scsi_add_device); 1502146f7262SJames Bottomley 15031da177e4SLinus Torvalds void scsi_rescan_device(struct device *dev) 15041da177e4SLinus Torvalds { 15051da177e4SLinus Torvalds struct scsi_driver *drv; 15061da177e4SLinus Torvalds 15071da177e4SLinus Torvalds if (!dev->driver) 15081da177e4SLinus Torvalds return; 15091da177e4SLinus Torvalds 15101da177e4SLinus Torvalds drv = to_scsi_driver(dev->driver); 15111da177e4SLinus Torvalds if (try_module_get(drv->owner)) { 15121da177e4SLinus Torvalds if (drv->rescan) 15131da177e4SLinus Torvalds drv->rescan(dev); 15141da177e4SLinus Torvalds module_put(drv->owner); 15151da177e4SLinus Torvalds } 15161da177e4SLinus Torvalds } 15171da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_rescan_device); 15181da177e4SLinus Torvalds 1519e517d313SAlan Stern static void __scsi_scan_target(struct device *parent, unsigned int channel, 15201da177e4SLinus Torvalds unsigned int id, unsigned int lun, int rescan) 15211da177e4SLinus Torvalds { 15221da177e4SLinus Torvalds struct Scsi_Host *shost = dev_to_shost(parent); 15231da177e4SLinus Torvalds int bflags = 0; 15241da177e4SLinus Torvalds int res; 15251da177e4SLinus Torvalds struct scsi_target *starget; 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds if (shost->this_id == id) 15281da177e4SLinus Torvalds /* 15291da177e4SLinus Torvalds * Don't scan the host adapter 15301da177e4SLinus Torvalds */ 15311da177e4SLinus Torvalds return; 15321da177e4SLinus Torvalds 15331da177e4SLinus Torvalds starget = scsi_alloc_target(parent, channel, id); 15341da177e4SLinus Torvalds if (!starget) 15351da177e4SLinus Torvalds return; 15361da177e4SLinus Torvalds 15371da177e4SLinus Torvalds if (lun != SCAN_WILD_CARD) { 15381da177e4SLinus Torvalds /* 15391da177e4SLinus Torvalds * Scan for a specific host/chan/id/lun. 15401da177e4SLinus Torvalds */ 15411da177e4SLinus Torvalds scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL); 15421da177e4SLinus Torvalds goto out_reap; 15431da177e4SLinus Torvalds } 15441da177e4SLinus Torvalds 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * Scan LUN 0, if there is some response, scan further. Ideally, we 15471da177e4SLinus Torvalds * would not configure LUN 0 until all LUNs are scanned. 15481da177e4SLinus Torvalds */ 15496f3a2024SJames Bottomley res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL); 15506f3a2024SJames Bottomley if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) { 15516f3a2024SJames Bottomley if (scsi_report_lun_scan(starget, bflags, rescan) != 0) 15521da177e4SLinus Torvalds /* 15531da177e4SLinus Torvalds * The REPORT LUN did not scan the target, 15541da177e4SLinus Torvalds * do a sequential scan. 15551da177e4SLinus Torvalds */ 15561da177e4SLinus Torvalds scsi_sequential_lun_scan(starget, bflags, 15574186ab19SKurt Garloff starget->scsi_level, rescan); 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds 15601da177e4SLinus Torvalds out_reap: 15611da177e4SLinus Torvalds /* now determine if the target has any children at all 15621da177e4SLinus Torvalds * and if not, nuke it */ 15631da177e4SLinus Torvalds scsi_target_reap(starget); 15641da177e4SLinus Torvalds 15651da177e4SLinus Torvalds put_device(&starget->dev); 15661da177e4SLinus Torvalds } 1567e517d313SAlan Stern 1568e517d313SAlan Stern /** 1569e517d313SAlan Stern * scsi_scan_target - scan a target id, possibly including all LUNs on the 1570e517d313SAlan Stern * target. 1571e517d313SAlan Stern * @parent: host to scan 1572e517d313SAlan Stern * @channel: channel to scan 1573e517d313SAlan Stern * @id: target id to scan 1574e517d313SAlan Stern * @lun: Specific LUN to scan or SCAN_WILD_CARD 1575e517d313SAlan Stern * @rescan: passed to LUN scanning routines 1576e517d313SAlan Stern * 1577e517d313SAlan Stern * Description: 1578e517d313SAlan Stern * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0, 1579e517d313SAlan Stern * and possibly all LUNs on the target id. 1580e517d313SAlan Stern * 1581e517d313SAlan Stern * First try a REPORT LUN scan, if that does not scan the target, do a 1582e517d313SAlan Stern * sequential scan of LUNs on the target id. 1583e517d313SAlan Stern **/ 1584e517d313SAlan Stern void scsi_scan_target(struct device *parent, unsigned int channel, 1585e517d313SAlan Stern unsigned int id, unsigned int lun, int rescan) 1586e517d313SAlan Stern { 1587e517d313SAlan Stern struct Scsi_Host *shost = dev_to_shost(parent); 1588e517d313SAlan Stern 158993b45af5SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 159093b45af5SMatthew Wilcox return; 159193b45af5SMatthew Wilcox 15923e082a91SMatthew Wilcox if (!shost->async_scan) 15933e082a91SMatthew Wilcox scsi_complete_async_scans(); 15943e082a91SMatthew Wilcox 15950b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 1596e517d313SAlan Stern if (scsi_host_scan_allowed(shost)) 1597e517d313SAlan Stern __scsi_scan_target(parent, channel, id, lun, rescan); 15980b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 1599e517d313SAlan Stern } 16001da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_target); 16011da177e4SLinus Torvalds 16021da177e4SLinus Torvalds static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel, 16031da177e4SLinus Torvalds unsigned int id, unsigned int lun, int rescan) 16041da177e4SLinus Torvalds { 16051da177e4SLinus Torvalds uint order_id; 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds if (id == SCAN_WILD_CARD) 16081da177e4SLinus Torvalds for (id = 0; id < shost->max_id; ++id) { 16091da177e4SLinus Torvalds /* 16101da177e4SLinus Torvalds * XXX adapter drivers when possible (FCP, iSCSI) 16111da177e4SLinus Torvalds * could modify max_id to match the current max, 16121da177e4SLinus Torvalds * not the absolute max. 16131da177e4SLinus Torvalds * 16141da177e4SLinus Torvalds * XXX add a shost id iterator, so for example, 16151da177e4SLinus Torvalds * the FC ID can be the same as a target id 16161da177e4SLinus Torvalds * without a huge overhead of sparse id's. 16171da177e4SLinus Torvalds */ 16181da177e4SLinus Torvalds if (shost->reverse_ordering) 16191da177e4SLinus Torvalds /* 16201da177e4SLinus Torvalds * Scan from high to low id. 16211da177e4SLinus Torvalds */ 16221da177e4SLinus Torvalds order_id = shost->max_id - id - 1; 16231da177e4SLinus Torvalds else 16241da177e4SLinus Torvalds order_id = id; 1625e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1626e517d313SAlan Stern order_id, lun, rescan); 16271da177e4SLinus Torvalds } 16281da177e4SLinus Torvalds else 1629e517d313SAlan Stern __scsi_scan_target(&shost->shost_gendev, channel, 1630e517d313SAlan Stern id, lun, rescan); 16311da177e4SLinus Torvalds } 16321da177e4SLinus Torvalds 16331da177e4SLinus Torvalds int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel, 16341da177e4SLinus Torvalds unsigned int id, unsigned int lun, int rescan) 16351da177e4SLinus Torvalds { 16363bf743e7SJeff Garzik SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost, 16373bf743e7SJeff Garzik "%s: <%u:%u:%u>\n", 16383bf743e7SJeff Garzik __FUNCTION__, channel, id, lun)); 16391da177e4SLinus Torvalds 16403e082a91SMatthew Wilcox if (!shost->async_scan) 16413e082a91SMatthew Wilcox scsi_complete_async_scans(); 16423e082a91SMatthew Wilcox 16431da177e4SLinus Torvalds if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || 1644091686d3SAmit Arora ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || 16451da177e4SLinus Torvalds ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun))) 16461da177e4SLinus Torvalds return -EINVAL; 16471da177e4SLinus Torvalds 16480b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 164982f29467SMike Anderson if (scsi_host_scan_allowed(shost)) { 16501da177e4SLinus Torvalds if (channel == SCAN_WILD_CARD) 165182f29467SMike Anderson for (channel = 0; channel <= shost->max_channel; 165282f29467SMike Anderson channel++) 165382f29467SMike Anderson scsi_scan_channel(shost, channel, id, lun, 165482f29467SMike Anderson rescan); 16551da177e4SLinus Torvalds else 16561da177e4SLinus Torvalds scsi_scan_channel(shost, channel, id, lun, rescan); 165782f29467SMike Anderson } 16580b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 16591da177e4SLinus Torvalds 16601da177e4SLinus Torvalds return 0; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 16633e082a91SMatthew Wilcox static void scsi_sysfs_add_devices(struct Scsi_Host *shost) 16643e082a91SMatthew Wilcox { 16653e082a91SMatthew Wilcox struct scsi_device *sdev; 16663e082a91SMatthew Wilcox shost_for_each_device(sdev, shost) { 16673e082a91SMatthew Wilcox if (scsi_sysfs_add_sdev(sdev) != 0) 16683e082a91SMatthew Wilcox scsi_destroy_sdev(sdev); 16693e082a91SMatthew Wilcox } 16703e082a91SMatthew Wilcox } 16713e082a91SMatthew Wilcox 16723e082a91SMatthew Wilcox /** 16733e082a91SMatthew Wilcox * scsi_prep_async_scan - prepare for an async scan 16743e082a91SMatthew Wilcox * @shost: the host which will be scanned 16753e082a91SMatthew Wilcox * Returns: a cookie to be passed to scsi_finish_async_scan() 16763e082a91SMatthew Wilcox * 16773e082a91SMatthew Wilcox * Tells the midlayer this host is going to do an asynchronous scan. 16783e082a91SMatthew Wilcox * It reserves the host's position in the scanning list and ensures 16793e082a91SMatthew Wilcox * that other asynchronous scans started after this one won't affect the 16803e082a91SMatthew Wilcox * ordering of the discovered devices. 16813e082a91SMatthew Wilcox */ 16821aa8fab2SMatthew Wilcox static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) 16833e082a91SMatthew Wilcox { 16843e082a91SMatthew Wilcox struct async_scan_data *data; 16853e082a91SMatthew Wilcox 16863e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "sync", 4) == 0) 16873e082a91SMatthew Wilcox return NULL; 16883e082a91SMatthew Wilcox 16893e082a91SMatthew Wilcox if (shost->async_scan) { 16903e082a91SMatthew Wilcox printk("%s called twice for host %d", __FUNCTION__, 16913e082a91SMatthew Wilcox shost->host_no); 16923e082a91SMatthew Wilcox dump_stack(); 16933e082a91SMatthew Wilcox return NULL; 16943e082a91SMatthew Wilcox } 16953e082a91SMatthew Wilcox 16963e082a91SMatthew Wilcox data = kmalloc(sizeof(*data), GFP_KERNEL); 16973e082a91SMatthew Wilcox if (!data) 16983e082a91SMatthew Wilcox goto err; 16993e082a91SMatthew Wilcox data->shost = scsi_host_get(shost); 17003e082a91SMatthew Wilcox if (!data->shost) 17013e082a91SMatthew Wilcox goto err; 17023e082a91SMatthew Wilcox init_completion(&data->prev_finished); 17033e082a91SMatthew Wilcox 17043e082a91SMatthew Wilcox spin_lock(&async_scan_lock); 17053e082a91SMatthew Wilcox shost->async_scan = 1; 17063e082a91SMatthew Wilcox if (list_empty(&scanning_hosts)) 17073e082a91SMatthew Wilcox complete(&data->prev_finished); 17083e082a91SMatthew Wilcox list_add_tail(&data->list, &scanning_hosts); 17093e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 17103e082a91SMatthew Wilcox 17113e082a91SMatthew Wilcox return data; 17123e082a91SMatthew Wilcox 17133e082a91SMatthew Wilcox err: 17143e082a91SMatthew Wilcox kfree(data); 17153e082a91SMatthew Wilcox return NULL; 17163e082a91SMatthew Wilcox } 17173e082a91SMatthew Wilcox 17183e082a91SMatthew Wilcox /** 17193e082a91SMatthew Wilcox * scsi_finish_async_scan - asynchronous scan has finished 17203e082a91SMatthew Wilcox * @data: cookie returned from earlier call to scsi_prep_async_scan() 17213e082a91SMatthew Wilcox * 17223e082a91SMatthew Wilcox * All the devices currently attached to this host have been found. 17233e082a91SMatthew Wilcox * This function announces all the devices it has found to the rest 17243e082a91SMatthew Wilcox * of the system. 17253e082a91SMatthew Wilcox */ 17261aa8fab2SMatthew Wilcox static void scsi_finish_async_scan(struct async_scan_data *data) 17273e082a91SMatthew Wilcox { 17283e082a91SMatthew Wilcox struct Scsi_Host *shost; 17293e082a91SMatthew Wilcox 17303e082a91SMatthew Wilcox if (!data) 17313e082a91SMatthew Wilcox return; 17323e082a91SMatthew Wilcox 17333e082a91SMatthew Wilcox shost = data->shost; 17343e082a91SMatthew Wilcox if (!shost->async_scan) { 17353e082a91SMatthew Wilcox printk("%s called twice for host %d", __FUNCTION__, 17363e082a91SMatthew Wilcox shost->host_no); 17373e082a91SMatthew Wilcox dump_stack(); 17383e082a91SMatthew Wilcox return; 17393e082a91SMatthew Wilcox } 17403e082a91SMatthew Wilcox 17413e082a91SMatthew Wilcox wait_for_completion(&data->prev_finished); 17423e082a91SMatthew Wilcox 17433e082a91SMatthew Wilcox scsi_sysfs_add_devices(shost); 17443e082a91SMatthew Wilcox 17453e082a91SMatthew Wilcox spin_lock(&async_scan_lock); 17463e082a91SMatthew Wilcox shost->async_scan = 0; 17473e082a91SMatthew Wilcox list_del(&data->list); 17483e082a91SMatthew Wilcox if (!list_empty(&scanning_hosts)) { 17493e082a91SMatthew Wilcox struct async_scan_data *next = list_entry(scanning_hosts.next, 17503e082a91SMatthew Wilcox struct async_scan_data, list); 17513e082a91SMatthew Wilcox complete(&next->prev_finished); 17523e082a91SMatthew Wilcox } 17533e082a91SMatthew Wilcox spin_unlock(&async_scan_lock); 17543e082a91SMatthew Wilcox 17553e082a91SMatthew Wilcox scsi_host_put(shost); 17563e082a91SMatthew Wilcox kfree(data); 17573e082a91SMatthew Wilcox } 17583e082a91SMatthew Wilcox 17591aa8fab2SMatthew Wilcox static void do_scsi_scan_host(struct Scsi_Host *shost) 17601aa8fab2SMatthew Wilcox { 17611aa8fab2SMatthew Wilcox if (shost->hostt->scan_finished) { 17621aa8fab2SMatthew Wilcox unsigned long start = jiffies; 17631aa8fab2SMatthew Wilcox if (shost->hostt->scan_start) 17641aa8fab2SMatthew Wilcox shost->hostt->scan_start(shost); 17651aa8fab2SMatthew Wilcox 17661aa8fab2SMatthew Wilcox while (!shost->hostt->scan_finished(shost, jiffies - start)) 17671aa8fab2SMatthew Wilcox msleep(10); 17681aa8fab2SMatthew Wilcox } else { 17691aa8fab2SMatthew Wilcox scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD, 17701aa8fab2SMatthew Wilcox SCAN_WILD_CARD, 0); 17711aa8fab2SMatthew Wilcox } 17721aa8fab2SMatthew Wilcox } 17731aa8fab2SMatthew Wilcox 17743e082a91SMatthew Wilcox static int do_scan_async(void *_data) 17753e082a91SMatthew Wilcox { 17763e082a91SMatthew Wilcox struct async_scan_data *data = _data; 17771aa8fab2SMatthew Wilcox do_scsi_scan_host(data->shost); 17783e082a91SMatthew Wilcox scsi_finish_async_scan(data); 17793e082a91SMatthew Wilcox return 0; 17803e082a91SMatthew Wilcox } 17813e082a91SMatthew Wilcox 17821da177e4SLinus Torvalds /** 17831da177e4SLinus Torvalds * scsi_scan_host - scan the given adapter 17841da177e4SLinus Torvalds * @shost: adapter to scan 17851da177e4SLinus Torvalds **/ 17861da177e4SLinus Torvalds void scsi_scan_host(struct Scsi_Host *shost) 17871da177e4SLinus Torvalds { 17883e082a91SMatthew Wilcox struct async_scan_data *data; 17893e082a91SMatthew Wilcox 17903e082a91SMatthew Wilcox if (strncmp(scsi_scan_type, "none", 4) == 0) 17913e082a91SMatthew Wilcox return; 17923e082a91SMatthew Wilcox 17933e082a91SMatthew Wilcox data = scsi_prep_async_scan(shost); 17943e082a91SMatthew Wilcox if (!data) { 17951aa8fab2SMatthew Wilcox do_scsi_scan_host(shost); 17963e082a91SMatthew Wilcox return; 17973e082a91SMatthew Wilcox } 17981aa8fab2SMatthew Wilcox 17993e082a91SMatthew Wilcox kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no); 18001da177e4SLinus Torvalds } 18011da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_scan_host); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds void scsi_forget_host(struct Scsi_Host *shost) 18041da177e4SLinus Torvalds { 1805a64358dbSAlan Stern struct scsi_device *sdev; 18061da177e4SLinus Torvalds unsigned long flags; 18071da177e4SLinus Torvalds 1808a64358dbSAlan Stern restart: 18091da177e4SLinus Torvalds spin_lock_irqsave(shost->host_lock, flags); 1810a64358dbSAlan Stern list_for_each_entry(sdev, &shost->__devices, siblings) { 1811a64358dbSAlan Stern if (sdev->sdev_state == SDEV_DEL) 1812a64358dbSAlan Stern continue; 18131da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 1814a64358dbSAlan Stern __scsi_remove_device(sdev); 1815a64358dbSAlan Stern goto restart; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds spin_unlock_irqrestore(shost->host_lock, flags); 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds /* 18211da177e4SLinus Torvalds * Function: scsi_get_host_dev() 18221da177e4SLinus Torvalds * 1823f64a181dSChristoph Hellwig * Purpose: Create a scsi_device that points to the host adapter itself. 18241da177e4SLinus Torvalds * 1825f64a181dSChristoph Hellwig * Arguments: SHpnt - Host that needs a scsi_device 18261da177e4SLinus Torvalds * 18271da177e4SLinus Torvalds * Lock status: None assumed. 18281da177e4SLinus Torvalds * 1829f64a181dSChristoph Hellwig * Returns: The scsi_device or NULL 18301da177e4SLinus Torvalds * 18311da177e4SLinus Torvalds * Notes: 1832f64a181dSChristoph Hellwig * Attach a single scsi_device to the Scsi_Host - this should 18331da177e4SLinus Torvalds * be made to look like a "pseudo-device" that points to the 18341da177e4SLinus Torvalds * HA itself. 18351da177e4SLinus Torvalds * 18361da177e4SLinus Torvalds * Note - this device is not accessible from any high-level 18371da177e4SLinus Torvalds * drivers (including generics), which is probably not 18381da177e4SLinus Torvalds * optimal. We can add hooks later to attach 18391da177e4SLinus Torvalds */ 18401da177e4SLinus Torvalds struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost) 18411da177e4SLinus Torvalds { 1842e517d313SAlan Stern struct scsi_device *sdev = NULL; 18431da177e4SLinus Torvalds struct scsi_target *starget; 18441da177e4SLinus Torvalds 18450b950672SArjan van de Ven mutex_lock(&shost->scan_mutex); 1846e517d313SAlan Stern if (!scsi_host_scan_allowed(shost)) 1847e517d313SAlan Stern goto out; 18481da177e4SLinus Torvalds starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id); 18491da177e4SLinus Torvalds if (!starget) 1850e517d313SAlan Stern goto out; 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds sdev = scsi_alloc_sdev(starget, 0, NULL); 18531da177e4SLinus Torvalds if (sdev) { 18541da177e4SLinus Torvalds sdev->sdev_gendev.parent = get_device(&starget->dev); 18551da177e4SLinus Torvalds sdev->borken = 0; 1856884d25ccSJames Bottomley } else 1857884d25ccSJames Bottomley scsi_target_reap(starget); 18581da177e4SLinus Torvalds put_device(&starget->dev); 1859e517d313SAlan Stern out: 18600b950672SArjan van de Ven mutex_unlock(&shost->scan_mutex); 18611da177e4SLinus Torvalds return sdev; 18621da177e4SLinus Torvalds } 18631da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_get_host_dev); 18641da177e4SLinus Torvalds 18651da177e4SLinus Torvalds /* 18661da177e4SLinus Torvalds * Function: scsi_free_host_dev() 18671da177e4SLinus Torvalds * 18681da177e4SLinus Torvalds * Purpose: Free a scsi_device that points to the host adapter itself. 18691da177e4SLinus Torvalds * 1870f64a181dSChristoph Hellwig * Arguments: SHpnt - Host that needs a scsi_device 18711da177e4SLinus Torvalds * 18721da177e4SLinus Torvalds * Lock status: None assumed. 18731da177e4SLinus Torvalds * 18741da177e4SLinus Torvalds * Returns: Nothing 18751da177e4SLinus Torvalds * 18761da177e4SLinus Torvalds * Notes: 18771da177e4SLinus Torvalds */ 18781da177e4SLinus Torvalds void scsi_free_host_dev(struct scsi_device *sdev) 18791da177e4SLinus Torvalds { 18801da177e4SLinus Torvalds BUG_ON(sdev->id != sdev->host->this_id); 18811da177e4SLinus Torvalds 18826f3a2024SJames Bottomley scsi_destroy_sdev(sdev); 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds EXPORT_SYMBOL(scsi_free_host_dev); 18851da177e4SLinus Torvalds 1886