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