1/*
2 * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
3 *
4 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5 *
6 *   Loosely based on the work of Robert De Vries' team and added:
7 *    - working real DMA
8 *    - Falcon support (untested yet!)   ++bjoern fixed and now it works
9 *    - lots of extensions and bug fixes.
10 *
11 * This file is subject to the terms and conditions of the GNU General Public
12 * License.  See the file COPYING in the main directory of this archive
13 * for more details.
14 *
15 */
16
17
18/**************************************************************************/
19/*                                                                        */
20/* Notes for Falcon SCSI:                                                 */
21/* ----------------------                                                 */
22/*                                                                        */
23/* Since the Falcon SCSI uses the ST-DMA chip, that is shared among       */
24/* several device drivers, locking and unlocking the access to this       */
25/* chip is required. But locking is not possible from an interrupt,       */
26/* since it puts the process to sleep if the lock is not available.       */
27/* This prevents "late" locking of the DMA chip, i.e. locking it just     */
28/* before using it, since in case of disconnection-reconnection           */
29/* commands, the DMA is started from the reselection interrupt.           */
30/*                                                                        */
31/* Two possible schemes for ST-DMA-locking would be:                      */
32/*  1) The lock is taken for each command separately and disconnecting    */
33/*     is forbidden (i.e. can_queue = 1).                                 */
34/*  2) The DMA chip is locked when the first command comes in and         */
35/*     released when the last command is finished and all queues are      */
36/*     empty.                                                             */
37/* The first alternative would result in bad performance, since the       */
38/* interleaving of commands would not be used. The second is unfair to    */
39/* other drivers using the ST-DMA, because the queues will seldom be      */
40/* totally empty if there is a lot of disk traffic.                       */
41/*                                                                        */
42/* For this reasons I decided to employ a more elaborate scheme:          */
43/*  - First, we give up the lock every time we can (for fairness), this    */
44/*    means every time a command finishes and there are no other commands */
45/*    on the disconnected queue.                                          */
46/*  - If there are others waiting to lock the DMA chip, we stop           */
47/*    issuing commands, i.e. moving them onto the issue queue.           */
48/*    Because of that, the disconnected queue will run empty in a         */
49/*    while. Instead we go to sleep on a 'fairness_queue'.                */
50/*  - If the lock is released, all processes waiting on the fairness      */
51/*    queue will be woken. The first of them tries to re-lock the DMA,     */
52/*    the others wait for the first to finish this task. After that,      */
53/*    they can all run on and do their commands...                        */
54/* This sounds complicated (and it is it :-(), but it seems to be a       */
55/* good compromise between fairness and performance: As long as no one     */
56/* else wants to work with the ST-DMA chip, SCSI can go along as          */
57/* usual. If now someone else comes, this behaviour is changed to a       */
58/* "fairness mode": just already initiated commands are finished and      */
59/* then the lock is released. The other one waiting will probably win     */
60/* the race for locking the DMA, since it was waiting for longer. And     */
61/* after it has finished, SCSI can go ahead again. Finally: I hope I      */
62/* have not produced any deadlock possibilities!                          */
63/*                                                                        */
64/**************************************************************************/
65
66
67
68#include <linux/config.h>
69#include <linux/module.h>
70
71#define NDEBUG (0)
72
73#define NDEBUG_ABORT	0x800000
74#define NDEBUG_TAGS	0x1000000
75#define NDEBUG_MERGING	0x2000000
76
77#define AUTOSENSE
78/* For the Atari version, use only polled IO or REAL_DMA */
79#define	REAL_DMA
80/* Support tagged queuing? (on devices that are able to... :-) */
81#define	SUPPORT_TAGS
82#define	MAX_TAGS 32
83
84#include <linux/types.h>
85#include <linux/stddef.h>
86#include <linux/ctype.h>
87#include <linux/delay.h>
88#include <linux/mm.h>
89#include <linux/blk.h>
90#include <linux/sched.h>
91#include <linux/interrupt.h>
92#include <linux/init.h>
93#include <linux/nvram.h>
94
95#include <asm/setup.h>
96#include <asm/atarihw.h>
97#include <asm/atariints.h>
98#include <asm/page.h>
99#include <asm/pgtable.h>
100#include <asm/irq.h>
101#include <asm/traps.h>
102#include <asm/bitops.h>
103
104#include "scsi.h"
105#include "hosts.h"
106#include "atari_scsi.h"
107#include "NCR5380.h"
108#include "constants.h"
109#include <asm/atari_stdma.h>
110#include <asm/atari_stram.h>
111#include <asm/io.h>
112
113#include <linux/stat.h>
114
115#define	IS_A_TT()	ATARIHW_PRESENT(TT_SCSI)
116
117#define	SCSI_DMA_WRITE_P(elt,val)				\
118	do {							\
119		unsigned long v = val;				\
120		tt_scsi_dma.elt##_lo = v & 0xff;		\
121		v >>= 8;					\
122		tt_scsi_dma.elt##_lmd = v & 0xff;		\
123		v >>= 8;					\
124		tt_scsi_dma.elt##_hmd = v & 0xff;		\
125		v >>= 8;					\
126		tt_scsi_dma.elt##_hi = v & 0xff;		\
127	} while(0)
128
129#define	SCSI_DMA_READ_P(elt)					\
130	(((((((unsigned long)tt_scsi_dma.elt##_hi << 8) |	\
131	     (unsigned long)tt_scsi_dma.elt##_hmd) << 8) |	\
132	   (unsigned long)tt_scsi_dma.elt##_lmd) << 8) |	\
133	 (unsigned long)tt_scsi_dma.elt##_lo)
134
135
136static inline void SCSI_DMA_SETADR(unsigned long adr)
137{
138	st_dma.dma_lo = (unsigned char)adr;
139	MFPDELAY();
140	adr >>= 8;
141	st_dma.dma_md = (unsigned char)adr;
142	MFPDELAY();
143	adr >>= 8;
144	st_dma.dma_hi = (unsigned char)adr;
145	MFPDELAY();
146}
147
148static inline unsigned long SCSI_DMA_GETADR(void)
149{
150	unsigned long adr;
151	adr = st_dma.dma_lo;
152	MFPDELAY();
153	adr |= (st_dma.dma_md & 0xff) << 8;
154	MFPDELAY();
155	adr |= (st_dma.dma_hi & 0xff) << 16;
156	MFPDELAY();
157	return adr;
158}
159
160static inline void ENABLE_IRQ(void)
161{
162	if (IS_A_TT())
163		atari_enable_irq(IRQ_TT_MFP_SCSI);
164	else
165		atari_enable_irq(IRQ_MFP_FSCSI);
166}
167
168static inline void DISABLE_IRQ(void)
169{
170	if (IS_A_TT())
171		atari_disable_irq(IRQ_TT_MFP_SCSI);
172	else
173		atari_disable_irq(IRQ_MFP_FSCSI);
174}
175
176
177#define HOSTDATA_DMALEN		(((struct NCR5380_hostdata *) \
178				(atari_scsi_host->hostdata))->dma_len)
179
180/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
181 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
182 * need ten times the standard value... */
183#ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
184#define	AFTER_RESET_DELAY	(HZ/2)
185#else
186#define	AFTER_RESET_DELAY	(5*HZ/2)
187#endif
188
189/***************************** Prototypes *****************************/
190
191#ifdef REAL_DMA
192static int scsi_dma_is_ignored_buserr( unsigned char dma_stat );
193static void atari_scsi_fetch_restbytes( void );
194static long atari_scsi_dma_residual( struct Scsi_Host *instance );
195static int falcon_classify_cmd( Scsi_Cmnd *cmd );
196static unsigned long atari_dma_xfer_len( unsigned long wanted_len,
197                                         Scsi_Cmnd *cmd, int write_flag );
198#endif
199static void scsi_tt_intr( int irq, void *dummy, struct pt_regs *fp);
200static void scsi_falcon_intr( int irq, void *dummy, struct pt_regs *fp);
201static void falcon_release_lock_if_possible( struct NCR5380_hostdata *
202                                             hostdata );
203static void falcon_get_lock( void );
204#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
205static void atari_scsi_reset_boot( void );
206#endif
207static unsigned char atari_scsi_tt_reg_read( unsigned char reg );
208static void atari_scsi_tt_reg_write( unsigned char reg, unsigned char value);
209static unsigned char atari_scsi_falcon_reg_read( unsigned char reg );
210static void atari_scsi_falcon_reg_write( unsigned char reg, unsigned char value );
211
212/************************* End of Prototypes **************************/
213
214
215static struct Scsi_Host *atari_scsi_host = NULL;
216static unsigned char (*atari_scsi_reg_read)( unsigned char reg );
217static void (*atari_scsi_reg_write)( unsigned char reg, unsigned char value );
218
219#ifdef REAL_DMA
220static unsigned long	atari_dma_residual, atari_dma_startaddr;
221static short		atari_dma_active;
222/* pointer to the dribble buffer */
223static char		*atari_dma_buffer = NULL;
224/* precalculated physical address of the dribble buffer */
225static unsigned long	atari_dma_phys_buffer;
226/* != 0 tells the Falcon int handler to copy data from the dribble buffer */
227static char		*atari_dma_orig_addr;
228/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
229 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
230 * cases where requests to physical contiguous buffers have been merged, this
231 * request is <= 4k (one page). So I don't think we have to split transfers
232 * just due to this buffer size...
233 */
234#define	STRAM_BUFFER_SIZE	(4096)
235/* mask for address bits that can't be used with the ST-DMA */
236static unsigned long	atari_dma_stram_mask;
237#define STRAM_ADDR(a)	(((a) & atari_dma_stram_mask) == 0)
238/* number of bytes to cut from a transfer to handle NCR overruns */
239static int atari_read_overruns = 0;
240#endif
241
242static int setup_can_queue = -1;
243MODULE_PARM(setup_can_queue, "i");
244static int setup_cmd_per_lun = -1;
245MODULE_PARM(setup_cmd_per_lun, "i");
246static int setup_sg_tablesize = -1;
247MODULE_PARM(setup_sg_tablesize, "i");
248#ifdef SUPPORT_TAGS
249static int setup_use_tagged_queuing = -1;
250MODULE_PARM(setup_use_tagged_queuing, "i");
251#endif
252static int setup_hostid = -1;
253MODULE_PARM(setup_hostid, "i");
254
255
256#if defined(CONFIG_TT_DMA_EMUL)
257#include "atari_dma_emul.c"
258#endif
259
260#if defined(REAL_DMA)
261
262static int scsi_dma_is_ignored_buserr( unsigned char dma_stat )
263{
264	int i;
265	unsigned long	addr = SCSI_DMA_READ_P( dma_addr ), end_addr;
266
267	if (dma_stat & 0x01) {
268
269		/* A bus error happens when DMA-ing from the last page of a
270		 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
271		 * Check for this case:
272		 */
273
274		for( i = 0; i < m68k_num_memory; ++i ) {
275			end_addr = m68k_memory[i].addr +
276				m68k_memory[i].size;
277			if (end_addr <= addr && addr <= end_addr + 4)
278				return( 1 );
279		}
280	}
281	return( 0 );
282}
283
284
285
286#endif
287
288
289static void scsi_tt_intr (int irq, void *dummy, struct pt_regs *fp)
290{
291#ifdef REAL_DMA
292	int dma_stat;
293
294	dma_stat = tt_scsi_dma.dma_ctrl;
295
296	INT_PRINTK("scsi%d: NCR5380 interrupt, DMA status = %02x\n",
297		   atari_scsi_host->host_no, dma_stat & 0xff);
298
299	/* Look if it was the DMA that has interrupted: First possibility
300	 * is that a bus error occurred...
301	 */
302	if (dma_stat & 0x80) {
303		if (!scsi_dma_is_ignored_buserr( dma_stat )) {
304			printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
305			       SCSI_DMA_READ_P(dma_addr));
306			printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
307		}
308	}
309
310	/* If the DMA is active but not finished, we have the case
311	 * that some other 5380 interrupt occurred within the DMA transfer.
312	 * This means we have residual bytes, if the desired end address
313	 * is not yet reached. Maybe we have to fetch some bytes from the
314	 * rest data register, too. The residual must be calculated from
315	 * the address pointer, not the counter register, because only the
316	 * addr reg counts bytes not yet written and pending in the rest
317	 * data reg!
318	 */
319	if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
320		atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P( dma_addr ) -
321												atari_dma_startaddr);
322
323		DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n",
324			   atari_dma_residual);
325
326		if ((signed int)atari_dma_residual < 0)
327			atari_dma_residual = 0;
328		if ((dma_stat & 1) == 0) {
329			/* After read operations, we maybe have to
330			   transport some rest bytes */
331			atari_scsi_fetch_restbytes();
332		}
333		else {
334			/* There seems to be a nasty bug in some SCSI-DMA/NCR
335			   combinations: If a target disconnects while a write
336			   operation is going on, the address register of the
337			   DMA may be a few bytes farer than it actually read.
338			   This is probably due to DMA prefetching and a delay
339			   between DMA and NCR.  Experiments showed that the
340			   dma_addr is 9 bytes to high, but this could vary.
341			   The problem is, that the residual is thus calculated
342			   wrong and the next transfer will start behind where
343			   it should.  So we round up the residual to the next
344			   multiple of a sector size, if it isn't already a
345			   multiple and the originally expected transfer size
346			   was.  The latter condition is there to ensure that
347			   the correction is taken only for "real" data
348			   transfers and not for, e.g., the parameters of some
349			   other command.  These shouldn't disconnect anyway.
350			   */
351			if (atari_dma_residual & 0x1ff) {
352				DMA_PRINTK("SCSI DMA: DMA bug corrected, "
353					   "difference %ld bytes\n",
354					   512 - (atari_dma_residual & 0x1ff));
355				atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
356			}
357		}
358		tt_scsi_dma.dma_ctrl = 0;
359	}
360
361	/* If the DMA is finished, fetch the rest bytes and turn it off */
362	if (dma_stat & 0x40) {
363		atari_dma_residual = 0;
364		if ((dma_stat & 1) == 0)
365			atari_scsi_fetch_restbytes();
366		tt_scsi_dma.dma_ctrl = 0;
367	}
368
369#endif /* REAL_DMA */
370
371	NCR5380_intr (0, 0, 0);
372
373}
374
375
376static void scsi_falcon_intr (int irq, void *dummy, struct pt_regs *fp)
377{
378#ifdef REAL_DMA
379	int dma_stat;
380
381	/* Turn off DMA and select sector counter register before
382	 * accessing the status register (Atari recommendation!)
383	 */
384	st_dma.dma_mode_status = 0x90;
385	dma_stat = st_dma.dma_mode_status;
386
387	/* Bit 0 indicates some error in the DMA process... don't know
388	 * what happened exactly (no further docu).
389	 */
390	if (!(dma_stat & 0x01)) {
391		/* DMA error */
392		printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
393	}
394
395	/* If the DMA was active, but now bit 1 is not clear, it is some
396	 * other 5380 interrupt that finishes the DMA transfer. We have to
397	 * calculate the number of residual bytes and give a warning if
398	 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
399	 */
400	if (atari_dma_active && (dma_stat & 0x02)) {
401		unsigned long	transferred;
402
403		transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
404		/* The ST-DMA address is incremented in 2-byte steps, but the
405		 * data are written only in 16-byte chunks. If the number of
406		 * transferred bytes is not divisible by 16, the remainder is
407		 * lost somewhere in outer space.
408		 */
409		if (transferred & 15)
410			printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
411			       "ST-DMA fifo\n", transferred & 15);
412
413		atari_dma_residual = HOSTDATA_DMALEN - transferred;
414		DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n",
415			   atari_dma_residual);
416	}
417	else
418		atari_dma_residual = 0;
419	atari_dma_active = 0;
420
421	if (atari_dma_orig_addr) {
422		/* If the dribble buffer was used on a read operation, copy the DMA-ed
423		 * data to the original destination address.
424		 */
425		memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
426		       HOSTDATA_DMALEN - atari_dma_residual);
427		atari_dma_orig_addr = NULL;
428	}
429
430#endif /* REAL_DMA */
431
432	NCR5380_intr (0, 0, 0);
433}
434
435
436#ifdef REAL_DMA
437static void atari_scsi_fetch_restbytes( void )
438{
439	int nr;
440	char *src, *dst;
441	unsigned long phys_dst;
442
443	/* fetch rest bytes in the DMA register */
444	phys_dst = SCSI_DMA_READ_P(dma_addr);
445	nr = phys_dst & 3;
446	if (nr) {
447		/* there are 'nr' bytes left for the last long address
448		   before the DMA pointer */
449		phys_dst ^= nr;
450		DMA_PRINTK("SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
451			   nr, phys_dst);
452		/* The content of the DMA pointer is a physical address!  */
453		dst = phys_to_virt(phys_dst);
454		DMA_PRINTK(" = virt addr %p\n", dst);
455		for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
456			*dst++ = *src++;
457	}
458}
459#endif /* REAL_DMA */
460
461
462static int falcon_got_lock = 0;
463static DECLARE_WAIT_QUEUE_HEAD(falcon_fairness_wait);
464static int falcon_trying_lock = 0;
465static DECLARE_WAIT_QUEUE_HEAD(falcon_try_wait);
466static int falcon_dont_release = 0;
467
468/* This function releases the lock on the DMA chip if there is no
469 * connected command and the disconnected queue is empty. On
470 * releasing, instances of falcon_get_lock are awoken, that put
471 * themselves to sleep for fairness. They can now try to get the lock
472 * again (but others waiting longer more probably will win).
473 */
474
475static void
476falcon_release_lock_if_possible( struct NCR5380_hostdata * hostdata )
477{
478	unsigned long	oldflags;
479
480	if (IS_A_TT()) return;
481
482	save_flags(oldflags);
483	cli();
484
485	if (falcon_got_lock &&
486		!hostdata->disconnected_queue &&
487		!hostdata->issue_queue &&
488		!hostdata->connected) {
489
490		if (falcon_dont_release) {
491			restore_flags(oldflags);
492			return;
493		}
494		falcon_got_lock = 0;
495		stdma_release();
496		wake_up( &falcon_fairness_wait );
497	}
498
499	restore_flags(oldflags);
500}
501
502/* This function manages the locking of the ST-DMA.
503 * If the DMA isn't locked already for SCSI, it tries to lock it by
504 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
505 * there are other drivers waiting for the chip, we do not issue the
506 * command immediately but wait on 'falcon_fairness_queue'. We will be
507 * waked up when the DMA is unlocked by some SCSI interrupt. After that
508 * we try to get the lock again.
509 * But we must be prepared that more than one instance of
510 * falcon_get_lock() is waiting on the fairness queue. They should not
511 * try all at once to call stdma_lock(), one is enough! For that, the
512 * first one sets 'falcon_trying_lock', others that see that variable
513 * set wait on the queue 'falcon_try_wait'.
514 * Complicated, complicated.... Sigh...
515 */
516
517static void falcon_get_lock( void )
518{
519	unsigned long	oldflags;
520
521	if (IS_A_TT()) return;
522
523	save_flags(oldflags);
524	cli();
525
526	while( !in_interrupt() && falcon_got_lock && stdma_others_waiting() )
527		sleep_on( &falcon_fairness_wait );
528
529	while (!falcon_got_lock) {
530		if (in_interrupt())
531			panic( "Falcon SCSI hasn't ST-DMA lock in interrupt" );
532		if (!falcon_trying_lock) {
533			falcon_trying_lock = 1;
534			stdma_lock(scsi_falcon_intr, NULL);
535			falcon_got_lock = 1;
536			falcon_trying_lock = 0;
537			wake_up( &falcon_try_wait );
538		}
539		else {
540			sleep_on( &falcon_try_wait );
541		}
542	}
543
544	restore_flags(oldflags);
545	if (!falcon_got_lock)
546		panic("Falcon SCSI: someone stole the lock :-(\n");
547}
548
549
550/* This is the wrapper function for NCR5380_queue_command(). It just
551 * tries to get the lock on the ST-DMA (see above) and then calls the
552 * original function.
553 */
554
555
556
557int atari_scsi_detect (Scsi_Host_Template *host)
558{
559	static int called = 0;
560	struct Scsi_Host *instance;
561
562	if (!MACH_IS_ATARI ||
563	    (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) ||
564	    called)
565		return( 0 );
566
567	host->proc_name = "Atari";
568
569	atari_scsi_reg_read  = IS_A_TT() ? atari_scsi_tt_reg_read :
570					   atari_scsi_falcon_reg_read;
571	atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write :
572					   atari_scsi_falcon_reg_write;
573
574	/* setup variables */
575	host->can_queue =
576		(setup_can_queue > 0) ? setup_can_queue :
577		IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE;
578	host->cmd_per_lun =
579		(setup_cmd_per_lun > 0) ? setup_cmd_per_lun :
580		IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN;
581	/* Force sg_tablesize to 0 on a Falcon! */
582	host->sg_tablesize =
583		!IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE :
584		(setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE;
585
586	if (setup_hostid >= 0)
587		host->this_id = setup_hostid;
588	else {
589		/* use 7 as default */
590		host->this_id = 7;
591		/* Test if a host id is set in the NVRam */
592		if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
593			unsigned char b = nvram_read_byte( 14 );
594			/* Arbitration enabled? (for TOS) If yes, use configured host ID */
595			if (b & 0x80)
596				host->this_id = b & 7;
597		}
598	}
599
600#ifdef SUPPORT_TAGS
601	if (setup_use_tagged_queuing < 0)
602		setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
603#endif
604#ifdef REAL_DMA
605	/* If running on a Falcon and if there's TT-Ram (i.e., more than one
606	 * memory block, since there's always ST-Ram in a Falcon), then allocate a
607	 * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative
608	 * Ram.
609	 */
610	if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
611	    !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) {
612		atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
613		if (!atari_dma_buffer) {
614			printk( KERN_ERR "atari_scsi_detect: can't allocate ST-RAM "
615					"double buffer\n" );
616			return( 0 );
617		}
618		atari_dma_phys_buffer = virt_to_phys( atari_dma_buffer );
619		atari_dma_orig_addr = 0;
620	}
621#endif
622	instance = scsi_register (host, sizeof (struct NCR5380_hostdata));
623	if(instance == NULL)
624	{
625		atari_stram_free(atari_dma_buffer);
626		atari_dma_buffer = 0;
627		return 0;
628	}
629	atari_scsi_host = instance;
630       /* Set irq to 0, to avoid that the mid-level code disables our interrupt
631        * during queue_command calls. This is completely unnecessary, and even
632        * worse causes bad problems on the Falcon, where the int is shared with
633        * IDE and floppy! */
634       instance->irq = 0;
635
636#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
637	atari_scsi_reset_boot();
638#endif
639	NCR5380_init (instance, 0);
640
641	if (IS_A_TT()) {
642
643		/* This int is actually "pseudo-slow", i.e. it acts like a slow
644		 * interrupt after having cleared the pending flag for the DMA
645		 * interrupt. */
646		if (request_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW,
647				 "SCSI NCR5380", scsi_tt_intr)) {
648			printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting",IRQ_TT_MFP_SCSI);
649			scsi_unregister(atari_scsi_host);
650			atari_stram_free(atari_dma_buffer);
651			atari_dma_buffer = 0;
652			return 0;
653		}
654		tt_mfp.active_edge |= 0x80;		/* SCSI int on L->H */
655#ifdef REAL_DMA
656		tt_scsi_dma.dma_ctrl = 0;
657		atari_dma_residual = 0;
658#ifdef CONFIG_TT_DMA_EMUL
659		if (MACH_IS_HADES) {
660			if (request_irq(IRQ_AUTO_2, hades_dma_emulator,
661					 IRQ_TYPE_PRIO, "Hades DMA emulator",
662					 hades_dma_emulator)) {
663				printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting (MACH_IS_HADES)",IRQ_AUTO_2);
664				free_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr);
665				scsi_unregister(atari_scsi_host);
666				atari_stram_free(atari_dma_buffer);
667				atari_dma_buffer = 0;
668				return 0;
669			}
670		}
671#endif
672		if (MACH_IS_MEDUSA || MACH_IS_HADES) {
673			/* While the read overruns (described by Drew Eckhardt in
674			 * NCR5380.c) never happened on TTs, they do in fact on the Medusa
675			 * (This was the cause why SCSI didn't work right for so long
676			 * there.) Since handling the overruns slows down a bit, I turned
677			 * the #ifdef's into a runtime condition.
678			 *
679			 * In principle it should be sufficient to do max. 1 byte with
680			 * PIO, but there is another problem on the Medusa with the DMA
681			 * rest data register. So 'atari_read_overruns' is currently set
682			 * to 4 to avoid having transfers that aren't a multiple of 4. If
683			 * the rest data bug is fixed, this can be lowered to 1.
684			 */
685			atari_read_overruns = 4;
686		}
687#endif /*REAL_DMA*/
688	}
689	else { /* ! IS_A_TT */
690
691		/* Nothing to do for the interrupt: the ST-DMA is initialized
692		 * already by atari_init_INTS()
693		 */
694
695#ifdef REAL_DMA
696		atari_dma_residual = 0;
697		atari_dma_active = 0;
698		atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
699					: 0xff000000);
700#endif
701	}
702
703	printk(KERN_INFO "scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d "
704#ifdef SUPPORT_TAGS
705			"TAGGED-QUEUING=%s "
706#endif
707			"HOSTID=%d",
708			instance->host_no, instance->hostt->can_queue,
709			instance->hostt->cmd_per_lun,
710			instance->hostt->sg_tablesize,
711#ifdef SUPPORT_TAGS
712			setup_use_tagged_queuing ? "yes" : "no",
713#endif
714			instance->hostt->this_id );
715	NCR5380_print_options (instance);
716	printk ("\n");
717
718	called = 1;
719	return( 1 );
720}
721
722#ifdef MODULE
723int atari_scsi_release (struct Scsi_Host *sh)
724{
725	if (IS_A_TT())
726		free_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr);
727	if (atari_dma_buffer)
728		atari_stram_free (atari_dma_buffer);
729	return 1;
730}
731#endif
732
733void __init atari_scsi_setup(char *str, int *ints)
734{
735	/* Format of atascsi parameter is:
736	 *   atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
737	 * Defaults depend on TT or Falcon, hostid determined at run time.
738	 * Negative values mean don't change.
739	 */
740
741	if (ints[0] < 1) {
742		printk( "atari_scsi_setup: no arguments!\n" );
743		return;
744	}
745
746	if (ints[0] >= 1) {
747		if (ints[1] > 0)
748			/* no limits on this, just > 0 */
749			setup_can_queue = ints[1];
750	}
751	if (ints[0] >= 2) {
752		if (ints[2] > 0)
753			setup_cmd_per_lun = ints[2];
754	}
755	if (ints[0] >= 3) {
756		if (ints[3] >= 0) {
757			setup_sg_tablesize = ints[3];
758			/* Must be <= SG_ALL (255) */
759			if (setup_sg_tablesize > SG_ALL)
760				setup_sg_tablesize = SG_ALL;
761		}
762	}
763	if (ints[0] >= 4) {
764		/* Must be between 0 and 7 */
765		if (ints[4] >= 0 && ints[4] <= 7)
766			setup_hostid = ints[4];
767		else if (ints[4] > 7)
768			printk( "atari_scsi_setup: invalid host ID %d !\n", ints[4] );
769	}
770#ifdef SUPPORT_TAGS
771	if (ints[0] >= 5) {
772		if (ints[5] >= 0)
773			setup_use_tagged_queuing = !!ints[5];
774	}
775#endif
776}
777
778int atari_scsi_reset( Scsi_Cmnd *cmd, unsigned int reset_flags)
779{
780	int		rv;
781	struct NCR5380_hostdata *hostdata =
782		(struct NCR5380_hostdata *)cmd->host->hostdata;
783
784	/* For doing the reset, SCSI interrupts must be disabled first,
785	 * since the 5380 raises its IRQ line while _RST is active and we
786	 * can't disable interrupts completely, since we need the timer.
787	 */
788	/* And abort a maybe active DMA transfer */
789	if (IS_A_TT()) {
790		atari_turnoff_irq( IRQ_TT_MFP_SCSI );
791#ifdef REAL_DMA
792		tt_scsi_dma.dma_ctrl = 0;
793#endif /* REAL_DMA */
794	}
795	else {
796		atari_turnoff_irq( IRQ_MFP_FSCSI );
797#ifdef REAL_DMA
798		st_dma.dma_mode_status = 0x90;
799		atari_dma_active = 0;
800		atari_dma_orig_addr = NULL;
801#endif /* REAL_DMA */
802	}
803
804	rv = NCR5380_reset(cmd, reset_flags);
805
806	/* Re-enable ints */
807	if (IS_A_TT()) {
808		atari_turnon_irq( IRQ_TT_MFP_SCSI );
809	}
810	else {
811		atari_turnon_irq( IRQ_MFP_FSCSI );
812	}
813	if ((rv & SCSI_RESET_ACTION) == SCSI_RESET_SUCCESS)
814		falcon_release_lock_if_possible(hostdata);
815
816	return( rv );
817}
818
819
820#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
821static void __init atari_scsi_reset_boot(void)
822{
823	unsigned long end;
824
825	/*
826	 * Do a SCSI reset to clean up the bus during initialization. No messing
827	 * with the queues, interrupts, or locks necessary here.
828	 */
829
830	printk( "Atari SCSI: resetting the SCSI bus..." );
831
832	/* get in phase */
833	NCR5380_write( TARGET_COMMAND_REG,
834		      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
835
836	/* assert RST */
837	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
838	/* The min. reset hold time is 25us, so 40us should be enough */
839	udelay( 50 );
840	/* reset RST and interrupt */
841	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
842	NCR5380_read( RESET_PARITY_INTERRUPT_REG );
843
844	end = jiffies + AFTER_RESET_DELAY;
845	while (time_before(jiffies, end))
846		barrier();
847
848	printk( " done\n" );
849}
850#endif
851
852
853const char * atari_scsi_info (struct Scsi_Host *host)
854{
855	/* atari_scsi_detect() is verbose enough... */
856	static const char string[] = "Atari native SCSI";
857	return string;
858}
859
860
861#if defined(REAL_DMA)
862
863unsigned long atari_scsi_dma_setup( struct Scsi_Host *instance, void *data,
864				   unsigned long count, int dir )
865{
866	unsigned long addr = virt_to_phys( data );
867
868	DMA_PRINTK("scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
869		   "dir = %d\n", instance->host_no, data, addr, count, dir);
870
871	if (!IS_A_TT() && !STRAM_ADDR(addr)) {
872		/* If we have a non-DMAable address on a Falcon, use the dribble
873		 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
874		 * handler to copy data from the dribble buffer to the originally
875		 * wanted address.
876		 */
877		if (dir)
878			memcpy( atari_dma_buffer, data, count );
879		else
880			atari_dma_orig_addr = data;
881		addr = atari_dma_phys_buffer;
882	}
883
884	atari_dma_startaddr = addr;	/* Needed for calculating residual later. */
885
886	/* Cache cleanup stuff: On writes, push any dirty cache out before sending
887	 * it to the peripheral. (Must be done before DMA setup, since at least
888	 * the ST-DMA begins to fill internal buffers right after setup. For
889	 * reads, invalidate any cache, may be altered after DMA without CPU
890	 * knowledge.
891	 *
892	 * ++roman: For the Medusa, there's no need at all for that cache stuff,
893	 * because the hardware does bus snooping (fine!).
894	 */
895	dma_cache_maintenance( addr, count, dir );
896
897	if (count == 0)
898		printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
899
900	if (IS_A_TT()) {
901		tt_scsi_dma.dma_ctrl = dir;
902		SCSI_DMA_WRITE_P( dma_addr, addr );
903		SCSI_DMA_WRITE_P( dma_cnt, count );
904		tt_scsi_dma.dma_ctrl = dir | 2;
905	}
906	else { /* ! IS_A_TT */
907
908		/* set address */
909		SCSI_DMA_SETADR( addr );
910
911		/* toggle direction bit to clear FIFO and set DMA direction */
912		dir <<= 8;
913		st_dma.dma_mode_status = 0x90 | dir;
914		st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
915		st_dma.dma_mode_status = 0x90 | dir;
916		udelay(40);
917		/* On writes, round up the transfer length to the next multiple of 512
918		 * (see also comment at atari_dma_xfer_len()). */
919		st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
920		udelay(40);
921		st_dma.dma_mode_status = 0x10 | dir;
922		udelay(40);
923		/* need not restore value of dir, only boolean value is tested */
924		atari_dma_active = 1;
925	}
926
927	return( count );
928}
929
930
931static long atari_scsi_dma_residual( struct Scsi_Host *instance )
932{
933	return( atari_dma_residual );
934}
935
936
937#define	CMD_SURELY_BLOCK_MODE	0
938#define	CMD_SURELY_BYTE_MODE	1
939#define	CMD_MODE_UNKNOWN		2
940
941static int falcon_classify_cmd( Scsi_Cmnd *cmd )
942{
943	unsigned char opcode = cmd->cmnd[0];
944
945	if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
946		opcode == READ_BUFFER)
947		return( CMD_SURELY_BYTE_MODE );
948	else if (opcode == READ_6 || opcode == READ_10 ||
949		 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
950		 opcode == RECOVER_BUFFERED_DATA) {
951		/* In case of a sequential-access target (tape), special care is
952		 * needed here: The transfer is block-mode only if the 'fixed' bit is
953		 * set! */
954		if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
955			return( CMD_SURELY_BYTE_MODE );
956		else
957			return( CMD_SURELY_BLOCK_MODE );
958	}
959	else
960		return( CMD_MODE_UNKNOWN );
961}
962
963
964/* This function calculates the number of bytes that can be transferred via
965 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
966 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
967 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
968 * possible on the Falcon, since that would require to program the DMA for
969 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
970 * the overrun problem, so this question is academic :-)
971 */
972
973static unsigned long atari_dma_xfer_len( unsigned long wanted_len,
974					Scsi_Cmnd *cmd,
975					int write_flag )
976{
977	unsigned long	possible_len, limit;
978#ifndef CONFIG_TT_DMA_EMUL
979	if (MACH_IS_HADES)
980		/* Hades has no SCSI DMA at all :-( Always force use of PIO */
981		return( 0 );
982#endif
983	if (IS_A_TT())
984		/* TT SCSI DMA can transfer arbitrary #bytes */
985		return( wanted_len );
986
987	/* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
988	 * 255*512 bytes, but this should be enough)
989	 *
990	 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
991	 * that return a number of bytes which cannot be known beforehand. In this
992	 * case, the given transfer length is an "allocation length". Now it
993	 * can happen that this allocation length is a multiple of 512 bytes and
994	 * the DMA is used. But if not n*512 bytes really arrive, some input data
995	 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
996	 * between commands that do block transfers and those that do byte
997	 * transfers. But this isn't easy... there are lots of vendor specific
998	 * commands, and the user can issue any command via the
999	 * SCSI_IOCTL_SEND_COMMAND.
1000	 *
1001	 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
1002	 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
1003	 * and 3), the thing to do is obvious: allow any number of blocks via DMA
1004	 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
1005	 * the transfer (allocation) length is < 1024, hoping that no cmd. not
1006	 * explicitly known as byte mode have such big allocation lengths...
1007	 * BTW, all the discussion above applies only to reads. DMA writes are
1008	 * unproblematic anyways, since the targets aborts the transfer after
1009	 * receiving a sufficient number of bytes.
1010	 *
1011	 * Another point: If the transfer is from/to an non-ST-RAM address, we
1012	 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
1013	 */
1014
1015	if (write_flag) {
1016		/* Write operation can always use the DMA, but the transfer size must
1017		 * be rounded up to the next multiple of 512 (atari_dma_setup() does
1018		 * this).
1019		 */
1020		possible_len = wanted_len;
1021	}
1022	else {
1023		/* Read operations: if the wanted transfer length is not a multiple of
1024		 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
1025		 * (no interrupt on DMA finished!)
1026		 */
1027		if (wanted_len & 0x1ff)
1028			possible_len = 0;
1029		else {
1030			/* Now classify the command (see above) and decide whether it is
1031			 * allowed to do DMA at all */
1032			switch( falcon_classify_cmd( cmd )) {
1033			  case CMD_SURELY_BLOCK_MODE:
1034				possible_len = wanted_len;
1035				break;
1036			  case CMD_SURELY_BYTE_MODE:
1037				possible_len = 0; /* DMA prohibited */
1038				break;
1039			  case CMD_MODE_UNKNOWN:
1040			  default:
1041				/* For unknown commands assume block transfers if the transfer
1042				 * size/allocation length is >= 1024 */
1043				possible_len = (wanted_len < 1024) ? 0 : wanted_len;
1044				break;
1045			}
1046		}
1047	}
1048
1049	/* Last step: apply the hard limit on DMA transfers */
1050	limit = (atari_dma_buffer && !STRAM_ADDR( virt_to_phys(cmd->SCp.ptr) )) ?
1051		    STRAM_BUFFER_SIZE : 255*512;
1052	if (possible_len > limit)
1053		possible_len = limit;
1054
1055	if (possible_len != wanted_len)
1056		DMA_PRINTK("Sorry, must cut DMA transfer size to %ld bytes "
1057			   "instead of %ld\n", possible_len, wanted_len);
1058
1059	return( possible_len );
1060}
1061
1062
1063#endif	/* REAL_DMA */
1064
1065
1066/* NCR5380 register access functions
1067 *
1068 * There are separate functions for TT and Falcon, because the access
1069 * methods are quite different. The calling macros NCR5380_read and
1070 * NCR5380_write call these functions via function pointers.
1071 */
1072
1073static unsigned char atari_scsi_tt_reg_read( unsigned char reg )
1074{
1075	return( tt_scsi_regp[reg * 2] );
1076}
1077
1078static void atari_scsi_tt_reg_write( unsigned char reg, unsigned char value )
1079{
1080	tt_scsi_regp[reg * 2] = value;
1081}
1082
1083static unsigned char atari_scsi_falcon_reg_read( unsigned char reg )
1084{
1085	dma_wd.dma_mode_status= (u_short)(0x88 + reg);
1086	return( (u_char)dma_wd.fdc_acces_seccount );
1087}
1088
1089static void atari_scsi_falcon_reg_write( unsigned char reg, unsigned char value )
1090{
1091	dma_wd.dma_mode_status = (u_short)(0x88 + reg);
1092	dma_wd.fdc_acces_seccount = (u_short)value;
1093}
1094
1095
1096#include "atari_NCR5380.c"
1097
1098static Scsi_Host_Template driver_template = ATARI_SCSI;
1099#include "scsi_module.c"
1100
1101MODULE_LICENSE("GPL");
1102