atari_scsi.c 30.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/*
 * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
 *
 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
 *
 *   Loosely based on the work of Robert De Vries' team and added:
 *    - working real DMA
 *    - Falcon support (untested yet!)   ++bjoern fixed and now it works
 *    - lots of extensions and bug fixes.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive
 * for more details.
 *
 */


/**************************************************************************/
/*                                                                        */
/* Notes for Falcon SCSI:                                                 */
/* ----------------------                                                 */
/*                                                                        */
/* Since the Falcon SCSI uses the ST-DMA chip, that is shared among       */
/* several device drivers, locking and unlocking the access to this       */
/* chip is required. But locking is not possible from an interrupt,       */
/* since it puts the process to sleep if the lock is not available.       */
/* This prevents "late" locking of the DMA chip, i.e. locking it just     */
/* before using it, since in case of disconnection-reconnection           */
/* commands, the DMA is started from the reselection interrupt.           */
/*                                                                        */
/* Two possible schemes for ST-DMA-locking would be:                      */
/*  1) The lock is taken for each command separately and disconnecting    */
/*     is forbidden (i.e. can_queue = 1).                                 */
/*  2) The DMA chip is locked when the first command comes in and         */
/*     released when the last command is finished and all queues are      */
/*     empty.                                                             */
/* The first alternative would result in bad performance, since the       */
/* interleaving of commands would not be used. The second is unfair to    */
/* other drivers using the ST-DMA, because the queues will seldom be      */
/* totally empty if there is a lot of disk traffic.                       */
/*                                                                        */
/* For this reasons I decided to employ a more elaborate scheme:          */
/*  - First, we give up the lock every time we can (for fairness), this    */
/*    means every time a command finishes and there are no other commands */
/*    on the disconnected queue.                                          */
/*  - If there are others waiting to lock the DMA chip, we stop           */
/*    issuing commands, i.e. moving them onto the issue queue.           */
/*    Because of that, the disconnected queue will run empty in a         */
/*    while. Instead we go to sleep on a 'fairness_queue'.                */
/*  - If the lock is released, all processes waiting on the fairness      */
/*    queue will be woken. The first of them tries to re-lock the DMA,     */
/*    the others wait for the first to finish this task. After that,      */
/*    they can all run on and do their commands...                        */
/* This sounds complicated (and it is it :-(), but it seems to be a       */
/* good compromise between fairness and performance: As long as no one     */
/* else wants to work with the ST-DMA chip, SCSI can go along as          */
/* usual. If now someone else comes, this behaviour is changed to a       */
/* "fairness mode": just already initiated commands are finished and      */
/* then the lock is released. The other one waiting will probably win     */
/* the race for locking the DMA, since it was waiting for longer. And     */
/* after it has finished, SCSI can go ahead again. Finally: I hope I      */
/* have not produced any deadlock possibilities!                          */
/*                                                                        */
/**************************************************************************/


#include <linux/module.h>
#include <linux/types.h>
#include <linux/blkdev.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/nvram.h>
#include <linux/bitops.h>
74
#include <linux/wait.h>
75
#include <linux/platform_device.h>
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83

#include <asm/setup.h>
#include <asm/atarihw.h>
#include <asm/atariints.h>
#include <asm/atari_stdma.h>
#include <asm/atari_stram.h>
#include <asm/io.h>

84 85
#include <scsi/scsi_host.h>

86 87
#define DMA_MIN_SIZE                    32

F
Finn Thain 已提交
88 89 90 91 92 93 94 95 96 97 98
/* Definitions for the core NCR5380 driver. */

#define NCR5380_implementation_fields   /* none */

#define NCR5380_read(reg)               atari_scsi_reg_read(reg)
#define NCR5380_write(reg, value)       atari_scsi_reg_write(reg, value)

#define NCR5380_queue_command           atari_scsi_queue_command
#define NCR5380_abort                   atari_scsi_abort
#define NCR5380_info                    atari_scsi_info

99
#define NCR5380_dma_recv_setup(instance, data, count) \
F
Finn Thain 已提交
100
        atari_scsi_dma_setup(instance, data, count, 0)
101
#define NCR5380_dma_send_setup(instance, data, count) \
F
Finn Thain 已提交
102 103 104 105 106 107
        atari_scsi_dma_setup(instance, data, count, 1)
#define NCR5380_dma_residual(instance) \
        atari_scsi_dma_residual(instance)
#define NCR5380_dma_xfer_len(instance, cmd, phase) \
        atari_dma_xfer_len(cmd->SCp.this_residual, cmd, !((phase) & SR_IO))

108
#define NCR5380_acquire_dma_irq(instance)      falcon_get_lock(instance)
109 110
#define NCR5380_release_dma_irq(instance)      falcon_release_lock()

111
#include "NCR5380.h"
L
Linus Torvalds 已提交
112

F
Finn Thain 已提交
113

L
Linus Torvalds 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#define	IS_A_TT()	ATARIHW_PRESENT(TT_SCSI)

#define	SCSI_DMA_WRITE_P(elt,val)				\
	do {							\
		unsigned long v = val;				\
		tt_scsi_dma.elt##_lo = v & 0xff;		\
		v >>= 8;					\
		tt_scsi_dma.elt##_lmd = v & 0xff;		\
		v >>= 8;					\
		tt_scsi_dma.elt##_hmd = v & 0xff;		\
		v >>= 8;					\
		tt_scsi_dma.elt##_hi = v & 0xff;		\
	} while(0)

#define	SCSI_DMA_READ_P(elt)					\
	(((((((unsigned long)tt_scsi_dma.elt##_hi << 8) |	\
	     (unsigned long)tt_scsi_dma.elt##_hmd) << 8) |	\
	   (unsigned long)tt_scsi_dma.elt##_lmd) << 8) |	\
	 (unsigned long)tt_scsi_dma.elt##_lo)


static inline void SCSI_DMA_SETADR(unsigned long adr)
{
	st_dma.dma_lo = (unsigned char)adr;
	MFPDELAY();
	adr >>= 8;
	st_dma.dma_md = (unsigned char)adr;
	MFPDELAY();
	adr >>= 8;
	st_dma.dma_hi = (unsigned char)adr;
	MFPDELAY();
}

static inline unsigned long SCSI_DMA_GETADR(void)
{
	unsigned long adr;
	adr = st_dma.dma_lo;
	MFPDELAY();
	adr |= (st_dma.dma_md & 0xff) << 8;
	MFPDELAY();
	adr |= (st_dma.dma_hi & 0xff) << 16;
	MFPDELAY();
	return adr;
}

159
static void atari_scsi_fetch_restbytes(void);
L
Linus Torvalds 已提交
160

161 162
static unsigned char (*atari_scsi_reg_read)(unsigned char reg);
static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value);
L
Linus Torvalds 已提交
163 164 165 166

static unsigned long	atari_dma_residual, atari_dma_startaddr;
static short		atari_dma_active;
/* pointer to the dribble buffer */
167
static char		*atari_dma_buffer;
L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/* precalculated physical address of the dribble buffer */
static unsigned long	atari_dma_phys_buffer;
/* != 0 tells the Falcon int handler to copy data from the dribble buffer */
static char		*atari_dma_orig_addr;
/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
 * cases where requests to physical contiguous buffers have been merged, this
 * request is <= 4k (one page). So I don't think we have to split transfers
 * just due to this buffer size...
 */
#define	STRAM_BUFFER_SIZE	(4096)
/* mask for address bits that can't be used with the ST-DMA */
static unsigned long	atari_dma_stram_mask;
#define STRAM_ADDR(a)	(((a) & atari_dma_stram_mask) == 0)

static int setup_can_queue = -1;
R
Rusty Russell 已提交
184
module_param(setup_can_queue, int, 0);
L
Linus Torvalds 已提交
185
static int setup_cmd_per_lun = -1;
R
Rusty Russell 已提交
186
module_param(setup_cmd_per_lun, int, 0);
L
Linus Torvalds 已提交
187
static int setup_sg_tablesize = -1;
R
Rusty Russell 已提交
188
module_param(setup_sg_tablesize, int, 0);
L
Linus Torvalds 已提交
189
static int setup_hostid = -1;
R
Rusty Russell 已提交
190
module_param(setup_hostid, int, 0);
191 192
static int setup_toshiba_delay = -1;
module_param(setup_toshiba_delay, int, 0);
L
Linus Torvalds 已提交
193 194


195
static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
L
Linus Torvalds 已提交
196 197
{
	int i;
198
	unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
L
Linus Torvalds 已提交
199 200 201 202 203 204 205

	if (dma_stat & 0x01) {

		/* A bus error happens when DMA-ing from the last page of a
		 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
		 * Check for this case:
		 */
206 207 208

		for (i = 0; i < m68k_num_memory; ++i) {
			end_addr = m68k_memory[i].addr + m68k_memory[i].size;
L
Linus Torvalds 已提交
209
			if (end_addr <= addr && addr <= end_addr + 4)
210
				return 1;
L
Linus Torvalds 已提交
211 212
		}
	}
213
	return 0;
L
Linus Torvalds 已提交
214 215 216 217 218 219 220 221
}


#if 0
/* Dead code... wasn't called anyway :-) and causes some trouble, because at
 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
 * to clear the DMA int pending bit before it allows other level 6 interrupts.
 */
222
static void scsi_dma_buserr(int irq, void *dummy)
L
Linus Torvalds 已提交
223
{
224
	unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
L
Linus Torvalds 已提交
225 226 227

	/* Don't do anything if a NCR interrupt is pending. Probably it's just
	 * masked... */
228
	if (atari_irq_pending(IRQ_TT_MFP_SCSI))
L
Linus Torvalds 已提交
229
		return;
230

L
Linus Torvalds 已提交
231 232 233
	printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
	       SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
	if (dma_stat & 0x80) {
234 235 236
		if (!scsi_dma_is_ignored_buserr(dma_stat))
			printk("SCSI DMA bus error -- bad DMA programming!\n");
	} else {
L
Linus Torvalds 已提交
237 238 239 240 241
		/* Under normal circumstances we never should get to this point,
		 * since both interrupts are triggered simultaneously and the 5380
		 * int has higher priority. When this irq is handled, that DMA
		 * interrupt is cleared. So a warning message is printed here.
		 */
242
		printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
L
Linus Torvalds 已提交
243 244 245 246 247
	}
}
#endif


248
static irqreturn_t scsi_tt_intr(int irq, void *dev)
L
Linus Torvalds 已提交
249
{
250 251
	struct Scsi_Host *instance = dev;
	struct NCR5380_hostdata *hostdata = shost_priv(instance);
L
Linus Torvalds 已提交
252 253 254 255
	int dma_stat;

	dma_stat = tt_scsi_dma.dma_ctrl;

256 257
	dsprintk(NDEBUG_INTR, instance, "NCR5380 interrupt, DMA status = %02x\n",
	         dma_stat & 0xff);
L
Linus Torvalds 已提交
258 259 260 261 262

	/* Look if it was the DMA that has interrupted: First possibility
	 * is that a bus error occurred...
	 */
	if (dma_stat & 0x80) {
263
		if (!scsi_dma_is_ignored_buserr(dma_stat)) {
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
			printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
			       SCSI_DMA_READ_P(dma_addr));
			printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
		}
	}

	/* If the DMA is active but not finished, we have the case
	 * that some other 5380 interrupt occurred within the DMA transfer.
	 * This means we have residual bytes, if the desired end address
	 * is not yet reached. Maybe we have to fetch some bytes from the
	 * rest data register, too. The residual must be calculated from
	 * the address pointer, not the counter register, because only the
	 * addr reg counts bytes not yet written and pending in the rest
	 * data reg!
	 */
	if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
280 281
		atari_dma_residual = hostdata->dma_len -
			(SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
L
Linus Torvalds 已提交
282

F
Finn Thain 已提交
283
		dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
L
Linus Torvalds 已提交
284 285 286 287 288
			   atari_dma_residual);

		if ((signed int)atari_dma_residual < 0)
			atari_dma_residual = 0;
		if ((dma_stat & 1) == 0) {
289 290 291 292
			/*
			 * After read operations, we maybe have to
			 * transport some rest bytes
			 */
L
Linus Torvalds 已提交
293
			atari_scsi_fetch_restbytes();
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
		} else {
			/*
			 * There seems to be a nasty bug in some SCSI-DMA/NCR
			 * combinations: If a target disconnects while a write
			 * operation is going on, the address register of the
			 * DMA may be a few bytes farer than it actually read.
			 * This is probably due to DMA prefetching and a delay
			 * between DMA and NCR.  Experiments showed that the
			 * dma_addr is 9 bytes to high, but this could vary.
			 * The problem is, that the residual is thus calculated
			 * wrong and the next transfer will start behind where
			 * it should.  So we round up the residual to the next
			 * multiple of a sector size, if it isn't already a
			 * multiple and the originally expected transfer size
			 * was.  The latter condition is there to ensure that
			 * the correction is taken only for "real" data
			 * transfers and not for, e.g., the parameters of some
			 * other command.  These shouldn't disconnect anyway.
			 */
L
Linus Torvalds 已提交
313
			if (atari_dma_residual & 0x1ff) {
F
Finn Thain 已提交
314
				dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
L
Linus Torvalds 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
					   "difference %ld bytes\n",
					   512 - (atari_dma_residual & 0x1ff));
				atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
			}
		}
		tt_scsi_dma.dma_ctrl = 0;
	}

	/* If the DMA is finished, fetch the rest bytes and turn it off */
	if (dma_stat & 0x40) {
		atari_dma_residual = 0;
		if ((dma_stat & 1) == 0)
			atari_scsi_fetch_restbytes();
		tt_scsi_dma.dma_ctrl = 0;
	}

331
	NCR5380_intr(irq, dev);
L
Linus Torvalds 已提交
332 333 334 335 336

	return IRQ_HANDLED;
}


337
static irqreturn_t scsi_falcon_intr(int irq, void *dev)
L
Linus Torvalds 已提交
338
{
339 340
	struct Scsi_Host *instance = dev;
	struct NCR5380_hostdata *hostdata = shost_priv(instance);
L
Linus Torvalds 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
	int dma_stat;

	/* Turn off DMA and select sector counter register before
	 * accessing the status register (Atari recommendation!)
	 */
	st_dma.dma_mode_status = 0x90;
	dma_stat = st_dma.dma_mode_status;

	/* Bit 0 indicates some error in the DMA process... don't know
	 * what happened exactly (no further docu).
	 */
	if (!(dma_stat & 0x01)) {
		/* DMA error */
		printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
	}

	/* If the DMA was active, but now bit 1 is not clear, it is some
	 * other 5380 interrupt that finishes the DMA transfer. We have to
	 * calculate the number of residual bytes and give a warning if
	 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
	 */
	if (atari_dma_active && (dma_stat & 0x02)) {
363
		unsigned long transferred;
L
Linus Torvalds 已提交
364 365 366 367 368 369 370 371 372 373 374

		transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
		/* The ST-DMA address is incremented in 2-byte steps, but the
		 * data are written only in 16-byte chunks. If the number of
		 * transferred bytes is not divisible by 16, the remainder is
		 * lost somewhere in outer space.
		 */
		if (transferred & 15)
			printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
			       "ST-DMA fifo\n", transferred & 15);

375
		atari_dma_residual = hostdata->dma_len - transferred;
F
Finn Thain 已提交
376
		dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
L
Linus Torvalds 已提交
377
			   atari_dma_residual);
378
	} else
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386
		atari_dma_residual = 0;
	atari_dma_active = 0;

	if (atari_dma_orig_addr) {
		/* If the dribble buffer was used on a read operation, copy the DMA-ed
		 * data to the original destination address.
		 */
		memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
387
		       hostdata->dma_len - atari_dma_residual);
L
Linus Torvalds 已提交
388 389 390
		atari_dma_orig_addr = NULL;
	}

391 392
	NCR5380_intr(irq, dev);

L
Linus Torvalds 已提交
393 394 395 396
	return IRQ_HANDLED;
}


397
static void atari_scsi_fetch_restbytes(void)
L
Linus Torvalds 已提交
398 399 400 401 402 403 404 405 406 407 408 409
{
	int nr;
	char *src, *dst;
	unsigned long phys_dst;

	/* fetch rest bytes in the DMA register */
	phys_dst = SCSI_DMA_READ_P(dma_addr);
	nr = phys_dst & 3;
	if (nr) {
		/* there are 'nr' bytes left for the last long address
		   before the DMA pointer */
		phys_dst ^= nr;
F
Finn Thain 已提交
410
		dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
L
Linus Torvalds 已提交
411 412 413
			   nr, phys_dst);
		/* The content of the DMA pointer is a physical address!  */
		dst = phys_to_virt(phys_dst);
F
Finn Thain 已提交
414
		dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
L
Linus Torvalds 已提交
415 416 417 418 419 420 421
		for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
			*dst++ = *src++;
	}
}


/* This function releases the lock on the DMA chip if there is no
422
 * connected command and the disconnected queue is empty.
L
Linus Torvalds 已提交
423 424
 */

425
static void falcon_release_lock(void)
L
Linus Torvalds 已提交
426
{
427 428 429
	if (IS_A_TT())
		return;

430
	if (stdma_is_locked_by(scsi_falcon_intr))
L
Linus Torvalds 已提交
431 432 433 434 435 436 437
		stdma_release();
}

/* This function manages the locking of the ST-DMA.
 * If the DMA isn't locked already for SCSI, it tries to lock it by
 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
 * there are other drivers waiting for the chip, we do not issue the
438
 * command immediately but tell the SCSI mid-layer to defer.
L
Linus Torvalds 已提交
439 440
 */

441
static int falcon_get_lock(struct Scsi_Host *instance)
L
Linus Torvalds 已提交
442
{
443
	if (IS_A_TT())
444
		return 1;
L
Linus Torvalds 已提交
445

446
	if (in_interrupt())
447
		return stdma_try_lock(scsi_falcon_intr, instance);
L
Linus Torvalds 已提交
448

449
	stdma_lock(scsi_falcon_intr, instance);
450
	return 1;
L
Linus Torvalds 已提交
451 452
}

453 454
#ifndef MODULE
static int __init atari_scsi_setup(char *str)
L
Linus Torvalds 已提交
455 456 457
{
	/* Format of atascsi parameter is:
	 *   atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
458
	 * Defaults depend on TT or Falcon, determined at run time.
L
Linus Torvalds 已提交
459 460
	 * Negative values mean don't change.
	 */
461
	int ints[8];
462 463

	get_options(str, ARRAY_SIZE(ints), ints);
464

L
Linus Torvalds 已提交
465
	if (ints[0] < 1) {
466
		printk("atari_scsi_setup: no arguments!\n");
467
		return 0;
L
Linus Torvalds 已提交
468
	}
469 470 471 472 473 474 475 476
	if (ints[0] >= 1)
		setup_can_queue = ints[1];
	if (ints[0] >= 2)
		setup_cmd_per_lun = ints[2];
	if (ints[0] >= 3)
		setup_sg_tablesize = ints[3];
	if (ints[0] >= 4)
		setup_hostid = ints[4];
477
	/* ints[5] (use_tagged_queuing) is ignored */
478 479 480
	/* ints[6] (use_pdma) is ignored */
	if (ints[0] >= 7)
		setup_toshiba_delay = ints[7];
481 482

	return 1;
L
Linus Torvalds 已提交
483 484
}

485 486 487
__setup("atascsi=", atari_scsi_setup);
#endif /* !MODULE */

488

489 490 491
static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
					  void *data, unsigned long count,
					  int dir)
L
Linus Torvalds 已提交
492
{
493
	unsigned long addr = virt_to_phys(data);
L
Linus Torvalds 已提交
494

F
Finn Thain 已提交
495
	dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504
		   "dir = %d\n", instance->host_no, data, addr, count, dir);

	if (!IS_A_TT() && !STRAM_ADDR(addr)) {
		/* If we have a non-DMAable address on a Falcon, use the dribble
		 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
		 * handler to copy data from the dribble buffer to the originally
		 * wanted address.
		 */
		if (dir)
505
			memcpy(atari_dma_buffer, data, count);
L
Linus Torvalds 已提交
506 507 508 509
		else
			atari_dma_orig_addr = data;
		addr = atari_dma_phys_buffer;
	}
510

L
Linus Torvalds 已提交
511
	atari_dma_startaddr = addr;	/* Needed for calculating residual later. */
512

L
Linus Torvalds 已提交
513 514 515 516 517
	/* Cache cleanup stuff: On writes, push any dirty cache out before sending
	 * it to the peripheral. (Must be done before DMA setup, since at least
	 * the ST-DMA begins to fill internal buffers right after setup. For
	 * reads, invalidate any cache, may be altered after DMA without CPU
	 * knowledge.
518
	 *
L
Linus Torvalds 已提交
519 520 521
	 * ++roman: For the Medusa, there's no need at all for that cache stuff,
	 * because the hardware does bus snooping (fine!).
	 */
522
	dma_cache_maintenance(addr, count, dir);
L
Linus Torvalds 已提交
523 524 525

	if (IS_A_TT()) {
		tt_scsi_dma.dma_ctrl = dir;
526 527
		SCSI_DMA_WRITE_P(dma_addr, addr);
		SCSI_DMA_WRITE_P(dma_cnt, count);
L
Linus Torvalds 已提交
528
		tt_scsi_dma.dma_ctrl = dir | 2;
529 530
	} else { /* ! IS_A_TT */

L
Linus Torvalds 已提交
531
		/* set address */
532
		SCSI_DMA_SETADR(addr);
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

		/* toggle direction bit to clear FIFO and set DMA direction */
		dir <<= 8;
		st_dma.dma_mode_status = 0x90 | dir;
		st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
		st_dma.dma_mode_status = 0x90 | dir;
		udelay(40);
		/* On writes, round up the transfer length to the next multiple of 512
		 * (see also comment at atari_dma_xfer_len()). */
		st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
		udelay(40);
		st_dma.dma_mode_status = 0x10 | dir;
		udelay(40);
		/* need not restore value of dir, only boolean value is tested */
		atari_dma_active = 1;
	}

550
	return count;
L
Linus Torvalds 已提交
551 552 553
}


554
static long atari_scsi_dma_residual(struct Scsi_Host *instance)
L
Linus Torvalds 已提交
555
{
556
	return atari_dma_residual;
L
Linus Torvalds 已提交
557 558 559 560 561 562 563
}


#define	CMD_SURELY_BLOCK_MODE	0
#define	CMD_SURELY_BYTE_MODE	1
#define	CMD_MODE_UNKNOWN		2

F
Finn Thain 已提交
564
static int falcon_classify_cmd(struct scsi_cmnd *cmd)
L
Linus Torvalds 已提交
565 566
{
	unsigned char opcode = cmd->cmnd[0];
567

L
Linus Torvalds 已提交
568
	if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
569 570
	    opcode == READ_BUFFER)
		return CMD_SURELY_BYTE_MODE;
L
Linus Torvalds 已提交
571 572 573 574 575 576 577
	else if (opcode == READ_6 || opcode == READ_10 ||
		 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
		 opcode == RECOVER_BUFFERED_DATA) {
		/* In case of a sequential-access target (tape), special care is
		 * needed here: The transfer is block-mode only if the 'fixed' bit is
		 * set! */
		if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
578
			return CMD_SURELY_BYTE_MODE;
L
Linus Torvalds 已提交
579
		else
580 581 582
			return CMD_SURELY_BLOCK_MODE;
	} else
		return CMD_MODE_UNKNOWN;
L
Linus Torvalds 已提交
583 584 585 586 587 588 589 590 591 592 593 594
}


/* This function calculates the number of bytes that can be transferred via
 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
 * possible on the Falcon, since that would require to program the DMA for
 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
 * the overrun problem, so this question is academic :-)
 */

595
static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
F
Finn Thain 已提交
596
					struct scsi_cmnd *cmd, int write_flag)
L
Linus Torvalds 已提交
597 598
{
	unsigned long	possible_len, limit;
599

600 601 602
	if (wanted_len < DMA_MIN_SIZE)
		return 0;

L
Linus Torvalds 已提交
603 604
	if (IS_A_TT())
		/* TT SCSI DMA can transfer arbitrary #bytes */
605
		return wanted_len;
L
Linus Torvalds 已提交
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640

	/* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
	 * 255*512 bytes, but this should be enough)
	 *
	 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
	 * that return a number of bytes which cannot be known beforehand. In this
	 * case, the given transfer length is an "allocation length". Now it
	 * can happen that this allocation length is a multiple of 512 bytes and
	 * the DMA is used. But if not n*512 bytes really arrive, some input data
	 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
	 * between commands that do block transfers and those that do byte
	 * transfers. But this isn't easy... there are lots of vendor specific
	 * commands, and the user can issue any command via the
	 * SCSI_IOCTL_SEND_COMMAND.
	 *
	 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
	 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
	 * and 3), the thing to do is obvious: allow any number of blocks via DMA
	 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
	 * the transfer (allocation) length is < 1024, hoping that no cmd. not
	 * explicitly known as byte mode have such big allocation lengths...
	 * BTW, all the discussion above applies only to reads. DMA writes are
	 * unproblematic anyways, since the targets aborts the transfer after
	 * receiving a sufficient number of bytes.
	 *
	 * Another point: If the transfer is from/to an non-ST-RAM address, we
	 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
	 */

	if (write_flag) {
		/* Write operation can always use the DMA, but the transfer size must
		 * be rounded up to the next multiple of 512 (atari_dma_setup() does
		 * this).
		 */
		possible_len = wanted_len;
641
	} else {
L
Linus Torvalds 已提交
642 643 644 645 646 647 648 649 650
		/* Read operations: if the wanted transfer length is not a multiple of
		 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
		 * (no interrupt on DMA finished!)
		 */
		if (wanted_len & 0x1ff)
			possible_len = 0;
		else {
			/* Now classify the command (see above) and decide whether it is
			 * allowed to do DMA at all */
651 652
			switch (falcon_classify_cmd(cmd)) {
			case CMD_SURELY_BLOCK_MODE:
L
Linus Torvalds 已提交
653 654
				possible_len = wanted_len;
				break;
655
			case CMD_SURELY_BYTE_MODE:
L
Linus Torvalds 已提交
656 657
				possible_len = 0; /* DMA prohibited */
				break;
658 659
			case CMD_MODE_UNKNOWN:
			default:
L
Linus Torvalds 已提交
660 661 662 663 664 665 666
				/* For unknown commands assume block transfers if the transfer
				 * size/allocation length is >= 1024 */
				possible_len = (wanted_len < 1024) ? 0 : wanted_len;
				break;
			}
		}
	}
667

L
Linus Torvalds 已提交
668
	/* Last step: apply the hard limit on DMA transfers */
669
	limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
L
Linus Torvalds 已提交
670 671 672 673 674
		    STRAM_BUFFER_SIZE : 255*512;
	if (possible_len > limit)
		possible_len = limit;

	if (possible_len != wanted_len)
F
Finn Thain 已提交
675
		dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
L
Linus Torvalds 已提交
676 677
			   "instead of %ld\n", possible_len, wanted_len);

678
	return possible_len;
L
Linus Torvalds 已提交
679 680 681 682 683 684 685 686 687 688
}


/* NCR5380 register access functions
 *
 * There are separate functions for TT and Falcon, because the access
 * methods are quite different. The calling macros NCR5380_read and
 * NCR5380_write call these functions via function pointers.
 */

689
static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
L
Linus Torvalds 已提交
690
{
691
	return tt_scsi_regp[reg * 2];
L
Linus Torvalds 已提交
692 693
}

694
static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
L
Linus Torvalds 已提交
695 696 697 698
{
	tt_scsi_regp[reg * 2] = value;
}

699
static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
L
Linus Torvalds 已提交
700 701
{
	dma_wd.dma_mode_status= (u_short)(0x88 + reg);
702
	return (u_char)dma_wd.fdc_acces_seccount;
L
Linus Torvalds 已提交
703 704
}

705
static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
L
Linus Torvalds 已提交
706 707 708 709 710 711
{
	dma_wd.dma_mode_status = (u_short)(0x88 + reg);
	dma_wd.fdc_acces_seccount = (u_short)value;
}


712
#include "NCR5380.c"
L
Linus Torvalds 已提交
713

714 715 716
static int atari_scsi_bus_reset(struct scsi_cmnd *cmd)
{
	int rv;
717 718 719
	unsigned long flags;

	local_irq_save(flags);
720

721 722
	/* Abort a maybe active DMA transfer */
	if (IS_A_TT()) {
723 724 725 726 727 728 729 730 731
		tt_scsi_dma.dma_ctrl = 0;
	} else {
		st_dma.dma_mode_status = 0x90;
		atari_dma_active = 0;
		atari_dma_orig_addr = NULL;
	}

	rv = NCR5380_bus_reset(cmd);

732 733 734 735 736
	/* The 5380 raises its IRQ line while _RST is active but the ST DMA
	 * "lock" has been released so this interrupt may end up handled by
	 * floppy or IDE driver (if one of them holds the lock). The NCR5380
	 * interrupt flag has been cleared already.
	 */
737

738
	local_irq_restore(flags);
739 740 741 742

	return rv;
}

743 744 745 746 747 748
#define DRV_MODULE_NAME         "atari_scsi"
#define PFX                     DRV_MODULE_NAME ": "

static struct scsi_host_template atari_scsi_template = {
	.module			= THIS_MODULE,
	.proc_name		= DRV_MODULE_NAME,
L
Linus Torvalds 已提交
749 750 751 752 753
	.name			= "Atari native SCSI",
	.info			= atari_scsi_info,
	.queuecommand		= atari_scsi_queue_command,
	.eh_abort_handler	= atari_scsi_abort,
	.eh_bus_reset_handler	= atari_scsi_bus_reset,
754
	.this_id		= 7,
755
	.use_clustering		= DISABLE_CLUSTERING,
756
	.cmd_size		= NCR5380_CMD_SIZE,
L
Linus Torvalds 已提交
757 758
};

759 760 761 762 763
static int __init atari_scsi_probe(struct platform_device *pdev)
{
	struct Scsi_Host *instance;
	int error;
	struct resource *irq;
764
	int host_flags = 0;
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813

	irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!irq)
		return -ENODEV;

	if (ATARIHW_PRESENT(TT_SCSI)) {
		atari_scsi_reg_read  = atari_scsi_tt_reg_read;
		atari_scsi_reg_write = atari_scsi_tt_reg_write;
	} else {
		atari_scsi_reg_read  = atari_scsi_falcon_reg_read;
		atari_scsi_reg_write = atari_scsi_falcon_reg_write;
	}

	/* The values for CMD_PER_LUN and CAN_QUEUE are somehow arbitrary.
	 * Higher values should work, too; try it!
	 * (But cmd_per_lun costs memory!)
	 *
	 * But there seems to be a bug somewhere that requires CAN_QUEUE to be
	 * 2*CMD_PER_LUN. At least on a TT, no spurious timeouts seen since
	 * changed CMD_PER_LUN...
	 *
	 * Note: The Falcon currently uses 8/1 setting due to unsolved problems
	 * with cmd_per_lun != 1
	 */
	if (ATARIHW_PRESENT(TT_SCSI)) {
		atari_scsi_template.can_queue    = 16;
		atari_scsi_template.cmd_per_lun  = 8;
		atari_scsi_template.sg_tablesize = SG_ALL;
	} else {
		atari_scsi_template.can_queue    = 8;
		atari_scsi_template.cmd_per_lun  = 1;
		atari_scsi_template.sg_tablesize = SG_NONE;
	}

	if (setup_can_queue > 0)
		atari_scsi_template.can_queue = setup_can_queue;

	if (setup_cmd_per_lun > 0)
		atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;

	/* Leave sg_tablesize at 0 on a Falcon! */
	if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize >= 0)
		atari_scsi_template.sg_tablesize = setup_sg_tablesize;

	if (setup_hostid >= 0) {
		atari_scsi_template.this_id = setup_hostid & 7;
	} else {
		/* Test if a host id is set in the NVRam */
		if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
814
			unsigned char b = nvram_read_byte(16);
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848

			/* Arbitration enabled? (for TOS)
			 * If yes, use configured host ID
			 */
			if (b & 0x80)
				atari_scsi_template.this_id = b & 7;
		}
	}

	/* If running on a Falcon and if there's TT-Ram (i.e., more than one
	 * memory block, since there's always ST-Ram in a Falcon), then
	 * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers
	 * from/to alternative Ram.
	 */
	if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
	    m68k_num_memory > 1) {
		atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
		if (!atari_dma_buffer) {
			pr_err(PFX "can't allocate ST-RAM double buffer\n");
			return -ENOMEM;
		}
		atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
		atari_dma_orig_addr = 0;
	}

	instance = scsi_host_alloc(&atari_scsi_template,
	                           sizeof(struct NCR5380_hostdata));
	if (!instance) {
		error = -ENOMEM;
		goto fail_alloc;
	}

	instance->irq = irq->start;

849
	host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
850
	host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
851

852 853 854
	error = NCR5380_init(instance, host_flags);
	if (error)
		goto fail_init;
855 856 857 858 859 860 861 862 863 864

	if (IS_A_TT()) {
		error = request_irq(instance->irq, scsi_tt_intr, 0,
		                    "NCR5380", instance);
		if (error) {
			pr_err(PFX "request irq %d failed, aborting\n",
			       instance->irq);
			goto fail_irq;
		}
		tt_mfp.active_edge |= 0x80;	/* SCSI int on L->H */
865

866 867 868 869 870 871 872 873 874 875 876
		tt_scsi_dma.dma_ctrl = 0;
		atari_dma_residual = 0;

		/* While the read overruns (described by Drew Eckhardt in
		 * NCR5380.c) never happened on TTs, they do in fact on the
		 * Medusa (This was the cause why SCSI didn't work right for
		 * so long there.) Since handling the overruns slows down
		 * a bit, I turned the #ifdef's into a runtime condition.
		 *
		 * In principle it should be sufficient to do max. 1 byte with
		 * PIO, but there is another problem on the Medusa with the DMA
877
		 * rest data register. So read_overruns is currently set
878 879 880
		 * to 4 to avoid having transfers that aren't a multiple of 4.
		 * If the rest data bug is fixed, this can be lowered to 1.
		 */
881 882 883 884 885 886
		if (MACH_IS_MEDUSA) {
			struct NCR5380_hostdata *hostdata =
				shost_priv(instance);

			hostdata->read_overruns = 4;
		}
887 888 889 890 891 892 893 894 895 896
	} else {
		/* Nothing to do for the interrupt: the ST-DMA is initialized
		 * already.
		 */
		atari_dma_residual = 0;
		atari_dma_active = 0;
		atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
					: 0xff000000);
	}

897 898
	NCR5380_maybe_reset_bus(instance);

899 900 901 902 903 904 905 906 907 908 909 910 911 912
	error = scsi_add_host(instance, NULL);
	if (error)
		goto fail_host;

	platform_set_drvdata(pdev, instance);

	scsi_scan_host(instance);
	return 0;

fail_host:
	if (IS_A_TT())
		free_irq(instance->irq, instance);
fail_irq:
	NCR5380_exit(instance);
913
fail_init:
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	scsi_host_put(instance);
fail_alloc:
	if (atari_dma_buffer)
		atari_stram_free(atari_dma_buffer);
	return error;
}

static int __exit atari_scsi_remove(struct platform_device *pdev)
{
	struct Scsi_Host *instance = platform_get_drvdata(pdev);

	scsi_remove_host(instance);
	if (IS_A_TT())
		free_irq(instance->irq, instance);
	NCR5380_exit(instance);
	scsi_host_put(instance);
	if (atari_dma_buffer)
		atari_stram_free(atari_dma_buffer);
	return 0;
}

static struct platform_driver atari_scsi_driver = {
	.remove = __exit_p(atari_scsi_remove),
	.driver = {
		.name	= DRV_MODULE_NAME,
	},
};
L
Linus Torvalds 已提交
941

942
module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
L
Linus Torvalds 已提交
943

944
MODULE_ALIAS("platform:" DRV_MODULE_NAME);
L
Linus Torvalds 已提交
945
MODULE_LICENSE("GPL");