blob: 3ccaa4be92d83cd1f165db3888ff170c8012e17c [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 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
112module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
113MODULE_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
119module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
120MODULE_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 Wilcox3e082a92006-09-28 15:19:20 -0600124static DEFINE_SPINLOCK(async_scan_lock);
125static LIST_HEAD(scanning_hosts);
126
127struct async_scan_data {
128 struct list_head list;
129 struct Scsi_Host *shost;
130 struct completion prev_finished;
131};
132
133/**
134 * scsi_complete_async_scans - Wait for asynchronous scans to complete
135 *
136 * Asynchronous scans add themselves to the scanning_hosts list. Once
137 * that list is empty, we know that the scans are complete. Rather than
138 * waking up periodically to check the state of the list, we pretend to be
139 * a scanning task by adding ourselves at the end of the list and going to
140 * sleep. When the task before us wakes us up, we take ourselves off the
141 * list and return.
142 */
143int scsi_complete_async_scans(void)
144{
145 struct async_scan_data *data;
146
147 do {
148 if (list_empty(&scanning_hosts))
149 return 0;
150 /* If we can't get memory immediately, that's OK. Just
151 * sleep a little. Even if we never get memory, the async
152 * scans will finish eventually.
153 */
154 data = kmalloc(sizeof(*data), GFP_KERNEL);
155 if (!data)
156 msleep(1);
157 } while (!data);
158
159 data->shost = NULL;
160 init_completion(&data->prev_finished);
161
162 spin_lock(&async_scan_lock);
163 /* Check that there's still somebody else on the list */
164 if (list_empty(&scanning_hosts))
165 goto done;
166 list_add_tail(&data->list, &scanning_hosts);
167 spin_unlock(&async_scan_lock);
168
169 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
170 wait_for_completion(&data->prev_finished);
171
172 spin_lock(&async_scan_lock);
173 list_del(&data->list);
174 done:
175 spin_unlock(&async_scan_lock);
176
177 kfree(data);
178 return 0;
179}
180
181#ifdef MODULE
182/* Only exported for the benefit of scsi_wait_scan */
183EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
184#endif
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/**
187 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
James Bottomley39216032005-06-15 18:48:29 -0500188 * @sdev: scsi device to send command to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * @result: area to store the result of the MODE SENSE
190 *
191 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500192 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * Called for BLIST_KEY devices.
194 **/
James Bottomley39216032005-06-15 18:48:29 -0500195static void scsi_unlock_floptical(struct scsi_device *sdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 unsigned char *result)
197{
198 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
199
200 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
201 scsi_cmd[0] = MODE_SENSE;
202 scsi_cmd[1] = 0;
203 scsi_cmd[2] = 0x2e;
204 scsi_cmd[3] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500205 scsi_cmd[4] = 0x2a; /* size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 scsi_cmd[5] = 0;
James Bottomley39216032005-06-15 18:48:29 -0500207 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
208 SCSI_TIMEOUT, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * scsi_alloc_sdev - allocate and setup a scsi_Device
213 *
214 * Description:
215 * Allocate, initialize for io, and return a pointer to a scsi_Device.
216 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
217 * adds scsi_Device to the appropriate list.
218 *
219 * Return value:
220 * scsi_Device pointer, or NULL on failure.
221 **/
222static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
223 unsigned int lun, void *hostdata)
224{
225 struct scsi_device *sdev;
226 int display_failure_msg = 1, ret;
227 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
228
Jes Sorensen24669f752006-01-16 10:31:18 -0500229 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 GFP_ATOMIC);
231 if (!sdev)
232 goto out;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 sdev->vendor = scsi_null_device_strs;
235 sdev->model = scsi_null_device_strs;
236 sdev->rev = scsi_null_device_strs;
237 sdev->host = shost;
238 sdev->id = starget->id;
239 sdev->lun = lun;
240 sdev->channel = starget->channel;
241 sdev->sdev_state = SDEV_CREATED;
242 INIT_LIST_HEAD(&sdev->siblings);
243 INIT_LIST_HEAD(&sdev->same_target_siblings);
244 INIT_LIST_HEAD(&sdev->cmd_list);
245 INIT_LIST_HEAD(&sdev->starved_entry);
246 spin_lock_init(&sdev->list_lock);
247
248 sdev->sdev_gendev.parent = get_device(&starget->dev);
249 sdev->sdev_target = starget;
250
251 /* usually NULL and set by ->slave_alloc instead */
252 sdev->hostdata = hostdata;
253
254 /* if the device needs this changing, it may do so in the
255 * slave_configure function */
256 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
257
258 /*
259 * Some low level driver could use device->type
260 */
261 sdev->type = -1;
262
263 /*
264 * Assume that the device will have handshaking problems,
265 * and then fix this field later if it turns out it
266 * doesn't
267 */
268 sdev->borken = 1;
269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 sdev->request_queue = scsi_alloc_queue(sdev);
271 if (!sdev->request_queue) {
272 /* release fn is set up in scsi_sysfs_device_initialise, so
273 * have to free and put manually here */
274 put_device(&starget->dev);
Dave Jones93f560892006-03-09 10:21:27 -0500275 kfree(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 goto out;
277 }
278
279 sdev->request_queue->queuedata = sdev;
280 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
281
282 scsi_sysfs_device_initialize(sdev);
283
284 if (shost->hostt->slave_alloc) {
285 ret = shost->hostt->slave_alloc(sdev);
286 if (ret) {
287 /*
288 * if LLDD reports slave not present, don't clutter
289 * console with alloc failure messages
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 */
291 if (ret == -ENXIO)
292 display_failure_msg = 0;
293 goto out_device_destroy;
294 }
295 }
296
297 return sdev;
298
299out_device_destroy:
300 transport_destroy_device(&sdev->sdev_gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 put_device(&sdev->sdev_gendev);
302out:
303 if (display_failure_msg)
304 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
305 return NULL;
306}
307
308static void scsi_target_dev_release(struct device *dev)
309{
310 struct device *parent = dev->parent;
311 struct scsi_target *starget = to_scsi_target(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 kfree(starget);
314 put_device(parent);
315}
316
317int scsi_is_target_device(const struct device *dev)
318{
319 return dev->release == scsi_target_dev_release;
320}
321EXPORT_SYMBOL(scsi_is_target_device);
322
323static struct scsi_target *__scsi_find_target(struct device *parent,
324 int channel, uint id)
325{
326 struct scsi_target *starget, *found_starget = NULL;
327 struct Scsi_Host *shost = dev_to_shost(parent);
328 /*
329 * Search for an existing target for this sdev.
330 */
331 list_for_each_entry(starget, &shost->__targets, siblings) {
332 if (starget->id == id &&
333 starget->channel == channel) {
334 found_starget = starget;
335 break;
336 }
337 }
338 if (found_starget)
339 get_device(&found_starget->dev);
340
341 return found_starget;
342}
343
James Bottomley884d25c2006-09-05 16:26:41 -0500344/**
345 * scsi_alloc_target - allocate a new or find an existing target
346 * @parent: parent of the target (need not be a scsi host)
347 * @channel: target channel number (zero if no channels)
348 * @id: target id number
349 *
350 * Return an existing target if one exists, provided it hasn't already
351 * gone into STARGET_DEL state, otherwise allocate a new target.
352 *
353 * The target is returned with an incremented reference, so the caller
354 * is responsible for both reaping and doing a last put
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356static struct scsi_target *scsi_alloc_target(struct device *parent,
357 int channel, uint id)
358{
359 struct Scsi_Host *shost = dev_to_shost(parent);
360 struct device *dev = NULL;
361 unsigned long flags;
362 const int size = sizeof(struct scsi_target)
363 + shost->transportt->target_size;
James.Smart@Emulex.Com5c44cd22005-06-10 22:24:30 -0400364 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 struct scsi_target *found_target;
Brian King32f95792006-02-22 14:28:24 -0600366 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Jes Sorensen24669f752006-01-16 10:31:18 -0500368 starget = kzalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (!starget) {
370 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
371 return NULL;
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 dev = &starget->dev;
374 device_initialize(dev);
375 starget->reap_ref = 1;
376 dev->parent = get_device(parent);
377 dev->release = scsi_target_dev_release;
378 sprintf(dev->bus_id, "target%d:%d:%d",
379 shost->host_no, channel, id);
380 starget->id = id;
381 starget->channel = channel;
382 INIT_LIST_HEAD(&starget->siblings);
383 INIT_LIST_HEAD(&starget->devices);
James Bottomleyffedb452006-02-23 14:27:18 -0600384 starget->state = STARGET_RUNNING;
385 retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock_irqsave(shost->host_lock, flags);
387
388 found_target = __scsi_find_target(parent, channel, id);
389 if (found_target)
390 goto found;
391
392 list_add_tail(&starget->siblings, &shost->__targets);
393 spin_unlock_irqrestore(shost->host_lock, flags);
394 /* allocate and add */
James Bottomleya283bd32005-05-24 12:06:38 -0500395 transport_setup_device(dev);
Brian King32f95792006-02-22 14:28:24 -0600396 error = device_add(dev);
397 if (error) {
398 dev_err(dev, "target device_add failed, error %d\n", error);
399 spin_lock_irqsave(shost->host_lock, flags);
400 list_del_init(&starget->siblings);
401 spin_unlock_irqrestore(shost->host_lock, flags);
402 transport_destroy_device(dev);
403 put_device(parent);
404 kfree(starget);
405 return NULL;
406 }
James Bottomleya283bd32005-05-24 12:06:38 -0500407 transport_add_device(dev);
408 if (shost->hostt->target_alloc) {
Brian King32f95792006-02-22 14:28:24 -0600409 error = shost->hostt->target_alloc(starget);
James Bottomleya283bd32005-05-24 12:06:38 -0500410
411 if(error) {
412 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
413 /* don't want scsi_target_reap to do the final
414 * put because it will be under the host lock */
415 get_device(dev);
416 scsi_target_reap(starget);
417 put_device(dev);
418 return NULL;
419 }
420 }
James Bottomley884d25c2006-09-05 16:26:41 -0500421 get_device(dev);
James Bottomleya283bd32005-05-24 12:06:38 -0500422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 return starget;
424
425 found:
426 found_target->reap_ref++;
427 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600428 if (found_target->state != STARGET_DEL) {
James Bottomley884d25c2006-09-05 16:26:41 -0500429 put_device(parent);
James Bottomleyffedb452006-02-23 14:27:18 -0600430 kfree(starget);
431 return found_target;
432 }
433 /* Unfortunately, we found a dying target; need to
434 * wait until it's dead before we can get a new one */
435 put_device(&found_target->dev);
436 flush_scheduled_work();
437 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
James Bottomley65110b22006-02-14 10:48:46 -0600440static void scsi_target_reap_usercontext(void *data)
441{
442 struct scsi_target *starget = data;
James Bottomley863a9302005-12-15 20:01:43 -0800443 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
444 unsigned long flags;
445
James Bottomleyffedb452006-02-23 14:27:18 -0600446 transport_remove_device(&starget->dev);
447 device_del(&starget->dev);
448 transport_destroy_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800449 spin_lock_irqsave(shost->host_lock, flags);
Mike Andersona50a5e32006-03-14 11:18:46 -0800450 if (shost->hostt->target_destroy)
451 shost->hostt->target_destroy(starget);
James Bottomleyffedb452006-02-23 14:27:18 -0600452 list_del_init(&starget->siblings);
James Bottomley863a9302005-12-15 20:01:43 -0800453 spin_unlock_irqrestore(shost->host_lock, flags);
James Bottomleyffedb452006-02-23 14:27:18 -0600454 put_device(&starget->dev);
James Bottomley863a9302005-12-15 20:01:43 -0800455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457/**
458 * scsi_target_reap - check to see if target is in use and destroy if not
459 *
460 * @starget: target to be checked
461 *
462 * This is used after removing a LUN or doing a last put of the target
463 * it checks atomically that nothing is using the target and removes
464 * it if so.
465 */
466void scsi_target_reap(struct scsi_target *starget)
467{
James Bottomleyffedb452006-02-23 14:27:18 -0600468 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
469 unsigned long flags;
470
471 spin_lock_irqsave(shost->host_lock, flags);
472
473 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
474 BUG_ON(starget->state == STARGET_DEL);
475 starget->state = STARGET_DEL;
476 spin_unlock_irqrestore(shost->host_lock, flags);
477 execute_in_process_context(scsi_target_reap_usercontext,
478 starget, &starget->ew);
479 return;
480
481 }
482 spin_unlock_irqrestore(shost->host_lock, flags);
483
484 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487/**
Alan Sterne5b3cd42006-08-21 15:53:25 -0400488 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
489 * @s: INQUIRY result string to sanitize
490 * @len: length of the string
491 *
492 * Description:
493 * The SCSI spec says that INQUIRY vendor, product, and revision
494 * strings must consist entirely of graphic ASCII characters,
495 * padded on the right with spaces. Since not all devices obey
496 * this rule, we will replace non-graphic or non-ASCII characters
497 * with spaces. Exception: a NUL character is interpreted as a
498 * string terminator, so all the following characters are set to
499 * spaces.
500 **/
501static void sanitize_inquiry_string(unsigned char *s, int len)
502{
503 int terminated = 0;
504
505 for (; len > 0; (--len, ++s)) {
506 if (*s == 0)
507 terminated = 1;
508 if (terminated || *s < 0x20 || *s > 0x7e)
509 *s = ' ';
510 }
511}
512
513/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
James Bottomley39216032005-06-15 18:48:29 -0500515 * @sdev: scsi_device to probe
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * @inq_result: area to store the INQUIRY result
James Bottomley39216032005-06-15 18:48:29 -0500517 * @result_len: len of inq_result
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * @bflags: store any bflags found here
519 *
520 * Description:
James Bottomley39216032005-06-15 18:48:29 -0500521 * Probe the lun associated with @req using a standard SCSI INQUIRY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 *
James Bottomley39216032005-06-15 18:48:29 -0500523 * If the INQUIRY is successful, zero is returned and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100525 * are copied to the scsi_device any flags value is stored in *@bflags.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400527static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
James Bottomley39216032005-06-15 18:48:29 -0500528 int result_len, int *bflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
531 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
532 int response_len = 0;
James Bottomley39216032005-06-15 18:48:29 -0500533 int pass, count, result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct scsi_sense_hdr sshdr;
535
536 *bflags = 0;
537
538 /* Perform up to 3 passes. The first pass uses a conservative
539 * transfer length of 36 unless sdev->inquiry_len specifies a
540 * different value. */
541 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
542 try_inquiry_len = first_inquiry_len;
543 pass = 1;
544
545 next_pass:
James Bottomley9ccfc752005-10-02 11:45:08 -0500546 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
547 "scsi scan: INQUIRY pass %d length %d\n",
548 pass, try_inquiry_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Each pass gets up to three chances to ignore Unit Attention */
551 for (count = 0; count < 3; ++count) {
552 memset(scsi_cmd, 0, 6);
553 scsi_cmd[0] = INQUIRY;
554 scsi_cmd[4] = (unsigned char) try_inquiry_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 memset(inq_result, 0, try_inquiry_len);
James Bottomley39216032005-06-15 18:48:29 -0500557
558 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -0500559 inq_result, try_inquiry_len, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -0500560 HZ / 2 + HZ * scsi_inq_timeout, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
563 "with code 0x%x\n",
James Bottomley39216032005-06-15 18:48:29 -0500564 result ? "failed" : "successful", result));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
James Bottomley39216032005-06-15 18:48:29 -0500566 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /*
568 * not-ready to ready transition [asc/ascq=0x28/0x0]
569 * or power-on, reset [asc/ascq=0x29/0x0], continue.
570 * INQUIRY should not yield UNIT_ATTENTION
571 * but many buggy devices do so anyway.
572 */
James Bottomley39216032005-06-15 18:48:29 -0500573 if ((driver_byte(result) & DRIVER_SENSE) &&
James Bottomleyea73a9f2005-08-28 11:33:52 -0500574 scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if ((sshdr.sense_key == UNIT_ATTENTION) &&
576 ((sshdr.asc == 0x28) ||
577 (sshdr.asc == 0x29)) &&
578 (sshdr.ascq == 0))
579 continue;
580 }
581 }
582 break;
583 }
584
James Bottomley39216032005-06-15 18:48:29 -0500585 if (result == 0) {
Alan Sterne5b3cd42006-08-21 15:53:25 -0400586 sanitize_inquiry_string(&inq_result[8], 8);
587 sanitize_inquiry_string(&inq_result[16], 16);
588 sanitize_inquiry_string(&inq_result[32], 4);
589
590 response_len = inq_result[4] + 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 if (response_len > 255)
592 response_len = first_inquiry_len; /* sanity */
593
594 /*
595 * Get any flags for this device.
596 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100597 * XXX add a bflags to scsi_device, and replace the
598 * corresponding bit fields in scsi_device, so bflags
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * need not be passed as an argument.
600 */
601 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
602 &inq_result[16]);
603
604 /* When the first pass succeeds we gain information about
605 * what larger transfer lengths might work. */
606 if (pass == 1) {
607 if (BLIST_INQUIRY_36 & *bflags)
608 next_inquiry_len = 36;
609 else if (BLIST_INQUIRY_58 & *bflags)
610 next_inquiry_len = 58;
611 else if (sdev->inquiry_len)
612 next_inquiry_len = sdev->inquiry_len;
613 else
614 next_inquiry_len = response_len;
615
616 /* If more data is available perform the second pass */
617 if (next_inquiry_len > try_inquiry_len) {
618 try_inquiry_len = next_inquiry_len;
619 pass = 2;
620 goto next_pass;
621 }
622 }
623
624 } else if (pass == 2) {
625 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
626 "Consider BLIST_INQUIRY_36 for this device\n",
627 try_inquiry_len);
628
629 /* If this pass failed, the third pass goes back and transfers
630 * the same amount as we successfully got in the first pass. */
631 try_inquiry_len = first_inquiry_len;
632 pass = 3;
633 goto next_pass;
634 }
635
636 /* If the last transfer attempt got an error, assume the
637 * peripheral doesn't exist or is dead. */
James Bottomley39216032005-06-15 18:48:29 -0500638 if (result)
639 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
641 /* Don't report any more data than the device says is valid */
642 sdev->inquiry_len = min(try_inquiry_len, response_len);
643
644 /*
645 * XXX Abort if the response length is less than 36? If less than
646 * 32, the lookup of the device flags (above) could be invalid,
647 * and it would be possible to take an incorrect action - we do
648 * not want to hang because of a short INQUIRY. On the flip side,
649 * if the device is spun down or becoming ready (and so it gives a
650 * short INQUIRY), an abort here prevents any further use of the
651 * device, including spin up.
652 *
653 * Related to the above issue:
654 *
655 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
656 * and if not ready, sent a START_STOP to start (maybe spin up) and
657 * then send the INQUIRY again, since the INQUIRY can change after
658 * a device is initialized.
659 *
660 * Ideally, start a device if explicitly asked to do so. This
661 * assumes that a device is spun up on power on, spun down on
662 * request, and then spun up on request.
663 */
664
665 /*
666 * The scanning code needs to know the scsi_level, even if no
667 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
668 * non-zero LUNs can be scanned.
669 */
670 sdev->scsi_level = inq_result[2] & 0x07;
671 if (sdev->scsi_level >= 2 ||
672 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
673 sdev->scsi_level++;
James Bottomley6f3a2022005-09-22 20:33:28 -0500674 sdev->sdev_target->scsi_level = sdev->scsi_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
James Bottomley39216032005-06-15 18:48:29 -0500676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679/**
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100680 * scsi_add_lun - allocate and fully initialze a scsi_device
681 * @sdevscan: holds information to be stored in the new scsi_device
682 * @sdevnew: store the address of the newly allocated scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 * @inq_result: holds the result of a previous INQUIRY to the LUN
684 * @bflags: black/white list flag
685 *
686 * Description:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100687 * Allocate and initialize a scsi_device matching sdevscan. Optionally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 * set fields based on values in *@bflags. If @sdevnew is not
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100689 * NULL, store the address of the new scsi_device in *@sdevnew (needed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * when scanning a particular LUN).
691 *
692 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100693 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
694 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 **/
Alan Sterne5b3cd42006-08-21 15:53:25 -0400696static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600697 int *bflags, int async)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
699 /*
700 * XXX do not save the inquiry, since it can change underneath us,
701 * save just vendor/model/rev.
702 *
703 * Rather than save it and have an ioctl that retrieves the saved
704 * value, have an ioctl that executes the same INQUIRY code used
705 * in scsi_probe_lun, let user level programs doing INQUIRY
706 * scanning run at their own risk, or supply a user level program
707 * that can correctly scan.
708 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Alan Stern09123d22006-11-10 12:27:57 -0800710 /*
711 * Copy at least 36 bytes of INQUIRY data, so that we don't
712 * dereference unallocated memory when accessing the Vendor,
713 * Product, and Revision strings. Badly behaved devices may set
714 * the INQUIRY Additional Length byte to a small value, indicating
715 * these strings are invalid, but often they contain plausible data
716 * nonetheless. It doesn't matter if the device sent < 36 bytes
717 * total, since scsi_probe_lun() initializes inq_result with 0s.
718 */
719 sdev->inquiry = kmemdup(inq_result,
720 max_t(size_t, sdev->inquiry_len, 36),
721 GFP_ATOMIC);
722 if (sdev->inquiry == NULL)
723 return SCSI_SCAN_NO_RESPONSE;
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 sdev->vendor = (char *) (sdev->inquiry + 8);
726 sdev->model = (char *) (sdev->inquiry + 16);
727 sdev->rev = (char *) (sdev->inquiry + 32);
728
729 if (*bflags & BLIST_ISROM) {
730 /*
731 * It would be better to modify sdev->type, and set
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600732 * sdev->removable; this can now be done since
733 * print_inquiry has gone away.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 */
735 inq_result[0] = TYPE_ROM;
736 inq_result[1] |= 0x80; /* removable */
737 } else if (*bflags & BLIST_NO_ULD_ATTACH)
738 sdev->no_uld_attach = 1;
739
740 switch (sdev->type = (inq_result[0] & 0x1f)) {
741 case TYPE_TAPE:
742 case TYPE_DISK:
743 case TYPE_PRINTER:
744 case TYPE_MOD:
745 case TYPE_PROCESSOR:
746 case TYPE_SCANNER:
747 case TYPE_MEDIUM_CHANGER:
748 case TYPE_ENCLOSURE:
749 case TYPE_COMM:
James Bottomley4d7db042006-03-31 20:07:45 -0600750 case TYPE_RAID:
Al Viro 631e8a12005-05-16 01:59:55 +0100751 case TYPE_RBC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 sdev->writeable = 1;
753 break;
754 case TYPE_WORM:
755 case TYPE_ROM:
756 sdev->writeable = 0;
757 break;
758 default:
759 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
760 }
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 /*
763 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
764 * spec says: The device server is capable of supporting the
765 * specified peripheral device type on this logical unit. However,
766 * the physical device is not currently connected to this logical
767 * unit.
768 *
769 * The above is vague, as it implies that we could treat 001 and
770 * 011 the same. Stay compatible with previous code, and create a
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100771 * scsi_device for a PQ of 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 *
773 * Don't set the device offline here; rather let the upper
774 * level drivers eval the PQ to decide whether they should
775 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
776 */
777
778 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
779 sdev->removable = (0x80 & inq_result[1]) >> 7;
780 sdev->lockable = sdev->removable;
781 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
782
783 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
784 inq_result[56] & 0x04))
785 sdev->ppr = 1;
786 if (inq_result[7] & 0x60)
787 sdev->wdtr = 1;
788 if (inq_result[7] & 0x10)
789 sdev->sdtr = 1;
790
James Bottomley19ac0db2006-08-06 18:15:22 -0500791 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
Matthew Wilcox4ff36712006-07-04 12:15:20 -0600792 "ANSI: %d%s\n", scsi_device_type(sdev->type),
793 sdev->vendor, sdev->model, sdev->rev,
794 sdev->inq_periph_qual, inq_result[2] & 0x07,
795 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /*
Greg KH5e3c34c2006-01-18 16:17:46 -0800798 * End sysfs code.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
800
801 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
802 !(*bflags & BLIST_NOTQ))
803 sdev->tagged_supported = 1;
804 /*
805 * Some devices (Texel CD ROM drives) have handshaking problems
806 * when used with the Seagate controllers. borken is initialized
807 * to 1, and then set it to 0 here.
808 */
809 if ((*bflags & BLIST_BORKEN) == 0)
810 sdev->borken = 0;
811
812 /*
813 * Apparently some really broken devices (contrary to the SCSI
814 * standards) need to be selected without asserting ATN
815 */
816 if (*bflags & BLIST_SELECT_NO_ATN)
817 sdev->select_no_atn = 1;
818
819 /*
James Bottomley4d7db042006-03-31 20:07:45 -0600820 * Maximum 512 sector transfer length
821 * broken RA4x00 Compaq Disk Array
822 */
823 if (*bflags & BLIST_MAX_512)
824 blk_queue_max_sectors(sdev->request_queue, 512);
825
826 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * Some devices may not want to have a start command automatically
828 * issued when a device is added.
829 */
830 if (*bflags & BLIST_NOSTARTONADD)
831 sdev->no_start_on_add = 1;
832
833 if (*bflags & BLIST_SINGLELUN)
834 sdev->single_lun = 1;
835
836
837 sdev->use_10_for_rw = 1;
838
839 if (*bflags & BLIST_MS_SKIP_PAGE_08)
840 sdev->skip_ms_page_8 = 1;
841
842 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
843 sdev->skip_ms_page_3f = 1;
844
845 if (*bflags & BLIST_USE_10_BYTE_MS)
846 sdev->use_10_for_ms = 1;
847
848 /* set the device running here so that slave configure
849 * may do I/O */
850 scsi_device_set_state(sdev, SDEV_RUNNING);
851
852 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
853 sdev->use_192_bytes_for_3f = 1;
854
855 if (*bflags & BLIST_NOT_LOCKABLE)
856 sdev->lockable = 0;
857
858 if (*bflags & BLIST_RETRY_HWERROR)
859 sdev->retry_hwerror = 1;
860
861 transport_configure_device(&sdev->sdev_gendev);
862
Christoph Hellwig93805092006-02-17 12:11:29 +0100863 if (sdev->host->hostt->slave_configure) {
864 int ret = sdev->host->hostt->slave_configure(sdev);
865 if (ret) {
866 /*
867 * if LLDD reports slave not present, don't clutter
868 * console with alloc failure messages
869 */
870 if (ret != -ENXIO) {
871 sdev_printk(KERN_ERR, sdev,
872 "failed to configure device\n");
873 }
874 return SCSI_SCAN_NO_RESPONSE;
875 }
876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 /*
879 * Ok, the device is now all set up, we can
880 * register it and tell the rest of the kernel
881 * about it.
882 */
Matthew Wilcox3e082a92006-09-28 15:19:20 -0600883 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
Alan Sternb24b1032005-07-27 11:43:46 -0700884 return SCSI_SCAN_NO_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 return SCSI_SCAN_LUN_PRESENT;
887}
888
James Bottomley6f3a2022005-09-22 20:33:28 -0500889static inline void scsi_destroy_sdev(struct scsi_device *sdev)
890{
Brian King309bd272006-06-27 11:10:43 -0500891 scsi_device_set_state(sdev, SDEV_DEL);
James Bottomley6f3a2022005-09-22 20:33:28 -0500892 if (sdev->host->hostt->slave_destroy)
893 sdev->host->hostt->slave_destroy(sdev);
894 transport_destroy_device(&sdev->sdev_gendev);
895 put_device(&sdev->sdev_gendev);
896}
897
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700898#ifdef CONFIG_SCSI_LOGGING
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200899/**
900 * scsi_inq_str - print INQUIRY data from min to max index,
901 * strip trailing whitespace
902 * @buf: Output buffer with at least end-first+1 bytes of space
903 * @inq: Inquiry buffer (input)
904 * @first: Offset of string into inq
905 * @end: Index after last character in inq
906 */
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700907static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200908 unsigned first, unsigned end)
909{
910 unsigned term = 0, idx;
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700911
912 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
913 if (inq[idx+first] > ' ') {
Kurt Garloff6c7154c2006-04-03 15:18:35 +0200914 buf[idx] = inq[idx+first];
915 term = idx+1;
916 } else {
917 buf[idx] = ' ';
918 }
919 }
920 buf[term] = 0;
921 return buf;
922}
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -0700923#endif
James Bottomley6f3a2022005-09-22 20:33:28 -0500924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925/**
926 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
927 * @starget: pointer to target device structure
928 * @lun: LUN of target device
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100929 * @sdevscan: probe the LUN corresponding to this scsi_device
930 * @sdevnew: store the value of any new scsi_device allocated
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 * @bflagsp: store bflags here if not NULL
932 *
933 * Description:
934 * Call scsi_probe_lun, if a LUN with an attached device is found,
935 * allocate and set it up by calling scsi_add_lun.
936 *
937 * Return:
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100938 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
940 * attached at the LUN
Christoph Hellwigf64a1812005-10-31 18:32:08 +0100941 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 **/
943static int scsi_probe_and_add_lun(struct scsi_target *starget,
944 uint lun, int *bflagsp,
945 struct scsi_device **sdevp, int rescan,
946 void *hostdata)
947{
948 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 unsigned char *result;
James Bottomley39216032005-06-15 18:48:29 -0500950 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
952
953 /*
954 * The rescan flag is used as an optimization, the first scan of a
955 * host adapter calls into here with rescan == 0.
956 */
James Bottomley6f3a2022005-09-22 20:33:28 -0500957 sdev = scsi_device_lookup_by_target(starget, lun);
958 if (sdev) {
959 if (rescan || sdev->sdev_state != SDEV_CREATED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
961 "scsi scan: device exists on %s\n",
962 sdev->sdev_gendev.bus_id));
963 if (sdevp)
964 *sdevp = sdev;
965 else
966 scsi_device_put(sdev);
967
968 if (bflagsp)
969 *bflagsp = scsi_get_device_flags(sdev,
970 sdev->vendor,
971 sdev->model);
972 return SCSI_SCAN_LUN_PRESENT;
973 }
James Bottomley6f3a2022005-09-22 20:33:28 -0500974 scsi_device_put(sdev);
975 } else
976 sdev = scsi_alloc_sdev(starget, lun, hostdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (!sdev)
978 goto out;
James Bottomley39216032005-06-15 18:48:29 -0500979
980 result = kmalloc(result_len, GFP_ATOMIC |
Al Virobc861202005-04-24 12:28:34 -0700981 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (!result)
James Bottomley39216032005-06-15 18:48:29 -0500983 goto out_free_sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
James Bottomley39216032005-06-15 18:48:29 -0500985 if (scsi_probe_lun(sdev, result, result_len, &bflags))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto out_free_result;
987
Kurt Garloff4186ab12006-04-03 15:16:48 +0200988 if (bflagsp)
989 *bflagsp = bflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 /*
991 * result contains valid SCSI INQUIRY data.
992 */
Kurt Garloff13f7e5a2006-04-03 15:20:08 +0200993 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 /*
995 * For a Peripheral qualifier 3 (011b), the SCSI
996 * spec says: The device server is not capable of
997 * supporting a physical device on this logical
998 * unit.
999 *
1000 * For disks, this implies that there is no
1001 * logical disk configured at sdev->lun, but there
1002 * is a target id responding.
1003 */
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001004 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1005 " peripheral qualifier of 3, device not"
1006 " added\n"))
1007 if (lun == 0) {
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001008 SCSI_LOG_SCAN_BUS(1, {
1009 unsigned char vend[9];
1010 unsigned char mod[17];
1011
1012 sdev_printk(KERN_INFO, sdev,
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001013 "scsi scan: consider passing scsi_mod."
1014 "dev_flags=%s:%s:0x240 or 0x800240\n",
1015 scsi_inq_str(vend, result, 8, 16),
akpm@osdl.orgc5f2e642006-04-15 00:30:24 -07001016 scsi_inq_str(mod, result, 16, 32));
1017 });
Kurt Garloff6c7154c2006-04-03 15:18:35 +02001018 }
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 res = SCSI_SCAN_TARGET_PRESENT;
1021 goto out_free_result;
1022 }
1023
Alan Stern1bfc5d92006-02-09 15:26:18 -05001024 /*
dave wysochanski84961f22006-08-09 14:56:32 -04001025 * Some targets may set slight variations of PQ and PDT to signal
1026 * that no LUN is present, so don't add sdev in these cases.
1027 * Two specific examples are:
1028 * 1) NetApp targets: return PQ=1, PDT=0x1f
1029 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1030 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1031 *
1032 * References:
1033 * 1) SCSI SPC-3, pp. 145-146
1034 * PQ=1: "A peripheral device having the specified peripheral
1035 * device type is not connected to this logical unit. However, the
1036 * device server is capable of supporting the specified peripheral
1037 * device type on this logical unit."
1038 * PDT=0x1f: "Unknown or no device type"
1039 * 2) USB UFI 1.0, p. 20
1040 * PDT=00h Direct-access device (floppy)
1041 * PDT=1Fh none (no FDD connected to the requested logical unit)
Alan Stern1bfc5d92006-02-09 15:26:18 -05001042 */
dave wysochanski84961f22006-08-09 14:56:32 -04001043 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1044 (result[0] & 0x1f) == 0x1f) {
Alan Stern1bfc5d92006-02-09 15:26:18 -05001045 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1046 "scsi scan: peripheral device type"
1047 " of 31, no device added\n"));
1048 res = SCSI_SCAN_TARGET_PRESENT;
1049 goto out_free_result;
1050 }
1051
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001052 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (res == SCSI_SCAN_LUN_PRESENT) {
1054 if (bflags & BLIST_KEY) {
1055 sdev->lockable = 0;
James Bottomley39216032005-06-15 18:48:29 -05001056 scsi_unlock_floptical(sdev, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
1060 out_free_result:
1061 kfree(result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 out_free_sdev:
1063 if (res == SCSI_SCAN_LUN_PRESENT) {
1064 if (sdevp) {
Alan Sternb70d37b2005-07-26 10:30:40 -04001065 if (scsi_device_get(sdev) == 0) {
1066 *sdevp = sdev;
1067 } else {
1068 __scsi_remove_device(sdev);
1069 res = SCSI_SCAN_NO_RESPONSE;
1070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
James Bottomley6f3a2022005-09-22 20:33:28 -05001072 } else
1073 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 out:
1075 return res;
1076}
1077
1078/**
1079 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1080 * @starget: pointer to target structure to scan
1081 * @bflags: black/white list flag for LUN 0
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 *
1083 * Description:
1084 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1085 * scanned) to some maximum lun until a LUN is found with no device
1086 * attached. Use the bflags to figure out any oddities.
1087 *
1088 * Modifies sdevscan->lun.
1089 **/
1090static void scsi_sequential_lun_scan(struct scsi_target *starget,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001091 int bflags, int scsi_level, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
1093 unsigned int sparse_lun, lun, max_dev_lun;
1094 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1095
1096 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1097 "%s\n", starget->dev.bus_id));
1098
1099 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1100 /*
1101 * If this device is known to support sparse multiple units,
1102 * override the other settings, and scan all of them. Normally,
1103 * SCSI-3 devices should be scanned via the REPORT LUNS.
1104 */
1105 if (bflags & BLIST_SPARSELUN) {
1106 max_dev_lun = shost->max_lun;
1107 sparse_lun = 1;
1108 } else
1109 sparse_lun = 0;
1110
1111 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * If less than SCSI_1_CSS, and no special lun scaning, stop
1113 * scanning; this matches 2.4 behaviour, but could just be a bug
1114 * (to continue scanning a SCSI_1_CSS device).
1115 *
1116 * This test is broken. We might not have any device on lun0 for
1117 * a sparselun device, and if that's the case then how would we
1118 * know the real scsi_level, eh? It might make sense to just not
1119 * scan any SCSI_1 device for non-0 luns, but that check would best
1120 * go into scsi_alloc_sdev() and just have it return null when asked
1121 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1122 *
1123 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1124 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1125 == 0))
1126 return;
1127 */
1128 /*
1129 * If this device is known to support multiple units, override
1130 * the other settings, and scan all of them.
1131 */
1132 if (bflags & BLIST_FORCELUN)
1133 max_dev_lun = shost->max_lun;
1134 /*
1135 * REGAL CDC-4X: avoid hang after LUN 4
1136 */
1137 if (bflags & BLIST_MAX5LUN)
1138 max_dev_lun = min(5U, max_dev_lun);
1139 /*
1140 * Do not scan SCSI-2 or lower device past LUN 7, unless
1141 * BLIST_LARGELUN.
1142 */
1143 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1144 max_dev_lun = min(8U, max_dev_lun);
1145
1146 /*
1147 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1148 * until we reach the max, or no LUN is found and we are not
1149 * sparse_lun.
1150 */
1151 for (lun = 1; lun < max_dev_lun; ++lun)
1152 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1153 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1154 !sparse_lun)
1155 return;
1156}
1157
1158/**
1159 * scsilun_to_int: convert a scsi_lun to an int
1160 * @scsilun: struct scsi_lun to be converted.
1161 *
1162 * Description:
1163 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1164 * integer, and return the result. The caller must check for
1165 * truncation before using this function.
1166 *
1167 * Notes:
1168 * The struct scsi_lun is assumed to be four levels, with each level
1169 * effectively containing a SCSI byte-ordered (big endian) short; the
1170 * addressing bits of each level are ignored (the highest two bits).
1171 * For a description of the LUN format, post SCSI-3 see the SCSI
1172 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1173 *
1174 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1175 * the integer: 0x0b030a04
1176 **/
1177static int scsilun_to_int(struct scsi_lun *scsilun)
1178{
1179 int i;
1180 unsigned int lun;
1181
1182 lun = 0;
1183 for (i = 0; i < sizeof(lun); i += 2)
1184 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1185 scsilun->scsi_lun[i + 1]) << (i * 8));
1186 return lun;
1187}
1188
1189/**
James.Smart@Emulex.Com2f4701d2005-07-13 22:05:03 -04001190 * int_to_scsilun: reverts an int into a scsi_lun
1191 * @int: integer to be reverted
1192 * @scsilun: struct scsi_lun to be set.
1193 *
1194 * Description:
1195 * Reverts the functionality of the scsilun_to_int, which packed
1196 * an 8-byte lun value into an int. This routine unpacks the int
1197 * back into the lun value.
1198 * Note: the scsilun_to_int() routine does not truly handle all
1199 * 8bytes of the lun value. This functions restores only as much
1200 * as was set by the routine.
1201 *
1202 * Notes:
1203 * Given an integer : 0x0b030a04, this function returns a
1204 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1205 *
1206 **/
1207void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1208{
1209 int i;
1210
1211 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1212
1213 for (i = 0; i < sizeof(lun); i += 2) {
1214 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1215 scsilun->scsi_lun[i+1] = lun & 0xFF;
1216 lun = lun >> 16;
1217 }
1218}
1219EXPORT_SYMBOL(int_to_scsilun);
1220
1221/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001223 * @sdevscan: scan the host, channel, and id of this scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 *
1225 * Description:
1226 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1227 * command, and scan the resulting list of LUNs by calling
1228 * scsi_probe_and_add_lun.
1229 *
1230 * Modifies sdevscan->lun.
1231 *
1232 * Return:
1233 * 0: scan completed (or no memory, so further scanning is futile)
1234 * 1: no report lun scan, or not configured
1235 **/
James Bottomley6f3a2022005-09-22 20:33:28 -05001236static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int rescan)
1238{
1239 char devname[64];
1240 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1241 unsigned int length;
1242 unsigned int lun;
1243 unsigned int num_luns;
1244 unsigned int retries;
James Bottomley39216032005-06-15 18:48:29 -05001245 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct scsi_lun *lunp, *lun_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 u8 *data;
1248 struct scsi_sense_hdr sshdr;
James Bottomley6f3a2022005-09-22 20:33:28 -05001249 struct scsi_device *sdev;
1250 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
Alan Stern2ef89192005-11-08 15:51:55 -05001251 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /*
1254 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1255 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1256 * support more than 8 LUNs.
1257 */
James Bottomley4d7db042006-03-31 20:07:45 -06001258 if (bflags & BLIST_NOREPORTLUN)
1259 return 1;
1260 if (starget->scsi_level < SCSI_2 &&
1261 starget->scsi_level != SCSI_UNKNOWN)
1262 return 1;
1263 if (starget->scsi_level < SCSI_3 &&
1264 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 return 1;
1266 if (bflags & BLIST_NOLUN)
1267 return 0;
1268
James Bottomley6f3a2022005-09-22 20:33:28 -05001269 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1270 sdev = scsi_alloc_sdev(starget, 0, NULL);
1271 if (!sdev)
1272 return 0;
1273 if (scsi_device_get(sdev))
1274 return 0;
1275 }
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sprintf(devname, "host %d channel %d id %d",
James Bottomley6f3a2022005-09-22 20:33:28 -05001278 shost->host_no, sdev->channel, sdev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 /*
1281 * Allocate enough to hold the header (the same size as one scsi_lun)
1282 * plus the max number of luns we are requesting.
1283 *
1284 * Reallocating and trying again (with the exact amount we need)
1285 * would be nice, but then we need to somehow limit the size
1286 * allocated based on the available memory and the limits of
1287 * kmalloc - we don't want a kmalloc() failure of a huge value to
1288 * prevent us from finding any LUNs on this target.
1289 */
1290 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1291 lun_data = kmalloc(length, GFP_ATOMIC |
1292 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
James Bottomley6f3a2022005-09-22 20:33:28 -05001293 if (!lun_data) {
1294 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
James Bottomley39216032005-06-15 18:48:29 -05001295 goto out;
James Bottomley6f3a2022005-09-22 20:33:28 -05001296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 scsi_cmd[0] = REPORT_LUNS;
1299
1300 /*
1301 * bytes 1 - 5: reserved, set to zero.
1302 */
1303 memset(&scsi_cmd[1], 0, 5);
1304
1305 /*
1306 * bytes 6 - 9: length of the command.
1307 */
1308 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1309 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1310 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1311 scsi_cmd[9] = (unsigned char) length & 0xff;
1312
1313 scsi_cmd[10] = 0; /* reserved */
1314 scsi_cmd[11] = 0; /* control */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 /*
1317 * We can get a UNIT ATTENTION, for example a power on/reset, so
1318 * retry a few times (like sd.c does for TEST UNIT READY).
1319 * Experience shows some combinations of adapter/devices get at
1320 * least two power on/resets.
1321 *
1322 * Illegal requests (for devices that do not support REPORT LUNS)
1323 * should come through as a check condition, and will not generate
1324 * a retry.
1325 */
1326 for (retries = 0; retries < 3; retries++) {
1327 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1328 " REPORT LUNS to %s (try %d)\n", devname,
1329 retries));
James Bottomley39216032005-06-15 18:48:29 -05001330
James Bottomley39216032005-06-15 18:48:29 -05001331 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
James Bottomleyea73a9f2005-08-28 11:33:52 -05001332 lun_data, length, &sshdr,
James Bottomley39216032005-06-15 18:48:29 -05001333 SCSI_TIMEOUT + 4 * HZ, 3);
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
James Bottomley39216032005-06-15 18:48:29 -05001336 " %s (try %d) result 0x%x\n", result
1337 ? "failed" : "successful", retries, result));
1338 if (result == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 break;
James Bottomleyea73a9f2005-08-28 11:33:52 -05001340 else if (scsi_sense_valid(&sshdr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 if (sshdr.sense_key != UNIT_ATTENTION)
1342 break;
1343 }
1344 }
1345
James Bottomley39216032005-06-15 18:48:29 -05001346 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /*
1348 * The device probably does not support a REPORT LUN command
1349 */
Alan Stern2ef89192005-11-08 15:51:55 -05001350 ret = 1;
1351 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /*
1355 * Get the length from the first four bytes of lun_data.
1356 */
1357 data = (u8 *) lun_data->scsi_lun;
1358 length = ((data[0] << 24) | (data[1] << 16) |
1359 (data[2] << 8) | (data[3] << 0));
1360
1361 num_luns = (length / sizeof(struct scsi_lun));
1362 if (num_luns > max_scsi_report_luns) {
1363 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1364 " of %d luns reported, try increasing"
1365 " max_scsi_report_luns.\n", devname,
1366 max_scsi_report_luns, num_luns);
1367 num_luns = max_scsi_report_luns;
1368 }
1369
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001370 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1371 "scsi scan: REPORT LUN scan\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
1373 /*
1374 * Scan the luns in lun_data. The entry at offset 0 is really
1375 * the header, so start at 1 and go up to and including num_luns.
1376 */
1377 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1378 lun = scsilun_to_int(lunp);
1379
1380 /*
1381 * Check if the unused part of lunp is non-zero, and so
1382 * does not fit in lun.
1383 */
1384 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1385 int i;
1386
1387 /*
1388 * Output an error displaying the LUN in byte order,
1389 * this differs from what linux would print for the
1390 * integer LUN value.
1391 */
1392 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1393 data = (char *)lunp->scsi_lun;
1394 for (i = 0; i < sizeof(struct scsi_lun); i++)
1395 printk("%02x", data[i]);
1396 printk(" has a LUN larger than currently supported.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 } else if (lun > sdev->host->max_lun) {
1398 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1399 " than allowed by the host adapter\n",
1400 devname, lun);
1401 } else {
1402 int res;
1403
1404 res = scsi_probe_and_add_lun(starget,
1405 lun, NULL, NULL, rescan, NULL);
1406 if (res == SCSI_SCAN_NO_RESPONSE) {
1407 /*
1408 * Got some results, but now none, abort.
1409 */
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001410 sdev_printk(KERN_ERR, sdev,
1411 "Unexpected response"
1412 " from lun %d while scanning, scan"
1413 " aborted\n", lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 break;
1415 }
1416 }
1417 }
1418
Alan Stern2ef89192005-11-08 15:51:55 -05001419 out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 kfree(lun_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 out:
James Bottomley6f3a2022005-09-22 20:33:28 -05001422 scsi_device_put(sdev);
1423 if (sdev->sdev_state == SDEV_CREATED)
1424 /*
1425 * the sdev we used didn't appear in the report luns scan
1426 */
1427 scsi_destroy_sdev(sdev);
Alan Stern2ef89192005-11-08 15:51:55 -05001428 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1432 uint id, uint lun, void *hostdata)
1433{
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001434 struct scsi_device *sdev = ERR_PTR(-ENODEV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 struct device *parent = &shost->shost_gendev;
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001436 struct scsi_target *starget;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Christoph Hellwige02f3f52006-01-13 19:04:00 +01001438 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 if (!starget)
1440 return ERR_PTR(-ENOMEM);
1441
Arjan van de Ven0b950672006-01-11 13:16:10 +01001442 mutex_lock(&shost->scan_mutex);
Matthew Wilcoxa97a83a2006-02-05 08:01:33 -07001443 if (scsi_host_scan_allowed(shost))
1444 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001445 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 scsi_target_reap(starget);
1447 put_device(&starget->dev);
1448
1449 return sdev;
1450}
1451EXPORT_SYMBOL(__scsi_add_device);
1452
James Bottomley146f7262005-09-10 12:44:09 -05001453int scsi_add_device(struct Scsi_Host *host, uint channel,
1454 uint target, uint lun)
1455{
1456 struct scsi_device *sdev =
1457 __scsi_add_device(host, channel, target, lun, NULL);
1458 if (IS_ERR(sdev))
1459 return PTR_ERR(sdev);
1460
1461 scsi_device_put(sdev);
1462 return 0;
1463}
1464EXPORT_SYMBOL(scsi_add_device);
1465
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466void scsi_rescan_device(struct device *dev)
1467{
1468 struct scsi_driver *drv;
1469
1470 if (!dev->driver)
1471 return;
1472
1473 drv = to_scsi_driver(dev->driver);
1474 if (try_module_get(drv->owner)) {
1475 if (drv->rescan)
1476 drv->rescan(dev);
1477 module_put(drv->owner);
1478 }
1479}
1480EXPORT_SYMBOL(scsi_rescan_device);
1481
Alan Sterne517d312005-07-26 10:18:45 -04001482static void __scsi_scan_target(struct device *parent, unsigned int channel,
1483 unsigned int id, unsigned int lun, int rescan)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484{
1485 struct Scsi_Host *shost = dev_to_shost(parent);
1486 int bflags = 0;
1487 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 struct scsi_target *starget;
1489
1490 if (shost->this_id == id)
1491 /*
1492 * Don't scan the host adapter
1493 */
1494 return;
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 starget = scsi_alloc_target(parent, channel, id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (!starget)
1498 return;
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 if (lun != SCAN_WILD_CARD) {
1501 /*
1502 * Scan for a specific host/chan/id/lun.
1503 */
1504 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1505 goto out_reap;
1506 }
1507
1508 /*
1509 * Scan LUN 0, if there is some response, scan further. Ideally, we
1510 * would not configure LUN 0 until all LUNs are scanned.
1511 */
James Bottomley6f3a2022005-09-22 20:33:28 -05001512 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1513 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1514 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 /*
1516 * The REPORT LUN did not scan the target,
1517 * do a sequential scan.
1518 */
1519 scsi_sequential_lun_scan(starget, bflags,
Kurt Garloff4186ab12006-04-03 15:16:48 +02001520 starget->scsi_level, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 out_reap:
1524 /* now determine if the target has any children at all
1525 * and if not, nuke it */
1526 scsi_target_reap(starget);
1527
1528 put_device(&starget->dev);
1529}
Alan Sterne517d312005-07-26 10:18:45 -04001530
1531/**
1532 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1533 * target.
1534 * @parent: host to scan
1535 * @channel: channel to scan
1536 * @id: target id to scan
1537 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1538 * @rescan: passed to LUN scanning routines
1539 *
1540 * Description:
1541 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1542 * and possibly all LUNs on the target id.
1543 *
1544 * First try a REPORT LUN scan, if that does not scan the target, do a
1545 * sequential scan of LUNs on the target id.
1546 **/
1547void scsi_scan_target(struct device *parent, unsigned int channel,
1548 unsigned int id, unsigned int lun, int rescan)
1549{
1550 struct Scsi_Host *shost = dev_to_shost(parent);
1551
Matthew Wilcox93b45af2006-11-22 13:24:53 -07001552 if (strncmp(scsi_scan_type, "none", 4) == 0)
1553 return;
1554
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001555 if (!shost->async_scan)
1556 scsi_complete_async_scans();
1557
Arjan van de Ven0b950672006-01-11 13:16:10 +01001558 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001559 if (scsi_host_scan_allowed(shost))
1560 __scsi_scan_target(parent, channel, id, lun, rescan);
Arjan van de Ven0b950672006-01-11 13:16:10 +01001561 mutex_unlock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563EXPORT_SYMBOL(scsi_scan_target);
1564
1565static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1566 unsigned int id, unsigned int lun, int rescan)
1567{
1568 uint order_id;
1569
1570 if (id == SCAN_WILD_CARD)
1571 for (id = 0; id < shost->max_id; ++id) {
1572 /*
1573 * XXX adapter drivers when possible (FCP, iSCSI)
1574 * could modify max_id to match the current max,
1575 * not the absolute max.
1576 *
1577 * XXX add a shost id iterator, so for example,
1578 * the FC ID can be the same as a target id
1579 * without a huge overhead of sparse id's.
1580 */
1581 if (shost->reverse_ordering)
1582 /*
1583 * Scan from high to low id.
1584 */
1585 order_id = shost->max_id - id - 1;
1586 else
1587 order_id = id;
Alan Sterne517d312005-07-26 10:18:45 -04001588 __scsi_scan_target(&shost->shost_gendev, channel,
1589 order_id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 }
1591 else
Alan Sterne517d312005-07-26 10:18:45 -04001592 __scsi_scan_target(&shost->shost_gendev, channel,
1593 id, lun, rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
1596int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1597 unsigned int id, unsigned int lun, int rescan)
1598{
Jeff Garzik3bf743e2005-10-24 18:04:06 -04001599 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1600 "%s: <%u:%u:%u>\n",
1601 __FUNCTION__, channel, id, lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001603 if (!shost->async_scan)
1604 scsi_complete_async_scans();
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
Amit Arora091686d2006-05-19 16:14:50 -07001607 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1609 return -EINVAL;
1610
Arjan van de Ven0b950672006-01-11 13:16:10 +01001611 mutex_lock(&shost->scan_mutex);
Mike Anderson82f29462005-06-16 11:14:33 -07001612 if (scsi_host_scan_allowed(shost)) {
1613 if (channel == SCAN_WILD_CARD)
1614 for (channel = 0; channel <= shost->max_channel;
1615 channel++)
1616 scsi_scan_channel(shost, channel, id, lun,
1617 rescan);
1618 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 scsi_scan_channel(shost, channel, id, lun, rescan);
Mike Anderson82f29462005-06-16 11:14:33 -07001620 }
Arjan van de Ven0b950672006-01-11 13:16:10 +01001621 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 return 0;
1624}
1625
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001626static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1627{
1628 struct scsi_device *sdev;
1629 shost_for_each_device(sdev, shost) {
1630 if (scsi_sysfs_add_sdev(sdev) != 0)
1631 scsi_destroy_sdev(sdev);
1632 }
1633}
1634
1635/**
1636 * scsi_prep_async_scan - prepare for an async scan
1637 * @shost: the host which will be scanned
1638 * Returns: a cookie to be passed to scsi_finish_async_scan()
1639 *
1640 * Tells the midlayer this host is going to do an asynchronous scan.
1641 * It reserves the host's position in the scanning list and ensures
1642 * that other asynchronous scans started after this one won't affect the
1643 * ordering of the discovered devices.
1644 */
1645struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1646{
1647 struct async_scan_data *data;
1648
1649 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1650 return NULL;
1651
1652 if (shost->async_scan) {
1653 printk("%s called twice for host %d", __FUNCTION__,
1654 shost->host_no);
1655 dump_stack();
1656 return NULL;
1657 }
1658
1659 data = kmalloc(sizeof(*data), GFP_KERNEL);
1660 if (!data)
1661 goto err;
1662 data->shost = scsi_host_get(shost);
1663 if (!data->shost)
1664 goto err;
1665 init_completion(&data->prev_finished);
1666
1667 spin_lock(&async_scan_lock);
1668 shost->async_scan = 1;
1669 if (list_empty(&scanning_hosts))
1670 complete(&data->prev_finished);
1671 list_add_tail(&data->list, &scanning_hosts);
1672 spin_unlock(&async_scan_lock);
1673
1674 return data;
1675
1676 err:
1677 kfree(data);
1678 return NULL;
1679}
1680
1681/**
1682 * scsi_finish_async_scan - asynchronous scan has finished
1683 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1684 *
1685 * All the devices currently attached to this host have been found.
1686 * This function announces all the devices it has found to the rest
1687 * of the system.
1688 */
1689void scsi_finish_async_scan(struct async_scan_data *data)
1690{
1691 struct Scsi_Host *shost;
1692
1693 if (!data)
1694 return;
1695
1696 shost = data->shost;
1697 if (!shost->async_scan) {
1698 printk("%s called twice for host %d", __FUNCTION__,
1699 shost->host_no);
1700 dump_stack();
1701 return;
1702 }
1703
1704 wait_for_completion(&data->prev_finished);
1705
1706 scsi_sysfs_add_devices(shost);
1707
1708 spin_lock(&async_scan_lock);
1709 shost->async_scan = 0;
1710 list_del(&data->list);
1711 if (!list_empty(&scanning_hosts)) {
1712 struct async_scan_data *next = list_entry(scanning_hosts.next,
1713 struct async_scan_data, list);
1714 complete(&next->prev_finished);
1715 }
1716 spin_unlock(&async_scan_lock);
1717
1718 scsi_host_put(shost);
1719 kfree(data);
1720}
1721
1722static int do_scan_async(void *_data)
1723{
1724 struct async_scan_data *data = _data;
1725 scsi_scan_host_selected(data->shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1726 SCAN_WILD_CARD, 0);
1727
1728 scsi_finish_async_scan(data);
1729 return 0;
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/**
1733 * scsi_scan_host - scan the given adapter
1734 * @shost: adapter to scan
1735 **/
1736void scsi_scan_host(struct Scsi_Host *shost)
1737{
Matthew Wilcox3e082a92006-09-28 15:19:20 -06001738 struct async_scan_data *data;
1739
1740 if (strncmp(scsi_scan_type, "none", 4) == 0)
1741 return;
1742
1743 data = scsi_prep_async_scan(shost);
1744 if (!data) {
1745 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1746 SCAN_WILD_CARD, 0);
1747 return;
1748 }
1749 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751EXPORT_SYMBOL(scsi_scan_host);
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753void scsi_forget_host(struct Scsi_Host *shost)
1754{
Alan Sterna64358d2005-07-26 10:27:10 -04001755 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 unsigned long flags;
1757
Alan Sterna64358d2005-07-26 10:27:10 -04001758 restart:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 spin_lock_irqsave(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001760 list_for_each_entry(sdev, &shost->__devices, siblings) {
1761 if (sdev->sdev_state == SDEV_DEL)
1762 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 spin_unlock_irqrestore(shost->host_lock, flags);
Alan Sterna64358d2005-07-26 10:27:10 -04001764 __scsi_remove_device(sdev);
1765 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 }
1767 spin_unlock_irqrestore(shost->host_lock, flags);
1768}
1769
1770/*
1771 * Function: scsi_get_host_dev()
1772 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001773 * Purpose: Create a scsi_device that points to the host adapter itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001775 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 *
1777 * Lock status: None assumed.
1778 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001779 * Returns: The scsi_device or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 *
1781 * Notes:
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001782 * Attach a single scsi_device to the Scsi_Host - this should
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * be made to look like a "pseudo-device" that points to the
1784 * HA itself.
1785 *
1786 * Note - this device is not accessible from any high-level
1787 * drivers (including generics), which is probably not
1788 * optimal. We can add hooks later to attach
1789 */
1790struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1791{
Alan Sterne517d312005-07-26 10:18:45 -04001792 struct scsi_device *sdev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 struct scsi_target *starget;
1794
Arjan van de Ven0b950672006-01-11 13:16:10 +01001795 mutex_lock(&shost->scan_mutex);
Alan Sterne517d312005-07-26 10:18:45 -04001796 if (!scsi_host_scan_allowed(shost))
1797 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1799 if (!starget)
Alan Sterne517d312005-07-26 10:18:45 -04001800 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 sdev = scsi_alloc_sdev(starget, 0, NULL);
1803 if (sdev) {
1804 sdev->sdev_gendev.parent = get_device(&starget->dev);
1805 sdev->borken = 0;
James Bottomley884d25c2006-09-05 16:26:41 -05001806 } else
1807 scsi_target_reap(starget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 put_device(&starget->dev);
Alan Sterne517d312005-07-26 10:18:45 -04001809 out:
Arjan van de Ven0b950672006-01-11 13:16:10 +01001810 mutex_unlock(&shost->scan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return sdev;
1812}
1813EXPORT_SYMBOL(scsi_get_host_dev);
1814
1815/*
1816 * Function: scsi_free_host_dev()
1817 *
1818 * Purpose: Free a scsi_device that points to the host adapter itself.
1819 *
Christoph Hellwigf64a1812005-10-31 18:32:08 +01001820 * Arguments: SHpnt - Host that needs a scsi_device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 *
1822 * Lock status: None assumed.
1823 *
1824 * Returns: Nothing
1825 *
1826 * Notes:
1827 */
1828void scsi_free_host_dev(struct scsi_device *sdev)
1829{
1830 BUG_ON(sdev->id != sdev->host->this_id);
1831
James Bottomley6f3a2022005-09-22 20:33:28 -05001832 scsi_destroy_sdev(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833}
1834EXPORT_SYMBOL(scsi_free_host_dev);
1835