blob: b2e86d0630ce98fdbdd4f60ada4e9e61fcaa9215 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/module.h>
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* For the Atari version, use only polled IO or REAL_DMA */
71#define REAL_DMA
72/* Support tagged queuing? (on devices that are able to... :-) */
73#define SUPPORT_TAGS
74#define MAX_TAGS 32
75
76#include <linux/types.h>
77#include <linux/stddef.h>
78#include <linux/ctype.h>
79#include <linux/delay.h>
80#include <linux/mm.h>
81#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#include <linux/interrupt.h>
83#include <linux/init.h>
84#include <linux/nvram.h>
85#include <linux/bitops.h>
Arnd Bergmanneff9cf82014-03-01 20:51:03 +130086#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#include <asm/setup.h>
89#include <asm/atarihw.h>
90#include <asm/atariints.h>
91#include <asm/page.h>
92#include <asm/pgtable.h>
93#include <asm/irq.h>
94#include <asm/traps.h>
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <scsi/scsi_host.h>
97#include "atari_scsi.h"
98#include "NCR5380.h"
99#include <asm/atari_stdma.h>
100#include <asm/atari_stram.h>
101#include <asm/io.h>
102
103#include <linux/stat.h>
104
105#define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
106
107#define SCSI_DMA_WRITE_P(elt,val) \
108 do { \
109 unsigned long v = val; \
110 tt_scsi_dma.elt##_lo = v & 0xff; \
111 v >>= 8; \
112 tt_scsi_dma.elt##_lmd = v & 0xff; \
113 v >>= 8; \
114 tt_scsi_dma.elt##_hmd = v & 0xff; \
115 v >>= 8; \
116 tt_scsi_dma.elt##_hi = v & 0xff; \
117 } while(0)
118
119#define SCSI_DMA_READ_P(elt) \
120 (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
121 (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
122 (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
123 (unsigned long)tt_scsi_dma.elt##_lo)
124
125
126static inline void SCSI_DMA_SETADR(unsigned long adr)
127{
128 st_dma.dma_lo = (unsigned char)adr;
129 MFPDELAY();
130 adr >>= 8;
131 st_dma.dma_md = (unsigned char)adr;
132 MFPDELAY();
133 adr >>= 8;
134 st_dma.dma_hi = (unsigned char)adr;
135 MFPDELAY();
136}
137
138static inline unsigned long SCSI_DMA_GETADR(void)
139{
140 unsigned long adr;
141 adr = st_dma.dma_lo;
142 MFPDELAY();
143 adr |= (st_dma.dma_md & 0xff) << 8;
144 MFPDELAY();
145 adr |= (st_dma.dma_hi & 0xff) << 16;
146 MFPDELAY();
147 return adr;
148}
149
150static inline void ENABLE_IRQ(void)
151{
152 if (IS_A_TT())
153 atari_enable_irq(IRQ_TT_MFP_SCSI);
154 else
155 atari_enable_irq(IRQ_MFP_FSCSI);
156}
157
158static inline void DISABLE_IRQ(void)
159{
160 if (IS_A_TT())
161 atari_disable_irq(IRQ_TT_MFP_SCSI);
162 else
163 atari_disable_irq(IRQ_MFP_FSCSI);
164}
165
166
167#define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \
168 (atari_scsi_host->hostdata))->dma_len)
169
170/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
171 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
172 * need ten times the standard value... */
173#ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
174#define AFTER_RESET_DELAY (HZ/2)
175#else
176#define AFTER_RESET_DELAY (5*HZ/2)
177#endif
178
179/***************************** Prototypes *****************************/
180
181#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200182static void atari_scsi_fetch_restbytes(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200184static irqreturn_t scsi_tt_intr(int irq, void *dummy);
185static irqreturn_t scsi_falcon_intr(int irq, void *dummy);
186static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata);
Finn Thain16b29e72014-11-12 16:12:08 +1100187static int falcon_get_lock(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
Roman Zippelc28bda22007-05-01 22:32:36 +0200189static void atari_scsi_reset_boot(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200191static unsigned char atari_scsi_tt_reg_read(unsigned char reg);
192static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value);
193static unsigned char atari_scsi_falcon_reg_read(unsigned char reg);
194static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/************************* End of Prototypes **************************/
197
198
Roman Zippelc28bda22007-05-01 22:32:36 +0200199static struct Scsi_Host *atari_scsi_host;
200static unsigned char (*atari_scsi_reg_read)(unsigned char reg);
201static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203#ifdef REAL_DMA
204static unsigned long atari_dma_residual, atari_dma_startaddr;
205static short atari_dma_active;
206/* pointer to the dribble buffer */
Roman Zippelc28bda22007-05-01 22:32:36 +0200207static char *atari_dma_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208/* precalculated physical address of the dribble buffer */
209static unsigned long atari_dma_phys_buffer;
210/* != 0 tells the Falcon int handler to copy data from the dribble buffer */
211static char *atari_dma_orig_addr;
212/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
213 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
214 * cases where requests to physical contiguous buffers have been merged, this
215 * request is <= 4k (one page). So I don't think we have to split transfers
216 * just due to this buffer size...
217 */
218#define STRAM_BUFFER_SIZE (4096)
219/* mask for address bits that can't be used with the ST-DMA */
220static unsigned long atari_dma_stram_mask;
221#define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
222/* number of bytes to cut from a transfer to handle NCR overruns */
Roman Zippelc28bda22007-05-01 22:32:36 +0200223static int atari_read_overruns;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224#endif
225
226static int setup_can_queue = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800227module_param(setup_can_queue, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int setup_cmd_per_lun = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800229module_param(setup_cmd_per_lun, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int setup_sg_tablesize = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800231module_param(setup_sg_tablesize, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#ifdef SUPPORT_TAGS
233static int setup_use_tagged_queuing = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800234module_param(setup_use_tagged_queuing, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#endif
236static int setup_hostid = -1;
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800237module_param(setup_hostid, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#if defined(REAL_DMA)
241
Roman Zippelc28bda22007-05-01 22:32:36 +0200242static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int i;
Roman Zippelc28bda22007-05-01 22:32:36 +0200245 unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 if (dma_stat & 0x01) {
248
249 /* A bus error happens when DMA-ing from the last page of a
250 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
251 * Check for this case:
252 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200253
254 for (i = 0; i < m68k_num_memory; ++i) {
255 end_addr = m68k_memory[i].addr + m68k_memory[i].size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (end_addr <= addr && addr <= end_addr + 4)
Roman Zippelc28bda22007-05-01 22:32:36 +0200257 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200260 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
263
264#if 0
265/* Dead code... wasn't called anyway :-) and causes some trouble, because at
266 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
267 * to clear the DMA int pending bit before it allows other level 6 interrupts.
268 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200269static void scsi_dma_buserr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Roman Zippelc28bda22007-05-01 22:32:36 +0200271 unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 /* Don't do anything if a NCR interrupt is pending. Probably it's just
274 * masked... */
Roman Zippelc28bda22007-05-01 22:32:36 +0200275 if (atari_irq_pending(IRQ_TT_MFP_SCSI))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return;
Roman Zippelc28bda22007-05-01 22:32:36 +0200277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
279 SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
280 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200281 if (!scsi_dma_is_ignored_buserr(dma_stat))
282 printk("SCSI DMA bus error -- bad DMA programming!\n");
283 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Under normal circumstances we never should get to this point,
285 * since both interrupts are triggered simultaneously and the 5380
286 * int has higher priority. When this irq is handled, that DMA
287 * interrupt is cleared. So a warning message is printed here.
288 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200289 printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291}
292#endif
293
294#endif
295
296
Roman Zippelc28bda22007-05-01 22:32:36 +0200297static irqreturn_t scsi_tt_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299#ifdef REAL_DMA
300 int dma_stat;
301
302 dma_stat = tt_scsi_dma.dma_ctrl;
303
Finn Thaind65e6342014-03-18 11:42:20 +1100304 dprintk(NDEBUG_INTR, "scsi%d: NCR5380 interrupt, DMA status = %02x\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 atari_scsi_host->host_no, dma_stat & 0xff);
306
307 /* Look if it was the DMA that has interrupted: First possibility
308 * is that a bus error occurred...
309 */
310 if (dma_stat & 0x80) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200311 if (!scsi_dma_is_ignored_buserr(dma_stat)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
313 SCSI_DMA_READ_P(dma_addr));
314 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
315 }
316 }
317
318 /* If the DMA is active but not finished, we have the case
319 * that some other 5380 interrupt occurred within the DMA transfer.
320 * This means we have residual bytes, if the desired end address
321 * is not yet reached. Maybe we have to fetch some bytes from the
322 * rest data register, too. The residual must be calculated from
323 * the address pointer, not the counter register, because only the
324 * addr reg counts bytes not yet written and pending in the rest
325 * data reg!
326 */
327 if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200328 atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Finn Thaind65e6342014-03-18 11:42:20 +1100330 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 atari_dma_residual);
332
333 if ((signed int)atari_dma_residual < 0)
334 atari_dma_residual = 0;
335 if ((dma_stat & 1) == 0) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200336 /*
337 * After read operations, we maybe have to
338 * transport some rest bytes
339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 atari_scsi_fetch_restbytes();
Roman Zippelc28bda22007-05-01 22:32:36 +0200341 } else {
342 /*
343 * There seems to be a nasty bug in some SCSI-DMA/NCR
344 * combinations: If a target disconnects while a write
345 * operation is going on, the address register of the
346 * DMA may be a few bytes farer than it actually read.
347 * This is probably due to DMA prefetching and a delay
348 * between DMA and NCR. Experiments showed that the
349 * dma_addr is 9 bytes to high, but this could vary.
350 * The problem is, that the residual is thus calculated
351 * wrong and the next transfer will start behind where
352 * it should. So we round up the residual to the next
353 * multiple of a sector size, if it isn't already a
354 * multiple and the originally expected transfer size
355 * was. The latter condition is there to ensure that
356 * the correction is taken only for "real" data
357 * transfers and not for, e.g., the parameters of some
358 * other command. These shouldn't disconnect anyway.
359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (atari_dma_residual & 0x1ff) {
Finn Thaind65e6342014-03-18 11:42:20 +1100361 dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 "difference %ld bytes\n",
363 512 - (atari_dma_residual & 0x1ff));
364 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
365 }
366 }
367 tt_scsi_dma.dma_ctrl = 0;
368 }
369
370 /* If the DMA is finished, fetch the rest bytes and turn it off */
371 if (dma_stat & 0x40) {
372 atari_dma_residual = 0;
373 if ((dma_stat & 1) == 0)
374 atari_scsi_fetch_restbytes();
375 tt_scsi_dma.dma_ctrl = 0;
376 }
377
378#endif /* REAL_DMA */
Roman Zippelc28bda22007-05-01 22:32:36 +0200379
Jeff Garzik1e641662007-11-11 19:52:05 -0500380 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382#if 0
383 /* To be sure the int is not masked */
Roman Zippelc28bda22007-05-01 22:32:36 +0200384 atari_enable_irq(IRQ_TT_MFP_SCSI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#endif
386 return IRQ_HANDLED;
387}
388
389
Roman Zippelc28bda22007-05-01 22:32:36 +0200390static irqreturn_t scsi_falcon_intr(int irq, void *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392#ifdef REAL_DMA
393 int dma_stat;
394
395 /* Turn off DMA and select sector counter register before
396 * accessing the status register (Atari recommendation!)
397 */
398 st_dma.dma_mode_status = 0x90;
399 dma_stat = st_dma.dma_mode_status;
400
401 /* Bit 0 indicates some error in the DMA process... don't know
402 * what happened exactly (no further docu).
403 */
404 if (!(dma_stat & 0x01)) {
405 /* DMA error */
406 printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
407 }
408
409 /* If the DMA was active, but now bit 1 is not clear, it is some
410 * other 5380 interrupt that finishes the DMA transfer. We have to
411 * calculate the number of residual bytes and give a warning if
412 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
413 */
414 if (atari_dma_active && (dma_stat & 0x02)) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200415 unsigned long transferred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
418 /* The ST-DMA address is incremented in 2-byte steps, but the
419 * data are written only in 16-byte chunks. If the number of
420 * transferred bytes is not divisible by 16, the remainder is
421 * lost somewhere in outer space.
422 */
423 if (transferred & 15)
424 printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
425 "ST-DMA fifo\n", transferred & 15);
426
427 atari_dma_residual = HOSTDATA_DMALEN - transferred;
Finn Thaind65e6342014-03-18 11:42:20 +1100428 dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 atari_dma_residual);
Roman Zippelc28bda22007-05-01 22:32:36 +0200430 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 atari_dma_residual = 0;
432 atari_dma_active = 0;
433
434 if (atari_dma_orig_addr) {
435 /* If the dribble buffer was used on a read operation, copy the DMA-ed
436 * data to the original destination address.
437 */
438 memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
439 HOSTDATA_DMALEN - atari_dma_residual);
440 atari_dma_orig_addr = NULL;
441 }
442
443#endif /* REAL_DMA */
444
Jeff Garzik1e641662007-11-11 19:52:05 -0500445 NCR5380_intr(irq, dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return IRQ_HANDLED;
447}
448
449
450#ifdef REAL_DMA
Roman Zippelc28bda22007-05-01 22:32:36 +0200451static void atari_scsi_fetch_restbytes(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 int nr;
454 char *src, *dst;
455 unsigned long phys_dst;
456
457 /* fetch rest bytes in the DMA register */
458 phys_dst = SCSI_DMA_READ_P(dma_addr);
459 nr = phys_dst & 3;
460 if (nr) {
461 /* there are 'nr' bytes left for the last long address
462 before the DMA pointer */
463 phys_dst ^= nr;
Finn Thaind65e6342014-03-18 11:42:20 +1100464 dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 nr, phys_dst);
466 /* The content of the DMA pointer is a physical address! */
467 dst = phys_to_virt(phys_dst);
Finn Thaind65e6342014-03-18 11:42:20 +1100468 dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
470 *dst++ = *src++;
471 }
472}
473#endif /* REAL_DMA */
474
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static int falcon_dont_release = 0;
477
478/* This function releases the lock on the DMA chip if there is no
Finn Thain16b29e72014-11-12 16:12:08 +1100479 * connected command and the disconnected queue is empty.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 */
481
Roman Zippelc28bda22007-05-01 22:32:36 +0200482static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 unsigned long flags;
Roman Zippelc28bda22007-05-01 22:32:36 +0200485
486 if (IS_A_TT())
487 return;
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 local_irq_save(flags);
490
Finn Thain16b29e72014-11-12 16:12:08 +1100491 if (!hostdata->disconnected_queue &&
492 !hostdata->issue_queue &&
493 !hostdata->connected &&
494 !falcon_dont_release &&
495 stdma_is_locked_by(scsi_falcon_intr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 stdma_release();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 local_irq_restore(flags);
499}
500
501/* This function manages the locking of the ST-DMA.
502 * If the DMA isn't locked already for SCSI, it tries to lock it by
503 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
504 * there are other drivers waiting for the chip, we do not issue the
Finn Thain16b29e72014-11-12 16:12:08 +1100505 * command immediately but tell the SCSI mid-layer to defer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 */
507
Finn Thain16b29e72014-11-12 16:12:08 +1100508static int falcon_get_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Roman Zippelc28bda22007-05-01 22:32:36 +0200510 if (IS_A_TT())
Finn Thain16b29e72014-11-12 16:12:08 +1100511 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Finn Thain16b29e72014-11-12 16:12:08 +1100513 if (in_interrupt())
514 return stdma_try_lock(scsi_falcon_intr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Finn Thain16b29e72014-11-12 16:12:08 +1100516 stdma_lock(scsi_falcon_intr, NULL);
517 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
520
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200521static int __init atari_scsi_detect(struct scsi_host_template *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
523 static int called = 0;
524 struct Scsi_Host *instance;
525
526 if (!MACH_IS_ATARI ||
527 (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) ||
528 called)
Roman Zippelc28bda22007-05-01 22:32:36 +0200529 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 host->proc_name = "Atari";
532
533 atari_scsi_reg_read = IS_A_TT() ? atari_scsi_tt_reg_read :
534 atari_scsi_falcon_reg_read;
535 atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write :
536 atari_scsi_falcon_reg_write;
537
538 /* setup variables */
539 host->can_queue =
540 (setup_can_queue > 0) ? setup_can_queue :
541 IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE;
542 host->cmd_per_lun =
543 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun :
544 IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN;
545 /* Force sg_tablesize to 0 on a Falcon! */
546 host->sg_tablesize =
547 !IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE :
548 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE;
549
550 if (setup_hostid >= 0)
551 host->this_id = setup_hostid;
552 else {
553 /* use 7 as default */
554 host->this_id = 7;
555 /* Test if a host id is set in the NVRam */
556 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
557 unsigned char b = nvram_read_byte( 14 );
558 /* Arbitration enabled? (for TOS) If yes, use configured host ID */
559 if (b & 0x80)
560 host->this_id = b & 7;
561 }
562 }
563
564#ifdef SUPPORT_TAGS
565 if (setup_use_tagged_queuing < 0)
Finn Thaind572f65f2014-11-12 16:12:00 +1100566 setup_use_tagged_queuing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#endif
568#ifdef REAL_DMA
569 /* If running on a Falcon and if there's TT-Ram (i.e., more than one
570 * memory block, since there's always ST-Ram in a Falcon), then allocate a
571 * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative
572 * Ram.
573 */
574 if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
575 !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) {
576 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
577 if (!atari_dma_buffer) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200578 printk(KERN_ERR "atari_scsi_detect: can't allocate ST-RAM "
579 "double buffer\n");
580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Michael Schmitza19f8162014-03-31 21:06:08 +1300582 atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 atari_dma_orig_addr = 0;
584 }
585#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200586 instance = scsi_register(host, sizeof(struct NCR5380_hostdata));
587 if (instance == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 atari_stram_free(atari_dma_buffer);
589 atari_dma_buffer = 0;
590 return 0;
591 }
592 atari_scsi_host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200593 /*
594 * Set irq to 0, to avoid that the mid-level code disables our interrupt
595 * during queue_command calls. This is completely unnecessary, and even
596 * worse causes bad problems on the Falcon, where the int is shared with
597 * IDE and floppy!
598 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 instance->irq = 0;
600
601#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
602 atari_scsi_reset_boot();
603#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200604 NCR5380_init(instance, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 if (IS_A_TT()) {
607
608 /* This int is actually "pseudo-slow", i.e. it acts like a slow
609 * interrupt after having cleared the pending flag for the DMA
610 * interrupt. */
611 if (request_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW,
Jeff Garzik1e641662007-11-11 19:52:05 -0500612 "SCSI NCR5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting",IRQ_TT_MFP_SCSI);
614 scsi_unregister(atari_scsi_host);
615 atari_stram_free(atari_dma_buffer);
616 atari_dma_buffer = 0;
617 return 0;
618 }
619 tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
620#ifdef REAL_DMA
621 tt_scsi_dma.dma_ctrl = 0;
622 atari_dma_residual = 0;
Adrian Bunk29c8a242008-10-13 21:58:59 +0200623
624 if (MACH_IS_MEDUSA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* While the read overruns (described by Drew Eckhardt in
626 * NCR5380.c) never happened on TTs, they do in fact on the Medusa
627 * (This was the cause why SCSI didn't work right for so long
628 * there.) Since handling the overruns slows down a bit, I turned
629 * the #ifdef's into a runtime condition.
630 *
631 * In principle it should be sufficient to do max. 1 byte with
632 * PIO, but there is another problem on the Medusa with the DMA
633 * rest data register. So 'atari_read_overruns' is currently set
634 * to 4 to avoid having transfers that aren't a multiple of 4. If
635 * the rest data bug is fixed, this can be lowered to 1.
636 */
637 atari_read_overruns = 4;
Roman Zippelc28bda22007-05-01 22:32:36 +0200638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639#endif /*REAL_DMA*/
Roman Zippelc28bda22007-05-01 22:32:36 +0200640 } else { /* ! IS_A_TT */
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* Nothing to do for the interrupt: the ST-DMA is initialized
643 * already by atari_init_INTS()
644 */
645
646#ifdef REAL_DMA
647 atari_dma_residual = 0;
648 atari_dma_active = 0;
649 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
650 : 0xff000000);
651#endif
652 }
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 called = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +0200655 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200658static int atari_scsi_release(struct Scsi_Host *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
660 if (IS_A_TT())
Jeff Garzik1e641662007-11-11 19:52:05 -0500661 free_irq(IRQ_TT_MFP_SCSI, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (atari_dma_buffer)
Roman Zippelc28bda22007-05-01 22:32:36 +0200663 atari_stram_free(atari_dma_buffer);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200664 NCR5380_exit(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 return 1;
666}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100668#ifndef MODULE
669static int __init atari_scsi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 /* Format of atascsi parameter is:
672 * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
673 * Defaults depend on TT or Falcon, hostid determined at run time.
674 * Negative values mean don't change.
675 */
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100676 int ints[6];
677
678 get_options(str, ARRAY_SIZE(ints), ints);
Roman Zippelc28bda22007-05-01 22:32:36 +0200679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (ints[0] < 1) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200681 printk("atari_scsi_setup: no arguments!\n");
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684
685 if (ints[0] >= 1) {
686 if (ints[1] > 0)
687 /* no limits on this, just > 0 */
688 setup_can_queue = ints[1];
689 }
690 if (ints[0] >= 2) {
691 if (ints[2] > 0)
692 setup_cmd_per_lun = ints[2];
693 }
694 if (ints[0] >= 3) {
695 if (ints[3] >= 0) {
696 setup_sg_tablesize = ints[3];
697 /* Must be <= SG_ALL (255) */
698 if (setup_sg_tablesize > SG_ALL)
699 setup_sg_tablesize = SG_ALL;
700 }
701 }
702 if (ints[0] >= 4) {
703 /* Must be between 0 and 7 */
704 if (ints[4] >= 0 && ints[4] <= 7)
705 setup_hostid = ints[4];
706 else if (ints[4] > 7)
Roman Zippelc28bda22007-05-01 22:32:36 +0200707 printk("atari_scsi_setup: invalid host ID %d !\n", ints[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709#ifdef SUPPORT_TAGS
710 if (ints[0] >= 5) {
711 if (ints[5] >= 0)
712 setup_use_tagged_queuing = !!ints[5];
713 }
714#endif
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100715
716 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100719__setup("atascsi=", atari_scsi_setup);
720#endif /* !MODULE */
721
Roman Zippelc28bda22007-05-01 22:32:36 +0200722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
724static void __init atari_scsi_reset_boot(void)
725{
726 unsigned long end;
Roman Zippelc28bda22007-05-01 22:32:36 +0200727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /*
729 * Do a SCSI reset to clean up the bus during initialization. No messing
730 * with the queues, interrupts, or locks necessary here.
731 */
732
Roman Zippelc28bda22007-05-01 22:32:36 +0200733 printk("Atari SCSI: resetting the SCSI bus...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /* get in phase */
Roman Zippelc28bda22007-05-01 22:32:36 +0200736 NCR5380_write(TARGET_COMMAND_REG,
737 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* assert RST */
Roman Zippelc28bda22007-05-01 22:32:36 +0200740 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* The min. reset hold time is 25us, so 40us should be enough */
Roman Zippelc28bda22007-05-01 22:32:36 +0200742 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* reset RST and interrupt */
Roman Zippelc28bda22007-05-01 22:32:36 +0200744 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
745 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 end = jiffies + AFTER_RESET_DELAY;
748 while (time_before(jiffies, end))
749 barrier();
750
Roman Zippelc28bda22007-05-01 22:32:36 +0200751 printk(" done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753#endif
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755#if defined(REAL_DMA)
756
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200757static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
758 void *data, unsigned long count,
759 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Roman Zippelc28bda22007-05-01 22:32:36 +0200761 unsigned long addr = virt_to_phys(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Finn Thaind65e6342014-03-18 11:42:20 +1100763 dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 "dir = %d\n", instance->host_no, data, addr, count, dir);
765
766 if (!IS_A_TT() && !STRAM_ADDR(addr)) {
767 /* If we have a non-DMAable address on a Falcon, use the dribble
768 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
769 * handler to copy data from the dribble buffer to the originally
770 * wanted address.
771 */
772 if (dir)
Roman Zippelc28bda22007-05-01 22:32:36 +0200773 memcpy(atari_dma_buffer, data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 else
775 atari_dma_orig_addr = data;
776 addr = atari_dma_phys_buffer;
777 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 atari_dma_startaddr = addr; /* Needed for calculating residual later. */
Roman Zippelc28bda22007-05-01 22:32:36 +0200780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 /* Cache cleanup stuff: On writes, push any dirty cache out before sending
782 * it to the peripheral. (Must be done before DMA setup, since at least
783 * the ST-DMA begins to fill internal buffers right after setup. For
784 * reads, invalidate any cache, may be altered after DMA without CPU
785 * knowledge.
Roman Zippelc28bda22007-05-01 22:32:36 +0200786 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 * ++roman: For the Medusa, there's no need at all for that cache stuff,
788 * because the hardware does bus snooping (fine!).
789 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200790 dma_cache_maintenance(addr, count, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 if (count == 0)
793 printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
794
795 if (IS_A_TT()) {
796 tt_scsi_dma.dma_ctrl = dir;
Roman Zippelc28bda22007-05-01 22:32:36 +0200797 SCSI_DMA_WRITE_P(dma_addr, addr);
798 SCSI_DMA_WRITE_P(dma_cnt, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 tt_scsi_dma.dma_ctrl = dir | 2;
Roman Zippelc28bda22007-05-01 22:32:36 +0200800 } else { /* ! IS_A_TT */
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 /* set address */
Roman Zippelc28bda22007-05-01 22:32:36 +0200803 SCSI_DMA_SETADR(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 /* toggle direction bit to clear FIFO and set DMA direction */
806 dir <<= 8;
807 st_dma.dma_mode_status = 0x90 | dir;
808 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
809 st_dma.dma_mode_status = 0x90 | dir;
810 udelay(40);
811 /* On writes, round up the transfer length to the next multiple of 512
812 * (see also comment at atari_dma_xfer_len()). */
813 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
814 udelay(40);
815 st_dma.dma_mode_status = 0x10 | dir;
816 udelay(40);
817 /* need not restore value of dir, only boolean value is tested */
818 atari_dma_active = 1;
819 }
820
Roman Zippelc28bda22007-05-01 22:32:36 +0200821 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822}
823
824
Roman Zippelc28bda22007-05-01 22:32:36 +0200825static long atari_scsi_dma_residual(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Roman Zippelc28bda22007-05-01 22:32:36 +0200827 return atari_dma_residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
830
831#define CMD_SURELY_BLOCK_MODE 0
832#define CMD_SURELY_BYTE_MODE 1
833#define CMD_MODE_UNKNOWN 2
834
Finn Thain710ddd02014-11-12 16:12:02 +1100835static int falcon_classify_cmd(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 unsigned char opcode = cmd->cmnd[0];
Roman Zippelc28bda22007-05-01 22:32:36 +0200838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
Roman Zippelc28bda22007-05-01 22:32:36 +0200840 opcode == READ_BUFFER)
841 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 else if (opcode == READ_6 || opcode == READ_10 ||
843 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
844 opcode == RECOVER_BUFFERED_DATA) {
845 /* In case of a sequential-access target (tape), special care is
846 * needed here: The transfer is block-mode only if the 'fixed' bit is
847 * set! */
848 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
Roman Zippelc28bda22007-05-01 22:32:36 +0200849 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 else
Roman Zippelc28bda22007-05-01 22:32:36 +0200851 return CMD_SURELY_BLOCK_MODE;
852 } else
853 return CMD_MODE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856
857/* This function calculates the number of bytes that can be transferred via
858 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
859 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
860 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
861 * possible on the Falcon, since that would require to program the DMA for
862 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
863 * the overrun problem, so this question is academic :-)
864 */
865
Roman Zippelc28bda22007-05-01 22:32:36 +0200866static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
Finn Thain710ddd02014-11-12 16:12:02 +1100867 struct scsi_cmnd *cmd, int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
869 unsigned long possible_len, limit;
Adrian Bunk29c8a242008-10-13 21:58:59 +0200870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (IS_A_TT())
872 /* TT SCSI DMA can transfer arbitrary #bytes */
Roman Zippelc28bda22007-05-01 22:32:36 +0200873 return wanted_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
876 * 255*512 bytes, but this should be enough)
877 *
878 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
879 * that return a number of bytes which cannot be known beforehand. In this
880 * case, the given transfer length is an "allocation length". Now it
881 * can happen that this allocation length is a multiple of 512 bytes and
882 * the DMA is used. But if not n*512 bytes really arrive, some input data
883 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
884 * between commands that do block transfers and those that do byte
885 * transfers. But this isn't easy... there are lots of vendor specific
886 * commands, and the user can issue any command via the
887 * SCSI_IOCTL_SEND_COMMAND.
888 *
889 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
890 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
891 * and 3), the thing to do is obvious: allow any number of blocks via DMA
892 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
893 * the transfer (allocation) length is < 1024, hoping that no cmd. not
894 * explicitly known as byte mode have such big allocation lengths...
895 * BTW, all the discussion above applies only to reads. DMA writes are
896 * unproblematic anyways, since the targets aborts the transfer after
897 * receiving a sufficient number of bytes.
898 *
899 * Another point: If the transfer is from/to an non-ST-RAM address, we
900 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
901 */
902
903 if (write_flag) {
904 /* Write operation can always use the DMA, but the transfer size must
905 * be rounded up to the next multiple of 512 (atari_dma_setup() does
906 * this).
907 */
908 possible_len = wanted_len;
Roman Zippelc28bda22007-05-01 22:32:36 +0200909 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 /* Read operations: if the wanted transfer length is not a multiple of
911 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
912 * (no interrupt on DMA finished!)
913 */
914 if (wanted_len & 0x1ff)
915 possible_len = 0;
916 else {
917 /* Now classify the command (see above) and decide whether it is
918 * allowed to do DMA at all */
Roman Zippelc28bda22007-05-01 22:32:36 +0200919 switch (falcon_classify_cmd(cmd)) {
920 case CMD_SURELY_BLOCK_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 possible_len = wanted_len;
922 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200923 case CMD_SURELY_BYTE_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 possible_len = 0; /* DMA prohibited */
925 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200926 case CMD_MODE_UNKNOWN:
927 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 /* For unknown commands assume block transfers if the transfer
929 * size/allocation length is >= 1024 */
930 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
931 break;
932 }
933 }
934 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200935
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 /* Last step: apply the hard limit on DMA transfers */
Roman Zippelc28bda22007-05-01 22:32:36 +0200937 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 STRAM_BUFFER_SIZE : 255*512;
939 if (possible_len > limit)
940 possible_len = limit;
941
942 if (possible_len != wanted_len)
Finn Thaind65e6342014-03-18 11:42:20 +1100943 dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 "instead of %ld\n", possible_len, wanted_len);
945
Roman Zippelc28bda22007-05-01 22:32:36 +0200946 return possible_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947}
948
949
950#endif /* REAL_DMA */
951
952
953/* NCR5380 register access functions
954 *
955 * There are separate functions for TT and Falcon, because the access
956 * methods are quite different. The calling macros NCR5380_read and
957 * NCR5380_write call these functions via function pointers.
958 */
959
Roman Zippelc28bda22007-05-01 22:32:36 +0200960static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Roman Zippelc28bda22007-05-01 22:32:36 +0200962 return tt_scsi_regp[reg * 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
Roman Zippelc28bda22007-05-01 22:32:36 +0200965static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
967 tt_scsi_regp[reg * 2] = value;
968}
969
Roman Zippelc28bda22007-05-01 22:32:36 +0200970static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
972 dma_wd.dma_mode_status= (u_short)(0x88 + reg);
Roman Zippelc28bda22007-05-01 22:32:36 +0200973 return (u_char)dma_wd.fdc_acces_seccount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974}
975
Roman Zippelc28bda22007-05-01 22:32:36 +0200976static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 dma_wd.dma_mode_status = (u_short)(0x88 + reg);
979 dma_wd.fdc_acces_seccount = (u_short)value;
980}
981
982
983#include "atari_NCR5380.c"
984
Finn Thain4d3d2a52014-11-12 16:11:52 +1100985static int atari_scsi_bus_reset(struct scsi_cmnd *cmd)
986{
987 int rv;
988 struct NCR5380_hostdata *hostdata = shost_priv(cmd->device->host);
989
990 /* For doing the reset, SCSI interrupts must be disabled first,
991 * since the 5380 raises its IRQ line while _RST is active and we
992 * can't disable interrupts completely, since we need the timer.
993 */
994 /* And abort a maybe active DMA transfer */
995 if (IS_A_TT()) {
996 atari_turnoff_irq(IRQ_TT_MFP_SCSI);
997#ifdef REAL_DMA
998 tt_scsi_dma.dma_ctrl = 0;
999#endif
1000 } else {
1001 atari_turnoff_irq(IRQ_MFP_FSCSI);
1002#ifdef REAL_DMA
1003 st_dma.dma_mode_status = 0x90;
1004 atari_dma_active = 0;
1005 atari_dma_orig_addr = NULL;
1006#endif
1007 }
1008
1009 rv = NCR5380_bus_reset(cmd);
1010
1011 if (IS_A_TT())
1012 atari_turnon_irq(IRQ_TT_MFP_SCSI);
1013 else
1014 atari_turnon_irq(IRQ_MFP_FSCSI);
1015
1016 if (rv == SUCCESS)
1017 falcon_release_lock_if_possible(hostdata);
1018
1019 return rv;
1020}
1021
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +01001022static struct scsi_host_template driver_template = {
Al Virod89537e2013-03-31 13:24:44 -04001023 .show_info = atari_scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 .name = "Atari native SCSI",
1025 .detect = atari_scsi_detect,
1026 .release = atari_scsi_release,
1027 .info = atari_scsi_info,
1028 .queuecommand = atari_scsi_queue_command,
1029 .eh_abort_handler = atari_scsi_abort,
1030 .eh_bus_reset_handler = atari_scsi_bus_reset,
1031 .can_queue = 0, /* initialized at run-time */
1032 .this_id = 0, /* initialized at run-time */
1033 .sg_tablesize = 0, /* initialized at run-time */
1034 .cmd_per_lun = 0, /* initialized at run-time */
1035 .use_clustering = DISABLE_CLUSTERING
1036};
1037
1038
1039#include "scsi_module.c"
1040
1041MODULE_LICENSE("GPL");