mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / scsi / scsi_scan.c
blob25073167bcc4e4656c5f920ab828d4a5489453c2
1 /*
2 * scsi_scan.c
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
20 * scsi_device for it.
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>
35 #include <linux/async.h>
36 #include <linux/slab.h>
38 #include <scsi/scsi.h>
39 #include <scsi/scsi_cmnd.h>
40 #include <scsi/scsi_device.h>
41 #include <scsi/scsi_driver.h>
42 #include <scsi/scsi_devinfo.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi_transport.h>
45 #include <scsi/scsi_eh.h>
47 #include "scsi_priv.h"
48 #include "scsi_logging.h"
50 #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
51 " SCSI scanning, some SCSI devices might not be configured\n"
54 * Default timeout
56 #define SCSI_TIMEOUT (2*HZ)
57 #define SCSI_REPORT_LUNS_TIMEOUT (30*HZ)
60 * Prefix values for the SCSI id's (stored in sysfs name field)
62 #define SCSI_UID_SER_NUM 'S'
63 #define SCSI_UID_UNKNOWN 'Z'
66 * Return values of some of the scanning functions.
68 * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
69 * includes allocation or general failures preventing IO from being sent.
71 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
72 * on the given LUN.
74 * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
75 * given LUN.
77 #define SCSI_SCAN_NO_RESPONSE 0
78 #define SCSI_SCAN_TARGET_PRESENT 1
79 #define SCSI_SCAN_LUN_PRESENT 2
81 static const char *scsi_null_device_strs = "nullnullnullnull";
83 #define MAX_SCSI_LUNS 512
85 #ifdef CONFIG_SCSI_MULTI_LUN
86 static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
87 #else
88 static unsigned int max_scsi_luns = 1;
89 #endif
91 module_param_named(max_luns, max_scsi_luns, uint, S_IRUGO|S_IWUSR);
92 MODULE_PARM_DESC(max_luns,
93 "last scsi LUN (should be between 1 and 2^32-1)");
95 #ifdef CONFIG_SCSI_SCAN_ASYNC
96 #define SCSI_SCAN_TYPE_DEFAULT "async"
97 #else
98 #define SCSI_SCAN_TYPE_DEFAULT "sync"
99 #endif
101 static char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT;
103 module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
104 MODULE_PARM_DESC(scan, "sync, async or none");
107 * max_scsi_report_luns: the maximum number of LUNS that will be
108 * returned from the REPORT LUNS command. 8 times this value must
109 * be allocated. In theory this could be up to an 8 byte value, but
110 * in practice, the maximum number of LUNs suppored by any device
111 * is about 16k.
113 static unsigned int max_scsi_report_luns = 511;
115 module_param_named(max_report_luns, max_scsi_report_luns, uint, S_IRUGO|S_IWUSR);
116 MODULE_PARM_DESC(max_report_luns,
117 "REPORT LUNS maximum number of LUNS received (should be"
118 " between 1 and 16384)");
120 static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ + 18;
122 module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR);
123 MODULE_PARM_DESC(inq_timeout,
124 "Timeout (in seconds) waiting for devices to answer INQUIRY."
125 " Default is 20. Some devices may need more; most need less.");
127 /* This lock protects only this list */
128 static DEFINE_SPINLOCK(async_scan_lock);
129 static LIST_HEAD(scanning_hosts);
131 struct async_scan_data {
132 struct list_head list;
133 struct Scsi_Host *shost;
134 struct completion prev_finished;
138 * scsi_complete_async_scans - Wait for asynchronous scans to complete
140 * When this function returns, any host which started scanning before
141 * this function was called will have finished its scan. Hosts which
142 * started scanning after this function was called may or may not have
143 * finished.
145 int scsi_complete_async_scans(void)
147 struct async_scan_data *data;
149 do {
150 if (list_empty(&scanning_hosts))
151 return 0;
152 /* If we can't get memory immediately, that's OK. Just
153 * sleep a little. Even if we never get memory, the async
154 * scans will finish eventually.
156 data = kmalloc(sizeof(*data), GFP_KERNEL);
157 if (!data)
158 msleep(1);
159 } while (!data);
161 data->shost = NULL;
162 init_completion(&data->prev_finished);
164 spin_lock(&async_scan_lock);
165 /* Check that there's still somebody else on the list */
166 if (list_empty(&scanning_hosts))
167 goto done;
168 list_add_tail(&data->list, &scanning_hosts);
169 spin_unlock(&async_scan_lock);
171 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
172 wait_for_completion(&data->prev_finished);
174 spin_lock(&async_scan_lock);
175 list_del(&data->list);
176 if (!list_empty(&scanning_hosts)) {
177 struct async_scan_data *next = list_entry(scanning_hosts.next,
178 struct async_scan_data, list);
179 complete(&next->prev_finished);
181 done:
182 spin_unlock(&async_scan_lock);
184 kfree(data);
185 return 0;
189 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
190 * @sdev: scsi device to send command to
191 * @result: area to store the result of the MODE SENSE
193 * Description:
194 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
195 * Called for BLIST_KEY devices.
197 static void scsi_unlock_floptical(struct scsi_device *sdev,
198 unsigned char *result)
200 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
202 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
203 scsi_cmd[0] = MODE_SENSE;
204 scsi_cmd[1] = 0;
205 scsi_cmd[2] = 0x2e;
206 scsi_cmd[3] = 0;
207 scsi_cmd[4] = 0x2a; /* size */
208 scsi_cmd[5] = 0;
209 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
210 SCSI_TIMEOUT, 3, NULL);
214 * scsi_alloc_sdev - allocate and setup a scsi_Device
215 * @starget: which target to allocate a &scsi_device for
216 * @lun: which lun
217 * @hostdata: usually NULL and set by ->slave_alloc instead
219 * Description:
220 * Allocate, initialize for io, and return a pointer to a scsi_Device.
221 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
222 * adds scsi_Device to the appropriate list.
224 * Return value:
225 * scsi_Device pointer, or NULL on failure.
227 static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
228 unsigned int lun, void *hostdata)
230 struct scsi_device *sdev;
231 int display_failure_msg = 1, ret;
232 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
233 extern void scsi_evt_thread(struct work_struct *work);
234 extern void scsi_requeue_run_queue(struct work_struct *work);
236 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
237 GFP_ATOMIC);
238 if (!sdev)
239 goto out;
241 sdev->vendor = scsi_null_device_strs;
242 sdev->model = scsi_null_device_strs;
243 sdev->rev = scsi_null_device_strs;
244 sdev->host = shost;
245 sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD;
246 sdev->id = starget->id;
247 sdev->lun = lun;
248 sdev->channel = starget->channel;
249 sdev->sdev_state = SDEV_CREATED;
250 INIT_LIST_HEAD(&sdev->siblings);
251 INIT_LIST_HEAD(&sdev->same_target_siblings);
252 INIT_LIST_HEAD(&sdev->cmd_list);
253 INIT_LIST_HEAD(&sdev->starved_entry);
254 INIT_LIST_HEAD(&sdev->event_list);
255 spin_lock_init(&sdev->list_lock);
256 INIT_WORK(&sdev->event_work, scsi_evt_thread);
257 INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue);
259 sdev->sdev_gendev.parent = get_device(&starget->dev);
260 sdev->sdev_target = starget;
262 /* usually NULL and set by ->slave_alloc instead */
263 sdev->hostdata = hostdata;
265 /* if the device needs this changing, it may do so in the
266 * slave_configure function */
267 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
270 * Some low level driver could use device->type
272 sdev->type = -1;
275 * Assume that the device will have handshaking problems,
276 * and then fix this field later if it turns out it
277 * doesn't
279 sdev->borken = 1;
281 sdev->request_queue = scsi_alloc_queue(sdev);
282 if (!sdev->request_queue) {
283 /* release fn is set up in scsi_sysfs_device_initialise, so
284 * have to free and put manually here */
285 put_device(&starget->dev);
286 kfree(sdev);
287 goto out;
289 WARN_ON_ONCE(!blk_get_queue(sdev->request_queue));
290 sdev->request_queue->queuedata = sdev;
291 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
293 scsi_sysfs_device_initialize(sdev);
295 if (shost->hostt->slave_alloc) {
296 ret = shost->hostt->slave_alloc(sdev);
297 if (ret) {
299 * if LLDD reports slave not present, don't clutter
300 * console with alloc failure messages
302 if (ret == -ENXIO)
303 display_failure_msg = 0;
304 goto out_device_destroy;
308 return sdev;
310 out_device_destroy:
311 __scsi_remove_device(sdev);
312 out:
313 if (display_failure_msg)
314 printk(ALLOC_FAILURE_MSG, __func__);
315 return NULL;
318 static void scsi_target_destroy(struct scsi_target *starget)
320 struct device *dev = &starget->dev;
321 struct Scsi_Host *shost = dev_to_shost(dev->parent);
322 unsigned long flags;
324 starget->state = STARGET_DEL;
325 transport_destroy_device(dev);
326 spin_lock_irqsave(shost->host_lock, flags);
327 if (shost->hostt->target_destroy)
328 shost->hostt->target_destroy(starget);
329 list_del_init(&starget->siblings);
330 spin_unlock_irqrestore(shost->host_lock, flags);
331 put_device(dev);
334 static void scsi_target_dev_release(struct device *dev)
336 struct device *parent = dev->parent;
337 struct scsi_target *starget = to_scsi_target(dev);
339 kfree(starget);
340 put_device(parent);
343 static struct device_type scsi_target_type = {
344 .name = "scsi_target",
345 .release = scsi_target_dev_release,
348 int scsi_is_target_device(const struct device *dev)
350 return dev->type == &scsi_target_type;
352 EXPORT_SYMBOL(scsi_is_target_device);
354 static struct scsi_target *__scsi_find_target(struct device *parent,
355 int channel, uint id)
357 struct scsi_target *starget, *found_starget = NULL;
358 struct Scsi_Host *shost = dev_to_shost(parent);
360 * Search for an existing target for this sdev.
362 list_for_each_entry(starget, &shost->__targets, siblings) {
363 if (starget->id == id &&
364 starget->channel == channel) {
365 found_starget = starget;
366 break;
369 if (found_starget)
370 get_device(&found_starget->dev);
372 return found_starget;
376 * scsi_target_reap_ref_release - remove target from visibility
377 * @kref: the reap_ref in the target being released
379 * Called on last put of reap_ref, which is the indication that no device
380 * under this target is visible anymore, so render the target invisible in
381 * sysfs. Note: we have to be in user context here because the target reaps
382 * should be done in places where the scsi device visibility is being removed.
384 static void scsi_target_reap_ref_release(struct kref *kref)
386 struct scsi_target *starget
387 = container_of(kref, struct scsi_target, reap_ref);
390 * if we get here and the target is still in the CREATED state that
391 * means it was allocated but never made visible (because a scan
392 * turned up no LUNs), so don't call device_del() on it.
394 if (starget->state != STARGET_CREATED) {
395 transport_remove_device(&starget->dev);
396 device_del(&starget->dev);
398 scsi_target_destroy(starget);
401 static void scsi_target_reap_ref_put(struct scsi_target *starget)
403 kref_put(&starget->reap_ref, scsi_target_reap_ref_release);
407 * scsi_alloc_target - allocate a new or find an existing target
408 * @parent: parent of the target (need not be a scsi host)
409 * @channel: target channel number (zero if no channels)
410 * @id: target id number
412 * Return an existing target if one exists, provided it hasn't already
413 * gone into STARGET_DEL state, otherwise allocate a new target.
415 * The target is returned with an incremented reference, so the caller
416 * is responsible for both reaping and doing a last put
418 static struct scsi_target *scsi_alloc_target(struct device *parent,
419 int channel, uint id)
421 struct Scsi_Host *shost = dev_to_shost(parent);
422 struct device *dev = NULL;
423 unsigned long flags;
424 const int size = sizeof(struct scsi_target)
425 + shost->transportt->target_size;
426 struct scsi_target *starget;
427 struct scsi_target *found_target;
428 int error, ref_got;
430 starget = kzalloc(size, GFP_KERNEL);
431 if (!starget) {
432 printk(KERN_ERR "%s: allocation failure\n", __func__);
433 return NULL;
435 dev = &starget->dev;
436 device_initialize(dev);
437 kref_init(&starget->reap_ref);
438 dev->parent = get_device(parent);
439 dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
440 dev->bus = &scsi_bus_type;
441 dev->type = &scsi_target_type;
442 starget->id = id;
443 starget->channel = channel;
444 starget->can_queue = 0;
445 INIT_LIST_HEAD(&starget->siblings);
446 INIT_LIST_HEAD(&starget->devices);
447 starget->state = STARGET_CREATED;
448 starget->scsi_level = SCSI_2;
449 starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED;
450 retry:
451 spin_lock_irqsave(shost->host_lock, flags);
453 found_target = __scsi_find_target(parent, channel, id);
454 if (found_target)
455 goto found;
457 list_add_tail(&starget->siblings, &shost->__targets);
458 spin_unlock_irqrestore(shost->host_lock, flags);
459 /* allocate and add */
460 transport_setup_device(dev);
461 if (shost->hostt->target_alloc) {
462 error = shost->hostt->target_alloc(starget);
464 if(error) {
465 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
466 /* don't want scsi_target_reap to do the final
467 * put because it will be under the host lock */
468 scsi_target_destroy(starget);
469 return NULL;
472 get_device(dev);
474 return starget;
476 found:
478 * release routine already fired if kref is zero, so if we can still
479 * take the reference, the target must be alive. If we can't, it must
480 * be dying and we need to wait for a new target
482 ref_got = kref_get_unless_zero(&found_target->reap_ref);
484 spin_unlock_irqrestore(shost->host_lock, flags);
485 if (ref_got) {
486 put_device(dev);
487 return found_target;
490 * Unfortunately, we found a dying target; need to wait until it's
491 * dead before we can get a new one. There is an anomaly here. We
492 * *should* call scsi_target_reap() to balance the kref_get() of the
493 * reap_ref above. However, since the target being released, it's
494 * already invisible and the reap_ref is irrelevant. If we call
495 * scsi_target_reap() we might spuriously do another device_del() on
496 * an already invisible target.
498 put_device(&found_target->dev);
500 * length of time is irrelevant here, we just want to yield the CPU
501 * for a tick to avoid busy waiting for the target to die.
503 msleep(1);
504 goto retry;
508 * scsi_target_reap - check to see if target is in use and destroy if not
509 * @starget: target to be checked
511 * This is used after removing a LUN or doing a last put of the target
512 * it checks atomically that nothing is using the target and removes
513 * it if so.
515 void scsi_target_reap(struct scsi_target *starget)
518 * serious problem if this triggers: STARGET_DEL is only set in the if
519 * the reap_ref drops to zero, so we're trying to do another final put
520 * on an already released kref
522 BUG_ON(starget->state == STARGET_DEL);
523 scsi_target_reap_ref_put(starget);
527 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
528 * @s: INQUIRY result string to sanitize
529 * @len: length of the string
531 * Description:
532 * The SCSI spec says that INQUIRY vendor, product, and revision
533 * strings must consist entirely of graphic ASCII characters,
534 * padded on the right with spaces. Since not all devices obey
535 * this rule, we will replace non-graphic or non-ASCII characters
536 * with spaces. Exception: a NUL character is interpreted as a
537 * string terminator, so all the following characters are set to
538 * spaces.
540 static void sanitize_inquiry_string(unsigned char *s, int len)
542 int terminated = 0;
544 for (; len > 0; (--len, ++s)) {
545 if (*s == 0)
546 terminated = 1;
547 if (terminated || *s < 0x20 || *s > 0x7e)
548 *s = ' ';
553 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
554 * @sdev: scsi_device to probe
555 * @inq_result: area to store the INQUIRY result
556 * @result_len: len of inq_result
557 * @bflags: store any bflags found here
559 * Description:
560 * Probe the lun associated with @req using a standard SCSI INQUIRY;
562 * If the INQUIRY is successful, zero is returned and the
563 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
564 * are copied to the scsi_device any flags value is stored in *@bflags.
566 static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
567 int result_len, int *bflags)
569 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
570 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
571 int response_len = 0;
572 int pass, count, result;
573 struct scsi_sense_hdr sshdr;
575 *bflags = 0;
577 /* Perform up to 3 passes. The first pass uses a conservative
578 * transfer length of 36 unless sdev->inquiry_len specifies a
579 * different value. */
580 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
581 try_inquiry_len = first_inquiry_len;
582 pass = 1;
584 next_pass:
585 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
586 "scsi scan: INQUIRY pass %d length %d\n",
587 pass, try_inquiry_len));
589 /* Each pass gets up to three chances to ignore Unit Attention */
590 for (count = 0; count < 3; ++count) {
591 int resid;
593 memset(scsi_cmd, 0, 6);
594 scsi_cmd[0] = INQUIRY;
595 scsi_cmd[4] = (unsigned char) try_inquiry_len;
597 memset(inq_result, 0, try_inquiry_len);
599 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
600 inq_result, try_inquiry_len, &sshdr,
601 HZ / 2 + HZ * scsi_inq_timeout, 3,
602 &resid);
604 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
605 "with code 0x%x\n",
606 result ? "failed" : "successful", result));
608 if (result) {
610 * not-ready to ready transition [asc/ascq=0x28/0x0]
611 * or power-on, reset [asc/ascq=0x29/0x0], continue.
612 * INQUIRY should not yield UNIT_ATTENTION
613 * but many buggy devices do so anyway.
615 if ((driver_byte(result) & DRIVER_SENSE) &&
616 scsi_sense_valid(&sshdr)) {
617 if ((sshdr.sense_key == UNIT_ATTENTION) &&
618 ((sshdr.asc == 0x28) ||
619 (sshdr.asc == 0x29)) &&
620 (sshdr.ascq == 0))
621 continue;
623 } else {
625 * if nothing was transferred, we try
626 * again. It's a workaround for some USB
627 * devices.
629 if (resid == try_inquiry_len)
630 continue;
632 break;
635 if (result == 0) {
636 sanitize_inquiry_string(&inq_result[8], 8);
637 sanitize_inquiry_string(&inq_result[16], 16);
638 sanitize_inquiry_string(&inq_result[32], 4);
640 response_len = inq_result[4] + 5;
641 if (response_len > 255)
642 response_len = first_inquiry_len; /* sanity */
645 * Get any flags for this device.
647 * XXX add a bflags to scsi_device, and replace the
648 * corresponding bit fields in scsi_device, so bflags
649 * need not be passed as an argument.
651 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
652 &inq_result[16]);
654 /* When the first pass succeeds we gain information about
655 * what larger transfer lengths might work. */
656 if (pass == 1) {
657 if (BLIST_INQUIRY_36 & *bflags)
658 next_inquiry_len = 36;
659 else if (BLIST_INQUIRY_58 & *bflags)
660 next_inquiry_len = 58;
661 else if (sdev->inquiry_len)
662 next_inquiry_len = sdev->inquiry_len;
663 else
664 next_inquiry_len = response_len;
666 /* If more data is available perform the second pass */
667 if (next_inquiry_len > try_inquiry_len) {
668 try_inquiry_len = next_inquiry_len;
669 pass = 2;
670 goto next_pass;
674 } else if (pass == 2) {
675 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
676 "Consider BLIST_INQUIRY_36 for this device\n",
677 try_inquiry_len);
679 /* If this pass failed, the third pass goes back and transfers
680 * the same amount as we successfully got in the first pass. */
681 try_inquiry_len = first_inquiry_len;
682 pass = 3;
683 goto next_pass;
686 /* If the last transfer attempt got an error, assume the
687 * peripheral doesn't exist or is dead. */
688 if (result)
689 return -EIO;
691 /* Don't report any more data than the device says is valid */
692 sdev->inquiry_len = min(try_inquiry_len, response_len);
695 * XXX Abort if the response length is less than 36? If less than
696 * 32, the lookup of the device flags (above) could be invalid,
697 * and it would be possible to take an incorrect action - we do
698 * not want to hang because of a short INQUIRY. On the flip side,
699 * if the device is spun down or becoming ready (and so it gives a
700 * short INQUIRY), an abort here prevents any further use of the
701 * device, including spin up.
703 * On the whole, the best approach seems to be to assume the first
704 * 36 bytes are valid no matter what the device says. That's
705 * better than copying < 36 bytes to the inquiry-result buffer
706 * and displaying garbage for the Vendor, Product, or Revision
707 * strings.
709 if (sdev->inquiry_len < 36) {
710 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
711 " using 36\n", sdev->inquiry_len);
712 sdev->inquiry_len = 36;
716 * Related to the above issue:
718 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
719 * and if not ready, sent a START_STOP to start (maybe spin up) and
720 * then send the INQUIRY again, since the INQUIRY can change after
721 * a device is initialized.
723 * Ideally, start a device if explicitly asked to do so. This
724 * assumes that a device is spun up on power on, spun down on
725 * request, and then spun up on request.
729 * The scanning code needs to know the scsi_level, even if no
730 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
731 * non-zero LUNs can be scanned.
733 sdev->scsi_level = inq_result[2] & 0x07;
734 if (sdev->scsi_level >= 2 ||
735 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
736 sdev->scsi_level++;
737 sdev->sdev_target->scsi_level = sdev->scsi_level;
739 return 0;
743 * scsi_add_lun - allocate and fully initialze a scsi_device
744 * @sdev: holds information to be stored in the new scsi_device
745 * @inq_result: holds the result of a previous INQUIRY to the LUN
746 * @bflags: black/white list flag
747 * @async: 1 if this device is being scanned asynchronously
749 * Description:
750 * Initialize the scsi_device @sdev. Optionally set fields based
751 * on values in *@bflags.
753 * Return:
754 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
755 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
757 static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
758 int *bflags, int async)
760 int ret;
763 * XXX do not save the inquiry, since it can change underneath us,
764 * save just vendor/model/rev.
766 * Rather than save it and have an ioctl that retrieves the saved
767 * value, have an ioctl that executes the same INQUIRY code used
768 * in scsi_probe_lun, let user level programs doing INQUIRY
769 * scanning run at their own risk, or supply a user level program
770 * that can correctly scan.
774 * Copy at least 36 bytes of INQUIRY data, so that we don't
775 * dereference unallocated memory when accessing the Vendor,
776 * Product, and Revision strings. Badly behaved devices may set
777 * the INQUIRY Additional Length byte to a small value, indicating
778 * these strings are invalid, but often they contain plausible data
779 * nonetheless. It doesn't matter if the device sent < 36 bytes
780 * total, since scsi_probe_lun() initializes inq_result with 0s.
782 sdev->inquiry = kmemdup(inq_result,
783 max_t(size_t, sdev->inquiry_len, 36),
784 GFP_ATOMIC);
785 if (sdev->inquiry == NULL)
786 return SCSI_SCAN_NO_RESPONSE;
788 sdev->vendor = (char *) (sdev->inquiry + 8);
789 sdev->model = (char *) (sdev->inquiry + 16);
790 sdev->rev = (char *) (sdev->inquiry + 32);
792 if (strncmp(sdev->vendor, "ATA ", 8) == 0) {
794 * sata emulation layer device. This is a hack to work around
795 * the SATL power management specifications which state that
796 * when the SATL detects the device has gone into standby
797 * mode, it shall respond with NOT READY.
799 sdev->allow_restart = 1;
802 if (*bflags & BLIST_ISROM) {
803 sdev->type = TYPE_ROM;
804 sdev->removable = 1;
805 } else {
806 sdev->type = (inq_result[0] & 0x1f);
807 sdev->removable = (inq_result[1] & 0x80) >> 7;
810 switch (sdev->type) {
811 case TYPE_RBC:
812 case TYPE_TAPE:
813 case TYPE_DISK:
814 case TYPE_PRINTER:
815 case TYPE_MOD:
816 case TYPE_PROCESSOR:
817 case TYPE_SCANNER:
818 case TYPE_MEDIUM_CHANGER:
819 case TYPE_ENCLOSURE:
820 case TYPE_COMM:
821 case TYPE_RAID:
822 case TYPE_OSD:
823 sdev->writeable = 1;
824 break;
825 case TYPE_ROM:
826 case TYPE_WORM:
827 sdev->writeable = 0;
828 break;
829 default:
830 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
833 if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
834 /* RBC and MMC devices can return SCSI-3 compliance and yet
835 * still not support REPORT LUNS, so make them act as
836 * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
837 * specifically set */
838 if ((*bflags & BLIST_REPORTLUN2) == 0)
839 *bflags |= BLIST_NOREPORTLUN;
843 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
844 * spec says: The device server is capable of supporting the
845 * specified peripheral device type on this logical unit. However,
846 * the physical device is not currently connected to this logical
847 * unit.
849 * The above is vague, as it implies that we could treat 001 and
850 * 011 the same. Stay compatible with previous code, and create a
851 * scsi_device for a PQ of 1
853 * Don't set the device offline here; rather let the upper
854 * level drivers eval the PQ to decide whether they should
855 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
858 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
859 sdev->lockable = sdev->removable;
860 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
862 if (sdev->scsi_level >= SCSI_3 ||
863 (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
864 sdev->ppr = 1;
865 if (inq_result[7] & 0x60)
866 sdev->wdtr = 1;
867 if (inq_result[7] & 0x10)
868 sdev->sdtr = 1;
870 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
871 "ANSI: %d%s\n", scsi_device_type(sdev->type),
872 sdev->vendor, sdev->model, sdev->rev,
873 sdev->inq_periph_qual, inq_result[2] & 0x07,
874 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
876 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
877 !(*bflags & BLIST_NOTQ))
878 sdev->tagged_supported = 1;
881 * Some devices (Texel CD ROM drives) have handshaking problems
882 * when used with the Seagate controllers. borken is initialized
883 * to 1, and then set it to 0 here.
885 if ((*bflags & BLIST_BORKEN) == 0)
886 sdev->borken = 0;
888 if (*bflags & BLIST_NO_ULD_ATTACH)
889 sdev->no_uld_attach = 1;
892 * Apparently some really broken devices (contrary to the SCSI
893 * standards) need to be selected without asserting ATN
895 if (*bflags & BLIST_SELECT_NO_ATN)
896 sdev->select_no_atn = 1;
899 * Maximum 512 sector transfer length
900 * broken RA4x00 Compaq Disk Array
902 if (*bflags & BLIST_MAX_512)
903 blk_queue_max_hw_sectors(sdev->request_queue, 512);
906 * Some devices may not want to have a start command automatically
907 * issued when a device is added.
909 if (*bflags & BLIST_NOSTARTONADD)
910 sdev->no_start_on_add = 1;
912 if (*bflags & BLIST_SINGLELUN)
913 scsi_target(sdev)->single_lun = 1;
915 sdev->use_10_for_rw = 1;
917 if (*bflags & BLIST_MS_SKIP_PAGE_08)
918 sdev->skip_ms_page_8 = 1;
920 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
921 sdev->skip_ms_page_3f = 1;
923 if (*bflags & BLIST_USE_10_BYTE_MS)
924 sdev->use_10_for_ms = 1;
926 /* some devices don't like REPORT SUPPORTED OPERATION CODES
927 * and will simply timeout causing sd_mod init to take a very
928 * very long time */
929 if (*bflags & BLIST_NO_RSOC)
930 sdev->no_report_opcodes = 1;
932 /* set the device running here so that slave configure
933 * may do I/O */
934 ret = scsi_device_set_state(sdev, SDEV_RUNNING);
935 if (ret) {
936 ret = scsi_device_set_state(sdev, SDEV_BLOCK);
938 if (ret) {
939 sdev_printk(KERN_ERR, sdev,
940 "in wrong state %s to complete scan\n",
941 scsi_device_state_name(sdev->sdev_state));
942 return SCSI_SCAN_NO_RESPONSE;
946 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
947 sdev->use_192_bytes_for_3f = 1;
949 if (*bflags & BLIST_NOT_LOCKABLE)
950 sdev->lockable = 0;
952 if (*bflags & BLIST_RETRY_HWERROR)
953 sdev->retry_hwerror = 1;
955 if (*bflags & BLIST_NO_DIF)
956 sdev->no_dif = 1;
958 sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT;
960 if (*bflags & BLIST_TRY_VPD_PAGES)
961 sdev->try_vpd_pages = 1;
962 else if (*bflags & BLIST_SKIP_VPD_PAGES)
963 sdev->skip_vpd_pages = 1;
965 transport_configure_device(&sdev->sdev_gendev);
967 if (sdev->host->hostt->slave_configure) {
968 ret = sdev->host->hostt->slave_configure(sdev);
969 if (ret) {
971 * if LLDD reports slave not present, don't clutter
972 * console with alloc failure messages
974 if (ret != -ENXIO) {
975 sdev_printk(KERN_ERR, sdev,
976 "failed to configure device\n");
978 return SCSI_SCAN_NO_RESPONSE;
982 sdev->max_queue_depth = sdev->queue_depth;
985 * Ok, the device is now all set up, we can
986 * register it and tell the rest of the kernel
987 * about it.
989 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
990 return SCSI_SCAN_NO_RESPONSE;
992 return SCSI_SCAN_LUN_PRESENT;
995 #ifdef CONFIG_SCSI_LOGGING
996 /**
997 * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace
998 * @buf: Output buffer with at least end-first+1 bytes of space
999 * @inq: Inquiry buffer (input)
1000 * @first: Offset of string into inq
1001 * @end: Index after last character in inq
1003 static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
1004 unsigned first, unsigned end)
1006 unsigned term = 0, idx;
1008 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
1009 if (inq[idx+first] > ' ') {
1010 buf[idx] = inq[idx+first];
1011 term = idx+1;
1012 } else {
1013 buf[idx] = ' ';
1016 buf[term] = 0;
1017 return buf;
1019 #endif
1022 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
1023 * @starget: pointer to target device structure
1024 * @lun: LUN of target device
1025 * @bflagsp: store bflags here if not NULL
1026 * @sdevp: probe the LUN corresponding to this scsi_device
1027 * @rescan: if nonzero skip some code only needed on first scan
1028 * @hostdata: passed to scsi_alloc_sdev()
1030 * Description:
1031 * Call scsi_probe_lun, if a LUN with an attached device is found,
1032 * allocate and set it up by calling scsi_add_lun.
1034 * Return:
1035 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
1036 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
1037 * attached at the LUN
1038 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
1040 static int scsi_probe_and_add_lun(struct scsi_target *starget,
1041 uint lun, int *bflagsp,
1042 struct scsi_device **sdevp, int rescan,
1043 void *hostdata)
1045 struct scsi_device *sdev;
1046 unsigned char *result;
1047 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
1048 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1051 * The rescan flag is used as an optimization, the first scan of a
1052 * host adapter calls into here with rescan == 0.
1054 sdev = scsi_device_lookup_by_target(starget, lun);
1055 if (sdev) {
1056 if (rescan || !scsi_device_created(sdev)) {
1057 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1058 "scsi scan: device exists on %s\n",
1059 dev_name(&sdev->sdev_gendev)));
1060 if (sdevp)
1061 *sdevp = sdev;
1062 else
1063 scsi_device_put(sdev);
1065 if (bflagsp)
1066 *bflagsp = scsi_get_device_flags(sdev,
1067 sdev->vendor,
1068 sdev->model);
1069 return SCSI_SCAN_LUN_PRESENT;
1071 scsi_device_put(sdev);
1072 } else
1073 sdev = scsi_alloc_sdev(starget, lun, hostdata);
1074 if (!sdev)
1075 goto out;
1077 result = kmalloc(result_len, GFP_ATOMIC |
1078 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
1079 if (!result)
1080 goto out_free_sdev;
1082 if (scsi_probe_lun(sdev, result, result_len, &bflags))
1083 goto out_free_result;
1085 if (bflagsp)
1086 *bflagsp = bflags;
1088 * result contains valid SCSI INQUIRY data.
1090 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
1092 * For a Peripheral qualifier 3 (011b), the SCSI
1093 * spec says: The device server is not capable of
1094 * supporting a physical device on this logical
1095 * unit.
1097 * For disks, this implies that there is no
1098 * logical disk configured at sdev->lun, but there
1099 * is a target id responding.
1101 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1102 " peripheral qualifier of 3, device not"
1103 " added\n"))
1104 if (lun == 0) {
1105 SCSI_LOG_SCAN_BUS(1, {
1106 unsigned char vend[9];
1107 unsigned char mod[17];
1109 sdev_printk(KERN_INFO, sdev,
1110 "scsi scan: consider passing scsi_mod."
1111 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1112 scsi_inq_str(vend, result, 8, 16),
1113 scsi_inq_str(mod, result, 16, 32));
1118 res = SCSI_SCAN_TARGET_PRESENT;
1119 goto out_free_result;
1123 * Some targets may set slight variations of PQ and PDT to signal
1124 * that no LUN is present, so don't add sdev in these cases.
1125 * Two specific examples are:
1126 * 1) NetApp targets: return PQ=1, PDT=0x1f
1127 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1128 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1130 * References:
1131 * 1) SCSI SPC-3, pp. 145-146
1132 * PQ=1: "A peripheral device having the specified peripheral
1133 * device type is not connected to this logical unit. However, the
1134 * device server is capable of supporting the specified peripheral
1135 * device type on this logical unit."
1136 * PDT=0x1f: "Unknown or no device type"
1137 * 2) USB UFI 1.0, p. 20
1138 * PDT=00h Direct-access device (floppy)
1139 * PDT=1Fh none (no FDD connected to the requested logical unit)
1141 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1142 (result[0] & 0x1f) == 0x1f &&
1143 !scsi_is_wlun(lun)) {
1144 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1145 "scsi scan: peripheral device type"
1146 " of 31, no device added\n"));
1147 res = SCSI_SCAN_TARGET_PRESENT;
1148 goto out_free_result;
1151 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
1152 if (res == SCSI_SCAN_LUN_PRESENT) {
1153 if (bflags & BLIST_KEY) {
1154 sdev->lockable = 0;
1155 scsi_unlock_floptical(sdev, result);
1159 out_free_result:
1160 kfree(result);
1161 out_free_sdev:
1162 if (res == SCSI_SCAN_LUN_PRESENT) {
1163 if (sdevp) {
1164 if (scsi_device_get(sdev) == 0) {
1165 *sdevp = sdev;
1166 } else {
1167 __scsi_remove_device(sdev);
1168 res = SCSI_SCAN_NO_RESPONSE;
1171 } else
1172 __scsi_remove_device(sdev);
1173 out:
1174 return res;
1178 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1179 * @starget: pointer to target structure to scan
1180 * @bflags: black/white list flag for LUN 0
1181 * @scsi_level: Which version of the standard does this device adhere to
1182 * @rescan: passed to scsi_probe_add_lun()
1184 * Description:
1185 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1186 * scanned) to some maximum lun until a LUN is found with no device
1187 * attached. Use the bflags to figure out any oddities.
1189 * Modifies sdevscan->lun.
1191 static void scsi_sequential_lun_scan(struct scsi_target *starget,
1192 int bflags, int scsi_level, int rescan)
1194 unsigned int sparse_lun, lun, max_dev_lun;
1195 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1197 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1198 "%s\n", dev_name(&starget->dev)));
1200 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1202 * If this device is known to support sparse multiple units,
1203 * override the other settings, and scan all of them. Normally,
1204 * SCSI-3 devices should be scanned via the REPORT LUNS.
1206 if (bflags & BLIST_SPARSELUN) {
1207 max_dev_lun = shost->max_lun;
1208 sparse_lun = 1;
1209 } else
1210 sparse_lun = 0;
1213 * If less than SCSI_1_CSS, and no special lun scaning, stop
1214 * scanning; this matches 2.4 behaviour, but could just be a bug
1215 * (to continue scanning a SCSI_1_CSS device).
1217 * This test is broken. We might not have any device on lun0 for
1218 * a sparselun device, and if that's the case then how would we
1219 * know the real scsi_level, eh? It might make sense to just not
1220 * scan any SCSI_1 device for non-0 luns, but that check would best
1221 * go into scsi_alloc_sdev() and just have it return null when asked
1222 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1224 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1225 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1226 == 0))
1227 return;
1230 * If this device is known to support multiple units, override
1231 * the other settings, and scan all of them.
1233 if (bflags & BLIST_FORCELUN)
1234 max_dev_lun = shost->max_lun;
1236 * REGAL CDC-4X: avoid hang after LUN 4
1238 if (bflags & BLIST_MAX5LUN)
1239 max_dev_lun = min(5U, max_dev_lun);
1241 * Do not scan SCSI-2 or lower device past LUN 7, unless
1242 * BLIST_LARGELUN.
1244 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1245 max_dev_lun = min(8U, max_dev_lun);
1248 * Stop scanning at 255 unless BLIST_SCSI3LUN
1250 if (!(bflags & BLIST_SCSI3LUN))
1251 max_dev_lun = min(256U, max_dev_lun);
1254 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1255 * until we reach the max, or no LUN is found and we are not
1256 * sparse_lun.
1258 for (lun = 1; lun < max_dev_lun; ++lun)
1259 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1260 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1261 !sparse_lun)
1262 return;
1266 * scsilun_to_int - convert a scsi_lun to an int
1267 * @scsilun: struct scsi_lun to be converted.
1269 * Description:
1270 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1271 * integer, and return the result. The caller must check for
1272 * truncation before using this function.
1274 * Notes:
1275 * The struct scsi_lun is assumed to be four levels, with each level
1276 * effectively containing a SCSI byte-ordered (big endian) short; the
1277 * addressing bits of each level are ignored (the highest two bits).
1278 * For a description of the LUN format, post SCSI-3 see the SCSI
1279 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1281 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1282 * the integer: 0x0b030a04
1284 int scsilun_to_int(struct scsi_lun *scsilun)
1286 int i;
1287 unsigned int lun;
1289 lun = 0;
1290 for (i = 0; i < sizeof(lun); i += 2)
1291 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1292 scsilun->scsi_lun[i + 1]) << (i * 8));
1293 return lun;
1295 EXPORT_SYMBOL(scsilun_to_int);
1298 * int_to_scsilun - reverts an int into a scsi_lun
1299 * @lun: integer to be reverted
1300 * @scsilun: struct scsi_lun to be set.
1302 * Description:
1303 * Reverts the functionality of the scsilun_to_int, which packed
1304 * an 8-byte lun value into an int. This routine unpacks the int
1305 * back into the lun value.
1306 * Note: the scsilun_to_int() routine does not truly handle all
1307 * 8bytes of the lun value. This functions restores only as much
1308 * as was set by the routine.
1310 * Notes:
1311 * Given an integer : 0x0b030a04, this function returns a
1312 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1315 void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1317 int i;
1319 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1321 for (i = 0; i < sizeof(lun); i += 2) {
1322 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1323 scsilun->scsi_lun[i+1] = lun & 0xFF;
1324 lun = lun >> 16;
1327 EXPORT_SYMBOL(int_to_scsilun);
1330 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1331 * @starget: which target
1332 * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN
1333 * @rescan: nonzero if we can skip code only needed on first scan
1335 * Description:
1336 * Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command.
1337 * Scan the resulting list of LUNs by calling scsi_probe_and_add_lun.
1339 * If BLINK_REPORTLUN2 is set, scan a target that supports more than 8
1340 * LUNs even if it's older than SCSI-3.
1341 * If BLIST_NOREPORTLUN is set, return 1 always.
1342 * If BLIST_NOLUN is set, return 0 always.
1343 * If starget->no_report_luns is set, return 1 always.
1345 * Return:
1346 * 0: scan completed (or no memory, so further scanning is futile)
1347 * 1: could not scan with REPORT LUN
1349 static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
1350 int rescan)
1352 char devname[64];
1353 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1354 unsigned int length;
1355 unsigned int lun;
1356 unsigned int num_luns;
1357 unsigned int retries;
1358 int result;
1359 struct scsi_lun *lunp, *lun_data;
1360 u8 *data;
1361 struct scsi_sense_hdr sshdr;
1362 struct scsi_device *sdev;
1363 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1364 int ret = 0;
1367 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1368 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1369 * support more than 8 LUNs.
1370 * Don't attempt if the target doesn't support REPORT LUNS.
1372 if (bflags & BLIST_NOREPORTLUN)
1373 return 1;
1374 if (starget->scsi_level < SCSI_2 &&
1375 starget->scsi_level != SCSI_UNKNOWN)
1376 return 1;
1377 if (starget->scsi_level < SCSI_3 &&
1378 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
1379 return 1;
1380 if (bflags & BLIST_NOLUN)
1381 return 0;
1382 if (starget->no_report_luns)
1383 return 1;
1385 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1386 sdev = scsi_alloc_sdev(starget, 0, NULL);
1387 if (!sdev)
1388 return 0;
1389 if (scsi_device_get(sdev)) {
1390 __scsi_remove_device(sdev);
1391 return 0;
1395 sprintf(devname, "host %d channel %d id %d",
1396 shost->host_no, sdev->channel, sdev->id);
1399 * Allocate enough to hold the header (the same size as one scsi_lun)
1400 * plus the max number of luns we are requesting.
1402 * Reallocating and trying again (with the exact amount we need)
1403 * would be nice, but then we need to somehow limit the size
1404 * allocated based on the available memory and the limits of
1405 * kmalloc - we don't want a kmalloc() failure of a huge value to
1406 * prevent us from finding any LUNs on this target.
1408 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1409 lun_data = kmalloc(length, GFP_ATOMIC |
1410 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
1411 if (!lun_data) {
1412 printk(ALLOC_FAILURE_MSG, __func__);
1413 goto out;
1416 scsi_cmd[0] = REPORT_LUNS;
1419 * bytes 1 - 5: reserved, set to zero.
1421 memset(&scsi_cmd[1], 0, 5);
1424 * bytes 6 - 9: length of the command.
1426 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1427 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1428 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1429 scsi_cmd[9] = (unsigned char) length & 0xff;
1431 scsi_cmd[10] = 0; /* reserved */
1432 scsi_cmd[11] = 0; /* control */
1435 * We can get a UNIT ATTENTION, for example a power on/reset, so
1436 * retry a few times (like sd.c does for TEST UNIT READY).
1437 * Experience shows some combinations of adapter/devices get at
1438 * least two power on/resets.
1440 * Illegal requests (for devices that do not support REPORT LUNS)
1441 * should come through as a check condition, and will not generate
1442 * a retry.
1444 for (retries = 0; retries < 3; retries++) {
1445 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1446 " REPORT LUNS to %s (try %d)\n", devname,
1447 retries));
1449 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1450 lun_data, length, &sshdr,
1451 SCSI_REPORT_LUNS_TIMEOUT, 3, NULL);
1453 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1454 " %s (try %d) result 0x%x\n", result
1455 ? "failed" : "successful", retries, result));
1456 if (result == 0)
1457 break;
1458 else if (scsi_sense_valid(&sshdr)) {
1459 if (sshdr.sense_key != UNIT_ATTENTION)
1460 break;
1464 if (result) {
1466 * The device probably does not support a REPORT LUN command
1468 ret = 1;
1469 goto out_err;
1473 * Get the length from the first four bytes of lun_data.
1475 data = (u8 *) lun_data->scsi_lun;
1476 length = ((data[0] << 24) | (data[1] << 16) |
1477 (data[2] << 8) | (data[3] << 0));
1479 num_luns = (length / sizeof(struct scsi_lun));
1480 if (num_luns > max_scsi_report_luns) {
1481 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1482 " of %d luns reported, try increasing"
1483 " max_scsi_report_luns.\n", devname,
1484 max_scsi_report_luns, num_luns);
1485 num_luns = max_scsi_report_luns;
1488 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1489 "scsi scan: REPORT LUN scan\n"));
1492 * Scan the luns in lun_data. The entry at offset 0 is really
1493 * the header, so start at 1 and go up to and including num_luns.
1495 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1496 lun = scsilun_to_int(lunp);
1499 * Check if the unused part of lunp is non-zero, and so
1500 * does not fit in lun.
1502 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1503 int i;
1506 * Output an error displaying the LUN in byte order,
1507 * this differs from what linux would print for the
1508 * integer LUN value.
1510 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1511 data = (char *)lunp->scsi_lun;
1512 for (i = 0; i < sizeof(struct scsi_lun); i++)
1513 printk("%02x", data[i]);
1514 printk(" has a LUN larger than currently supported.\n");
1515 } else if (lun > sdev->host->max_lun) {
1516 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1517 " than allowed by the host adapter\n",
1518 devname, lun);
1519 } else {
1520 int res;
1522 res = scsi_probe_and_add_lun(starget,
1523 lun, NULL, NULL, rescan, NULL);
1524 if (res == SCSI_SCAN_NO_RESPONSE) {
1526 * Got some results, but now none, abort.
1528 sdev_printk(KERN_ERR, sdev,
1529 "Unexpected response"
1530 " from lun %d while scanning, scan"
1531 " aborted\n", lun);
1532 break;
1537 out_err:
1538 kfree(lun_data);
1539 out:
1540 scsi_device_put(sdev);
1541 if (scsi_device_created(sdev))
1543 * the sdev we used didn't appear in the report luns scan
1545 __scsi_remove_device(sdev);
1546 return ret;
1549 struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1550 uint id, uint lun, void *hostdata)
1552 struct scsi_device *sdev = ERR_PTR(-ENODEV);
1553 struct device *parent = &shost->shost_gendev;
1554 struct scsi_target *starget;
1556 if (strncmp(scsi_scan_type, "none", 4) == 0)
1557 return ERR_PTR(-ENODEV);
1559 starget = scsi_alloc_target(parent, channel, id);
1560 if (!starget)
1561 return ERR_PTR(-ENOMEM);
1562 scsi_autopm_get_target(starget);
1564 mutex_lock(&shost->scan_mutex);
1565 if (!shost->async_scan)
1566 scsi_complete_async_scans();
1568 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1569 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
1570 scsi_autopm_put_host(shost);
1572 mutex_unlock(&shost->scan_mutex);
1573 scsi_autopm_put_target(starget);
1575 * paired with scsi_alloc_target(). Target will be destroyed unless
1576 * scsi_probe_and_add_lun made an underlying device visible
1578 scsi_target_reap(starget);
1579 put_device(&starget->dev);
1581 return sdev;
1583 EXPORT_SYMBOL(__scsi_add_device);
1585 int scsi_add_device(struct Scsi_Host *host, uint channel,
1586 uint target, uint lun)
1588 struct scsi_device *sdev =
1589 __scsi_add_device(host, channel, target, lun, NULL);
1590 if (IS_ERR(sdev))
1591 return PTR_ERR(sdev);
1593 scsi_device_put(sdev);
1594 return 0;
1596 EXPORT_SYMBOL(scsi_add_device);
1598 void scsi_rescan_device(struct device *dev)
1600 struct scsi_driver *drv;
1602 if (!dev->driver)
1603 return;
1605 drv = to_scsi_driver(dev->driver);
1606 if (try_module_get(drv->owner)) {
1607 if (drv->rescan)
1608 drv->rescan(dev);
1609 module_put(drv->owner);
1612 EXPORT_SYMBOL(scsi_rescan_device);
1614 static void __scsi_scan_target(struct device *parent, unsigned int channel,
1615 unsigned int id, unsigned int lun, int rescan)
1617 struct Scsi_Host *shost = dev_to_shost(parent);
1618 int bflags = 0;
1619 int res;
1620 struct scsi_target *starget;
1622 if (shost->this_id == id)
1624 * Don't scan the host adapter
1626 return;
1628 starget = scsi_alloc_target(parent, channel, id);
1629 if (!starget)
1630 return;
1631 scsi_autopm_get_target(starget);
1633 if (lun != SCAN_WILD_CARD) {
1635 * Scan for a specific host/chan/id/lun.
1637 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1638 goto out_reap;
1642 * Scan LUN 0, if there is some response, scan further. Ideally, we
1643 * would not configure LUN 0 until all LUNs are scanned.
1645 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1646 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1647 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1649 * The REPORT LUN did not scan the target,
1650 * do a sequential scan.
1652 scsi_sequential_lun_scan(starget, bflags,
1653 starget->scsi_level, rescan);
1656 out_reap:
1657 scsi_autopm_put_target(starget);
1659 * paired with scsi_alloc_target(): determine if the target has
1660 * any children at all and if not, nuke it
1662 scsi_target_reap(starget);
1664 put_device(&starget->dev);
1668 * scsi_scan_target - scan a target id, possibly including all LUNs on the target.
1669 * @parent: host to scan
1670 * @channel: channel to scan
1671 * @id: target id to scan
1672 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1673 * @rescan: passed to LUN scanning routines
1675 * Description:
1676 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1677 * and possibly all LUNs on the target id.
1679 * First try a REPORT LUN scan, if that does not scan the target, do a
1680 * sequential scan of LUNs on the target id.
1682 void scsi_scan_target(struct device *parent, unsigned int channel,
1683 unsigned int id, unsigned int lun, int rescan)
1685 struct Scsi_Host *shost = dev_to_shost(parent);
1687 if (strncmp(scsi_scan_type, "none", 4) == 0)
1688 return;
1690 mutex_lock(&shost->scan_mutex);
1691 if (!shost->async_scan)
1692 scsi_complete_async_scans();
1694 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1695 __scsi_scan_target(parent, channel, id, lun, rescan);
1696 scsi_autopm_put_host(shost);
1698 mutex_unlock(&shost->scan_mutex);
1700 EXPORT_SYMBOL(scsi_scan_target);
1702 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1703 unsigned int id, unsigned int lun, int rescan)
1705 uint order_id;
1707 if (id == SCAN_WILD_CARD)
1708 for (id = 0; id < shost->max_id; ++id) {
1710 * XXX adapter drivers when possible (FCP, iSCSI)
1711 * could modify max_id to match the current max,
1712 * not the absolute max.
1714 * XXX add a shost id iterator, so for example,
1715 * the FC ID can be the same as a target id
1716 * without a huge overhead of sparse id's.
1718 if (shost->reverse_ordering)
1720 * Scan from high to low id.
1722 order_id = shost->max_id - id - 1;
1723 else
1724 order_id = id;
1725 __scsi_scan_target(&shost->shost_gendev, channel,
1726 order_id, lun, rescan);
1728 else
1729 __scsi_scan_target(&shost->shost_gendev, channel,
1730 id, lun, rescan);
1733 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1734 unsigned int id, unsigned int lun, int rescan)
1736 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1737 "%s: <%u:%u:%u>\n",
1738 __func__, channel, id, lun));
1740 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1741 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1742 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1743 return -EINVAL;
1745 mutex_lock(&shost->scan_mutex);
1746 if (!shost->async_scan)
1747 scsi_complete_async_scans();
1749 if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
1750 if (channel == SCAN_WILD_CARD)
1751 for (channel = 0; channel <= shost->max_channel;
1752 channel++)
1753 scsi_scan_channel(shost, channel, id, lun,
1754 rescan);
1755 else
1756 scsi_scan_channel(shost, channel, id, lun, rescan);
1757 scsi_autopm_put_host(shost);
1759 mutex_unlock(&shost->scan_mutex);
1761 return 0;
1764 static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1766 struct scsi_device *sdev;
1767 shost_for_each_device(sdev, shost) {
1768 /* target removed before the device could be added */
1769 if (sdev->sdev_state == SDEV_DEL)
1770 continue;
1771 if (!scsi_host_scan_allowed(shost) ||
1772 scsi_sysfs_add_sdev(sdev) != 0)
1773 __scsi_remove_device(sdev);
1778 * scsi_prep_async_scan - prepare for an async scan
1779 * @shost: the host which will be scanned
1780 * Returns: a cookie to be passed to scsi_finish_async_scan()
1782 * Tells the midlayer this host is going to do an asynchronous scan.
1783 * It reserves the host's position in the scanning list and ensures
1784 * that other asynchronous scans started after this one won't affect the
1785 * ordering of the discovered devices.
1787 static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1789 struct async_scan_data *data;
1790 unsigned long flags;
1792 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1793 return NULL;
1795 if (shost->async_scan) {
1796 printk("%s called twice for host %d", __func__,
1797 shost->host_no);
1798 dump_stack();
1799 return NULL;
1802 data = kmalloc(sizeof(*data), GFP_KERNEL);
1803 if (!data)
1804 goto err;
1805 data->shost = scsi_host_get(shost);
1806 if (!data->shost)
1807 goto err;
1808 init_completion(&data->prev_finished);
1810 mutex_lock(&shost->scan_mutex);
1811 spin_lock_irqsave(shost->host_lock, flags);
1812 shost->async_scan = 1;
1813 spin_unlock_irqrestore(shost->host_lock, flags);
1814 mutex_unlock(&shost->scan_mutex);
1816 spin_lock(&async_scan_lock);
1817 if (list_empty(&scanning_hosts))
1818 complete(&data->prev_finished);
1819 list_add_tail(&data->list, &scanning_hosts);
1820 spin_unlock(&async_scan_lock);
1822 return data;
1824 err:
1825 kfree(data);
1826 return NULL;
1830 * scsi_finish_async_scan - asynchronous scan has finished
1831 * @data: cookie returned from earlier call to scsi_prep_async_scan()
1833 * All the devices currently attached to this host have been found.
1834 * This function announces all the devices it has found to the rest
1835 * of the system.
1837 static void scsi_finish_async_scan(struct async_scan_data *data)
1839 struct Scsi_Host *shost;
1840 unsigned long flags;
1842 if (!data)
1843 return;
1845 shost = data->shost;
1847 mutex_lock(&shost->scan_mutex);
1849 if (!shost->async_scan) {
1850 printk("%s called twice for host %d", __func__,
1851 shost->host_no);
1852 dump_stack();
1853 mutex_unlock(&shost->scan_mutex);
1854 return;
1857 wait_for_completion(&data->prev_finished);
1859 scsi_sysfs_add_devices(shost);
1861 spin_lock_irqsave(shost->host_lock, flags);
1862 shost->async_scan = 0;
1863 spin_unlock_irqrestore(shost->host_lock, flags);
1865 mutex_unlock(&shost->scan_mutex);
1867 spin_lock(&async_scan_lock);
1868 list_del(&data->list);
1869 if (!list_empty(&scanning_hosts)) {
1870 struct async_scan_data *next = list_entry(scanning_hosts.next,
1871 struct async_scan_data, list);
1872 complete(&next->prev_finished);
1874 spin_unlock(&async_scan_lock);
1876 scsi_autopm_put_host(shost);
1877 scsi_host_put(shost);
1878 kfree(data);
1881 static void do_scsi_scan_host(struct Scsi_Host *shost)
1883 if (shost->hostt->scan_finished) {
1884 unsigned long start = jiffies;
1885 if (shost->hostt->scan_start)
1886 shost->hostt->scan_start(shost);
1888 while (!shost->hostt->scan_finished(shost, jiffies - start))
1889 msleep(10);
1890 } else {
1891 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1892 SCAN_WILD_CARD, 0);
1896 static void do_scan_async(void *_data, async_cookie_t c)
1898 struct async_scan_data *data = _data;
1899 struct Scsi_Host *shost = data->shost;
1901 do_scsi_scan_host(shost);
1902 scsi_finish_async_scan(data);
1906 * scsi_scan_host - scan the given adapter
1907 * @shost: adapter to scan
1909 void scsi_scan_host(struct Scsi_Host *shost)
1911 struct async_scan_data *data;
1913 if (strncmp(scsi_scan_type, "none", 4) == 0)
1914 return;
1915 if (scsi_autopm_get_host(shost) < 0)
1916 return;
1918 data = scsi_prep_async_scan(shost);
1919 if (!data) {
1920 do_scsi_scan_host(shost);
1921 scsi_autopm_put_host(shost);
1922 return;
1925 /* register with the async subsystem so wait_for_device_probe()
1926 * will flush this work
1928 async_schedule(do_scan_async, data);
1930 /* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */
1932 EXPORT_SYMBOL(scsi_scan_host);
1934 void scsi_forget_host(struct Scsi_Host *shost)
1936 struct scsi_device *sdev;
1937 unsigned long flags;
1939 restart:
1940 spin_lock_irqsave(shost->host_lock, flags);
1941 list_for_each_entry(sdev, &shost->__devices, siblings) {
1942 if (sdev->sdev_state == SDEV_DEL)
1943 continue;
1944 spin_unlock_irqrestore(shost->host_lock, flags);
1945 __scsi_remove_device(sdev);
1946 goto restart;
1948 spin_unlock_irqrestore(shost->host_lock, flags);
1952 * scsi_get_host_dev - Create a scsi_device that points to the host adapter itself
1953 * @shost: Host that needs a scsi_device
1955 * Lock status: None assumed.
1957 * Returns: The scsi_device or NULL
1959 * Notes:
1960 * Attach a single scsi_device to the Scsi_Host - this should
1961 * be made to look like a "pseudo-device" that points to the
1962 * HA itself.
1964 * Note - this device is not accessible from any high-level
1965 * drivers (including generics), which is probably not
1966 * optimal. We can add hooks later to attach.
1968 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1970 struct scsi_device *sdev = NULL;
1971 struct scsi_target *starget;
1973 mutex_lock(&shost->scan_mutex);
1974 if (!scsi_host_scan_allowed(shost))
1975 goto out;
1976 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1977 if (!starget)
1978 goto out;
1980 sdev = scsi_alloc_sdev(starget, 0, NULL);
1981 if (sdev)
1982 sdev->borken = 0;
1983 else
1984 scsi_target_reap(starget);
1985 put_device(&starget->dev);
1986 out:
1987 mutex_unlock(&shost->scan_mutex);
1988 return sdev;
1990 EXPORT_SYMBOL(scsi_get_host_dev);
1993 * scsi_free_host_dev - Free a scsi_device that points to the host adapter itself
1994 * @sdev: Host device to be freed
1996 * Lock status: None assumed.
1998 * Returns: Nothing
2000 void scsi_free_host_dev(struct scsi_device *sdev)
2002 BUG_ON(sdev->id != sdev->host->this_id);
2004 __scsi_remove_device(sdev);
2006 EXPORT_SYMBOL(scsi_free_host_dev);