1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/acorn/scsi/acornscsi.c
8 * Abandoned using the Select and Transfer command since there were
9 * some nasty races between our software and the target devices that
10 * were not easy to solve, and the device errata had a lot of entries
11 * for this command, some of them quite nasty...
14 * 26-Sep-1997 RMK Re-jigged to use the queue module.
15 * Re-coded state machine to be based on driver
16 * state not scsi state. Should be easier to debug.
17 * Added acornscsi_release to clean up properly.
18 * Updated proc/scsi reporting.
19 * 05-Oct-1997 RMK Implemented writing to SCSI devices.
20 * 06-Oct-1997 RMK Corrected small (non-serious) bug with the connect/
21 * reconnect race condition causing a warning message.
22 * 12-Oct-1997 RMK Added catch for re-entering interrupt routine.
23 * 15-Oct-1997 RMK Improved handling of commands.
24 * 27-Jun-1998 RMK Changed asm/delay.h to linux/delay.h.
25 * 13-Dec-1998 RMK Better abort code and command handling. Extra state
26 * transitions added to allow dodgy devices to work.
28 #define DEBUG_NO_WRITE 1
29 #define DEBUG_QUEUES 2
32 #define DEBUG_DISCON 16
33 #define DEBUG_CONNECT 32
34 #define DEBUG_PHASES 64
35 #define DEBUG_WRITE 128
36 #define DEBUG_LINK 256
37 #define DEBUG_MESSAGES 512
38 #define DEBUG_RESET 1024
39 #define DEBUG_ALL (DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
40 DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
41 DEBUG_DMA|DEBUG_QUEUES)
43 /* DRIVER CONFIGURATION
45 * SCSI-II Tagged queue support.
47 * I don't have any SCSI devices that support it, so it is totally untested
48 * (except to make sure that it doesn't interfere with any non-tagging
49 * devices). It is not fully implemented either - what happens when a
50 * tagging device reconnects???
52 * You can tell if you have a device that supports tagged queueing my
53 * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
58 * SCSI-II Synchronous transfer support.
62 * SDTR_SIZE - maximum number of un-acknowledged bytes (0 = off, 12 = max)
63 * SDTR_PERIOD - period of REQ signal (min=125, max=1020)
64 * DEFAULT_PERIOD - default REQ period.
67 #define SDTR_PERIOD 125
68 #define DEFAULT_PERIOD 500
71 * Debugging information
73 * DEBUG - bit mask from list above
74 * DEBUG_TARGET - is defined to the target number if you want to debug
75 * a specific target. [only recon/write/dma].
77 #define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
78 /* only allow writing to SCSI device 0 */
80 /*#define DEBUG_TARGET 2*/
82 * Select timeout time (in 10ms units)
84 * This is the timeout used between the start of selection and the WD33C93
85 * chip deciding that the device isn't responding.
87 #define TIMEOUT_TIME 10
89 * Define this if you want to have verbose explanation of SCSI
92 #undef CONFIG_ACORNSCSI_CONSTANTS
94 * Define this if you want to use the on board DMAC [don't remove this option]
95 * If not set, then use PIO mode (not currently supported).
100 * ====================================================================================
104 #define DBG(cmd,xxx...) \
105 if (cmd->device->id == DEBUG_TARGET) { \
109 #define DBG(cmd,xxx...) xxx
112 #include <linux/module.h>
113 #include <linux/kernel.h>
114 #include <linux/string.h>
115 #include <linux/signal.h>
116 #include <linux/errno.h>
117 #include <linux/proc_fs.h>
118 #include <linux/ioport.h>
119 #include <linux/blkdev.h>
120 #include <linux/delay.h>
121 #include <linux/interrupt.h>
122 #include <linux/init.h>
123 #include <linux/bitops.h>
124 #include <linux/stringify.h>
125 #include <linux/io.h>
127 #include <asm/ecard.h>
130 #include <scsi/scsi_dbg.h>
131 #include <scsi/scsi_host.h>
132 #include <scsi/scsi_transport_spi.h>
133 #include "acornscsi.h"
134 #include "msgqueue.h"
137 #include <scsi/scsicam.h>
145 * DMAC setup parameters
147 #define INIT_DEVCON0 (DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
148 #define INIT_DEVCON1 (DEVCON1_BHLD)
149 #define DMAC_READ (MODECON_READ)
150 #define DMAC_WRITE (MODECON_WRITE)
151 #define INIT_SBICDMA (CTRL_DMABURST)
153 #define scsi_xferred have_data_in
156 * Size of on-board DMA buffer
158 #define DMAC_BUFFER_SIZE 65536
161 #define STATUS_BUFFER_TO_PRINT 24
163 unsigned int sdtr_period = SDTR_PERIOD;
164 unsigned int sdtr_size = SDTR_SIZE;
166 static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
167 unsigned int result);
168 static int acornscsi_reconnect_finish(AS_Host *host);
169 static void acornscsi_dma_cleanup(AS_Host *host);
170 static void acornscsi_abortcmd(AS_Host *host);
172 /* ====================================================================================
176 /* Offsets from MEMC base */
177 #define SBIC_REGIDX 0x2000
178 #define SBIC_REGVAL 0x2004
179 #define DMAC_OFFSET 0x3000
181 /* Offsets from FAST IOC base */
182 #define INT_REG 0x2000
183 #define PAGE_REG 0x3000
185 static inline void sbic_arm_write(AS_Host *host, unsigned int reg, unsigned int value)
187 writeb(reg, host->base + SBIC_REGIDX);
188 writeb(value, host->base + SBIC_REGVAL);
191 static inline int sbic_arm_read(AS_Host *host, unsigned int reg)
194 return readl(host->base + SBIC_REGIDX) & 255;
195 writeb(reg, host->base + SBIC_REGIDX);
196 return readl(host->base + SBIC_REGVAL) & 255;
199 #define sbic_arm_writenext(host, val) writeb((val), (host)->base + SBIC_REGVAL)
200 #define sbic_arm_readnext(host) readb((host)->base + SBIC_REGVAL)
203 #define dmac_read(host,reg) \
204 readb((host)->base + DMAC_OFFSET + ((reg) << 2))
206 #define dmac_write(host,reg,value) \
207 ({ writeb((value), (host)->base + DMAC_OFFSET + ((reg) << 2)); })
209 #define dmac_clearintr(host) writeb(0, (host)->fast + INT_REG)
211 static inline unsigned int dmac_address(AS_Host *host)
213 return dmac_read(host, DMAC_TXADRHI) << 16 |
214 dmac_read(host, DMAC_TXADRMD) << 8 |
215 dmac_read(host, DMAC_TXADRLO);
219 void acornscsi_dumpdma(AS_Host *host, char *where)
221 unsigned int mode, addr, len;
223 mode = dmac_read(host, DMAC_MODECON);
224 addr = dmac_address(host);
225 len = dmac_read(host, DMAC_TXCNTHI) << 8 |
226 dmac_read(host, DMAC_TXCNTLO);
228 printk("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
229 host->host->host_no, where,
230 mode, addr, (len + 1) & 0xffff,
231 dmac_read(host, DMAC_MASKREG));
233 printk("DMA @%06x, ", host->dma.start_addr);
234 printk("BH @%p +%04x, ", host->scsi.SCp.ptr,
235 host->scsi.SCp.this_residual);
236 printk("DT @+%04x ST @+%04x", host->dma.transferred,
237 host->scsi.SCp.scsi_xferred);
243 unsigned long acornscsi_sbic_xfcount(AS_Host *host)
245 unsigned long length;
247 length = sbic_arm_read(host, SBIC_TRANSCNTH) << 16;
248 length |= sbic_arm_readnext(host) << 8;
249 length |= sbic_arm_readnext(host);
255 acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
260 asr = sbic_arm_read(host, SBIC_ASR);
262 if ((asr & stat_mask) == stat)
268 printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
274 int acornscsi_sbic_issuecmd(AS_Host *host, int command)
276 if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
279 sbic_arm_write(host, SBIC_CMND, command);
285 acornscsi_csdelay(unsigned int cs)
287 unsigned long target_jiffies, flags;
289 target_jiffies = jiffies + 1 + cs * HZ / 100;
291 local_save_flags(flags);
294 while (time_before(jiffies, target_jiffies)) barrier();
296 local_irq_restore(flags);
300 void acornscsi_resetcard(AS_Host *host)
302 unsigned int i, timeout;
304 /* assert reset line */
305 host->card.page_reg = 0x80;
306 writeb(host->card.page_reg, host->fast + PAGE_REG);
308 /* wait 3 cs. SCSI standard says 25ms. */
309 acornscsi_csdelay(3);
311 host->card.page_reg = 0;
312 writeb(host->card.page_reg, host->fast + PAGE_REG);
315 * Should get a reset from the card
319 if (readb(host->fast + INT_REG) & 8)
325 printk("scsi%d: timeout while resetting card\n",
326 host->host->host_no);
328 sbic_arm_read(host, SBIC_ASR);
329 sbic_arm_read(host, SBIC_SSR);
331 /* setup sbic - WD33C93A */
332 sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
333 sbic_arm_write(host, SBIC_CMND, CMND_RESET);
336 * Command should cause a reset interrupt
340 if (readb(host->fast + INT_REG) & 8)
346 printk("scsi%d: timeout while resetting card\n",
347 host->host->host_no);
349 sbic_arm_read(host, SBIC_ASR);
350 if (sbic_arm_read(host, SBIC_SSR) != 0x01)
351 printk(KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
352 host->host->host_no);
354 sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
355 sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
356 sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
357 sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
359 host->card.page_reg = 0x40;
360 writeb(host->card.page_reg, host->fast + PAGE_REG);
362 /* setup dmac - uPC71071 */
363 dmac_write(host, DMAC_INIT, 0);
365 dmac_write(host, DMAC_INIT, INIT_8BIT);
366 dmac_write(host, DMAC_CHANNEL, CHANNEL_0);
367 dmac_write(host, DMAC_DEVCON0, INIT_DEVCON0);
368 dmac_write(host, DMAC_DEVCON1, INIT_DEVCON1);
372 host->scsi.phase = PHASE_IDLE;
373 host->scsi.disconnectable = 0;
375 memset(host->busyluns, 0, sizeof(host->busyluns));
377 for (i = 0; i < 8; i++) {
378 host->device[i].sync_state = SYNC_NEGOCIATE;
379 host->device[i].disconnect_ok = 1;
382 /* wait 25 cs. SCSI standard says 250ms. */
383 acornscsi_csdelay(25);
386 /*=============================================================================================
387 * Utility routines (eg. debug)
389 #ifdef CONFIG_ACORNSCSI_CONSTANTS
390 static char *acornscsi_interrupttype[] = {
391 "rst", "suc", "p/a", "3",
392 "term", "5", "6", "7",
393 "serv", "9", "a", "b",
397 static signed char acornscsi_map[] = {
398 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
399 -1, 2, -1, -1, -1, -1, 3, -1, 4, 5, 6, 7, 8, 9, 10, 11,
400 12, 13, 14, -1, -1, -1, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
401 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
402 15, 16, 17, 18, 19, -1, -1, 20, 4, 5, 6, 7, 8, 9, 10, 11,
403 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
404 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
405 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
406 21, 22, -1, -1, -1, 23, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
407 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
408 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
409 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
410 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
411 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
412 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
413 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
416 static char *acornscsi_interruptcode[] = {
418 "reset - normal mode", /* 00 */
419 "reset - advanced mode", /* 01 */
434 "/ACK asserted", /* 20 */
435 "save-data-ptr", /* 21 */
440 "unexpected disconnect", /* 41 */
441 "sel timeout", /* 42 */
443 "P err+ATN", /* 44 */
444 "bad status byte", /* 47 */
447 "resel, no id", /* 80 */
453 void print_scsi_status(unsigned int ssr)
455 if (acornscsi_map[ssr] != -1)
457 acornscsi_interrupttype[(ssr >> 4)],
458 acornscsi_interruptcode[acornscsi_map[ssr]]);
460 printk("%X:%X", ssr >> 4, ssr & 0x0f);
465 void print_sbic_status(int asr, int ssr, int cmdphase)
467 #ifdef CONFIG_ACORNSCSI_CONSTANTS
468 printk("sbic: %c%c%c%c%c%c ",
469 asr & ASR_INT ? 'I' : 'i',
470 asr & ASR_LCI ? 'L' : 'l',
471 asr & ASR_BSY ? 'B' : 'b',
472 asr & ASR_CIP ? 'C' : 'c',
473 asr & ASR_PE ? 'P' : 'p',
474 asr & ASR_DBR ? 'D' : 'd');
476 print_scsi_status(ssr);
477 printk(" ph %02X\n", cmdphase);
479 printk("sbic: %02X scsi: %X:%X ph: %02X\n",
480 asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
485 acornscsi_dumplogline(AS_Host *host, int target, int line)
490 ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
492 ptr += STATUS_BUFFER_SIZE;
494 printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
495 line == 0 ? "ph" : line == 1 ? "ssr" : "int");
497 prev = host->status[target][ptr].when;
499 for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
500 unsigned long time_diff;
502 if (!host->status[target][ptr].when)
507 printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
508 host->status[target][ptr].ph);
512 printk(" %02X", host->status[target][ptr].ssr);
516 time_diff = host->status[target][ptr].when - prev;
517 prev = host->status[target][ptr].when;
520 else if (time_diff >= 100)
523 printk(" %02ld", time_diff);
532 void acornscsi_dumplog(AS_Host *host, int target)
535 acornscsi_dumplogline(host, target, 0);
536 acornscsi_dumplogline(host, target, 1);
537 acornscsi_dumplogline(host, target, 2);
547 char acornscsi_target(AS_Host *host)
550 return '0' + host->SCpnt->device->id;
555 * Prototype: cmdtype_t acornscsi_cmdtype(int command)
556 * Purpose : differentiate READ from WRITE from other commands
557 * Params : command - command to interpret
558 * Returns : CMD_READ - command reads data,
559 * CMD_WRITE - command writes data,
560 * CMD_MISC - everything else
563 cmdtype_t acornscsi_cmdtype(int command)
566 case WRITE_6: case WRITE_10: case WRITE_12:
568 case READ_6: case READ_10: case READ_12:
576 * Prototype: int acornscsi_datadirection(int command)
577 * Purpose : differentiate between commands that have a DATA IN phase
578 * and a DATA OUT phase
579 * Params : command - command to interpret
580 * Returns : DATADIR_OUT - data out phase expected
581 * DATADIR_IN - data in phase expected
584 datadir_t acornscsi_datadirection(int command)
587 case CHANGE_DEFINITION: case COMPARE: case COPY:
588 case COPY_VERIFY: case LOG_SELECT: case MODE_SELECT:
589 case MODE_SELECT_10: case SEND_DIAGNOSTIC: case WRITE_BUFFER:
590 case FORMAT_UNIT: case REASSIGN_BLOCKS: case RESERVE:
591 case SEARCH_EQUAL: case SEARCH_HIGH: case SEARCH_LOW:
592 case WRITE_6: case WRITE_10: case WRITE_VERIFY:
593 case UPDATE_BLOCK: case WRITE_LONG: case WRITE_SAME:
594 case SEARCH_HIGH_12: case SEARCH_EQUAL_12: case SEARCH_LOW_12:
595 case WRITE_12: case WRITE_VERIFY_12: case SET_WINDOW:
596 case MEDIUM_SCAN: case SEND_VOLUME_TAG: case 0xea:
604 * Purpose : provide values for synchronous transfers with 33C93.
605 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
606 * Modified by Russell King for 8MHz WD33C93A
608 static struct sync_xfer_tbl {
609 unsigned int period_ns;
610 unsigned char reg_value;
611 } sync_xfer_table[] = {
612 { 1, 0x20 }, { 249, 0x20 }, { 374, 0x30 },
613 { 499, 0x40 }, { 624, 0x50 }, { 749, 0x60 },
614 { 874, 0x70 }, { 999, 0x00 }, { 0, 0 }
618 * Prototype: int acornscsi_getperiod(unsigned char syncxfer)
619 * Purpose : period for the synchronous transfer setting
620 * Params : syncxfer SYNCXFER register value
621 * Returns : period in ns.
624 int acornscsi_getperiod(unsigned char syncxfer)
629 if (syncxfer == 0x10)
632 for (i = 1; sync_xfer_table[i].period_ns; i++)
633 if (syncxfer == sync_xfer_table[i].reg_value)
634 return sync_xfer_table[i].period_ns;
639 * Prototype: int round_period(unsigned int period)
640 * Purpose : return index into above table for a required REQ period
641 * Params : period - time (ns) for REQ
642 * Returns : table index
643 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
646 int round_period(unsigned int period)
650 for (i = 1; sync_xfer_table[i].period_ns; i++) {
651 if ((period <= sync_xfer_table[i].period_ns) &&
652 (period > sync_xfer_table[i - 1].period_ns))
659 * Prototype: unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
660 * Purpose : calculate value for 33c93s SYNC register
661 * Params : period - time (ns) for REQ
662 * offset - offset in bytes between REQ/ACK
663 * Returns : value for SYNC register
664 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
667 unsigned char __maybe_unused calc_sync_xfer(unsigned int period,
670 return sync_xfer_table[round_period(period)].reg_value |
671 ((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
674 /* ====================================================================================
678 * Function: acornscsi_kick(AS_Host *host)
679 * Purpose : kick next command to interface
680 * Params : host - host to send command to
681 * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
682 * Notes : interrupts are always disabled!
685 intr_ret_t acornscsi_kick(AS_Host *host)
688 struct scsi_cmnd *SCpnt;
690 /* first check to see if a command is waiting to be executed */
691 SCpnt = host->origSCpnt;
692 host->origSCpnt = NULL;
694 /* retrieve next command */
696 SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
703 if (host->scsi.disconnectable && host->SCpnt) {
704 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
705 host->scsi.disconnectable = 0;
706 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
707 DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
708 host->host->host_no, acornscsi_target(host)));
714 * If we have an interrupt pending, then we may have been reselected.
715 * In this case, we don't want to write to the registers
717 if (!(sbic_arm_read(host, SBIC_ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
718 sbic_arm_write(host, SBIC_DESTID, SCpnt->device->id);
719 sbic_arm_write(host, SBIC_CMND, CMND_SELWITHATN);
723 * claim host busy - all of these must happen atomically wrt
724 * our interrupt routine. Failure means command loss.
726 host->scsi.phase = PHASE_CONNECTING;
728 host->scsi.SCp = SCpnt->SCp;
729 host->dma.xfer_setup = 0;
730 host->dma.xfer_required = 0;
731 host->dma.xfer_done = 0;
733 #if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
734 DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
735 host->host->host_no, '0' + SCpnt->device->id,
740 set_bit(SCpnt->device->id * 8 +
741 (u8)(SCpnt->device->lun & 0x07), host->busyluns);
743 host->stats.removes += 1;
745 switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
747 host->stats.writes += 1;
750 host->stats.reads += 1;
753 host->stats.miscs += 1;
758 return INTR_PROCESSING;
762 * Function: void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp, unsigned int result)
763 * Purpose : complete processing for command
764 * Params : host - interface that completed
765 * result - driver byte of result
767 static void acornscsi_done(AS_Host *host, struct scsi_cmnd **SCpntp,
770 struct scsi_cmnd *SCpnt = *SCpntp;
773 sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
775 host->stats.fins += 1;
780 acornscsi_dma_cleanup(host);
782 set_host_byte(SCpnt, result);
783 if (result == DID_OK)
784 scsi_msg_to_host_byte(SCpnt, host->scsi.SCp.Message);
785 set_status_byte(SCpnt, host->scsi.SCp.Status);
788 * In theory, this should not happen. In practice, it seems to.
789 * Only trigger an error if the device attempts to report all happy
790 * but with untransferred buffers... If we don't do something, then
791 * data loss will occur. Should we check SCpnt->underflow here?
792 * It doesn't appear to be set to something meaningful by the higher
793 * levels all the time.
795 if (result == DID_OK) {
798 if (SCpnt->underflow == 0) {
799 if (host->scsi.SCp.ptr &&
800 acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
803 if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
804 host->scsi.SCp.scsi_xferred != host->dma.transferred)
808 /* ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.6)
809 * Targets which break data transfers into multiple
810 * connections shall end each successful connection
811 * (except possibly the last) with a SAVE DATA
812 * POINTER - DISCONNECT message sequence.
814 * This makes it difficult to ensure that a transfer has
815 * completed. If we reach the end of a transfer during
816 * the command, then we can only have finished the transfer.
817 * therefore, if we seem to have some data remaining, this
820 if (host->dma.xfer_done)
824 switch (get_status_byte(SCpnt)) {
825 case SAM_STAT_CHECK_CONDITION:
826 case SAM_STAT_COMMAND_TERMINATED:
828 case SAM_STAT_TASK_SET_FULL:
829 case SAM_STAT_RESERVATION_CONFLICT:
833 scmd_printk(KERN_ERR, SCpnt,
834 "incomplete data transfer detected: "
835 "result=%08X", SCpnt->result);
836 scsi_print_command(SCpnt);
837 acornscsi_dumpdma(host, "done");
838 acornscsi_dumplog(host, SCpnt->device->id);
839 set_host_byte(SCpnt, DID_ERROR);
844 if (!SCpnt->scsi_done)
845 panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
847 clear_bit(SCpnt->device->id * 8 +
848 (u8)(SCpnt->device->lun & 0x7), host->busyluns);
850 SCpnt->scsi_done(SCpnt);
852 printk("scsi%d: null command in acornscsi_done", host->host->host_no);
854 host->scsi.phase = PHASE_IDLE;
857 /* ====================================================================================
861 * Purpose : update SCSI Data Pointer
862 * Notes : this will only be one SG entry or less
865 void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
868 SCp->this_residual -= length;
870 if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
871 host->dma.xfer_done = 1;
875 * Prototype: void acornscsi_data_read(AS_Host *host, char *ptr,
876 * unsigned int start_addr, unsigned int length)
877 * Purpose : read data from DMA RAM
878 * Params : host - host to transfer from
880 * start_addr - host mem address
881 * length - number of bytes to transfer
882 * Notes : this will only be one SG entry or less
885 void acornscsi_data_read(AS_Host *host, char *ptr,
886 unsigned int start_addr, unsigned int length)
888 extern void __acornscsi_in(void __iomem *, char *buf, int len);
889 unsigned int page, offset, len = length;
891 page = (start_addr >> 12);
892 offset = start_addr & ((1 << 12) - 1);
894 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
897 unsigned int this_len;
899 if (len + offset > (1 << 12))
900 this_len = (1 << 12) - offset;
904 __acornscsi_in(host->base + (offset << 1), ptr, this_len);
910 if (offset == (1 << 12)) {
913 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
916 writeb(host->card.page_reg, host->fast + PAGE_REG);
920 * Prototype: void acornscsi_data_write(AS_Host *host, char *ptr,
921 * unsigned int start_addr, unsigned int length)
922 * Purpose : write data to DMA RAM
923 * Params : host - host to transfer from
925 * start_addr - host mem address
926 * length - number of bytes to transfer
927 * Notes : this will only be one SG entry or less
930 void acornscsi_data_write(AS_Host *host, char *ptr,
931 unsigned int start_addr, unsigned int length)
933 extern void __acornscsi_out(void __iomem *, char *buf, int len);
934 unsigned int page, offset, len = length;
936 page = (start_addr >> 12);
937 offset = start_addr & ((1 << 12) - 1);
939 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
942 unsigned int this_len;
944 if (len + offset > (1 << 12))
945 this_len = (1 << 12) - offset;
949 __acornscsi_out(host->base + (offset << 1), ptr, this_len);
955 if (offset == (1 << 12)) {
958 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
961 writeb(host->card.page_reg, host->fast + PAGE_REG);
964 /* =========================================================================================
965 * On-board DMA routines
969 * Prototype: void acornscsi_dmastop(AS_Host *host)
970 * Purpose : stop all DMA
971 * Params : host - host on which to stop DMA
972 * Notes : This is called when leaving DATA IN/OUT phase,
973 * or when interface is RESET
976 void acornscsi_dma_stop(AS_Host *host)
978 dmac_write(host, DMAC_MASKREG, MASK_ON);
979 dmac_clearintr(host);
981 #if (DEBUG & DEBUG_DMA)
982 DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
987 * Function: void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
988 * Purpose : setup DMA controller for data transfer
989 * Params : host - host to setup
990 * direction - data transfer direction
991 * Notes : This is called when entering DATA I/O phase, not
992 * while we're in a DATA I/O phase
995 void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
997 unsigned int address, length, mode;
999 host->dma.direction = direction;
1001 dmac_write(host, DMAC_MASKREG, MASK_ON);
1003 if (direction == DMA_OUT) {
1004 #if (DEBUG & DEBUG_NO_WRITE)
1005 if (NO_WRITE & (1 << host->SCpnt->device->id)) {
1006 printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
1007 host->host->host_no, acornscsi_target(host));
1016 * Allocate some buffer space, limited to half the buffer size
1018 length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1020 host->dma.start_addr = address = host->dma.free_addr;
1021 host->dma.free_addr = (host->dma.free_addr + length) &
1022 (DMAC_BUFFER_SIZE - 1);
1025 * Transfer data to DMA memory
1027 if (direction == DMA_OUT)
1028 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1032 dmac_write(host, DMAC_TXCNTLO, length);
1033 dmac_write(host, DMAC_TXCNTHI, length >> 8);
1034 dmac_write(host, DMAC_TXADRLO, address);
1035 dmac_write(host, DMAC_TXADRMD, address >> 8);
1036 dmac_write(host, DMAC_TXADRHI, 0);
1037 dmac_write(host, DMAC_MODECON, mode);
1038 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1040 #if (DEBUG & DEBUG_DMA)
1041 DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
1043 host->dma.xfer_setup = 1;
1048 * Function: void acornscsi_dma_cleanup(AS_Host *host)
1049 * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
1050 * Params : host - host to finish
1051 * Notes : This is called when a command is:
1052 * terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONNECT
1053 * : This must not return until all transfers are completed.
1056 void acornscsi_dma_cleanup(AS_Host *host)
1058 dmac_write(host, DMAC_MASKREG, MASK_ON);
1059 dmac_clearintr(host);
1062 * Check for a pending transfer
1064 if (host->dma.xfer_required) {
1065 host->dma.xfer_required = 0;
1066 if (host->dma.direction == DMA_IN)
1067 acornscsi_data_read(host, host->dma.xfer_ptr,
1068 host->dma.xfer_start, host->dma.xfer_length);
1072 * Has a transfer been setup?
1074 if (host->dma.xfer_setup) {
1075 unsigned int transferred;
1077 host->dma.xfer_setup = 0;
1079 #if (DEBUG & DEBUG_DMA)
1080 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
1084 * Calculate number of bytes transferred from DMA.
1086 transferred = dmac_address(host) - host->dma.start_addr;
1087 host->dma.transferred += transferred;
1089 if (host->dma.direction == DMA_IN)
1090 acornscsi_data_read(host, host->scsi.SCp.ptr,
1091 host->dma.start_addr, transferred);
1094 * Update SCSI pointers
1096 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1097 #if (DEBUG & DEBUG_DMA)
1098 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
1104 * Function: void acornscsi_dmacintr(AS_Host *host)
1105 * Purpose : handle interrupts from DMAC device
1106 * Params : host - host to process
1107 * Notes : If reading, we schedule the read to main memory &
1108 * allow the transfer to continue.
1109 * : If writing, we fill the onboard DMA memory from main
1111 * : Called whenever DMAC finished it's current transfer.
1114 void acornscsi_dma_intr(AS_Host *host)
1116 unsigned int address, length, transferred;
1118 #if (DEBUG & DEBUG_DMA)
1119 DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
1122 dmac_write(host, DMAC_MASKREG, MASK_ON);
1123 dmac_clearintr(host);
1126 * Calculate amount transferred via DMA
1128 transferred = dmac_address(host) - host->dma.start_addr;
1129 host->dma.transferred += transferred;
1132 * Schedule DMA transfer off board
1134 if (host->dma.direction == DMA_IN) {
1135 host->dma.xfer_start = host->dma.start_addr;
1136 host->dma.xfer_length = transferred;
1137 host->dma.xfer_ptr = host->scsi.SCp.ptr;
1138 host->dma.xfer_required = 1;
1141 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1144 * Allocate some buffer space, limited to half the on-board RAM size
1146 length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1148 host->dma.start_addr = address = host->dma.free_addr;
1149 host->dma.free_addr = (host->dma.free_addr + length) &
1150 (DMAC_BUFFER_SIZE - 1);
1153 * Transfer data to DMA memory
1155 if (host->dma.direction == DMA_OUT)
1156 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1160 dmac_write(host, DMAC_TXCNTLO, length);
1161 dmac_write(host, DMAC_TXCNTHI, length >> 8);
1162 dmac_write(host, DMAC_TXADRLO, address);
1163 dmac_write(host, DMAC_TXADRMD, address >> 8);
1164 dmac_write(host, DMAC_TXADRHI, 0);
1165 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1167 #if (DEBUG & DEBUG_DMA)
1168 DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
1171 host->dma.xfer_setup = 0;
1174 * If the interface still wants more, then this is an error.
1175 * We give it another byte, but we also attempt to raise an
1176 * attention condition. We continue giving one byte until
1177 * the device recognises the attention.
1179 if (dmac_read(host, DMAC_STATUS) & STATUS_RQ0) {
1180 acornscsi_abortcmd(host);
1182 dmac_write(host, DMAC_TXCNTLO, 0);
1183 dmac_write(host, DMAC_TXCNTHI, 0);
1184 dmac_write(host, DMAC_TXADRLO, 0);
1185 dmac_write(host, DMAC_TXADRMD, 0);
1186 dmac_write(host, DMAC_TXADRHI, 0);
1187 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1194 * Function: void acornscsi_dma_xfer(AS_Host *host)
1195 * Purpose : transfer data between AcornSCSI and memory
1196 * Params : host - host to process
1199 void acornscsi_dma_xfer(AS_Host *host)
1201 host->dma.xfer_required = 0;
1203 if (host->dma.direction == DMA_IN)
1204 acornscsi_data_read(host, host->dma.xfer_ptr,
1205 host->dma.xfer_start, host->dma.xfer_length);
1209 * Function: void acornscsi_dma_adjust(AS_Host *host)
1210 * Purpose : adjust DMA pointers & count for bytes transferred to
1211 * SBIC but not SCSI bus.
1212 * Params : host - host to adjust DMA count for
1215 void acornscsi_dma_adjust(AS_Host *host)
1217 if (host->dma.xfer_setup) {
1218 signed long transferred;
1219 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1220 DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
1223 * Calculate correct DMA address - DMA is ahead of SCSI bus while
1225 * host->scsi.SCp.scsi_xferred is the number of bytes
1226 * actually transferred to/from the SCSI bus.
1227 * host->dma.transferred is the number of bytes transferred
1228 * over DMA since host->dma.start_addr was last set.
1230 * real_dma_addr = host->dma.start_addr + host->scsi.SCp.scsi_xferred
1231 * - host->dma.transferred
1233 transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
1234 if (transferred < 0)
1235 printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1236 host->host->host_no, acornscsi_target(host), transferred);
1237 else if (transferred == 0)
1238 host->dma.xfer_setup = 0;
1240 transferred += host->dma.start_addr;
1241 dmac_write(host, DMAC_TXADRLO, transferred);
1242 dmac_write(host, DMAC_TXADRMD, transferred >> 8);
1243 dmac_write(host, DMAC_TXADRHI, transferred >> 16);
1244 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1245 DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
1252 /* =========================================================================================
1256 acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
1258 unsigned int asr, timeout = max_timeout;
1261 while (my_ptr < len) {
1262 asr = sbic_arm_read(host, SBIC_ASR);
1264 if (asr & ASR_DBR) {
1265 timeout = max_timeout;
1267 sbic_arm_write(host, SBIC_DATA, bytes[my_ptr++]);
1268 } else if (asr & ASR_INT)
1270 else if (--timeout == 0)
1277 return (timeout == 0) ? -1 : 0;
1281 * Function: void acornscsi_sendcommand(AS_Host *host)
1282 * Purpose : send a command to a target
1283 * Params : host - host which is connected to target
1286 acornscsi_sendcommand(AS_Host *host)
1288 struct scsi_cmnd *SCpnt = host->SCpnt;
1290 sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1291 sbic_arm_writenext(host, 0);
1292 sbic_arm_writenext(host, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1294 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1296 if (acornscsi_write_pio(host, SCpnt->cmnd,
1297 (int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
1298 printk("scsi%d: timeout while sending command\n", host->host->host_no);
1300 host->scsi.phase = PHASE_COMMAND;
1304 void acornscsi_sendmessage(AS_Host *host)
1306 unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
1308 struct message *msg;
1310 #if (DEBUG & DEBUG_MESSAGES)
1311 printk("scsi%d.%c: sending message ",
1312 host->host->host_no, acornscsi_target(host));
1315 switch (message_length) {
1317 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1319 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
1321 sbic_arm_write(host, SBIC_DATA, NOP);
1323 host->scsi.last_message = NOP;
1324 #if (DEBUG & DEBUG_MESSAGES)
1330 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1331 msg = msgqueue_getmsg(&host->scsi.msgs, 0);
1333 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
1335 sbic_arm_write(host, SBIC_DATA, msg->msg[0]);
1337 host->scsi.last_message = msg->msg[0];
1338 #if (DEBUG & DEBUG_MESSAGES)
1339 spi_print_msg(msg->msg);
1345 * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
1346 * 'When a target sends this (MESSAGE_REJECT) message, it
1347 * shall change to MESSAGE IN phase and send this message
1348 * prior to requesting additional message bytes from the
1349 * initiator. This provides an interlock so that the
1350 * initiator can determine which message byte is rejected.
1352 sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1353 sbic_arm_writenext(host, 0);
1354 sbic_arm_writenext(host, message_length);
1355 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1358 while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
1360 #if (DEBUG & DEBUG_MESSAGES)
1364 if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
1365 printk("scsi%d: timeout while sending message\n", host->host->host_no);
1367 host->scsi.last_message = msg->msg[0];
1368 if (msg->msg[0] == EXTENDED_MESSAGE)
1369 host->scsi.last_message |= msg->msg[2] << 8;
1371 if (i != msg->length)
1376 #if (DEBUG & DEBUG_MESSAGES)
1382 * Function: void acornscsi_readstatusbyte(AS_Host *host)
1383 * Purpose : Read status byte from connected target
1384 * Params : host - host connected to target
1387 void acornscsi_readstatusbyte(AS_Host *host)
1389 acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
1390 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
1391 host->scsi.SCp.Status = sbic_arm_read(host, SBIC_DATA);
1395 * Function: unsigned char acornscsi_readmessagebyte(AS_Host *host)
1396 * Purpose : Read one message byte from connected target
1397 * Params : host - host connected to target
1400 unsigned char acornscsi_readmessagebyte(AS_Host *host)
1402 unsigned char message;
1404 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1406 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
1408 message = sbic_arm_read(host, SBIC_DATA);
1410 /* wait for MSGIN-XFER-PAUSED */
1411 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
1413 sbic_arm_read(host, SBIC_SSR);
1419 * Function: void acornscsi_message(AS_Host *host)
1420 * Purpose : Read complete message from connected target & action message
1421 * Params : host - host connected to target
1424 void acornscsi_message(AS_Host *host)
1426 unsigned char message[16];
1427 unsigned int msgidx = 0, msglen = 1;
1430 message[msgidx] = acornscsi_readmessagebyte(host);
1434 if (message[0] == EXTENDED_MESSAGE ||
1435 (message[0] >= 0x20 && message[0] <= 0x2f))
1440 if (message[0] == EXTENDED_MESSAGE)
1441 msglen += message[msgidx];
1445 if (msgidx < msglen) {
1446 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1448 /* wait for next msg-in */
1449 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
1450 sbic_arm_read(host, SBIC_SSR);
1452 } while (msgidx < msglen);
1454 #if (DEBUG & DEBUG_MESSAGES)
1455 printk("scsi%d.%c: message in: ",
1456 host->host->host_no, acornscsi_target(host));
1457 spi_print_msg(message);
1461 if (host->scsi.phase == PHASE_RECONNECTED) {
1463 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1464 * 'Whenever a target reconnects to an initiator to continue
1465 * a tagged I/O process, the SIMPLE QUEUE TAG message shall
1466 * be sent immediately following the IDENTIFY message...'
1468 if (message[0] == SIMPLE_QUEUE_TAG)
1469 host->scsi.reconnected.tag = message[1];
1470 if (acornscsi_reconnect_finish(host))
1471 host->scsi.phase = PHASE_MSGIN;
1474 switch (message[0]) {
1475 case ABORT_TASK_SET:
1477 case COMMAND_COMPLETE:
1478 if (host->scsi.phase != PHASE_STATUSIN) {
1479 printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1480 host->host->host_no, acornscsi_target(host));
1481 acornscsi_dumplog(host, host->SCpnt->device->id);
1483 host->scsi.phase = PHASE_DONE;
1484 host->scsi.SCp.Message = message[0];
1489 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
1490 * 'The SAVE DATA POINTER message is sent from a target to
1491 * direct the initiator to copy the active data pointer to
1492 * the saved data pointer for the current I/O process.
1494 acornscsi_dma_cleanup(host);
1495 host->SCpnt->SCp = host->scsi.SCp;
1496 host->SCpnt->SCp.sent_command = 0;
1497 host->scsi.phase = PHASE_MSGIN;
1500 case RESTORE_POINTERS:
1502 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
1503 * 'The RESTORE POINTERS message is sent from a target to
1504 * direct the initiator to copy the most recently saved
1505 * command, data, and status pointers for the I/O process
1506 * to the corresponding active pointers. The command and
1507 * status pointers shall be restored to the beginning of
1508 * the present command and status areas.'
1510 acornscsi_dma_cleanup(host);
1511 host->scsi.SCp = host->SCpnt->SCp;
1512 host->scsi.phase = PHASE_MSGIN;
1517 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
1518 * 'On those occasions when an error or exception condition occurs
1519 * and the target elects to repeat the information transfer, the
1520 * target may repeat the transfer either issuing a RESTORE POINTERS
1521 * message or by disconnecting without issuing a SAVE POINTERS
1522 * message. When reconnection is completed, the most recent
1523 * saved pointer values are restored.'
1525 acornscsi_dma_cleanup(host);
1526 host->scsi.phase = PHASE_DISCONNECT;
1529 case MESSAGE_REJECT:
1530 #if 0 /* this isn't needed any more */
1532 * If we were negociating sync transfer, we don't yet know if
1533 * this REJECT is for the sync transfer or for the tagged queue/wide
1534 * transfer. Re-initiate sync transfer negotiation now, and if
1535 * we got a REJECT in response to SDTR, then it'll be set to DONE.
1537 if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST)
1538 host->device[host->SCpnt->device->id].sync_state = SYNC_NEGOCIATE;
1542 * If we have any messages waiting to go out, then assert ATN now
1544 if (msgqueue_msglength(&host->scsi.msgs))
1545 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1547 switch (host->scsi.last_message) {
1548 case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1550 * Target can't handle synchronous transfers
1552 printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1553 host->host->host_no, acornscsi_target(host));
1554 host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
1555 host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
1556 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1564 case SIMPLE_QUEUE_TAG:
1565 /* tag queue reconnect... message[1] = queue tag. Print something to indicate something happened! */
1566 printk("scsi%d.%c: reconnect queue tag %02X\n",
1567 host->host->host_no, acornscsi_target(host),
1571 case EXTENDED_MESSAGE:
1572 switch (message[2]) {
1573 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1575 if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
1577 * We requested synchronous transfers. This isn't quite right...
1578 * We can only say if this succeeded if we proceed on to execute the
1579 * command from this message. If we get a MESSAGE PARITY ERROR,
1580 * and the target retries fail, then we fallback to asynchronous mode
1582 host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
1583 printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1584 host->host->host_no, acornscsi_target(host),
1585 message[4], message[3] * 4);
1586 host->device[host->SCpnt->device->id].sync_xfer =
1587 calc_sync_xfer(message[3] * 4, message[4]);
1589 unsigned char period, length;
1591 * Target requested synchronous transfers. The agreement is only
1592 * to be in operation AFTER the target leaves message out phase.
1594 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1595 period = max_t(unsigned int, message[3], sdtr_period / 4);
1596 length = min_t(unsigned int, message[4], sdtr_size);
1597 msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1598 EXTENDED_SDTR, period, length);
1599 host->device[host->SCpnt->device->id].sync_xfer =
1600 calc_sync_xfer(period * 4, length);
1602 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1605 /* We do not accept synchronous transfers. Respond with a
1611 /* The WD33C93A is only 8-bit. We respond with a MESSAGE_REJECT
1612 * to a wide data transfer request.
1615 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1616 msgqueue_flush(&host->scsi.msgs);
1617 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1622 default: /* reject message */
1623 printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1624 host->host->host_no, acornscsi_target(host),
1626 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1627 msgqueue_flush(&host->scsi.msgs);
1628 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1629 host->scsi.phase = PHASE_MSGIN;
1632 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1636 * Function: int acornscsi_buildmessages(AS_Host *host)
1637 * Purpose : build the connection messages for a host
1638 * Params : host - host to add messages to
1641 void acornscsi_buildmessages(AS_Host *host)
1644 /* does the device need resetting? */
1646 msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
1651 msgqueue_addmsg(&host->scsi.msgs, 1,
1652 IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
1653 host->SCpnt->device->lun));
1656 /* does the device need the current command aborted */
1658 acornscsi_abortcmd(host);
1664 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1665 if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
1666 host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
1667 msgqueue_addmsg(&host->scsi.msgs, 5,
1668 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1669 sdtr_period / 4, sdtr_size);
1675 * Function: int acornscsi_starttransfer(AS_Host *host)
1676 * Purpose : transfer data to/from connected target
1677 * Params : host - host to which target is connected
1678 * Returns : 0 if failure
1681 int acornscsi_starttransfer(AS_Host *host)
1685 if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
1686 printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1687 host->host->host_no, acornscsi_target(host));
1691 residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
1693 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1694 sbic_arm_writenext(host, residual >> 16);
1695 sbic_arm_writenext(host, residual >> 8);
1696 sbic_arm_writenext(host, residual);
1697 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1701 /* =========================================================================================
1702 * Connection & Disconnection
1705 * Function : acornscsi_reconnect(AS_Host *host)
1706 * Purpose : reconnect a previously disconnected command
1707 * Params : host - host specific data
1708 * Remarks : SCSI spec says:
1709 * 'The set of active pointers is restored from the set
1710 * of saved pointers upon reconnection of the I/O process'
1713 int acornscsi_reconnect(AS_Host *host)
1715 unsigned int target, lun, ok = 0;
1717 target = sbic_arm_read(host, SBIC_SOURCEID);
1720 printk(KERN_ERR "scsi%d: invalid source id after reselection "
1721 "- device fault?\n",
1722 host->host->host_no);
1726 if (host->SCpnt && !host->scsi.disconnectable) {
1727 printk(KERN_ERR "scsi%d.%d: reconnected while command in "
1728 "progress to target %d?\n",
1729 host->host->host_no, target, host->SCpnt->device->id);
1733 lun = sbic_arm_read(host, SBIC_DATA) & 7;
1735 host->scsi.reconnected.target = target;
1736 host->scsi.reconnected.lun = lun;
1737 host->scsi.reconnected.tag = 0;
1739 if (host->scsi.disconnectable && host->SCpnt &&
1740 host->SCpnt->device->id == target && host->SCpnt->device->lun == lun)
1743 if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
1746 ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1749 host->scsi.phase = PHASE_RECONNECTED;
1751 /* this doesn't seem to work */
1752 printk(KERN_ERR "scsi%d.%c: reselected with no command "
1753 "to reconnect with\n",
1754 host->host->host_no, '0' + target);
1755 acornscsi_dumplog(host, target);
1756 acornscsi_abortcmd(host);
1758 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1762 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1767 * Function: int acornscsi_reconnect_finish(AS_Host *host)
1768 * Purpose : finish reconnecting a command
1769 * Params : host - host to complete
1770 * Returns : 0 if failed
1773 int acornscsi_reconnect_finish(AS_Host *host)
1775 if (host->scsi.disconnectable && host->SCpnt) {
1776 host->scsi.disconnectable = 0;
1777 if (host->SCpnt->device->id == host->scsi.reconnected.target &&
1778 host->SCpnt->device->lun == host->scsi.reconnected.lun &&
1779 scsi_cmd_to_rq(host->SCpnt)->tag == host->scsi.reconnected.tag) {
1780 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1781 DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
1782 host->host->host_no, acornscsi_target(host)));
1785 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1786 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1787 DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
1788 "to disconnected queue\n",
1789 host->host->host_no, acornscsi_target(host)));
1795 host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
1796 host->scsi.reconnected.target,
1797 host->scsi.reconnected.lun,
1798 host->scsi.reconnected.tag);
1799 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1800 DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
1801 host->host->host_no, acornscsi_target(host)));
1806 acornscsi_abortcmd(host);
1809 * Restore data pointer from SAVED pointers.
1811 host->scsi.SCp = host->SCpnt->SCp;
1812 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1813 printk(", data pointers: [%p, %X]",
1814 host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1817 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1821 host->dma.transferred = host->scsi.SCp.scsi_xferred;
1823 return host->SCpnt != NULL;
1827 * Function: void acornscsi_disconnect_unexpected(AS_Host *host)
1828 * Purpose : handle an unexpected disconnect
1829 * Params : host - host on which disconnect occurred
1832 void acornscsi_disconnect_unexpected(AS_Host *host)
1834 printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1835 host->host->host_no, acornscsi_target(host));
1836 #if (DEBUG & DEBUG_ABORT)
1837 acornscsi_dumplog(host, 8);
1840 acornscsi_done(host, &host->SCpnt, DID_ERROR);
1844 * Function: void acornscsi_abortcmd(AS_host *host, unsigned char tag)
1845 * Purpose : abort a currently executing command
1846 * Params : host - host with connected command to abort
1849 void acornscsi_abortcmd(AS_Host *host)
1851 host->scsi.phase = PHASE_ABORTED;
1852 sbic_arm_write(host, SBIC_CMND, CMND_ASSERTATN);
1854 msgqueue_flush(&host->scsi.msgs);
1855 msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1858 /* ==========================================================================================
1859 * Interrupt routines.
1862 * Function: int acornscsi_sbicintr(AS_Host *host)
1863 * Purpose : handle interrupts from SCSI device
1864 * Params : host - host to process
1865 * Returns : INTR_PROCESS if expecting another SBIC interrupt
1866 * INTR_IDLE if no interrupt
1867 * INTR_NEXT_COMMAND if we have finished processing the command
1870 intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
1872 unsigned int asr, ssr;
1874 asr = sbic_arm_read(host, SBIC_ASR);
1875 if (!(asr & ASR_INT))
1878 ssr = sbic_arm_read(host, SBIC_SSR);
1880 #if (DEBUG & DEBUG_PHASES)
1881 print_sbic_status(asr, ssr, host->scsi.phase);
1884 ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
1886 if (host->SCpnt && !host->scsi.disconnectable)
1887 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
1890 case 0x00: /* reset state - not advanced */
1891 printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
1892 host->host->host_no);
1893 /* setup sbic - WD33C93A */
1894 sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
1895 sbic_arm_write(host, SBIC_CMND, CMND_RESET);
1898 case 0x01: /* reset state - advanced */
1899 sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
1900 sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
1901 sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
1902 sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
1903 msgqueue_flush(&host->scsi.msgs);
1906 case 0x41: /* unexpected disconnect aborted command */
1907 acornscsi_disconnect_unexpected(host);
1908 return INTR_NEXT_COMMAND;
1911 switch (host->scsi.phase) {
1912 case PHASE_CONNECTING: /* STATE: command removed from issue queue */
1914 case 0x11: /* -> PHASE_CONNECTED */
1915 /* BUS FREE -> SELECTION */
1916 host->scsi.phase = PHASE_CONNECTED;
1917 msgqueue_flush(&host->scsi.msgs);
1918 host->dma.transferred = host->scsi.SCp.scsi_xferred;
1919 /* 33C93 gives next interrupt indicating bus phase */
1920 asr = sbic_arm_read(host, SBIC_ASR);
1921 if (!(asr & ASR_INT))
1923 ssr = sbic_arm_read(host, SBIC_SSR);
1924 ADD_STATUS(8, ssr, host->scsi.phase, 1);
1925 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, 1);
1928 case 0x42: /* select timed out */
1930 acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
1931 return INTR_NEXT_COMMAND;
1933 case 0x81: /* -> PHASE_RECONNECTED or PHASE_ABORTED */
1934 /* BUS FREE -> RESELECTION */
1935 host->origSCpnt = host->SCpnt;
1937 msgqueue_flush(&host->scsi.msgs);
1938 acornscsi_reconnect(host);
1942 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
1943 host->host->host_no, acornscsi_target(host), ssr);
1944 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
1945 acornscsi_abortcmd(host);
1947 return INTR_PROCESSING;
1950 case PHASE_CONNECTED: /* STATE: device selected ok */
1953 case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
1954 /* SELECTION -> COMMAND */
1955 acornscsi_sendcommand(host);
1958 case 0x8b: /* -> PHASE_STATUS */
1959 /* SELECTION -> STATUS */
1960 acornscsi_readstatusbyte(host);
1961 host->scsi.phase = PHASE_STATUSIN;
1965 case 0x8e: /* -> PHASE_MSGOUT */
1966 /* SELECTION ->MESSAGE OUT */
1967 host->scsi.phase = PHASE_MSGOUT;
1968 acornscsi_buildmessages(host);
1969 acornscsi_sendmessage(host);
1972 /* these should not happen */
1973 case 0x85: /* target disconnected */
1974 acornscsi_done(host, &host->SCpnt, DID_ERROR);
1978 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
1979 host->host->host_no, acornscsi_target(host), ssr);
1980 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
1981 acornscsi_abortcmd(host);
1983 return INTR_PROCESSING;
1985 case PHASE_MSGOUT: /* STATE: connected & sent IDENTIFY message */
1987 * SCSI standard says that MESSAGE OUT phases can be followed by a
1988 * DATA phase, STATUS phase, MESSAGE IN phase or COMMAND phase
1991 case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
1992 case 0x1a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
1993 /* MESSAGE OUT -> COMMAND */
1994 acornscsi_sendcommand(host);
1997 case 0x8b: /* -> PHASE_STATUS */
1998 case 0x1b: /* -> PHASE_STATUS */
1999 /* MESSAGE OUT -> STATUS */
2000 acornscsi_readstatusbyte(host);
2001 host->scsi.phase = PHASE_STATUSIN;
2004 case 0x8e: /* -> PHASE_MSGOUT */
2005 /* MESSAGE_OUT(MESSAGE_IN) ->MESSAGE OUT */
2006 acornscsi_sendmessage(host);
2009 case 0x4f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2010 case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2011 /* MESSAGE OUT -> MESSAGE IN */
2012 acornscsi_message(host);
2016 printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
2017 host->host->host_no, acornscsi_target(host), ssr);
2018 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2020 return INTR_PROCESSING;
2022 case PHASE_COMMAND: /* STATE: connected & command sent */
2024 case 0x18: /* -> PHASE_DATAOUT */
2025 /* COMMAND -> DATA OUT */
2026 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2027 acornscsi_abortcmd(host);
2028 acornscsi_dma_setup(host, DMA_OUT);
2029 if (!acornscsi_starttransfer(host))
2030 acornscsi_abortcmd(host);
2031 host->scsi.phase = PHASE_DATAOUT;
2034 case 0x19: /* -> PHASE_DATAIN */
2035 /* COMMAND -> DATA IN */
2036 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2037 acornscsi_abortcmd(host);
2038 acornscsi_dma_setup(host, DMA_IN);
2039 if (!acornscsi_starttransfer(host))
2040 acornscsi_abortcmd(host);
2041 host->scsi.phase = PHASE_DATAIN;
2044 case 0x1b: /* -> PHASE_STATUS */
2045 /* COMMAND -> STATUS */
2046 acornscsi_readstatusbyte(host);
2047 host->scsi.phase = PHASE_STATUSIN;
2050 case 0x1e: /* -> PHASE_MSGOUT */
2051 /* COMMAND -> MESSAGE OUT */
2052 acornscsi_sendmessage(host);
2055 case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2056 /* COMMAND -> MESSAGE IN */
2057 acornscsi_message(host);
2061 printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2062 host->host->host_no, acornscsi_target(host), ssr);
2063 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2065 return INTR_PROCESSING;
2067 case PHASE_DISCONNECT: /* STATE: connected, received DISCONNECT msg */
2068 if (ssr == 0x85) { /* -> PHASE_IDLE */
2069 host->scsi.disconnectable = 1;
2070 host->scsi.reconnected.tag = 0;
2071 host->scsi.phase = PHASE_IDLE;
2072 host->stats.disconnects += 1;
2074 printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2075 host->host->host_no, acornscsi_target(host), ssr);
2076 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2078 return INTR_NEXT_COMMAND;
2080 case PHASE_IDLE: /* STATE: disconnected */
2081 if (ssr == 0x81) /* -> PHASE_RECONNECTED or PHASE_ABORTED */
2082 acornscsi_reconnect(host);
2084 printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2085 host->host->host_no, acornscsi_target(host), ssr);
2086 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2088 return INTR_PROCESSING;
2090 case PHASE_RECONNECTED: /* STATE: device reconnected to initiator */
2092 * Command reconnected - if MESGIN, get message - it may be
2093 * the tag. If not, get command out of disconnected queue
2096 * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
2097 * reconnect I_T_L command
2099 if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
2101 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
2103 case 0x88: /* data out phase */
2104 /* -> PHASE_DATAOUT */
2105 /* MESSAGE IN -> DATA OUT */
2106 acornscsi_dma_setup(host, DMA_OUT);
2107 if (!acornscsi_starttransfer(host))
2108 acornscsi_abortcmd(host);
2109 host->scsi.phase = PHASE_DATAOUT;
2112 case 0x89: /* data in phase */
2113 /* -> PHASE_DATAIN */
2114 /* MESSAGE IN -> DATA IN */
2115 acornscsi_dma_setup(host, DMA_IN);
2116 if (!acornscsi_starttransfer(host))
2117 acornscsi_abortcmd(host);
2118 host->scsi.phase = PHASE_DATAIN;
2121 case 0x8a: /* command out */
2122 /* MESSAGE IN -> COMMAND */
2123 acornscsi_sendcommand(host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2126 case 0x8b: /* status in */
2127 /* -> PHASE_STATUSIN */
2128 /* MESSAGE IN -> STATUS */
2129 acornscsi_readstatusbyte(host);
2130 host->scsi.phase = PHASE_STATUSIN;
2133 case 0x8e: /* message out */
2134 /* -> PHASE_MSGOUT */
2135 /* MESSAGE IN -> MESSAGE OUT */
2136 acornscsi_sendmessage(host);
2139 case 0x8f: /* message in */
2140 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2144 printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2145 host->host->host_no, acornscsi_target(host), ssr);
2146 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2148 return INTR_PROCESSING;
2150 case PHASE_DATAIN: /* STATE: transferred data in */
2152 * This is simple - if we disconnect then the DMA address & count is
2156 case 0x19: /* -> PHASE_DATAIN */
2157 case 0x89: /* -> PHASE_DATAIN */
2158 acornscsi_abortcmd(host);
2161 case 0x1b: /* -> PHASE_STATUSIN */
2162 case 0x4b: /* -> PHASE_STATUSIN */
2163 case 0x8b: /* -> PHASE_STATUSIN */
2164 /* DATA IN -> STATUS */
2165 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2166 acornscsi_sbic_xfcount(host);
2167 acornscsi_dma_stop(host);
2168 acornscsi_readstatusbyte(host);
2169 host->scsi.phase = PHASE_STATUSIN;
2172 case 0x1e: /* -> PHASE_MSGOUT */
2173 case 0x4e: /* -> PHASE_MSGOUT */
2174 case 0x8e: /* -> PHASE_MSGOUT */
2175 /* DATA IN -> MESSAGE OUT */
2176 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2177 acornscsi_sbic_xfcount(host);
2178 acornscsi_dma_stop(host);
2179 acornscsi_sendmessage(host);
2182 case 0x1f: /* message in */
2183 case 0x4f: /* message in */
2184 case 0x8f: /* message in */
2185 /* DATA IN -> MESSAGE IN */
2186 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2187 acornscsi_sbic_xfcount(host);
2188 acornscsi_dma_stop(host);
2189 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2193 printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2194 host->host->host_no, acornscsi_target(host), ssr);
2195 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2197 return INTR_PROCESSING;
2199 case PHASE_DATAOUT: /* STATE: transferred data out */
2201 * This is more complicated - if we disconnect, the DMA could be 12
2202 * bytes ahead of us. We need to correct this.
2205 case 0x18: /* -> PHASE_DATAOUT */
2206 case 0x88: /* -> PHASE_DATAOUT */
2207 acornscsi_abortcmd(host);
2210 case 0x1b: /* -> PHASE_STATUSIN */
2211 case 0x4b: /* -> PHASE_STATUSIN */
2212 case 0x8b: /* -> PHASE_STATUSIN */
2213 /* DATA OUT -> STATUS */
2214 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2215 acornscsi_sbic_xfcount(host);
2216 acornscsi_dma_stop(host);
2217 acornscsi_dma_adjust(host);
2218 acornscsi_readstatusbyte(host);
2219 host->scsi.phase = PHASE_STATUSIN;
2222 case 0x1e: /* -> PHASE_MSGOUT */
2223 case 0x4e: /* -> PHASE_MSGOUT */
2224 case 0x8e: /* -> PHASE_MSGOUT */
2225 /* DATA OUT -> MESSAGE OUT */
2226 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2227 acornscsi_sbic_xfcount(host);
2228 acornscsi_dma_stop(host);
2229 acornscsi_dma_adjust(host);
2230 acornscsi_sendmessage(host);
2233 case 0x1f: /* message in */
2234 case 0x4f: /* message in */
2235 case 0x8f: /* message in */
2236 /* DATA OUT -> MESSAGE IN */
2237 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2238 acornscsi_sbic_xfcount(host);
2239 acornscsi_dma_stop(host);
2240 acornscsi_dma_adjust(host);
2241 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2245 printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2246 host->host->host_no, acornscsi_target(host), ssr);
2247 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2249 return INTR_PROCESSING;
2251 case PHASE_STATUSIN: /* STATE: status in complete */
2253 case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2254 case 0x8f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2255 /* STATUS -> MESSAGE IN */
2256 acornscsi_message(host);
2259 case 0x1e: /* -> PHASE_MSGOUT */
2260 case 0x8e: /* -> PHASE_MSGOUT */
2261 /* STATUS -> MESSAGE OUT */
2262 acornscsi_sendmessage(host);
2266 printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2267 host->host->host_no, acornscsi_target(host), ssr);
2268 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2270 return INTR_PROCESSING;
2272 case PHASE_MSGIN: /* STATE: message in */
2274 case 0x1e: /* -> PHASE_MSGOUT */
2275 case 0x4e: /* -> PHASE_MSGOUT */
2276 case 0x8e: /* -> PHASE_MSGOUT */
2277 /* MESSAGE IN -> MESSAGE OUT */
2278 acornscsi_sendmessage(host);
2281 case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2285 acornscsi_message(host);
2289 printk("scsi%d.%c: strange message in disconnection\n",
2290 host->host->host_no, acornscsi_target(host));
2291 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2292 acornscsi_done(host, &host->SCpnt, DID_ERROR);
2296 printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2297 host->host->host_no, acornscsi_target(host), ssr);
2298 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2300 return INTR_PROCESSING;
2302 case PHASE_DONE: /* STATE: received status & message */
2304 case 0x85: /* -> PHASE_IDLE */
2305 acornscsi_done(host, &host->SCpnt, DID_OK);
2306 return INTR_NEXT_COMMAND;
2310 acornscsi_sendmessage(host);
2314 printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2315 host->host->host_no, acornscsi_target(host), ssr);
2316 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2318 return INTR_PROCESSING;
2324 acornscsi_done(host, &host->SCpnt, DID_ABORT);
2326 clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
2328 host->scsi.phase = PHASE_IDLE;
2330 return INTR_NEXT_COMMAND;
2336 acornscsi_sendmessage(host);
2340 printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2341 host->host->host_no, acornscsi_target(host), ssr);
2342 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2344 return INTR_PROCESSING;
2347 printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2348 host->host->host_no, acornscsi_target(host), ssr);
2349 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2351 return INTR_PROCESSING;
2355 * Prototype: void acornscsi_intr(int irq, void *dev_id)
2356 * Purpose : handle interrupts from Acorn SCSI card
2357 * Params : irq - interrupt number
2358 * dev_id - device specific data (AS_Host structure)
2361 acornscsi_intr(int irq, void *dev_id)
2363 AS_Host *host = (AS_Host *)dev_id;
2371 iostatus = readb(host->fast + INT_REG);
2374 acornscsi_dma_intr(host);
2375 iostatus = readb(host->fast + INT_REG);
2379 ret = acornscsi_sbicintr(host, in_irq);
2382 * If we have a transfer pending, start it.
2383 * Only start it if the interface has already started transferring
2386 if (host->dma.xfer_required)
2387 acornscsi_dma_xfer(host);
2389 if (ret == INTR_NEXT_COMMAND)
2390 ret = acornscsi_kick(host);
2393 } while (ret != INTR_IDLE);
2398 /*=============================================================================================
2399 * Interfaces between interrupt handler and rest of scsi code
2403 * Function : acornscsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2404 * Purpose : queues a SCSI command
2405 * Params : cmd - SCSI command
2406 * done - function called on completion, with pointer to command descriptor
2407 * Returns : 0, or < 0 on error.
2409 static int acornscsi_queuecmd_lck(struct scsi_cmnd *SCpnt,
2410 void (*done)(struct scsi_cmnd *))
2412 AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2415 /* there should be some way of rejecting errors like this without panicing... */
2416 panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
2417 host->host->host_no, SCpnt);
2421 #if (DEBUG & DEBUG_NO_WRITE)
2422 if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
2423 printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2424 host->host->host_no, '0' + SCpnt->device->id);
2425 set_host_byte(SCpnt, DID_NO_CONNECT);
2431 SCpnt->scsi_done = done;
2432 SCpnt->host_scribble = NULL;
2434 SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
2435 SCpnt->SCp.sent_command = 0;
2436 SCpnt->SCp.scsi_xferred = 0;
2440 host->stats.queues += 1;
2443 unsigned long flags;
2445 if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
2446 set_host_byte(SCpnt, DID_ERROR);
2450 local_irq_save(flags);
2451 if (host->scsi.phase == PHASE_IDLE)
2452 acornscsi_kick(host);
2453 local_irq_restore(flags);
2458 DEF_SCSI_QCMD(acornscsi_queuecmd)
2460 enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
2463 * Prototype: enum res acornscsi_do_abort(struct scsi_cmnd *SCpnt)
2464 * Purpose : abort a command on this host
2465 * Params : SCpnt - command to abort
2466 * Returns : our abort status
2468 static enum res_abort acornscsi_do_abort(AS_Host *host, struct scsi_cmnd *SCpnt)
2470 enum res_abort res = res_not_running;
2472 if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
2474 * The command was on the issue queue, and has not been
2475 * issued yet. We can remove the command from the queue,
2476 * and acknowledge the abort. Neither the devices nor the
2477 * interface know about the command.
2479 //#if (DEBUG & DEBUG_ABORT)
2480 printk("on issue queue ");
2483 } else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
2485 * The command was on the disconnected queue. Simply
2486 * acknowledge the abort condition, and when the target
2487 * reconnects, we will give it an ABORT message. The
2488 * target should then disconnect, and we will clear
2491 //#if (DEBUG & DEBUG_ABORT)
2492 printk("on disconnected queue ");
2495 } else if (host->SCpnt == SCpnt) {
2496 unsigned long flags;
2498 //#if (DEBUG & DEBUG_ABORT)
2499 printk("executing ");
2502 local_irq_save(flags);
2503 switch (host->scsi.phase) {
2505 * If the interface is idle, and the command is 'disconnectable',
2506 * then it is the same as on the disconnected queue. We simply
2507 * remove all traces of the command. When the target reconnects,
2508 * we will give it an ABORT message since the command could not
2509 * be found. When the target finally disconnects, we will clear
2513 if (host->scsi.disconnectable) {
2514 host->scsi.disconnectable = 0;
2521 * If the command has connected and done nothing further,
2522 * simply force a disconnect. We also need to clear the
2525 case PHASE_CONNECTED:
2526 sbic_arm_write(host, SBIC_CMND, CMND_DISCONNECT);
2528 res = res_success_clear;
2532 acornscsi_abortcmd(host);
2535 local_irq_restore(flags);
2536 } else if (host->origSCpnt == SCpnt) {
2538 * The command will be executed next, but a command
2539 * is currently using the interface. This is similar to
2540 * being on the issue queue, except the busylun bit has
2543 host->origSCpnt = NULL;
2544 //#if (DEBUG & DEBUG_ABORT)
2545 printk("waiting for execution ");
2547 res = res_success_clear;
2555 * Prototype: int acornscsi_abort(struct scsi_cmnd *SCpnt)
2556 * Purpose : abort a command on this host
2557 * Params : SCpnt - command to abort
2558 * Returns : one of SCSI_ABORT_ macros
2560 int acornscsi_abort(struct scsi_cmnd *SCpnt)
2562 AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
2565 host->stats.aborts += 1;
2567 #if (DEBUG & DEBUG_ABORT)
2570 asr = sbic_arm_read(host, SBIC_ASR);
2571 ssr = sbic_arm_read(host, SBIC_SSR);
2573 printk(KERN_WARNING "acornscsi_abort: ");
2574 print_sbic_status(asr, ssr, host->scsi.phase);
2575 acornscsi_dumplog(host, SCpnt->device->id);
2579 printk("scsi%d: ", host->host->host_no);
2581 switch (acornscsi_do_abort(host, SCpnt)) {
2583 * We managed to find the command and cleared it out.
2584 * We do not expect the command to be executing on the
2585 * target, but we have set the busylun bit.
2587 case res_success_clear:
2588 //#if (DEBUG & DEBUG_ABORT)
2591 clear_bit(SCpnt->device->id * 8 +
2592 (u8)(SCpnt->device->lun & 0x7), host->busyluns);
2596 * We found the command, and cleared it out. Either
2597 * the command is still known to be executing on the
2598 * target, or the busylun bit is not set.
2601 //#if (DEBUG & DEBUG_ABORT)
2602 printk("success\n");
2608 * We did find the command, but unfortunately we couldn't
2609 * unhook it from ourselves. Wait some more, and if it
2610 * still doesn't complete, reset the interface.
2613 //#if (DEBUG & DEBUG_ABORT)
2620 * The command could not be found (either because it completed,
2621 * or it got dropped.
2624 case res_not_running:
2625 acornscsi_dumplog(host, SCpnt->device->id);
2627 //#if (DEBUG & DEBUG_ABORT)
2628 printk("not running\n");
2637 * Prototype: int acornscsi_reset(struct scsi_cmnd *SCpnt)
2638 * Purpose : reset a command on this host/reset this host
2639 * Params : SCpnt - command causing reset
2640 * Returns : one of SCSI_RESET_ macros
2642 int acornscsi_host_reset(struct scsi_cmnd *SCpnt)
2644 AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2645 struct scsi_cmnd *SCptr;
2647 host->stats.resets += 1;
2649 #if (DEBUG & DEBUG_RESET)
2651 int asr, ssr, devidx;
2653 asr = sbic_arm_read(host, SBIC_ASR);
2654 ssr = sbic_arm_read(host, SBIC_SSR);
2656 printk(KERN_WARNING "acornscsi_reset: ");
2657 print_sbic_status(asr, ssr, host->scsi.phase);
2658 for (devidx = 0; devidx < 9; devidx++)
2659 acornscsi_dumplog(host, devidx);
2663 acornscsi_dma_stop(host);
2666 * do hard reset. This resets all devices on this host, and so we
2667 * must set the reset status on all commands.
2669 acornscsi_resetcard(host);
2671 while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
2677 /*==============================================================================================
2678 * initialisation & miscellaneous support
2682 * Function: char *acornscsi_info(struct Scsi_Host *host)
2683 * Purpose : return a string describing this interface
2684 * Params : host - host to give information on
2685 * Returns : a constant string
2688 char *acornscsi_info(struct Scsi_Host *host)
2690 static char string[100], *p;
2694 p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
2695 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2698 #if (DEBUG & DEBUG_NO_WRITE)
2699 " NOWRITE (" __stringify(NO_WRITE) ")"
2701 , host->hostt->name, host->io_port, host->irq,
2702 VER_MAJOR, VER_MINOR, VER_PATCH);
2706 static int acornscsi_show_info(struct seq_file *m, struct Scsi_Host *instance)
2709 struct scsi_device *scd;
2712 host = (AS_Host *)instance->hostdata;
2714 seq_printf(m, "AcornSCSI driver v%d.%d.%d"
2715 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2718 #if (DEBUG & DEBUG_NO_WRITE)
2719 " NOWRITE (" __stringify(NO_WRITE) ")"
2721 "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
2723 seq_printf(m, "SBIC: WD33C93A Address: %p IRQ : %d\n",
2724 host->base + SBIC_REGIDX, host->scsi.irq);
2726 seq_printf(m, "DMAC: uPC71071 Address: %p IRQ : %d\n\n",
2727 host->base + DMAC_OFFSET, host->scsi.irq);
2730 seq_printf(m, "Statistics:\n"
2731 "Queued commands: %-10u Issued commands: %-10u\n"
2732 "Done commands : %-10u Reads : %-10u\n"
2733 "Writes : %-10u Others : %-10u\n"
2734 "Disconnects : %-10u Aborts : %-10u\n"
2735 "Resets : %-10u\n\nLast phases:",
2736 host->stats.queues, host->stats.removes,
2737 host->stats.fins, host->stats.reads,
2738 host->stats.writes, host->stats.miscs,
2739 host->stats.disconnects, host->stats.aborts,
2740 host->stats.resets);
2742 for (devidx = 0; devidx < 9; devidx ++) {
2743 unsigned int statptr, prev;
2745 seq_printf(m, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
2746 statptr = host->status_ptr[devidx] - 10;
2748 if ((signed int)statptr < 0)
2749 statptr += STATUS_BUFFER_SIZE;
2751 prev = host->status[devidx][statptr].when;
2753 for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
2754 if (host->status[devidx][statptr].when) {
2755 seq_printf(m, "%c%02X:%02X+%2ld",
2756 host->status[devidx][statptr].irq ? '-' : ' ',
2757 host->status[devidx][statptr].ph,
2758 host->status[devidx][statptr].ssr,
2759 (host->status[devidx][statptr].when - prev) < 100 ?
2760 (host->status[devidx][statptr].when - prev) : 99);
2761 prev = host->status[devidx][statptr].when;
2766 seq_printf(m, "\nAttached devices:\n");
2768 shost_for_each_device(scd, instance) {
2769 seq_printf(m, "Device/Lun TaggedQ Sync\n");
2770 seq_printf(m, " %d/%llu ", scd->id, scd->lun);
2771 if (scd->tagged_supported)
2772 seq_printf(m, "%3sabled ",
2773 scd->simple_tags ? "en" : "dis");
2775 seq_printf(m, "unsupported ");
2777 if (host->device[scd->id].sync_xfer & 15)
2778 seq_printf(m, "offset %d, %d ns\n",
2779 host->device[scd->id].sync_xfer & 15,
2780 acornscsi_getperiod(host->device[scd->id].sync_xfer));
2782 seq_printf(m, "async\n");
2788 static struct scsi_host_template acornscsi_template = {
2789 .module = THIS_MODULE,
2790 .show_info = acornscsi_show_info,
2791 .name = "AcornSCSI",
2792 .info = acornscsi_info,
2793 .queuecommand = acornscsi_queuecmd,
2794 .eh_abort_handler = acornscsi_abort,
2795 .eh_host_reset_handler = acornscsi_host_reset,
2798 .sg_tablesize = SG_ALL,
2800 .dma_boundary = PAGE_SIZE - 1,
2801 .proc_name = "acornscsi",
2804 static int acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
2806 struct Scsi_Host *host;
2810 ret = ecard_request_resources(ec);
2814 host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
2820 ashost = (AS_Host *)host->hostdata;
2822 ashost->base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
2823 ashost->fast = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
2824 if (!ashost->base || !ashost->fast) {
2829 host->irq = ec->irq;
2830 ashost->host = host;
2831 ashost->scsi.irq = host->irq;
2833 ec->irqaddr = ashost->fast + INT_REG;
2836 ret = request_irq(host->irq, acornscsi_intr, 0, "acornscsi", ashost);
2838 printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
2839 host->host_no, ashost->scsi.irq, ret);
2843 memset(&ashost->stats, 0, sizeof (ashost->stats));
2844 queue_initialise(&ashost->queues.issue);
2845 queue_initialise(&ashost->queues.disconnected);
2846 msgqueue_initialise(&ashost->scsi.msgs);
2848 acornscsi_resetcard(ashost);
2850 ret = scsi_add_host(host, &ec->dev);
2854 scsi_scan_host(host);
2858 free_irq(host->irq, ashost);
2859 msgqueue_free(&ashost->scsi.msgs);
2860 queue_free(&ashost->queues.disconnected);
2861 queue_free(&ashost->queues.issue);
2863 ecardm_iounmap(ec, ashost->fast);
2864 ecardm_iounmap(ec, ashost->base);
2865 scsi_host_put(host);
2867 ecard_release_resources(ec);
2872 static void acornscsi_remove(struct expansion_card *ec)
2874 struct Scsi_Host *host = ecard_get_drvdata(ec);
2875 AS_Host *ashost = (AS_Host *)host->hostdata;
2877 ecard_set_drvdata(ec, NULL);
2878 scsi_remove_host(host);
2881 * Put card into RESET state
2883 writeb(0x80, ashost->fast + PAGE_REG);
2885 free_irq(host->irq, ashost);
2887 msgqueue_free(&ashost->scsi.msgs);
2888 queue_free(&ashost->queues.disconnected);
2889 queue_free(&ashost->queues.issue);
2890 ecardm_iounmap(ec, ashost->fast);
2891 ecardm_iounmap(ec, ashost->base);
2892 scsi_host_put(host);
2893 ecard_release_resources(ec);
2896 static const struct ecard_id acornscsi_cids[] = {
2897 { MANU_ACORN, PROD_ACORN_SCSI },
2901 static struct ecard_driver acornscsi_driver = {
2902 .probe = acornscsi_probe,
2903 .remove = acornscsi_remove,
2904 .id_table = acornscsi_cids,
2906 .name = "acornscsi",
2910 static int __init acornscsi_init(void)
2912 return ecard_register_driver(&acornscsi_driver);
2915 static void __exit acornscsi_exit(void)
2917 ecard_remove_driver(&acornscsi_driver);
2920 module_init(acornscsi_init);
2921 module_exit(acornscsi_exit);
2923 MODULE_AUTHOR("Russell King");
2924 MODULE_DESCRIPTION("AcornSCSI driver");
2925 MODULE_LICENSE("GPL");