blob: 48fabebdbbb00601622698fed858b56b15bd339f [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);
187static void 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
476static int falcon_got_lock = 0;
477static DECLARE_WAIT_QUEUE_HEAD(falcon_fairness_wait);
478static int falcon_trying_lock = 0;
479static DECLARE_WAIT_QUEUE_HEAD(falcon_try_wait);
480static int falcon_dont_release = 0;
481
482/* This function releases the lock on the DMA chip if there is no
483 * connected command and the disconnected queue is empty. On
484 * releasing, instances of falcon_get_lock are awoken, that put
485 * themselves to sleep for fairness. They can now try to get the lock
486 * again (but others waiting longer more probably will win).
487 */
488
Roman Zippelc28bda22007-05-01 22:32:36 +0200489static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
491 unsigned long flags;
Roman Zippelc28bda22007-05-01 22:32:36 +0200492
493 if (IS_A_TT())
494 return;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 local_irq_save(flags);
497
Roman Zippelc28bda22007-05-01 22:32:36 +0200498 if (falcon_got_lock && !hostdata->disconnected_queue &&
499 !hostdata->issue_queue && !hostdata->connected) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
501 if (falcon_dont_release) {
502#if 0
503 printk("WARNING: Lock release not allowed. Ignored\n");
504#endif
505 local_irq_restore(flags);
506 return;
507 }
508 falcon_got_lock = 0;
509 stdma_release();
Roman Zippelc28bda22007-05-01 22:32:36 +0200510 wake_up(&falcon_fairness_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512
513 local_irq_restore(flags);
514}
515
516/* This function manages the locking of the ST-DMA.
517 * If the DMA isn't locked already for SCSI, it tries to lock it by
518 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
519 * there are other drivers waiting for the chip, we do not issue the
520 * command immediately but wait on 'falcon_fairness_queue'. We will be
521 * waked up when the DMA is unlocked by some SCSI interrupt. After that
522 * we try to get the lock again.
523 * But we must be prepared that more than one instance of
524 * falcon_get_lock() is waiting on the fairness queue. They should not
525 * try all at once to call stdma_lock(), one is enough! For that, the
526 * first one sets 'falcon_trying_lock', others that see that variable
527 * set wait on the queue 'falcon_try_wait'.
528 * Complicated, complicated.... Sigh...
529 */
530
Roman Zippelc28bda22007-05-01 22:32:36 +0200531static void falcon_get_lock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 unsigned long flags;
534
Roman Zippelc28bda22007-05-01 22:32:36 +0200535 if (IS_A_TT())
536 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 local_irq_save(flags);
539
Arnd Bergmanneff9cf82014-03-01 20:51:03 +1300540 wait_event_cmd(falcon_fairness_wait,
541 in_interrupt() || !falcon_got_lock || !stdma_others_waiting(),
542 local_irq_restore(flags),
543 local_irq_save(flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 while (!falcon_got_lock) {
Michael Schmitzfb810d12007-05-01 22:32:35 +0200546 if (in_irq())
Roman Zippelc28bda22007-05-01 22:32:36 +0200547 panic("Falcon SCSI hasn't ST-DMA lock in interrupt");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (!falcon_trying_lock) {
549 falcon_trying_lock = 1;
550 stdma_lock(scsi_falcon_intr, NULL);
551 falcon_got_lock = 1;
552 falcon_trying_lock = 0;
Roman Zippelc28bda22007-05-01 22:32:36 +0200553 wake_up(&falcon_try_wait);
554 } else {
Arnd Bergmanneff9cf82014-03-01 20:51:03 +1300555 wait_event_cmd(falcon_try_wait,
556 falcon_got_lock && !falcon_trying_lock,
557 local_irq_restore(flags),
558 local_irq_save(flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 local_irq_restore(flags);
563 if (!falcon_got_lock)
564 panic("Falcon SCSI: someone stole the lock :-(\n");
565}
566
567
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200568static int __init atari_scsi_detect(struct scsi_host_template *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 static int called = 0;
571 struct Scsi_Host *instance;
572
573 if (!MACH_IS_ATARI ||
574 (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) ||
575 called)
Roman Zippelc28bda22007-05-01 22:32:36 +0200576 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 host->proc_name = "Atari";
579
580 atari_scsi_reg_read = IS_A_TT() ? atari_scsi_tt_reg_read :
581 atari_scsi_falcon_reg_read;
582 atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write :
583 atari_scsi_falcon_reg_write;
584
585 /* setup variables */
586 host->can_queue =
587 (setup_can_queue > 0) ? setup_can_queue :
588 IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE;
589 host->cmd_per_lun =
590 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun :
591 IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN;
592 /* Force sg_tablesize to 0 on a Falcon! */
593 host->sg_tablesize =
594 !IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE :
595 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE;
596
597 if (setup_hostid >= 0)
598 host->this_id = setup_hostid;
599 else {
600 /* use 7 as default */
601 host->this_id = 7;
602 /* Test if a host id is set in the NVRam */
603 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
604 unsigned char b = nvram_read_byte( 14 );
605 /* Arbitration enabled? (for TOS) If yes, use configured host ID */
606 if (b & 0x80)
607 host->this_id = b & 7;
608 }
609 }
610
611#ifdef SUPPORT_TAGS
612 if (setup_use_tagged_queuing < 0)
Finn Thaind572f65f2014-11-12 16:12:00 +1100613 setup_use_tagged_queuing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
615#ifdef REAL_DMA
616 /* If running on a Falcon and if there's TT-Ram (i.e., more than one
617 * memory block, since there's always ST-Ram in a Falcon), then allocate a
618 * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative
619 * Ram.
620 */
621 if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
622 !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) {
623 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
624 if (!atari_dma_buffer) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200625 printk(KERN_ERR "atari_scsi_detect: can't allocate ST-RAM "
626 "double buffer\n");
627 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Michael Schmitza19f8162014-03-31 21:06:08 +1300629 atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 atari_dma_orig_addr = 0;
631 }
632#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200633 instance = scsi_register(host, sizeof(struct NCR5380_hostdata));
634 if (instance == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 atari_stram_free(atari_dma_buffer);
636 atari_dma_buffer = 0;
637 return 0;
638 }
639 atari_scsi_host = instance;
Roman Zippelc28bda22007-05-01 22:32:36 +0200640 /*
641 * Set irq to 0, to avoid that the mid-level code disables our interrupt
642 * during queue_command calls. This is completely unnecessary, and even
643 * worse causes bad problems on the Falcon, where the int is shared with
644 * IDE and floppy!
645 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 instance->irq = 0;
647
648#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
649 atari_scsi_reset_boot();
650#endif
Roman Zippelc28bda22007-05-01 22:32:36 +0200651 NCR5380_init(instance, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 if (IS_A_TT()) {
654
655 /* This int is actually "pseudo-slow", i.e. it acts like a slow
656 * interrupt after having cleared the pending flag for the DMA
657 * interrupt. */
658 if (request_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW,
Jeff Garzik1e641662007-11-11 19:52:05 -0500659 "SCSI NCR5380", instance)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting",IRQ_TT_MFP_SCSI);
661 scsi_unregister(atari_scsi_host);
662 atari_stram_free(atari_dma_buffer);
663 atari_dma_buffer = 0;
664 return 0;
665 }
666 tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
667#ifdef REAL_DMA
668 tt_scsi_dma.dma_ctrl = 0;
669 atari_dma_residual = 0;
Adrian Bunk29c8a242008-10-13 21:58:59 +0200670
671 if (MACH_IS_MEDUSA) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 /* While the read overruns (described by Drew Eckhardt in
673 * NCR5380.c) never happened on TTs, they do in fact on the Medusa
674 * (This was the cause why SCSI didn't work right for so long
675 * there.) Since handling the overruns slows down a bit, I turned
676 * the #ifdef's into a runtime condition.
677 *
678 * In principle it should be sufficient to do max. 1 byte with
679 * PIO, but there is another problem on the Medusa with the DMA
680 * rest data register. So 'atari_read_overruns' is currently set
681 * to 4 to avoid having transfers that aren't a multiple of 4. If
682 * the rest data bug is fixed, this can be lowered to 1.
683 */
684 atari_read_overruns = 4;
Roman Zippelc28bda22007-05-01 22:32:36 +0200685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686#endif /*REAL_DMA*/
Roman Zippelc28bda22007-05-01 22:32:36 +0200687 } else { /* ! IS_A_TT */
688
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 /* Nothing to do for the interrupt: the ST-DMA is initialized
690 * already by atari_init_INTS()
691 */
692
693#ifdef REAL_DMA
694 atari_dma_residual = 0;
695 atari_dma_active = 0;
696 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
697 : 0xff000000);
698#endif
699 }
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 called = 1;
Roman Zippelc28bda22007-05-01 22:32:36 +0200702 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200705static int atari_scsi_release(struct Scsi_Host *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 if (IS_A_TT())
Jeff Garzik1e641662007-11-11 19:52:05 -0500708 free_irq(IRQ_TT_MFP_SCSI, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (atari_dma_buffer)
Roman Zippelc28bda22007-05-01 22:32:36 +0200710 atari_stram_free(atari_dma_buffer);
Geert Uytterhoeven6323e4f2011-06-13 20:39:15 +0200711 NCR5380_exit(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return 1;
713}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100715#ifndef MODULE
716static int __init atari_scsi_setup(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 /* Format of atascsi parameter is:
719 * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
720 * Defaults depend on TT or Falcon, hostid determined at run time.
721 * Negative values mean don't change.
722 */
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100723 int ints[6];
724
725 get_options(str, ARRAY_SIZE(ints), ints);
Roman Zippelc28bda22007-05-01 22:32:36 +0200726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 if (ints[0] < 1) {
Roman Zippelc28bda22007-05-01 22:32:36 +0200728 printk("atari_scsi_setup: no arguments!\n");
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
731
732 if (ints[0] >= 1) {
733 if (ints[1] > 0)
734 /* no limits on this, just > 0 */
735 setup_can_queue = ints[1];
736 }
737 if (ints[0] >= 2) {
738 if (ints[2] > 0)
739 setup_cmd_per_lun = ints[2];
740 }
741 if (ints[0] >= 3) {
742 if (ints[3] >= 0) {
743 setup_sg_tablesize = ints[3];
744 /* Must be <= SG_ALL (255) */
745 if (setup_sg_tablesize > SG_ALL)
746 setup_sg_tablesize = SG_ALL;
747 }
748 }
749 if (ints[0] >= 4) {
750 /* Must be between 0 and 7 */
751 if (ints[4] >= 0 && ints[4] <= 7)
752 setup_hostid = ints[4];
753 else if (ints[4] > 7)
Roman Zippelc28bda22007-05-01 22:32:36 +0200754 printk("atari_scsi_setup: invalid host ID %d !\n", ints[4]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756#ifdef SUPPORT_TAGS
757 if (ints[0] >= 5) {
758 if (ints[5] >= 0)
759 setup_use_tagged_queuing = !!ints[5];
760 }
761#endif
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100762
763 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Geert Uytterhoeven7b54e432012-03-18 11:54:40 +0100766__setup("atascsi=", atari_scsi_setup);
767#endif /* !MODULE */
768
Roman Zippelc28bda22007-05-01 22:32:36 +0200769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770#ifdef CONFIG_ATARI_SCSI_RESET_BOOT
771static void __init atari_scsi_reset_boot(void)
772{
773 unsigned long end;
Roman Zippelc28bda22007-05-01 22:32:36 +0200774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 /*
776 * Do a SCSI reset to clean up the bus during initialization. No messing
777 * with the queues, interrupts, or locks necessary here.
778 */
779
Roman Zippelc28bda22007-05-01 22:32:36 +0200780 printk("Atari SCSI: resetting the SCSI bus...");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /* get in phase */
Roman Zippelc28bda22007-05-01 22:32:36 +0200783 NCR5380_write(TARGET_COMMAND_REG,
784 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 /* assert RST */
Roman Zippelc28bda22007-05-01 22:32:36 +0200787 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 /* The min. reset hold time is 25us, so 40us should be enough */
Roman Zippelc28bda22007-05-01 22:32:36 +0200789 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /* reset RST and interrupt */
Roman Zippelc28bda22007-05-01 22:32:36 +0200791 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
792 NCR5380_read(RESET_PARITY_INTERRUPT_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 end = jiffies + AFTER_RESET_DELAY;
795 while (time_before(jiffies, end))
796 barrier();
797
Roman Zippelc28bda22007-05-01 22:32:36 +0200798 printk(" done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800#endif
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#if defined(REAL_DMA)
803
Geert Uytterhoeven107b5d52011-06-12 20:48:33 +0200804static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
805 void *data, unsigned long count,
806 int dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Roman Zippelc28bda22007-05-01 22:32:36 +0200808 unsigned long addr = virt_to_phys(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Finn Thaind65e6342014-03-18 11:42:20 +1100810 dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 "dir = %d\n", instance->host_no, data, addr, count, dir);
812
813 if (!IS_A_TT() && !STRAM_ADDR(addr)) {
814 /* If we have a non-DMAable address on a Falcon, use the dribble
815 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
816 * handler to copy data from the dribble buffer to the originally
817 * wanted address.
818 */
819 if (dir)
Roman Zippelc28bda22007-05-01 22:32:36 +0200820 memcpy(atari_dma_buffer, data, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 else
822 atari_dma_orig_addr = data;
823 addr = atari_dma_phys_buffer;
824 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200825
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 atari_dma_startaddr = addr; /* Needed for calculating residual later. */
Roman Zippelc28bda22007-05-01 22:32:36 +0200827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Cache cleanup stuff: On writes, push any dirty cache out before sending
829 * it to the peripheral. (Must be done before DMA setup, since at least
830 * the ST-DMA begins to fill internal buffers right after setup. For
831 * reads, invalidate any cache, may be altered after DMA without CPU
832 * knowledge.
Roman Zippelc28bda22007-05-01 22:32:36 +0200833 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 * ++roman: For the Medusa, there's no need at all for that cache stuff,
835 * because the hardware does bus snooping (fine!).
836 */
Roman Zippelc28bda22007-05-01 22:32:36 +0200837 dma_cache_maintenance(addr, count, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 if (count == 0)
840 printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
841
842 if (IS_A_TT()) {
843 tt_scsi_dma.dma_ctrl = dir;
Roman Zippelc28bda22007-05-01 22:32:36 +0200844 SCSI_DMA_WRITE_P(dma_addr, addr);
845 SCSI_DMA_WRITE_P(dma_cnt, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 tt_scsi_dma.dma_ctrl = dir | 2;
Roman Zippelc28bda22007-05-01 22:32:36 +0200847 } else { /* ! IS_A_TT */
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* set address */
Roman Zippelc28bda22007-05-01 22:32:36 +0200850 SCSI_DMA_SETADR(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 /* toggle direction bit to clear FIFO and set DMA direction */
853 dir <<= 8;
854 st_dma.dma_mode_status = 0x90 | dir;
855 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
856 st_dma.dma_mode_status = 0x90 | dir;
857 udelay(40);
858 /* On writes, round up the transfer length to the next multiple of 512
859 * (see also comment at atari_dma_xfer_len()). */
860 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
861 udelay(40);
862 st_dma.dma_mode_status = 0x10 | dir;
863 udelay(40);
864 /* need not restore value of dir, only boolean value is tested */
865 atari_dma_active = 1;
866 }
867
Roman Zippelc28bda22007-05-01 22:32:36 +0200868 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
871
Roman Zippelc28bda22007-05-01 22:32:36 +0200872static long atari_scsi_dma_residual(struct Scsi_Host *instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
Roman Zippelc28bda22007-05-01 22:32:36 +0200874 return atari_dma_residual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877
878#define CMD_SURELY_BLOCK_MODE 0
879#define CMD_SURELY_BYTE_MODE 1
880#define CMD_MODE_UNKNOWN 2
881
Finn Thain710ddd02014-11-12 16:12:02 +1100882static int falcon_classify_cmd(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 unsigned char opcode = cmd->cmnd[0];
Roman Zippelc28bda22007-05-01 22:32:36 +0200885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
Roman Zippelc28bda22007-05-01 22:32:36 +0200887 opcode == READ_BUFFER)
888 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 else if (opcode == READ_6 || opcode == READ_10 ||
890 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
891 opcode == RECOVER_BUFFERED_DATA) {
892 /* In case of a sequential-access target (tape), special care is
893 * needed here: The transfer is block-mode only if the 'fixed' bit is
894 * set! */
895 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
Roman Zippelc28bda22007-05-01 22:32:36 +0200896 return CMD_SURELY_BYTE_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 else
Roman Zippelc28bda22007-05-01 22:32:36 +0200898 return CMD_SURELY_BLOCK_MODE;
899 } else
900 return CMD_MODE_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903
904/* This function calculates the number of bytes that can be transferred via
905 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
906 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
907 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
908 * possible on the Falcon, since that would require to program the DMA for
909 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
910 * the overrun problem, so this question is academic :-)
911 */
912
Roman Zippelc28bda22007-05-01 22:32:36 +0200913static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
Finn Thain710ddd02014-11-12 16:12:02 +1100914 struct scsi_cmnd *cmd, int write_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915{
916 unsigned long possible_len, limit;
Adrian Bunk29c8a242008-10-13 21:58:59 +0200917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (IS_A_TT())
919 /* TT SCSI DMA can transfer arbitrary #bytes */
Roman Zippelc28bda22007-05-01 22:32:36 +0200920 return wanted_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
923 * 255*512 bytes, but this should be enough)
924 *
925 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
926 * that return a number of bytes which cannot be known beforehand. In this
927 * case, the given transfer length is an "allocation length". Now it
928 * can happen that this allocation length is a multiple of 512 bytes and
929 * the DMA is used. But if not n*512 bytes really arrive, some input data
930 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
931 * between commands that do block transfers and those that do byte
932 * transfers. But this isn't easy... there are lots of vendor specific
933 * commands, and the user can issue any command via the
934 * SCSI_IOCTL_SEND_COMMAND.
935 *
936 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
937 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
938 * and 3), the thing to do is obvious: allow any number of blocks via DMA
939 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
940 * the transfer (allocation) length is < 1024, hoping that no cmd. not
941 * explicitly known as byte mode have such big allocation lengths...
942 * BTW, all the discussion above applies only to reads. DMA writes are
943 * unproblematic anyways, since the targets aborts the transfer after
944 * receiving a sufficient number of bytes.
945 *
946 * Another point: If the transfer is from/to an non-ST-RAM address, we
947 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
948 */
949
950 if (write_flag) {
951 /* Write operation can always use the DMA, but the transfer size must
952 * be rounded up to the next multiple of 512 (atari_dma_setup() does
953 * this).
954 */
955 possible_len = wanted_len;
Roman Zippelc28bda22007-05-01 22:32:36 +0200956 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 /* Read operations: if the wanted transfer length is not a multiple of
958 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
959 * (no interrupt on DMA finished!)
960 */
961 if (wanted_len & 0x1ff)
962 possible_len = 0;
963 else {
964 /* Now classify the command (see above) and decide whether it is
965 * allowed to do DMA at all */
Roman Zippelc28bda22007-05-01 22:32:36 +0200966 switch (falcon_classify_cmd(cmd)) {
967 case CMD_SURELY_BLOCK_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 possible_len = wanted_len;
969 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200970 case CMD_SURELY_BYTE_MODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 possible_len = 0; /* DMA prohibited */
972 break;
Roman Zippelc28bda22007-05-01 22:32:36 +0200973 case CMD_MODE_UNKNOWN:
974 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /* For unknown commands assume block transfers if the transfer
976 * size/allocation length is >= 1024 */
977 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
978 break;
979 }
980 }
981 }
Roman Zippelc28bda22007-05-01 22:32:36 +0200982
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 /* Last step: apply the hard limit on DMA transfers */
Roman Zippelc28bda22007-05-01 22:32:36 +0200984 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 STRAM_BUFFER_SIZE : 255*512;
986 if (possible_len > limit)
987 possible_len = limit;
988
989 if (possible_len != wanted_len)
Finn Thaind65e6342014-03-18 11:42:20 +1100990 dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 "instead of %ld\n", possible_len, wanted_len);
992
Roman Zippelc28bda22007-05-01 22:32:36 +0200993 return possible_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
995
996
997#endif /* REAL_DMA */
998
999
1000/* NCR5380 register access functions
1001 *
1002 * There are separate functions for TT and Falcon, because the access
1003 * methods are quite different. The calling macros NCR5380_read and
1004 * NCR5380_write call these functions via function pointers.
1005 */
1006
Roman Zippelc28bda22007-05-01 22:32:36 +02001007static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Roman Zippelc28bda22007-05-01 22:32:36 +02001009 return tt_scsi_regp[reg * 2];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
Roman Zippelc28bda22007-05-01 22:32:36 +02001012static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 tt_scsi_regp[reg * 2] = value;
1015}
1016
Roman Zippelc28bda22007-05-01 22:32:36 +02001017static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 dma_wd.dma_mode_status= (u_short)(0x88 + reg);
Roman Zippelc28bda22007-05-01 22:32:36 +02001020 return (u_char)dma_wd.fdc_acces_seccount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Roman Zippelc28bda22007-05-01 22:32:36 +02001023static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 dma_wd.dma_mode_status = (u_short)(0x88 + reg);
1026 dma_wd.fdc_acces_seccount = (u_short)value;
1027}
1028
1029
1030#include "atari_NCR5380.c"
1031
Finn Thain4d3d2a52014-11-12 16:11:52 +11001032static int atari_scsi_bus_reset(struct scsi_cmnd *cmd)
1033{
1034 int rv;
1035 struct NCR5380_hostdata *hostdata = shost_priv(cmd->device->host);
1036
1037 /* For doing the reset, SCSI interrupts must be disabled first,
1038 * since the 5380 raises its IRQ line while _RST is active and we
1039 * can't disable interrupts completely, since we need the timer.
1040 */
1041 /* And abort a maybe active DMA transfer */
1042 if (IS_A_TT()) {
1043 atari_turnoff_irq(IRQ_TT_MFP_SCSI);
1044#ifdef REAL_DMA
1045 tt_scsi_dma.dma_ctrl = 0;
1046#endif
1047 } else {
1048 atari_turnoff_irq(IRQ_MFP_FSCSI);
1049#ifdef REAL_DMA
1050 st_dma.dma_mode_status = 0x90;
1051 atari_dma_active = 0;
1052 atari_dma_orig_addr = NULL;
1053#endif
1054 }
1055
1056 rv = NCR5380_bus_reset(cmd);
1057
1058 if (IS_A_TT())
1059 atari_turnon_irq(IRQ_TT_MFP_SCSI);
1060 else
1061 atari_turnon_irq(IRQ_MFP_FSCSI);
1062
1063 if (rv == SUCCESS)
1064 falcon_release_lock_if_possible(hostdata);
1065
1066 return rv;
1067}
1068
Christoph Hellwigd0be4a7d2005-10-31 18:31:40 +01001069static struct scsi_host_template driver_template = {
Al Virod89537e2013-03-31 13:24:44 -04001070 .show_info = atari_scsi_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 .name = "Atari native SCSI",
1072 .detect = atari_scsi_detect,
1073 .release = atari_scsi_release,
1074 .info = atari_scsi_info,
1075 .queuecommand = atari_scsi_queue_command,
1076 .eh_abort_handler = atari_scsi_abort,
1077 .eh_bus_reset_handler = atari_scsi_bus_reset,
1078 .can_queue = 0, /* initialized at run-time */
1079 .this_id = 0, /* initialized at run-time */
1080 .sg_tablesize = 0, /* initialized at run-time */
1081 .cmd_per_lun = 0, /* initialized at run-time */
1082 .use_clustering = DISABLE_CLUSTERING
1083};
1084
1085
1086#include "scsi_module.c"
1087
1088MODULE_LICENSE("GPL");