Linux 4.18.10
[linux/fpc-iii.git] / drivers / scsi / arm / acornscsi.c
blob421fe869a11ef0cbdb8130aa7b8ebdffbc775236
1 /*
2 * linux/drivers/acorn/scsi/acornscsi.c
4 * Acorn SCSI 3 driver
5 * By R.M.King.
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.
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...
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.
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)
46 /* DRIVER CONFIGURATION
48 * SCSI-II Tagged queue support.
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???
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'.
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.
63 #undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
65 * SCSI-II Synchronous transfer support.
67 * Tried and tested...
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.
73 #define SDTR_SIZE 12
74 #define SDTR_PERIOD 125
75 #define DEFAULT_PERIOD 500
78 * Debugging information
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].
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*/
89 * Select timeout time (in 10ms units)
91 * This is the timeout used between the start of selection and the WD33C93
92 * chip deciding that the device isn't responding.
94 #define TIMEOUT_TIME 10
96 * Define this if you want to have verbose explanation of SCSI
97 * status/messages.
99 #undef CONFIG_ACORNSCSI_CONSTANTS
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).
104 #define USE_DMAC
107 * ====================================================================================
110 #ifdef DEBUG_TARGET
111 #define DBG(cmd,xxx...) \
112 if (cmd->device->id == DEBUG_TARGET) { \
113 xxx; \
115 #else
116 #define DBG(cmd,xxx...) xxx
117 #endif
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>
134 #include <asm/ecard.h>
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"
144 #include <scsi/scsicam.h>
146 #define VER_MAJOR 2
147 #define VER_MINOR 0
148 #define VER_PATCH 6
150 #ifndef ABORT_TAG
151 #define ABORT_TAG 0xd
152 #else
153 #error "Yippee! ABORT TAG is now defined! Remove this error!"
154 #endif
156 #ifdef USE_DMAC
158 * DMAC setup parameters
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)
166 #define scsi_xferred have_data_in
169 * Size of on-board DMA buffer
171 #define DMAC_BUFFER_SIZE 65536
172 #endif
174 #define STATUS_BUFFER_TO_PRINT 24
176 unsigned int sdtr_period = SDTR_PERIOD;
177 unsigned int sdtr_size = SDTR_SIZE;
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);
185 /* ====================================================================================
186 * Miscellaneous
189 /* Offsets from MEMC base */
190 #define SBIC_REGIDX 0x2000
191 #define SBIC_REGVAL 0x2004
192 #define DMAC_OFFSET 0x3000
194 /* Offsets from FAST IOC base */
195 #define INT_REG 0x2000
196 #define PAGE_REG 0x3000
198 static inline void sbic_arm_write(AS_Host *host, unsigned int reg, unsigned int value)
200 writeb(reg, host->base + SBIC_REGIDX);
201 writeb(value, host->base + SBIC_REGVAL);
204 static inline int sbic_arm_read(AS_Host *host, unsigned int reg)
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;
212 #define sbic_arm_writenext(host, val) writeb((val), (host)->base + SBIC_REGVAL)
213 #define sbic_arm_readnext(host) readb((host)->base + SBIC_REGVAL)
215 #ifdef USE_DMAC
216 #define dmac_read(host,reg) \
217 readb((host)->base + DMAC_OFFSET + ((reg) << 2))
219 #define dmac_write(host,reg,value) \
220 ({ writeb((value), (host)->base + DMAC_OFFSET + ((reg) << 2)); })
222 #define dmac_clearintr(host) writeb(0, (host)->fast + INT_REG)
224 static inline unsigned int dmac_address(AS_Host *host)
226 return dmac_read(host, DMAC_TXADRHI) << 16 |
227 dmac_read(host, DMAC_TXADRMD) << 8 |
228 dmac_read(host, DMAC_TXADRLO);
231 static
232 void acornscsi_dumpdma(AS_Host *host, char *where)
234 unsigned int mode, addr, len;
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);
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));
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");
253 #endif
255 static
256 unsigned long acornscsi_sbic_xfcount(AS_Host *host)
258 unsigned long length;
260 length = sbic_arm_read(host, SBIC_TRANSCNTH) << 16;
261 length |= sbic_arm_readnext(host) << 8;
262 length |= sbic_arm_readnext(host);
264 return length;
267 static int
268 acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
270 int asr;
272 do {
273 asr = sbic_arm_read(host, SBIC_ASR);
275 if ((asr & stat_mask) == stat)
276 return 0;
278 udelay(1);
279 } while (--timeout);
281 printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
283 return -1;
286 static
287 int acornscsi_sbic_issuecmd(AS_Host *host, int command)
289 if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
290 return -1;
292 sbic_arm_write(host, SBIC_CMND, command);
294 return 0;
297 static void
298 acornscsi_csdelay(unsigned int cs)
300 unsigned long target_jiffies, flags;
302 target_jiffies = jiffies + 1 + cs * HZ / 100;
304 local_save_flags(flags);
305 local_irq_enable();
307 while (time_before(jiffies, target_jiffies)) barrier();
309 local_irq_restore(flags);
312 static
313 void acornscsi_resetcard(AS_Host *host)
315 unsigned int i, timeout;
317 /* assert reset line */
318 host->card.page_reg = 0x80;
319 writeb(host->card.page_reg, host->fast + PAGE_REG);
321 /* wait 3 cs. SCSI standard says 25ms. */
322 acornscsi_csdelay(3);
324 host->card.page_reg = 0;
325 writeb(host->card.page_reg, host->fast + PAGE_REG);
328 * Should get a reset from the card
330 timeout = 1000;
331 do {
332 if (readb(host->fast + INT_REG) & 8)
333 break;
334 udelay(1);
335 } while (--timeout);
337 if (timeout == 0)
338 printk("scsi%d: timeout while resetting card\n",
339 host->host->host_no);
341 sbic_arm_read(host, SBIC_ASR);
342 sbic_arm_read(host, SBIC_SSR);
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);
349 * Command should cause a reset interrupt
351 timeout = 1000;
352 do {
353 if (readb(host->fast + INT_REG) & 8)
354 break;
355 udelay(1);
356 } while (--timeout);
358 if (timeout == 0)
359 printk("scsi%d: timeout while resetting card\n",
360 host->host->host_no);
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);
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);
372 host->card.page_reg = 0x40;
373 writeb(host->card.page_reg, host->fast + PAGE_REG);
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
384 host->SCpnt = NULL;
385 host->scsi.phase = PHASE_IDLE;
386 host->scsi.disconnectable = 0;
388 memset(host->busyluns, 0, sizeof(host->busyluns));
390 for (i = 0; i < 8; i++) {
391 host->device[i].sync_state = SYNC_NEGOCIATE;
392 host->device[i].disconnect_ok = 1;
395 /* wait 25 cs. SCSI standard says 250ms. */
396 acornscsi_csdelay(25);
399 /*=============================================================================================
400 * Utility routines (eg. debug)
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"
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
429 static char *acornscsi_interruptcode[] = {
430 /* 0 */
431 "reset - normal mode", /* 00 */
432 "reset - advanced mode", /* 01 */
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 */
446 /* 12 */
447 "/ACK asserted", /* 20 */
448 "save-data-ptr", /* 21 */
449 "{re}sel", /* 22 */
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 */
459 /* 21 */
460 "resel, no id", /* 80 */
461 "resel", /* 81 */
462 "discon", /* 85 */
465 static
466 void print_scsi_status(unsigned int ssr)
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);
475 #endif
477 static
478 void print_sbic_status(int asr, int ssr, int cmdphase)
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
497 static void
498 acornscsi_dumplogline(AS_Host *host, int target, int line)
500 unsigned long prev;
501 signed int ptr;
503 ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
504 if (ptr < 0)
505 ptr += STATUS_BUFFER_SIZE;
507 printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
508 line == 0 ? "ph" : line == 1 ? "ssr" : "int");
510 prev = host->status[target][ptr].when;
512 for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
513 unsigned long time_diff;
515 if (!host->status[target][ptr].when)
516 continue;
518 switch (line) {
519 case 0:
520 printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
521 host->status[target][ptr].ph);
522 break;
524 case 1:
525 printk(" %02X", host->status[target][ptr].ssr);
526 break;
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;
541 printk("\n");
544 static
545 void acornscsi_dumplog(AS_Host *host, int target)
547 do {
548 acornscsi_dumplogline(host, target, 0);
549 acornscsi_dumplogline(host, target, 1);
550 acornscsi_dumplogline(host, target, 2);
552 if (target == 8)
553 break;
555 target = 8;
556 } while (1);
559 static
560 char acornscsi_target(AS_Host *host)
562 if (host->SCpnt)
563 return '0' + host->SCpnt->device->id;
564 return 'H';
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
575 static inline
576 cmdtype_t acornscsi_cmdtype(int command)
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;
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
596 static
597 datadir_t acornscsi_datadirection(int command)
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;
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
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 }
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.
636 static
637 int acornscsi_getperiod(unsigned char syncxfer)
639 int i;
641 syncxfer &= 0xf0;
642 if (syncxfer == 0x10)
643 syncxfer = 0;
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;
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
658 static inline
659 int round_period(unsigned int period)
661 int i;
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;
668 return 7;
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
679 static
680 unsigned char __maybe_unused calc_sync_xfer(unsigned int period,
681 unsigned int offset)
683 return sync_xfer_table[round_period(period)].reg_value |
684 ((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
687 /* ====================================================================================
688 * Command functions
691 * Function: acornscsi_kick(AS_Host *host)
692 * Purpose : kick next command to interface
693 * Params : host - host to send command to
694 * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
695 * Notes : interrupts are always disabled!
697 static
698 intr_ret_t acornscsi_kick(AS_Host *host)
700 int from_queue = 0;
701 struct scsi_cmnd *SCpnt;
703 /* first check to see if a command is waiting to be executed */
704 SCpnt = host->origSCpnt;
705 host->origSCpnt = NULL;
707 /* retrieve next command */
708 if (!SCpnt) {
709 SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
710 if (!SCpnt)
711 return INTR_IDLE;
713 from_queue = 1;
716 if (host->scsi.disconnectable && host->SCpnt) {
717 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
718 host->scsi.disconnectable = 0;
719 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
720 DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
721 host->host->host_no, acornscsi_target(host)));
722 #endif
723 host->SCpnt = NULL;
727 * If we have an interrupt pending, then we may have been reselected.
728 * In this case, we don't want to write to the registers
730 if (!(sbic_arm_read(host, SBIC_ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
731 sbic_arm_write(host, SBIC_DESTID, SCpnt->device->id);
732 sbic_arm_write(host, SBIC_CMND, CMND_SELWITHATN);
736 * claim host busy - all of these must happen atomically wrt
737 * our interrupt routine. Failure means command loss.
739 host->scsi.phase = PHASE_CONNECTING;
740 host->SCpnt = SCpnt;
741 host->scsi.SCp = SCpnt->SCp;
742 host->dma.xfer_setup = 0;
743 host->dma.xfer_required = 0;
744 host->dma.xfer_done = 0;
746 #if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
747 DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
748 host->host->host_no, '0' + SCpnt->device->id,
749 SCpnt->cmnd[0]));
750 #endif
752 if (from_queue) {
753 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
755 * tagged queueing - allocate a new tag to this command
757 if (SCpnt->device->simple_tags) {
758 SCpnt->device->current_tag += 1;
759 if (SCpnt->device->current_tag == 0)
760 SCpnt->device->current_tag = 1;
761 SCpnt->tag = SCpnt->device->current_tag;
762 } else
763 #endif
764 set_bit(SCpnt->device->id * 8 +
765 (u8)(SCpnt->device->lun & 0x07), host->busyluns);
767 host->stats.removes += 1;
769 switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
770 case CMD_WRITE:
771 host->stats.writes += 1;
772 break;
773 case CMD_READ:
774 host->stats.reads += 1;
775 break;
776 case CMD_MISC:
777 host->stats.miscs += 1;
778 break;
782 return INTR_PROCESSING;
786 * Function: void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp, unsigned int result)
787 * Purpose : complete processing for command
788 * Params : host - interface that completed
789 * result - driver byte of result
791 static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
792 unsigned int result)
794 struct scsi_cmnd *SCpnt = *SCpntp;
796 /* clean up */
797 sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
799 host->stats.fins += 1;
801 if (SCpnt) {
802 *SCpntp = NULL;
804 acornscsi_dma_cleanup(host);
806 SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
809 * In theory, this should not happen. In practice, it seems to.
810 * Only trigger an error if the device attempts to report all happy
811 * but with untransferred buffers... If we don't do something, then
812 * data loss will occur. Should we check SCpnt->underflow here?
813 * It doesn't appear to be set to something meaningful by the higher
814 * levels all the time.
816 if (result == DID_OK) {
817 int xfer_warn = 0;
819 if (SCpnt->underflow == 0) {
820 if (host->scsi.SCp.ptr &&
821 acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
822 xfer_warn = 1;
823 } else {
824 if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
825 host->scsi.SCp.scsi_xferred != host->dma.transferred)
826 xfer_warn = 1;
829 /* ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.6)
830 * Targets which break data transfers into multiple
831 * connections shall end each successful connection
832 * (except possibly the last) with a SAVE DATA
833 * POINTER - DISCONNECT message sequence.
835 * This makes it difficult to ensure that a transfer has
836 * completed. If we reach the end of a transfer during
837 * the command, then we can only have finished the transfer.
838 * therefore, if we seem to have some data remaining, this
839 * is not a problem.
841 if (host->dma.xfer_done)
842 xfer_warn = 0;
844 if (xfer_warn) {
845 switch (status_byte(SCpnt->result)) {
846 case CHECK_CONDITION:
847 case COMMAND_TERMINATED:
848 case BUSY:
849 case QUEUE_FULL:
850 case RESERVATION_CONFLICT:
851 break;
853 default:
854 scmd_printk(KERN_ERR, SCpnt,
855 "incomplete data transfer detected: "
856 "result=%08X", SCpnt->result);
857 scsi_print_command(SCpnt);
858 acornscsi_dumpdma(host, "done");
859 acornscsi_dumplog(host, SCpnt->device->id);
860 set_host_byte(SCpnt, DID_ERROR);
865 if (!SCpnt->scsi_done)
866 panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
868 clear_bit(SCpnt->device->id * 8 +
869 (u8)(SCpnt->device->lun & 0x7), host->busyluns);
871 SCpnt->scsi_done(SCpnt);
872 } else
873 printk("scsi%d: null command in acornscsi_done", host->host->host_no);
875 host->scsi.phase = PHASE_IDLE;
878 /* ====================================================================================
879 * DMA routines
882 * Purpose : update SCSI Data Pointer
883 * Notes : this will only be one SG entry or less
885 static
886 void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
888 SCp->ptr += length;
889 SCp->this_residual -= length;
891 if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
892 host->dma.xfer_done = 1;
896 * Prototype: void acornscsi_data_read(AS_Host *host, char *ptr,
897 * unsigned int start_addr, unsigned int length)
898 * Purpose : read data from DMA RAM
899 * Params : host - host to transfer from
900 * ptr - DRAM address
901 * start_addr - host mem address
902 * length - number of bytes to transfer
903 * Notes : this will only be one SG entry or less
905 static
906 void acornscsi_data_read(AS_Host *host, char *ptr,
907 unsigned int start_addr, unsigned int length)
909 extern void __acornscsi_in(void __iomem *, char *buf, int len);
910 unsigned int page, offset, len = length;
912 page = (start_addr >> 12);
913 offset = start_addr & ((1 << 12) - 1);
915 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
917 while (len > 0) {
918 unsigned int this_len;
920 if (len + offset > (1 << 12))
921 this_len = (1 << 12) - offset;
922 else
923 this_len = len;
925 __acornscsi_in(host->base + (offset << 1), ptr, this_len);
927 offset += this_len;
928 ptr += this_len;
929 len -= this_len;
931 if (offset == (1 << 12)) {
932 offset = 0;
933 page ++;
934 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
937 writeb(host->card.page_reg, host->fast + PAGE_REG);
941 * Prototype: void acornscsi_data_write(AS_Host *host, char *ptr,
942 * unsigned int start_addr, unsigned int length)
943 * Purpose : write data to DMA RAM
944 * Params : host - host to transfer from
945 * ptr - DRAM address
946 * start_addr - host mem address
947 * length - number of bytes to transfer
948 * Notes : this will only be one SG entry or less
950 static
951 void acornscsi_data_write(AS_Host *host, char *ptr,
952 unsigned int start_addr, unsigned int length)
954 extern void __acornscsi_out(void __iomem *, char *buf, int len);
955 unsigned int page, offset, len = length;
957 page = (start_addr >> 12);
958 offset = start_addr & ((1 << 12) - 1);
960 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
962 while (len > 0) {
963 unsigned int this_len;
965 if (len + offset > (1 << 12))
966 this_len = (1 << 12) - offset;
967 else
968 this_len = len;
970 __acornscsi_out(host->base + (offset << 1), ptr, this_len);
972 offset += this_len;
973 ptr += this_len;
974 len -= this_len;
976 if (offset == (1 << 12)) {
977 offset = 0;
978 page ++;
979 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
982 writeb(host->card.page_reg, host->fast + PAGE_REG);
985 /* =========================================================================================
986 * On-board DMA routines
988 #ifdef USE_DMAC
990 * Prototype: void acornscsi_dmastop(AS_Host *host)
991 * Purpose : stop all DMA
992 * Params : host - host on which to stop DMA
993 * Notes : This is called when leaving DATA IN/OUT phase,
994 * or when interface is RESET
996 static inline
997 void acornscsi_dma_stop(AS_Host *host)
999 dmac_write(host, DMAC_MASKREG, MASK_ON);
1000 dmac_clearintr(host);
1002 #if (DEBUG & DEBUG_DMA)
1003 DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
1004 #endif
1008 * Function: void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1009 * Purpose : setup DMA controller for data transfer
1010 * Params : host - host to setup
1011 * direction - data transfer direction
1012 * Notes : This is called when entering DATA I/O phase, not
1013 * while we're in a DATA I/O phase
1015 static
1016 void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1018 unsigned int address, length, mode;
1020 host->dma.direction = direction;
1022 dmac_write(host, DMAC_MASKREG, MASK_ON);
1024 if (direction == DMA_OUT) {
1025 #if (DEBUG & DEBUG_NO_WRITE)
1026 if (NO_WRITE & (1 << host->SCpnt->device->id)) {
1027 printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
1028 host->host->host_no, acornscsi_target(host));
1029 return;
1031 #endif
1032 mode = DMAC_WRITE;
1033 } else
1034 mode = DMAC_READ;
1037 * Allocate some buffer space, limited to half the buffer size
1039 length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1040 if (length) {
1041 host->dma.start_addr = address = host->dma.free_addr;
1042 host->dma.free_addr = (host->dma.free_addr + length) &
1043 (DMAC_BUFFER_SIZE - 1);
1046 * Transfer data to DMA memory
1048 if (direction == DMA_OUT)
1049 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1050 length);
1052 length -= 1;
1053 dmac_write(host, DMAC_TXCNTLO, length);
1054 dmac_write(host, DMAC_TXCNTHI, length >> 8);
1055 dmac_write(host, DMAC_TXADRLO, address);
1056 dmac_write(host, DMAC_TXADRMD, address >> 8);
1057 dmac_write(host, DMAC_TXADRHI, 0);
1058 dmac_write(host, DMAC_MODECON, mode);
1059 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1061 #if (DEBUG & DEBUG_DMA)
1062 DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
1063 #endif
1064 host->dma.xfer_setup = 1;
1069 * Function: void acornscsi_dma_cleanup(AS_Host *host)
1070 * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
1071 * Params : host - host to finish
1072 * Notes : This is called when a command is:
1073 * terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONECT
1074 * : This must not return until all transfers are completed.
1076 static
1077 void acornscsi_dma_cleanup(AS_Host *host)
1079 dmac_write(host, DMAC_MASKREG, MASK_ON);
1080 dmac_clearintr(host);
1083 * Check for a pending transfer
1085 if (host->dma.xfer_required) {
1086 host->dma.xfer_required = 0;
1087 if (host->dma.direction == DMA_IN)
1088 acornscsi_data_read(host, host->dma.xfer_ptr,
1089 host->dma.xfer_start, host->dma.xfer_length);
1093 * Has a transfer been setup?
1095 if (host->dma.xfer_setup) {
1096 unsigned int transferred;
1098 host->dma.xfer_setup = 0;
1100 #if (DEBUG & DEBUG_DMA)
1101 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
1102 #endif
1105 * Calculate number of bytes transferred from DMA.
1107 transferred = dmac_address(host) - host->dma.start_addr;
1108 host->dma.transferred += transferred;
1110 if (host->dma.direction == DMA_IN)
1111 acornscsi_data_read(host, host->scsi.SCp.ptr,
1112 host->dma.start_addr, transferred);
1115 * Update SCSI pointers
1117 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1118 #if (DEBUG & DEBUG_DMA)
1119 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
1120 #endif
1125 * Function: void acornscsi_dmacintr(AS_Host *host)
1126 * Purpose : handle interrupts from DMAC device
1127 * Params : host - host to process
1128 * Notes : If reading, we schedule the read to main memory &
1129 * allow the transfer to continue.
1130 * : If writing, we fill the onboard DMA memory from main
1131 * memory.
1132 * : Called whenever DMAC finished it's current transfer.
1134 static
1135 void acornscsi_dma_intr(AS_Host *host)
1137 unsigned int address, length, transferred;
1139 #if (DEBUG & DEBUG_DMA)
1140 DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
1141 #endif
1143 dmac_write(host, DMAC_MASKREG, MASK_ON);
1144 dmac_clearintr(host);
1147 * Calculate amount transferred via DMA
1149 transferred = dmac_address(host) - host->dma.start_addr;
1150 host->dma.transferred += transferred;
1153 * Schedule DMA transfer off board
1155 if (host->dma.direction == DMA_IN) {
1156 host->dma.xfer_start = host->dma.start_addr;
1157 host->dma.xfer_length = transferred;
1158 host->dma.xfer_ptr = host->scsi.SCp.ptr;
1159 host->dma.xfer_required = 1;
1162 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1165 * Allocate some buffer space, limited to half the on-board RAM size
1167 length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1168 if (length) {
1169 host->dma.start_addr = address = host->dma.free_addr;
1170 host->dma.free_addr = (host->dma.free_addr + length) &
1171 (DMAC_BUFFER_SIZE - 1);
1174 * Transfer data to DMA memory
1176 if (host->dma.direction == DMA_OUT)
1177 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1178 length);
1180 length -= 1;
1181 dmac_write(host, DMAC_TXCNTLO, length);
1182 dmac_write(host, DMAC_TXCNTHI, length >> 8);
1183 dmac_write(host, DMAC_TXADRLO, address);
1184 dmac_write(host, DMAC_TXADRMD, address >> 8);
1185 dmac_write(host, DMAC_TXADRHI, 0);
1186 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1188 #if (DEBUG & DEBUG_DMA)
1189 DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
1190 #endif
1191 } else {
1192 host->dma.xfer_setup = 0;
1193 #if 0
1195 * If the interface still wants more, then this is an error.
1196 * We give it another byte, but we also attempt to raise an
1197 * attention condition. We continue giving one byte until
1198 * the device recognises the attention.
1200 if (dmac_read(host, DMAC_STATUS) & STATUS_RQ0) {
1201 acornscsi_abortcmd(host, host->SCpnt->tag);
1203 dmac_write(host, DMAC_TXCNTLO, 0);
1204 dmac_write(host, DMAC_TXCNTHI, 0);
1205 dmac_write(host, DMAC_TXADRLO, 0);
1206 dmac_write(host, DMAC_TXADRMD, 0);
1207 dmac_write(host, DMAC_TXADRHI, 0);
1208 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1210 #endif
1215 * Function: void acornscsi_dma_xfer(AS_Host *host)
1216 * Purpose : transfer data between AcornSCSI and memory
1217 * Params : host - host to process
1219 static
1220 void acornscsi_dma_xfer(AS_Host *host)
1222 host->dma.xfer_required = 0;
1224 if (host->dma.direction == DMA_IN)
1225 acornscsi_data_read(host, host->dma.xfer_ptr,
1226 host->dma.xfer_start, host->dma.xfer_length);
1230 * Function: void acornscsi_dma_adjust(AS_Host *host)
1231 * Purpose : adjust DMA pointers & count for bytes transferred to
1232 * SBIC but not SCSI bus.
1233 * Params : host - host to adjust DMA count for
1235 static
1236 void acornscsi_dma_adjust(AS_Host *host)
1238 if (host->dma.xfer_setup) {
1239 signed long transferred;
1240 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1241 DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
1242 #endif
1244 * Calculate correct DMA address - DMA is ahead of SCSI bus while
1245 * writing.
1246 * host->scsi.SCp.scsi_xferred is the number of bytes
1247 * actually transferred to/from the SCSI bus.
1248 * host->dma.transferred is the number of bytes transferred
1249 * over DMA since host->dma.start_addr was last set.
1251 * real_dma_addr = host->dma.start_addr + host->scsi.SCp.scsi_xferred
1252 * - host->dma.transferred
1254 transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
1255 if (transferred < 0)
1256 printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1257 host->host->host_no, acornscsi_target(host), transferred);
1258 else if (transferred == 0)
1259 host->dma.xfer_setup = 0;
1260 else {
1261 transferred += host->dma.start_addr;
1262 dmac_write(host, DMAC_TXADRLO, transferred);
1263 dmac_write(host, DMAC_TXADRMD, transferred >> 8);
1264 dmac_write(host, DMAC_TXADRHI, transferred >> 16);
1265 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1266 DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
1267 #endif
1271 #endif
1273 /* =========================================================================================
1274 * Data I/O
1276 static int
1277 acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
1279 unsigned int asr, timeout = max_timeout;
1280 int my_ptr = *ptr;
1282 while (my_ptr < len) {
1283 asr = sbic_arm_read(host, SBIC_ASR);
1285 if (asr & ASR_DBR) {
1286 timeout = max_timeout;
1288 sbic_arm_write(host, SBIC_DATA, bytes[my_ptr++]);
1289 } else if (asr & ASR_INT)
1290 break;
1291 else if (--timeout == 0)
1292 break;
1293 udelay(1);
1296 *ptr = my_ptr;
1298 return (timeout == 0) ? -1 : 0;
1302 * Function: void acornscsi_sendcommand(AS_Host *host)
1303 * Purpose : send a command to a target
1304 * Params : host - host which is connected to target
1306 static void
1307 acornscsi_sendcommand(AS_Host *host)
1309 struct scsi_cmnd *SCpnt = host->SCpnt;
1311 sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1312 sbic_arm_writenext(host, 0);
1313 sbic_arm_writenext(host, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1315 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1317 if (acornscsi_write_pio(host, SCpnt->cmnd,
1318 (int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
1319 printk("scsi%d: timeout while sending command\n", host->host->host_no);
1321 host->scsi.phase = PHASE_COMMAND;
1324 static
1325 void acornscsi_sendmessage(AS_Host *host)
1327 unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
1328 unsigned int msgnr;
1329 struct message *msg;
1331 #if (DEBUG & DEBUG_MESSAGES)
1332 printk("scsi%d.%c: sending message ",
1333 host->host->host_no, acornscsi_target(host));
1334 #endif
1336 switch (message_length) {
1337 case 0:
1338 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1340 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
1342 sbic_arm_write(host, SBIC_DATA, NOP);
1344 host->scsi.last_message = NOP;
1345 #if (DEBUG & DEBUG_MESSAGES)
1346 printk("NOP");
1347 #endif
1348 break;
1350 case 1:
1351 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1352 msg = msgqueue_getmsg(&host->scsi.msgs, 0);
1354 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
1356 sbic_arm_write(host, SBIC_DATA, msg->msg[0]);
1358 host->scsi.last_message = msg->msg[0];
1359 #if (DEBUG & DEBUG_MESSAGES)
1360 spi_print_msg(msg->msg);
1361 #endif
1362 break;
1364 default:
1366 * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
1367 * 'When a target sends this (MESSAGE_REJECT) message, it
1368 * shall change to MESSAGE IN phase and send this message
1369 * prior to requesting additional message bytes from the
1370 * initiator. This provides an interlock so that the
1371 * initiator can determine which message byte is rejected.
1373 sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1374 sbic_arm_writenext(host, 0);
1375 sbic_arm_writenext(host, message_length);
1376 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1378 msgnr = 0;
1379 while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
1380 unsigned int i;
1381 #if (DEBUG & DEBUG_MESSAGES)
1382 spi_print_msg(msg);
1383 #endif
1384 i = 0;
1385 if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
1386 printk("scsi%d: timeout while sending message\n", host->host->host_no);
1388 host->scsi.last_message = msg->msg[0];
1389 if (msg->msg[0] == EXTENDED_MESSAGE)
1390 host->scsi.last_message |= msg->msg[2] << 8;
1392 if (i != msg->length)
1393 break;
1395 break;
1397 #if (DEBUG & DEBUG_MESSAGES)
1398 printk("\n");
1399 #endif
1403 * Function: void acornscsi_readstatusbyte(AS_Host *host)
1404 * Purpose : Read status byte from connected target
1405 * Params : host - host connected to target
1407 static
1408 void acornscsi_readstatusbyte(AS_Host *host)
1410 acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
1411 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
1412 host->scsi.SCp.Status = sbic_arm_read(host, SBIC_DATA);
1416 * Function: unsigned char acornscsi_readmessagebyte(AS_Host *host)
1417 * Purpose : Read one message byte from connected target
1418 * Params : host - host connected to target
1420 static
1421 unsigned char acornscsi_readmessagebyte(AS_Host *host)
1423 unsigned char message;
1425 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1427 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
1429 message = sbic_arm_read(host, SBIC_DATA);
1431 /* wait for MSGIN-XFER-PAUSED */
1432 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
1434 sbic_arm_read(host, SBIC_SSR);
1436 return message;
1440 * Function: void acornscsi_message(AS_Host *host)
1441 * Purpose : Read complete message from connected target & action message
1442 * Params : host - host connected to target
1444 static
1445 void acornscsi_message(AS_Host *host)
1447 unsigned char message[16];
1448 unsigned int msgidx = 0, msglen = 1;
1450 do {
1451 message[msgidx] = acornscsi_readmessagebyte(host);
1453 switch (msgidx) {
1454 case 0:
1455 if (message[0] == EXTENDED_MESSAGE ||
1456 (message[0] >= 0x20 && message[0] <= 0x2f))
1457 msglen = 2;
1458 break;
1460 case 1:
1461 if (message[0] == EXTENDED_MESSAGE)
1462 msglen += message[msgidx];
1463 break;
1465 msgidx += 1;
1466 if (msgidx < msglen) {
1467 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1469 /* wait for next msg-in */
1470 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
1471 sbic_arm_read(host, SBIC_SSR);
1473 } while (msgidx < msglen);
1475 #if (DEBUG & DEBUG_MESSAGES)
1476 printk("scsi%d.%c: message in: ",
1477 host->host->host_no, acornscsi_target(host));
1478 spi_print_msg(message);
1479 printk("\n");
1480 #endif
1482 if (host->scsi.phase == PHASE_RECONNECTED) {
1484 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1485 * 'Whenever a target reconnects to an initiator to continue
1486 * a tagged I/O process, the SIMPLE QUEUE TAG message shall
1487 * be sent immediately following the IDENTIFY message...'
1489 if (message[0] == SIMPLE_QUEUE_TAG)
1490 host->scsi.reconnected.tag = message[1];
1491 if (acornscsi_reconnect_finish(host))
1492 host->scsi.phase = PHASE_MSGIN;
1495 switch (message[0]) {
1496 case ABORT:
1497 case ABORT_TAG:
1498 case COMMAND_COMPLETE:
1499 if (host->scsi.phase != PHASE_STATUSIN) {
1500 printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1501 host->host->host_no, acornscsi_target(host));
1502 acornscsi_dumplog(host, host->SCpnt->device->id);
1504 host->scsi.phase = PHASE_DONE;
1505 host->scsi.SCp.Message = message[0];
1506 break;
1508 case SAVE_POINTERS:
1510 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
1511 * 'The SAVE DATA POINTER message is sent from a target to
1512 * direct the initiator to copy the active data pointer to
1513 * the saved data pointer for the current I/O process.
1515 acornscsi_dma_cleanup(host);
1516 host->SCpnt->SCp = host->scsi.SCp;
1517 host->SCpnt->SCp.sent_command = 0;
1518 host->scsi.phase = PHASE_MSGIN;
1519 break;
1521 case RESTORE_POINTERS:
1523 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
1524 * 'The RESTORE POINTERS message is sent from a target to
1525 * direct the initiator to copy the most recently saved
1526 * command, data, and status pointers for the I/O process
1527 * to the corresponding active pointers. The command and
1528 * status pointers shall be restored to the beginning of
1529 * the present command and status areas.'
1531 acornscsi_dma_cleanup(host);
1532 host->scsi.SCp = host->SCpnt->SCp;
1533 host->scsi.phase = PHASE_MSGIN;
1534 break;
1536 case DISCONNECT:
1538 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
1539 * 'On those occasions when an error or exception condition occurs
1540 * and the target elects to repeat the information transfer, the
1541 * target may repeat the transfer either issuing a RESTORE POINTERS
1542 * message or by disconnecting without issuing a SAVE POINTERS
1543 * message. When reconnection is completed, the most recent
1544 * saved pointer values are restored.'
1546 acornscsi_dma_cleanup(host);
1547 host->scsi.phase = PHASE_DISCONNECT;
1548 break;
1550 case MESSAGE_REJECT:
1551 #if 0 /* this isn't needed any more */
1553 * If we were negociating sync transfer, we don't yet know if
1554 * this REJECT is for the sync transfer or for the tagged queue/wide
1555 * transfer. Re-initiate sync transfer negotiation now, and if
1556 * we got a REJECT in response to SDTR, then it'll be set to DONE.
1558 if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST)
1559 host->device[host->SCpnt->device->id].sync_state = SYNC_NEGOCIATE;
1560 #endif
1563 * If we have any messages waiting to go out, then assert ATN now
1565 if (msgqueue_msglength(&host->scsi.msgs))
1566 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1568 switch (host->scsi.last_message) {
1569 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1570 case HEAD_OF_QUEUE_TAG:
1571 case ORDERED_QUEUE_TAG:
1572 case SIMPLE_QUEUE_TAG:
1574 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1575 * If a target does not implement tagged queuing and a queue tag
1576 * message is received, it shall respond with a MESSAGE REJECT
1577 * message and accept the I/O process as if it were untagged.
1579 printk(KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
1580 host->host->host_no, acornscsi_target(host));
1581 host->SCpnt->device->simple_tags = 0;
1582 set_bit(host->SCpnt->device->id * 8 +
1583 (u8)(host->SCpnt->device->lun & 0x7), host->busyluns);
1584 break;
1585 #endif
1586 case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1588 * Target can't handle synchronous transfers
1590 printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1591 host->host->host_no, acornscsi_target(host));
1592 host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
1593 host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
1594 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1595 break;
1597 default:
1598 break;
1600 break;
1602 case QUEUE_FULL:
1603 /* TODO: target queue is full */
1604 break;
1606 case SIMPLE_QUEUE_TAG:
1607 /* tag queue reconnect... message[1] = queue tag. Print something to indicate something happened! */
1608 printk("scsi%d.%c: reconnect queue tag %02X\n",
1609 host->host->host_no, acornscsi_target(host),
1610 message[1]);
1611 break;
1613 case EXTENDED_MESSAGE:
1614 switch (message[2]) {
1615 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1616 case EXTENDED_SDTR:
1617 if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
1619 * We requested synchronous transfers. This isn't quite right...
1620 * We can only say if this succeeded if we proceed on to execute the
1621 * command from this message. If we get a MESSAGE PARITY ERROR,
1622 * and the target retries fail, then we fallback to asynchronous mode
1624 host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
1625 printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1626 host->host->host_no, acornscsi_target(host),
1627 message[4], message[3] * 4);
1628 host->device[host->SCpnt->device->id].sync_xfer =
1629 calc_sync_xfer(message[3] * 4, message[4]);
1630 } else {
1631 unsigned char period, length;
1633 * Target requested synchronous transfers. The agreement is only
1634 * to be in operation AFTER the target leaves message out phase.
1636 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1637 period = max_t(unsigned int, message[3], sdtr_period / 4);
1638 length = min_t(unsigned int, message[4], sdtr_size);
1639 msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1640 EXTENDED_SDTR, period, length);
1641 host->device[host->SCpnt->device->id].sync_xfer =
1642 calc_sync_xfer(period * 4, length);
1644 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1645 break;
1646 #else
1647 /* We do not accept synchronous transfers. Respond with a
1648 * MESSAGE_REJECT.
1650 #endif
1652 case EXTENDED_WDTR:
1653 /* The WD33C93A is only 8-bit. We respond with a MESSAGE_REJECT
1654 * to a wide data transfer request.
1656 default:
1657 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1658 msgqueue_flush(&host->scsi.msgs);
1659 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1660 break;
1662 break;
1664 default: /* reject message */
1665 printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1666 host->host->host_no, acornscsi_target(host),
1667 message[0]);
1668 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1669 msgqueue_flush(&host->scsi.msgs);
1670 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1671 host->scsi.phase = PHASE_MSGIN;
1672 break;
1674 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1678 * Function: int acornscsi_buildmessages(AS_Host *host)
1679 * Purpose : build the connection messages for a host
1680 * Params : host - host to add messages to
1682 static
1683 void acornscsi_buildmessages(AS_Host *host)
1685 #if 0
1686 /* does the device need resetting? */
1687 if (cmd_reset) {
1688 msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
1689 return;
1691 #endif
1693 msgqueue_addmsg(&host->scsi.msgs, 1,
1694 IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
1695 host->SCpnt->device->lun));
1697 #if 0
1698 /* does the device need the current command aborted */
1699 if (cmd_aborted) {
1700 acornscsi_abortcmd(host->SCpnt->tag);
1701 return;
1703 #endif
1705 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1706 if (host->SCpnt->tag) {
1707 unsigned int tag_type;
1709 if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
1710 host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
1711 host->SCpnt->cmnd[0] == INQUIRY)
1712 tag_type = HEAD_OF_QUEUE_TAG;
1713 else
1714 tag_type = SIMPLE_QUEUE_TAG;
1715 msgqueue_addmsg(&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
1717 #endif
1719 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1720 if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
1721 host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
1722 msgqueue_addmsg(&host->scsi.msgs, 5,
1723 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1724 sdtr_period / 4, sdtr_size);
1726 #endif
1730 * Function: int acornscsi_starttransfer(AS_Host *host)
1731 * Purpose : transfer data to/from connected target
1732 * Params : host - host to which target is connected
1733 * Returns : 0 if failure
1735 static
1736 int acornscsi_starttransfer(AS_Host *host)
1738 int residual;
1740 if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
1741 printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1742 host->host->host_no, acornscsi_target(host));
1743 return 0;
1746 residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
1748 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1749 sbic_arm_writenext(host, residual >> 16);
1750 sbic_arm_writenext(host, residual >> 8);
1751 sbic_arm_writenext(host, residual);
1752 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1753 return 1;
1756 /* =========================================================================================
1757 * Connection & Disconnection
1760 * Function : acornscsi_reconnect(AS_Host *host)
1761 * Purpose : reconnect a previously disconnected command
1762 * Params : host - host specific data
1763 * Remarks : SCSI spec says:
1764 * 'The set of active pointers is restored from the set
1765 * of saved pointers upon reconnection of the I/O process'
1767 static
1768 int acornscsi_reconnect(AS_Host *host)
1770 unsigned int target, lun, ok = 0;
1772 target = sbic_arm_read(host, SBIC_SOURCEID);
1774 if (!(target & 8))
1775 printk(KERN_ERR "scsi%d: invalid source id after reselection "
1776 "- device fault?\n",
1777 host->host->host_no);
1779 target &= 7;
1781 if (host->SCpnt && !host->scsi.disconnectable) {
1782 printk(KERN_ERR "scsi%d.%d: reconnected while command in "
1783 "progress to target %d?\n",
1784 host->host->host_no, target, host->SCpnt->device->id);
1785 host->SCpnt = NULL;
1788 lun = sbic_arm_read(host, SBIC_DATA) & 7;
1790 host->scsi.reconnected.target = target;
1791 host->scsi.reconnected.lun = lun;
1792 host->scsi.reconnected.tag = 0;
1794 if (host->scsi.disconnectable && host->SCpnt &&
1795 host->SCpnt->device->id == target && host->SCpnt->device->lun == lun)
1796 ok = 1;
1798 if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
1799 ok = 1;
1801 ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1803 if (ok) {
1804 host->scsi.phase = PHASE_RECONNECTED;
1805 } else {
1806 /* this doesn't seem to work */
1807 printk(KERN_ERR "scsi%d.%c: reselected with no command "
1808 "to reconnect with\n",
1809 host->host->host_no, '0' + target);
1810 acornscsi_dumplog(host, target);
1811 acornscsi_abortcmd(host, 0);
1812 if (host->SCpnt) {
1813 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1814 host->SCpnt = NULL;
1817 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1818 return !ok;
1822 * Function: int acornscsi_reconect_finish(AS_Host *host)
1823 * Purpose : finish reconnecting a command
1824 * Params : host - host to complete
1825 * Returns : 0 if failed
1827 static
1828 int acornscsi_reconnect_finish(AS_Host *host)
1830 if (host->scsi.disconnectable && host->SCpnt) {
1831 host->scsi.disconnectable = 0;
1832 if (host->SCpnt->device->id == host->scsi.reconnected.target &&
1833 host->SCpnt->device->lun == host->scsi.reconnected.lun &&
1834 host->SCpnt->tag == host->scsi.reconnected.tag) {
1835 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1836 DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
1837 host->host->host_no, acornscsi_target(host)));
1838 #endif
1839 } else {
1840 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1841 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1842 DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
1843 "to disconnected queue\n",
1844 host->host->host_no, acornscsi_target(host)));
1845 #endif
1846 host->SCpnt = NULL;
1849 if (!host->SCpnt) {
1850 host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
1851 host->scsi.reconnected.target,
1852 host->scsi.reconnected.lun,
1853 host->scsi.reconnected.tag);
1854 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1855 DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
1856 host->host->host_no, acornscsi_target(host)));
1857 #endif
1860 if (!host->SCpnt)
1861 acornscsi_abortcmd(host, host->scsi.reconnected.tag);
1862 else {
1864 * Restore data pointer from SAVED pointers.
1866 host->scsi.SCp = host->SCpnt->SCp;
1867 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1868 printk(", data pointers: [%p, %X]",
1869 host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1870 #endif
1872 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1873 printk("\n");
1874 #endif
1876 host->dma.transferred = host->scsi.SCp.scsi_xferred;
1878 return host->SCpnt != NULL;
1882 * Function: void acornscsi_disconnect_unexpected(AS_Host *host)
1883 * Purpose : handle an unexpected disconnect
1884 * Params : host - host on which disconnect occurred
1886 static
1887 void acornscsi_disconnect_unexpected(AS_Host *host)
1889 printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1890 host->host->host_no, acornscsi_target(host));
1891 #if (DEBUG & DEBUG_ABORT)
1892 acornscsi_dumplog(host, 8);
1893 #endif
1895 acornscsi_done(host, &host->SCpnt, DID_ERROR);
1899 * Function: void acornscsi_abortcmd(AS_host *host, unsigned char tag)
1900 * Purpose : abort a currently executing command
1901 * Params : host - host with connected command to abort
1902 * tag - tag to abort
1904 static
1905 void acornscsi_abortcmd(AS_Host *host, unsigned char tag)
1907 host->scsi.phase = PHASE_ABORTED;
1908 sbic_arm_write(host, SBIC_CMND, CMND_ASSERTATN);
1910 msgqueue_flush(&host->scsi.msgs);
1911 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1912 if (tag)
1913 msgqueue_addmsg(&host->scsi.msgs, 2, ABORT_TAG, tag);
1914 else
1915 #endif
1916 msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1919 /* ==========================================================================================
1920 * Interrupt routines.
1923 * Function: int acornscsi_sbicintr(AS_Host *host)
1924 * Purpose : handle interrupts from SCSI device
1925 * Params : host - host to process
1926 * Returns : INTR_PROCESS if expecting another SBIC interrupt
1927 * INTR_IDLE if no interrupt
1928 * INTR_NEXT_COMMAND if we have finished processing the command
1930 static
1931 intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
1933 unsigned int asr, ssr;
1935 asr = sbic_arm_read(host, SBIC_ASR);
1936 if (!(asr & ASR_INT))
1937 return INTR_IDLE;
1939 ssr = sbic_arm_read(host, SBIC_SSR);
1941 #if (DEBUG & DEBUG_PHASES)
1942 print_sbic_status(asr, ssr, host->scsi.phase);
1943 #endif
1945 ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
1947 if (host->SCpnt && !host->scsi.disconnectable)
1948 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
1950 switch (ssr) {
1951 case 0x00: /* reset state - not advanced */
1952 printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
1953 host->host->host_no);
1954 /* setup sbic - WD33C93A */
1955 sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
1956 sbic_arm_write(host, SBIC_CMND, CMND_RESET);
1957 return INTR_IDLE;
1959 case 0x01: /* reset state - advanced */
1960 sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
1961 sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
1962 sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
1963 sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
1964 msgqueue_flush(&host->scsi.msgs);
1965 return INTR_IDLE;
1967 case 0x41: /* unexpected disconnect aborted command */
1968 acornscsi_disconnect_unexpected(host);
1969 return INTR_NEXT_COMMAND;
1972 switch (host->scsi.phase) {
1973 case PHASE_CONNECTING: /* STATE: command removed from issue queue */
1974 switch (ssr) {
1975 case 0x11: /* -> PHASE_CONNECTED */
1976 /* BUS FREE -> SELECTION */
1977 host->scsi.phase = PHASE_CONNECTED;
1978 msgqueue_flush(&host->scsi.msgs);
1979 host->dma.transferred = host->scsi.SCp.scsi_xferred;
1980 /* 33C93 gives next interrupt indicating bus phase */
1981 asr = sbic_arm_read(host, SBIC_ASR);
1982 if (!(asr & ASR_INT))
1983 break;
1984 ssr = sbic_arm_read(host, SBIC_SSR);
1985 ADD_STATUS(8, ssr, host->scsi.phase, 1);
1986 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, 1);
1987 goto connected;
1989 case 0x42: /* select timed out */
1990 /* -> PHASE_IDLE */
1991 acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
1992 return INTR_NEXT_COMMAND;
1994 case 0x81: /* -> PHASE_RECONNECTED or PHASE_ABORTED */
1995 /* BUS FREE -> RESELECTION */
1996 host->origSCpnt = host->SCpnt;
1997 host->SCpnt = NULL;
1998 msgqueue_flush(&host->scsi.msgs);
1999 acornscsi_reconnect(host);
2000 break;
2002 default:
2003 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
2004 host->host->host_no, acornscsi_target(host), ssr);
2005 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2006 acornscsi_abortcmd(host, host->SCpnt->tag);
2008 return INTR_PROCESSING;
2010 connected:
2011 case PHASE_CONNECTED: /* STATE: device selected ok */
2012 switch (ssr) {
2013 #ifdef NONSTANDARD
2014 case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2015 /* SELECTION -> COMMAND */
2016 acornscsi_sendcommand(host);
2017 break;
2019 case 0x8b: /* -> PHASE_STATUS */
2020 /* SELECTION -> STATUS */
2021 acornscsi_readstatusbyte(host);
2022 host->scsi.phase = PHASE_STATUSIN;
2023 break;
2024 #endif
2026 case 0x8e: /* -> PHASE_MSGOUT */
2027 /* SELECTION ->MESSAGE OUT */
2028 host->scsi.phase = PHASE_MSGOUT;
2029 acornscsi_buildmessages(host);
2030 acornscsi_sendmessage(host);
2031 break;
2033 /* these should not happen */
2034 case 0x85: /* target disconnected */
2035 acornscsi_done(host, &host->SCpnt, DID_ERROR);
2036 break;
2038 default:
2039 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
2040 host->host->host_no, acornscsi_target(host), ssr);
2041 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2042 acornscsi_abortcmd(host, host->SCpnt->tag);
2044 return INTR_PROCESSING;
2046 case PHASE_MSGOUT: /* STATE: connected & sent IDENTIFY message */
2048 * SCSI standard says that MESSAGE OUT phases can be followed by a
2049 * DATA phase, STATUS phase, MESSAGE IN phase or COMMAND phase
2051 switch (ssr) {
2052 case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2053 case 0x1a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2054 /* MESSAGE OUT -> COMMAND */
2055 acornscsi_sendcommand(host);
2056 break;
2058 case 0x8b: /* -> PHASE_STATUS */
2059 case 0x1b: /* -> PHASE_STATUS */
2060 /* MESSAGE OUT -> STATUS */
2061 acornscsi_readstatusbyte(host);
2062 host->scsi.phase = PHASE_STATUSIN;
2063 break;
2065 case 0x8e: /* -> PHASE_MSGOUT */
2066 /* MESSAGE_OUT(MESSAGE_IN) ->MESSAGE OUT */
2067 acornscsi_sendmessage(host);
2068 break;
2070 case 0x4f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2071 case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2072 /* MESSAGE OUT -> MESSAGE IN */
2073 acornscsi_message(host);
2074 break;
2076 default:
2077 printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
2078 host->host->host_no, acornscsi_target(host), ssr);
2079 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2081 return INTR_PROCESSING;
2083 case PHASE_COMMAND: /* STATE: connected & command sent */
2084 switch (ssr) {
2085 case 0x18: /* -> PHASE_DATAOUT */
2086 /* COMMAND -> DATA OUT */
2087 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2088 acornscsi_abortcmd(host, host->SCpnt->tag);
2089 acornscsi_dma_setup(host, DMA_OUT);
2090 if (!acornscsi_starttransfer(host))
2091 acornscsi_abortcmd(host, host->SCpnt->tag);
2092 host->scsi.phase = PHASE_DATAOUT;
2093 return INTR_IDLE;
2095 case 0x19: /* -> PHASE_DATAIN */
2096 /* COMMAND -> DATA IN */
2097 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2098 acornscsi_abortcmd(host, host->SCpnt->tag);
2099 acornscsi_dma_setup(host, DMA_IN);
2100 if (!acornscsi_starttransfer(host))
2101 acornscsi_abortcmd(host, host->SCpnt->tag);
2102 host->scsi.phase = PHASE_DATAIN;
2103 return INTR_IDLE;
2105 case 0x1b: /* -> PHASE_STATUS */
2106 /* COMMAND -> STATUS */
2107 acornscsi_readstatusbyte(host);
2108 host->scsi.phase = PHASE_STATUSIN;
2109 break;
2111 case 0x1e: /* -> PHASE_MSGOUT */
2112 /* COMMAND -> MESSAGE OUT */
2113 acornscsi_sendmessage(host);
2114 break;
2116 case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2117 /* COMMAND -> MESSAGE IN */
2118 acornscsi_message(host);
2119 break;
2121 default:
2122 printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2123 host->host->host_no, acornscsi_target(host), ssr);
2124 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2126 return INTR_PROCESSING;
2128 case PHASE_DISCONNECT: /* STATE: connected, received DISCONNECT msg */
2129 if (ssr == 0x85) { /* -> PHASE_IDLE */
2130 host->scsi.disconnectable = 1;
2131 host->scsi.reconnected.tag = 0;
2132 host->scsi.phase = PHASE_IDLE;
2133 host->stats.disconnects += 1;
2134 } else {
2135 printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2136 host->host->host_no, acornscsi_target(host), ssr);
2137 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2139 return INTR_NEXT_COMMAND;
2141 case PHASE_IDLE: /* STATE: disconnected */
2142 if (ssr == 0x81) /* -> PHASE_RECONNECTED or PHASE_ABORTED */
2143 acornscsi_reconnect(host);
2144 else {
2145 printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2146 host->host->host_no, acornscsi_target(host), ssr);
2147 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2149 return INTR_PROCESSING;
2151 case PHASE_RECONNECTED: /* STATE: device reconnected to initiator */
2153 * Command reconnected - if MESGIN, get message - it may be
2154 * the tag. If not, get command out of disconnected queue
2157 * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
2158 * reconnect I_T_L command
2160 if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
2161 return INTR_IDLE;
2162 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
2163 switch (ssr) {
2164 case 0x88: /* data out phase */
2165 /* -> PHASE_DATAOUT */
2166 /* MESSAGE IN -> DATA OUT */
2167 acornscsi_dma_setup(host, DMA_OUT);
2168 if (!acornscsi_starttransfer(host))
2169 acornscsi_abortcmd(host, host->SCpnt->tag);
2170 host->scsi.phase = PHASE_DATAOUT;
2171 return INTR_IDLE;
2173 case 0x89: /* data in phase */
2174 /* -> PHASE_DATAIN */
2175 /* MESSAGE IN -> DATA IN */
2176 acornscsi_dma_setup(host, DMA_IN);
2177 if (!acornscsi_starttransfer(host))
2178 acornscsi_abortcmd(host, host->SCpnt->tag);
2179 host->scsi.phase = PHASE_DATAIN;
2180 return INTR_IDLE;
2182 case 0x8a: /* command out */
2183 /* MESSAGE IN -> COMMAND */
2184 acornscsi_sendcommand(host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2185 break;
2187 case 0x8b: /* status in */
2188 /* -> PHASE_STATUSIN */
2189 /* MESSAGE IN -> STATUS */
2190 acornscsi_readstatusbyte(host);
2191 host->scsi.phase = PHASE_STATUSIN;
2192 break;
2194 case 0x8e: /* message out */
2195 /* -> PHASE_MSGOUT */
2196 /* MESSAGE IN -> MESSAGE OUT */
2197 acornscsi_sendmessage(host);
2198 break;
2200 case 0x8f: /* message in */
2201 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2202 break;
2204 default:
2205 printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2206 host->host->host_no, acornscsi_target(host), ssr);
2207 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2209 return INTR_PROCESSING;
2211 case PHASE_DATAIN: /* STATE: transferred data in */
2213 * This is simple - if we disconnect then the DMA address & count is
2214 * correct.
2216 switch (ssr) {
2217 case 0x19: /* -> PHASE_DATAIN */
2218 case 0x89: /* -> PHASE_DATAIN */
2219 acornscsi_abortcmd(host, host->SCpnt->tag);
2220 return INTR_IDLE;
2222 case 0x1b: /* -> PHASE_STATUSIN */
2223 case 0x4b: /* -> PHASE_STATUSIN */
2224 case 0x8b: /* -> PHASE_STATUSIN */
2225 /* DATA IN -> STATUS */
2226 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2227 acornscsi_sbic_xfcount(host);
2228 acornscsi_dma_stop(host);
2229 acornscsi_readstatusbyte(host);
2230 host->scsi.phase = PHASE_STATUSIN;
2231 break;
2233 case 0x1e: /* -> PHASE_MSGOUT */
2234 case 0x4e: /* -> PHASE_MSGOUT */
2235 case 0x8e: /* -> PHASE_MSGOUT */
2236 /* DATA IN -> MESSAGE OUT */
2237 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2238 acornscsi_sbic_xfcount(host);
2239 acornscsi_dma_stop(host);
2240 acornscsi_sendmessage(host);
2241 break;
2243 case 0x1f: /* message in */
2244 case 0x4f: /* message in */
2245 case 0x8f: /* message in */
2246 /* DATA IN -> MESSAGE IN */
2247 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2248 acornscsi_sbic_xfcount(host);
2249 acornscsi_dma_stop(host);
2250 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2251 break;
2253 default:
2254 printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2255 host->host->host_no, acornscsi_target(host), ssr);
2256 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2258 return INTR_PROCESSING;
2260 case PHASE_DATAOUT: /* STATE: transferred data out */
2262 * This is more complicated - if we disconnect, the DMA could be 12
2263 * bytes ahead of us. We need to correct this.
2265 switch (ssr) {
2266 case 0x18: /* -> PHASE_DATAOUT */
2267 case 0x88: /* -> PHASE_DATAOUT */
2268 acornscsi_abortcmd(host, host->SCpnt->tag);
2269 return INTR_IDLE;
2271 case 0x1b: /* -> PHASE_STATUSIN */
2272 case 0x4b: /* -> PHASE_STATUSIN */
2273 case 0x8b: /* -> PHASE_STATUSIN */
2274 /* DATA OUT -> STATUS */
2275 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2276 acornscsi_sbic_xfcount(host);
2277 acornscsi_dma_stop(host);
2278 acornscsi_dma_adjust(host);
2279 acornscsi_readstatusbyte(host);
2280 host->scsi.phase = PHASE_STATUSIN;
2281 break;
2283 case 0x1e: /* -> PHASE_MSGOUT */
2284 case 0x4e: /* -> PHASE_MSGOUT */
2285 case 0x8e: /* -> PHASE_MSGOUT */
2286 /* DATA OUT -> MESSAGE OUT */
2287 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2288 acornscsi_sbic_xfcount(host);
2289 acornscsi_dma_stop(host);
2290 acornscsi_dma_adjust(host);
2291 acornscsi_sendmessage(host);
2292 break;
2294 case 0x1f: /* message in */
2295 case 0x4f: /* message in */
2296 case 0x8f: /* message in */
2297 /* DATA OUT -> MESSAGE IN */
2298 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2299 acornscsi_sbic_xfcount(host);
2300 acornscsi_dma_stop(host);
2301 acornscsi_dma_adjust(host);
2302 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2303 break;
2305 default:
2306 printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2307 host->host->host_no, acornscsi_target(host), ssr);
2308 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2310 return INTR_PROCESSING;
2312 case PHASE_STATUSIN: /* STATE: status in complete */
2313 switch (ssr) {
2314 case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2315 case 0x8f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2316 /* STATUS -> MESSAGE IN */
2317 acornscsi_message(host);
2318 break;
2320 case 0x1e: /* -> PHASE_MSGOUT */
2321 case 0x8e: /* -> PHASE_MSGOUT */
2322 /* STATUS -> MESSAGE OUT */
2323 acornscsi_sendmessage(host);
2324 break;
2326 default:
2327 printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2328 host->host->host_no, acornscsi_target(host), ssr);
2329 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2331 return INTR_PROCESSING;
2333 case PHASE_MSGIN: /* STATE: message in */
2334 switch (ssr) {
2335 case 0x1e: /* -> PHASE_MSGOUT */
2336 case 0x4e: /* -> PHASE_MSGOUT */
2337 case 0x8e: /* -> PHASE_MSGOUT */
2338 /* MESSAGE IN -> MESSAGE OUT */
2339 acornscsi_sendmessage(host);
2340 break;
2342 case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2343 case 0x2f:
2344 case 0x4f:
2345 case 0x8f:
2346 acornscsi_message(host);
2347 break;
2349 case 0x85:
2350 printk("scsi%d.%c: strange message in disconnection\n",
2351 host->host->host_no, acornscsi_target(host));
2352 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2353 acornscsi_done(host, &host->SCpnt, DID_ERROR);
2354 break;
2356 default:
2357 printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2358 host->host->host_no, acornscsi_target(host), ssr);
2359 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2361 return INTR_PROCESSING;
2363 case PHASE_DONE: /* STATE: received status & message */
2364 switch (ssr) {
2365 case 0x85: /* -> PHASE_IDLE */
2366 acornscsi_done(host, &host->SCpnt, DID_OK);
2367 return INTR_NEXT_COMMAND;
2369 case 0x1e:
2370 case 0x8e:
2371 acornscsi_sendmessage(host);
2372 break;
2374 default:
2375 printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2376 host->host->host_no, acornscsi_target(host), ssr);
2377 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2379 return INTR_PROCESSING;
2381 case PHASE_ABORTED:
2382 switch (ssr) {
2383 case 0x85:
2384 if (host->SCpnt)
2385 acornscsi_done(host, &host->SCpnt, DID_ABORT);
2386 else {
2387 clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
2388 host->busyluns);
2389 host->scsi.phase = PHASE_IDLE;
2391 return INTR_NEXT_COMMAND;
2393 case 0x1e:
2394 case 0x2e:
2395 case 0x4e:
2396 case 0x8e:
2397 acornscsi_sendmessage(host);
2398 break;
2400 default:
2401 printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2402 host->host->host_no, acornscsi_target(host), ssr);
2403 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2405 return INTR_PROCESSING;
2407 default:
2408 printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2409 host->host->host_no, acornscsi_target(host), ssr);
2410 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2412 return INTR_PROCESSING;
2416 * Prototype: void acornscsi_intr(int irq, void *dev_id)
2417 * Purpose : handle interrupts from Acorn SCSI card
2418 * Params : irq - interrupt number
2419 * dev_id - device specific data (AS_Host structure)
2421 static irqreturn_t
2422 acornscsi_intr(int irq, void *dev_id)
2424 AS_Host *host = (AS_Host *)dev_id;
2425 intr_ret_t ret;
2426 int iostatus;
2427 int in_irq = 0;
2429 do {
2430 ret = INTR_IDLE;
2432 iostatus = readb(host->fast + INT_REG);
2434 if (iostatus & 2) {
2435 acornscsi_dma_intr(host);
2436 iostatus = readb(host->fast + INT_REG);
2439 if (iostatus & 8)
2440 ret = acornscsi_sbicintr(host, in_irq);
2443 * If we have a transfer pending, start it.
2444 * Only start it if the interface has already started transferring
2445 * it's data
2447 if (host->dma.xfer_required)
2448 acornscsi_dma_xfer(host);
2450 if (ret == INTR_NEXT_COMMAND)
2451 ret = acornscsi_kick(host);
2453 in_irq = 1;
2454 } while (ret != INTR_IDLE);
2456 return IRQ_HANDLED;
2459 /*=============================================================================================
2460 * Interfaces between interrupt handler and rest of scsi code
2464 * Function : acornscsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2465 * Purpose : queues a SCSI command
2466 * Params : cmd - SCSI command
2467 * done - function called on completion, with pointer to command descriptor
2468 * Returns : 0, or < 0 on error.
2470 static int acornscsi_queuecmd_lck(struct scsi_cmnd *SCpnt,
2471 void (*done)(struct scsi_cmnd *))
2473 AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2475 if (!done) {
2476 /* there should be some way of rejecting errors like this without panicing... */
2477 panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
2478 host->host->host_no, SCpnt);
2479 return -EINVAL;
2482 #if (DEBUG & DEBUG_NO_WRITE)
2483 if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
2484 printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2485 host->host->host_no, '0' + SCpnt->device->id);
2486 SCpnt->result = DID_NO_CONNECT << 16;
2487 done(SCpnt);
2488 return 0;
2490 #endif
2492 SCpnt->scsi_done = done;
2493 SCpnt->host_scribble = NULL;
2494 SCpnt->result = 0;
2495 SCpnt->tag = 0;
2496 SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
2497 SCpnt->SCp.sent_command = 0;
2498 SCpnt->SCp.scsi_xferred = 0;
2500 init_SCp(SCpnt);
2502 host->stats.queues += 1;
2505 unsigned long flags;
2507 if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
2508 SCpnt->result = DID_ERROR << 16;
2509 done(SCpnt);
2510 return 0;
2512 local_irq_save(flags);
2513 if (host->scsi.phase == PHASE_IDLE)
2514 acornscsi_kick(host);
2515 local_irq_restore(flags);
2517 return 0;
2520 DEF_SCSI_QCMD(acornscsi_queuecmd)
2523 * Prototype: void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1, struct scsi_cmnd **SCpntp2, int result)
2524 * Purpose : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
2525 * Params : SCpntp1 - pointer to command to return
2526 * SCpntp2 - pointer to command to check
2527 * result - result to pass back to mid-level done function
2528 * Returns : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
2530 static inline void acornscsi_reportstatus(struct scsi_cmnd **SCpntp1,
2531 struct scsi_cmnd **SCpntp2,
2532 int result)
2534 struct scsi_cmnd *SCpnt = *SCpntp1;
2536 if (SCpnt) {
2537 *SCpntp1 = NULL;
2539 SCpnt->result = result;
2540 SCpnt->scsi_done(SCpnt);
2543 if (SCpnt == *SCpntp2)
2544 *SCpntp2 = NULL;
2547 enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
2550 * Prototype: enum res acornscsi_do_abort(struct scsi_cmnd *SCpnt)
2551 * Purpose : abort a command on this host
2552 * Params : SCpnt - command to abort
2553 * Returns : our abort status
2555 static enum res_abort acornscsi_do_abort(AS_Host *host, struct scsi_cmnd *SCpnt)
2557 enum res_abort res = res_not_running;
2559 if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
2561 * The command was on the issue queue, and has not been
2562 * issued yet. We can remove the command from the queue,
2563 * and acknowledge the abort. Neither the devices nor the
2564 * interface know about the command.
2566 //#if (DEBUG & DEBUG_ABORT)
2567 printk("on issue queue ");
2568 //#endif
2569 res = res_success;
2570 } else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
2572 * The command was on the disconnected queue. Simply
2573 * acknowledge the abort condition, and when the target
2574 * reconnects, we will give it an ABORT message. The
2575 * target should then disconnect, and we will clear
2576 * the busylun bit.
2578 //#if (DEBUG & DEBUG_ABORT)
2579 printk("on disconnected queue ");
2580 //#endif
2581 res = res_success;
2582 } else if (host->SCpnt == SCpnt) {
2583 unsigned long flags;
2585 //#if (DEBUG & DEBUG_ABORT)
2586 printk("executing ");
2587 //#endif
2589 local_irq_save(flags);
2590 switch (host->scsi.phase) {
2592 * If the interface is idle, and the command is 'disconnectable',
2593 * then it is the same as on the disconnected queue. We simply
2594 * remove all traces of the command. When the target reconnects,
2595 * we will give it an ABORT message since the command could not
2596 * be found. When the target finally disconnects, we will clear
2597 * the busylun bit.
2599 case PHASE_IDLE:
2600 if (host->scsi.disconnectable) {
2601 host->scsi.disconnectable = 0;
2602 host->SCpnt = NULL;
2603 res = res_success;
2605 break;
2608 * If the command has connected and done nothing further,
2609 * simply force a disconnect. We also need to clear the
2610 * busylun bit.
2612 case PHASE_CONNECTED:
2613 sbic_arm_write(host, SBIC_CMND, CMND_DISCONNECT);
2614 host->SCpnt = NULL;
2615 res = res_success_clear;
2616 break;
2618 default:
2619 acornscsi_abortcmd(host, host->SCpnt->tag);
2620 res = res_snooze;
2622 local_irq_restore(flags);
2623 } else if (host->origSCpnt == SCpnt) {
2625 * The command will be executed next, but a command
2626 * is currently using the interface. This is similar to
2627 * being on the issue queue, except the busylun bit has
2628 * been set.
2630 host->origSCpnt = NULL;
2631 //#if (DEBUG & DEBUG_ABORT)
2632 printk("waiting for execution ");
2633 //#endif
2634 res = res_success_clear;
2635 } else
2636 printk("unknown ");
2638 return res;
2642 * Prototype: int acornscsi_abort(struct scsi_cmnd *SCpnt)
2643 * Purpose : abort a command on this host
2644 * Params : SCpnt - command to abort
2645 * Returns : one of SCSI_ABORT_ macros
2647 int acornscsi_abort(struct scsi_cmnd *SCpnt)
2649 AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
2650 int result;
2652 host->stats.aborts += 1;
2654 #if (DEBUG & DEBUG_ABORT)
2656 int asr, ssr;
2657 asr = sbic_arm_read(host, SBIC_ASR);
2658 ssr = sbic_arm_read(host, SBIC_SSR);
2660 printk(KERN_WARNING "acornscsi_abort: ");
2661 print_sbic_status(asr, ssr, host->scsi.phase);
2662 acornscsi_dumplog(host, SCpnt->device->id);
2664 #endif
2666 printk("scsi%d: ", host->host->host_no);
2668 switch (acornscsi_do_abort(host, SCpnt)) {
2670 * We managed to find the command and cleared it out.
2671 * We do not expect the command to be executing on the
2672 * target, but we have set the busylun bit.
2674 case res_success_clear:
2675 //#if (DEBUG & DEBUG_ABORT)
2676 printk("clear ");
2677 //#endif
2678 clear_bit(SCpnt->device->id * 8 +
2679 (u8)(SCpnt->device->lun & 0x7), host->busyluns);
2682 * We found the command, and cleared it out. Either
2683 * the command is still known to be executing on the
2684 * target, or the busylun bit is not set.
2686 case res_success:
2687 //#if (DEBUG & DEBUG_ABORT)
2688 printk("success\n");
2689 //#endif
2690 result = SUCCESS;
2691 break;
2694 * We did find the command, but unfortunately we couldn't
2695 * unhook it from ourselves. Wait some more, and if it
2696 * still doesn't complete, reset the interface.
2698 case res_snooze:
2699 //#if (DEBUG & DEBUG_ABORT)
2700 printk("snooze\n");
2701 //#endif
2702 result = FAILED;
2703 break;
2706 * The command could not be found (either because it completed,
2707 * or it got dropped.
2709 default:
2710 case res_not_running:
2711 acornscsi_dumplog(host, SCpnt->device->id);
2712 result = FAILED;
2713 //#if (DEBUG & DEBUG_ABORT)
2714 printk("not running\n");
2715 //#endif
2716 break;
2719 return result;
2723 * Prototype: int acornscsi_reset(struct scsi_cmnd *SCpnt)
2724 * Purpose : reset a command on this host/reset this host
2725 * Params : SCpnt - command causing reset
2726 * Returns : one of SCSI_RESET_ macros
2728 int acornscsi_host_reset(struct scsi_cmnd *SCpnt)
2730 AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2731 struct scsi_cmnd *SCptr;
2733 host->stats.resets += 1;
2735 #if (DEBUG & DEBUG_RESET)
2737 int asr, ssr, devidx;
2739 asr = sbic_arm_read(host, SBIC_ASR);
2740 ssr = sbic_arm_read(host, SBIC_SSR);
2742 printk(KERN_WARNING "acornscsi_reset: ");
2743 print_sbic_status(asr, ssr, host->scsi.phase);
2744 for (devidx = 0; devidx < 9; devidx++)
2745 acornscsi_dumplog(host, devidx);
2747 #endif
2749 acornscsi_dma_stop(host);
2752 * do hard reset. This resets all devices on this host, and so we
2753 * must set the reset status on all commands.
2755 acornscsi_resetcard(host);
2757 while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
2760 return SUCCESS;
2763 /*==============================================================================================
2764 * initialisation & miscellaneous support
2768 * Function: char *acornscsi_info(struct Scsi_Host *host)
2769 * Purpose : return a string describing this interface
2770 * Params : host - host to give information on
2771 * Returns : a constant string
2773 const
2774 char *acornscsi_info(struct Scsi_Host *host)
2776 static char string[100], *p;
2778 p = string;
2780 p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
2781 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2782 " SYNC"
2783 #endif
2784 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2785 " TAG"
2786 #endif
2787 #if (DEBUG & DEBUG_NO_WRITE)
2788 " NOWRITE (" __stringify(NO_WRITE) ")"
2789 #endif
2790 , host->hostt->name, host->io_port, host->irq,
2791 VER_MAJOR, VER_MINOR, VER_PATCH);
2792 return string;
2795 static int acornscsi_show_info(struct seq_file *m, struct Scsi_Host *instance)
2797 int devidx;
2798 struct scsi_device *scd;
2799 AS_Host *host;
2801 host = (AS_Host *)instance->hostdata;
2803 seq_printf(m, "AcornSCSI driver v%d.%d.%d"
2804 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2805 " SYNC"
2806 #endif
2807 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2808 " TAG"
2809 #endif
2810 #if (DEBUG & DEBUG_NO_WRITE)
2811 " NOWRITE (" __stringify(NO_WRITE) ")"
2812 #endif
2813 "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
2815 seq_printf(m, "SBIC: WD33C93A Address: %p IRQ : %d\n",
2816 host->base + SBIC_REGIDX, host->scsi.irq);
2817 #ifdef USE_DMAC
2818 seq_printf(m, "DMAC: uPC71071 Address: %p IRQ : %d\n\n",
2819 host->base + DMAC_OFFSET, host->scsi.irq);
2820 #endif
2822 seq_printf(m, "Statistics:\n"
2823 "Queued commands: %-10u Issued commands: %-10u\n"
2824 "Done commands : %-10u Reads : %-10u\n"
2825 "Writes : %-10u Others : %-10u\n"
2826 "Disconnects : %-10u Aborts : %-10u\n"
2827 "Resets : %-10u\n\nLast phases:",
2828 host->stats.queues, host->stats.removes,
2829 host->stats.fins, host->stats.reads,
2830 host->stats.writes, host->stats.miscs,
2831 host->stats.disconnects, host->stats.aborts,
2832 host->stats.resets);
2834 for (devidx = 0; devidx < 9; devidx ++) {
2835 unsigned int statptr, prev;
2837 seq_printf(m, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
2838 statptr = host->status_ptr[devidx] - 10;
2840 if ((signed int)statptr < 0)
2841 statptr += STATUS_BUFFER_SIZE;
2843 prev = host->status[devidx][statptr].when;
2845 for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
2846 if (host->status[devidx][statptr].when) {
2847 seq_printf(m, "%c%02X:%02X+%2ld",
2848 host->status[devidx][statptr].irq ? '-' : ' ',
2849 host->status[devidx][statptr].ph,
2850 host->status[devidx][statptr].ssr,
2851 (host->status[devidx][statptr].when - prev) < 100 ?
2852 (host->status[devidx][statptr].when - prev) : 99);
2853 prev = host->status[devidx][statptr].when;
2858 seq_printf(m, "\nAttached devices:\n");
2860 shost_for_each_device(scd, instance) {
2861 seq_printf(m, "Device/Lun TaggedQ Sync\n");
2862 seq_printf(m, " %d/%llu ", scd->id, scd->lun);
2863 if (scd->tagged_supported)
2864 seq_printf(m, "%3sabled(%3d) ",
2865 scd->simple_tags ? "en" : "dis",
2866 scd->current_tag);
2867 else
2868 seq_printf(m, "unsupported ");
2870 if (host->device[scd->id].sync_xfer & 15)
2871 seq_printf(m, "offset %d, %d ns\n",
2872 host->device[scd->id].sync_xfer & 15,
2873 acornscsi_getperiod(host->device[scd->id].sync_xfer));
2874 else
2875 seq_printf(m, "async\n");
2878 return 0;
2881 static struct scsi_host_template acornscsi_template = {
2882 .module = THIS_MODULE,
2883 .show_info = acornscsi_show_info,
2884 .name = "AcornSCSI",
2885 .info = acornscsi_info,
2886 .queuecommand = acornscsi_queuecmd,
2887 .eh_abort_handler = acornscsi_abort,
2888 .eh_host_reset_handler = acornscsi_host_reset,
2889 .can_queue = 16,
2890 .this_id = 7,
2891 .sg_tablesize = SG_ALL,
2892 .cmd_per_lun = 2,
2893 .use_clustering = DISABLE_CLUSTERING,
2894 .proc_name = "acornscsi",
2897 static int acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
2899 struct Scsi_Host *host;
2900 AS_Host *ashost;
2901 int ret;
2903 ret = ecard_request_resources(ec);
2904 if (ret)
2905 goto out;
2907 host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
2908 if (!host) {
2909 ret = -ENOMEM;
2910 goto out_release;
2913 ashost = (AS_Host *)host->hostdata;
2915 ashost->base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
2916 ashost->fast = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
2917 if (!ashost->base || !ashost->fast)
2918 goto out_put;
2920 host->irq = ec->irq;
2921 ashost->host = host;
2922 ashost->scsi.irq = host->irq;
2924 ec->irqaddr = ashost->fast + INT_REG;
2925 ec->irqmask = 0x0a;
2927 ret = request_irq(host->irq, acornscsi_intr, 0, "acornscsi", ashost);
2928 if (ret) {
2929 printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
2930 host->host_no, ashost->scsi.irq, ret);
2931 goto out_put;
2934 memset(&ashost->stats, 0, sizeof (ashost->stats));
2935 queue_initialise(&ashost->queues.issue);
2936 queue_initialise(&ashost->queues.disconnected);
2937 msgqueue_initialise(&ashost->scsi.msgs);
2939 acornscsi_resetcard(ashost);
2941 ret = scsi_add_host(host, &ec->dev);
2942 if (ret)
2943 goto out_irq;
2945 scsi_scan_host(host);
2946 goto out;
2948 out_irq:
2949 free_irq(host->irq, ashost);
2950 msgqueue_free(&ashost->scsi.msgs);
2951 queue_free(&ashost->queues.disconnected);
2952 queue_free(&ashost->queues.issue);
2953 out_put:
2954 ecardm_iounmap(ec, ashost->fast);
2955 ecardm_iounmap(ec, ashost->base);
2956 scsi_host_put(host);
2957 out_release:
2958 ecard_release_resources(ec);
2959 out:
2960 return ret;
2963 static void acornscsi_remove(struct expansion_card *ec)
2965 struct Scsi_Host *host = ecard_get_drvdata(ec);
2966 AS_Host *ashost = (AS_Host *)host->hostdata;
2968 ecard_set_drvdata(ec, NULL);
2969 scsi_remove_host(host);
2972 * Put card into RESET state
2974 writeb(0x80, ashost->fast + PAGE_REG);
2976 free_irq(host->irq, ashost);
2978 msgqueue_free(&ashost->scsi.msgs);
2979 queue_free(&ashost->queues.disconnected);
2980 queue_free(&ashost->queues.issue);
2981 ecardm_iounmap(ec, ashost->fast);
2982 ecardm_iounmap(ec, ashost->base);
2983 scsi_host_put(host);
2984 ecard_release_resources(ec);
2987 static const struct ecard_id acornscsi_cids[] = {
2988 { MANU_ACORN, PROD_ACORN_SCSI },
2989 { 0xffff, 0xffff },
2992 static struct ecard_driver acornscsi_driver = {
2993 .probe = acornscsi_probe,
2994 .remove = acornscsi_remove,
2995 .id_table = acornscsi_cids,
2996 .drv = {
2997 .name = "acornscsi",
3001 static int __init acornscsi_init(void)
3003 return ecard_register_driver(&acornscsi_driver);
3006 static void __exit acornscsi_exit(void)
3008 ecard_remove_driver(&acornscsi_driver);
3011 module_init(acornscsi_init);
3012 module_exit(acornscsi_exit);
3014 MODULE_AUTHOR("Russell King");
3015 MODULE_DESCRIPTION("AcornSCSI driver");
3016 MODULE_LICENSE("GPL");