blob: 148e24cc3222a50f72ce480a0681865a7ceba0e1 [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/*
57 * Prefix values for the SCSI id's (stored in driverfs name field)
58 */
59#define SCSI_UID_SER_NUM 'S'
60#define SCSI_UID_UNKNOWN 'Z'
61
62/*
63 * Return values of some of the scanning functions.
64 *
65 * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
66 * includes allocation or general failures preventing IO from being sent.
67 *
68 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
69 * on the given LUN.
70 *
71 * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
72 * given LUN.
73 */
74#define SCSI_SCAN_NO_RESPONSE 0
75#define SCSI_SCAN_TARGET_PRESENT 1
76#define SCSI_SCAN_LUN_PRESENT 2
77
Arjan van de 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
88module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR);
89MODULE_PARM_DESC(max_luns,
90 "last scsi LUN (should be between 1 and 2^32-1)");
91
Matthew Wilcox3e082a92006-09-28 15:19:20 -060092static char scsi_scan_type[6] = "sync";
93
94module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
95MODULE_PARM_DESC(scan, "sync, async or none");
96
Linus Torvalds1da177e2005-04-16 15:20:36 -070097/*
98 * max_scsi_report_luns: the maximum number of LUNS that will be
99 * returned from the REPORT LUNS command. 8 times this value must
100 * be allocated. In theory this could be up to an 8 byte value, but
101 * in practice, the maximum number of LUNs suppored by any device
102 * is about 16k.
103 */
104static unsigned int max_scsi_report_luns = 511;
105
106module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
107MODULE_PARM_DESC(max_report_luns,
108 "REPORT LUNS maximum number of LUNS received (should be"
109 " between 1 and 16384)");
110
111static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
112
113module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
114MODULE_PARM_DESC(inq_timeout,
115 "Timeout (in seconds) waiting for devices to answer INQUIRY."
116 " Default is 5. Some non-compliant devices need more.");
117
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600118static DEFINE_SPINLOCK(async_scan_lock);
119static LIST_HEAD(scanning_hosts);
120
121struct async_scan_data {
122 struct list_head list;
123 struct Scsi_Host *shost;
124 struct completion prev_finished;
125};
126
127/**
128 * scsi_complete_async_scans - Wait for asynchronous scans to complete
129 *
130 * Asynchronous scans add themselves to the scanning_hosts list. Once
131 * that list is empty, we know that the scans are complete. Rather than
132 * waking up periodically to check the state of the list, we pretend to be
133 * a scanning task by adding ourselves at the end of the list and going to
134 * sleep. When the task before us wakes us up, we take ourselves off the
135 * list and return.
136 */
137int scsi_complete_async_scans(void)
138{
139 struct async_scan_data *data;
140
141 do {
142 if (list_empty(&scanning_hosts))
143 return 0;
144 /* If we can't get memory immediately, that's OK. Just
145 * sleep a little. Even if we never get memory, the async
146 * scans will finish eventually.
147 */
148 data = kmalloc(sizeof(*data), GFP_KERNEL);
149 if (!data)
150 msleep(1);
151 } while (!data);
152
153 data->shost = NULL;
154 init_completion(&data->prev_finished);
155
156 spin_lock(&async_scan_lock);
157 /* Check that there's still somebody else on the list */
158 if (list_empty(&scanning_hosts))
159 goto done;
160 list_add_tail(&data->list, &scanning_hosts);
161 spin_unlock(&async_scan_lock);
162
163 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
164 wait_for_completion(&data->prev_finished);
165
166 spin_lock(&async_scan_lock);
167 list_del(&data->list);
168 done:
169 spin_unlock(&async_scan_lock);
170
171 kfree(data);
172 return 0;
173}
174
175#ifdef MODULE
176/* Only exported for the benefit of scsi_wait_scan */
177EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
178#endif
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/**
181 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
James Bottomley39216032005-06-15 18:48:29 -0500182 * @sdev: scsi device to send command to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * @result: area to store the result of the MODE SENSE
184 *
185 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500186 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * Called for BLIST_KEY devices.
188 **/
James Bottomley39216032005-06-15 18:48:29 -0500189static void scsi_unlock_floptical(struct scsi_device *sdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned char *result)
191{
192 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
193
194 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
195 scsi_cmd[0] = MODE_SENSE;
196 scsi_cmd[1] = 0;
197 scsi_cmd[2] = 0x2e;
198 scsi_cmd[3] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500199 scsi_cmd[4] = 0x2a; /* size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 scsi_cmd[5] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500201 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
202 SCSI_TIMEOUT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 * scsi_alloc_sdev - allocate and setup a scsi_Device
207 *
208 * Description:
209 * Allocate, initialize for io, and return a pointer to a scsi_Device.
210 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
211 * adds scsi_Device to the appropriate list.
212 *
213 * Return value:
214 * scsi_Device pointer, or NULL on failure.
215 **/
216static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
217 unsigned int lun, void *hostdata)
218{
219 struct scsi_device *sdev;
220 int display_failure_msg = 1, ret;
221 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
222
Jes Sorensen24669f752006-01-16 10:31:18 -0500223 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 GFP_ATOMIC);
225 if (!sdev)
226 goto out;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 sdev->vendor = scsi_null_device_strs;
229 sdev->model = scsi_null_device_strs;
230 sdev->rev = scsi_null_device_strs;
231 sdev->host = shost;
232 sdev->id = starget->id;
233 sdev->lun = lun;
234 sdev->channel = starget->channel;
235 sdev->sdev_state = SDEV_CREATED;
236 INIT_LIST_HEAD(&sdev->siblings);
237 INIT_LIST_HEAD(&sdev->same_target_siblings);
238 INIT_LIST_HEAD(&sdev->cmd_list);
239 INIT_LIST_HEAD(&sdev->starved_entry);
240 spin_lock_init(&sdev->list_lock);
241
242 sdev->sdev_gendev.parent = get_device(&starget->dev);
243 sdev->sdev_target = starget;
244
245 /* usually NULL and set by ->slave_alloc instead */
246 sdev->hostdata = hostdata;
247
248 /* if the device needs this changing, it may do so in the
249 * slave_configure function */
250 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
251
252 /*
253 * Some low level driver could use device->type
254 */
255 sdev->type = -1;
256
257 /*
258 * Assume that the device will have handshaking problems,
259 * and then fix this field later if it turns out it
260 * doesn't
261 */
262 sdev->borken = 1;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 sdev->request_queue = scsi_alloc_queue(sdev);
265 if (!sdev->request_queue) {
266 /* release fn is set up in scsi_sysfs_device_initialise, so
267 * have to free and put manually here */
268 put_device(&starget->dev);
Dave Jones93f56082006-03-09 10:21:27 -0500269 kfree(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 goto out;
271 }
272
273 sdev->request_queue->queuedata = sdev;
274 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
275
276 scsi_sysfs_device_initialize(sdev);
277
278 if (shost->hostt->slave_alloc) {
279 ret = shost->hostt->slave_alloc(sdev);
280 if (ret) {
281 /*
282 * if LLDD reports slave not present, don't clutter
283 * console with alloc failure messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285 if (ret == -ENXIO)
286 display_failure_msg = 0;
287 goto out_device_destroy;
288 }
289 }
290
291 return sdev;
292
293out_device_destroy:
294 transport_destroy_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 put_device(&sdev->sdev_gendev);
296out:
297 if (display_failure_msg)
298 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
299 return NULL;
300}
301
302static void scsi_target_dev_release(struct device *dev)
303{
304 struct device *parent = dev->parent;
305 struct scsi_target *starget = to_scsi_target(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 kfree(starget);
308 put_device(parent);
309}
310
311int scsi_is_target_device(const struct device *dev)
312{
313 return dev->release == scsi_target_dev_release;
314}
315EXPORT_SYMBOL(scsi_is_target_device);
316
317static struct scsi_target *__scsi_find_target(struct device *parent,
318 int channel, uint id)
319{
320 struct scsi_target *starget, *found_starget = NULL;
321 struct Scsi_Host *shost = dev_to_shost(parent);
322 /*
323 * Search for an existing target for this sdev.
324 */
325 list_for_each_entry(starget, &shost->__targets, siblings) {
326 if (starget->id == id &&
327 starget->channel == channel) {
328 found_starget = starget;
329 break;
330 }
331 }
332 if (found_starget)
333 get_device(&found_starget->dev);
334
335 return found_starget;
336}
337
James Bottomley884d25c2006-09-05 16:26:41 -0500338/**
339 * scsi_alloc_target - allocate a new or find an existing target
340 * @parent: parent of the target (need not be a scsi host)
341 * @channel: target channel number (zero if no channels)
342 * @id: target id number
343 *
344 * Return an existing target if one exists, provided it hasn't already
345 * gone into STARGET_DEL state, otherwise allocate a new target.
346 *
347 * The target is returned with an incremented reference, so the caller
348 * is responsible for both reaping and doing a last put
349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350static struct scsi_target *scsi_alloc_target(struct device *parent,
351 int channel, uint id)
352{
353 struct Scsi_Host *shost = dev_to_shost(parent);
354 struct device *dev = NULL;
355 unsigned long flags;
356 const int size = sizeof(struct scsi_target)
357 + shost->transportt->target_size;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -0400358 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 struct scsi_target *found_target;
Brian King32f95792006-02-22 14:28:24 -0600360 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Jes Sorensen24669f752006-01-16 10:31:18 -0500362 starget = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (!starget) {
364 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
365 return NULL;
366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 dev = &starget->dev;
368 device_initialize(dev);
369 starget->reap_ref = 1;
370 dev->parent = get_device(parent);
371 dev->release = scsi_target_dev_release;
372 sprintf(dev->bus_id, "target%d:%d:%d",
373 shost->host_no, channel, id);
374 starget->id = id;
375 starget->channel = channel;
376 INIT_LIST_HEAD(&starget->siblings);
377 INIT_LIST_HEAD(&starget->devices);
James Bottomleyffedb452006-02-23 14:27:18 -0600378 starget->state = STARGET_RUNNING;
379 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_lock_irqsave(shost->host_lock, flags);
381
382 found_target = __scsi_find_target(parent, channel, id);
383 if (found_target)
384 goto found;
385
386 list_add_tail(&starget->siblings, &shost->__targets);
387 spin_unlock_irqrestore(shost->host_lock, flags);
388 /* allocate and add */
James Bottomleya283bd32005-05-24 12:06:38 -0500389 transport_setup_device(dev);
Brian King32f95792006-02-22 14:28:24 -0600390 error = device_add(dev);
391 if (error) {
392 dev_err(dev, "target device_add failed, error %d\n", error);
393 spin_lock_irqsave(shost->host_lock, flags);
394 list_del_init(&starget->siblings);
395 spin_unlock_irqrestore(shost->host_lock, flags);
396 transport_destroy_device(dev);
397 put_device(parent);
398 kfree(starget);
399 return NULL;
400 }
James Bottomleya283bd32005-05-24 12:06:38 -0500401 transport_add_device(dev);
402 if (shost->hostt->target_alloc) {
Brian King32f95792006-02-22 14:28:24 -0600403 error = shost->hostt->target_alloc(starget);
James Bottomleya283bd32005-05-24 12:06:38 -0500404
405 if(error) {
406 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
407 /* don't want scsi_target_reap to do the final
408 * put because it will be under the host lock */
409 get_device(dev);
410 scsi_target_reap(starget);
411 put_device(dev);
412 return NULL;
413 }
414 }
James Bottomley884d25c2006-09-05 16:26:41 -0500415 get_device(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 return starget;
418
419 found:
420 found_target->reap_ref++;
421 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600422 if (found_target->state != STARGET_DEL) {
James Bottomley884d25c2006-09-05 16:26:41 -0500423 put_device(parent);
James Bottomleyffedb452006-02-23 14:27:18 -0600424 kfree(starget);
425 return found_target;
426 }
427 /* Unfortunately, we found a dying target; need to
428 * wait until it's dead before we can get a new one */
429 put_device(&found_target->dev);
430 flush_scheduled_work();
431 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
James Bottomley65110b22006-02-14 10:48:46 -0600434static void scsi_target_reap_usercontext(void *data)
435{
436 struct scsi_target *starget = data;
James Bottomley863a9302005-12-15 20:01:43 -0800437 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
438 unsigned long flags;
439
James Bottomleyffedb452006-02-23 14:27:18 -0600440 transport_remove_device(&starget->dev);
441 device_del(&starget->dev);
442 transport_destroy_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800443 spin_lock_irqsave(shost->host_lock, flags);
Mike Andersona50a5e32006-03-14 11:18:46 -0800444 if (shost->hostt->target_destroy)
445 shost->hostt->target_destroy(starget);
James Bottomleyffedb452006-02-23 14:27:18 -0600446 list_del_init(&starget->siblings);
James Bottomley863a9302005-12-15 20:01:43 -0800447 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600448 put_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800449}
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451/**
452 * scsi_target_reap - check to see if target is in use and destroy if not
453 *
454 * @starget: target to be checked
455 *
456 * This is used after removing a LUN or doing a last put of the target
457 * it checks atomically that nothing is using the target and removes
458 * it if so.
459 */
460void scsi_target_reap(struct scsi_target *starget)
461{
James Bottomleyffedb452006-02-23 14:27:18 -0600462 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
463 unsigned long flags;
464
465 spin_lock_irqsave(shost->host_lock, flags);
466
467 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
468 BUG_ON(starget->state == STARGET_DEL);
469 starget->state = STARGET_DEL;
470 spin_unlock_irqrestore(shost->host_lock, flags);
471 execute_in_process_context(scsi_target_reap_usercontext,
472 starget, &starget->ew);
473 return;
474
475 }
476 spin_unlock_irqrestore(shost->host_lock, flags);
477
478 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481/**
Alan Sterne5b3cd42006-08-21 15:53:25 -0400482 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
483 * @s: INQUIRY result string to sanitize
484 * @len: length of the string
485 *
486 * Description:
487 * The SCSI spec says that INQUIRY vendor, product, and revision
488 * strings must consist entirely of graphic ASCII characters,
489 * padded on the right with spaces. Since not all devices obey
490 * this rule, we will replace non-graphic or non-ASCII characters
491 * with spaces. Exception: a NUL character is interpreted as a
492 * string terminator, so all the following characters are set to
493 * spaces.
494 **/
495static void sanitize_inquiry_string(unsigned char *s, int len)
496{
497 int terminated = 0;
498
499 for (; len > 0; (--len, ++s)) {
500 if (*s == 0)
501 terminated = 1;
502 if (terminated || *s < 0x20 || *s > 0x7e)
503 *s = ' ';
504 }
505}
506
507/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
James Bottomley39216032005-06-15 18:48:29 -0500509 * @sdev: scsi_device to probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * @inq_result: area to store the INQUIRY result
James Bottomley39216032005-06-15 18:48:29 -0500511 * @result_len: len of inq_result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * @bflags: store any bflags found here
513 *
514 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500515 * Probe the lun associated with @req using a standard SCSI INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 *
James Bottomley39216032005-06-15 18:48:29 -0500517 * If the INQUIRY is successful, zero is returned and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100519 * are copied to the scsi_device any flags value is stored in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400521static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
James Bottomley39216032005-06-15 18:48:29 -0500522 int result_len, int *bflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
525 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
526 int response_len = 0;
James Bottomley39216032005-06-15 18:48:29 -0500527 int pass, count, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 struct scsi_sense_hdr sshdr;
529
530 *bflags = 0;
531
532 /* Perform up to 3 passes. The first pass uses a conservative
533 * transfer length of 36 unless sdev->inquiry_len specifies a
534 * different value. */
535 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
536 try_inquiry_len = first_inquiry_len;
537 pass = 1;
538
539 next_pass:
James Bottomley9ccfc752005-10-02 11:45:08 -0500540 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
541 "scsi scan: INQUIRY pass %d length %d\n",
542 pass, try_inquiry_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* Each pass gets up to three chances to ignore Unit Attention */
545 for (count = 0; count < 3; ++count) {
546 memset(scsi_cmd, 0, 6);
547 scsi_cmd[0] = INQUIRY;
548 scsi_cmd[4] = (unsigned char) try_inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 memset(inq_result, 0, try_inquiry_len);
James Bottomley39216032005-06-15 18:48:29 -0500551
552 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500553 inq_result, try_inquiry_len, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -0500554 HZ / 2 + HZ * scsi_inq_timeout, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
557 "with code 0x%x\n",
James Bottomley39216032005-06-15 18:48:29 -0500558 result ? "failed" : "successful", result));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
James Bottomley39216032005-06-15 18:48:29 -0500560 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /*
562 * not-ready to ready transition [asc/ascq=0x28/0x0]
563 * or power-on, reset [asc/ascq=0x29/0x0], continue.
564 * INQUIRY should not yield UNIT_ATTENTION
565 * but many buggy devices do so anyway.
566 */
James Bottomley39216032005-06-15 18:48:29 -0500567 if ((driver_byte(result) & DRIVER_SENSE) &&
James Bottomleyea73a9f2005-08-28 11:33:52 -0500568 scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if ((sshdr.sense_key == UNIT_ATTENTION) &&
570 ((sshdr.asc == 0x28) ||
571 (sshdr.asc == 0x29)) &&
572 (sshdr.ascq == 0))
573 continue;
574 }
575 }
576 break;
577 }
578
James Bottomley39216032005-06-15 18:48:29 -0500579 if (result == 0) {
Alan Sterne5b3cd42006-08-21 15:53:25 -0400580 sanitize_inquiry_string(&inq_result[8], 8);
581 sanitize_inquiry_string(&inq_result[16], 16);
582 sanitize_inquiry_string(&inq_result[32], 4);
583
584 response_len = inq_result[4] + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (response_len > 255)
586 response_len = first_inquiry_len; /* sanity */
587
588 /*
589 * Get any flags for this device.
590 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100591 * XXX add a bflags to scsi_device, and replace the
592 * corresponding bit fields in scsi_device, so bflags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 * need not be passed as an argument.
594 */
595 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
596 &inq_result[16]);
597
598 /* When the first pass succeeds we gain information about
599 * what larger transfer lengths might work. */
600 if (pass == 1) {
601 if (BLIST_INQUIRY_36 & *bflags)
602 next_inquiry_len = 36;
603 else if (BLIST_INQUIRY_58 & *bflags)
604 next_inquiry_len = 58;
605 else if (sdev->inquiry_len)
606 next_inquiry_len = sdev->inquiry_len;
607 else
608 next_inquiry_len = response_len;
609
610 /* If more data is available perform the second pass */
611 if (next_inquiry_len > try_inquiry_len) {
612 try_inquiry_len = next_inquiry_len;
613 pass = 2;
614 goto next_pass;
615 }
616 }
617
618 } else if (pass == 2) {
619 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
620 "Consider BLIST_INQUIRY_36 for this device\n",
621 try_inquiry_len);
622
623 /* If this pass failed, the third pass goes back and transfers
624 * the same amount as we successfully got in the first pass. */
625 try_inquiry_len = first_inquiry_len;
626 pass = 3;
627 goto next_pass;
628 }
629
630 /* If the last transfer attempt got an error, assume the
631 * peripheral doesn't exist or is dead. */
James Bottomley39216032005-06-15 18:48:29 -0500632 if (result)
633 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 /* Don't report any more data than the device says is valid */
636 sdev->inquiry_len = min(try_inquiry_len, response_len);
637
638 /*
639 * XXX Abort if the response length is less than 36? If less than
640 * 32, the lookup of the device flags (above) could be invalid,
641 * and it would be possible to take an incorrect action - we do
642 * not want to hang because of a short INQUIRY. On the flip side,
643 * if the device is spun down or becoming ready (and so it gives a
644 * short INQUIRY), an abort here prevents any further use of the
645 * device, including spin up.
646 *
647 * Related to the above issue:
648 *
649 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
650 * and if not ready, sent a START_STOP to start (maybe spin up) and
651 * then send the INQUIRY again, since the INQUIRY can change after
652 * a device is initialized.
653 *
654 * Ideally, start a device if explicitly asked to do so. This
655 * assumes that a device is spun up on power on, spun down on
656 * request, and then spun up on request.
657 */
658
659 /*
660 * The scanning code needs to know the scsi_level, even if no
661 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
662 * non-zero LUNs can be scanned.
663 */
664 sdev->scsi_level = inq_result[2] & 0x07;
665 if (sdev->scsi_level >= 2 ||
666 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
667 sdev->scsi_level++;
James Bottomley6f3a2022005-09-22 20:33:28 -0500668 sdev->sdev_target->scsi_level = sdev->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
James Bottomley39216032005-06-15 18:48:29 -0500670 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/**
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100674 * scsi_add_lun - allocate and fully initialze a scsi_device
675 * @sdevscan: holds information to be stored in the new scsi_device
676 * @sdevnew: store the address of the newly allocated scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 * @inq_result: holds the result of a previous INQUIRY to the LUN
678 * @bflags: black/white list flag
679 *
680 * Description:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100681 * Allocate and initialize a scsi_device matching sdevscan. Optionally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * set fields based on values in *@bflags. If @sdevnew is not
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100683 * NULL, store the address of the new scsi_device in *@sdevnew (needed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 * when scanning a particular LUN).
685 *
686 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100687 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
688 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400690static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600691 int *bflags, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 /*
694 * XXX do not save the inquiry, since it can change underneath us,
695 * save just vendor/model/rev.
696 *
697 * Rather than save it and have an ioctl that retrieves the saved
698 * value, have an ioctl that executes the same INQUIRY code used
699 * in scsi_probe_lun, let user level programs doing INQUIRY
700 * scanning run at their own risk, or supply a user level program
701 * that can correctly scan.
702 */
703 sdev->inquiry = kmalloc(sdev->inquiry_len, GFP_ATOMIC);
704 if (sdev->inquiry == NULL) {
705 return SCSI_SCAN_NO_RESPONSE;
706 }
707
708 memcpy(sdev->inquiry, inq_result, sdev->inquiry_len);
709 sdev->vendor = (char *) (sdev->inquiry + 8);
710 sdev->model = (char *) (sdev->inquiry + 16);
711 sdev->rev = (char *) (sdev->inquiry + 32);
712
713 if (*bflags & BLIST_ISROM) {
714 /*
715 * It would be better to modify sdev->type, and set
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600716 * sdev->removable; this can now be done since
717 * print_inquiry has gone away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
719 inq_result[0] = TYPE_ROM;
720 inq_result[1] |= 0x80; /* removable */
721 } else if (*bflags & BLIST_NO_ULD_ATTACH)
722 sdev->no_uld_attach = 1;
723
724 switch (sdev->type = (inq_result[0] & 0x1f)) {
725 case TYPE_TAPE:
726 case TYPE_DISK:
727 case TYPE_PRINTER:
728 case TYPE_MOD:
729 case TYPE_PROCESSOR:
730 case TYPE_SCANNER:
731 case TYPE_MEDIUM_CHANGER:
732 case TYPE_ENCLOSURE:
733 case TYPE_COMM:
James Bottomley4d7db042006-03-31 20:07:45 -0600734 case TYPE_RAID:
Al Viro 631e8a12005-05-16 01:59:55 +0100735 case TYPE_RBC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 sdev->writeable = 1;
737 break;
738 case TYPE_WORM:
739 case TYPE_ROM:
740 sdev->writeable = 0;
741 break;
742 default:
743 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
744 }
745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 /*
747 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
748 * spec says: The device server is capable of supporting the
749 * specified peripheral device type on this logical unit. However,
750 * the physical device is not currently connected to this logical
751 * unit.
752 *
753 * The above is vague, as it implies that we could treat 001 and
754 * 011 the same. Stay compatible with previous code, and create a
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100755 * scsi_device for a PQ of 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 *
757 * Don't set the device offline here; rather let the upper
758 * level drivers eval the PQ to decide whether they should
759 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
760 */
761
762 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
763 sdev->removable = (0x80 & inq_result[1]) >> 7;
764 sdev->lockable = sdev->removable;
765 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
766
767 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
768 inq_result[56] & 0x04))
769 sdev->ppr = 1;
770 if (inq_result[7] & 0x60)
771 sdev->wdtr = 1;
772 if (inq_result[7] & 0x10)
773 sdev->sdtr = 1;
774
James Bottomley19ac0db2006-08-06 18:15:22 -0500775 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600776 "ANSI: %d%s\n", scsi_device_type(sdev->type),
777 sdev->vendor, sdev->model, sdev->rev,
778 sdev->inq_periph_qual, inq_result[2] & 0x07,
779 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /*
Greg KH5e3c34c2006-01-18 16:17:46 -0800782 * End sysfs code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
784
785 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
786 !(*bflags & BLIST_NOTQ))
787 sdev->tagged_supported = 1;
788 /*
789 * Some devices (Texel CD ROM drives) have handshaking problems
790 * when used with the Seagate controllers. borken is initialized
791 * to 1, and then set it to 0 here.
792 */
793 if ((*bflags & BLIST_BORKEN) == 0)
794 sdev->borken = 0;
795
796 /*
797 * Apparently some really broken devices (contrary to the SCSI
798 * standards) need to be selected without asserting ATN
799 */
800 if (*bflags & BLIST_SELECT_NO_ATN)
801 sdev->select_no_atn = 1;
802
803 /*
James Bottomley4d7db042006-03-31 20:07:45 -0600804 * Maximum 512 sector transfer length
805 * broken RA4x00 Compaq Disk Array
806 */
807 if (*bflags & BLIST_MAX_512)
808 blk_queue_max_sectors(sdev->request_queue, 512);
809
810 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * Some devices may not want to have a start command automatically
812 * issued when a device is added.
813 */
814 if (*bflags & BLIST_NOSTARTONADD)
815 sdev->no_start_on_add = 1;
816
817 if (*bflags & BLIST_SINGLELUN)
818 sdev->single_lun = 1;
819
820
821 sdev->use_10_for_rw = 1;
822
823 if (*bflags & BLIST_MS_SKIP_PAGE_08)
824 sdev->skip_ms_page_8 = 1;
825
826 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
827 sdev->skip_ms_page_3f = 1;
828
829 if (*bflags & BLIST_USE_10_BYTE_MS)
830 sdev->use_10_for_ms = 1;
831
832 /* set the device running here so that slave configure
833 * may do I/O */
834 scsi_device_set_state(sdev, SDEV_RUNNING);
835
836 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
837 sdev->use_192_bytes_for_3f = 1;
838
839 if (*bflags & BLIST_NOT_LOCKABLE)
840 sdev->lockable = 0;
841
842 if (*bflags & BLIST_RETRY_HWERROR)
843 sdev->retry_hwerror = 1;
844
845 transport_configure_device(&sdev->sdev_gendev);
846
Christoph Hellwig93805092006-02-17 12:11:29 +0100847 if (sdev->host->hostt->slave_configure) {
848 int ret = sdev->host->hostt->slave_configure(sdev);
849 if (ret) {
850 /*
851 * if LLDD reports slave not present, don't clutter
852 * console with alloc failure messages
853 */
854 if (ret != -ENXIO) {
855 sdev_printk(KERN_ERR, sdev,
856 "failed to configure device\n");
857 }
858 return SCSI_SCAN_NO_RESPONSE;
859 }
860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862 /*
863 * Ok, the device is now all set up, we can
864 * register it and tell the rest of the kernel
865 * about it.
866 */
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600867 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
Alan Sternb24b1032005-07-27 11:43:46 -0700868 return SCSI_SCAN_NO_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 return SCSI_SCAN_LUN_PRESENT;
871}
872
James Bottomley6f3a2022005-09-22 20:33:28 -0500873static inline void scsi_destroy_sdev(struct scsi_device *sdev)
874{
Brian King309bd272006-06-27 11:10:43 -0500875 scsi_device_set_state(sdev, SDEV_DEL);
James Bottomley6f3a2022005-09-22 20:33:28 -0500876 if (sdev->host->hostt->slave_destroy)
877 sdev->host->hostt->slave_destroy(sdev);
878 transport_destroy_device(&sdev->sdev_gendev);
879 put_device(&sdev->sdev_gendev);
880}
881
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700882#ifdef CONFIG_SCSI_LOGGING
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200883/**
884 * scsi_inq_str - print INQUIRY data from min to max index,
885 * strip trailing whitespace
886 * @buf: Output buffer with at least end-first+1 bytes of space
887 * @inq: Inquiry buffer (input)
888 * @first: Offset of string into inq
889 * @end: Index after last character in inq
890 */
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700891static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200892 unsigned first, unsigned end)
893{
894 unsigned term = 0, idx;
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700895
896 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
897 if (inq[idx+first] > ' ') {
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200898 buf[idx] = inq[idx+first];
899 term = idx+1;
900 } else {
901 buf[idx] = ' ';
902 }
903 }
904 buf[term] = 0;
905 return buf;
906}
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700907#endif
James Bottomley6f3a2022005-09-22 20:33:28 -0500908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909/**
910 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
911 * @starget: pointer to target device structure
912 * @lun: LUN of target device
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100913 * @sdevscan: probe the LUN corresponding to this scsi_device
914 * @sdevnew: store the value of any new scsi_device allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * @bflagsp: store bflags here if not NULL
916 *
917 * Description:
918 * Call scsi_probe_lun, if a LUN with an attached device is found,
919 * allocate and set it up by calling scsi_add_lun.
920 *
921 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100922 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
924 * attached at the LUN
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100925 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 **/
927static int scsi_probe_and_add_lun(struct scsi_target *starget,
928 uint lun, int *bflagsp,
929 struct scsi_device **sdevp, int rescan,
930 void *hostdata)
931{
932 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 unsigned char *result;
James Bottomley39216032005-06-15 18:48:29 -0500934 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
936
937 /*
938 * The rescan flag is used as an optimization, the first scan of a
939 * host adapter calls into here with rescan == 0.
940 */
James Bottomley6f3a2022005-09-22 20:33:28 -0500941 sdev = scsi_device_lookup_by_target(starget, lun);
942 if (sdev) {
943 if (rescan || sdev->sdev_state != SDEV_CREATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
945 "scsi scan: device exists on %s\n",
946 sdev->sdev_gendev.bus_id));
947 if (sdevp)
948 *sdevp = sdev;
949 else
950 scsi_device_put(sdev);
951
952 if (bflagsp)
953 *bflagsp = scsi_get_device_flags(sdev,
954 sdev->vendor,
955 sdev->model);
956 return SCSI_SCAN_LUN_PRESENT;
957 }
James Bottomley6f3a2022005-09-22 20:33:28 -0500958 scsi_device_put(sdev);
959 } else
960 sdev = scsi_alloc_sdev(starget, lun, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (!sdev)
962 goto out;
James Bottomley39216032005-06-15 18:48:29 -0500963
964 result = kmalloc(result_len, GFP_ATOMIC |
Al Virobc861202005-04-24 12:28:34 -0700965 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!result)
James Bottomley39216032005-06-15 18:48:29 -0500967 goto out_free_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
James Bottomley39216032005-06-15 18:48:29 -0500969 if (scsi_probe_lun(sdev, result, result_len, &bflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto out_free_result;
971
Kurt Garloff4186ab12006-04-03 15:16:48 +0200972 if (bflagsp)
973 *bflagsp = bflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /*
975 * result contains valid SCSI INQUIRY data.
976 */
Kurt Garloff13f7e5a2006-04-03 15:20:08 +0200977 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * For a Peripheral qualifier 3 (011b), the SCSI
980 * spec says: The device server is not capable of
981 * supporting a physical device on this logical
982 * unit.
983 *
984 * For disks, this implies that there is no
985 * logical disk configured at sdev->lun, but there
986 * is a target id responding.
987 */
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200988 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
989 " peripheral qualifier of 3, device not"
990 " added\n"))
991 if (lun == 0) {
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700992 SCSI_LOG_SCAN_BUS(1, {
993 unsigned char vend[9];
994 unsigned char mod[17];
995
996 sdev_printk(KERN_INFO, sdev,
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200997 "scsi scan: consider passing scsi_mod."
998 "dev_flags=%s:%s:0x240 or 0x800240\n",
999 scsi_inq_str(vend, result, 8, 16),
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001000 scsi_inq_str(mod, result, 16, 32));
1001 });
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001002 }
1003
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 res = SCSI_SCAN_TARGET_PRESENT;
1005 goto out_free_result;
1006 }
1007
Alan Stern1bfc5d92006-02-09 15:26:18 -05001008 /*
dave wysochanski84961f22006-08-09 14:56:32 -04001009 * Some targets may set slight variations of PQ and PDT to signal
1010 * that no LUN is present, so don't add sdev in these cases.
1011 * Two specific examples are:
1012 * 1) NetApp targets: return PQ=1, PDT=0x1f
1013 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1014 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1015 *
1016 * References:
1017 * 1) SCSI SPC-3, pp. 145-146
1018 * PQ=1: "A peripheral device having the specified peripheral
1019 * device type is not connected to this logical unit. However, the
1020 * device server is capable of supporting the specified peripheral
1021 * device type on this logical unit."
1022 * PDT=0x1f: "Unknown or no device type"
1023 * 2) USB UFI 1.0, p. 20
1024 * PDT=00h Direct-access device (floppy)
1025 * PDT=1Fh none (no FDD connected to the requested logical unit)
Alan Stern1bfc5d92006-02-09 15:26:18 -05001026 */
dave wysochanski84961f22006-08-09 14:56:32 -04001027 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1028 (result[0] & 0x1f) == 0x1f) {
Alan Stern1bfc5d92006-02-09 15:26:18 -05001029 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1030 "scsi scan: peripheral device type"
1031 " of 31, no device added\n"));
1032 res = SCSI_SCAN_TARGET_PRESENT;
1033 goto out_free_result;
1034 }
1035
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001036 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 if (res == SCSI_SCAN_LUN_PRESENT) {
1038 if (bflags & BLIST_KEY) {
1039 sdev->lockable = 0;
James Bottomley39216032005-06-15 18:48:29 -05001040 scsi_unlock_floptical(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
1044 out_free_result:
1045 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 out_free_sdev:
1047 if (res == SCSI_SCAN_LUN_PRESENT) {
1048 if (sdevp) {
Alan Sternb70d37b2005-07-26 10:30:40 -04001049 if (scsi_device_get(sdev) == 0) {
1050 *sdevp = sdev;
1051 } else {
1052 __scsi_remove_device(sdev);
1053 res = SCSI_SCAN_NO_RESPONSE;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001056 } else
1057 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 out:
1059 return res;
1060}
1061
1062/**
1063 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1064 * @starget: pointer to target structure to scan
1065 * @bflags: black/white list flag for LUN 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 *
1067 * Description:
1068 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1069 * scanned) to some maximum lun until a LUN is found with no device
1070 * attached. Use the bflags to figure out any oddities.
1071 *
1072 * Modifies sdevscan->lun.
1073 **/
1074static void scsi_sequential_lun_scan(struct scsi_target *starget,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001075 int bflags, int scsi_level, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076{
1077 unsigned int sparse_lun, lun, max_dev_lun;
1078 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1079
1080 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1081 "%s\n", starget->dev.bus_id));
1082
1083 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1084 /*
1085 * If this device is known to support sparse multiple units,
1086 * override the other settings, and scan all of them. Normally,
1087 * SCSI-3 devices should be scanned via the REPORT LUNS.
1088 */
1089 if (bflags & BLIST_SPARSELUN) {
1090 max_dev_lun = shost->max_lun;
1091 sparse_lun = 1;
1092 } else
1093 sparse_lun = 0;
1094
1095 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 * If less than SCSI_1_CSS, and no special lun scaning, stop
1097 * scanning; this matches 2.4 behaviour, but could just be a bug
1098 * (to continue scanning a SCSI_1_CSS device).
1099 *
1100 * This test is broken. We might not have any device on lun0 for
1101 * a sparselun device, and if that's the case then how would we
1102 * know the real scsi_level, eh? It might make sense to just not
1103 * scan any SCSI_1 device for non-0 luns, but that check would best
1104 * go into scsi_alloc_sdev() and just have it return null when asked
1105 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1106 *
1107 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1108 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1109 == 0))
1110 return;
1111 */
1112 /*
1113 * If this device is known to support multiple units, override
1114 * the other settings, and scan all of them.
1115 */
1116 if (bflags & BLIST_FORCELUN)
1117 max_dev_lun = shost->max_lun;
1118 /*
1119 * REGAL CDC-4X: avoid hang after LUN 4
1120 */
1121 if (bflags & BLIST_MAX5LUN)
1122 max_dev_lun = min(5U, max_dev_lun);
1123 /*
1124 * Do not scan SCSI-2 or lower device past LUN 7, unless
1125 * BLIST_LARGELUN.
1126 */
1127 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1128 max_dev_lun = min(8U, max_dev_lun);
1129
1130 /*
1131 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1132 * until we reach the max, or no LUN is found and we are not
1133 * sparse_lun.
1134 */
1135 for (lun = 1; lun < max_dev_lun; ++lun)
1136 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1137 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1138 !sparse_lun)
1139 return;
1140}
1141
1142/**
1143 * scsilun_to_int: convert a scsi_lun to an int
1144 * @scsilun: struct scsi_lun to be converted.
1145 *
1146 * Description:
1147 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1148 * integer, and return the result. The caller must check for
1149 * truncation before using this function.
1150 *
1151 * Notes:
1152 * The struct scsi_lun is assumed to be four levels, with each level
1153 * effectively containing a SCSI byte-ordered (big endian) short; the
1154 * addressing bits of each level are ignored (the highest two bits).
1155 * For a description of the LUN format, post SCSI-3 see the SCSI
1156 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1157 *
1158 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1159 * the integer: 0x0b030a04
1160 **/
1161static int scsilun_to_int(struct scsi_lun *scsilun)
1162{
1163 int i;
1164 unsigned int lun;
1165
1166 lun = 0;
1167 for (i = 0; i < sizeof(lun); i += 2)
1168 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1169 scsilun->scsi_lun[i + 1]) << (i * 8));
1170 return lun;
1171}
1172
1173/**
James.Smart@Emulex.Com2f4701d2005-07-13 22:05:03 -04001174 * int_to_scsilun: reverts an int into a scsi_lun
1175 * @int: integer to be reverted
1176 * @scsilun: struct scsi_lun to be set.
1177 *
1178 * Description:
1179 * Reverts the functionality of the scsilun_to_int, which packed
1180 * an 8-byte lun value into an int. This routine unpacks the int
1181 * back into the lun value.
1182 * Note: the scsilun_to_int() routine does not truly handle all
1183 * 8bytes of the lun value. This functions restores only as much
1184 * as was set by the routine.
1185 *
1186 * Notes:
1187 * Given an integer : 0x0b030a04, this function returns a
1188 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1189 *
1190 **/
1191void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1192{
1193 int i;
1194
1195 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1196
1197 for (i = 0; i < sizeof(lun); i += 2) {
1198 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1199 scsilun->scsi_lun[i+1] = lun & 0xFF;
1200 lun = lun >> 16;
1201 }
1202}
1203EXPORT_SYMBOL(int_to_scsilun);
1204
1205/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001207 * @sdevscan: scan the host, channel, and id of this scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 *
1209 * Description:
1210 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1211 * command, and scan the resulting list of LUNs by calling
1212 * scsi_probe_and_add_lun.
1213 *
1214 * Modifies sdevscan->lun.
1215 *
1216 * Return:
1217 * 0: scan completed (or no memory, so further scanning is futile)
1218 * 1: no report lun scan, or not configured
1219 **/
James Bottomley6f3a2022005-09-22 20:33:28 -05001220static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int rescan)
1222{
1223 char devname[64];
1224 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1225 unsigned int length;
1226 unsigned int lun;
1227 unsigned int num_luns;
1228 unsigned int retries;
James Bottomley39216032005-06-15 18:48:29 -05001229 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 struct scsi_lun *lunp, *lun_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 u8 *data;
1232 struct scsi_sense_hdr sshdr;
James Bottomley6f3a2022005-09-22 20:33:28 -05001233 struct scsi_device *sdev;
1234 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
Alan Stern2ef89192005-11-08 15:51:55 -05001235 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 /*
1238 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1239 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1240 * support more than 8 LUNs.
1241 */
James Bottomley4d7db042006-03-31 20:07:45 -06001242 if (bflags & BLIST_NOREPORTLUN)
1243 return 1;
1244 if (starget->scsi_level < SCSI_2 &&
1245 starget->scsi_level != SCSI_UNKNOWN)
1246 return 1;
1247 if (starget->scsi_level < SCSI_3 &&
1248 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return 1;
1250 if (bflags & BLIST_NOLUN)
1251 return 0;
1252
James Bottomley6f3a2022005-09-22 20:33:28 -05001253 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1254 sdev = scsi_alloc_sdev(starget, 0, NULL);
1255 if (!sdev)
1256 return 0;
1257 if (scsi_device_get(sdev))
1258 return 0;
1259 }
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 sprintf(devname, "host %d channel %d id %d",
James Bottomley6f3a2022005-09-22 20:33:28 -05001262 shost->host_no, sdev->channel, sdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 /*
1265 * Allocate enough to hold the header (the same size as one scsi_lun)
1266 * plus the max number of luns we are requesting.
1267 *
1268 * Reallocating and trying again (with the exact amount we need)
1269 * would be nice, but then we need to somehow limit the size
1270 * allocated based on the available memory and the limits of
1271 * kmalloc - we don't want a kmalloc() failure of a huge value to
1272 * prevent us from finding any LUNs on this target.
1273 */
1274 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1275 lun_data = kmalloc(length, GFP_ATOMIC |
1276 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
James Bottomley6f3a2022005-09-22 20:33:28 -05001277 if (!lun_data) {
1278 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
James Bottomley39216032005-06-15 18:48:29 -05001279 goto out;
James Bottomley6f3a2022005-09-22 20:33:28 -05001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
1282 scsi_cmd[0] = REPORT_LUNS;
1283
1284 /*
1285 * bytes 1 - 5: reserved, set to zero.
1286 */
1287 memset(&scsi_cmd[1], 0, 5);
1288
1289 /*
1290 * bytes 6 - 9: length of the command.
1291 */
1292 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1293 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1294 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1295 scsi_cmd[9] = (unsigned char) length & 0xff;
1296
1297 scsi_cmd[10] = 0; /* reserved */
1298 scsi_cmd[11] = 0; /* control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /*
1301 * We can get a UNIT ATTENTION, for example a power on/reset, so
1302 * retry a few times (like sd.c does for TEST UNIT READY).
1303 * Experience shows some combinations of adapter/devices get at
1304 * least two power on/resets.
1305 *
1306 * Illegal requests (for devices that do not support REPORT LUNS)
1307 * should come through as a check condition, and will not generate
1308 * a retry.
1309 */
1310 for (retries = 0; retries < 3; retries++) {
1311 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1312 " REPORT LUNS to %s (try %d)\n", devname,
1313 retries));
James Bottomley39216032005-06-15 18:48:29 -05001314
James Bottomley39216032005-06-15 18:48:29 -05001315 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001316 lun_data, length, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -05001317 SCSI_TIMEOUT + 4 * HZ, 3);
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
James Bottomley39216032005-06-15 18:48:29 -05001320 " %s (try %d) result 0x%x\n", result
1321 ? "failed" : "successful", retries, result));
1322 if (result == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 break;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001324 else if (scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 if (sshdr.sense_key != UNIT_ATTENTION)
1326 break;
1327 }
1328 }
1329
James Bottomley39216032005-06-15 18:48:29 -05001330 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /*
1332 * The device probably does not support a REPORT LUN command
1333 */
Alan Stern2ef89192005-11-08 15:51:55 -05001334 ret = 1;
1335 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /*
1339 * Get the length from the first four bytes of lun_data.
1340 */
1341 data = (u8 *) lun_data->scsi_lun;
1342 length = ((data[0] << 24) | (data[1] << 16) |
1343 (data[2] << 8) | (data[3] << 0));
1344
1345 num_luns = (length / sizeof(struct scsi_lun));
1346 if (num_luns > max_scsi_report_luns) {
1347 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1348 " of %d luns reported, try increasing"
1349 " max_scsi_report_luns.\n", devname,
1350 max_scsi_report_luns, num_luns);
1351 num_luns = max_scsi_report_luns;
1352 }
1353
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001354 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1355 "scsi scan: REPORT LUN scan\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /*
1358 * Scan the luns in lun_data. The entry at offset 0 is really
1359 * the header, so start at 1 and go up to and including num_luns.
1360 */
1361 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1362 lun = scsilun_to_int(lunp);
1363
1364 /*
1365 * Check if the unused part of lunp is non-zero, and so
1366 * does not fit in lun.
1367 */
1368 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1369 int i;
1370
1371 /*
1372 * Output an error displaying the LUN in byte order,
1373 * this differs from what linux would print for the
1374 * integer LUN value.
1375 */
1376 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1377 data = (char *)lunp->scsi_lun;
1378 for (i = 0; i < sizeof(struct scsi_lun); i++)
1379 printk("%02x", data[i]);
1380 printk(" has a LUN larger than currently supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 } else if (lun > sdev->host->max_lun) {
1382 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1383 " than allowed by the host adapter\n",
1384 devname, lun);
1385 } else {
1386 int res;
1387
1388 res = scsi_probe_and_add_lun(starget,
1389 lun, NULL, NULL, rescan, NULL);
1390 if (res == SCSI_SCAN_NO_RESPONSE) {
1391 /*
1392 * Got some results, but now none, abort.
1393 */
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001394 sdev_printk(KERN_ERR, sdev,
1395 "Unexpected response"
1396 " from lun %d while scanning, scan"
1397 " aborted\n", lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
1399 }
1400 }
1401 }
1402
Alan Stern2ef89192005-11-08 15:51:55 -05001403 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 kfree(lun_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 out:
James Bottomley6f3a2022005-09-22 20:33:28 -05001406 scsi_device_put(sdev);
1407 if (sdev->sdev_state == SDEV_CREATED)
1408 /*
1409 * the sdev we used didn't appear in the report luns scan
1410 */
1411 scsi_destroy_sdev(sdev);
Alan Stern2ef89192005-11-08 15:51:55 -05001412 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
1415struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1416 uint id, uint lun, void *hostdata)
1417{
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001418 struct scsi_device *sdev = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 struct device *parent = &shost->shost_gendev;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001420 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001422 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!starget)
1424 return ERR_PTR(-ENOMEM);
1425
Arjan van de Ven0b950672006-01-11 13:16:10 +01001426 mutex_lock(&shost->scan_mutex);
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001427 if (scsi_host_scan_allowed(shost))
1428 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001429 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 scsi_target_reap(starget);
1431 put_device(&starget->dev);
1432
1433 return sdev;
1434}
1435EXPORT_SYMBOL(__scsi_add_device);
1436
James Bottomley146f7262005-09-10 12:44:09 -05001437int scsi_add_device(struct Scsi_Host *host, uint channel,
1438 uint target, uint lun)
1439{
1440 struct scsi_device *sdev =
1441 __scsi_add_device(host, channel, target, lun, NULL);
1442 if (IS_ERR(sdev))
1443 return PTR_ERR(sdev);
1444
1445 scsi_device_put(sdev);
1446 return 0;
1447}
1448EXPORT_SYMBOL(scsi_add_device);
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450void scsi_rescan_device(struct device *dev)
1451{
1452 struct scsi_driver *drv;
1453
1454 if (!dev->driver)
1455 return;
1456
1457 drv = to_scsi_driver(dev->driver);
1458 if (try_module_get(drv->owner)) {
1459 if (drv->rescan)
1460 drv->rescan(dev);
1461 module_put(drv->owner);
1462 }
1463}
1464EXPORT_SYMBOL(scsi_rescan_device);
1465
Alan Sterne517d312005-07-26 10:18:45 -04001466static void __scsi_scan_target(struct device *parent, unsigned int channel,
1467 unsigned int id, unsigned int lun, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468{
1469 struct Scsi_Host *shost = dev_to_shost(parent);
1470 int bflags = 0;
1471 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 struct scsi_target *starget;
1473
1474 if (shost->this_id == id)
1475 /*
1476 * Don't scan the host adapter
1477 */
1478 return;
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 if (!starget)
1482 return;
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 if (lun != SCAN_WILD_CARD) {
1485 /*
1486 * Scan for a specific host/chan/id/lun.
1487 */
1488 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1489 goto out_reap;
1490 }
1491
1492 /*
1493 * Scan LUN 0, if there is some response, scan further. Ideally, we
1494 * would not configure LUN 0 until all LUNs are scanned.
1495 */
James Bottomley6f3a2022005-09-22 20:33:28 -05001496 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1497 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1498 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 /*
1500 * The REPORT LUN did not scan the target,
1501 * do a sequential scan.
1502 */
1503 scsi_sequential_lun_scan(starget, bflags,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001504 starget->scsi_level, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 out_reap:
1508 /* now determine if the target has any children at all
1509 * and if not, nuke it */
1510 scsi_target_reap(starget);
1511
1512 put_device(&starget->dev);
1513}
Alan Sterne517d312005-07-26 10:18:45 -04001514
1515/**
1516 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1517 * target.
1518 * @parent: host to scan
1519 * @channel: channel to scan
1520 * @id: target id to scan
1521 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1522 * @rescan: passed to LUN scanning routines
1523 *
1524 * Description:
1525 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1526 * and possibly all LUNs on the target id.
1527 *
1528 * First try a REPORT LUN scan, if that does not scan the target, do a
1529 * sequential scan of LUNs on the target id.
1530 **/
1531void scsi_scan_target(struct device *parent, unsigned int channel,
1532 unsigned int id, unsigned int lun, int rescan)
1533{
1534 struct Scsi_Host *shost = dev_to_shost(parent);
1535
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001536 if (!shost->async_scan)
1537 scsi_complete_async_scans();
1538
Arjan van de Ven0b950672006-01-11 13:16:10 +01001539 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001540 if (scsi_host_scan_allowed(shost))
1541 __scsi_scan_target(parent, channel, id, lun, rescan);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001542 mutex_unlock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544EXPORT_SYMBOL(scsi_scan_target);
1545
1546static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1547 unsigned int id, unsigned int lun, int rescan)
1548{
1549 uint order_id;
1550
1551 if (id == SCAN_WILD_CARD)
1552 for (id = 0; id < shost->max_id; ++id) {
1553 /*
1554 * XXX adapter drivers when possible (FCP, iSCSI)
1555 * could modify max_id to match the current max,
1556 * not the absolute max.
1557 *
1558 * XXX add a shost id iterator, so for example,
1559 * the FC ID can be the same as a target id
1560 * without a huge overhead of sparse id's.
1561 */
1562 if (shost->reverse_ordering)
1563 /*
1564 * Scan from high to low id.
1565 */
1566 order_id = shost->max_id - id - 1;
1567 else
1568 order_id = id;
Alan Sterne517d312005-07-26 10:18:45 -04001569 __scsi_scan_target(&shost->shost_gendev, channel,
1570 order_id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 }
1572 else
Alan Sterne517d312005-07-26 10:18:45 -04001573 __scsi_scan_target(&shost->shost_gendev, channel,
1574 id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575}
1576
1577int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1578 unsigned int id, unsigned int lun, int rescan)
1579{
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001580 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1581 "%s: <%u:%u:%u>\n",
1582 __FUNCTION__, channel, id, lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001584 if (!shost->async_scan)
1585 scsi_complete_async_scans();
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
Amit Arora091686d2006-05-19 16:14:50 -07001588 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1590 return -EINVAL;
1591
Arjan van de Ven0b950672006-01-11 13:16:10 +01001592 mutex_lock(&shost->scan_mutex);
Mike Anderson82f29462005-06-16 11:14:33 -07001593 if (scsi_host_scan_allowed(shost)) {
1594 if (channel == SCAN_WILD_CARD)
1595 for (channel = 0; channel <= shost->max_channel;
1596 channel++)
1597 scsi_scan_channel(shost, channel, id, lun,
1598 rescan);
1599 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 scsi_scan_channel(shost, channel, id, lun, rescan);
Mike Anderson82f29462005-06-16 11:14:33 -07001601 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001602 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 return 0;
1605}
1606
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001607static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1608{
1609 struct scsi_device *sdev;
1610 shost_for_each_device(sdev, shost) {
1611 if (scsi_sysfs_add_sdev(sdev) != 0)
1612 scsi_destroy_sdev(sdev);
1613 }
1614}
1615
1616/**
1617 * scsi_prep_async_scan - prepare for an async scan
1618 * @shost: the host which will be scanned
1619 * Returns: a cookie to be passed to scsi_finish_async_scan()
1620 *
1621 * Tells the midlayer this host is going to do an asynchronous scan.
1622 * It reserves the host's position in the scanning list and ensures
1623 * that other asynchronous scans started after this one won't affect the
1624 * ordering of the discovered devices.
1625 */
1626struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1627{
1628 struct async_scan_data *data;
1629
1630 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1631 return NULL;
1632
1633 if (shost->async_scan) {
1634 printk("%s called twice for host %d", __FUNCTION__,
1635 shost->host_no);
1636 dump_stack();
1637 return NULL;
1638 }
1639
1640 data = kmalloc(sizeof(*data), GFP_KERNEL);
1641 if (!data)
1642 goto err;
1643 data->shost = scsi_host_get(shost);
1644 if (!data->shost)
1645 goto err;
1646 init_completion(&data->prev_finished);
1647
1648 spin_lock(&async_scan_lock);
1649 shost->async_scan = 1;
1650 if (list_empty(&scanning_hosts))
1651 complete(&data->prev_finished);
1652 list_add_tail(&data->list, &scanning_hosts);
1653 spin_unlock(&async_scan_lock);
1654
1655 return data;
1656
1657 err:
1658 kfree(data);
1659 return NULL;
1660}
1661
1662/**
1663 * scsi_finish_async_scan - asynchronous scan has finished
1664 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1665 *
1666 * All the devices currently attached to this host have been found.
1667 * This function announces all the devices it has found to the rest
1668 * of the system.
1669 */
1670void scsi_finish_async_scan(struct async_scan_data *data)
1671{
1672 struct Scsi_Host *shost;
1673
1674 if (!data)
1675 return;
1676
1677 shost = data->shost;
1678 if (!shost->async_scan) {
1679 printk("%s called twice for host %d", __FUNCTION__,
1680 shost->host_no);
1681 dump_stack();
1682 return;
1683 }
1684
1685 wait_for_completion(&data->prev_finished);
1686
1687 scsi_sysfs_add_devices(shost);
1688
1689 spin_lock(&async_scan_lock);
1690 shost->async_scan = 0;
1691 list_del(&data->list);
1692 if (!list_empty(&scanning_hosts)) {
1693 struct async_scan_data *next = list_entry(scanning_hosts.next,
1694 struct async_scan_data, list);
1695 complete(&next->prev_finished);
1696 }
1697 spin_unlock(&async_scan_lock);
1698
1699 scsi_host_put(shost);
1700 kfree(data);
1701}
1702
1703static int do_scan_async(void *_data)
1704{
1705 struct async_scan_data *data = _data;
1706 scsi_scan_host_selected(data->shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1707 SCAN_WILD_CARD, 0);
1708
1709 scsi_finish_async_scan(data);
1710 return 0;
1711}
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713/**
1714 * scsi_scan_host - scan the given adapter
1715 * @shost: adapter to scan
1716 **/
1717void scsi_scan_host(struct Scsi_Host *shost)
1718{
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001719 struct async_scan_data *data;
1720
1721 if (strncmp(scsi_scan_type, "none", 4) == 0)
1722 return;
1723
1724 data = scsi_prep_async_scan(shost);
1725 if (!data) {
1726 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1727 SCAN_WILD_CARD, 0);
1728 return;
1729 }
1730 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732EXPORT_SYMBOL(scsi_scan_host);
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734void scsi_forget_host(struct Scsi_Host *shost)
1735{
Alan Sterna64358d2005-07-26 10:27:10 -04001736 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 unsigned long flags;
1738
Alan Sterna64358d2005-07-26 10:27:10 -04001739 restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001741 list_for_each_entry(sdev, &shost->__devices, siblings) {
1742 if (sdev->sdev_state == SDEV_DEL)
1743 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 spin_unlock_irqrestore(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001745 __scsi_remove_device(sdev);
1746 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 }
1748 spin_unlock_irqrestore(shost->host_lock, flags);
1749}
1750
1751/*
1752 * Function: scsi_get_host_dev()
1753 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001754 * Purpose: Create a scsi_device that points to the host adapter itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001756 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 *
1758 * Lock status: None assumed.
1759 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001760 * Returns: The scsi_device or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 *
1762 * Notes:
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001763 * Attach a single scsi_device to the Scsi_Host - this should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 * be made to look like a "pseudo-device" that points to the
1765 * HA itself.
1766 *
1767 * Note - this device is not accessible from any high-level
1768 * drivers (including generics), which is probably not
1769 * optimal. We can add hooks later to attach
1770 */
1771struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1772{
Alan Sterne517d312005-07-26 10:18:45 -04001773 struct scsi_device *sdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct scsi_target *starget;
1775
Arjan van de Ven0b950672006-01-11 13:16:10 +01001776 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001777 if (!scsi_host_scan_allowed(shost))
1778 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1780 if (!starget)
Alan Sterne517d312005-07-26 10:18:45 -04001781 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782
1783 sdev = scsi_alloc_sdev(starget, 0, NULL);
1784 if (sdev) {
1785 sdev->sdev_gendev.parent = get_device(&starget->dev);
1786 sdev->borken = 0;
James Bottomley884d25c2006-09-05 16:26:41 -05001787 } else
1788 scsi_target_reap(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 put_device(&starget->dev);
Alan Sterne517d312005-07-26 10:18:45 -04001790 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +01001791 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return sdev;
1793}
1794EXPORT_SYMBOL(scsi_get_host_dev);
1795
1796/*
1797 * Function: scsi_free_host_dev()
1798 *
1799 * Purpose: Free a scsi_device that points to the host adapter itself.
1800 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001801 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 *
1803 * Lock status: None assumed.
1804 *
1805 * Returns: Nothing
1806 *
1807 * Notes:
1808 */
1809void scsi_free_host_dev(struct scsi_device *sdev)
1810{
1811 BUG_ON(sdev->id != sdev->host->this_id);
1812
James Bottomley6f3a2022005-09-22 20:33:28 -05001813 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814}
1815EXPORT_SYMBOL(scsi_free_host_dev);
1816