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