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