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