File: /usr/src/linux/drivers/block/acsi.c

1     /*
2      * acsi.c -- Device driver for Atari ACSI hard disks
3      *
4      * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5      *
6      * Some parts are based on hd.c by Linus Torvalds
7      *
8      * This file is subject to the terms and conditions of the GNU General Public
9      * License.  See the file COPYING in the main directory of this archive for
10      * more details.
11      *
12      */
13     
14     /*
15      * Still to in this file:
16      *  - If a command ends with an error status (!= 0), the following
17      *    REQUEST SENSE commands (4 to fill the ST-DMA FIFO) are done by
18      *    polling the _IRQ signal (not interrupt-driven). This should be
19      *    avoided in future because it takes up a non-neglectible time in
20      *    the interrupt service routine while interrupts are disabled.
21      *    Maybe a timer interrupt will get lost :-(
22      */
23     
24     /*
25      * General notes:
26      *
27      *  - All ACSI devices (disks, CD-ROMs, ...) use major number 28.
28      *    Minors are organized like it is with SCSI: The upper 4 bits
29      *    identify the device, the lower 4 bits the partition.
30      *    The device numbers (the upper 4 bits) are given in the same
31      *    order as the devices are found on the bus.
32      *  - Up to 8 LUNs are supported for each target (if CONFIG_ACSI_MULTI_LUN
33      *    is defined), but only a total of 16 devices (due to minor
34      *    numbers...). Note that Atari allows only a maximum of 4 targets
35      *    (i.e. controllers, not devices) on the ACSI bus!
36      *  - A optimizing scheme similar to SCSI scatter-gather is implemented.
37      *  - Removable media are supported. After a medium change to device
38      *    is reinitialized (partition check etc.). Also, if the device
39      *    knows the PREVENT/ALLOW MEDIUM REMOVAL command, the door should
40      *    be locked and unlocked when mounting the first or unmounting the
41      *    last filesystem on the device. The code is untested, because I
42      *    don't have a removable hard disk.
43      *
44      */
45     
46     #define MAJOR_NR ACSI_MAJOR
47     
48     #include <linux/config.h>
49     #include <linux/module.h>
50     #include <linux/errno.h>
51     #include <linux/signal.h>
52     #include <linux/sched.h>
53     #include <linux/timer.h>
54     #include <linux/fs.h>
55     #include <linux/kernel.h>
56     #include <linux/genhd.h>
57     #include <linux/devfs_fs_kernel.h>
58     #include <linux/delay.h>
59     #include <linux/mm.h>
60     #include <linux/major.h>
61     #include <linux/blk.h>
62     #include <linux/slab.h>
63     #include <linux/interrupt.h>
64     #include <scsi/scsi.h> /* for SCSI_IOCTL_GET_IDLUN */
65     typedef void Scsi_Device; /* hack to avoid including scsi.h */
66     #include <scsi/scsi_ioctl.h>
67     #include <linux/hdreg.h> /* for HDIO_GETGEO */
68     #include <linux/blkpg.h>
69     
70     #include <asm/setup.h>
71     #include <asm/pgtable.h>
72     #include <asm/system.h>
73     #include <asm/uaccess.h>
74     #include <asm/atarihw.h>
75     #include <asm/atariints.h>
76     #include <asm/atari_acsi.h>
77     #include <asm/atari_stdma.h>
78     #include <asm/atari_stram.h>
79     
80     
81     #define DEBUG
82     #undef DEBUG_DETECT
83     #undef NO_WRITE
84     
85     #define MAX_ERRORS     		8	/* Max read/write errors/sector */
86     #define MAX_LUN				8	/* Max LUNs per target */
87     #define MAX_DEV		   		16
88     
89     #define ACSI_BUFFER_SIZE			(16*1024) /* "normal" ACSI buffer size */
90     #define ACSI_BUFFER_MINSIZE			(2048) 	  /* min. buf size if ext. DMA */
91     #define ACSI_BUFFER_SIZE_ORDER	 	2		  /* order size for above */
92     #define ACSI_BUFFER_MINSIZE_ORDER	0 	  	  /* order size for above */
93     #define ACSI_BUFFER_SECTORS	(ACSI_BUFFER_SIZE/512)
94     
95     #define ACSI_BUFFER_ORDER \
96     	(ATARIHW_PRESENT(EXTD_DMA) ? \
97     	 ACSI_BUFFER_MINSIZE_ORDER : \
98     	 ACSI_BUFFER_SIZE_ORDER)
99     
100     #define ACSI_TIMEOUT		(4*HZ)
101     
102     /* minimum delay between two commands */
103     
104     #define COMMAND_DELAY 500
105     
106     typedef enum {
107     	NONE, HARDDISK, CDROM
108     } ACSI_TYPE;
109     
110     struct acsi_info_struct {
111     	ACSI_TYPE		type;			/* type of device */
112     	unsigned		target;			/* target number */
113     	unsigned		lun;			/* LUN in target controller */
114     	unsigned		removable : 1;	/* Flag for removable media */
115     	unsigned		read_only : 1;	/* Flag for read only devices */
116     	unsigned		old_atari_disk : 1; /* Is an old Atari disk       */
117     	unsigned		changed : 1;	/* Medium has been changed */
118     	unsigned long 	size;			/* #blocks */
119     } acsi_info[MAX_DEV];
120     
121     /*
122      *	SENSE KEYS
123      */
124     
125     #define NO_SENSE		0x00
126     #define RECOVERED_ERROR 	0x01
127     #define NOT_READY		0x02
128     #define MEDIUM_ERROR		0x03
129     #define HARDWARE_ERROR		0x04
130     #define ILLEGAL_REQUEST 	0x05
131     #define UNIT_ATTENTION		0x06
132     #define DATA_PROTECT		0x07
133     #define BLANK_CHECK		0x08
134     #define COPY_ABORTED		0x0a
135     #define ABORTED_COMMAND 	0x0b
136     #define VOLUME_OVERFLOW 	0x0d
137     #define MISCOMPARE		0x0e
138     
139     
140     /*
141      *	DEVICE TYPES
142      */
143     
144     #define TYPE_DISK	0x00
145     #define TYPE_TAPE	0x01
146     #define TYPE_WORM	0x04
147     #define TYPE_ROM	0x05
148     #define TYPE_MOD	0x07
149     #define TYPE_NO_LUN	0x7f
150     
151     /* The data returned by MODE SENSE differ between the old Atari
152      * hard disks and SCSI disks connected to ACSI. In the following, both
153      * formats are defined and some macros to operate on them potably.
154      */
155     
156     typedef struct {
157     	unsigned long	dummy[2];
158     	unsigned long	sector_size;
159     	unsigned char	format_code;
160     #define ATARI_SENSE_FORMAT_FIX	1	
161     #define ATARI_SENSE_FORMAT_CHNG	2
162     	unsigned char	cylinders_h;
163     	unsigned char	cylinders_l;
164     	unsigned char	heads;
165     	unsigned char	reduced_h;
166     	unsigned char	reduced_l;
167     	unsigned char	precomp_h;
168     	unsigned char	precomp_l;
169     	unsigned char	landing_zone;
170     	unsigned char	steprate;
171     	unsigned char	type;
172     #define ATARI_SENSE_TYPE_FIXCHNG_MASK		4
173     #define ATARI_SENSE_TYPE_SOFTHARD_MASK		8
174     #define ATARI_SENSE_TYPE_FIX				4
175     #define ATARI_SENSE_TYPE_CHNG				0
176     #define ATARI_SENSE_TYPE_SOFT				0
177     #define ATARI_SENSE_TYPE_HARD				8
178     	unsigned char	sectors;
179     } ATARI_SENSE_DATA;
180     
181     #define ATARI_CAPACITY(sd) \
182     	(((int)((sd).cylinders_h<<8)|(sd).cylinders_l) * \
183     	 (sd).heads * (sd).sectors)
184     
185     
186     typedef struct {
187     	unsigned char   dummy1;
188     	unsigned char   medium_type;
189     	unsigned char   dummy2;
190     	unsigned char   descriptor_size;
191     	unsigned long   block_count;
192     	unsigned long   sector_size;
193     	/* Page 0 data */
194     	unsigned char	page_code;
195     	unsigned char	page_size;
196     	unsigned char	page_flags;
197     	unsigned char	qualifier;
198     } SCSI_SENSE_DATA;
199     
200     #define SCSI_CAPACITY(sd) 	((sd).block_count & 0xffffff)
201     
202     
203     typedef union {
204     	ATARI_SENSE_DATA	atari;
205     	SCSI_SENSE_DATA		scsi;
206     } SENSE_DATA;
207     
208     #define SENSE_TYPE_UNKNOWN	0
209     #define SENSE_TYPE_ATARI	1
210     #define SENSE_TYPE_SCSI		2
211     
212     #define SENSE_TYPE(sd)										\
213     	(((sd).atari.dummy[0] == 8 &&							\
214     	  ((sd).atari.format_code == 1 ||						\
215     	   (sd).atari.format_code == 2)) ? SENSE_TYPE_ATARI :	\
216     	 ((sd).scsi.dummy1 >= 11) ? SENSE_TYPE_SCSI :			\
217     	 SENSE_TYPE_UNKNOWN)
218     	 
219     #define CAPACITY(sd)							\
220     	(SENSE_TYPE(sd) == SENSE_TYPE_ATARI ?		\
221     	 ATARI_CAPACITY((sd).atari) :				\
222     	 SCSI_CAPACITY((sd).scsi))
223     
224     #define SECTOR_SIZE(sd)							\
225     	(SENSE_TYPE(sd) == SENSE_TYPE_ATARI ?		\
226     	 (sd).atari.sector_size :					\
227     	 (sd).scsi.sector_size & 0xffffff)
228     
229     /* Default size if capacity cannot be determined (1 GByte) */
230     #define	DEFAULT_SIZE	0x1fffff
231     
232     #define CARTRCH_STAT(dev,buf)					\
233     	(acsi_info[(dev)].old_atari_disk ?			\
234     	 (((buf)[0] & 0x7f) == 0x28) :					\
235     	 ((((buf)[0] & 0x70) == 0x70) ?					\
236     	  (((buf)[2] & 0x0f) == 0x06) :					\
237     	  (((buf)[0] & 0x0f) == 0x06)))					\
238     
239     /* These two are also exported to other drivers that work on the ACSI bus and
240      * need an ST-RAM buffer. */
241     char 			*acsi_buffer;
242     unsigned long 	phys_acsi_buffer;
243     
244     static int				NDevices = 0;
245     static int				acsi_sizes[MAX_DEV<<4] = { 0, };
246     static int				acsi_blocksizes[MAX_DEV<<4] = { 0, };
247     static struct hd_struct	acsi_part[MAX_DEV<<4] = { {0,0}, };
248     static int 				access_count[MAX_DEV] = { 0, };
249     static char 			busy[MAX_DEV] = { 0, };
250     static DECLARE_WAIT_QUEUE_HEAD(busy_wait);
251     
252     static int				CurrentNReq;
253     static int				CurrentNSect;
254     static char				*CurrentBuffer;
255     
256     
257     #define SET_TIMER()	mod_timer(&acsi_timer, jiffies + ACSI_TIMEOUT)
258     #define CLEAR_TIMER()	del_timer(&acsi_timer)
259     
260     static unsigned long	STramMask;
261     #define STRAM_ADDR(a)	(((a) & STramMask) == 0)
262     
263     
264     
265     /* ACSI commands */
266     
267     static char tur_cmd[6]        = { 0x00, 0, 0, 0, 0, 0 };
268     static char modesense_cmd[6]  = { 0x1a, 0, 0, 0, 24, 0 };
269     static char modeselect_cmd[6] = { 0x15, 0, 0, 0, 12, 0 };
270     static char inquiry_cmd[6]    = { 0x12, 0, 0, 0,255, 0 };
271     static char reqsense_cmd[6]   = { 0x03, 0, 0, 0, 4, 0 };
272     static char read_cmd[6]       = { 0x08, 0, 0, 0, 0, 0 };
273     static char write_cmd[6]      = { 0x0a, 0, 0, 0, 0, 0 };
274     static char pa_med_rem_cmd[6] = { 0x1e, 0, 0, 0, 0, 0 };
275     
276     #define CMDSET_TARG_LUN(cmd,targ,lun)			\
277         do {						\
278     		cmd[0] = (cmd[0] & ~0xe0) | (targ)<<5;	\
279     		cmd[1] = (cmd[1] & ~0xe0) | (lun)<<5;	\
280     	} while(0)
281     
282     #define CMDSET_BLOCK(cmd,blk)						\
283         do {											\
284     		unsigned long __blk = (blk);				\
285     		cmd[3] = __blk; __blk >>= 8;				\
286     		cmd[2] = __blk; __blk >>= 8;				\
287     		cmd[1] = (cmd[1] & 0xe0) | (__blk & 0x1f);	\
288     	} while(0)
289     
290     #define CMDSET_LEN(cmd,len)						\
291     	do {										\
292     		cmd[4] = (len);							\
293     	} while(0)
294     
295     /* ACSI errors (from REQUEST SENSE); There are two tables, one for the
296      * old Atari disks and one for SCSI on ACSI disks.
297      */
298     
299     struct acsi_error {
300     	unsigned char	code;
301     	const char		*text;
302     } atari_acsi_errors[] = {
303     	{ 0x00, "No error (??)" },
304     	{ 0x01, "No index pulses" },
305     	{ 0x02, "Seek not complete" },
306     	{ 0x03, "Write fault" },
307     	{ 0x04, "Drive not ready" },
308     	{ 0x06, "No Track 00 signal" },
309     	{ 0x10, "ECC error in ID field" },
310     	{ 0x11, "Uncorrectable data error" },
311     	{ 0x12, "ID field address mark not found" },
312     	{ 0x13, "Data field address mark not found" },
313     	{ 0x14, "Record not found" },
314     	{ 0x15, "Seek error" },
315     	{ 0x18, "Data check in no retry mode" },
316     	{ 0x19, "ECC error during verify" },
317     	{ 0x1a, "Access to bad block" },
318     	{ 0x1c, "Unformatted or bad format" },
319     	{ 0x20, "Invalid command" },
320     	{ 0x21, "Invalid block address" },
321     	{ 0x23, "Volume overflow" },
322     	{ 0x24, "Invalid argument" },
323     	{ 0x25, "Invalid drive number" },
324     	{ 0x26, "Byte zero parity check" },
325     	{ 0x28, "Cartride changed" },
326     	{ 0x2c, "Error count overflow" },
327     	{ 0x30, "Controller selftest failed" }
328     },
329     
330     	scsi_acsi_errors[] = {
331     	{ 0x00, "No error (??)" },
332     	{ 0x01, "Recovered error" },
333     	{ 0x02, "Drive not ready" },
334     	{ 0x03, "Uncorrectable medium error" },
335     	{ 0x04, "Hardware error" },
336     	{ 0x05, "Illegal request" },
337     	{ 0x06, "Unit attention (Reset or cartridge changed)" },
338     	{ 0x07, "Data protection" },
339     	{ 0x08, "Blank check" },
340     	{ 0x0b, "Aborted Command" },
341     	{ 0x0d, "Volume overflow" }
342     };
343     
344     
345     
346     /***************************** Prototypes *****************************/
347     
348     static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int
349                             rwflag, int enable);
350     static int acsi_reqsense( char *buffer, int targ, int lun);
351     static void acsi_print_error( const unsigned char *errblk, int dev );
352     static void acsi_interrupt (int irq, void *data, struct pt_regs *fp);
353     static void unexpected_acsi_interrupt( void );
354     static void bad_rw_intr( void );
355     static void read_intr( void );
356     static void write_intr( void);
357     static void acsi_times_out( unsigned long dummy );
358     static void copy_to_acsibuffer( void );
359     static void copy_from_acsibuffer( void );
360     static void do_end_requests( void );
361     static void do_acsi_request( request_queue_t * );
362     static void redo_acsi_request( void );
363     static int acsi_ioctl( struct inode *inode, struct file *file, unsigned int
364                            cmd, unsigned long arg );
365     static int acsi_open( struct inode * inode, struct file * filp );
366     static int acsi_release( struct inode * inode, struct file * file );
367     static void acsi_prevent_removal( int target, int flag );
368     static int acsi_change_blk_size( int target, int lun);
369     static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd );
370     static void acsi_geninit(void);
371     static int revalidate_acsidisk( int dev, int maxusage );
372     static int acsi_revalidate (dev_t);
373     
374     /************************* End of Prototypes **************************/
375     
376     
377     struct timer_list acsi_timer = { NULL, NULL, 0, 0, acsi_times_out };
378     
379     
380     #ifdef CONFIG_ATARI_SLM
381     
382     extern int attach_slm( int target, int lun );
383     extern int slm_init( void );
384     
385     #endif
386     
387     
388     
389     /***********************************************************************
390      *
391      *   ACSI primitives
392      *
393      **********************************************************************/
394     
395     
396     /*
397      * The following two functions wait for _IRQ to become Low or High,
398      * resp., with a timeout. The 'timeout' parameter is in jiffies
399      * (10ms).
400      * If the functions are called with timer interrupts on (int level <
401      * 6), the timeout is based on the 'jiffies' variable to provide exact
402      * timeouts for device probing etc.
403      * If interrupts are disabled, the number of tries is based on the
404      * 'loops_per_jiffy' variable. A rough estimation is sufficient here...
405      */
406     
407     #define INT_LEVEL													\
408     	({	unsigned __sr;												\
409     		__asm__ __volatile__ ( "movew	%/sr,%0" : "=dm" (__sr) );	\
410     		(__sr >> 8) & 7;											\
411     	})
412     
413     int acsi_wait_for_IRQ( unsigned timeout )
414     
415     {
416     	if (INT_LEVEL < 6) {
417     		unsigned long maxjif = jiffies + timeout;
418     		while (time_before(jiffies, maxjif))
419     			if (!(mfp.par_dt_reg & 0x20)) return( 1 );
420     	}
421     	else {
422     		long tries = loops_per_jiffy / 8 * timeout;
423     		while( --tries >= 0 )
424     			if (!(mfp.par_dt_reg & 0x20)) return( 1 );
425     	}		
426     	return( 0 ); /* timeout! */
427     }
428     
429     
430     int acsi_wait_for_noIRQ( unsigned timeout )
431     
432     {
433     	if (INT_LEVEL < 6) {
434     		unsigned long maxjif = jiffies + timeout;
435     		while (time_before(jiffies, maxjif))
436     			if (mfp.par_dt_reg & 0x20) return( 1 );
437     	}
438     	else {
439     		long tries = loops_per_jiffy * timeout / 8;
440     		while( tries-- >= 0 )
441     			if (mfp.par_dt_reg & 0x20) return( 1 );
442     	}		
443     	return( 0 ); /* timeout! */
444     }
445     
446     static struct timeval start_time;
447     
448     void
449     acsi_delay_start(void)
450     {
451     	do_gettimeofday(&start_time);
452     }
453     
454     /* wait from acsi_delay_start to now usec (<1E6) usec */
455     
456     void
457     acsi_delay_end(long usec)
458     {
459     	struct timeval end_time;
460     	long deltau,deltas;
461     	do_gettimeofday(&end_time);
462     	deltau=end_time.tv_usec - start_time.tv_usec;
463     	deltas=end_time.tv_sec - start_time.tv_sec;
464     	if (deltas > 1 || deltas < 0)
465     		return;
466     	if (deltas > 0)
467     		deltau += 1000*1000;
468     	if (deltau >= usec)
469     		return;
470     	udelay(usec-deltau);
471     }
472     
473     /* acsicmd_dma() sends an ACSI command and sets up the DMA to transfer
474      * 'blocks' blocks of 512 bytes from/to 'buffer'.
475      * Because the _IRQ signal is used for handshaking the command bytes,
476      * the ACSI interrupt has to be disabled in this function. If the end
477      * of the operation should be signalled by a real interrupt, it has to be
478      * reenabled afterwards.
479      */
480     
481     static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
482     
483     {	unsigned long	flags, paddr;
484     	int				i;
485     
486     #ifdef NO_WRITE
487     	if (rwflag || *cmd == 0x0a) {
488     		printk( "ACSI: Write commands disabled!\n" );
489     		return( 0 );
490     	}
491     #endif
492     	
493     	rwflag = rwflag ? 0x100 : 0;
494     	paddr = virt_to_phys( buffer );
495     
496     	acsi_delay_end(COMMAND_DELAY);
497     	DISABLE_IRQ();
498     
499     	save_flags(flags);  
500     	cli();
501     	/* Low on A1 */
502     	dma_wd.dma_mode_status = 0x88 | rwflag;
503     	MFPDELAY();
504     
505     	/* set DMA address */
506     	dma_wd.dma_lo = (unsigned char)paddr;
507     	paddr >>= 8;
508     	MFPDELAY();
509     	dma_wd.dma_md = (unsigned char)paddr;
510     	paddr >>= 8;
511     	MFPDELAY();
512     	if (ATARIHW_PRESENT(EXTD_DMA))
513     		st_dma_ext_dmahi = (unsigned short)paddr;
514     	else
515     		dma_wd.dma_hi = (unsigned char)paddr;
516     	MFPDELAY();
517     	restore_flags(flags);
518     
519     	/* send the command bytes except the last */
520     	for( i = 0; i < 5; ++i ) {
521     		DMA_LONG_WRITE( *cmd++, 0x8a | rwflag );
522     		udelay(20);
523     		if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
524     	}
525     
526     	/* Clear FIFO and switch DMA to correct direction */  
527     	dma_wd.dma_mode_status = 0x92 | (rwflag ^ 0x100);  
528     	MFPDELAY();
529     	dma_wd.dma_mode_status = 0x92 | rwflag;
530     	MFPDELAY();
531     
532     	/* How many sectors for DMA */
533     	dma_wd.fdc_acces_seccount = blocks;
534     	MFPDELAY();
535     	
536     	/* send last command byte */
537     	dma_wd.dma_mode_status = 0x8a | rwflag;
538     	MFPDELAY();
539     	DMA_LONG_WRITE( *cmd++, 0x0a | rwflag );
540     	if (enable)
541     		ENABLE_IRQ();
542     	udelay(80);
543     
544     	return( 1 );
545     }
546     
547     
548     /*
549      * acsicmd_nodma() sends an ACSI command that requires no DMA.
550      */
551     
552     int acsicmd_nodma( const char *cmd, int enable)
553     
554     {	int	i;
555     
556     	acsi_delay_end(COMMAND_DELAY);
557     	DISABLE_IRQ();
558     
559     	/* send first command byte */
560     	dma_wd.dma_mode_status = 0x88;
561     	MFPDELAY();
562     	DMA_LONG_WRITE( *cmd++, 0x8a );
563     	udelay(20);
564     	if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
565     
566     	/* send the intermediate command bytes */
567     	for( i = 0; i < 4; ++i ) {
568     		DMA_LONG_WRITE( *cmd++, 0x8a );
569     		udelay(20);
570     		if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
571     	}
572     
573     	/* send last command byte */
574     	DMA_LONG_WRITE( *cmd++, 0x0a );
575     	if (enable)
576     		ENABLE_IRQ();
577     	udelay(80);
578     	
579     	return( 1 );
580     	/* Note that the ACSI interrupt is still disabled after this
581     	 * function. If you want to get the IRQ delivered, enable it manually!
582     	 */
583     }
584     
585     
586     static int acsi_reqsense( char *buffer, int targ, int lun)
587     
588     {
589     	CMDSET_TARG_LUN( reqsense_cmd, targ, lun);
590     	if (!acsicmd_dma( reqsense_cmd, buffer, 1, 0, 0 )) return( 0 );
591     	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
592     	acsi_getstatus();
593     	if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
594     	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
595     	acsi_getstatus();
596     	if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
597     	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
598     	acsi_getstatus();
599     	if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
600     	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
601     	acsi_getstatus();
602     	dma_cache_maintenance( virt_to_phys(buffer), 16, 0 );
603     	
604     	return( 1 );
605     }	
606     
607     
608     /*
609      * ACSI status phase: get the status byte from the bus
610      *
611      * I've seen several times that a 0xff status is read, propably due to
612      * a timing error. In this case, the procedure is repeated after the
613      * next _IRQ edge.
614      */
615     
616     int acsi_getstatus( void )
617     
618     {	int	status;
619     
620     	DISABLE_IRQ();
621     	for(;;) {
622     		if (!acsi_wait_for_IRQ( 100 )) {
623     			acsi_delay_start();
624     			return( -1 );
625     		}
626     		dma_wd.dma_mode_status = 0x8a;
627     		MFPDELAY();
628     		status = dma_wd.fdc_acces_seccount;
629     		if (status != 0xff) break;
630     #ifdef DEBUG
631     		printk("ACSI: skipping 0xff status byte\n" );
632     #endif
633     		udelay(40);
634     		acsi_wait_for_noIRQ( 20 );
635     	}
636     	dma_wd.dma_mode_status = 0x80;
637     	udelay(40);
638     	acsi_wait_for_noIRQ( 20 );
639     
640     	acsi_delay_start();
641     	return( status & 0x1f ); /* mask of the device# */
642     }
643     
644     
645     #if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
646     
647     /* Receive data in an extended status phase. Needed by SLM printer. */
648     
649     int acsi_extstatus( char *buffer, int cnt )
650     
651     {	int	status;
652     
653     	DISABLE_IRQ();
654     	udelay(80);
655     	while( cnt-- > 0 ) {
656     		if (!acsi_wait_for_IRQ( 40 )) return( 0 );
657     		dma_wd.dma_mode_status = 0x8a;
658     		MFPDELAY();
659     		status = dma_wd.fdc_acces_seccount;
660     		MFPDELAY();
661     		*buffer++ = status & 0xff;
662     		udelay(40);
663     	}
664     	return( 1 );
665     }
666     
667     
668     /* Finish an extended status phase */
669     
670     void acsi_end_extstatus( void )
671     
672     {
673     	dma_wd.dma_mode_status = 0x80;
674     	udelay(40);
675     	acsi_wait_for_noIRQ( 20 );
676     	acsi_delay_start();
677     }
678     
679     
680     /* Send data in an extended command phase */
681     
682     int acsi_extcmd( unsigned char *buffer, int cnt )
683     
684     {
685     	while( cnt-- > 0 ) {
686     		DMA_LONG_WRITE( *buffer++, 0x8a );
687     		udelay(20);
688     		if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
689     	}
690     	return( 1 );
691     }
692     
693     #endif
694     
695     
696     static void acsi_print_error( const unsigned char *errblk, int dev  )
697     
698     {	int atari_err, i, errcode;
699     	struct acsi_error *arr;
700     
701     	atari_err = acsi_info[dev].old_atari_disk;
702     	if (atari_err)
703     		errcode = errblk[0] & 0x7f;
704     	else
705     		if ((errblk[0] & 0x70) == 0x70)
706     			errcode = errblk[2] & 0x0f;
707     		else
708     			errcode = errblk[0] & 0x0f;
709     	
710     	printk( KERN_ERR "ACSI error 0x%02x", errcode );
711     
712     	if (errblk[0] & 0x80)
713     		printk( " for sector %d",
714     				((errblk[1] & 0x1f) << 16) |
715     				(errblk[2] << 8) | errblk[0] );
716     
717     	arr = atari_err ? atari_acsi_errors : scsi_acsi_errors;
718     	i = atari_err ? sizeof(atari_acsi_errors)/sizeof(*atari_acsi_errors) :
719     		            sizeof(scsi_acsi_errors)/sizeof(*scsi_acsi_errors);
720     	
721     	for( --i; i >= 0; --i )
722     		if (arr[i].code == errcode) break;
723     	if (i >= 0)
724     		printk( ": %s\n", arr[i].text );
725     }
726     
727     /*******************************************************************
728      *
729      * ACSI interrupt routine
730      *   Test, if this is a ACSI interrupt and call the irq handler
731      *   Otherwise ignore this interrupt.
732      *
733      *******************************************************************/
734     
735     static void acsi_interrupt(int irq, void *data, struct pt_regs *fp )
736     
737     {	void (*acsi_irq_handler)(void) = DEVICE_INTR;
738     
739     	DEVICE_INTR = NULL;
740     	CLEAR_TIMER();
741     
742     	if (!acsi_irq_handler)
743     		acsi_irq_handler = unexpected_acsi_interrupt;
744     	acsi_irq_handler();
745     }
746     
747     
748     /******************************************************************
749      *
750      * The Interrupt handlers
751      *
752      *******************************************************************/
753     
754     
755     static void unexpected_acsi_interrupt( void )
756     
757     {
758     	printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
759     }
760     
761     
762     /* This function is called in case of errors. Because we cannot reset
763      * the ACSI bus or a single device, there is no other choice than
764      * retrying several times :-(
765      */
766     
767     static void bad_rw_intr( void )
768     
769     {
770     	if (QUEUE_EMPTY)
771     		return;
772     
773     	if (++CURRENT->errors >= MAX_ERRORS)
774     		end_request(0);
775     	/* Otherwise just retry */
776     }
777     
778     
779     static void read_intr( void )
780     
781     {	int		status;
782     	
783     	status = acsi_getstatus();
784     	if (status != 0) {
785     		int dev = DEVICE_NR(MINOR(CURRENT->rq_dev));
786     		printk( KERN_ERR "ad%c: ", dev+'a' );
787     		if (!acsi_reqsense( acsi_buffer, acsi_info[dev].target, 
788     					acsi_info[dev].lun))
789     			printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
790     		else {
791     			acsi_print_error( acsi_buffer, dev );
792     			if (CARTRCH_STAT( dev, acsi_buffer ))
793     				acsi_info[dev].changed = 1;
794     		}
795     		ENABLE_IRQ();
796     		bad_rw_intr();
797     		redo_acsi_request();
798     		return;
799     	}
800     
801     	dma_cache_maintenance( virt_to_phys(CurrentBuffer), CurrentNSect*512, 0 );
802     	if (CurrentBuffer == acsi_buffer)
803     		copy_from_acsibuffer();
804     
805     	do_end_requests();
806     	redo_acsi_request();
807     }
808     
809     
810     static void write_intr(void)
811     
812     {	int	status;
813     
814     	status = acsi_getstatus();
815     	if (status != 0) {
816     		int	dev = DEVICE_NR(MINOR(CURRENT->rq_dev));
817     		printk( KERN_ERR "ad%c: ", dev+'a' );
818     		if (!acsi_reqsense( acsi_buffer, acsi_info[dev].target,
819     					acsi_info[dev].lun))
820     			printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
821     		else {
822     			acsi_print_error( acsi_buffer, dev );
823     			if (CARTRCH_STAT( dev, acsi_buffer ))
824     				acsi_info[dev].changed = 1;
825     		}
826     		bad_rw_intr();
827     		redo_acsi_request();
828     		return;
829     	}
830     
831     	do_end_requests();
832     	redo_acsi_request();
833     }
834     
835     
836     static void acsi_times_out( unsigned long dummy )
837     
838     {
839     	DISABLE_IRQ();
840     	if (!DEVICE_INTR) return;
841     
842     	DEVICE_INTR = NULL;
843     	printk( KERN_ERR "ACSI timeout\n" );
844     	if (QUEUE_EMPTY) return;
845     	if (++CURRENT->errors >= MAX_ERRORS) {
846     #ifdef DEBUG
847     		printk( KERN_ERR "ACSI: too many errors.\n" );
848     #endif
849     		end_request(0);
850     	}
851     
852     	redo_acsi_request();
853     }
854     
855     
856     
857     /***********************************************************************
858      *
859      *  Scatter-gather utility functions
860      *
861      ***********************************************************************/
862     
863     
864     static void copy_to_acsibuffer( void )
865     
866     {	int					i;
867     	char				*src, *dst;
868     	struct buffer_head	*bh;
869     	
870     	src = CURRENT->buffer;
871     	dst = acsi_buffer;
872     	bh = CURRENT->bh;
873     
874     	if (!bh)
875     		memcpy( dst, src, CurrentNSect*512 );
876     	else
877     		for( i = 0; i < CurrentNReq; ++i ) {
878     			memcpy( dst, src, bh->b_size );
879     			dst += bh->b_size;
880     			if ((bh = bh->b_reqnext))
881     				src = bh->b_data;
882     		}
883     }
884     
885     
886     static void copy_from_acsibuffer( void )
887     
888     {	int					i;
889     	char				*src, *dst;
890     	struct buffer_head	*bh;
891     	
892     	dst = CURRENT->buffer;
893     	src = acsi_buffer;
894     	bh = CURRENT->bh;
895     
896     	if (!bh)
897     		memcpy( dst, src, CurrentNSect*512 );
898     	else
899     		for( i = 0; i < CurrentNReq; ++i ) {
900     			memcpy( dst, src, bh->b_size );
901     			src += bh->b_size;
902     			if ((bh = bh->b_reqnext))
903     				dst = bh->b_data;
904     		}
905     }
906     
907     
908     static void do_end_requests( void )
909     
910     {	int		i, n;
911     
912     	if (!CURRENT->bh) {
913     		CURRENT->nr_sectors -= CurrentNSect;
914     		CURRENT->current_nr_sectors -= CurrentNSect;
915     		CURRENT->sector += CurrentNSect;
916     		if (CURRENT->nr_sectors == 0)
917     			end_request(1);
918     	}
919     	else {
920     		for( i = 0; i < CurrentNReq; ++i ) {
921     			n = CURRENT->bh->b_size >> 9;
922     			CURRENT->nr_sectors -= n;
923     			CURRENT->current_nr_sectors -= n;
924     			CURRENT->sector += n;
925     			end_request(1);
926     		}
927     	}
928     }
929     
930     
931     
932     
933     /***********************************************************************
934      *
935      *  do_acsi_request and friends
936      *
937      ***********************************************************************/
938     
939     static void do_acsi_request( request_queue_t * q )
940     
941     {
942     	stdma_lock( acsi_interrupt, NULL );
943     	redo_acsi_request();
944     }
945     
946     
947     static void redo_acsi_request( void )
948     
949     {	unsigned			block, dev, target, lun, nsect;
950     	char 				*buffer;
951     	unsigned long		pbuffer;
952     	struct buffer_head	*bh;
953     	
954     	if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE) {
955     		if (!DEVICE_INTR) {
956     			ENABLE_IRQ();
957     			stdma_release();
958     		}
959     		return;
960     	}
961     
962     	if (DEVICE_INTR)
963     		return;
964     
965       repeat:
966     	CLEAR_TIMER();
967     	/* Another check here: An interrupt or timer event could have
968     	 * happened since the last check!
969     	 */
970     	if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE) {
971     		if (!DEVICE_INTR) {
972     			ENABLE_IRQ();
973     			stdma_release();
974     		}
975     		return;
976     	}
977     	if (DEVICE_INTR)
978     		return;
979     
980     	if (QUEUE_EMPTY) {
981     		CLEAR_INTR;
982     		ENABLE_IRQ();
983     		stdma_release();
984     		return;
985     	}
986     	
987     	if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
988     		panic(DEVICE_NAME ": request list destroyed");
989     	if (CURRENT->bh) {
990     		if (!CURRENT->bh && !buffer_locked(CURRENT->bh))
991     			panic(DEVICE_NAME ": block not locked");
992     	}
993     
994     	dev = MINOR(CURRENT->rq_dev);
995     	block = CURRENT->sector;
996     	if (DEVICE_NR(dev) >= NDevices ||
997     		block+CURRENT->nr_sectors >= acsi_part[dev].nr_sects) {
998     #ifdef DEBUG
999     		printk( "ad%c: attempted access for blocks %d...%ld past end of device at block %ld.\n",
1000     		       DEVICE_NR(dev)+'a',
1001     		       block, block + CURRENT->nr_sectors - 1,
1002     		       acsi_part[dev].nr_sects);
1003     #endif
1004     		end_request(0);
1005     		goto repeat;
1006     	}
1007     	if (acsi_info[DEVICE_NR(dev)].changed) {
1008     		printk( KERN_NOTICE "ad%c: request denied because cartridge has "
1009     				"been changed.\n", DEVICE_NR(dev)+'a' );
1010     		end_request(0);
1011     		goto repeat;
1012     	}
1013     	
1014     	block += acsi_part[dev].start_sect;
1015     	target = acsi_info[DEVICE_NR(dev)].target;
1016     	lun    = acsi_info[DEVICE_NR(dev)].lun;
1017     
1018     	/* Find out how many sectors should be transferred from/to
1019     	 * consecutive buffers and thus can be done with a single command.
1020     	 */
1021     	buffer      = CURRENT->buffer;
1022     	pbuffer     = virt_to_phys(buffer);
1023     	nsect       = CURRENT->current_nr_sectors;
1024     	CurrentNReq = 1;
1025     
1026     	if ((bh = CURRENT->bh) && bh != CURRENT->bhtail) {
1027     		if (!STRAM_ADDR(pbuffer)) {
1028     			/* If transfer is done via the ACSI buffer anyway, we can
1029     			 * assemble as much bh's as fit in the buffer.
1030     			 */
1031     			while( (bh = bh->b_reqnext) ) {
1032     				if (nsect + (bh->b_size>>9) > ACSI_BUFFER_SECTORS) break;
1033     				nsect += bh->b_size >> 9;
1034     				++CurrentNReq;
1035     				if (bh == CURRENT->bhtail) break;
1036     			}
1037     			buffer = acsi_buffer;
1038     			pbuffer = phys_acsi_buffer;
1039     		}
1040     		else {
1041     			unsigned long pendadr, pnewadr;
1042     			pendadr = pbuffer + nsect*512;
1043     			while( (bh = bh->b_reqnext) ) {
1044     				pnewadr = virt_to_phys(bh->b_data);
1045     				if (!STRAM_ADDR(pnewadr) || pendadr != pnewadr) break;
1046     				nsect += bh->b_size >> 9;
1047     				pendadr = pnewadr + bh->b_size;
1048     				++CurrentNReq;
1049     				if (bh == CURRENT->bhtail) break;
1050     			}
1051     		}
1052     	}
1053     	else {
1054     		if (!STRAM_ADDR(pbuffer)) {
1055     			buffer = acsi_buffer;
1056     			pbuffer = phys_acsi_buffer;
1057     			if (nsect > ACSI_BUFFER_SECTORS)
1058     				nsect = ACSI_BUFFER_SECTORS;
1059     		}
1060     	}
1061     	CurrentBuffer = buffer;
1062     	CurrentNSect  = nsect;
1063     	
1064     	if (CURRENT->cmd == WRITE) {
1065     		CMDSET_TARG_LUN( write_cmd, target, lun );
1066     		CMDSET_BLOCK( write_cmd, block );
1067     		CMDSET_LEN( write_cmd, nsect );
1068     		if (buffer == acsi_buffer)
1069     			copy_to_acsibuffer();
1070     		dma_cache_maintenance( pbuffer, nsect*512, 1 );
1071     		SET_INTR(write_intr);
1072     		if (!acsicmd_dma( write_cmd, buffer, nsect, 1, 1)) {
1073     			CLEAR_INTR;
1074     			printk( KERN_ERR "ACSI (write): Timeout in command block\n" );
1075     			bad_rw_intr();
1076     			goto repeat;
1077     		}
1078     		SET_TIMER();
1079     		return;
1080     	}
1081     	if (CURRENT->cmd == READ) {
1082     		CMDSET_TARG_LUN( read_cmd, target, lun );
1083     		CMDSET_BLOCK( read_cmd, block );
1084     		CMDSET_LEN( read_cmd, nsect );
1085     		SET_INTR(read_intr);
1086     		if (!acsicmd_dma( read_cmd, buffer, nsect, 0, 1)) {
1087     			CLEAR_INTR;
1088     			printk( KERN_ERR "ACSI (read): Timeout in command block\n" );
1089     			bad_rw_intr();
1090     			goto repeat;
1091     		}
1092     		SET_TIMER();
1093     		return;
1094     	}
1095     	panic("unknown ACSI command");
1096     }
1097     
1098     
1099     
1100     /***********************************************************************
1101      *
1102      *  Misc functions: ioctl, open, release, check_change, ...
1103      *
1104      ***********************************************************************/
1105     
1106     
1107     static int acsi_ioctl( struct inode *inode, struct file *file,
1108     					   unsigned int cmd, unsigned long arg )
1109     {	int dev;
1110     
1111     	if (!inode)
1112     		return -EINVAL;
1113     	dev = DEVICE_NR(MINOR(inode->i_rdev));
1114     	if (dev >= NDevices)
1115     		return -EINVAL;
1116     	switch (cmd) {
1117     	  case HDIO_GETGEO:
1118     		/* HDIO_GETGEO is supported more for getting the partition's
1119     		 * start sector... */
1120     	  { struct hd_geometry *geo = (struct hd_geometry *)arg;
1121     	    /* just fake some geometry here, it's nonsense anyway; to make it
1122     		 * easy, use Adaptec's usual 64/32 mapping */
1123     	    put_user( 64, &geo->heads );
1124     	    put_user( 32, &geo->sectors );
1125     	    put_user( acsi_info[dev].size >> 11, &geo->cylinders );
1126     		put_user( acsi_part[MINOR(inode->i_rdev)].start_sect, &geo->start );
1127     		return 0;
1128     	  }
1129     		
1130     	  case SCSI_IOCTL_GET_IDLUN:
1131     		/* SCSI compatible GET_IDLUN call to get target's ID and LUN number */
1132     		put_user( acsi_info[dev].target | (acsi_info[dev].lun << 8),
1133     				  &((Scsi_Idlun *) arg)->dev_id );
1134     		put_user( 0, &((Scsi_Idlun *) arg)->host_unique_id );
1135     		return 0;
1136     		
1137     	  case BLKGETSIZE:   /* Return device size */
1138     		return put_user(acsi_part[MINOR(inode->i_rdev)].nr_sects,
1139     				(long *) arg);
1140     
1141     	  case BLKGETSIZE64:   /* Return device size */
1142     		return put_user((u64)acsi_part[MINOR(inode->i_rdev)].nr_sects << 9,
1143     				(u64 *) arg);
1144     
1145     	  case BLKROSET:
1146     	  case BLKROGET:
1147     	  case BLKFLSBUF:
1148     	  case BLKPG:
1149     		return blk_ioctl(inode->i_rdev, cmd, arg);
1150     
1151     	  case BLKRRPART: /* Re-read partition tables */
1152     	        if (!capable(CAP_SYS_ADMIN)) 
1153     			return -EACCES;
1154     		return revalidate_acsidisk(inode->i_rdev, 1);
1155     
1156     	  default:
1157     		return -EINVAL;
1158     	}
1159     }
1160     
1161     
1162     /*
1163      * Open a device, check for read-only and lock the medium if it is
1164      * removable.
1165      *
1166      * Changes by Martin Rogge, 9th Aug 1995:
1167      * Check whether check_disk_change (and therefore revalidate_acsidisk)
1168      * was successful. They fail when there is no medium in the drive.
1169      *
1170      * The problem of media being changed during an operation can be 
1171      * ignored because of the prevent_removal code.
1172      *
1173      * Added check for the validity of the device number.
1174      *
1175      */
1176     
1177     static int acsi_open( struct inode * inode, struct file * filp )
1178     {
1179     	int  device;
1180     	struct acsi_info_struct *aip;
1181     
1182     	device = DEVICE_NR(MINOR(inode->i_rdev));
1183     	if (device >= NDevices)
1184     		return -ENXIO;
1185     	aip = &acsi_info[device];
1186     	while (busy[device])
1187     		sleep_on(&busy_wait);
1188     
1189     	if (access_count[device] == 0 && aip->removable) {
1190     #if 0
1191     		aip->changed = 1;	/* safety first */
1192     #endif
1193     		check_disk_change( inode->i_rdev );
1194     		if (aip->changed)	/* revalidate was not successful (no medium) */
1195     			return -ENXIO;
1196     		acsi_prevent_removal(device, 1);
1197     	}
1198     	access_count[device]++;
1199     	MOD_INC_USE_COUNT;
1200     
1201     	if (filp && filp->f_mode) {
1202     		check_disk_change( inode->i_rdev );
1203     		if (filp->f_mode & 2) {
1204     			if (aip->read_only) {
1205     				acsi_release( inode, filp );
1206     				return -EROFS;
1207     			}
1208     		}
1209     	}
1210     
1211     	return 0;
1212     }
1213     
1214     /*
1215      * Releasing a block device means we sync() it, so that it can safely
1216      * be forgotten about...
1217      */
1218     
1219     static int acsi_release( struct inode * inode, struct file * file )
1220     {
1221     	int device = DEVICE_NR(MINOR(inode->i_rdev));
1222     	if (--access_count[device] == 0 && acsi_info[device].removable)
1223     		acsi_prevent_removal(device, 0);
1224     	MOD_DEC_USE_COUNT;
1225     	return( 0 );
1226     }
1227     
1228     /*
1229      * Prevent or allow a media change for removable devices.
1230      */
1231     
1232     static void acsi_prevent_removal(int device, int flag)
1233     {
1234     	stdma_lock( NULL, NULL );
1235     	
1236     	CMDSET_TARG_LUN(pa_med_rem_cmd, acsi_info[device].target,
1237     			acsi_info[device].lun);
1238     	CMDSET_LEN( pa_med_rem_cmd, flag );
1239     	
1240     	if (acsicmd_nodma(pa_med_rem_cmd, 0) && acsi_wait_for_IRQ(3*HZ))
1241     		acsi_getstatus();
1242     	/* Do not report errors -- some devices may not know this command. */
1243     
1244     	ENABLE_IRQ();
1245     	stdma_release();
1246     }
1247     
1248     static int acsi_media_change (dev_t dev)
1249     {
1250     	int device = DEVICE_NR(MINOR(dev));
1251     	struct acsi_info_struct *aip;
1252     
1253     	aip = &acsi_info[device];
1254     	if (!aip->removable) 
1255     		return 0;
1256     
1257     	if (aip->changed)
1258     		/* We can be sure that the medium has been changed -- REQUEST
1259     		 * SENSE has reported this earlier.
1260     		 */
1261     		return 1;
1262     
1263     	/* If the flag isn't set, make a test by reading block 0.
1264     	 * If errors happen, it seems to be better to say "changed"...
1265     	 */
1266     	stdma_lock( NULL, NULL );
1267     	CMDSET_TARG_LUN(read_cmd, aip->target, aip->lun);
1268     	CMDSET_BLOCK( read_cmd, 0 );
1269     	CMDSET_LEN( read_cmd, 1 );
1270     	if (acsicmd_dma(read_cmd, acsi_buffer, 1, 0, 0) &&
1271     	    acsi_wait_for_IRQ(3*HZ)) {
1272     		if (acsi_getstatus()) {
1273     			if (acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1274     				if (CARTRCH_STAT(device, acsi_buffer))
1275     					aip->changed = 1;
1276     			}
1277     			else {
1278     				printk( KERN_ERR "ad%c: REQUEST SENSE failed in test for "
1279     				       "medium change; assuming a change\n", device + 'a' );
1280     				aip->changed = 1;
1281     			}
1282     		}
1283     	}
1284     	else {
1285     		printk( KERN_ERR "ad%c: Test for medium changed timed out; "
1286     				"assuming a change\n", device + 'a');
1287     		aip->changed = 1;
1288     	}
1289     	ENABLE_IRQ();
1290     	stdma_release();
1291     
1292     	/* Now, after reading a block, the changed status is surely valid. */
1293     	return aip->changed;
1294     }
1295     
1296     
1297     static int acsi_change_blk_size( int target, int lun)
1298     
1299     {	int i;
1300     
1301     	for (i=0; i<12; i++)
1302     		acsi_buffer[i] = 0;
1303     
1304     	acsi_buffer[3] = 8;
1305     	acsi_buffer[10] = 2;
1306     	CMDSET_TARG_LUN( modeselect_cmd, target, lun);
1307     
1308     	if (!acsicmd_dma( modeselect_cmd, acsi_buffer, 1,1,0) ||
1309     		!acsi_wait_for_IRQ( 3*HZ ) ||
1310     		acsi_getstatus() != 0 ) {
1311     		return(0);
1312     	}
1313     	return(1);
1314     }
1315     
1316     
1317     static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd )
1318     
1319     {
1320     	int page;
1321     
1322     	CMDSET_TARG_LUN( modesense_cmd, target, lun );
1323     	for (page=0; page<4; page++) {
1324     		modesense_cmd[2] = page;
1325     		if (!acsicmd_dma( modesense_cmd, acsi_buffer, 1, 0, 0 ) ||
1326     		    !acsi_wait_for_IRQ( 3*HZ ) ||
1327     		    acsi_getstatus())
1328     			continue;
1329     
1330     		/* read twice to jump over the second 16-byte border! */
1331     		udelay(300);
1332     		if (acsi_wait_for_noIRQ( 20 ) &&
1333     		    acsicmd_nodma( modesense_cmd, 0 ) &&
1334     		    acsi_wait_for_IRQ( 3*HZ ) &&
1335     		    acsi_getstatus() == 0)
1336     			break;
1337     	}
1338     	if (page == 4) {
1339     		return(0);
1340     	}
1341     
1342     	dma_cache_maintenance( phys_acsi_buffer, sizeof(SENSE_DATA), 0 );
1343     	*sd = *(SENSE_DATA *)acsi_buffer;
1344     
1345     	/* Validity check, depending on type of data */
1346     	
1347     	switch( SENSE_TYPE(*sd) ) {
1348     
1349     	  case SENSE_TYPE_ATARI:
1350     		if (CAPACITY(*sd) == 0)
1351     			goto invalid_sense;
1352     		break;
1353     
1354     	  case SENSE_TYPE_SCSI:
1355     		if (sd->scsi.descriptor_size != 8)
1356     			goto invalid_sense;
1357     		break;
1358     
1359     	  case SENSE_TYPE_UNKNOWN:
1360     
1361     		printk( KERN_ERR "ACSI target %d, lun %d: Cannot interpret "
1362     				"sense data\n", target, lun ); 
1363     		
1364     	  invalid_sense:
1365     
1366     #ifdef DEBUG
1367     		{	int i;
1368     		printk( "Mode sense data for ACSI target %d, lun %d seem not valid:",
1369     				target, lun );
1370     		for( i = 0; i < sizeof(SENSE_DATA); ++i )
1371     			printk( "%02x ", (unsigned char)acsi_buffer[i] );
1372     		printk( "\n" );
1373     		}
1374     #endif
1375     		return( 0 );
1376     	}
1377     		
1378     	return( 1 );
1379     }
1380     
1381     
1382     
1383     /*******************************************************************
1384      *
1385      *  Initialization
1386      *
1387      ********************************************************************/
1388     
1389     
1390     extern struct block_device_operations acsi_fops;
1391     
1392     static struct gendisk acsi_gendisk = {
1393     	major:		MAJOR_NR,
1394     	major_name:	"ad",
1395     	minor_shift:	4,
1396     	max_p:		1 << 4,
1397     	part:		acsi_part,
1398     	sizes:		acsi_sizes,
1399     	real_devices:	(void *)acsi_info,
1400     	fops:		&acsi_fops,
1401     };
1402     	
1403     #define MAX_SCSI_DEVICE_CODE 10
1404     
1405     static const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
1406     {
1407      "Direct-Access    ",
1408      "Sequential-Access",
1409      "Printer          ",
1410      "Processor        ",
1411      "WORM             ",
1412      "CD-ROM           ",
1413      "Scanner          ",
1414      "Optical Device   ",
1415      "Medium Changer   ",
1416      "Communications   "
1417     };
1418     
1419     static void print_inquiry(unsigned char *data)
1420     {
1421     	int i;
1422     
1423     	printk(KERN_INFO "  Vendor: ");
1424     	for (i = 8; i < 16; i++)
1425     		{
1426     	        if (data[i] >= 0x20 && i < data[4] + 5)
1427     			printk("%c", data[i]);
1428     		else
1429     			printk(" ");
1430     		}
1431     
1432     	printk("  Model: ");
1433     	for (i = 16; i < 32; i++)
1434     		{
1435     	        if (data[i] >= 0x20 && i < data[4] + 5)
1436     			printk("%c", data[i]);
1437     		else
1438     			printk(" ");
1439     		}
1440     
1441     	printk("  Rev: ");
1442     	for (i = 32; i < 36; i++)
1443     		{
1444     	        if (data[i] >= 0x20 && i < data[4] + 5)
1445     			printk("%c", data[i]);
1446     		else
1447     			printk(" ");
1448     		}
1449     
1450     	printk("\n");
1451     
1452     	i = data[0] & 0x1f;
1453     
1454     	printk(KERN_INFO "  Type:   %s ", (i < MAX_SCSI_DEVICE_CODE
1455     									   ? scsi_device_types[i]
1456     									   : "Unknown          "));
1457     	printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
1458     	if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1459     	  printk(" CCS\n");
1460     	else
1461     	  printk("\n");
1462     }
1463     
1464     
1465     /* 
1466      * Changes by Martin Rogge, 9th Aug 1995: 
1467      * acsi_devinit has been taken out of acsi_geninit, because it needs 
1468      * to be called from revalidate_acsidisk. The result of request sense 
1469      * is now checked for DRIVE NOT READY.
1470      *
1471      * The structure *aip is only valid when acsi_devinit returns 
1472      * DEV_SUPPORTED. 
1473      *
1474      */
1475     	
1476     #define DEV_NONE	0
1477     #define DEV_UNKNOWN	1
1478     #define DEV_SUPPORTED	2
1479     #define DEV_SLM		3
1480     
1481     static int acsi_devinit(struct acsi_info_struct *aip)
1482     {
1483     	int status, got_inquiry;
1484     	SENSE_DATA sense;
1485     	unsigned char reqsense, extsense;
1486     
1487     	/*****************************************************************/
1488     	/* Do a TEST UNIT READY command to test the presence of a device */
1489     	/*****************************************************************/
1490     
1491     	CMDSET_TARG_LUN(tur_cmd, aip->target, aip->lun);
1492     	if (!acsicmd_nodma(tur_cmd, 0)) {
1493     		/* timed out -> no device here */
1494     #ifdef DEBUG_DETECT
1495     		printk("target %d lun %d: timeout\n", aip->target, aip->lun);
1496     #endif
1497     		return DEV_NONE;
1498     	}
1499     		
1500     	/*************************/
1501     	/* Read the ACSI status. */
1502     	/*************************/
1503     
1504     	status = acsi_getstatus();
1505     	if (status) {
1506     		if (status == 0x12) {
1507     			/* The SLM printer should be the only device that
1508     			 * responds with the error code in the status byte. In
1509     			 * correct status bytes, bit 4 is never set.
1510     			 */
1511     			printk( KERN_INFO "Detected SLM printer at id %d lun %d\n",
1512     			       aip->target, aip->lun);
1513     			return DEV_SLM;
1514     		}
1515     		/* ignore CHECK CONDITION, since some devices send a
1516     		   UNIT ATTENTION */
1517     		if ((status & 0x1e) != 0x2) {
1518     #ifdef DEBUG_DETECT
1519     			printk("target %d lun %d: status %d\n",
1520     			       aip->target, aip->lun, status);
1521     #endif
1522     			return DEV_UNKNOWN;
1523     		}
1524     	}
1525     
1526     	/*******************************/
1527     	/* Do a REQUEST SENSE command. */
1528     	/*******************************/
1529     
1530     	if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1531     		printk( KERN_WARNING "acsi_reqsense failed\n");
1532     		acsi_buffer[0] = 0;
1533     		acsi_buffer[2] = UNIT_ATTENTION;
1534     	}
1535     	reqsense = acsi_buffer[0];
1536     	extsense = acsi_buffer[2] & 0xf;
1537     	if (status) {
1538     		if ((reqsense & 0x70) == 0x70) {	/* extended sense */
1539     			if (extsense != UNIT_ATTENTION &&
1540     			    extsense != NOT_READY) {
1541     #ifdef DEBUG_DETECT
1542     				printk("target %d lun %d: extended sense %d\n",
1543     				       aip->target, aip->lun, extsense);
1544     #endif
1545     				return DEV_UNKNOWN;
1546     			}
1547     		}
1548     		else {
1549     			if (reqsense & 0x7f) {
1550     #ifdef DEBUG_DETECT
1551     				printk("target %d lun %d: sense %d\n",
1552     				       aip->target, aip->lun, reqsense);
1553     #endif
1554     				return DEV_UNKNOWN;
1555     			}
1556     		}
1557     	}
1558     	else 
1559     		if (reqsense == 0x4) {	/* SH204 Bug workaround */
1560     #ifdef DEBUG_DETECT
1561     			printk("target %d lun %d status=0 sense=4\n",
1562     			       aip->target, aip->lun);
1563     #endif
1564     			return DEV_UNKNOWN;
1565     		}
1566     
1567     	/***********************************************************/
1568     	/* Do an INQUIRY command to get more infos on this device. */
1569     	/***********************************************************/
1570     
1571     	/* Assume default values */
1572     	aip->removable = 1;
1573     	aip->read_only = 0;
1574     	aip->old_atari_disk = 0;
1575     	aip->changed = (extsense == NOT_READY);	/* medium inserted? */
1576     	aip->size = DEFAULT_SIZE;
1577     	got_inquiry = 0;
1578     	/* Fake inquiry result for old atari disks */
1579     	memcpy(acsi_buffer, "\000\000\001\000    Adaptec 40xx"
1580     	       "                    ", 40);
1581     	CMDSET_TARG_LUN(inquiry_cmd, aip->target, aip->lun);
1582     	if (acsicmd_dma(inquiry_cmd, acsi_buffer, 1, 0, 0) &&
1583     	    acsi_getstatus() == 0) {
1584     		acsicmd_nodma(inquiry_cmd, 0);
1585     		acsi_getstatus();
1586     		dma_cache_maintenance( phys_acsi_buffer, 256, 0 );
1587     		got_inquiry = 1;
1588     		aip->removable = !!(acsi_buffer[1] & 0x80);
1589     	}
1590     	if (aip->type == NONE)	/* only at boot time */
1591     		print_inquiry(acsi_buffer);
1592     	switch(acsi_buffer[0]) {
1593     	  case TYPE_DISK:
1594     		aip->type = HARDDISK;
1595     		break;
1596     	  case TYPE_ROM:
1597     		aip->type = CDROM;
1598     		aip->read_only = 1;
1599     		break;
1600     	  default:
1601     		return DEV_UNKNOWN;
1602     	}
1603     	/****************************/
1604     	/* Do a MODE SENSE command. */
1605     	/****************************/
1606     
1607     	if (!acsi_mode_sense(aip->target, aip->lun, &sense)) {
1608     		printk( KERN_WARNING "No mode sense data.\n" );
1609     		return DEV_UNKNOWN;
1610     	}
1611     	if ((SECTOR_SIZE(sense) != 512) &&
1612     	    ((aip->type != CDROM) ||
1613     	     !acsi_change_blk_size(aip->target, aip->lun) ||
1614     	     !acsi_mode_sense(aip->target, aip->lun, &sense) ||
1615     	     (SECTOR_SIZE(sense) != 512))) {
1616     		printk( KERN_WARNING "Sector size != 512 not supported.\n" );
1617     		return DEV_UNKNOWN;
1618     	}
1619     	/* There are disks out there that claim to have 0 sectors... */
1620     	if (CAPACITY(sense))
1621     		aip->size = CAPACITY(sense);	/* else keep DEFAULT_SIZE */
1622     	if (!got_inquiry && SENSE_TYPE(sense) == SENSE_TYPE_ATARI) {
1623     		/* If INQUIRY failed and the sense data suggest an old
1624     		 * Atari disk (SH20x, Megafile), the disk is not removable
1625     		 */
1626     		aip->removable = 0;
1627     		aip->old_atari_disk = 1;
1628     	}
1629     	
1630     	/******************/
1631     	/* We've done it. */
1632     	/******************/
1633     	
1634     	return DEV_SUPPORTED;
1635     }
1636     
1637     EXPORT_SYMBOL(acsi_delay_start);
1638     EXPORT_SYMBOL(acsi_delay_end);
1639     EXPORT_SYMBOL(acsi_wait_for_IRQ);
1640     EXPORT_SYMBOL(acsi_wait_for_noIRQ);
1641     EXPORT_SYMBOL(acsicmd_nodma);
1642     EXPORT_SYMBOL(acsi_getstatus);
1643     EXPORT_SYMBOL(acsi_buffer);
1644     EXPORT_SYMBOL(phys_acsi_buffer);
1645     
1646     #ifdef CONFIG_ATARI_SLM_MODULE
1647     void acsi_attach_SLMs( int (*attach_func)( int, int ) );
1648     
1649     EXPORT_SYMBOL(acsi_extstatus);
1650     EXPORT_SYMBOL(acsi_end_extstatus);
1651     EXPORT_SYMBOL(acsi_extcmd);
1652     EXPORT_SYMBOL(acsi_attach_SLMs);
1653     
1654     /* to remember IDs of SLM devices, SLM module is loaded later
1655      * (index is target#, contents is lun#, -1 means "no SLM") */
1656     int SLM_devices[8];
1657     #endif
1658     
1659     static struct block_device_operations acsi_fops = {
1660     	open:			acsi_open,
1661     	release:		acsi_release,
1662     	ioctl:			acsi_ioctl,
1663     	check_media_change:	acsi_media_change,
1664     	revalidate:		acsi_revalidate,
1665     };
1666     
1667     static void acsi_geninit(void)
1668     {
1669     	int i, target, lun;
1670     	struct acsi_info_struct *aip;
1671     #ifdef CONFIG_ATARI_SLM
1672     	int n_slm = 0;
1673     #endif
1674     
1675     	printk( KERN_INFO "Probing ACSI devices:\n" );
1676     	NDevices = 0;
1677     #ifdef CONFIG_ATARI_SLM_MODULE
1678     	for( i = 0; i < 8; ++i )
1679     		SLM_devices[i] = -1;
1680     #endif
1681     	stdma_lock(NULL, NULL);
1682     
1683     	for (target = 0; target < 8 && NDevices < MAX_DEV; ++target) {
1684     		lun = 0;
1685     		do {
1686     			aip = &acsi_info[NDevices];
1687     			aip->type = NONE;
1688     			aip->target = target;
1689     			aip->lun = lun;
1690     			i = acsi_devinit(aip);
1691     			switch (i) {
1692     			  case DEV_SUPPORTED:
1693     				printk( KERN_INFO "Detected ");
1694     				switch (aip->type) {
1695     				  case HARDDISK:
1696     					printk("disk");
1697     					break;
1698     				  case CDROM:
1699     					printk("cdrom");
1700     					break;
1701     				  default:
1702     				}
1703     				printk(" ad%c at id %d lun %d ",
1704     				       'a' + NDevices, target, lun);
1705     				if (aip->removable) 
1706     					printk("(removable) ");
1707     				if (aip->read_only) 
1708     					printk("(read-only) ");
1709     				if (aip->size == DEFAULT_SIZE)
1710     					printk(" unkown size, using default ");
1711     				printk("%ld MByte\n",
1712     				       (aip->size*512+1024*1024/2)/(1024*1024));
1713     				NDevices++;
1714     				break;
1715     			  case DEV_SLM:
1716     #ifdef CONFIG_ATARI_SLM
1717     				n_slm += attach_slm( target, lun );
1718     				break;
1719     #endif
1720     #ifdef CONFIG_ATARI_SLM_MODULE
1721     				SLM_devices[target] = lun;
1722     				break;
1723     #endif
1724     				/* neither of the above: fall through to unknown device */
1725     			  case DEV_UNKNOWN:
1726     				printk( KERN_INFO "Detected unsupported device at "
1727     						"id %d lun %d\n", target, lun);
1728     				break;
1729     			}
1730     		}
1731     #ifdef CONFIG_ACSI_MULTI_LUN
1732     		while (i != DEV_NONE && ++lun < MAX_LUN);
1733     #else
1734     		while (0);
1735     #endif
1736     	}
1737     
1738     	/* reenable interrupt */
1739     	ENABLE_IRQ();
1740     	stdma_release();
1741     
1742     #ifndef CONFIG_ATARI_SLM
1743     	printk( KERN_INFO "Found %d ACSI device(s) total.\n", NDevices );
1744     #else
1745     	printk( KERN_INFO "Found %d ACSI device(s) and %d SLM printer(s) total.\n",
1746     			NDevices, n_slm );
1747     #endif
1748     					 
1749     	for( i = 0; i < (MAX_DEV << 4); i++ )
1750     		acsi_blocksizes[i] = 1024;
1751     	blksize_size[MAJOR_NR] = acsi_blocksizes;
1752     	for( i = 0; i < NDevices; ++i )
1753     		register_disk(&acsi_gendisk, MKDEV(MAJOR_NR,i<<4),
1754     				(acsi_info[i].type==HARDDISK)?1<<4:1,
1755     				&acsi_fops,
1756     				acsi_info[i].size);
1757     	acsi_gendisk.nr_real = NDevices;
1758     }
1759     
1760     #ifdef CONFIG_ATARI_SLM_MODULE
1761     /* call attach_slm() for each device that is a printer; needed for init of SLM
1762      * driver as a module, since it's not yet present if acsi.c is inited and thus
1763      * the bus gets scanned. */
1764     void acsi_attach_SLMs( int (*attach_func)( int, int ) )
1765     {
1766     	int i, n = 0;
1767     
1768     	for( i = 0; i < 8; ++i )
1769     		if (SLM_devices[i] >= 0)
1770     			n += (*attach_func)( i, SLM_devices[i] );
1771     	printk( KERN_INFO "Found %d SLM printer(s) total.\n", n );
1772     }
1773     #endif /* CONFIG_ATARI_SLM_MODULE */
1774     
1775     
1776     int acsi_init( void )
1777     
1778     {
1779     	int err = 0;
1780     	if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ACSI))
1781     		return 0;
1782     	if (devfs_register_blkdev( MAJOR_NR, "ad", &acsi_fops )) {
1783     		printk( KERN_ERR "Unable to get major %d for ACSI\n", MAJOR_NR );
1784     		return -EBUSY;
1785     	}
1786     	if (!(acsi_buffer =
1787     		  (char *)atari_stram_alloc(ACSI_BUFFER_SIZE, "acsi"))) {
1788     		printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
1789     		devfs_unregister_blkdev( MAJOR_NR, "ad" );
1790     		return -ENOMEM;
1791     	}
1792     	phys_acsi_buffer = virt_to_phys( acsi_buffer );
1793     	STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
1794     	
1795     	blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1796     	read_ahead[MAJOR_NR] = 8;		/* 8 sector (4kB) read-ahead */
1797     	add_gendisk(&acsi_gendisk);
1798     
1799     #ifdef CONFIG_ATARI_SLM
1800     	err = slm_init();
1801     #endif
1802     	if (!err)
1803     		acsi_geninit();
1804     	return err;
1805     }
1806     
1807     
1808     #ifdef MODULE
1809     
1810     MODULE_LICENSE("GPL");
1811     
1812     int init_module(void)
1813     {
1814     	int err;
1815     
1816     	if ((err = acsi_init()))
1817     		return( err );
1818     	printk( KERN_INFO "ACSI driver loaded as module.\n");
1819     	return( 0 );
1820     }
1821     
1822     void cleanup_module(void)
1823     {
1824     	del_timer( &acsi_timer );
1825     	blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1826     	atari_stram_free( acsi_buffer );
1827     
1828     	if (devfs_unregister_blkdev( MAJOR_NR, "ad" ) != 0)
1829     		printk( KERN_ERR "acsi: cleanup_module failed\n");
1830     
1831     	del_gendisk(&acsi_gendisk);
1832     }
1833     #endif
1834     
1835     #define DEVICE_BUSY busy[device]
1836     #define USAGE access_count[device]
1837     #define GENDISK_STRUCT acsi_gendisk
1838     
1839     /*
1840      * This routine is called to flush all partitions and partition tables
1841      * for a changed scsi disk, and then re-read the new partition table.
1842      * If we are revalidating a disk because of a media change, then we
1843      * enter with usage == 0.  If we are using an ioctl, we automatically have
1844      * usage == 1 (we need an open channel to use an ioctl :-), so this
1845      * is our limit.
1846      *
1847      * Changes by Martin Rogge, 9th Aug 1995: 
1848      * got cd-roms to work by calling acsi_devinit. There are only two problems:
1849      * First, if there is no medium inserted, the status will remain "changed".
1850      * That is no problem at all, but our design of three-valued logic (medium
1851      * changed, medium not changed, no medium inserted).
1852      * Secondly the check could fail completely and the drive could deliver
1853      * nonsensical data, which could mess up the acsi_info[] structure. In
1854      * that case we try to make the entry safe.
1855      *
1856      */
1857     
1858     static int revalidate_acsidisk( int dev, int maxusage )
1859     {
1860     	int device;
1861     	struct gendisk * gdev;
1862     	int max_p, start, i;
1863     	struct acsi_info_struct *aip;
1864     	
1865     	device = DEVICE_NR(MINOR(dev));
1866     	aip = &acsi_info[device];
1867     	gdev = &GENDISK_STRUCT;
1868     
1869     	cli();
1870     	if (DEVICE_BUSY || USAGE > maxusage) {
1871     		sti();
1872     		return -EBUSY;
1873     	};
1874     	DEVICE_BUSY = 1;
1875     	sti();
1876     
1877     	max_p = gdev->max_p;
1878     	start = device << gdev->minor_shift;
1879     
1880     	for( i = max_p - 1; i >= 0 ; i-- ) {
1881     		if (gdev->part[start + i].nr_sects != 0) {
1882     			invalidate_device(MKDEV(MAJOR_NR, start + i), 1);
1883     			gdev->part[start + i].nr_sects = 0;
1884     		}
1885     		gdev->part[start+i].start_sect = 0;
1886     	};
1887     
1888     	stdma_lock( NULL, NULL );
1889     
1890     	if (acsi_devinit(aip) != DEV_SUPPORTED) {
1891     		printk( KERN_ERR "ACSI: revalidate failed for target %d lun %d\n",
1892     		       aip->target, aip->lun);
1893     		aip->size = 0;
1894     		aip->read_only = 1;
1895     		aip->removable = 1;
1896     		aip->changed = 1; /* next acsi_open will try again... */
1897     	}
1898     
1899     	ENABLE_IRQ();
1900     	stdma_release();
1901     	
1902     	grok_partitions(gdev, device, (aip->type==HARDDISK)?1<<4:1, aip->size);
1903     
1904     	DEVICE_BUSY = 0;
1905     	wake_up(&busy_wait);
1906     	return 0;
1907     }
1908     
1909     
1910     static int acsi_revalidate (dev_t dev)
1911     {
1912       return revalidate_acsidisk (dev, 0);
1913     }
1914