blob: 7e8b2045ccfadb0985c975657b7f467e86dd81a4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Hellwigf64a1812005-10-31 18:32:08 +010012 * device attached, a scsi_device is allocated and setup for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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 Hellwigf64a1812005-10-31 18:32:08 +010020 * scsi_device for it.
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 *
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 Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/init.h>
31#include <linux/blkdev.h>
Matthew Wilcox3e082a92006-09-28 15:19:20 -060032#include <linux/delay.h>
33#include <linux/kthread.h>
34#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <scsi/scsi.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020037#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <scsi/scsi_device.h>
39#include <scsi/scsi_driver.h>
40#include <scsi/scsi_devinfo.h>
41#include <scsi/scsi_host.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#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. Day405ae7d2007-02-17 19:13:42 +010057 * Prefix values for the SCSI id's (stored in sysfs name field)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
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 Ven0ad78202005-11-28 16:22:25 +010078static const char *scsi_null_device_strs = "nullnullnullnull";
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define MAX_SCSI_LUNS 512
81
82#ifdef CONFIG_SCSI_MULTI_LUN
83static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
84#else
85static unsigned int max_scsi_luns = 1;
86#endif
87
Masatake YAMATO10f4b892007-09-19 22:59:16 +090088module_param_named(max_luns, max_scsi_luns, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089MODULE_PARM_DESC(max_luns,
90 "last scsi LUN (should be between 1 and 2^32-1)");
91
Matthew Wilcox21db1882006-11-22 13:24:52 -070092#ifdef CONFIG_SCSI_SCAN_ASYNC
93#define SCSI_SCAN_TYPE_DEFAULT "async"
94#else
95#define SCSI_SCAN_TYPE_DEFAULT "sync"
96#endif
97
98static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT;
Matthew Wilcox3e082a92006-09-28 15:19:20 -060099
100module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
101MODULE_PARM_DESC(scan, "sync, async or none");
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
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 */
110static unsigned int max_scsi_report_luns = 511;
111
Masatake YAMATO10f4b892007-09-19 22:59:16 +0900112module_param_named(max_report_luns, max_scsi_report_luns, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113MODULE_PARM_DESC(max_report_luns,
114 "REPORT LUNS maximum number of LUNS received (should be"
115 " between 1 and 16384)");
116
117static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
118
Masatake YAMATO10f4b892007-09-19 22:59:16 +0900119module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120MODULE_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 Wilcox6b7f1232007-06-26 15:18:51 -0600124/* This lock protects only this list */
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600125static DEFINE_SPINLOCK(async_scan_lock);
126static LIST_HEAD(scanning_hosts);
127
128struct 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 Wilcox8bcc2412006-12-07 19:29:27 -0700137 * 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 Wilcox3e082a92006-09-28 15:19:20 -0600141 */
142int 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 Wilcox8bcc2412006-12-07 19:29:27 -0700173 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 Wilcox3e082a92006-09-28 15:19:20 -0600178 done:
179 spin_unlock(&async_scan_lock);
180
181 kfree(data);
182 return 0;
183}
184
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600185/* Only exported for the benefit of scsi_wait_scan */
186EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600187
Hugh Dickinsf2f027c2007-05-23 14:41:42 -0700188#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 */
194late_initcall(scsi_complete_async_scans);
195#endif
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/**
198 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
James Bottomley39216032005-06-15 18:48:29 -0500199 * @sdev: scsi device to send command to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * @result: area to store the result of the MODE SENSE
201 *
202 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500203 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 * Called for BLIST_KEY devices.
205 **/
James Bottomley39216032005-06-15 18:48:29 -0500206static void scsi_unlock_floptical(struct scsi_device *sdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 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 Bottomley39216032005-06-15 18:48:29 -0500216 scsi_cmd[4] = 0x2a; /* size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 scsi_cmd[5] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500218 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
219 SCSI_TIMEOUT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * scsi_alloc_sdev - allocate and setup a scsi_Device
Rob Landleyeb448202007-11-03 13:30:39 -0500224 * @starget: which target to allocate a &scsi_device for
225 * @lun: which lun
226 * @hostdata: usually NULL and set by ->slave_alloc instead
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 *
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 **/
236static 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 Garzika341cd02007-10-29 17:15:22 -0400242 extern void scsi_evt_thread(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Jes Sorensen24669f752006-01-16 10:31:18 -0500244 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 GFP_ATOMIC);
246 if (!sdev)
247 goto out;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 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 Garzika341cd02007-10-29 17:15:22 -0400261 INIT_LIST_HEAD(&sdev->event_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 spin_lock_init(&sdev->list_lock);
Jeff Garzika341cd02007-10-29 17:15:22 -0400263 INIT_WORK(&sdev->event_work, scsi_evt_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
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 Torvalds1da177e2005-04-16 15:20:36 -0700287 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 Jones93f560892006-03-09 10:21:27 -0500292 kfree(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 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 Torvalds1da177e2005-04-16 15:20:36 -0700307 */
308 if (ret == -ENXIO)
309 display_failure_msg = 0;
310 goto out_device_destroy;
311 }
312 }
313
314 return sdev;
315
316out_device_destroy:
317 transport_destroy_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 put_device(&sdev->sdev_gendev);
319out:
320 if (display_failure_msg)
321 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
322 return NULL;
323}
324
325static 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 Bottomleya283bd32005-05-24 12:06:38 -0500329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 kfree(starget);
331 put_device(parent);
332}
333
334int scsi_is_target_device(const struct device *dev)
335{
336 return dev->release == scsi_target_dev_release;
337}
338EXPORT_SYMBOL(scsi_is_target_device);
339
340static 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 Bottomley884d25c2006-09-05 16:26:41 -0500361/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700373static 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.Com5c44cd22005-06-10 22:24:30 -0400381 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 struct scsi_target *found_target;
Brian King32f95792006-02-22 14:28:24 -0600383 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Jes Sorensen24669f752006-01-16 10:31:18 -0500385 starget = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (!starget) {
387 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
388 return NULL;
389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 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 Bottomleyffedb452006-02-23 14:27:18 -0600401 starget->state = STARGET_RUNNING;
Alan Stern7c9d6f12007-01-08 11:12:32 -0500402 starget->scsi_level = SCSI_2;
James Bottomleyffedb452006-02-23 14:27:18 -0600403 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 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 Bottomleya283bd32005-05-24 12:06:38 -0500413 transport_setup_device(dev);
Brian King32f95792006-02-22 14:28:24 -0600414 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 Bottomleya283bd32005-05-24 12:06:38 -0500425 transport_add_device(dev);
426 if (shost->hostt->target_alloc) {
Brian King32f95792006-02-22 14:28:24 -0600427 error = shost->hostt->target_alloc(starget);
James Bottomleya283bd32005-05-24 12:06:38 -0500428
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 Bottomley884d25c2006-09-05 16:26:41 -0500439 get_device(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return starget;
442
443 found:
444 found_target->reap_ref++;
445 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600446 if (found_target->state != STARGET_DEL) {
James Bottomley884d25c2006-09-05 16:26:41 -0500447 put_device(parent);
James Bottomleyffedb452006-02-23 14:27:18 -0600448 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 Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
David Howells65f27f32006-11-22 14:55:48 +0000458static void scsi_target_reap_usercontext(struct work_struct *work)
James Bottomley65110b22006-02-14 10:48:46 -0600459{
David Howells65f27f32006-11-22 14:55:48 +0000460 struct scsi_target *starget =
461 container_of(work, struct scsi_target, ew.work);
James Bottomley863a9302005-12-15 20:01:43 -0800462 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
463 unsigned long flags;
464
James Bottomleyffedb452006-02-23 14:27:18 -0600465 transport_remove_device(&starget->dev);
466 device_del(&starget->dev);
467 transport_destroy_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800468 spin_lock_irqsave(shost->host_lock, flags);
Mike Andersona50a5e32006-03-14 11:18:46 -0800469 if (shost->hostt->target_destroy)
470 shost->hostt->target_destroy(starget);
James Bottomleyffedb452006-02-23 14:27:18 -0600471 list_del_init(&starget->siblings);
James Bottomley863a9302005-12-15 20:01:43 -0800472 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600473 put_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/**
477 * scsi_target_reap - check to see if target is in use and destroy if not
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * @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 */
484void scsi_target_reap(struct scsi_target *starget)
485{
James Bottomleyffedb452006-02-23 14:27:18 -0600486 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 Howells65f27f32006-11-22 14:55:48 +0000496 &starget->ew);
James Bottomleyffedb452006-02-23 14:27:18 -0600497 return;
498
499 }
500 spin_unlock_irqrestore(shost->host_lock, flags);
501
502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/**
Alan Sterne5b3cd42006-08-21 15:53:25 -0400506 * 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 **/
519static 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 Torvalds1da177e2005-04-16 15:20:36 -0700532 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
James Bottomley39216032005-06-15 18:48:29 -0500533 * @sdev: scsi_device to probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * @inq_result: area to store the INQUIRY result
James Bottomley39216032005-06-15 18:48:29 -0500535 * @result_len: len of inq_result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * @bflags: store any bflags found here
537 *
538 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500539 * Probe the lun associated with @req using a standard SCSI INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 *
James Bottomley39216032005-06-15 18:48:29 -0500541 * If the INQUIRY is successful, zero is returned and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100543 * are copied to the scsi_device any flags value is stored in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400545static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
James Bottomley39216032005-06-15 18:48:29 -0500546 int result_len, int *bflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
549 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
550 int response_len = 0;
James Bottomley39216032005-06-15 18:48:29 -0500551 int pass, count, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 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 Bottomley9ccfc752005-10-02 11:45:08 -0500564 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 Torvalds1da177e2005-04-16 15:20:36 -0700567
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 Torvalds1da177e2005-04-16 15:20:36 -0700573
574 memset(inq_result, 0, try_inquiry_len);
James Bottomley39216032005-06-15 18:48:29 -0500575
576 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500577 inq_result, try_inquiry_len, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -0500578 HZ / 2 + HZ * scsi_inq_timeout, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
581 "with code 0x%x\n",
James Bottomley39216032005-06-15 18:48:29 -0500582 result ? "failed" : "successful", result));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
James Bottomley39216032005-06-15 18:48:29 -0500584 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /*
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 Bottomley39216032005-06-15 18:48:29 -0500591 if ((driver_byte(result) & DRIVER_SENSE) &&
James Bottomleyea73a9f2005-08-28 11:33:52 -0500592 scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 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 Bottomley39216032005-06-15 18:48:29 -0500603 if (result == 0) {
Alan Sterne5b3cd42006-08-21 15:53:25 -0400604 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 Torvalds1da177e2005-04-16 15:20:36 -0700609 if (response_len > 255)
610 response_len = first_inquiry_len; /* sanity */
611
612 /*
613 * Get any flags for this device.
614 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100615 * XXX add a bflags to scsi_device, and replace the
616 * corresponding bit fields in scsi_device, so bflags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * 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 Bottomley39216032005-06-15 18:48:29 -0500656 if (result)
657 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
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 Sterne423ee32007-02-16 01:46:38 -0800671 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700684 * 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 Bottomley6f3a2022005-09-22 20:33:28 -0500705 sdev->sdev_target->scsi_level = sdev->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
James Bottomley39216032005-06-15 18:48:29 -0500707 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710/**
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100711 * scsi_add_lun - allocate and fully initialze a scsi_device
Matthew Wilcox6d877682007-07-11 12:54:55 -0600712 * @sdev: holds information to be stored in the new scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * @inq_result: holds the result of a previous INQUIRY to the LUN
714 * @bflags: black/white list flag
Matthew Wilcox6d877682007-07-11 12:54:55 -0600715 * @async: 1 if this device is being scanned asynchronously
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 *
717 * Description:
Matthew Wilcox6d877682007-07-11 12:54:55 -0600718 * Initialize the scsi_device @sdev. Optionally set fields based
719 * on values in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 *
721 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100722 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700724 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400725static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600726 int *bflags, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
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 Torvalds1da177e2005-04-16 15:20:36 -0700738
Alan Stern09123d22006-11-10 12:27:57 -0800739 /*
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 Torvalds1da177e2005-04-16 15:20:36 -0700754 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 Wilcox6d877682007-07-11 12:54:55 -0600759 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 Torvalds1da177e2005-04-16 15:20:36 -0700765
Matthew Wilcox6d877682007-07-11 12:54:55 -0600766 switch (sdev->type) {
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600767 case TYPE_RBC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 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 Bottomley4d7db042006-03-31 20:07:45 -0600777 case TYPE_RAID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 sdev->writeable = 1;
779 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 case TYPE_ROM:
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600781 case TYPE_WORM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 sdev->writeable = 0;
783 break;
784 default:
785 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
786 }
787
Matthew Wilcox6d877682007-07-11 12:54:55 -0600788 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 Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
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 Hellwigf64a1812005-10-31 18:32:08 +0100806 * scsi_device for a PQ of 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 *
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 Torvalds1da177e2005-04-16 15:20:36 -0700814 sdev->lockable = sdev->removable;
815 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
816
Matthew Wilcox6d877682007-07-11 12:54:55 -0600817 if (sdev->scsi_level >= SCSI_3 ||
818 (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 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 Bottomley19ac0db2006-08-06 18:15:22 -0500825 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600826 "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 Torvalds1da177e2005-04-16 15:20:36 -0700831 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
832 !(*bflags & BLIST_NOTQ))
833 sdev->tagged_supported = 1;
Matthew Wilcox6d877682007-07-11 12:54:55 -0600834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
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 Wilcox6d877682007-07-11 12:54:55 -0600843 if (*bflags & BLIST_NO_ULD_ATTACH)
844 sdev->no_uld_attach = 1;
845
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 /*
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 Bottomley4d7db042006-03-31 20:07:45 -0600854 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700861 * 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)
868 sdev->single_lun = 1;
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 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 Hellwig93805092006-02-17 12:11:29 +0100896 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 Torvalds1da177e2005-04-16 15:20:36 -0700910
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 Wilcox3e082a92006-09-28 15:19:20 -0600916 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
Alan Sternb24b1032005-07-27 11:43:46 -0700917 return SCSI_SCAN_NO_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
919 return SCSI_SCAN_LUN_PRESENT;
920}
921
James Bottomley6f3a2022005-09-22 20:33:28 -0500922static inline void scsi_destroy_sdev(struct scsi_device *sdev)
923{
Brian King309bd272006-06-27 11:10:43 -0500924 scsi_device_set_state(sdev, SDEV_DEL);
James Bottomley6f3a2022005-09-22 20:33:28 -0500925 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.orgc5f2e642006-04-15 00:30:24 -0700931#ifdef CONFIG_SCSI_LOGGING
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200932/**
Rob Landleyeb448202007-11-03 13:30:39 -0500933 * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200934 * @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.orgc5f2e642006-04-15 00:30:24 -0700939static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200940 unsigned first, unsigned end)
941{
942 unsigned term = 0, idx;
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700943
944 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
945 if (inq[idx+first] > ' ') {
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200946 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.orgc5f2e642006-04-15 00:30:24 -0700955#endif
James Bottomley6f3a2022005-09-22 20:33:28 -0500956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957/**
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 Torvalds1da177e2005-04-16 15:20:36 -0700961 * @bflagsp: store bflags here if not NULL
Rob Landleyeb448202007-11-03 13:30:39 -0500962 * @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 Torvalds1da177e2005-04-16 15:20:36 -0700965 *
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 Hellwigf64a1812005-10-31 18:32:08 +0100971 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
973 * attached at the LUN
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100974 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 **/
976static 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 Torvalds1da177e2005-04-16 15:20:36 -0700982 unsigned char *result;
James Bottomley39216032005-06-15 18:48:29 -0500983 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 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 Bottomley6f3a2022005-09-22 20:33:28 -0500990 sdev = scsi_device_lookup_by_target(starget, lun);
991 if (sdev) {
992 if (rescan || sdev->sdev_state != SDEV_CREATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 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 Bottomley6f3a2022005-09-22 20:33:28 -05001007 scsi_device_put(sdev);
1008 } else
1009 sdev = scsi_alloc_sdev(starget, lun, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!sdev)
1011 goto out;
James Bottomley39216032005-06-15 18:48:29 -05001012
1013 result = kmalloc(result_len, GFP_ATOMIC |
Al Virobc861202005-04-24 12:28:34 -07001014 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!result)
James Bottomley39216032005-06-15 18:48:29 -05001016 goto out_free_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
James Bottomley39216032005-06-15 18:48:29 -05001018 if (scsi_probe_lun(sdev, result, result_len, &bflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 goto out_free_result;
1020
Kurt Garloff4186ab12006-04-03 15:16:48 +02001021 if (bflagsp)
1022 *bflagsp = bflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * result contains valid SCSI INQUIRY data.
1025 */
Kurt Garloff13f7e5a2006-04-03 15:20:08 +02001026 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /*
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 Garloff6c7154c2006-04-03 15:18:35 +02001037 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.orgc5f2e642006-04-15 00:30:24 -07001041 SCSI_LOG_SCAN_BUS(1, {
1042 unsigned char vend[9];
1043 unsigned char mod[17];
1044
1045 sdev_printk(KERN_INFO, sdev,
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001046 "scsi scan: consider passing scsi_mod."
Kurt Garloff3424a652007-01-09 02:28:54 +01001047 "dev_flags=%s:%s:0x240 or 0x1000240\n",
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001048 scsi_inq_str(vend, result, 8, 16),
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001049 scsi_inq_str(mod, result, 16, 32));
1050 });
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001051 }
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 res = SCSI_SCAN_TARGET_PRESENT;
1054 goto out_free_result;
1055 }
1056
Alan Stern1bfc5d92006-02-09 15:26:18 -05001057 /*
dave wysochanski84961f22006-08-09 14:56:32 -04001058 * 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 Stern1bfc5d92006-02-09 15:26:18 -05001075 */
dave wysochanski84961f22006-08-09 14:56:32 -04001076 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1077 (result[0] & 0x1f) == 0x1f) {
Alan Stern1bfc5d92006-02-09 15:26:18 -05001078 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 Wilcox3e082a92006-09-28 15:19:20 -06001085 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (res == SCSI_SCAN_LUN_PRESENT) {
1087 if (bflags & BLIST_KEY) {
1088 sdev->lockable = 0;
James Bottomley39216032005-06-15 18:48:29 -05001089 scsi_unlock_floptical(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 out_free_result:
1094 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 out_free_sdev:
1096 if (res == SCSI_SCAN_LUN_PRESENT) {
1097 if (sdevp) {
Alan Sternb70d37b2005-07-26 10:30:40 -04001098 if (scsi_device_get(sdev) == 0) {
1099 *sdevp = sdev;
1100 } else {
1101 __scsi_remove_device(sdev);
1102 res = SCSI_SCAN_NO_RESPONSE;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001105 } else
1106 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 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 Landleyeb448202007-11-03 13:30:39 -05001115 * @scsi_level: Which version of the standard does this device adhere to
1116 * @rescan: passed to scsi_probe_add_lun()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 *
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 **/
1125static void scsi_sequential_lun_scan(struct scsi_target *starget,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001126 int bflags, int scsi_level, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
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 Torvalds1da177e2005-04-16 15:20:36 -07001147 * 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 Schmitt462b7852007-06-19 10:25:30 +02001212int scsilun_to_int(struct scsi_lun *scsilun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
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 Schmitt462b7852007-06-19 10:25:30 +02001223EXPORT_SYMBOL(scsilun_to_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225/**
James.Smart@Emulex.Com2f4701d2005-07-13 22:05:03 -04001226 * int_to_scsilun: reverts an int into a scsi_lun
Rob Landleyeb448202007-11-03 13:30:39 -05001227 * @lun: integer to be reverted
James.Smart@Emulex.Com2f4701d2005-07-13 22:05:03 -04001228 * @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 **/
1243void 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}
1255EXPORT_SYMBOL(int_to_scsilun);
1256
1257/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
Rob Landleyeb448202007-11-03 13:30:39 -05001259 * @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 Torvalds1da177e2005-04-16 15:20:36 -07001262 *
1263 * Description:
Rob Landleyeb448202007-11-03 13:30:39 -05001264 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001266 *
Rob Landleyeb448202007-11-03 13:30:39 -05001267 * 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 Torvalds1da177e2005-04-16 15:20:36 -07001271 *
1272 * Return:
1273 * 0: scan completed (or no memory, so further scanning is futile)
Rob Landleyeb448202007-11-03 13:30:39 -05001274 * 1: could not scan with REPORT LUN
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 **/
James Bottomley6f3a2022005-09-22 20:33:28 -05001276static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 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 Bottomley39216032005-06-15 18:48:29 -05001285 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 struct scsi_lun *lunp, *lun_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 u8 *data;
1288 struct scsi_sense_hdr sshdr;
James Bottomley6f3a2022005-09-22 20:33:28 -05001289 struct scsi_device *sdev;
1290 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
Alan Stern2ef89192005-11-08 15:51:55 -05001291 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
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 Bottomley4d7db042006-03-31 20:07:45 -06001298 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 Torvalds1da177e2005-04-16 15:20:36 -07001305 return 1;
1306 if (bflags & BLIST_NOLUN)
1307 return 0;
1308
James Bottomley6f3a2022005-09-22 20:33:28 -05001309 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 Torvalds1da177e2005-04-16 15:20:36 -07001317 sprintf(devname, "host %d channel %d id %d",
James Bottomley6f3a2022005-09-22 20:33:28 -05001318 shost->host_no, sdev->channel, sdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
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 Bottomley6f3a2022005-09-22 20:33:28 -05001333 if (!lun_data) {
1334 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
James Bottomley39216032005-06-15 18:48:29 -05001335 goto out;
James Bottomley6f3a2022005-09-22 20:33:28 -05001336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
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 Torvalds1da177e2005-04-16 15:20:36 -07001355
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 Bottomley39216032005-06-15 18:48:29 -05001370
James Bottomley39216032005-06-15 18:48:29 -05001371 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001372 lun_data, length, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -05001373 SCSI_TIMEOUT + 4 * HZ, 3);
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
James Bottomley39216032005-06-15 18:48:29 -05001376 " %s (try %d) result 0x%x\n", result
1377 ? "failed" : "successful", retries, result));
1378 if (result == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 break;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001380 else if (scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 if (sshdr.sense_key != UNIT_ATTENTION)
1382 break;
1383 }
1384 }
1385
James Bottomley39216032005-06-15 18:48:29 -05001386 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 /*
1388 * The device probably does not support a REPORT LUN command
1389 */
Alan Stern2ef89192005-11-08 15:51:55 -05001390 ret = 1;
1391 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
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 Garzik3bf743e2005-10-24 18:04:06 -04001410 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1411 "scsi scan: REPORT LUN scan\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412
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 Torvalds1da177e2005-04-16 15:20:36 -07001437 } 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 Garzik3bf743e2005-10-24 18:04:06 -04001450 sdev_printk(KERN_ERR, sdev,
1451 "Unexpected response"
1452 " from lun %d while scanning, scan"
1453 " aborted\n", lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 break;
1455 }
1456 }
1457 }
1458
Alan Stern2ef89192005-11-08 15:51:55 -05001459 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 kfree(lun_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 out:
James Bottomley6f3a2022005-09-22 20:33:28 -05001462 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 Stern2ef89192005-11-08 15:51:55 -05001468 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1472 uint id, uint lun, void *hostdata)
1473{
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001474 struct scsi_device *sdev = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 struct device *parent = &shost->shost_gendev;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001476 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Matthew Wilcox938e2ac2007-01-15 18:07:09 -07001478 if (strncmp(scsi_scan_type, "none", 4) == 0)
1479 return ERR_PTR(-ENODEV);
1480
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001481 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 if (!starget)
1483 return ERR_PTR(-ENOMEM);
1484
Arjan van de Ven0b950672006-01-11 13:16:10 +01001485 mutex_lock(&shost->scan_mutex);
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001486 if (!shost->async_scan)
1487 scsi_complete_async_scans();
1488
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001489 if (scsi_host_scan_allowed(shost))
1490 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001491 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 scsi_target_reap(starget);
1493 put_device(&starget->dev);
1494
1495 return sdev;
1496}
1497EXPORT_SYMBOL(__scsi_add_device);
1498
James Bottomley146f7262005-09-10 12:44:09 -05001499int scsi_add_device(struct Scsi_Host *host, uint channel,
1500 uint target, uint lun)
1501{
1502 struct scsi_device *sdev =
1503 __scsi_add_device(host, channel, target, lun, NULL);
1504 if (IS_ERR(sdev))
1505 return PTR_ERR(sdev);
1506
1507 scsi_device_put(sdev);
1508 return 0;
1509}
1510EXPORT_SYMBOL(scsi_add_device);
1511
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512void scsi_rescan_device(struct device *dev)
1513{
1514 struct scsi_driver *drv;
1515
1516 if (!dev->driver)
1517 return;
1518
1519 drv = to_scsi_driver(dev->driver);
1520 if (try_module_get(drv->owner)) {
1521 if (drv->rescan)
1522 drv->rescan(dev);
1523 module_put(drv->owner);
1524 }
1525}
1526EXPORT_SYMBOL(scsi_rescan_device);
1527
Alan Sterne517d312005-07-26 10:18:45 -04001528static void __scsi_scan_target(struct device *parent, unsigned int channel,
1529 unsigned int id, unsigned int lun, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
1531 struct Scsi_Host *shost = dev_to_shost(parent);
1532 int bflags = 0;
1533 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct scsi_target *starget;
1535
1536 if (shost->this_id == id)
1537 /*
1538 * Don't scan the host adapter
1539 */
1540 return;
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 if (!starget)
1544 return;
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (lun != SCAN_WILD_CARD) {
1547 /*
1548 * Scan for a specific host/chan/id/lun.
1549 */
1550 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1551 goto out_reap;
1552 }
1553
1554 /*
1555 * Scan LUN 0, if there is some response, scan further. Ideally, we
1556 * would not configure LUN 0 until all LUNs are scanned.
1557 */
James Bottomley6f3a2022005-09-22 20:33:28 -05001558 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1559 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1560 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /*
1562 * The REPORT LUN did not scan the target,
1563 * do a sequential scan.
1564 */
1565 scsi_sequential_lun_scan(starget, bflags,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001566 starget->scsi_level, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
1569 out_reap:
1570 /* now determine if the target has any children at all
1571 * and if not, nuke it */
1572 scsi_target_reap(starget);
1573
1574 put_device(&starget->dev);
1575}
Alan Sterne517d312005-07-26 10:18:45 -04001576
1577/**
1578 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1579 * target.
1580 * @parent: host to scan
1581 * @channel: channel to scan
1582 * @id: target id to scan
1583 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1584 * @rescan: passed to LUN scanning routines
1585 *
1586 * Description:
1587 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1588 * and possibly all LUNs on the target id.
1589 *
1590 * First try a REPORT LUN scan, if that does not scan the target, do a
1591 * sequential scan of LUNs on the target id.
1592 **/
1593void scsi_scan_target(struct device *parent, unsigned int channel,
1594 unsigned int id, unsigned int lun, int rescan)
1595{
1596 struct Scsi_Host *shost = dev_to_shost(parent);
1597
Matthew Wilcox93b45af2006-11-22 13:24:53 -07001598 if (strncmp(scsi_scan_type, "none", 4) == 0)
1599 return;
1600
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001601 mutex_lock(&shost->scan_mutex);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001602 if (!shost->async_scan)
1603 scsi_complete_async_scans();
1604
Alan Sterne517d312005-07-26 10:18:45 -04001605 if (scsi_host_scan_allowed(shost))
1606 __scsi_scan_target(parent, channel, id, lun, rescan);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001607 mutex_unlock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609EXPORT_SYMBOL(scsi_scan_target);
1610
1611static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1612 unsigned int id, unsigned int lun, int rescan)
1613{
1614 uint order_id;
1615
1616 if (id == SCAN_WILD_CARD)
1617 for (id = 0; id < shost->max_id; ++id) {
1618 /*
1619 * XXX adapter drivers when possible (FCP, iSCSI)
1620 * could modify max_id to match the current max,
1621 * not the absolute max.
1622 *
1623 * XXX add a shost id iterator, so for example,
1624 * the FC ID can be the same as a target id
1625 * without a huge overhead of sparse id's.
1626 */
1627 if (shost->reverse_ordering)
1628 /*
1629 * Scan from high to low id.
1630 */
1631 order_id = shost->max_id - id - 1;
1632 else
1633 order_id = id;
Alan Sterne517d312005-07-26 10:18:45 -04001634 __scsi_scan_target(&shost->shost_gendev, channel,
1635 order_id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 }
1637 else
Alan Sterne517d312005-07-26 10:18:45 -04001638 __scsi_scan_target(&shost->shost_gendev, channel,
1639 id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640}
1641
1642int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1643 unsigned int id, unsigned int lun, int rescan)
1644{
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001645 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1646 "%s: <%u:%u:%u>\n",
1647 __FUNCTION__, channel, id, lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
1649 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
Amit Arora091686d2006-05-19 16:14:50 -07001650 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1652 return -EINVAL;
1653
Arjan van de Ven0b950672006-01-11 13:16:10 +01001654 mutex_lock(&shost->scan_mutex);
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001655 if (!shost->async_scan)
1656 scsi_complete_async_scans();
1657
Mike Anderson82f29462005-06-16 11:14:33 -07001658 if (scsi_host_scan_allowed(shost)) {
1659 if (channel == SCAN_WILD_CARD)
1660 for (channel = 0; channel <= shost->max_channel;
1661 channel++)
1662 scsi_scan_channel(shost, channel, id, lun,
1663 rescan);
1664 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 scsi_scan_channel(shost, channel, id, lun, rescan);
Mike Anderson82f29462005-06-16 11:14:33 -07001666 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001667 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 return 0;
1670}
1671
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001672static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1673{
1674 struct scsi_device *sdev;
1675 shost_for_each_device(sdev, shost) {
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001676 if (!scsi_host_scan_allowed(shost) ||
1677 scsi_sysfs_add_sdev(sdev) != 0)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001678 scsi_destroy_sdev(sdev);
1679 }
1680}
1681
1682/**
1683 * scsi_prep_async_scan - prepare for an async scan
1684 * @shost: the host which will be scanned
1685 * Returns: a cookie to be passed to scsi_finish_async_scan()
1686 *
1687 * Tells the midlayer this host is going to do an asynchronous scan.
1688 * It reserves the host's position in the scanning list and ensures
1689 * that other asynchronous scans started after this one won't affect the
1690 * ordering of the discovered devices.
1691 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001692static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001693{
1694 struct async_scan_data *data;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001695 unsigned long flags;
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001696
1697 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1698 return NULL;
1699
1700 if (shost->async_scan) {
1701 printk("%s called twice for host %d", __FUNCTION__,
1702 shost->host_no);
1703 dump_stack();
1704 return NULL;
1705 }
1706
1707 data = kmalloc(sizeof(*data), GFP_KERNEL);
1708 if (!data)
1709 goto err;
1710 data->shost = scsi_host_get(shost);
1711 if (!data->shost)
1712 goto err;
1713 init_completion(&data->prev_finished);
1714
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001715 mutex_lock(&shost->scan_mutex);
1716 spin_lock_irqsave(shost->host_lock, flags);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001717 shost->async_scan = 1;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001718 spin_unlock_irqrestore(shost->host_lock, flags);
1719 mutex_unlock(&shost->scan_mutex);
1720
1721 spin_lock(&async_scan_lock);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001722 if (list_empty(&scanning_hosts))
1723 complete(&data->prev_finished);
1724 list_add_tail(&data->list, &scanning_hosts);
1725 spin_unlock(&async_scan_lock);
1726
1727 return data;
1728
1729 err:
1730 kfree(data);
1731 return NULL;
1732}
1733
1734/**
1735 * scsi_finish_async_scan - asynchronous scan has finished
1736 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1737 *
1738 * All the devices currently attached to this host have been found.
1739 * This function announces all the devices it has found to the rest
1740 * of the system.
1741 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001742static void scsi_finish_async_scan(struct async_scan_data *data)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001743{
1744 struct Scsi_Host *shost;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001745 unsigned long flags;
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001746
1747 if (!data)
1748 return;
1749
1750 shost = data->shost;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001751
1752 mutex_lock(&shost->scan_mutex);
1753
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001754 if (!shost->async_scan) {
1755 printk("%s called twice for host %d", __FUNCTION__,
1756 shost->host_no);
1757 dump_stack();
1758 return;
1759 }
1760
1761 wait_for_completion(&data->prev_finished);
1762
1763 scsi_sysfs_add_devices(shost);
1764
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001765 spin_lock_irqsave(shost->host_lock, flags);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001766 shost->async_scan = 0;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001767 spin_unlock_irqrestore(shost->host_lock, flags);
1768
1769 mutex_unlock(&shost->scan_mutex);
1770
1771 spin_lock(&async_scan_lock);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001772 list_del(&data->list);
1773 if (!list_empty(&scanning_hosts)) {
1774 struct async_scan_data *next = list_entry(scanning_hosts.next,
1775 struct async_scan_data, list);
1776 complete(&next->prev_finished);
1777 }
1778 spin_unlock(&async_scan_lock);
1779
1780 scsi_host_put(shost);
1781 kfree(data);
1782}
1783
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001784static void do_scsi_scan_host(struct Scsi_Host *shost)
1785{
1786 if (shost->hostt->scan_finished) {
1787 unsigned long start = jiffies;
1788 if (shost->hostt->scan_start)
1789 shost->hostt->scan_start(shost);
1790
1791 while (!shost->hostt->scan_finished(shost, jiffies - start))
1792 msleep(10);
1793 } else {
1794 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1795 SCAN_WILD_CARD, 0);
1796 }
1797}
1798
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001799static int do_scan_async(void *_data)
1800{
1801 struct async_scan_data *data = _data;
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001802 do_scsi_scan_host(data->shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001803 scsi_finish_async_scan(data);
1804 return 0;
1805}
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807/**
1808 * scsi_scan_host - scan the given adapter
1809 * @shost: adapter to scan
1810 **/
1811void scsi_scan_host(struct Scsi_Host *shost)
1812{
Matthew Wilcoxa57b1fc2007-08-20 09:18:48 -06001813 struct task_struct *p;
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001814 struct async_scan_data *data;
1815
1816 if (strncmp(scsi_scan_type, "none", 4) == 0)
1817 return;
1818
1819 data = scsi_prep_async_scan(shost);
1820 if (!data) {
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001821 do_scsi_scan_host(shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001822 return;
1823 }
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001824
Matthew Wilcoxa57b1fc2007-08-20 09:18:48 -06001825 p = kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1826 if (unlikely(IS_ERR(p)))
1827 do_scan_async(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829EXPORT_SYMBOL(scsi_scan_host);
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831void scsi_forget_host(struct Scsi_Host *shost)
1832{
Alan Sterna64358d2005-07-26 10:27:10 -04001833 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 unsigned long flags;
1835
Alan Sterna64358d2005-07-26 10:27:10 -04001836 restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001838 list_for_each_entry(sdev, &shost->__devices, siblings) {
1839 if (sdev->sdev_state == SDEV_DEL)
1840 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 spin_unlock_irqrestore(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001842 __scsi_remove_device(sdev);
1843 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845 spin_unlock_irqrestore(shost->host_lock, flags);
1846}
1847
1848/*
1849 * Function: scsi_get_host_dev()
1850 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001851 * Purpose: Create a scsi_device that points to the host adapter itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001853 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 *
1855 * Lock status: None assumed.
1856 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001857 * Returns: The scsi_device or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 *
1859 * Notes:
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001860 * Attach a single scsi_device to the Scsi_Host - this should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 * be made to look like a "pseudo-device" that points to the
1862 * HA itself.
1863 *
1864 * Note - this device is not accessible from any high-level
1865 * drivers (including generics), which is probably not
1866 * optimal. We can add hooks later to attach
1867 */
1868struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1869{
Alan Sterne517d312005-07-26 10:18:45 -04001870 struct scsi_device *sdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 struct scsi_target *starget;
1872
Arjan van de Ven0b950672006-01-11 13:16:10 +01001873 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001874 if (!scsi_host_scan_allowed(shost))
1875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1877 if (!starget)
Alan Sterne517d312005-07-26 10:18:45 -04001878 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 sdev = scsi_alloc_sdev(starget, 0, NULL);
1881 if (sdev) {
1882 sdev->sdev_gendev.parent = get_device(&starget->dev);
1883 sdev->borken = 0;
James Bottomley884d25c2006-09-05 16:26:41 -05001884 } else
1885 scsi_target_reap(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 put_device(&starget->dev);
Alan Sterne517d312005-07-26 10:18:45 -04001887 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +01001888 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 return sdev;
1890}
1891EXPORT_SYMBOL(scsi_get_host_dev);
1892
1893/*
1894 * Function: scsi_free_host_dev()
1895 *
1896 * Purpose: Free a scsi_device that points to the host adapter itself.
1897 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001898 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 *
1900 * Lock status: None assumed.
1901 *
1902 * Returns: Nothing
1903 *
1904 * Notes:
1905 */
1906void scsi_free_host_dev(struct scsi_device *sdev)
1907{
1908 BUG_ON(sdev->id != sdev->host->this_id);
1909
James Bottomley6f3a2022005-09-22 20:33:28 -05001910 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
1912EXPORT_SYMBOL(scsi_free_host_dev);
1913