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