make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / scsi / scsi_scan.c
1 /*
2  *  scsi_scan.c Copyright (C) 2000 Eric Youngdale
3  *
4  *  Bus scan logic.
5  *
6  *  This used to live in scsi.c, but that file was just a laundry basket
7  *  full of misc stuff.  This got separated out in order to make things
8  *  clearer.
9  */
10
11 #define __NO_VERSION__
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15
16 #include <linux/blk.h>
17
18 #include "scsi.h"
19 #include "hosts.h"
20 #include "constants.h"
21
22 #ifdef CONFIG_KMOD
23 #include <linux/kmod.h>
24 #endif
25
26 /* 
27  * Flags for irregular SCSI devices that need special treatment 
28  */
29 #define BLIST_NOLUN             0x001   /* Don't scan for LUNs */
30 #define BLIST_FORCELUN          0x002   /* Known to have LUNs, force sanning */
31 #define BLIST_BORKEN            0x004   /* Flag for broken handshaking */
32 #define BLIST_KEY               0x008   /* Needs to be unlocked by special command */
33 #define BLIST_SINGLELUN         0x010   /* LUNs should better not be used in parallel */
34 #define BLIST_NOTQ              0x020   /* Buggy Tagged Command Queuing */
35 #define BLIST_SPARSELUN         0x040   /* Non consecutive LUN numbering */
36 #define BLIST_MAX5LUN           0x080   /* Avoid LUNS >= 5 */
37 #define BLIST_ISDISK            0x100   /* Treat as (removable) disk */
38 #define BLIST_ISROM             0x200   /* Treat as (removable) CD-ROM */
39 #define BLIST_LARGELUN          0x400   /* LUNs larger than 7 despite reporting as SCSI 2 */
40 #define BLIST_NOSTARTONADD      0x1000  /* do not do automatic start on add,+Wilson04012004,from 2.4.24 */
41
42 static void print_inquiry(unsigned char *data);
43 static int scan_scsis_single(unsigned int channel, unsigned int dev,
44                 unsigned int lun, int lun0_scsi_level, 
45                 unsigned int *max_scsi_dev, unsigned int *sparse_lun, 
46                 Scsi_Device ** SDpnt, struct Scsi_Host *shpnt, 
47                 char *scsi_result);
48 static int find_lun0_scsi_level(unsigned int channel, unsigned int dev,
49                                 struct Scsi_Host *shpnt);
50
51 struct dev_info {
52         const char *vendor;
53         const char *model;
54         const char *revision;   /* Latest revision known to be bad.  Not used yet */
55         unsigned flags;
56 };
57
58 /*
59  * This is what was previously known as the blacklist.  The concept
60  * has been expanded so that we can specify other types of things we
61  * need to be aware of.
62  */
63 static struct dev_info device_list[] =
64 {
65 /* The following devices are known not to tolerate a lun != 0 scan for
66  * one reason or another.  Some will respond to all luns, others will
67  * lock up.
68  */
69         {"Aashima", "IMAGERY 2400SP", "1.03", BLIST_NOLUN},     /* Locks up if polled for lun != 0 */
70         {"CHINON", "CD-ROM CDS-431", "H42", BLIST_NOLUN},       /* Locks up if polled for lun != 0 */
71         {"CHINON", "CD-ROM CDS-535", "Q14", BLIST_NOLUN},       /* Locks up if polled for lun != 0 */
72         {"DENON", "DRD-25X", "V", BLIST_NOLUN},                 /* Locks up if probed for lun != 0 */
73         {"HITACHI", "DK312C", "CM81", BLIST_NOLUN},             /* Responds to all lun - dtg */
74         {"HITACHI", "DK314C", "CR21", BLIST_NOLUN},             /* responds to all lun */
75         {"IMS", "CDD521/10", "2.06", BLIST_NOLUN},              /* Locks-up when LUN>0 polled. */
76         {"MAXTOR", "XT-3280", "PR02", BLIST_NOLUN},             /* Locks-up when LUN>0 polled. */
77         {"MAXTOR", "XT-4380S", "B3C", BLIST_NOLUN},             /* Locks-up when LUN>0 polled. */
78         {"MAXTOR", "MXT-1240S", "I1.2", BLIST_NOLUN},           /* Locks up when LUN>0 polled */
79         {"MAXTOR", "XT-4170S", "B5A", BLIST_NOLUN},             /* Locks-up sometimes when LUN>0 polled. */
80         {"MAXTOR", "XT-8760S", "B7B", BLIST_NOLUN},             /* guess what? */
81         {"MEDIAVIS", "RENO CD-ROMX2A", "2.03", BLIST_NOLUN},    /*Responds to all lun */
82         {"NEC", "CD-ROM DRIVE:841", "1.0", BLIST_NOLUN},        /* Locks-up when LUN>0 polled. */
83         {"PHILIPS", "PCA80SC", "V4-2", BLIST_NOLUN},            /* Responds to all lun */
84         {"RODIME", "RO3000S", "2.33", BLIST_NOLUN},             /* Locks up if polled for lun != 0 */
85         {"SANYO", "CRD-250S", "1.20", BLIST_NOLUN},             /* causes failed REQUEST SENSE on lun 1
86                                                                  * for aha152x controller, which causes
87                                                                  * SCSI code to reset bus.*/
88         {"SEAGATE", "ST157N", "\004|j", BLIST_NOLUN},           /* causes failed REQUEST SENSE on lun 1
89                                                                  * for aha152x controller, which causes
90                                                                  * SCSI code to reset bus.*/
91         {"SEAGATE", "ST296", "921", BLIST_NOLUN},               /* Responds to all lun */
92         {"SEAGATE", "ST1581", "6538", BLIST_NOLUN},             /* Responds to all lun */
93         {"SONY", "CD-ROM CDU-541", "4.3d", BLIST_NOLUN},        
94         {"SONY", "CD-ROM CDU-55S", "1.0i", BLIST_NOLUN},
95         {"SONY", "CD-ROM CDU-561", "1.7x", BLIST_NOLUN},
96         {"SONY", "CD-ROM CDU-8012", "*", BLIST_NOLUN},
97         {"TANDBERG", "TDC 3600", "U07", BLIST_NOLUN},           /* Locks up if polled for lun != 0 */
98         {"TEAC", "CD-R55S", "1.0H", BLIST_NOLUN},               /* Locks up if polled for lun != 0 */
99         {"TEAC", "CD-ROM", "1.06", BLIST_NOLUN},                /* causes failed REQUEST SENSE on lun 1
100                                                                  * for seagate controller, which causes
101                                                                  * SCSI code to reset bus.*/
102         {"TEAC", "MT-2ST/45S2-27", "RV M", BLIST_NOLUN},        /* Responds to all lun */
103         {"TEXEL", "CD-ROM", "1.06", BLIST_NOLUN},               /* causes failed REQUEST SENSE on lun 1
104                                                                  * for seagate controller, which causes
105                                                                  * SCSI code to reset bus.*/
106         {"QUANTUM", "LPS525S", "3110", BLIST_NOLUN},            /* Locks sometimes if polled for lun != 0 */
107         {"QUANTUM", "PD1225S", "3110", BLIST_NOLUN},            /* Locks sometimes if polled for lun != 0 */
108         {"QUANTUM", "FIREBALL ST4.3S", "0F0C", BLIST_NOLUN},    /* Locks up when polled for lun != 0 */
109         {"MEDIAVIS", "CDR-H93MV", "1.31", BLIST_NOLUN},         /* Locks up if polled for lun != 0 */
110         {"SANKYO", "CP525", "6.64", BLIST_NOLUN},               /* causes failed REQ SENSE, extra reset */
111         {"HP", "C1750A", "3226", BLIST_NOLUN},                  /* scanjet iic */
112         {"HP", "C1790A", "", BLIST_NOLUN},                      /* scanjet iip */
113         {"HP", "C2500A", "", BLIST_NOLUN},                      /* scanjet iicx */
114         {"HP", "A6188A", "*", BLIST_SPARSELUN},                 /* HP Va7100 Array */
115         {"HP", "A6189A", "*", BLIST_SPARSELUN},                 /* HP Va7400 Array */
116         {"HP", "A6189B", "*", BLIST_SPARSELUN},                 /* HP Va7410 Array */
117         {"YAMAHA", "CDR100", "1.00", BLIST_NOLUN},              /* Locks up if polled for lun != 0 */
118         {"YAMAHA", "CDR102", "1.00", BLIST_NOLUN},              /* Locks up if polled for lun != 0  
119                                                                  * extra reset */
120         {"YAMAHA", "CRW8424S", "1.0", BLIST_NOLUN},             /* Locks up if polled for lun != 0 */
121         {"YAMAHA", "CRW6416S", "1.0c", BLIST_NOLUN},            /* Locks up if polled for lun != 0 */
122         {"MITSUMI", "CD-R CR-2201CS", "6119", BLIST_NOLUN},     /* Locks up if polled for lun != 0 */
123         {"RELISYS", "Scorpio", "*", BLIST_NOLUN},               /* responds to all LUN */
124         {"RELISYS", "VM3530+", "*", BLIST_NOLUN},               /* responds to all LUN */
125         {"ACROSS", "", "*", BLIST_NOLUN},                       /* responds to all LUN */
126         {"MICROTEK", "ScanMaker II", "5.61", BLIST_NOLUN},      /* responds to all LUN */
127
128 /*
129  * Other types of devices that have special flags.
130  */
131         {"SONY", "CD-ROM CDU-8001", "*", BLIST_BORKEN},
132         {"TEXEL", "CD-ROM", "1.06", BLIST_BORKEN},
133         {"IOMEGA", "Io20S         *F", "*", BLIST_KEY},
134         {"INSITE", "Floptical   F*8I", "*", BLIST_KEY},
135         {"INSITE", "I325VM", "*", BLIST_KEY},
136         {"LASOUND","CDX7405","3.10", BLIST_MAX5LUN | BLIST_SINGLELUN},
137         {"MICROP", "4110", "*", BLIST_NOTQ},                    /* Buggy Tagged Queuing */
138         {"NRC", "MBR-7", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
139         {"NRC", "MBR-7.4", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
140         {"REGAL", "CDC-4X", "*", BLIST_MAX5LUN | BLIST_SINGLELUN},
141         {"NAKAMICH", "MJ-4.8S", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
142         {"NAKAMICH", "MJ-5.16S", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
143         {"PIONEER", "CD-ROM DRM-600", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
144         {"PIONEER", "CD-ROM DRM-602X", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
145         {"PIONEER", "CD-ROM DRM-604X", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
146         {"EMULEX", "MD21/S2     ESDI", "*", BLIST_SINGLELUN},
147         {"CANON", "IPUBJD", "*", BLIST_SPARSELUN},
148         {"nCipher", "Fastness Crypto", "*", BLIST_FORCELUN},
149         {"DEC","HSG80","*", BLIST_FORCELUN},
150         {"COMPAQ","LOGICAL VOLUME","*", BLIST_FORCELUN},
151         {"COMPAQ","CR3500","*", BLIST_FORCELUN},
152         {"NEC", "PD-1 ODX654P", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
153         {"MATSHITA", "PD-1", "*", BLIST_FORCELUN | BLIST_SINGLELUN},
154         {"iomega", "jaz 1GB", "J.86", BLIST_NOTQ | BLIST_NOLUN},
155         {"TOSHIBA","CDROM","*", BLIST_ISROM},
156         {"TOSHIBA","CD-ROM","*", BLIST_ISROM},
157         {"MegaRAID", "LD", "*", BLIST_FORCELUN},
158         {"DGC",  "RAID",      "*", BLIST_SPARSELUN | BLIST_LARGELUN}, // Dell PV 650F (tgt @ LUN 0)
159         {"DGC",  "DISK",      "*", BLIST_SPARSELUN | BLIST_LARGELUN}, // Dell PV 650F (no tgt @ LUN 0) 
160         {"DELL", "PV660F",   "*", BLIST_SPARSELUN | BLIST_LARGELUN},
161         {"DELL", "PV660F   PSEUDO",   "*", BLIST_SPARSELUN | BLIST_LARGELUN},
162         {"DELL", "PSEUDO DEVICE .",   "*", BLIST_SPARSELUN | BLIST_LARGELUN}, // Dell PV 530F
163         {"DELL", "PV530F",    "*", BLIST_SPARSELUN | BLIST_LARGELUN}, // Dell PV 530F
164         {"EMC", "SYMMETRIX", "*", BLIST_SPARSELUN | BLIST_LARGELUN | BLIST_FORCELUN},
165         {"HP", "A6189A", "*", BLIST_SPARSELUN |  BLIST_LARGELUN}, // HP VA7400, by Alar Aun
166         {"HP", "OPEN-", "*", BLIST_SPARSELUN | BLIST_LARGELUN}, /* HP XP Arrays */
167         {"CMD", "CRA-7280", "*", BLIST_SPARSELUN | BLIST_LARGELUN},   // CMD RAID Controller
168         {"CNSI", "G7324", "*", BLIST_SPARSELUN | BLIST_LARGELUN},     // Chaparral G7324 RAID
169         {"CNSi", "G8324", "*", BLIST_SPARSELUN | BLIST_LARGELUN},     // Chaparral G8324 RAID
170         {"Zzyzx", "RocketStor 500S", "*", BLIST_SPARSELUN},
171         {"Zzyzx", "RocketStor 2000", "*", BLIST_SPARSELUN},
172         {"SONY", "TSL",       "*", BLIST_FORCELUN},  // DDS3 & DDS4 autoloaders
173         {"DELL", "PERCRAID", "*", BLIST_FORCELUN},
174         {"HP", "NetRAID-4M", "*", BLIST_FORCELUN},
175         {"ADAPTEC", "AACRAID", "*", BLIST_FORCELUN},
176         {"ADAPTEC", "Adaptec 5400S", "*", BLIST_FORCELUN},
177         {"COMPAQ", "MSA1000", "*", BLIST_FORCELUN},
178         {"HP", "C1557A", "*", BLIST_FORCELUN},
179         {"IBM", "AuSaV1S2", "*", BLIST_FORCELUN},
180         {"FSC", "CentricStor", "*", BLIST_SPARSELUN | BLIST_LARGELUN},
181         {"DDN", "SAN DataDirector", "*", BLIST_SPARSELUN},
182         {"HITACHI", "DF400", "*", BLIST_SPARSELUN},
183         {"HITACHI", "DF500", "*", BLIST_SPARSELUN},
184         {"HITACHI", "DF600", "*", BLIST_SPARSELUN},
185         {"IBM", "ProFibre 4000R", "*", BLIST_SPARSELUN | BLIST_LARGELUN},
186
187         /*
188          * Must be at end of list...
189          */
190         {NULL, NULL, NULL}
191 };
192
193 #define MAX_SCSI_LUNS 0xFFFFFFFF
194
195 #ifdef CONFIG_SCSI_MULTI_LUN
196 static unsigned int max_scsi_luns = MAX_SCSI_LUNS;
197 #else
198 static unsigned int max_scsi_luns = 1;
199 #endif
200
201 #ifdef MODULE
202
203 MODULE_PARM(max_scsi_luns, "i");
204 MODULE_PARM_DESC(max_scsi_luns, "last scsi LUN (should be between 1 and 2^32-1)");
205
206 #else
207
208 static int __init scsi_luns_setup(char *str)
209 {
210         unsigned int tmp;
211
212         if (get_option(&str, &tmp) == 1) {
213                 max_scsi_luns = tmp;
214                 return 1;
215         } else {
216                 printk("scsi_luns_setup : usage max_scsi_luns=n "
217                        "(n should be between 1 and 2^32-1)\n");
218                 return 0;
219         }
220 }
221
222 __setup("max_scsi_luns=", scsi_luns_setup);
223
224 #endif
225
226 static void print_inquiry(unsigned char *data)
227 {
228         int i;
229
230         printk("  Vendor: ");
231         for (i = 8; i < 16; i++) {
232                 if (data[i] >= 0x20 && i < data[4] + 5)
233                         printk("%c", data[i]);
234                 else
235                         printk(" ");
236         }
237
238         printk("  Model: ");
239         for (i = 16; i < 32; i++) {
240                 if (data[i] >= 0x20 && i < data[4] + 5)
241                         printk("%c", data[i]);
242                 else
243                         printk(" ");
244         }
245
246         printk("  Rev: ");
247         for (i = 32; i < 36; i++) {
248                 if (data[i] >= 0x20 && i < data[4] + 5)
249                         printk("%c", data[i]);
250                 else
251                         printk(" ");
252         }
253
254         printk("\n");
255
256         i = data[0] & 0x1f;
257
258         printk("  Type:   %s ",
259                i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] : "Unknown          ");
260         printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
261         if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
262                 printk(" CCS\n");
263         else
264                 printk("\n");
265 }
266
267 static int get_device_flags(unsigned char *response_data)
268 {
269         int i = 0;
270         unsigned char *pnt;
271         for (i = 0; 1; i++) {
272                 if (device_list[i].vendor == NULL)
273                         return 0;
274                 pnt = &response_data[8];
275                 while (*pnt && *pnt == ' ')
276                         pnt++;
277                 if (memcmp(device_list[i].vendor, pnt,
278                            strlen(device_list[i].vendor)))
279                         continue;
280                 pnt = &response_data[16];
281                 while (*pnt && *pnt == ' ')
282                         pnt++;
283                 if (memcmp(device_list[i].model, pnt,
284                            strlen(device_list[i].model)))
285                         continue;
286                 return device_list[i].flags;
287         }
288         return 0;
289 }
290
291 /*
292  *  Detecting SCSI devices :
293  *  We scan all present host adapter's busses,  from ID 0 to ID (max_id).
294  *  We use the INQUIRY command, determine device type, and pass the ID /
295  *  lun address of all sequential devices to the tape driver, all random
296  *  devices to the disk driver.
297  */
298 void scan_scsis(struct Scsi_Host *shpnt,
299                        uint hardcoded,
300                        uint hchannel,
301                        uint hid,
302                        uint hlun)
303 {
304         uint channel;
305         unsigned int dev;
306         unsigned int lun;
307         unsigned int max_dev_lun;
308         unsigned char *scsi_result;
309         unsigned char scsi_result0[256];
310         Scsi_Device *SDpnt;
311         Scsi_Device *SDtail;
312         unsigned int sparse_lun;
313         int lun0_sl;
314
315         scsi_result = NULL;
316
317         SDpnt = (Scsi_Device *) kmalloc(sizeof(Scsi_Device),
318                                         GFP_ATOMIC);
319         if (SDpnt) {
320                 memset(SDpnt, 0, sizeof(Scsi_Device));
321                 /*
322                  * Register the queue for the device.  All I/O requests will
323                  * come in through here.  We also need to register a pointer to
324                  * ourselves, since the queue handler won't know what device
325                  * the queue actually represents.   We could look it up, but it
326                  * is pointless work.
327                  */
328                 scsi_initialize_queue(SDpnt, shpnt);
329                 SDpnt->request_queue.queuedata = (void *) SDpnt;
330                 /* Make sure we have something that is valid for DMA purposes */
331                 scsi_result = ((!shpnt->unchecked_isa_dma)
332                                ? &scsi_result0[0] : kmalloc(512, GFP_DMA));
333         }
334
335         if (scsi_result == NULL) {
336                 printk("Unable to obtain scsi_result buffer\n");
337                 goto leave;
338         }
339         /*
340          * We must chain ourself in the host_queue, so commands can time out 
341          */
342         SDpnt->queue_depth = 1;
343         SDpnt->host = shpnt;
344         SDpnt->online = TRUE;
345
346         initialize_merge_fn(SDpnt);
347
348         /*
349          * Initialize the object that we will use to wait for command blocks.
350          */
351         init_waitqueue_head(&SDpnt->scpnt_wait);
352
353         /*
354          * Next, hook the device to the host in question.
355          */
356         SDpnt->prev = NULL;
357         SDpnt->next = NULL;
358         if (shpnt->host_queue != NULL) {
359                 SDtail = shpnt->host_queue;
360                 while (SDtail->next != NULL)
361                         SDtail = SDtail->next;
362
363                 SDtail->next = SDpnt;
364                 SDpnt->prev = SDtail;
365         } else {
366                 shpnt->host_queue = SDpnt;
367         }
368
369         /*
370          * We need to increment the counter for this one device so we can track
371          * when things are quiet.
372          */
373         if (hardcoded == 1) {
374                 Scsi_Device *oldSDpnt = SDpnt;
375                 struct Scsi_Device_Template *sdtpnt;
376                 channel = hchannel;
377                 if (channel > shpnt->max_channel)
378                         goto leave;
379                 dev = hid;
380                 if (dev >= shpnt->max_id)
381                         goto leave;
382                 lun = hlun;
383                 if (lun >= shpnt->max_lun)
384                         goto leave;
385                 if ((0 == lun) || (lun > 7))
386                         lun0_sl = SCSI_3; /* actually don't care for 0 == lun */
387                 else
388                         lun0_sl = find_lun0_scsi_level(channel, dev, shpnt);
389                 scan_scsis_single(channel, dev, lun, lun0_sl, &max_dev_lun, 
390                                   &sparse_lun, &SDpnt, shpnt, scsi_result);
391                 if (SDpnt != oldSDpnt) {
392
393                         /* it could happen the blockdevice hasn't yet been inited */
394                         /* queue_depth() moved from scsi_proc_info() so that
395                            it is called before scsi_build_commandblocks() */
396                         if (shpnt->select_queue_depths != NULL)
397                                 (shpnt->select_queue_depths)(shpnt,
398                                                              shpnt->host_queue);
399
400                         for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next)
401                                 if (sdtpnt->init && sdtpnt->dev_noticed)
402                                         (*sdtpnt->init) ();
403
404                         for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
405                                 if (sdtpnt->attach) {
406                                         (*sdtpnt->attach) (oldSDpnt);
407                                         if (oldSDpnt->attached) {
408                                                 scsi_build_commandblocks(oldSDpnt);
409                                                 if (0 == oldSDpnt->has_cmdblocks) {
410                                                         printk("scan_scsis: DANGER, no command blocks\n");
411                                                         /* What to do now ?? */
412                                                 }
413                                         }
414                                 }
415                         }
416                         scsi_resize_dma_pool();
417
418                         for (sdtpnt = scsi_devicelist; sdtpnt; sdtpnt = sdtpnt->next) {
419                                 if (sdtpnt->finish && sdtpnt->nr_dev) {
420                                         (*sdtpnt->finish) ();
421                                 }
422                         }
423                 }
424         } else {
425                 /* Actual LUN. PC ordering is 0->n IBM/spec ordering is n->0 */
426                 int order_dev;
427
428                 for (channel = 0; channel <= shpnt->max_channel; channel++) {
429                         for (dev = 0; dev < shpnt->max_id; ++dev) {
430                                 if (shpnt->reverse_ordering)
431                                         /* Shift to scanning 15,14,13... or 7,6,5,4, */
432                                         order_dev = shpnt->max_id - dev - 1;
433                                 else
434                                         order_dev = dev;
435
436                                 if (shpnt->this_id != order_dev) {
437
438                                         /*
439                                          * We need the for so our continue, etc. work fine. We put this in
440                                          * a variable so that we can override it during the scan if we
441                                          * detect a device *KNOWN* to have multiple logical units.
442                                          */
443                                         max_dev_lun = (max_scsi_luns < shpnt->max_lun ?
444                                          max_scsi_luns : shpnt->max_lun);
445                                         sparse_lun = 0;
446                                         for (lun = 0, lun0_sl = SCSI_2; lun < max_dev_lun; ++lun) {
447                                                 /* don't probe further for luns > 7 for targets <= SCSI_2 */
448                                                 if ((lun0_sl < SCSI_3) && (lun > 7))
449                                                         break;
450
451                                                 if (!scan_scsis_single(channel, order_dev, lun, lun0_sl,
452                                                                        &max_dev_lun, &sparse_lun, &SDpnt, shpnt,
453                                                                        scsi_result)
454                                                     && !sparse_lun)
455                                                         break;  /* break means don't probe further for luns!=0 */
456                                                 if (SDpnt && (0 == lun)) {
457                                                         int bflags = get_device_flags (scsi_result);
458                                                         if (bflags & BLIST_LARGELUN)
459                                                                 lun0_sl = SCSI_3; /* treat as SCSI 3 */
460                                                         else
461                                                                 lun0_sl = SDpnt->scsi_level;
462                                                 }
463                                         }       /* for lun ends */
464                                 }       /* if this_id != id ends */
465                         }       /* for dev ends */
466                 }               /* for channel ends */
467         }                       /* if/else hardcoded */
468
469       leave:
470
471         {                       /* Unchain SRpnt from host_queue */
472                 Scsi_Device *prev, *next;
473                 Scsi_Device *dqptr;
474
475                 for (dqptr = shpnt->host_queue; dqptr != SDpnt; dqptr = dqptr->next)
476                         continue;
477                 if (dqptr) {
478                         prev = dqptr->prev;
479                         next = dqptr->next;
480                         if (prev)
481                                 prev->next = next;
482                         else
483                                 shpnt->host_queue = next;
484                         if (next)
485                                 next->prev = prev;
486                 }
487         }
488
489         /* Last device block does not exist.  Free memory. */
490         if (SDpnt != NULL) {
491                 blk_cleanup_queue(&SDpnt->request_queue);
492                 kfree((char *) SDpnt);
493         }
494
495         /* If we allocated a buffer so we could do DMA, free it now */
496         if (scsi_result != &scsi_result0[0] && scsi_result != NULL) {
497                 kfree(scsi_result);
498         } {
499                 Scsi_Device *sdev;
500                 Scsi_Cmnd *scmd;
501
502                 SCSI_LOG_SCAN_BUS(4, printk("Host status for host %p:\n", shpnt));
503                 for (sdev = shpnt->host_queue; sdev; sdev = sdev->next) {
504                         SCSI_LOG_SCAN_BUS(4, printk("Device %d %p: ", sdev->id, sdev));
505                         for (scmd = sdev->device_queue; scmd; scmd = scmd->next) {
506                                 SCSI_LOG_SCAN_BUS(4, printk("%p ", scmd));
507                         }
508                         SCSI_LOG_SCAN_BUS(4, printk("\n"));
509                 }
510         }
511 }
512
513 /*
514  * The worker for scan_scsis.
515  * Returning 0 means Please don't ask further for lun!=0, 1 means OK go on.
516  * Global variables used : scsi_devices(linked list)
517  */
518 static int scan_scsis_single(unsigned int channel, unsigned int dev,
519                 unsigned int lun, int lun0_scsi_level,
520                 unsigned int *max_dev_lun, unsigned int *sparse_lun, 
521                 Scsi_Device ** SDpnt2, struct Scsi_Host *shpnt, 
522                 char *scsi_result)
523 {
524         char devname[64];
525         unsigned char scsi_cmd[MAX_COMMAND_SIZE];
526         struct Scsi_Device_Template *sdtpnt;
527         Scsi_Device *SDtail, *SDpnt = *SDpnt2;
528         Scsi_Request * SRpnt;
529         int bflags, type = -1;
530         extern devfs_handle_t scsi_devfs_handle;
531         int scsi_level;
532
533         SDpnt->host = shpnt;
534         SDpnt->id = dev;
535         SDpnt->lun = lun;
536         SDpnt->channel = channel;
537         SDpnt->online = TRUE;
538
539         scsi_build_commandblocks(SDpnt);
540  
541         /* Some low level driver could use device->type (DB) */
542         SDpnt->type = -1;
543
544         /*
545          * Assume that the device will have handshaking problems, and then fix
546          * this field later if it turns out it doesn't
547          */
548         SDpnt->borken = 1;
549         SDpnt->was_reset = 0;
550         SDpnt->expecting_cc_ua = 0;
551         SDpnt->starved = 0;
552
553         if (NULL == (SRpnt = scsi_allocate_request(SDpnt))) {
554                 printk("scan_scsis_single: no memory\n");
555                 return 0;
556         }
557
558         /*
559          * We used to do a TEST_UNIT_READY before the INQUIRY but that was 
560          * not really necessary.  Spec recommends using INQUIRY to scan for
561          * devices (and TEST_UNIT_READY to poll for media change). - Paul G.
562          */
563
564         SCSI_LOG_SCAN_BUS(3, printk("scsi: performing INQUIRY\n"));
565         /*
566          * Build an INQUIRY command block.
567          */
568         scsi_cmd[0] = INQUIRY;
569         if ((lun > 0) && (lun0_scsi_level <= SCSI_2))
570                 scsi_cmd[1] = (lun << 5) & 0xe0;
571         else    
572                 scsi_cmd[1] = 0;        /* SCSI_3 and higher, don't touch */
573         scsi_cmd[2] = 0;
574         scsi_cmd[3] = 0;
575         scsi_cmd[4] = 255;
576         scsi_cmd[5] = 0;
577         SRpnt->sr_cmd_len = 0;
578         SRpnt->sr_data_direction = SCSI_DATA_READ;
579
580         scsi_wait_req (SRpnt, (void *) scsi_cmd,
581                   (void *) scsi_result,
582                   256, SCSI_TIMEOUT+4*HZ, 3);
583
584         SCSI_LOG_SCAN_BUS(3, printk("scsi: INQUIRY %s with code 0x%x\n",
585                 SRpnt->sr_result ? "failed" : "successful", SRpnt->sr_result));
586
587         /*
588          * Now that we don't do TEST_UNIT_READY anymore, we must be prepared
589          * for media change conditions here, so cannot require zero result.
590          */
591         if (SRpnt->sr_result) {
592                 if ((driver_byte(SRpnt->sr_result) & DRIVER_SENSE) != 0 &&
593                     (SRpnt->sr_sense_buffer[2] & 0xf) == UNIT_ATTENTION &&
594                     SRpnt->sr_sense_buffer[12] == 0x28 &&
595                     SRpnt->sr_sense_buffer[13] == 0) {
596                         /* not-ready to ready transition - good */
597                 } else {
598                         /* assume no peripheral if any other sort of error */
599                         scsi_release_request(SRpnt);
600                         return 0;
601                 }
602         }
603
604         /*
605          * Check for SPARSELUN before checking the peripheral qualifier,
606          * so sparse lun devices are completely scanned.
607          */
608
609         /*
610          * Get any flags for this device.  
611          */
612         bflags = get_device_flags (scsi_result);
613
614         if (bflags & BLIST_SPARSELUN) {
615           *sparse_lun = 1;
616         }
617         /*
618          * Check the peripheral qualifier field - this tells us whether LUNS
619          * are supported here or not.
620          */
621         if ((scsi_result[0] >> 5) == 3) {
622                 scsi_release_request(SRpnt);
623                 return 0;       /* assume no peripheral if any sort of error */
624         }
625          /*   The Toshiba ROM was "gender-changed" here as an inline hack.
626               This is now much more generic.
627               This is a mess: What we really want is to leave the scsi_result
628               alone, and just change the SDpnt structure. And the SDpnt is what
629               we want print_inquiry to print.  -- REW
630          */
631         if (bflags & BLIST_ISDISK) {
632                 scsi_result[0] = TYPE_DISK;                                                
633                 scsi_result[1] |= 0x80;     /* removable */
634         }
635
636         if (bflags & BLIST_ISROM) {
637                 scsi_result[0] = TYPE_ROM;
638                 scsi_result[1] |= 0x80;     /* removable */
639         }
640     
641         memcpy(SDpnt->vendor, scsi_result + 8, 8);
642         memcpy(SDpnt->model, scsi_result + 16, 16);
643         memcpy(SDpnt->rev, scsi_result + 32, 4);
644
645         SDpnt->removable = (0x80 & scsi_result[1]) >> 7;
646         /* Use the peripheral qualifier field to determine online/offline */
647         if (((scsi_result[0] >> 5) & 7) == 1)   SDpnt->online = FALSE;
648         else SDpnt->online = TRUE;
649         SDpnt->lockable = SDpnt->removable;
650         SDpnt->changed = 0;
651         SDpnt->access_count = 0;
652         SDpnt->busy = 0;
653         SDpnt->has_cmdblocks = 0;
654         /*
655          * Currently, all sequential devices are assumed to be tapes, all random
656          * devices disk, with the appropriate read only flags set for ROM / WORM
657          * treated as RO.
658          */
659         switch (type = (scsi_result[0] & 0x1f)) {
660         case TYPE_TAPE:
661         case TYPE_DISK:
662         case TYPE_PRINTER:
663         case TYPE_MOD:
664         case TYPE_PROCESSOR:
665         case TYPE_SCANNER:
666         case TYPE_MEDIUM_CHANGER:
667         case TYPE_ENCLOSURE:
668         case TYPE_COMM:
669                 SDpnt->writeable = 1;
670                 break;
671         case TYPE_WORM:
672         case TYPE_ROM:
673                 SDpnt->writeable = 0;
674                 break;
675         default:
676                 printk("scsi: unknown type %d\n", type);
677         }
678
679         SDpnt->device_blocked = FALSE;
680         SDpnt->device_busy = 0;
681         SDpnt->single_lun = 0;
682         SDpnt->soft_reset =
683             (scsi_result[7] & 1) && ((scsi_result[3] & 7) == 2);
684         SDpnt->random = (type == TYPE_TAPE) ? 0 : 1;
685         SDpnt->type = (type & 0x1f);
686
687         print_inquiry(scsi_result);
688
689         sprintf (devname, "host%d/bus%d/target%d/lun%d",
690                  SDpnt->host->host_no, SDpnt->channel, SDpnt->id, SDpnt->lun);
691         if (SDpnt->de) printk ("DEBUG: dir: \"%s\" already exists\n", devname);
692         else SDpnt->de = devfs_mk_dir (scsi_devfs_handle, devname, NULL);
693
694         for (sdtpnt = scsi_devicelist; sdtpnt;
695              sdtpnt = sdtpnt->next)
696                 if (sdtpnt->detect)
697                         SDpnt->attached +=
698                             (*sdtpnt->detect) (SDpnt);
699
700         SDpnt->scsi_level = scsi_result[2] & 0x07;
701         if (SDpnt->scsi_level >= 2 ||
702             (SDpnt->scsi_level == 1 &&
703              (scsi_result[3] & 0x0f) == 1))
704                 SDpnt->scsi_level++;
705         scsi_level = SDpnt->scsi_level;
706
707         /*
708          * Accommodate drivers that want to sleep when they should be in a polling
709          * loop.
710          */
711         SDpnt->disconnect = 0;
712
713
714         /*
715          * Set the tagged_queue flag for SCSI-II devices that purport to support
716          * tagged queuing in the INQUIRY data.
717          */
718         SDpnt->tagged_queue = 0;
719         if ((SDpnt->scsi_level >= SCSI_2) &&
720             (scsi_result[7] & 2) &&
721             !(bflags & BLIST_NOTQ)) {
722                 SDpnt->tagged_supported = 1;
723                 SDpnt->current_tag = 0;
724         }
725         /*
726          * Some revisions of the Texel CD ROM drives have handshaking problems when
727          * used with the Seagate controllers.  Before we know what type of device
728          * we're talking to, we assume it's borken and then change it here if it
729          * turns out that it isn't a TEXEL drive.
730          */
731         if ((bflags & BLIST_BORKEN) == 0)
732                 SDpnt->borken = 0;
733
734         //+Wilson04012004,from 2.4.24
735         /*
736          * Some devices may not want to have a start command automatically
737          * issued when a device is added.
738          */
739         if (bflags & BLIST_NOSTARTONADD)
740                 SDpnt->no_start_on_add = 1;
741         //Wilson04012004+
742
743         /*
744          * If we want to only allow I/O to one of the luns attached to this device
745          * at a time, then we set this flag.
746          */
747         if (bflags & BLIST_SINGLELUN)
748                 SDpnt->single_lun = 1;
749
750         /*
751          * These devices need this "key" to unlock the devices so we can use it
752          */
753         if ((bflags & BLIST_KEY) != 0) {
754                 printk("Unlocked floptical drive.\n");
755                 SDpnt->lockable = 0;
756                 scsi_cmd[0] = MODE_SENSE;
757                 if (shpnt->max_lun <= 8)
758                         scsi_cmd[1] = (lun << 5) & 0xe0;
759                 else    scsi_cmd[1] = 0;        /* any other idea? */
760                 scsi_cmd[2] = 0x2e;
761                 scsi_cmd[3] = 0;
762                 scsi_cmd[4] = 0x2a;
763                 scsi_cmd[5] = 0;
764                 SRpnt->sr_cmd_len = 0;
765                 SRpnt->sr_data_direction = SCSI_DATA_READ;
766                 scsi_wait_req (SRpnt, (void *) scsi_cmd,
767                         (void *) scsi_result, 0x2a,
768                         SCSI_TIMEOUT, 3);
769         }
770
771         scsi_release_request(SRpnt);
772         SRpnt = NULL;
773
774         scsi_release_commandblocks(SDpnt);
775
776         /*
777          * This device was already hooked up to the host in question,
778          * so at this point we just let go of it and it should be fine.  We do need to
779          * allocate a new one and attach it to the host so that we can further scan the bus.
780          */
781         SDpnt = (Scsi_Device *) kmalloc(sizeof(Scsi_Device), GFP_ATOMIC);
782         if (!SDpnt) {
783                 printk("scsi: scan_scsis_single: Cannot malloc\n");
784                 return 0;
785         }
786         memset(SDpnt, 0, sizeof(Scsi_Device));
787
788         *SDpnt2 = SDpnt;
789         SDpnt->queue_depth = 1;
790         SDpnt->host = shpnt;
791         SDpnt->online = TRUE;
792         SDpnt->scsi_level = scsi_level;
793
794         /*
795          * Register the queue for the device.  All I/O requests will come
796          * in through here.  We also need to register a pointer to
797          * ourselves, since the queue handler won't know what device
798          * the queue actually represents.   We could look it up, but it
799          * is pointless work.
800          */
801         scsi_initialize_queue(SDpnt, shpnt);
802         SDpnt->host = shpnt;
803         initialize_merge_fn(SDpnt);
804
805         /*
806          * Mark this device as online, or otherwise we won't be able to do much with it.
807          */
808         SDpnt->online = TRUE;
809
810         /*
811          * Initialize the object that we will use to wait for command blocks.
812          */
813         init_waitqueue_head(&SDpnt->scpnt_wait);
814
815         /*
816          * Since we just found one device, there had damn well better be one in the list
817          * already.
818          */
819         if (shpnt->host_queue == NULL)
820                 panic("scan_scsis_single: Host queue == NULL\n");
821
822         SDtail = shpnt->host_queue;
823         while (SDtail->next) {
824                 SDtail = SDtail->next;
825         }
826
827         /* Add this device to the linked list at the end */
828         SDtail->next = SDpnt;
829         SDpnt->prev = SDtail;
830         SDpnt->next = NULL;
831
832         /*
833          * Some scsi devices cannot be polled for lun != 0 due to firmware bugs
834          */
835         if (bflags & BLIST_NOLUN)
836                 return 0;       /* break; */
837
838         /*
839          * If this device is known to support sparse multiple units, override the
840          * other settings, and scan all of them.
841          */
842         if (bflags & BLIST_SPARSELUN) {
843                 *max_dev_lun = shpnt->max_lun;
844                 *sparse_lun = 1;
845                 return 1;
846         }
847         /*
848          * If this device is known to support multiple units, override the other
849          * settings, and scan all of them.
850          */
851         if (bflags & BLIST_FORCELUN) {
852                 /* 
853                  * Scanning MAX_SCSI_LUNS units would be a bad idea.
854                  * Any better idea?
855                  * I think we need REPORT LUNS in future to avoid scanning
856                  * of unused LUNs. But, that is another item.
857                  */
858                 if (*max_dev_lun < shpnt->max_lun)
859                         *max_dev_lun = shpnt->max_lun;
860                 else    if ((max_scsi_luns >> 1) >= *max_dev_lun)
861                                 *max_dev_lun += shpnt->max_lun;
862                         else    *max_dev_lun = max_scsi_luns;
863                 return 1;
864         }
865         /*
866          * REGAL CDC-4X: avoid hang after LUN 4
867          */
868         if (bflags & BLIST_MAX5LUN) {
869                 *max_dev_lun = 5;
870                 return 1;
871         }
872
873         /*
874          * We assume the device can't handle lun!=0 if: - it reports scsi-0
875          * (ANSI SCSI Revision 0) (old drives like MAXTOR XT-3280) or - it
876          * reports scsi-1 (ANSI SCSI Revision 1) and Response Data Format 0
877          */
878         if (((scsi_result[2] & 0x07) == 0)
879             ||
880             ((scsi_result[2] & 0x07) == 1 &&
881              (scsi_result[3] & 0x0f) == 0))
882                 return 0;
883         return 1;
884 }
885
886 /*
887  * The worker for scan_scsis.
888  * Returns the scsi_level of lun0 on this host, channel and dev (if already
889  * known), otherwise returns SCSI_2.
890  */
891 static int find_lun0_scsi_level(unsigned int channel, unsigned int dev,
892                                 struct Scsi_Host *shpnt)
893 {
894         int res = SCSI_2;
895         Scsi_Device *SDpnt;
896
897         for (SDpnt = shpnt->host_queue; SDpnt; SDpnt = SDpnt->next)
898         {
899                 if ((0 == SDpnt->lun) && (dev == SDpnt->id) &&
900                     (channel == SDpnt->channel))
901                         return (int)SDpnt->scsi_level;
902         }
903         /* haven't found lun0, should send INQUIRY but take easy route */
904         return res;
905 }