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