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