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 sysfs 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 * When this function returns, any host which started scanning before
137 * this function was called will have finished its scan. Hosts which
138 * started scanning after this function was called may or may not have
141 int scsi_complete_async_scans(void)
143 struct async_scan_data
*data
;
146 if (list_empty(&scanning_hosts
))
148 /* If we can't get memory immediately, that's OK. Just
149 * sleep a little. Even if we never get memory, the async
150 * scans will finish eventually.
152 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
158 init_completion(&data
->prev_finished
);
160 spin_lock(&async_scan_lock
);
161 /* Check that there's still somebody else on the list */
162 if (list_empty(&scanning_hosts
))
164 list_add_tail(&data
->list
, &scanning_hosts
);
165 spin_unlock(&async_scan_lock
);
167 printk(KERN_INFO
"scsi: waiting for bus probes to complete ...\n");
168 wait_for_completion(&data
->prev_finished
);
170 spin_lock(&async_scan_lock
);
171 list_del(&data
->list
);
172 if (!list_empty(&scanning_hosts
)) {
173 struct async_scan_data
*next
= list_entry(scanning_hosts
.next
,
174 struct async_scan_data
, list
);
175 complete(&next
->prev_finished
);
178 spin_unlock(&async_scan_lock
);
184 /* Only exported for the benefit of scsi_wait_scan */
185 EXPORT_SYMBOL_GPL(scsi_complete_async_scans
);
188 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
189 * @sdev: scsi device to send command to
190 * @result: area to store the result of the MODE SENSE
193 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
194 * Called for BLIST_KEY devices.
196 static void scsi_unlock_floptical(struct scsi_device
*sdev
,
197 unsigned char *result
)
199 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
201 printk(KERN_NOTICE
"scsi: unlocking floptical drive\n");
202 scsi_cmd
[0] = MODE_SENSE
;
206 scsi_cmd
[4] = 0x2a; /* size */
208 scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
, result
, 0x2a, NULL
,
213 * scsi_alloc_sdev - allocate and setup a scsi_Device
216 * Allocate, initialize for io, and return a pointer to a scsi_Device.
217 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
218 * adds scsi_Device to the appropriate list.
221 * scsi_Device pointer, or NULL on failure.
223 static struct scsi_device
*scsi_alloc_sdev(struct scsi_target
*starget
,
224 unsigned int lun
, void *hostdata
)
226 struct scsi_device
*sdev
;
227 int display_failure_msg
= 1, ret
;
228 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
230 sdev
= kzalloc(sizeof(*sdev
) + shost
->transportt
->device_size
,
235 sdev
->vendor
= scsi_null_device_strs
;
236 sdev
->model
= scsi_null_device_strs
;
237 sdev
->rev
= scsi_null_device_strs
;
239 sdev
->id
= starget
->id
;
241 sdev
->channel
= starget
->channel
;
242 sdev
->sdev_state
= SDEV_CREATED
;
243 INIT_LIST_HEAD(&sdev
->siblings
);
244 INIT_LIST_HEAD(&sdev
->same_target_siblings
);
245 INIT_LIST_HEAD(&sdev
->cmd_list
);
246 INIT_LIST_HEAD(&sdev
->starved_entry
);
247 spin_lock_init(&sdev
->list_lock
);
249 sdev
->sdev_gendev
.parent
= get_device(&starget
->dev
);
250 sdev
->sdev_target
= starget
;
252 /* usually NULL and set by ->slave_alloc instead */
253 sdev
->hostdata
= hostdata
;
255 /* if the device needs this changing, it may do so in the
256 * slave_configure function */
257 sdev
->max_device_blocked
= SCSI_DEFAULT_DEVICE_BLOCKED
;
260 * Some low level driver could use device->type
265 * Assume that the device will have handshaking problems,
266 * and then fix this field later if it turns out it
271 sdev
->request_queue
= scsi_alloc_queue(sdev
);
272 if (!sdev
->request_queue
) {
273 /* release fn is set up in scsi_sysfs_device_initialise, so
274 * have to free and put manually here */
275 put_device(&starget
->dev
);
280 sdev
->request_queue
->queuedata
= sdev
;
281 scsi_adjust_queue_depth(sdev
, 0, sdev
->host
->cmd_per_lun
);
283 scsi_sysfs_device_initialize(sdev
);
285 if (shost
->hostt
->slave_alloc
) {
286 ret
= shost
->hostt
->slave_alloc(sdev
);
289 * if LLDD reports slave not present, don't clutter
290 * console with alloc failure messages
293 display_failure_msg
= 0;
294 goto out_device_destroy
;
301 transport_destroy_device(&sdev
->sdev_gendev
);
302 put_device(&sdev
->sdev_gendev
);
304 if (display_failure_msg
)
305 printk(ALLOC_FAILURE_MSG
, __FUNCTION__
);
309 static void scsi_target_dev_release(struct device
*dev
)
311 struct device
*parent
= dev
->parent
;
312 struct scsi_target
*starget
= to_scsi_target(dev
);
318 int scsi_is_target_device(const struct device
*dev
)
320 return dev
->release
== scsi_target_dev_release
;
322 EXPORT_SYMBOL(scsi_is_target_device
);
324 static struct scsi_target
*__scsi_find_target(struct device
*parent
,
325 int channel
, uint id
)
327 struct scsi_target
*starget
, *found_starget
= NULL
;
328 struct Scsi_Host
*shost
= dev_to_shost(parent
);
330 * Search for an existing target for this sdev.
332 list_for_each_entry(starget
, &shost
->__targets
, siblings
) {
333 if (starget
->id
== id
&&
334 starget
->channel
== channel
) {
335 found_starget
= starget
;
340 get_device(&found_starget
->dev
);
342 return found_starget
;
346 * scsi_alloc_target - allocate a new or find an existing target
347 * @parent: parent of the target (need not be a scsi host)
348 * @channel: target channel number (zero if no channels)
349 * @id: target id number
351 * Return an existing target if one exists, provided it hasn't already
352 * gone into STARGET_DEL state, otherwise allocate a new target.
354 * The target is returned with an incremented reference, so the caller
355 * is responsible for both reaping and doing a last put
357 static struct scsi_target
*scsi_alloc_target(struct device
*parent
,
358 int channel
, uint id
)
360 struct Scsi_Host
*shost
= dev_to_shost(parent
);
361 struct device
*dev
= NULL
;
363 const int size
= sizeof(struct scsi_target
)
364 + shost
->transportt
->target_size
;
365 struct scsi_target
*starget
;
366 struct scsi_target
*found_target
;
369 starget
= kzalloc(size
, GFP_KERNEL
);
371 printk(KERN_ERR
"%s: allocation failure\n", __FUNCTION__
);
375 device_initialize(dev
);
376 starget
->reap_ref
= 1;
377 dev
->parent
= get_device(parent
);
378 dev
->release
= scsi_target_dev_release
;
379 sprintf(dev
->bus_id
, "target%d:%d:%d",
380 shost
->host_no
, channel
, id
);
382 starget
->channel
= channel
;
383 INIT_LIST_HEAD(&starget
->siblings
);
384 INIT_LIST_HEAD(&starget
->devices
);
385 starget
->state
= STARGET_RUNNING
;
386 starget
->scsi_level
= SCSI_2
;
388 spin_lock_irqsave(shost
->host_lock
, flags
);
390 found_target
= __scsi_find_target(parent
, channel
, id
);
394 list_add_tail(&starget
->siblings
, &shost
->__targets
);
395 spin_unlock_irqrestore(shost
->host_lock
, flags
);
396 /* allocate and add */
397 transport_setup_device(dev
);
398 error
= device_add(dev
);
400 dev_err(dev
, "target device_add failed, error %d\n", error
);
401 spin_lock_irqsave(shost
->host_lock
, flags
);
402 list_del_init(&starget
->siblings
);
403 spin_unlock_irqrestore(shost
->host_lock
, flags
);
404 transport_destroy_device(dev
);
409 transport_add_device(dev
);
410 if (shost
->hostt
->target_alloc
) {
411 error
= shost
->hostt
->target_alloc(starget
);
414 dev_printk(KERN_ERR
, dev
, "target allocation failed, error %d\n", error
);
415 /* don't want scsi_target_reap to do the final
416 * put because it will be under the host lock */
418 scsi_target_reap(starget
);
428 found_target
->reap_ref
++;
429 spin_unlock_irqrestore(shost
->host_lock
, flags
);
430 if (found_target
->state
!= STARGET_DEL
) {
435 /* Unfortunately, we found a dying target; need to
436 * wait until it's dead before we can get a new one */
437 put_device(&found_target
->dev
);
438 flush_scheduled_work();
442 static void scsi_target_reap_usercontext(struct work_struct
*work
)
444 struct scsi_target
*starget
=
445 container_of(work
, struct scsi_target
, ew
.work
);
446 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
449 transport_remove_device(&starget
->dev
);
450 device_del(&starget
->dev
);
451 transport_destroy_device(&starget
->dev
);
452 spin_lock_irqsave(shost
->host_lock
, flags
);
453 if (shost
->hostt
->target_destroy
)
454 shost
->hostt
->target_destroy(starget
);
455 list_del_init(&starget
->siblings
);
456 spin_unlock_irqrestore(shost
->host_lock
, flags
);
457 put_device(&starget
->dev
);
461 * scsi_target_reap - check to see if target is in use and destroy if not
463 * @starget: target to be checked
465 * This is used after removing a LUN or doing a last put of the target
466 * it checks atomically that nothing is using the target and removes
469 void scsi_target_reap(struct scsi_target
*starget
)
471 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
474 spin_lock_irqsave(shost
->host_lock
, flags
);
476 if (--starget
->reap_ref
== 0 && list_empty(&starget
->devices
)) {
477 BUG_ON(starget
->state
== STARGET_DEL
);
478 starget
->state
= STARGET_DEL
;
479 spin_unlock_irqrestore(shost
->host_lock
, flags
);
480 execute_in_process_context(scsi_target_reap_usercontext
,
485 spin_unlock_irqrestore(shost
->host_lock
, flags
);
491 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
492 * @s: INQUIRY result string to sanitize
493 * @len: length of the string
496 * The SCSI spec says that INQUIRY vendor, product, and revision
497 * strings must consist entirely of graphic ASCII characters,
498 * padded on the right with spaces. Since not all devices obey
499 * this rule, we will replace non-graphic or non-ASCII characters
500 * with spaces. Exception: a NUL character is interpreted as a
501 * string terminator, so all the following characters are set to
504 static void sanitize_inquiry_string(unsigned char *s
, int len
)
508 for (; len
> 0; (--len
, ++s
)) {
511 if (terminated
|| *s
< 0x20 || *s
> 0x7e)
517 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
518 * @sdev: scsi_device to probe
519 * @inq_result: area to store the INQUIRY result
520 * @result_len: len of inq_result
521 * @bflags: store any bflags found here
524 * Probe the lun associated with @req using a standard SCSI INQUIRY;
526 * If the INQUIRY is successful, zero is returned and the
527 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
528 * are copied to the scsi_device any flags value is stored in *@bflags.
530 static int scsi_probe_lun(struct scsi_device
*sdev
, unsigned char *inq_result
,
531 int result_len
, int *bflags
)
533 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
534 int first_inquiry_len
, try_inquiry_len
, next_inquiry_len
;
535 int response_len
= 0;
536 int pass
, count
, result
;
537 struct scsi_sense_hdr sshdr
;
541 /* Perform up to 3 passes. The first pass uses a conservative
542 * transfer length of 36 unless sdev->inquiry_len specifies a
543 * different value. */
544 first_inquiry_len
= sdev
->inquiry_len
? sdev
->inquiry_len
: 36;
545 try_inquiry_len
= first_inquiry_len
;
549 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO
, sdev
,
550 "scsi scan: INQUIRY pass %d length %d\n",
551 pass
, try_inquiry_len
));
553 /* Each pass gets up to three chances to ignore Unit Attention */
554 for (count
= 0; count
< 3; ++count
) {
555 memset(scsi_cmd
, 0, 6);
556 scsi_cmd
[0] = INQUIRY
;
557 scsi_cmd
[4] = (unsigned char) try_inquiry_len
;
559 memset(inq_result
, 0, try_inquiry_len
);
561 result
= scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
,
562 inq_result
, try_inquiry_len
, &sshdr
,
563 HZ
/ 2 + HZ
* scsi_inq_timeout
, 3);
565 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
"scsi scan: INQUIRY %s "
567 result
? "failed" : "successful", result
));
571 * not-ready to ready transition [asc/ascq=0x28/0x0]
572 * or power-on, reset [asc/ascq=0x29/0x0], continue.
573 * INQUIRY should not yield UNIT_ATTENTION
574 * but many buggy devices do so anyway.
576 if ((driver_byte(result
) & DRIVER_SENSE
) &&
577 scsi_sense_valid(&sshdr
)) {
578 if ((sshdr
.sense_key
== UNIT_ATTENTION
) &&
579 ((sshdr
.asc
== 0x28) ||
580 (sshdr
.asc
== 0x29)) &&
589 sanitize_inquiry_string(&inq_result
[8], 8);
590 sanitize_inquiry_string(&inq_result
[16], 16);
591 sanitize_inquiry_string(&inq_result
[32], 4);
593 response_len
= inq_result
[4] + 5;
594 if (response_len
> 255)
595 response_len
= first_inquiry_len
; /* sanity */
598 * Get any flags for this device.
600 * XXX add a bflags to scsi_device, and replace the
601 * corresponding bit fields in scsi_device, so bflags
602 * need not be passed as an argument.
604 *bflags
= scsi_get_device_flags(sdev
, &inq_result
[8],
607 /* When the first pass succeeds we gain information about
608 * what larger transfer lengths might work. */
610 if (BLIST_INQUIRY_36
& *bflags
)
611 next_inquiry_len
= 36;
612 else if (BLIST_INQUIRY_58
& *bflags
)
613 next_inquiry_len
= 58;
614 else if (sdev
->inquiry_len
)
615 next_inquiry_len
= sdev
->inquiry_len
;
617 next_inquiry_len
= response_len
;
619 /* If more data is available perform the second pass */
620 if (next_inquiry_len
> try_inquiry_len
) {
621 try_inquiry_len
= next_inquiry_len
;
627 } else if (pass
== 2) {
628 printk(KERN_INFO
"scsi scan: %d byte inquiry failed. "
629 "Consider BLIST_INQUIRY_36 for this device\n",
632 /* If this pass failed, the third pass goes back and transfers
633 * the same amount as we successfully got in the first pass. */
634 try_inquiry_len
= first_inquiry_len
;
639 /* If the last transfer attempt got an error, assume the
640 * peripheral doesn't exist or is dead. */
644 /* Don't report any more data than the device says is valid */
645 sdev
->inquiry_len
= min(try_inquiry_len
, response_len
);
648 * XXX Abort if the response length is less than 36? If less than
649 * 32, the lookup of the device flags (above) could be invalid,
650 * and it would be possible to take an incorrect action - we do
651 * not want to hang because of a short INQUIRY. On the flip side,
652 * if the device is spun down or becoming ready (and so it gives a
653 * short INQUIRY), an abort here prevents any further use of the
654 * device, including spin up.
656 * On the whole, the best approach seems to be to assume the first
657 * 36 bytes are valid no matter what the device says. That's
658 * better than copying < 36 bytes to the inquiry-result buffer
659 * and displaying garbage for the Vendor, Product, or Revision
662 if (sdev
->inquiry_len
< 36) {
663 printk(KERN_INFO
"scsi scan: INQUIRY result too short (%d),"
664 " using 36\n", sdev
->inquiry_len
);
665 sdev
->inquiry_len
= 36;
669 * Related to the above issue:
671 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
672 * and if not ready, sent a START_STOP to start (maybe spin up) and
673 * then send the INQUIRY again, since the INQUIRY can change after
674 * a device is initialized.
676 * Ideally, start a device if explicitly asked to do so. This
677 * assumes that a device is spun up on power on, spun down on
678 * request, and then spun up on request.
682 * The scanning code needs to know the scsi_level, even if no
683 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
684 * non-zero LUNs can be scanned.
686 sdev
->scsi_level
= inq_result
[2] & 0x07;
687 if (sdev
->scsi_level
>= 2 ||
688 (sdev
->scsi_level
== 1 && (inq_result
[3] & 0x0f) == 1))
690 sdev
->sdev_target
->scsi_level
= sdev
->scsi_level
;
696 * scsi_add_lun - allocate and fully initialze a scsi_device
697 * @sdevscan: holds information to be stored in the new scsi_device
698 * @sdevnew: store the address of the newly allocated scsi_device
699 * @inq_result: holds the result of a previous INQUIRY to the LUN
700 * @bflags: black/white list flag
703 * Allocate and initialize a scsi_device matching sdevscan. Optionally
704 * set fields based on values in *@bflags. If @sdevnew is not
705 * NULL, store the address of the new scsi_device in *@sdevnew (needed
706 * when scanning a particular LUN).
709 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
710 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
712 static int scsi_add_lun(struct scsi_device
*sdev
, unsigned char *inq_result
,
713 int *bflags
, int async
)
716 * XXX do not save the inquiry, since it can change underneath us,
717 * save just vendor/model/rev.
719 * Rather than save it and have an ioctl that retrieves the saved
720 * value, have an ioctl that executes the same INQUIRY code used
721 * in scsi_probe_lun, let user level programs doing INQUIRY
722 * scanning run at their own risk, or supply a user level program
723 * that can correctly scan.
727 * Copy at least 36 bytes of INQUIRY data, so that we don't
728 * dereference unallocated memory when accessing the Vendor,
729 * Product, and Revision strings. Badly behaved devices may set
730 * the INQUIRY Additional Length byte to a small value, indicating
731 * these strings are invalid, but often they contain plausible data
732 * nonetheless. It doesn't matter if the device sent < 36 bytes
733 * total, since scsi_probe_lun() initializes inq_result with 0s.
735 sdev
->inquiry
= kmemdup(inq_result
,
736 max_t(size_t, sdev
->inquiry_len
, 36),
738 if (sdev
->inquiry
== NULL
)
739 return SCSI_SCAN_NO_RESPONSE
;
741 sdev
->vendor
= (char *) (sdev
->inquiry
+ 8);
742 sdev
->model
= (char *) (sdev
->inquiry
+ 16);
743 sdev
->rev
= (char *) (sdev
->inquiry
+ 32);
745 if (*bflags
& BLIST_ISROM
) {
747 * It would be better to modify sdev->type, and set
748 * sdev->removable; this can now be done since
749 * print_inquiry has gone away.
751 inq_result
[0] = TYPE_ROM
;
752 inq_result
[1] |= 0x80; /* removable */
753 } else if (*bflags
& BLIST_NO_ULD_ATTACH
)
754 sdev
->no_uld_attach
= 1;
756 switch (sdev
->type
= (inq_result
[0] & 0x1f)) {
758 /* RBC devices can return SCSI-3 compliance and yet
759 * still not support REPORT LUNS, so make them act as
760 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
761 * specifically set */
762 if ((*bflags
& BLIST_REPORTLUN2
) == 0)
763 *bflags
|= BLIST_NOREPORTLUN
;
771 case TYPE_MEDIUM_CHANGER
:
778 /* MMC devices can return SCSI-3 compliance and yet
779 * still not support REPORT LUNS, so make them act as
780 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
781 * specifically set */
782 if ((*bflags
& BLIST_REPORTLUN2
) == 0)
783 *bflags
|= BLIST_NOREPORTLUN
;
789 printk(KERN_INFO
"scsi: unknown device type %d\n", sdev
->type
);
793 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
794 * spec says: The device server is capable of supporting the
795 * specified peripheral device type on this logical unit. However,
796 * the physical device is not currently connected to this logical
799 * The above is vague, as it implies that we could treat 001 and
800 * 011 the same. Stay compatible with previous code, and create a
801 * scsi_device for a PQ of 1
803 * Don't set the device offline here; rather let the upper
804 * level drivers eval the PQ to decide whether they should
805 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
808 sdev
->inq_periph_qual
= (inq_result
[0] >> 5) & 7;
809 sdev
->removable
= (0x80 & inq_result
[1]) >> 7;
810 sdev
->lockable
= sdev
->removable
;
811 sdev
->soft_reset
= (inq_result
[7] & 1) && ((inq_result
[3] & 7) == 2);
813 if (sdev
->scsi_level
>= SCSI_3
|| (sdev
->inquiry_len
> 56 &&
814 inq_result
[56] & 0x04))
816 if (inq_result
[7] & 0x60)
818 if (inq_result
[7] & 0x10)
821 sdev_printk(KERN_NOTICE
, sdev
, "%s %.8s %.16s %.4s PQ: %d "
822 "ANSI: %d%s\n", scsi_device_type(sdev
->type
),
823 sdev
->vendor
, sdev
->model
, sdev
->rev
,
824 sdev
->inq_periph_qual
, inq_result
[2] & 0x07,
825 (inq_result
[3] & 0x0f) == 1 ? " CCS" : "");
831 if ((sdev
->scsi_level
>= SCSI_2
) && (inq_result
[7] & 2) &&
832 !(*bflags
& BLIST_NOTQ
))
833 sdev
->tagged_supported
= 1;
835 * Some devices (Texel CD ROM drives) have handshaking problems
836 * when used with the Seagate controllers. borken is initialized
837 * to 1, and then set it to 0 here.
839 if ((*bflags
& BLIST_BORKEN
) == 0)
843 * Apparently some really broken devices (contrary to the SCSI
844 * standards) need to be selected without asserting ATN
846 if (*bflags
& BLIST_SELECT_NO_ATN
)
847 sdev
->select_no_atn
= 1;
850 * Maximum 512 sector transfer length
851 * broken RA4x00 Compaq Disk Array
853 if (*bflags
& BLIST_MAX_512
)
854 blk_queue_max_sectors(sdev
->request_queue
, 512);
857 * Some devices may not want to have a start command automatically
858 * issued when a device is added.
860 if (*bflags
& BLIST_NOSTARTONADD
)
861 sdev
->no_start_on_add
= 1;
863 if (*bflags
& BLIST_SINGLELUN
)
864 sdev
->single_lun
= 1;
867 sdev
->use_10_for_rw
= 1;
869 if (*bflags
& BLIST_MS_SKIP_PAGE_08
)
870 sdev
->skip_ms_page_8
= 1;
872 if (*bflags
& BLIST_MS_SKIP_PAGE_3F
)
873 sdev
->skip_ms_page_3f
= 1;
875 if (*bflags
& BLIST_USE_10_BYTE_MS
)
876 sdev
->use_10_for_ms
= 1;
878 /* set the device running here so that slave configure
880 scsi_device_set_state(sdev
, SDEV_RUNNING
);
882 if (*bflags
& BLIST_MS_192_BYTES_FOR_3F
)
883 sdev
->use_192_bytes_for_3f
= 1;
885 if (*bflags
& BLIST_NOT_LOCKABLE
)
888 if (*bflags
& BLIST_RETRY_HWERROR
)
889 sdev
->retry_hwerror
= 1;
891 transport_configure_device(&sdev
->sdev_gendev
);
893 if (sdev
->host
->hostt
->slave_configure
) {
894 int ret
= sdev
->host
->hostt
->slave_configure(sdev
);
897 * if LLDD reports slave not present, don't clutter
898 * console with alloc failure messages
901 sdev_printk(KERN_ERR
, sdev
,
902 "failed to configure device\n");
904 return SCSI_SCAN_NO_RESPONSE
;
909 * Ok, the device is now all set up, we can
910 * register it and tell the rest of the kernel
913 if (!async
&& scsi_sysfs_add_sdev(sdev
) != 0)
914 return SCSI_SCAN_NO_RESPONSE
;
916 return SCSI_SCAN_LUN_PRESENT
;
919 static inline void scsi_destroy_sdev(struct scsi_device
*sdev
)
921 scsi_device_set_state(sdev
, SDEV_DEL
);
922 if (sdev
->host
->hostt
->slave_destroy
)
923 sdev
->host
->hostt
->slave_destroy(sdev
);
924 transport_destroy_device(&sdev
->sdev_gendev
);
925 put_device(&sdev
->sdev_gendev
);
928 #ifdef CONFIG_SCSI_LOGGING
930 * scsi_inq_str - print INQUIRY data from min to max index,
931 * strip trailing whitespace
932 * @buf: Output buffer with at least end-first+1 bytes of space
933 * @inq: Inquiry buffer (input)
934 * @first: Offset of string into inq
935 * @end: Index after last character in inq
937 static unsigned char *scsi_inq_str(unsigned char *buf
, unsigned char *inq
,
938 unsigned first
, unsigned end
)
940 unsigned term
= 0, idx
;
942 for (idx
= 0; idx
+ first
< end
&& idx
+ first
< inq
[4] + 5; idx
++) {
943 if (inq
[idx
+first
] > ' ') {
944 buf
[idx
] = inq
[idx
+first
];
956 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
957 * @starget: pointer to target device structure
958 * @lun: LUN of target device
959 * @sdevscan: probe the LUN corresponding to this scsi_device
960 * @sdevnew: store the value of any new scsi_device allocated
961 * @bflagsp: store bflags here if not NULL
964 * Call scsi_probe_lun, if a LUN with an attached device is found,
965 * allocate and set it up by calling scsi_add_lun.
968 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
969 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
970 * attached at the LUN
971 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
973 static int scsi_probe_and_add_lun(struct scsi_target
*starget
,
974 uint lun
, int *bflagsp
,
975 struct scsi_device
**sdevp
, int rescan
,
978 struct scsi_device
*sdev
;
979 unsigned char *result
;
980 int bflags
, res
= SCSI_SCAN_NO_RESPONSE
, result_len
= 256;
981 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
984 * The rescan flag is used as an optimization, the first scan of a
985 * host adapter calls into here with rescan == 0.
987 sdev
= scsi_device_lookup_by_target(starget
, lun
);
989 if (rescan
|| sdev
->sdev_state
!= SDEV_CREATED
) {
990 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
991 "scsi scan: device exists on %s\n",
992 sdev
->sdev_gendev
.bus_id
));
996 scsi_device_put(sdev
);
999 *bflagsp
= scsi_get_device_flags(sdev
,
1002 return SCSI_SCAN_LUN_PRESENT
;
1004 scsi_device_put(sdev
);
1006 sdev
= scsi_alloc_sdev(starget
, lun
, hostdata
);
1010 result
= kmalloc(result_len
, GFP_ATOMIC
|
1011 ((shost
->unchecked_isa_dma
) ? __GFP_DMA
: 0));
1015 if (scsi_probe_lun(sdev
, result
, result_len
, &bflags
))
1016 goto out_free_result
;
1021 * result contains valid SCSI INQUIRY data.
1023 if (((result
[0] >> 5) == 3) && !(bflags
& BLIST_ATTACH_PQ3
)) {
1025 * For a Peripheral qualifier 3 (011b), the SCSI
1026 * spec says: The device server is not capable of
1027 * supporting a physical device on this logical
1030 * For disks, this implies that there is no
1031 * logical disk configured at sdev->lun, but there
1032 * is a target id responding.
1034 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO
, sdev
, "scsi scan:"
1035 " peripheral qualifier of 3, device not"
1038 SCSI_LOG_SCAN_BUS(1, {
1039 unsigned char vend
[9];
1040 unsigned char mod
[17];
1042 sdev_printk(KERN_INFO
, sdev
,
1043 "scsi scan: consider passing scsi_mod."
1044 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1045 scsi_inq_str(vend
, result
, 8, 16),
1046 scsi_inq_str(mod
, result
, 16, 32));
1050 res
= SCSI_SCAN_TARGET_PRESENT
;
1051 goto out_free_result
;
1055 * Some targets may set slight variations of PQ and PDT to signal
1056 * that no LUN is present, so don't add sdev in these cases.
1057 * Two specific examples are:
1058 * 1) NetApp targets: return PQ=1, PDT=0x1f
1059 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1060 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1063 * 1) SCSI SPC-3, pp. 145-146
1064 * PQ=1: "A peripheral device having the specified peripheral
1065 * device type is not connected to this logical unit. However, the
1066 * device server is capable of supporting the specified peripheral
1067 * device type on this logical unit."
1068 * PDT=0x1f: "Unknown or no device type"
1069 * 2) USB UFI 1.0, p. 20
1070 * PDT=00h Direct-access device (floppy)
1071 * PDT=1Fh none (no FDD connected to the requested logical unit)
1073 if (((result
[0] >> 5) == 1 || starget
->pdt_1f_for_no_lun
) &&
1074 (result
[0] & 0x1f) == 0x1f) {
1075 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1076 "scsi scan: peripheral device type"
1077 " of 31, no device added\n"));
1078 res
= SCSI_SCAN_TARGET_PRESENT
;
1079 goto out_free_result
;
1082 res
= scsi_add_lun(sdev
, result
, &bflags
, shost
->async_scan
);
1083 if (res
== SCSI_SCAN_LUN_PRESENT
) {
1084 if (bflags
& BLIST_KEY
) {
1086 scsi_unlock_floptical(sdev
, result
);
1093 if (res
== SCSI_SCAN_LUN_PRESENT
) {
1095 if (scsi_device_get(sdev
) == 0) {
1098 __scsi_remove_device(sdev
);
1099 res
= SCSI_SCAN_NO_RESPONSE
;
1103 scsi_destroy_sdev(sdev
);
1109 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1110 * @starget: pointer to target structure to scan
1111 * @bflags: black/white list flag for LUN 0
1114 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1115 * scanned) to some maximum lun until a LUN is found with no device
1116 * attached. Use the bflags to figure out any oddities.
1118 * Modifies sdevscan->lun.
1120 static void scsi_sequential_lun_scan(struct scsi_target
*starget
,
1121 int bflags
, int scsi_level
, int rescan
)
1123 unsigned int sparse_lun
, lun
, max_dev_lun
;
1124 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
1126 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
"scsi scan: Sequential scan of"
1127 "%s\n", starget
->dev
.bus_id
));
1129 max_dev_lun
= min(max_scsi_luns
, shost
->max_lun
);
1131 * If this device is known to support sparse multiple units,
1132 * override the other settings, and scan all of them. Normally,
1133 * SCSI-3 devices should be scanned via the REPORT LUNS.
1135 if (bflags
& BLIST_SPARSELUN
) {
1136 max_dev_lun
= shost
->max_lun
;
1142 * If less than SCSI_1_CSS, and no special lun scaning, stop
1143 * scanning; this matches 2.4 behaviour, but could just be a bug
1144 * (to continue scanning a SCSI_1_CSS device).
1146 * This test is broken. We might not have any device on lun0 for
1147 * a sparselun device, and if that's the case then how would we
1148 * know the real scsi_level, eh? It might make sense to just not
1149 * scan any SCSI_1 device for non-0 luns, but that check would best
1150 * go into scsi_alloc_sdev() and just have it return null when asked
1151 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1153 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1154 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1159 * If this device is known to support multiple units, override
1160 * the other settings, and scan all of them.
1162 if (bflags
& BLIST_FORCELUN
)
1163 max_dev_lun
= shost
->max_lun
;
1165 * REGAL CDC-4X: avoid hang after LUN 4
1167 if (bflags
& BLIST_MAX5LUN
)
1168 max_dev_lun
= min(5U, max_dev_lun
);
1170 * Do not scan SCSI-2 or lower device past LUN 7, unless
1173 if (scsi_level
< SCSI_3
&& !(bflags
& BLIST_LARGELUN
))
1174 max_dev_lun
= min(8U, max_dev_lun
);
1177 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1178 * until we reach the max, or no LUN is found and we are not
1181 for (lun
= 1; lun
< max_dev_lun
; ++lun
)
1182 if ((scsi_probe_and_add_lun(starget
, lun
, NULL
, NULL
, rescan
,
1183 NULL
) != SCSI_SCAN_LUN_PRESENT
) &&
1189 * scsilun_to_int: convert a scsi_lun to an int
1190 * @scsilun: struct scsi_lun to be converted.
1193 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1194 * integer, and return the result. The caller must check for
1195 * truncation before using this function.
1198 * The struct scsi_lun is assumed to be four levels, with each level
1199 * effectively containing a SCSI byte-ordered (big endian) short; the
1200 * addressing bits of each level are ignored (the highest two bits).
1201 * For a description of the LUN format, post SCSI-3 see the SCSI
1202 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1204 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1205 * the integer: 0x0b030a04
1207 static int scsilun_to_int(struct scsi_lun
*scsilun
)
1213 for (i
= 0; i
< sizeof(lun
); i
+= 2)
1214 lun
= lun
| (((scsilun
->scsi_lun
[i
] << 8) |
1215 scsilun
->scsi_lun
[i
+ 1]) << (i
* 8));
1220 * int_to_scsilun: reverts an int into a scsi_lun
1221 * @int: integer to be reverted
1222 * @scsilun: struct scsi_lun to be set.
1225 * Reverts the functionality of the scsilun_to_int, which packed
1226 * an 8-byte lun value into an int. This routine unpacks the int
1227 * back into the lun value.
1228 * Note: the scsilun_to_int() routine does not truly handle all
1229 * 8bytes of the lun value. This functions restores only as much
1230 * as was set by the routine.
1233 * Given an integer : 0x0b030a04, this function returns a
1234 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1237 void int_to_scsilun(unsigned int lun
, struct scsi_lun
*scsilun
)
1241 memset(scsilun
->scsi_lun
, 0, sizeof(scsilun
->scsi_lun
));
1243 for (i
= 0; i
< sizeof(lun
); i
+= 2) {
1244 scsilun
->scsi_lun
[i
] = (lun
>> 8) & 0xFF;
1245 scsilun
->scsi_lun
[i
+1] = lun
& 0xFF;
1249 EXPORT_SYMBOL(int_to_scsilun
);
1252 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1253 * @sdevscan: scan the host, channel, and id of this scsi_device
1256 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1257 * command, and scan the resulting list of LUNs by calling
1258 * scsi_probe_and_add_lun.
1260 * Modifies sdevscan->lun.
1263 * 0: scan completed (or no memory, so further scanning is futile)
1264 * 1: no report lun scan, or not configured
1266 static int scsi_report_lun_scan(struct scsi_target
*starget
, int bflags
,
1270 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
1271 unsigned int length
;
1273 unsigned int num_luns
;
1274 unsigned int retries
;
1276 struct scsi_lun
*lunp
, *lun_data
;
1278 struct scsi_sense_hdr sshdr
;
1279 struct scsi_device
*sdev
;
1280 struct Scsi_Host
*shost
= dev_to_shost(&starget
->dev
);
1284 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1285 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1286 * support more than 8 LUNs.
1288 if (bflags
& BLIST_NOREPORTLUN
)
1290 if (starget
->scsi_level
< SCSI_2
&&
1291 starget
->scsi_level
!= SCSI_UNKNOWN
)
1293 if (starget
->scsi_level
< SCSI_3
&&
1294 (!(bflags
& BLIST_REPORTLUN2
) || shost
->max_lun
<= 8))
1296 if (bflags
& BLIST_NOLUN
)
1299 if (!(sdev
= scsi_device_lookup_by_target(starget
, 0))) {
1300 sdev
= scsi_alloc_sdev(starget
, 0, NULL
);
1303 if (scsi_device_get(sdev
))
1307 sprintf(devname
, "host %d channel %d id %d",
1308 shost
->host_no
, sdev
->channel
, sdev
->id
);
1311 * Allocate enough to hold the header (the same size as one scsi_lun)
1312 * plus the max number of luns we are requesting.
1314 * Reallocating and trying again (with the exact amount we need)
1315 * would be nice, but then we need to somehow limit the size
1316 * allocated based on the available memory and the limits of
1317 * kmalloc - we don't want a kmalloc() failure of a huge value to
1318 * prevent us from finding any LUNs on this target.
1320 length
= (max_scsi_report_luns
+ 1) * sizeof(struct scsi_lun
);
1321 lun_data
= kmalloc(length
, GFP_ATOMIC
|
1322 (sdev
->host
->unchecked_isa_dma
? __GFP_DMA
: 0));
1324 printk(ALLOC_FAILURE_MSG
, __FUNCTION__
);
1328 scsi_cmd
[0] = REPORT_LUNS
;
1331 * bytes 1 - 5: reserved, set to zero.
1333 memset(&scsi_cmd
[1], 0, 5);
1336 * bytes 6 - 9: length of the command.
1338 scsi_cmd
[6] = (unsigned char) (length
>> 24) & 0xff;
1339 scsi_cmd
[7] = (unsigned char) (length
>> 16) & 0xff;
1340 scsi_cmd
[8] = (unsigned char) (length
>> 8) & 0xff;
1341 scsi_cmd
[9] = (unsigned char) length
& 0xff;
1343 scsi_cmd
[10] = 0; /* reserved */
1344 scsi_cmd
[11] = 0; /* control */
1347 * We can get a UNIT ATTENTION, for example a power on/reset, so
1348 * retry a few times (like sd.c does for TEST UNIT READY).
1349 * Experience shows some combinations of adapter/devices get at
1350 * least two power on/resets.
1352 * Illegal requests (for devices that do not support REPORT LUNS)
1353 * should come through as a check condition, and will not generate
1356 for (retries
= 0; retries
< 3; retries
++) {
1357 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO
"scsi scan: Sending"
1358 " REPORT LUNS to %s (try %d)\n", devname
,
1361 result
= scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
,
1362 lun_data
, length
, &sshdr
,
1363 SCSI_TIMEOUT
+ 4 * HZ
, 3);
1365 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO
"scsi scan: REPORT LUNS"
1366 " %s (try %d) result 0x%x\n", result
1367 ? "failed" : "successful", retries
, result
));
1370 else if (scsi_sense_valid(&sshdr
)) {
1371 if (sshdr
.sense_key
!= UNIT_ATTENTION
)
1378 * The device probably does not support a REPORT LUN command
1385 * Get the length from the first four bytes of lun_data.
1387 data
= (u8
*) lun_data
->scsi_lun
;
1388 length
= ((data
[0] << 24) | (data
[1] << 16) |
1389 (data
[2] << 8) | (data
[3] << 0));
1391 num_luns
= (length
/ sizeof(struct scsi_lun
));
1392 if (num_luns
> max_scsi_report_luns
) {
1393 printk(KERN_WARNING
"scsi: On %s only %d (max_scsi_report_luns)"
1394 " of %d luns reported, try increasing"
1395 " max_scsi_report_luns.\n", devname
,
1396 max_scsi_report_luns
, num_luns
);
1397 num_luns
= max_scsi_report_luns
;
1400 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO
, sdev
,
1401 "scsi scan: REPORT LUN scan\n"));
1404 * Scan the luns in lun_data. The entry at offset 0 is really
1405 * the header, so start at 1 and go up to and including num_luns.
1407 for (lunp
= &lun_data
[1]; lunp
<= &lun_data
[num_luns
]; lunp
++) {
1408 lun
= scsilun_to_int(lunp
);
1411 * Check if the unused part of lunp is non-zero, and so
1412 * does not fit in lun.
1414 if (memcmp(&lunp
->scsi_lun
[sizeof(lun
)], "\0\0\0\0", 4)) {
1418 * Output an error displaying the LUN in byte order,
1419 * this differs from what linux would print for the
1420 * integer LUN value.
1422 printk(KERN_WARNING
"scsi: %s lun 0x", devname
);
1423 data
= (char *)lunp
->scsi_lun
;
1424 for (i
= 0; i
< sizeof(struct scsi_lun
); i
++)
1425 printk("%02x", data
[i
]);
1426 printk(" has a LUN larger than currently supported.\n");
1427 } else if (lun
> sdev
->host
->max_lun
) {
1428 printk(KERN_WARNING
"scsi: %s lun%d has a LUN larger"
1429 " than allowed by the host adapter\n",
1434 res
= scsi_probe_and_add_lun(starget
,
1435 lun
, NULL
, NULL
, rescan
, NULL
);
1436 if (res
== SCSI_SCAN_NO_RESPONSE
) {
1438 * Got some results, but now none, abort.
1440 sdev_printk(KERN_ERR
, sdev
,
1441 "Unexpected response"
1442 " from lun %d while scanning, scan"
1452 scsi_device_put(sdev
);
1453 if (sdev
->sdev_state
== SDEV_CREATED
)
1455 * the sdev we used didn't appear in the report luns scan
1457 scsi_destroy_sdev(sdev
);
1461 struct scsi_device
*__scsi_add_device(struct Scsi_Host
*shost
, uint channel
,
1462 uint id
, uint lun
, void *hostdata
)
1464 struct scsi_device
*sdev
= ERR_PTR(-ENODEV
);
1465 struct device
*parent
= &shost
->shost_gendev
;
1466 struct scsi_target
*starget
;
1468 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1469 return ERR_PTR(-ENODEV
);
1471 if (!shost
->async_scan
)
1472 scsi_complete_async_scans();
1474 starget
= scsi_alloc_target(parent
, channel
, id
);
1476 return ERR_PTR(-ENOMEM
);
1478 mutex_lock(&shost
->scan_mutex
);
1479 if (scsi_host_scan_allowed(shost
))
1480 scsi_probe_and_add_lun(starget
, lun
, NULL
, &sdev
, 1, hostdata
);
1481 mutex_unlock(&shost
->scan_mutex
);
1482 scsi_target_reap(starget
);
1483 put_device(&starget
->dev
);
1487 EXPORT_SYMBOL(__scsi_add_device
);
1489 int scsi_add_device(struct Scsi_Host
*host
, uint channel
,
1490 uint target
, uint lun
)
1492 struct scsi_device
*sdev
=
1493 __scsi_add_device(host
, channel
, target
, lun
, NULL
);
1495 return PTR_ERR(sdev
);
1497 scsi_device_put(sdev
);
1500 EXPORT_SYMBOL(scsi_add_device
);
1502 void scsi_rescan_device(struct device
*dev
)
1504 struct scsi_driver
*drv
;
1509 drv
= to_scsi_driver(dev
->driver
);
1510 if (try_module_get(drv
->owner
)) {
1513 module_put(drv
->owner
);
1516 EXPORT_SYMBOL(scsi_rescan_device
);
1518 static void __scsi_scan_target(struct device
*parent
, unsigned int channel
,
1519 unsigned int id
, unsigned int lun
, int rescan
)
1521 struct Scsi_Host
*shost
= dev_to_shost(parent
);
1524 struct scsi_target
*starget
;
1526 if (shost
->this_id
== id
)
1528 * Don't scan the host adapter
1532 starget
= scsi_alloc_target(parent
, channel
, id
);
1536 if (lun
!= SCAN_WILD_CARD
) {
1538 * Scan for a specific host/chan/id/lun.
1540 scsi_probe_and_add_lun(starget
, lun
, NULL
, NULL
, rescan
, NULL
);
1545 * Scan LUN 0, if there is some response, scan further. Ideally, we
1546 * would not configure LUN 0 until all LUNs are scanned.
1548 res
= scsi_probe_and_add_lun(starget
, 0, &bflags
, NULL
, rescan
, NULL
);
1549 if (res
== SCSI_SCAN_LUN_PRESENT
|| res
== SCSI_SCAN_TARGET_PRESENT
) {
1550 if (scsi_report_lun_scan(starget
, bflags
, rescan
) != 0)
1552 * The REPORT LUN did not scan the target,
1553 * do a sequential scan.
1555 scsi_sequential_lun_scan(starget
, bflags
,
1556 starget
->scsi_level
, rescan
);
1560 /* now determine if the target has any children at all
1561 * and if not, nuke it */
1562 scsi_target_reap(starget
);
1564 put_device(&starget
->dev
);
1568 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1570 * @parent: host to scan
1571 * @channel: channel to scan
1572 * @id: target id to scan
1573 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1574 * @rescan: passed to LUN scanning routines
1577 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1578 * and possibly all LUNs on the target id.
1580 * First try a REPORT LUN scan, if that does not scan the target, do a
1581 * sequential scan of LUNs on the target id.
1583 void scsi_scan_target(struct device
*parent
, unsigned int channel
,
1584 unsigned int id
, unsigned int lun
, int rescan
)
1586 struct Scsi_Host
*shost
= dev_to_shost(parent
);
1588 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1591 if (!shost
->async_scan
)
1592 scsi_complete_async_scans();
1594 mutex_lock(&shost
->scan_mutex
);
1595 if (scsi_host_scan_allowed(shost
))
1596 __scsi_scan_target(parent
, channel
, id
, lun
, rescan
);
1597 mutex_unlock(&shost
->scan_mutex
);
1599 EXPORT_SYMBOL(scsi_scan_target
);
1601 static void scsi_scan_channel(struct Scsi_Host
*shost
, unsigned int channel
,
1602 unsigned int id
, unsigned int lun
, int rescan
)
1606 if (id
== SCAN_WILD_CARD
)
1607 for (id
= 0; id
< shost
->max_id
; ++id
) {
1609 * XXX adapter drivers when possible (FCP, iSCSI)
1610 * could modify max_id to match the current max,
1611 * not the absolute max.
1613 * XXX add a shost id iterator, so for example,
1614 * the FC ID can be the same as a target id
1615 * without a huge overhead of sparse id's.
1617 if (shost
->reverse_ordering
)
1619 * Scan from high to low id.
1621 order_id
= shost
->max_id
- id
- 1;
1624 __scsi_scan_target(&shost
->shost_gendev
, channel
,
1625 order_id
, lun
, rescan
);
1628 __scsi_scan_target(&shost
->shost_gendev
, channel
,
1632 int scsi_scan_host_selected(struct Scsi_Host
*shost
, unsigned int channel
,
1633 unsigned int id
, unsigned int lun
, int rescan
)
1635 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO
, shost
,
1637 __FUNCTION__
, channel
, id
, lun
));
1639 if (!shost
->async_scan
)
1640 scsi_complete_async_scans();
1642 if (((channel
!= SCAN_WILD_CARD
) && (channel
> shost
->max_channel
)) ||
1643 ((id
!= SCAN_WILD_CARD
) && (id
>= shost
->max_id
)) ||
1644 ((lun
!= SCAN_WILD_CARD
) && (lun
> shost
->max_lun
)))
1647 mutex_lock(&shost
->scan_mutex
);
1648 if (scsi_host_scan_allowed(shost
)) {
1649 if (channel
== SCAN_WILD_CARD
)
1650 for (channel
= 0; channel
<= shost
->max_channel
;
1652 scsi_scan_channel(shost
, channel
, id
, lun
,
1655 scsi_scan_channel(shost
, channel
, id
, lun
, rescan
);
1657 mutex_unlock(&shost
->scan_mutex
);
1662 static void scsi_sysfs_add_devices(struct Scsi_Host
*shost
)
1664 struct scsi_device
*sdev
;
1665 shost_for_each_device(sdev
, shost
) {
1666 if (scsi_sysfs_add_sdev(sdev
) != 0)
1667 scsi_destroy_sdev(sdev
);
1672 * scsi_prep_async_scan - prepare for an async scan
1673 * @shost: the host which will be scanned
1674 * Returns: a cookie to be passed to scsi_finish_async_scan()
1676 * Tells the midlayer this host is going to do an asynchronous scan.
1677 * It reserves the host's position in the scanning list and ensures
1678 * that other asynchronous scans started after this one won't affect the
1679 * ordering of the discovered devices.
1681 static struct async_scan_data
*scsi_prep_async_scan(struct Scsi_Host
*shost
)
1683 struct async_scan_data
*data
;
1685 if (strncmp(scsi_scan_type
, "sync", 4) == 0)
1688 if (shost
->async_scan
) {
1689 printk("%s called twice for host %d", __FUNCTION__
,
1695 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
1698 data
->shost
= scsi_host_get(shost
);
1701 init_completion(&data
->prev_finished
);
1703 spin_lock(&async_scan_lock
);
1704 shost
->async_scan
= 1;
1705 if (list_empty(&scanning_hosts
))
1706 complete(&data
->prev_finished
);
1707 list_add_tail(&data
->list
, &scanning_hosts
);
1708 spin_unlock(&async_scan_lock
);
1718 * scsi_finish_async_scan - asynchronous scan has finished
1719 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1721 * All the devices currently attached to this host have been found.
1722 * This function announces all the devices it has found to the rest
1725 static void scsi_finish_async_scan(struct async_scan_data
*data
)
1727 struct Scsi_Host
*shost
;
1732 shost
= data
->shost
;
1733 if (!shost
->async_scan
) {
1734 printk("%s called twice for host %d", __FUNCTION__
,
1740 wait_for_completion(&data
->prev_finished
);
1742 scsi_sysfs_add_devices(shost
);
1744 spin_lock(&async_scan_lock
);
1745 shost
->async_scan
= 0;
1746 list_del(&data
->list
);
1747 if (!list_empty(&scanning_hosts
)) {
1748 struct async_scan_data
*next
= list_entry(scanning_hosts
.next
,
1749 struct async_scan_data
, list
);
1750 complete(&next
->prev_finished
);
1752 spin_unlock(&async_scan_lock
);
1754 scsi_host_put(shost
);
1758 static void do_scsi_scan_host(struct Scsi_Host
*shost
)
1760 if (shost
->hostt
->scan_finished
) {
1761 unsigned long start
= jiffies
;
1762 if (shost
->hostt
->scan_start
)
1763 shost
->hostt
->scan_start(shost
);
1765 while (!shost
->hostt
->scan_finished(shost
, jiffies
- start
))
1768 scsi_scan_host_selected(shost
, SCAN_WILD_CARD
, SCAN_WILD_CARD
,
1773 static int do_scan_async(void *_data
)
1775 struct async_scan_data
*data
= _data
;
1776 do_scsi_scan_host(data
->shost
);
1777 scsi_finish_async_scan(data
);
1782 * scsi_scan_host - scan the given adapter
1783 * @shost: adapter to scan
1785 void scsi_scan_host(struct Scsi_Host
*shost
)
1787 struct async_scan_data
*data
;
1789 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1792 data
= scsi_prep_async_scan(shost
);
1794 do_scsi_scan_host(shost
);
1798 kthread_run(do_scan_async
, data
, "scsi_scan_%d", shost
->host_no
);
1800 EXPORT_SYMBOL(scsi_scan_host
);
1802 void scsi_forget_host(struct Scsi_Host
*shost
)
1804 struct scsi_device
*sdev
;
1805 unsigned long flags
;
1808 spin_lock_irqsave(shost
->host_lock
, flags
);
1809 list_for_each_entry(sdev
, &shost
->__devices
, siblings
) {
1810 if (sdev
->sdev_state
== SDEV_DEL
)
1812 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1813 __scsi_remove_device(sdev
);
1816 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1820 * Function: scsi_get_host_dev()
1822 * Purpose: Create a scsi_device that points to the host adapter itself.
1824 * Arguments: SHpnt - Host that needs a scsi_device
1826 * Lock status: None assumed.
1828 * Returns: The scsi_device or NULL
1831 * Attach a single scsi_device to the Scsi_Host - this should
1832 * be made to look like a "pseudo-device" that points to the
1835 * Note - this device is not accessible from any high-level
1836 * drivers (including generics), which is probably not
1837 * optimal. We can add hooks later to attach
1839 struct scsi_device
*scsi_get_host_dev(struct Scsi_Host
*shost
)
1841 struct scsi_device
*sdev
= NULL
;
1842 struct scsi_target
*starget
;
1844 mutex_lock(&shost
->scan_mutex
);
1845 if (!scsi_host_scan_allowed(shost
))
1847 starget
= scsi_alloc_target(&shost
->shost_gendev
, 0, shost
->this_id
);
1851 sdev
= scsi_alloc_sdev(starget
, 0, NULL
);
1853 sdev
->sdev_gendev
.parent
= get_device(&starget
->dev
);
1856 scsi_target_reap(starget
);
1857 put_device(&starget
->dev
);
1859 mutex_unlock(&shost
->scan_mutex
);
1862 EXPORT_SYMBOL(scsi_get_host_dev
);
1865 * Function: scsi_free_host_dev()
1867 * Purpose: Free a scsi_device that points to the host adapter itself.
1869 * Arguments: SHpnt - Host that needs a scsi_device
1871 * Lock status: None assumed.
1877 void scsi_free_host_dev(struct scsi_device
*sdev
)
1879 BUG_ON(sdev
->id
!= sdev
->host
->this_id
);
1881 scsi_destroy_sdev(sdev
);
1883 EXPORT_SYMBOL(scsi_free_host_dev
);