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