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