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