This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / drivers / scsi / scsi_scan.c
1 /*
2  * scsi_scan.c
3  *
4  * Copyright (C) 2000 Eric Youngdale,
5  * Copyright (C) 2002 Patrick Mansfield
6  *
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.
10  *
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.
13  *
14  * For every id of every channel on the given host:
15  *
16  *      Scan LUN 0; if the target responds to LUN 0 (even if there is no
17  *      device or storage attached to LUN 0):
18  *
19  *              If LUN 0 has a device attached, allocate and setup a
20  *              Scsi_Device for it.
21  *
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.
26  */
27
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>
34
35 #include <scsi/scsi_driver.h>
36 #include <scsi/scsi_devinfo.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_transport.h>
39 #include "scsi.h"
40
41 #include "scsi_priv.h"
42 #include "scsi_logging.h"
43
44 #define ALLOC_FAILURE_MSG       KERN_ERR "%s: Allocation failure during" \
45         " SCSI scanning, some SCSI devices might not be configured\n"
46
47 /*
48  * Default timeout
49  */
50 #define SCSI_TIMEOUT (2*HZ)
51
52 /*
53  * Prefix values for the SCSI id's (stored in driverfs name field)
54  */
55 #define SCSI_UID_SER_NUM 'S'
56 #define SCSI_UID_UNKNOWN 'Z'
57
58 /*
59  * Return values of some of the scanning functions.
60  *
61  * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
62  * includes allocation or general failures preventing IO from being sent.
63  *
64  * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
65  * on the given LUN.
66  *
67  * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
68  * given LUN.
69  */
70 #define SCSI_SCAN_NO_RESPONSE           0
71 #define SCSI_SCAN_TARGET_PRESENT        1
72 #define SCSI_SCAN_LUN_PRESENT           2
73
74 static char *scsi_null_device_strs = "nullnullnullnull";
75
76 #define MAX_SCSI_LUNS   512
77
78 #ifdef CONFIG_SCSI_MULTI_LUN
79 static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
80 #else
81 static unsigned int max_scsi_luns = 1;
82 #endif
83
84 module_param_named(max_luns, max_scsi_luns, int, S_IRUGO|S_IWUSR);
85 MODULE_PARM_DESC(max_luns,
86                  "last scsi LUN (should be between 1 and 2^32-1)");
87
88 /*
89  * max_scsi_report_luns: the maximum number of LUNS that will be
90  * returned from the REPORT LUNS command. 8 times this value must
91  * be allocated. In theory this could be up to an 8 byte value, but
92  * in practice, the maximum number of LUNs suppored by any device
93  * is about 16k.
94  */
95 static unsigned int max_scsi_report_luns = 511;
96
97 module_param_named(max_report_luns, max_scsi_report_luns, int, S_IRUGO|S_IWUSR);
98 MODULE_PARM_DESC(max_report_luns,
99                  "REPORT LUNS maximum number of LUNS received (should be"
100                  " between 1 and 16384)");
101
102 static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ+3;
103
104 module_param_named(inq_timeout, scsi_inq_timeout, int, S_IRUGO|S_IWUSR);
105 MODULE_PARM_DESC(inq_timeout, 
106                  "Timeout (in seconds) waiting for devices to answer INQUIRY."
107                  " Default is 5. Some non-compliant devices need more.");
108
109 /**
110  * scsi_unlock_floptical - unlock device via a special MODE SENSE command
111  * @sreq:       used to send the command
112  * @result:     area to store the result of the MODE SENSE
113  *
114  * Description:
115  *     Send a vendor specific MODE SENSE (not a MODE SELECT) command using
116  *     @sreq to unlock a device, storing the (unused) results into result.
117  *     Called for BLIST_KEY devices.
118  **/
119 static void scsi_unlock_floptical(struct scsi_request *sreq,
120                                   unsigned char *result)
121 {
122         unsigned char scsi_cmd[MAX_COMMAND_SIZE];
123
124         printk(KERN_NOTICE "scsi: unlocking floptical drive\n");
125         scsi_cmd[0] = MODE_SENSE;
126         scsi_cmd[1] = 0;
127         scsi_cmd[2] = 0x2e;
128         scsi_cmd[3] = 0;
129         scsi_cmd[4] = 0x2a;     /* size */
130         scsi_cmd[5] = 0;
131         sreq->sr_cmd_len = 0;
132         sreq->sr_data_direction = DMA_FROM_DEVICE;
133         scsi_wait_req(sreq, scsi_cmd, result, 0x2a /* size */, SCSI_TIMEOUT, 3);
134 }
135
136 /**
137  * print_inquiry - printk the inquiry information
138  * @inq_result: printk this SCSI INQUIRY
139  *
140  * Description:
141  *     printk the vendor, model, and other information found in the
142  *     INQUIRY data in @inq_result.
143  *
144  * Notes:
145  *     Remove this, and replace with a hotplug event that logs any
146  *     relevant information.
147  **/
148 static void print_inquiry(unsigned char *inq_result)
149 {
150         int i;
151
152         printk(KERN_NOTICE "  Vendor: ");
153         for (i = 8; i < 16; i++)
154                 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
155                         printk("%c", inq_result[i]);
156                 else
157                         printk(" ");
158
159         printk("  Model: ");
160         for (i = 16; i < 32; i++)
161                 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
162                         printk("%c", inq_result[i]);
163                 else
164                         printk(" ");
165
166         printk("  Rev: ");
167         for (i = 32; i < 36; i++)
168                 if (inq_result[i] >= 0x20 && i < inq_result[4] + 5)
169                         printk("%c", inq_result[i]);
170                 else
171                         printk(" ");
172
173         printk("\n");
174
175         i = inq_result[0] & 0x1f;
176
177         printk(KERN_NOTICE "  Type:   %s ",
178                i <
179                MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
180                "Unknown          ");
181         printk("                 ANSI SCSI revision: %02x",
182                inq_result[2] & 0x07);
183         if ((inq_result[2] & 0x07) == 1 && (inq_result[3] & 0x0f) == 1)
184                 printk(" CCS\n");
185         else
186                 printk("\n");
187 }
188
189 /**
190  * scsi_alloc_sdev - allocate and setup a scsi_Device
191  *
192  * Description:
193  *     Allocate, initialize for io, and return a pointer to a scsi_Device.
194  *     Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
195  *     adds scsi_Device to the appropriate list.
196  *
197  * Return value:
198  *     scsi_Device pointer, or NULL on failure.
199  **/
200 static struct scsi_device *scsi_alloc_sdev(struct Scsi_Host *shost,
201                 uint channel, uint id, uint lun)
202 {
203         struct scsi_device *sdev, *device;
204         unsigned long flags;
205
206         sdev = kmalloc(sizeof(*sdev) + shost->transportt->size, GFP_ATOMIC);
207         if (!sdev)
208                 goto out;
209
210         memset(sdev, 0, sizeof(*sdev));
211         sdev->vendor = scsi_null_device_strs;
212         sdev->model = scsi_null_device_strs;
213         sdev->rev = scsi_null_device_strs;
214         sdev->host = shost;
215         sdev->id = id;
216         sdev->lun = lun;
217         sdev->channel = channel;
218         sdev->sdev_state = SDEV_CREATED;
219         INIT_LIST_HEAD(&sdev->siblings);
220         INIT_LIST_HEAD(&sdev->same_target_siblings);
221         INIT_LIST_HEAD(&sdev->cmd_list);
222         INIT_LIST_HEAD(&sdev->starved_entry);
223         spin_lock_init(&sdev->list_lock);
224
225
226         /* if the device needs this changing, it may do so in the
227          * slave_configure function */
228         sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
229
230         /*
231          * Some low level driver could use device->type
232          */
233         sdev->type = -1;
234
235         /*
236          * Assume that the device will have handshaking problems,
237          * and then fix this field later if it turns out it
238          * doesn't
239          */
240         sdev->borken = 1;
241
242         spin_lock_init(&sdev->sdev_lock);
243         sdev->request_queue = scsi_alloc_queue(sdev);
244         if (!sdev->request_queue)
245                 goto out_free_dev;
246
247         sdev->request_queue->queuedata = sdev;
248         scsi_adjust_queue_depth(sdev, 0, sdev->host->cmd_per_lun);
249
250         if (shost->hostt->slave_alloc) {
251                 if (shost->hostt->slave_alloc(sdev))
252                         goto out_free_queue;
253         }
254
255         if (shost->transportt->setup) {
256                 if (shost->transportt->setup(sdev))
257                         goto out_cleanup_slave;
258         }
259
260         if (get_device(&sdev->host->shost_gendev)) {
261
262                 device_initialize(&sdev->sdev_gendev);
263                 sdev->sdev_gendev.parent = &sdev->host->shost_gendev;
264                 sdev->sdev_gendev.bus = &scsi_bus_type;
265                 sdev->sdev_gendev.release = scsi_device_dev_release;
266                 sprintf(sdev->sdev_gendev.bus_id,"%d:%d:%d:%d",
267                         sdev->host->host_no, sdev->channel, sdev->id,
268                         sdev->lun);
269
270                 class_device_initialize(&sdev->sdev_classdev);
271                 sdev->sdev_classdev.dev = &sdev->sdev_gendev;
272                 sdev->sdev_classdev.class = &sdev_class;
273                 snprintf(sdev->sdev_classdev.class_id, BUS_ID_SIZE,
274                          "%d:%d:%d:%d", sdev->host->host_no,
275                          sdev->channel, sdev->id, sdev->lun);
276
277                 class_device_initialize(&sdev->transport_classdev);
278                 sdev->transport_classdev.dev = &sdev->sdev_gendev;
279                 sdev->transport_classdev.class = sdev->host->transportt->class;
280                 snprintf(sdev->transport_classdev.class_id, BUS_ID_SIZE,
281                          "%d:%d:%d:%d", sdev->host->host_no,
282                          sdev->channel, sdev->id, sdev->lun);
283         } else
284                 goto out_cleanup_transport;
285
286         /*
287          * If there are any same target siblings, add this to the
288          * sibling list
289          */
290         spin_lock_irqsave(shost->host_lock, flags);
291         list_for_each_entry(device, &shost->__devices, siblings) {
292                 if (device->id == sdev->id &&
293                     device->channel == sdev->channel) {
294                         list_add_tail(&sdev->same_target_siblings,
295                                       &device->same_target_siblings);
296                         sdev->scsi_level = device->scsi_level;
297                         break;
298                 }
299         }
300
301         /*
302          * If there wasn't another lun already configured at this
303          * target, then default this device to SCSI_2 until we
304          * know better
305          */
306         if (!sdev->scsi_level)
307                 sdev->scsi_level = SCSI_2;
308
309         list_add_tail(&sdev->siblings, &shost->__devices);
310         spin_unlock_irqrestore(shost->host_lock, flags);
311         return sdev;
312
313 out_cleanup_transport:
314         if (shost->transportt->cleanup)
315                 shost->transportt->cleanup(sdev);
316 out_cleanup_slave:
317         if (shost->hostt->slave_destroy)
318                 shost->hostt->slave_destroy(sdev);
319 out_free_queue:
320         scsi_free_queue(sdev->request_queue);
321 out_free_dev:
322         kfree(sdev);
323 out:
324         printk(ALLOC_FAILURE_MSG, __FUNCTION__);
325         return NULL;
326 }
327
328 /**
329  * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
330  * @sreq:       used to send the INQUIRY
331  * @inq_result: area to store the INQUIRY result
332  * @bflags:     store any bflags found here
333  *
334  * Description:
335  *     Probe the lun associated with @sreq using a standard SCSI INQUIRY;
336  *
337  *     If the INQUIRY is successful, sreq->sr_result is zero and: the
338  *     INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
339  *     are copied to the Scsi_Device at @sreq->sr_device (sdev);
340  *     any flags value is stored in *@bflags.
341  **/
342 static void scsi_probe_lun(struct scsi_request *sreq, char *inq_result,
343                            int *bflags)
344 {
345         struct scsi_device *sdev = sreq->sr_device;     /* a bit ugly */
346         unsigned char scsi_cmd[MAX_COMMAND_SIZE];
347         int possible_inq_resp_len;
348         int count = 0;
349
350         *bflags = 0;
351  repeat_inquiry:
352         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: INQUIRY to host %d"
353                         " channel %d id %d lun %d\n", sdev->host->host_no,
354                         sdev->channel, sdev->id, sdev->lun));
355
356         memset(scsi_cmd, 0, 6);
357         scsi_cmd[0] = INQUIRY;
358         scsi_cmd[4] = 36;       /* issue conservative alloc_length */
359         sreq->sr_cmd_len = 0;
360         sreq->sr_data_direction = DMA_FROM_DEVICE;
361
362         memset(inq_result, 0, 36);
363         scsi_wait_req(sreq, (void *) scsi_cmd, (void *) inq_result, 36,
364                       HZ/2 + HZ*scsi_inq_timeout, 3);
365
366         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: 1st INQUIRY %s with"
367                         " code 0x%x\n", sreq->sr_result ?
368                         "failed" : "successful", sreq->sr_result));
369         ++count;
370
371         if (sreq->sr_result) {
372                 if ((driver_byte(sreq->sr_result) & DRIVER_SENSE) != 0 &&
373                     (sreq->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION &&
374                     (sreq->sr_sense_buffer[12] == 0x28 ||
375                      sreq->sr_sense_buffer[12] == 0x29) &&
376                     sreq->sr_sense_buffer[13] == 0) {
377                         /* not-ready to ready transition or power-on - good */
378                         /* dpg: bogus? INQUIRY never returns UNIT_ATTENTION */
379                         /* Supposedly, but many buggy devices do so anyway */
380                         if (count < 3)
381                                 goto repeat_inquiry;
382                 }
383                 /*
384                  * assume no peripheral if any other sort of error
385                  */
386                 return;
387         }
388
389         /*
390          * Get any flags for this device.
391          *
392          * XXX add a bflags to Scsi_Device, and replace the corresponding
393          * bit fields in Scsi_Device, so bflags need not be passed as an
394          * argument.
395          */
396         *bflags |= scsi_get_device_flags(sdev, &inq_result[8], &inq_result[16]);
397
398         possible_inq_resp_len = (unsigned char) inq_result[4] + 5;
399         if (BLIST_INQUIRY_36 & *bflags)
400                 possible_inq_resp_len = 36;
401         else if (BLIST_INQUIRY_58 & *bflags)
402                 possible_inq_resp_len = 58;
403         else if (possible_inq_resp_len > 255)
404                 possible_inq_resp_len = 36;     /* sanity */
405
406         if (possible_inq_resp_len > 36) {       /* do additional INQUIRY */
407                 memset(scsi_cmd, 0, 6);
408                 scsi_cmd[0] = INQUIRY;
409                 scsi_cmd[4] = (unsigned char) possible_inq_resp_len;
410                 sreq->sr_cmd_len = 0;
411                 sreq->sr_data_direction = DMA_FROM_DEVICE;
412                 /*
413                  * re-zero inq_result just to be safe.
414                  */
415                 memset(inq_result, 0, possible_inq_resp_len);
416                 scsi_wait_req(sreq, (void *) scsi_cmd,
417                               (void *) inq_result,
418                               possible_inq_resp_len, (1+scsi_inq_timeout)*(HZ/2), 3);
419                 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: 2nd INQUIRY"
420                                 " %s with code 0x%x\n", sreq->sr_result ?
421                                 "failed" : "successful", sreq->sr_result));
422                 if (sreq->sr_result) {
423                         /* if the longer inquiry has failed, flag the device
424                          * as only accepting 36 byte inquiries and retry the
425                          * 36 byte inquiry */
426                         printk(KERN_INFO "scsi scan: %d byte inquiry failed"
427                                " with code %d.  Consider BLIST_INQUIRY_36 for"
428                                " this device\n", possible_inq_resp_len,
429                                sreq->sr_result);
430                         *bflags = BLIST_INQUIRY_36;
431                         goto repeat_inquiry;
432                 }
433
434                 /*
435                  * The INQUIRY can change, this means the length can change.
436                  */
437                 possible_inq_resp_len = (unsigned char) inq_result[4] + 5;
438                 if (BLIST_INQUIRY_58 & *bflags)
439                         possible_inq_resp_len = 58;
440                 else if (possible_inq_resp_len > 255)
441                         possible_inq_resp_len = 36;     /* sanity */
442         }
443
444         sdev->inquiry_len = possible_inq_resp_len;
445
446         /*
447          * XXX Abort if the response length is less than 36? If less than
448          * 32, the lookup of the device flags (above) could be invalid,
449          * and it would be possible to take an incorrect action - we do
450          * not want to hang because of a short INQUIRY. On the flip side,
451          * if the device is spun down or becoming ready (and so it gives a
452          * short INQUIRY), an abort here prevents any further use of the
453          * device, including spin up.
454          *
455          * Related to the above issue:
456          *
457          * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
458          * and if not ready, sent a START_STOP to start (maybe spin up) and
459          * then send the INQUIRY again, since the INQUIRY can change after
460          * a device is initialized.
461          *
462          * Ideally, start a device if explicitly asked to do so.  This
463          * assumes that a device is spun up on power on, spun down on
464          * request, and then spun up on request.
465          */
466
467         /*
468          * The scanning code needs to know the scsi_level, even if no
469          * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
470          * non-zero LUNs can be scanned.
471          */
472         sdev->scsi_level = inq_result[2] & 0x07;
473         if (sdev->scsi_level >= 2 ||
474             (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
475                 sdev->scsi_level++;
476
477         return;
478 }
479
480 /**
481  * scsi_add_lun - allocate and fully initialze a Scsi_Device
482  * @sdevscan:   holds information to be stored in the new Scsi_Device
483  * @sdevnew:    store the address of the newly allocated Scsi_Device
484  * @inq_result: holds the result of a previous INQUIRY to the LUN
485  * @bflags:     black/white list flag
486  *
487  * Description:
488  *     Allocate and initialize a Scsi_Device matching sdevscan. Optionally
489  *     set fields based on values in *@bflags. If @sdevnew is not
490  *     NULL, store the address of the new Scsi_Device in *@sdevnew (needed
491  *     when scanning a particular LUN).
492  *
493  * Return:
494  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
495  *     SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
496  **/
497 static int scsi_add_lun(struct scsi_device *sdev, char *inq_result, int *bflags)
498 {
499         struct scsi_device *sdev_sibling;
500         struct scsi_target *starget;
501         unsigned long flags;
502
503         /*
504          * XXX do not save the inquiry, since it can change underneath us,
505          * save just vendor/model/rev.
506          *
507          * Rather than save it and have an ioctl that retrieves the saved
508          * value, have an ioctl that executes the same INQUIRY code used
509          * in scsi_probe_lun, let user level programs doing INQUIRY
510          * scanning run at their own risk, or supply a user level program
511          * that can correctly scan.
512          */
513         sdev->inquiry = kmalloc(sdev->inquiry_len, GFP_ATOMIC);
514         if (sdev->inquiry == NULL) {
515                 return SCSI_SCAN_NO_RESPONSE;
516         }
517
518         memcpy(sdev->inquiry, inq_result, sdev->inquiry_len);
519         sdev->vendor = (char *) (sdev->inquiry + 8);
520         sdev->model = (char *) (sdev->inquiry + 16);
521         sdev->rev = (char *) (sdev->inquiry + 32);
522
523         if (*bflags & BLIST_ISROM) {
524                 /*
525                  * It would be better to modify sdev->type, and set
526                  * sdev->removable, but then the print_inquiry() output
527                  * would not show TYPE_ROM; if print_inquiry() is removed
528                  * the issue goes away.
529                  */
530                 inq_result[0] = TYPE_ROM;
531                 inq_result[1] |= 0x80;  /* removable */
532         }
533
534         switch (sdev->type = (inq_result[0] & 0x1f)) {
535         case TYPE_TAPE:
536         case TYPE_DISK:
537         case TYPE_PRINTER:
538         case TYPE_MOD:
539         case TYPE_PROCESSOR:
540         case TYPE_SCANNER:
541         case TYPE_MEDIUM_CHANGER:
542         case TYPE_ENCLOSURE:
543         case TYPE_COMM:
544                 sdev->writeable = 1;
545                 break;
546         case TYPE_WORM:
547         case TYPE_ROM:
548                 sdev->writeable = 0;
549                 break;
550         default:
551                 printk(KERN_INFO "scsi: unknown device type %d\n", sdev->type);
552         }
553
554         print_inquiry(inq_result);
555
556         /*
557          * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
558          * spec says: The device server is capable of supporting the
559          * specified peripheral device type on this logical unit. However,
560          * the physical device is not currently connected to this logical
561          * unit.
562          *
563          * The above is vague, as it implies that we could treat 001 and
564          * 011 the same. Stay compatible with previous code, and create a
565          * Scsi_Device for a PQ of 1
566          *
567          * Don't set the device offline here; rather let the upper
568          * level drivers eval the PQ to decide whether they should
569          * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
570          */ 
571
572         sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
573         sdev->removable = (0x80 & inq_result[1]) >> 7;
574         sdev->lockable = sdev->removable;
575         sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
576
577         if (sdev->scsi_level >= SCSI_3 || (sdev->inquiry_len > 56 &&
578                 inq_result[56] & 0x04))
579                 sdev->ppr = 1;
580         if (inq_result[7] & 0x60)
581                 sdev->wdtr = 1;
582         if (inq_result[7] & 0x10)
583                 sdev->sdtr = 1;
584
585         sprintf(sdev->devfs_name, "scsi/host%d/bus%d/target%d/lun%d",
586                                 sdev->host->host_no, sdev->channel,
587                                 sdev->id, sdev->lun);
588
589         /*
590          * End driverfs/devfs code.
591          */
592
593         if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
594             !(*bflags & BLIST_NOTQ))
595                 sdev->tagged_supported = 1;
596         /*
597          * Some devices (Texel CD ROM drives) have handshaking problems
598          * when used with the Seagate controllers. borken is initialized
599          * to 1, and then set it to 0 here.
600          */
601         if ((*bflags & BLIST_BORKEN) == 0)
602                 sdev->borken = 0;
603
604         /*
605          * Some devices may not want to have a start command automatically
606          * issued when a device is added.
607          */
608         if (*bflags & BLIST_NOSTARTONADD)
609                 sdev->no_start_on_add = 1;
610
611         /*
612          * If we need to allow I/O to only one of the luns attached to
613          * this target id at a time set single_lun, and allocate or modify
614          * sdev_target.
615          */
616         if (*bflags & BLIST_SINGLELUN) {
617                 sdev->single_lun = 1;
618                 spin_lock_irqsave(sdev->host->host_lock, flags);
619                 starget = NULL;
620                 /*
621                  * Search for an existing target for this sdev.
622                  */
623                 list_for_each_entry(sdev_sibling, &sdev->same_target_siblings,
624                                     same_target_siblings) {
625                         if (sdev_sibling->sdev_target != NULL) {
626                                 starget = sdev_sibling->sdev_target;
627                                 break;
628                         }
629                 }
630                 if (!starget) {
631                         starget = kmalloc(sizeof(*starget), GFP_ATOMIC);
632                         if (!starget) {
633                                 printk(ALLOC_FAILURE_MSG, __FUNCTION__);
634                                 spin_unlock_irqrestore(sdev->host->host_lock,
635                                                        flags);
636                                 return SCSI_SCAN_NO_RESPONSE;
637                         }
638                         starget->starget_refcnt = 0;
639                         starget->starget_sdev_user = NULL;
640                 }
641                 starget->starget_refcnt++;
642                 sdev->sdev_target = starget;
643                 spin_unlock_irqrestore(sdev->host->host_lock, flags);
644         }
645
646         sdev->use_10_for_rw = 1;
647
648         if (*bflags & BLIST_MS_SKIP_PAGE_08)
649                 sdev->skip_ms_page_8 = 1;
650
651         if (*bflags & BLIST_MS_SKIP_PAGE_3F)
652                 sdev->skip_ms_page_3f = 1;
653
654         if (*bflags & BLIST_USE_10_BYTE_MS)
655                 sdev->use_10_for_ms = 1;
656
657         /* set the device running here so that slave configure
658          * may do I/O */
659         scsi_device_set_state(sdev, SDEV_RUNNING);
660
661         if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
662                 sdev->use_192_bytes_for_3f = 1;
663
664         if(sdev->host->hostt->slave_configure)
665                 sdev->host->hostt->slave_configure(sdev);
666
667         /*
668          * Ok, the device is now all set up, we can
669          * register it and tell the rest of the kernel
670          * about it.
671          */
672         scsi_sysfs_add_sdev(sdev);
673
674         return SCSI_SCAN_LUN_PRESENT;
675 }
676
677 /**
678  * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
679  * @sdevscan:   probe the LUN corresponding to this Scsi_Device
680  * @sdevnew:    store the value of any new Scsi_Device allocated
681  * @bflagsp:    store bflags here if not NULL
682  *
683  * Description:
684  *     Call scsi_probe_lun, if a LUN with an attached device is found,
685  *     allocate and set it up by calling scsi_add_lun.
686  *
687  * Return:
688  *     SCSI_SCAN_NO_RESPONSE: could not allocate or setup a Scsi_Device
689  *     SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
690  *         attached at the LUN
691  *     SCSI_SCAN_LUN_PRESENT: a new Scsi_Device was allocated and initialized
692  **/
693 static int scsi_probe_and_add_lun(struct Scsi_Host *host,
694                 uint channel, uint id, uint lun, int *bflagsp,
695                 struct scsi_device **sdevp, int rescan)
696 {
697         struct scsi_device *sdev;
698         struct scsi_request *sreq;
699         unsigned char *result;
700         int bflags, res = SCSI_SCAN_NO_RESPONSE;
701
702         /*
703          * The rescan flag is used as an optimization, the first scan of a
704          * host adapter calls into here with rescan == 0.
705          */
706         if (rescan) {
707                 sdev = scsi_device_lookup(host, channel, id, lun);
708                 if (sdev) {
709                         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
710                                 "scsi scan: device exists on <%d:%d:%d:%d>\n",
711                                 host->host_no, channel, id, lun));
712                         if (sdevp)
713                                 *sdevp = sdev;
714                         if (bflagsp)
715                                 *bflagsp = scsi_get_device_flags(sdev,
716                                                                  sdev->vendor,
717                                                                  sdev->model);
718                         /* XXX: bandaid until callers do refcounting */
719                         scsi_device_put(sdev);
720                         return SCSI_SCAN_LUN_PRESENT;
721                 }
722         }
723
724         sdev = scsi_alloc_sdev(host, channel, id, lun);
725         if (!sdev)
726                 goto out;
727         sreq = scsi_allocate_request(sdev, GFP_ATOMIC);
728         if (!sreq)
729                 goto out_free_sdev;
730         result = kmalloc(256, GFP_ATOMIC |
731                         (host->unchecked_isa_dma) ? __GFP_DMA : 0);
732         if (!result)
733                 goto out_free_sreq;
734
735         scsi_probe_lun(sreq, result, &bflags);
736         if (sreq->sr_result)
737                 goto out_free_result;
738
739         /*
740          * result contains valid SCSI INQUIRY data.
741          */
742         if ((result[0] >> 5) == 3) {
743                 /*
744                  * For a Peripheral qualifier 3 (011b), the SCSI
745                  * spec says: The device server is not capable of
746                  * supporting a physical device on this logical
747                  * unit.
748                  *
749                  * For disks, this implies that there is no
750                  * logical disk configured at sdev->lun, but there
751                  * is a target id responding.
752                  */
753                 SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO
754                                         "scsi scan: peripheral qualifier of 3,"
755                                         " no device added\n"));
756                 res = SCSI_SCAN_TARGET_PRESENT;
757                 goto out_free_result;
758         }
759
760         res = scsi_add_lun(sdev, result, &bflags);
761         if (res == SCSI_SCAN_LUN_PRESENT) {
762                 if (bflags & BLIST_KEY) {
763                         sdev->lockable = 0;
764                         scsi_unlock_floptical(sreq, result);
765                 }
766                 if (bflagsp)
767                         *bflagsp = bflags;
768         }
769
770  out_free_result:
771         kfree(result);
772  out_free_sreq:
773         scsi_release_request(sreq);
774  out_free_sdev:
775         if (res == SCSI_SCAN_LUN_PRESENT) {
776                 if (sdevp)
777                         *sdevp = sdev;
778         } else {
779                 if (sdev->host->hostt->slave_destroy)
780                         sdev->host->hostt->slave_destroy(sdev);
781                 if (sdev->host->transportt->cleanup)
782                         sdev->host->transportt->cleanup(sdev);
783                 put_device(&sdev->sdev_gendev);
784         }
785  out:
786         return res;
787 }
788
789 /**
790  * scsi_sequential_lun_scan - sequentially scan a SCSI target
791  * @sdevscan:   scan the host, channel, and id of this Scsi_Device
792  * @bflags:     black/white list flag for LUN 0
793  * @lun0_res:   result of scanning LUN 0
794  *
795  * Description:
796  *     Generally, scan from LUN 1 (LUN 0 is assumed to already have been
797  *     scanned) to some maximum lun until a LUN is found with no device
798  *     attached. Use the bflags to figure out any oddities.
799  *
800  *     Modifies sdevscan->lun.
801  **/
802 static void scsi_sequential_lun_scan(struct Scsi_Host *shost, uint channel,
803                 uint id, int bflags, int lun0_res, int scsi_level, int rescan)
804 {
805         unsigned int sparse_lun, lun, max_dev_lun;
806
807         SCSI_LOG_SCAN_BUS(3, printk(KERN_INFO "scsi scan: Sequential scan of"
808                         " host %d channel %d id %d\n", shost->host_no,
809                         channel, id));
810
811         max_dev_lun = min(max_scsi_luns, shost->max_lun);
812         /*
813          * If this device is known to support sparse multiple units,
814          * override the other settings, and scan all of them. Normally,
815          * SCSI-3 devices should be scanned via the REPORT LUNS.
816          */
817         if (bflags & BLIST_SPARSELUN) {
818                 max_dev_lun = shost->max_lun;
819                 sparse_lun = 1;
820         } else
821                 sparse_lun = 0;
822
823         /*
824          * If not sparse lun and no device attached at LUN 0 do not scan
825          * any further.
826          */
827         if (!sparse_lun && (lun0_res != SCSI_SCAN_LUN_PRESENT))
828                 return;
829
830         /*
831          * If less than SCSI_1_CSS, and no special lun scaning, stop
832          * scanning; this matches 2.4 behaviour, but could just be a bug
833          * (to continue scanning a SCSI_1_CSS device).
834          *
835          * This test is broken.  We might not have any device on lun0 for
836          * a sparselun device, and if that's the case then how would we
837          * know the real scsi_level, eh?  It might make sense to just not
838          * scan any SCSI_1 device for non-0 luns, but that check would best
839          * go into scsi_alloc_sdev() and just have it return null when asked
840          * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
841          *
842         if ((sdevscan->scsi_level < SCSI_1_CCS) &&
843             ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
844              == 0))
845                 return;
846          */
847         /*
848          * If this device is known to support multiple units, override
849          * the other settings, and scan all of them.
850          */
851         if (bflags & BLIST_FORCELUN)
852                 max_dev_lun = shost->max_lun;
853         /*
854          * REGAL CDC-4X: avoid hang after LUN 4
855          */
856         if (bflags & BLIST_MAX5LUN)
857                 max_dev_lun = min(5U, max_dev_lun);
858         /*
859          * Do not scan SCSI-2 or lower device past LUN 7, unless
860          * BLIST_LARGELUN.
861          */
862         if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
863                 max_dev_lun = min(8U, max_dev_lun);
864
865         /*
866          * We have already scanned LUN 0, so start at LUN 1. Keep scanning
867          * until we reach the max, or no LUN is found and we are not
868          * sparse_lun.
869          */
870         for (lun = 1; lun < max_dev_lun; ++lun)
871                 if ((scsi_probe_and_add_lun(shost, channel, id, lun,
872                       NULL, NULL, rescan) != SCSI_SCAN_LUN_PRESENT) &&
873                     !sparse_lun)
874                         return;
875 }
876
877 /**
878  * scsilun_to_int: convert a scsi_lun to an int
879  * @scsilun:    struct scsi_lun to be converted.
880  *
881  * Description:
882  *     Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
883  *     integer, and return the result. The caller must check for
884  *     truncation before using this function.
885  *
886  * Notes:
887  *     The struct scsi_lun is assumed to be four levels, with each level
888  *     effectively containing a SCSI byte-ordered (big endian) short; the
889  *     addressing bits of each level are ignored (the highest two bits).
890  *     For a description of the LUN format, post SCSI-3 see the SCSI
891  *     Architecture Model, for SCSI-3 see the SCSI Controller Commands.
892  *
893  *     Given a struct scsi_lun of: 0a 04 0b 03 00 00 00 00, this function returns
894  *     the integer: 0x0b030a04
895  **/
896 static int scsilun_to_int(struct scsi_lun *scsilun)
897 {
898         int i;
899         unsigned int lun;
900
901         lun = 0;
902         for (i = 0; i < sizeof(lun); i += 2)
903                 lun = lun | (((scsilun->scsi_lun[i] << 8) |
904                               scsilun->scsi_lun[i + 1]) << (i * 8));
905         return lun;
906 }
907
908 /**
909  * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
910  * @sdevscan:   scan the host, channel, and id of this Scsi_Device
911  *
912  * Description:
913  *     If @sdevscan is for a SCSI-3 or up device, send a REPORT LUN
914  *     command, and scan the resulting list of LUNs by calling
915  *     scsi_probe_and_add_lun.
916  *
917  *     Modifies sdevscan->lun.
918  *
919  * Return:
920  *     0: scan completed (or no memory, so further scanning is futile)
921  *     1: no report lun scan, or not configured
922  **/
923 static int scsi_report_lun_scan(struct scsi_device *sdev, int bflags,
924                                 int rescan)
925 {
926         char devname[64];
927         unsigned char scsi_cmd[MAX_COMMAND_SIZE];
928         unsigned int length;
929         unsigned int lun;
930         unsigned int num_luns;
931         unsigned int retries;
932         struct scsi_lun *lunp, *lun_data;
933         struct scsi_request *sreq;
934         u8 *data;
935
936         /*
937          * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
938          * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
939          * support more than 8 LUNs.
940          */
941         if ((bflags & BLIST_NOREPORTLUN) || 
942              sdev->scsi_level < SCSI_2 ||
943             (sdev->scsi_level < SCSI_3 && 
944              (!(bflags & BLIST_REPORTLUN2) || sdev->host->max_lun <= 8)) )
945                 return 1;
946         if (bflags & BLIST_NOLUN)
947                 return 0;
948
949         sreq = scsi_allocate_request(sdev, GFP_ATOMIC);
950         if (!sreq)
951                 goto out;
952
953         sprintf(devname, "host %d channel %d id %d",
954                 sdev->host->host_no, sdev->channel, sdev->id);
955
956         /*
957          * Allocate enough to hold the header (the same size as one scsi_lun)
958          * plus the max number of luns we are requesting.
959          *
960          * Reallocating and trying again (with the exact amount we need)
961          * would be nice, but then we need to somehow limit the size
962          * allocated based on the available memory and the limits of
963          * kmalloc - we don't want a kmalloc() failure of a huge value to
964          * prevent us from finding any LUNs on this target.
965          */
966         length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
967         lun_data = kmalloc(length, GFP_ATOMIC |
968                            (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
969         if (!lun_data)
970                 goto out_release_request;
971
972         scsi_cmd[0] = REPORT_LUNS;
973
974         /*
975          * bytes 1 - 5: reserved, set to zero.
976          */
977         memset(&scsi_cmd[1], 0, 5);
978
979         /*
980          * bytes 6 - 9: length of the command.
981          */
982         scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
983         scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
984         scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
985         scsi_cmd[9] = (unsigned char) length & 0xff;
986
987         scsi_cmd[10] = 0;       /* reserved */
988         scsi_cmd[11] = 0;       /* control */
989         sreq->sr_cmd_len = 0;
990         sreq->sr_data_direction = DMA_FROM_DEVICE;
991
992         /*
993          * We can get a UNIT ATTENTION, for example a power on/reset, so
994          * retry a few times (like sd.c does for TEST UNIT READY).
995          * Experience shows some combinations of adapter/devices get at
996          * least two power on/resets.
997          *
998          * Illegal requests (for devices that do not support REPORT LUNS)
999          * should come through as a check condition, and will not generate
1000          * a retry.
1001          */
1002         for (retries = 0; retries < 3; retries++) {
1003                 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: Sending"
1004                                 " REPORT LUNS to %s (try %d)\n", devname,
1005                                 retries));
1006                 scsi_wait_req(sreq, scsi_cmd, lun_data, length,
1007                                 SCSI_TIMEOUT + 4*HZ, 3);
1008                 SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUNS"
1009                                 " %s (try %d) result 0x%x\n", sreq->sr_result
1010                                 ?  "failed" : "successful", retries,
1011                                 sreq->sr_result));
1012                 if (sreq->sr_result == 0 ||
1013                     sreq->sr_sense_buffer[2] != UNIT_ATTENTION)
1014                         break;
1015         }
1016
1017         if (sreq->sr_result) {
1018                 /*
1019                  * The device probably does not support a REPORT LUN command
1020                  */
1021                 kfree(lun_data);
1022                 scsi_release_request(sreq);
1023                 return 1;
1024         }
1025         scsi_release_request(sreq);
1026
1027         /*
1028          * Get the length from the first four bytes of lun_data.
1029          */
1030         data = (u8 *) lun_data->scsi_lun;
1031         length = ((data[0] << 24) | (data[1] << 16) |
1032                   (data[2] << 8) | (data[3] << 0));
1033
1034         num_luns = (length / sizeof(struct scsi_lun));
1035         if (num_luns > max_scsi_report_luns) {
1036                 printk(KERN_WARNING "scsi: On %s only %d (max_scsi_report_luns)"
1037                        " of %d luns reported, try increasing"
1038                        " max_scsi_report_luns.\n", devname,
1039                        max_scsi_report_luns, num_luns);
1040                 num_luns = max_scsi_report_luns;
1041         }
1042
1043         SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "scsi scan: REPORT LUN scan of"
1044                         " host %d channel %d id %d\n", sdev->host->host_no,
1045                         sdev->channel, sdev->id));
1046
1047         /*
1048          * Scan the luns in lun_data. The entry at offset 0 is really
1049          * the header, so start at 1 and go up to and including num_luns.
1050          */
1051         for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
1052                 lun = scsilun_to_int(lunp);
1053
1054                 /*
1055                  * Check if the unused part of lunp is non-zero, and so
1056                  * does not fit in lun.
1057                  */
1058                 if (memcmp(&lunp->scsi_lun[sizeof(lun)], "\0\0\0\0", 4)) {
1059                         int i;
1060
1061                         /*
1062                          * Output an error displaying the LUN in byte order,
1063                          * this differs from what linux would print for the
1064                          * integer LUN value.
1065                          */
1066                         printk(KERN_WARNING "scsi: %s lun 0x", devname);
1067                         data = (char *)lunp->scsi_lun;
1068                         for (i = 0; i < sizeof(struct scsi_lun); i++)
1069                                 printk("%02x", data[i]);
1070                         printk(" has a LUN larger than currently supported.\n");
1071                 } else if (lun == 0) {
1072                         /*
1073                          * LUN 0 has already been scanned.
1074                          */
1075                 } else if (lun > sdev->host->max_lun) {
1076                         printk(KERN_WARNING "scsi: %s lun%d has a LUN larger"
1077                                " than allowed by the host adapter\n",
1078                                devname, lun);
1079                 } else {
1080                         int res;
1081
1082                         res = scsi_probe_and_add_lun(sdev->host, sdev->channel,
1083                                 sdev->id, lun, NULL, NULL, rescan);
1084                         if (res == SCSI_SCAN_NO_RESPONSE) {
1085                                 /*
1086                                  * Got some results, but now none, abort.
1087                                  */
1088                                 printk(KERN_ERR "scsi: Unexpected response"
1089                                        " from %s lun %d while scanning, scan"
1090                                        " aborted\n", devname, lun);
1091                                 break;
1092                         }
1093                 }
1094         }
1095
1096         kfree(lun_data);
1097         return 0;
1098
1099  out_release_request:
1100         scsi_release_request(sreq);
1101  out:
1102         /*
1103          * We are out of memory, don't try scanning any further.
1104          */
1105         printk(ALLOC_FAILURE_MSG, __FUNCTION__);
1106         return 0;
1107 }
1108
1109 struct scsi_device *scsi_add_device(struct Scsi_Host *shost,
1110                                     uint channel, uint id, uint lun)
1111 {
1112         struct scsi_device *sdev;
1113         int res;
1114
1115         down(&shost->scan_mutex);
1116         res = scsi_probe_and_add_lun(shost, channel, id, lun, NULL, &sdev, 1);
1117         if (res != SCSI_SCAN_LUN_PRESENT)
1118                 sdev = ERR_PTR(-ENODEV);
1119         up(&shost->scan_mutex);
1120
1121         return sdev;
1122 }
1123
1124 void scsi_rescan_device(struct device *dev)
1125 {
1126         struct scsi_driver *drv;
1127         
1128         if (!dev->driver)
1129                 return;
1130
1131         drv = to_scsi_driver(dev->driver);
1132         if (try_module_get(drv->owner)) {
1133                 if (drv->rescan)
1134                         drv->rescan(dev);
1135                 module_put(drv->owner);
1136         }
1137 }
1138
1139 /**
1140  * scsi_scan_target - scan a target id, possibly including all LUNs on the
1141  *     target.
1142  * @sdevsca:    Scsi_Device handle for scanning
1143  * @shost:      host to scan
1144  * @channel:    channel to scan
1145  * @id:         target id to scan
1146  *
1147  * Description:
1148  *     Scan the target id on @shost, @channel, and @id. Scan at least LUN
1149  *     0, and possibly all LUNs on the target id.
1150  *
1151  *     Use the pre-allocated @sdevscan as a handle for the scanning. This
1152  *     function sets sdevscan->host, sdevscan->id and sdevscan->lun; the
1153  *     scanning functions modify sdevscan->lun.
1154  *
1155  *     First try a REPORT LUN scan, if that does not scan the target, do a
1156  *     sequential scan of LUNs on the target id.
1157  **/
1158 static void scsi_scan_target(struct Scsi_Host *shost, unsigned int channel,
1159                              unsigned int id, unsigned int lun, int rescan)
1160 {
1161         int bflags = 0;
1162         int res;
1163         struct scsi_device *sdev;
1164
1165         if (shost->this_id == id)
1166                 /*
1167                  * Don't scan the host adapter
1168                  */
1169                 return;
1170
1171         if (lun != SCAN_WILD_CARD) {
1172                 /*
1173                  * Scan for a specific host/chan/id/lun.
1174                  */
1175                 scsi_probe_and_add_lun(shost, channel, id, lun, NULL, NULL,
1176                                        rescan);
1177                 return;
1178         }
1179
1180         /*
1181          * Scan LUN 0, if there is some response, scan further. Ideally, we
1182          * would not configure LUN 0 until all LUNs are scanned.
1183          */
1184         res = scsi_probe_and_add_lun(shost, channel, id, 0, &bflags, &sdev,
1185                                      rescan);
1186         if (res == SCSI_SCAN_LUN_PRESENT) {
1187                 if (scsi_report_lun_scan(sdev, bflags, rescan) != 0)
1188                         /*
1189                          * The REPORT LUN did not scan the target,
1190                          * do a sequential scan.
1191                          */
1192                         scsi_sequential_lun_scan(shost, channel, id, bflags,
1193                                         res, sdev->scsi_level, rescan);
1194         } else if (res == SCSI_SCAN_TARGET_PRESENT) {
1195                 /*
1196                  * There's a target here, but lun 0 is offline so we
1197                  * can't use the report_lun scan.  Fall back to a
1198                  * sequential lun scan with a bflags of SPARSELUN and
1199                  * a default scsi level of SCSI_2
1200                  */
1201                 scsi_sequential_lun_scan(shost, channel, id, BLIST_SPARSELUN,
1202                                 SCSI_SCAN_TARGET_PRESENT, SCSI_2, rescan);
1203         }
1204 }
1205
1206 static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
1207                               unsigned int id, unsigned int lun, int rescan)
1208 {
1209         uint order_id;
1210
1211         if (id == SCAN_WILD_CARD)
1212                 for (id = 0; id < shost->max_id; ++id) {
1213                         /*
1214                          * XXX adapter drivers when possible (FCP, iSCSI)
1215                          * could modify max_id to match the current max,
1216                          * not the absolute max.
1217                          *
1218                          * XXX add a shost id iterator, so for example,
1219                          * the FC ID can be the same as a target id
1220                          * without a huge overhead of sparse id's.
1221                          */
1222                         if (shost->reverse_ordering)
1223                                 /*
1224                                  * Scan from high to low id.
1225                                  */
1226                                 order_id = shost->max_id - id - 1;
1227                         else
1228                                 order_id = id;
1229                         scsi_scan_target(shost, channel, order_id, lun, rescan);
1230                 }
1231         else
1232                 scsi_scan_target(shost, channel, id, lun, rescan);
1233 }
1234
1235 int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
1236                             unsigned int id, unsigned int lun, int rescan)
1237 {
1238         SCSI_LOG_SCAN_BUS(3, printk (KERN_INFO "%s: <%u:%u:%u:%u>\n",
1239                 __FUNCTION__, shost->host_no, channel, id, lun));
1240
1241         if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
1242             ((id != SCAN_WILD_CARD) && (id > shost->max_id)) ||
1243             ((lun != SCAN_WILD_CARD) && (lun > shost->max_lun)))
1244                 return -EINVAL;
1245
1246         down(&shost->scan_mutex);
1247         if (channel == SCAN_WILD_CARD) 
1248                 for (channel = 0; channel <= shost->max_channel; channel++)
1249                         scsi_scan_channel(shost, channel, id, lun, rescan);
1250         else
1251                 scsi_scan_channel(shost, channel, id, lun, rescan);
1252         up(&shost->scan_mutex);
1253
1254         return 0;
1255 }
1256
1257 /**
1258  * scsi_scan_host - scan the given adapter
1259  * @shost:      adapter to scan
1260  **/
1261 void scsi_scan_host(struct Scsi_Host *shost)
1262 {
1263         scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
1264                                 SCAN_WILD_CARD, 0);
1265 }
1266
1267 void scsi_forget_host(struct Scsi_Host *shost)
1268 {
1269         struct scsi_device *sdev, *tmp;
1270         unsigned long flags;
1271
1272         /*
1273          * Ok, this look a bit strange.  We always look for the first device
1274          * on the list as scsi_remove_device removes them from it - thus we
1275          * also have to release the lock.
1276          * We don't need to get another reference to the device before
1277          * releasing the lock as we already own the reference from
1278          * scsi_register_device that's release in scsi_remove_device.  And
1279          * after that we don't look at sdev anymore.
1280          */
1281         spin_lock_irqsave(shost->host_lock, flags);
1282         list_for_each_entry_safe(sdev, tmp, &shost->__devices, siblings) {
1283                 spin_unlock_irqrestore(shost->host_lock, flags);
1284                 scsi_remove_device(sdev);
1285                 spin_lock_irqsave(shost->host_lock, flags);
1286         }
1287         spin_unlock_irqrestore(shost->host_lock, flags);
1288 }
1289
1290 /*
1291  * Function:    scsi_get_host_dev()
1292  *
1293  * Purpose:     Create a Scsi_Device that points to the host adapter itself.
1294  *
1295  * Arguments:   SHpnt   - Host that needs a Scsi_Device
1296  *
1297  * Lock status: None assumed.
1298  *
1299  * Returns:     The Scsi_Device or NULL
1300  *
1301  * Notes:
1302  *      Attach a single Scsi_Device to the Scsi_Host - this should
1303  *      be made to look like a "pseudo-device" that points to the
1304  *      HA itself.
1305  *
1306  *      Note - this device is not accessible from any high-level
1307  *      drivers (including generics), which is probably not
1308  *      optimal.  We can add hooks later to attach 
1309  */
1310 struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
1311 {
1312         struct scsi_device *sdev;
1313
1314         sdev = scsi_alloc_sdev(shost, 0, shost->this_id, 0);
1315         if (sdev) {
1316                 sdev->borken = 0;
1317         }
1318         return sdev;
1319 }
1320
1321 /*
1322  * Function:    scsi_free_host_dev()
1323  *
1324  * Purpose:     Free a scsi_device that points to the host adapter itself.
1325  *
1326  * Arguments:   SHpnt   - Host that needs a Scsi_Device
1327  *
1328  * Lock status: None assumed.
1329  *
1330  * Returns:     Nothing
1331  *
1332  * Notes:
1333  */
1334 void scsi_free_host_dev(struct scsi_device *sdev)
1335 {
1336         BUG_ON(sdev->id != sdev->host->this_id);
1337
1338         if (sdev->host->hostt->slave_destroy)
1339                 sdev->host->hostt->slave_destroy(sdev);
1340         if (sdev->host->transportt->cleanup)
1341                 sdev->host->transportt->cleanup(sdev);
1342         put_device(&sdev->sdev_gendev);
1343 }