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 = {
378     function:	acsi_times_out
379 };
380 
381 
382 #ifdef CONFIG_ATARI_SLM
383 
384 extern int attach_slm( int target, int lun );
385 extern int slm_init( void );
386 
387 #endif
388 
389 
390 
391 /***********************************************************************
392  *
393  *   ACSI primitives
394  *
395  **********************************************************************/
396 
397 
398 /*
399  * The following two functions wait for _IRQ to become Low or High,
400  * resp., with a timeout. The 'timeout' parameter is in jiffies
401  * (10ms).
402  * If the functions are called with timer interrupts on (int level <
403  * 6), the timeout is based on the 'jiffies' variable to provide exact
404  * timeouts for device probing etc.
405  * If interrupts are disabled, the number of tries is based on the
406  * 'loops_per_jiffy' variable. A rough estimation is sufficient here...
407  */
408 
409 #define INT_LEVEL													\
410 	({	unsigned __sr;												\
411 		__asm__ __volatile__ ( "movew	%/sr,%0" : "=dm" (__sr) );	\
412 		(__sr >> 8) & 7;											\
413 	})
414 
acsi_wait_for_IRQ(unsigned timeout)415 int acsi_wait_for_IRQ( unsigned timeout )
416 
417 {
418 	if (INT_LEVEL < 6) {
419 		unsigned long maxjif = jiffies + timeout;
420 		while (time_before(jiffies, maxjif))
421 			if (!(mfp.par_dt_reg & 0x20)) return( 1 );
422 	}
423 	else {
424 		long tries = loops_per_jiffy / 8 * timeout;
425 		while( --tries >= 0 )
426 			if (!(mfp.par_dt_reg & 0x20)) return( 1 );
427 	}
428 	return( 0 ); /* timeout! */
429 }
430 
431 
acsi_wait_for_noIRQ(unsigned timeout)432 int acsi_wait_for_noIRQ( unsigned timeout )
433 
434 {
435 	if (INT_LEVEL < 6) {
436 		unsigned long maxjif = jiffies + timeout;
437 		while (time_before(jiffies, maxjif))
438 			if (mfp.par_dt_reg & 0x20) return( 1 );
439 	}
440 	else {
441 		long tries = loops_per_jiffy * timeout / 8;
442 		while( tries-- >= 0 )
443 			if (mfp.par_dt_reg & 0x20) return( 1 );
444 	}
445 	return( 0 ); /* timeout! */
446 }
447 
448 static struct timeval start_time;
449 
450 void
acsi_delay_start(void)451 acsi_delay_start(void)
452 {
453 	do_gettimeofday(&start_time);
454 }
455 
456 /* wait from acsi_delay_start to now usec (<1E6) usec */
457 
458 void
acsi_delay_end(long usec)459 acsi_delay_end(long usec)
460 {
461 	struct timeval end_time;
462 	long deltau,deltas;
463 	do_gettimeofday(&end_time);
464 	deltau=end_time.tv_usec - start_time.tv_usec;
465 	deltas=end_time.tv_sec - start_time.tv_sec;
466 	if (deltas > 1 || deltas < 0)
467 		return;
468 	if (deltas > 0)
469 		deltau += 1000*1000;
470 	if (deltau >= usec)
471 		return;
472 	udelay(usec-deltau);
473 }
474 
475 /* acsicmd_dma() sends an ACSI command and sets up the DMA to transfer
476  * 'blocks' blocks of 512 bytes from/to 'buffer'.
477  * Because the _IRQ signal is used for handshaking the command bytes,
478  * the ACSI interrupt has to be disabled in this function. If the end
479  * of the operation should be signalled by a real interrupt, it has to be
480  * reenabled afterwards.
481  */
482 
acsicmd_dma(const char * cmd,char * buffer,int blocks,int rwflag,int enable)483 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
484 
485 {	unsigned long	flags, paddr;
486 	int				i;
487 
488 #ifdef NO_WRITE
489 	if (rwflag || *cmd == 0x0a) {
490 		printk( "ACSI: Write commands disabled!\n" );
491 		return( 0 );
492 	}
493 #endif
494 
495 	rwflag = rwflag ? 0x100 : 0;
496 	paddr = virt_to_phys( buffer );
497 
498 	acsi_delay_end(COMMAND_DELAY);
499 	DISABLE_IRQ();
500 
501 	save_flags(flags);
502 	cli();
503 	/* Low on A1 */
504 	dma_wd.dma_mode_status = 0x88 | rwflag;
505 	MFPDELAY();
506 
507 	/* set DMA address */
508 	dma_wd.dma_lo = (unsigned char)paddr;
509 	paddr >>= 8;
510 	MFPDELAY();
511 	dma_wd.dma_md = (unsigned char)paddr;
512 	paddr >>= 8;
513 	MFPDELAY();
514 	if (ATARIHW_PRESENT(EXTD_DMA))
515 		st_dma_ext_dmahi = (unsigned short)paddr;
516 	else
517 		dma_wd.dma_hi = (unsigned char)paddr;
518 	MFPDELAY();
519 	restore_flags(flags);
520 
521 	/* send the command bytes except the last */
522 	for( i = 0; i < 5; ++i ) {
523 		DMA_LONG_WRITE( *cmd++, 0x8a | rwflag );
524 		udelay(20);
525 		if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
526 	}
527 
528 	/* Clear FIFO and switch DMA to correct direction */
529 	dma_wd.dma_mode_status = 0x92 | (rwflag ^ 0x100);
530 	MFPDELAY();
531 	dma_wd.dma_mode_status = 0x92 | rwflag;
532 	MFPDELAY();
533 
534 	/* How many sectors for DMA */
535 	dma_wd.fdc_acces_seccount = blocks;
536 	MFPDELAY();
537 
538 	/* send last command byte */
539 	dma_wd.dma_mode_status = 0x8a | rwflag;
540 	MFPDELAY();
541 	DMA_LONG_WRITE( *cmd++, 0x0a | rwflag );
542 	if (enable)
543 		ENABLE_IRQ();
544 	udelay(80);
545 
546 	return( 1 );
547 }
548 
549 
550 /*
551  * acsicmd_nodma() sends an ACSI command that requires no DMA.
552  */
553 
acsicmd_nodma(const char * cmd,int enable)554 int acsicmd_nodma( const char *cmd, int enable)
555 
556 {	int	i;
557 
558 	acsi_delay_end(COMMAND_DELAY);
559 	DISABLE_IRQ();
560 
561 	/* send first command byte */
562 	dma_wd.dma_mode_status = 0x88;
563 	MFPDELAY();
564 	DMA_LONG_WRITE( *cmd++, 0x8a );
565 	udelay(20);
566 	if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
567 
568 	/* send the intermediate command bytes */
569 	for( i = 0; i < 4; ++i ) {
570 		DMA_LONG_WRITE( *cmd++, 0x8a );
571 		udelay(20);
572 		if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
573 	}
574 
575 	/* send last command byte */
576 	DMA_LONG_WRITE( *cmd++, 0x0a );
577 	if (enable)
578 		ENABLE_IRQ();
579 	udelay(80);
580 
581 	return( 1 );
582 	/* Note that the ACSI interrupt is still disabled after this
583 	 * function. If you want to get the IRQ delivered, enable it manually!
584 	 */
585 }
586 
587 
acsi_reqsense(char * buffer,int targ,int lun)588 static int acsi_reqsense( char *buffer, int targ, int lun)
589 
590 {
591 	CMDSET_TARG_LUN( reqsense_cmd, targ, lun);
592 	if (!acsicmd_dma( reqsense_cmd, buffer, 1, 0, 0 )) return( 0 );
593 	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
594 	acsi_getstatus();
595 	if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
596 	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
597 	acsi_getstatus();
598 	if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
599 	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
600 	acsi_getstatus();
601 	if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
602 	if (!acsi_wait_for_IRQ( 10 )) return( 0 );
603 	acsi_getstatus();
604 	dma_cache_maintenance( virt_to_phys(buffer), 16, 0 );
605 
606 	return( 1 );
607 }
608 
609 
610 /*
611  * ACSI status phase: get the status byte from the bus
612  *
613  * I've seen several times that a 0xff status is read, propably due to
614  * a timing error. In this case, the procedure is repeated after the
615  * next _IRQ edge.
616  */
617 
acsi_getstatus(void)618 int acsi_getstatus( void )
619 
620 {	int	status;
621 
622 	DISABLE_IRQ();
623 	for(;;) {
624 		if (!acsi_wait_for_IRQ( 100 )) {
625 			acsi_delay_start();
626 			return( -1 );
627 		}
628 		dma_wd.dma_mode_status = 0x8a;
629 		MFPDELAY();
630 		status = dma_wd.fdc_acces_seccount;
631 		if (status != 0xff) break;
632 #ifdef DEBUG
633 		printk("ACSI: skipping 0xff status byte\n" );
634 #endif
635 		udelay(40);
636 		acsi_wait_for_noIRQ( 20 );
637 	}
638 	dma_wd.dma_mode_status = 0x80;
639 	udelay(40);
640 	acsi_wait_for_noIRQ( 20 );
641 
642 	acsi_delay_start();
643 	return( status & 0x1f ); /* mask of the device# */
644 }
645 
646 
647 #if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
648 
649 /* Receive data in an extended status phase. Needed by SLM printer. */
650 
acsi_extstatus(char * buffer,int cnt)651 int acsi_extstatus( char *buffer, int cnt )
652 
653 {	int	status;
654 
655 	DISABLE_IRQ();
656 	udelay(80);
657 	while( cnt-- > 0 ) {
658 		if (!acsi_wait_for_IRQ( 40 )) return( 0 );
659 		dma_wd.dma_mode_status = 0x8a;
660 		MFPDELAY();
661 		status = dma_wd.fdc_acces_seccount;
662 		MFPDELAY();
663 		*buffer++ = status & 0xff;
664 		udelay(40);
665 	}
666 	return( 1 );
667 }
668 
669 
670 /* Finish an extended status phase */
671 
acsi_end_extstatus(void)672 void acsi_end_extstatus( void )
673 
674 {
675 	dma_wd.dma_mode_status = 0x80;
676 	udelay(40);
677 	acsi_wait_for_noIRQ( 20 );
678 	acsi_delay_start();
679 }
680 
681 
682 /* Send data in an extended command phase */
683 
acsi_extcmd(unsigned char * buffer,int cnt)684 int acsi_extcmd( unsigned char *buffer, int cnt )
685 
686 {
687 	while( cnt-- > 0 ) {
688 		DMA_LONG_WRITE( *buffer++, 0x8a );
689 		udelay(20);
690 		if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
691 	}
692 	return( 1 );
693 }
694 
695 #endif
696 
697 
acsi_print_error(const unsigned char * errblk,int dev)698 static void acsi_print_error( const unsigned char *errblk, int dev  )
699 
700 {	int atari_err, i, errcode;
701 	struct acsi_error *arr;
702 
703 	atari_err = acsi_info[dev].old_atari_disk;
704 	if (atari_err)
705 		errcode = errblk[0] & 0x7f;
706 	else
707 		if ((errblk[0] & 0x70) == 0x70)
708 			errcode = errblk[2] & 0x0f;
709 		else
710 			errcode = errblk[0] & 0x0f;
711 
712 	printk( KERN_ERR "ACSI error 0x%02x", errcode );
713 
714 	if (errblk[0] & 0x80)
715 		printk( " for sector %d",
716 				((errblk[1] & 0x1f) << 16) |
717 				(errblk[2] << 8) | errblk[0] );
718 
719 	arr = atari_err ? atari_acsi_errors : scsi_acsi_errors;
720 	i = atari_err ? sizeof(atari_acsi_errors)/sizeof(*atari_acsi_errors) :
721 		            sizeof(scsi_acsi_errors)/sizeof(*scsi_acsi_errors);
722 
723 	for( --i; i >= 0; --i )
724 		if (arr[i].code == errcode) break;
725 	if (i >= 0)
726 		printk( ": %s\n", arr[i].text );
727 }
728 
729 /*******************************************************************
730  *
731  * ACSI interrupt routine
732  *   Test, if this is a ACSI interrupt and call the irq handler
733  *   Otherwise ignore this interrupt.
734  *
735  *******************************************************************/
736 
acsi_interrupt(int irq,void * data,struct pt_regs * fp)737 static void acsi_interrupt(int irq, void *data, struct pt_regs *fp )
738 
739 {	void (*acsi_irq_handler)(void) = DEVICE_INTR;
740 
741 	DEVICE_INTR = NULL;
742 	CLEAR_TIMER();
743 
744 	if (!acsi_irq_handler)
745 		acsi_irq_handler = unexpected_acsi_interrupt;
746 	acsi_irq_handler();
747 }
748 
749 
750 /******************************************************************
751  *
752  * The Interrupt handlers
753  *
754  *******************************************************************/
755 
756 
unexpected_acsi_interrupt(void)757 static void unexpected_acsi_interrupt( void )
758 
759 {
760 	printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
761 }
762 
763 
764 /* This function is called in case of errors. Because we cannot reset
765  * the ACSI bus or a single device, there is no other choice than
766  * retrying several times :-(
767  */
768 
bad_rw_intr(void)769 static void bad_rw_intr( void )
770 
771 {
772 	if (QUEUE_EMPTY)
773 		return;
774 
775 	if (++CURRENT->errors >= MAX_ERRORS)
776 		end_request(0);
777 	/* Otherwise just retry */
778 }
779 
780 
read_intr(void)781 static void read_intr( void )
782 
783 {	int		status;
784 
785 	status = acsi_getstatus();
786 	if (status != 0) {
787 		int dev = DEVICE_NR(MINOR(CURRENT->rq_dev));
788 		printk( KERN_ERR "ad%c: ", dev+'a' );
789 		if (!acsi_reqsense( acsi_buffer, acsi_info[dev].target,
790 					acsi_info[dev].lun))
791 			printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
792 		else {
793 			acsi_print_error( acsi_buffer, dev );
794 			if (CARTRCH_STAT( dev, acsi_buffer ))
795 				acsi_info[dev].changed = 1;
796 		}
797 		ENABLE_IRQ();
798 		bad_rw_intr();
799 		redo_acsi_request();
800 		return;
801 	}
802 
803 	dma_cache_maintenance( virt_to_phys(CurrentBuffer), CurrentNSect*512, 0 );
804 	if (CurrentBuffer == acsi_buffer)
805 		copy_from_acsibuffer();
806 
807 	do_end_requests();
808 	redo_acsi_request();
809 }
810 
811 
write_intr(void)812 static void write_intr(void)
813 
814 {	int	status;
815 
816 	status = acsi_getstatus();
817 	if (status != 0) {
818 		int	dev = DEVICE_NR(MINOR(CURRENT->rq_dev));
819 		printk( KERN_ERR "ad%c: ", dev+'a' );
820 		if (!acsi_reqsense( acsi_buffer, acsi_info[dev].target,
821 					acsi_info[dev].lun))
822 			printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
823 		else {
824 			acsi_print_error( acsi_buffer, dev );
825 			if (CARTRCH_STAT( dev, acsi_buffer ))
826 				acsi_info[dev].changed = 1;
827 		}
828 		bad_rw_intr();
829 		redo_acsi_request();
830 		return;
831 	}
832 
833 	do_end_requests();
834 	redo_acsi_request();
835 }
836 
837 
acsi_times_out(unsigned long dummy)838 static void acsi_times_out( unsigned long dummy )
839 
840 {
841 	DISABLE_IRQ();
842 	if (!DEVICE_INTR) return;
843 
844 	DEVICE_INTR = NULL;
845 	printk( KERN_ERR "ACSI timeout\n" );
846 	if (QUEUE_EMPTY) return;
847 	if (++CURRENT->errors >= MAX_ERRORS) {
848 #ifdef DEBUG
849 		printk( KERN_ERR "ACSI: too many errors.\n" );
850 #endif
851 		end_request(0);
852 	}
853 
854 	redo_acsi_request();
855 }
856 
857 
858 
859 /***********************************************************************
860  *
861  *  Scatter-gather utility functions
862  *
863  ***********************************************************************/
864 
865 
copy_to_acsibuffer(void)866 static void copy_to_acsibuffer( void )
867 
868 {	int					i;
869 	char				*src, *dst;
870 	struct buffer_head	*bh;
871 
872 	src = CURRENT->buffer;
873 	dst = acsi_buffer;
874 	bh = CURRENT->bh;
875 
876 	if (!bh)
877 		memcpy( dst, src, CurrentNSect*512 );
878 	else
879 		for( i = 0; i < CurrentNReq; ++i ) {
880 			memcpy( dst, src, bh->b_size );
881 			dst += bh->b_size;
882 			if ((bh = bh->b_reqnext))
883 				src = bh->b_data;
884 		}
885 }
886 
887 
copy_from_acsibuffer(void)888 static void copy_from_acsibuffer( void )
889 
890 {	int					i;
891 	char				*src, *dst;
892 	struct buffer_head	*bh;
893 
894 	dst = CURRENT->buffer;
895 	src = acsi_buffer;
896 	bh = CURRENT->bh;
897 
898 	if (!bh)
899 		memcpy( dst, src, CurrentNSect*512 );
900 	else
901 		for( i = 0; i < CurrentNReq; ++i ) {
902 			memcpy( dst, src, bh->b_size );
903 			src += bh->b_size;
904 			if ((bh = bh->b_reqnext))
905 				dst = bh->b_data;
906 		}
907 }
908 
909 
do_end_requests(void)910 static void do_end_requests( void )
911 
912 {	int		i, n;
913 
914 	if (!CURRENT->bh) {
915 		CURRENT->nr_sectors -= CurrentNSect;
916 		CURRENT->current_nr_sectors -= CurrentNSect;
917 		CURRENT->sector += CurrentNSect;
918 		if (CURRENT->nr_sectors == 0)
919 			end_request(1);
920 	}
921 	else {
922 		for( i = 0; i < CurrentNReq; ++i ) {
923 			n = CURRENT->bh->b_size >> 9;
924 			CURRENT->nr_sectors -= n;
925 			CURRENT->current_nr_sectors -= n;
926 			CURRENT->sector += n;
927 			end_request(1);
928 		}
929 	}
930 }
931 
932 
933 
934 
935 /***********************************************************************
936  *
937  *  do_acsi_request and friends
938  *
939  ***********************************************************************/
940 
do_acsi_request(request_queue_t * q)941 static void do_acsi_request( request_queue_t * q )
942 
943 {
944 	stdma_lock( acsi_interrupt, NULL );
945 	redo_acsi_request();
946 }
947 
948 
redo_acsi_request(void)949 static void redo_acsi_request( void )
950 
951 {	unsigned			block, dev, target, lun, nsect;
952 	char 				*buffer;
953 	unsigned long		pbuffer;
954 	struct buffer_head	*bh;
955 
956 	if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE) {
957 		if (!DEVICE_INTR) {
958 			ENABLE_IRQ();
959 			stdma_release();
960 		}
961 		return;
962 	}
963 
964 	if (DEVICE_INTR)
965 		return;
966 
967   repeat:
968 	CLEAR_TIMER();
969 	/* Another check here: An interrupt or timer event could have
970 	 * happened since the last check!
971 	 */
972 	if (!QUEUE_EMPTY && CURRENT->rq_status == RQ_INACTIVE) {
973 		if (!DEVICE_INTR) {
974 			ENABLE_IRQ();
975 			stdma_release();
976 		}
977 		return;
978 	}
979 	if (DEVICE_INTR)
980 		return;
981 
982 	if (QUEUE_EMPTY) {
983 		CLEAR_INTR;
984 		ENABLE_IRQ();
985 		stdma_release();
986 		return;
987 	}
988 
989 	if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
990 		panic(DEVICE_NAME ": request list destroyed");
991 	if (CURRENT->bh) {
992 		if (!CURRENT->bh && !buffer_locked(CURRENT->bh))
993 			panic(DEVICE_NAME ": block not locked");
994 	}
995 
996 	dev = MINOR(CURRENT->rq_dev);
997 	block = CURRENT->sector;
998 	if (DEVICE_NR(dev) >= NDevices ||
999 		block+CURRENT->nr_sectors >= acsi_part[dev].nr_sects) {
1000 #ifdef DEBUG
1001 		printk( "ad%c: attempted access for blocks %d...%ld past end of device at block %ld.\n",
1002 		       DEVICE_NR(dev)+'a',
1003 		       block, block + CURRENT->nr_sectors - 1,
1004 		       acsi_part[dev].nr_sects);
1005 #endif
1006 		end_request(0);
1007 		goto repeat;
1008 	}
1009 	if (acsi_info[DEVICE_NR(dev)].changed) {
1010 		printk( KERN_NOTICE "ad%c: request denied because cartridge has "
1011 				"been changed.\n", DEVICE_NR(dev)+'a' );
1012 		end_request(0);
1013 		goto repeat;
1014 	}
1015 
1016 	block += acsi_part[dev].start_sect;
1017 	target = acsi_info[DEVICE_NR(dev)].target;
1018 	lun    = acsi_info[DEVICE_NR(dev)].lun;
1019 
1020 	/* Find out how many sectors should be transferred from/to
1021 	 * consecutive buffers and thus can be done with a single command.
1022 	 */
1023 	buffer      = CURRENT->buffer;
1024 	pbuffer     = virt_to_phys(buffer);
1025 	nsect       = CURRENT->current_nr_sectors;
1026 	CurrentNReq = 1;
1027 
1028 	if ((bh = CURRENT->bh) && bh != CURRENT->bhtail) {
1029 		if (!STRAM_ADDR(pbuffer)) {
1030 			/* If transfer is done via the ACSI buffer anyway, we can
1031 			 * assemble as much bh's as fit in the buffer.
1032 			 */
1033 			while( (bh = bh->b_reqnext) ) {
1034 				if (nsect + (bh->b_size>>9) > ACSI_BUFFER_SECTORS) break;
1035 				nsect += bh->b_size >> 9;
1036 				++CurrentNReq;
1037 				if (bh == CURRENT->bhtail) break;
1038 			}
1039 			buffer = acsi_buffer;
1040 			pbuffer = phys_acsi_buffer;
1041 		}
1042 		else {
1043 			unsigned long pendadr, pnewadr;
1044 			pendadr = pbuffer + nsect*512;
1045 			while( (bh = bh->b_reqnext) ) {
1046 				pnewadr = virt_to_phys(bh->b_data);
1047 				if (!STRAM_ADDR(pnewadr) || pendadr != pnewadr) break;
1048 				nsect += bh->b_size >> 9;
1049 				pendadr = pnewadr + bh->b_size;
1050 				++CurrentNReq;
1051 				if (bh == CURRENT->bhtail) break;
1052 			}
1053 		}
1054 	}
1055 	else {
1056 		if (!STRAM_ADDR(pbuffer)) {
1057 			buffer = acsi_buffer;
1058 			pbuffer = phys_acsi_buffer;
1059 			if (nsect > ACSI_BUFFER_SECTORS)
1060 				nsect = ACSI_BUFFER_SECTORS;
1061 		}
1062 	}
1063 	CurrentBuffer = buffer;
1064 	CurrentNSect  = nsect;
1065 
1066 	if (CURRENT->cmd == WRITE) {
1067 		CMDSET_TARG_LUN( write_cmd, target, lun );
1068 		CMDSET_BLOCK( write_cmd, block );
1069 		CMDSET_LEN( write_cmd, nsect );
1070 		if (buffer == acsi_buffer)
1071 			copy_to_acsibuffer();
1072 		dma_cache_maintenance( pbuffer, nsect*512, 1 );
1073 		SET_INTR(write_intr);
1074 		if (!acsicmd_dma( write_cmd, buffer, nsect, 1, 1)) {
1075 			CLEAR_INTR;
1076 			printk( KERN_ERR "ACSI (write): Timeout in command block\n" );
1077 			bad_rw_intr();
1078 			goto repeat;
1079 		}
1080 		SET_TIMER();
1081 		return;
1082 	}
1083 	if (CURRENT->cmd == READ) {
1084 		CMDSET_TARG_LUN( read_cmd, target, lun );
1085 		CMDSET_BLOCK( read_cmd, block );
1086 		CMDSET_LEN( read_cmd, nsect );
1087 		SET_INTR(read_intr);
1088 		if (!acsicmd_dma( read_cmd, buffer, nsect, 0, 1)) {
1089 			CLEAR_INTR;
1090 			printk( KERN_ERR "ACSI (read): Timeout in command block\n" );
1091 			bad_rw_intr();
1092 			goto repeat;
1093 		}
1094 		SET_TIMER();
1095 		return;
1096 	}
1097 	panic("unknown ACSI command");
1098 }
1099 
1100 
1101 
1102 /***********************************************************************
1103  *
1104  *  Misc functions: ioctl, open, release, check_change, ...
1105  *
1106  ***********************************************************************/
1107 
1108 
acsi_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)1109 static int acsi_ioctl( struct inode *inode, struct file *file,
1110 					   unsigned int cmd, unsigned long arg )
1111 {	int dev;
1112 
1113 	if (!inode)
1114 		return -EINVAL;
1115 	dev = DEVICE_NR(MINOR(inode->i_rdev));
1116 	if (dev >= NDevices)
1117 		return -EINVAL;
1118 	switch (cmd) {
1119 	  case HDIO_GETGEO:
1120 		/* HDIO_GETGEO is supported more for getting the partition's
1121 		 * start sector... */
1122 	  { struct hd_geometry *geo = (struct hd_geometry *)arg;
1123 	    /* just fake some geometry here, it's nonsense anyway; to make it
1124 		 * easy, use Adaptec's usual 64/32 mapping */
1125 	    put_user( 64, &geo->heads );
1126 	    put_user( 32, &geo->sectors );
1127 	    put_user( acsi_info[dev].size >> 11, &geo->cylinders );
1128 		put_user( acsi_part[MINOR(inode->i_rdev)].start_sect, &geo->start );
1129 		return 0;
1130 	  }
1131 
1132 	  case SCSI_IOCTL_GET_IDLUN:
1133 		/* SCSI compatible GET_IDLUN call to get target's ID and LUN number */
1134 		put_user( acsi_info[dev].target | (acsi_info[dev].lun << 8),
1135 				  &((Scsi_Idlun *) arg)->dev_id );
1136 		put_user( 0, &((Scsi_Idlun *) arg)->host_unique_id );
1137 		return 0;
1138 
1139 	  case BLKGETSIZE:
1140 	  case BLKGETSIZE64:
1141 	  case BLKROSET:
1142 	  case BLKROGET:
1143 	  case BLKFLSBUF:
1144 	  case BLKPG:
1145 		return blk_ioctl(inode->i_rdev, cmd, arg);
1146 
1147 	  case BLKRRPART: /* Re-read partition tables */
1148 	        if (!capable(CAP_SYS_ADMIN))
1149 			return -EACCES;
1150 		return revalidate_acsidisk(inode->i_rdev, 1);
1151 
1152 	  default:
1153 		return -EINVAL;
1154 	}
1155 }
1156 
1157 
1158 /*
1159  * Open a device, check for read-only and lock the medium if it is
1160  * removable.
1161  *
1162  * Changes by Martin Rogge, 9th Aug 1995:
1163  * Check whether check_disk_change (and therefore revalidate_acsidisk)
1164  * was successful. They fail when there is no medium in the drive.
1165  *
1166  * The problem of media being changed during an operation can be
1167  * ignored because of the prevent_removal code.
1168  *
1169  * Added check for the validity of the device number.
1170  *
1171  */
1172 
acsi_open(struct inode * inode,struct file * filp)1173 static int acsi_open( struct inode * inode, struct file * filp )
1174 {
1175 	int  device;
1176 	struct acsi_info_struct *aip;
1177 
1178 	device = DEVICE_NR(MINOR(inode->i_rdev));
1179 	if (device >= NDevices)
1180 		return -ENXIO;
1181 	aip = &acsi_info[device];
1182 	while (busy[device])
1183 		sleep_on(&busy_wait);
1184 
1185 	if (access_count[device] == 0 && aip->removable) {
1186 #if 0
1187 		aip->changed = 1;	/* safety first */
1188 #endif
1189 		check_disk_change( inode->i_rdev );
1190 		if (aip->changed)	/* revalidate was not successful (no medium) */
1191 			return -ENXIO;
1192 		acsi_prevent_removal(device, 1);
1193 	}
1194 	access_count[device]++;
1195 
1196 	if (filp && filp->f_mode) {
1197 		check_disk_change( inode->i_rdev );
1198 		if (filp->f_mode & 2) {
1199 			if (aip->read_only) {
1200 				acsi_release( inode, filp );
1201 				return -EROFS;
1202 			}
1203 		}
1204 	}
1205 
1206 	return 0;
1207 }
1208 
1209 /*
1210  * Releasing a block device means we sync() it, so that it can safely
1211  * be forgotten about...
1212  */
1213 
acsi_release(struct inode * inode,struct file * file)1214 static int acsi_release( struct inode * inode, struct file * file )
1215 {
1216 	int device = DEVICE_NR(MINOR(inode->i_rdev));
1217 	if (--access_count[device] == 0 && acsi_info[device].removable)
1218 		acsi_prevent_removal(device, 0);
1219 	return( 0 );
1220 }
1221 
1222 /*
1223  * Prevent or allow a media change for removable devices.
1224  */
1225 
acsi_prevent_removal(int device,int flag)1226 static void acsi_prevent_removal(int device, int flag)
1227 {
1228 	stdma_lock( NULL, NULL );
1229 
1230 	CMDSET_TARG_LUN(pa_med_rem_cmd, acsi_info[device].target,
1231 			acsi_info[device].lun);
1232 	CMDSET_LEN( pa_med_rem_cmd, flag );
1233 
1234 	if (acsicmd_nodma(pa_med_rem_cmd, 0) && acsi_wait_for_IRQ(3*HZ))
1235 		acsi_getstatus();
1236 	/* Do not report errors -- some devices may not know this command. */
1237 
1238 	ENABLE_IRQ();
1239 	stdma_release();
1240 }
1241 
acsi_media_change(dev_t dev)1242 static int acsi_media_change (dev_t dev)
1243 {
1244 	int device = DEVICE_NR(MINOR(dev));
1245 	struct acsi_info_struct *aip;
1246 
1247 	aip = &acsi_info[device];
1248 	if (!aip->removable)
1249 		return 0;
1250 
1251 	if (aip->changed)
1252 		/* We can be sure that the medium has been changed -- REQUEST
1253 		 * SENSE has reported this earlier.
1254 		 */
1255 		return 1;
1256 
1257 	/* If the flag isn't set, make a test by reading block 0.
1258 	 * If errors happen, it seems to be better to say "changed"...
1259 	 */
1260 	stdma_lock( NULL, NULL );
1261 	CMDSET_TARG_LUN(read_cmd, aip->target, aip->lun);
1262 	CMDSET_BLOCK( read_cmd, 0 );
1263 	CMDSET_LEN( read_cmd, 1 );
1264 	if (acsicmd_dma(read_cmd, acsi_buffer, 1, 0, 0) &&
1265 	    acsi_wait_for_IRQ(3*HZ)) {
1266 		if (acsi_getstatus()) {
1267 			if (acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1268 				if (CARTRCH_STAT(device, acsi_buffer))
1269 					aip->changed = 1;
1270 			}
1271 			else {
1272 				printk( KERN_ERR "ad%c: REQUEST SENSE failed in test for "
1273 				       "medium change; assuming a change\n", device + 'a' );
1274 				aip->changed = 1;
1275 			}
1276 		}
1277 	}
1278 	else {
1279 		printk( KERN_ERR "ad%c: Test for medium changed timed out; "
1280 				"assuming a change\n", device + 'a');
1281 		aip->changed = 1;
1282 	}
1283 	ENABLE_IRQ();
1284 	stdma_release();
1285 
1286 	/* Now, after reading a block, the changed status is surely valid. */
1287 	return aip->changed;
1288 }
1289 
1290 
acsi_change_blk_size(int target,int lun)1291 static int acsi_change_blk_size( int target, int lun)
1292 
1293 {	int i;
1294 
1295 	for (i=0; i<12; i++)
1296 		acsi_buffer[i] = 0;
1297 
1298 	acsi_buffer[3] = 8;
1299 	acsi_buffer[10] = 2;
1300 	CMDSET_TARG_LUN( modeselect_cmd, target, lun);
1301 
1302 	if (!acsicmd_dma( modeselect_cmd, acsi_buffer, 1,1,0) ||
1303 		!acsi_wait_for_IRQ( 3*HZ ) ||
1304 		acsi_getstatus() != 0 ) {
1305 		return(0);
1306 	}
1307 	return(1);
1308 }
1309 
1310 
acsi_mode_sense(int target,int lun,SENSE_DATA * sd)1311 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd )
1312 
1313 {
1314 	int page;
1315 
1316 	CMDSET_TARG_LUN( modesense_cmd, target, lun );
1317 	for (page=0; page<4; page++) {
1318 		modesense_cmd[2] = page;
1319 		if (!acsicmd_dma( modesense_cmd, acsi_buffer, 1, 0, 0 ) ||
1320 		    !acsi_wait_for_IRQ( 3*HZ ) ||
1321 		    acsi_getstatus())
1322 			continue;
1323 
1324 		/* read twice to jump over the second 16-byte border! */
1325 		udelay(300);
1326 		if (acsi_wait_for_noIRQ( 20 ) &&
1327 		    acsicmd_nodma( modesense_cmd, 0 ) &&
1328 		    acsi_wait_for_IRQ( 3*HZ ) &&
1329 		    acsi_getstatus() == 0)
1330 			break;
1331 	}
1332 	if (page == 4) {
1333 		return(0);
1334 	}
1335 
1336 	dma_cache_maintenance( phys_acsi_buffer, sizeof(SENSE_DATA), 0 );
1337 	*sd = *(SENSE_DATA *)acsi_buffer;
1338 
1339 	/* Validity check, depending on type of data */
1340 
1341 	switch( SENSE_TYPE(*sd) ) {
1342 
1343 	  case SENSE_TYPE_ATARI:
1344 		if (CAPACITY(*sd) == 0)
1345 			goto invalid_sense;
1346 		break;
1347 
1348 	  case SENSE_TYPE_SCSI:
1349 		if (sd->scsi.descriptor_size != 8)
1350 			goto invalid_sense;
1351 		break;
1352 
1353 	  case SENSE_TYPE_UNKNOWN:
1354 
1355 		printk( KERN_ERR "ACSI target %d, lun %d: Cannot interpret "
1356 				"sense data\n", target, lun );
1357 
1358 	  invalid_sense:
1359 
1360 #ifdef DEBUG
1361 		{	int i;
1362 		printk( "Mode sense data for ACSI target %d, lun %d seem not valid:",
1363 				target, lun );
1364 		for( i = 0; i < sizeof(SENSE_DATA); ++i )
1365 			printk( "%02x ", (unsigned char)acsi_buffer[i] );
1366 		printk( "\n" );
1367 		}
1368 #endif
1369 		return( 0 );
1370 	}
1371 
1372 	return( 1 );
1373 }
1374 
1375 
1376 
1377 /*******************************************************************
1378  *
1379  *  Initialization
1380  *
1381  ********************************************************************/
1382 
1383 
1384 extern struct block_device_operations acsi_fops;
1385 
1386 static struct gendisk acsi_gendisk = {
1387 	major:		MAJOR_NR,
1388 	major_name:	"ad",
1389 	minor_shift:	4,
1390 	max_p:		1 << 4,
1391 	part:		acsi_part,
1392 	sizes:		acsi_sizes,
1393 	real_devices:	(void *)acsi_info,
1394 	fops:		&acsi_fops,
1395 };
1396 
1397 #define MAX_SCSI_DEVICE_CODE 10
1398 
1399 static const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
1400 {
1401  "Direct-Access    ",
1402  "Sequential-Access",
1403  "Printer          ",
1404  "Processor        ",
1405  "WORM             ",
1406  "CD-ROM           ",
1407  "Scanner          ",
1408  "Optical Device   ",
1409  "Medium Changer   ",
1410  "Communications   "
1411 };
1412 
print_inquiry(unsigned char * data)1413 static void print_inquiry(unsigned char *data)
1414 {
1415 	int i;
1416 
1417 	printk(KERN_INFO "  Vendor: ");
1418 	for (i = 8; i < 16; i++)
1419 		{
1420 	        if (data[i] >= 0x20 && i < data[4] + 5)
1421 			printk("%c", data[i]);
1422 		else
1423 			printk(" ");
1424 		}
1425 
1426 	printk("  Model: ");
1427 	for (i = 16; i < 32; i++)
1428 		{
1429 	        if (data[i] >= 0x20 && i < data[4] + 5)
1430 			printk("%c", data[i]);
1431 		else
1432 			printk(" ");
1433 		}
1434 
1435 	printk("  Rev: ");
1436 	for (i = 32; i < 36; i++)
1437 		{
1438 	        if (data[i] >= 0x20 && i < data[4] + 5)
1439 			printk("%c", data[i]);
1440 		else
1441 			printk(" ");
1442 		}
1443 
1444 	printk("\n");
1445 
1446 	i = data[0] & 0x1f;
1447 
1448 	printk(KERN_INFO "  Type:   %s ", (i < MAX_SCSI_DEVICE_CODE
1449 									   ? scsi_device_types[i]
1450 									   : "Unknown          "));
1451 	printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
1452 	if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1453 	  printk(" CCS\n");
1454 	else
1455 	  printk("\n");
1456 }
1457 
1458 
1459 /*
1460  * Changes by Martin Rogge, 9th Aug 1995:
1461  * acsi_devinit has been taken out of acsi_geninit, because it needs
1462  * to be called from revalidate_acsidisk. The result of request sense
1463  * is now checked for DRIVE NOT READY.
1464  *
1465  * The structure *aip is only valid when acsi_devinit returns
1466  * DEV_SUPPORTED.
1467  *
1468  */
1469 
1470 #define DEV_NONE	0
1471 #define DEV_UNKNOWN	1
1472 #define DEV_SUPPORTED	2
1473 #define DEV_SLM		3
1474 
acsi_devinit(struct acsi_info_struct * aip)1475 static int acsi_devinit(struct acsi_info_struct *aip)
1476 {
1477 	int status, got_inquiry;
1478 	SENSE_DATA sense;
1479 	unsigned char reqsense, extsense;
1480 
1481 	/*****************************************************************/
1482 	/* Do a TEST UNIT READY command to test the presence of a device */
1483 	/*****************************************************************/
1484 
1485 	CMDSET_TARG_LUN(tur_cmd, aip->target, aip->lun);
1486 	if (!acsicmd_nodma(tur_cmd, 0)) {
1487 		/* timed out -> no device here */
1488 #ifdef DEBUG_DETECT
1489 		printk("target %d lun %d: timeout\n", aip->target, aip->lun);
1490 #endif
1491 		return DEV_NONE;
1492 	}
1493 
1494 	/*************************/
1495 	/* Read the ACSI status. */
1496 	/*************************/
1497 
1498 	status = acsi_getstatus();
1499 	if (status) {
1500 		if (status == 0x12) {
1501 			/* The SLM printer should be the only device that
1502 			 * responds with the error code in the status byte. In
1503 			 * correct status bytes, bit 4 is never set.
1504 			 */
1505 			printk( KERN_INFO "Detected SLM printer at id %d lun %d\n",
1506 			       aip->target, aip->lun);
1507 			return DEV_SLM;
1508 		}
1509 		/* ignore CHECK CONDITION, since some devices send a
1510 		   UNIT ATTENTION */
1511 		if ((status & 0x1e) != 0x2) {
1512 #ifdef DEBUG_DETECT
1513 			printk("target %d lun %d: status %d\n",
1514 			       aip->target, aip->lun, status);
1515 #endif
1516 			return DEV_UNKNOWN;
1517 		}
1518 	}
1519 
1520 	/*******************************/
1521 	/* Do a REQUEST SENSE command. */
1522 	/*******************************/
1523 
1524 	if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1525 		printk( KERN_WARNING "acsi_reqsense failed\n");
1526 		acsi_buffer[0] = 0;
1527 		acsi_buffer[2] = UNIT_ATTENTION;
1528 	}
1529 	reqsense = acsi_buffer[0];
1530 	extsense = acsi_buffer[2] & 0xf;
1531 	if (status) {
1532 		if ((reqsense & 0x70) == 0x70) {	/* extended sense */
1533 			if (extsense != UNIT_ATTENTION &&
1534 			    extsense != NOT_READY) {
1535 #ifdef DEBUG_DETECT
1536 				printk("target %d lun %d: extended sense %d\n",
1537 				       aip->target, aip->lun, extsense);
1538 #endif
1539 				return DEV_UNKNOWN;
1540 			}
1541 		}
1542 		else {
1543 			if (reqsense & 0x7f) {
1544 #ifdef DEBUG_DETECT
1545 				printk("target %d lun %d: sense %d\n",
1546 				       aip->target, aip->lun, reqsense);
1547 #endif
1548 				return DEV_UNKNOWN;
1549 			}
1550 		}
1551 	}
1552 	else
1553 		if (reqsense == 0x4) {	/* SH204 Bug workaround */
1554 #ifdef DEBUG_DETECT
1555 			printk("target %d lun %d status=0 sense=4\n",
1556 			       aip->target, aip->lun);
1557 #endif
1558 			return DEV_UNKNOWN;
1559 		}
1560 
1561 	/***********************************************************/
1562 	/* Do an INQUIRY command to get more infos on this device. */
1563 	/***********************************************************/
1564 
1565 	/* Assume default values */
1566 	aip->removable = 1;
1567 	aip->read_only = 0;
1568 	aip->old_atari_disk = 0;
1569 	aip->changed = (extsense == NOT_READY);	/* medium inserted? */
1570 	aip->size = DEFAULT_SIZE;
1571 	got_inquiry = 0;
1572 	/* Fake inquiry result for old atari disks */
1573 	memcpy(acsi_buffer, "\000\000\001\000    Adaptec 40xx"
1574 	       "                    ", 40);
1575 	CMDSET_TARG_LUN(inquiry_cmd, aip->target, aip->lun);
1576 	if (acsicmd_dma(inquiry_cmd, acsi_buffer, 1, 0, 0) &&
1577 	    acsi_getstatus() == 0) {
1578 		acsicmd_nodma(inquiry_cmd, 0);
1579 		acsi_getstatus();
1580 		dma_cache_maintenance( phys_acsi_buffer, 256, 0 );
1581 		got_inquiry = 1;
1582 		aip->removable = !!(acsi_buffer[1] & 0x80);
1583 	}
1584 	if (aip->type == NONE)	/* only at boot time */
1585 		print_inquiry(acsi_buffer);
1586 	switch(acsi_buffer[0]) {
1587 	  case TYPE_DISK:
1588 		aip->type = HARDDISK;
1589 		break;
1590 	  case TYPE_ROM:
1591 		aip->type = CDROM;
1592 		aip->read_only = 1;
1593 		break;
1594 	  default:
1595 		return DEV_UNKNOWN;
1596 	}
1597 	/****************************/
1598 	/* Do a MODE SENSE command. */
1599 	/****************************/
1600 
1601 	if (!acsi_mode_sense(aip->target, aip->lun, &sense)) {
1602 		printk( KERN_WARNING "No mode sense data.\n" );
1603 		return DEV_UNKNOWN;
1604 	}
1605 	if ((SECTOR_SIZE(sense) != 512) &&
1606 	    ((aip->type != CDROM) ||
1607 	     !acsi_change_blk_size(aip->target, aip->lun) ||
1608 	     !acsi_mode_sense(aip->target, aip->lun, &sense) ||
1609 	     (SECTOR_SIZE(sense) != 512))) {
1610 		printk( KERN_WARNING "Sector size != 512 not supported.\n" );
1611 		return DEV_UNKNOWN;
1612 	}
1613 	/* There are disks out there that claim to have 0 sectors... */
1614 	if (CAPACITY(sense))
1615 		aip->size = CAPACITY(sense);	/* else keep DEFAULT_SIZE */
1616 	if (!got_inquiry && SENSE_TYPE(sense) == SENSE_TYPE_ATARI) {
1617 		/* If INQUIRY failed and the sense data suggest an old
1618 		 * Atari disk (SH20x, Megafile), the disk is not removable
1619 		 */
1620 		aip->removable = 0;
1621 		aip->old_atari_disk = 1;
1622 	}
1623 
1624 	/******************/
1625 	/* We've done it. */
1626 	/******************/
1627 
1628 	return DEV_SUPPORTED;
1629 }
1630 
1631 EXPORT_SYMBOL(acsi_delay_start);
1632 EXPORT_SYMBOL(acsi_delay_end);
1633 EXPORT_SYMBOL(acsi_wait_for_IRQ);
1634 EXPORT_SYMBOL(acsi_wait_for_noIRQ);
1635 EXPORT_SYMBOL(acsicmd_nodma);
1636 EXPORT_SYMBOL(acsi_getstatus);
1637 EXPORT_SYMBOL(acsi_buffer);
1638 EXPORT_SYMBOL(phys_acsi_buffer);
1639 
1640 #ifdef CONFIG_ATARI_SLM_MODULE
1641 void acsi_attach_SLMs( int (*attach_func)( int, int ) );
1642 
1643 EXPORT_SYMBOL(acsi_extstatus);
1644 EXPORT_SYMBOL(acsi_end_extstatus);
1645 EXPORT_SYMBOL(acsi_extcmd);
1646 EXPORT_SYMBOL(acsi_attach_SLMs);
1647 
1648 /* to remember IDs of SLM devices, SLM module is loaded later
1649  * (index is target#, contents is lun#, -1 means "no SLM") */
1650 int SLM_devices[8];
1651 #endif
1652 
1653 static struct block_device_operations acsi_fops = {
1654 	owner:			THIS_MODULE,
1655 	open:			acsi_open,
1656 	release:		acsi_release,
1657 	ioctl:			acsi_ioctl,
1658 	check_media_change:	acsi_media_change,
1659 	revalidate:		acsi_revalidate,
1660 };
1661 
acsi_geninit(void)1662 static void acsi_geninit(void)
1663 {
1664 	int i, target, lun;
1665 	struct acsi_info_struct *aip;
1666 #ifdef CONFIG_ATARI_SLM
1667 	int n_slm = 0;
1668 #endif
1669 
1670 	printk( KERN_INFO "Probing ACSI devices:\n" );
1671 	NDevices = 0;
1672 #ifdef CONFIG_ATARI_SLM_MODULE
1673 	for( i = 0; i < 8; ++i )
1674 		SLM_devices[i] = -1;
1675 #endif
1676 	stdma_lock(NULL, NULL);
1677 
1678 	for (target = 0; target < 8 && NDevices < MAX_DEV; ++target) {
1679 		lun = 0;
1680 		do {
1681 			aip = &acsi_info[NDevices];
1682 			aip->type = NONE;
1683 			aip->target = target;
1684 			aip->lun = lun;
1685 			i = acsi_devinit(aip);
1686 			switch (i) {
1687 			  case DEV_SUPPORTED:
1688 				printk( KERN_INFO "Detected ");
1689 				switch (aip->type) {
1690 				  case HARDDISK:
1691 					printk("disk");
1692 					break;
1693 				  case CDROM:
1694 					printk("cdrom");
1695 					break;
1696 				  default:
1697 				}
1698 				printk(" ad%c at id %d lun %d ",
1699 				       'a' + NDevices, target, lun);
1700 				if (aip->removable)
1701 					printk("(removable) ");
1702 				if (aip->read_only)
1703 					printk("(read-only) ");
1704 				if (aip->size == DEFAULT_SIZE)
1705 					printk(" unkown size, using default ");
1706 				printk("%ld MByte\n",
1707 				       (aip->size*512+1024*1024/2)/(1024*1024));
1708 				NDevices++;
1709 				break;
1710 			  case DEV_SLM:
1711 #ifdef CONFIG_ATARI_SLM
1712 				n_slm += attach_slm( target, lun );
1713 				break;
1714 #endif
1715 #ifdef CONFIG_ATARI_SLM_MODULE
1716 				SLM_devices[target] = lun;
1717 				break;
1718 #endif
1719 				/* neither of the above: fall through to unknown device */
1720 			  case DEV_UNKNOWN:
1721 				printk( KERN_INFO "Detected unsupported device at "
1722 						"id %d lun %d\n", target, lun);
1723 				break;
1724 			}
1725 		}
1726 #ifdef CONFIG_ACSI_MULTI_LUN
1727 		while (i != DEV_NONE && ++lun < MAX_LUN);
1728 #else
1729 		while (0);
1730 #endif
1731 	}
1732 
1733 	/* reenable interrupt */
1734 	ENABLE_IRQ();
1735 	stdma_release();
1736 
1737 #ifndef CONFIG_ATARI_SLM
1738 	printk( KERN_INFO "Found %d ACSI device(s) total.\n", NDevices );
1739 #else
1740 	printk( KERN_INFO "Found %d ACSI device(s) and %d SLM printer(s) total.\n",
1741 			NDevices, n_slm );
1742 #endif
1743 
1744 	for( i = 0; i < (MAX_DEV << 4); i++ )
1745 		acsi_blocksizes[i] = 1024;
1746 	blksize_size[MAJOR_NR] = acsi_blocksizes;
1747 	for( i = 0; i < NDevices; ++i )
1748 		register_disk(&acsi_gendisk, MKDEV(MAJOR_NR,i<<4),
1749 				(acsi_info[i].type==HARDDISK)?1<<4:1,
1750 				&acsi_fops,
1751 				acsi_info[i].size);
1752 	acsi_gendisk.nr_real = NDevices;
1753 }
1754 
1755 #ifdef CONFIG_ATARI_SLM_MODULE
1756 /* call attach_slm() for each device that is a printer; needed for init of SLM
1757  * driver as a module, since it's not yet present if acsi.c is inited and thus
1758  * the bus gets scanned. */
acsi_attach_SLMs(int (* attach_func)(int,int))1759 void acsi_attach_SLMs( int (*attach_func)( int, int ) )
1760 {
1761 	int i, n = 0;
1762 
1763 	for( i = 0; i < 8; ++i )
1764 		if (SLM_devices[i] >= 0)
1765 			n += (*attach_func)( i, SLM_devices[i] );
1766 	printk( KERN_INFO "Found %d SLM printer(s) total.\n", n );
1767 }
1768 #endif /* CONFIG_ATARI_SLM_MODULE */
1769 
1770 
acsi_init(void)1771 int acsi_init( void )
1772 
1773 {
1774 	int err = 0;
1775 	if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ACSI))
1776 		return 0;
1777 	if (devfs_register_blkdev( MAJOR_NR, "ad", &acsi_fops )) {
1778 		printk( KERN_ERR "Unable to get major %d for ACSI\n", MAJOR_NR );
1779 		return -EBUSY;
1780 	}
1781 	if (!(acsi_buffer =
1782 		  (char *)atari_stram_alloc(ACSI_BUFFER_SIZE, "acsi"))) {
1783 		printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
1784 		devfs_unregister_blkdev( MAJOR_NR, "ad" );
1785 		return -ENOMEM;
1786 	}
1787 	phys_acsi_buffer = virt_to_phys( acsi_buffer );
1788 	STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
1789 
1790 	blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
1791 	read_ahead[MAJOR_NR] = 8;		/* 8 sector (4kB) read-ahead */
1792 	add_gendisk(&acsi_gendisk);
1793 
1794 #ifdef CONFIG_ATARI_SLM
1795 	err = slm_init();
1796 #endif
1797 	if (!err)
1798 		acsi_geninit();
1799 	return err;
1800 }
1801 
1802 
1803 #ifdef MODULE
1804 
1805 MODULE_LICENSE("GPL");
1806 
init_module(void)1807 int init_module(void)
1808 {
1809 	int err;
1810 
1811 	if ((err = acsi_init()))
1812 		return( err );
1813 	printk( KERN_INFO "ACSI driver loaded as module.\n");
1814 	return( 0 );
1815 }
1816 
cleanup_module(void)1817 void cleanup_module(void)
1818 {
1819 	del_timer( &acsi_timer );
1820 	blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
1821 	atari_stram_free( acsi_buffer );
1822 
1823 	if (devfs_unregister_blkdev( MAJOR_NR, "ad" ) != 0)
1824 		printk( KERN_ERR "acsi: cleanup_module failed\n");
1825 
1826 	del_gendisk(&acsi_gendisk);
1827 }
1828 #endif
1829 
1830 #define DEVICE_BUSY busy[device]
1831 #define USAGE access_count[device]
1832 #define GENDISK_STRUCT acsi_gendisk
1833 
1834 /*
1835  * This routine is called to flush all partitions and partition tables
1836  * for a changed scsi disk, and then re-read the new partition table.
1837  * If we are revalidating a disk because of a media change, then we
1838  * enter with usage == 0.  If we are using an ioctl, we automatically have
1839  * usage == 1 (we need an open channel to use an ioctl :-), so this
1840  * is our limit.
1841  *
1842  * Changes by Martin Rogge, 9th Aug 1995:
1843  * got cd-roms to work by calling acsi_devinit. There are only two problems:
1844  * First, if there is no medium inserted, the status will remain "changed".
1845  * That is no problem at all, but our design of three-valued logic (medium
1846  * changed, medium not changed, no medium inserted).
1847  * Secondly the check could fail completely and the drive could deliver
1848  * nonsensical data, which could mess up the acsi_info[] structure. In
1849  * that case we try to make the entry safe.
1850  *
1851  */
1852 
revalidate_acsidisk(int dev,int maxusage)1853 static int revalidate_acsidisk( int dev, int maxusage )
1854 {
1855 	int device;
1856 	struct gendisk * gdev;
1857 	int max_p, start, i;
1858 	struct acsi_info_struct *aip;
1859 
1860 	device = DEVICE_NR(MINOR(dev));
1861 	aip = &acsi_info[device];
1862 	gdev = &GENDISK_STRUCT;
1863 
1864 	cli();
1865 	if (DEVICE_BUSY || USAGE > maxusage) {
1866 		sti();
1867 		return -EBUSY;
1868 	};
1869 	DEVICE_BUSY = 1;
1870 	sti();
1871 
1872 	max_p = gdev->max_p;
1873 	start = device << gdev->minor_shift;
1874 
1875 	for( i = max_p - 1; i >= 0 ; i-- ) {
1876 		if (gdev->part[start + i].nr_sects != 0) {
1877 			invalidate_device(MKDEV(MAJOR_NR, start + i), 1);
1878 			gdev->part[start + i].nr_sects = 0;
1879 		}
1880 		gdev->part[start+i].start_sect = 0;
1881 	};
1882 
1883 	stdma_lock( NULL, NULL );
1884 
1885 	if (acsi_devinit(aip) != DEV_SUPPORTED) {
1886 		printk( KERN_ERR "ACSI: revalidate failed for target %d lun %d\n",
1887 		       aip->target, aip->lun);
1888 		aip->size = 0;
1889 		aip->read_only = 1;
1890 		aip->removable = 1;
1891 		aip->changed = 1; /* next acsi_open will try again... */
1892 	}
1893 
1894 	ENABLE_IRQ();
1895 	stdma_release();
1896 
1897 	grok_partitions(gdev, device, (aip->type==HARDDISK)?1<<4:1, aip->size);
1898 
1899 	DEVICE_BUSY = 0;
1900 	wake_up(&busy_wait);
1901 	return 0;
1902 }
1903 
1904 
acsi_revalidate(dev_t dev)1905 static int acsi_revalidate (dev_t dev)
1906 {
1907   return revalidate_acsidisk (dev, 0);
1908 }
1909