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