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
, uint
, 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
, uint
, 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
, uint
, 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 /* This lock protects only this list */
125 static DEFINE_SPINLOCK(async_scan_lock
);
126 static LIST_HEAD(scanning_hosts
);
128 struct async_scan_data
{
129 struct list_head list
;
130 struct Scsi_Host
*shost
;
131 struct completion prev_finished
;
135 * scsi_complete_async_scans - Wait for asynchronous scans to complete
137 * When this function returns, any host which started scanning before
138 * this function was called will have finished its scan. Hosts which
139 * started scanning after this function was called may or may not have
142 int scsi_complete_async_scans(void)
144 struct async_scan_data
*data
;
147 if (list_empty(&scanning_hosts
))
149 /* If we can't get memory immediately, that's OK. Just
150 * sleep a little. Even if we never get memory, the async
151 * scans will finish eventually.
153 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
159 init_completion(&data
->prev_finished
);
161 spin_lock(&async_scan_lock
);
162 /* Check that there's still somebody else on the list */
163 if (list_empty(&scanning_hosts
))
165 list_add_tail(&data
->list
, &scanning_hosts
);
166 spin_unlock(&async_scan_lock
);
168 printk(KERN_INFO
"scsi: waiting for bus probes to complete ...\n");
169 wait_for_completion(&data
->prev_finished
);
171 spin_lock(&async_scan_lock
);
172 list_del(&data
->list
);
173 if (!list_empty(&scanning_hosts
)) {
174 struct async_scan_data
*next
= list_entry(scanning_hosts
.next
,
175 struct async_scan_data
, list
);
176 complete(&next
->prev_finished
);
179 spin_unlock(&async_scan_lock
);
185 /* Only exported for the benefit of scsi_wait_scan */
186 EXPORT_SYMBOL_GPL(scsi_complete_async_scans
);
190 * For async scanning we need to wait for all the scans to complete before
191 * trying to mount the root fs. Otherwise non-modular drivers may not be ready
194 late_initcall(scsi_complete_async_scans
);
198 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
199 * @sdev: scsi device to send command to
200 * @result: area to store the result of the MODE SENSE
203 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
204 * Called for BLIST_KEY devices.
206 static void scsi_unlock_floptical(struct scsi_device
*sdev
,
207 unsigned char *result
)
209 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
211 printk(KERN_NOTICE
"scsi: unlocking floptical drive\n");
212 scsi_cmd
[0] = MODE_SENSE
;
216 scsi_cmd
[4] = 0x2a; /* size */
218 scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
, result
, 0x2a, NULL
,
223 * scsi_alloc_sdev - allocate and setup a scsi_Device
226 * Allocate, initialize for io, and return a pointer to a scsi_Device.
227 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
228 * adds scsi_Device to the appropriate list.
231 * scsi_Device pointer, or NULL on failure.
233 static struct scsi_device
*scsi_alloc_sdev(struct scsi_target
*starget
,
234 unsigned int lun
, void *hostdata
)
236 struct scsi_device
*sdev
;
237 int display_failure_msg
= 1, ret
;
238 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
240 sdev
= kzalloc(sizeof(*sdev
) + shost
->transportt
->device_size
,
245 sdev
->vendor
= scsi_null_device_strs
;
246 sdev
->model
= scsi_null_device_strs
;
247 sdev
->rev
= scsi_null_device_strs
;
249 sdev
->id
= starget
->id
;
251 sdev
->channel
= starget
->channel
;
252 sdev
->sdev_state
= SDEV_CREATED
;
253 INIT_LIST_HEAD(&sdev
->siblings
);
254 INIT_LIST_HEAD(&sdev
->same_target_siblings
);
255 INIT_LIST_HEAD(&sdev
->cmd_list
);
256 INIT_LIST_HEAD(&sdev
->starved_entry
);
257 spin_lock_init(&sdev
->list_lock
);
259 sdev
->sdev_gendev
.parent
= get_device(&starget
->dev
);
260 sdev
->sdev_target
= starget
;
262 /* usually NULL and set by ->slave_alloc instead */
263 sdev
->hostdata
= hostdata
;
265 /* if the device needs this changing, it may do so in the
266 * slave_configure function */
267 sdev
->max_device_blocked
= SCSI_DEFAULT_DEVICE_BLOCKED
;
270 * Some low level driver could use device->type
275 * Assume that the device will have handshaking problems,
276 * and then fix this field later if it turns out it
281 sdev
->request_queue
= scsi_alloc_queue(sdev
);
282 if (!sdev
->request_queue
) {
283 /* release fn is set up in scsi_sysfs_device_initialise, so
284 * have to free and put manually here */
285 put_device(&starget
->dev
);
290 sdev
->request_queue
->queuedata
= sdev
;
291 scsi_adjust_queue_depth(sdev
, 0, sdev
->host
->cmd_per_lun
);
293 scsi_sysfs_device_initialize(sdev
);
295 if (shost
->hostt
->slave_alloc
) {
296 ret
= shost
->hostt
->slave_alloc(sdev
);
299 * if LLDD reports slave not present, don't clutter
300 * console with alloc failure messages
303 display_failure_msg
= 0;
304 goto out_device_destroy
;
311 transport_destroy_device(&sdev
->sdev_gendev
);
312 put_device(&sdev
->sdev_gendev
);
314 if (display_failure_msg
)
315 printk(ALLOC_FAILURE_MSG
, __FUNCTION__
);
319 static void scsi_target_dev_release(struct device
*dev
)
321 struct device
*parent
= dev
->parent
;
322 struct scsi_target
*starget
= to_scsi_target(dev
);
328 int scsi_is_target_device(const struct device
*dev
)
330 return dev
->release
== scsi_target_dev_release
;
332 EXPORT_SYMBOL(scsi_is_target_device
);
334 static struct scsi_target
*__scsi_find_target(struct device
*parent
,
335 int channel
, uint id
)
337 struct scsi_target
*starget
, *found_starget
= NULL
;
338 struct Scsi_Host
*shost
= dev_to_shost(parent
);
340 * Search for an existing target for this sdev.
342 list_for_each_entry(starget
, &shost
->__targets
, siblings
) {
343 if (starget
->id
== id
&&
344 starget
->channel
== channel
) {
345 found_starget
= starget
;
350 get_device(&found_starget
->dev
);
352 return found_starget
;
356 * scsi_alloc_target - allocate a new or find an existing target
357 * @parent: parent of the target (need not be a scsi host)
358 * @channel: target channel number (zero if no channels)
359 * @id: target id number
361 * Return an existing target if one exists, provided it hasn't already
362 * gone into STARGET_DEL state, otherwise allocate a new target.
364 * The target is returned with an incremented reference, so the caller
365 * is responsible for both reaping and doing a last put
367 static struct scsi_target
*scsi_alloc_target(struct device
*parent
,
368 int channel
, uint id
)
370 struct Scsi_Host
*shost
= dev_to_shost(parent
);
371 struct device
*dev
= NULL
;
373 const int size
= sizeof(struct scsi_target
)
374 + shost
->transportt
->target_size
;
375 struct scsi_target
*starget
;
376 struct scsi_target
*found_target
;
379 starget
= kzalloc(size
, GFP_KERNEL
);
381 printk(KERN_ERR
"%s: allocation failure\n", __FUNCTION__
);
385 device_initialize(dev
);
386 starget
->reap_ref
= 1;
387 dev
->parent
= get_device(parent
);
388 dev
->release
= scsi_target_dev_release
;
389 sprintf(dev
->bus_id
, "target%d:%d:%d",
390 shost
->host_no
, channel
, id
);
392 starget
->channel
= channel
;
393 INIT_LIST_HEAD(&starget
->siblings
);
394 INIT_LIST_HEAD(&starget
->devices
);
395 starget
->state
= STARGET_RUNNING
;
396 starget
->scsi_level
= SCSI_2
;
398 spin_lock_irqsave(shost
->host_lock
, flags
);
400 found_target
= __scsi_find_target(parent
, channel
, id
);
404 list_add_tail(&starget
->siblings
, &shost
->__targets
);
405 spin_unlock_irqrestore(shost
->host_lock
, flags
);
406 /* allocate and add */
407 transport_setup_device(dev
);
408 error
= device_add(dev
);
410 dev_err(dev
, "target device_add failed, error %d\n", error
);
411 spin_lock_irqsave(shost
->host_lock
, flags
);
412 list_del_init(&starget
->siblings
);
413 spin_unlock_irqrestore(shost
->host_lock
, flags
);
414 transport_destroy_device(dev
);
419 transport_add_device(dev
);
420 if (shost
->hostt
->target_alloc
) {
421 error
= shost
->hostt
->target_alloc(starget
);
424 dev_printk(KERN_ERR
, dev
, "target allocation failed, error %d\n", error
);
425 /* don't want scsi_target_reap to do the final
426 * put because it will be under the host lock */
428 scsi_target_reap(starget
);
438 found_target
->reap_ref
++;
439 spin_unlock_irqrestore(shost
->host_lock
, flags
);
440 if (found_target
->state
!= STARGET_DEL
) {
445 /* Unfortunately, we found a dying target; need to
446 * wait until it's dead before we can get a new one */
447 put_device(&found_target
->dev
);
448 flush_scheduled_work();
452 static void scsi_target_reap_usercontext(struct work_struct
*work
)
454 struct scsi_target
*starget
=
455 container_of(work
, struct scsi_target
, ew
.work
);
456 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
459 transport_remove_device(&starget
->dev
);
460 device_del(&starget
->dev
);
461 transport_destroy_device(&starget
->dev
);
462 spin_lock_irqsave(shost
->host_lock
, flags
);
463 if (shost
->hostt
->target_destroy
)
464 shost
->hostt
->target_destroy(starget
);
465 list_del_init(&starget
->siblings
);
466 spin_unlock_irqrestore(shost
->host_lock
, flags
);
467 put_device(&starget
->dev
);
471 * scsi_target_reap - check to see if target is in use and destroy if not
473 * @starget: target to be checked
475 * This is used after removing a LUN or doing a last put of the target
476 * it checks atomically that nothing is using the target and removes
479 void scsi_target_reap(struct scsi_target
*starget
)
481 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
484 spin_lock_irqsave(shost
->host_lock
, flags
);
486 if (--starget
->reap_ref
== 0 && list_empty(&starget
->devices
)) {
487 BUG_ON(starget
->state
== STARGET_DEL
);
488 starget
->state
= STARGET_DEL
;
489 spin_unlock_irqrestore(shost
->host_lock
, flags
);
490 execute_in_process_context(scsi_target_reap_usercontext
,
495 spin_unlock_irqrestore(shost
->host_lock
, flags
);
501 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
502 * @s: INQUIRY result string to sanitize
503 * @len: length of the string
506 * The SCSI spec says that INQUIRY vendor, product, and revision
507 * strings must consist entirely of graphic ASCII characters,
508 * padded on the right with spaces. Since not all devices obey
509 * this rule, we will replace non-graphic or non-ASCII characters
510 * with spaces. Exception: a NUL character is interpreted as a
511 * string terminator, so all the following characters are set to
514 static void sanitize_inquiry_string(unsigned char *s
, int len
)
518 for (; len
> 0; (--len
, ++s
)) {
521 if (terminated
|| *s
< 0x20 || *s
> 0x7e)
527 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
528 * @sdev: scsi_device to probe
529 * @inq_result: area to store the INQUIRY result
530 * @result_len: len of inq_result
531 * @bflags: store any bflags found here
534 * Probe the lun associated with @req using a standard SCSI INQUIRY;
536 * If the INQUIRY is successful, zero is returned and the
537 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
538 * are copied to the scsi_device any flags value is stored in *@bflags.
540 static int scsi_probe_lun(struct scsi_device
*sdev
, unsigned char *inq_result
,
541 int result_len
, int *bflags
)
543 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
544 int first_inquiry_len
, try_inquiry_len
, next_inquiry_len
;
545 int response_len
= 0;
546 int pass
, count
, result
;
547 struct scsi_sense_hdr sshdr
;
551 /* Perform up to 3 passes. The first pass uses a conservative
552 * transfer length of 36 unless sdev->inquiry_len specifies a
553 * different value. */
554 first_inquiry_len
= sdev
->inquiry_len
? sdev
->inquiry_len
: 36;
555 try_inquiry_len
= first_inquiry_len
;
559 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO
, sdev
,
560 "scsi scan: INQUIRY pass %d length %d\n",
561 pass
, try_inquiry_len
));
563 /* Each pass gets up to three chances to ignore Unit Attention */
564 for (count
= 0; count
< 3; ++count
) {
565 memset(scsi_cmd
, 0, 6);
566 scsi_cmd
[0] = INQUIRY
;
567 scsi_cmd
[4] = (unsigned char) try_inquiry_len
;
569 memset(inq_result
, 0, try_inquiry_len
);
571 result
= scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
,
572 inq_result
, try_inquiry_len
, &sshdr
,
573 HZ
/ 2 + HZ
* scsi_inq_timeout
, 3);
575 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
"scsi scan: INQUIRY %s "
577 result
? "failed" : "successful", result
));
581 * not-ready to ready transition [asc/ascq=0x28/0x0]
582 * or power-on, reset [asc/ascq=0x29/0x0], continue.
583 * INQUIRY should not yield UNIT_ATTENTION
584 * but many buggy devices do so anyway.
586 if ((driver_byte(result
) & DRIVER_SENSE
) &&
587 scsi_sense_valid(&sshdr
)) {
588 if ((sshdr
.sense_key
== UNIT_ATTENTION
) &&
589 ((sshdr
.asc
== 0x28) ||
590 (sshdr
.asc
== 0x29)) &&
599 sanitize_inquiry_string(&inq_result
[8], 8);
600 sanitize_inquiry_string(&inq_result
[16], 16);
601 sanitize_inquiry_string(&inq_result
[32], 4);
603 response_len
= inq_result
[4] + 5;
604 if (response_len
> 255)
605 response_len
= first_inquiry_len
; /* sanity */
608 * Get any flags for this device.
610 * XXX add a bflags to scsi_device, and replace the
611 * corresponding bit fields in scsi_device, so bflags
612 * need not be passed as an argument.
614 *bflags
= scsi_get_device_flags(sdev
, &inq_result
[8],
617 /* When the first pass succeeds we gain information about
618 * what larger transfer lengths might work. */
620 if (BLIST_INQUIRY_36
& *bflags
)
621 next_inquiry_len
= 36;
622 else if (BLIST_INQUIRY_58
& *bflags
)
623 next_inquiry_len
= 58;
624 else if (sdev
->inquiry_len
)
625 next_inquiry_len
= sdev
->inquiry_len
;
627 next_inquiry_len
= response_len
;
629 /* If more data is available perform the second pass */
630 if (next_inquiry_len
> try_inquiry_len
) {
631 try_inquiry_len
= next_inquiry_len
;
637 } else if (pass
== 2) {
638 printk(KERN_INFO
"scsi scan: %d byte inquiry failed. "
639 "Consider BLIST_INQUIRY_36 for this device\n",
642 /* If this pass failed, the third pass goes back and transfers
643 * the same amount as we successfully got in the first pass. */
644 try_inquiry_len
= first_inquiry_len
;
649 /* If the last transfer attempt got an error, assume the
650 * peripheral doesn't exist or is dead. */
654 /* Don't report any more data than the device says is valid */
655 sdev
->inquiry_len
= min(try_inquiry_len
, response_len
);
658 * XXX Abort if the response length is less than 36? If less than
659 * 32, the lookup of the device flags (above) could be invalid,
660 * and it would be possible to take an incorrect action - we do
661 * not want to hang because of a short INQUIRY. On the flip side,
662 * if the device is spun down or becoming ready (and so it gives a
663 * short INQUIRY), an abort here prevents any further use of the
664 * device, including spin up.
666 * On the whole, the best approach seems to be to assume the first
667 * 36 bytes are valid no matter what the device says. That's
668 * better than copying < 36 bytes to the inquiry-result buffer
669 * and displaying garbage for the Vendor, Product, or Revision
672 if (sdev
->inquiry_len
< 36) {
673 printk(KERN_INFO
"scsi scan: INQUIRY result too short (%d),"
674 " using 36\n", sdev
->inquiry_len
);
675 sdev
->inquiry_len
= 36;
679 * Related to the above issue:
681 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
682 * and if not ready, sent a START_STOP to start (maybe spin up) and
683 * then send the INQUIRY again, since the INQUIRY can change after
684 * a device is initialized.
686 * Ideally, start a device if explicitly asked to do so. This
687 * assumes that a device is spun up on power on, spun down on
688 * request, and then spun up on request.
692 * The scanning code needs to know the scsi_level, even if no
693 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
694 * non-zero LUNs can be scanned.
696 sdev
->scsi_level
= inq_result
[2] & 0x07;
697 if (sdev
->scsi_level
>= 2 ||
698 (sdev
->scsi_level
== 1 && (inq_result
[3] & 0x0f) == 1))
700 sdev
->sdev_target
->scsi_level
= sdev
->scsi_level
;
706 * scsi_add_lun - allocate and fully initialze a scsi_device
707 * @sdev: holds information to be stored in the new scsi_device
708 * @inq_result: holds the result of a previous INQUIRY to the LUN
709 * @bflags: black/white list flag
710 * @async: 1 if this device is being scanned asynchronously
713 * Initialize the scsi_device @sdev. Optionally set fields based
714 * on values in *@bflags.
717 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
718 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
720 static int scsi_add_lun(struct scsi_device
*sdev
, unsigned char *inq_result
,
721 int *bflags
, int async
)
724 * XXX do not save the inquiry, since it can change underneath us,
725 * save just vendor/model/rev.
727 * Rather than save it and have an ioctl that retrieves the saved
728 * value, have an ioctl that executes the same INQUIRY code used
729 * in scsi_probe_lun, let user level programs doing INQUIRY
730 * scanning run at their own risk, or supply a user level program
731 * that can correctly scan.
735 * Copy at least 36 bytes of INQUIRY data, so that we don't
736 * dereference unallocated memory when accessing the Vendor,
737 * Product, and Revision strings. Badly behaved devices may set
738 * the INQUIRY Additional Length byte to a small value, indicating
739 * these strings are invalid, but often they contain plausible data
740 * nonetheless. It doesn't matter if the device sent < 36 bytes
741 * total, since scsi_probe_lun() initializes inq_result with 0s.
743 sdev
->inquiry
= kmemdup(inq_result
,
744 max_t(size_t, sdev
->inquiry_len
, 36),
746 if (sdev
->inquiry
== NULL
)
747 return SCSI_SCAN_NO_RESPONSE
;
749 sdev
->vendor
= (char *) (sdev
->inquiry
+ 8);
750 sdev
->model
= (char *) (sdev
->inquiry
+ 16);
751 sdev
->rev
= (char *) (sdev
->inquiry
+ 32);
753 if (*bflags
& BLIST_ISROM
) {
754 sdev
->type
= TYPE_ROM
;
757 sdev
->type
= (inq_result
[0] & 0x1f);
758 sdev
->removable
= (inq_result
[1] & 0x80) >> 7;
761 switch (sdev
->type
) {
769 case TYPE_MEDIUM_CHANGER
:
780 printk(KERN_INFO
"scsi: unknown device type %d\n", sdev
->type
);
783 if (sdev
->type
== TYPE_RBC
|| sdev
->type
== TYPE_ROM
) {
784 /* RBC and MMC devices can return SCSI-3 compliance and yet
785 * still not support REPORT LUNS, so make them act as
786 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
787 * specifically set */
788 if ((*bflags
& BLIST_REPORTLUN2
) == 0)
789 *bflags
|= BLIST_NOREPORTLUN
;
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
->lockable
= sdev
->removable
;
810 sdev
->soft_reset
= (inq_result
[7] & 1) && ((inq_result
[3] & 7) == 2);
812 if (sdev
->scsi_level
>= SCSI_3
||
813 (sdev
->inquiry_len
> 56 && inq_result
[56] & 0x04))
815 if (inq_result
[7] & 0x60)
817 if (inq_result
[7] & 0x10)
820 sdev_printk(KERN_NOTICE
, sdev
, "%s %.8s %.16s %.4s PQ: %d "
821 "ANSI: %d%s\n", scsi_device_type(sdev
->type
),
822 sdev
->vendor
, sdev
->model
, sdev
->rev
,
823 sdev
->inq_periph_qual
, inq_result
[2] & 0x07,
824 (inq_result
[3] & 0x0f) == 1 ? " CCS" : "");
826 if ((sdev
->scsi_level
>= SCSI_2
) && (inq_result
[7] & 2) &&
827 !(*bflags
& BLIST_NOTQ
))
828 sdev
->tagged_supported
= 1;
831 * Some devices (Texel CD ROM drives) have handshaking problems
832 * when used with the Seagate controllers. borken is initialized
833 * to 1, and then set it to 0 here.
835 if ((*bflags
& BLIST_BORKEN
) == 0)
838 if (*bflags
& BLIST_NO_ULD_ATTACH
)
839 sdev
->no_uld_attach
= 1;
842 * Apparently some really broken devices (contrary to the SCSI
843 * standards) need to be selected without asserting ATN
845 if (*bflags
& BLIST_SELECT_NO_ATN
)
846 sdev
->select_no_atn
= 1;
849 * Maximum 512 sector transfer length
850 * broken RA4x00 Compaq Disk Array
852 if (*bflags
& BLIST_MAX_512
)
853 blk_queue_max_sectors(sdev
->request_queue
, 512);
856 * Some devices may not want to have a start command automatically
857 * issued when a device is added.
859 if (*bflags
& BLIST_NOSTARTONADD
)
860 sdev
->no_start_on_add
= 1;
862 if (*bflags
& BLIST_SINGLELUN
)
863 sdev
->single_lun
= 1;
865 sdev
->use_10_for_rw
= 1;
867 if (*bflags
& BLIST_MS_SKIP_PAGE_08
)
868 sdev
->skip_ms_page_8
= 1;
870 if (*bflags
& BLIST_MS_SKIP_PAGE_3F
)
871 sdev
->skip_ms_page_3f
= 1;
873 if (*bflags
& BLIST_USE_10_BYTE_MS
)
874 sdev
->use_10_for_ms
= 1;
876 /* set the device running here so that slave configure
878 scsi_device_set_state(sdev
, SDEV_RUNNING
);
880 if (*bflags
& BLIST_MS_192_BYTES_FOR_3F
)
881 sdev
->use_192_bytes_for_3f
= 1;
883 if (*bflags
& BLIST_NOT_LOCKABLE
)
886 if (*bflags
& BLIST_RETRY_HWERROR
)
887 sdev
->retry_hwerror
= 1;
889 transport_configure_device(&sdev
->sdev_gendev
);
891 if (sdev
->host
->hostt
->slave_configure
) {
892 int ret
= sdev
->host
->hostt
->slave_configure(sdev
);
895 * if LLDD reports slave not present, don't clutter
896 * console with alloc failure messages
899 sdev_printk(KERN_ERR
, sdev
,
900 "failed to configure device\n");
902 return SCSI_SCAN_NO_RESPONSE
;
907 * Ok, the device is now all set up, we can
908 * register it and tell the rest of the kernel
911 if (!async
&& scsi_sysfs_add_sdev(sdev
) != 0)
912 return SCSI_SCAN_NO_RESPONSE
;
914 return SCSI_SCAN_LUN_PRESENT
;
917 static inline void scsi_destroy_sdev(struct scsi_device
*sdev
)
919 scsi_device_set_state(sdev
, SDEV_DEL
);
920 if (sdev
->host
->hostt
->slave_destroy
)
921 sdev
->host
->hostt
->slave_destroy(sdev
);
922 transport_destroy_device(&sdev
->sdev_gendev
);
923 put_device(&sdev
->sdev_gendev
);
926 #ifdef CONFIG_SCSI_LOGGING
928 * scsi_inq_str - print INQUIRY data from min to max index,
929 * strip trailing whitespace
930 * @buf: Output buffer with at least end-first+1 bytes of space
931 * @inq: Inquiry buffer (input)
932 * @first: Offset of string into inq
933 * @end: Index after last character in inq
935 static unsigned char *scsi_inq_str(unsigned char *buf
, unsigned char *inq
,
936 unsigned first
, unsigned end
)
938 unsigned term
= 0, idx
;
940 for (idx
= 0; idx
+ first
< end
&& idx
+ first
< inq
[4] + 5; idx
++) {
941 if (inq
[idx
+first
] > ' ') {
942 buf
[idx
] = inq
[idx
+first
];
954 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
955 * @starget: pointer to target device structure
956 * @lun: LUN of target device
957 * @sdevscan: probe the LUN corresponding to this scsi_device
958 * @sdevnew: store the value of any new scsi_device allocated
959 * @bflagsp: store bflags here if not NULL
962 * Call scsi_probe_lun, if a LUN with an attached device is found,
963 * allocate and set it up by calling scsi_add_lun.
966 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
967 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
968 * attached at the LUN
969 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
971 static int scsi_probe_and_add_lun(struct scsi_target
*starget
,
972 uint lun
, int *bflagsp
,
973 struct scsi_device
**sdevp
, int rescan
,
976 struct scsi_device
*sdev
;
977 unsigned char *result
;
978 int bflags
, res
= SCSI_SCAN_NO_RESPONSE
, result_len
= 256;
979 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
982 * The rescan flag is used as an optimization, the first scan of a
983 * host adapter calls into here with rescan == 0.
985 sdev
= scsi_device_lookup_by_target(starget
, lun
);
987 if (rescan
|| sdev
->sdev_state
!= SDEV_CREATED
) {
988 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
989 "scsi scan: device exists on %s\n",
990 sdev
->sdev_gendev
.bus_id
));
994 scsi_device_put(sdev
);
997 *bflagsp
= scsi_get_device_flags(sdev
,
1000 return SCSI_SCAN_LUN_PRESENT
;
1002 scsi_device_put(sdev
);
1004 sdev
= scsi_alloc_sdev(starget
, lun
, hostdata
);
1008 result
= kmalloc(result_len
, GFP_ATOMIC
|
1009 ((shost
->unchecked_isa_dma
) ? __GFP_DMA
: 0));
1013 if (scsi_probe_lun(sdev
, result
, result_len
, &bflags
))
1014 goto out_free_result
;
1019 * result contains valid SCSI INQUIRY data.
1021 if (((result
[0] >> 5) == 3) && !(bflags
& BLIST_ATTACH_PQ3
)) {
1023 * For a Peripheral qualifier 3 (011b), the SCSI
1024 * spec says: The device server is not capable of
1025 * supporting a physical device on this logical
1028 * For disks, this implies that there is no
1029 * logical disk configured at sdev->lun, but there
1030 * is a target id responding.
1032 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO
, sdev
, "scsi scan:"
1033 " peripheral qualifier of 3, device not"
1036 SCSI_LOG_SCAN_BUS(1, {
1037 unsigned char vend
[9];
1038 unsigned char mod
[17];
1040 sdev_printk(KERN_INFO
, sdev
,
1041 "scsi scan: consider passing scsi_mod."
1042 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1043 scsi_inq_str(vend
, result
, 8, 16),
1044 scsi_inq_str(mod
, result
, 16, 32));
1048 res
= SCSI_SCAN_TARGET_PRESENT
;
1049 goto out_free_result
;
1053 * Some targets may set slight variations of PQ and PDT to signal
1054 * that no LUN is present, so don't add sdev in these cases.
1055 * Two specific examples are:
1056 * 1) NetApp targets: return PQ=1, PDT=0x1f
1057 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1058 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1061 * 1) SCSI SPC-3, pp. 145-146
1062 * PQ=1: "A peripheral device having the specified peripheral
1063 * device type is not connected to this logical unit. However, the
1064 * device server is capable of supporting the specified peripheral
1065 * device type on this logical unit."
1066 * PDT=0x1f: "Unknown or no device type"
1067 * 2) USB UFI 1.0, p. 20
1068 * PDT=00h Direct-access device (floppy)
1069 * PDT=1Fh none (no FDD connected to the requested logical unit)
1071 if (((result
[0] >> 5) == 1 || starget
->pdt_1f_for_no_lun
) &&
1072 (result
[0] & 0x1f) == 0x1f) {
1073 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1074 "scsi scan: peripheral device type"
1075 " of 31, no device added\n"));
1076 res
= SCSI_SCAN_TARGET_PRESENT
;
1077 goto out_free_result
;
1080 res
= scsi_add_lun(sdev
, result
, &bflags
, shost
->async_scan
);
1081 if (res
== SCSI_SCAN_LUN_PRESENT
) {
1082 if (bflags
& BLIST_KEY
) {
1084 scsi_unlock_floptical(sdev
, result
);
1091 if (res
== SCSI_SCAN_LUN_PRESENT
) {
1093 if (scsi_device_get(sdev
) == 0) {
1096 __scsi_remove_device(sdev
);
1097 res
= SCSI_SCAN_NO_RESPONSE
;
1101 scsi_destroy_sdev(sdev
);
1107 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1108 * @starget: pointer to target structure to scan
1109 * @bflags: black/white list flag for LUN 0
1112 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1113 * scanned) to some maximum lun until a LUN is found with no device
1114 * attached. Use the bflags to figure out any oddities.
1116 * Modifies sdevscan->lun.
1118 static void scsi_sequential_lun_scan(struct scsi_target
*starget
,
1119 int bflags
, int scsi_level
, int rescan
)
1121 unsigned int sparse_lun
, lun
, max_dev_lun
;
1122 struct Scsi_Host
*shost
= dev_to_shost(starget
->dev
.parent
);
1124 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
"scsi scan: Sequential scan of"
1125 "%s\n", starget
->dev
.bus_id
));
1127 max_dev_lun
= min(max_scsi_luns
, shost
->max_lun
);
1129 * If this device is known to support sparse multiple units,
1130 * override the other settings, and scan all of them. Normally,
1131 * SCSI-3 devices should be scanned via the REPORT LUNS.
1133 if (bflags
& BLIST_SPARSELUN
) {
1134 max_dev_lun
= shost
->max_lun
;
1140 * If less than SCSI_1_CSS, and no special lun scaning, stop
1141 * scanning; this matches 2.4 behaviour, but could just be a bug
1142 * (to continue scanning a SCSI_1_CSS device).
1144 * This test is broken. We might not have any device on lun0 for
1145 * a sparselun device, and if that's the case then how would we
1146 * know the real scsi_level, eh? It might make sense to just not
1147 * scan any SCSI_1 device for non-0 luns, but that check would best
1148 * go into scsi_alloc_sdev() and just have it return null when asked
1149 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1151 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1152 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1157 * If this device is known to support multiple units, override
1158 * the other settings, and scan all of them.
1160 if (bflags
& BLIST_FORCELUN
)
1161 max_dev_lun
= shost
->max_lun
;
1163 * REGAL CDC-4X: avoid hang after LUN 4
1165 if (bflags
& BLIST_MAX5LUN
)
1166 max_dev_lun
= min(5U, max_dev_lun
);
1168 * Do not scan SCSI-2 or lower device past LUN 7, unless
1171 if (scsi_level
< SCSI_3
&& !(bflags
& BLIST_LARGELUN
))
1172 max_dev_lun
= min(8U, max_dev_lun
);
1175 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1176 * until we reach the max, or no LUN is found and we are not
1179 for (lun
= 1; lun
< max_dev_lun
; ++lun
)
1180 if ((scsi_probe_and_add_lun(starget
, lun
, NULL
, NULL
, rescan
,
1181 NULL
) != SCSI_SCAN_LUN_PRESENT
) &&
1187 * scsilun_to_int: convert a scsi_lun to an int
1188 * @scsilun: struct scsi_lun to be converted.
1191 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1192 * integer, and return the result. The caller must check for
1193 * truncation before using this function.
1196 * The struct scsi_lun is assumed to be four levels, with each level
1197 * effectively containing a SCSI byte-ordered (big endian) short; the
1198 * addressing bits of each level are ignored (the highest two bits).
1199 * For a description of the LUN format, post SCSI-3 see the SCSI
1200 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1202 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1203 * the integer: 0x0b030a04
1205 int scsilun_to_int(struct scsi_lun
*scsilun
)
1211 for (i
= 0; i
< sizeof(lun
); i
+= 2)
1212 lun
= lun
| (((scsilun
->scsi_lun
[i
] << 8) |
1213 scsilun
->scsi_lun
[i
+ 1]) << (i
* 8));
1216 EXPORT_SYMBOL(scsilun_to_int
);
1219 * int_to_scsilun: reverts an int into a scsi_lun
1220 * @int: integer to be reverted
1221 * @scsilun: struct scsi_lun to be set.
1224 * Reverts the functionality of the scsilun_to_int, which packed
1225 * an 8-byte lun value into an int. This routine unpacks the int
1226 * back into the lun value.
1227 * Note: the scsilun_to_int() routine does not truly handle all
1228 * 8bytes of the lun value. This functions restores only as much
1229 * as was set by the routine.
1232 * Given an integer : 0x0b030a04, this function returns a
1233 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1236 void int_to_scsilun(unsigned int lun
, struct scsi_lun
*scsilun
)
1240 memset(scsilun
->scsi_lun
, 0, sizeof(scsilun
->scsi_lun
));
1242 for (i
= 0; i
< sizeof(lun
); i
+= 2) {
1243 scsilun
->scsi_lun
[i
] = (lun
>> 8) & 0xFF;
1244 scsilun
->scsi_lun
[i
+1] = lun
& 0xFF;
1248 EXPORT_SYMBOL(int_to_scsilun
);
1251 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1252 * @sdevscan: scan the host, channel, and id of this scsi_device
1255 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1256 * command, and scan the resulting list of LUNs by calling
1257 * scsi_probe_and_add_lun.
1259 * Modifies sdevscan->lun.
1262 * 0: scan completed (or no memory, so further scanning is futile)
1263 * 1: no report lun scan, or not configured
1265 static int scsi_report_lun_scan(struct scsi_target
*starget
, int bflags
,
1269 unsigned char scsi_cmd
[MAX_COMMAND_SIZE
];
1270 unsigned int length
;
1272 unsigned int num_luns
;
1273 unsigned int retries
;
1275 struct scsi_lun
*lunp
, *lun_data
;
1277 struct scsi_sense_hdr sshdr
;
1278 struct scsi_device
*sdev
;
1279 struct Scsi_Host
*shost
= dev_to_shost(&starget
->dev
);
1283 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1284 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1285 * support more than 8 LUNs.
1287 if (bflags
& BLIST_NOREPORTLUN
)
1289 if (starget
->scsi_level
< SCSI_2
&&
1290 starget
->scsi_level
!= SCSI_UNKNOWN
)
1292 if (starget
->scsi_level
< SCSI_3
&&
1293 (!(bflags
& BLIST_REPORTLUN2
) || shost
->max_lun
<= 8))
1295 if (bflags
& BLIST_NOLUN
)
1298 if (!(sdev
= scsi_device_lookup_by_target(starget
, 0))) {
1299 sdev
= scsi_alloc_sdev(starget
, 0, NULL
);
1302 if (scsi_device_get(sdev
))
1306 sprintf(devname
, "host %d channel %d id %d",
1307 shost
->host_no
, sdev
->channel
, sdev
->id
);
1310 * Allocate enough to hold the header (the same size as one scsi_lun)
1311 * plus the max number of luns we are requesting.
1313 * Reallocating and trying again (with the exact amount we need)
1314 * would be nice, but then we need to somehow limit the size
1315 * allocated based on the available memory and the limits of
1316 * kmalloc - we don't want a kmalloc() failure of a huge value to
1317 * prevent us from finding any LUNs on this target.
1319 length
= (max_scsi_report_luns
+ 1) * sizeof(struct scsi_lun
);
1320 lun_data
= kmalloc(length
, GFP_ATOMIC
|
1321 (sdev
->host
->unchecked_isa_dma
? __GFP_DMA
: 0));
1323 printk(ALLOC_FAILURE_MSG
, __FUNCTION__
);
1327 scsi_cmd
[0] = REPORT_LUNS
;
1330 * bytes 1 - 5: reserved, set to zero.
1332 memset(&scsi_cmd
[1], 0, 5);
1335 * bytes 6 - 9: length of the command.
1337 scsi_cmd
[6] = (unsigned char) (length
>> 24) & 0xff;
1338 scsi_cmd
[7] = (unsigned char) (length
>> 16) & 0xff;
1339 scsi_cmd
[8] = (unsigned char) (length
>> 8) & 0xff;
1340 scsi_cmd
[9] = (unsigned char) length
& 0xff;
1342 scsi_cmd
[10] = 0; /* reserved */
1343 scsi_cmd
[11] = 0; /* control */
1346 * We can get a UNIT ATTENTION, for example a power on/reset, so
1347 * retry a few times (like sd.c does for TEST UNIT READY).
1348 * Experience shows some combinations of adapter/devices get at
1349 * least two power on/resets.
1351 * Illegal requests (for devices that do not support REPORT LUNS)
1352 * should come through as a check condition, and will not generate
1355 for (retries
= 0; retries
< 3; retries
++) {
1356 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO
"scsi scan: Sending"
1357 " REPORT LUNS to %s (try %d)\n", devname
,
1360 result
= scsi_execute_req(sdev
, scsi_cmd
, DMA_FROM_DEVICE
,
1361 lun_data
, length
, &sshdr
,
1362 SCSI_TIMEOUT
+ 4 * HZ
, 3);
1364 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO
"scsi scan: REPORT LUNS"
1365 " %s (try %d) result 0x%x\n", result
1366 ? "failed" : "successful", retries
, result
));
1369 else if (scsi_sense_valid(&sshdr
)) {
1370 if (sshdr
.sense_key
!= UNIT_ATTENTION
)
1377 * The device probably does not support a REPORT LUN command
1384 * Get the length from the first four bytes of lun_data.
1386 data
= (u8
*) lun_data
->scsi_lun
;
1387 length
= ((data
[0] << 24) | (data
[1] << 16) |
1388 (data
[2] << 8) | (data
[3] << 0));
1390 num_luns
= (length
/ sizeof(struct scsi_lun
));
1391 if (num_luns
> max_scsi_report_luns
) {
1392 printk(KERN_WARNING
"scsi: On %s only %d (max_scsi_report_luns)"
1393 " of %d luns reported, try increasing"
1394 " max_scsi_report_luns.\n", devname
,
1395 max_scsi_report_luns
, num_luns
);
1396 num_luns
= max_scsi_report_luns
;
1399 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO
, sdev
,
1400 "scsi scan: REPORT LUN scan\n"));
1403 * Scan the luns in lun_data. The entry at offset 0 is really
1404 * the header, so start at 1 and go up to and including num_luns.
1406 for (lunp
= &lun_data
[1]; lunp
<= &lun_data
[num_luns
]; lunp
++) {
1407 lun
= scsilun_to_int(lunp
);
1410 * Check if the unused part of lunp is non-zero, and so
1411 * does not fit in lun.
1413 if (memcmp(&lunp
->scsi_lun
[sizeof(lun
)], "\0\0\0\0", 4)) {
1417 * Output an error displaying the LUN in byte order,
1418 * this differs from what linux would print for the
1419 * integer LUN value.
1421 printk(KERN_WARNING
"scsi: %s lun 0x", devname
);
1422 data
= (char *)lunp
->scsi_lun
;
1423 for (i
= 0; i
< sizeof(struct scsi_lun
); i
++)
1424 printk("%02x", data
[i
]);
1425 printk(" has a LUN larger than currently supported.\n");
1426 } else if (lun
> sdev
->host
->max_lun
) {
1427 printk(KERN_WARNING
"scsi: %s lun%d has a LUN larger"
1428 " than allowed by the host adapter\n",
1433 res
= scsi_probe_and_add_lun(starget
,
1434 lun
, NULL
, NULL
, rescan
, NULL
);
1435 if (res
== SCSI_SCAN_NO_RESPONSE
) {
1437 * Got some results, but now none, abort.
1439 sdev_printk(KERN_ERR
, sdev
,
1440 "Unexpected response"
1441 " from lun %d while scanning, scan"
1451 scsi_device_put(sdev
);
1452 if (sdev
->sdev_state
== SDEV_CREATED
)
1454 * the sdev we used didn't appear in the report luns scan
1456 scsi_destroy_sdev(sdev
);
1460 struct scsi_device
*__scsi_add_device(struct Scsi_Host
*shost
, uint channel
,
1461 uint id
, uint lun
, void *hostdata
)
1463 struct scsi_device
*sdev
= ERR_PTR(-ENODEV
);
1464 struct device
*parent
= &shost
->shost_gendev
;
1465 struct scsi_target
*starget
;
1467 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1468 return ERR_PTR(-ENODEV
);
1470 starget
= scsi_alloc_target(parent
, channel
, id
);
1472 return ERR_PTR(-ENOMEM
);
1474 mutex_lock(&shost
->scan_mutex
);
1475 if (!shost
->async_scan
)
1476 scsi_complete_async_scans();
1478 if (scsi_host_scan_allowed(shost
))
1479 scsi_probe_and_add_lun(starget
, lun
, NULL
, &sdev
, 1, hostdata
);
1480 mutex_unlock(&shost
->scan_mutex
);
1481 scsi_target_reap(starget
);
1482 put_device(&starget
->dev
);
1486 EXPORT_SYMBOL(__scsi_add_device
);
1488 int scsi_add_device(struct Scsi_Host
*host
, uint channel
,
1489 uint target
, uint lun
)
1491 struct scsi_device
*sdev
=
1492 __scsi_add_device(host
, channel
, target
, lun
, NULL
);
1494 return PTR_ERR(sdev
);
1496 scsi_device_put(sdev
);
1499 EXPORT_SYMBOL(scsi_add_device
);
1501 void scsi_rescan_device(struct device
*dev
)
1503 struct scsi_driver
*drv
;
1508 drv
= to_scsi_driver(dev
->driver
);
1509 if (try_module_get(drv
->owner
)) {
1512 module_put(drv
->owner
);
1515 EXPORT_SYMBOL(scsi_rescan_device
);
1517 static void __scsi_scan_target(struct device
*parent
, unsigned int channel
,
1518 unsigned int id
, unsigned int lun
, int rescan
)
1520 struct Scsi_Host
*shost
= dev_to_shost(parent
);
1523 struct scsi_target
*starget
;
1525 if (shost
->this_id
== id
)
1527 * Don't scan the host adapter
1531 starget
= scsi_alloc_target(parent
, channel
, id
);
1535 if (lun
!= SCAN_WILD_CARD
) {
1537 * Scan for a specific host/chan/id/lun.
1539 scsi_probe_and_add_lun(starget
, lun
, NULL
, NULL
, rescan
, NULL
);
1544 * Scan LUN 0, if there is some response, scan further. Ideally, we
1545 * would not configure LUN 0 until all LUNs are scanned.
1547 res
= scsi_probe_and_add_lun(starget
, 0, &bflags
, NULL
, rescan
, NULL
);
1548 if (res
== SCSI_SCAN_LUN_PRESENT
|| res
== SCSI_SCAN_TARGET_PRESENT
) {
1549 if (scsi_report_lun_scan(starget
, bflags
, rescan
) != 0)
1551 * The REPORT LUN did not scan the target,
1552 * do a sequential scan.
1554 scsi_sequential_lun_scan(starget
, bflags
,
1555 starget
->scsi_level
, rescan
);
1559 /* now determine if the target has any children at all
1560 * and if not, nuke it */
1561 scsi_target_reap(starget
);
1563 put_device(&starget
->dev
);
1567 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1569 * @parent: host to scan
1570 * @channel: channel to scan
1571 * @id: target id to scan
1572 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1573 * @rescan: passed to LUN scanning routines
1576 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1577 * and possibly all LUNs on the target id.
1579 * First try a REPORT LUN scan, if that does not scan the target, do a
1580 * sequential scan of LUNs on the target id.
1582 void scsi_scan_target(struct device
*parent
, unsigned int channel
,
1583 unsigned int id
, unsigned int lun
, int rescan
)
1585 struct Scsi_Host
*shost
= dev_to_shost(parent
);
1587 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1590 mutex_lock(&shost
->scan_mutex
);
1591 if (!shost
->async_scan
)
1592 scsi_complete_async_scans();
1594 if (scsi_host_scan_allowed(shost
))
1595 __scsi_scan_target(parent
, channel
, id
, lun
, rescan
);
1596 mutex_unlock(&shost
->scan_mutex
);
1598 EXPORT_SYMBOL(scsi_scan_target
);
1600 static void scsi_scan_channel(struct Scsi_Host
*shost
, unsigned int channel
,
1601 unsigned int id
, unsigned int lun
, int rescan
)
1605 if (id
== SCAN_WILD_CARD
)
1606 for (id
= 0; id
< shost
->max_id
; ++id
) {
1608 * XXX adapter drivers when possible (FCP, iSCSI)
1609 * could modify max_id to match the current max,
1610 * not the absolute max.
1612 * XXX add a shost id iterator, so for example,
1613 * the FC ID can be the same as a target id
1614 * without a huge overhead of sparse id's.
1616 if (shost
->reverse_ordering
)
1618 * Scan from high to low id.
1620 order_id
= shost
->max_id
- id
- 1;
1623 __scsi_scan_target(&shost
->shost_gendev
, channel
,
1624 order_id
, lun
, rescan
);
1627 __scsi_scan_target(&shost
->shost_gendev
, channel
,
1631 int scsi_scan_host_selected(struct Scsi_Host
*shost
, unsigned int channel
,
1632 unsigned int id
, unsigned int lun
, int rescan
)
1634 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO
, shost
,
1636 __FUNCTION__
, channel
, id
, lun
));
1638 if (((channel
!= SCAN_WILD_CARD
) && (channel
> shost
->max_channel
)) ||
1639 ((id
!= SCAN_WILD_CARD
) && (id
>= shost
->max_id
)) ||
1640 ((lun
!= SCAN_WILD_CARD
) && (lun
> shost
->max_lun
)))
1643 mutex_lock(&shost
->scan_mutex
);
1644 if (!shost
->async_scan
)
1645 scsi_complete_async_scans();
1647 if (scsi_host_scan_allowed(shost
)) {
1648 if (channel
== SCAN_WILD_CARD
)
1649 for (channel
= 0; channel
<= shost
->max_channel
;
1651 scsi_scan_channel(shost
, channel
, id
, lun
,
1654 scsi_scan_channel(shost
, channel
, id
, lun
, rescan
);
1656 mutex_unlock(&shost
->scan_mutex
);
1661 static void scsi_sysfs_add_devices(struct Scsi_Host
*shost
)
1663 struct scsi_device
*sdev
;
1664 shost_for_each_device(sdev
, shost
) {
1665 if (!scsi_host_scan_allowed(shost
) ||
1666 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
;
1684 unsigned long flags
;
1686 if (strncmp(scsi_scan_type
, "sync", 4) == 0)
1689 if (shost
->async_scan
) {
1690 printk("%s called twice for host %d", __FUNCTION__
,
1696 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
1699 data
->shost
= scsi_host_get(shost
);
1702 init_completion(&data
->prev_finished
);
1704 mutex_lock(&shost
->scan_mutex
);
1705 spin_lock_irqsave(shost
->host_lock
, flags
);
1706 shost
->async_scan
= 1;
1707 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1708 mutex_unlock(&shost
->scan_mutex
);
1710 spin_lock(&async_scan_lock
);
1711 if (list_empty(&scanning_hosts
))
1712 complete(&data
->prev_finished
);
1713 list_add_tail(&data
->list
, &scanning_hosts
);
1714 spin_unlock(&async_scan_lock
);
1724 * scsi_finish_async_scan - asynchronous scan has finished
1725 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1727 * All the devices currently attached to this host have been found.
1728 * This function announces all the devices it has found to the rest
1731 static void scsi_finish_async_scan(struct async_scan_data
*data
)
1733 struct Scsi_Host
*shost
;
1734 unsigned long flags
;
1739 shost
= data
->shost
;
1741 mutex_lock(&shost
->scan_mutex
);
1743 if (!shost
->async_scan
) {
1744 printk("%s called twice for host %d", __FUNCTION__
,
1750 wait_for_completion(&data
->prev_finished
);
1752 scsi_sysfs_add_devices(shost
);
1754 spin_lock_irqsave(shost
->host_lock
, flags
);
1755 shost
->async_scan
= 0;
1756 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1758 mutex_unlock(&shost
->scan_mutex
);
1760 spin_lock(&async_scan_lock
);
1761 list_del(&data
->list
);
1762 if (!list_empty(&scanning_hosts
)) {
1763 struct async_scan_data
*next
= list_entry(scanning_hosts
.next
,
1764 struct async_scan_data
, list
);
1765 complete(&next
->prev_finished
);
1767 spin_unlock(&async_scan_lock
);
1769 scsi_host_put(shost
);
1773 static void do_scsi_scan_host(struct Scsi_Host
*shost
)
1775 if (shost
->hostt
->scan_finished
) {
1776 unsigned long start
= jiffies
;
1777 if (shost
->hostt
->scan_start
)
1778 shost
->hostt
->scan_start(shost
);
1780 while (!shost
->hostt
->scan_finished(shost
, jiffies
- start
))
1783 scsi_scan_host_selected(shost
, SCAN_WILD_CARD
, SCAN_WILD_CARD
,
1788 static int do_scan_async(void *_data
)
1790 struct async_scan_data
*data
= _data
;
1791 do_scsi_scan_host(data
->shost
);
1792 scsi_finish_async_scan(data
);
1797 * scsi_scan_host - scan the given adapter
1798 * @shost: adapter to scan
1800 void scsi_scan_host(struct Scsi_Host
*shost
)
1802 struct task_struct
*p
;
1803 struct async_scan_data
*data
;
1805 if (strncmp(scsi_scan_type
, "none", 4) == 0)
1808 data
= scsi_prep_async_scan(shost
);
1810 do_scsi_scan_host(shost
);
1814 p
= kthread_run(do_scan_async
, data
, "scsi_scan_%d", shost
->host_no
);
1815 if (unlikely(IS_ERR(p
)))
1816 do_scan_async(data
);
1818 EXPORT_SYMBOL(scsi_scan_host
);
1820 void scsi_forget_host(struct Scsi_Host
*shost
)
1822 struct scsi_device
*sdev
;
1823 unsigned long flags
;
1826 spin_lock_irqsave(shost
->host_lock
, flags
);
1827 list_for_each_entry(sdev
, &shost
->__devices
, siblings
) {
1828 if (sdev
->sdev_state
== SDEV_DEL
)
1830 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1831 __scsi_remove_device(sdev
);
1834 spin_unlock_irqrestore(shost
->host_lock
, flags
);
1838 * Function: scsi_get_host_dev()
1840 * Purpose: Create a scsi_device that points to the host adapter itself.
1842 * Arguments: SHpnt - Host that needs a scsi_device
1844 * Lock status: None assumed.
1846 * Returns: The scsi_device or NULL
1849 * Attach a single scsi_device to the Scsi_Host - this should
1850 * be made to look like a "pseudo-device" that points to the
1853 * Note - this device is not accessible from any high-level
1854 * drivers (including generics), which is probably not
1855 * optimal. We can add hooks later to attach
1857 struct scsi_device
*scsi_get_host_dev(struct Scsi_Host
*shost
)
1859 struct scsi_device
*sdev
= NULL
;
1860 struct scsi_target
*starget
;
1862 mutex_lock(&shost
->scan_mutex
);
1863 if (!scsi_host_scan_allowed(shost
))
1865 starget
= scsi_alloc_target(&shost
->shost_gendev
, 0, shost
->this_id
);
1869 sdev
= scsi_alloc_sdev(starget
, 0, NULL
);
1871 sdev
->sdev_gendev
.parent
= get_device(&starget
->dev
);
1874 scsi_target_reap(starget
);
1875 put_device(&starget
->dev
);
1877 mutex_unlock(&shost
->scan_mutex
);
1880 EXPORT_SYMBOL(scsi_get_host_dev
);
1883 * Function: scsi_free_host_dev()
1885 * Purpose: Free a scsi_device that points to the host adapter itself.
1887 * Arguments: SHpnt - Host that needs a scsi_device
1889 * Lock status: None assumed.
1895 void scsi_free_host_dev(struct scsi_device
*sdev
)
1897 BUG_ON(sdev
->id
!= sdev
->host
->this_id
);
1899 scsi_destroy_sdev(sdev
);
1901 EXPORT_SYMBOL(scsi_free_host_dev
);