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