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