Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * scsi_scan.c |
| 3 | * |
| 4 | * Copyright (C) 2000 Eric Youngdale, |
| 5 | * Copyright (C) 2002 Patrick Mansfield |
| 6 | * |
| 7 | * The general scanning/probing algorithm is as follows, exceptions are |
| 8 | * made to it depending on device specific flags, compilation options, and |
| 9 | * global variable (boot or module load time) settings. |
| 10 | * |
| 11 | * A specific LUN is scanned via an INQUIRY command; if the LUN has a |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 12 | * device attached, a scsi_device is allocated and setup for it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
| 14 | * For every id of every channel on the given host: |
| 15 | * |
| 16 | * Scan LUN 0; if the target responds to LUN 0 (even if there is no |
| 17 | * device or storage attached to LUN 0): |
| 18 | * |
| 19 | * If LUN 0 has a device attached, allocate and setup a |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 20 | * scsi_device for it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * |
| 22 | * If target is SCSI-3 or up, issue a REPORT LUN, and scan |
| 23 | * all of the LUNs returned by the REPORT LUN; else, |
| 24 | * sequentially scan LUNs up until some maximum is reached, |
| 25 | * or a LUN is seen that cannot have a device attached to it. |
| 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
| 29 | #include <linux/moduleparam.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/blkdev.h> |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 32 | #include <linux/delay.h> |
| 33 | #include <linux/kthread.h> |
| 34 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <scsi/scsi.h> |
Christoph Hellwig | beb4048 | 2006-06-10 18:01:03 +0200 | [diff] [blame] | 37 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <scsi/scsi_device.h> |
| 39 | #include <scsi/scsi_driver.h> |
| 40 | #include <scsi/scsi_devinfo.h> |
| 41 | #include <scsi/scsi_host.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <scsi/scsi_transport.h> |
| 43 | #include <scsi/scsi_eh.h> |
| 44 | |
| 45 | #include "scsi_priv.h" |
| 46 | #include "scsi_logging.h" |
| 47 | |
| 48 | #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \ |
| 49 | " SCSI scanning, some SCSI devices might not be configured\n" |
| 50 | |
| 51 | /* |
| 52 | * Default timeout |
| 53 | */ |
| 54 | #define SCSI_TIMEOUT (2*HZ) |
| 55 | |
| 56 | /* |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 57 | * Prefix values for the SCSI id's (stored in sysfs name field) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | */ |
| 59 | #define SCSI_UID_SER_NUM 'S' |
| 60 | #define SCSI_UID_UNKNOWN 'Z' |
| 61 | |
| 62 | /* |
| 63 | * Return values of some of the scanning functions. |
| 64 | * |
| 65 | * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this |
| 66 | * includes allocation or general failures preventing IO from being sent. |
| 67 | * |
| 68 | * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available |
| 69 | * on the given LUN. |
| 70 | * |
| 71 | * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a |
| 72 | * given LUN. |
| 73 | */ |
| 74 | #define SCSI_SCAN_NO_RESPONSE 0 |
| 75 | #define SCSI_SCAN_TARGET_PRESENT 1 |
| 76 | #define SCSI_SCAN_LUN_PRESENT 2 |
| 77 | |
Arjan van de Ven | 0ad7820 | 2005-11-28 16:22:25 +0100 | [diff] [blame] | 78 | static const char *scsi_null_device_strs = "nullnullnullnull"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | #define MAX_SCSI_LUNS 512 |
| 81 | |
| 82 | #ifdef CONFIG_SCSI_MULTI_LUN |
| 83 | static unsigned int max_scsi_luns = MAX_SCSI_LUNS; |
| 84 | #else |
| 85 | static unsigned int max_scsi_luns = 1; |
| 86 | #endif |
| 87 | |
Masatake YAMATO | 10f4b89 | 2007-09-19 22:59:16 +0900 | [diff] [blame] | 88 | module_param_named(max_luns, max_scsi_luns, uint, S_IRUGO|S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | MODULE_PARM_DESC(max_luns, |
| 90 | "last scsi LUN (should be between 1 and 2^32-1)"); |
| 91 | |
Matthew Wilcox | 21db188 | 2006-11-22 13:24:52 -0700 | [diff] [blame] | 92 | #ifdef CONFIG_SCSI_SCAN_ASYNC |
| 93 | #define SCSI_SCAN_TYPE_DEFAULT "async" |
| 94 | #else |
| 95 | #define SCSI_SCAN_TYPE_DEFAULT "sync" |
| 96 | #endif |
| 97 | |
| 98 | static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT; |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 99 | |
| 100 | module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO); |
| 101 | MODULE_PARM_DESC(scan, "sync, async or none"); |
| 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* |
| 104 | * max_scsi_report_luns: the maximum number of LUNS that will be |
| 105 | * returned from the REPORT LUNS command. 8 times this value must |
| 106 | * be allocated. In theory this could be up to an 8 byte value, but |
| 107 | * in practice, the maximum number of LUNs suppored by any device |
| 108 | * is about 16k. |
| 109 | */ |
| 110 | static unsigned int max_scsi_report_luns = 511; |
| 111 | |
Masatake YAMATO | 10f4b89 | 2007-09-19 22:59:16 +0900 | [diff] [blame] | 112 | module_param_named(max_report_luns, max_scsi_report_luns, uint, S_IRUGO|S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | MODULE_PARM_DESC(max_report_luns, |
| 114 | "REPORT LUNS maximum number of LUNS received (should be" |
| 115 | " between 1 and 16384)"); |
| 116 | |
| 117 | static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3; |
| 118 | |
Masatake YAMATO | 10f4b89 | 2007-09-19 22:59:16 +0900 | [diff] [blame] | 119 | module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | MODULE_PARM_DESC(inq_timeout, |
| 121 | "Timeout (in seconds) waiting for devices to answer INQUIRY." |
| 122 | " Default is 5. Some non-compliant devices need more."); |
| 123 | |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 124 | /* This lock protects only this list */ |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 125 | static DEFINE_SPINLOCK(async_scan_lock); |
| 126 | static LIST_HEAD(scanning_hosts); |
| 127 | |
| 128 | struct async_scan_data { |
| 129 | struct list_head list; |
| 130 | struct Scsi_Host *shost; |
| 131 | struct completion prev_finished; |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * scsi_complete_async_scans - Wait for asynchronous scans to complete |
| 136 | * |
Matthew Wilcox | 8bcc241 | 2006-12-07 19:29:27 -0700 | [diff] [blame] | 137 | * When this function returns, any host which started scanning before |
| 138 | * this function was called will have finished its scan. Hosts which |
| 139 | * started scanning after this function was called may or may not have |
| 140 | * finished. |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 141 | */ |
| 142 | int scsi_complete_async_scans(void) |
| 143 | { |
| 144 | struct async_scan_data *data; |
| 145 | |
| 146 | do { |
| 147 | if (list_empty(&scanning_hosts)) |
| 148 | return 0; |
| 149 | /* If we can't get memory immediately, that's OK. Just |
| 150 | * sleep a little. Even if we never get memory, the async |
| 151 | * scans will finish eventually. |
| 152 | */ |
| 153 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 154 | if (!data) |
| 155 | msleep(1); |
| 156 | } while (!data); |
| 157 | |
| 158 | data->shost = NULL; |
| 159 | init_completion(&data->prev_finished); |
| 160 | |
| 161 | spin_lock(&async_scan_lock); |
| 162 | /* Check that there's still somebody else on the list */ |
| 163 | if (list_empty(&scanning_hosts)) |
| 164 | goto done; |
| 165 | list_add_tail(&data->list, &scanning_hosts); |
| 166 | spin_unlock(&async_scan_lock); |
| 167 | |
| 168 | printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n"); |
| 169 | wait_for_completion(&data->prev_finished); |
| 170 | |
| 171 | spin_lock(&async_scan_lock); |
| 172 | list_del(&data->list); |
Matthew Wilcox | 8bcc241 | 2006-12-07 19:29:27 -0700 | [diff] [blame] | 173 | if (!list_empty(&scanning_hosts)) { |
| 174 | struct async_scan_data *next = list_entry(scanning_hosts.next, |
| 175 | struct async_scan_data, list); |
| 176 | complete(&next->prev_finished); |
| 177 | } |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 178 | done: |
| 179 | spin_unlock(&async_scan_lock); |
| 180 | |
| 181 | kfree(data); |
| 182 | return 0; |
| 183 | } |
| 184 | |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 185 | /* Only exported for the benefit of scsi_wait_scan */ |
| 186 | EXPORT_SYMBOL_GPL(scsi_complete_async_scans); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 187 | |
Hugh Dickins | f2f027c | 2007-05-23 14:41:42 -0700 | [diff] [blame] | 188 | #ifndef MODULE |
| 189 | /* |
| 190 | * For async scanning we need to wait for all the scans to complete before |
| 191 | * trying to mount the root fs. Otherwise non-modular drivers may not be ready |
| 192 | * yet. |
| 193 | */ |
| 194 | late_initcall(scsi_complete_async_scans); |
| 195 | #endif |
| 196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | /** |
| 198 | * scsi_unlock_floptical - unlock device via a special MODE SENSE command |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 199 | * @sdev: scsi device to send command to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * @result: area to store the result of the MODE SENSE |
| 201 | * |
| 202 | * Description: |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 203 | * Send a vendor specific MODE SENSE (not a MODE SELECT) command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * Called for BLIST_KEY devices. |
| 205 | **/ |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 206 | static void scsi_unlock_floptical(struct scsi_device *sdev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | unsigned char *result) |
| 208 | { |
| 209 | unsigned char scsi_cmd[MAX_COMMAND_SIZE]; |
| 210 | |
| 211 | printk(KERN_NOTICE "scsi: unlocking floptical drive\n"); |
| 212 | scsi_cmd[0] = MODE_SENSE; |
| 213 | scsi_cmd[1] = 0; |
| 214 | scsi_cmd[2] = 0x2e; |
| 215 | scsi_cmd[3] = 0; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 216 | scsi_cmd[4] = 0x2a; /* size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | scsi_cmd[5] = 0; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 218 | scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL, |
| 219 | SCSI_TIMEOUT, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | * scsi_alloc_sdev - allocate and setup a scsi_Device |
| 224 | * |
| 225 | * Description: |
| 226 | * Allocate, initialize for io, and return a pointer to a scsi_Device. |
| 227 | * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and |
| 228 | * adds scsi_Device to the appropriate list. |
| 229 | * |
| 230 | * Return value: |
| 231 | * scsi_Device pointer, or NULL on failure. |
| 232 | **/ |
| 233 | static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget, |
| 234 | unsigned int lun, void *hostdata) |
| 235 | { |
| 236 | struct scsi_device *sdev; |
| 237 | int display_failure_msg = 1, ret; |
| 238 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 239 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 240 | sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | GFP_ATOMIC); |
| 242 | if (!sdev) |
| 243 | goto out; |
| 244 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | sdev->vendor = scsi_null_device_strs; |
| 246 | sdev->model = scsi_null_device_strs; |
| 247 | sdev->rev = scsi_null_device_strs; |
| 248 | sdev->host = shost; |
| 249 | sdev->id = starget->id; |
| 250 | sdev->lun = lun; |
| 251 | sdev->channel = starget->channel; |
| 252 | sdev->sdev_state = SDEV_CREATED; |
| 253 | INIT_LIST_HEAD(&sdev->siblings); |
| 254 | INIT_LIST_HEAD(&sdev->same_target_siblings); |
| 255 | INIT_LIST_HEAD(&sdev->cmd_list); |
| 256 | INIT_LIST_HEAD(&sdev->starved_entry); |
| 257 | spin_lock_init(&sdev->list_lock); |
| 258 | |
| 259 | sdev->sdev_gendev.parent = get_device(&starget->dev); |
| 260 | sdev->sdev_target = starget; |
| 261 | |
| 262 | /* usually NULL and set by ->slave_alloc instead */ |
| 263 | sdev->hostdata = hostdata; |
| 264 | |
| 265 | /* if the device needs this changing, it may do so in the |
| 266 | * slave_configure function */ |
| 267 | sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED; |
| 268 | |
| 269 | /* |
| 270 | * Some low level driver could use device->type |
| 271 | */ |
| 272 | sdev->type = -1; |
| 273 | |
| 274 | /* |
| 275 | * Assume that the device will have handshaking problems, |
| 276 | * and then fix this field later if it turns out it |
| 277 | * doesn't |
| 278 | */ |
| 279 | sdev->borken = 1; |
| 280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | sdev->request_queue = scsi_alloc_queue(sdev); |
| 282 | if (!sdev->request_queue) { |
| 283 | /* release fn is set up in scsi_sysfs_device_initialise, so |
| 284 | * have to free and put manually here */ |
| 285 | put_device(&starget->dev); |
Dave Jones | 93f56089 | 2006-03-09 10:21:27 -0500 | [diff] [blame] | 286 | kfree(sdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | goto out; |
| 288 | } |
| 289 | |
| 290 | sdev->request_queue->queuedata = sdev; |
| 291 | scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun); |
| 292 | |
| 293 | scsi_sysfs_device_initialize(sdev); |
| 294 | |
| 295 | if (shost->hostt->slave_alloc) { |
| 296 | ret = shost->hostt->slave_alloc(sdev); |
| 297 | if (ret) { |
| 298 | /* |
| 299 | * if LLDD reports slave not present, don't clutter |
| 300 | * console with alloc failure messages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | */ |
| 302 | if (ret == -ENXIO) |
| 303 | display_failure_msg = 0; |
| 304 | goto out_device_destroy; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return sdev; |
| 309 | |
| 310 | out_device_destroy: |
| 311 | transport_destroy_device(&sdev->sdev_gendev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | put_device(&sdev->sdev_gendev); |
| 313 | out: |
| 314 | if (display_failure_msg) |
| 315 | printk(ALLOC_FAILURE_MSG, __FUNCTION__); |
| 316 | return NULL; |
| 317 | } |
| 318 | |
| 319 | static void scsi_target_dev_release(struct device *dev) |
| 320 | { |
| 321 | struct device *parent = dev->parent; |
| 322 | struct scsi_target *starget = to_scsi_target(dev); |
James Bottomley | a283bd3 | 2005-05-24 12:06:38 -0500 | [diff] [blame] | 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | kfree(starget); |
| 325 | put_device(parent); |
| 326 | } |
| 327 | |
| 328 | int scsi_is_target_device(const struct device *dev) |
| 329 | { |
| 330 | return dev->release == scsi_target_dev_release; |
| 331 | } |
| 332 | EXPORT_SYMBOL(scsi_is_target_device); |
| 333 | |
| 334 | static struct scsi_target *__scsi_find_target(struct device *parent, |
| 335 | int channel, uint id) |
| 336 | { |
| 337 | struct scsi_target *starget, *found_starget = NULL; |
| 338 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 339 | /* |
| 340 | * Search for an existing target for this sdev. |
| 341 | */ |
| 342 | list_for_each_entry(starget, &shost->__targets, siblings) { |
| 343 | if (starget->id == id && |
| 344 | starget->channel == channel) { |
| 345 | found_starget = starget; |
| 346 | break; |
| 347 | } |
| 348 | } |
| 349 | if (found_starget) |
| 350 | get_device(&found_starget->dev); |
| 351 | |
| 352 | return found_starget; |
| 353 | } |
| 354 | |
James Bottomley | 884d25c | 2006-09-05 16:26:41 -0500 | [diff] [blame] | 355 | /** |
| 356 | * scsi_alloc_target - allocate a new or find an existing target |
| 357 | * @parent: parent of the target (need not be a scsi host) |
| 358 | * @channel: target channel number (zero if no channels) |
| 359 | * @id: target id number |
| 360 | * |
| 361 | * Return an existing target if one exists, provided it hasn't already |
| 362 | * gone into STARGET_DEL state, otherwise allocate a new target. |
| 363 | * |
| 364 | * The target is returned with an incremented reference, so the caller |
| 365 | * is responsible for both reaping and doing a last put |
| 366 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | static struct scsi_target *scsi_alloc_target(struct device *parent, |
| 368 | int channel, uint id) |
| 369 | { |
| 370 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 371 | struct device *dev = NULL; |
| 372 | unsigned long flags; |
| 373 | const int size = sizeof(struct scsi_target) |
| 374 | + shost->transportt->target_size; |
James.Smart@Emulex.Com | 5c44cd2 | 2005-06-10 22:24:30 -0400 | [diff] [blame] | 375 | struct scsi_target *starget; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | struct scsi_target *found_target; |
Brian King | 32f9579 | 2006-02-22 14:28:24 -0600 | [diff] [blame] | 377 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Jes Sorensen | 24669f75 | 2006-01-16 10:31:18 -0500 | [diff] [blame] | 379 | starget = kzalloc(size, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | if (!starget) { |
| 381 | printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__); |
| 382 | return NULL; |
| 383 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | dev = &starget->dev; |
| 385 | device_initialize(dev); |
| 386 | starget->reap_ref = 1; |
| 387 | dev->parent = get_device(parent); |
| 388 | dev->release = scsi_target_dev_release; |
| 389 | sprintf(dev->bus_id, "target%d:%d:%d", |
| 390 | shost->host_no, channel, id); |
| 391 | starget->id = id; |
| 392 | starget->channel = channel; |
| 393 | INIT_LIST_HEAD(&starget->siblings); |
| 394 | INIT_LIST_HEAD(&starget->devices); |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 395 | starget->state = STARGET_RUNNING; |
Alan Stern | 7c9d6f1 | 2007-01-08 11:12:32 -0500 | [diff] [blame] | 396 | starget->scsi_level = SCSI_2; |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 397 | retry: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | spin_lock_irqsave(shost->host_lock, flags); |
| 399 | |
| 400 | found_target = __scsi_find_target(parent, channel, id); |
| 401 | if (found_target) |
| 402 | goto found; |
| 403 | |
| 404 | list_add_tail(&starget->siblings, &shost->__targets); |
| 405 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 406 | /* allocate and add */ |
James Bottomley | a283bd3 | 2005-05-24 12:06:38 -0500 | [diff] [blame] | 407 | transport_setup_device(dev); |
Brian King | 32f9579 | 2006-02-22 14:28:24 -0600 | [diff] [blame] | 408 | error = device_add(dev); |
| 409 | if (error) { |
| 410 | dev_err(dev, "target device_add failed, error %d\n", error); |
| 411 | spin_lock_irqsave(shost->host_lock, flags); |
| 412 | list_del_init(&starget->siblings); |
| 413 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 414 | transport_destroy_device(dev); |
| 415 | put_device(parent); |
| 416 | kfree(starget); |
| 417 | return NULL; |
| 418 | } |
James Bottomley | a283bd3 | 2005-05-24 12:06:38 -0500 | [diff] [blame] | 419 | transport_add_device(dev); |
| 420 | if (shost->hostt->target_alloc) { |
Brian King | 32f9579 | 2006-02-22 14:28:24 -0600 | [diff] [blame] | 421 | error = shost->hostt->target_alloc(starget); |
James Bottomley | a283bd3 | 2005-05-24 12:06:38 -0500 | [diff] [blame] | 422 | |
| 423 | if(error) { |
| 424 | dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error); |
| 425 | /* don't want scsi_target_reap to do the final |
| 426 | * put because it will be under the host lock */ |
| 427 | get_device(dev); |
| 428 | scsi_target_reap(starget); |
| 429 | put_device(dev); |
| 430 | return NULL; |
| 431 | } |
| 432 | } |
James Bottomley | 884d25c | 2006-09-05 16:26:41 -0500 | [diff] [blame] | 433 | get_device(dev); |
James Bottomley | a283bd3 | 2005-05-24 12:06:38 -0500 | [diff] [blame] | 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | return starget; |
| 436 | |
| 437 | found: |
| 438 | found_target->reap_ref++; |
| 439 | spin_unlock_irqrestore(shost->host_lock, flags); |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 440 | if (found_target->state != STARGET_DEL) { |
James Bottomley | 884d25c | 2006-09-05 16:26:41 -0500 | [diff] [blame] | 441 | put_device(parent); |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 442 | kfree(starget); |
| 443 | return found_target; |
| 444 | } |
| 445 | /* Unfortunately, we found a dying target; need to |
| 446 | * wait until it's dead before we can get a new one */ |
| 447 | put_device(&found_target->dev); |
| 448 | flush_scheduled_work(); |
| 449 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 452 | static void scsi_target_reap_usercontext(struct work_struct *work) |
James Bottomley | 65110b2 | 2006-02-14 10:48:46 -0600 | [diff] [blame] | 453 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 454 | struct scsi_target *starget = |
| 455 | container_of(work, struct scsi_target, ew.work); |
James Bottomley | 863a930 | 2005-12-15 20:01:43 -0800 | [diff] [blame] | 456 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 457 | unsigned long flags; |
| 458 | |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 459 | transport_remove_device(&starget->dev); |
| 460 | device_del(&starget->dev); |
| 461 | transport_destroy_device(&starget->dev); |
James Bottomley | 863a930 | 2005-12-15 20:01:43 -0800 | [diff] [blame] | 462 | spin_lock_irqsave(shost->host_lock, flags); |
Mike Anderson | a50a5e3 | 2006-03-14 11:18:46 -0800 | [diff] [blame] | 463 | if (shost->hostt->target_destroy) |
| 464 | shost->hostt->target_destroy(starget); |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 465 | list_del_init(&starget->siblings); |
James Bottomley | 863a930 | 2005-12-15 20:01:43 -0800 | [diff] [blame] | 466 | spin_unlock_irqrestore(shost->host_lock, flags); |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 467 | put_device(&starget->dev); |
James Bottomley | 863a930 | 2005-12-15 20:01:43 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /** |
| 471 | * scsi_target_reap - check to see if target is in use and destroy if not |
| 472 | * |
| 473 | * @starget: target to be checked |
| 474 | * |
| 475 | * This is used after removing a LUN or doing a last put of the target |
| 476 | * it checks atomically that nothing is using the target and removes |
| 477 | * it if so. |
| 478 | */ |
| 479 | void scsi_target_reap(struct scsi_target *starget) |
| 480 | { |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 481 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 482 | unsigned long flags; |
| 483 | |
| 484 | spin_lock_irqsave(shost->host_lock, flags); |
| 485 | |
| 486 | if (--starget->reap_ref == 0 && list_empty(&starget->devices)) { |
| 487 | BUG_ON(starget->state == STARGET_DEL); |
| 488 | starget->state = STARGET_DEL; |
| 489 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 490 | execute_in_process_context(scsi_target_reap_usercontext, |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 491 | &starget->ew); |
James Bottomley | ffedb45 | 2006-02-23 14:27:18 -0600 | [diff] [blame] | 492 | return; |
| 493 | |
| 494 | } |
| 495 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 496 | |
| 497 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | /** |
Alan Stern | e5b3cd4 | 2006-08-21 15:53:25 -0400 | [diff] [blame] | 501 | * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string |
| 502 | * @s: INQUIRY result string to sanitize |
| 503 | * @len: length of the string |
| 504 | * |
| 505 | * Description: |
| 506 | * The SCSI spec says that INQUIRY vendor, product, and revision |
| 507 | * strings must consist entirely of graphic ASCII characters, |
| 508 | * padded on the right with spaces. Since not all devices obey |
| 509 | * this rule, we will replace non-graphic or non-ASCII characters |
| 510 | * with spaces. Exception: a NUL character is interpreted as a |
| 511 | * string terminator, so all the following characters are set to |
| 512 | * spaces. |
| 513 | **/ |
| 514 | static void sanitize_inquiry_string(unsigned char *s, int len) |
| 515 | { |
| 516 | int terminated = 0; |
| 517 | |
| 518 | for (; len > 0; (--len, ++s)) { |
| 519 | if (*s == 0) |
| 520 | terminated = 1; |
| 521 | if (terminated || *s < 0x20 || *s > 0x7e) |
| 522 | *s = ' '; |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 528 | * @sdev: scsi_device to probe |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | * @inq_result: area to store the INQUIRY result |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 530 | * @result_len: len of inq_result |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | * @bflags: store any bflags found here |
| 532 | * |
| 533 | * Description: |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 534 | * Probe the lun associated with @req using a standard SCSI INQUIRY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | * |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 536 | * If the INQUIRY is successful, zero is returned and the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 538 | * are copied to the scsi_device any flags value is stored in *@bflags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | **/ |
Alan Stern | e5b3cd4 | 2006-08-21 15:53:25 -0400 | [diff] [blame] | 540 | static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result, |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 541 | int result_len, int *bflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | unsigned char scsi_cmd[MAX_COMMAND_SIZE]; |
| 544 | int first_inquiry_len, try_inquiry_len, next_inquiry_len; |
| 545 | int response_len = 0; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 546 | int pass, count, result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | struct scsi_sense_hdr sshdr; |
| 548 | |
| 549 | *bflags = 0; |
| 550 | |
| 551 | /* Perform up to 3 passes. The first pass uses a conservative |
| 552 | * transfer length of 36 unless sdev->inquiry_len specifies a |
| 553 | * different value. */ |
| 554 | first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36; |
| 555 | try_inquiry_len = first_inquiry_len; |
| 556 | pass = 1; |
| 557 | |
| 558 | next_pass: |
James Bottomley | 9ccfc75 | 2005-10-02 11:45:08 -0500 | [diff] [blame] | 559 | SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev, |
| 560 | "scsi scan: INQUIRY pass %d length %d\n", |
| 561 | pass, try_inquiry_len)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
| 563 | /* Each pass gets up to three chances to ignore Unit Attention */ |
| 564 | for (count = 0; count < 3; ++count) { |
| 565 | memset(scsi_cmd, 0, 6); |
| 566 | scsi_cmd[0] = INQUIRY; |
| 567 | scsi_cmd[4] = (unsigned char) try_inquiry_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | |
| 569 | memset(inq_result, 0, try_inquiry_len); |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 570 | |
| 571 | result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 572 | inq_result, try_inquiry_len, &sshdr, |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 573 | HZ / 2 + HZ * scsi_inq_timeout, 3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
| 575 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s " |
| 576 | "with code 0x%x\n", |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 577 | result ? "failed" : "successful", result)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 579 | if (result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | /* |
| 581 | * not-ready to ready transition [asc/ascq=0x28/0x0] |
| 582 | * or power-on, reset [asc/ascq=0x29/0x0], continue. |
| 583 | * INQUIRY should not yield UNIT_ATTENTION |
| 584 | * but many buggy devices do so anyway. |
| 585 | */ |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 586 | if ((driver_byte(result) & DRIVER_SENSE) && |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 587 | scsi_sense_valid(&sshdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | if ((sshdr.sense_key == UNIT_ATTENTION) && |
| 589 | ((sshdr.asc == 0x28) || |
| 590 | (sshdr.asc == 0x29)) && |
| 591 | (sshdr.ascq == 0)) |
| 592 | continue; |
| 593 | } |
| 594 | } |
| 595 | break; |
| 596 | } |
| 597 | |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 598 | if (result == 0) { |
Alan Stern | e5b3cd4 | 2006-08-21 15:53:25 -0400 | [diff] [blame] | 599 | sanitize_inquiry_string(&inq_result[8], 8); |
| 600 | sanitize_inquiry_string(&inq_result[16], 16); |
| 601 | sanitize_inquiry_string(&inq_result[32], 4); |
| 602 | |
| 603 | response_len = inq_result[4] + 5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | if (response_len > 255) |
| 605 | response_len = first_inquiry_len; /* sanity */ |
| 606 | |
| 607 | /* |
| 608 | * Get any flags for this device. |
| 609 | * |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 610 | * XXX add a bflags to scsi_device, and replace the |
| 611 | * corresponding bit fields in scsi_device, so bflags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | * need not be passed as an argument. |
| 613 | */ |
| 614 | *bflags = scsi_get_device_flags(sdev, &inq_result[8], |
| 615 | &inq_result[16]); |
| 616 | |
| 617 | /* When the first pass succeeds we gain information about |
| 618 | * what larger transfer lengths might work. */ |
| 619 | if (pass == 1) { |
| 620 | if (BLIST_INQUIRY_36 & *bflags) |
| 621 | next_inquiry_len = 36; |
| 622 | else if (BLIST_INQUIRY_58 & *bflags) |
| 623 | next_inquiry_len = 58; |
| 624 | else if (sdev->inquiry_len) |
| 625 | next_inquiry_len = sdev->inquiry_len; |
| 626 | else |
| 627 | next_inquiry_len = response_len; |
| 628 | |
| 629 | /* If more data is available perform the second pass */ |
| 630 | if (next_inquiry_len > try_inquiry_len) { |
| 631 | try_inquiry_len = next_inquiry_len; |
| 632 | pass = 2; |
| 633 | goto next_pass; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | } else if (pass == 2) { |
| 638 | printk(KERN_INFO "scsi scan: %d byte inquiry failed. " |
| 639 | "Consider BLIST_INQUIRY_36 for this device\n", |
| 640 | try_inquiry_len); |
| 641 | |
| 642 | /* If this pass failed, the third pass goes back and transfers |
| 643 | * the same amount as we successfully got in the first pass. */ |
| 644 | try_inquiry_len = first_inquiry_len; |
| 645 | pass = 3; |
| 646 | goto next_pass; |
| 647 | } |
| 648 | |
| 649 | /* If the last transfer attempt got an error, assume the |
| 650 | * peripheral doesn't exist or is dead. */ |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 651 | if (result) |
| 652 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | /* Don't report any more data than the device says is valid */ |
| 655 | sdev->inquiry_len = min(try_inquiry_len, response_len); |
| 656 | |
| 657 | /* |
| 658 | * XXX Abort if the response length is less than 36? If less than |
| 659 | * 32, the lookup of the device flags (above) could be invalid, |
| 660 | * and it would be possible to take an incorrect action - we do |
| 661 | * not want to hang because of a short INQUIRY. On the flip side, |
| 662 | * if the device is spun down or becoming ready (and so it gives a |
| 663 | * short INQUIRY), an abort here prevents any further use of the |
| 664 | * device, including spin up. |
| 665 | * |
Alan Stern | e423ee3 | 2007-02-16 01:46:38 -0800 | [diff] [blame] | 666 | * On the whole, the best approach seems to be to assume the first |
| 667 | * 36 bytes are valid no matter what the device says. That's |
| 668 | * better than copying < 36 bytes to the inquiry-result buffer |
| 669 | * and displaying garbage for the Vendor, Product, or Revision |
| 670 | * strings. |
| 671 | */ |
| 672 | if (sdev->inquiry_len < 36) { |
| 673 | printk(KERN_INFO "scsi scan: INQUIRY result too short (%d)," |
| 674 | " using 36\n", sdev->inquiry_len); |
| 675 | sdev->inquiry_len = 36; |
| 676 | } |
| 677 | |
| 678 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | * Related to the above issue: |
| 680 | * |
| 681 | * XXX Devices (disk or all?) should be sent a TEST UNIT READY, |
| 682 | * and if not ready, sent a START_STOP to start (maybe spin up) and |
| 683 | * then send the INQUIRY again, since the INQUIRY can change after |
| 684 | * a device is initialized. |
| 685 | * |
| 686 | * Ideally, start a device if explicitly asked to do so. This |
| 687 | * assumes that a device is spun up on power on, spun down on |
| 688 | * request, and then spun up on request. |
| 689 | */ |
| 690 | |
| 691 | /* |
| 692 | * The scanning code needs to know the scsi_level, even if no |
| 693 | * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so |
| 694 | * non-zero LUNs can be scanned. |
| 695 | */ |
| 696 | sdev->scsi_level = inq_result[2] & 0x07; |
| 697 | if (sdev->scsi_level >= 2 || |
| 698 | (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1)) |
| 699 | sdev->scsi_level++; |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 700 | sdev->sdev_target->scsi_level = sdev->scsi_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 702 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 706 | * scsi_add_lun - allocate and fully initialze a scsi_device |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 707 | * @sdev: holds information to be stored in the new scsi_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | * @inq_result: holds the result of a previous INQUIRY to the LUN |
| 709 | * @bflags: black/white list flag |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 710 | * @async: 1 if this device is being scanned asynchronously |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | * |
| 712 | * Description: |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 713 | * Initialize the scsi_device @sdev. Optionally set fields based |
| 714 | * on values in *@bflags. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | * |
| 716 | * Return: |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 717 | * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device |
| 718 | * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | **/ |
Alan Stern | e5b3cd4 | 2006-08-21 15:53:25 -0400 | [diff] [blame] | 720 | static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result, |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 721 | int *bflags, int async) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | { |
| 723 | /* |
| 724 | * XXX do not save the inquiry, since it can change underneath us, |
| 725 | * save just vendor/model/rev. |
| 726 | * |
| 727 | * Rather than save it and have an ioctl that retrieves the saved |
| 728 | * value, have an ioctl that executes the same INQUIRY code used |
| 729 | * in scsi_probe_lun, let user level programs doing INQUIRY |
| 730 | * scanning run at their own risk, or supply a user level program |
| 731 | * that can correctly scan. |
| 732 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Alan Stern | 09123d2 | 2006-11-10 12:27:57 -0800 | [diff] [blame] | 734 | /* |
| 735 | * Copy at least 36 bytes of INQUIRY data, so that we don't |
| 736 | * dereference unallocated memory when accessing the Vendor, |
| 737 | * Product, and Revision strings. Badly behaved devices may set |
| 738 | * the INQUIRY Additional Length byte to a small value, indicating |
| 739 | * these strings are invalid, but often they contain plausible data |
| 740 | * nonetheless. It doesn't matter if the device sent < 36 bytes |
| 741 | * total, since scsi_probe_lun() initializes inq_result with 0s. |
| 742 | */ |
| 743 | sdev->inquiry = kmemdup(inq_result, |
| 744 | max_t(size_t, sdev->inquiry_len, 36), |
| 745 | GFP_ATOMIC); |
| 746 | if (sdev->inquiry == NULL) |
| 747 | return SCSI_SCAN_NO_RESPONSE; |
| 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | sdev->vendor = (char *) (sdev->inquiry + 8); |
| 750 | sdev->model = (char *) (sdev->inquiry + 16); |
| 751 | sdev->rev = (char *) (sdev->inquiry + 32); |
| 752 | |
| 753 | if (*bflags & BLIST_ISROM) { |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 754 | sdev->type = TYPE_ROM; |
| 755 | sdev->removable = 1; |
| 756 | } else { |
| 757 | sdev->type = (inq_result[0] & 0x1f); |
| 758 | sdev->removable = (inq_result[1] & 0x80) >> 7; |
| 759 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 761 | switch (sdev->type) { |
James Bottomley | ddaf6fc | 2006-12-13 10:10:40 -0600 | [diff] [blame] | 762 | case TYPE_RBC: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | case TYPE_TAPE: |
| 764 | case TYPE_DISK: |
| 765 | case TYPE_PRINTER: |
| 766 | case TYPE_MOD: |
| 767 | case TYPE_PROCESSOR: |
| 768 | case TYPE_SCANNER: |
| 769 | case TYPE_MEDIUM_CHANGER: |
| 770 | case TYPE_ENCLOSURE: |
| 771 | case TYPE_COMM: |
James Bottomley | 4d7db04 | 2006-03-31 20:07:45 -0600 | [diff] [blame] | 772 | case TYPE_RAID: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | sdev->writeable = 1; |
| 774 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | case TYPE_ROM: |
James Bottomley | ddaf6fc | 2006-12-13 10:10:40 -0600 | [diff] [blame] | 776 | case TYPE_WORM: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | sdev->writeable = 0; |
| 778 | break; |
| 779 | default: |
| 780 | printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type); |
| 781 | } |
| 782 | |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 783 | if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) { |
| 784 | /* RBC and MMC devices can return SCSI-3 compliance and yet |
| 785 | * still not support REPORT LUNS, so make them act as |
| 786 | * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is |
| 787 | * specifically set */ |
| 788 | if ((*bflags & BLIST_REPORTLUN2) == 0) |
| 789 | *bflags |= BLIST_NOREPORTLUN; |
| 790 | } |
| 791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | /* |
| 793 | * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI |
| 794 | * spec says: The device server is capable of supporting the |
| 795 | * specified peripheral device type on this logical unit. However, |
| 796 | * the physical device is not currently connected to this logical |
| 797 | * unit. |
| 798 | * |
| 799 | * The above is vague, as it implies that we could treat 001 and |
| 800 | * 011 the same. Stay compatible with previous code, and create a |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 801 | * scsi_device for a PQ of 1 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | * |
| 803 | * Don't set the device offline here; rather let the upper |
| 804 | * level drivers eval the PQ to decide whether they should |
| 805 | * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check. |
| 806 | */ |
| 807 | |
| 808 | sdev->inq_periph_qual = (inq_result[0] >> 5) & 7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | sdev->lockable = sdev->removable; |
| 810 | sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2); |
| 811 | |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 812 | if (sdev->scsi_level >= SCSI_3 || |
| 813 | (sdev->inquiry_len > 56 && inq_result[56] & 0x04)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | sdev->ppr = 1; |
| 815 | if (inq_result[7] & 0x60) |
| 816 | sdev->wdtr = 1; |
| 817 | if (inq_result[7] & 0x10) |
| 818 | sdev->sdtr = 1; |
| 819 | |
James Bottomley | 19ac0db | 2006-08-06 18:15:22 -0500 | [diff] [blame] | 820 | sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d " |
Matthew Wilcox | 4ff3671 | 2006-07-04 12:15:20 -0600 | [diff] [blame] | 821 | "ANSI: %d%s\n", scsi_device_type(sdev->type), |
| 822 | sdev->vendor, sdev->model, sdev->rev, |
| 823 | sdev->inq_periph_qual, inq_result[2] & 0x07, |
| 824 | (inq_result[3] & 0x0f) == 1 ? " CCS" : ""); |
| 825 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) && |
| 827 | !(*bflags & BLIST_NOTQ)) |
| 828 | sdev->tagged_supported = 1; |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 829 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | /* |
| 831 | * Some devices (Texel CD ROM drives) have handshaking problems |
| 832 | * when used with the Seagate controllers. borken is initialized |
| 833 | * to 1, and then set it to 0 here. |
| 834 | */ |
| 835 | if ((*bflags & BLIST_BORKEN) == 0) |
| 836 | sdev->borken = 0; |
| 837 | |
Matthew Wilcox | 6d87768 | 2007-07-11 12:54:55 -0600 | [diff] [blame] | 838 | if (*bflags & BLIST_NO_ULD_ATTACH) |
| 839 | sdev->no_uld_attach = 1; |
| 840 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | /* |
| 842 | * Apparently some really broken devices (contrary to the SCSI |
| 843 | * standards) need to be selected without asserting ATN |
| 844 | */ |
| 845 | if (*bflags & BLIST_SELECT_NO_ATN) |
| 846 | sdev->select_no_atn = 1; |
| 847 | |
| 848 | /* |
James Bottomley | 4d7db04 | 2006-03-31 20:07:45 -0600 | [diff] [blame] | 849 | * Maximum 512 sector transfer length |
| 850 | * broken RA4x00 Compaq Disk Array |
| 851 | */ |
| 852 | if (*bflags & BLIST_MAX_512) |
| 853 | blk_queue_max_sectors(sdev->request_queue, 512); |
| 854 | |
| 855 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | * Some devices may not want to have a start command automatically |
| 857 | * issued when a device is added. |
| 858 | */ |
| 859 | if (*bflags & BLIST_NOSTARTONADD) |
| 860 | sdev->no_start_on_add = 1; |
| 861 | |
| 862 | if (*bflags & BLIST_SINGLELUN) |
| 863 | sdev->single_lun = 1; |
| 864 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | sdev->use_10_for_rw = 1; |
| 866 | |
| 867 | if (*bflags & BLIST_MS_SKIP_PAGE_08) |
| 868 | sdev->skip_ms_page_8 = 1; |
| 869 | |
| 870 | if (*bflags & BLIST_MS_SKIP_PAGE_3F) |
| 871 | sdev->skip_ms_page_3f = 1; |
| 872 | |
| 873 | if (*bflags & BLIST_USE_10_BYTE_MS) |
| 874 | sdev->use_10_for_ms = 1; |
| 875 | |
| 876 | /* set the device running here so that slave configure |
| 877 | * may do I/O */ |
| 878 | scsi_device_set_state(sdev, SDEV_RUNNING); |
| 879 | |
| 880 | if (*bflags & BLIST_MS_192_BYTES_FOR_3F) |
| 881 | sdev->use_192_bytes_for_3f = 1; |
| 882 | |
| 883 | if (*bflags & BLIST_NOT_LOCKABLE) |
| 884 | sdev->lockable = 0; |
| 885 | |
| 886 | if (*bflags & BLIST_RETRY_HWERROR) |
| 887 | sdev->retry_hwerror = 1; |
| 888 | |
| 889 | transport_configure_device(&sdev->sdev_gendev); |
| 890 | |
Christoph Hellwig | 9380509 | 2006-02-17 12:11:29 +0100 | [diff] [blame] | 891 | if (sdev->host->hostt->slave_configure) { |
| 892 | int ret = sdev->host->hostt->slave_configure(sdev); |
| 893 | if (ret) { |
| 894 | /* |
| 895 | * if LLDD reports slave not present, don't clutter |
| 896 | * console with alloc failure messages |
| 897 | */ |
| 898 | if (ret != -ENXIO) { |
| 899 | sdev_printk(KERN_ERR, sdev, |
| 900 | "failed to configure device\n"); |
| 901 | } |
| 902 | return SCSI_SCAN_NO_RESPONSE; |
| 903 | } |
| 904 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
| 906 | /* |
| 907 | * Ok, the device is now all set up, we can |
| 908 | * register it and tell the rest of the kernel |
| 909 | * about it. |
| 910 | */ |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 911 | if (!async && scsi_sysfs_add_sdev(sdev) != 0) |
Alan Stern | b24b103 | 2005-07-27 11:43:46 -0700 | [diff] [blame] | 912 | return SCSI_SCAN_NO_RESPONSE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
| 914 | return SCSI_SCAN_LUN_PRESENT; |
| 915 | } |
| 916 | |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 917 | static inline void scsi_destroy_sdev(struct scsi_device *sdev) |
| 918 | { |
Brian King | 309bd27 | 2006-06-27 11:10:43 -0500 | [diff] [blame] | 919 | scsi_device_set_state(sdev, SDEV_DEL); |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 920 | if (sdev->host->hostt->slave_destroy) |
| 921 | sdev->host->hostt->slave_destroy(sdev); |
| 922 | transport_destroy_device(&sdev->sdev_gendev); |
| 923 | put_device(&sdev->sdev_gendev); |
| 924 | } |
| 925 | |
akpm@osdl.org | c5f2e64 | 2006-04-15 00:30:24 -0700 | [diff] [blame] | 926 | #ifdef CONFIG_SCSI_LOGGING |
Kurt Garloff | 6c7154c | 2006-04-03 15:18:35 +0200 | [diff] [blame] | 927 | /** |
| 928 | * scsi_inq_str - print INQUIRY data from min to max index, |
| 929 | * strip trailing whitespace |
| 930 | * @buf: Output buffer with at least end-first+1 bytes of space |
| 931 | * @inq: Inquiry buffer (input) |
| 932 | * @first: Offset of string into inq |
| 933 | * @end: Index after last character in inq |
| 934 | */ |
akpm@osdl.org | c5f2e64 | 2006-04-15 00:30:24 -0700 | [diff] [blame] | 935 | static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq, |
Kurt Garloff | 6c7154c | 2006-04-03 15:18:35 +0200 | [diff] [blame] | 936 | unsigned first, unsigned end) |
| 937 | { |
| 938 | unsigned term = 0, idx; |
akpm@osdl.org | c5f2e64 | 2006-04-15 00:30:24 -0700 | [diff] [blame] | 939 | |
| 940 | for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) { |
| 941 | if (inq[idx+first] > ' ') { |
Kurt Garloff | 6c7154c | 2006-04-03 15:18:35 +0200 | [diff] [blame] | 942 | buf[idx] = inq[idx+first]; |
| 943 | term = idx+1; |
| 944 | } else { |
| 945 | buf[idx] = ' '; |
| 946 | } |
| 947 | } |
| 948 | buf[term] = 0; |
| 949 | return buf; |
| 950 | } |
akpm@osdl.org | c5f2e64 | 2006-04-15 00:30:24 -0700 | [diff] [blame] | 951 | #endif |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 952 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | /** |
| 954 | * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it |
| 955 | * @starget: pointer to target device structure |
| 956 | * @lun: LUN of target device |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 957 | * @sdevscan: probe the LUN corresponding to this scsi_device |
| 958 | * @sdevnew: store the value of any new scsi_device allocated |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | * @bflagsp: store bflags here if not NULL |
| 960 | * |
| 961 | * Description: |
| 962 | * Call scsi_probe_lun, if a LUN with an attached device is found, |
| 963 | * allocate and set it up by calling scsi_add_lun. |
| 964 | * |
| 965 | * Return: |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 966 | * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is |
| 968 | * attached at the LUN |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 969 | * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | **/ |
| 971 | static int scsi_probe_and_add_lun(struct scsi_target *starget, |
| 972 | uint lun, int *bflagsp, |
| 973 | struct scsi_device **sdevp, int rescan, |
| 974 | void *hostdata) |
| 975 | { |
| 976 | struct scsi_device *sdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | unsigned char *result; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 978 | int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 980 | |
| 981 | /* |
| 982 | * The rescan flag is used as an optimization, the first scan of a |
| 983 | * host adapter calls into here with rescan == 0. |
| 984 | */ |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 985 | sdev = scsi_device_lookup_by_target(starget, lun); |
| 986 | if (sdev) { |
| 987 | if (rescan || sdev->sdev_state != SDEV_CREATED) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO |
| 989 | "scsi scan: device exists on %s\n", |
| 990 | sdev->sdev_gendev.bus_id)); |
| 991 | if (sdevp) |
| 992 | *sdevp = sdev; |
| 993 | else |
| 994 | scsi_device_put(sdev); |
| 995 | |
| 996 | if (bflagsp) |
| 997 | *bflagsp = scsi_get_device_flags(sdev, |
| 998 | sdev->vendor, |
| 999 | sdev->model); |
| 1000 | return SCSI_SCAN_LUN_PRESENT; |
| 1001 | } |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1002 | scsi_device_put(sdev); |
| 1003 | } else |
| 1004 | sdev = scsi_alloc_sdev(starget, lun, hostdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | if (!sdev) |
| 1006 | goto out; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1007 | |
| 1008 | result = kmalloc(result_len, GFP_ATOMIC | |
Al Viro | bc86120 | 2005-04-24 12:28:34 -0700 | [diff] [blame] | 1009 | ((shost->unchecked_isa_dma) ? __GFP_DMA : 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | if (!result) |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1011 | goto out_free_sdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1013 | if (scsi_probe_lun(sdev, result, result_len, &bflags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | goto out_free_result; |
| 1015 | |
Kurt Garloff | 4186ab1 | 2006-04-03 15:16:48 +0200 | [diff] [blame] | 1016 | if (bflagsp) |
| 1017 | *bflagsp = bflags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | /* |
| 1019 | * result contains valid SCSI INQUIRY data. |
| 1020 | */ |
Kurt Garloff | 13f7e5a | 2006-04-03 15:20:08 +0200 | [diff] [blame] | 1021 | if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | /* |
| 1023 | * For a Peripheral qualifier 3 (011b), the SCSI |
| 1024 | * spec says: The device server is not capable of |
| 1025 | * supporting a physical device on this logical |
| 1026 | * unit. |
| 1027 | * |
| 1028 | * For disks, this implies that there is no |
| 1029 | * logical disk configured at sdev->lun, but there |
| 1030 | * is a target id responding. |
| 1031 | */ |
Kurt Garloff | 6c7154c | 2006-04-03 15:18:35 +0200 | [diff] [blame] | 1032 | SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:" |
| 1033 | " peripheral qualifier of 3, device not" |
| 1034 | " added\n")) |
| 1035 | if (lun == 0) { |
akpm@osdl.org | c5f2e64 | 2006-04-15 00:30:24 -0700 | [diff] [blame] | 1036 | SCSI_LOG_SCAN_BUS(1, { |
| 1037 | unsigned char vend[9]; |
| 1038 | unsigned char mod[17]; |
| 1039 | |
| 1040 | sdev_printk(KERN_INFO, sdev, |
Kurt Garloff | 6c7154c | 2006-04-03 15:18:35 +0200 | [diff] [blame] | 1041 | "scsi scan: consider passing scsi_mod." |
Kurt Garloff | 3424a65 | 2007-01-09 02:28:54 +0100 | [diff] [blame] | 1042 | "dev_flags=%s:%s:0x240 or 0x1000240\n", |
Kurt Garloff | 6c7154c | 2006-04-03 15:18:35 +0200 | [diff] [blame] | 1043 | scsi_inq_str(vend, result, 8, 16), |
akpm@osdl.org | c5f2e64 | 2006-04-15 00:30:24 -0700 | [diff] [blame] | 1044 | scsi_inq_str(mod, result, 16, 32)); |
| 1045 | }); |
Kurt Garloff | 6c7154c | 2006-04-03 15:18:35 +0200 | [diff] [blame] | 1046 | } |
| 1047 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | res = SCSI_SCAN_TARGET_PRESENT; |
| 1049 | goto out_free_result; |
| 1050 | } |
| 1051 | |
Alan Stern | 1bfc5d9 | 2006-02-09 15:26:18 -0500 | [diff] [blame] | 1052 | /* |
dave wysochanski | 84961f2 | 2006-08-09 14:56:32 -0400 | [diff] [blame] | 1053 | * Some targets may set slight variations of PQ and PDT to signal |
| 1054 | * that no LUN is present, so don't add sdev in these cases. |
| 1055 | * Two specific examples are: |
| 1056 | * 1) NetApp targets: return PQ=1, PDT=0x1f |
| 1057 | * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved" |
| 1058 | * in the UFI 1.0 spec (we cannot rely on reserved bits). |
| 1059 | * |
| 1060 | * References: |
| 1061 | * 1) SCSI SPC-3, pp. 145-146 |
| 1062 | * PQ=1: "A peripheral device having the specified peripheral |
| 1063 | * device type is not connected to this logical unit. However, the |
| 1064 | * device server is capable of supporting the specified peripheral |
| 1065 | * device type on this logical unit." |
| 1066 | * PDT=0x1f: "Unknown or no device type" |
| 1067 | * 2) USB UFI 1.0, p. 20 |
| 1068 | * PDT=00h Direct-access device (floppy) |
| 1069 | * PDT=1Fh none (no FDD connected to the requested logical unit) |
Alan Stern | 1bfc5d9 | 2006-02-09 15:26:18 -0500 | [diff] [blame] | 1070 | */ |
dave wysochanski | 84961f2 | 2006-08-09 14:56:32 -0400 | [diff] [blame] | 1071 | if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) && |
| 1072 | (result[0] & 0x1f) == 0x1f) { |
Alan Stern | 1bfc5d9 | 2006-02-09 15:26:18 -0500 | [diff] [blame] | 1073 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO |
| 1074 | "scsi scan: peripheral device type" |
| 1075 | " of 31, no device added\n")); |
| 1076 | res = SCSI_SCAN_TARGET_PRESENT; |
| 1077 | goto out_free_result; |
| 1078 | } |
| 1079 | |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1080 | res = scsi_add_lun(sdev, result, &bflags, shost->async_scan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | if (res == SCSI_SCAN_LUN_PRESENT) { |
| 1082 | if (bflags & BLIST_KEY) { |
| 1083 | sdev->lockable = 0; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1084 | scsi_unlock_floptical(sdev, result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | out_free_result: |
| 1089 | kfree(result); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1090 | out_free_sdev: |
| 1091 | if (res == SCSI_SCAN_LUN_PRESENT) { |
| 1092 | if (sdevp) { |
Alan Stern | b70d37b | 2005-07-26 10:30:40 -0400 | [diff] [blame] | 1093 | if (scsi_device_get(sdev) == 0) { |
| 1094 | *sdevp = sdev; |
| 1095 | } else { |
| 1096 | __scsi_remove_device(sdev); |
| 1097 | res = SCSI_SCAN_NO_RESPONSE; |
| 1098 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1100 | } else |
| 1101 | scsi_destroy_sdev(sdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | out: |
| 1103 | return res; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * scsi_sequential_lun_scan - sequentially scan a SCSI target |
| 1108 | * @starget: pointer to target structure to scan |
| 1109 | * @bflags: black/white list flag for LUN 0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | * |
| 1111 | * Description: |
| 1112 | * Generally, scan from LUN 1 (LUN 0 is assumed to already have been |
| 1113 | * scanned) to some maximum lun until a LUN is found with no device |
| 1114 | * attached. Use the bflags to figure out any oddities. |
| 1115 | * |
| 1116 | * Modifies sdevscan->lun. |
| 1117 | **/ |
| 1118 | static void scsi_sequential_lun_scan(struct scsi_target *starget, |
Kurt Garloff | 4186ab1 | 2006-04-03 15:16:48 +0200 | [diff] [blame] | 1119 | int bflags, int scsi_level, int rescan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | { |
| 1121 | unsigned int sparse_lun, lun, max_dev_lun; |
| 1122 | struct Scsi_Host *shost = dev_to_shost(starget->dev.parent); |
| 1123 | |
| 1124 | SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of" |
| 1125 | "%s\n", starget->dev.bus_id)); |
| 1126 | |
| 1127 | max_dev_lun = min(max_scsi_luns, shost->max_lun); |
| 1128 | /* |
| 1129 | * If this device is known to support sparse multiple units, |
| 1130 | * override the other settings, and scan all of them. Normally, |
| 1131 | * SCSI-3 devices should be scanned via the REPORT LUNS. |
| 1132 | */ |
| 1133 | if (bflags & BLIST_SPARSELUN) { |
| 1134 | max_dev_lun = shost->max_lun; |
| 1135 | sparse_lun = 1; |
| 1136 | } else |
| 1137 | sparse_lun = 0; |
| 1138 | |
| 1139 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | * If less than SCSI_1_CSS, and no special lun scaning, stop |
| 1141 | * scanning; this matches 2.4 behaviour, but could just be a bug |
| 1142 | * (to continue scanning a SCSI_1_CSS device). |
| 1143 | * |
| 1144 | * This test is broken. We might not have any device on lun0 for |
| 1145 | * a sparselun device, and if that's the case then how would we |
| 1146 | * know the real scsi_level, eh? It might make sense to just not |
| 1147 | * scan any SCSI_1 device for non-0 luns, but that check would best |
| 1148 | * go into scsi_alloc_sdev() and just have it return null when asked |
| 1149 | * to alloc an sdev for lun > 0 on an already found SCSI_1 device. |
| 1150 | * |
| 1151 | if ((sdevscan->scsi_level < SCSI_1_CCS) && |
| 1152 | ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN)) |
| 1153 | == 0)) |
| 1154 | return; |
| 1155 | */ |
| 1156 | /* |
| 1157 | * If this device is known to support multiple units, override |
| 1158 | * the other settings, and scan all of them. |
| 1159 | */ |
| 1160 | if (bflags & BLIST_FORCELUN) |
| 1161 | max_dev_lun = shost->max_lun; |
| 1162 | /* |
| 1163 | * REGAL CDC-4X: avoid hang after LUN 4 |
| 1164 | */ |
| 1165 | if (bflags & BLIST_MAX5LUN) |
| 1166 | max_dev_lun = min(5U, max_dev_lun); |
| 1167 | /* |
| 1168 | * Do not scan SCSI-2 or lower device past LUN 7, unless |
| 1169 | * BLIST_LARGELUN. |
| 1170 | */ |
| 1171 | if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN)) |
| 1172 | max_dev_lun = min(8U, max_dev_lun); |
| 1173 | |
| 1174 | /* |
| 1175 | * We have already scanned LUN 0, so start at LUN 1. Keep scanning |
| 1176 | * until we reach the max, or no LUN is found and we are not |
| 1177 | * sparse_lun. |
| 1178 | */ |
| 1179 | for (lun = 1; lun < max_dev_lun; ++lun) |
| 1180 | if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, |
| 1181 | NULL) != SCSI_SCAN_LUN_PRESENT) && |
| 1182 | !sparse_lun) |
| 1183 | return; |
| 1184 | } |
| 1185 | |
| 1186 | /** |
| 1187 | * scsilun_to_int: convert a scsi_lun to an int |
| 1188 | * @scsilun: struct scsi_lun to be converted. |
| 1189 | * |
| 1190 | * Description: |
| 1191 | * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered |
| 1192 | * integer, and return the result. The caller must check for |
| 1193 | * truncation before using this function. |
| 1194 | * |
| 1195 | * Notes: |
| 1196 | * The struct scsi_lun is assumed to be four levels, with each level |
| 1197 | * effectively containing a SCSI byte-ordered (big endian) short; the |
| 1198 | * addressing bits of each level are ignored (the highest two bits). |
| 1199 | * For a description of the LUN format, post SCSI-3 see the SCSI |
| 1200 | * Architecture Model, for SCSI-3 see the SCSI Controller Commands. |
| 1201 | * |
| 1202 | * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns |
| 1203 | * the integer: 0x0b030a04 |
| 1204 | **/ |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 1205 | int scsilun_to_int(struct scsi_lun *scsilun) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | { |
| 1207 | int i; |
| 1208 | unsigned int lun; |
| 1209 | |
| 1210 | lun = 0; |
| 1211 | for (i = 0; i < sizeof(lun); i += 2) |
| 1212 | lun = lun | (((scsilun->scsi_lun[i] << 8) | |
| 1213 | scsilun->scsi_lun[i + 1]) << (i * 8)); |
| 1214 | return lun; |
| 1215 | } |
Christof Schmitt | 462b785 | 2007-06-19 10:25:30 +0200 | [diff] [blame] | 1216 | EXPORT_SYMBOL(scsilun_to_int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | /** |
James.Smart@Emulex.Com | 2f4701d | 2005-07-13 22:05:03 -0400 | [diff] [blame] | 1219 | * int_to_scsilun: reverts an int into a scsi_lun |
| 1220 | * @int: integer to be reverted |
| 1221 | * @scsilun: struct scsi_lun to be set. |
| 1222 | * |
| 1223 | * Description: |
| 1224 | * Reverts the functionality of the scsilun_to_int, which packed |
| 1225 | * an 8-byte lun value into an int. This routine unpacks the int |
| 1226 | * back into the lun value. |
| 1227 | * Note: the scsilun_to_int() routine does not truly handle all |
| 1228 | * 8bytes of the lun value. This functions restores only as much |
| 1229 | * as was set by the routine. |
| 1230 | * |
| 1231 | * Notes: |
| 1232 | * Given an integer : 0x0b030a04, this function returns a |
| 1233 | * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00 |
| 1234 | * |
| 1235 | **/ |
| 1236 | void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun) |
| 1237 | { |
| 1238 | int i; |
| 1239 | |
| 1240 | memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun)); |
| 1241 | |
| 1242 | for (i = 0; i < sizeof(lun); i += 2) { |
| 1243 | scsilun->scsi_lun[i] = (lun >> 8) & 0xFF; |
| 1244 | scsilun->scsi_lun[i+1] = lun & 0xFF; |
| 1245 | lun = lun >> 16; |
| 1246 | } |
| 1247 | } |
| 1248 | EXPORT_SYMBOL(int_to_scsilun); |
| 1249 | |
| 1250 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1251 | * scsi_report_lun_scan - Scan using SCSI REPORT LUN results |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 1252 | * @sdevscan: scan the host, channel, and id of this scsi_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | * |
| 1254 | * Description: |
| 1255 | * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN |
| 1256 | * command, and scan the resulting list of LUNs by calling |
| 1257 | * scsi_probe_and_add_lun. |
| 1258 | * |
| 1259 | * Modifies sdevscan->lun. |
| 1260 | * |
| 1261 | * Return: |
| 1262 | * 0: scan completed (or no memory, so further scanning is futile) |
| 1263 | * 1: no report lun scan, or not configured |
| 1264 | **/ |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1265 | static int scsi_report_lun_scan(struct scsi_target *starget, int bflags, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | int rescan) |
| 1267 | { |
| 1268 | char devname[64]; |
| 1269 | unsigned char scsi_cmd[MAX_COMMAND_SIZE]; |
| 1270 | unsigned int length; |
| 1271 | unsigned int lun; |
| 1272 | unsigned int num_luns; |
| 1273 | unsigned int retries; |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1274 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | struct scsi_lun *lunp, *lun_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | u8 *data; |
| 1277 | struct scsi_sense_hdr sshdr; |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1278 | struct scsi_device *sdev; |
| 1279 | struct Scsi_Host *shost = dev_to_shost(&starget->dev); |
Alan Stern | 2ef8919 | 2005-11-08 15:51:55 -0500 | [diff] [blame] | 1280 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
| 1282 | /* |
| 1283 | * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set. |
| 1284 | * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does |
| 1285 | * support more than 8 LUNs. |
| 1286 | */ |
James Bottomley | 4d7db04 | 2006-03-31 20:07:45 -0600 | [diff] [blame] | 1287 | if (bflags & BLIST_NOREPORTLUN) |
| 1288 | return 1; |
| 1289 | if (starget->scsi_level < SCSI_2 && |
| 1290 | starget->scsi_level != SCSI_UNKNOWN) |
| 1291 | return 1; |
| 1292 | if (starget->scsi_level < SCSI_3 && |
| 1293 | (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | return 1; |
| 1295 | if (bflags & BLIST_NOLUN) |
| 1296 | return 0; |
| 1297 | |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1298 | if (!(sdev = scsi_device_lookup_by_target(starget, 0))) { |
| 1299 | sdev = scsi_alloc_sdev(starget, 0, NULL); |
| 1300 | if (!sdev) |
| 1301 | return 0; |
| 1302 | if (scsi_device_get(sdev)) |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | sprintf(devname, "host %d channel %d id %d", |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1307 | shost->host_no, sdev->channel, sdev->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | |
| 1309 | /* |
| 1310 | * Allocate enough to hold the header (the same size as one scsi_lun) |
| 1311 | * plus the max number of luns we are requesting. |
| 1312 | * |
| 1313 | * Reallocating and trying again (with the exact amount we need) |
| 1314 | * would be nice, but then we need to somehow limit the size |
| 1315 | * allocated based on the available memory and the limits of |
| 1316 | * kmalloc - we don't want a kmalloc() failure of a huge value to |
| 1317 | * prevent us from finding any LUNs on this target. |
| 1318 | */ |
| 1319 | length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun); |
| 1320 | lun_data = kmalloc(length, GFP_ATOMIC | |
| 1321 | (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0)); |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1322 | if (!lun_data) { |
| 1323 | printk(ALLOC_FAILURE_MSG, __FUNCTION__); |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1324 | goto out; |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1325 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
| 1327 | scsi_cmd[0] = REPORT_LUNS; |
| 1328 | |
| 1329 | /* |
| 1330 | * bytes 1 - 5: reserved, set to zero. |
| 1331 | */ |
| 1332 | memset(&scsi_cmd[1], 0, 5); |
| 1333 | |
| 1334 | /* |
| 1335 | * bytes 6 - 9: length of the command. |
| 1336 | */ |
| 1337 | scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff; |
| 1338 | scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff; |
| 1339 | scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff; |
| 1340 | scsi_cmd[9] = (unsigned char) length & 0xff; |
| 1341 | |
| 1342 | scsi_cmd[10] = 0; /* reserved */ |
| 1343 | scsi_cmd[11] = 0; /* control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1344 | |
| 1345 | /* |
| 1346 | * We can get a UNIT ATTENTION, for example a power on/reset, so |
| 1347 | * retry a few times (like sd.c does for TEST UNIT READY). |
| 1348 | * Experience shows some combinations of adapter/devices get at |
| 1349 | * least two power on/resets. |
| 1350 | * |
| 1351 | * Illegal requests (for devices that do not support REPORT LUNS) |
| 1352 | * should come through as a check condition, and will not generate |
| 1353 | * a retry. |
| 1354 | */ |
| 1355 | for (retries = 0; retries < 3; retries++) { |
| 1356 | SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending" |
| 1357 | " REPORT LUNS to %s (try %d)\n", devname, |
| 1358 | retries)); |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1359 | |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1360 | result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1361 | lun_data, length, &sshdr, |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1362 | SCSI_TIMEOUT + 4 * HZ, 3); |
| 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS" |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1365 | " %s (try %d) result 0x%x\n", result |
| 1366 | ? "failed" : "successful", retries, result)); |
| 1367 | if (result == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | break; |
James Bottomley | ea73a9f | 2005-08-28 11:33:52 -0500 | [diff] [blame] | 1369 | else if (scsi_sense_valid(&sshdr)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | if (sshdr.sense_key != UNIT_ATTENTION) |
| 1371 | break; |
| 1372 | } |
| 1373 | } |
| 1374 | |
James Bottomley | 3921603 | 2005-06-15 18:48:29 -0500 | [diff] [blame] | 1375 | if (result) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | /* |
| 1377 | * The device probably does not support a REPORT LUN command |
| 1378 | */ |
Alan Stern | 2ef8919 | 2005-11-08 15:51:55 -0500 | [diff] [blame] | 1379 | ret = 1; |
| 1380 | goto out_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
| 1383 | /* |
| 1384 | * Get the length from the first four bytes of lun_data. |
| 1385 | */ |
| 1386 | data = (u8 *) lun_data->scsi_lun; |
| 1387 | length = ((data[0] << 24) | (data[1] << 16) | |
| 1388 | (data[2] << 8) | (data[3] << 0)); |
| 1389 | |
| 1390 | num_luns = (length / sizeof(struct scsi_lun)); |
| 1391 | if (num_luns > max_scsi_report_luns) { |
| 1392 | printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)" |
| 1393 | " of %d luns reported, try increasing" |
| 1394 | " max_scsi_report_luns.\n", devname, |
| 1395 | max_scsi_report_luns, num_luns); |
| 1396 | num_luns = max_scsi_report_luns; |
| 1397 | } |
| 1398 | |
Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 1399 | SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev, |
| 1400 | "scsi scan: REPORT LUN scan\n")); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | |
| 1402 | /* |
| 1403 | * Scan the luns in lun_data. The entry at offset 0 is really |
| 1404 | * the header, so start at 1 and go up to and including num_luns. |
| 1405 | */ |
| 1406 | for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) { |
| 1407 | lun = scsilun_to_int(lunp); |
| 1408 | |
| 1409 | /* |
| 1410 | * Check if the unused part of lunp is non-zero, and so |
| 1411 | * does not fit in lun. |
| 1412 | */ |
| 1413 | if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) { |
| 1414 | int i; |
| 1415 | |
| 1416 | /* |
| 1417 | * Output an error displaying the LUN in byte order, |
| 1418 | * this differs from what linux would print for the |
| 1419 | * integer LUN value. |
| 1420 | */ |
| 1421 | printk(KERN_WARNING "scsi: %s lun 0x", devname); |
| 1422 | data = (char *)lunp->scsi_lun; |
| 1423 | for (i = 0; i < sizeof(struct scsi_lun); i++) |
| 1424 | printk("%02x", data[i]); |
| 1425 | printk(" has a LUN larger than currently supported.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | } else if (lun > sdev->host->max_lun) { |
| 1427 | printk(KERN_WARNING "scsi: %s lun%d has a LUN larger" |
| 1428 | " than allowed by the host adapter\n", |
| 1429 | devname, lun); |
| 1430 | } else { |
| 1431 | int res; |
| 1432 | |
| 1433 | res = scsi_probe_and_add_lun(starget, |
| 1434 | lun, NULL, NULL, rescan, NULL); |
| 1435 | if (res == SCSI_SCAN_NO_RESPONSE) { |
| 1436 | /* |
| 1437 | * Got some results, but now none, abort. |
| 1438 | */ |
Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 1439 | sdev_printk(KERN_ERR, sdev, |
| 1440 | "Unexpected response" |
| 1441 | " from lun %d while scanning, scan" |
| 1442 | " aborted\n", lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1443 | break; |
| 1444 | } |
| 1445 | } |
| 1446 | } |
| 1447 | |
Alan Stern | 2ef8919 | 2005-11-08 15:51:55 -0500 | [diff] [blame] | 1448 | out_err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | kfree(lun_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | out: |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1451 | scsi_device_put(sdev); |
| 1452 | if (sdev->sdev_state == SDEV_CREATED) |
| 1453 | /* |
| 1454 | * the sdev we used didn't appear in the report luns scan |
| 1455 | */ |
| 1456 | scsi_destroy_sdev(sdev); |
Alan Stern | 2ef8919 | 2005-11-08 15:51:55 -0500 | [diff] [blame] | 1457 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel, |
| 1461 | uint id, uint lun, void *hostdata) |
| 1462 | { |
Matthew Wilcox | a97a83a | 2006-02-05 08:01:33 -0700 | [diff] [blame] | 1463 | struct scsi_device *sdev = ERR_PTR(-ENODEV); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | struct device *parent = &shost->shost_gendev; |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1465 | struct scsi_target *starget; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
Matthew Wilcox | 938e2ac | 2007-01-15 18:07:09 -0700 | [diff] [blame] | 1467 | if (strncmp(scsi_scan_type, "none", 4) == 0) |
| 1468 | return ERR_PTR(-ENODEV); |
| 1469 | |
Christoph Hellwig | e02f3f5 | 2006-01-13 19:04:00 +0100 | [diff] [blame] | 1470 | starget = scsi_alloc_target(parent, channel, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | if (!starget) |
| 1472 | return ERR_PTR(-ENOMEM); |
| 1473 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1474 | mutex_lock(&shost->scan_mutex); |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1475 | if (!shost->async_scan) |
| 1476 | scsi_complete_async_scans(); |
| 1477 | |
Matthew Wilcox | a97a83a | 2006-02-05 08:01:33 -0700 | [diff] [blame] | 1478 | if (scsi_host_scan_allowed(shost)) |
| 1479 | scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1480 | mutex_unlock(&shost->scan_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | scsi_target_reap(starget); |
| 1482 | put_device(&starget->dev); |
| 1483 | |
| 1484 | return sdev; |
| 1485 | } |
| 1486 | EXPORT_SYMBOL(__scsi_add_device); |
| 1487 | |
James Bottomley | 146f726 | 2005-09-10 12:44:09 -0500 | [diff] [blame] | 1488 | int scsi_add_device(struct Scsi_Host *host, uint channel, |
| 1489 | uint target, uint lun) |
| 1490 | { |
| 1491 | struct scsi_device *sdev = |
| 1492 | __scsi_add_device(host, channel, target, lun, NULL); |
| 1493 | if (IS_ERR(sdev)) |
| 1494 | return PTR_ERR(sdev); |
| 1495 | |
| 1496 | scsi_device_put(sdev); |
| 1497 | return 0; |
| 1498 | } |
| 1499 | EXPORT_SYMBOL(scsi_add_device); |
| 1500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1501 | void scsi_rescan_device(struct device *dev) |
| 1502 | { |
| 1503 | struct scsi_driver *drv; |
| 1504 | |
| 1505 | if (!dev->driver) |
| 1506 | return; |
| 1507 | |
| 1508 | drv = to_scsi_driver(dev->driver); |
| 1509 | if (try_module_get(drv->owner)) { |
| 1510 | if (drv->rescan) |
| 1511 | drv->rescan(dev); |
| 1512 | module_put(drv->owner); |
| 1513 | } |
| 1514 | } |
| 1515 | EXPORT_SYMBOL(scsi_rescan_device); |
| 1516 | |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1517 | static void __scsi_scan_target(struct device *parent, unsigned int channel, |
| 1518 | unsigned int id, unsigned int lun, int rescan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | { |
| 1520 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 1521 | int bflags = 0; |
| 1522 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | struct scsi_target *starget; |
| 1524 | |
| 1525 | if (shost->this_id == id) |
| 1526 | /* |
| 1527 | * Don't scan the host adapter |
| 1528 | */ |
| 1529 | return; |
| 1530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | starget = scsi_alloc_target(parent, channel, id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | if (!starget) |
| 1533 | return; |
| 1534 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | if (lun != SCAN_WILD_CARD) { |
| 1536 | /* |
| 1537 | * Scan for a specific host/chan/id/lun. |
| 1538 | */ |
| 1539 | scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL); |
| 1540 | goto out_reap; |
| 1541 | } |
| 1542 | |
| 1543 | /* |
| 1544 | * Scan LUN 0, if there is some response, scan further. Ideally, we |
| 1545 | * would not configure LUN 0 until all LUNs are scanned. |
| 1546 | */ |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1547 | res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL); |
| 1548 | if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) { |
| 1549 | if (scsi_report_lun_scan(starget, bflags, rescan) != 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | /* |
| 1551 | * The REPORT LUN did not scan the target, |
| 1552 | * do a sequential scan. |
| 1553 | */ |
| 1554 | scsi_sequential_lun_scan(starget, bflags, |
Kurt Garloff | 4186ab1 | 2006-04-03 15:16:48 +0200 | [diff] [blame] | 1555 | starget->scsi_level, rescan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
| 1558 | out_reap: |
| 1559 | /* now determine if the target has any children at all |
| 1560 | * and if not, nuke it */ |
| 1561 | scsi_target_reap(starget); |
| 1562 | |
| 1563 | put_device(&starget->dev); |
| 1564 | } |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1565 | |
| 1566 | /** |
| 1567 | * scsi_scan_target - scan a target id, possibly including all LUNs on the |
| 1568 | * target. |
| 1569 | * @parent: host to scan |
| 1570 | * @channel: channel to scan |
| 1571 | * @id: target id to scan |
| 1572 | * @lun: Specific LUN to scan or SCAN_WILD_CARD |
| 1573 | * @rescan: passed to LUN scanning routines |
| 1574 | * |
| 1575 | * Description: |
| 1576 | * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0, |
| 1577 | * and possibly all LUNs on the target id. |
| 1578 | * |
| 1579 | * First try a REPORT LUN scan, if that does not scan the target, do a |
| 1580 | * sequential scan of LUNs on the target id. |
| 1581 | **/ |
| 1582 | void scsi_scan_target(struct device *parent, unsigned int channel, |
| 1583 | unsigned int id, unsigned int lun, int rescan) |
| 1584 | { |
| 1585 | struct Scsi_Host *shost = dev_to_shost(parent); |
| 1586 | |
Matthew Wilcox | 93b45af | 2006-11-22 13:24:53 -0700 | [diff] [blame] | 1587 | if (strncmp(scsi_scan_type, "none", 4) == 0) |
| 1588 | return; |
| 1589 | |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1590 | mutex_lock(&shost->scan_mutex); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1591 | if (!shost->async_scan) |
| 1592 | scsi_complete_async_scans(); |
| 1593 | |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1594 | if (scsi_host_scan_allowed(shost)) |
| 1595 | __scsi_scan_target(parent, channel, id, lun, rescan); |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1596 | mutex_unlock(&shost->scan_mutex); |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1597 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | EXPORT_SYMBOL(scsi_scan_target); |
| 1599 | |
| 1600 | static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel, |
| 1601 | unsigned int id, unsigned int lun, int rescan) |
| 1602 | { |
| 1603 | uint order_id; |
| 1604 | |
| 1605 | if (id == SCAN_WILD_CARD) |
| 1606 | for (id = 0; id < shost->max_id; ++id) { |
| 1607 | /* |
| 1608 | * XXX adapter drivers when possible (FCP, iSCSI) |
| 1609 | * could modify max_id to match the current max, |
| 1610 | * not the absolute max. |
| 1611 | * |
| 1612 | * XXX add a shost id iterator, so for example, |
| 1613 | * the FC ID can be the same as a target id |
| 1614 | * without a huge overhead of sparse id's. |
| 1615 | */ |
| 1616 | if (shost->reverse_ordering) |
| 1617 | /* |
| 1618 | * Scan from high to low id. |
| 1619 | */ |
| 1620 | order_id = shost->max_id - id - 1; |
| 1621 | else |
| 1622 | order_id = id; |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1623 | __scsi_scan_target(&shost->shost_gendev, channel, |
| 1624 | order_id, lun, rescan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | } |
| 1626 | else |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1627 | __scsi_scan_target(&shost->shost_gendev, channel, |
| 1628 | id, lun, rescan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | } |
| 1630 | |
| 1631 | int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel, |
| 1632 | unsigned int id, unsigned int lun, int rescan) |
| 1633 | { |
Jeff Garzik | 3bf743e | 2005-10-24 18:04:06 -0400 | [diff] [blame] | 1634 | SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost, |
| 1635 | "%s: <%u:%u:%u>\n", |
| 1636 | __FUNCTION__, channel, id, lun)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1637 | |
| 1638 | if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) || |
Amit Arora | 091686d | 2006-05-19 16:14:50 -0700 | [diff] [blame] | 1639 | ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun))) |
| 1641 | return -EINVAL; |
| 1642 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1643 | mutex_lock(&shost->scan_mutex); |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1644 | if (!shost->async_scan) |
| 1645 | scsi_complete_async_scans(); |
| 1646 | |
Mike Anderson | 82f2946 | 2005-06-16 11:14:33 -0700 | [diff] [blame] | 1647 | if (scsi_host_scan_allowed(shost)) { |
| 1648 | if (channel == SCAN_WILD_CARD) |
| 1649 | for (channel = 0; channel <= shost->max_channel; |
| 1650 | channel++) |
| 1651 | scsi_scan_channel(shost, channel, id, lun, |
| 1652 | rescan); |
| 1653 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | scsi_scan_channel(shost, channel, id, lun, rescan); |
Mike Anderson | 82f2946 | 2005-06-16 11:14:33 -0700 | [diff] [blame] | 1655 | } |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1656 | mutex_unlock(&shost->scan_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1657 | |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1661 | static void scsi_sysfs_add_devices(struct Scsi_Host *shost) |
| 1662 | { |
| 1663 | struct scsi_device *sdev; |
| 1664 | shost_for_each_device(sdev, shost) { |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1665 | if (!scsi_host_scan_allowed(shost) || |
| 1666 | scsi_sysfs_add_sdev(sdev) != 0) |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1667 | scsi_destroy_sdev(sdev); |
| 1668 | } |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * scsi_prep_async_scan - prepare for an async scan |
| 1673 | * @shost: the host which will be scanned |
| 1674 | * Returns: a cookie to be passed to scsi_finish_async_scan() |
| 1675 | * |
| 1676 | * Tells the midlayer this host is going to do an asynchronous scan. |
| 1677 | * It reserves the host's position in the scanning list and ensures |
| 1678 | * that other asynchronous scans started after this one won't affect the |
| 1679 | * ordering of the discovered devices. |
| 1680 | */ |
Matthew Wilcox | 1aa8fab | 2006-11-22 13:24:54 -0700 | [diff] [blame] | 1681 | static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost) |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1682 | { |
| 1683 | struct async_scan_data *data; |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1684 | unsigned long flags; |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1685 | |
| 1686 | if (strncmp(scsi_scan_type, "sync", 4) == 0) |
| 1687 | return NULL; |
| 1688 | |
| 1689 | if (shost->async_scan) { |
| 1690 | printk("%s called twice for host %d", __FUNCTION__, |
| 1691 | shost->host_no); |
| 1692 | dump_stack(); |
| 1693 | return NULL; |
| 1694 | } |
| 1695 | |
| 1696 | data = kmalloc(sizeof(*data), GFP_KERNEL); |
| 1697 | if (!data) |
| 1698 | goto err; |
| 1699 | data->shost = scsi_host_get(shost); |
| 1700 | if (!data->shost) |
| 1701 | goto err; |
| 1702 | init_completion(&data->prev_finished); |
| 1703 | |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1704 | mutex_lock(&shost->scan_mutex); |
| 1705 | spin_lock_irqsave(shost->host_lock, flags); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1706 | shost->async_scan = 1; |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1707 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1708 | mutex_unlock(&shost->scan_mutex); |
| 1709 | |
| 1710 | spin_lock(&async_scan_lock); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1711 | if (list_empty(&scanning_hosts)) |
| 1712 | complete(&data->prev_finished); |
| 1713 | list_add_tail(&data->list, &scanning_hosts); |
| 1714 | spin_unlock(&async_scan_lock); |
| 1715 | |
| 1716 | return data; |
| 1717 | |
| 1718 | err: |
| 1719 | kfree(data); |
| 1720 | return NULL; |
| 1721 | } |
| 1722 | |
| 1723 | /** |
| 1724 | * scsi_finish_async_scan - asynchronous scan has finished |
| 1725 | * @data: cookie returned from earlier call to scsi_prep_async_scan() |
| 1726 | * |
| 1727 | * All the devices currently attached to this host have been found. |
| 1728 | * This function announces all the devices it has found to the rest |
| 1729 | * of the system. |
| 1730 | */ |
Matthew Wilcox | 1aa8fab | 2006-11-22 13:24:54 -0700 | [diff] [blame] | 1731 | static void scsi_finish_async_scan(struct async_scan_data *data) |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1732 | { |
| 1733 | struct Scsi_Host *shost; |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1734 | unsigned long flags; |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1735 | |
| 1736 | if (!data) |
| 1737 | return; |
| 1738 | |
| 1739 | shost = data->shost; |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1740 | |
| 1741 | mutex_lock(&shost->scan_mutex); |
| 1742 | |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1743 | if (!shost->async_scan) { |
| 1744 | printk("%s called twice for host %d", __FUNCTION__, |
| 1745 | shost->host_no); |
| 1746 | dump_stack(); |
| 1747 | return; |
| 1748 | } |
| 1749 | |
| 1750 | wait_for_completion(&data->prev_finished); |
| 1751 | |
| 1752 | scsi_sysfs_add_devices(shost); |
| 1753 | |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1754 | spin_lock_irqsave(shost->host_lock, flags); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1755 | shost->async_scan = 0; |
Matthew Wilcox | 6b7f123 | 2007-06-26 15:18:51 -0600 | [diff] [blame] | 1756 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1757 | |
| 1758 | mutex_unlock(&shost->scan_mutex); |
| 1759 | |
| 1760 | spin_lock(&async_scan_lock); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1761 | list_del(&data->list); |
| 1762 | if (!list_empty(&scanning_hosts)) { |
| 1763 | struct async_scan_data *next = list_entry(scanning_hosts.next, |
| 1764 | struct async_scan_data, list); |
| 1765 | complete(&next->prev_finished); |
| 1766 | } |
| 1767 | spin_unlock(&async_scan_lock); |
| 1768 | |
| 1769 | scsi_host_put(shost); |
| 1770 | kfree(data); |
| 1771 | } |
| 1772 | |
Matthew Wilcox | 1aa8fab | 2006-11-22 13:24:54 -0700 | [diff] [blame] | 1773 | static void do_scsi_scan_host(struct Scsi_Host *shost) |
| 1774 | { |
| 1775 | if (shost->hostt->scan_finished) { |
| 1776 | unsigned long start = jiffies; |
| 1777 | if (shost->hostt->scan_start) |
| 1778 | shost->hostt->scan_start(shost); |
| 1779 | |
| 1780 | while (!shost->hostt->scan_finished(shost, jiffies - start)) |
| 1781 | msleep(10); |
| 1782 | } else { |
| 1783 | scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD, |
| 1784 | SCAN_WILD_CARD, 0); |
| 1785 | } |
| 1786 | } |
| 1787 | |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1788 | static int do_scan_async(void *_data) |
| 1789 | { |
| 1790 | struct async_scan_data *data = _data; |
Matthew Wilcox | 1aa8fab | 2006-11-22 13:24:54 -0700 | [diff] [blame] | 1791 | do_scsi_scan_host(data->shost); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1792 | scsi_finish_async_scan(data); |
| 1793 | return 0; |
| 1794 | } |
| 1795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | /** |
| 1797 | * scsi_scan_host - scan the given adapter |
| 1798 | * @shost: adapter to scan |
| 1799 | **/ |
| 1800 | void scsi_scan_host(struct Scsi_Host *shost) |
| 1801 | { |
Matthew Wilcox | a57b1fc | 2007-08-20 09:18:48 -0600 | [diff] [blame] | 1802 | struct task_struct *p; |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1803 | struct async_scan_data *data; |
| 1804 | |
| 1805 | if (strncmp(scsi_scan_type, "none", 4) == 0) |
| 1806 | return; |
| 1807 | |
| 1808 | data = scsi_prep_async_scan(shost); |
| 1809 | if (!data) { |
Matthew Wilcox | 1aa8fab | 2006-11-22 13:24:54 -0700 | [diff] [blame] | 1810 | do_scsi_scan_host(shost); |
Matthew Wilcox | 3e082a9 | 2006-09-28 15:19:20 -0600 | [diff] [blame] | 1811 | return; |
| 1812 | } |
Matthew Wilcox | 1aa8fab | 2006-11-22 13:24:54 -0700 | [diff] [blame] | 1813 | |
Matthew Wilcox | a57b1fc | 2007-08-20 09:18:48 -0600 | [diff] [blame] | 1814 | p = kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no); |
| 1815 | if (unlikely(IS_ERR(p))) |
| 1816 | do_scan_async(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | } |
| 1818 | EXPORT_SYMBOL(scsi_scan_host); |
| 1819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | void scsi_forget_host(struct Scsi_Host *shost) |
| 1821 | { |
Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 1822 | struct scsi_device *sdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | unsigned long flags; |
| 1824 | |
Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 1825 | restart: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | spin_lock_irqsave(shost->host_lock, flags); |
Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 1827 | list_for_each_entry(sdev, &shost->__devices, siblings) { |
| 1828 | if (sdev->sdev_state == SDEV_DEL) |
| 1829 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | spin_unlock_irqrestore(shost->host_lock, flags); |
Alan Stern | a64358d | 2005-07-26 10:27:10 -0400 | [diff] [blame] | 1831 | __scsi_remove_device(sdev); |
| 1832 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | } |
| 1834 | spin_unlock_irqrestore(shost->host_lock, flags); |
| 1835 | } |
| 1836 | |
| 1837 | /* |
| 1838 | * Function: scsi_get_host_dev() |
| 1839 | * |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 1840 | * Purpose: Create a scsi_device that points to the host adapter itself. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | * |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 1842 | * Arguments: SHpnt - Host that needs a scsi_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1843 | * |
| 1844 | * Lock status: None assumed. |
| 1845 | * |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 1846 | * Returns: The scsi_device or NULL |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | * |
| 1848 | * Notes: |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 1849 | * Attach a single scsi_device to the Scsi_Host - this should |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | * be made to look like a "pseudo-device" that points to the |
| 1851 | * HA itself. |
| 1852 | * |
| 1853 | * Note - this device is not accessible from any high-level |
| 1854 | * drivers (including generics), which is probably not |
| 1855 | * optimal. We can add hooks later to attach |
| 1856 | */ |
| 1857 | struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost) |
| 1858 | { |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1859 | struct scsi_device *sdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | struct scsi_target *starget; |
| 1861 | |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1862 | mutex_lock(&shost->scan_mutex); |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1863 | if (!scsi_host_scan_allowed(shost)) |
| 1864 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id); |
| 1866 | if (!starget) |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1867 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | |
| 1869 | sdev = scsi_alloc_sdev(starget, 0, NULL); |
| 1870 | if (sdev) { |
| 1871 | sdev->sdev_gendev.parent = get_device(&starget->dev); |
| 1872 | sdev->borken = 0; |
James Bottomley | 884d25c | 2006-09-05 16:26:41 -0500 | [diff] [blame] | 1873 | } else |
| 1874 | scsi_target_reap(starget); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | put_device(&starget->dev); |
Alan Stern | e517d31 | 2005-07-26 10:18:45 -0400 | [diff] [blame] | 1876 | out: |
Arjan van de Ven | 0b95067 | 2006-01-11 13:16:10 +0100 | [diff] [blame] | 1877 | mutex_unlock(&shost->scan_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | return sdev; |
| 1879 | } |
| 1880 | EXPORT_SYMBOL(scsi_get_host_dev); |
| 1881 | |
| 1882 | /* |
| 1883 | * Function: scsi_free_host_dev() |
| 1884 | * |
| 1885 | * Purpose: Free a scsi_device that points to the host adapter itself. |
| 1886 | * |
Christoph Hellwig | f64a181 | 2005-10-31 18:32:08 +0100 | [diff] [blame] | 1887 | * Arguments: SHpnt - Host that needs a scsi_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | * |
| 1889 | * Lock status: None assumed. |
| 1890 | * |
| 1891 | * Returns: Nothing |
| 1892 | * |
| 1893 | * Notes: |
| 1894 | */ |
| 1895 | void scsi_free_host_dev(struct scsi_device *sdev) |
| 1896 | { |
| 1897 | BUG_ON(sdev->id != sdev->host->this_id); |
| 1898 | |
James Bottomley | 6f3a202 | 2005-09-22 20:33:28 -0500 | [diff] [blame] | 1899 | scsi_destroy_sdev(sdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | } |
| 1901 | EXPORT_SYMBOL(scsi_free_host_dev); |
| 1902 | |