Kobject: convert block/elevator.c to use kobject_init/add_ng()
[linux-2.6/mini2440.git] / drivers / scsi / scsi_scan.c
blob40ea71cd2ca6d5b9a7accefa25de353a98fd1b6d
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, uint, S_IRUGO|S_IWUSR);
89 MODULE_PARM_DESC(max_luns,
90 "last scsi LUN (should be between 1 and 2^32-1)");
92 #ifdef CONFIG_SCSI_SCAN_ASYNC
93 #define SCSI_SCAN_TYPE_DEFAULT "async"
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, uint, S_IRUGO|S_IWUSR);
113 MODULE_PARM_DESC(max_report_luns,
114 "REPORT LUNS maximum number of LUNS received (should be"
115 " between 1 and 16384)");
117 static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
119 module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR);
120 MODULE_PARM_DESC(inq_timeout,
121 "Timeout (in seconds) waiting for devices to answer INQUIRY."
122 " Default is 5. Some non-compliant devices need more.");
124 /* This lock protects only this list */
125 static DEFINE_SPINLOCK(async_scan_lock);
126 static LIST_HEAD(scanning_hosts);
128 struct async_scan_data {
129 struct list_head list;
130 struct Scsi_Host *shost;
131 struct completion prev_finished;
135 * scsi_complete_async_scans - Wait for asynchronous scans to complete
137 * When this function returns, any host which started scanning before
138 * this function was called will have finished its scan. Hosts which
139 * started scanning after this function was called may or may not have
140 * finished.
142 int scsi_complete_async_scans(void)
144 struct async_scan_data *data;
146 do {
147 if (list_empty(&scanning_hosts))
148 return 0;
149 /* If we can't get memory immediately, that's OK. Just
150 * sleep a little. Even if we never get memory, the async
151 * scans will finish eventually.
153 data = kmalloc(sizeof(*data), GFP_KERNEL);
154 if (!data)
155 msleep(1);
156 } while (!data);
158 data->shost = NULL;
159 init_completion(&data->prev_finished);
161 spin_lock(&async_scan_lock);
162 /* Check that there's still somebody else on the list */
163 if (list_empty(&scanning_hosts))
164 goto done;
165 list_add_tail(&data->list, &scanning_hosts);
166 spin_unlock(&async_scan_lock);
168 printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
169 wait_for_completion(&data->prev_finished);
171 spin_lock(&async_scan_lock);
172 list_del(&data->list);
173 if (!list_empty(&scanning_hosts)) {
174 struct async_scan_data *next = list_entry(scanning_hosts.next,
175 struct async_scan_data, list);
176 complete(&next->prev_finished);
178 done:
179 spin_unlock(&async_scan_lock);
181 kfree(data);
182 return 0;
185 /* Only exported for the benefit of scsi_wait_scan */
186 EXPORT_SYMBOL_GPL(scsi_complete_async_scans);
188 #ifndef MODULE
190 * For async scanning we need to wait for all the scans to complete before
191 * trying to mount the root fs. Otherwise non-modular drivers may not be ready
192 * yet.
194 late_initcall(scsi_complete_async_scans);
195 #endif
198 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
199 * @sdev: scsi device to send command to
200 * @result: area to store the result of the MODE SENSE
202 * Description:
203 * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
204 * Called for BLIST_KEY devices.
206 static void scsi_unlock_floptical(struct scsi_device *sdev,
207 unsigned char *result)
209 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
211 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
212 scsi_cmd[0] = MODE_SENSE;
213 scsi_cmd[1] = 0;
214 scsi_cmd[2] = 0x2e;
215 scsi_cmd[3] = 0;
216 scsi_cmd[4] = 0x2a; /* size */
217 scsi_cmd[5] = 0;
218 scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
219 SCSI_TIMEOUT, 3);
223 * scsi_alloc_sdev - allocate and setup a scsi_Device
225 * Description:
226 * Allocate, initialize for io, and return a pointer to a scsi_Device.
227 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
228 * adds scsi_Device to the appropriate list.
230 * Return value:
231 * scsi_Device pointer, or NULL on failure.
233 static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
234 unsigned int lun, void *hostdata)
236 struct scsi_device *sdev;
237 int display_failure_msg = 1, ret;
238 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
239 extern void scsi_evt_thread(struct work_struct *work);
241 sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
242 GFP_ATOMIC);
243 if (!sdev)
244 goto out;
246 sdev->vendor = scsi_null_device_strs;
247 sdev->model = scsi_null_device_strs;
248 sdev->rev = scsi_null_device_strs;
249 sdev->host = shost;
250 sdev->id = starget->id;
251 sdev->lun = lun;
252 sdev->channel = starget->channel;
253 sdev->sdev_state = SDEV_CREATED;
254 INIT_LIST_HEAD(&sdev->siblings);
255 INIT_LIST_HEAD(&sdev->same_target_siblings);
256 INIT_LIST_HEAD(&sdev->cmd_list);
257 INIT_LIST_HEAD(&sdev->starved_entry);
258 INIT_LIST_HEAD(&sdev->event_list);
259 spin_lock_init(&sdev->list_lock);
260 INIT_WORK(&sdev->event_work, scsi_evt_thread);
262 sdev->sdev_gendev.parent = get_device(&starget->dev);
263 sdev->sdev_target = starget;
265 /* usually NULL and set by ->slave_alloc instead */
266 sdev->hostdata = hostdata;
268 /* if the device needs this changing, it may do so in the
269 * slave_configure function */
270 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
273 * Some low level driver could use device->type
275 sdev->type = -1;
278 * Assume that the device will have handshaking problems,
279 * and then fix this field later if it turns out it
280 * doesn't
282 sdev->borken = 1;
284 sdev->request_queue = scsi_alloc_queue(sdev);
285 if (!sdev->request_queue) {
286 /* release fn is set up in scsi_sysfs_device_initialise, so
287 * have to free and put manually here */
288 put_device(&starget->dev);
289 kfree(sdev);
290 goto out;
293 sdev->request_queue->queuedata = sdev;
294 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
296 scsi_sysfs_device_initialize(sdev);
298 if (shost->hostt->slave_alloc) {
299 ret = shost->hostt->slave_alloc(sdev);
300 if (ret) {
302 * if LLDD reports slave not present, don't clutter
303 * console with alloc failure messages
305 if (ret == -ENXIO)
306 display_failure_msg = 0;
307 goto out_device_destroy;
311 return sdev;
313 out_device_destroy:
314 transport_destroy_device(&sdev->sdev_gendev);
315 put_device(&sdev->sdev_gendev);
316 out:
317 if (display_failure_msg)
318 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
319 return NULL;
322 static void scsi_target_dev_release(struct device *dev)
324 struct device *parent = dev->parent;
325 struct scsi_target *starget = to_scsi_target(dev);
327 kfree(starget);
328 put_device(parent);
331 int scsi_is_target_device(const struct device *dev)
333 return dev->release == scsi_target_dev_release;
335 EXPORT_SYMBOL(scsi_is_target_device);
337 static struct scsi_target *__scsi_find_target(struct device *parent,
338 int channel, uint id)
340 struct scsi_target *starget, *found_starget = NULL;
341 struct Scsi_Host *shost = dev_to_shost(parent);
343 * Search for an existing target for this sdev.
345 list_for_each_entry(starget, &shost->__targets, siblings) {
346 if (starget->id == id &&
347 starget->channel == channel) {
348 found_starget = starget;
349 break;
352 if (found_starget)
353 get_device(&found_starget->dev);
355 return found_starget;
359 * scsi_alloc_target - allocate a new or find an existing target
360 * @parent: parent of the target (need not be a scsi host)
361 * @channel: target channel number (zero if no channels)
362 * @id: target id number
364 * Return an existing target if one exists, provided it hasn't already
365 * gone into STARGET_DEL state, otherwise allocate a new target.
367 * The target is returned with an incremented reference, so the caller
368 * is responsible for both reaping and doing a last put
370 static struct scsi_target *scsi_alloc_target(struct device *parent,
371 int channel, uint id)
373 struct Scsi_Host *shost = dev_to_shost(parent);
374 struct device *dev = NULL;
375 unsigned long flags;
376 const int size = sizeof(struct scsi_target)
377 + shost->transportt->target_size;
378 struct scsi_target *starget;
379 struct scsi_target *found_target;
380 int error;
382 starget = kzalloc(size, GFP_KERNEL);
383 if (!starget) {
384 printk(KERN_ERR "%s: allocation failure\n", __FUNCTION__);
385 return NULL;
387 dev = &starget->dev;
388 device_initialize(dev);
389 starget->reap_ref = 1;
390 dev->parent = get_device(parent);
391 dev->release = scsi_target_dev_release;
392 sprintf(dev->bus_id, "target%d:%d:%d",
393 shost->host_no, channel, id);
394 starget->id = id;
395 starget->channel = channel;
396 INIT_LIST_HEAD(&starget->siblings);
397 INIT_LIST_HEAD(&starget->devices);
398 starget->state = STARGET_RUNNING;
399 starget->scsi_level = SCSI_2;
400 retry:
401 spin_lock_irqsave(shost->host_lock, flags);
403 found_target = __scsi_find_target(parent, channel, id);
404 if (found_target)
405 goto found;
407 list_add_tail(&starget->siblings, &shost->__targets);
408 spin_unlock_irqrestore(shost->host_lock, flags);
409 /* allocate and add */
410 transport_setup_device(dev);
411 error = device_add(dev);
412 if (error) {
413 dev_err(dev, "target device_add failed, error %d\n", error);
414 spin_lock_irqsave(shost->host_lock, flags);
415 list_del_init(&starget->siblings);
416 spin_unlock_irqrestore(shost->host_lock, flags);
417 transport_destroy_device(dev);
418 put_device(parent);
419 kfree(starget);
420 return NULL;
422 transport_add_device(dev);
423 if (shost->hostt->target_alloc) {
424 error = shost->hostt->target_alloc(starget);
426 if(error) {
427 dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
428 /* don't want scsi_target_reap to do the final
429 * put because it will be under the host lock */
430 get_device(dev);
431 scsi_target_reap(starget);
432 put_device(dev);
433 return NULL;
436 get_device(dev);
438 return starget;
440 found:
441 found_target->reap_ref++;
442 spin_unlock_irqrestore(shost->host_lock, flags);
443 if (found_target->state != STARGET_DEL) {
444 put_device(parent);
445 kfree(starget);
446 return found_target;
448 /* Unfortunately, we found a dying target; need to
449 * wait until it's dead before we can get a new one */
450 put_device(&found_target->dev);
451 flush_scheduled_work();
452 goto retry;
455 static void scsi_target_reap_usercontext(struct work_struct *work)
457 struct scsi_target *starget =
458 container_of(work, struct scsi_target, ew.work);
459 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
460 unsigned long flags;
462 transport_remove_device(&starget->dev);
463 device_del(&starget->dev);
464 transport_destroy_device(&starget->dev);
465 spin_lock_irqsave(shost->host_lock, flags);
466 if (shost->hostt->target_destroy)
467 shost->hostt->target_destroy(starget);
468 list_del_init(&starget->siblings);
469 spin_unlock_irqrestore(shost->host_lock, flags);
470 put_device(&starget->dev);
474 * scsi_target_reap - check to see if target is in use and destroy if not
476 * @starget: target to be checked
478 * This is used after removing a LUN or doing a last put of the target
479 * it checks atomically that nothing is using the target and removes
480 * it if so.
482 void scsi_target_reap(struct scsi_target *starget)
484 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
485 unsigned long flags;
487 spin_lock_irqsave(shost->host_lock, flags);
489 if (--starget->reap_ref == 0 && list_empty(&starget->devices)) {
490 BUG_ON(starget->state == STARGET_DEL);
491 starget->state = STARGET_DEL;
492 spin_unlock_irqrestore(shost->host_lock, flags);
493 execute_in_process_context(scsi_target_reap_usercontext,
494 &starget->ew);
495 return;
498 spin_unlock_irqrestore(shost->host_lock, flags);
500 return;
504 * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
505 * @s: INQUIRY result string to sanitize
506 * @len: length of the string
508 * Description:
509 * The SCSI spec says that INQUIRY vendor, product, and revision
510 * strings must consist entirely of graphic ASCII characters,
511 * padded on the right with spaces. Since not all devices obey
512 * this rule, we will replace non-graphic or non-ASCII characters
513 * with spaces. Exception: a NUL character is interpreted as a
514 * string terminator, so all the following characters are set to
515 * spaces.
517 static void sanitize_inquiry_string(unsigned char *s, int len)
519 int terminated = 0;
521 for (; len > 0; (--len, ++s)) {
522 if (*s == 0)
523 terminated = 1;
524 if (terminated || *s < 0x20 || *s > 0x7e)
525 *s = ' ';
530 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
531 * @sdev: scsi_device to probe
532 * @inq_result: area to store the INQUIRY result
533 * @result_len: len of inq_result
534 * @bflags: store any bflags found here
536 * Description:
537 * Probe the lun associated with @req using a standard SCSI INQUIRY;
539 * If the INQUIRY is successful, zero is returned and the
540 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
541 * are copied to the scsi_device any flags value is stored in *@bflags.
543 static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
544 int result_len, int *bflags)
546 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
547 int first_inquiry_len, try_inquiry_len, next_inquiry_len;
548 int response_len = 0;
549 int pass, count, result;
550 struct scsi_sense_hdr sshdr;
552 *bflags = 0;
554 /* Perform up to 3 passes. The first pass uses a conservative
555 * transfer length of 36 unless sdev->inquiry_len specifies a
556 * different value. */
557 first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
558 try_inquiry_len = first_inquiry_len;
559 pass = 1;
561 next_pass:
562 SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
563 "scsi scan: INQUIRY pass %d length %d\n",
564 pass, try_inquiry_len));
566 /* Each pass gets up to three chances to ignore Unit Attention */
567 for (count = 0; count < 3; ++count) {
568 memset(scsi_cmd, 0, 6);
569 scsi_cmd[0] = INQUIRY;
570 scsi_cmd[4] = (unsigned char) try_inquiry_len;
572 memset(inq_result, 0, try_inquiry_len);
574 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
575 inq_result, try_inquiry_len, &sshdr,
576 HZ / 2 + HZ * scsi_inq_timeout, 3);
578 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY %s "
579 "with code 0x%x\n",
580 result ? "failed" : "successful", result));
582 if (result) {
584 * not-ready to ready transition [asc/ascq=0x28/0x0]
585 * or power-on, reset [asc/ascq=0x29/0x0], continue.
586 * INQUIRY should not yield UNIT_ATTENTION
587 * but many buggy devices do so anyway.
589 if ((driver_byte(result) & DRIVER_SENSE) &&
590 scsi_sense_valid(&sshdr)) {
591 if ((sshdr.sense_key == UNIT_ATTENTION) &&
592 ((sshdr.asc == 0x28) ||
593 (sshdr.asc == 0x29)) &&
594 (sshdr.ascq == 0))
595 continue;
598 break;
601 if (result == 0) {
602 sanitize_inquiry_string(&inq_result[8], 8);
603 sanitize_inquiry_string(&inq_result[16], 16);
604 sanitize_inquiry_string(&inq_result[32], 4);
606 response_len = inq_result[4] + 5;
607 if (response_len > 255)
608 response_len = first_inquiry_len; /* sanity */
611 * Get any flags for this device.
613 * XXX add a bflags to scsi_device, and replace the
614 * corresponding bit fields in scsi_device, so bflags
615 * need not be passed as an argument.
617 *bflags = scsi_get_device_flags(sdev, &inq_result[8],
618 &inq_result[16]);
620 /* When the first pass succeeds we gain information about
621 * what larger transfer lengths might work. */
622 if (pass == 1) {
623 if (BLIST_INQUIRY_36 & *bflags)
624 next_inquiry_len = 36;
625 else if (BLIST_INQUIRY_58 & *bflags)
626 next_inquiry_len = 58;
627 else if (sdev->inquiry_len)
628 next_inquiry_len = sdev->inquiry_len;
629 else
630 next_inquiry_len = response_len;
632 /* If more data is available perform the second pass */
633 if (next_inquiry_len > try_inquiry_len) {
634 try_inquiry_len = next_inquiry_len;
635 pass = 2;
636 goto next_pass;
640 } else if (pass == 2) {
641 printk(KERN_INFO "scsi scan: %d byte inquiry failed. "
642 "Consider BLIST_INQUIRY_36 for this device\n",
643 try_inquiry_len);
645 /* If this pass failed, the third pass goes back and transfers
646 * the same amount as we successfully got in the first pass. */
647 try_inquiry_len = first_inquiry_len;
648 pass = 3;
649 goto next_pass;
652 /* If the last transfer attempt got an error, assume the
653 * peripheral doesn't exist or is dead. */
654 if (result)
655 return -EIO;
657 /* Don't report any more data than the device says is valid */
658 sdev->inquiry_len = min(try_inquiry_len, response_len);
661 * XXX Abort if the response length is less than 36? If less than
662 * 32, the lookup of the device flags (above) could be invalid,
663 * and it would be possible to take an incorrect action - we do
664 * not want to hang because of a short INQUIRY. On the flip side,
665 * if the device is spun down or becoming ready (and so it gives a
666 * short INQUIRY), an abort here prevents any further use of the
667 * device, including spin up.
669 * On the whole, the best approach seems to be to assume the first
670 * 36 bytes are valid no matter what the device says. That's
671 * better than copying < 36 bytes to the inquiry-result buffer
672 * and displaying garbage for the Vendor, Product, or Revision
673 * strings.
675 if (sdev->inquiry_len < 36) {
676 printk(KERN_INFO "scsi scan: INQUIRY result too short (%d),"
677 " using 36\n", sdev->inquiry_len);
678 sdev->inquiry_len = 36;
682 * Related to the above issue:
684 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
685 * and if not ready, sent a START_STOP to start (maybe spin up) and
686 * then send the INQUIRY again, since the INQUIRY can change after
687 * a device is initialized.
689 * Ideally, start a device if explicitly asked to do so. This
690 * assumes that a device is spun up on power on, spun down on
691 * request, and then spun up on request.
695 * The scanning code needs to know the scsi_level, even if no
696 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
697 * non-zero LUNs can be scanned.
699 sdev->scsi_level = inq_result[2] & 0x07;
700 if (sdev->scsi_level >= 2 ||
701 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
702 sdev->scsi_level++;
703 sdev->sdev_target->scsi_level = sdev->scsi_level;
705 return 0;
709 * scsi_add_lun - allocate and fully initialze a scsi_device
710 * @sdev: holds information to be stored in the new scsi_device
711 * @inq_result: holds the result of a previous INQUIRY to the LUN
712 * @bflags: black/white list flag
713 * @async: 1 if this device is being scanned asynchronously
715 * Description:
716 * Initialize the scsi_device @sdev. Optionally set fields based
717 * on values in *@bflags.
719 * Return:
720 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
721 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
723 static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
724 int *bflags, int async)
727 * XXX do not save the inquiry, since it can change underneath us,
728 * save just vendor/model/rev.
730 * Rather than save it and have an ioctl that retrieves the saved
731 * value, have an ioctl that executes the same INQUIRY code used
732 * in scsi_probe_lun, let user level programs doing INQUIRY
733 * scanning run at their own risk, or supply a user level program
734 * that can correctly scan.
738 * Copy at least 36 bytes of INQUIRY data, so that we don't
739 * dereference unallocated memory when accessing the Vendor,
740 * Product, and Revision strings. Badly behaved devices may set
741 * the INQUIRY Additional Length byte to a small value, indicating
742 * these strings are invalid, but often they contain plausible data
743 * nonetheless. It doesn't matter if the device sent < 36 bytes
744 * total, since scsi_probe_lun() initializes inq_result with 0s.
746 sdev->inquiry = kmemdup(inq_result,
747 max_t(size_t, sdev->inquiry_len, 36),
748 GFP_ATOMIC);
749 if (sdev->inquiry == NULL)
750 return SCSI_SCAN_NO_RESPONSE;
752 sdev->vendor = (char *) (sdev->inquiry + 8);
753 sdev->model = (char *) (sdev->inquiry + 16);
754 sdev->rev = (char *) (sdev->inquiry + 32);
756 if (*bflags & BLIST_ISROM) {
757 sdev->type = TYPE_ROM;
758 sdev->removable = 1;
759 } else {
760 sdev->type = (inq_result[0] & 0x1f);
761 sdev->removable = (inq_result[1] & 0x80) >> 7;
764 switch (sdev->type) {
765 case TYPE_RBC:
766 case TYPE_TAPE:
767 case TYPE_DISK:
768 case TYPE_PRINTER:
769 case TYPE_MOD:
770 case TYPE_PROCESSOR:
771 case TYPE_SCANNER:
772 case TYPE_MEDIUM_CHANGER:
773 case TYPE_ENCLOSURE:
774 case TYPE_COMM:
775 case TYPE_RAID:
776 sdev->writeable = 1;
777 break;
778 case TYPE_ROM:
779 case TYPE_WORM:
780 sdev->writeable = 0;
781 break;
782 default:
783 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
786 if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
787 /* RBC and 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;
796 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
797 * spec says: The device server is capable of supporting the
798 * specified peripheral device type on this logical unit. However,
799 * the physical device is not currently connected to this logical
800 * unit.
802 * The above is vague, as it implies that we could treat 001 and
803 * 011 the same. Stay compatible with previous code, and create a
804 * scsi_device for a PQ of 1
806 * Don't set the device offline here; rather let the upper
807 * level drivers eval the PQ to decide whether they should
808 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
811 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
812 sdev->lockable = sdev->removable;
813 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
815 if (sdev->scsi_level >= SCSI_3 ||
816 (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
817 sdev->ppr = 1;
818 if (inq_result[7] & 0x60)
819 sdev->wdtr = 1;
820 if (inq_result[7] & 0x10)
821 sdev->sdtr = 1;
823 sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
824 "ANSI: %d%s\n", scsi_device_type(sdev->type),
825 sdev->vendor, sdev->model, sdev->rev,
826 sdev->inq_periph_qual, inq_result[2] & 0x07,
827 (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
829 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
830 !(*bflags & BLIST_NOTQ))
831 sdev->tagged_supported = 1;
834 * Some devices (Texel CD ROM drives) have handshaking problems
835 * when used with the Seagate controllers. borken is initialized
836 * to 1, and then set it to 0 here.
838 if ((*bflags & BLIST_BORKEN) == 0)
839 sdev->borken = 0;
841 if (*bflags & BLIST_NO_ULD_ATTACH)
842 sdev->no_uld_attach = 1;
845 * Apparently some really broken devices (contrary to the SCSI
846 * standards) need to be selected without asserting ATN
848 if (*bflags & BLIST_SELECT_NO_ATN)
849 sdev->select_no_atn = 1;
852 * Maximum 512 sector transfer length
853 * broken RA4x00 Compaq Disk Array
855 if (*bflags & BLIST_MAX_512)
856 blk_queue_max_sectors(sdev->request_queue, 512);
859 * Some devices may not want to have a start command automatically
860 * issued when a device is added.
862 if (*bflags & BLIST_NOSTARTONADD)
863 sdev->no_start_on_add = 1;
865 if (*bflags & BLIST_SINGLELUN)
866 sdev->single_lun = 1;
868 sdev->use_10_for_rw = 1;
870 if (*bflags & BLIST_MS_SKIP_PAGE_08)
871 sdev->skip_ms_page_8 = 1;
873 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
874 sdev->skip_ms_page_3f = 1;
876 if (*bflags & BLIST_USE_10_BYTE_MS)
877 sdev->use_10_for_ms = 1;
879 /* set the device running here so that slave configure
880 * may do I/O */
881 scsi_device_set_state(sdev, SDEV_RUNNING);
883 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
884 sdev->use_192_bytes_for_3f = 1;
886 if (*bflags & BLIST_NOT_LOCKABLE)
887 sdev->lockable = 0;
889 if (*bflags & BLIST_RETRY_HWERROR)
890 sdev->retry_hwerror = 1;
892 transport_configure_device(&sdev->sdev_gendev);
894 if (sdev->host->hostt->slave_configure) {
895 int ret = sdev->host->hostt->slave_configure(sdev);
896 if (ret) {
898 * if LLDD reports slave not present, don't clutter
899 * console with alloc failure messages
901 if (ret != -ENXIO) {
902 sdev_printk(KERN_ERR, sdev,
903 "failed to configure device\n");
905 return SCSI_SCAN_NO_RESPONSE;
910 * Ok, the device is now all set up, we can
911 * register it and tell the rest of the kernel
912 * about it.
914 if (!async && scsi_sysfs_add_sdev(sdev) != 0)
915 return SCSI_SCAN_NO_RESPONSE;
917 return SCSI_SCAN_LUN_PRESENT;
920 static inline void scsi_destroy_sdev(struct scsi_device *sdev)
922 scsi_device_set_state(sdev, SDEV_DEL);
923 if (sdev->host->hostt->slave_destroy)
924 sdev->host->hostt->slave_destroy(sdev);
925 transport_destroy_device(&sdev->sdev_gendev);
926 put_device(&sdev->sdev_gendev);
929 #ifdef CONFIG_SCSI_LOGGING
930 /**
931 * scsi_inq_str - print INQUIRY data from min to max index,
932 * strip trailing whitespace
933 * @buf: Output buffer with at least end-first+1 bytes of space
934 * @inq: Inquiry buffer (input)
935 * @first: Offset of string into inq
936 * @end: Index after last character in inq
938 static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
939 unsigned first, unsigned end)
941 unsigned term = 0, idx;
943 for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
944 if (inq[idx+first] > ' ') {
945 buf[idx] = inq[idx+first];
946 term = idx+1;
947 } else {
948 buf[idx] = ' ';
951 buf[term] = 0;
952 return buf;
954 #endif
957 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
958 * @starget: pointer to target device structure
959 * @lun: LUN of target device
960 * @sdevscan: probe the LUN corresponding to this scsi_device
961 * @sdevnew: store the value of any new scsi_device allocated
962 * @bflagsp: store bflags here if not NULL
964 * Description:
965 * Call scsi_probe_lun, if a LUN with an attached device is found,
966 * allocate and set it up by calling scsi_add_lun.
968 * Return:
969 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
970 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
971 * attached at the LUN
972 * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
974 static int scsi_probe_and_add_lun(struct scsi_target *starget,
975 uint lun, int *bflagsp,
976 struct scsi_device **sdevp, int rescan,
977 void *hostdata)
979 struct scsi_device *sdev;
980 unsigned char *result;
981 int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
982 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
985 * The rescan flag is used as an optimization, the first scan of a
986 * host adapter calls into here with rescan == 0.
988 sdev = scsi_device_lookup_by_target(starget, lun);
989 if (sdev) {
990 if (rescan || sdev->sdev_state != SDEV_CREATED) {
991 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
992 "scsi scan: device exists on %s\n",
993 sdev->sdev_gendev.bus_id));
994 if (sdevp)
995 *sdevp = sdev;
996 else
997 scsi_device_put(sdev);
999 if (bflagsp)
1000 *bflagsp = scsi_get_device_flags(sdev,
1001 sdev->vendor,
1002 sdev->model);
1003 return SCSI_SCAN_LUN_PRESENT;
1005 scsi_device_put(sdev);
1006 } else
1007 sdev = scsi_alloc_sdev(starget, lun, hostdata);
1008 if (!sdev)
1009 goto out;
1011 result = kmalloc(result_len, GFP_ATOMIC |
1012 ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
1013 if (!result)
1014 goto out_free_sdev;
1016 if (scsi_probe_lun(sdev, result, result_len, &bflags))
1017 goto out_free_result;
1019 if (bflagsp)
1020 *bflagsp = bflags;
1022 * result contains valid SCSI INQUIRY data.
1024 if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
1026 * For a Peripheral qualifier 3 (011b), the SCSI
1027 * spec says: The device server is not capable of
1028 * supporting a physical device on this logical
1029 * unit.
1031 * For disks, this implies that there is no
1032 * logical disk configured at sdev->lun, but there
1033 * is a target id responding.
1035 SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
1036 " peripheral qualifier of 3, device not"
1037 " added\n"))
1038 if (lun == 0) {
1039 SCSI_LOG_SCAN_BUS(1, {
1040 unsigned char vend[9];
1041 unsigned char mod[17];
1043 sdev_printk(KERN_INFO, sdev,
1044 "scsi scan: consider passing scsi_mod."
1045 "dev_flags=%s:%s:0x240 or 0x1000240\n",
1046 scsi_inq_str(vend, result, 8, 16),
1047 scsi_inq_str(mod, result, 16, 32));
1051 res = SCSI_SCAN_TARGET_PRESENT;
1052 goto out_free_result;
1056 * Some targets may set slight variations of PQ and PDT to signal
1057 * that no LUN is present, so don't add sdev in these cases.
1058 * Two specific examples are:
1059 * 1) NetApp targets: return PQ=1, PDT=0x1f
1060 * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
1061 * in the UFI 1.0 spec (we cannot rely on reserved bits).
1063 * References:
1064 * 1) SCSI SPC-3, pp. 145-146
1065 * PQ=1: "A peripheral device having the specified peripheral
1066 * device type is not connected to this logical unit. However, the
1067 * device server is capable of supporting the specified peripheral
1068 * device type on this logical unit."
1069 * PDT=0x1f: "Unknown or no device type"
1070 * 2) USB UFI 1.0, p. 20
1071 * PDT=00h Direct-access device (floppy)
1072 * PDT=1Fh none (no FDD connected to the requested logical unit)
1074 if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
1075 (result[0] & 0x1f) == 0x1f) {
1076 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
1077 "scsi scan: peripheral device type"
1078 " of 31, no device added\n"));
1079 res = SCSI_SCAN_TARGET_PRESENT;
1080 goto out_free_result;
1083 res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
1084 if (res == SCSI_SCAN_LUN_PRESENT) {
1085 if (bflags & BLIST_KEY) {
1086 sdev->lockable = 0;
1087 scsi_unlock_floptical(sdev, result);
1091 out_free_result:
1092 kfree(result);
1093 out_free_sdev:
1094 if (res == SCSI_SCAN_LUN_PRESENT) {
1095 if (sdevp) {
1096 if (scsi_device_get(sdev) == 0) {
1097 *sdevp = sdev;
1098 } else {
1099 __scsi_remove_device(sdev);
1100 res = SCSI_SCAN_NO_RESPONSE;
1103 } else
1104 scsi_destroy_sdev(sdev);
1105 out:
1106 return res;
1110 * scsi_sequential_lun_scan - sequentially scan a SCSI target
1111 * @starget: pointer to target structure to scan
1112 * @bflags: black/white list flag for LUN 0
1114 * Description:
1115 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
1116 * scanned) to some maximum lun until a LUN is found with no device
1117 * attached. Use the bflags to figure out any oddities.
1119 * Modifies sdevscan->lun.
1121 static void scsi_sequential_lun_scan(struct scsi_target *starget,
1122 int bflags, int scsi_level, int rescan)
1124 unsigned int sparse_lun, lun, max_dev_lun;
1125 struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
1127 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
1128 "%s\n", starget->dev.bus_id));
1130 max_dev_lun = min(max_scsi_luns, shost->max_lun);
1132 * If this device is known to support sparse multiple units,
1133 * override the other settings, and scan all of them. Normally,
1134 * SCSI-3 devices should be scanned via the REPORT LUNS.
1136 if (bflags & BLIST_SPARSELUN) {
1137 max_dev_lun = shost->max_lun;
1138 sparse_lun = 1;
1139 } else
1140 sparse_lun = 0;
1143 * If less than SCSI_1_CSS, and no special lun scaning, stop
1144 * scanning; this matches 2.4 behaviour, but could just be a bug
1145 * (to continue scanning a SCSI_1_CSS device).
1147 * This test is broken. We might not have any device on lun0 for
1148 * a sparselun device, and if that's the case then how would we
1149 * know the real scsi_level, eh? It might make sense to just not
1150 * scan any SCSI_1 device for non-0 luns, but that check would best
1151 * go into scsi_alloc_sdev() and just have it return null when asked
1152 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
1154 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
1155 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
1156 == 0))
1157 return;
1160 * If this device is known to support multiple units, override
1161 * the other settings, and scan all of them.
1163 if (bflags & BLIST_FORCELUN)
1164 max_dev_lun = shost->max_lun;
1166 * REGAL CDC-4X: avoid hang after LUN 4
1168 if (bflags & BLIST_MAX5LUN)
1169 max_dev_lun = min(5U, max_dev_lun);
1171 * Do not scan SCSI-2 or lower device past LUN 7, unless
1172 * BLIST_LARGELUN.
1174 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
1175 max_dev_lun = min(8U, max_dev_lun);
1178 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
1179 * until we reach the max, or no LUN is found and we are not
1180 * sparse_lun.
1182 for (lun = 1; lun < max_dev_lun; ++lun)
1183 if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
1184 NULL) != SCSI_SCAN_LUN_PRESENT) &&
1185 !sparse_lun)
1186 return;
1190 * scsilun_to_int: convert a scsi_lun to an int
1191 * @scsilun: struct scsi_lun to be converted.
1193 * Description:
1194 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
1195 * integer, and return the result. The caller must check for
1196 * truncation before using this function.
1198 * Notes:
1199 * The struct scsi_lun is assumed to be four levels, with each level
1200 * effectively containing a SCSI byte-ordered (big endian) short; the
1201 * addressing bits of each level are ignored (the highest two bits).
1202 * For a description of the LUN format, post SCSI-3 see the SCSI
1203 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
1205 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
1206 * the integer: 0x0b030a04
1208 int scsilun_to_int(struct scsi_lun *scsilun)
1210 int i;
1211 unsigned int lun;
1213 lun = 0;
1214 for (i = 0; i < sizeof(lun); i += 2)
1215 lun = lun | (((scsilun->scsi_lun[i] << 8) |
1216 scsilun->scsi_lun[i + 1]) << (i * 8));
1217 return lun;
1219 EXPORT_SYMBOL(scsilun_to_int);
1222 * int_to_scsilun: reverts an int into a scsi_lun
1223 * @int: integer to be reverted
1224 * @scsilun: struct scsi_lun to be set.
1226 * Description:
1227 * Reverts the functionality of the scsilun_to_int, which packed
1228 * an 8-byte lun value into an int. This routine unpacks the int
1229 * back into the lun value.
1230 * Note: the scsilun_to_int() routine does not truly handle all
1231 * 8bytes of the lun value. This functions restores only as much
1232 * as was set by the routine.
1234 * Notes:
1235 * Given an integer : 0x0b030a04, this function returns a
1236 * scsi_lun of : struct scsi_lun of: 0a 04 0b 03 00 00 00 00
1239 void int_to_scsilun(unsigned int lun, struct scsi_lun *scsilun)
1241 int i;
1243 memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
1245 for (i = 0; i < sizeof(lun); i += 2) {
1246 scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
1247 scsilun->scsi_lun[i+1] = lun & 0xFF;
1248 lun = lun >> 16;
1251 EXPORT_SYMBOL(int_to_scsilun);
1254 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
1255 * @sdevscan: scan the host, channel, and id of this scsi_device
1257 * Description:
1258 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
1259 * command, and scan the resulting list of LUNs by calling
1260 * scsi_probe_and_add_lun.
1262 * Modifies sdevscan->lun.
1264 * Return:
1265 * 0: scan completed (or no memory, so further scanning is futile)
1266 * 1: no report lun scan, or not configured
1268 static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
1269 int rescan)
1271 char devname[64];
1272 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
1273 unsigned int length;
1274 unsigned int lun;
1275 unsigned int num_luns;
1276 unsigned int retries;
1277 int result;
1278 struct scsi_lun *lunp, *lun_data;
1279 u8 *data;
1280 struct scsi_sense_hdr sshdr;
1281 struct scsi_device *sdev;
1282 struct Scsi_Host *shost = dev_to_shost(&starget->dev);
1283 int ret = 0;
1286 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
1287 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
1288 * support more than 8 LUNs.
1290 if (bflags & BLIST_NOREPORTLUN)
1291 return 1;
1292 if (starget->scsi_level < SCSI_2 &&
1293 starget->scsi_level != SCSI_UNKNOWN)
1294 return 1;
1295 if (starget->scsi_level < SCSI_3 &&
1296 (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
1297 return 1;
1298 if (bflags & BLIST_NOLUN)
1299 return 0;
1301 if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
1302 sdev = scsi_alloc_sdev(starget, 0, NULL);
1303 if (!sdev)
1304 return 0;
1305 if (scsi_device_get(sdev))
1306 return 0;
1309 sprintf(devname, "host %d channel %d id %d",
1310 shost->host_no, sdev->channel, sdev->id);
1313 * Allocate enough to hold the header (the same size as one scsi_lun)
1314 * plus the max number of luns we are requesting.
1316 * Reallocating and trying again (with the exact amount we need)
1317 * would be nice, but then we need to somehow limit the size
1318 * allocated based on the available memory and the limits of
1319 * kmalloc - we don't want a kmalloc() failure of a huge value to
1320 * prevent us from finding any LUNs on this target.
1322 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
1323 lun_data = kmalloc(length, GFP_ATOMIC |
1324 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
1325 if (!lun_data) {
1326 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1327 goto out;
1330 scsi_cmd[0] = REPORT_LUNS;
1333 * bytes 1 - 5: reserved, set to zero.
1335 memset(&scsi_cmd[1], 0, 5);
1338 * bytes 6 - 9: length of the command.
1340 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
1341 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
1342 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
1343 scsi_cmd[9] = (unsigned char) length & 0xff;
1345 scsi_cmd[10] = 0; /* reserved */
1346 scsi_cmd[11] = 0; /* control */
1349 * We can get a UNIT ATTENTION, for example a power on/reset, so
1350 * retry a few times (like sd.c does for TEST UNIT READY).
1351 * Experience shows some combinations of adapter/devices get at
1352 * least two power on/resets.
1354 * Illegal requests (for devices that do not support REPORT LUNS)
1355 * should come through as a check condition, and will not generate
1356 * a retry.
1358 for (retries = 0; retries < 3; retries++) {
1359 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1360 " REPORT LUNS to %s (try %d)\n", devname,
1361 retries));
1363 result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
1364 lun_data, length, &sshdr,
1365 SCSI_TIMEOUT + 4 * HZ, 3);
1367 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1368 " %s (try %d) result 0x%x\n", result
1369 ? "failed" : "successful", retries, result));
1370 if (result == 0)
1371 break;
1372 else if (scsi_sense_valid(&sshdr)) {
1373 if (sshdr.sense_key != UNIT_ATTENTION)
1374 break;
1378 if (result) {
1380 * The device probably does not support a REPORT LUN command
1382 ret = 1;
1383 goto out_err;
1387 * Get the length from the first four bytes of lun_data.
1389 data = (u8 *) lun_data->scsi_lun;
1390 length = ((data[0] << 24) | (data[1] << 16) |
1391 (data[2] << 8) | (data[3] << 0));
1393 num_luns = (length / sizeof(struct scsi_lun));
1394 if (num_luns > max_scsi_report_luns) {
1395 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1396 " of %d luns reported, try increasing"
1397 " max_scsi_report_luns.\n", devname,
1398 max_scsi_report_luns, num_luns);
1399 num_luns = max_scsi_report_luns;
1402 SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
1403 "scsi scan: REPORT LUN scan\n"));
1406 * Scan the luns in lun_data. The entry at offset 0 is really
1407 * the header, so start at 1 and go up to and including num_luns.
1409 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1410 lun = scsilun_to_int(lunp);
1413 * Check if the unused part of lunp is non-zero, and so
1414 * does not fit in lun.
1416 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1417 int i;
1420 * Output an error displaying the LUN in byte order,
1421 * this differs from what linux would print for the
1422 * integer LUN value.
1424 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1425 data = (char *)lunp->scsi_lun;
1426 for (i = 0; i < sizeof(struct scsi_lun); i++)
1427 printk("%02x", data[i]);
1428 printk(" has a LUN larger than currently supported.\n");
1429 } else if (lun > sdev->host->max_lun) {
1430 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1431 " than allowed by the host adapter\n",
1432 devname, lun);
1433 } else {
1434 int res;
1436 res = scsi_probe_and_add_lun(starget,
1437 lun, NULL, NULL, rescan, NULL);
1438 if (res == SCSI_SCAN_NO_RESPONSE) {
1440 * Got some results, but now none, abort.
1442 sdev_printk(KERN_ERR, sdev,
1443 "Unexpected response"
1444 " from lun %d while scanning, scan"
1445 " aborted\n", lun);
1446 break;
1451 out_err:
1452 kfree(lun_data);
1453 out:
1454 scsi_device_put(sdev);
1455 if (sdev->sdev_state == SDEV_CREATED)
1457 * the sdev we used didn't appear in the report luns scan
1459 scsi_destroy_sdev(sdev);
1460 return ret;
1463 struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
1464 uint id, uint lun, void *hostdata)
1466 struct scsi_device *sdev = ERR_PTR(-ENODEV);
1467 struct device *parent = &shost->shost_gendev;
1468 struct scsi_target *starget;
1470 if (strncmp(scsi_scan_type, "none", 4) == 0)
1471 return ERR_PTR(-ENODEV);
1473 starget = scsi_alloc_target(parent, channel, id);
1474 if (!starget)
1475 return ERR_PTR(-ENOMEM);
1477 mutex_lock(&shost->scan_mutex);
1478 if (!shost->async_scan)
1479 scsi_complete_async_scans();
1481 if (scsi_host_scan_allowed(shost))
1482 scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
1483 mutex_unlock(&shost->scan_mutex);
1484 scsi_target_reap(starget);
1485 put_device(&starget->dev);
1487 return sdev;
1489 EXPORT_SYMBOL(__scsi_add_device);
1491 int scsi_add_device(struct Scsi_Host *host, uint channel,
1492 uint target, uint lun)
1494 struct scsi_device *sdev =
1495 __scsi_add_device(host, channel, target, lun, NULL);
1496 if (IS_ERR(sdev))
1497 return PTR_ERR(sdev);
1499 scsi_device_put(sdev);
1500 return 0;
1502 EXPORT_SYMBOL(scsi_add_device);
1504 void scsi_rescan_device(struct device *dev)
1506 struct scsi_driver *drv;
1508 if (!dev->driver)
1509 return;
1511 drv = to_scsi_driver(dev->driver);
1512 if (try_module_get(drv->owner)) {
1513 if (drv->rescan)
1514 drv->rescan(dev);
1515 module_put(drv->owner);
1518 EXPORT_SYMBOL(scsi_rescan_device);
1520 static void __scsi_scan_target(struct device *parent, unsigned int channel,
1521 unsigned int id, unsigned int lun, int rescan)
1523 struct Scsi_Host *shost = dev_to_shost(parent);
1524 int bflags = 0;
1525 int res;
1526 struct scsi_target *starget;
1528 if (shost->this_id == id)
1530 * Don't scan the host adapter
1532 return;
1534 starget = scsi_alloc_target(parent, channel, id);
1535 if (!starget)
1536 return;
1538 if (lun != SCAN_WILD_CARD) {
1540 * Scan for a specific host/chan/id/lun.
1542 scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
1543 goto out_reap;
1547 * Scan LUN 0, if there is some response, scan further. Ideally, we
1548 * would not configure LUN 0 until all LUNs are scanned.
1550 res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
1551 if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
1552 if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
1554 * The REPORT LUN did not scan the target,
1555 * do a sequential scan.
1557 scsi_sequential_lun_scan(starget, bflags,
1558 starget->scsi_level, rescan);
1561 out_reap:
1562 /* now determine if the target has any children at all
1563 * and if not, nuke it */
1564 scsi_target_reap(starget);
1566 put_device(&starget->dev);
1570 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1571 * target.
1572 * @parent: host to scan
1573 * @channel: channel to scan
1574 * @id: target id to scan
1575 * @lun: Specific LUN to scan or SCAN_WILD_CARD
1576 * @rescan: passed to LUN scanning routines
1578 * Description:
1579 * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
1580 * and possibly all LUNs on the target id.
1582 * First try a REPORT LUN scan, if that does not scan the target, do a
1583 * sequential scan of LUNs on the target id.
1585 void scsi_scan_target(struct device *parent, unsigned int channel,
1586 unsigned int id, unsigned int lun, int rescan)
1588 struct Scsi_Host *shost = dev_to_shost(parent);
1590 if (strncmp(scsi_scan_type, "none", 4) == 0)
1591 return;
1593 mutex_lock(&shost->scan_mutex);
1594 if (!shost->async_scan)
1595 scsi_complete_async_scans();
1597 if (scsi_host_scan_allowed(shost))
1598 __scsi_scan_target(parent, channel, id, lun, rescan);
1599 mutex_unlock(&shost->scan_mutex);
1601 EXPORT_SYMBOL(scsi_scan_target);
1603 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1604 unsigned int id, unsigned int lun, int rescan)
1606 uint order_id;
1608 if (id == SCAN_WILD_CARD)
1609 for (id = 0; id < shost->max_id; ++id) {
1611 * XXX adapter drivers when possible (FCP, iSCSI)
1612 * could modify max_id to match the current max,
1613 * not the absolute max.
1615 * XXX add a shost id iterator, so for example,
1616 * the FC ID can be the same as a target id
1617 * without a huge overhead of sparse id's.
1619 if (shost->reverse_ordering)
1621 * Scan from high to low id.
1623 order_id = shost->max_id - id - 1;
1624 else
1625 order_id = id;
1626 __scsi_scan_target(&shost->shost_gendev, channel,
1627 order_id, lun, rescan);
1629 else
1630 __scsi_scan_target(&shost->shost_gendev, channel,
1631 id, lun, rescan);
1634 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1635 unsigned int id, unsigned int lun, int rescan)
1637 SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
1638 "%s: <%u:%u:%u>\n",
1639 __FUNCTION__, channel, id, lun));
1641 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1642 ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
1643 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1644 return -EINVAL;
1646 mutex_lock(&shost->scan_mutex);
1647 if (!shost->async_scan)
1648 scsi_complete_async_scans();
1650 if (scsi_host_scan_allowed(shost)) {
1651 if (channel == SCAN_WILD_CARD)
1652 for (channel = 0; channel <= shost->max_channel;
1653 channel++)
1654 scsi_scan_channel(shost, channel, id, lun,
1655 rescan);
1656 else
1657 scsi_scan_channel(shost, channel, id, lun, rescan);
1659 mutex_unlock(&shost->scan_mutex);
1661 return 0;
1664 static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
1666 struct scsi_device *sdev;
1667 shost_for_each_device(sdev, shost) {
1668 if (!scsi_host_scan_allowed(shost) ||
1669 scsi_sysfs_add_sdev(sdev) != 0)
1670 scsi_destroy_sdev(sdev);
1675 * scsi_prep_async_scan - prepare for an async scan
1676 * @shost: the host which will be scanned
1677 * Returns: a cookie to be passed to scsi_finish_async_scan()
1679 * Tells the midlayer this host is going to do an asynchronous scan.
1680 * It reserves the host's position in the scanning list and ensures
1681 * that other asynchronous scans started after this one won't affect the
1682 * ordering of the discovered devices.
1684 static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
1686 struct async_scan_data *data;
1687 unsigned long flags;
1689 if (strncmp(scsi_scan_type, "sync", 4) == 0)
1690 return NULL;
1692 if (shost->async_scan) {
1693 printk("%s called twice for host %d", __FUNCTION__,
1694 shost->host_no);
1695 dump_stack();
1696 return NULL;
1699 data = kmalloc(sizeof(*data), GFP_KERNEL);
1700 if (!data)
1701 goto err;
1702 data->shost = scsi_host_get(shost);
1703 if (!data->shost)
1704 goto err;
1705 init_completion(&data->prev_finished);
1707 mutex_lock(&shost->scan_mutex);
1708 spin_lock_irqsave(shost->host_lock, flags);
1709 shost->async_scan = 1;
1710 spin_unlock_irqrestore(shost->host_lock, flags);
1711 mutex_unlock(&shost->scan_mutex);
1713 spin_lock(&async_scan_lock);
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;
1737 unsigned long flags;
1739 if (!data)
1740 return;
1742 shost = data->shost;
1744 mutex_lock(&shost->scan_mutex);
1746 if (!shost->async_scan) {
1747 printk("%s called twice for host %d", __FUNCTION__,
1748 shost->host_no);
1749 dump_stack();
1750 return;
1753 wait_for_completion(&data->prev_finished);
1755 scsi_sysfs_add_devices(shost);
1757 spin_lock_irqsave(shost->host_lock, flags);
1758 shost->async_scan = 0;
1759 spin_unlock_irqrestore(shost->host_lock, flags);
1761 mutex_unlock(&shost->scan_mutex);
1763 spin_lock(&async_scan_lock);
1764 list_del(&data->list);
1765 if (!list_empty(&scanning_hosts)) {
1766 struct async_scan_data *next = list_entry(scanning_hosts.next,
1767 struct async_scan_data, list);
1768 complete(&next->prev_finished);
1770 spin_unlock(&async_scan_lock);
1772 scsi_host_put(shost);
1773 kfree(data);
1776 static void do_scsi_scan_host(struct Scsi_Host *shost)
1778 if (shost->hostt->scan_finished) {
1779 unsigned long start = jiffies;
1780 if (shost->hostt->scan_start)
1781 shost->hostt->scan_start(shost);
1783 while (!shost->hostt->scan_finished(shost, jiffies - start))
1784 msleep(10);
1785 } else {
1786 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1787 SCAN_WILD_CARD, 0);
1791 static int do_scan_async(void *_data)
1793 struct async_scan_data *data = _data;
1794 do_scsi_scan_host(data->shost);
1795 scsi_finish_async_scan(data);
1796 return 0;
1800 * scsi_scan_host - scan the given adapter
1801 * @shost: adapter to scan
1803 void scsi_scan_host(struct Scsi_Host *shost)
1805 struct task_struct *p;
1806 struct async_scan_data *data;
1808 if (strncmp(scsi_scan_type, "none", 4) == 0)
1809 return;
1811 data = scsi_prep_async_scan(shost);
1812 if (!data) {
1813 do_scsi_scan_host(shost);
1814 return;
1817 p = kthread_run(do_scan_async, data, "scsi_scan_%d", shost->host_no);
1818 if (unlikely(IS_ERR(p)))
1819 do_scan_async(data);
1821 EXPORT_SYMBOL(scsi_scan_host);
1823 void scsi_forget_host(struct Scsi_Host *shost)
1825 struct scsi_device *sdev;
1826 unsigned long flags;
1828 restart:
1829 spin_lock_irqsave(shost->host_lock, flags);
1830 list_for_each_entry(sdev, &shost->__devices, siblings) {
1831 if (sdev->sdev_state == SDEV_DEL)
1832 continue;
1833 spin_unlock_irqrestore(shost->host_lock, flags);
1834 __scsi_remove_device(sdev);
1835 goto restart;
1837 spin_unlock_irqrestore(shost->host_lock, flags);
1841 * Function: scsi_get_host_dev()
1843 * Purpose: Create a scsi_device that points to the host adapter itself.
1845 * Arguments: SHpnt - Host that needs a scsi_device
1847 * Lock status: None assumed.
1849 * Returns: The scsi_device or NULL
1851 * Notes:
1852 * Attach a single scsi_device to the Scsi_Host - this should
1853 * be made to look like a "pseudo-device" that points to the
1854 * HA itself.
1856 * Note - this device is not accessible from any high-level
1857 * drivers (including generics), which is probably not
1858 * optimal. We can add hooks later to attach
1860 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1862 struct scsi_device *sdev = NULL;
1863 struct scsi_target *starget;
1865 mutex_lock(&shost->scan_mutex);
1866 if (!scsi_host_scan_allowed(shost))
1867 goto out;
1868 starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
1869 if (!starget)
1870 goto out;
1872 sdev = scsi_alloc_sdev(starget, 0, NULL);
1873 if (sdev) {
1874 sdev->sdev_gendev.parent = get_device(&starget->dev);
1875 sdev->borken = 0;
1876 } else
1877 scsi_target_reap(starget);
1878 put_device(&starget->dev);
1879 out:
1880 mutex_unlock(&shost->scan_mutex);
1881 return sdev;
1883 EXPORT_SYMBOL(scsi_get_host_dev);
1886 * Function: scsi_free_host_dev()
1888 * Purpose: Free a scsi_device that points to the host adapter itself.
1890 * Arguments: SHpnt - Host that needs a scsi_device
1892 * Lock status: None assumed.
1894 * Returns: Nothing
1896 * Notes:
1898 void scsi_free_host_dev(struct scsi_device *sdev)
1900 BUG_ON(sdev->id != sdev->host->this_id);
1902 scsi_destroy_sdev(sdev);
1904 EXPORT_SYMBOL(scsi_free_host_dev);