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