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