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