4 * Copyright (C) 2000 Eric Youngdale,
5 * Copyright (C) 2002 Patrick Mansfield
7 * The general scanning/probing algorithm is as follows, exceptions are
8 * made to it depending on device specific flags, compilation options, and
9 * global variable (boot or module load time) settings.
11 * A specific LUN is scanned via an INQUIRY command; if the LUN has a
12 * device attached, a scsi_device is allocated and setup for it.
14 * For every id of every channel on the given host:
16 * Scan LUN 0; if the target responds to LUN 0 (even if there is no
17 * device or storage attached to LUN 0):
19 * If LUN 0 has a device attached, allocate and setup a
22 * If target is SCSI-3 or up, issue a REPORT LUN, and scan
23 * all of the LUNs returned by the REPORT LUN; else,
24 * sequentially scan LUNs up until some maximum is reached,
25 * or a LUN is seen that cannot have a device attached to it.
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/init.h>
31 #include <linux/blkdev.h>
32 #include <linux/delay.h>
33 #include <linux/kthread.h>
34 #include <linux/spinlock.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_cmnd.h>
38 #include <scsi/scsi_device.h>
39 #include <scsi/scsi_driver.h>
40 #include <scsi/scsi_devinfo.h>
41 #include <scsi/scsi_host.h>
42 #include <scsi/scsi_transport.h>
43 #include <scsi/scsi_eh.h>
45 #include "scsi_priv.h"
46 #include "scsi_logging.h"
48 #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
49 " SCSI scanning, some SCSI devices might not be configured\n"
54 #define SCSI_TIMEOUT (2*HZ)
57 * Prefix values for the SCSI id's (stored in driverfs name field)
59 #define SCSI_UID_SER_NUM 'S'
60 #define SCSI_UID_UNKNOWN 'Z'
63 * Return values of some of the scanning functions.
65 * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
66 * includes allocation or general failures preventing IO from being sent.
68 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
71 * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
74 #define SCSI_SCAN_NO_RESPONSE 0
75 #define SCSI_SCAN_TARGET_PRESENT 1
76 #define SCSI_SCAN_LUN_PRESENT 2
78 static const char *scsi_null_device_strs = "nullnullnullnull";
80 #define MAX_SCSI_LUNS 512
82 #ifdef CONFIG_SCSI_MULTI_LUN
83 static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
85 static unsigned int max_scsi_luns = 1;
88 module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR);
89 MODULE_PARM_DESC(max_luns,
90 "last scsi LUN (should be between 1 and 2^32-1)");
92 #ifdef CONFIG_SCSI_SCAN_ASYNC
93 #define SCSI_SCAN_TYPE_DEFAULT "async"
95 #define SCSI_SCAN_TYPE_DEFAULT "sync"
98 static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT;
100 module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
101 MODULE_PARM_DESC(scan, "sync, async or none");
104 * max_scsi_report_luns: the maximum number of LUNS that will be
105 * returned from the REPORT LUNS command. 8 times this value must
106 * be allocated. In theory this could be up to an 8 byte value, but
107 * in practice, the maximum number of LUNs suppored by any device
110 static unsigned int max_scsi_report_luns = 511;
112 module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
113 MODULE_PARM_DESC(max_report_luns,
114 "REPORT LUNS maximum number of LUNS received (should be"
115 " between 1 and 16384)");
117 static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
119 module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
120 MODULE_PARM_DESC(inq_timeout,
121 "Timeout (in seconds) waiting for devices to answer INQUIRY."
122 " Default is 5. Some non-compliant devices need more.");
124 static DEFINE_SPINLOCK(async_scan_lock);
125 static LIST_HEAD(scanning_hosts);
127 struct async_scan_data {
128 struct list_head list;
129 struct Scsi_Host *shost;
130 struct completion prev_finished;
134 * scsi_complete_async_scans - Wait for asynchronous scans to complete
136 * Asynchronous scans add themselves to the scanning_hosts list. Once
137 * that list is empty, we know that the scans are complete. Rather than
138 * waking up periodically to check the state of the list, we pretend to be
139 * a scanning task by adding ourselves at the end of the list and going to
140 * sleep. When the task before us wakes us up, we take ourselves off the
143 int scsi_complete_async_scans(void)
145 struct async_scan_data *data;
148 if (list_empty(&scanning_hosts))
150 /* If we can't get memory immediately, that's OK. Just
151 * sleep a little. Even if we never get memory, the async
152 * scans will finish eventually.
154 data = kmalloc(sizeof(*data), GFP_KERNEL);
160 init_completion(&data->prev_finished);
162 spin_lock(&async_scan_lock);
163 /* Check that there's still somebody else on the list */
164 if (list_empty(&scanning_hosts))
166 list_add_tail(&data->list, &scanning_hosts);
167 spin_unlock(&async_scan_lock);
169 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
170 wait_for_completion(&data->prev_finished);
172 spin_lock(&async_scan_lock);
173 list_del(&data->list);
175 spin_unlock(&async_scan_lock);
182 /* Only exported for the benefit of scsi_wait_scan */
183 EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
187 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
188 * @sdev: scsi device to send command to
189 * @result: area to store the result of the MODE SENSE
192 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
193 * Called for BLIST_KEY devices.
195 static void scsi_unlock_floptical(struct scsi_device *sdev,
196 unsigned char *result)
198 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
200 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
201 scsi_cmd[0] = MODE_SENSE;
205 scsi_cmd[4] = 0x2a; /* size */
207 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
212 * scsi_alloc_sdev - allocate and setup a scsi_Device
215 * Allocate, initialize for io, and return a pointer to a scsi_Device.
216 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
217 * adds scsi_Device to the appropriate list.
220 * scsi_Device pointer, or NULL on failure.
222 static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
223 unsigned int lun, void *hostdata)
225 struct scsi_device *sdev;
226 int display_failure_msg = 1, ret;
227 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
229 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
234 sdev->vendor = scsi_null_device_strs;
235 sdev->model = scsi_null_device_strs;
236 sdev->rev = scsi_null_device_strs;
238 sdev->id = starget->id;
240 sdev->channel = starget->channel;
241 sdev->sdev_state = SDEV_CREATED;
242 INIT_LIST_HEAD(&sdev->siblings);
243 INIT_LIST_HEAD(&sdev->same_target_siblings);
244 INIT_LIST_HEAD(&sdev->cmd_list);
245 INIT_LIST_HEAD(&sdev->starved_entry);
246 spin_lock_init(&sdev->list_lock);
248 sdev->sdev_gendev.parent = get_device(&starget->dev);
249 sdev->sdev_target = starget;
251 /* usually NULL and set by ->slave_alloc instead */
252 sdev->hostdata = hostdata;
254 /* if the device needs this changing, it may do so in the
255 * slave_configure function */
256 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
259 * Some low level driver could use device->type
264 * Assume that the device will have handshaking problems,
265 * and then fix this field later if it turns out it
270 sdev->request_queue = scsi_alloc_queue(sdev);
271 if (!sdev->request_queue) {
272 /* release fn is set up in scsi_sysfs_device_initialise, so
273 * have to free and put manually here */
274 put_device(&starget->dev);
279 sdev->request_queue->queuedata = sdev;
280 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
282 scsi_sysfs_device_initialize(sdev);
284 if (shost->hostt->slave_alloc) {
285 ret = shost->hostt->slave_alloc(sdev);
288 * if LLDD reports slave not present, don't clutter
289 * console with alloc failure messages
292 display_failure_msg = 0;
293 goto out_device_destroy;
300 transport_destroy_device(&sdev->sdev_gendev);
301 put_device(&sdev->sdev_gendev);
303 if (display_failure_msg)
304 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
308 static void scsi_target_dev_release(struct device *dev)
310 struct device *parent = dev->parent;
311 struct scsi_target *starget = to_scsi_target(dev);
317 int scsi_is_target_device(const struct device *dev)
319 return dev->release == scsi_target_dev_release;
321 EXPORT_SYMBOL(scsi_is_target_device);
323 static struct scsi_target *__scsi_find_target(struct device *parent,
324 int channel, uint id)
326 struct scsi_target *starget, *found_starget = NULL;
327 struct Scsi_Host *shost = dev_to_shost(parent);
329 * Search for an existing target for this sdev.
331 list_for_each_entry(starget, &shost->__targets, siblings) {
332 if (starget->id == id &&
333 starget->channel == channel) {
334 found_starget = starget;
339 get_device(&found_starget->dev);
341 return found_starget;
345 * scsi_alloc_target - allocate a new or find an existing target
346 * @parent: parent of the target (need not be a scsi host)
347 * @channel: target channel number (zero if no channels)
348 * @id: target id number
350 * Return an existing target if one exists, provided it hasn't already
351 * gone into STARGET_DEL state, otherwise allocate a new target.
353 * The target is returned with an incremented reference, so the caller
354 * is responsible for both reaping and doing a last put
356 static struct scsi_target *scsi_alloc_target(struct device *parent,
357 int channel, uint id)
359 struct Scsi_Host *shost = dev_to_shost(parent);
360 struct device *dev = NULL;
362 const int size = sizeof(struct scsi_target)
363 + shost->transportt->target_size;
364 struct scsi_target *starget;
365 struct scsi_target *found_target;
368 starget = kzalloc(size, GFP_KERNEL);
370 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
374 device_initialize(dev);
375 starget->reap_ref = 1;
376 dev->parent = get_device(parent);
377 dev->release = scsi_target_dev_release;
378 sprintf(dev->bus_id, "target%d:%d:%d",
379 shost->host_no, channel, id);
381 starget->channel = channel;
382 INIT_LIST_HEAD(&starget->siblings);
383 INIT_LIST_HEAD(&starget->devices);
384 starget->state = STARGET_RUNNING;
386 spin_lock_irqsave(shost->host_lock, flags);
388 found_target = __scsi_find_target(parent, channel, id);
392 list_add_tail(&starget->siblings, &shost->__targets);
393 spin_unlock_irqrestore(shost->host_lock, flags);
394 /* allocate and add */
395 transport_setup_device(dev);
396 error = device_add(dev);
398 dev_err(dev, "target device_add failed, error %d\n", error);
399 spin_lock_irqsave(shost->host_lock, flags);
400 list_del_init(&starget->siblings);
401 spin_unlock_irqrestore(shost->host_lock, flags);
402 transport_destroy_device(dev);
407 transport_add_device(dev);
408 if (shost->hostt->target_alloc) {
409 error = shost->hostt->target_alloc(starget);
412 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
413 /* don't want scsi_target_reap to do the final
414 * put because it will be under the host lock */
416 scsi_target_reap(starget);
426 found_target->reap_ref++;
427 spin_unlock_irqrestore(shost->host_lock, flags);
428 if (found_target->state != STARGET_DEL) {
433 /* Unfortunately, we found a dying target; need to
434 * wait until it's dead before we can get a new one */
435 put_device(&found_target->dev);
436 flush_scheduled_work();
440 static void scsi_target_reap_usercontext(void *data)
442 struct scsi_target *starget = data;
443 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
446 transport_remove_device(&starget->dev);
447 device_del(&starget->dev);
448 transport_destroy_device(&starget->dev);
449 spin_lock_irqsave(shost->host_lock, flags);
450 if (shost->hostt->target_destroy)
451 shost->hostt->target_destroy(starget);
452 list_del_init(&starget->siblings);
453 spin_unlock_irqrestore(shost->host_lock, flags);
454 put_device(&starget->dev);
458 * scsi_target_reap - check to see if target is in use and destroy if not
460 * @starget: target to be checked
462 * This is used after removing a LUN or doing a last put of the target
463 * it checks atomically that nothing is using the target and removes
466 void scsi_target_reap(struct scsi_target *starget)
468 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
471 spin_lock_irqsave(shost->host_lock, flags);
473 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
474 BUG_ON(starget->state == STARGET_DEL);
475 starget->state = STARGET_DEL;
476 spin_unlock_irqrestore(shost->host_lock, flags);
477 execute_in_process_context(scsi_target_reap_usercontext,
478 starget, &starget->ew);
482 spin_unlock_irqrestore(shost->host_lock, flags);
488 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
489 * @s: INQUIRY result string to sanitize
490 * @len: length of the string
493 * The SCSI spec says that INQUIRY vendor, product, and revision
494 * strings must consist entirely of graphic ASCII characters,
495 * padded on the right with spaces. Since not all devices obey
496 * this rule, we will replace non-graphic or non-ASCII characters
497 * with spaces. Exception: a NUL character is interpreted as a
498 * string terminator, so all the following characters are set to
501 static void sanitize_inquiry_string(unsigned char *s, int len)
505 for (; len > 0; (--len, ++s)) {
508 if (terminated || *s < 0x20 || *s > 0x7e)
514 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
515 * @sdev: scsi_device to probe
516 * @inq_result: area to store the INQUIRY result
517 * @result_len: len of inq_result
518 * @bflags: store any bflags found here
521 * Probe the lun associated with @req using a standard SCSI INQUIRY;
523 * If the INQUIRY is successful, zero is returned and the
524 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
525 * are copied to the scsi_device any flags value is stored in *@bflags.
527 static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
528 int result_len, int *bflags)
530 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
531 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
532 int response_len = 0;
533 int pass, count, result;
534 struct scsi_sense_hdr sshdr;
538 /* Perform up to 3 passes. The first pass uses a conservative
539 * transfer length of 36 unless sdev->inquiry_len specifies a
540 * different value. */
541 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
542 try_inquiry_len = first_inquiry_len;
546 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
547 "scsi scan: INQUIRY pass %d length %d\n",
548 pass, try_inquiry_len));
550 /* Each pass gets up to three chances to ignore Unit Attention */
551 for (count = 0; count < 3; ++count) {
552 memset(scsi_cmd, 0, 6);
553 scsi_cmd[0] = INQUIRY;
554 scsi_cmd[4] = (unsigned char) try_inquiry_len;
556 memset(inq_result, 0, try_inquiry_len);
558 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
559 inq_result, try_inquiry_len, &sshdr,
560 HZ / 2 + HZ * scsi_inq_timeout, 3);
562 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
564 result ? "failed" : "successful", result));
568 * not-ready to ready transition [asc/ascq=0x28/0x0]
569 * or power-on, reset [asc/ascq=0x29/0x0], continue.
570 * INQUIRY should not yield UNIT_ATTENTION
571 * but many buggy devices do so anyway.
573 if ((driver_byte(result) & DRIVER_SENSE) &&
574 scsi_sense_valid(&sshdr)) {
575 if ((sshdr.sense_key == UNIT_ATTENTION) &&
576 ((sshdr.asc == 0x28) ||
577 (sshdr.asc == 0x29)) &&
586 sanitize_inquiry_string(&inq_result[8], 8);
587 sanitize_inquiry_string(&inq_result[16], 16);
588 sanitize_inquiry_string(&inq_result[32], 4);
590 response_len = inq_result[4] + 5;
591 if (response_len > 255)
592 response_len = first_inquiry_len; /* sanity */
595 * Get any flags for this device.
597 * XXX add a bflags to scsi_device, and replace the
598 * corresponding bit fields in scsi_device, so bflags
599 * need not be passed as an argument.
601 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
604 /* When the first pass succeeds we gain information about
605 * what larger transfer lengths might work. */
607 if (BLIST_INQUIRY_36 & *bflags)
608 next_inquiry_len = 36;
609 else if (BLIST_INQUIRY_58 & *bflags)
610 next_inquiry_len = 58;
611 else if (sdev->inquiry_len)
612 next_inquiry_len = sdev->inquiry_len;
614 next_inquiry_len = response_len;
616 /* If more data is available perform the second pass */
617 if (next_inquiry_len > try_inquiry_len) {
618 try_inquiry_len = next_inquiry_len;
624 } else if (pass == 2) {
625 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
626 "Consider BLIST_INQUIRY_36 for this device\n",
629 /* If this pass failed, the third pass goes back and transfers
630 * the same amount as we successfully got in the first pass. */
631 try_inquiry_len = first_inquiry_len;
636 /* If the last transfer attempt got an error, assume the
637 * peripheral doesn't exist or is dead. */
641 /* Don't report any more data than the device says is valid */
642 sdev->inquiry_len = min(try_inquiry_len, response_len);
645 * XXX Abort if the response length is less than 36? If less than
646 * 32, the lookup of the device flags (above) could be invalid,
647 * and it would be possible to take an incorrect action - we do
648 * not want to hang because of a short INQUIRY. On the flip side,
649 * if the device is spun down or becoming ready (and so it gives a
650 * short INQUIRY), an abort here prevents any further use of the
651 * device, including spin up.
653 * Related to the above issue:
655 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
656 * and if not ready, sent a START_STOP to start (maybe spin up) and
657 * then send the INQUIRY again, since the INQUIRY can change after
658 * a device is initialized.
660 * Ideally, start a device if explicitly asked to do so. This
661 * assumes that a device is spun up on power on, spun down on
662 * request, and then spun up on request.
666 * The scanning code needs to know the scsi_level, even if no
667 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
668 * non-zero LUNs can be scanned.
670 sdev->scsi_level = inq_result[2] & 0x07;
671 if (sdev->scsi_level >= 2 ||
672 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
674 sdev->sdev_target->scsi_level = sdev->scsi_level;
680 * scsi_add_lun - allocate and fully initialze a scsi_device
681 * @sdevscan: holds information to be stored in the new scsi_device
682 * @sdevnew: store the address of the newly allocated scsi_device
683 * @inq_result: holds the result of a previous INQUIRY to the LUN
684 * @bflags: black/white list flag
687 * Allocate and initialize a scsi_device matching sdevscan. Optionally
688 * set fields based on values in *@bflags. If @sdevnew is not
689 * NULL, store the address of the new scsi_device in *@sdevnew (needed
690 * when scanning a particular LUN).
693 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
694 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
696 static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
697 int *bflags, int async)
700 * XXX do not save the inquiry, since it can change underneath us,
701 * save just vendor/model/rev.
703 * Rather than save it and have an ioctl that retrieves the saved
704 * value, have an ioctl that executes the same INQUIRY code used
705 * in scsi_probe_lun, let user level programs doing INQUIRY
706 * scanning run at their own risk, or supply a user level program
707 * that can correctly scan.
711 * Copy at least 36 bytes of INQUIRY data, so that we don't
712 * dereference unallocated memory when accessing the Vendor,
713 * Product, and Revision strings. Badly behaved devices may set
714 * the INQUIRY Additional Length byte to a small value, indicating
715 * these strings are invalid, but often they contain plausible data
716 * nonetheless. It doesn't matter if the device sent < 36 bytes
717 * total, since scsi_probe_lun() initializes inq_result with 0s.
719 sdev->inquiry = kmemdup(inq_result,
720 max_t(size_t, sdev->inquiry_len, 36),
722 if (sdev->inquiry == NULL)
723 return SCSI_SCAN_NO_RESPONSE;
725 sdev->vendor = (char *) (sdev->inquiry + 8);
726 sdev->model = (char *) (sdev->inquiry + 16);
727 sdev->rev = (char *) (sdev->inquiry + 32);
729 if (*bflags & BLIST_ISROM) {
731 * It would be better to modify sdev->type, and set
732 * sdev->removable; this can now be done since
733 * print_inquiry has gone away.
735 inq_result[0] = TYPE_ROM;
736 inq_result[1] |= 0x80; /* removable */
737 } else if (*bflags & BLIST_NO_ULD_ATTACH)
738 sdev->no_uld_attach = 1;
740 switch (sdev->type = (inq_result[0] & 0x1f)) {
747 case TYPE_MEDIUM_CHANGER:
759 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
763 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
764 * spec says: The device server is capable of supporting the
765 * specified peripheral device type on this logical unit. However,
766 * the physical device is not currently connected to this logical
769 * The above is vague, as it implies that we could treat 001 and
770 * 011 the same. Stay compatible with previous code, and create a
771 * scsi_device for a PQ of 1
773 * Don't set the device offline here; rather let the upper
774 * level drivers eval the PQ to decide whether they should
775 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
778 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
779 sdev->removable = (0x80 & inq_result[1]) >> 7;
780 sdev->lockable = sdev->removable;
781 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
783 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
784 inq_result[56] & 0x04))
786 if (inq_result[7] & 0x60)
788 if (inq_result[7] & 0x10)
791 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
792 "ANSI: %d%s\n", scsi_device_type(sdev->type),
793 sdev->vendor, sdev->model, sdev->rev,
794 sdev->inq_periph_qual, inq_result[2] & 0x07,
795 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
801 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
802 !(*bflags & BLIST_NOTQ))
803 sdev->tagged_supported = 1;
805 * Some devices (Texel CD ROM drives) have handshaking problems
806 * when used with the Seagate controllers. borken is initialized
807 * to 1, and then set it to 0 here.
809 if ((*bflags & BLIST_BORKEN) == 0)
813 * Apparently some really broken devices (contrary to the SCSI
814 * standards) need to be selected without asserting ATN
816 if (*bflags & BLIST_SELECT_NO_ATN)
817 sdev->select_no_atn = 1;
820 * Maximum 512 sector transfer length
821 * broken RA4x00 Compaq Disk Array
823 if (*bflags & BLIST_MAX_512)
824 blk_queue_max_sectors(sdev->request_queue, 512);
827 * Some devices may not want to have a start command automatically
828 * issued when a device is added.
830 if (*bflags & BLIST_NOSTARTONADD)
831 sdev->no_start_on_add = 1;
833 if (*bflags & BLIST_SINGLELUN)
834 sdev->single_lun = 1;
837 sdev->use_10_for_rw = 1;
839 if (*bflags & BLIST_MS_SKIP_PAGE_08)
840 sdev->skip_ms_page_8 = 1;
842 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
843 sdev->skip_ms_page_3f = 1;
845 if (*bflags & BLIST_USE_10_BYTE_MS)
846 sdev->use_10_for_ms = 1;
848 /* set the device running here so that slave configure
850 scsi_device_set_state(sdev, SDEV_RUNNING);
852 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
853 sdev->use_192_bytes_for_3f = 1;
855 if (*bflags & BLIST_NOT_LOCKABLE)
858 if (*bflags & BLIST_RETRY_HWERROR)
859 sdev->retry_hwerror = 1;
861 transport_configure_device(&sdev->sdev_gendev);
863 if (sdev->host->hostt->slave_configure) {
864 int ret = sdev->host->hostt->slave_configure(sdev);
867 * if LLDD reports slave not present, don't clutter
868 * console with alloc failure messages
871 sdev_printk(KERN_ERR, sdev,
872 "failed to configure device\n");
874 return SCSI_SCAN_NO_RESPONSE;
879 * Ok, the device is now all set up, we can
880 * register it and tell the rest of the kernel
883 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
884 return SCSI_SCAN_NO_RESPONSE;
886 return SCSI_SCAN_LUN_PRESENT;
889 static inline void scsi_destroy_sdev(struct scsi_device *sdev)
891 scsi_device_set_state(sdev, SDEV_DEL);
892 if (sdev->host->hostt->slave_destroy)
893 sdev->host->hostt->slave_destroy(sdev);
894 transport_destroy_device(&sdev->sdev_gendev);
895 put_device(&sdev->sdev_gendev);
898 #ifdef CONFIG_SCSI_LOGGING
900 * scsi_inq_str - print INQUIRY data from min to max index,
901 * strip trailing whitespace
902 * @buf: Output buffer with at least end-first+1 bytes of space
903 * @inq: Inquiry buffer (input)
904 * @first: Offset of string into inq
905 * @end: Index after last character in inq
907 static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
908 unsigned first, unsigned end)
910 unsigned term = 0, idx;
912 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
913 if (inq[idx+first] > ' ') {
914 buf[idx] = inq[idx+first];
926 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
927 * @starget: pointer to target device structure
928 * @lun: LUN of target device
929 * @sdevscan: probe the LUN corresponding to this scsi_device
930 * @sdevnew: store the value of any new scsi_device allocated
931 * @bflagsp: store bflags here if not NULL
934 * Call scsi_probe_lun, if a LUN with an attached device is found,
935 * allocate and set it up by calling scsi_add_lun.
938 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
939 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
940 * attached at the LUN
941 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
943 static int scsi_probe_and_add_lun(struct scsi_target *starget,
944 uint lun, int *bflagsp,
945 struct scsi_device **sdevp, int rescan,
948 struct scsi_device *sdev;
949 unsigned char *result;
950 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
951 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
954 * The rescan flag is used as an optimization, the first scan of a
955 * host adapter calls into here with rescan == 0.
957 sdev = scsi_device_lookup_by_target(starget, lun);
959 if (rescan || sdev->sdev_state != SDEV_CREATED) {
960 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
961 "scsi scan: device exists on %s\n",
962 sdev->sdev_gendev.bus_id));
966 scsi_device_put(sdev);
969 *bflagsp = scsi_get_device_flags(sdev,
972 return SCSI_SCAN_LUN_PRESENT;
974 scsi_device_put(sdev);
976 sdev = scsi_alloc_sdev(starget, lun, hostdata);
980 result = kmalloc(result_len, GFP_ATOMIC |
981 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
985 if (scsi_probe_lun(sdev, result, result_len, &bflags))
986 goto out_free_result;
991 * result contains valid SCSI INQUIRY data.
993 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
995 * For a Peripheral qualifier 3 (011b), the SCSI
996 * spec says: The device server is not capable of
997 * supporting a physical device on this logical
1000 * For disks, this implies that there is no
1001 * logical disk configured at sdev->lun, but there
1002 * is a target id responding.
1004 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1005 " peripheral qualifier of 3, device not"
1008 SCSI_LOG_SCAN_BUS(1, {
1009 unsigned char vend[9];
1010 unsigned char mod[17];
1012 sdev_printk(KERN_INFO, sdev,
1013 "scsi scan: consider passing scsi_mod."
1014 "dev_flags=%s:%s:0x240 or 0x800240\n",
1015 scsi_inq_str(vend, result, 8, 16),
1016 scsi_inq_str(mod, result, 16, 32));
1020 res = SCSI_SCAN_TARGET_PRESENT;
1021 goto out_free_result;
1025 * Some targets may set slight variations of PQ and PDT to signal
1026 * that no LUN is present, so don't add sdev in these cases.
1027 * Two specific examples are:
1028 * 1) NetApp targets: return PQ=1, PDT=0x1f
1029 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1030 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1033 * 1) SCSI SPC-3, pp. 145-146
1034 * PQ=1: "A peripheral device having the specified peripheral
1035 * device type is not connected to this logical unit. However, the
1036 * device server is capable of supporting the specified peripheral
1037 * device type on this logical unit."
1038 * PDT=0x1f: "Unknown or no device type"
1039 * 2) USB UFI 1.0, p. 20
1040 * PDT=00h Direct-access device (floppy)
1041 * PDT=1Fh none (no FDD connected to the requested logical unit)
1043 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1044 (result[0] & 0x1f) == 0x1f) {
1045 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1046 "scsi scan: peripheral device type"
1047 " of 31, no device added\n"));
1048 res = SCSI_SCAN_TARGET_PRESENT;
1049 goto out_free_result;
1052 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
1053 if (res == SCSI_SCAN_LUN_PRESENT) {
1054 if (bflags & BLIST_KEY) {
1056 scsi_unlock_floptical(sdev, result);
1063 if (res == SCSI_SCAN_LUN_PRESENT) {
1065 if (scsi_device_get(sdev) == 0) {
1068 __scsi_remove_device(sdev);
1069 res = SCSI_SCAN_NO_RESPONSE;
1073 scsi_destroy_sdev(sdev);
1079 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1080 * @starget: pointer to target structure to scan
1081 * @bflags: black/white list flag for LUN 0
1084 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1085 * scanned) to some maximum lun until a LUN is found with no device
1086 * attached. Use the bflags to figure out any oddities.
1088 * Modifies sdevscan->lun.
1090 static void scsi_sequential_lun_scan(struct scsi_target *starget,
1091 int bflags, int scsi_level, int rescan)
1093 unsigned int sparse_lun, lun, max_dev_lun;
1094 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1096 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1097 "%s\n", starget->dev.bus_id));
1099 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1101 * If this device is known to support sparse multiple units,
1102 * override the other settings, and scan all of them. Normally,
1103 * SCSI-3 devices should be scanned via the REPORT LUNS.
1105 if (bflags & BLIST_SPARSELUN) {
1106 max_dev_lun = shost->max_lun;
1112 * If less than SCSI_1_CSS, and no special lun scaning, stop
1113 * scanning; this matches 2.4 behaviour, but could just be a bug
1114 * (to continue scanning a SCSI_1_CSS device).
1116 * This test is broken. We might not have any device on lun0 for
1117 * a sparselun device, and if that's the case then how would we
1118 * know the real scsi_level, eh? It might make sense to just not
1119 * scan any SCSI_1 device for non-0 luns, but that check would best
1120 * go into scsi_alloc_sdev() and just have it return null when asked
1121 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1123 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1124 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1129 * If this device is known to support multiple units, override
1130 * the other settings, and scan all of them.
1132 if (bflags & BLIST_FORCELUN)
1133 max_dev_lun = shost->max_lun;
1135 * REGAL CDC-4X: avoid hang after LUN 4
1137 if (bflags & BLIST_MAX5LUN)
1138 max_dev_lun = min(5U, max_dev_lun);
1140 * Do not scan SCSI-2 or lower device past LUN 7, unless
1143 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1144 max_dev_lun = min(8U, max_dev_lun);
1147 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1148 * until we reach the max, or no LUN is found and we are not
1151 for (lun = 1; lun < max_dev_lun; ++lun)
1152 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1153 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1159 * scsilun_to_int: convert a scsi_lun to an int
1160 * @scsilun: struct scsi_lun to be converted.
1163 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1164 * integer, and return the result. The caller must check for
1165 * truncation before using this function.
1168 * The struct scsi_lun is assumed to be four levels, with each level
1169 * effectively containing a SCSI byte-ordered (big endian) short; the
1170 * addressing bits of each level are ignored (the highest two bits).
1171 * For a description of the LUN format, post SCSI-3 see the SCSI
1172 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1174 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1175 * the integer: 0x0b030a04
1177 static int scsilun_to_int(struct scsi_lun *scsilun)
1183 for (i = 0; i < sizeof(lun); i += 2)
1184 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1185 scsilun->scsi_lun[i + 1]) << (i * 8));
1190 * int_to_scsilun: reverts an int into a scsi_lun
1191 * @int: integer to be reverted
1192 * @scsilun: struct scsi_lun to be set.
1195 * Reverts the functionality of the scsilun_to_int, which packed
1196 * an 8-byte lun value into an int. This routine unpacks the int
1197 * back into the lun value.
1198 * Note: the scsilun_to_int() routine does not truly handle all
1199 * 8bytes of the lun value. This functions restores only as much
1200 * as was set by the routine.
1203 * Given an integer : 0x0b030a04, this function returns a
1204 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1207 void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1211 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1213 for (i = 0; i < sizeof(lun); i += 2) {
1214 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1215 scsilun->scsi_lun[i+1] = lun & 0xFF;
1219 EXPORT_SYMBOL(int_to_scsilun);
1222 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1223 * @sdevscan: scan the host, channel, and id of this scsi_device
1226 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1227 * command, and scan the resulting list of LUNs by calling
1228 * scsi_probe_and_add_lun.
1230 * Modifies sdevscan->lun.
1233 * 0: scan completed (or no memory, so further scanning is futile)
1234 * 1: no report lun scan, or not configured
1236 static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
1240 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1241 unsigned int length;
1243 unsigned int num_luns;
1244 unsigned int retries;
1246 struct scsi_lun *lunp, *lun_data;
1248 struct scsi_sense_hdr sshdr;
1249 struct scsi_device *sdev;
1250 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1254 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1255 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1256 * support more than 8 LUNs.
1258 if (bflags & BLIST_NOREPORTLUN)
1260 if (starget->scsi_level < SCSI_2 &&
1261 starget->scsi_level != SCSI_UNKNOWN)
1263 if (starget->scsi_level < SCSI_3 &&
1264 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
1266 if (bflags & BLIST_NOLUN)
1269 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1270 sdev = scsi_alloc_sdev(starget, 0, NULL);
1273 if (scsi_device_get(sdev))
1277 sprintf(devname, "host %d channel %d id %d",
1278 shost->host_no, sdev->channel, sdev->id);
1281 * Allocate enough to hold the header (the same size as one scsi_lun)
1282 * plus the max number of luns we are requesting.
1284 * Reallocating and trying again (with the exact amount we need)
1285 * would be nice, but then we need to somehow limit the size
1286 * allocated based on the available memory and the limits of
1287 * kmalloc - we don't want a kmalloc() failure of a huge value to
1288 * prevent us from finding any LUNs on this target.
1290 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1291 lun_data = kmalloc(length, GFP_ATOMIC |
1292 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
1294 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1298 scsi_cmd[0] = REPORT_LUNS;
1301 * bytes 1 - 5: reserved, set to zero.
1303 memset(&scsi_cmd[1], 0, 5);
1306 * bytes 6 - 9: length of the command.
1308 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1309 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1310 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1311 scsi_cmd[9] = (unsigned char) length & 0xff;
1313 scsi_cmd[10] = 0; /* reserved */
1314 scsi_cmd[11] = 0; /* control */
1317 * We can get a UNIT ATTENTION, for example a power on/reset, so
1318 * retry a few times (like sd.c does for TEST UNIT READY).
1319 * Experience shows some combinations of adapter/devices get at
1320 * least two power on/resets.
1322 * Illegal requests (for devices that do not support REPORT LUNS)
1323 * should come through as a check condition, and will not generate
1326 for (retries = 0; retries < 3; retries++) {
1327 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1328 " REPORT LUNS to %s (try %d)\n", devname,
1331 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1332 lun_data, length, &sshdr,
1333 SCSI_TIMEOUT + 4 * HZ, 3);
1335 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1336 " %s (try %d) result 0x%x\n", result
1337 ? "failed" : "successful", retries, result));
1340 else if (scsi_sense_valid(&sshdr)) {
1341 if (sshdr.sense_key != UNIT_ATTENTION)
1348 * The device probably does not support a REPORT LUN command
1355 * Get the length from the first four bytes of lun_data.
1357 data = (u8 *) lun_data->scsi_lun;
1358 length = ((data[0] << 24) | (data[1] << 16) |
1359 (data[2] << 8) | (data[3] << 0));
1361 num_luns = (length / sizeof(struct scsi_lun));
1362 if (num_luns > max_scsi_report_luns) {
1363 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1364 " of %d luns reported, try increasing"
1365 " max_scsi_report_luns.\n", devname,
1366 max_scsi_report_luns, num_luns);
1367 num_luns = max_scsi_report_luns;
1370 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1371 "scsi scan: REPORT LUN scan\n"));
1374 * Scan the luns in lun_data. The entry at offset 0 is really
1375 * the header, so start at 1 and go up to and including num_luns.
1377 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1378 lun = scsilun_to_int(lunp);
1381 * Check if the unused part of lunp is non-zero, and so
1382 * does not fit in lun.
1384 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1388 * Output an error displaying the LUN in byte order,
1389 * this differs from what linux would print for the
1390 * integer LUN value.
1392 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1393 data = (char *)lunp->scsi_lun;
1394 for (i = 0; i < sizeof(struct scsi_lun); i++)
1395 printk("%02x", data[i]);
1396 printk(" has a LUN larger than currently supported.\n");
1397 } else if (lun > sdev->host->max_lun) {
1398 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1399 " than allowed by the host adapter\n",
1404 res = scsi_probe_and_add_lun(starget,
1405 lun, NULL, NULL, rescan, NULL);
1406 if (res == SCSI_SCAN_NO_RESPONSE) {
1408 * Got some results, but now none, abort.
1410 sdev_printk(KERN_ERR, sdev,
1411 "Unexpected response"
1412 " from lun %d while scanning, scan"
1422 scsi_device_put(sdev);
1423 if (sdev->sdev_state == SDEV_CREATED)
1425 * the sdev we used didn't appear in the report luns scan
1427 scsi_destroy_sdev(sdev);
1431 struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1432 uint id, uint lun, void *hostdata)
1434 struct scsi_device *sdev = ERR_PTR(-ENODEV);
1435 struct device *parent = &shost->shost_gendev;
1436 struct scsi_target *starget;
1438 starget = scsi_alloc_target(parent, channel, id);
1440 return ERR_PTR(-ENOMEM);
1442 mutex_lock(&shost->scan_mutex);
1443 if (scsi_host_scan_allowed(shost))
1444 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
1445 mutex_unlock(&shost->scan_mutex);
1446 scsi_target_reap(starget);
1447 put_device(&starget->dev);
1451 EXPORT_SYMBOL(__scsi_add_device);
1453 int scsi_add_device(struct Scsi_Host *host, uint channel,
1454 uint target, uint lun)
1456 struct scsi_device *sdev =
1457 __scsi_add_device(host, channel, target, lun, NULL);
1459 return PTR_ERR(sdev);
1461 scsi_device_put(sdev);
1464 EXPORT_SYMBOL(scsi_add_device);
1466 void scsi_rescan_device(struct device *dev)
1468 struct scsi_driver *drv;
1473 drv = to_scsi_driver(dev->driver);
1474 if (try_module_get(drv->owner)) {
1477 module_put(drv->owner);
1480 EXPORT_SYMBOL(scsi_rescan_device);
1482 static void __scsi_scan_target(struct device *parent, unsigned int channel,
1483 unsigned int id, unsigned int lun, int rescan)
1485 struct Scsi_Host *shost = dev_to_shost(parent);
1488 struct scsi_target *starget;
1490 if (shost->this_id == id)
1492 * Don't scan the host adapter
1496 starget = scsi_alloc_target(parent, channel, id);
1500 if (lun != SCAN_WILD_CARD) {
1502 * Scan for a specific host/chan/id/lun.
1504 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1509 * Scan LUN 0, if there is some response, scan further. Ideally, we
1510 * would not configure LUN 0 until all LUNs are scanned.
1512 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1513 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1514 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1516 * The REPORT LUN did not scan the target,
1517 * do a sequential scan.
1519 scsi_sequential_lun_scan(starget, bflags,
1520 starget->scsi_level, rescan);
1524 /* now determine if the target has any children at all
1525 * and if not, nuke it */
1526 scsi_target_reap(starget);
1528 put_device(&starget->dev);
1532 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1534 * @parent: host to scan
1535 * @channel: channel to scan
1536 * @id: target id to scan
1537 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1538 * @rescan: passed to LUN scanning routines
1541 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1542 * and possibly all LUNs on the target id.
1544 * First try a REPORT LUN scan, if that does not scan the target, do a
1545 * sequential scan of LUNs on the target id.
1547 void scsi_scan_target(struct device *parent, unsigned int channel,
1548 unsigned int id, unsigned int lun, int rescan)
1550 struct Scsi_Host *shost = dev_to_shost(parent);
1552 if (!shost->async_scan)
1553 scsi_complete_async_scans();
1555 mutex_lock(&shost->scan_mutex);
1556 if (scsi_host_scan_allowed(shost))
1557 __scsi_scan_target(parent, channel, id, lun, rescan);
1558 mutex_unlock(&shost->scan_mutex);
1560 EXPORT_SYMBOL(scsi_scan_target);
1562 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1563 unsigned int id, unsigned int lun, int rescan)
1567 if (id == SCAN_WILD_CARD)
1568 for (id = 0; id < shost->max_id; ++id) {
1570 * XXX adapter drivers when possible (FCP, iSCSI)
1571 * could modify max_id to match the current max,
1572 * not the absolute max.
1574 * XXX add a shost id iterator, so for example,
1575 * the FC ID can be the same as a target id
1576 * without a huge overhead of sparse id's.
1578 if (shost->reverse_ordering)
1580 * Scan from high to low id.
1582 order_id = shost->max_id - id - 1;
1585 __scsi_scan_target(&shost->shost_gendev, channel,
1586 order_id, lun, rescan);
1589 __scsi_scan_target(&shost->shost_gendev, channel,
1593 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1594 unsigned int id, unsigned int lun, int rescan)
1596 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1598 __FUNCTION__, channel, id, lun));
1600 if (!shost->async_scan)
1601 scsi_complete_async_scans();
1603 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1604 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1605 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1608 mutex_lock(&shost->scan_mutex);
1609 if (scsi_host_scan_allowed(shost)) {
1610 if (channel == SCAN_WILD_CARD)
1611 for (channel = 0; channel <= shost->max_channel;
1613 scsi_scan_channel(shost, channel, id, lun,
1616 scsi_scan_channel(shost, channel, id, lun, rescan);
1618 mutex_unlock(&shost->scan_mutex);
1623 static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1625 struct scsi_device *sdev;
1626 shost_for_each_device(sdev, shost) {
1627 if (scsi_sysfs_add_sdev(sdev) != 0)
1628 scsi_destroy_sdev(sdev);
1633 * scsi_prep_async_scan - prepare for an async scan
1634 * @shost: the host which will be scanned
1635 * Returns: a cookie to be passed to scsi_finish_async_scan()
1637 * Tells the midlayer this host is going to do an asynchronous scan.
1638 * It reserves the host's position in the scanning list and ensures
1639 * that other asynchronous scans started after this one won't affect the
1640 * ordering of the discovered devices.
1642 struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1644 struct async_scan_data *data;
1646 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1649 if (shost->async_scan) {
1650 printk("%s called twice for host %d", __FUNCTION__,
1656 data = kmalloc(sizeof(*data), GFP_KERNEL);
1659 data->shost = scsi_host_get(shost);
1662 init_completion(&data->prev_finished);
1664 spin_lock(&async_scan_lock);
1665 shost->async_scan = 1;
1666 if (list_empty(&scanning_hosts))
1667 complete(&data->prev_finished);
1668 list_add_tail(&data->list, &scanning_hosts);
1669 spin_unlock(&async_scan_lock);
1679 * scsi_finish_async_scan - asynchronous scan has finished
1680 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1682 * All the devices currently attached to this host have been found.
1683 * This function announces all the devices it has found to the rest
1686 void scsi_finish_async_scan(struct async_scan_data *data)
1688 struct Scsi_Host *shost;
1693 shost = data->shost;
1694 if (!shost->async_scan) {
1695 printk("%s called twice for host %d", __FUNCTION__,
1701 wait_for_completion(&data->prev_finished);
1703 scsi_sysfs_add_devices(shost);
1705 spin_lock(&async_scan_lock);
1706 shost->async_scan = 0;
1707 list_del(&data->list);
1708 if (!list_empty(&scanning_hosts)) {
1709 struct async_scan_data *next = list_entry(scanning_hosts.next,
1710 struct async_scan_data, list);
1711 complete(&next->prev_finished);
1713 spin_unlock(&async_scan_lock);
1715 scsi_host_put(shost);
1719 static int do_scan_async(void *_data)
1721 struct async_scan_data *data = _data;
1722 scsi_scan_host_selected(data->shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1725 scsi_finish_async_scan(data);
1730 * scsi_scan_host - scan the given adapter
1731 * @shost: adapter to scan
1733 void scsi_scan_host(struct Scsi_Host *shost)
1735 struct async_scan_data *data;
1737 if (strncmp(scsi_scan_type, "none", 4) == 0)
1740 data = scsi_prep_async_scan(shost);
1742 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1746 kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1748 EXPORT_SYMBOL(scsi_scan_host);
1750 void scsi_forget_host(struct Scsi_Host *shost)
1752 struct scsi_device *sdev;
1753 unsigned long flags;
1756 spin_lock_irqsave(shost->host_lock, flags);
1757 list_for_each_entry(sdev, &shost->__devices, siblings) {
1758 if (sdev->sdev_state == SDEV_DEL)
1760 spin_unlock_irqrestore(shost->host_lock, flags);
1761 __scsi_remove_device(sdev);
1764 spin_unlock_irqrestore(shost->host_lock, flags);
1768 * Function: scsi_get_host_dev()
1770 * Purpose: Create a scsi_device that points to the host adapter itself.
1772 * Arguments: SHpnt - Host that needs a scsi_device
1774 * Lock status: None assumed.
1776 * Returns: The scsi_device or NULL
1779 * Attach a single scsi_device to the Scsi_Host - this should
1780 * be made to look like a "pseudo-device" that points to the
1783 * Note - this device is not accessible from any high-level
1784 * drivers (including generics), which is probably not
1785 * optimal. We can add hooks later to attach
1787 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1789 struct scsi_device *sdev = NULL;
1790 struct scsi_target *starget;
1792 mutex_lock(&shost->scan_mutex);
1793 if (!scsi_host_scan_allowed(shost))
1795 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1799 sdev = scsi_alloc_sdev(starget, 0, NULL);
1801 sdev->sdev_gendev.parent = get_device(&starget->dev);
1804 scsi_target_reap(starget);
1805 put_device(&starget->dev);
1807 mutex_unlock(&shost->scan_mutex);
1810 EXPORT_SYMBOL(scsi_get_host_dev);
1813 * Function: scsi_free_host_dev()
1815 * Purpose: Free a scsi_device that points to the host adapter itself.
1817 * Arguments: SHpnt - Host that needs a scsi_device
1819 * Lock status: None assumed.
1825 void scsi_free_host_dev(struct scsi_device *sdev)
1827 BUG_ON(sdev->id != sdev->host->this_id);
1829 scsi_destroy_sdev(sdev);
1831 EXPORT_SYMBOL(scsi_free_host_dev);