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);
189 * For async scanning we need to wait for all the scans to complete before
190 * trying to mount the root fs. Otherwise non-modular drivers may not be ready
193 late_initcall(scsi_complete_async_scans);
197 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
198 * @sdev: scsi device to send command to
199 * @result: area to store the result of the MODE SENSE
202 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
203 * Called for BLIST_KEY devices.
205 static void scsi_unlock_floptical(struct scsi_device *sdev,
206 unsigned char *result)
208 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
210 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
211 scsi_cmd[0] = MODE_SENSE;
215 scsi_cmd[4] = 0x2a; /* size */
217 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
222 * scsi_alloc_sdev - allocate and setup a scsi_Device
225 * Allocate, initialize for io, and return a pointer to a scsi_Device.
226 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
227 * adds scsi_Device to the appropriate list.
230 * scsi_Device pointer, or NULL on failure.
232 static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
233 unsigned int lun, void *hostdata)
235 struct scsi_device *sdev;
236 int display_failure_msg = 1, ret;
237 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
239 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
244 sdev->vendor = scsi_null_device_strs;
245 sdev->model = scsi_null_device_strs;
246 sdev->rev = scsi_null_device_strs;
248 sdev->id = starget->id;
250 sdev->channel = starget->channel;
251 sdev->sdev_state = SDEV_CREATED;
252 INIT_LIST_HEAD(&sdev->siblings);
253 INIT_LIST_HEAD(&sdev->same_target_siblings);
254 INIT_LIST_HEAD(&sdev->cmd_list);
255 INIT_LIST_HEAD(&sdev->starved_entry);
256 spin_lock_init(&sdev->list_lock);
258 sdev->sdev_gendev.parent = get_device(&starget->dev);
259 sdev->sdev_target = starget;
261 /* usually NULL and set by ->slave_alloc instead */
262 sdev->hostdata = hostdata;
264 /* if the device needs this changing, it may do so in the
265 * slave_configure function */
266 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
269 * Some low level driver could use device->type
274 * Assume that the device will have handshaking problems,
275 * and then fix this field later if it turns out it
280 sdev->request_queue = scsi_alloc_queue(sdev);
281 if (!sdev->request_queue) {
282 /* release fn is set up in scsi_sysfs_device_initialise, so
283 * have to free and put manually here */
284 put_device(&starget->dev);
289 sdev->request_queue->queuedata = sdev;
290 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
292 scsi_sysfs_device_initialize(sdev);
294 if (shost->hostt->slave_alloc) {
295 ret = shost->hostt->slave_alloc(sdev);
298 * if LLDD reports slave not present, don't clutter
299 * console with alloc failure messages
302 display_failure_msg = 0;
303 goto out_device_destroy;
310 transport_destroy_device(&sdev->sdev_gendev);
311 put_device(&sdev->sdev_gendev);
313 if (display_failure_msg)
314 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
318 static void scsi_target_dev_release(struct device *dev)
320 struct device *parent = dev->parent;
321 struct scsi_target *starget = to_scsi_target(dev);
327 int scsi_is_target_device(const struct device *dev)
329 return dev->release == scsi_target_dev_release;
331 EXPORT_SYMBOL(scsi_is_target_device);
333 static struct scsi_target *__scsi_find_target(struct device *parent,
334 int channel, uint id)
336 struct scsi_target *starget, *found_starget = NULL;
337 struct Scsi_Host *shost = dev_to_shost(parent);
339 * Search for an existing target for this sdev.
341 list_for_each_entry(starget, &shost->__targets, siblings) {
342 if (starget->id == id &&
343 starget->channel == channel) {
344 found_starget = starget;
349 get_device(&found_starget->dev);
351 return found_starget;
355 * scsi_alloc_target - allocate a new or find an existing target
356 * @parent: parent of the target (need not be a scsi host)
357 * @channel: target channel number (zero if no channels)
358 * @id: target id number
360 * Return an existing target if one exists, provided it hasn't already
361 * gone into STARGET_DEL state, otherwise allocate a new target.
363 * The target is returned with an incremented reference, so the caller
364 * is responsible for both reaping and doing a last put
366 static struct scsi_target *scsi_alloc_target(struct device *parent,
367 int channel, uint id)
369 struct Scsi_Host *shost = dev_to_shost(parent);
370 struct device *dev = NULL;
372 const int size = sizeof(struct scsi_target)
373 + shost->transportt->target_size;
374 struct scsi_target *starget;
375 struct scsi_target *found_target;
378 starget = kzalloc(size, GFP_KERNEL);
380 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
384 device_initialize(dev);
385 starget->reap_ref = 1;
386 dev->parent = get_device(parent);
387 dev->release = scsi_target_dev_release;
388 sprintf(dev->bus_id, "target%d:%d:%d",
389 shost->host_no, channel, id);
391 starget->channel = channel;
392 INIT_LIST_HEAD(&starget->siblings);
393 INIT_LIST_HEAD(&starget->devices);
394 starget->state = STARGET_RUNNING;
395 starget->scsi_level = SCSI_2;
397 spin_lock_irqsave(shost->host_lock, flags);
399 found_target = __scsi_find_target(parent, channel, id);
403 list_add_tail(&starget->siblings, &shost->__targets);
404 spin_unlock_irqrestore(shost->host_lock, flags);
405 /* allocate and add */
406 transport_setup_device(dev);
407 error = device_add(dev);
409 dev_err(dev, "target device_add failed, error %d\n", error);
410 spin_lock_irqsave(shost->host_lock, flags);
411 list_del_init(&starget->siblings);
412 spin_unlock_irqrestore(shost->host_lock, flags);
413 transport_destroy_device(dev);
418 transport_add_device(dev);
419 if (shost->hostt->target_alloc) {
420 error = shost->hostt->target_alloc(starget);
423 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
424 /* don't want scsi_target_reap to do the final
425 * put because it will be under the host lock */
427 scsi_target_reap(starget);
437 found_target->reap_ref++;
438 spin_unlock_irqrestore(shost->host_lock, flags);
439 if (found_target->state != STARGET_DEL) {
444 /* Unfortunately, we found a dying target; need to
445 * wait until it's dead before we can get a new one */
446 put_device(&found_target->dev);
447 flush_scheduled_work();
451 static void scsi_target_reap_usercontext(struct work_struct *work)
453 struct scsi_target *starget =
454 container_of(work, struct scsi_target, ew.work);
455 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
458 transport_remove_device(&starget->dev);
459 device_del(&starget->dev);
460 transport_destroy_device(&starget->dev);
461 spin_lock_irqsave(shost->host_lock, flags);
462 if (shost->hostt->target_destroy)
463 shost->hostt->target_destroy(starget);
464 list_del_init(&starget->siblings);
465 spin_unlock_irqrestore(shost->host_lock, flags);
466 put_device(&starget->dev);
470 * scsi_target_reap - check to see if target is in use and destroy if not
472 * @starget: target to be checked
474 * This is used after removing a LUN or doing a last put of the target
475 * it checks atomically that nothing is using the target and removes
478 void scsi_target_reap(struct scsi_target *starget)
480 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
483 spin_lock_irqsave(shost->host_lock, flags);
485 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
486 BUG_ON(starget->state == STARGET_DEL);
487 starget->state = STARGET_DEL;
488 spin_unlock_irqrestore(shost->host_lock, flags);
489 execute_in_process_context(scsi_target_reap_usercontext,
494 spin_unlock_irqrestore(shost->host_lock, flags);
500 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
501 * @s: INQUIRY result string to sanitize
502 * @len: length of the string
505 * The SCSI spec says that INQUIRY vendor, product, and revision
506 * strings must consist entirely of graphic ASCII characters,
507 * padded on the right with spaces. Since not all devices obey
508 * this rule, we will replace non-graphic or non-ASCII characters
509 * with spaces. Exception: a NUL character is interpreted as a
510 * string terminator, so all the following characters are set to
513 static void sanitize_inquiry_string(unsigned char *s, int len)
517 for (; len > 0; (--len, ++s)) {
520 if (terminated || *s < 0x20 || *s > 0x7e)
526 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
527 * @sdev: scsi_device to probe
528 * @inq_result: area to store the INQUIRY result
529 * @result_len: len of inq_result
530 * @bflags: store any bflags found here
533 * Probe the lun associated with @req using a standard SCSI INQUIRY;
535 * If the INQUIRY is successful, zero is returned and the
536 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
537 * are copied to the scsi_device any flags value is stored in *@bflags.
539 static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
540 int result_len, int *bflags)
542 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
543 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
544 int response_len = 0;
545 int pass, count, result;
546 struct scsi_sense_hdr sshdr;
550 /* Perform up to 3 passes. The first pass uses a conservative
551 * transfer length of 36 unless sdev->inquiry_len specifies a
552 * different value. */
553 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
554 try_inquiry_len = first_inquiry_len;
558 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
559 "scsi scan: INQUIRY pass %d length %d\n",
560 pass, try_inquiry_len));
562 /* Each pass gets up to three chances to ignore Unit Attention */
563 for (count = 0; count < 3; ++count) {
564 memset(scsi_cmd, 0, 6);
565 scsi_cmd[0] = INQUIRY;
566 scsi_cmd[4] = (unsigned char) try_inquiry_len;
568 memset(inq_result, 0, try_inquiry_len);
570 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
571 inq_result, try_inquiry_len, &sshdr,
572 HZ / 2 + HZ * scsi_inq_timeout, 3);
574 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
576 result ? "failed" : "successful", result));
580 * not-ready to ready transition [asc/ascq=0x28/0x0]
581 * or power-on, reset [asc/ascq=0x29/0x0], continue.
582 * INQUIRY should not yield UNIT_ATTENTION
583 * but many buggy devices do so anyway.
585 if ((driver_byte(result) & DRIVER_SENSE) &&
586 scsi_sense_valid(&sshdr)) {
587 if ((sshdr.sense_key == UNIT_ATTENTION) &&
588 ((sshdr.asc == 0x28) ||
589 (sshdr.asc == 0x29)) &&
598 sanitize_inquiry_string(&inq_result[8], 8);
599 sanitize_inquiry_string(&inq_result[16], 16);
600 sanitize_inquiry_string(&inq_result[32], 4);
602 response_len = inq_result[4] + 5;
603 if (response_len > 255)
604 response_len = first_inquiry_len; /* sanity */
607 * Get any flags for this device.
609 * XXX add a bflags to scsi_device, and replace the
610 * corresponding bit fields in scsi_device, so bflags
611 * need not be passed as an argument.
613 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
616 /* When the first pass succeeds we gain information about
617 * what larger transfer lengths might work. */
619 if (BLIST_INQUIRY_36 & *bflags)
620 next_inquiry_len = 36;
621 else if (BLIST_INQUIRY_58 & *bflags)
622 next_inquiry_len = 58;
623 else if (sdev->inquiry_len)
624 next_inquiry_len = sdev->inquiry_len;
626 next_inquiry_len = response_len;
628 /* If more data is available perform the second pass */
629 if (next_inquiry_len > try_inquiry_len) {
630 try_inquiry_len = next_inquiry_len;
636 } else if (pass == 2) {
637 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
638 "Consider BLIST_INQUIRY_36 for this device\n",
641 /* If this pass failed, the third pass goes back and transfers
642 * the same amount as we successfully got in the first pass. */
643 try_inquiry_len = first_inquiry_len;
648 /* If the last transfer attempt got an error, assume the
649 * peripheral doesn't exist or is dead. */
653 /* Don't report any more data than the device says is valid */
654 sdev->inquiry_len = min(try_inquiry_len, response_len);
657 * XXX Abort if the response length is less than 36? If less than
658 * 32, the lookup of the device flags (above) could be invalid,
659 * and it would be possible to take an incorrect action - we do
660 * not want to hang because of a short INQUIRY. On the flip side,
661 * if the device is spun down or becoming ready (and so it gives a
662 * short INQUIRY), an abort here prevents any further use of the
663 * device, including spin up.
665 * On the whole, the best approach seems to be to assume the first
666 * 36 bytes are valid no matter what the device says. That's
667 * better than copying < 36 bytes to the inquiry-result buffer
668 * and displaying garbage for the Vendor, Product, or Revision
671 if (sdev->inquiry_len < 36) {
672 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
673 " using 36\n", sdev->inquiry_len);
674 sdev->inquiry_len = 36;
678 * Related to the above issue:
680 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
681 * and if not ready, sent a START_STOP to start (maybe spin up) and
682 * then send the INQUIRY again, since the INQUIRY can change after
683 * a device is initialized.
685 * Ideally, start a device if explicitly asked to do so. This
686 * assumes that a device is spun up on power on, spun down on
687 * request, and then spun up on request.
691 * The scanning code needs to know the scsi_level, even if no
692 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
693 * non-zero LUNs can be scanned.
695 sdev->scsi_level = inq_result[2] & 0x07;
696 if (sdev->scsi_level >= 2 ||
697 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
699 sdev->sdev_target->scsi_level = sdev->scsi_level;
705 * scsi_add_lun - allocate and fully initialze a scsi_device
706 * @sdevscan: holds information to be stored in the new scsi_device
707 * @sdevnew: store the address of the newly allocated scsi_device
708 * @inq_result: holds the result of a previous INQUIRY to the LUN
709 * @bflags: black/white list flag
712 * Allocate and initialize a scsi_device matching sdevscan. Optionally
713 * set fields based on values in *@bflags. If @sdevnew is not
714 * NULL, store the address of the new scsi_device in *@sdevnew (needed
715 * when scanning a particular LUN).
718 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
719 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
721 static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
722 int *bflags, int async)
725 * XXX do not save the inquiry, since it can change underneath us,
726 * save just vendor/model/rev.
728 * Rather than save it and have an ioctl that retrieves the saved
729 * value, have an ioctl that executes the same INQUIRY code used
730 * in scsi_probe_lun, let user level programs doing INQUIRY
731 * scanning run at their own risk, or supply a user level program
732 * that can correctly scan.
736 * Copy at least 36 bytes of INQUIRY data, so that we don't
737 * dereference unallocated memory when accessing the Vendor,
738 * Product, and Revision strings. Badly behaved devices may set
739 * the INQUIRY Additional Length byte to a small value, indicating
740 * these strings are invalid, but often they contain plausible data
741 * nonetheless. It doesn't matter if the device sent < 36 bytes
742 * total, since scsi_probe_lun() initializes inq_result with 0s.
744 sdev->inquiry = kmemdup(inq_result,
745 max_t(size_t, sdev->inquiry_len, 36),
747 if (sdev->inquiry == NULL)
748 return SCSI_SCAN_NO_RESPONSE;
750 sdev->vendor = (char *) (sdev->inquiry + 8);
751 sdev->model = (char *) (sdev->inquiry + 16);
752 sdev->rev = (char *) (sdev->inquiry + 32);
754 if (*bflags & BLIST_ISROM) {
756 * It would be better to modify sdev->type, and set
757 * sdev->removable; this can now be done since
758 * print_inquiry has gone away.
760 inq_result[0] = TYPE_ROM;
761 inq_result[1] |= 0x80; /* removable */
762 } else if (*bflags & BLIST_NO_ULD_ATTACH)
763 sdev->no_uld_attach = 1;
765 switch (sdev->type = (inq_result[0] & 0x1f)) {
767 /* RBC devices can return SCSI-3 compliance and yet
768 * still not support REPORT LUNS, so make them act as
769 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
770 * specifically set */
771 if ((*bflags & BLIST_REPORTLUN2) == 0)
772 *bflags |= BLIST_NOREPORTLUN;
780 case TYPE_MEDIUM_CHANGER:
787 /* MMC devices can return SCSI-3 compliance and yet
788 * still not support REPORT LUNS, so make them act as
789 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
790 * specifically set */
791 if ((*bflags & BLIST_REPORTLUN2) == 0)
792 *bflags |= BLIST_NOREPORTLUN;
798 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
802 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
803 * spec says: The device server is capable of supporting the
804 * specified peripheral device type on this logical unit. However,
805 * the physical device is not currently connected to this logical
808 * The above is vague, as it implies that we could treat 001 and
809 * 011 the same. Stay compatible with previous code, and create a
810 * scsi_device for a PQ of 1
812 * Don't set the device offline here; rather let the upper
813 * level drivers eval the PQ to decide whether they should
814 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
817 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
818 sdev->removable = (0x80 & inq_result[1]) >> 7;
819 sdev->lockable = sdev->removable;
820 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
822 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
823 inq_result[56] & 0x04))
825 if (inq_result[7] & 0x60)
827 if (inq_result[7] & 0x10)
830 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
831 "ANSI: %d%s\n", scsi_device_type(sdev->type),
832 sdev->vendor, sdev->model, sdev->rev,
833 sdev->inq_periph_qual, inq_result[2] & 0x07,
834 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
840 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
841 !(*bflags & BLIST_NOTQ))
842 sdev->tagged_supported = 1;
844 * Some devices (Texel CD ROM drives) have handshaking problems
845 * when used with the Seagate controllers. borken is initialized
846 * to 1, and then set it to 0 here.
848 if ((*bflags & BLIST_BORKEN) == 0)
852 * Apparently some really broken devices (contrary to the SCSI
853 * standards) need to be selected without asserting ATN
855 if (*bflags & BLIST_SELECT_NO_ATN)
856 sdev->select_no_atn = 1;
859 * Maximum 512 sector transfer length
860 * broken RA4x00 Compaq Disk Array
862 if (*bflags & BLIST_MAX_512)
863 blk_queue_max_sectors(sdev->request_queue, 512);
866 * Some devices may not want to have a start command automatically
867 * issued when a device is added.
869 if (*bflags & BLIST_NOSTARTONADD)
870 sdev->no_start_on_add = 1;
872 if (*bflags & BLIST_SINGLELUN)
873 sdev->single_lun = 1;
876 sdev->use_10_for_rw = 1;
878 if (*bflags & BLIST_MS_SKIP_PAGE_08)
879 sdev->skip_ms_page_8 = 1;
881 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
882 sdev->skip_ms_page_3f = 1;
884 if (*bflags & BLIST_USE_10_BYTE_MS)
885 sdev->use_10_for_ms = 1;
887 /* set the device running here so that slave configure
889 scsi_device_set_state(sdev, SDEV_RUNNING);
891 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
892 sdev->use_192_bytes_for_3f = 1;
894 if (*bflags & BLIST_NOT_LOCKABLE)
897 if (*bflags & BLIST_RETRY_HWERROR)
898 sdev->retry_hwerror = 1;
900 transport_configure_device(&sdev->sdev_gendev);
902 if (sdev->host->hostt->slave_configure) {
903 int ret = sdev->host->hostt->slave_configure(sdev);
906 * if LLDD reports slave not present, don't clutter
907 * console with alloc failure messages
910 sdev_printk(KERN_ERR, sdev,
911 "failed to configure device\n");
913 return SCSI_SCAN_NO_RESPONSE;
918 * Ok, the device is now all set up, we can
919 * register it and tell the rest of the kernel
922 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
923 return SCSI_SCAN_NO_RESPONSE;
925 return SCSI_SCAN_LUN_PRESENT;
928 static inline void scsi_destroy_sdev(struct scsi_device *sdev)
930 scsi_device_set_state(sdev, SDEV_DEL);
931 if (sdev->host->hostt->slave_destroy)
932 sdev->host->hostt->slave_destroy(sdev);
933 transport_destroy_device(&sdev->sdev_gendev);
934 put_device(&sdev->sdev_gendev);
937 #ifdef CONFIG_SCSI_LOGGING
939 * scsi_inq_str - print INQUIRY data from min to max index,
940 * strip trailing whitespace
941 * @buf: Output buffer with at least end-first+1 bytes of space
942 * @inq: Inquiry buffer (input)
943 * @first: Offset of string into inq
944 * @end: Index after last character in inq
946 static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
947 unsigned first, unsigned end)
949 unsigned term = 0, idx;
951 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
952 if (inq[idx+first] > ' ') {
953 buf[idx] = inq[idx+first];
965 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
966 * @starget: pointer to target device structure
967 * @lun: LUN of target device
968 * @sdevscan: probe the LUN corresponding to this scsi_device
969 * @sdevnew: store the value of any new scsi_device allocated
970 * @bflagsp: store bflags here if not NULL
973 * Call scsi_probe_lun, if a LUN with an attached device is found,
974 * allocate and set it up by calling scsi_add_lun.
977 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
978 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
979 * attached at the LUN
980 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
982 static int scsi_probe_and_add_lun(struct scsi_target *starget,
983 uint lun, int *bflagsp,
984 struct scsi_device **sdevp, int rescan,
987 struct scsi_device *sdev;
988 unsigned char *result;
989 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
990 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
993 * The rescan flag is used as an optimization, the first scan of a
994 * host adapter calls into here with rescan == 0.
996 sdev = scsi_device_lookup_by_target(starget, lun);
998 if (rescan || sdev->sdev_state != SDEV_CREATED) {
999 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1000 "scsi scan: device exists on %s\n",
1001 sdev->sdev_gendev.bus_id));
1005 scsi_device_put(sdev);
1008 *bflagsp = scsi_get_device_flags(sdev,
1011 return SCSI_SCAN_LUN_PRESENT;
1013 scsi_device_put(sdev);
1015 sdev = scsi_alloc_sdev(starget, lun, hostdata);
1019 result = kmalloc(result_len, GFP_ATOMIC |
1020 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
1024 if (scsi_probe_lun(sdev, result, result_len, &bflags))
1025 goto out_free_result;
1030 * result contains valid SCSI INQUIRY data.
1032 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
1034 * For a Peripheral qualifier 3 (011b), the SCSI
1035 * spec says: The device server is not capable of
1036 * supporting a physical device on this logical
1039 * For disks, this implies that there is no
1040 * logical disk configured at sdev->lun, but there
1041 * is a target id responding.
1043 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1044 " peripheral qualifier of 3, device not"
1047 SCSI_LOG_SCAN_BUS(1, {
1048 unsigned char vend[9];
1049 unsigned char mod[17];
1051 sdev_printk(KERN_INFO, sdev,
1052 "scsi scan: consider passing scsi_mod."
1053 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1054 scsi_inq_str(vend, result, 8, 16),
1055 scsi_inq_str(mod, result, 16, 32));
1059 res = SCSI_SCAN_TARGET_PRESENT;
1060 goto out_free_result;
1064 * Some targets may set slight variations of PQ and PDT to signal
1065 * that no LUN is present, so don't add sdev in these cases.
1066 * Two specific examples are:
1067 * 1) NetApp targets: return PQ=1, PDT=0x1f
1068 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1069 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1072 * 1) SCSI SPC-3, pp. 145-146
1073 * PQ=1: "A peripheral device having the specified peripheral
1074 * device type is not connected to this logical unit. However, the
1075 * device server is capable of supporting the specified peripheral
1076 * device type on this logical unit."
1077 * PDT=0x1f: "Unknown or no device type"
1078 * 2) USB UFI 1.0, p. 20
1079 * PDT=00h Direct-access device (floppy)
1080 * PDT=1Fh none (no FDD connected to the requested logical unit)
1082 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1083 (result[0] & 0x1f) == 0x1f) {
1084 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1085 "scsi scan: peripheral device type"
1086 " of 31, no device added\n"));
1087 res = SCSI_SCAN_TARGET_PRESENT;
1088 goto out_free_result;
1091 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
1092 if (res == SCSI_SCAN_LUN_PRESENT) {
1093 if (bflags & BLIST_KEY) {
1095 scsi_unlock_floptical(sdev, result);
1102 if (res == SCSI_SCAN_LUN_PRESENT) {
1104 if (scsi_device_get(sdev) == 0) {
1107 __scsi_remove_device(sdev);
1108 res = SCSI_SCAN_NO_RESPONSE;
1112 scsi_destroy_sdev(sdev);
1118 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1119 * @starget: pointer to target structure to scan
1120 * @bflags: black/white list flag for LUN 0
1123 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1124 * scanned) to some maximum lun until a LUN is found with no device
1125 * attached. Use the bflags to figure out any oddities.
1127 * Modifies sdevscan->lun.
1129 static void scsi_sequential_lun_scan(struct scsi_target *starget,
1130 int bflags, int scsi_level, int rescan)
1132 unsigned int sparse_lun, lun, max_dev_lun;
1133 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1135 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1136 "%s\n", starget->dev.bus_id));
1138 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1140 * If this device is known to support sparse multiple units,
1141 * override the other settings, and scan all of them. Normally,
1142 * SCSI-3 devices should be scanned via the REPORT LUNS.
1144 if (bflags & BLIST_SPARSELUN) {
1145 max_dev_lun = shost->max_lun;
1151 * If less than SCSI_1_CSS, and no special lun scaning, stop
1152 * scanning; this matches 2.4 behaviour, but could just be a bug
1153 * (to continue scanning a SCSI_1_CSS device).
1155 * This test is broken. We might not have any device on lun0 for
1156 * a sparselun device, and if that's the case then how would we
1157 * know the real scsi_level, eh? It might make sense to just not
1158 * scan any SCSI_1 device for non-0 luns, but that check would best
1159 * go into scsi_alloc_sdev() and just have it return null when asked
1160 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1162 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1163 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1168 * If this device is known to support multiple units, override
1169 * the other settings, and scan all of them.
1171 if (bflags & BLIST_FORCELUN)
1172 max_dev_lun = shost->max_lun;
1174 * REGAL CDC-4X: avoid hang after LUN 4
1176 if (bflags & BLIST_MAX5LUN)
1177 max_dev_lun = min(5U, max_dev_lun);
1179 * Do not scan SCSI-2 or lower device past LUN 7, unless
1182 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1183 max_dev_lun = min(8U, max_dev_lun);
1186 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1187 * until we reach the max, or no LUN is found and we are not
1190 for (lun = 1; lun < max_dev_lun; ++lun)
1191 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1192 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1198 * scsilun_to_int: convert a scsi_lun to an int
1199 * @scsilun: struct scsi_lun to be converted.
1202 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1203 * integer, and return the result. The caller must check for
1204 * truncation before using this function.
1207 * The struct scsi_lun is assumed to be four levels, with each level
1208 * effectively containing a SCSI byte-ordered (big endian) short; the
1209 * addressing bits of each level are ignored (the highest two bits).
1210 * For a description of the LUN format, post SCSI-3 see the SCSI
1211 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1213 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1214 * the integer: 0x0b030a04
1216 static int scsilun_to_int(struct scsi_lun *scsilun)
1222 for (i = 0; i < sizeof(lun); i += 2)
1223 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1224 scsilun->scsi_lun[i + 1]) << (i * 8));
1229 * int_to_scsilun: reverts an int into a scsi_lun
1230 * @int: integer to be reverted
1231 * @scsilun: struct scsi_lun to be set.
1234 * Reverts the functionality of the scsilun_to_int, which packed
1235 * an 8-byte lun value into an int. This routine unpacks the int
1236 * back into the lun value.
1237 * Note: the scsilun_to_int() routine does not truly handle all
1238 * 8bytes of the lun value. This functions restores only as much
1239 * as was set by the routine.
1242 * Given an integer : 0x0b030a04, this function returns a
1243 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1246 void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1250 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1252 for (i = 0; i < sizeof(lun); i += 2) {
1253 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1254 scsilun->scsi_lun[i+1] = lun & 0xFF;
1258 EXPORT_SYMBOL(int_to_scsilun);
1261 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1262 * @sdevscan: scan the host, channel, and id of this scsi_device
1265 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1266 * command, and scan the resulting list of LUNs by calling
1267 * scsi_probe_and_add_lun.
1269 * Modifies sdevscan->lun.
1272 * 0: scan completed (or no memory, so further scanning is futile)
1273 * 1: no report lun scan, or not configured
1275 static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
1279 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1280 unsigned int length;
1282 unsigned int num_luns;
1283 unsigned int retries;
1285 struct scsi_lun *lunp, *lun_data;
1287 struct scsi_sense_hdr sshdr;
1288 struct scsi_device *sdev;
1289 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1293 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1294 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1295 * support more than 8 LUNs.
1297 if (bflags & BLIST_NOREPORTLUN)
1299 if (starget->scsi_level < SCSI_2 &&
1300 starget->scsi_level != SCSI_UNKNOWN)
1302 if (starget->scsi_level < SCSI_3 &&
1303 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
1305 if (bflags & BLIST_NOLUN)
1308 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1309 sdev = scsi_alloc_sdev(starget, 0, NULL);
1312 if (scsi_device_get(sdev))
1316 sprintf(devname, "host %d channel %d id %d",
1317 shost->host_no, sdev->channel, sdev->id);
1320 * Allocate enough to hold the header (the same size as one scsi_lun)
1321 * plus the max number of luns we are requesting.
1323 * Reallocating and trying again (with the exact amount we need)
1324 * would be nice, but then we need to somehow limit the size
1325 * allocated based on the available memory and the limits of
1326 * kmalloc - we don't want a kmalloc() failure of a huge value to
1327 * prevent us from finding any LUNs on this target.
1329 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1330 lun_data = kmalloc(length, GFP_ATOMIC |
1331 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
1333 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1337 scsi_cmd[0] = REPORT_LUNS;
1340 * bytes 1 - 5: reserved, set to zero.
1342 memset(&scsi_cmd[1], 0, 5);
1345 * bytes 6 - 9: length of the command.
1347 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1348 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1349 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1350 scsi_cmd[9] = (unsigned char) length & 0xff;
1352 scsi_cmd[10] = 0; /* reserved */
1353 scsi_cmd[11] = 0; /* control */
1356 * We can get a UNIT ATTENTION, for example a power on/reset, so
1357 * retry a few times (like sd.c does for TEST UNIT READY).
1358 * Experience shows some combinations of adapter/devices get at
1359 * least two power on/resets.
1361 * Illegal requests (for devices that do not support REPORT LUNS)
1362 * should come through as a check condition, and will not generate
1365 for (retries = 0; retries < 3; retries++) {
1366 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1367 " REPORT LUNS to %s (try %d)\n", devname,
1370 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1371 lun_data, length, &sshdr,
1372 SCSI_TIMEOUT + 4 * HZ, 3);
1374 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1375 " %s (try %d) result 0x%x\n", result
1376 ? "failed" : "successful", retries, result));
1379 else if (scsi_sense_valid(&sshdr)) {
1380 if (sshdr.sense_key != UNIT_ATTENTION)
1387 * The device probably does not support a REPORT LUN command
1394 * Get the length from the first four bytes of lun_data.
1396 data = (u8 *) lun_data->scsi_lun;
1397 length = ((data[0] << 24) | (data[1] << 16) |
1398 (data[2] << 8) | (data[3] << 0));
1400 num_luns = (length / sizeof(struct scsi_lun));
1401 if (num_luns > max_scsi_report_luns) {
1402 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1403 " of %d luns reported, try increasing"
1404 " max_scsi_report_luns.\n", devname,
1405 max_scsi_report_luns, num_luns);
1406 num_luns = max_scsi_report_luns;
1409 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1410 "scsi scan: REPORT LUN scan\n"));
1413 * Scan the luns in lun_data. The entry at offset 0 is really
1414 * the header, so start at 1 and go up to and including num_luns.
1416 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1417 lun = scsilun_to_int(lunp);
1420 * Check if the unused part of lunp is non-zero, and so
1421 * does not fit in lun.
1423 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1427 * Output an error displaying the LUN in byte order,
1428 * this differs from what linux would print for the
1429 * integer LUN value.
1431 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1432 data = (char *)lunp->scsi_lun;
1433 for (i = 0; i < sizeof(struct scsi_lun); i++)
1434 printk("%02x", data[i]);
1435 printk(" has a LUN larger than currently supported.\n");
1436 } else if (lun > sdev->host->max_lun) {
1437 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1438 " than allowed by the host adapter\n",
1443 res = scsi_probe_and_add_lun(starget,
1444 lun, NULL, NULL, rescan, NULL);
1445 if (res == SCSI_SCAN_NO_RESPONSE) {
1447 * Got some results, but now none, abort.
1449 sdev_printk(KERN_ERR, sdev,
1450 "Unexpected response"
1451 " from lun %d while scanning, scan"
1461 scsi_device_put(sdev);
1462 if (sdev->sdev_state == SDEV_CREATED)
1464 * the sdev we used didn't appear in the report luns scan
1466 scsi_destroy_sdev(sdev);
1470 struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1471 uint id, uint lun, void *hostdata)
1473 struct scsi_device *sdev = ERR_PTR(-ENODEV);
1474 struct device *parent = &shost->shost_gendev;
1475 struct scsi_target *starget;
1477 if (strncmp(scsi_scan_type, "none", 4) == 0)
1478 return ERR_PTR(-ENODEV);
1480 if (!shost->async_scan)
1481 scsi_complete_async_scans();
1483 starget = scsi_alloc_target(parent, channel, id);
1485 return ERR_PTR(-ENOMEM);
1487 mutex_lock(&shost->scan_mutex);
1488 if (scsi_host_scan_allowed(shost))
1489 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
1490 mutex_unlock(&shost->scan_mutex);
1491 scsi_target_reap(starget);
1492 put_device(&starget->dev);
1496 EXPORT_SYMBOL(__scsi_add_device);
1498 int scsi_add_device(struct Scsi_Host *host, uint channel,
1499 uint target, uint lun)
1501 struct scsi_device *sdev =
1502 __scsi_add_device(host, channel, target, lun, NULL);
1504 return PTR_ERR(sdev);
1506 scsi_device_put(sdev);
1509 EXPORT_SYMBOL(scsi_add_device);
1511 void scsi_rescan_device(struct device *dev)
1513 struct scsi_driver *drv;
1518 drv = to_scsi_driver(dev->driver);
1519 if (try_module_get(drv->owner)) {
1522 module_put(drv->owner);
1525 EXPORT_SYMBOL(scsi_rescan_device);
1527 static void __scsi_scan_target(struct device *parent, unsigned int channel,
1528 unsigned int id, unsigned int lun, int rescan)
1530 struct Scsi_Host *shost = dev_to_shost(parent);
1533 struct scsi_target *starget;
1535 if (shost->this_id == id)
1537 * Don't scan the host adapter
1541 starget = scsi_alloc_target(parent, channel, id);
1545 if (lun != SCAN_WILD_CARD) {
1547 * Scan for a specific host/chan/id/lun.
1549 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1554 * Scan LUN 0, if there is some response, scan further. Ideally, we
1555 * would not configure LUN 0 until all LUNs are scanned.
1557 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1558 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1559 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1561 * The REPORT LUN did not scan the target,
1562 * do a sequential scan.
1564 scsi_sequential_lun_scan(starget, bflags,
1565 starget->scsi_level, rescan);
1569 /* now determine if the target has any children at all
1570 * and if not, nuke it */
1571 scsi_target_reap(starget);
1573 put_device(&starget->dev);
1577 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1579 * @parent: host to scan
1580 * @channel: channel to scan
1581 * @id: target id to scan
1582 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1583 * @rescan: passed to LUN scanning routines
1586 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1587 * and possibly all LUNs on the target id.
1589 * First try a REPORT LUN scan, if that does not scan the target, do a
1590 * sequential scan of LUNs on the target id.
1592 void scsi_scan_target(struct device *parent, unsigned int channel,
1593 unsigned int id, unsigned int lun, int rescan)
1595 struct Scsi_Host *shost = dev_to_shost(parent);
1597 if (strncmp(scsi_scan_type, "none", 4) == 0)
1600 if (!shost->async_scan)
1601 scsi_complete_async_scans();
1603 mutex_lock(&shost->scan_mutex);
1604 if (scsi_host_scan_allowed(shost))
1605 __scsi_scan_target(parent, channel, id, lun, rescan);
1606 mutex_unlock(&shost->scan_mutex);
1608 EXPORT_SYMBOL(scsi_scan_target);
1610 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1611 unsigned int id, unsigned int lun, int rescan)
1615 if (id == SCAN_WILD_CARD)
1616 for (id = 0; id < shost->max_id; ++id) {
1618 * XXX adapter drivers when possible (FCP, iSCSI)
1619 * could modify max_id to match the current max,
1620 * not the absolute max.
1622 * XXX add a shost id iterator, so for example,
1623 * the FC ID can be the same as a target id
1624 * without a huge overhead of sparse id's.
1626 if (shost->reverse_ordering)
1628 * Scan from high to low id.
1630 order_id = shost->max_id - id - 1;
1633 __scsi_scan_target(&shost->shost_gendev, channel,
1634 order_id, lun, rescan);
1637 __scsi_scan_target(&shost->shost_gendev, channel,
1641 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1642 unsigned int id, unsigned int lun, int rescan)
1644 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1646 __FUNCTION__, channel, id, lun));
1648 if (!shost->async_scan)
1649 scsi_complete_async_scans();
1651 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1652 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1653 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1656 mutex_lock(&shost->scan_mutex);
1657 if (scsi_host_scan_allowed(shost)) {
1658 if (channel == SCAN_WILD_CARD)
1659 for (channel = 0; channel <= shost->max_channel;
1661 scsi_scan_channel(shost, channel, id, lun,
1664 scsi_scan_channel(shost, channel, id, lun, rescan);
1666 mutex_unlock(&shost->scan_mutex);
1671 static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1673 struct scsi_device *sdev;
1674 shost_for_each_device(sdev, shost) {
1675 if (scsi_sysfs_add_sdev(sdev) != 0)
1676 scsi_destroy_sdev(sdev);
1681 * scsi_prep_async_scan - prepare for an async scan
1682 * @shost: the host which will be scanned
1683 * Returns: a cookie to be passed to scsi_finish_async_scan()
1685 * Tells the midlayer this host is going to do an asynchronous scan.
1686 * It reserves the host's position in the scanning list and ensures
1687 * that other asynchronous scans started after this one won't affect the
1688 * ordering of the discovered devices.
1690 static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1692 struct async_scan_data *data;
1694 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1697 if (shost->async_scan) {
1698 printk("%s called twice for host %d", __FUNCTION__,
1704 data = kmalloc(sizeof(*data), GFP_KERNEL);
1707 data->shost = scsi_host_get(shost);
1710 init_completion(&data->prev_finished);
1712 spin_lock(&async_scan_lock);
1713 shost->async_scan = 1;
1714 if (list_empty(&scanning_hosts))
1715 complete(&data->prev_finished);
1716 list_add_tail(&data->list, &scanning_hosts);
1717 spin_unlock(&async_scan_lock);
1727 * scsi_finish_async_scan - asynchronous scan has finished
1728 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1730 * All the devices currently attached to this host have been found.
1731 * This function announces all the devices it has found to the rest
1734 static void scsi_finish_async_scan(struct async_scan_data *data)
1736 struct Scsi_Host *shost;
1741 shost = data->shost;
1742 if (!shost->async_scan) {
1743 printk("%s called twice for host %d", __FUNCTION__,
1749 wait_for_completion(&data->prev_finished);
1751 scsi_sysfs_add_devices(shost);
1753 spin_lock(&async_scan_lock);
1754 shost->async_scan = 0;
1755 list_del(&data->list);
1756 if (!list_empty(&scanning_hosts)) {
1757 struct async_scan_data *next = list_entry(scanning_hosts.next,
1758 struct async_scan_data, list);
1759 complete(&next->prev_finished);
1761 spin_unlock(&async_scan_lock);
1763 scsi_host_put(shost);
1767 static void do_scsi_scan_host(struct Scsi_Host *shost)
1769 if (shost->hostt->scan_finished) {
1770 unsigned long start = jiffies;
1771 if (shost->hostt->scan_start)
1772 shost->hostt->scan_start(shost);
1774 while (!shost->hostt->scan_finished(shost, jiffies - start))
1777 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1782 static int do_scan_async(void *_data)
1784 struct async_scan_data *data = _data;
1785 do_scsi_scan_host(data->shost);
1786 scsi_finish_async_scan(data);
1791 * scsi_scan_host - scan the given adapter
1792 * @shost: adapter to scan
1794 void scsi_scan_host(struct Scsi_Host *shost)
1796 struct async_scan_data *data;
1798 if (strncmp(scsi_scan_type, "none", 4) == 0)
1801 data = scsi_prep_async_scan(shost);
1803 do_scsi_scan_host(shost);
1807 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1809 EXPORT_SYMBOL(scsi_scan_host);
1811 void scsi_forget_host(struct Scsi_Host *shost)
1813 struct scsi_device *sdev;
1814 unsigned long flags;
1817 spin_lock_irqsave(shost->host_lock, flags);
1818 list_for_each_entry(sdev, &shost->__devices, siblings) {
1819 if (sdev->sdev_state == SDEV_DEL)
1821 spin_unlock_irqrestore(shost->host_lock, flags);
1822 __scsi_remove_device(sdev);
1825 spin_unlock_irqrestore(shost->host_lock, flags);
1829 * Function: scsi_get_host_dev()
1831 * Purpose: Create a scsi_device that points to the host adapter itself.
1833 * Arguments: SHpnt - Host that needs a scsi_device
1835 * Lock status: None assumed.
1837 * Returns: The scsi_device or NULL
1840 * Attach a single scsi_device to the Scsi_Host - this should
1841 * be made to look like a "pseudo-device" that points to the
1844 * Note - this device is not accessible from any high-level
1845 * drivers (including generics), which is probably not
1846 * optimal. We can add hooks later to attach
1848 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1850 struct scsi_device *sdev = NULL;
1851 struct scsi_target *starget;
1853 mutex_lock(&shost->scan_mutex);
1854 if (!scsi_host_scan_allowed(shost))
1856 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1860 sdev = scsi_alloc_sdev(starget, 0, NULL);
1862 sdev->sdev_gendev.parent = get_device(&starget->dev);
1865 scsi_target_reap(starget);
1866 put_device(&starget->dev);
1868 mutex_unlock(&shost->scan_mutex);
1871 EXPORT_SYMBOL(scsi_get_host_dev);
1874 * Function: scsi_free_host_dev()
1876 * Purpose: Free a scsi_device that points to the host adapter itself.
1878 * Arguments: SHpnt - Host that needs a scsi_device
1880 * Lock status: None assumed.
1886 void scsi_free_host_dev(struct scsi_device *sdev)
1888 BUG_ON(sdev->id != sdev->host->this_id);
1890 scsi_destroy_sdev(sdev);
1892 EXPORT_SYMBOL(scsi_free_host_dev);