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