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