4 * Copyright (C) 2000 Eric Youngdale,
5 * Copyright (C) 2002 Patrick Mansfield
7 * The general scanning/probing algorithm is as follows, exceptions are
8 * made to it depending on device specific flags, compilation options, and
9 * global variable (boot or module load time) settings.
11 * A specific LUN is scanned via an INQUIRY command; if the LUN has a
12 * device attached, a Scsi_Device is allocated and setup for it.
14 * For every id of every channel on the given host:
16 * Scan LUN 0; if the target responds to LUN 0 (even if there is no
17 * device or storage attached to LUN 0):
19 * If LUN 0 has a device attached, allocate and setup a
22 * If target is SCSI-3 or up, issue a REPORT LUN, and scan
23 * all of the LUNs returned by the REPORT LUN; else,
24 * sequentially scan LUNs up until some maximum is reached,
25 * or a LUN is seen that cannot have a device attached to it.
28 #include <linux/config.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/blkdev.h>
33 #include <asm/semaphore.h>
35 #include <scsi/scsi_driver.h>
36 #include <scsi/scsi_devinfo.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport.h>
41 #include "scsi_priv.h"
42 #include "scsi_logging.h"
44 #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
45 " SCSI scanning, some SCSI devices might not be configured\n"
48 * Prefix values for the SCSI id's (stored in driverfs name field)
50 #define SCSI_UID_SER_NUM 'S'
51 #define SCSI_UID_UNKNOWN 'Z'
54 * Return values of some of the scanning functions.
56 * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
57 * includes allocation or general failures preventing IO from being sent.
59 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
62 * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
65 #define SCSI_SCAN_NO_RESPONSE 0
66 #define SCSI_SCAN_TARGET_PRESENT 1
67 #define SCSI_SCAN_LUN_PRESENT 2
69 static char *scsi_null_device_strs = "nullnullnullnull";
71 #define MAX_SCSI_LUNS 512
73 #ifdef CONFIG_SCSI_MULTI_LUN
74 static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
76 static unsigned int max_scsi_luns = 1;
79 module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR);
80 MODULE_PARM_DESC(max_luns,
81 "last scsi LUN (should be between 1 and 2^32-1)");
84 * max_scsi_report_luns: the maximum number of LUNS that will be
85 * returned from the REPORT LUNS command. 8 times this value must
86 * be allocated. In theory this could be up to an 8 byte value, but
87 * in practice, the maximum number of LUNs suppored by any device
90 static unsigned int max_scsi_report_luns = 511;
92 module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
93 MODULE_PARM_DESC(max_report_luns,
94 "REPORT LUNS maximum number of LUNS received (should be"
95 " between 1 and 16384)");
97 static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
99 module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
100 MODULE_PARM_DESC(inq_timeout,
101 "Timeout (in seconds) waiting for devices to answer INQUIRY."
102 " Default is 5. Some non-compliant devices need more.");
105 * scsi_unlock_floptical - unlock device via a special MODE SENSE command
106 * @sreq: used to send the command
107 * @result: area to store the result of the MODE SENSE
110 * Send a vendor specific MODE SENSE (not a MODE SELECT) command using
111 * @sreq to unlock a device, storing the (unused) results into result.
112 * Called for BLIST_KEY devices.
114 static void scsi_unlock_floptical(struct scsi_request *sreq,
115 unsigned char *result)
117 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
119 printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
120 scsi_cmd[0] = MODE_SENSE;
124 scsi_cmd[4] = 0x2a; /* size */
126 sreq->sr_cmd_len = 0;
127 sreq->sr_data_direction = DMA_FROM_DEVICE;
128 scsi_wait_req(sreq, scsi_cmd, result, 0x2a /* size */, SCSI_TIMEOUT, 3);
132 * print_inquiry - printk the inquiry information
133 * @inq_result: printk this SCSI INQUIRY
136 * printk the vendor, model, and other information found in the
137 * INQUIRY data in @inq_result.
140 * Remove this, and replace with a hotplug event that logs any
141 * relevant information.
143 static void print_inquiry(unsigned char *inq_result)
147 printk(KERN_NOTICE " Vendor: ");
148 for (i = 8; i < 16; i++)
149 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
150 printk("%c", inq_result[i]);
155 for (i = 16; i < 32; i++)
156 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
157 printk("%c", inq_result[i]);
162 for (i = 32; i < 36; i++)
163 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
164 printk("%c", inq_result[i]);
170 i = inq_result[0] & 0x1f;
172 printk(KERN_NOTICE " Type: %s ",
174 MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
176 printk(" ANSI SCSI revision: %02x",
177 inq_result[2] & 0x07);
178 if ((inq_result[2] & 0x07) == 1 && (inq_result[3] & 0x0f) == 1)
185 * scsi_alloc_sdev - allocate and setup a scsi_Device
188 * Allocate, initialize for io, and return a pointer to a scsi_Device.
189 * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
190 * adds scsi_Device to the appropriate list.
193 * scsi_Device pointer, or NULL on failure.
195 static struct scsi_device *scsi_alloc_sdev(struct Scsi_Host *shost,
196 uint channel, uint id, uint lun)
198 struct scsi_device *sdev, *device;
201 sdev = kmalloc(sizeof(*sdev) + shost->transportt->size, GFP_ATOMIC);
205 memset(sdev, 0, sizeof(*sdev));
206 sdev->vendor = scsi_null_device_strs;
207 sdev->model = scsi_null_device_strs;
208 sdev->rev = scsi_null_device_strs;
212 sdev->channel = channel;
213 sdev->sdev_state = SDEV_CREATED;
214 INIT_LIST_HEAD(&sdev->siblings);
215 INIT_LIST_HEAD(&sdev->same_target_siblings);
216 INIT_LIST_HEAD(&sdev->cmd_list);
217 INIT_LIST_HEAD(&sdev->starved_entry);
218 spin_lock_init(&sdev->list_lock);
221 /* if the device needs this changing, it may do so in the
222 * slave_configure function */
223 sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
226 * Some low level driver could use device->type
231 * Assume that the device will have handshaking problems,
232 * and then fix this field later if it turns out it
237 spin_lock_init(&sdev->sdev_lock);
238 sdev->request_queue = scsi_alloc_queue(sdev);
239 if (!sdev->request_queue)
242 sdev->request_queue->queuedata = sdev;
243 scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
245 if (shost->hostt->slave_alloc) {
246 if (shost->hostt->slave_alloc(sdev))
250 if (shost->transportt->setup) {
251 if (shost->transportt->setup(sdev))
252 goto out_cleanup_slave;
255 if (get_device(&sdev->host->shost_gendev)) {
257 device_initialize(&sdev->sdev_gendev);
258 sdev->sdev_gendev.parent = &sdev->host->shost_gendev;
259 sdev->sdev_gendev.bus = &scsi_bus_type;
260 sdev->sdev_gendev.release = scsi_device_dev_release;
261 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
262 sdev->host->host_no, sdev->channel, sdev->id,
265 class_device_initialize(&sdev->sdev_classdev);
266 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
267 sdev->sdev_classdev.class = &sdev_class;
268 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
269 "%d:%d:%d:%d", sdev->host->host_no,
270 sdev->channel, sdev->id, sdev->lun);
272 class_device_initialize(&sdev->transport_classdev);
273 sdev->transport_classdev.dev = &sdev->sdev_gendev;
274 sdev->transport_classdev.class = sdev->host->transportt->class;
275 snprintf(sdev->transport_classdev.class_id, BUS_ID_SIZE,
276 "%d:%d:%d:%d", sdev->host->host_no,
277 sdev->channel, sdev->id, sdev->lun);
279 goto out_cleanup_transport;
282 * If there are any same target siblings, add this to the
285 spin_lock_irqsave(shost->host_lock, flags);
286 list_for_each_entry(device, &shost->__devices, siblings) {
287 if (device->id == sdev->id &&
288 device->channel == sdev->channel) {
289 list_add_tail(&sdev->same_target_siblings,
290 &device->same_target_siblings);
291 sdev->scsi_level = device->scsi_level;
297 * If there wasn't another lun already configured at this
298 * target, then default this device to SCSI_2 until we
301 if (!sdev->scsi_level)
302 sdev->scsi_level = SCSI_2;
304 list_add_tail(&sdev->siblings, &shost->__devices);
305 spin_unlock_irqrestore(shost->host_lock, flags);
308 out_cleanup_transport:
309 if (shost->transportt->cleanup)
310 shost->transportt->cleanup(sdev);
312 if (shost->hostt->slave_destroy)
313 shost->hostt->slave_destroy(sdev);
315 scsi_free_queue(sdev->request_queue);
319 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
324 * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
325 * @sreq: used to send the INQUIRY
326 * @inq_result: area to store the INQUIRY result
327 * @bflags: store any bflags found here
330 * Probe the lun associated with @sreq using a standard SCSI INQUIRY;
332 * If the INQUIRY is successful, sreq->sr_result is zero and: the
333 * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
334 * are copied to the Scsi_Device at @sreq->sr_device (sdev);
335 * any flags value is stored in *@bflags.
337 static void scsi_probe_lun(struct scsi_request *sreq, char *inq_result,
340 struct scsi_device *sdev = sreq->sr_device; /* a bit ugly */
341 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
342 int possible_inq_resp_len;
347 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY to host %d"
348 " channel %d id %d lun %d\n", sdev->host->host_no,
349 sdev->channel, sdev->id, sdev->lun));
351 memset(scsi_cmd, 0, 6);
352 scsi_cmd[0] = INQUIRY;
353 scsi_cmd[4] = 36; /* issue conservative alloc_length */
354 sreq->sr_cmd_len = 0;
355 sreq->sr_data_direction = DMA_FROM_DEVICE;
357 memset(inq_result, 0, 36);
358 scsi_wait_req(sreq, (void *) scsi_cmd, (void *) inq_result, 36,
359 HZ/2 + HZ*scsi_inq_timeout, 3);
361 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: 1st INQUIRY %s with"
362 " code 0x%x\n", sreq->sr_result ?
363 "failed" : "successful", sreq->sr_result));
366 if (sreq->sr_result) {
367 if ((driver_byte(sreq->sr_result) & DRIVER_SENSE) != 0 &&
368 (sreq->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION &&
369 (sreq->sr_sense_buffer[12] == 0x28 ||
370 sreq->sr_sense_buffer[12] == 0x29) &&
371 sreq->sr_sense_buffer[13] == 0) {
372 /* not-ready to ready transition or power-on - good */
373 /* dpg: bogus? INQUIRY never returns UNIT_ATTENTION */
374 /* Supposedly, but many buggy devices do so anyway */
379 * assume no peripheral if any other sort of error
385 * Get any flags for this device.
387 * XXX add a bflags to Scsi_Device, and replace the corresponding
388 * bit fields in Scsi_Device, so bflags need not be passed as an
391 *bflags |= scsi_get_device_flags(sdev, &inq_result[8], &inq_result[16]);
393 possible_inq_resp_len = (unsigned char) inq_result[4] + 5;
394 if (BLIST_INQUIRY_36 & *bflags)
395 possible_inq_resp_len = 36;
396 else if (BLIST_INQUIRY_58 & *bflags)
397 possible_inq_resp_len = 58;
398 else if (possible_inq_resp_len > 255)
399 possible_inq_resp_len = 36; /* sanity */
401 if (possible_inq_resp_len > 36) { /* do additional INQUIRY */
402 memset(scsi_cmd, 0, 6);
403 scsi_cmd[0] = INQUIRY;
404 scsi_cmd[4] = (unsigned char) possible_inq_resp_len;
405 sreq->sr_cmd_len = 0;
406 sreq->sr_data_direction = DMA_FROM_DEVICE;
408 * re-zero inq_result just to be safe.
410 memset(inq_result, 0, possible_inq_resp_len);
411 scsi_wait_req(sreq, (void *) scsi_cmd,
413 possible_inq_resp_len, (1+scsi_inq_timeout)*(HZ/2), 3);
414 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: 2nd INQUIRY"
415 " %s with code 0x%x\n", sreq->sr_result ?
416 "failed" : "successful", sreq->sr_result));
417 if (sreq->sr_result) {
418 /* if the longer inquiry has failed, flag the device
419 * as only accepting 36 byte inquiries and retry the
421 printk(KERN_INFO "scsi scan: %d byte inquiry failed"
422 " with code %d. Consider BLIST_INQUIRY_36 for"
423 " this device\n", possible_inq_resp_len,
425 *bflags = BLIST_INQUIRY_36;
430 * The INQUIRY can change, this means the length can change.
432 possible_inq_resp_len = (unsigned char) inq_result[4] + 5;
433 if (BLIST_INQUIRY_58 & *bflags)
434 possible_inq_resp_len = 58;
435 else if (possible_inq_resp_len > 255)
436 possible_inq_resp_len = 36; /* sanity */
439 sdev->inquiry_len = possible_inq_resp_len;
442 * XXX Abort if the response length is less than 36? If less than
443 * 32, the lookup of the device flags (above) could be invalid,
444 * and it would be possible to take an incorrect action - we do
445 * not want to hang because of a short INQUIRY. On the flip side,
446 * if the device is spun down or becoming ready (and so it gives a
447 * short INQUIRY), an abort here prevents any further use of the
448 * device, including spin up.
450 * Related to the above issue:
452 * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
453 * and if not ready, sent a START_STOP to start (maybe spin up) and
454 * then send the INQUIRY again, since the INQUIRY can change after
455 * a device is initialized.
457 * Ideally, start a device if explicitly asked to do so. This
458 * assumes that a device is spun up on power on, spun down on
459 * request, and then spun up on request.
463 * The scanning code needs to know the scsi_level, even if no
464 * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
465 * non-zero LUNs can be scanned.
467 sdev->scsi_level = inq_result[2] & 0x07;
468 if (sdev->scsi_level >= 2 ||
469 (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
476 * scsi_add_lun - allocate and fully initialze a Scsi_Device
477 * @sdevscan: holds information to be stored in the new Scsi_Device
478 * @sdevnew: store the address of the newly allocated Scsi_Device
479 * @inq_result: holds the result of a previous INQUIRY to the LUN
480 * @bflags: black/white list flag
483 * Allocate and initialize a Scsi_Device matching sdevscan. Optionally
484 * set fields based on values in *@bflags. If @sdevnew is not
485 * NULL, store the address of the new Scsi_Device in *@sdevnew (needed
486 * when scanning a particular LUN).
489 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
490 * SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
492 static int scsi_add_lun(struct scsi_device *sdev, char *inq_result, int *bflags)
494 struct scsi_device *sdev_sibling;
495 struct scsi_target *starget;
499 * XXX do not save the inquiry, since it can change underneath us,
500 * save just vendor/model/rev.
502 * Rather than save it and have an ioctl that retrieves the saved
503 * value, have an ioctl that executes the same INQUIRY code used
504 * in scsi_probe_lun, let user level programs doing INQUIRY
505 * scanning run at their own risk, or supply a user level program
506 * that can correctly scan.
508 sdev->inquiry = kmalloc(sdev->inquiry_len, GFP_ATOMIC);
509 if (sdev->inquiry == NULL) {
510 return SCSI_SCAN_NO_RESPONSE;
513 memcpy(sdev->inquiry, inq_result, sdev->inquiry_len);
514 sdev->vendor = (char *) (sdev->inquiry + 8);
515 sdev->model = (char *) (sdev->inquiry + 16);
516 sdev->rev = (char *) (sdev->inquiry + 32);
518 if (*bflags & BLIST_ISROM) {
520 * It would be better to modify sdev->type, and set
521 * sdev->removable, but then the print_inquiry() output
522 * would not show TYPE_ROM; if print_inquiry() is removed
523 * the issue goes away.
525 inq_result[0] = TYPE_ROM;
526 inq_result[1] |= 0x80; /* removable */
529 switch (sdev->type = (inq_result[0] & 0x1f)) {
536 case TYPE_MEDIUM_CHANGER:
546 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
549 print_inquiry(inq_result);
552 * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
553 * spec says: The device server is capable of supporting the
554 * specified peripheral device type on this logical unit. However,
555 * the physical device is not currently connected to this logical
558 * The above is vague, as it implies that we could treat 001 and
559 * 011 the same. Stay compatible with previous code, and create a
560 * Scsi_Device for a PQ of 1
562 * Don't set the device offline here; rather let the upper
563 * level drivers eval the PQ to decide whether they should
564 * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
567 sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
568 sdev->removable = (0x80 & inq_result[1]) >> 7;
569 sdev->lockable = sdev->removable;
570 sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
572 if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
573 inq_result[56] & 0x04))
575 if (inq_result[7] & 0x60)
577 if (inq_result[7] & 0x10)
580 sprintf(sdev->devfs_name, "scsi/host%d/bus%d/target%d/lun%d",
581 sdev->host->host_no, sdev->channel,
582 sdev->id, sdev->lun);
585 * End driverfs/devfs code.
588 if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
589 !(*bflags & BLIST_NOTQ))
590 sdev->tagged_supported = 1;
592 * Some devices (Texel CD ROM drives) have handshaking problems
593 * when used with the Seagate controllers. borken is initialized
594 * to 1, and then set it to 0 here.
596 if ((*bflags & BLIST_BORKEN) == 0)
600 * Some devices may not want to have a start command automatically
601 * issued when a device is added.
603 if (*bflags & BLIST_NOSTARTONADD)
604 sdev->no_start_on_add = 1;
607 * If we need to allow I/O to only one of the luns attached to
608 * this target id at a time set single_lun, and allocate or modify
611 if (*bflags & BLIST_SINGLELUN) {
612 sdev->single_lun = 1;
613 spin_lock_irqsave(sdev->host->host_lock, flags);
616 * Search for an existing target for this sdev.
618 list_for_each_entry(sdev_sibling, &sdev->same_target_siblings,
619 same_target_siblings) {
620 if (sdev_sibling->sdev_target != NULL) {
621 starget = sdev_sibling->sdev_target;
626 starget = kmalloc(sizeof(*starget), GFP_ATOMIC);
628 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
629 spin_unlock_irqrestore(sdev->host->host_lock,
631 return SCSI_SCAN_NO_RESPONSE;
633 starget->starget_refcnt = 0;
634 starget->starget_sdev_user = NULL;
636 starget->starget_refcnt++;
637 sdev->sdev_target = starget;
638 spin_unlock_irqrestore(sdev->host->host_lock, flags);
641 sdev->use_10_for_rw = 1;
643 if (*bflags & BLIST_MS_SKIP_PAGE_08)
644 sdev->skip_ms_page_8 = 1;
646 if (*bflags & BLIST_MS_SKIP_PAGE_3F)
647 sdev->skip_ms_page_3f = 1;
649 if (*bflags & BLIST_USE_10_BYTE_MS)
650 sdev->use_10_for_ms = 1;
652 /* set the device running here so that slave configure
654 scsi_device_set_state(sdev, SDEV_RUNNING);
656 if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
657 sdev->use_192_bytes_for_3f = 1;
659 if(sdev->host->hostt->slave_configure)
660 sdev->host->hostt->slave_configure(sdev);
663 * Ok, the device is now all set up, we can
664 * register it and tell the rest of the kernel
667 scsi_sysfs_add_sdev(sdev);
669 return SCSI_SCAN_LUN_PRESENT;
673 * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
674 * @sdevscan: probe the LUN corresponding to this Scsi_Device
675 * @sdevnew: store the value of any new Scsi_Device allocated
676 * @bflagsp: store bflags here if not NULL
679 * Call scsi_probe_lun, if a LUN with an attached device is found,
680 * allocate and set it up by calling scsi_add_lun.
683 * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
684 * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
685 * attached at the LUN
686 * SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
688 static int scsi_probe_and_add_lun(struct Scsi_Host *host,
689 uint channel, uint id, uint lun, int *bflagsp,
690 struct scsi_device **sdevp, int rescan)
692 struct scsi_device *sdev;
693 struct scsi_request *sreq;
694 unsigned char *result;
695 int bflags, res = SCSI_SCAN_NO_RESPONSE;
698 * The rescan flag is used as an optimization, the first scan of a
699 * host adapter calls into here with rescan == 0.
702 sdev = scsi_device_lookup(host, channel, id, lun);
704 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
705 "scsi scan: device exists on <%d:%d:%d:%d>\n",
706 host->host_no, channel, id, lun));
710 *bflagsp = scsi_get_device_flags(sdev,
713 /* XXX: bandaid until callers do refcounting */
714 scsi_device_put(sdev);
715 return SCSI_SCAN_LUN_PRESENT;
719 sdev = scsi_alloc_sdev(host, channel, id, lun);
722 sreq = scsi_allocate_request(sdev, GFP_ATOMIC);
725 result = kmalloc(256, GFP_ATOMIC |
726 (host->unchecked_isa_dma) ? __GFP_DMA : 0);
730 scsi_probe_lun(sreq, result, &bflags);
732 goto out_free_result;
735 * result contains valid SCSI INQUIRY data.
737 if ((result[0] >> 5) == 3) {
739 * For a Peripheral qualifier 3 (011b), the SCSI
740 * spec says: The device server is not capable of
741 * supporting a physical device on this logical
744 * For disks, this implies that there is no
745 * logical disk configured at sdev->lun, but there
746 * is a target id responding.
748 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
749 "scsi scan: peripheral qualifier of 3,"
750 " no device added\n"));
751 res = SCSI_SCAN_TARGET_PRESENT;
752 goto out_free_result;
755 res = scsi_add_lun(sdev, result, &bflags);
756 if (res == SCSI_SCAN_LUN_PRESENT) {
757 if (bflags & BLIST_KEY) {
759 scsi_unlock_floptical(sreq, result);
768 scsi_release_request(sreq);
770 if (res == SCSI_SCAN_LUN_PRESENT) {
774 if (sdev->host->hostt->slave_destroy)
775 sdev->host->hostt->slave_destroy(sdev);
776 if (sdev->host->transportt->cleanup)
777 sdev->host->transportt->cleanup(sdev);
778 put_device(&sdev->sdev_gendev);
785 * scsi_sequential_lun_scan - sequentially scan a SCSI target
786 * @sdevscan: scan the host, channel, and id of this Scsi_Device
787 * @bflags: black/white list flag for LUN 0
788 * @lun0_res: result of scanning LUN 0
791 * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
792 * scanned) to some maximum lun until a LUN is found with no device
793 * attached. Use the bflags to figure out any oddities.
795 * Modifies sdevscan->lun.
797 static void scsi_sequential_lun_scan(struct Scsi_Host *shost, uint channel,
798 uint id, int bflags, int lun0_res, int scsi_level, int rescan)
800 unsigned int sparse_lun, lun, max_dev_lun;
802 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
803 " host %d channel %d id %d\n", shost->host_no,
806 max_dev_lun = min(max_scsi_luns, shost->max_lun);
808 * If this device is known to support sparse multiple units,
809 * override the other settings, and scan all of them. Normally,
810 * SCSI-3 devices should be scanned via the REPORT LUNS.
812 if (bflags & BLIST_SPARSELUN) {
813 max_dev_lun = shost->max_lun;
819 * If not sparse lun and no device attached at LUN 0 do not scan
822 if (!sparse_lun && (lun0_res != SCSI_SCAN_LUN_PRESENT))
826 * If less than SCSI_1_CSS, and no special lun scaning, stop
827 * scanning; this matches 2.4 behaviour, but could just be a bug
828 * (to continue scanning a SCSI_1_CSS device).
830 * This test is broken. We might not have any device on lun0 for
831 * a sparselun device, and if that's the case then how would we
832 * know the real scsi_level, eh? It might make sense to just not
833 * scan any SCSI_1 device for non-0 luns, but that check would best
834 * go into scsi_alloc_sdev() and just have it return null when asked
835 * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
837 if ((sdevscan->scsi_level < SCSI_1_CCS) &&
838 ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
843 * If this device is known to support multiple units, override
844 * the other settings, and scan all of them.
846 if (bflags & BLIST_FORCELUN)
847 max_dev_lun = shost->max_lun;
849 * REGAL CDC-4X: avoid hang after LUN 4
851 if (bflags & BLIST_MAX5LUN)
852 max_dev_lun = min(5U, max_dev_lun);
854 * Do not scan SCSI-2 or lower device past LUN 7, unless
857 if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
858 max_dev_lun = min(8U, max_dev_lun);
861 * We have already scanned LUN 0, so start at LUN 1. Keep scanning
862 * until we reach the max, or no LUN is found and we are not
865 for (lun = 1; lun < max_dev_lun; ++lun)
866 if ((scsi_probe_and_add_lun(shost, channel, id, lun,
867 NULL, NULL, rescan) != SCSI_SCAN_LUN_PRESENT) &&
873 * scsilun_to_int: convert a scsi_lun to an int
874 * @scsilun: struct scsi_lun to be converted.
877 * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
878 * integer, and return the result. The caller must check for
879 * truncation before using this function.
882 * The struct scsi_lun is assumed to be four levels, with each level
883 * effectively containing a SCSI byte-ordered (big endian) short; the
884 * addressing bits of each level are ignored (the highest two bits).
885 * For a description of the LUN format, post SCSI-3 see the SCSI
886 * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
888 * Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
889 * the integer: 0x0b030a04
891 static int scsilun_to_int(struct scsi_lun *scsilun)
897 for (i = 0; i < sizeof(lun); i += 2)
898 lun = lun | (((scsilun->scsi_lun[i] << 8) |
899 scsilun->scsi_lun[i + 1]) << (i * 8));
904 * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
905 * @sdevscan: scan the host, channel, and id of this Scsi_Device
908 * If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
909 * command, and scan the resulting list of LUNs by calling
910 * scsi_probe_and_add_lun.
912 * Modifies sdevscan->lun.
915 * 0: scan completed (or no memory, so further scanning is futile)
916 * 1: no report lun scan, or not configured
918 static int scsi_report_lun_scan(struct scsi_device *sdev, int bflags,
922 unsigned char scsi_cmd[MAX_COMMAND_SIZE];
925 unsigned int num_luns;
926 unsigned int retries;
927 struct scsi_lun *lunp, *lun_data;
928 struct scsi_request *sreq;
932 * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
933 * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
934 * support more than 8 LUNs.
936 if ((bflags & BLIST_NOREPORTLUN) ||
937 sdev->scsi_level < SCSI_2 ||
938 (sdev->scsi_level < SCSI_3 &&
939 (!(bflags & BLIST_REPORTLUN2) || sdev->host->max_lun <= 8)) )
941 if (bflags & BLIST_NOLUN)
944 sreq = scsi_allocate_request(sdev, GFP_ATOMIC);
948 sprintf(devname, "host %d channel %d id %d",
949 sdev->host->host_no, sdev->channel, sdev->id);
952 * Allocate enough to hold the header (the same size as one scsi_lun)
953 * plus the max number of luns we are requesting.
955 * Reallocating and trying again (with the exact amount we need)
956 * would be nice, but then we need to somehow limit the size
957 * allocated based on the available memory and the limits of
958 * kmalloc - we don't want a kmalloc() failure of a huge value to
959 * prevent us from finding any LUNs on this target.
961 length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
962 lun_data = kmalloc(length, GFP_ATOMIC |
963 (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
965 goto out_release_request;
967 scsi_cmd[0] = REPORT_LUNS;
970 * bytes 1 - 5: reserved, set to zero.
972 memset(&scsi_cmd[1], 0, 5);
975 * bytes 6 - 9: length of the command.
977 scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
978 scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
979 scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
980 scsi_cmd[9] = (unsigned char) length & 0xff;
982 scsi_cmd[10] = 0; /* reserved */
983 scsi_cmd[11] = 0; /* control */
984 sreq->sr_cmd_len = 0;
985 sreq->sr_data_direction = DMA_FROM_DEVICE;
988 * We can get a UNIT ATTENTION, for example a power on/reset, so
989 * retry a few times (like sd.c does for TEST UNIT READY).
990 * Experience shows some combinations of adapter/devices get at
991 * least two power on/resets.
993 * Illegal requests (for devices that do not support REPORT LUNS)
994 * should come through as a check condition, and will not generate
997 for (retries = 0; retries < 3; retries++) {
998 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
999 " REPORT LUNS to %s (try %d)\n", devname,
1001 scsi_wait_req(sreq, scsi_cmd, lun_data, length,
1002 SCSI_TIMEOUT + 4*HZ, 3);
1003 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1004 " %s (try %d) result 0x%x\n", sreq->sr_result
1005 ? "failed" : "successful", retries,
1007 if (sreq->sr_result == 0 ||
1008 sreq->sr_sense_buffer[2] != UNIT_ATTENTION)
1012 if (sreq->sr_result) {
1014 * The device probably does not support a REPORT LUN command
1017 scsi_release_request(sreq);
1020 scsi_release_request(sreq);
1023 * Get the length from the first four bytes of lun_data.
1025 data = (u8 *) lun_data->scsi_lun;
1026 length = ((data[0] << 24) | (data[1] << 16) |
1027 (data[2] << 8) | (data[3] << 0));
1029 num_luns = (length / sizeof(struct scsi_lun));
1030 if (num_luns > max_scsi_report_luns) {
1031 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1032 " of %d luns reported, try increasing"
1033 " max_scsi_report_luns.\n", devname,
1034 max_scsi_report_luns, num_luns);
1035 num_luns = max_scsi_report_luns;
1038 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUN scan of"
1039 " host %d channel %d id %d\n", sdev->host->host_no,
1040 sdev->channel, sdev->id));
1043 * Scan the luns in lun_data. The entry at offset 0 is really
1044 * the header, so start at 1 and go up to and including num_luns.
1046 for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1047 lun = scsilun_to_int(lunp);
1050 * Check if the unused part of lunp is non-zero, and so
1051 * does not fit in lun.
1053 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1057 * Output an error displaying the LUN in byte order,
1058 * this differs from what linux would print for the
1059 * integer LUN value.
1061 printk(KERN_WARNING "scsi: %s lun 0x", devname);
1062 data = (char *)lunp->scsi_lun;
1063 for (i = 0; i < sizeof(struct scsi_lun); i++)
1064 printk("%02x", data[i]);
1065 printk(" has a LUN larger than currently supported.\n");
1066 } else if (lun == 0) {
1068 * LUN 0 has already been scanned.
1070 } else if (lun > sdev->host->max_lun) {
1071 printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1072 " than allowed by the host adapter\n",
1077 res = scsi_probe_and_add_lun(sdev->host, sdev->channel,
1078 sdev->id, lun, NULL, NULL, rescan);
1079 if (res == SCSI_SCAN_NO_RESPONSE) {
1081 * Got some results, but now none, abort.
1083 printk(KERN_ERR "scsi: Unexpected response"
1084 " from %s lun %d while scanning, scan"
1085 " aborted\n", devname, lun);
1094 out_release_request:
1095 scsi_release_request(sreq);
1098 * We are out of memory, don't try scanning any further.
1100 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1104 struct scsi_device *scsi_add_device(struct Scsi_Host *shost,
1105 uint channel, uint id, uint lun)
1107 struct scsi_device *sdev;
1110 down(&shost->scan_mutex);
1111 res = scsi_probe_and_add_lun(shost, channel, id, lun, NULL, &sdev, 1);
1112 if (res != SCSI_SCAN_LUN_PRESENT)
1113 sdev = ERR_PTR(-ENODEV);
1114 up(&shost->scan_mutex);
1119 void scsi_rescan_device(struct device *dev)
1121 struct scsi_driver *drv;
1126 drv = to_scsi_driver(dev->driver);
1127 if (try_module_get(drv->owner)) {
1130 module_put(drv->owner);
1135 * scsi_scan_target - scan a target id, possibly including all LUNs on the
1137 * @sdevsca: Scsi_Device handle for scanning
1138 * @shost: host to scan
1139 * @channel: channel to scan
1140 * @id: target id to scan
1143 * Scan the target id on @shost, @channel, and @id. Scan at least LUN
1144 * 0, and possibly all LUNs on the target id.
1146 * Use the pre-allocated @sdevscan as a handle for the scanning. This
1147 * function sets sdevscan->host, sdevscan->id and sdevscan->lun; the
1148 * scanning functions modify sdevscan->lun.
1150 * First try a REPORT LUN scan, if that does not scan the target, do a
1151 * sequential scan of LUNs on the target id.
1153 static void scsi_scan_target(struct Scsi_Host *shost, unsigned int channel,
1154 unsigned int id, unsigned int lun, int rescan)
1158 struct scsi_device *sdev;
1160 if (shost->this_id == id)
1162 * Don't scan the host adapter
1166 if (lun != SCAN_WILD_CARD) {
1168 * Scan for a specific host/chan/id/lun.
1170 scsi_probe_and_add_lun(shost, channel, id, lun, NULL, NULL,
1176 * Scan LUN 0, if there is some response, scan further. Ideally, we
1177 * would not configure LUN 0 until all LUNs are scanned.
1179 res = scsi_probe_and_add_lun(shost, channel, id, 0, &bflags, &sdev,
1181 if (res == SCSI_SCAN_LUN_PRESENT) {
1182 if (scsi_report_lun_scan(sdev, bflags, rescan) != 0)
1184 * The REPORT LUN did not scan the target,
1185 * do a sequential scan.
1187 scsi_sequential_lun_scan(shost, channel, id, bflags,
1188 res, sdev->scsi_level, rescan);
1189 } else if (res == SCSI_SCAN_TARGET_PRESENT) {
1191 * There's a target here, but lun 0 is offline so we
1192 * can't use the report_lun scan. Fall back to a
1193 * sequential lun scan with a bflags of SPARSELUN and
1194 * a default scsi level of SCSI_2
1196 scsi_sequential_lun_scan(shost, channel, id, BLIST_SPARSELUN,
1197 SCSI_SCAN_TARGET_PRESENT, SCSI_2, rescan);
1201 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1202 unsigned int id, unsigned int lun, int rescan)
1206 if (id == SCAN_WILD_CARD)
1207 for (id = 0; id < shost->max_id; ++id) {
1209 * XXX adapter drivers when possible (FCP, iSCSI)
1210 * could modify max_id to match the current max,
1211 * not the absolute max.
1213 * XXX add a shost id iterator, so for example,
1214 * the FC ID can be the same as a target id
1215 * without a huge overhead of sparse id's.
1217 if (shost->reverse_ordering)
1219 * Scan from high to low id.
1221 order_id = shost->max_id - id - 1;
1224 scsi_scan_target(shost, channel, order_id, lun, rescan);
1227 scsi_scan_target(shost, channel, id, lun, rescan);
1230 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1231 unsigned int id, unsigned int lun, int rescan)
1233 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "%s: <%u:%u:%u:%u>\n",
1234 __FUNCTION__, shost->host_no, channel, id, lun));
1236 if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1237 ((id != SCAN_WILD_CARD) && (id > shost->max_id)) ||
1238 ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1241 down(&shost->scan_mutex);
1242 if (channel == SCAN_WILD_CARD)
1243 for (channel = 0; channel <= shost->max_channel; channel++)
1244 scsi_scan_channel(shost, channel, id, lun, rescan);
1246 scsi_scan_channel(shost, channel, id, lun, rescan);
1247 up(&shost->scan_mutex);
1253 * scsi_scan_host - scan the given adapter
1254 * @shost: adapter to scan
1256 void scsi_scan_host(struct Scsi_Host *shost)
1258 scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1262 void scsi_forget_host(struct Scsi_Host *shost)
1264 struct scsi_device *sdev, *tmp;
1265 unsigned long flags;
1268 * Ok, this look a bit strange. We always look for the first device
1269 * on the list as scsi_remove_device removes them from it - thus we
1270 * also have to release the lock.
1271 * We don't need to get another reference to the device before
1272 * releasing the lock as we already own the reference from
1273 * scsi_register_device that's release in scsi_remove_device. And
1274 * after that we don't look at sdev anymore.
1276 spin_lock_irqsave(shost->host_lock, flags);
1277 list_for_each_entry_safe(sdev, tmp, &shost->__devices, siblings) {
1278 spin_unlock_irqrestore(shost->host_lock, flags);
1279 scsi_remove_device(sdev);
1280 spin_lock_irqsave(shost->host_lock, flags);
1282 spin_unlock_irqrestore(shost->host_lock, flags);
1286 * Function: scsi_get_host_dev()
1288 * Purpose: Create a Scsi_Device that points to the host adapter itself.
1290 * Arguments: SHpnt - Host that needs a Scsi_Device
1292 * Lock status: None assumed.
1294 * Returns: The Scsi_Device or NULL
1297 * Attach a single Scsi_Device to the Scsi_Host - this should
1298 * be made to look like a "pseudo-device" that points to the
1301 * Note - this device is not accessible from any high-level
1302 * drivers (including generics), which is probably not
1303 * optimal. We can add hooks later to attach
1305 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1307 struct scsi_device *sdev;
1309 sdev = scsi_alloc_sdev(shost, 0, shost->this_id, 0);
1317 * Function: scsi_free_host_dev()
1319 * Purpose: Free a scsi_device that points to the host adapter itself.
1321 * Arguments: SHpnt - Host that needs a Scsi_Device
1323 * Lock status: None assumed.
1329 void scsi_free_host_dev(struct scsi_device *sdev)
1331 BUG_ON(sdev->id != sdev->host->this_id);
1333 if (sdev->host->hostt->slave_destroy)
1334 sdev->host->hostt->slave_destroy(sdev);
1335 if (sdev->host->transportt->cleanup)
1336 sdev->host->transportt->cleanup(sdev);
1337 put_device(&sdev->sdev_gendev);