4 * Copyright (C) 2000 Eric Youngdale,
5 * Copyright (C) 2002 Patrick Mansfield
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.
11 * A specific LUN is scanned via an INQUIRY command; if the LUN has a
12 * device attached, a scsi_device is allocated and setup for it.
14 * For every id of every channel on the given host:
16 * Scan LUN 0; if the target responds to LUN 0 (even if there is no
17 * device or storage attached to LUN 0):
19 * If LUN 0 has a device attached, allocate and setup a
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.
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/blkdev.h>
32 #include <linux/delay.h>
33 #include <linux/kthread.h>
34 #include <linux/spinlock.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_driver.h>
40 #include <scsi/scsi_devinfo.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi_transport.h>
43 #include <scsi/scsi_eh.h>
45 #include "scsi_priv.h"
46 #include "scsi_logging.h"
48 #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
49 " SCSI scanning, some SCSI devices might not be configured\n"
54 #define SCSI_TIMEOUT (2*HZ)
57 * Prefix values for the SCSI id's (stored in driverfs name field)
59 #define SCSI_UID_SER_NUM 'S'
60 #define SCSI_UID_UNKNOWN 'Z'
63 * Return values of some of the scanning functions.
65 * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
66 * includes allocation or general failures preventing IO from being sent.
68 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
71 * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
74 #define SCSI_SCAN_NO_RESPONSE 0
75 #define SCSI_SCAN_TARGET_PRESENT 1
76 #define SCSI_SCAN_LUN_PRESENT 2
78 static const char *scsi_null_device_strs
= "nullnullnullnull";
80 #define MAX_SCSI_LUNS 512
82 #ifdef CONFIG_SCSI_MULTI_LUN
83 static unsigned int max_scsi_luns
= MAX_SCSI_LUNS
;
85 static unsigned int max_scsi_luns
= 1;
88 module_param_named(max_luns
, max_scsi_luns
, int, S_IRUGO
|S_IWUSR
);
89 MODULE_PARM_DESC(max_luns
,
90 "last scsi LUN (should be between 1 and 2^32-1)");
92 #ifdef CONFIG_SCSI_SCAN_ASYNC
93 #define SCSI_SCAN_TYPE_DEFAULT "async"
95 #define SCSI_SCAN_TYPE_DEFAULT "sync"
98 static char scsi_scan_type
[6] = SCSI_SCAN_TYPE_DEFAULT
;
100 module_param_string(scan
, scsi_scan_type
, sizeof(scsi_scan_type
), S_IRUGO
);
101 MODULE_PARM_DESC(scan
, "sync, async or none");
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
110 static unsigned int max_scsi_report_luns
= 511;
112 module_param_named(max_report_luns
, max_scsi_report_luns
, int, S_IRUGO
|S_IWUSR
);
113 MODULE_PARM_DESC(max_report_luns
,
114 "REPORT LUNS maximum number of LUNS received (should be"
115 " between 1 and 16384)");
117 static unsigned int scsi_inq_timeout
= SCSI_TIMEOUT
/HZ
+3;
119 module_param_named(inq_timeout
, scsi_inq_timeout
, int, S_IRUGO
|S_IWUSR
);
120 MODULE_PARM_DESC(inq_timeout
,
121 "Timeout (in seconds) waiting for devices to answer INQUIRY."
122 " Default is 5. Some non-compliant devices need more.");
124 static DEFINE_SPINLOCK(async_scan_lock
);
125 static LIST_HEAD(scanning_hosts
);
127 struct async_scan_data
{
128 struct list_head list
;
129 struct Scsi_Host
*shost
;
130 struct completion prev_finished
;
134 * scsi_complete_async_scans - Wait for asynchronous scans to complete
136 * Asynchronous scans add themselves to the scanning_hosts list. Once
137 * that list is empty, we know that the scans are complete. Rather than
138 * waking up periodically to check the state of the list, we pretend to be
139 * a scanning task by adding ourselves at the end of the list and going to
140 * sleep. When the task before us wakes us up, we take ourselves off the
143 int scsi_complete_async_scans(void)
145 struct async_scan_data
*data
;
148 if (list_empty(&scanning_hosts
))
150 /* If we can't get memory immediately, that's OK. Just
151 * sleep a little. Even if we never get memory, the async
152 * scans will finish eventually.
154 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
160 init_completion(&data
->prev_finished
);
162 spin_lock(&async_scan_lock
);
163 /* Check that there's still somebody else on the list */
164 if (list_empty(&scanning_hosts
))
166 list_add_tail(&data
->list
, &scanning_hosts
);
167 spin_unlock(&async_scan_lock
);
169 printk(KERN_INFO
"scsi: waiting for bus probes to complete ...\n");
170 wait_for_completion(&data
->prev_finished
);
172 spin_lock(&async_scan_lock
);
173 list_del(&data
->list
);
175 spin_unlock(&async_scan_lock
);
182 /* Only exported for the benefit of scsi_wait_scan */
183 EXPORT_SYMBOL_GPL(scsi_complete_async_scans
);
187 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
188 * @sdev: scsi device to send command to
189 * @result: area to store the result of the MODE SENSE
192 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
193 * Called for BLIST_KEY devices.
195 static void scsi_unlock_floptical(struct scsi_device
*sdev
,
196 unsigned char *result
)
198 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
200 printk(KERN_NOTICE
"scsi: unlocking floptical drive\n");
201 scsi_cmd
[0] = MODE_SENSE
;
205 scsi_cmd
[4] = 0x2a; /* size */
207 scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
, result
, 0x2a, NULL
,
212 * scsi_alloc_sdev - allocate and setup a scsi_Device
215 * Allocate, initialize for io, and return a pointer to a scsi_Device.
216 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
217 * adds scsi_Device to the appropriate list.
220 * scsi_Device pointer, or NULL on failure.
222 static struct scsi_device
*scsi_alloc_sdev(struct scsi_target
*starget
,
223 unsigned int lun
, void *hostdata
)
225 struct scsi_device
*sdev
;
226 int display_failure_msg
= 1, ret
;
227 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
229 sdev
= kzalloc(sizeof(*sdev
) + shost
->transportt
->device_size
,
234 sdev
->vendor
= scsi_null_device_strs
;
235 sdev
->model
= scsi_null_device_strs
;
236 sdev
->rev
= scsi_null_device_strs
;
238 sdev
->id
= starget
->id
;
240 sdev
->channel
= starget
->channel
;
241 sdev
->sdev_state
= SDEV_CREATED
;
242 INIT_LIST_HEAD(&sdev
->siblings
);
243 INIT_LIST_HEAD(&sdev
->same_target_siblings
);
244 INIT_LIST_HEAD(&sdev
->cmd_list
);
245 INIT_LIST_HEAD(&sdev
->starved_entry
);
246 spin_lock_init(&sdev
->list_lock
);
248 sdev
->sdev_gendev
.parent
= get_device(&starget
->dev
);
249 sdev
->sdev_target
= starget
;
251 /* usually NULL and set by ->slave_alloc instead */
252 sdev
->hostdata
= hostdata
;
254 /* if the device needs this changing, it may do so in the
255 * slave_configure function */
256 sdev
->max_device_blocked
= SCSI_DEFAULT_DEVICE_BLOCKED
;
259 * Some low level driver could use device->type
264 * Assume that the device will have handshaking problems,
265 * and then fix this field later if it turns out it
270 sdev
->request_queue
= scsi_alloc_queue(sdev
);
271 if (!sdev
->request_queue
) {
272 /* release fn is set up in scsi_sysfs_device_initialise, so
273 * have to free and put manually here */
274 put_device(&starget
->dev
);
279 sdev
->request_queue
->queuedata
= sdev
;
280 scsi_adjust_queue_depth(sdev
, 0, sdev
->host
->cmd_per_lun
);
282 scsi_sysfs_device_initialize(sdev
);
284 if (shost
->hostt
->slave_alloc
) {
285 ret
= shost
->hostt
->slave_alloc(sdev
);
288 * if LLDD reports slave not present, don't clutter
289 * console with alloc failure messages
292 display_failure_msg
= 0;
293 goto out_device_destroy
;
300 transport_destroy_device(&sdev
->sdev_gendev
);
301 put_device(&sdev
->sdev_gendev
);
303 if (display_failure_msg
)
304 printk(ALLOC_FAILURE_MSG
, __FUNCTION__
);
308 static void scsi_target_dev_release(struct device
*dev
)
310 struct device
*parent
= dev
->parent
;
311 struct scsi_target
*starget
= to_scsi_target(dev
);
317 int scsi_is_target_device(const struct device
*dev
)
319 return dev
->release
== scsi_target_dev_release
;
321 EXPORT_SYMBOL(scsi_is_target_device
);
323 static struct scsi_target
*__scsi_find_target(struct device
*parent
,
324 int channel
, uint id
)
326 struct scsi_target
*starget
, *found_starget
= NULL
;
327 struct Scsi_Host
*shost
= dev_to_shost(parent
);
329 * Search for an existing target for this sdev.
331 list_for_each_entry(starget
, &shost
->__targets
, siblings
) {
332 if (starget
->id
== id
&&
333 starget
->channel
== channel
) {
334 found_starget
= starget
;
339 get_device(&found_starget
->dev
);
341 return found_starget
;
345 * scsi_alloc_target - allocate a new or find an existing target
346 * @parent: parent of the target (need not be a scsi host)
347 * @channel: target channel number (zero if no channels)
348 * @id: target id number
350 * Return an existing target if one exists, provided it hasn't already
351 * gone into STARGET_DEL state, otherwise allocate a new target.
353 * The target is returned with an incremented reference, so the caller
354 * is responsible for both reaping and doing a last put
356 static struct scsi_target
*scsi_alloc_target(struct device
*parent
,
357 int channel
, uint id
)
359 struct Scsi_Host
*shost
= dev_to_shost(parent
);
360 struct device
*dev
= NULL
;
362 const int size
= sizeof(struct scsi_target
)
363 + shost
->transportt
->target_size
;
364 struct scsi_target
*starget
;
365 struct scsi_target
*found_target
;
368 starget
= kzalloc(size
, GFP_KERNEL
);
370 printk(KERN_ERR
"%s: allocation failure\n", __FUNCTION__
);
374 device_initialize(dev
);
375 starget
->reap_ref
= 1;
376 dev
->parent
= get_device(parent
);
377 dev
->release
= scsi_target_dev_release
;
378 sprintf(dev
->bus_id
, "target%d:%d:%d",
379 shost
->host_no
, channel
, id
);
381 starget
->channel
= channel
;
382 INIT_LIST_HEAD(&starget
->siblings
);
383 INIT_LIST_HEAD(&starget
->devices
);
384 starget
->state
= STARGET_RUNNING
;
386 spin_lock_irqsave(shost
->host_lock
, flags
);
388 found_target
= __scsi_find_target(parent
, channel
, id
);
392 list_add_tail(&starget
->siblings
, &shost
->__targets
);
393 spin_unlock_irqrestore(shost
->host_lock
, flags
);
394 /* allocate and add */
395 transport_setup_device(dev
);
396 error
= device_add(dev
);
398 dev_err(dev
, "target device_add failed, error %d\n", error
);
399 spin_lock_irqsave(shost
->host_lock
, flags
);
400 list_del_init(&starget
->siblings
);
401 spin_unlock_irqrestore(shost
->host_lock
, flags
);
402 transport_destroy_device(dev
);
407 transport_add_device(dev
);
408 if (shost
->hostt
->target_alloc
) {
409 error
= shost
->hostt
->target_alloc(starget
);
412 dev_printk(KERN_ERR
, dev
, "target allocation failed, error %d\n", error
);
413 /* don't want scsi_target_reap to do the final
414 * put because it will be under the host lock */
416 scsi_target_reap(starget
);
426 found_target
->reap_ref
++;
427 spin_unlock_irqrestore(shost
->host_lock
, flags
);
428 if (found_target
->state
!= STARGET_DEL
) {
433 /* Unfortunately, we found a dying target; need to
434 * wait until it's dead before we can get a new one */
435 put_device(&found_target
->dev
);
436 flush_scheduled_work();
440 static void scsi_target_reap_usercontext(struct work_struct
*work
)
442 struct scsi_target
*starget
=
443 container_of(work
, struct scsi_target
, ew
.work
);
444 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
447 transport_remove_device(&starget
->dev
);
448 device_del(&starget
->dev
);
449 transport_destroy_device(&starget
->dev
);
450 spin_lock_irqsave(shost
->host_lock
, flags
);
451 if (shost
->hostt
->target_destroy
)
452 shost
->hostt
->target_destroy(starget
);
453 list_del_init(&starget
->siblings
);
454 spin_unlock_irqrestore(shost
->host_lock
, flags
);
455 put_device(&starget
->dev
);
459 * scsi_target_reap - check to see if target is in use and destroy if not
461 * @starget: target to be checked
463 * This is used after removing a LUN or doing a last put of the target
464 * it checks atomically that nothing is using the target and removes
467 void scsi_target_reap(struct scsi_target
*starget
)
469 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
472 spin_lock_irqsave(shost
->host_lock
, flags
);
474 if (--starget
->reap_ref
== 0 && list_empty(&starget
->devices
)) {
475 BUG_ON(starget
->state
== STARGET_DEL
);
476 starget
->state
= STARGET_DEL
;
477 spin_unlock_irqrestore(shost
->host_lock
, flags
);
478 execute_in_process_context(scsi_target_reap_usercontext
,
483 spin_unlock_irqrestore(shost
->host_lock
, flags
);
489 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
490 * @s: INQUIRY result string to sanitize
491 * @len: length of the string
494 * The SCSI spec says that INQUIRY vendor, product, and revision
495 * strings must consist entirely of graphic ASCII characters,
496 * padded on the right with spaces. Since not all devices obey
497 * this rule, we will replace non-graphic or non-ASCII characters
498 * with spaces. Exception: a NUL character is interpreted as a
499 * string terminator, so all the following characters are set to
502 static void sanitize_inquiry_string(unsigned char *s
, int len
)
506 for (; len
> 0; (--len
, ++s
)) {
509 if (terminated
|| *s
< 0x20 || *s
> 0x7e)
515 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
516 * @sdev: scsi_device to probe
517 * @inq_result: area to store the INQUIRY result
518 * @result_len: len of inq_result
519 * @bflags: store any bflags found here
522 * Probe the lun associated with @req using a standard SCSI INQUIRY;
524 * If the INQUIRY is successful, zero is returned and the
525 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
526 * are copied to the scsi_device any flags value is stored in *@bflags.
528 static int scsi_probe_lun(struct scsi_device
*sdev
, unsigned char *inq_result
,
529 int result_len
, int *bflags
)
531 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
532 int first_inquiry_len
, try_inquiry_len
, next_inquiry_len
;
533 int response_len
= 0;
534 int pass
, count
, result
;
535 struct scsi_sense_hdr sshdr
;
539 /* Perform up to 3 passes. The first pass uses a conservative
540 * transfer length of 36 unless sdev->inquiry_len specifies a
541 * different value. */
542 first_inquiry_len
= sdev
->inquiry_len
? sdev
->inquiry_len
: 36;
543 try_inquiry_len
= first_inquiry_len
;
547 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO
, sdev
,
548 "scsi scan: INQUIRY pass %d length %d\n",
549 pass
, try_inquiry_len
));
551 /* Each pass gets up to three chances to ignore Unit Attention */
552 for (count
= 0; count
< 3; ++count
) {
553 memset(scsi_cmd
, 0, 6);
554 scsi_cmd
[0] = INQUIRY
;
555 scsi_cmd
[4] = (unsigned char) try_inquiry_len
;
557 memset(inq_result
, 0, try_inquiry_len
);
559 result
= scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
,
560 inq_result
, try_inquiry_len
, &sshdr
,
561 HZ
/ 2 + HZ
* scsi_inq_timeout
, 3);
563 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
"scsi scan: INQUIRY %s "
565 result
? "failed" : "successful", result
));
569 * not-ready to ready transition [asc/ascq=0x28/0x0]
570 * or power-on, reset [asc/ascq=0x29/0x0], continue.
571 * INQUIRY should not yield UNIT_ATTENTION
572 * but many buggy devices do so anyway.
574 if ((driver_byte(result
) & DRIVER_SENSE
) &&
575 scsi_sense_valid(&sshdr
)) {
576 if ((sshdr
.sense_key
== UNIT_ATTENTION
) &&
577 ((sshdr
.asc
== 0x28) ||
578 (sshdr
.asc
== 0x29)) &&
587 sanitize_inquiry_string(&inq_result
[8], 8);
588 sanitize_inquiry_string(&inq_result
[16], 16);
589 sanitize_inquiry_string(&inq_result
[32], 4);
591 response_len
= inq_result
[4] + 5;
592 if (response_len
> 255)
593 response_len
= first_inquiry_len
; /* sanity */
596 * Get any flags for this device.
598 * XXX add a bflags to scsi_device, and replace the
599 * corresponding bit fields in scsi_device, so bflags
600 * need not be passed as an argument.
602 *bflags
= scsi_get_device_flags(sdev
, &inq_result
[8],
605 /* When the first pass succeeds we gain information about
606 * what larger transfer lengths might work. */
608 if (BLIST_INQUIRY_36
& *bflags
)
609 next_inquiry_len
= 36;
610 else if (BLIST_INQUIRY_58
& *bflags
)
611 next_inquiry_len
= 58;
612 else if (sdev
->inquiry_len
)
613 next_inquiry_len
= sdev
->inquiry_len
;
615 next_inquiry_len
= response_len
;
617 /* If more data is available perform the second pass */
618 if (next_inquiry_len
> try_inquiry_len
) {
619 try_inquiry_len
= next_inquiry_len
;
625 } else if (pass
== 2) {
626 printk(KERN_INFO
"scsi scan: %d byte inquiry failed. "
627 "Consider BLIST_INQUIRY_36 for this device\n",
630 /* If this pass failed, the third pass goes back and transfers
631 * the same amount as we successfully got in the first pass. */
632 try_inquiry_len
= first_inquiry_len
;
637 /* If the last transfer attempt got an error, assume the
638 * peripheral doesn't exist or is dead. */
642 /* Don't report any more data than the device says is valid */
643 sdev
->inquiry_len
= min(try_inquiry_len
, response_len
);
646 * XXX Abort if the response length is less than 36? If less than
647 * 32, the lookup of the device flags (above) could be invalid,
648 * and it would be possible to take an incorrect action - we do
649 * not want to hang because of a short INQUIRY. On the flip side,
650 * if the device is spun down or becoming ready (and so it gives a
651 * short INQUIRY), an abort here prevents any further use of the
652 * device, including spin up.
654 * Related to the above issue:
656 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
657 * and if not ready, sent a START_STOP to start (maybe spin up) and
658 * then send the INQUIRY again, since the INQUIRY can change after
659 * a device is initialized.
661 * Ideally, start a device if explicitly asked to do so. This
662 * assumes that a device is spun up on power on, spun down on
663 * request, and then spun up on request.
667 * The scanning code needs to know the scsi_level, even if no
668 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
669 * non-zero LUNs can be scanned.
671 sdev
->scsi_level
= inq_result
[2] & 0x07;
672 if (sdev
->scsi_level
>= 2 ||
673 (sdev
->scsi_level
== 1 && (inq_result
[3] & 0x0f) == 1))
675 sdev
->sdev_target
->scsi_level
= sdev
->scsi_level
;
681 * scsi_add_lun - allocate and fully initialze a scsi_device
682 * @sdevscan: holds information to be stored in the new scsi_device
683 * @sdevnew: store the address of the newly allocated scsi_device
684 * @inq_result: holds the result of a previous INQUIRY to the LUN
685 * @bflags: black/white list flag
688 * Allocate and initialize a scsi_device matching sdevscan. Optionally
689 * set fields based on values in *@bflags. If @sdevnew is not
690 * NULL, store the address of the new scsi_device in *@sdevnew (needed
691 * when scanning a particular LUN).
694 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
695 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
697 static int scsi_add_lun(struct scsi_device
*sdev
, unsigned char *inq_result
,
698 int *bflags
, int async
)
701 * XXX do not save the inquiry, since it can change underneath us,
702 * save just vendor/model/rev.
704 * Rather than save it and have an ioctl that retrieves the saved
705 * value, have an ioctl that executes the same INQUIRY code used
706 * in scsi_probe_lun, let user level programs doing INQUIRY
707 * scanning run at their own risk, or supply a user level program
708 * that can correctly scan.
712 * Copy at least 36 bytes of INQUIRY data, so that we don't
713 * dereference unallocated memory when accessing the Vendor,
714 * Product, and Revision strings. Badly behaved devices may set
715 * the INQUIRY Additional Length byte to a small value, indicating
716 * these strings are invalid, but often they contain plausible data
717 * nonetheless. It doesn't matter if the device sent < 36 bytes
718 * total, since scsi_probe_lun() initializes inq_result with 0s.
720 sdev
->inquiry
= kmemdup(inq_result
,
721 max_t(size_t, sdev
->inquiry_len
, 36),
723 if (sdev
->inquiry
== NULL
)
724 return SCSI_SCAN_NO_RESPONSE
;
726 sdev
->vendor
= (char *) (sdev
->inquiry
+ 8);
727 sdev
->model
= (char *) (sdev
->inquiry
+ 16);
728 sdev
->rev
= (char *) (sdev
->inquiry
+ 32);
730 if (*bflags
& BLIST_ISROM
) {
732 * It would be better to modify sdev->type, and set
733 * sdev->removable; this can now be done since
734 * print_inquiry has gone away.
736 inq_result
[0] = TYPE_ROM
;
737 inq_result
[1] |= 0x80; /* removable */
738 } else if (*bflags
& BLIST_NO_ULD_ATTACH
)
739 sdev
->no_uld_attach
= 1;
741 switch (sdev
->type
= (inq_result
[0] & 0x1f)) {
748 case TYPE_MEDIUM_CHANGER
:
760 printk(KERN_INFO
"scsi: unknown device type %d\n", sdev
->type
);
764 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
765 * spec says: The device server is capable of supporting the
766 * specified peripheral device type on this logical unit. However,
767 * the physical device is not currently connected to this logical
770 * The above is vague, as it implies that we could treat 001 and
771 * 011 the same. Stay compatible with previous code, and create a
772 * scsi_device for a PQ of 1
774 * Don't set the device offline here; rather let the upper
775 * level drivers eval the PQ to decide whether they should
776 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
779 sdev
->inq_periph_qual
= (inq_result
[0] >> 5) & 7;
780 sdev
->removable
= (0x80 & inq_result
[1]) >> 7;
781 sdev
->lockable
= sdev
->removable
;
782 sdev
->soft_reset
= (inq_result
[7] & 1) && ((inq_result
[3] & 7) == 2);
784 if (sdev
->scsi_level
>= SCSI_3
|| (sdev
->inquiry_len
> 56 &&
785 inq_result
[56] & 0x04))
787 if (inq_result
[7] & 0x60)
789 if (inq_result
[7] & 0x10)
792 sdev_printk(KERN_NOTICE
, sdev
, "%s %.8s %.16s %.4s PQ: %d "
793 "ANSI: %d%s\n", scsi_device_type(sdev
->type
),
794 sdev
->vendor
, sdev
->model
, sdev
->rev
,
795 sdev
->inq_periph_qual
, inq_result
[2] & 0x07,
796 (inq_result
[3] & 0x0f) == 1 ? " CCS" : "");
802 if ((sdev
->scsi_level
>= SCSI_2
) && (inq_result
[7] & 2) &&
803 !(*bflags
& BLIST_NOTQ
))
804 sdev
->tagged_supported
= 1;
806 * Some devices (Texel CD ROM drives) have handshaking problems
807 * when used with the Seagate controllers. borken is initialized
808 * to 1, and then set it to 0 here.
810 if ((*bflags
& BLIST_BORKEN
) == 0)
814 * Apparently some really broken devices (contrary to the SCSI
815 * standards) need to be selected without asserting ATN
817 if (*bflags
& BLIST_SELECT_NO_ATN
)
818 sdev
->select_no_atn
= 1;
821 * Maximum 512 sector transfer length
822 * broken RA4x00 Compaq Disk Array
824 if (*bflags
& BLIST_MAX_512
)
825 blk_queue_max_sectors(sdev
->request_queue
, 512);
828 * Some devices may not want to have a start command automatically
829 * issued when a device is added.
831 if (*bflags
& BLIST_NOSTARTONADD
)
832 sdev
->no_start_on_add
= 1;
834 if (*bflags
& BLIST_SINGLELUN
)
835 sdev
->single_lun
= 1;
838 sdev
->use_10_for_rw
= 1;
840 if (*bflags
& BLIST_MS_SKIP_PAGE_08
)
841 sdev
->skip_ms_page_8
= 1;
843 if (*bflags
& BLIST_MS_SKIP_PAGE_3F
)
844 sdev
->skip_ms_page_3f
= 1;
846 if (*bflags
& BLIST_USE_10_BYTE_MS
)
847 sdev
->use_10_for_ms
= 1;
849 /* set the device running here so that slave configure
851 scsi_device_set_state(sdev
, SDEV_RUNNING
);
853 if (*bflags
& BLIST_MS_192_BYTES_FOR_3F
)
854 sdev
->use_192_bytes_for_3f
= 1;
856 if (*bflags
& BLIST_NOT_LOCKABLE
)
859 if (*bflags
& BLIST_RETRY_HWERROR
)
860 sdev
->retry_hwerror
= 1;
862 transport_configure_device(&sdev
->sdev_gendev
);
864 if (sdev
->host
->hostt
->slave_configure
) {
865 int ret
= sdev
->host
->hostt
->slave_configure(sdev
);
868 * if LLDD reports slave not present, don't clutter
869 * console with alloc failure messages
872 sdev_printk(KERN_ERR
, sdev
,
873 "failed to configure device\n");
875 return SCSI_SCAN_NO_RESPONSE
;
880 * Ok, the device is now all set up, we can
881 * register it and tell the rest of the kernel
884 if (!async
&& scsi_sysfs_add_sdev(sdev
) != 0)
885 return SCSI_SCAN_NO_RESPONSE
;
887 return SCSI_SCAN_LUN_PRESENT
;
890 static inline void scsi_destroy_sdev(struct scsi_device
*sdev
)
892 scsi_device_set_state(sdev
, SDEV_DEL
);
893 if (sdev
->host
->hostt
->slave_destroy
)
894 sdev
->host
->hostt
->slave_destroy(sdev
);
895 transport_destroy_device(&sdev
->sdev_gendev
);
896 put_device(&sdev
->sdev_gendev
);
899 #ifdef CONFIG_SCSI_LOGGING
901 * scsi_inq_str - print INQUIRY data from min to max index,
902 * strip trailing whitespace
903 * @buf: Output buffer with at least end-first+1 bytes of space
904 * @inq: Inquiry buffer (input)
905 * @first: Offset of string into inq
906 * @end: Index after last character in inq
908 static unsigned char *scsi_inq_str(unsigned char *buf
, unsigned char *inq
,
909 unsigned first
, unsigned end
)
911 unsigned term
= 0, idx
;
913 for (idx
= 0; idx
+ first
< end
&& idx
+ first
< inq
[4] + 5; idx
++) {
914 if (inq
[idx
+first
] > ' ') {
915 buf
[idx
] = inq
[idx
+first
];
927 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
928 * @starget: pointer to target device structure
929 * @lun: LUN of target device
930 * @sdevscan: probe the LUN corresponding to this scsi_device
931 * @sdevnew: store the value of any new scsi_device allocated
932 * @bflagsp: store bflags here if not NULL
935 * Call scsi_probe_lun, if a LUN with an attached device is found,
936 * allocate and set it up by calling scsi_add_lun.
939 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
940 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
941 * attached at the LUN
942 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
944 static int scsi_probe_and_add_lun(struct scsi_target
*starget
,
945 uint lun
, int *bflagsp
,
946 struct scsi_device
**sdevp
, int rescan
,
949 struct scsi_device
*sdev
;
950 unsigned char *result
;
951 int bflags
, res
= SCSI_SCAN_NO_RESPONSE
, result_len
= 256;
952 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
955 * The rescan flag is used as an optimization, the first scan of a
956 * host adapter calls into here with rescan == 0.
958 sdev
= scsi_device_lookup_by_target(starget
, lun
);
960 if (rescan
|| sdev
->sdev_state
!= SDEV_CREATED
) {
961 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
962 "scsi scan: device exists on %s\n",
963 sdev
->sdev_gendev
.bus_id
));
967 scsi_device_put(sdev
);
970 *bflagsp
= scsi_get_device_flags(sdev
,
973 return SCSI_SCAN_LUN_PRESENT
;
975 scsi_device_put(sdev
);
977 sdev
= scsi_alloc_sdev(starget
, lun
, hostdata
);
981 result
= kmalloc(result_len
, GFP_ATOMIC
|
982 ((shost
->unchecked_isa_dma
) ? __GFP_DMA
: 0));
986 if (scsi_probe_lun(sdev
, result
, result_len
, &bflags
))
987 goto out_free_result
;
992 * result contains valid SCSI INQUIRY data.
994 if (((result
[0] >> 5) == 3) && !(bflags
& BLIST_ATTACH_PQ3
)) {
996 * For a Peripheral qualifier 3 (011b), the SCSI
997 * spec says: The device server is not capable of
998 * supporting a physical device on this logical
1001 * For disks, this implies that there is no
1002 * logical disk configured at sdev->lun, but there
1003 * is a target id responding.
1005 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO
, sdev
, "scsi scan:"
1006 " peripheral qualifier of 3, device not"
1009 SCSI_LOG_SCAN_BUS(1, {
1010 unsigned char vend
[9];
1011 unsigned char mod
[17];
1013 sdev_printk(KERN_INFO
, sdev
,
1014 "scsi scan: consider passing scsi_mod."
1015 "dev_flags=%s:%s:0x240 or 0x800240\n",
1016 scsi_inq_str(vend
, result
, 8, 16),
1017 scsi_inq_str(mod
, result
, 16, 32));
1021 res
= SCSI_SCAN_TARGET_PRESENT
;
1022 goto out_free_result
;
1026 * Some targets may set slight variations of PQ and PDT to signal
1027 * that no LUN is present, so don't add sdev in these cases.
1028 * Two specific examples are:
1029 * 1) NetApp targets: return PQ=1, PDT=0x1f
1030 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1031 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1034 * 1) SCSI SPC-3, pp. 145-146
1035 * PQ=1: "A peripheral device having the specified peripheral
1036 * device type is not connected to this logical unit. However, the
1037 * device server is capable of supporting the specified peripheral
1038 * device type on this logical unit."
1039 * PDT=0x1f: "Unknown or no device type"
1040 * 2) USB UFI 1.0, p. 20
1041 * PDT=00h Direct-access device (floppy)
1042 * PDT=1Fh none (no FDD connected to the requested logical unit)
1044 if (((result
[0] >> 5) == 1 || starget
->pdt_1f_for_no_lun
) &&
1045 (result
[0] & 0x1f) == 0x1f) {
1046 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1047 "scsi scan: peripheral device type"
1048 " of 31, no device added\n"));
1049 res
= SCSI_SCAN_TARGET_PRESENT
;
1050 goto out_free_result
;
1053 res
= scsi_add_lun(sdev
, result
, &bflags
, shost
->async_scan
);
1054 if (res
== SCSI_SCAN_LUN_PRESENT
) {
1055 if (bflags
& BLIST_KEY
) {
1057 scsi_unlock_floptical(sdev
, result
);
1064 if (res
== SCSI_SCAN_LUN_PRESENT
) {
1066 if (scsi_device_get(sdev
) == 0) {
1069 __scsi_remove_device(sdev
);
1070 res
= SCSI_SCAN_NO_RESPONSE
;
1074 scsi_destroy_sdev(sdev
);
1080 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1081 * @starget: pointer to target structure to scan
1082 * @bflags: black/white list flag for LUN 0
1085 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1086 * scanned) to some maximum lun until a LUN is found with no device
1087 * attached. Use the bflags to figure out any oddities.
1089 * Modifies sdevscan->lun.
1091 static void scsi_sequential_lun_scan(struct scsi_target
*starget
,
1092 int bflags
, int scsi_level
, int rescan
)
1094 unsigned int sparse_lun
, lun
, max_dev_lun
;
1095 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
1097 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
"scsi scan: Sequential scan of"
1098 "%s\n", starget
->dev
.bus_id
));
1100 max_dev_lun
= min(max_scsi_luns
, shost
->max_lun
);
1102 * If this device is known to support sparse multiple units,
1103 * override the other settings, and scan all of them. Normally,
1104 * SCSI-3 devices should be scanned via the REPORT LUNS.
1106 if (bflags
& BLIST_SPARSELUN
) {
1107 max_dev_lun
= shost
->max_lun
;
1113 * If less than SCSI_1_CSS, and no special lun scaning, stop
1114 * scanning; this matches 2.4 behaviour, but could just be a bug
1115 * (to continue scanning a SCSI_1_CSS device).
1117 * This test is broken. We might not have any device on lun0 for
1118 * a sparselun device, and if that's the case then how would we
1119 * know the real scsi_level, eh? It might make sense to just not
1120 * scan any SCSI_1 device for non-0 luns, but that check would best
1121 * go into scsi_alloc_sdev() and just have it return null when asked
1122 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1124 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1125 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1130 * If this device is known to support multiple units, override
1131 * the other settings, and scan all of them.
1133 if (bflags
& BLIST_FORCELUN
)
1134 max_dev_lun
= shost
->max_lun
;
1136 * REGAL CDC-4X: avoid hang after LUN 4
1138 if (bflags
& BLIST_MAX5LUN
)
1139 max_dev_lun
= min(5U, max_dev_lun
);
1141 * Do not scan SCSI-2 or lower device past LUN 7, unless
1144 if (scsi_level
< SCSI_3
&& !(bflags
& BLIST_LARGELUN
))
1145 max_dev_lun
= min(8U, max_dev_lun
);
1148 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1149 * until we reach the max, or no LUN is found and we are not
1152 for (lun
= 1; lun
< max_dev_lun
; ++lun
)
1153 if ((scsi_probe_and_add_lun(starget
, lun
, NULL
, NULL
, rescan
,
1154 NULL
) != SCSI_SCAN_LUN_PRESENT
) &&
1160 * scsilun_to_int: convert a scsi_lun to an int
1161 * @scsilun: struct scsi_lun to be converted.
1164 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1165 * integer, and return the result. The caller must check for
1166 * truncation before using this function.
1169 * The struct scsi_lun is assumed to be four levels, with each level
1170 * effectively containing a SCSI byte-ordered (big endian) short; the
1171 * addressing bits of each level are ignored (the highest two bits).
1172 * For a description of the LUN format, post SCSI-3 see the SCSI
1173 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1175 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1176 * the integer: 0x0b030a04
1178 static int scsilun_to_int(struct scsi_lun
*scsilun
)
1184 for (i
= 0; i
< sizeof(lun
); i
+= 2)
1185 lun
= lun
| (((scsilun
->scsi_lun
[i
] << 8) |
1186 scsilun
->scsi_lun
[i
+ 1]) << (i
* 8));
1191 * int_to_scsilun: reverts an int into a scsi_lun
1192 * @int: integer to be reverted
1193 * @scsilun: struct scsi_lun to be set.
1196 * Reverts the functionality of the scsilun_to_int, which packed
1197 * an 8-byte lun value into an int. This routine unpacks the int
1198 * back into the lun value.
1199 * Note: the scsilun_to_int() routine does not truly handle all
1200 * 8bytes of the lun value. This functions restores only as much
1201 * as was set by the routine.
1204 * Given an integer : 0x0b030a04, this function returns a
1205 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1208 void int_to_scsilun(unsigned int lun
, struct scsi_lun
*scsilun
)
1212 memset(scsilun
->scsi_lun
, 0, sizeof(scsilun
->scsi_lun
));
1214 for (i
= 0; i
< sizeof(lun
); i
+= 2) {
1215 scsilun
->scsi_lun
[i
] = (lun
>> 8) & 0xFF;
1216 scsilun
->scsi_lun
[i
+1] = lun
& 0xFF;
1220 EXPORT_SYMBOL(int_to_scsilun
);
1223 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1224 * @sdevscan: scan the host, channel, and id of this scsi_device
1227 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1228 * command, and scan the resulting list of LUNs by calling
1229 * scsi_probe_and_add_lun.
1231 * Modifies sdevscan->lun.
1234 * 0: scan completed (or no memory, so further scanning is futile)
1235 * 1: no report lun scan, or not configured
1237 static int scsi_report_lun_scan(struct scsi_target
*starget
, int bflags
,
1241 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
1242 unsigned int length
;
1244 unsigned int num_luns
;
1245 unsigned int retries
;
1247 struct scsi_lun
*lunp
, *lun_data
;
1249 struct scsi_sense_hdr sshdr
;
1250 struct scsi_device
*sdev
;
1251 struct Scsi_Host
*shost
= dev_to_shost(&starget
->dev
);
1255 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1256 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1257 * support more than 8 LUNs.
1259 if (bflags
& BLIST_NOREPORTLUN
)
1261 if (starget
->scsi_level
< SCSI_2
&&
1262 starget
->scsi_level
!= SCSI_UNKNOWN
)
1264 if (starget
->scsi_level
< SCSI_3
&&
1265 (!(bflags
& BLIST_REPORTLUN2
) || shost
->max_lun
<= 8))
1267 if (bflags
& BLIST_NOLUN
)
1270 if (!(sdev
= scsi_device_lookup_by_target(starget
, 0))) {
1271 sdev
= scsi_alloc_sdev(starget
, 0, NULL
);
1274 if (scsi_device_get(sdev
))
1278 sprintf(devname
, "host %d channel %d id %d",
1279 shost
->host_no
, sdev
->channel
, sdev
->id
);
1282 * Allocate enough to hold the header (the same size as one scsi_lun)
1283 * plus the max number of luns we are requesting.
1285 * Reallocating and trying again (with the exact amount we need)
1286 * would be nice, but then we need to somehow limit the size
1287 * allocated based on the available memory and the limits of
1288 * kmalloc - we don't want a kmalloc() failure of a huge value to
1289 * prevent us from finding any LUNs on this target.
1291 length
= (max_scsi_report_luns
+ 1) * sizeof(struct scsi_lun
);
1292 lun_data
= kmalloc(length
, GFP_ATOMIC
|
1293 (sdev
->host
->unchecked_isa_dma
? __GFP_DMA
: 0));
1295 printk(ALLOC_FAILURE_MSG
, __FUNCTION__
);
1299 scsi_cmd
[0] = REPORT_LUNS
;
1302 * bytes 1 - 5: reserved, set to zero.
1304 memset(&scsi_cmd
[1], 0, 5);
1307 * bytes 6 - 9: length of the command.
1309 scsi_cmd
[6] = (unsigned char) (length
>> 24) & 0xff;
1310 scsi_cmd
[7] = (unsigned char) (length
>> 16) & 0xff;
1311 scsi_cmd
[8] = (unsigned char) (length
>> 8) & 0xff;
1312 scsi_cmd
[9] = (unsigned char) length
& 0xff;
1314 scsi_cmd
[10] = 0; /* reserved */
1315 scsi_cmd
[11] = 0; /* control */
1318 * We can get a UNIT ATTENTION, for example a power on/reset, so
1319 * retry a few times (like sd.c does for TEST UNIT READY).
1320 * Experience shows some combinations of adapter/devices get at
1321 * least two power on/resets.
1323 * Illegal requests (for devices that do not support REPORT LUNS)
1324 * should come through as a check condition, and will not generate
1327 for (retries
= 0; retries
< 3; retries
++) {
1328 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO
"scsi scan: Sending"
1329 " REPORT LUNS to %s (try %d)\n", devname
,
1332 result
= scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
,
1333 lun_data
, length
, &sshdr
,
1334 SCSI_TIMEOUT
+ 4 * HZ
, 3);
1336 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO
"scsi scan: REPORT LUNS"
1337 " %s (try %d) result 0x%x\n", result
1338 ? "failed" : "successful", retries
, result
));
1341 else if (scsi_sense_valid(&sshdr
)) {
1342 if (sshdr
.sense_key
!= UNIT_ATTENTION
)
1349 * The device probably does not support a REPORT LUN command
1356 * Get the length from the first four bytes of lun_data.
1358 data
= (u8
*) lun_data
->scsi_lun
;
1359 length
= ((data
[0] << 24) | (data
[1] << 16) |
1360 (data
[2] << 8) | (data
[3] << 0));
1362 num_luns
= (length
/ sizeof(struct scsi_lun
));
1363 if (num_luns
> max_scsi_report_luns
) {
1364 printk(KERN_WARNING
"scsi: On %s only %d (max_scsi_report_luns)"
1365 " of %d luns reported, try increasing"
1366 " max_scsi_report_luns.\n", devname
,
1367 max_scsi_report_luns
, num_luns
);
1368 num_luns
= max_scsi_report_luns
;
1371 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO
, sdev
,
1372 "scsi scan: REPORT LUN scan\n"));
1375 * Scan the luns in lun_data. The entry at offset 0 is really
1376 * the header, so start at 1 and go up to and including num_luns.
1378 for (lunp
= &lun_data
[1]; lunp
<= &lun_data
[num_luns
]; lunp
++) {
1379 lun
= scsilun_to_int(lunp
);
1382 * Check if the unused part of lunp is non-zero, and so
1383 * does not fit in lun.
1385 if (memcmp(&lunp
->scsi_lun
[sizeof(lun
)], "\0\0\0\0", 4)) {
1389 * Output an error displaying the LUN in byte order,
1390 * this differs from what linux would print for the
1391 * integer LUN value.
1393 printk(KERN_WARNING
"scsi: %s lun 0x", devname
);
1394 data
= (char *)lunp
->scsi_lun
;
1395 for (i
= 0; i
< sizeof(struct scsi_lun
); i
++)
1396 printk("%02x", data
[i
]);
1397 printk(" has a LUN larger than currently supported.\n");
1398 } else if (lun
> sdev
->host
->max_lun
) {
1399 printk(KERN_WARNING
"scsi: %s lun%d has a LUN larger"
1400 " than allowed by the host adapter\n",
1405 res
= scsi_probe_and_add_lun(starget
,
1406 lun
, NULL
, NULL
, rescan
, NULL
);
1407 if (res
== SCSI_SCAN_NO_RESPONSE
) {
1409 * Got some results, but now none, abort.
1411 sdev_printk(KERN_ERR
, sdev
,
1412 "Unexpected response"
1413 " from lun %d while scanning, scan"
1423 scsi_device_put(sdev
);
1424 if (sdev
->sdev_state
== SDEV_CREATED
)
1426 * the sdev we used didn't appear in the report luns scan
1428 scsi_destroy_sdev(sdev
);
1432 struct scsi_device
*__scsi_add_device(struct Scsi_Host
*shost
, uint channel
,
1433 uint id
, uint lun
, void *hostdata
)
1435 struct scsi_device
*sdev
= ERR_PTR(-ENODEV
);
1436 struct device
*parent
= &shost
->shost_gendev
;
1437 struct scsi_target
*starget
;
1439 starget
= scsi_alloc_target(parent
, channel
, id
);
1441 return ERR_PTR(-ENOMEM
);
1443 mutex_lock(&shost
->scan_mutex
);
1444 if (scsi_host_scan_allowed(shost
))
1445 scsi_probe_and_add_lun(starget
, lun
, NULL
, &sdev
, 1, hostdata
);
1446 mutex_unlock(&shost
->scan_mutex
);
1447 scsi_target_reap(starget
);
1448 put_device(&starget
->dev
);
1452 EXPORT_SYMBOL(__scsi_add_device
);
1454 int scsi_add_device(struct Scsi_Host
*host
, uint channel
,
1455 uint target
, uint lun
)
1457 struct scsi_device
*sdev
=
1458 __scsi_add_device(host
, channel
, target
, lun
, NULL
);
1460 return PTR_ERR(sdev
);
1462 scsi_device_put(sdev
);
1465 EXPORT_SYMBOL(scsi_add_device
);
1467 void scsi_rescan_device(struct device
*dev
)
1469 struct scsi_driver
*drv
;
1474 drv
= to_scsi_driver(dev
->driver
);
1475 if (try_module_get(drv
->owner
)) {
1478 module_put(drv
->owner
);
1481 EXPORT_SYMBOL(scsi_rescan_device
);
1483 static void __scsi_scan_target(struct device
*parent
, unsigned int channel
,
1484 unsigned int id
, unsigned int lun
, int rescan
)
1486 struct Scsi_Host
*shost
= dev_to_shost(parent
);
1489 struct scsi_target
*starget
;
1491 if (shost
->this_id
== id
)
1493 * Don't scan the host adapter
1497 starget
= scsi_alloc_target(parent
, channel
, id
);
1501 if (lun
!= SCAN_WILD_CARD
) {
1503 * Scan for a specific host/chan/id/lun.
1505 scsi_probe_and_add_lun(starget
, lun
, NULL
, NULL
, rescan
, NULL
);
1510 * Scan LUN 0, if there is some response, scan further. Ideally, we
1511 * would not configure LUN 0 until all LUNs are scanned.
1513 res
= scsi_probe_and_add_lun(starget
, 0, &bflags
, NULL
, rescan
, NULL
);
1514 if (res
== SCSI_SCAN_LUN_PRESENT
|| res
== SCSI_SCAN_TARGET_PRESENT
) {
1515 if (scsi_report_lun_scan(starget
, bflags
, rescan
) != 0)
1517 * The REPORT LUN did not scan the target,
1518 * do a sequential scan.
1520 scsi_sequential_lun_scan(starget
, bflags
,
1521 starget
->scsi_level
, rescan
);
1525 /* now determine if the target has any children at all
1526 * and if not, nuke it */
1527 scsi_target_reap(starget
);
1529 put_device(&starget
->dev
);
1533 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1535 * @parent: host to scan
1536 * @channel: channel to scan
1537 * @id: target id to scan
1538 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1539 * @rescan: passed to LUN scanning routines
1542 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1543 * and possibly all LUNs on the target id.
1545 * First try a REPORT LUN scan, if that does not scan the target, do a
1546 * sequential scan of LUNs on the target id.
1548 void scsi_scan_target(struct device
*parent
, unsigned int channel
,
1549 unsigned int id
, unsigned int lun
, int rescan
)
1551 struct Scsi_Host
*shost
= dev_to_shost(parent
);
1553 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1556 if (!shost
->async_scan
)
1557 scsi_complete_async_scans();
1559 mutex_lock(&shost
->scan_mutex
);
1560 if (scsi_host_scan_allowed(shost
))
1561 __scsi_scan_target(parent
, channel
, id
, lun
, rescan
);
1562 mutex_unlock(&shost
->scan_mutex
);
1564 EXPORT_SYMBOL(scsi_scan_target
);
1566 static void scsi_scan_channel(struct Scsi_Host
*shost
, unsigned int channel
,
1567 unsigned int id
, unsigned int lun
, int rescan
)
1571 if (id
== SCAN_WILD_CARD
)
1572 for (id
= 0; id
< shost
->max_id
; ++id
) {
1574 * XXX adapter drivers when possible (FCP, iSCSI)
1575 * could modify max_id to match the current max,
1576 * not the absolute max.
1578 * XXX add a shost id iterator, so for example,
1579 * the FC ID can be the same as a target id
1580 * without a huge overhead of sparse id's.
1582 if (shost
->reverse_ordering
)
1584 * Scan from high to low id.
1586 order_id
= shost
->max_id
- id
- 1;
1589 __scsi_scan_target(&shost
->shost_gendev
, channel
,
1590 order_id
, lun
, rescan
);
1593 __scsi_scan_target(&shost
->shost_gendev
, channel
,
1597 int scsi_scan_host_selected(struct Scsi_Host
*shost
, unsigned int channel
,
1598 unsigned int id
, unsigned int lun
, int rescan
)
1600 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO
, shost
,
1602 __FUNCTION__
, channel
, id
, lun
));
1604 if (!shost
->async_scan
)
1605 scsi_complete_async_scans();
1607 if (((channel
!= SCAN_WILD_CARD
) && (channel
> shost
->max_channel
)) ||
1608 ((id
!= SCAN_WILD_CARD
) && (id
>= shost
->max_id
)) ||
1609 ((lun
!= SCAN_WILD_CARD
) && (lun
> shost
->max_lun
)))
1612 mutex_lock(&shost
->scan_mutex
);
1613 if (scsi_host_scan_allowed(shost
)) {
1614 if (channel
== SCAN_WILD_CARD
)
1615 for (channel
= 0; channel
<= shost
->max_channel
;
1617 scsi_scan_channel(shost
, channel
, id
, lun
,
1620 scsi_scan_channel(shost
, channel
, id
, lun
, rescan
);
1622 mutex_unlock(&shost
->scan_mutex
);
1627 static void scsi_sysfs_add_devices(struct Scsi_Host
*shost
)
1629 struct scsi_device
*sdev
;
1630 shost_for_each_device(sdev
, shost
) {
1631 if (scsi_sysfs_add_sdev(sdev
) != 0)
1632 scsi_destroy_sdev(sdev
);
1637 * scsi_prep_async_scan - prepare for an async scan
1638 * @shost: the host which will be scanned
1639 * Returns: a cookie to be passed to scsi_finish_async_scan()
1641 * Tells the midlayer this host is going to do an asynchronous scan.
1642 * It reserves the host's position in the scanning list and ensures
1643 * that other asynchronous scans started after this one won't affect the
1644 * ordering of the discovered devices.
1646 static struct async_scan_data
*scsi_prep_async_scan(struct Scsi_Host
*shost
)
1648 struct async_scan_data
*data
;
1650 if (strncmp(scsi_scan_type
, "sync", 4) == 0)
1653 if (shost
->async_scan
) {
1654 printk("%s called twice for host %d", __FUNCTION__
,
1660 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
1663 data
->shost
= scsi_host_get(shost
);
1666 init_completion(&data
->prev_finished
);
1668 spin_lock(&async_scan_lock
);
1669 shost
->async_scan
= 1;
1670 if (list_empty(&scanning_hosts
))
1671 complete(&data
->prev_finished
);
1672 list_add_tail(&data
->list
, &scanning_hosts
);
1673 spin_unlock(&async_scan_lock
);
1683 * scsi_finish_async_scan - asynchronous scan has finished
1684 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1686 * All the devices currently attached to this host have been found.
1687 * This function announces all the devices it has found to the rest
1690 static void scsi_finish_async_scan(struct async_scan_data
*data
)
1692 struct Scsi_Host
*shost
;
1697 shost
= data
->shost
;
1698 if (!shost
->async_scan
) {
1699 printk("%s called twice for host %d", __FUNCTION__
,
1705 wait_for_completion(&data
->prev_finished
);
1707 scsi_sysfs_add_devices(shost
);
1709 spin_lock(&async_scan_lock
);
1710 shost
->async_scan
= 0;
1711 list_del(&data
->list
);
1712 if (!list_empty(&scanning_hosts
)) {
1713 struct async_scan_data
*next
= list_entry(scanning_hosts
.next
,
1714 struct async_scan_data
, list
);
1715 complete(&next
->prev_finished
);
1717 spin_unlock(&async_scan_lock
);
1719 scsi_host_put(shost
);
1723 static void do_scsi_scan_host(struct Scsi_Host
*shost
)
1725 if (shost
->hostt
->scan_finished
) {
1726 unsigned long start
= jiffies
;
1727 if (shost
->hostt
->scan_start
)
1728 shost
->hostt
->scan_start(shost
);
1730 while (!shost
->hostt
->scan_finished(shost
, jiffies
- start
))
1733 scsi_scan_host_selected(shost
, SCAN_WILD_CARD
, SCAN_WILD_CARD
,
1738 static int do_scan_async(void *_data
)
1740 struct async_scan_data
*data
= _data
;
1741 do_scsi_scan_host(data
->shost
);
1742 scsi_finish_async_scan(data
);
1747 * scsi_scan_host - scan the given adapter
1748 * @shost: adapter to scan
1750 void scsi_scan_host(struct Scsi_Host
*shost
)
1752 struct async_scan_data
*data
;
1754 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1757 data
= scsi_prep_async_scan(shost
);
1759 do_scsi_scan_host(shost
);
1763 kthread_run(do_scan_async
, data
, "scsi_scan_%d", shost
->host_no
);
1765 EXPORT_SYMBOL(scsi_scan_host
);
1767 void scsi_forget_host(struct Scsi_Host
*shost
)
1769 struct scsi_device
*sdev
;
1770 unsigned long flags
;
1773 spin_lock_irqsave(shost
->host_lock
, flags
);
1774 list_for_each_entry(sdev
, &shost
->__devices
, siblings
) {
1775 if (sdev
->sdev_state
== SDEV_DEL
)
1777 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1778 __scsi_remove_device(sdev
);
1781 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1785 * Function: scsi_get_host_dev()
1787 * Purpose: Create a scsi_device that points to the host adapter itself.
1789 * Arguments: SHpnt - Host that needs a scsi_device
1791 * Lock status: None assumed.
1793 * Returns: The scsi_device or NULL
1796 * Attach a single scsi_device to the Scsi_Host - this should
1797 * be made to look like a "pseudo-device" that points to the
1800 * Note - this device is not accessible from any high-level
1801 * drivers (including generics), which is probably not
1802 * optimal. We can add hooks later to attach
1804 struct scsi_device
*scsi_get_host_dev(struct Scsi_Host
*shost
)
1806 struct scsi_device
*sdev
= NULL
;
1807 struct scsi_target
*starget
;
1809 mutex_lock(&shost
->scan_mutex
);
1810 if (!scsi_host_scan_allowed(shost
))
1812 starget
= scsi_alloc_target(&shost
->shost_gendev
, 0, shost
->this_id
);
1816 sdev
= scsi_alloc_sdev(starget
, 0, NULL
);
1818 sdev
->sdev_gendev
.parent
= get_device(&starget
->dev
);
1821 scsi_target_reap(starget
);
1822 put_device(&starget
->dev
);
1824 mutex_unlock(&shost
->scan_mutex
);
1827 EXPORT_SYMBOL(scsi_get_host_dev
);
1830 * Function: scsi_free_host_dev()
1832 * Purpose: Free a scsi_device that points to the host adapter itself.
1834 * Arguments: SHpnt - Host that needs a scsi_device
1836 * Lock status: None assumed.
1842 void scsi_free_host_dev(struct scsi_device
*sdev
)
1844 BUG_ON(sdev
->id
!= sdev
->host
->this_id
);
1846 scsi_destroy_sdev(sdev
);
1848 EXPORT_SYMBOL(scsi_free_host_dev
);