blob: b53c5f67e372684cc23c81a9f0ce19a41109e1ac [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
224 *
225 * Description:
226 * Allocate, initialize for io, and return a pointer to a scsi_Device.
227 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
228 * adds scsi_Device to the appropriate list.
229 *
230 * Return value:
231 * scsi_Device pointer, or NULL on failure.
232 **/
233static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
234 unsigned int lun, void *hostdata)
235{
236 struct scsi_device *sdev;
237 int display_failure_msg = 1, ret;
238 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
239
Jes Sorensen24669f752006-01-16 10:31:18 -0500240 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 GFP_ATOMIC);
242 if (!sdev)
243 goto out;
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 sdev->vendor = scsi_null_device_strs;
246 sdev->model = scsi_null_device_strs;
247 sdev->rev = scsi_null_device_strs;
248 sdev->host = shost;
249 sdev->id = starget->id;
250 sdev->lun = lun;
251 sdev->channel = starget->channel;
252 sdev->sdev_state = SDEV_CREATED;
253 INIT_LIST_HEAD(&sdev->siblings);
254 INIT_LIST_HEAD(&sdev->same_target_siblings);
255 INIT_LIST_HEAD(&sdev->cmd_list);
256 INIT_LIST_HEAD(&sdev->starved_entry);
257 spin_lock_init(&sdev->list_lock);
258
259 sdev->sdev_gendev.parent = get_device(&starget->dev);
260 sdev->sdev_target = starget;
261
262 /* usually NULL and set by ->slave_alloc instead */
263 sdev->hostdata = hostdata;
264
265 /* if the device needs this changing, it may do so in the
266 * slave_configure function */
267 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
268
269 /*
270 * Some low level driver could use device->type
271 */
272 sdev->type = -1;
273
274 /*
275 * Assume that the device will have handshaking problems,
276 * and then fix this field later if it turns out it
277 * doesn't
278 */
279 sdev->borken = 1;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 sdev->request_queue = scsi_alloc_queue(sdev);
282 if (!sdev->request_queue) {
283 /* release fn is set up in scsi_sysfs_device_initialise, so
284 * have to free and put manually here */
285 put_device(&starget->dev);
Dave Jones93f560892006-03-09 10:21:27 -0500286 kfree(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 goto out;
288 }
289
290 sdev->request_queue->queuedata = sdev;
291 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
292
293 scsi_sysfs_device_initialize(sdev);
294
295 if (shost->hostt->slave_alloc) {
296 ret = shost->hostt->slave_alloc(sdev);
297 if (ret) {
298 /*
299 * if LLDD reports slave not present, don't clutter
300 * console with alloc failure messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 */
302 if (ret == -ENXIO)
303 display_failure_msg = 0;
304 goto out_device_destroy;
305 }
306 }
307
308 return sdev;
309
310out_device_destroy:
311 transport_destroy_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 put_device(&sdev->sdev_gendev);
313out:
314 if (display_failure_msg)
315 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
316 return NULL;
317}
318
319static void scsi_target_dev_release(struct device *dev)
320{
321 struct device *parent = dev->parent;
322 struct scsi_target *starget = to_scsi_target(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 kfree(starget);
325 put_device(parent);
326}
327
328int scsi_is_target_device(const struct device *dev)
329{
330 return dev->release == scsi_target_dev_release;
331}
332EXPORT_SYMBOL(scsi_is_target_device);
333
334static struct scsi_target *__scsi_find_target(struct device *parent,
335 int channel, uint id)
336{
337 struct scsi_target *starget, *found_starget = NULL;
338 struct Scsi_Host *shost = dev_to_shost(parent);
339 /*
340 * Search for an existing target for this sdev.
341 */
342 list_for_each_entry(starget, &shost->__targets, siblings) {
343 if (starget->id == id &&
344 starget->channel == channel) {
345 found_starget = starget;
346 break;
347 }
348 }
349 if (found_starget)
350 get_device(&found_starget->dev);
351
352 return found_starget;
353}
354
James Bottomley884d25c2006-09-05 16:26:41 -0500355/**
356 * scsi_alloc_target - allocate a new or find an existing target
357 * @parent: parent of the target (need not be a scsi host)
358 * @channel: target channel number (zero if no channels)
359 * @id: target id number
360 *
361 * Return an existing target if one exists, provided it hasn't already
362 * gone into STARGET_DEL state, otherwise allocate a new target.
363 *
364 * The target is returned with an incremented reference, so the caller
365 * is responsible for both reaping and doing a last put
366 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367static struct scsi_target *scsi_alloc_target(struct device *parent,
368 int channel, uint id)
369{
370 struct Scsi_Host *shost = dev_to_shost(parent);
371 struct device *dev = NULL;
372 unsigned long flags;
373 const int size = sizeof(struct scsi_target)
374 + shost->transportt->target_size;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -0400375 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 struct scsi_target *found_target;
Brian King32f95792006-02-22 14:28:24 -0600377 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jes Sorensen24669f752006-01-16 10:31:18 -0500379 starget = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!starget) {
381 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
382 return NULL;
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dev = &starget->dev;
385 device_initialize(dev);
386 starget->reap_ref = 1;
387 dev->parent = get_device(parent);
388 dev->release = scsi_target_dev_release;
389 sprintf(dev->bus_id, "target%d:%d:%d",
390 shost->host_no, channel, id);
391 starget->id = id;
392 starget->channel = channel;
393 INIT_LIST_HEAD(&starget->siblings);
394 INIT_LIST_HEAD(&starget->devices);
James Bottomleyffedb452006-02-23 14:27:18 -0600395 starget->state = STARGET_RUNNING;
Alan Stern7c9d6f12007-01-08 11:12:32 -0500396 starget->scsi_level = SCSI_2;
James Bottomleyffedb452006-02-23 14:27:18 -0600397 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_lock_irqsave(shost->host_lock, flags);
399
400 found_target = __scsi_find_target(parent, channel, id);
401 if (found_target)
402 goto found;
403
404 list_add_tail(&starget->siblings, &shost->__targets);
405 spin_unlock_irqrestore(shost->host_lock, flags);
406 /* allocate and add */
James Bottomleya283bd32005-05-24 12:06:38 -0500407 transport_setup_device(dev);
Brian King32f95792006-02-22 14:28:24 -0600408 error = device_add(dev);
409 if (error) {
410 dev_err(dev, "target device_add failed, error %d\n", error);
411 spin_lock_irqsave(shost->host_lock, flags);
412 list_del_init(&starget->siblings);
413 spin_unlock_irqrestore(shost->host_lock, flags);
414 transport_destroy_device(dev);
415 put_device(parent);
416 kfree(starget);
417 return NULL;
418 }
James Bottomleya283bd32005-05-24 12:06:38 -0500419 transport_add_device(dev);
420 if (shost->hostt->target_alloc) {
Brian King32f95792006-02-22 14:28:24 -0600421 error = shost->hostt->target_alloc(starget);
James Bottomleya283bd32005-05-24 12:06:38 -0500422
423 if(error) {
424 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
425 /* don't want scsi_target_reap to do the final
426 * put because it will be under the host lock */
427 get_device(dev);
428 scsi_target_reap(starget);
429 put_device(dev);
430 return NULL;
431 }
432 }
James Bottomley884d25c2006-09-05 16:26:41 -0500433 get_device(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return starget;
436
437 found:
438 found_target->reap_ref++;
439 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600440 if (found_target->state != STARGET_DEL) {
James Bottomley884d25c2006-09-05 16:26:41 -0500441 put_device(parent);
James Bottomleyffedb452006-02-23 14:27:18 -0600442 kfree(starget);
443 return found_target;
444 }
445 /* Unfortunately, we found a dying target; need to
446 * wait until it's dead before we can get a new one */
447 put_device(&found_target->dev);
448 flush_scheduled_work();
449 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
David Howells65f27f32006-11-22 14:55:48 +0000452static void scsi_target_reap_usercontext(struct work_struct *work)
James Bottomley65110b22006-02-14 10:48:46 -0600453{
David Howells65f27f32006-11-22 14:55:48 +0000454 struct scsi_target *starget =
455 container_of(work, struct scsi_target, ew.work);
James Bottomley863a9302005-12-15 20:01:43 -0800456 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
457 unsigned long flags;
458
James Bottomleyffedb452006-02-23 14:27:18 -0600459 transport_remove_device(&starget->dev);
460 device_del(&starget->dev);
461 transport_destroy_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800462 spin_lock_irqsave(shost->host_lock, flags);
Mike Andersona50a5e32006-03-14 11:18:46 -0800463 if (shost->hostt->target_destroy)
464 shost->hostt->target_destroy(starget);
James Bottomleyffedb452006-02-23 14:27:18 -0600465 list_del_init(&starget->siblings);
James Bottomley863a9302005-12-15 20:01:43 -0800466 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600467 put_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800468}
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470/**
471 * scsi_target_reap - check to see if target is in use and destroy if not
472 *
473 * @starget: target to be checked
474 *
475 * This is used after removing a LUN or doing a last put of the target
476 * it checks atomically that nothing is using the target and removes
477 * it if so.
478 */
479void scsi_target_reap(struct scsi_target *starget)
480{
James Bottomleyffedb452006-02-23 14:27:18 -0600481 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
482 unsigned long flags;
483
484 spin_lock_irqsave(shost->host_lock, flags);
485
486 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
487 BUG_ON(starget->state == STARGET_DEL);
488 starget->state = STARGET_DEL;
489 spin_unlock_irqrestore(shost->host_lock, flags);
490 execute_in_process_context(scsi_target_reap_usercontext,
David Howells65f27f32006-11-22 14:55:48 +0000491 &starget->ew);
James Bottomleyffedb452006-02-23 14:27:18 -0600492 return;
493
494 }
495 spin_unlock_irqrestore(shost->host_lock, flags);
496
497 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/**
Alan Sterne5b3cd42006-08-21 15:53:25 -0400501 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
502 * @s: INQUIRY result string to sanitize
503 * @len: length of the string
504 *
505 * Description:
506 * The SCSI spec says that INQUIRY vendor, product, and revision
507 * strings must consist entirely of graphic ASCII characters,
508 * padded on the right with spaces. Since not all devices obey
509 * this rule, we will replace non-graphic or non-ASCII characters
510 * with spaces. Exception: a NUL character is interpreted as a
511 * string terminator, so all the following characters are set to
512 * spaces.
513 **/
514static void sanitize_inquiry_string(unsigned char *s, int len)
515{
516 int terminated = 0;
517
518 for (; len > 0; (--len, ++s)) {
519 if (*s == 0)
520 terminated = 1;
521 if (terminated || *s < 0x20 || *s > 0x7e)
522 *s = ' ';
523 }
524}
525
526/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
James Bottomley39216032005-06-15 18:48:29 -0500528 * @sdev: scsi_device to probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 * @inq_result: area to store the INQUIRY result
James Bottomley39216032005-06-15 18:48:29 -0500530 * @result_len: len of inq_result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * @bflags: store any bflags found here
532 *
533 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500534 * Probe the lun associated with @req using a standard SCSI INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 *
James Bottomley39216032005-06-15 18:48:29 -0500536 * If the INQUIRY is successful, zero is returned and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100538 * are copied to the scsi_device any flags value is stored in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400540static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
James Bottomley39216032005-06-15 18:48:29 -0500541 int result_len, int *bflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
544 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
545 int response_len = 0;
James Bottomley39216032005-06-15 18:48:29 -0500546 int pass, count, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 struct scsi_sense_hdr sshdr;
548
549 *bflags = 0;
550
551 /* Perform up to 3 passes. The first pass uses a conservative
552 * transfer length of 36 unless sdev->inquiry_len specifies a
553 * different value. */
554 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
555 try_inquiry_len = first_inquiry_len;
556 pass = 1;
557
558 next_pass:
James Bottomley9ccfc752005-10-02 11:45:08 -0500559 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
560 "scsi scan: INQUIRY pass %d length %d\n",
561 pass, try_inquiry_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* Each pass gets up to three chances to ignore Unit Attention */
564 for (count = 0; count < 3; ++count) {
565 memset(scsi_cmd, 0, 6);
566 scsi_cmd[0] = INQUIRY;
567 scsi_cmd[4] = (unsigned char) try_inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 memset(inq_result, 0, try_inquiry_len);
James Bottomley39216032005-06-15 18:48:29 -0500570
571 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500572 inq_result, try_inquiry_len, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -0500573 HZ / 2 + HZ * scsi_inq_timeout, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
576 "with code 0x%x\n",
James Bottomley39216032005-06-15 18:48:29 -0500577 result ? "failed" : "successful", result));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
James Bottomley39216032005-06-15 18:48:29 -0500579 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 /*
581 * not-ready to ready transition [asc/ascq=0x28/0x0]
582 * or power-on, reset [asc/ascq=0x29/0x0], continue.
583 * INQUIRY should not yield UNIT_ATTENTION
584 * but many buggy devices do so anyway.
585 */
James Bottomley39216032005-06-15 18:48:29 -0500586 if ((driver_byte(result) & DRIVER_SENSE) &&
James Bottomleyea73a9f2005-08-28 11:33:52 -0500587 scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if ((sshdr.sense_key == UNIT_ATTENTION) &&
589 ((sshdr.asc == 0x28) ||
590 (sshdr.asc == 0x29)) &&
591 (sshdr.ascq == 0))
592 continue;
593 }
594 }
595 break;
596 }
597
James Bottomley39216032005-06-15 18:48:29 -0500598 if (result == 0) {
Alan Sterne5b3cd42006-08-21 15:53:25 -0400599 sanitize_inquiry_string(&inq_result[8], 8);
600 sanitize_inquiry_string(&inq_result[16], 16);
601 sanitize_inquiry_string(&inq_result[32], 4);
602
603 response_len = inq_result[4] + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (response_len > 255)
605 response_len = first_inquiry_len; /* sanity */
606
607 /*
608 * Get any flags for this device.
609 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100610 * XXX add a bflags to scsi_device, and replace the
611 * corresponding bit fields in scsi_device, so bflags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * need not be passed as an argument.
613 */
614 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
615 &inq_result[16]);
616
617 /* When the first pass succeeds we gain information about
618 * what larger transfer lengths might work. */
619 if (pass == 1) {
620 if (BLIST_INQUIRY_36 & *bflags)
621 next_inquiry_len = 36;
622 else if (BLIST_INQUIRY_58 & *bflags)
623 next_inquiry_len = 58;
624 else if (sdev->inquiry_len)
625 next_inquiry_len = sdev->inquiry_len;
626 else
627 next_inquiry_len = response_len;
628
629 /* If more data is available perform the second pass */
630 if (next_inquiry_len > try_inquiry_len) {
631 try_inquiry_len = next_inquiry_len;
632 pass = 2;
633 goto next_pass;
634 }
635 }
636
637 } else if (pass == 2) {
638 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
639 "Consider BLIST_INQUIRY_36 for this device\n",
640 try_inquiry_len);
641
642 /* If this pass failed, the third pass goes back and transfers
643 * the same amount as we successfully got in the first pass. */
644 try_inquiry_len = first_inquiry_len;
645 pass = 3;
646 goto next_pass;
647 }
648
649 /* If the last transfer attempt got an error, assume the
650 * peripheral doesn't exist or is dead. */
James Bottomley39216032005-06-15 18:48:29 -0500651 if (result)
652 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 /* Don't report any more data than the device says is valid */
655 sdev->inquiry_len = min(try_inquiry_len, response_len);
656
657 /*
658 * XXX Abort if the response length is less than 36? If less than
659 * 32, the lookup of the device flags (above) could be invalid,
660 * and it would be possible to take an incorrect action - we do
661 * not want to hang because of a short INQUIRY. On the flip side,
662 * if the device is spun down or becoming ready (and so it gives a
663 * short INQUIRY), an abort here prevents any further use of the
664 * device, including spin up.
665 *
Alan Sterne423ee32007-02-16 01:46:38 -0800666 * On the whole, the best approach seems to be to assume the first
667 * 36 bytes are valid no matter what the device says. That's
668 * better than copying < 36 bytes to the inquiry-result buffer
669 * and displaying garbage for the Vendor, Product, or Revision
670 * strings.
671 */
672 if (sdev->inquiry_len < 36) {
673 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
674 " using 36\n", sdev->inquiry_len);
675 sdev->inquiry_len = 36;
676 }
677
678 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 * Related to the above issue:
680 *
681 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
682 * and if not ready, sent a START_STOP to start (maybe spin up) and
683 * then send the INQUIRY again, since the INQUIRY can change after
684 * a device is initialized.
685 *
686 * Ideally, start a device if explicitly asked to do so. This
687 * assumes that a device is spun up on power on, spun down on
688 * request, and then spun up on request.
689 */
690
691 /*
692 * The scanning code needs to know the scsi_level, even if no
693 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
694 * non-zero LUNs can be scanned.
695 */
696 sdev->scsi_level = inq_result[2] & 0x07;
697 if (sdev->scsi_level >= 2 ||
698 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
699 sdev->scsi_level++;
James Bottomley6f3a2022005-09-22 20:33:28 -0500700 sdev->sdev_target->scsi_level = sdev->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
James Bottomley39216032005-06-15 18:48:29 -0500702 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
705/**
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100706 * scsi_add_lun - allocate and fully initialze a scsi_device
Matthew Wilcox6d877682007-07-11 12:54:55 -0600707 * @sdev: holds information to be stored in the new scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 * @inq_result: holds the result of a previous INQUIRY to the LUN
709 * @bflags: black/white list flag
Matthew Wilcox6d877682007-07-11 12:54:55 -0600710 * @async: 1 if this device is being scanned asynchronously
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
712 * Description:
Matthew Wilcox6d877682007-07-11 12:54:55 -0600713 * Initialize the scsi_device @sdev. Optionally set fields based
714 * on values in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 *
716 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100717 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
718 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400720static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600721 int *bflags, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 /*
724 * XXX do not save the inquiry, since it can change underneath us,
725 * save just vendor/model/rev.
726 *
727 * Rather than save it and have an ioctl that retrieves the saved
728 * value, have an ioctl that executes the same INQUIRY code used
729 * in scsi_probe_lun, let user level programs doing INQUIRY
730 * scanning run at their own risk, or supply a user level program
731 * that can correctly scan.
732 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Alan Stern09123d22006-11-10 12:27:57 -0800734 /*
735 * Copy at least 36 bytes of INQUIRY data, so that we don't
736 * dereference unallocated memory when accessing the Vendor,
737 * Product, and Revision strings. Badly behaved devices may set
738 * the INQUIRY Additional Length byte to a small value, indicating
739 * these strings are invalid, but often they contain plausible data
740 * nonetheless. It doesn't matter if the device sent < 36 bytes
741 * total, since scsi_probe_lun() initializes inq_result with 0s.
742 */
743 sdev->inquiry = kmemdup(inq_result,
744 max_t(size_t, sdev->inquiry_len, 36),
745 GFP_ATOMIC);
746 if (sdev->inquiry == NULL)
747 return SCSI_SCAN_NO_RESPONSE;
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 sdev->vendor = (char *) (sdev->inquiry + 8);
750 sdev->model = (char *) (sdev->inquiry + 16);
751 sdev->rev = (char *) (sdev->inquiry + 32);
752
753 if (*bflags & BLIST_ISROM) {
Matthew Wilcox6d877682007-07-11 12:54:55 -0600754 sdev->type = TYPE_ROM;
755 sdev->removable = 1;
756 } else {
757 sdev->type = (inq_result[0] & 0x1f);
758 sdev->removable = (inq_result[1] & 0x80) >> 7;
759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Matthew Wilcox6d877682007-07-11 12:54:55 -0600761 switch (sdev->type) {
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600762 case TYPE_RBC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 case TYPE_TAPE:
764 case TYPE_DISK:
765 case TYPE_PRINTER:
766 case TYPE_MOD:
767 case TYPE_PROCESSOR:
768 case TYPE_SCANNER:
769 case TYPE_MEDIUM_CHANGER:
770 case TYPE_ENCLOSURE:
771 case TYPE_COMM:
James Bottomley4d7db042006-03-31 20:07:45 -0600772 case TYPE_RAID:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 sdev->writeable = 1;
774 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 case TYPE_ROM:
James Bottomleyddaf6fc2006-12-13 10:10:40 -0600776 case TYPE_WORM:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 sdev->writeable = 0;
778 break;
779 default:
780 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
781 }
782
Matthew Wilcox6d877682007-07-11 12:54:55 -0600783 if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
784 /* RBC and MMC devices can return SCSI-3 compliance and yet
785 * still not support REPORT LUNS, so make them act as
786 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
787 * specifically set */
788 if ((*bflags & BLIST_REPORTLUN2) == 0)
789 *bflags |= BLIST_NOREPORTLUN;
790 }
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 /*
793 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
794 * spec says: The device server is capable of supporting the
795 * specified peripheral device type on this logical unit. However,
796 * the physical device is not currently connected to this logical
797 * unit.
798 *
799 * The above is vague, as it implies that we could treat 001 and
800 * 011 the same. Stay compatible with previous code, and create a
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100801 * scsi_device for a PQ of 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
803 * Don't set the device offline here; rather let the upper
804 * level drivers eval the PQ to decide whether they should
805 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
806 */
807
808 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 sdev->lockable = sdev->removable;
810 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
811
Matthew Wilcox6d877682007-07-11 12:54:55 -0600812 if (sdev->scsi_level >= SCSI_3 ||
813 (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 sdev->ppr = 1;
815 if (inq_result[7] & 0x60)
816 sdev->wdtr = 1;
817 if (inq_result[7] & 0x10)
818 sdev->sdtr = 1;
819
James Bottomley19ac0db2006-08-06 18:15:22 -0500820 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600821 "ANSI: %d%s\n", scsi_device_type(sdev->type),
822 sdev->vendor, sdev->model, sdev->rev,
823 sdev->inq_periph_qual, inq_result[2] & 0x07,
824 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
827 !(*bflags & BLIST_NOTQ))
828 sdev->tagged_supported = 1;
Matthew Wilcox6d877682007-07-11 12:54:55 -0600829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /*
831 * Some devices (Texel CD ROM drives) have handshaking problems
832 * when used with the Seagate controllers. borken is initialized
833 * to 1, and then set it to 0 here.
834 */
835 if ((*bflags & BLIST_BORKEN) == 0)
836 sdev->borken = 0;
837
Matthew Wilcox6d877682007-07-11 12:54:55 -0600838 if (*bflags & BLIST_NO_ULD_ATTACH)
839 sdev->no_uld_attach = 1;
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 /*
842 * Apparently some really broken devices (contrary to the SCSI
843 * standards) need to be selected without asserting ATN
844 */
845 if (*bflags & BLIST_SELECT_NO_ATN)
846 sdev->select_no_atn = 1;
847
848 /*
James Bottomley4d7db042006-03-31 20:07:45 -0600849 * Maximum 512 sector transfer length
850 * broken RA4x00 Compaq Disk Array
851 */
852 if (*bflags & BLIST_MAX_512)
853 blk_queue_max_sectors(sdev->request_queue, 512);
854
855 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * Some devices may not want to have a start command automatically
857 * issued when a device is added.
858 */
859 if (*bflags & BLIST_NOSTARTONADD)
860 sdev->no_start_on_add = 1;
861
862 if (*bflags & BLIST_SINGLELUN)
863 sdev->single_lun = 1;
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 sdev->use_10_for_rw = 1;
866
867 if (*bflags & BLIST_MS_SKIP_PAGE_08)
868 sdev->skip_ms_page_8 = 1;
869
870 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
871 sdev->skip_ms_page_3f = 1;
872
873 if (*bflags & BLIST_USE_10_BYTE_MS)
874 sdev->use_10_for_ms = 1;
875
876 /* set the device running here so that slave configure
877 * may do I/O */
878 scsi_device_set_state(sdev, SDEV_RUNNING);
879
880 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
881 sdev->use_192_bytes_for_3f = 1;
882
883 if (*bflags & BLIST_NOT_LOCKABLE)
884 sdev->lockable = 0;
885
886 if (*bflags & BLIST_RETRY_HWERROR)
887 sdev->retry_hwerror = 1;
888
889 transport_configure_device(&sdev->sdev_gendev);
890
Christoph Hellwig93805092006-02-17 12:11:29 +0100891 if (sdev->host->hostt->slave_configure) {
892 int ret = sdev->host->hostt->slave_configure(sdev);
893 if (ret) {
894 /*
895 * if LLDD reports slave not present, don't clutter
896 * console with alloc failure messages
897 */
898 if (ret != -ENXIO) {
899 sdev_printk(KERN_ERR, sdev,
900 "failed to configure device\n");
901 }
902 return SCSI_SCAN_NO_RESPONSE;
903 }
904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /*
907 * Ok, the device is now all set up, we can
908 * register it and tell the rest of the kernel
909 * about it.
910 */
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600911 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
Alan Sternb24b1032005-07-27 11:43:46 -0700912 return SCSI_SCAN_NO_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 return SCSI_SCAN_LUN_PRESENT;
915}
916
James Bottomley6f3a2022005-09-22 20:33:28 -0500917static inline void scsi_destroy_sdev(struct scsi_device *sdev)
918{
Brian King309bd272006-06-27 11:10:43 -0500919 scsi_device_set_state(sdev, SDEV_DEL);
James Bottomley6f3a2022005-09-22 20:33:28 -0500920 if (sdev->host->hostt->slave_destroy)
921 sdev->host->hostt->slave_destroy(sdev);
922 transport_destroy_device(&sdev->sdev_gendev);
923 put_device(&sdev->sdev_gendev);
924}
925
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700926#ifdef CONFIG_SCSI_LOGGING
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200927/**
928 * scsi_inq_str - print INQUIRY data from min to max index,
929 * strip trailing whitespace
930 * @buf: Output buffer with at least end-first+1 bytes of space
931 * @inq: Inquiry buffer (input)
932 * @first: Offset of string into inq
933 * @end: Index after last character in inq
934 */
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700935static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200936 unsigned first, unsigned end)
937{
938 unsigned term = 0, idx;
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700939
940 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
941 if (inq[idx+first] > ' ') {
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200942 buf[idx] = inq[idx+first];
943 term = idx+1;
944 } else {
945 buf[idx] = ' ';
946 }
947 }
948 buf[term] = 0;
949 return buf;
950}
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700951#endif
James Bottomley6f3a2022005-09-22 20:33:28 -0500952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953/**
954 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
955 * @starget: pointer to target device structure
956 * @lun: LUN of target device
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100957 * @sdevscan: probe the LUN corresponding to this scsi_device
958 * @sdevnew: store the value of any new scsi_device allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 * @bflagsp: store bflags here if not NULL
960 *
961 * Description:
962 * Call scsi_probe_lun, if a LUN with an attached device is found,
963 * allocate and set it up by calling scsi_add_lun.
964 *
965 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100966 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
968 * attached at the LUN
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100969 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 **/
971static int scsi_probe_and_add_lun(struct scsi_target *starget,
972 uint lun, int *bflagsp,
973 struct scsi_device **sdevp, int rescan,
974 void *hostdata)
975{
976 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 unsigned char *result;
James Bottomley39216032005-06-15 18:48:29 -0500978 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
980
981 /*
982 * The rescan flag is used as an optimization, the first scan of a
983 * host adapter calls into here with rescan == 0.
984 */
James Bottomley6f3a2022005-09-22 20:33:28 -0500985 sdev = scsi_device_lookup_by_target(starget, lun);
986 if (sdev) {
987 if (rescan || sdev->sdev_state != SDEV_CREATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
989 "scsi scan: device exists on %s\n",
990 sdev->sdev_gendev.bus_id));
991 if (sdevp)
992 *sdevp = sdev;
993 else
994 scsi_device_put(sdev);
995
996 if (bflagsp)
997 *bflagsp = scsi_get_device_flags(sdev,
998 sdev->vendor,
999 sdev->model);
1000 return SCSI_SCAN_LUN_PRESENT;
1001 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001002 scsi_device_put(sdev);
1003 } else
1004 sdev = scsi_alloc_sdev(starget, lun, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (!sdev)
1006 goto out;
James Bottomley39216032005-06-15 18:48:29 -05001007
1008 result = kmalloc(result_len, GFP_ATOMIC |
Al Virobc861202005-04-24 12:28:34 -07001009 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 if (!result)
James Bottomley39216032005-06-15 18:48:29 -05001011 goto out_free_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
James Bottomley39216032005-06-15 18:48:29 -05001013 if (scsi_probe_lun(sdev, result, result_len, &bflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto out_free_result;
1015
Kurt Garloff4186ab12006-04-03 15:16:48 +02001016 if (bflagsp)
1017 *bflagsp = bflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 /*
1019 * result contains valid SCSI INQUIRY data.
1020 */
Kurt Garloff13f7e5a2006-04-03 15:20:08 +02001021 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 /*
1023 * For a Peripheral qualifier 3 (011b), the SCSI
1024 * spec says: The device server is not capable of
1025 * supporting a physical device on this logical
1026 * unit.
1027 *
1028 * For disks, this implies that there is no
1029 * logical disk configured at sdev->lun, but there
1030 * is a target id responding.
1031 */
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001032 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1033 " peripheral qualifier of 3, device not"
1034 " added\n"))
1035 if (lun == 0) {
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001036 SCSI_LOG_SCAN_BUS(1, {
1037 unsigned char vend[9];
1038 unsigned char mod[17];
1039
1040 sdev_printk(KERN_INFO, sdev,
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001041 "scsi scan: consider passing scsi_mod."
Kurt Garloff3424a652007-01-09 02:28:54 +01001042 "dev_flags=%s:%s:0x240 or 0x1000240\n",
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001043 scsi_inq_str(vend, result, 8, 16),
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001044 scsi_inq_str(mod, result, 16, 32));
1045 });
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001046 }
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 res = SCSI_SCAN_TARGET_PRESENT;
1049 goto out_free_result;
1050 }
1051
Alan Stern1bfc5d92006-02-09 15:26:18 -05001052 /*
dave wysochanski84961f22006-08-09 14:56:32 -04001053 * Some targets may set slight variations of PQ and PDT to signal
1054 * that no LUN is present, so don't add sdev in these cases.
1055 * Two specific examples are:
1056 * 1) NetApp targets: return PQ=1, PDT=0x1f
1057 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1058 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1059 *
1060 * References:
1061 * 1) SCSI SPC-3, pp. 145-146
1062 * PQ=1: "A peripheral device having the specified peripheral
1063 * device type is not connected to this logical unit. However, the
1064 * device server is capable of supporting the specified peripheral
1065 * device type on this logical unit."
1066 * PDT=0x1f: "Unknown or no device type"
1067 * 2) USB UFI 1.0, p. 20
1068 * PDT=00h Direct-access device (floppy)
1069 * PDT=1Fh none (no FDD connected to the requested logical unit)
Alan Stern1bfc5d92006-02-09 15:26:18 -05001070 */
dave wysochanski84961f22006-08-09 14:56:32 -04001071 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1072 (result[0] & 0x1f) == 0x1f) {
Alan Stern1bfc5d92006-02-09 15:26:18 -05001073 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1074 "scsi scan: peripheral device type"
1075 " of 31, no device added\n"));
1076 res = SCSI_SCAN_TARGET_PRESENT;
1077 goto out_free_result;
1078 }
1079
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001080 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 if (res == SCSI_SCAN_LUN_PRESENT) {
1082 if (bflags & BLIST_KEY) {
1083 sdev->lockable = 0;
James Bottomley39216032005-06-15 18:48:29 -05001084 scsi_unlock_floptical(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087
1088 out_free_result:
1089 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 out_free_sdev:
1091 if (res == SCSI_SCAN_LUN_PRESENT) {
1092 if (sdevp) {
Alan Sternb70d37b2005-07-26 10:30:40 -04001093 if (scsi_device_get(sdev) == 0) {
1094 *sdevp = sdev;
1095 } else {
1096 __scsi_remove_device(sdev);
1097 res = SCSI_SCAN_NO_RESPONSE;
1098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001100 } else
1101 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 out:
1103 return res;
1104}
1105
1106/**
1107 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1108 * @starget: pointer to target structure to scan
1109 * @bflags: black/white list flag for LUN 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 *
1111 * Description:
1112 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1113 * scanned) to some maximum lun until a LUN is found with no device
1114 * attached. Use the bflags to figure out any oddities.
1115 *
1116 * Modifies sdevscan->lun.
1117 **/
1118static void scsi_sequential_lun_scan(struct scsi_target *starget,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001119 int bflags, int scsi_level, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
1121 unsigned int sparse_lun, lun, max_dev_lun;
1122 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1123
1124 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1125 "%s\n", starget->dev.bus_id));
1126
1127 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1128 /*
1129 * If this device is known to support sparse multiple units,
1130 * override the other settings, and scan all of them. Normally,
1131 * SCSI-3 devices should be scanned via the REPORT LUNS.
1132 */
1133 if (bflags & BLIST_SPARSELUN) {
1134 max_dev_lun = shost->max_lun;
1135 sparse_lun = 1;
1136 } else
1137 sparse_lun = 0;
1138
1139 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * If less than SCSI_1_CSS, and no special lun scaning, stop
1141 * scanning; this matches 2.4 behaviour, but could just be a bug
1142 * (to continue scanning a SCSI_1_CSS device).
1143 *
1144 * This test is broken. We might not have any device on lun0 for
1145 * a sparselun device, and if that's the case then how would we
1146 * know the real scsi_level, eh? It might make sense to just not
1147 * scan any SCSI_1 device for non-0 luns, but that check would best
1148 * go into scsi_alloc_sdev() and just have it return null when asked
1149 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1150 *
1151 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1152 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1153 == 0))
1154 return;
1155 */
1156 /*
1157 * If this device is known to support multiple units, override
1158 * the other settings, and scan all of them.
1159 */
1160 if (bflags & BLIST_FORCELUN)
1161 max_dev_lun = shost->max_lun;
1162 /*
1163 * REGAL CDC-4X: avoid hang after LUN 4
1164 */
1165 if (bflags & BLIST_MAX5LUN)
1166 max_dev_lun = min(5U, max_dev_lun);
1167 /*
1168 * Do not scan SCSI-2 or lower device past LUN 7, unless
1169 * BLIST_LARGELUN.
1170 */
1171 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1172 max_dev_lun = min(8U, max_dev_lun);
1173
1174 /*
1175 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1176 * until we reach the max, or no LUN is found and we are not
1177 * sparse_lun.
1178 */
1179 for (lun = 1; lun < max_dev_lun; ++lun)
1180 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1181 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1182 !sparse_lun)
1183 return;
1184}
1185
1186/**
1187 * scsilun_to_int: convert a scsi_lun to an int
1188 * @scsilun: struct scsi_lun to be converted.
1189 *
1190 * Description:
1191 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1192 * integer, and return the result. The caller must check for
1193 * truncation before using this function.
1194 *
1195 * Notes:
1196 * The struct scsi_lun is assumed to be four levels, with each level
1197 * effectively containing a SCSI byte-ordered (big endian) short; the
1198 * addressing bits of each level are ignored (the highest two bits).
1199 * For a description of the LUN format, post SCSI-3 see the SCSI
1200 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1201 *
1202 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1203 * the integer: 0x0b030a04
1204 **/
Christof Schmitt462b7852007-06-19 10:25:30 +02001205int scsilun_to_int(struct scsi_lun *scsilun)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206{
1207 int i;
1208 unsigned int lun;
1209
1210 lun = 0;
1211 for (i = 0; i < sizeof(lun); i += 2)
1212 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1213 scsilun->scsi_lun[i + 1]) << (i * 8));
1214 return lun;
1215}
Christof Schmitt462b7852007-06-19 10:25:30 +02001216EXPORT_SYMBOL(scsilun_to_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218/**
James.Smart@Emulex.Com2f4701d2005-07-13 22:05:03 -04001219 * int_to_scsilun: reverts an int into a scsi_lun
1220 * @int: integer to be reverted
1221 * @scsilun: struct scsi_lun to be set.
1222 *
1223 * Description:
1224 * Reverts the functionality of the scsilun_to_int, which packed
1225 * an 8-byte lun value into an int. This routine unpacks the int
1226 * back into the lun value.
1227 * Note: the scsilun_to_int() routine does not truly handle all
1228 * 8bytes of the lun value. This functions restores only as much
1229 * as was set by the routine.
1230 *
1231 * Notes:
1232 * Given an integer : 0x0b030a04, this function returns a
1233 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1234 *
1235 **/
1236void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1237{
1238 int i;
1239
1240 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1241
1242 for (i = 0; i < sizeof(lun); i += 2) {
1243 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1244 scsilun->scsi_lun[i+1] = lun & 0xFF;
1245 lun = lun >> 16;
1246 }
1247}
1248EXPORT_SYMBOL(int_to_scsilun);
1249
1250/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001252 * @sdevscan: scan the host, channel, and id of this scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 *
1254 * Description:
1255 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1256 * command, and scan the resulting list of LUNs by calling
1257 * scsi_probe_and_add_lun.
1258 *
1259 * Modifies sdevscan->lun.
1260 *
1261 * Return:
1262 * 0: scan completed (or no memory, so further scanning is futile)
1263 * 1: no report lun scan, or not configured
1264 **/
James Bottomley6f3a2022005-09-22 20:33:28 -05001265static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 int rescan)
1267{
1268 char devname[64];
1269 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1270 unsigned int length;
1271 unsigned int lun;
1272 unsigned int num_luns;
1273 unsigned int retries;
James Bottomley39216032005-06-15 18:48:29 -05001274 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 struct scsi_lun *lunp, *lun_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 u8 *data;
1277 struct scsi_sense_hdr sshdr;
James Bottomley6f3a2022005-09-22 20:33:28 -05001278 struct scsi_device *sdev;
1279 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
Alan Stern2ef89192005-11-08 15:51:55 -05001280 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 /*
1283 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1284 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1285 * support more than 8 LUNs.
1286 */
James Bottomley4d7db042006-03-31 20:07:45 -06001287 if (bflags & BLIST_NOREPORTLUN)
1288 return 1;
1289 if (starget->scsi_level < SCSI_2 &&
1290 starget->scsi_level != SCSI_UNKNOWN)
1291 return 1;
1292 if (starget->scsi_level < SCSI_3 &&
1293 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 1;
1295 if (bflags & BLIST_NOLUN)
1296 return 0;
1297
James Bottomley6f3a2022005-09-22 20:33:28 -05001298 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1299 sdev = scsi_alloc_sdev(starget, 0, NULL);
1300 if (!sdev)
1301 return 0;
1302 if (scsi_device_get(sdev))
1303 return 0;
1304 }
1305
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 sprintf(devname, "host %d channel %d id %d",
James Bottomley6f3a2022005-09-22 20:33:28 -05001307 shost->host_no, sdev->channel, sdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
1309 /*
1310 * Allocate enough to hold the header (the same size as one scsi_lun)
1311 * plus the max number of luns we are requesting.
1312 *
1313 * Reallocating and trying again (with the exact amount we need)
1314 * would be nice, but then we need to somehow limit the size
1315 * allocated based on the available memory and the limits of
1316 * kmalloc - we don't want a kmalloc() failure of a huge value to
1317 * prevent us from finding any LUNs on this target.
1318 */
1319 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1320 lun_data = kmalloc(length, GFP_ATOMIC |
1321 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
James Bottomley6f3a2022005-09-22 20:33:28 -05001322 if (!lun_data) {
1323 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
James Bottomley39216032005-06-15 18:48:29 -05001324 goto out;
James Bottomley6f3a2022005-09-22 20:33:28 -05001325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 scsi_cmd[0] = REPORT_LUNS;
1328
1329 /*
1330 * bytes 1 - 5: reserved, set to zero.
1331 */
1332 memset(&scsi_cmd[1], 0, 5);
1333
1334 /*
1335 * bytes 6 - 9: length of the command.
1336 */
1337 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1338 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1339 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1340 scsi_cmd[9] = (unsigned char) length & 0xff;
1341
1342 scsi_cmd[10] = 0; /* reserved */
1343 scsi_cmd[11] = 0; /* control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345 /*
1346 * We can get a UNIT ATTENTION, for example a power on/reset, so
1347 * retry a few times (like sd.c does for TEST UNIT READY).
1348 * Experience shows some combinations of adapter/devices get at
1349 * least two power on/resets.
1350 *
1351 * Illegal requests (for devices that do not support REPORT LUNS)
1352 * should come through as a check condition, and will not generate
1353 * a retry.
1354 */
1355 for (retries = 0; retries < 3; retries++) {
1356 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1357 " REPORT LUNS to %s (try %d)\n", devname,
1358 retries));
James Bottomley39216032005-06-15 18:48:29 -05001359
James Bottomley39216032005-06-15 18:48:29 -05001360 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001361 lun_data, length, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -05001362 SCSI_TIMEOUT + 4 * HZ, 3);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
James Bottomley39216032005-06-15 18:48:29 -05001365 " %s (try %d) result 0x%x\n", result
1366 ? "failed" : "successful", retries, result));
1367 if (result == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 break;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001369 else if (scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (sshdr.sense_key != UNIT_ATTENTION)
1371 break;
1372 }
1373 }
1374
James Bottomley39216032005-06-15 18:48:29 -05001375 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /*
1377 * The device probably does not support a REPORT LUN command
1378 */
Alan Stern2ef89192005-11-08 15:51:55 -05001379 ret = 1;
1380 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 /*
1384 * Get the length from the first four bytes of lun_data.
1385 */
1386 data = (u8 *) lun_data->scsi_lun;
1387 length = ((data[0] << 24) | (data[1] << 16) |
1388 (data[2] << 8) | (data[3] << 0));
1389
1390 num_luns = (length / sizeof(struct scsi_lun));
1391 if (num_luns > max_scsi_report_luns) {
1392 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1393 " of %d luns reported, try increasing"
1394 " max_scsi_report_luns.\n", devname,
1395 max_scsi_report_luns, num_luns);
1396 num_luns = max_scsi_report_luns;
1397 }
1398
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001399 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1400 "scsi scan: REPORT LUN scan\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 /*
1403 * Scan the luns in lun_data. The entry at offset 0 is really
1404 * the header, so start at 1 and go up to and including num_luns.
1405 */
1406 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1407 lun = scsilun_to_int(lunp);
1408
1409 /*
1410 * Check if the unused part of lunp is non-zero, and so
1411 * does not fit in lun.
1412 */
1413 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1414 int i;
1415
1416 /*
1417 * Output an error displaying the LUN in byte order,
1418 * this differs from what linux would print for the
1419 * integer LUN value.
1420 */
1421 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1422 data = (char *)lunp->scsi_lun;
1423 for (i = 0; i < sizeof(struct scsi_lun); i++)
1424 printk("%02x", data[i]);
1425 printk(" has a LUN larger than currently supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 } else if (lun > sdev->host->max_lun) {
1427 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1428 " than allowed by the host adapter\n",
1429 devname, lun);
1430 } else {
1431 int res;
1432
1433 res = scsi_probe_and_add_lun(starget,
1434 lun, NULL, NULL, rescan, NULL);
1435 if (res == SCSI_SCAN_NO_RESPONSE) {
1436 /*
1437 * Got some results, but now none, abort.
1438 */
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001439 sdev_printk(KERN_ERR, sdev,
1440 "Unexpected response"
1441 " from lun %d while scanning, scan"
1442 " aborted\n", lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444 }
1445 }
1446 }
1447
Alan Stern2ef89192005-11-08 15:51:55 -05001448 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 kfree(lun_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 out:
James Bottomley6f3a2022005-09-22 20:33:28 -05001451 scsi_device_put(sdev);
1452 if (sdev->sdev_state == SDEV_CREATED)
1453 /*
1454 * the sdev we used didn't appear in the report luns scan
1455 */
1456 scsi_destroy_sdev(sdev);
Alan Stern2ef89192005-11-08 15:51:55 -05001457 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
1460struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1461 uint id, uint lun, void *hostdata)
1462{
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001463 struct scsi_device *sdev = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 struct device *parent = &shost->shost_gendev;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001465 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Matthew Wilcox938e2ac2007-01-15 18:07:09 -07001467 if (strncmp(scsi_scan_type, "none", 4) == 0)
1468 return ERR_PTR(-ENODEV);
1469
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001470 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (!starget)
1472 return ERR_PTR(-ENOMEM);
1473
Arjan van de Ven0b950672006-01-11 13:16:10 +01001474 mutex_lock(&shost->scan_mutex);
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001475 if (!shost->async_scan)
1476 scsi_complete_async_scans();
1477
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001478 if (scsi_host_scan_allowed(shost))
1479 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001480 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 scsi_target_reap(starget);
1482 put_device(&starget->dev);
1483
1484 return sdev;
1485}
1486EXPORT_SYMBOL(__scsi_add_device);
1487
James Bottomley146f7262005-09-10 12:44:09 -05001488int scsi_add_device(struct Scsi_Host *host, uint channel,
1489 uint target, uint lun)
1490{
1491 struct scsi_device *sdev =
1492 __scsi_add_device(host, channel, target, lun, NULL);
1493 if (IS_ERR(sdev))
1494 return PTR_ERR(sdev);
1495
1496 scsi_device_put(sdev);
1497 return 0;
1498}
1499EXPORT_SYMBOL(scsi_add_device);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501void scsi_rescan_device(struct device *dev)
1502{
1503 struct scsi_driver *drv;
1504
1505 if (!dev->driver)
1506 return;
1507
1508 drv = to_scsi_driver(dev->driver);
1509 if (try_module_get(drv->owner)) {
1510 if (drv->rescan)
1511 drv->rescan(dev);
1512 module_put(drv->owner);
1513 }
1514}
1515EXPORT_SYMBOL(scsi_rescan_device);
1516
Alan Sterne517d312005-07-26 10:18:45 -04001517static void __scsi_scan_target(struct device *parent, unsigned int channel,
1518 unsigned int id, unsigned int lun, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct Scsi_Host *shost = dev_to_shost(parent);
1521 int bflags = 0;
1522 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct scsi_target *starget;
1524
1525 if (shost->this_id == id)
1526 /*
1527 * Don't scan the host adapter
1528 */
1529 return;
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (!starget)
1533 return;
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (lun != SCAN_WILD_CARD) {
1536 /*
1537 * Scan for a specific host/chan/id/lun.
1538 */
1539 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1540 goto out_reap;
1541 }
1542
1543 /*
1544 * Scan LUN 0, if there is some response, scan further. Ideally, we
1545 * would not configure LUN 0 until all LUNs are scanned.
1546 */
James Bottomley6f3a2022005-09-22 20:33:28 -05001547 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1548 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1549 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 /*
1551 * The REPORT LUN did not scan the target,
1552 * do a sequential scan.
1553 */
1554 scsi_sequential_lun_scan(starget, bflags,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001555 starget->scsi_level, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 out_reap:
1559 /* now determine if the target has any children at all
1560 * and if not, nuke it */
1561 scsi_target_reap(starget);
1562
1563 put_device(&starget->dev);
1564}
Alan Sterne517d312005-07-26 10:18:45 -04001565
1566/**
1567 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1568 * target.
1569 * @parent: host to scan
1570 * @channel: channel to scan
1571 * @id: target id to scan
1572 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1573 * @rescan: passed to LUN scanning routines
1574 *
1575 * Description:
1576 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1577 * and possibly all LUNs on the target id.
1578 *
1579 * First try a REPORT LUN scan, if that does not scan the target, do a
1580 * sequential scan of LUNs on the target id.
1581 **/
1582void scsi_scan_target(struct device *parent, unsigned int channel,
1583 unsigned int id, unsigned int lun, int rescan)
1584{
1585 struct Scsi_Host *shost = dev_to_shost(parent);
1586
Matthew Wilcox93b45af2006-11-22 13:24:53 -07001587 if (strncmp(scsi_scan_type, "none", 4) == 0)
1588 return;
1589
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001590 mutex_lock(&shost->scan_mutex);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001591 if (!shost->async_scan)
1592 scsi_complete_async_scans();
1593
Alan Sterne517d312005-07-26 10:18:45 -04001594 if (scsi_host_scan_allowed(shost))
1595 __scsi_scan_target(parent, channel, id, lun, rescan);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001596 mutex_unlock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001597}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598EXPORT_SYMBOL(scsi_scan_target);
1599
1600static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1601 unsigned int id, unsigned int lun, int rescan)
1602{
1603 uint order_id;
1604
1605 if (id == SCAN_WILD_CARD)
1606 for (id = 0; id < shost->max_id; ++id) {
1607 /*
1608 * XXX adapter drivers when possible (FCP, iSCSI)
1609 * could modify max_id to match the current max,
1610 * not the absolute max.
1611 *
1612 * XXX add a shost id iterator, so for example,
1613 * the FC ID can be the same as a target id
1614 * without a huge overhead of sparse id's.
1615 */
1616 if (shost->reverse_ordering)
1617 /*
1618 * Scan from high to low id.
1619 */
1620 order_id = shost->max_id - id - 1;
1621 else
1622 order_id = id;
Alan Sterne517d312005-07-26 10:18:45 -04001623 __scsi_scan_target(&shost->shost_gendev, channel,
1624 order_id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626 else
Alan Sterne517d312005-07-26 10:18:45 -04001627 __scsi_scan_target(&shost->shost_gendev, channel,
1628 id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
1631int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1632 unsigned int id, unsigned int lun, int rescan)
1633{
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001634 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1635 "%s: <%u:%u:%u>\n",
1636 __FUNCTION__, channel, id, lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
Amit Arora091686d2006-05-19 16:14:50 -07001639 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1641 return -EINVAL;
1642
Arjan van de Ven0b950672006-01-11 13:16:10 +01001643 mutex_lock(&shost->scan_mutex);
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001644 if (!shost->async_scan)
1645 scsi_complete_async_scans();
1646
Mike Anderson82f29462005-06-16 11:14:33 -07001647 if (scsi_host_scan_allowed(shost)) {
1648 if (channel == SCAN_WILD_CARD)
1649 for (channel = 0; channel <= shost->max_channel;
1650 channel++)
1651 scsi_scan_channel(shost, channel, id, lun,
1652 rescan);
1653 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 scsi_scan_channel(shost, channel, id, lun, rescan);
Mike Anderson82f29462005-06-16 11:14:33 -07001655 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001656 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 return 0;
1659}
1660
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001661static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1662{
1663 struct scsi_device *sdev;
1664 shost_for_each_device(sdev, shost) {
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001665 if (!scsi_host_scan_allowed(shost) ||
1666 scsi_sysfs_add_sdev(sdev) != 0)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001667 scsi_destroy_sdev(sdev);
1668 }
1669}
1670
1671/**
1672 * scsi_prep_async_scan - prepare for an async scan
1673 * @shost: the host which will be scanned
1674 * Returns: a cookie to be passed to scsi_finish_async_scan()
1675 *
1676 * Tells the midlayer this host is going to do an asynchronous scan.
1677 * It reserves the host's position in the scanning list and ensures
1678 * that other asynchronous scans started after this one won't affect the
1679 * ordering of the discovered devices.
1680 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001681static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001682{
1683 struct async_scan_data *data;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001684 unsigned long flags;
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001685
1686 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1687 return NULL;
1688
1689 if (shost->async_scan) {
1690 printk("%s called twice for host %d", __FUNCTION__,
1691 shost->host_no);
1692 dump_stack();
1693 return NULL;
1694 }
1695
1696 data = kmalloc(sizeof(*data), GFP_KERNEL);
1697 if (!data)
1698 goto err;
1699 data->shost = scsi_host_get(shost);
1700 if (!data->shost)
1701 goto err;
1702 init_completion(&data->prev_finished);
1703
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001704 mutex_lock(&shost->scan_mutex);
1705 spin_lock_irqsave(shost->host_lock, flags);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001706 shost->async_scan = 1;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001707 spin_unlock_irqrestore(shost->host_lock, flags);
1708 mutex_unlock(&shost->scan_mutex);
1709
1710 spin_lock(&async_scan_lock);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001711 if (list_empty(&scanning_hosts))
1712 complete(&data->prev_finished);
1713 list_add_tail(&data->list, &scanning_hosts);
1714 spin_unlock(&async_scan_lock);
1715
1716 return data;
1717
1718 err:
1719 kfree(data);
1720 return NULL;
1721}
1722
1723/**
1724 * scsi_finish_async_scan - asynchronous scan has finished
1725 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1726 *
1727 * All the devices currently attached to this host have been found.
1728 * This function announces all the devices it has found to the rest
1729 * of the system.
1730 */
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001731static void scsi_finish_async_scan(struct async_scan_data *data)
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001732{
1733 struct Scsi_Host *shost;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001734 unsigned long flags;
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001735
1736 if (!data)
1737 return;
1738
1739 shost = data->shost;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001740
1741 mutex_lock(&shost->scan_mutex);
1742
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001743 if (!shost->async_scan) {
1744 printk("%s called twice for host %d", __FUNCTION__,
1745 shost->host_no);
1746 dump_stack();
1747 return;
1748 }
1749
1750 wait_for_completion(&data->prev_finished);
1751
1752 scsi_sysfs_add_devices(shost);
1753
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001754 spin_lock_irqsave(shost->host_lock, flags);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001755 shost->async_scan = 0;
Matthew Wilcox6b7f1232007-06-26 15:18:51 -06001756 spin_unlock_irqrestore(shost->host_lock, flags);
1757
1758 mutex_unlock(&shost->scan_mutex);
1759
1760 spin_lock(&async_scan_lock);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001761 list_del(&data->list);
1762 if (!list_empty(&scanning_hosts)) {
1763 struct async_scan_data *next = list_entry(scanning_hosts.next,
1764 struct async_scan_data, list);
1765 complete(&next->prev_finished);
1766 }
1767 spin_unlock(&async_scan_lock);
1768
1769 scsi_host_put(shost);
1770 kfree(data);
1771}
1772
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001773static void do_scsi_scan_host(struct Scsi_Host *shost)
1774{
1775 if (shost->hostt->scan_finished) {
1776 unsigned long start = jiffies;
1777 if (shost->hostt->scan_start)
1778 shost->hostt->scan_start(shost);
1779
1780 while (!shost->hostt->scan_finished(shost, jiffies - start))
1781 msleep(10);
1782 } else {
1783 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1784 SCAN_WILD_CARD, 0);
1785 }
1786}
1787
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001788static int do_scan_async(void *_data)
1789{
1790 struct async_scan_data *data = _data;
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001791 do_scsi_scan_host(data->shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001792 scsi_finish_async_scan(data);
1793 return 0;
1794}
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796/**
1797 * scsi_scan_host - scan the given adapter
1798 * @shost: adapter to scan
1799 **/
1800void scsi_scan_host(struct Scsi_Host *shost)
1801{
Matthew Wilcoxa57b1fc2007-08-20 09:18:48 -06001802 struct task_struct *p;
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001803 struct async_scan_data *data;
1804
1805 if (strncmp(scsi_scan_type, "none", 4) == 0)
1806 return;
1807
1808 data = scsi_prep_async_scan(shost);
1809 if (!data) {
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001810 do_scsi_scan_host(shost);
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001811 return;
1812 }
Matthew Wilcox1aa8fab2006-11-22 13:24:54 -07001813
Matthew Wilcoxa57b1fc2007-08-20 09:18:48 -06001814 p = kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1815 if (unlikely(IS_ERR(p)))
1816 do_scan_async(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818EXPORT_SYMBOL(scsi_scan_host);
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820void scsi_forget_host(struct Scsi_Host *shost)
1821{
Alan Sterna64358d2005-07-26 10:27:10 -04001822 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 unsigned long flags;
1824
Alan Sterna64358d2005-07-26 10:27:10 -04001825 restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001827 list_for_each_entry(sdev, &shost->__devices, siblings) {
1828 if (sdev->sdev_state == SDEV_DEL)
1829 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 spin_unlock_irqrestore(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001831 __scsi_remove_device(sdev);
1832 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
1834 spin_unlock_irqrestore(shost->host_lock, flags);
1835}
1836
1837/*
1838 * Function: scsi_get_host_dev()
1839 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001840 * Purpose: Create a scsi_device that points to the host adapter itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001842 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 *
1844 * Lock status: None assumed.
1845 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001846 * Returns: The scsi_device or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 *
1848 * Notes:
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001849 * Attach a single scsi_device to the Scsi_Host - this should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 * be made to look like a "pseudo-device" that points to the
1851 * HA itself.
1852 *
1853 * Note - this device is not accessible from any high-level
1854 * drivers (including generics), which is probably not
1855 * optimal. We can add hooks later to attach
1856 */
1857struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1858{
Alan Sterne517d312005-07-26 10:18:45 -04001859 struct scsi_device *sdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 struct scsi_target *starget;
1861
Arjan van de Ven0b950672006-01-11 13:16:10 +01001862 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001863 if (!scsi_host_scan_allowed(shost))
1864 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1866 if (!starget)
Alan Sterne517d312005-07-26 10:18:45 -04001867 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 sdev = scsi_alloc_sdev(starget, 0, NULL);
1870 if (sdev) {
1871 sdev->sdev_gendev.parent = get_device(&starget->dev);
1872 sdev->borken = 0;
James Bottomley884d25c2006-09-05 16:26:41 -05001873 } else
1874 scsi_target_reap(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 put_device(&starget->dev);
Alan Sterne517d312005-07-26 10:18:45 -04001876 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +01001877 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 return sdev;
1879}
1880EXPORT_SYMBOL(scsi_get_host_dev);
1881
1882/*
1883 * Function: scsi_free_host_dev()
1884 *
1885 * Purpose: Free a scsi_device that points to the host adapter itself.
1886 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001887 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 *
1889 * Lock status: None assumed.
1890 *
1891 * Returns: Nothing
1892 *
1893 * Notes:
1894 */
1895void scsi_free_host_dev(struct scsi_device *sdev)
1896{
1897 BUG_ON(sdev->id != sdev->host->this_id);
1898
James Bottomley6f3a2022005-09-22 20:33:28 -05001899 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901EXPORT_SYMBOL(scsi_free_host_dev);
1902