• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/scsi/arm/
1/*
2 *  linux/drivers/acorn/scsi/acornscsi.c
3 *
4 *  Acorn SCSI 3 driver
5 *  By R.M.King.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Abandoned using the Select and Transfer command since there were
12 * some nasty races between our software and the target devices that
13 * were not easy to solve, and the device errata had a lot of entries
14 * for this command, some of them quite nasty...
15 *
16 * Changelog:
17 *  26-Sep-1997	RMK	Re-jigged to use the queue module.
18 *			Re-coded state machine to be based on driver
19 *			state not scsi state.  Should be easier to debug.
20 *			Added acornscsi_release to clean up properly.
21 *			Updated proc/scsi reporting.
22 *  05-Oct-1997	RMK	Implemented writing to SCSI devices.
23 *  06-Oct-1997	RMK	Corrected small (non-serious) bug with the connect/
24 *			reconnect race condition causing a warning message.
25 *  12-Oct-1997	RMK	Added catch for re-entering interrupt routine.
26 *  15-Oct-1997	RMK	Improved handling of commands.
27 *  27-Jun-1998	RMK	Changed asm/delay.h to linux/delay.h.
28 *  13-Dec-1998	RMK	Better abort code and command handling.  Extra state
29 *			transitions added to allow dodgy devices to work.
30 */
31#define DEBUG_NO_WRITE	1
32#define DEBUG_QUEUES	2
33#define DEBUG_DMA	4
34#define DEBUG_ABORT	8
35#define DEBUG_DISCON	16
36#define DEBUG_CONNECT	32
37#define DEBUG_PHASES	64
38#define DEBUG_WRITE	128
39#define DEBUG_LINK	256
40#define DEBUG_MESSAGES	512
41#define DEBUG_RESET	1024
42#define DEBUG_ALL	(DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
43			 DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
44			 DEBUG_DMA|DEBUG_QUEUES)
45
46/* DRIVER CONFIGURATION
47 *
48 * SCSI-II Tagged queue support.
49 *
50 * I don't have any SCSI devices that support it, so it is totally untested
51 * (except to make sure that it doesn't interfere with any non-tagging
52 * devices).  It is not fully implemented either - what happens when a
53 * tagging device reconnects???
54 *
55 * You can tell if you have a device that supports tagged queueing my
56 * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
57 * as '2 TAG'.
58 *
59 * Also note that CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE is normally set in the config
60 * scripts, but disabled here.  Once debugged, remove the #undef, otherwise to debug,
61 * comment out the undef.
62 */
63#undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
64/*
65 * SCSI-II Linked command support.
66 *
67 * The higher level code doesn't support linked commands yet, and so the option
68 * is undef'd here.
69 */
70#undef CONFIG_SCSI_ACORNSCSI_LINK
71/*
72 * SCSI-II Synchronous transfer support.
73 *
74 * Tried and tested...
75 *
76 * SDTR_SIZE	  - maximum number of un-acknowledged bytes (0 = off, 12 = max)
77 * SDTR_PERIOD	  - period of REQ signal (min=125, max=1020)
78 * DEFAULT_PERIOD - default REQ period.
79 */
80#define SDTR_SIZE	12
81#define SDTR_PERIOD	125
82#define DEFAULT_PERIOD	500
83
84/*
85 * Debugging information
86 *
87 * DEBUG	  - bit mask from list above
88 * DEBUG_TARGET   - is defined to the target number if you want to debug
89 *		    a specific target. [only recon/write/dma].
90 */
91#define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
92/* only allow writing to SCSI device 0 */
93#define NO_WRITE 0xFE
94/*#define DEBUG_TARGET 2*/
95/*
96 * Select timeout time (in 10ms units)
97 *
98 * This is the timeout used between the start of selection and the WD33C93
99 * chip deciding that the device isn't responding.
100 */
101#define TIMEOUT_TIME 10
102/*
103 * Define this if you want to have verbose explaination of SCSI
104 * status/messages.
105 */
106#undef CONFIG_ACORNSCSI_CONSTANTS
107/*
108 * Define this if you want to use the on board DMAC [don't remove this option]
109 * If not set, then use PIO mode (not currently supported).
110 */
111#define USE_DMAC
112
113/*
114 * ====================================================================================
115 */
116
117#ifdef DEBUG_TARGET
118#define DBG(cmd,xxx...) \
119  if (cmd->device->id == DEBUG_TARGET) { \
120    xxx; \
121  }
122#else
123#define DBG(cmd,xxx...) xxx
124#endif
125
126#include <linux/module.h>
127#include <linux/kernel.h>
128#include <linux/string.h>
129#include <linux/signal.h>
130#include <linux/errno.h>
131#include <linux/proc_fs.h>
132#include <linux/ioport.h>
133#include <linux/blkdev.h>
134#include <linux/delay.h>
135#include <linux/interrupt.h>
136#include <linux/init.h>
137#include <linux/bitops.h>
138#include <linux/stringify.h>
139#include <linux/io.h>
140
141#include <asm/system.h>
142#include <asm/ecard.h>
143
144#include "../scsi.h"
145#include <scsi/scsi_dbg.h>
146#include <scsi/scsi_host.h>
147#include <scsi/scsi_transport_spi.h>
148#include "acornscsi.h"
149#include "msgqueue.h"
150#include "scsi.h"
151
152#include <scsi/scsicam.h>
153
154#define VER_MAJOR 2
155#define VER_MINOR 0
156#define VER_PATCH 6
157
158#ifndef ABORT_TAG
159#define ABORT_TAG 0xd
160#else
161#error "Yippee!  ABORT TAG is now defined!  Remove this error!"
162#endif
163
164#ifdef CONFIG_SCSI_ACORNSCSI_LINK
165#error SCSI2 LINKed commands not supported (yet)!
166#endif
167
168#ifdef USE_DMAC
169/*
170 * DMAC setup parameters
171 */
172#define INIT_DEVCON0	(DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
173#define INIT_DEVCON1	(DEVCON1_BHLD)
174#define DMAC_READ	(MODECON_READ)
175#define DMAC_WRITE	(MODECON_WRITE)
176#define INIT_SBICDMA	(CTRL_DMABURST)
177
178#define scsi_xferred	have_data_in
179
180/*
181 * Size of on-board DMA buffer
182 */
183#define DMAC_BUFFER_SIZE	65536
184#endif
185
186#define STATUS_BUFFER_TO_PRINT	24
187
188unsigned int sdtr_period = SDTR_PERIOD;
189unsigned int sdtr_size   = SDTR_SIZE;
190
191static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
192			   unsigned int result);
193static int acornscsi_reconnect_finish(AS_Host *host);
194static void acornscsi_dma_cleanup(AS_Host *host);
195static void acornscsi_abortcmd(AS_Host *host, unsigned char tag);
196
197/* ====================================================================================
198 * Miscellaneous
199 */
200
201/* Offsets from MEMC base */
202#define SBIC_REGIDX	0x2000
203#define SBIC_REGVAL	0x2004
204#define DMAC_OFFSET	0x3000
205
206/* Offsets from FAST IOC base */
207#define INT_REG		0x2000
208#define PAGE_REG	0x3000
209
210static inline void sbic_arm_write(AS_Host *host, unsigned int reg, unsigned int value)
211{
212    writeb(reg, host->base + SBIC_REGIDX);
213    writeb(value, host->base + SBIC_REGVAL);
214}
215
216static inline int sbic_arm_read(AS_Host *host, unsigned int reg)
217{
218    if(reg == SBIC_ASR)
219	   return readl(host->base + SBIC_REGIDX) & 255;
220    writeb(reg, host->base + SBIC_REGIDX);
221    return readl(host->base + SBIC_REGVAL) & 255;
222}
223
224#define sbic_arm_writenext(host, val)	writeb((val), (host)->base + SBIC_REGVAL)
225#define sbic_arm_readnext(host) 	readb((host)->base + SBIC_REGVAL)
226
227#ifdef USE_DMAC
228#define dmac_read(host,reg) \
229	readb((host)->base + DMAC_OFFSET + ((reg) << 2))
230
231#define dmac_write(host,reg,value) \
232	({ writeb((value), (host)->base + DMAC_OFFSET + ((reg) << 2)); })
233
234#define dmac_clearintr(host) 	writeb(0, (host)->fast + INT_REG)
235
236static inline unsigned int dmac_address(AS_Host *host)
237{
238    return dmac_read(host, DMAC_TXADRHI) << 16 |
239	   dmac_read(host, DMAC_TXADRMD) << 8 |
240	   dmac_read(host, DMAC_TXADRLO);
241}
242
243static
244void acornscsi_dumpdma(AS_Host *host, char *where)
245{
246	unsigned int mode, addr, len;
247
248	mode = dmac_read(host, DMAC_MODECON);
249	addr = dmac_address(host);
250	len  = dmac_read(host, DMAC_TXCNTHI) << 8 |
251	       dmac_read(host, DMAC_TXCNTLO);
252
253	printk("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
254		host->host->host_no, where,
255		mode, addr, (len + 1) & 0xffff,
256		dmac_read(host, DMAC_MASKREG));
257
258	printk("DMA @%06x, ", host->dma.start_addr);
259	printk("BH @%p +%04x, ", host->scsi.SCp.ptr,
260		host->scsi.SCp.this_residual);
261	printk("DT @+%04x ST @+%04x", host->dma.transferred,
262		host->scsi.SCp.scsi_xferred);
263	printk("\n");
264}
265#endif
266
267static
268unsigned long acornscsi_sbic_xfcount(AS_Host *host)
269{
270    unsigned long length;
271
272    length = sbic_arm_read(host, SBIC_TRANSCNTH) << 16;
273    length |= sbic_arm_readnext(host) << 8;
274    length |= sbic_arm_readnext(host);
275
276    return length;
277}
278
279static int
280acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
281{
282	int asr;
283
284	do {
285		asr = sbic_arm_read(host, SBIC_ASR);
286
287		if ((asr & stat_mask) == stat)
288			return 0;
289
290		udelay(1);
291	} while (--timeout);
292
293	printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
294
295	return -1;
296}
297
298static
299int acornscsi_sbic_issuecmd(AS_Host *host, int command)
300{
301    if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
302	return -1;
303
304    sbic_arm_write(host, SBIC_CMND, command);
305
306    return 0;
307}
308
309static void
310acornscsi_csdelay(unsigned int cs)
311{
312    unsigned long target_jiffies, flags;
313
314    target_jiffies = jiffies + 1 + cs * HZ / 100;
315
316    local_save_flags(flags);
317    local_irq_enable();
318
319    while (time_before(jiffies, target_jiffies)) barrier();
320
321    local_irq_restore(flags);
322}
323
324static
325void acornscsi_resetcard(AS_Host *host)
326{
327    unsigned int i, timeout;
328
329    /* assert reset line */
330    host->card.page_reg = 0x80;
331    writeb(host->card.page_reg, host->fast + PAGE_REG);
332
333    /* wait 3 cs.  SCSI standard says 25ms. */
334    acornscsi_csdelay(3);
335
336    host->card.page_reg = 0;
337    writeb(host->card.page_reg, host->fast + PAGE_REG);
338
339    /*
340     * Should get a reset from the card
341     */
342    timeout = 1000;
343    do {
344	if (readb(host->fast + INT_REG) & 8)
345	    break;
346	udelay(1);
347    } while (--timeout);
348
349    if (timeout == 0)
350	printk("scsi%d: timeout while resetting card\n",
351		host->host->host_no);
352
353    sbic_arm_read(host, SBIC_ASR);
354    sbic_arm_read(host, SBIC_SSR);
355
356    /* setup sbic - WD33C93A */
357    sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
358    sbic_arm_write(host, SBIC_CMND, CMND_RESET);
359
360    /*
361     * Command should cause a reset interrupt
362     */
363    timeout = 1000;
364    do {
365	if (readb(host->fast + INT_REG) & 8)
366	    break;
367	udelay(1);
368    } while (--timeout);
369
370    if (timeout == 0)
371	printk("scsi%d: timeout while resetting card\n",
372		host->host->host_no);
373
374    sbic_arm_read(host, SBIC_ASR);
375    if (sbic_arm_read(host, SBIC_SSR) != 0x01)
376	printk(KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
377		host->host->host_no);
378
379    sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
380    sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
381    sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
382    sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
383
384    host->card.page_reg = 0x40;
385    writeb(host->card.page_reg, host->fast + PAGE_REG);
386
387    /* setup dmac - uPC71071 */
388    dmac_write(host, DMAC_INIT, 0);
389#ifdef USE_DMAC
390    dmac_write(host, DMAC_INIT, INIT_8BIT);
391    dmac_write(host, DMAC_CHANNEL, CHANNEL_0);
392    dmac_write(host, DMAC_DEVCON0, INIT_DEVCON0);
393    dmac_write(host, DMAC_DEVCON1, INIT_DEVCON1);
394#endif
395
396    host->SCpnt = NULL;
397    host->scsi.phase = PHASE_IDLE;
398    host->scsi.disconnectable = 0;
399
400    memset(host->busyluns, 0, sizeof(host->busyluns));
401
402    for (i = 0; i < 8; i++) {
403	host->device[i].sync_state = SYNC_NEGOCIATE;
404	host->device[i].disconnect_ok = 1;
405    }
406
407    /* wait 25 cs.  SCSI standard says 250ms. */
408    acornscsi_csdelay(25);
409}
410
411/*=============================================================================================
412 * Utility routines (eg. debug)
413 */
414#ifdef CONFIG_ACORNSCSI_CONSTANTS
415static char *acornscsi_interrupttype[] = {
416  "rst",  "suc",  "p/a",  "3",
417  "term", "5",	  "6",	  "7",
418  "serv", "9",	  "a",	  "b",
419  "c",	  "d",	  "e",	  "f"
420};
421
422static signed char acornscsi_map[] = {
423  0,  1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
424 -1,  2, -1, -1,  -1, -1,  3, -1,   4,	5,  6,	7,   8,  9, 10, 11,
425 12, 13, 14, -1,  -1, -1, -1, -1,   4,	5,  6,	7,   8,  9, 10, 11,
426 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
427 15, 16, 17, 18,  19, -1, -1, 20,   4,	5,  6,	7,   8,  9, 10, 11,
428 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
429 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
430 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
431 21, 22, -1, -1,  -1, 23, -1, -1,   4,	5,  6,	7,   8,  9, 10, 11,
432 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
433 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
434 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
435 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
436 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
437 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,
438 -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1,  -1, -1, -1, -1
439};
440
441static char *acornscsi_interruptcode[] = {
442    /* 0 */
443    "reset - normal mode",	/* 00 */
444    "reset - advanced mode",	/* 01 */
445
446    /* 2 */
447    "sel",			/* 11 */
448    "sel+xfer", 		/* 16 */
449    "data-out", 		/* 18 */
450    "data-in",			/* 19 */
451    "cmd",			/* 1A */
452    "stat",			/* 1B */
453    "??-out",			/* 1C */
454    "??-in",			/* 1D */
455    "msg-out",			/* 1E */
456    "msg-in",			/* 1F */
457
458    /* 12 */
459    "/ACK asserted",		/* 20 */
460    "save-data-ptr",		/* 21 */
461    "{re}sel",			/* 22 */
462
463    /* 15 */
464    "inv cmd",			/* 40 */
465    "unexpected disconnect",	/* 41 */
466    "sel timeout",		/* 42 */
467    "P err",			/* 43 */
468    "P err+ATN",		/* 44 */
469    "bad status byte",		/* 47 */
470
471    /* 21 */
472    "resel, no id",		/* 80 */
473    "resel",			/* 81 */
474    "discon",			/* 85 */
475};
476
477static
478void print_scsi_status(unsigned int ssr)
479{
480    if (acornscsi_map[ssr] != -1)
481	printk("%s:%s",
482		acornscsi_interrupttype[(ssr >> 4)],
483		acornscsi_interruptcode[acornscsi_map[ssr]]);
484    else
485	printk("%X:%X", ssr >> 4, ssr & 0x0f);
486}
487#endif
488
489static
490void print_sbic_status(int asr, int ssr, int cmdphase)
491{
492#ifdef CONFIG_ACORNSCSI_CONSTANTS
493    printk("sbic: %c%c%c%c%c%c ",
494	    asr & ASR_INT ? 'I' : 'i',
495	    asr & ASR_LCI ? 'L' : 'l',
496	    asr & ASR_BSY ? 'B' : 'b',
497	    asr & ASR_CIP ? 'C' : 'c',
498	    asr & ASR_PE  ? 'P' : 'p',
499	    asr & ASR_DBR ? 'D' : 'd');
500    printk("scsi: ");
501    print_scsi_status(ssr);
502    printk(" ph %02X\n", cmdphase);
503#else
504    printk("sbic: %02X scsi: %X:%X ph: %02X\n",
505	    asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
506#endif
507}
508
509static void
510acornscsi_dumplogline(AS_Host *host, int target, int line)
511{
512	unsigned long prev;
513	signed int ptr;
514
515	ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
516	if (ptr < 0)
517		ptr += STATUS_BUFFER_SIZE;
518
519	printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
520		line == 0 ? "ph" : line == 1 ? "ssr" : "int");
521
522	prev = host->status[target][ptr].when;
523
524	for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
525		unsigned long time_diff;
526
527		if (!host->status[target][ptr].when)
528			continue;
529
530		switch (line) {
531		case 0:
532			printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
533					 host->status[target][ptr].ph);
534			break;
535
536		case 1:
537			printk(" %02X", host->status[target][ptr].ssr);
538			break;
539
540		case 2:
541			time_diff = host->status[target][ptr].when - prev;
542			prev = host->status[target][ptr].when;
543			if (time_diff == 0)
544				printk("==^");
545			else if (time_diff >= 100)
546				printk("   ");
547			else
548				printk(" %02ld", time_diff);
549			break;
550		}
551	}
552
553	printk("\n");
554}
555
556static
557void acornscsi_dumplog(AS_Host *host, int target)
558{
559    do {
560	acornscsi_dumplogline(host, target, 0);
561	acornscsi_dumplogline(host, target, 1);
562	acornscsi_dumplogline(host, target, 2);
563
564	if (target == 8)
565	    break;
566
567	target = 8;
568    } while (1);
569}
570
571static
572char acornscsi_target(AS_Host *host)
573{
574	if (host->SCpnt)
575		return '0' + host->SCpnt->device->id;
576	return 'H';
577}
578
579/*
580 * Prototype: cmdtype_t acornscsi_cmdtype(int command)
581 * Purpose  : differentiate READ from WRITE from other commands
582 * Params   : command - command to interpret
583 * Returns  : CMD_READ	- command reads data,
584 *	      CMD_WRITE - command writes data,
585 *	      CMD_MISC	- everything else
586 */
587static inline
588cmdtype_t acornscsi_cmdtype(int command)
589{
590    switch (command) {
591    case WRITE_6:  case WRITE_10:  case WRITE_12:
592	return CMD_WRITE;
593    case READ_6:   case READ_10:   case READ_12:
594	return CMD_READ;
595    default:
596	return CMD_MISC;
597    }
598}
599
600/*
601 * Prototype: int acornscsi_datadirection(int command)
602 * Purpose  : differentiate between commands that have a DATA IN phase
603 *	      and a DATA OUT phase
604 * Params   : command - command to interpret
605 * Returns  : DATADIR_OUT - data out phase expected
606 *	      DATADIR_IN  - data in phase expected
607 */
608static
609datadir_t acornscsi_datadirection(int command)
610{
611    switch (command) {
612    case CHANGE_DEFINITION:	case COMPARE:		case COPY:
613    case COPY_VERIFY:		case LOG_SELECT:	case MODE_SELECT:
614    case MODE_SELECT_10:	case SEND_DIAGNOSTIC:	case WRITE_BUFFER:
615    case FORMAT_UNIT:		case REASSIGN_BLOCKS:	case RESERVE:
616    case SEARCH_EQUAL:		case SEARCH_HIGH:	case SEARCH_LOW:
617    case WRITE_6:		case WRITE_10:		case WRITE_VERIFY:
618    case UPDATE_BLOCK:		case WRITE_LONG:	case WRITE_SAME:
619    case SEARCH_HIGH_12:	case SEARCH_EQUAL_12:	case SEARCH_LOW_12:
620    case WRITE_12:		case WRITE_VERIFY_12:	case SET_WINDOW:
621    case MEDIUM_SCAN:		case SEND_VOLUME_TAG:	case 0xea:
622	return DATADIR_OUT;
623    default:
624	return DATADIR_IN;
625    }
626}
627
628/*
629 * Purpose  : provide values for synchronous transfers with 33C93.
630 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
631 *	Modified by Russell King for 8MHz WD33C93A
632 */
633static struct sync_xfer_tbl {
634    unsigned int period_ns;
635    unsigned char reg_value;
636} sync_xfer_table[] = {
637    {	1, 0x20 },    { 249, 0x20 },	{ 374, 0x30 },
638    { 499, 0x40 },    { 624, 0x50 },	{ 749, 0x60 },
639    { 874, 0x70 },    { 999, 0x00 },	{   0,	  0 }
640};
641
642/*
643 * Prototype: int acornscsi_getperiod(unsigned char syncxfer)
644 * Purpose  : period for the synchronous transfer setting
645 * Params   : syncxfer SYNCXFER register value
646 * Returns  : period in ns.
647 */
648static
649int acornscsi_getperiod(unsigned char syncxfer)
650{
651    int i;
652
653    syncxfer &= 0xf0;
654    if (syncxfer == 0x10)
655	syncxfer = 0;
656
657    for (i = 1; sync_xfer_table[i].period_ns; i++)
658	if (syncxfer == sync_xfer_table[i].reg_value)
659	    return sync_xfer_table[i].period_ns;
660    return 0;
661}
662
663/*
664 * Prototype: int round_period(unsigned int period)
665 * Purpose  : return index into above table for a required REQ period
666 * Params   : period - time (ns) for REQ
667 * Returns  : table index
668 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
669 */
670static inline
671int round_period(unsigned int period)
672{
673    int i;
674
675    for (i = 1; sync_xfer_table[i].period_ns; i++) {
676	if ((period <= sync_xfer_table[i].period_ns) &&
677	    (period > sync_xfer_table[i - 1].period_ns))
678	    return i;
679    }
680    return 7;
681}
682
683/*
684 * Prototype: unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
685 * Purpose  : calculate value for 33c93s SYNC register
686 * Params   : period - time (ns) for REQ
687 *	      offset - offset in bytes between REQ/ACK
688 * Returns  : value for SYNC register
689 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
690 */
691static
692unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
693{
694    return sync_xfer_table[round_period(period)].reg_value |
695		((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
696}
697
698/* ====================================================================================
699 * Command functions
700 */
701/*
702 * Function: acornscsi_kick(AS_Host *host)
703 * Purpose : kick next command to interface
704 * Params  : host - host to send command to
705 * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
706 * Notes   : interrupts are always disabled!
707 */
708static
709intr_ret_t acornscsi_kick(AS_Host *host)
710{
711    int from_queue = 0;
712    struct scsi_cmnd *SCpnt;
713
714    /* first check to see if a command is waiting to be executed */
715    SCpnt = host->origSCpnt;
716    host->origSCpnt = NULL;
717
718    /* retrieve next command */
719    if (!SCpnt) {
720	SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
721	if (!SCpnt)
722	    return INTR_IDLE;
723
724	from_queue = 1;
725    }
726
727    if (host->scsi.disconnectable && host->SCpnt) {
728	queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
729	host->scsi.disconnectable = 0;
730#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
731	DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
732		host->host->host_no, acornscsi_target(host)));
733#endif
734	host->SCpnt = NULL;
735    }
736
737    /*
738     * If we have an interrupt pending, then we may have been reselected.
739     * In this case, we don't want to write to the registers
740     */
741    if (!(sbic_arm_read(host, SBIC_ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
742	sbic_arm_write(host, SBIC_DESTID, SCpnt->device->id);
743	sbic_arm_write(host, SBIC_CMND, CMND_SELWITHATN);
744    }
745
746    /*
747     * claim host busy - all of these must happen atomically wrt
748     * our interrupt routine.  Failure means command loss.
749     */
750    host->scsi.phase = PHASE_CONNECTING;
751    host->SCpnt = SCpnt;
752    host->scsi.SCp = SCpnt->SCp;
753    host->dma.xfer_setup = 0;
754    host->dma.xfer_required = 0;
755    host->dma.xfer_done = 0;
756
757#if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
758    DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
759	    host->host->host_no, '0' + SCpnt->device->id,
760	    SCpnt->cmnd[0]));
761#endif
762
763    if (from_queue) {
764#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
765	/*
766	 * tagged queueing - allocate a new tag to this command
767	 */
768	if (SCpnt->device->simple_tags) {
769	    SCpnt->device->current_tag += 1;
770	    if (SCpnt->device->current_tag == 0)
771		SCpnt->device->current_tag = 1;
772	    SCpnt->tag = SCpnt->device->current_tag;
773	} else
774#endif
775	    set_bit(SCpnt->device->id * 8 + SCpnt->device->lun, host->busyluns);
776
777	host->stats.removes += 1;
778
779	switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
780	case CMD_WRITE:
781	    host->stats.writes += 1;
782	    break;
783	case CMD_READ:
784	    host->stats.reads += 1;
785	    break;
786	case CMD_MISC:
787	    host->stats.miscs += 1;
788	    break;
789	}
790    }
791
792    return INTR_PROCESSING;
793}
794
795/*
796 * Function: void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp, unsigned int result)
797 * Purpose : complete processing for command
798 * Params  : host   - interface that completed
799 *	     result - driver byte of result
800 */
801static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
802			   unsigned int result)
803{
804	struct scsi_cmnd *SCpnt = *SCpntp;
805
806    /* clean up */
807    sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
808
809    host->stats.fins += 1;
810
811    if (SCpnt) {
812	*SCpntp = NULL;
813
814	acornscsi_dma_cleanup(host);
815
816	SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
817
818	/*
819	 * In theory, this should not happen.  In practice, it seems to.
820	 * Only trigger an error if the device attempts to report all happy
821	 * but with untransferred buffers...  If we don't do something, then
822	 * data loss will occur.  Should we check SCpnt->underflow here?
823	 * It doesn't appear to be set to something meaningful by the higher
824	 * levels all the time.
825	 */
826	if (result == DID_OK) {
827		int xfer_warn = 0;
828
829		if (SCpnt->underflow == 0) {
830			if (host->scsi.SCp.ptr &&
831			    acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
832				xfer_warn = 1;
833		} else {
834			if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
835			    host->scsi.SCp.scsi_xferred != host->dma.transferred)
836				xfer_warn = 1;
837		}
838
839		/* ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.6)
840		 *  Targets which break data transfers into multiple
841		 *  connections shall end each successful connection
842		 *  (except possibly the last) with a SAVE DATA
843		 *  POINTER - DISCONNECT message sequence.
844		 *
845		 * This makes it difficult to ensure that a transfer has
846		 * completed.  If we reach the end of a transfer during
847		 * the command, then we can only have finished the transfer.
848		 * therefore, if we seem to have some data remaining, this
849		 * is not a problem.
850		 */
851		if (host->dma.xfer_done)
852			xfer_warn = 0;
853
854		if (xfer_warn) {
855		    switch (status_byte(SCpnt->result)) {
856		    case CHECK_CONDITION:
857		    case COMMAND_TERMINATED:
858		    case BUSY:
859		    case QUEUE_FULL:
860		    case RESERVATION_CONFLICT:
861			break;
862
863		    default:
864			printk(KERN_ERR "scsi%d.H: incomplete data transfer detected: result=%08X command=",
865				host->host->host_no, SCpnt->result);
866			__scsi_print_command(SCpnt->cmnd);
867			acornscsi_dumpdma(host, "done");
868		 	acornscsi_dumplog(host, SCpnt->device->id);
869			SCpnt->result &= 0xffff;
870			SCpnt->result |= DID_ERROR << 16;
871		    }
872		}
873	}
874
875	if (!SCpnt->scsi_done)
876	    panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
877
878	clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, host->busyluns);
879
880	SCpnt->scsi_done(SCpnt);
881    } else
882	printk("scsi%d: null command in acornscsi_done", host->host->host_no);
883
884    host->scsi.phase = PHASE_IDLE;
885}
886
887/* ====================================================================================
888 * DMA routines
889 */
890/*
891 * Purpose  : update SCSI Data Pointer
892 * Notes    : this will only be one SG entry or less
893 */
894static
895void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
896{
897    SCp->ptr += length;
898    SCp->this_residual -= length;
899
900    if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
901	host->dma.xfer_done = 1;
902}
903
904/*
905 * Prototype: void acornscsi_data_read(AS_Host *host, char *ptr,
906 *				unsigned int start_addr, unsigned int length)
907 * Purpose  : read data from DMA RAM
908 * Params   : host - host to transfer from
909 *	      ptr  - DRAM address
910 *	      start_addr - host mem address
911 *	      length - number of bytes to transfer
912 * Notes    : this will only be one SG entry or less
913 */
914static
915void acornscsi_data_read(AS_Host *host, char *ptr,
916				 unsigned int start_addr, unsigned int length)
917{
918    extern void __acornscsi_in(void __iomem *, char *buf, int len);
919    unsigned int page, offset, len = length;
920
921    page = (start_addr >> 12);
922    offset = start_addr & ((1 << 12) - 1);
923
924    writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
925
926    while (len > 0) {
927	unsigned int this_len;
928
929	if (len + offset > (1 << 12))
930	    this_len = (1 << 12) - offset;
931	else
932	    this_len = len;
933
934	__acornscsi_in(host->base + (offset << 1), ptr, this_len);
935
936	offset += this_len;
937	ptr += this_len;
938	len -= this_len;
939
940	if (offset == (1 << 12)) {
941	    offset = 0;
942	    page ++;
943	    writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
944	}
945    }
946    writeb(host->card.page_reg, host->fast + PAGE_REG);
947}
948
949/*
950 * Prototype: void acornscsi_data_write(AS_Host *host, char *ptr,
951 *				unsigned int start_addr, unsigned int length)
952 * Purpose  : write data to DMA RAM
953 * Params   : host - host to transfer from
954 *	      ptr  - DRAM address
955 *	      start_addr - host mem address
956 *	      length - number of bytes to transfer
957 * Notes    : this will only be one SG entry or less
958 */
959static
960void acornscsi_data_write(AS_Host *host, char *ptr,
961				 unsigned int start_addr, unsigned int length)
962{
963    extern void __acornscsi_out(void __iomem *, char *buf, int len);
964    unsigned int page, offset, len = length;
965
966    page = (start_addr >> 12);
967    offset = start_addr & ((1 << 12) - 1);
968
969    writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
970
971    while (len > 0) {
972	unsigned int this_len;
973
974	if (len + offset > (1 << 12))
975	    this_len = (1 << 12) - offset;
976	else
977	    this_len = len;
978
979	__acornscsi_out(host->base + (offset << 1), ptr, this_len);
980
981	offset += this_len;
982	ptr += this_len;
983	len -= this_len;
984
985	if (offset == (1 << 12)) {
986	    offset = 0;
987	    page ++;
988	    writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
989	}
990    }
991    writeb(host->card.page_reg, host->fast + PAGE_REG);
992}
993
994/* =========================================================================================
995 * On-board DMA routines
996 */
997#ifdef USE_DMAC
998/*
999 * Prototype: void acornscsi_dmastop(AS_Host *host)
1000 * Purpose  : stop all DMA
1001 * Params   : host - host on which to stop DMA
1002 * Notes    : This is called when leaving DATA IN/OUT phase,
1003 *	      or when interface is RESET
1004 */
1005static inline
1006void acornscsi_dma_stop(AS_Host *host)
1007{
1008    dmac_write(host, DMAC_MASKREG, MASK_ON);
1009    dmac_clearintr(host);
1010
1011#if (DEBUG & DEBUG_DMA)
1012    DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
1013#endif
1014}
1015
1016/*
1017 * Function: void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1018 * Purpose : setup DMA controller for data transfer
1019 * Params  : host - host to setup
1020 *	     direction - data transfer direction
1021 * Notes   : This is called when entering DATA I/O phase, not
1022 *	     while we're in a DATA I/O phase
1023 */
1024static
1025void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1026{
1027    unsigned int address, length, mode;
1028
1029    host->dma.direction = direction;
1030
1031    dmac_write(host, DMAC_MASKREG, MASK_ON);
1032
1033    if (direction == DMA_OUT) {
1034#if (DEBUG & DEBUG_NO_WRITE)
1035	if (NO_WRITE & (1 << host->SCpnt->device->id)) {
1036	    printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
1037		    host->host->host_no, acornscsi_target(host));
1038	    return;
1039	}
1040#endif
1041	mode = DMAC_WRITE;
1042    } else
1043	mode = DMAC_READ;
1044
1045    /*
1046     * Allocate some buffer space, limited to half the buffer size
1047     */
1048    length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1049    if (length) {
1050	host->dma.start_addr = address = host->dma.free_addr;
1051	host->dma.free_addr = (host->dma.free_addr + length) &
1052				(DMAC_BUFFER_SIZE - 1);
1053
1054	/*
1055	 * Transfer data to DMA memory
1056	 */
1057	if (direction == DMA_OUT)
1058	    acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1059				length);
1060
1061	length -= 1;
1062	dmac_write(host, DMAC_TXCNTLO, length);
1063	dmac_write(host, DMAC_TXCNTHI, length >> 8);
1064	dmac_write(host, DMAC_TXADRLO, address);
1065	dmac_write(host, DMAC_TXADRMD, address >> 8);
1066	dmac_write(host, DMAC_TXADRHI, 0);
1067	dmac_write(host, DMAC_MODECON, mode);
1068	dmac_write(host, DMAC_MASKREG, MASK_OFF);
1069
1070#if (DEBUG & DEBUG_DMA)
1071	DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
1072#endif
1073	host->dma.xfer_setup = 1;
1074    }
1075}
1076
1077/*
1078 * Function: void acornscsi_dma_cleanup(AS_Host *host)
1079 * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
1080 * Params  : host - host to finish
1081 * Notes   : This is called when a command is:
1082 *		terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONECT
1083 *	   : This must not return until all transfers are completed.
1084 */
1085static
1086void acornscsi_dma_cleanup(AS_Host *host)
1087{
1088    dmac_write(host, DMAC_MASKREG, MASK_ON);
1089    dmac_clearintr(host);
1090
1091    /*
1092     * Check for a pending transfer
1093     */
1094    if (host->dma.xfer_required) {
1095	host->dma.xfer_required = 0;
1096	if (host->dma.direction == DMA_IN)
1097	    acornscsi_data_read(host, host->dma.xfer_ptr,
1098				 host->dma.xfer_start, host->dma.xfer_length);
1099    }
1100
1101    /*
1102     * Has a transfer been setup?
1103     */
1104    if (host->dma.xfer_setup) {
1105	unsigned int transferred;
1106
1107	host->dma.xfer_setup = 0;
1108
1109#if (DEBUG & DEBUG_DMA)
1110	DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
1111#endif
1112
1113	/*
1114	 * Calculate number of bytes transferred from DMA.
1115	 */
1116	transferred = dmac_address(host) - host->dma.start_addr;
1117	host->dma.transferred += transferred;
1118
1119	if (host->dma.direction == DMA_IN)
1120	    acornscsi_data_read(host, host->scsi.SCp.ptr,
1121				 host->dma.start_addr, transferred);
1122
1123	/*
1124	 * Update SCSI pointers
1125	 */
1126	acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1127#if (DEBUG & DEBUG_DMA)
1128	DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
1129#endif
1130    }
1131}
1132
1133/*
1134 * Function: void acornscsi_dmacintr(AS_Host *host)
1135 * Purpose : handle interrupts from DMAC device
1136 * Params  : host - host to process
1137 * Notes   : If reading, we schedule the read to main memory &
1138 *	     allow the transfer to continue.
1139 *	   : If writing, we fill the onboard DMA memory from main
1140 *	     memory.
1141 *	   : Called whenever DMAC finished it's current transfer.
1142 */
1143static
1144void acornscsi_dma_intr(AS_Host *host)
1145{
1146    unsigned int address, length, transferred;
1147
1148#if (DEBUG & DEBUG_DMA)
1149    DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
1150#endif
1151
1152    dmac_write(host, DMAC_MASKREG, MASK_ON);
1153    dmac_clearintr(host);
1154
1155    /*
1156     * Calculate amount transferred via DMA
1157     */
1158    transferred = dmac_address(host) - host->dma.start_addr;
1159    host->dma.transferred += transferred;
1160
1161    /*
1162     * Schedule DMA transfer off board
1163     */
1164    if (host->dma.direction == DMA_IN) {
1165	host->dma.xfer_start = host->dma.start_addr;
1166	host->dma.xfer_length = transferred;
1167	host->dma.xfer_ptr = host->scsi.SCp.ptr;
1168	host->dma.xfer_required = 1;
1169    }
1170
1171    acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1172
1173    /*
1174     * Allocate some buffer space, limited to half the on-board RAM size
1175     */
1176    length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1177    if (length) {
1178	host->dma.start_addr = address = host->dma.free_addr;
1179	host->dma.free_addr = (host->dma.free_addr + length) &
1180				(DMAC_BUFFER_SIZE - 1);
1181
1182	/*
1183	 * Transfer data to DMA memory
1184	 */
1185	if (host->dma.direction == DMA_OUT)
1186	    acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1187				length);
1188
1189	length -= 1;
1190	dmac_write(host, DMAC_TXCNTLO, length);
1191	dmac_write(host, DMAC_TXCNTHI, length >> 8);
1192	dmac_write(host, DMAC_TXADRLO, address);
1193	dmac_write(host, DMAC_TXADRMD, address >> 8);
1194	dmac_write(host, DMAC_TXADRHI, 0);
1195	dmac_write(host, DMAC_MASKREG, MASK_OFF);
1196
1197#if (DEBUG & DEBUG_DMA)
1198	DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
1199#endif
1200    } else {
1201	host->dma.xfer_setup = 0;
1202    }
1203}
1204
1205/*
1206 * Function: void acornscsi_dma_xfer(AS_Host *host)
1207 * Purpose : transfer data between AcornSCSI and memory
1208 * Params  : host - host to process
1209 */
1210static
1211void acornscsi_dma_xfer(AS_Host *host)
1212{
1213    host->dma.xfer_required = 0;
1214
1215    if (host->dma.direction == DMA_IN)
1216	acornscsi_data_read(host, host->dma.xfer_ptr,
1217				host->dma.xfer_start, host->dma.xfer_length);
1218}
1219
1220/*
1221 * Function: void acornscsi_dma_adjust(AS_Host *host)
1222 * Purpose : adjust DMA pointers & count for bytes transferred to
1223 *	     SBIC but not SCSI bus.
1224 * Params  : host - host to adjust DMA count for
1225 */
1226static
1227void acornscsi_dma_adjust(AS_Host *host)
1228{
1229    if (host->dma.xfer_setup) {
1230	signed long transferred;
1231#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1232	DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
1233#endif
1234	/*
1235	 * Calculate correct DMA address - DMA is ahead of SCSI bus while
1236	 * writing.
1237	 *  host->scsi.SCp.scsi_xferred is the number of bytes
1238	 *  actually transferred to/from the SCSI bus.
1239	 *  host->dma.transferred is the number of bytes transferred
1240	 *  over DMA since host->dma.start_addr was last set.
1241	 *
1242	 * real_dma_addr = host->dma.start_addr + host->scsi.SCp.scsi_xferred
1243	 *		   - host->dma.transferred
1244	 */
1245	transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
1246	if (transferred < 0)
1247	    printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1248		    host->host->host_no, acornscsi_target(host), transferred);
1249	else if (transferred == 0)
1250	    host->dma.xfer_setup = 0;
1251	else {
1252	    transferred += host->dma.start_addr;
1253	    dmac_write(host, DMAC_TXADRLO, transferred);
1254	    dmac_write(host, DMAC_TXADRMD, transferred >> 8);
1255	    dmac_write(host, DMAC_TXADRHI, transferred >> 16);
1256#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1257	    DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
1258#endif
1259	}
1260    }
1261}
1262#endif
1263
1264/* =========================================================================================
1265 * Data I/O
1266 */
1267static int
1268acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
1269{
1270	unsigned int asr, timeout = max_timeout;
1271	int my_ptr = *ptr;
1272
1273	while (my_ptr < len) {
1274		asr = sbic_arm_read(host, SBIC_ASR);
1275
1276		if (asr & ASR_DBR) {
1277			timeout = max_timeout;
1278
1279			sbic_arm_write(host, SBIC_DATA, bytes[my_ptr++]);
1280		} else if (asr & ASR_INT)
1281			break;
1282		else if (--timeout == 0)
1283			break;
1284		udelay(1);
1285	}
1286
1287	*ptr = my_ptr;
1288
1289	return (timeout == 0) ? -1 : 0;
1290}
1291
1292/*
1293 * Function: void acornscsi_sendcommand(AS_Host *host)
1294 * Purpose : send a command to a target
1295 * Params  : host - host which is connected to target
1296 */
1297static void
1298acornscsi_sendcommand(AS_Host *host)
1299{
1300	struct scsi_cmnd *SCpnt = host->SCpnt;
1301
1302    sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1303    sbic_arm_writenext(host, 0);
1304    sbic_arm_writenext(host, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1305
1306    acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1307
1308    if (acornscsi_write_pio(host, SCpnt->cmnd,
1309	(int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
1310	printk("scsi%d: timeout while sending command\n", host->host->host_no);
1311
1312    host->scsi.phase = PHASE_COMMAND;
1313}
1314
1315static
1316void acornscsi_sendmessage(AS_Host *host)
1317{
1318    unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
1319    unsigned int msgnr;
1320    struct message *msg;
1321
1322#if (DEBUG & DEBUG_MESSAGES)
1323    printk("scsi%d.%c: sending message ",
1324	    host->host->host_no, acornscsi_target(host));
1325#endif
1326
1327    switch (message_length) {
1328    case 0:
1329	acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1330
1331	acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
1332
1333	sbic_arm_write(host, SBIC_DATA, NOP);
1334
1335	host->scsi.last_message = NOP;
1336#if (DEBUG & DEBUG_MESSAGES)
1337	printk("NOP");
1338#endif
1339	break;
1340
1341    case 1:
1342	acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1343	msg = msgqueue_getmsg(&host->scsi.msgs, 0);
1344
1345	acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
1346
1347	sbic_arm_write(host, SBIC_DATA, msg->msg[0]);
1348
1349	host->scsi.last_message = msg->msg[0];
1350#if (DEBUG & DEBUG_MESSAGES)
1351	spi_print_msg(msg->msg);
1352#endif
1353	break;
1354
1355    default:
1356	/*
1357	 * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
1358	 * 'When a target sends this (MESSAGE_REJECT) message, it
1359	 *  shall change to MESSAGE IN phase and send this message
1360	 *  prior to requesting additional message bytes from the
1361	 *  initiator.  This provides an interlock so that the
1362	 *  initiator can determine which message byte is rejected.
1363	 */
1364	sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1365	sbic_arm_writenext(host, 0);
1366	sbic_arm_writenext(host, message_length);
1367	acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1368
1369	msgnr = 0;
1370	while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
1371	    unsigned int i;
1372#if (DEBUG & DEBUG_MESSAGES)
1373	    spi_print_msg(msg);
1374#endif
1375	    i = 0;
1376	    if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
1377		printk("scsi%d: timeout while sending message\n", host->host->host_no);
1378
1379	    host->scsi.last_message = msg->msg[0];
1380	    if (msg->msg[0] == EXTENDED_MESSAGE)
1381		host->scsi.last_message |= msg->msg[2] << 8;
1382
1383	    if (i != msg->length)
1384		break;
1385	}
1386	break;
1387    }
1388#if (DEBUG & DEBUG_MESSAGES)
1389    printk("\n");
1390#endif
1391}
1392
1393/*
1394 * Function: void acornscsi_readstatusbyte(AS_Host *host)
1395 * Purpose : Read status byte from connected target
1396 * Params  : host - host connected to target
1397 */
1398static
1399void acornscsi_readstatusbyte(AS_Host *host)
1400{
1401    acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
1402    acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
1403    host->scsi.SCp.Status = sbic_arm_read(host, SBIC_DATA);
1404}
1405
1406/*
1407 * Function: unsigned char acornscsi_readmessagebyte(AS_Host *host)
1408 * Purpose : Read one message byte from connected target
1409 * Params  : host - host connected to target
1410 */
1411static
1412unsigned char acornscsi_readmessagebyte(AS_Host *host)
1413{
1414    unsigned char message;
1415
1416    acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1417
1418    acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
1419
1420    message = sbic_arm_read(host, SBIC_DATA);
1421
1422    /* wait for MSGIN-XFER-PAUSED */
1423    acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
1424
1425    sbic_arm_read(host, SBIC_SSR);
1426
1427    return message;
1428}
1429
1430/*
1431 * Function: void acornscsi_message(AS_Host *host)
1432 * Purpose : Read complete message from connected target & action message
1433 * Params  : host - host connected to target
1434 */
1435static
1436void acornscsi_message(AS_Host *host)
1437{
1438    unsigned char message[16];
1439    unsigned int msgidx = 0, msglen = 1;
1440
1441    do {
1442	message[msgidx] = acornscsi_readmessagebyte(host);
1443
1444	switch (msgidx) {
1445	case 0:
1446	    if (message[0] == EXTENDED_MESSAGE ||
1447		(message[0] >= 0x20 && message[0] <= 0x2f))
1448		msglen = 2;
1449	    break;
1450
1451	case 1:
1452	    if (message[0] == EXTENDED_MESSAGE)
1453		msglen += message[msgidx];
1454	    break;
1455	}
1456	msgidx += 1;
1457	if (msgidx < msglen) {
1458	    acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1459
1460	    /* wait for next msg-in */
1461	    acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
1462	    sbic_arm_read(host, SBIC_SSR);
1463	}
1464    } while (msgidx < msglen);
1465
1466#if (DEBUG & DEBUG_MESSAGES)
1467    printk("scsi%d.%c: message in: ",
1468	    host->host->host_no, acornscsi_target(host));
1469    spi_print_msg(message);
1470    printk("\n");
1471#endif
1472
1473    if (host->scsi.phase == PHASE_RECONNECTED) {
1474	/*
1475	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1476	 * 'Whenever a target reconnects to an initiator to continue
1477	 *  a tagged I/O process, the SIMPLE QUEUE TAG message shall
1478	 *  be sent immediately following the IDENTIFY message...'
1479	 */
1480	if (message[0] == SIMPLE_QUEUE_TAG)
1481	    host->scsi.reconnected.tag = message[1];
1482	if (acornscsi_reconnect_finish(host))
1483	    host->scsi.phase = PHASE_MSGIN;
1484    }
1485
1486    switch (message[0]) {
1487    case ABORT:
1488    case ABORT_TAG:
1489    case COMMAND_COMPLETE:
1490	if (host->scsi.phase != PHASE_STATUSIN) {
1491	    printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1492		    host->host->host_no, acornscsi_target(host));
1493	    acornscsi_dumplog(host, host->SCpnt->device->id);
1494	}
1495	host->scsi.phase = PHASE_DONE;
1496	host->scsi.SCp.Message = message[0];
1497	break;
1498
1499    case SAVE_POINTERS:
1500	/*
1501	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
1502	 * 'The SAVE DATA POINTER message is sent from a target to
1503	 *  direct the initiator to copy the active data pointer to
1504	 *  the saved data pointer for the current I/O process.
1505	 */
1506	acornscsi_dma_cleanup(host);
1507	host->SCpnt->SCp = host->scsi.SCp;
1508	host->SCpnt->SCp.sent_command = 0;
1509	host->scsi.phase = PHASE_MSGIN;
1510	break;
1511
1512    case RESTORE_POINTERS:
1513	/*
1514	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
1515	 * 'The RESTORE POINTERS message is sent from a target to
1516	 *  direct the initiator to copy the most recently saved
1517	 *  command, data, and status pointers for the I/O process
1518	 *  to the corresponding active pointers.  The command and
1519	 *  status pointers shall be restored to the beginning of
1520	 *  the present command and status areas.'
1521	 */
1522	acornscsi_dma_cleanup(host);
1523	host->scsi.SCp = host->SCpnt->SCp;
1524	host->scsi.phase = PHASE_MSGIN;
1525	break;
1526
1527    case DISCONNECT:
1528	/*
1529	 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
1530	 * 'On those occasions when an error or exception condition occurs
1531	 *  and the target elects to repeat the information transfer, the
1532	 *  target may repeat the transfer either issuing a RESTORE POINTERS
1533	 *  message or by disconnecting without issuing a SAVE POINTERS
1534	 *  message.  When reconnection is completed, the most recent
1535	 *  saved pointer values are restored.'
1536	 */
1537	acornscsi_dma_cleanup(host);
1538	host->scsi.phase = PHASE_DISCONNECT;
1539	break;
1540
1541    case MESSAGE_REJECT:
1542
1543	/*
1544	 * If we have any messages waiting to go out, then assert ATN now
1545	 */
1546	if (msgqueue_msglength(&host->scsi.msgs))
1547	    acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1548
1549	switch (host->scsi.last_message) {
1550#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1551	case HEAD_OF_QUEUE_TAG:
1552	case ORDERED_QUEUE_TAG:
1553	case SIMPLE_QUEUE_TAG:
1554	    /*
1555	     * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1556	     *  If a target does not implement tagged queuing and a queue tag
1557	     *  message is received, it shall respond with a MESSAGE REJECT
1558	     *  message and accept the I/O process as if it were untagged.
1559	     */
1560	    printk(KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
1561		    host->host->host_no, acornscsi_target(host));
1562	    host->SCpnt->device->simple_tags = 0;
1563	    set_bit(host->SCpnt->device->id * 8 + host->SCpnt->device->lun, host->busyluns);
1564	    break;
1565#endif
1566	case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1567	    /*
1568	     * Target can't handle synchronous transfers
1569	     */
1570	    printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1571		    host->host->host_no, acornscsi_target(host));
1572	    host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
1573	    host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
1574	    sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1575	    break;
1576
1577	default:
1578	    break;
1579	}
1580	break;
1581
1582    case QUEUE_FULL:
1583	/* TODO: target queue is full */
1584	break;
1585
1586    case SIMPLE_QUEUE_TAG:
1587	/* tag queue reconnect... message[1] = queue tag.  Print something to indicate something happened! */
1588	printk("scsi%d.%c: reconnect queue tag %02X\n",
1589		host->host->host_no, acornscsi_target(host),
1590		message[1]);
1591	break;
1592
1593    case EXTENDED_MESSAGE:
1594	switch (message[2]) {
1595#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1596	case EXTENDED_SDTR:
1597	    if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
1598		/*
1599		 * We requested synchronous transfers.  This isn't quite right...
1600		 * We can only say if this succeeded if we proceed on to execute the
1601		 * command from this message.  If we get a MESSAGE PARITY ERROR,
1602		 * and the target retries fail, then we fallback to asynchronous mode
1603		 */
1604		host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
1605		printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1606			host->host->host_no, acornscsi_target(host),
1607			message[4], message[3] * 4);
1608		host->device[host->SCpnt->device->id].sync_xfer =
1609			calc_sync_xfer(message[3] * 4, message[4]);
1610	    } else {
1611		unsigned char period, length;
1612		/*
1613		 * Target requested synchronous transfers.  The agreement is only
1614		 * to be in operation AFTER the target leaves message out phase.
1615		 */
1616		acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1617		period = max_t(unsigned int, message[3], sdtr_period / 4);
1618		length = min_t(unsigned int, message[4], sdtr_size);
1619		msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1620				 EXTENDED_SDTR, period, length);
1621		host->device[host->SCpnt->device->id].sync_xfer =
1622			calc_sync_xfer(period * 4, length);
1623	    }
1624	    sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1625	    break;
1626#else
1627	    /* We do not accept synchronous transfers.  Respond with a
1628	     * MESSAGE_REJECT.
1629	     */
1630#endif
1631
1632	case EXTENDED_WDTR:
1633	    /* The WD33C93A is only 8-bit.  We respond with a MESSAGE_REJECT
1634	     * to a wide data transfer request.
1635	     */
1636	default:
1637	    acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1638	    msgqueue_flush(&host->scsi.msgs);
1639	    msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1640	    break;
1641	}
1642	break;
1643
1644#ifdef CONFIG_SCSI_ACORNSCSI_LINK
1645    case LINKED_CMD_COMPLETE:
1646    case LINKED_FLG_CMD_COMPLETE:
1647	/*
1648	 * We don't support linked commands yet
1649	 */
1650	if (0) {
1651#if (DEBUG & DEBUG_LINK)
1652	    printk("scsi%d.%c: lun %d tag %d linked command complete\n",
1653		    host->host->host_no, acornscsi_target(host), host->SCpnt->tag);
1654#endif
1655	    /*
1656	     * A linked command should only terminate with one of these messages
1657	     * if there are more linked commands available.
1658	     */
1659	    if (!host->SCpnt->next_link) {
1660		printk(KERN_WARNING "scsi%d.%c: lun %d tag %d linked command complete, but no next_link\n",
1661			instance->host_no, acornscsi_target(host), host->SCpnt->tag);
1662		acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1663		msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1664	    } else {
1665		struct scsi_cmnd *SCpnt = host->SCpnt;
1666
1667		acornscsi_dma_cleanup(host);
1668
1669		host->SCpnt = host->SCpnt->next_link;
1670		host->SCpnt->tag = SCpnt->tag;
1671		SCpnt->result = DID_OK | host->scsi.SCp.Message << 8 | host->Scsi.SCp.Status;
1672		SCpnt->done(SCpnt);
1673
1674		/* initialise host->SCpnt->SCp */
1675	    }
1676	    break;
1677	}
1678#endif
1679
1680    default: /* reject message */
1681	printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1682		host->host->host_no, acornscsi_target(host),
1683		message[0]);
1684	acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1685	msgqueue_flush(&host->scsi.msgs);
1686	msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1687	host->scsi.phase = PHASE_MSGIN;
1688	break;
1689    }
1690    acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1691}
1692
1693/*
1694 * Function: int acornscsi_buildmessages(AS_Host *host)
1695 * Purpose : build the connection messages for a host
1696 * Params  : host - host to add messages to
1697 */
1698static
1699void acornscsi_buildmessages(AS_Host *host)
1700{
1701
1702    msgqueue_addmsg(&host->scsi.msgs, 1,
1703		     IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
1704			     host->SCpnt->device->lun));
1705
1706
1707#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1708    if (host->SCpnt->tag) {
1709	unsigned int tag_type;
1710
1711	if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
1712	    host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
1713	    host->SCpnt->cmnd[0] == INQUIRY)
1714	    tag_type = HEAD_OF_QUEUE_TAG;
1715	else
1716	    tag_type = SIMPLE_QUEUE_TAG;
1717	msgqueue_addmsg(&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
1718    }
1719#endif
1720
1721#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1722    if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
1723	host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
1724	msgqueue_addmsg(&host->scsi.msgs, 5,
1725			 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1726			 sdtr_period / 4, sdtr_size);
1727    }
1728#endif
1729}
1730
1731/*
1732 * Function: int acornscsi_starttransfer(AS_Host *host)
1733 * Purpose : transfer data to/from connected target
1734 * Params  : host - host to which target is connected
1735 * Returns : 0 if failure
1736 */
1737static
1738int acornscsi_starttransfer(AS_Host *host)
1739{
1740    int residual;
1741
1742    if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
1743	printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1744		host->host->host_no, acornscsi_target(host));
1745	return 0;
1746    }
1747
1748    residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
1749
1750    sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1751    sbic_arm_writenext(host, residual >> 16);
1752    sbic_arm_writenext(host, residual >> 8);
1753    sbic_arm_writenext(host, residual);
1754    acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1755    return 1;
1756}
1757
1758/* =========================================================================================
1759 * Connection & Disconnection
1760 */
1761/*
1762 * Function : acornscsi_reconnect(AS_Host *host)
1763 * Purpose  : reconnect a previously disconnected command
1764 * Params   : host - host specific data
1765 * Remarks  : SCSI spec says:
1766 *		'The set of active pointers is restored from the set
1767 *		 of saved pointers upon reconnection of the I/O process'
1768 */
1769static
1770int acornscsi_reconnect(AS_Host *host)
1771{
1772    unsigned int target, lun, ok = 0;
1773
1774    target = sbic_arm_read(host, SBIC_SOURCEID);
1775
1776    if (!(target & 8))
1777	printk(KERN_ERR "scsi%d: invalid source id after reselection "
1778		"- device fault?\n",
1779		host->host->host_no);
1780
1781    target &= 7;
1782
1783    if (host->SCpnt && !host->scsi.disconnectable) {
1784	printk(KERN_ERR "scsi%d.%d: reconnected while command in "
1785		"progress to target %d?\n",
1786		host->host->host_no, target, host->SCpnt->device->id);
1787	host->SCpnt = NULL;
1788    }
1789
1790    lun = sbic_arm_read(host, SBIC_DATA) & 7;
1791
1792    host->scsi.reconnected.target = target;
1793    host->scsi.reconnected.lun = lun;
1794    host->scsi.reconnected.tag = 0;
1795
1796    if (host->scsi.disconnectable && host->SCpnt &&
1797	host->SCpnt->device->id == target && host->SCpnt->device->lun == lun)
1798	ok = 1;
1799
1800    if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
1801	ok = 1;
1802
1803    ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1804
1805    if (ok) {
1806	host->scsi.phase = PHASE_RECONNECTED;
1807    } else {
1808	/* this doesn't seem to work */
1809	printk(KERN_ERR "scsi%d.%c: reselected with no command "
1810		"to reconnect with\n",
1811		host->host->host_no, '0' + target);
1812	acornscsi_dumplog(host, target);
1813	acornscsi_abortcmd(host, 0);
1814	if (host->SCpnt) {
1815	    queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1816	    host->SCpnt = NULL;
1817	}
1818    }
1819    acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1820    return !ok;
1821}
1822
1823/*
1824 * Function: int acornscsi_reconect_finish(AS_Host *host)
1825 * Purpose : finish reconnecting a command
1826 * Params  : host - host to complete
1827 * Returns : 0 if failed
1828 */
1829static
1830int acornscsi_reconnect_finish(AS_Host *host)
1831{
1832    if (host->scsi.disconnectable && host->SCpnt) {
1833	host->scsi.disconnectable = 0;
1834	if (host->SCpnt->device->id  == host->scsi.reconnected.target &&
1835	    host->SCpnt->device->lun == host->scsi.reconnected.lun &&
1836	    host->SCpnt->tag         == host->scsi.reconnected.tag) {
1837#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1838	    DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
1839		    host->host->host_no, acornscsi_target(host)));
1840#endif
1841	} else {
1842	    queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1843#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1844	    DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
1845		    "to disconnected queue\n",
1846		    host->host->host_no, acornscsi_target(host)));
1847#endif
1848	    host->SCpnt = NULL;
1849	}
1850    }
1851    if (!host->SCpnt) {
1852	host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
1853				host->scsi.reconnected.target,
1854				host->scsi.reconnected.lun,
1855				host->scsi.reconnected.tag);
1856#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1857	DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
1858		host->host->host_no, acornscsi_target(host)));
1859#endif
1860    }
1861
1862    if (!host->SCpnt)
1863	acornscsi_abortcmd(host, host->scsi.reconnected.tag);
1864    else {
1865	/*
1866	 * Restore data pointer from SAVED pointers.
1867	 */
1868	host->scsi.SCp = host->SCpnt->SCp;
1869#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1870	printk(", data pointers: [%p, %X]",
1871		host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1872#endif
1873    }
1874#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1875    printk("\n");
1876#endif
1877
1878    host->dma.transferred = host->scsi.SCp.scsi_xferred;
1879
1880    return host->SCpnt != NULL;
1881}
1882
1883/*
1884 * Function: void acornscsi_disconnect_unexpected(AS_Host *host)
1885 * Purpose : handle an unexpected disconnect
1886 * Params  : host - host on which disconnect occurred
1887 */
1888static
1889void acornscsi_disconnect_unexpected(AS_Host *host)
1890{
1891    printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1892	    host->host->host_no, acornscsi_target(host));
1893#if (DEBUG & DEBUG_ABORT)
1894    acornscsi_dumplog(host, 8);
1895#endif
1896
1897    acornscsi_done(host, &host->SCpnt, DID_ERROR);
1898}
1899
1900/*
1901 * Function: void acornscsi_abortcmd(AS_host *host, unsigned char tag)
1902 * Purpose : abort a currently executing command
1903 * Params  : host - host with connected command to abort
1904 *	     tag  - tag to abort
1905 */
1906static
1907void acornscsi_abortcmd(AS_Host *host, unsigned char tag)
1908{
1909    host->scsi.phase = PHASE_ABORTED;
1910    sbic_arm_write(host, SBIC_CMND, CMND_ASSERTATN);
1911
1912    msgqueue_flush(&host->scsi.msgs);
1913#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1914    if (tag)
1915	msgqueue_addmsg(&host->scsi.msgs, 2, ABORT_TAG, tag);
1916    else
1917#endif
1918	msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1919}
1920
1921/* ==========================================================================================
1922 * Interrupt routines.
1923 */
1924/*
1925 * Function: int acornscsi_sbicintr(AS_Host *host)
1926 * Purpose : handle interrupts from SCSI device
1927 * Params  : host - host to process
1928 * Returns : INTR_PROCESS if expecting another SBIC interrupt
1929 *	     INTR_IDLE if no interrupt
1930 *	     INTR_NEXT_COMMAND if we have finished processing the command
1931 */
1932static
1933intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
1934{
1935    unsigned int asr, ssr;
1936
1937    asr = sbic_arm_read(host, SBIC_ASR);
1938    if (!(asr & ASR_INT))
1939	return INTR_IDLE;
1940
1941    ssr = sbic_arm_read(host, SBIC_SSR);
1942
1943#if (DEBUG & DEBUG_PHASES)
1944    print_sbic_status(asr, ssr, host->scsi.phase);
1945#endif
1946
1947    ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
1948
1949    if (host->SCpnt && !host->scsi.disconnectable)
1950	ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
1951
1952    switch (ssr) {
1953    case 0x00:				/* reset state - not advanced			*/
1954	printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
1955		host->host->host_no);
1956	/* setup sbic - WD33C93A */
1957	sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
1958	sbic_arm_write(host, SBIC_CMND, CMND_RESET);
1959	return INTR_IDLE;
1960
1961    case 0x01:				/* reset state - advanced			*/
1962	sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
1963	sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
1964	sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
1965	sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
1966	msgqueue_flush(&host->scsi.msgs);
1967	return INTR_IDLE;
1968
1969    case 0x41:				/* unexpected disconnect aborted command	*/
1970	acornscsi_disconnect_unexpected(host);
1971	return INTR_NEXT_COMMAND;
1972    }
1973
1974    switch (host->scsi.phase) {
1975    case PHASE_CONNECTING:		/* STATE: command removed from issue queue	*/
1976	switch (ssr) {
1977	case 0x11:			/* -> PHASE_CONNECTED				*/
1978	    /* BUS FREE -> SELECTION */
1979	    host->scsi.phase = PHASE_CONNECTED;
1980	    msgqueue_flush(&host->scsi.msgs);
1981	    host->dma.transferred = host->scsi.SCp.scsi_xferred;
1982	    /* 33C93 gives next interrupt indicating bus phase */
1983	    asr = sbic_arm_read(host, SBIC_ASR);
1984	    if (!(asr & ASR_INT))
1985		break;
1986	    ssr = sbic_arm_read(host, SBIC_SSR);
1987	    ADD_STATUS(8, ssr, host->scsi.phase, 1);
1988	    ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, 1);
1989	    goto connected;
1990
1991	case 0x42:			/* select timed out				*/
1992					/* -> PHASE_IDLE				*/
1993	    acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
1994	    return INTR_NEXT_COMMAND;
1995
1996	case 0x81:			/* -> PHASE_RECONNECTED or PHASE_ABORTED	*/
1997	    /* BUS FREE -> RESELECTION */
1998	    host->origSCpnt = host->SCpnt;
1999	    host->SCpnt = NULL;
2000	    msgqueue_flush(&host->scsi.msgs);
2001	    acornscsi_reconnect(host);
2002	    break;
2003
2004	default:
2005	    printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
2006		    host->host->host_no, acornscsi_target(host), ssr);
2007	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2008	    acornscsi_abortcmd(host, host->SCpnt->tag);
2009	}
2010	return INTR_PROCESSING;
2011
2012    connected:
2013    case PHASE_CONNECTED:		/* STATE: device selected ok			*/
2014	switch (ssr) {
2015#ifdef NONSTANDARD
2016	case 0x8a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2017	    /* SELECTION -> COMMAND */
2018	    acornscsi_sendcommand(host);
2019	    break;
2020
2021	case 0x8b:			/* -> PHASE_STATUS				*/
2022	    /* SELECTION -> STATUS */
2023	    acornscsi_readstatusbyte(host);
2024	    host->scsi.phase = PHASE_STATUSIN;
2025	    break;
2026#endif
2027
2028	case 0x8e:			/* -> PHASE_MSGOUT				*/
2029	    /* SELECTION ->MESSAGE OUT */
2030	    host->scsi.phase = PHASE_MSGOUT;
2031	    acornscsi_buildmessages(host);
2032	    acornscsi_sendmessage(host);
2033	    break;
2034
2035	/* these should not happen */
2036	case 0x85:			/* target disconnected				*/
2037	    acornscsi_done(host, &host->SCpnt, DID_ERROR);
2038	    break;
2039
2040	default:
2041	    printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
2042		    host->host->host_no, acornscsi_target(host), ssr);
2043	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2044	    acornscsi_abortcmd(host, host->SCpnt->tag);
2045	}
2046	return INTR_PROCESSING;
2047
2048    case PHASE_MSGOUT:			/* STATE: connected & sent IDENTIFY message	*/
2049	/*
2050	 * SCSI standard says that MESSAGE OUT phases can be followed by a
2051	 * DATA phase, STATUS phase, MESSAGE IN phase or COMMAND phase
2052	 */
2053	switch (ssr) {
2054	case 0x8a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2055	case 0x1a:			/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2056	    /* MESSAGE OUT -> COMMAND */
2057	    acornscsi_sendcommand(host);
2058	    break;
2059
2060	case 0x8b:			/* -> PHASE_STATUS				*/
2061	case 0x1b:			/* -> PHASE_STATUS				*/
2062	    /* MESSAGE OUT -> STATUS */
2063	    acornscsi_readstatusbyte(host);
2064	    host->scsi.phase = PHASE_STATUSIN;
2065	    break;
2066
2067	case 0x8e:			/* -> PHASE_MSGOUT				*/
2068	    /* MESSAGE_OUT(MESSAGE_IN) ->MESSAGE OUT */
2069	    acornscsi_sendmessage(host);
2070	    break;
2071
2072	case 0x4f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2073	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2074	    /* MESSAGE OUT -> MESSAGE IN */
2075	    acornscsi_message(host);
2076	    break;
2077
2078	default:
2079	    printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
2080		    host->host->host_no, acornscsi_target(host), ssr);
2081	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2082	}
2083	return INTR_PROCESSING;
2084
2085    case PHASE_COMMAND: 		/* STATE: connected & command sent		*/
2086	switch (ssr) {
2087	case 0x18:			/* -> PHASE_DATAOUT				*/
2088	    /* COMMAND -> DATA OUT */
2089	    if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2090		acornscsi_abortcmd(host, host->SCpnt->tag);
2091	    acornscsi_dma_setup(host, DMA_OUT);
2092	    if (!acornscsi_starttransfer(host))
2093		acornscsi_abortcmd(host, host->SCpnt->tag);
2094	    host->scsi.phase = PHASE_DATAOUT;
2095	    return INTR_IDLE;
2096
2097	case 0x19:			/* -> PHASE_DATAIN				*/
2098	    /* COMMAND -> DATA IN */
2099	    if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2100		acornscsi_abortcmd(host, host->SCpnt->tag);
2101	    acornscsi_dma_setup(host, DMA_IN);
2102	    if (!acornscsi_starttransfer(host))
2103		acornscsi_abortcmd(host, host->SCpnt->tag);
2104	    host->scsi.phase = PHASE_DATAIN;
2105	    return INTR_IDLE;
2106
2107	case 0x1b:			/* -> PHASE_STATUS				*/
2108	    /* COMMAND -> STATUS */
2109	    acornscsi_readstatusbyte(host);
2110	    host->scsi.phase = PHASE_STATUSIN;
2111	    break;
2112
2113	case 0x1e:			/* -> PHASE_MSGOUT				*/
2114	    /* COMMAND -> MESSAGE OUT */
2115	    acornscsi_sendmessage(host);
2116	    break;
2117
2118	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2119	    /* COMMAND -> MESSAGE IN */
2120	    acornscsi_message(host);
2121	    break;
2122
2123	default:
2124	    printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2125		    host->host->host_no, acornscsi_target(host), ssr);
2126	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2127	}
2128	return INTR_PROCESSING;
2129
2130    case PHASE_DISCONNECT:		/* STATE: connected, received DISCONNECT msg	*/
2131	if (ssr == 0x85) {		/* -> PHASE_IDLE				*/
2132	    host->scsi.disconnectable = 1;
2133	    host->scsi.reconnected.tag = 0;
2134	    host->scsi.phase = PHASE_IDLE;
2135	    host->stats.disconnects += 1;
2136	} else {
2137	    printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2138		    host->host->host_no, acornscsi_target(host), ssr);
2139	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2140	}
2141	return INTR_NEXT_COMMAND;
2142
2143    case PHASE_IDLE:			/* STATE: disconnected				*/
2144	if (ssr == 0x81)		/* -> PHASE_RECONNECTED or PHASE_ABORTED	*/
2145	    acornscsi_reconnect(host);
2146	else {
2147	    printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2148		    host->host->host_no, acornscsi_target(host), ssr);
2149	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2150	}
2151	return INTR_PROCESSING;
2152
2153    case PHASE_RECONNECTED:		/* STATE: device reconnected to initiator	*/
2154	/*
2155	 * Command reconnected - if MESGIN, get message - it may be
2156	 * the tag.  If not, get command out of disconnected queue
2157	 */
2158	/*
2159	 * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
2160	 * reconnect I_T_L command
2161	 */
2162	if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
2163	    return INTR_IDLE;
2164	ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
2165	switch (ssr) {
2166	case 0x88:			/* data out phase				*/
2167					/* -> PHASE_DATAOUT				*/
2168	    /* MESSAGE IN -> DATA OUT */
2169	    acornscsi_dma_setup(host, DMA_OUT);
2170	    if (!acornscsi_starttransfer(host))
2171		acornscsi_abortcmd(host, host->SCpnt->tag);
2172	    host->scsi.phase = PHASE_DATAOUT;
2173	    return INTR_IDLE;
2174
2175	case 0x89:			/* data in phase				*/
2176					/* -> PHASE_DATAIN				*/
2177	    /* MESSAGE IN -> DATA IN */
2178	    acornscsi_dma_setup(host, DMA_IN);
2179	    if (!acornscsi_starttransfer(host))
2180		acornscsi_abortcmd(host, host->SCpnt->tag);
2181	    host->scsi.phase = PHASE_DATAIN;
2182	    return INTR_IDLE;
2183
2184	case 0x8a:			/* command out					*/
2185	    /* MESSAGE IN -> COMMAND */
2186	    acornscsi_sendcommand(host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED	*/
2187	    break;
2188
2189	case 0x8b:			/* status in					*/
2190					/* -> PHASE_STATUSIN				*/
2191	    /* MESSAGE IN -> STATUS */
2192	    acornscsi_readstatusbyte(host);
2193	    host->scsi.phase = PHASE_STATUSIN;
2194	    break;
2195
2196	case 0x8e:			/* message out					*/
2197					/* -> PHASE_MSGOUT				*/
2198	    /* MESSAGE IN -> MESSAGE OUT */
2199	    acornscsi_sendmessage(host);
2200	    break;
2201
2202	case 0x8f:			/* message in					*/
2203	    acornscsi_message(host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2204	    break;
2205
2206	default:
2207	    printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2208		    host->host->host_no, acornscsi_target(host), ssr);
2209	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2210	}
2211	return INTR_PROCESSING;
2212
2213    case PHASE_DATAIN:			/* STATE: transferred data in			*/
2214	/*
2215	 * This is simple - if we disconnect then the DMA address & count is
2216	 * correct.
2217	 */
2218	switch (ssr) {
2219	case 0x19:			/* -> PHASE_DATAIN				*/
2220	case 0x89:			/* -> PHASE_DATAIN				*/
2221	    acornscsi_abortcmd(host, host->SCpnt->tag);
2222	    return INTR_IDLE;
2223
2224	case 0x1b:			/* -> PHASE_STATUSIN				*/
2225	case 0x4b:			/* -> PHASE_STATUSIN				*/
2226	case 0x8b:			/* -> PHASE_STATUSIN				*/
2227	    /* DATA IN -> STATUS */
2228	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2229					  acornscsi_sbic_xfcount(host);
2230	    acornscsi_dma_stop(host);
2231	    acornscsi_readstatusbyte(host);
2232	    host->scsi.phase = PHASE_STATUSIN;
2233	    break;
2234
2235	case 0x1e:			/* -> PHASE_MSGOUT				*/
2236	case 0x4e:			/* -> PHASE_MSGOUT				*/
2237	case 0x8e:			/* -> PHASE_MSGOUT				*/
2238	    /* DATA IN -> MESSAGE OUT */
2239	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2240					  acornscsi_sbic_xfcount(host);
2241	    acornscsi_dma_stop(host);
2242	    acornscsi_sendmessage(host);
2243	    break;
2244
2245	case 0x1f:			/* message in					*/
2246	case 0x4f:			/* message in					*/
2247	case 0x8f:			/* message in					*/
2248	    /* DATA IN -> MESSAGE IN */
2249	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2250					  acornscsi_sbic_xfcount(host);
2251	    acornscsi_dma_stop(host);
2252	    acornscsi_message(host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2253	    break;
2254
2255	default:
2256	    printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2257		    host->host->host_no, acornscsi_target(host), ssr);
2258	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2259	}
2260	return INTR_PROCESSING;
2261
2262    case PHASE_DATAOUT: 		/* STATE: transferred data out			*/
2263	/*
2264	 * This is more complicated - if we disconnect, the DMA could be 12
2265	 * bytes ahead of us.  We need to correct this.
2266	 */
2267	switch (ssr) {
2268	case 0x18:			/* -> PHASE_DATAOUT				*/
2269	case 0x88:			/* -> PHASE_DATAOUT				*/
2270	    acornscsi_abortcmd(host, host->SCpnt->tag);
2271	    return INTR_IDLE;
2272
2273	case 0x1b:			/* -> PHASE_STATUSIN				*/
2274	case 0x4b:			/* -> PHASE_STATUSIN				*/
2275	case 0x8b:			/* -> PHASE_STATUSIN				*/
2276	    /* DATA OUT -> STATUS */
2277	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2278					  acornscsi_sbic_xfcount(host);
2279	    acornscsi_dma_stop(host);
2280	    acornscsi_dma_adjust(host);
2281	    acornscsi_readstatusbyte(host);
2282	    host->scsi.phase = PHASE_STATUSIN;
2283	    break;
2284
2285	case 0x1e:			/* -> PHASE_MSGOUT				*/
2286	case 0x4e:			/* -> PHASE_MSGOUT				*/
2287	case 0x8e:			/* -> PHASE_MSGOUT				*/
2288	    /* DATA OUT -> MESSAGE OUT */
2289	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2290					  acornscsi_sbic_xfcount(host);
2291	    acornscsi_dma_stop(host);
2292	    acornscsi_dma_adjust(host);
2293	    acornscsi_sendmessage(host);
2294	    break;
2295
2296	case 0x1f:			/* message in					*/
2297	case 0x4f:			/* message in					*/
2298	case 0x8f:			/* message in					*/
2299	    /* DATA OUT -> MESSAGE IN */
2300	    host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2301					  acornscsi_sbic_xfcount(host);
2302	    acornscsi_dma_stop(host);
2303	    acornscsi_dma_adjust(host);
2304	    acornscsi_message(host);	/* -> PHASE_MSGIN, PHASE_DISCONNECT		*/
2305	    break;
2306
2307	default:
2308	    printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2309		    host->host->host_no, acornscsi_target(host), ssr);
2310	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2311	}
2312	return INTR_PROCESSING;
2313
2314    case PHASE_STATUSIN:		/* STATE: status in complete			*/
2315	switch (ssr) {
2316	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2317	case 0x8f:			/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2318	    /* STATUS -> MESSAGE IN */
2319	    acornscsi_message(host);
2320	    break;
2321
2322	case 0x1e:			/* -> PHASE_MSGOUT				*/
2323	case 0x8e:			/* -> PHASE_MSGOUT				*/
2324	    /* STATUS -> MESSAGE OUT */
2325	    acornscsi_sendmessage(host);
2326	    break;
2327
2328	default:
2329	    printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2330		    host->host->host_no, acornscsi_target(host), ssr);
2331	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2332	}
2333	return INTR_PROCESSING;
2334
2335    case PHASE_MSGIN:			/* STATE: message in				*/
2336	switch (ssr) {
2337	case 0x1e:			/* -> PHASE_MSGOUT				*/
2338	case 0x4e:			/* -> PHASE_MSGOUT				*/
2339	case 0x8e:			/* -> PHASE_MSGOUT				*/
2340	    /* MESSAGE IN -> MESSAGE OUT */
2341	    acornscsi_sendmessage(host);
2342	    break;
2343
2344	case 0x1f:			/* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2345	case 0x2f:
2346	case 0x4f:
2347	case 0x8f:
2348	    acornscsi_message(host);
2349	    break;
2350
2351	case 0x85:
2352	    printk("scsi%d.%c: strange message in disconnection\n",
2353		host->host->host_no, acornscsi_target(host));
2354	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2355	    acornscsi_done(host, &host->SCpnt, DID_ERROR);
2356	    break;
2357
2358	default:
2359	    printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2360		    host->host->host_no, acornscsi_target(host), ssr);
2361	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2362	}
2363	return INTR_PROCESSING;
2364
2365    case PHASE_DONE:			/* STATE: received status & message		*/
2366	switch (ssr) {
2367	case 0x85:			/* -> PHASE_IDLE				*/
2368	    acornscsi_done(host, &host->SCpnt, DID_OK);
2369	    return INTR_NEXT_COMMAND;
2370
2371	case 0x1e:
2372	case 0x8e:
2373	    acornscsi_sendmessage(host);
2374	    break;
2375
2376	default:
2377	    printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2378		    host->host->host_no, acornscsi_target(host), ssr);
2379	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2380	}
2381	return INTR_PROCESSING;
2382
2383    case PHASE_ABORTED:
2384	switch (ssr) {
2385	case 0x85:
2386	    if (host->SCpnt)
2387		acornscsi_done(host, &host->SCpnt, DID_ABORT);
2388	    else {
2389		clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
2390			  host->busyluns);
2391		host->scsi.phase = PHASE_IDLE;
2392	    }
2393	    return INTR_NEXT_COMMAND;
2394
2395	case 0x1e:
2396	case 0x2e:
2397	case 0x4e:
2398	case 0x8e:
2399	    acornscsi_sendmessage(host);
2400	    break;
2401
2402	default:
2403	    printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2404		    host->host->host_no, acornscsi_target(host), ssr);
2405	    acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2406	}
2407	return INTR_PROCESSING;
2408
2409    default:
2410	printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2411		host->host->host_no, acornscsi_target(host), ssr);
2412	acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2413    }
2414    return INTR_PROCESSING;
2415}
2416
2417/*
2418 * Prototype: void acornscsi_intr(int irq, void *dev_id)
2419 * Purpose  : handle interrupts from Acorn SCSI card
2420 * Params   : irq    - interrupt number
2421 *	      dev_id - device specific data (AS_Host structure)
2422 */
2423static irqreturn_t
2424acornscsi_intr(int irq, void *dev_id)
2425{
2426    AS_Host *host = (AS_Host *)dev_id;
2427    intr_ret_t ret;
2428    int iostatus;
2429    int in_irq = 0;
2430
2431    do {
2432	ret = INTR_IDLE;
2433
2434	iostatus = readb(host->fast + INT_REG);
2435
2436	if (iostatus & 2) {
2437	    acornscsi_dma_intr(host);
2438	    iostatus = readb(host->fast + INT_REG);
2439	}
2440
2441	if (iostatus & 8)
2442	    ret = acornscsi_sbicintr(host, in_irq);
2443
2444	/*
2445	 * If we have a transfer pending, start it.
2446	 * Only start it if the interface has already started transferring
2447	 * it's data
2448	 */
2449	if (host->dma.xfer_required)
2450	    acornscsi_dma_xfer(host);
2451
2452	if (ret == INTR_NEXT_COMMAND)
2453	    ret = acornscsi_kick(host);
2454
2455	in_irq = 1;
2456    } while (ret != INTR_IDLE);
2457
2458    return IRQ_HANDLED;
2459}
2460
2461/*=============================================================================================
2462 * Interfaces between interrupt handler and rest of scsi code
2463 */
2464
2465/*
2466 * Function : acornscsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2467 * Purpose  : queues a SCSI command
2468 * Params   : cmd  - SCSI command
2469 *	      done - function called on completion, with pointer to command descriptor
2470 * Returns  : 0, or < 0 on error.
2471 */
2472int acornscsi_queuecmd(struct scsi_cmnd *SCpnt,
2473		       void (*done)(struct scsi_cmnd *))
2474{
2475    AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2476
2477    if (!done) {
2478	/* there should be some way of rejecting errors like this without panicing... */
2479	panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
2480		host->host->host_no, SCpnt);
2481	return -EINVAL;
2482    }
2483
2484#if (DEBUG & DEBUG_NO_WRITE)
2485    if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
2486	printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2487	    host->host->host_no, '0' + SCpnt->device->id);
2488	SCpnt->result = DID_NO_CONNECT << 16;
2489	done(SCpnt);
2490	return 0;
2491    }
2492#endif
2493
2494    SCpnt->scsi_done = done;
2495    SCpnt->host_scribble = NULL;
2496    SCpnt->result = 0;
2497    SCpnt->tag = 0;
2498    SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
2499    SCpnt->SCp.sent_command = 0;
2500    SCpnt->SCp.scsi_xferred = 0;
2501
2502    init_SCp(SCpnt);
2503
2504    host->stats.queues += 1;
2505
2506    {
2507	unsigned long flags;
2508
2509	if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
2510	    SCpnt->result = DID_ERROR << 16;
2511	    done(SCpnt);
2512	    return 0;
2513	}
2514	local_irq_save(flags);
2515	if (host->scsi.phase == PHASE_IDLE)
2516	    acornscsi_kick(host);
2517	local_irq_restore(flags);
2518    }
2519    return 0;
2520}
2521
2522/*
2523 * Prototype: void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1, struct scsi_cmnd **SCpntp2, int result)
2524 * Purpose  : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
2525 * Params   : SCpntp1 - pointer to command to return
2526 *	      SCpntp2 - pointer to command to check
2527 *	      result  - result to pass back to mid-level done function
2528 * Returns  : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
2529 */
2530static inline void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1,
2531					  struct scsi_cmnd **SCpntp2,
2532					  int result)
2533{
2534	struct scsi_cmnd *SCpnt = *SCpntp1;
2535
2536    if (SCpnt) {
2537	*SCpntp1 = NULL;
2538
2539	SCpnt->result = result;
2540	SCpnt->scsi_done(SCpnt);
2541    }
2542
2543    if (SCpnt == *SCpntp2)
2544	*SCpntp2 = NULL;
2545}
2546
2547enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
2548
2549/*
2550 * Prototype: enum res acornscsi_do_abort(struct scsi_cmnd *SCpnt)
2551 * Purpose  : abort a command on this host
2552 * Params   : SCpnt - command to abort
2553 * Returns  : our abort status
2554 */
2555static enum res_abort acornscsi_do_abort(AS_Host *host, struct scsi_cmnd *SCpnt)
2556{
2557	enum res_abort res = res_not_running;
2558
2559	if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
2560		/*
2561		 * The command was on the issue queue, and has not been
2562		 * issued yet.  We can remove the command from the queue,
2563		 * and acknowledge the abort.  Neither the devices nor the
2564		 * interface know about the command.
2565		 */
2566//#if (DEBUG & DEBUG_ABORT)
2567		printk("on issue queue ");
2568//#endif
2569		res = res_success;
2570	} else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
2571		/*
2572		 * The command was on the disconnected queue.  Simply
2573		 * acknowledge the abort condition, and when the target
2574		 * reconnects, we will give it an ABORT message.  The
2575		 * target should then disconnect, and we will clear
2576		 * the busylun bit.
2577		 */
2578//#if (DEBUG & DEBUG_ABORT)
2579		printk("on disconnected queue ");
2580//#endif
2581		res = res_success;
2582	} else if (host->SCpnt == SCpnt) {
2583		unsigned long flags;
2584
2585//#if (DEBUG & DEBUG_ABORT)
2586		printk("executing ");
2587//#endif
2588
2589		local_irq_save(flags);
2590		switch (host->scsi.phase) {
2591		/*
2592		 * If the interface is idle, and the command is 'disconnectable',
2593		 * then it is the same as on the disconnected queue.  We simply
2594		 * remove all traces of the command.  When the target reconnects,
2595		 * we will give it an ABORT message since the command could not
2596		 * be found.  When the target finally disconnects, we will clear
2597		 * the busylun bit.
2598		 */
2599		case PHASE_IDLE:
2600			if (host->scsi.disconnectable) {
2601				host->scsi.disconnectable = 0;
2602				host->SCpnt = NULL;
2603				res = res_success;
2604			}
2605			break;
2606
2607		/*
2608		 * If the command has connected and done nothing further,
2609		 * simply force a disconnect.  We also need to clear the
2610		 * busylun bit.
2611		 */
2612		case PHASE_CONNECTED:
2613			sbic_arm_write(host, SBIC_CMND, CMND_DISCONNECT);
2614			host->SCpnt = NULL;
2615			res = res_success_clear;
2616			break;
2617
2618		default:
2619			acornscsi_abortcmd(host, host->SCpnt->tag);
2620			res = res_snooze;
2621		}
2622		local_irq_restore(flags);
2623	} else if (host->origSCpnt == SCpnt) {
2624		/*
2625		 * The command will be executed next, but a command
2626		 * is currently using the interface.  This is similar to
2627		 * being on the issue queue, except the busylun bit has
2628		 * been set.
2629		 */
2630		host->origSCpnt = NULL;
2631//#if (DEBUG & DEBUG_ABORT)
2632		printk("waiting for execution ");
2633//#endif
2634		res = res_success_clear;
2635	} else
2636		printk("unknown ");
2637
2638	return res;
2639}
2640
2641/*
2642 * Prototype: int acornscsi_abort(struct scsi_cmnd *SCpnt)
2643 * Purpose  : abort a command on this host
2644 * Params   : SCpnt - command to abort
2645 * Returns  : one of SCSI_ABORT_ macros
2646 */
2647int acornscsi_abort(struct scsi_cmnd *SCpnt)
2648{
2649	AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
2650	int result;
2651
2652	host->stats.aborts += 1;
2653
2654#if (DEBUG & DEBUG_ABORT)
2655	{
2656		int asr, ssr;
2657		asr = sbic_arm_read(host, SBIC_ASR);
2658		ssr = sbic_arm_read(host, SBIC_SSR);
2659
2660		printk(KERN_WARNING "acornscsi_abort: ");
2661		print_sbic_status(asr, ssr, host->scsi.phase);
2662		acornscsi_dumplog(host, SCpnt->device->id);
2663	}
2664#endif
2665
2666	printk("scsi%d: ", host->host->host_no);
2667
2668	switch (acornscsi_do_abort(host, SCpnt)) {
2669	/*
2670	 * We managed to find the command and cleared it out.
2671	 * We do not expect the command to be executing on the
2672	 * target, but we have set the busylun bit.
2673	 */
2674	case res_success_clear:
2675//#if (DEBUG & DEBUG_ABORT)
2676		printk("clear ");
2677//#endif
2678		clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, host->busyluns);
2679
2680	/*
2681	 * We found the command, and cleared it out.  Either
2682	 * the command is still known to be executing on the
2683	 * target, or the busylun bit is not set.
2684	 */
2685	case res_success:
2686//#if (DEBUG & DEBUG_ABORT)
2687		printk("success\n");
2688//#endif
2689		result = SUCCESS;
2690		break;
2691
2692	/*
2693	 * We did find the command, but unfortunately we couldn't
2694	 * unhook it from ourselves.  Wait some more, and if it
2695	 * still doesn't complete, reset the interface.
2696	 */
2697	case res_snooze:
2698//#if (DEBUG & DEBUG_ABORT)
2699		printk("snooze\n");
2700//#endif
2701		result = FAILED;
2702		break;
2703
2704	/*
2705	 * The command could not be found (either because it completed,
2706	 * or it got dropped.
2707	 */
2708	default:
2709	case res_not_running:
2710		acornscsi_dumplog(host, SCpnt->device->id);
2711		result = FAILED;
2712//#if (DEBUG & DEBUG_ABORT)
2713		printk("not running\n");
2714//#endif
2715		break;
2716	}
2717
2718	return result;
2719}
2720
2721/*
2722 * Prototype: int acornscsi_reset(struct scsi_cmnd *SCpnt)
2723 * Purpose  : reset a command on this host/reset this host
2724 * Params   : SCpnt  - command causing reset
2725 * Returns  : one of SCSI_RESET_ macros
2726 */
2727int acornscsi_bus_reset(struct scsi_cmnd *SCpnt)
2728{
2729	AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2730	struct scsi_cmnd *SCptr;
2731
2732    host->stats.resets += 1;
2733
2734#if (DEBUG & DEBUG_RESET)
2735    {
2736	int asr, ssr;
2737
2738	asr = sbic_arm_read(host, SBIC_ASR);
2739	ssr = sbic_arm_read(host, SBIC_SSR);
2740
2741	printk(KERN_WARNING "acornscsi_reset: ");
2742	print_sbic_status(asr, ssr, host->scsi.phase);
2743	acornscsi_dumplog(host, SCpnt->device->id);
2744    }
2745#endif
2746
2747    acornscsi_dma_stop(host);
2748
2749    /*
2750     * do hard reset.  This resets all devices on this host, and so we
2751     * must set the reset status on all commands.
2752     */
2753    acornscsi_resetcard(host);
2754
2755    while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
2756	;
2757
2758    return SUCCESS;
2759}
2760
2761/*==============================================================================================
2762 * initialisation & miscellaneous support
2763 */
2764
2765/*
2766 * Function: char *acornscsi_info(struct Scsi_Host *host)
2767 * Purpose : return a string describing this interface
2768 * Params  : host - host to give information on
2769 * Returns : a constant string
2770 */
2771const
2772char *acornscsi_info(struct Scsi_Host *host)
2773{
2774    static char string[100], *p;
2775
2776    p = string;
2777
2778    p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
2779#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2780    " SYNC"
2781#endif
2782#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2783    " TAG"
2784#endif
2785#ifdef CONFIG_SCSI_ACORNSCSI_LINK
2786    " LINK"
2787#endif
2788#if (DEBUG & DEBUG_NO_WRITE)
2789    " NOWRITE (" __stringify(NO_WRITE) ")"
2790#endif
2791		, host->hostt->name, host->io_port, host->irq,
2792		VER_MAJOR, VER_MINOR, VER_PATCH);
2793    return string;
2794}
2795
2796int acornscsi_proc_info(struct Scsi_Host *instance, char *buffer, char **start, off_t offset,
2797			int length, int inout)
2798{
2799    int pos, begin = 0, devidx;
2800    struct scsi_device *scd;
2801    AS_Host *host;
2802    char *p = buffer;
2803
2804    if (inout == 1)
2805	return -EINVAL;
2806
2807    host  = (AS_Host *)instance->hostdata;
2808
2809    p += sprintf(p, "AcornSCSI driver v%d.%d.%d"
2810#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2811    " SYNC"
2812#endif
2813#ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2814    " TAG"
2815#endif
2816#ifdef CONFIG_SCSI_ACORNSCSI_LINK
2817    " LINK"
2818#endif
2819#if (DEBUG & DEBUG_NO_WRITE)
2820    " NOWRITE (" __stringify(NO_WRITE) ")"
2821#endif
2822		"\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
2823
2824    p += sprintf(p,	"SBIC: WD33C93A  Address: %p    IRQ : %d\n",
2825			host->base + SBIC_REGIDX, host->scsi.irq);
2826#ifdef USE_DMAC
2827    p += sprintf(p,	"DMAC: uPC71071  Address: %p  IRQ : %d\n\n",
2828			host->base + DMAC_OFFSET, host->scsi.irq);
2829#endif
2830
2831    p += sprintf(p,	"Statistics:\n"
2832			"Queued commands: %-10u    Issued commands: %-10u\n"
2833			"Done commands  : %-10u    Reads          : %-10u\n"
2834			"Writes         : %-10u    Others         : %-10u\n"
2835			"Disconnects    : %-10u    Aborts         : %-10u\n"
2836			"Resets         : %-10u\n\nLast phases:",
2837			host->stats.queues,		host->stats.removes,
2838			host->stats.fins,		host->stats.reads,
2839			host->stats.writes,		host->stats.miscs,
2840			host->stats.disconnects,	host->stats.aborts,
2841			host->stats.resets);
2842
2843    for (devidx = 0; devidx < 9; devidx ++) {
2844	unsigned int statptr, prev;
2845
2846	p += sprintf(p, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
2847	statptr = host->status_ptr[devidx] - 10;
2848
2849	if ((signed int)statptr < 0)
2850	    statptr += STATUS_BUFFER_SIZE;
2851
2852	prev = host->status[devidx][statptr].when;
2853
2854	for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
2855	    if (host->status[devidx][statptr].when) {
2856		p += sprintf(p, "%c%02X:%02X+%2ld",
2857			host->status[devidx][statptr].irq ? '-' : ' ',
2858			host->status[devidx][statptr].ph,
2859			host->status[devidx][statptr].ssr,
2860			(host->status[devidx][statptr].when - prev) < 100 ?
2861				(host->status[devidx][statptr].when - prev) : 99);
2862		prev = host->status[devidx][statptr].when;
2863	    }
2864	}
2865    }
2866
2867    p += sprintf(p, "\nAttached devices:\n");
2868
2869    shost_for_each_device(scd, instance) {
2870	p += sprintf(p, "Device/Lun TaggedQ      Sync\n");
2871	p += sprintf(p, "     %d/%d   ", scd->id, scd->lun);
2872	if (scd->tagged_supported)
2873		p += sprintf(p, "%3sabled(%3d) ",
2874			     scd->simple_tags ? "en" : "dis",
2875			     scd->current_tag);
2876	else
2877		p += sprintf(p, "unsupported  ");
2878
2879	if (host->device[scd->id].sync_xfer & 15)
2880		p += sprintf(p, "offset %d, %d ns\n",
2881			     host->device[scd->id].sync_xfer & 15,
2882			     acornscsi_getperiod(host->device[scd->id].sync_xfer));
2883	else
2884		p += sprintf(p, "async\n");
2885
2886	pos = p - buffer;
2887	if (pos + begin < offset) {
2888	    begin += pos;
2889	    p = buffer;
2890	}
2891	pos = p - buffer;
2892	if (pos + begin > offset + length) {
2893	    scsi_device_put(scd);
2894	    break;
2895	}
2896    }
2897
2898    pos = p - buffer;
2899
2900    *start = buffer + (offset - begin);
2901    pos -= offset - begin;
2902
2903    if (pos > length)
2904	pos = length;
2905
2906    return pos;
2907}
2908
2909static struct scsi_host_template acornscsi_template = {
2910	.module			= THIS_MODULE,
2911	.proc_info		= acornscsi_proc_info,
2912	.name			= "AcornSCSI",
2913	.info			= acornscsi_info,
2914	.queuecommand		= acornscsi_queuecmd,
2915	.eh_abort_handler	= acornscsi_abort,
2916	.eh_bus_reset_handler	= acornscsi_bus_reset,
2917	.can_queue		= 16,
2918	.this_id		= 7,
2919	.sg_tablesize		= SG_ALL,
2920	.cmd_per_lun		= 2,
2921	.use_clustering		= DISABLE_CLUSTERING,
2922	.proc_name		= "acornscsi",
2923};
2924
2925static int __devinit
2926acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
2927{
2928	struct Scsi_Host *host;
2929	AS_Host *ashost;
2930	int ret;
2931
2932	ret = ecard_request_resources(ec);
2933	if (ret)
2934		goto out;
2935
2936	host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
2937	if (!host) {
2938		ret = -ENOMEM;
2939		goto out_release;
2940	}
2941
2942	ashost = (AS_Host *)host->hostdata;
2943
2944	ashost->base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
2945	ashost->fast = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
2946	if (!ashost->base || !ashost->fast)
2947		goto out_put;
2948
2949	host->irq = ec->irq;
2950	ashost->host = host;
2951	ashost->scsi.irq = host->irq;
2952
2953	ec->irqaddr	= ashost->fast + INT_REG;
2954	ec->irqmask	= 0x0a;
2955
2956	ret = request_irq(host->irq, acornscsi_intr, IRQF_DISABLED, "acornscsi", ashost);
2957	if (ret) {
2958		printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
2959			host->host_no, ashost->scsi.irq, ret);
2960		goto out_put;
2961	}
2962
2963	memset(&ashost->stats, 0, sizeof (ashost->stats));
2964	queue_initialise(&ashost->queues.issue);
2965	queue_initialise(&ashost->queues.disconnected);
2966	msgqueue_initialise(&ashost->scsi.msgs);
2967
2968	acornscsi_resetcard(ashost);
2969
2970	ret = scsi_add_host(host, &ec->dev);
2971	if (ret)
2972		goto out_irq;
2973
2974	scsi_scan_host(host);
2975	goto out;
2976
2977 out_irq:
2978	free_irq(host->irq, ashost);
2979	msgqueue_free(&ashost->scsi.msgs);
2980	queue_free(&ashost->queues.disconnected);
2981	queue_free(&ashost->queues.issue);
2982 out_put:
2983	ecardm_iounmap(ec, ashost->fast);
2984	ecardm_iounmap(ec, ashost->base);
2985	scsi_host_put(host);
2986 out_release:
2987	ecard_release_resources(ec);
2988 out:
2989	return ret;
2990}
2991
2992static void __devexit acornscsi_remove(struct expansion_card *ec)
2993{
2994	struct Scsi_Host *host = ecard_get_drvdata(ec);
2995	AS_Host *ashost = (AS_Host *)host->hostdata;
2996
2997	ecard_set_drvdata(ec, NULL);
2998	scsi_remove_host(host);
2999
3000	/*
3001	 * Put card into RESET state
3002	 */
3003	writeb(0x80, ashost->fast + PAGE_REG);
3004
3005	free_irq(host->irq, ashost);
3006
3007	msgqueue_free(&ashost->scsi.msgs);
3008	queue_free(&ashost->queues.disconnected);
3009	queue_free(&ashost->queues.issue);
3010	ecardm_iounmap(ec, ashost->fast);
3011	ecardm_iounmap(ec, ashost->base);
3012	scsi_host_put(host);
3013	ecard_release_resources(ec);
3014}
3015
3016static const struct ecard_id acornscsi_cids[] = {
3017	{ MANU_ACORN, PROD_ACORN_SCSI },
3018	{ 0xffff, 0xffff },
3019};
3020
3021static struct ecard_driver acornscsi_driver = {
3022	.probe		= acornscsi_probe,
3023	.remove		= __devexit_p(acornscsi_remove),
3024	.id_table	= acornscsi_cids,
3025	.drv = {
3026		.name		= "acornscsi",
3027	},
3028};
3029
3030static int __init acornscsi_init(void)
3031{
3032	return ecard_register_driver(&acornscsi_driver);
3033}
3034
3035static void __exit acornscsi_exit(void)
3036{
3037	ecard_remove_driver(&acornscsi_driver);
3038}
3039
3040module_init(acornscsi_init);
3041module_exit(acornscsi_exit);
3042
3043MODULE_AUTHOR("Russell King");
3044MODULE_DESCRIPTION("AcornSCSI driver");
3045MODULE_LICENSE("GPL");
3046