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