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