* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / acorn / scsi / acornscsi.c
blobe8a136753d032fdfe9780bc73c89d15dc8988af6
1 /*
2 * linux/arch/arm/drivers/scsi/acornscsi.c
4 * Acorn SCSI 3 driver
5 * By R.M.King.
7 * Abandoned using the Select and Transfer command since there were
8 * some nasty races between our software and the target devices that
9 * were not easy to solve, and the device errata had a lot of entries
10 * for this command, some of them quite nasty...
12 * Changelog:
13 * 26-Sep-1997 RMK Re-jigged to use the queue module.
14 * Re-coded state machine to be based on driver
15 * state not scsi state. Should be easier to debug.
16 * Added acornscsi_release to clean up properly.
17 * Updated proc/scsi reporting.
18 * 05-Oct-1997 RMK Implemented writing to SCSI devices.
19 * 06-Oct-1997 RMK Corrected small (non-serious) bug with the connect/
20 * reconnect race condition causing a warning message.
21 * 12-Oct-1997 RMK Added catch for re-entering interrupt routine.
22 * 15-Oct-1997 RMK Improved handling of commands.
23 * 27-Jun-1998 RMK Changed asm/delay.h to linux/delay.h.
24 * 13-Dec-1998 RMK Better abort code and command handling. Extra state
25 * transitions added to allow dodgy devices to work.
27 #define DEBUG_NO_WRITE 1
28 #define DEBUG_QUEUES 2
29 #define DEBUG_DMA 4
30 #define DEBUG_ABORT 8
31 #define DEBUG_DISCON 16
32 #define DEBUG_CONNECT 32
33 #define DEBUG_PHASES 64
34 #define DEBUG_WRITE 128
35 #define DEBUG_LINK 256
36 #define DEBUG_MESSAGES 512
37 #define DEBUG_RESET 1024
38 #define DEBUG_ALL (DEBUG_RESET|DEBUG_MESSAGES|DEBUG_LINK|DEBUG_WRITE|\
39 DEBUG_PHASES|DEBUG_CONNECT|DEBUG_DISCON|DEBUG_ABORT|\
40 DEBUG_DMA|DEBUG_QUEUES)
42 /* DRIVER CONFIGURATION
44 * SCSI-II Tagged queue support.
46 * I don't have any SCSI devices that support it, so it is totally untested
47 * (except to make sure that it doesn't interfere with any non-tagging
48 * devices). It is not fully implemented either - what happens when a
49 * tagging device reconnects???
51 * You can tell if you have a device that supports tagged queueing my
52 * cating (eg) /proc/scsi/acornscsi/0 and see if the SCSI revision is reported
53 * as '2 TAG'.
55 * Also note that CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE is normally set in the config
56 * scripts, but disabled here. Once debugged, remove the #undef, otherwise to debug,
57 * comment out the undef.
59 #undef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
61 * SCSI-II Linked command support.
63 * The higher level code doesn't support linked commands yet, and so the option
64 * is undef'd here.
66 #undef CONFIG_SCSI_ACORNSCSI_LINK
68 * SCSI-II Synchronous transfer support.
70 * Tried and tested...
72 * SDTR_SIZE - maximum number of un-acknowledged bytes (0 = off, 12 = max)
73 * SDTR_PERIOD - period of REQ signal (min=125, max=1020)
74 * DEFAULT_PERIOD - default REQ period.
76 #define SDTR_SIZE 12
77 #define SDTR_PERIOD 125
78 #define DEFAULT_PERIOD 500
81 * Debugging information
83 * DEBUG - bit mask from list above
84 * DEBUG_TARGET - is defined to the target number if you want to debug
85 * a specific target. [only recon/write/dma].
87 #define DEBUG (DEBUG_RESET|DEBUG_WRITE|DEBUG_NO_WRITE)
88 /* only allow writing to SCSI device 0 */
89 #define NO_WRITE 0xFE
90 /*#define DEBUG_TARGET 2*/
92 * Select timeout time (in 10ms units)
94 * This is the timeout used between the start of selection and the WD33C93
95 * chip deciding that the device isn't responding.
97 #define TIMEOUT_TIME 10
99 * Define this if you want to have verbose explaination of SCSI
100 * status/messages.
102 #undef CONFIG_ACORNSCSI_CONSTANTS
104 * Define this if you want to use the on board DMAC [don't remove this option]
105 * If not set, then use PIO mode (not currently supported).
107 #define USE_DMAC
109 * List of devices that the driver will recognise
111 #define ACORNSCSI_LIST { MANU_ACORN, PROD_ACORN_SCSI }
113 * ====================================================================================
116 #ifdef DEBUG_TARGET
117 #define DBG(cmd,xxx...) \
118 if (cmd->target == DEBUG_TARGET) { \
119 xxx; \
121 #else
122 #define DBG(cmd,xxx...) xxx
123 #endif
125 #ifndef STRINGIFY
126 #define STRINGIFY(x) #x
127 #endif
128 #define STRx(x) STRINGIFY(x)
129 #define NO_WRITE_STR STRx(NO_WRITE)
131 #include <linux/config.h>
132 #include <linux/module.h>
133 #include <linux/kernel.h>
134 #include <linux/sched.h>
135 #include <linux/string.h>
136 #include <linux/signal.h>
137 #include <linux/errno.h>
138 #include <linux/proc_fs.h>
139 #include <linux/stat.h>
140 #include <linux/ioport.h>
141 #include <linux/blk.h>
142 #include <linux/delay.h>
144 #include <asm/bitops.h>
145 #include <asm/system.h>
146 #include <asm/io.h>
147 #include <asm/irq.h>
148 #include <asm/ecard.h>
150 #include "../../scsi/scsi.h"
151 #include "../../scsi/hosts.h"
152 #include "../../scsi/constants.h"
153 #include "acornscsi.h"
154 #include "msgqueue.h"
156 #define VER_MAJOR 2
157 #define VER_MINOR 0
158 #define VER_PATCH 6
160 #ifndef ABORT_TAG
161 #define ABORT_TAG 0xd
162 #else
163 #error "Yippee! ABORT TAG is now defined! Remove this error!"
164 #endif
166 #ifdef CONFIG_SCSI_ACORNSCSI_LINK
167 #error SCSI2 LINKed commands not supported (yet)!
168 #endif
170 #ifdef USE_DMAC
172 * DMAC setup parameters
174 #define INIT_DEVCON0 (DEVCON0_RQL|DEVCON0_EXW|DEVCON0_CMP)
175 #define INIT_DEVCON1 (DEVCON1_BHLD)
176 #define DMAC_READ (MODECON_READ)
177 #define DMAC_WRITE (MODECON_WRITE)
178 #define INIT_SBICDMA (CTRL_DMABURST)
180 #define scsi_xferred have_data_in
183 * Size of on-board DMA buffer
185 #define DMAC_BUFFER_SIZE 65536
186 #endif
188 #define STATUS_BUFFER_TO_PRINT 24
190 unsigned int sdtr_period = SDTR_PERIOD;
191 unsigned int sdtr_size = SDTR_SIZE;
193 static struct proc_dir_entry proc_scsi_acornscsi = {
194 PROC_SCSI_EATA, 9, "acornscsi", S_IFDIR | S_IRUGO | S_IXUGO, 2
197 static void acornscsi_done(AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result);
198 static int acornscsi_reconnect_finish(AS_Host *host);
199 static void acornscsi_dma_cleanup(AS_Host *host);
200 static void acornscsi_abortcmd(AS_Host *host, unsigned char tag);
202 /* ====================================================================================
203 * Miscellaneous
206 static inline void
207 sbic_arm_write(unsigned int io_port, int reg, int value)
209 outb_t(reg, io_port);
210 outb_t(value, io_port + 4);
213 #define sbic_arm_writenext(io,val) \
214 outb_t((val), (io) + 4)
216 static inline
217 int sbic_arm_read(unsigned int io_port, int reg)
219 if(reg == ASR)
220 return inl_t(io_port) & 255;
221 outb_t(reg, io_port);
222 return inl_t(io_port + 4) & 255;
225 #define sbic_arm_readnext(io) \
226 inb_t((io) + 4)
228 #ifdef USE_DMAC
229 #define dmac_read(io_port,reg) \
230 inb((io_port) + (reg))
232 #define dmac_write(io_port,reg,value) \
233 ({ outb((value), (io_port) + (reg)); })
235 #define dmac_clearintr(io_port) \
236 ({ outb(0, (io_port)); })
238 static inline
239 unsigned int dmac_address(unsigned int io_port)
241 return dmac_read(io_port, TXADRHI) << 16 |
242 dmac_read(io_port, TXADRMD) << 8 |
243 dmac_read(io_port, TXADRLO);
246 static
247 void acornscsi_dumpdma(AS_Host *host, char *where)
249 unsigned int mode, addr, len;
251 mode = dmac_read(host->dma.io_port, MODECON);
252 addr = dmac_address(host->dma.io_port);
253 len = dmac_read(host->dma.io_port, TXCNTHI) << 8 |
254 dmac_read(host->dma.io_port, TXCNTLO);
256 printk("scsi%d: %s: DMAC %02x @%06x+%04x msk %02x, ",
257 host->host->host_no, where,
258 mode, addr, (len + 1) & 0xffff,
259 dmac_read(host->dma.io_port, MASKREG));
261 printk("DMA @%06x, ", host->dma.start_addr);
262 printk("BH @%p +%04x, ", host->scsi.SCp.ptr,
263 host->scsi.SCp.this_residual);
264 printk("DT @+%04x ST @+%04x", host->dma.transferred,
265 host->scsi.SCp.scsi_xferred);
266 printk("\n");
268 #endif
270 static
271 unsigned long acornscsi_sbic_xfcount(AS_Host *host)
273 unsigned long length;
275 length = sbic_arm_read(host->scsi.io_port, TRANSCNTH) << 16;
276 length |= sbic_arm_readnext(host->scsi.io_port) << 8;
277 length |= sbic_arm_readnext(host->scsi.io_port);
279 return length;
282 static int
283 acornscsi_sbic_wait(AS_Host *host, int stat_mask, int stat, int timeout, char *msg)
285 int asr;
287 do {
288 asr = sbic_arm_read(host->scsi.io_port, ASR);
290 if ((asr & stat_mask) == stat)
291 return 0;
293 udelay(1);
294 } while (--timeout);
296 printk("scsi%d: timeout while %s\n", host->host->host_no, msg);
298 return -1;
301 static
302 int acornscsi_sbic_issuecmd(AS_Host *host, int command)
304 if (acornscsi_sbic_wait(host, ASR_CIP, 0, 1000, "issuing command"))
305 return -1;
307 sbic_arm_write(host->scsi.io_port, CMND, command);
309 return 0;
312 static void
313 acornscsi_csdelay(unsigned int cs)
315 unsigned long target_jiffies, flags;
317 target_jiffies = jiffies + 1 + cs * HZ / 100;
319 save_flags(flags);
320 sti();
322 while (time_before(jiffies, target_jiffies)) barrier();
324 restore_flags(flags);
327 static
328 void acornscsi_resetcard(AS_Host *host)
330 unsigned int i, timeout;
332 /* assert reset line */
333 host->card.page_reg = 0x80;
334 outb(host->card.page_reg, host->card.io_page);
336 /* wait 3 cs. SCSI standard says 25ms. */
337 acornscsi_csdelay(3);
339 host->card.page_reg = 0;
340 outb(host->card.page_reg, host->card.io_page);
343 * Should get a reset from the card
345 timeout = 1000;
346 do {
347 if (inb(host->card.io_intr) & 8)
348 break;
349 udelay(1);
350 } while (--timeout);
352 if (timeout == 0)
353 printk("scsi%d: timeout while resetting card\n",
354 host->host->host_no);
356 sbic_arm_read(host->scsi.io_port, ASR);
357 sbic_arm_read(host->scsi.io_port, SSR);
359 /* setup sbic - WD33C93A */
360 sbic_arm_write(host->scsi.io_port, OWNID, OWNID_EAF | host->host->this_id);
361 sbic_arm_write(host->scsi.io_port, CMND, CMND_RESET);
364 * Command should cause a reset interrupt
366 timeout = 1000;
367 do {
368 if (inb(host->card.io_intr) & 8)
369 break;
370 udelay(1);
371 } while (--timeout);
373 if (timeout == 0)
374 printk("scsi%d: timeout while resetting card\n",
375 host->host->host_no);
377 sbic_arm_read(host->scsi.io_port, ASR);
378 if (sbic_arm_read(host->scsi.io_port, SSR) != 0x01)
379 printk(KERN_CRIT "scsi%d: WD33C93A didn't give enhanced reset interrupt\n",
380 host->host->host_no);
382 sbic_arm_write(host->scsi.io_port, CTRL, INIT_SBICDMA | CTRL_IDI);
383 sbic_arm_write(host->scsi.io_port, TIMEOUT, TIMEOUT_TIME);
384 sbic_arm_write(host->scsi.io_port, SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
385 sbic_arm_write(host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
387 host->card.page_reg = 0x40;
388 outb(host->card.page_reg, host->card.io_page);
390 /* setup dmac - uPC71071 */
391 dmac_write(host->dma.io_port, INIT, 0);
392 #ifdef USE_DMAC
393 dmac_write(host->dma.io_port, INIT, INIT_8BIT);
394 dmac_write(host->dma.io_port, CHANNEL, CHANNEL_0);
395 dmac_write(host->dma.io_port, DEVCON0, INIT_DEVCON0);
396 dmac_write(host->dma.io_port, DEVCON1, INIT_DEVCON1);
397 #endif
399 host->SCpnt = NULL;
400 host->scsi.phase = PHASE_IDLE;
401 host->scsi.disconnectable = 0;
403 for (i = 0; i < 8; i++) {
404 host->busyluns[i] = 0;
405 host->device[i].sync_state = SYNC_NEGOCIATE;
406 host->device[i].disconnect_ok = 1;
409 /* wait 25 cs. SCSI standard says 250ms. */
410 acornscsi_csdelay(25);
413 /*=============================================================================================
414 * Utility routines (eg. debug)
416 #ifdef CONFIG_ACORNSCSI_CONSTANTS
417 static char *acornscsi_interrupttype[] = {
418 "rst", "suc", "p/a", "3",
419 "term", "5", "6", "7",
420 "serv", "9", "a", "b",
421 "c", "d", "e", "f"
424 static signed char acornscsi_map[] = {
425 0, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
426 -1, 2, -1, -1, -1, -1, 3, -1, 4, 5, 6, 7, 8, 9, 10, 11,
427 12, 13, 14, -1, -1, -1, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
428 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
429 15, 16, 17, 18, 19, -1, -1, 20, 4, 5, 6, 7, 8, 9, 10, 11,
430 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
431 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
432 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
433 21, 22, -1, -1, -1, 23, -1, -1, 4, 5, 6, 7, 8, 9, 10, 11,
434 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
435 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
436 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
437 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
438 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
439 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
440 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
443 static char *acornscsi_interruptcode[] = {
444 /* 0 */
445 "reset - normal mode", /* 00 */
446 "reset - advanced mode", /* 01 */
448 /* 2 */
449 "sel", /* 11 */
450 "sel+xfer", /* 16 */
451 "data-out", /* 18 */
452 "data-in", /* 19 */
453 "cmd", /* 1A */
454 "stat", /* 1B */
455 "??-out", /* 1C */
456 "??-in", /* 1D */
457 "msg-out", /* 1E */
458 "msg-in", /* 1F */
460 /* 12 */
461 "/ACK asserted", /* 20 */
462 "save-data-ptr", /* 21 */
463 "{re}sel", /* 22 */
465 /* 15 */
466 "inv cmd", /* 40 */
467 "unexpected disconnect", /* 41 */
468 "sel timeout", /* 42 */
469 "P err", /* 43 */
470 "P err+ATN", /* 44 */
471 "bad status byte", /* 47 */
473 /* 21 */
474 "resel, no id", /* 80 */
475 "resel", /* 81 */
476 "discon", /* 85 */
479 static
480 void print_scsi_status(unsigned int ssr)
482 if (acornscsi_map[ssr] != -1)
483 printk("%s:%s",
484 acornscsi_interrupttype[(ssr >> 4)],
485 acornscsi_interruptcode[acornscsi_map[ssr]]);
486 else
487 printk("%X:%X", ssr >> 4, ssr & 0x0f);
489 #endif
491 static
492 void print_sbic_status(int asr, int ssr, int cmdphase)
494 #ifdef CONFIG_ACORNSCSI_CONSTANTS
495 printk("sbic: %c%c%c%c%c%c ",
496 asr & ASR_INT ? 'I' : 'i',
497 asr & ASR_LCI ? 'L' : 'l',
498 asr & ASR_BSY ? 'B' : 'b',
499 asr & ASR_CIP ? 'C' : 'c',
500 asr & ASR_PE ? 'P' : 'p',
501 asr & ASR_DBR ? 'D' : 'd');
502 printk("scsi: ");
503 print_scsi_status(ssr);
504 printk(" ph %02X\n", cmdphase);
505 #else
506 printk("sbic: %02X scsi: %X:%X ph: %02X\n",
507 asr, (ssr & 0xf0)>>4, ssr & 0x0f, cmdphase);
508 #endif
511 static void
512 acornscsi_dumplogline(AS_Host *host, int target, int line)
514 unsigned long prev;
515 signed int ptr;
517 ptr = host->status_ptr[target] - STATUS_BUFFER_TO_PRINT;
518 if (ptr < 0)
519 ptr += STATUS_BUFFER_SIZE;
521 printk("%c: %3s:", target == 8 ? 'H' : '0' + target,
522 line == 0 ? "ph" : line == 1 ? "ssr" : "int");
524 prev = host->status[target][ptr].when;
526 for (; ptr != host->status_ptr[target]; ptr = (ptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
527 unsigned long time_diff;
529 if (!host->status[target][ptr].when)
530 continue;
532 switch (line) {
533 case 0:
534 printk("%c%02X", host->status[target][ptr].irq ? '-' : ' ',
535 host->status[target][ptr].ph);
536 break;
538 case 1:
539 printk(" %02X", host->status[target][ptr].ssr);
540 break;
542 case 2:
543 time_diff = host->status[target][ptr].when - prev;
544 prev = host->status[target][ptr].when;
545 if (time_diff == 0)
546 printk("==^");
547 else if (time_diff >= 100)
548 printk(" ");
549 else
550 printk(" %02ld", time_diff);
551 break;
555 printk("\n");
558 static
559 void acornscsi_dumplog(AS_Host *host, int target)
561 do {
562 acornscsi_dumplogline(host, target, 0);
563 acornscsi_dumplogline(host, target, 1);
564 acornscsi_dumplogline(host, target, 2);
566 if (target == 8)
567 break;
569 target = 8;
570 } while (1);
573 static
574 char acornscsi_target(AS_Host *host)
576 if (host->SCpnt)
577 return '0' + host->SCpnt->target;
578 return 'H';
582 * Prototype: cmdtype_t acornscsi_cmdtype(int command)
583 * Purpose : differentiate READ from WRITE from other commands
584 * Params : command - command to interpret
585 * Returns : CMD_READ - command reads data,
586 * CMD_WRITE - command writes data,
587 * CMD_MISC - everything else
589 static inline
590 cmdtype_t acornscsi_cmdtype(int command)
592 switch (command) {
593 case WRITE_6: case WRITE_10: case WRITE_12:
594 return CMD_WRITE;
595 case READ_6: case READ_10: case READ_12:
596 return CMD_READ;
597 default:
598 return CMD_MISC;
603 * Prototype: int acornscsi_datadirection(int command)
604 * Purpose : differentiate between commands that have a DATA IN phase
605 * and a DATA OUT phase
606 * Params : command - command to interpret
607 * Returns : DATADIR_OUT - data out phase expected
608 * DATADIR_IN - data in phase expected
610 static
611 datadir_t acornscsi_datadirection(int command)
613 switch (command) {
614 case CHANGE_DEFINITION: case COMPARE: case COPY:
615 case COPY_VERIFY: case LOG_SELECT: case MODE_SELECT:
616 case MODE_SELECT_10: case SEND_DIAGNOSTIC: case WRITE_BUFFER:
617 case FORMAT_UNIT: case REASSIGN_BLOCKS: case RESERVE:
618 case SEARCH_EQUAL: case SEARCH_HIGH: case SEARCH_LOW:
619 case WRITE_6: case WRITE_10: case WRITE_VERIFY:
620 case UPDATE_BLOCK: case WRITE_LONG: case WRITE_SAME:
621 case SEARCH_HIGH_12: case SEARCH_EQUAL_12: case SEARCH_LOW_12:
622 case WRITE_12: case WRITE_VERIFY_12: case SET_WINDOW:
623 case MEDIUM_SCAN: case SEND_VOLUME_TAG: case 0xea:
624 return DATADIR_OUT;
625 default:
626 return DATADIR_IN;
631 * Purpose : provide values for synchronous transfers with 33C93.
632 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
633 * Modified by Russell King for 8MHz WD33C93A
635 static struct sync_xfer_tbl {
636 unsigned int period_ns;
637 unsigned char reg_value;
638 } sync_xfer_table[] = {
639 { 1, 0x20 }, { 249, 0x20 }, { 374, 0x30 },
640 { 499, 0x40 }, { 624, 0x50 }, { 749, 0x60 },
641 { 874, 0x70 }, { 999, 0x00 }, { 0, 0 }
645 * Prototype: int acornscsi_getperiod(unsigned char syncxfer)
646 * Purpose : period for the synchronous transfer setting
647 * Params : syncxfer SYNCXFER register value
648 * Returns : period in ns.
650 static
651 int acornscsi_getperiod(unsigned char syncxfer)
653 int i;
655 syncxfer &= 0xf0;
656 if (syncxfer == 0x10)
657 syncxfer = 0;
659 for (i = 1; sync_xfer_table[i].period_ns; i++)
660 if (syncxfer == sync_xfer_table[i].reg_value)
661 return sync_xfer_table[i].period_ns;
662 return 0;
666 * Prototype: int round_period(unsigned int period)
667 * Purpose : return index into above table for a required REQ period
668 * Params : period - time (ns) for REQ
669 * Returns : table index
670 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
672 static inline
673 int round_period(unsigned int period)
675 int i;
677 for (i = 1; sync_xfer_table[i].period_ns; i++) {
678 if ((period <= sync_xfer_table[i].period_ns) &&
679 (period > sync_xfer_table[i - 1].period_ns))
680 return i;
682 return 7;
686 * Prototype: unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
687 * Purpose : calculate value for 33c93s SYNC register
688 * Params : period - time (ns) for REQ
689 * offset - offset in bytes between REQ/ACK
690 * Returns : value for SYNC register
691 * Copyright: Copyright (c) 1996 John Shifflett, GeoLog Consulting
693 static
694 unsigned char calc_sync_xfer(unsigned int period, unsigned int offset)
696 return sync_xfer_table[round_period(period)].reg_value |
697 ((offset < SDTR_SIZE) ? offset : SDTR_SIZE);
700 /* ====================================================================================
701 * Command functions
704 * Function: acornscsi_kick(AS_Host *host)
705 * Purpose : kick next command to interface
706 * Params : host - host to send command to
707 * Returns : INTR_IDLE if idle, otherwise INTR_PROCESSING
708 * Notes : interrupts are always disabled!
710 static
711 intr_ret_t acornscsi_kick(AS_Host *host)
713 int from_queue = 0;
714 Scsi_Cmnd *SCpnt;
716 /* first check to see if a command is waiting to be executed */
717 SCpnt = host->origSCpnt;
718 host->origSCpnt = NULL;
720 /* retrieve next command */
721 if (!SCpnt) {
722 SCpnt = queue_remove_exclude(&host->queues.issue, host->busyluns);
723 if (!SCpnt)
724 return INTR_IDLE;
726 from_queue = 1;
729 if (host->scsi.disconnectable && host->SCpnt) {
730 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
731 host->scsi.disconnectable = 0;
732 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
733 DBG(host->SCpnt, printk("scsi%d.%c: moved command to disconnected queue\n",
734 host->host->host_no, acornscsi_target(host)));
735 #endif
736 host->SCpnt = NULL;
740 * If we have an interrupt pending, then we may have been reselected.
741 * In this case, we don't want to write to the registers
743 if (!(sbic_arm_read(host->scsi.io_port, ASR) & (ASR_INT|ASR_BSY|ASR_CIP))) {
744 sbic_arm_write(host->scsi.io_port, DESTID, SCpnt->target);
745 sbic_arm_write(host->scsi.io_port, CMND, CMND_SELWITHATN);
749 * claim host busy - all of these must happen atomically wrt
750 * our interrupt routine. Failure means command loss.
752 host->scsi.phase = PHASE_CONNECTING;
753 host->SCpnt = SCpnt;
754 host->scsi.SCp = SCpnt->SCp;
755 host->dma.xfer_setup = 0;
756 host->dma.xfer_required = 0;
757 host->dma.xfer_done = 0;
759 #if (DEBUG & (DEBUG_ABORT|DEBUG_CONNECT))
760 DBG(SCpnt,printk("scsi%d.%c: starting cmd %02X\n",
761 host->host->host_no, '0' + SCpnt->target,
762 SCpnt->cmnd[0]));
763 #endif
765 if (from_queue) {
766 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
768 * tagged queueing - allocate a new tag to this command
770 if (SCpnt->device->tagged_queue) {
771 SCpnt->device->current_tag += 1;
772 if (SCpnt->device->current_tag == 0)
773 SCpnt->device->current_tag = 1;
774 SCpnt->tag = SCpnt->device->current_tag;
775 } else
776 #endif
777 set_bit(SCpnt->target * 8 + SCpnt->lun, host->busyluns);
779 host->stats.removes += 1;
781 switch (acornscsi_cmdtype(SCpnt->cmnd[0])) {
782 case CMD_WRITE:
783 host->stats.writes += 1;
784 break;
785 case CMD_READ:
786 host->stats.reads += 1;
787 break;
788 case CMD_MISC:
789 host->stats.miscs += 1;
790 break;
794 return INTR_PROCESSING;
798 * Function: void acornscsi_done(AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result)
799 * Purpose : complete processing for command
800 * Params : host - interface that completed
801 * result - driver byte of result
803 static
804 void acornscsi_done(AS_Host *host, Scsi_Cmnd **SCpntp, unsigned int result)
806 Scsi_Cmnd *SCpnt = *SCpntp;
808 /* clean up */
809 sbic_arm_write(host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
811 host->stats.fins += 1;
813 if (SCpnt) {
814 *SCpntp = NULL;
816 acornscsi_dma_cleanup(host);
818 SCpnt->result = result << 16 | host->scsi.SCp.Message << 8 | host->scsi.SCp.Status;
821 * In theory, this should not happen. In practice, it seems to.
822 * Only trigger an error if the device attempts to report all happy
823 * but with untransferred buffers... If we don't do something, then
824 * data loss will occur. Should we check SCpnt->underflow here?
825 * It doesn't appear to be set to something meaningful by the higher
826 * levels all the time.
828 if (result == DID_OK) {
829 int xfer_warn = 0;
831 if (SCpnt->underflow == 0) {
832 if (host->scsi.SCp.ptr &&
833 acornscsi_cmdtype(SCpnt->cmnd[0]) != CMD_MISC)
834 xfer_warn = 1;
835 } else {
836 if (host->scsi.SCp.scsi_xferred < SCpnt->underflow ||
837 host->scsi.SCp.scsi_xferred != host->dma.transferred)
838 xfer_warn = 1;
841 /* ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.6)
842 * Targets which break data transfers into multiple
843 * connections shall end each successful connection
844 * (except possibly the last) with a SAVE DATA
845 * POINTER - DISCONNECT message sequence.
847 * This makes it difficult to ensure that a transfer has
848 * completed. If we reach the end of a transfer during
849 * the command, then we can only have finished the transfer.
850 * therefore, if we seem to have some data remaining, this
851 * is not a problem.
853 if (host->dma.xfer_done)
854 xfer_warn = 0;
856 if (xfer_warn) {
857 switch (status_byte(SCpnt->result)) {
858 case CHECK_CONDITION:
859 case COMMAND_TERMINATED:
860 case BUSY:
861 case QUEUE_FULL:
862 case RESERVATION_CONFLICT:
863 break;
865 default:
866 printk(KERN_ERR "scsi%d.H: incomplete data transfer detected: result=%08X command=",
867 host->host->host_no, SCpnt->result);
868 print_command(SCpnt->cmnd);
869 acornscsi_dumpdma(host, "done");
870 acornscsi_dumplog(host, SCpnt->target);
871 SCpnt->result &= 0xffff;
872 SCpnt->result |= DID_ERROR << 16;
877 if (!SCpnt->scsi_done)
878 panic("scsi%d.H: null scsi_done function in acornscsi_done", host->host->host_no);
880 clear_bit(SCpnt->target * 8 + SCpnt->lun, host->busyluns);
882 SCpnt->scsi_done(SCpnt);
883 } else
884 printk("scsi%d: null command in acornscsi_done", host->host->host_no);
886 host->scsi.phase = PHASE_IDLE;
889 /* ====================================================================================
890 * DMA routines
893 * Purpose : update SCSI Data Pointer
894 * Notes : this will only be one SG entry or less
896 static
897 void acornscsi_data_updateptr(AS_Host *host, Scsi_Pointer *SCp, unsigned int length)
899 SCp->ptr += length;
900 SCp->this_residual -= length;
902 if (!SCp->this_residual) {
903 if (SCp->buffers_residual) {
904 SCp->buffer++;
905 SCp->buffers_residual--;
906 SCp->ptr = (char *)SCp->buffer->address;
907 SCp->this_residual = SCp->buffer->length;
908 } else {
909 SCp->ptr = NULL;
910 host->dma.xfer_done = 1;
916 * Prototype: void acornscsi_data_read(AS_Host *host, char *ptr,
917 * unsigned int start_addr, unsigned int length)
918 * Purpose : read data from DMA RAM
919 * Params : host - host to transfer from
920 * ptr - DRAM address
921 * start_addr - host mem address
922 * length - number of bytes to transfer
923 * Notes : this will only be one SG entry or less
925 static
926 void acornscsi_data_read(AS_Host *host, char *ptr,
927 unsigned int start_addr, unsigned int length)
929 extern void __acornscsi_in(int port, char *buf, int len);
930 unsigned int page, offset, len = length;
932 page = (start_addr >> 12);
933 offset = start_addr & ((1 << 12) - 1);
935 outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
937 while (len > 0) {
938 unsigned int this_len;
940 if (len + offset > (1 << 12))
941 this_len = (1 << 12) - offset;
942 else
943 this_len = len;
945 __acornscsi_in(host->card.io_ram + (offset << 1), ptr, this_len);
947 offset += this_len;
948 ptr += this_len;
949 len -= this_len;
951 if (offset == (1 << 12)) {
952 offset = 0;
953 page ++;
954 outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
957 outb(host->card.page_reg, host->card.io_page);
961 * Prototype: void acornscsi_data_write(AS_Host *host, char *ptr,
962 * unsigned int start_addr, unsigned int length)
963 * Purpose : write data to DMA RAM
964 * Params : host - host to transfer from
965 * ptr - DRAM address
966 * start_addr - host mem address
967 * length - number of bytes to transfer
968 * Notes : this will only be one SG entry or less
970 static
971 void acornscsi_data_write(AS_Host *host, char *ptr,
972 unsigned int start_addr, unsigned int length)
974 extern void __acornscsi_out(int port, char *buf, int len);
975 unsigned int page, offset, len = length;
977 page = (start_addr >> 12);
978 offset = start_addr & ((1 << 12) - 1);
980 outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
982 while (len > 0) {
983 unsigned int this_len;
985 if (len + offset > (1 << 12))
986 this_len = (1 << 12) - offset;
987 else
988 this_len = len;
990 __acornscsi_out(host->card.io_ram + (offset << 1), ptr, this_len);
992 offset += this_len;
993 ptr += this_len;
994 len -= this_len;
996 if (offset == (1 << 12)) {
997 offset = 0;
998 page ++;
999 outb((page & 0x3f) | host->card.page_reg, host->card.io_page);
1002 outb(host->card.page_reg, host->card.io_page);
1005 /* =========================================================================================
1006 * On-board DMA routines
1008 #ifdef USE_DMAC
1010 * Prototype: void acornscsi_dmastop(AS_Host *host)
1011 * Purpose : stop all DMA
1012 * Params : host - host on which to stop DMA
1013 * Notes : This is called when leaving DATA IN/OUT phase,
1014 * or when interface is RESET
1016 static inline
1017 void acornscsi_dma_stop(AS_Host *host)
1019 dmac_write(host->dma.io_port, MASKREG, MASK_ON);
1020 dmac_clearintr(host->dma.io_intr_clear);
1022 #if (DEBUG & DEBUG_DMA)
1023 DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
1024 #endif
1028 * Function: void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1029 * Purpose : setup DMA controller for data transfer
1030 * Params : host - host to setup
1031 * direction - data transfer direction
1032 * Notes : This is called when entering DATA I/O phase, not
1033 * while we're in a DATA I/O phase
1035 static
1036 void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
1038 unsigned int address, length, mode;
1040 host->dma.direction = direction;
1042 dmac_write(host->dma.io_port, MASKREG, MASK_ON);
1044 if (direction == DMA_OUT) {
1045 #if (DEBUG & DEBUG_NO_WRITE)
1046 if (NO_WRITE & (1 << host->SCpnt->target)) {
1047 printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
1048 host->host->host_no, acornscsi_target(host));
1049 return;
1051 #endif
1052 mode = DMAC_WRITE;
1053 } else
1054 mode = DMAC_READ;
1057 * Allocate some buffer space, limited to half the buffer size
1059 length = min(host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1060 if (length) {
1061 host->dma.start_addr = address = host->dma.free_addr;
1062 host->dma.free_addr = (host->dma.free_addr + length) &
1063 (DMAC_BUFFER_SIZE - 1);
1066 * Transfer data to DMA memory
1068 if (direction == DMA_OUT)
1069 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1070 length);
1072 length -= 1;
1073 dmac_write(host->dma.io_port, TXCNTLO, length);
1074 dmac_write(host->dma.io_port, TXCNTHI, length >> 8);
1075 dmac_write(host->dma.io_port, TXADRLO, address);
1076 dmac_write(host->dma.io_port, TXADRMD, address >> 8);
1077 dmac_write(host->dma.io_port, TXADRHI, 0);
1078 dmac_write(host->dma.io_port, MODECON, mode);
1079 dmac_write(host->dma.io_port, MASKREG, MASK_OFF);
1081 #if (DEBUG & DEBUG_DMA)
1082 DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
1083 #endif
1084 host->dma.xfer_setup = 1;
1089 * Function: void acornscsi_dma_cleanup(AS_Host *host)
1090 * Purpose : ensure that all DMA transfers are up-to-date & host->scsi.SCp is correct
1091 * Params : host - host to finish
1092 * Notes : This is called when a command is:
1093 * terminating, RESTORE_POINTERS, SAVE_POINTERS, DISCONECT
1094 * : This must not return until all transfers are completed.
1096 static
1097 void acornscsi_dma_cleanup(AS_Host *host)
1099 dmac_write(host->dma.io_port, MASKREG, MASK_ON);
1100 dmac_clearintr(host->dma.io_intr_clear);
1103 * Check for a pending transfer
1105 if (host->dma.xfer_required) {
1106 host->dma.xfer_required = 0;
1107 if (host->dma.direction == DMA_IN)
1108 acornscsi_data_read(host, host->dma.xfer_ptr,
1109 host->dma.xfer_start, host->dma.xfer_length);
1113 * Has a transfer been setup?
1115 if (host->dma.xfer_setup) {
1116 unsigned int transferred;
1118 host->dma.xfer_setup = 0;
1120 #if (DEBUG & DEBUG_DMA)
1121 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
1122 #endif
1125 * Calculate number of bytes transferred from DMA.
1127 transferred = dmac_address(host->dma.io_port) - host->dma.start_addr;
1128 host->dma.transferred += transferred;
1130 if (host->dma.direction == DMA_IN)
1131 acornscsi_data_read(host, host->scsi.SCp.ptr,
1132 host->dma.start_addr, transferred);
1135 * Update SCSI pointers
1137 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1138 #if (DEBUG & DEBUG_DMA)
1139 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
1140 #endif
1145 * Function: void acornscsi_dmacintr(AS_Host *host)
1146 * Purpose : handle interrupts from DMAC device
1147 * Params : host - host to process
1148 * Notes : If reading, we schedule the read to main memory &
1149 * allow the transfer to continue.
1150 * : If writing, we fill the onboard DMA memory from main
1151 * memory.
1152 * : Called whenever DMAC finished it's current transfer.
1154 static
1155 void acornscsi_dma_intr(AS_Host *host)
1157 unsigned int address, length, transferred;
1159 #if (DEBUG & DEBUG_DMA)
1160 DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
1161 #endif
1163 dmac_write(host->dma.io_port, MASKREG, MASK_ON);
1164 dmac_clearintr(host->dma.io_intr_clear);
1167 * Calculate amount transferred via DMA
1169 transferred = dmac_address(host->dma.io_port) - host->dma.start_addr;
1170 host->dma.transferred += transferred;
1173 * Schedule DMA transfer off board
1175 if (host->dma.direction == DMA_IN) {
1176 host->dma.xfer_start = host->dma.start_addr;
1177 host->dma.xfer_length = transferred;
1178 host->dma.xfer_ptr = host->scsi.SCp.ptr;
1179 host->dma.xfer_required = 1;
1182 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1185 * Allocate some buffer space, limited to half the on-board RAM size
1187 length = min(host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1188 if (length) {
1189 host->dma.start_addr = address = host->dma.free_addr;
1190 host->dma.free_addr = (host->dma.free_addr + length) &
1191 (DMAC_BUFFER_SIZE - 1);
1194 * Transfer data to DMA memory
1196 if (host->dma.direction == DMA_OUT)
1197 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1198 length);
1200 length -= 1;
1201 dmac_write(host->dma.io_port, TXCNTLO, length);
1202 dmac_write(host->dma.io_port, TXCNTHI, length >> 8);
1203 dmac_write(host->dma.io_port, TXADRLO, address);
1204 dmac_write(host->dma.io_port, TXADRMD, address >> 8);
1205 dmac_write(host->dma.io_port, TXADRHI, 0);
1206 dmac_write(host->dma.io_port, MASKREG, MASK_OFF);
1208 #if (DEBUG & DEBUG_DMA)
1209 DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
1210 #endif
1211 } else {
1212 host->dma.xfer_setup = 0;
1213 #if 0
1215 * If the interface still wants more, then this is an error.
1216 * We give it another byte, but we also attempt to raise an
1217 * attention condition. We continue giving one byte until
1218 * the device recognises the attention.
1220 if (dmac_read(host->dma.io_port, STATUS) & STATUS_RQ0) {
1221 acornscsi_abortcmd(host, host->SCpnt->tag);
1223 dmac_write(host->dma.io_port, TXCNTLO, 0);
1224 dmac_write(host->dma.io_port, TXCNTHI, 0);
1225 dmac_write(host->dma.io_port, TXADRLO, 0);
1226 dmac_write(host->dma.io_port, TXADRMD, 0);
1227 dmac_write(host->dma.io_port, TXADRHI, 0);
1228 dmac_write(host->dma.io_port, MASKREG, MASK_OFF);
1230 #endif
1235 * Function: void acornscsi_dma_xfer(AS_Host *host)
1236 * Purpose : transfer data between AcornSCSI and memory
1237 * Params : host - host to process
1239 static
1240 void acornscsi_dma_xfer(AS_Host *host)
1242 host->dma.xfer_required = 0;
1244 if (host->dma.direction == DMA_IN)
1245 acornscsi_data_read(host, host->dma.xfer_ptr,
1246 host->dma.xfer_start, host->dma.xfer_length);
1250 * Function: void acornscsi_dma_adjust(AS_Host *host)
1251 * Purpose : adjust DMA pointers & count for bytes transfered to
1252 * SBIC but not SCSI bus.
1253 * Params : host - host to adjust DMA count for
1255 static
1256 void acornscsi_dma_adjust(AS_Host *host)
1258 if (host->dma.xfer_setup) {
1259 signed long transferred;
1260 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1261 DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
1262 #endif
1264 * Calculate correct DMA address - DMA is ahead of SCSI bus while
1265 * writing.
1266 * host->scsi.SCp.scsi_xferred is the number of bytes
1267 * actually transferred to/from the SCSI bus.
1268 * host->dma.transferred is the number of bytes transferred
1269 * over DMA since host->dma.start_addr was last set.
1271 * real_dma_addr = host->dma.start_addr + host->scsi.SCp.scsi_xferred
1272 * - host->dma.transferred
1274 transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
1275 if (transferred < 0)
1276 printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1277 host->host->host_no, acornscsi_target(host), transferred);
1278 else if (transferred == 0)
1279 host->dma.xfer_setup = 0;
1280 else {
1281 transferred += host->dma.start_addr;
1282 dmac_write(host->dma.io_port, TXADRLO, transferred);
1283 dmac_write(host->dma.io_port, TXADRMD, transferred >> 8);
1284 dmac_write(host->dma.io_port, TXADRHI, transferred >> 16);
1285 #if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1286 DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
1287 #endif
1291 #endif
1293 /* =========================================================================================
1294 * Data I/O
1296 static int
1297 acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
1299 unsigned int asr, timeout = max_timeout;
1300 int my_ptr = *ptr;
1302 while (my_ptr < len) {
1303 asr = sbic_arm_read(host->scsi.io_port, ASR);
1305 if (asr & ASR_DBR) {
1306 timeout = max_timeout;
1308 sbic_arm_write(host->scsi.io_port, DATA, bytes[my_ptr++]);
1309 } else if (asr & ASR_INT)
1310 break;
1311 else if (--timeout == 0)
1312 break;
1313 udelay(1);
1316 *ptr = my_ptr;
1318 return (timeout == 0) ? -1 : 0;
1322 * Function: void acornscsi_sendcommand(AS_Host *host)
1323 * Purpose : send a command to a target
1324 * Params : host - host which is connected to target
1326 static void
1327 acornscsi_sendcommand(AS_Host *host)
1329 Scsi_Cmnd *SCpnt = host->SCpnt;
1331 sbic_arm_write(host->scsi.io_port, TRANSCNTH, 0);
1332 sbic_arm_writenext(host->scsi.io_port, 0);
1333 sbic_arm_writenext(host->scsi.io_port, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1335 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1337 if (acornscsi_write_pio(host, SCpnt->cmnd,
1338 (int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
1339 printk("scsi%d: timeout while sending command\n", host->host->host_no);
1341 host->scsi.phase = PHASE_COMMAND;
1344 static
1345 void acornscsi_sendmessage(AS_Host *host)
1347 unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
1348 unsigned int msgnr;
1349 struct message *msg;
1351 #if (DEBUG & DEBUG_MESSAGES)
1352 printk("scsi%d.%c: sending message ",
1353 host->host->host_no, acornscsi_target(host));
1354 #endif
1356 switch (message_length) {
1357 case 0:
1358 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1360 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
1362 sbic_arm_write(host->scsi.io_port, DATA, NOP);
1364 host->scsi.last_message = NOP;
1365 #if (DEBUG & DEBUG_MESSAGES)
1366 printk("NOP");
1367 #endif
1368 break;
1370 case 1:
1371 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1372 msg = msgqueue_getmsg(&host->scsi.msgs, 0);
1374 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
1376 sbic_arm_write(host->scsi.io_port, DATA, msg->msg[0]);
1378 host->scsi.last_message = msg->msg[0];
1379 #if (DEBUG & DEBUG_MESSAGES)
1380 print_msg(msg->msg);
1381 #endif
1382 break;
1384 default:
1386 * ANSI standard says: (SCSI-2 Rev 10c Sect 5.6.14)
1387 * 'When a target sends this (MESSAGE_REJECT) message, it
1388 * shall change to MESSAGE IN phase and send this message
1389 * prior to requesting additional message bytes from the
1390 * initiator. This provides an interlock so that the
1391 * initiator can determine which message byte is rejected.
1393 sbic_arm_write(host->scsi.io_port, TRANSCNTH, 0);
1394 sbic_arm_writenext(host->scsi.io_port, 0);
1395 sbic_arm_writenext(host->scsi.io_port, message_length);
1396 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1398 msgnr = 0;
1399 while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
1400 unsigned int i;
1401 #if (DEBUG & DEBUG_MESSAGES)
1402 print_msg(msg);
1403 #endif
1404 i = 0;
1405 if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
1406 printk("scsi%d: timeout while sending message\n", host->host->host_no);
1408 host->scsi.last_message = msg->msg[0];
1409 if (msg->msg[0] == EXTENDED_MESSAGE)
1410 host->scsi.last_message |= msg->msg[2] << 8;
1412 if (i != msg->length)
1413 break;
1415 break;
1417 #if (DEBUG & DEBUG_MESSAGES)
1418 printk("\n");
1419 #endif
1423 * Function: void acornscsi_readstatusbyte(AS_Host *host)
1424 * Purpose : Read status byte from connected target
1425 * Params : host - host connected to target
1427 static
1428 void acornscsi_readstatusbyte(AS_Host *host)
1430 acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
1431 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
1432 host->scsi.SCp.Status = sbic_arm_read(host->scsi.io_port, DATA);
1436 * Function: unsigned char acornscsi_readmessagebyte(AS_Host *host)
1437 * Purpose : Read one message byte from connected target
1438 * Params : host - host connected to target
1440 static
1441 unsigned char acornscsi_readmessagebyte(AS_Host *host)
1443 unsigned char message;
1445 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1447 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
1449 message = sbic_arm_read(host->scsi.io_port, DATA);
1451 /* wait for MSGIN-XFER-PAUSED */
1452 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
1454 sbic_arm_read(host->scsi.io_port, SSR);
1456 return message;
1460 * Function: void acornscsi_message(AS_Host *host)
1461 * Purpose : Read complete message from connected target & action message
1462 * Params : host - host connected to target
1464 static
1465 void acornscsi_message(AS_Host *host)
1467 unsigned char message[16];
1468 unsigned int msgidx = 0, msglen = 1;
1470 do {
1471 message[msgidx] = acornscsi_readmessagebyte(host);
1473 switch (msgidx) {
1474 case 0:
1475 if (message[0] == EXTENDED_MESSAGE ||
1476 (message[0] >= 0x20 && message[0] <= 0x2f))
1477 msglen = 2;
1478 break;
1480 case 1:
1481 if (message[0] == EXTENDED_MESSAGE)
1482 msglen += message[msgidx];
1483 break;
1485 msgidx += 1;
1486 if (msgidx < msglen) {
1487 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1489 /* wait for next msg-in */
1490 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
1491 sbic_arm_read(host->scsi.io_port, SSR);
1493 } while (msgidx < msglen);
1495 #if (DEBUG & DEBUG_MESSAGES)
1496 printk("scsi%d.%c: message in: ",
1497 host->host->host_no, acornscsi_target(host));
1498 print_msg(message);
1499 printk("\n");
1500 #endif
1502 if (host->scsi.phase == PHASE_RECONNECTED) {
1504 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1505 * 'Whenever a target reconnects to an initiator to continue
1506 * a tagged I/O process, the SIMPLE QUEUE TAG message shall
1507 * be sent immediately following the IDENTIFY message...'
1509 if (message[0] == SIMPLE_QUEUE_TAG)
1510 host->scsi.reconnected.tag = message[1];
1511 if (acornscsi_reconnect_finish(host))
1512 host->scsi.phase = PHASE_MSGIN;
1515 switch (message[0]) {
1516 case ABORT:
1517 case ABORT_TAG:
1518 case COMMAND_COMPLETE:
1519 if (host->scsi.phase != PHASE_STATUSIN) {
1520 printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1521 host->host->host_no, acornscsi_target(host));
1522 acornscsi_dumplog(host, host->SCpnt->target);
1524 host->scsi.phase = PHASE_DONE;
1525 host->scsi.SCp.Message = message[0];
1526 break;
1528 case SAVE_POINTERS:
1530 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.20)
1531 * 'The SAVE DATA POINTER message is sent from a target to
1532 * direct the initiator to copy the active data pointer to
1533 * the saved data pointer for the current I/O process.
1535 acornscsi_dma_cleanup(host);
1536 host->SCpnt->SCp = host->scsi.SCp;
1537 host->SCpnt->SCp.sent_command = 0;
1538 host->scsi.phase = PHASE_MSGIN;
1539 break;
1541 case RESTORE_POINTERS:
1543 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.19)
1544 * 'The RESTORE POINTERS message is sent from a target to
1545 * direct the initiator to copy the most recently saved
1546 * command, data, and status pointers for the I/O process
1547 * to the corresponding active pointers. The command and
1548 * status pointers shall be restored to the beginning of
1549 * the present command and status areas.'
1551 acornscsi_dma_cleanup(host);
1552 host->scsi.SCp = host->SCpnt->SCp;
1553 host->scsi.phase = PHASE_MSGIN;
1554 break;
1556 case DISCONNECT:
1558 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 6.4.2)
1559 * 'On those occasions when an error or exception condition occurs
1560 * and the target elects to repeat the information transfer, the
1561 * target may repeat the transfer either issuing a RESTORE POINTERS
1562 * message or by disconnecting without issuing a SAVE POINTERS
1563 * message. When reconnection is completed, the most recent
1564 * saved pointer values are restored.'
1566 acornscsi_dma_cleanup(host);
1567 host->scsi.phase = PHASE_DISCONNECT;
1568 break;
1570 case MESSAGE_REJECT:
1571 #if 0 /* this isn't needed any more */
1573 * If we were negociating sync transfer, we don't yet know if
1574 * this REJECT is for the sync transfer or for the tagged queue/wide
1575 * transfer. Re-initiate sync transfer negociation now, and if
1576 * we got a REJECT in response to SDTR, then it'll be set to DONE.
1578 if (host->device[host->SCpnt->target].sync_state == SYNC_SENT_REQUEST)
1579 host->device[host->SCpnt->target].sync_state = SYNC_NEGOCIATE;
1580 #endif
1583 * If we have any messages waiting to go out, then assert ATN now
1585 if (msgqueue_msglength(&host->scsi.msgs))
1586 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1588 switch (host->scsi.last_message) {
1589 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1590 case HEAD_OF_QUEUE_TAG:
1591 case ORDERED_QUEUE_TAG:
1592 case SIMPLE_QUEUE_TAG:
1594 * ANSI standard says: (Section SCSI-2 Rev. 10c Sect 5.6.17)
1595 * If a target does not implement tagged queuing and a queue tag
1596 * message is received, it shall respond with a MESSAGE REJECT
1597 * message and accept the I/O process as if it were untagged.
1599 printk(KERN_NOTICE "scsi%d.%c: disabling tagged queueing\n",
1600 host->host->host_no, acornscsi_target(host));
1601 host->SCpnt->device->tagged_queue = 0;
1602 set_bit(host->SCpnt->target * 8 + host->SCpnt->lun, &host->busyluns);
1603 break;
1604 #endif
1605 case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1607 * Target can't handle synchronous transfers
1609 printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1610 host->host->host_no, acornscsi_target(host));
1611 host->device[host->SCpnt->target].sync_xfer = SYNCHTRANSFER_2DBA;
1612 host->device[host->SCpnt->target].sync_state = SYNC_ASYNCHRONOUS;
1613 sbic_arm_write(host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
1614 break;
1616 default:
1617 break;
1619 break;
1621 case QUEUE_FULL:
1622 /* TODO: target queue is full */
1623 break;
1625 case SIMPLE_QUEUE_TAG:
1626 /* tag queue reconnect... message[1] = queue tag. Print something to indicate something happened! */
1627 printk("scsi%d.%c: reconnect queue tag %02X\n",
1628 host->host->host_no, acornscsi_target(host),
1629 message[1]);
1630 break;
1632 case EXTENDED_MESSAGE:
1633 switch (message[2]) {
1634 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1635 case EXTENDED_SDTR:
1636 if (host->device[host->SCpnt->target].sync_state == SYNC_SENT_REQUEST) {
1638 * We requested synchronous transfers. This isn't quite right...
1639 * We can only say if this succeeded if we proceed on to execute the
1640 * command from this message. If we get a MESSAGE PARITY ERROR,
1641 * and the target retries fail, then we fallback to asynchronous mode
1643 host->device[host->SCpnt->target].sync_state = SYNC_COMPLETED;
1644 printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1645 host->host->host_no, acornscsi_target(host),
1646 message[4], message[3] * 4);
1647 host->device[host->SCpnt->target].sync_xfer =
1648 calc_sync_xfer(message[3] * 4, message[4]);
1649 } else {
1650 unsigned char period, length;
1652 * Target requested synchronous transfers. The agreement is only
1653 * to be in operation AFTER the target leaves message out phase.
1655 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1656 period = max(message[3], sdtr_period / 4);
1657 length = min(message[4], sdtr_size);
1658 msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1659 EXTENDED_SDTR, period, length);
1660 host->device[host->SCpnt->target].sync_xfer =
1661 calc_sync_xfer(period * 4, length);
1663 sbic_arm_write(host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
1664 break;
1665 #else
1666 /* We do not accept synchronous transfers. Respond with a
1667 * MESSAGE_REJECT.
1669 #endif
1671 case EXTENDED_WDTR:
1672 /* The WD33C93A is only 8-bit. We respond with a MESSAGE_REJECT
1673 * to a wide data transfer request.
1675 default:
1676 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1677 msgqueue_flush(&host->scsi.msgs);
1678 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1679 break;
1681 break;
1683 #ifdef CONFIG_SCSI_ACORNSCSI_LINK
1684 case LINKED_CMD_COMPLETE:
1685 case LINKED_FLG_CMD_COMPLETE:
1687 * We don't support linked commands yet
1689 if (0) {
1690 #if (DEBUG & DEBUG_LINK)
1691 printk("scsi%d.%c: lun %d tag %d linked command complete\n",
1692 host->host->host_no, acornscsi_target(host), host->SCpnt->tag);
1693 #endif
1695 * A linked command should only terminate with one of these messages
1696 * if there are more linked commands available.
1698 if (!host->SCpnt->next_link) {
1699 printk(KERN_WARNING "scsi%d.%c: lun %d tag %d linked command complete, but no next_link\n",
1700 instance->host_no, acornscsi_target(host), host->SCpnt->tag);
1701 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1702 msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1703 } else {
1704 Scsi_Cmnd *SCpnt = host->SCpnt;
1706 acornscsi_dma_cleanup(host);
1708 host->SCpnt = host->SCpnt->next_link;
1709 host->SCpnt->tag = SCpnt->tag;
1710 SCpnt->result = DID_OK | host->scsi.SCp.Message << 8 | host->Scsi.SCp.Status;
1711 SCpnt->done(SCpnt);
1713 /* initialise host->SCpnt->SCp */
1715 break;
1717 #endif
1719 default: /* reject message */
1720 printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1721 host->host->host_no, acornscsi_target(host),
1722 message[0]);
1723 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1724 msgqueue_flush(&host->scsi.msgs);
1725 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1726 host->scsi.phase = PHASE_MSGIN;
1727 break;
1729 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1733 * Function: int acornscsi_buildmessages(AS_Host *host)
1734 * Purpose : build the connection messages for a host
1735 * Params : host - host to add messages to
1737 static
1738 void acornscsi_buildmessages(AS_Host *host)
1740 #if 0
1741 /* does the device need resetting? */
1742 if (cmd_reset) {
1743 msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
1744 return;
1746 #endif
1748 msgqueue_addmsg(&host->scsi.msgs, 1,
1749 IDENTIFY(host->device[host->SCpnt->target].disconnect_ok,
1750 host->SCpnt->lun));
1752 #if 0
1753 /* does the device need the current command aborted */
1754 if (cmd_aborted) {
1755 acornscsi_abortcmd(host->SCpnt->tag);
1756 return;
1758 #endif
1760 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1761 if (host->SCpnt->tag) {
1762 unsigned int tag_type;
1764 if (host->SCpnt->cmnd[0] == REQUEST_SENSE ||
1765 host->SCpnt->cmnd[0] == TEST_UNIT_READY ||
1766 host->SCpnt->cmnd[0] == INQUIRY)
1767 tag_type = HEAD_OF_QUEUE_TAG;
1768 else
1769 tag_type = SIMPLE_QUEUE_TAG;
1770 msgqueue_addmsg(&host->scsi.msgs, 2, tag_type, host->SCpnt->tag);
1772 #endif
1774 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1775 if (host->device[host->SCpnt->target].sync_state == SYNC_NEGOCIATE) {
1776 host->device[host->SCpnt->target].sync_state = SYNC_SENT_REQUEST;
1777 msgqueue_addmsg(&host->scsi.msgs, 5,
1778 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1779 sdtr_period / 4, sdtr_size);
1781 #endif
1785 * Function: int acornscsi_starttransfer(AS_Host *host)
1786 * Purpose : transfer data to/from connected target
1787 * Params : host - host to which target is connected
1788 * Returns : 0 if failure
1790 static
1791 int acornscsi_starttransfer(AS_Host *host)
1793 int residual;
1795 if (!host->scsi.SCp.ptr /*&& host->scsi.SCp.this_residual*/) {
1796 printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1797 host->host->host_no, acornscsi_target(host));
1798 return 0;
1801 residual = host->SCpnt->request_bufflen - host->scsi.SCp.scsi_xferred;
1803 sbic_arm_write(host->scsi.io_port, SYNCHTRANSFER, host->device[host->SCpnt->target].sync_xfer);
1804 sbic_arm_writenext(host->scsi.io_port, residual >> 16);
1805 sbic_arm_writenext(host->scsi.io_port, residual >> 8);
1806 sbic_arm_writenext(host->scsi.io_port, residual);
1807 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1808 return 1;
1811 /* =========================================================================================
1812 * Connection & Disconnection
1815 * Function : acornscsi_reconnect(AS_Host *host)
1816 * Purpose : reconnect a previously disconnected command
1817 * Params : host - host specific data
1818 * Remarks : SCSI spec says:
1819 * 'The set of active pointers is restored from the set
1820 * of saved pointers upon reconnection of the I/O process'
1822 static
1823 int acornscsi_reconnect(AS_Host *host)
1825 unsigned int target, lun, ok = 0;
1827 target = sbic_arm_read(host->scsi.io_port, SOURCEID);
1829 if (!(target & 8))
1830 printk(KERN_ERR "scsi%d: invalid source id after reselection "
1831 "- device fault?\n",
1832 host->host->host_no);
1834 target &= 7;
1836 if (host->SCpnt && !host->scsi.disconnectable) {
1837 printk(KERN_ERR "scsi%d.%d: reconnected while command in "
1838 "progress to target %d?\n",
1839 host->host->host_no, target, host->SCpnt->target);
1840 host->SCpnt = NULL;
1843 lun = sbic_arm_read(host->scsi.io_port, DATA) & 7;
1845 host->scsi.reconnected.target = target;
1846 host->scsi.reconnected.lun = lun;
1847 host->scsi.reconnected.tag = 0;
1849 if (host->scsi.disconnectable && host->SCpnt &&
1850 host->SCpnt->target == target && host->SCpnt->lun == lun)
1851 ok = 1;
1853 if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
1854 ok = 1;
1856 ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1858 if (ok) {
1859 host->scsi.phase = PHASE_RECONNECTED;
1860 } else {
1861 /* this doesn't seem to work */
1862 printk(KERN_ERR "scsi%d.%c: reselected with no command "
1863 "to reconnect with\n",
1864 host->host->host_no, '0' + target);
1865 acornscsi_dumplog(host, target);
1866 acornscsi_abortcmd(host, 0);
1867 if (host->SCpnt) {
1868 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1869 host->SCpnt = NULL;
1872 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1873 return !ok;
1877 * Function: int acornscsi_reconect_finish(AS_Host *host)
1878 * Purpose : finish reconnecting a command
1879 * Params : host - host to complete
1880 * Returns : 0 if failed
1882 static
1883 int acornscsi_reconnect_finish(AS_Host *host)
1885 if (host->scsi.disconnectable && host->SCpnt) {
1886 host->scsi.disconnectable = 0;
1887 if (host->SCpnt->target == host->scsi.reconnected.target &&
1888 host->SCpnt->lun == host->scsi.reconnected.lun &&
1889 host->SCpnt->tag == host->scsi.reconnected.tag) {
1890 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1891 DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
1892 host->host->host_no, acornscsi_target(host)));
1893 #endif
1894 } else {
1895 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1896 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1897 DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
1898 "to disconnected queue\n",
1899 host->host->host_no, acornscsi_target(host)));
1900 #endif
1901 host->SCpnt = NULL;
1904 if (!host->SCpnt) {
1905 host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
1906 host->scsi.reconnected.target,
1907 host->scsi.reconnected.lun,
1908 host->scsi.reconnected.tag);
1909 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1910 DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
1911 host->host->host_no, acornscsi_target(host)));
1912 #endif
1915 if (!host->SCpnt)
1916 acornscsi_abortcmd(host, host->scsi.reconnected.tag);
1917 else {
1919 * Restore data pointer from SAVED pointers.
1921 host->scsi.SCp = host->SCpnt->SCp;
1922 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1923 printk(", data pointers: [%p, %X]",
1924 host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1925 #endif
1927 #if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1928 printk("\n");
1929 #endif
1931 host->dma.transferred = host->scsi.SCp.scsi_xferred;
1933 return host->SCpnt != NULL;
1937 * Function: void acornscsi_disconnect_unexpected(AS_Host *host)
1938 * Purpose : handle an unexpected disconnect
1939 * Params : host - host on which disconnect occurred
1941 static
1942 void acornscsi_disconnect_unexpected(AS_Host *host)
1944 printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1945 host->host->host_no, acornscsi_target(host));
1946 #if (DEBUG & DEBUG_ABORT)
1947 acornscsi_dumplog(host, 8);
1948 #endif
1950 acornscsi_done(host, &host->SCpnt, DID_ERROR);
1954 * Function: void acornscsi_abortcmd(AS_host *host, unsigned char tag)
1955 * Purpose : abort a currently executing command
1956 * Params : host - host with connected command to abort
1957 * tag - tag to abort
1959 static
1960 void acornscsi_abortcmd(AS_Host *host, unsigned char tag)
1962 host->scsi.phase = PHASE_ABORTED;
1963 sbic_arm_write(host->scsi.io_port, CMND, CMND_ASSERTATN);
1965 msgqueue_flush(&host->scsi.msgs);
1966 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
1967 if (tag)
1968 msgqueue_addmsg(&host->scsi.msgs, 2, ABORT_TAG, tag);
1969 else
1970 #endif
1971 msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1974 /* ==========================================================================================
1975 * Interrupt routines.
1978 * Function: int acornscsi_sbicintr(AS_Host *host)
1979 * Purpose : handle interrupts from SCSI device
1980 * Params : host - host to process
1981 * Returns : INTR_PROCESS if expecting another SBIC interrupt
1982 * INTR_IDLE if no interrupt
1983 * INTR_NEXT_COMMAND if we have finished processing the command
1985 static
1986 intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
1988 unsigned int asr, ssr;
1990 asr = sbic_arm_read(host->scsi.io_port, ASR);
1991 if (!(asr & ASR_INT))
1992 return INTR_IDLE;
1994 ssr = sbic_arm_read(host->scsi.io_port, SSR);
1996 #if (DEBUG & DEBUG_PHASES)
1997 print_sbic_status(asr, ssr, host->scsi.phase);
1998 #endif
2000 ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
2002 if (host->SCpnt && !host->scsi.disconnectable)
2003 ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, in_irq);
2005 switch (ssr) {
2006 case 0x00: /* reset state - not advanced */
2007 printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
2008 host->host->host_no);
2009 /* setup sbic - WD33C93A */
2010 sbic_arm_write(host->scsi.io_port, OWNID, OWNID_EAF | host->host->this_id);
2011 sbic_arm_write(host->scsi.io_port, CMND, CMND_RESET);
2012 return INTR_IDLE;
2014 case 0x01: /* reset state - advanced */
2015 sbic_arm_write(host->scsi.io_port, CTRL, INIT_SBICDMA | CTRL_IDI);
2016 sbic_arm_write(host->scsi.io_port, TIMEOUT, TIMEOUT_TIME);
2017 sbic_arm_write(host->scsi.io_port, SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
2018 sbic_arm_write(host->scsi.io_port, SOURCEID, SOURCEID_ER | SOURCEID_DSP);
2019 msgqueue_flush(&host->scsi.msgs);
2020 return INTR_IDLE;
2022 case 0x41: /* unexpected disconnect aborted command */
2023 acornscsi_disconnect_unexpected(host);
2024 return INTR_NEXT_COMMAND;
2027 switch (host->scsi.phase) {
2028 case PHASE_CONNECTING: /* STATE: command removed from issue queue */
2029 switch (ssr) {
2030 case 0x11: /* -> PHASE_CONNECTED */
2031 /* BUS FREE -> SELECTION */
2032 host->scsi.phase = PHASE_CONNECTED;
2033 msgqueue_flush(&host->scsi.msgs);
2034 host->dma.transferred = host->scsi.SCp.scsi_xferred;
2035 /* 33C93 gives next interrupt indicating bus phase */
2036 asr = sbic_arm_read(host->scsi.io_port, ASR);
2037 if (!(asr & ASR_INT))
2038 break;
2039 ssr = sbic_arm_read(host->scsi.io_port, SSR);
2040 ADD_STATUS(8, ssr, host->scsi.phase, 1);
2041 ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, 1);
2042 goto connected;
2044 case 0x42: /* select timed out */
2045 /* -> PHASE_IDLE */
2046 acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
2047 return INTR_NEXT_COMMAND;
2049 case 0x81: /* -> PHASE_RECONNECTED or PHASE_ABORTED */
2050 /* BUS FREE -> RESELECTION */
2051 host->origSCpnt = host->SCpnt;
2052 host->SCpnt = NULL;
2053 msgqueue_flush(&host->scsi.msgs);
2054 acornscsi_reconnect(host);
2055 break;
2057 default:
2058 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
2059 host->host->host_no, acornscsi_target(host), ssr);
2060 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2061 acornscsi_abortcmd(host, host->SCpnt->tag);
2063 return INTR_PROCESSING;
2065 connected:
2066 case PHASE_CONNECTED: /* STATE: device selected ok */
2067 switch (ssr) {
2068 #ifdef NONSTANDARD
2069 case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2070 /* SELECTION -> COMMAND */
2071 acornscsi_sendcommand(host);
2072 break;
2074 case 0x8b: /* -> PHASE_STATUS */
2075 /* SELECTION -> STATUS */
2076 acornscsi_readstatusbyte(host);
2077 host->scsi.phase = PHASE_STATUSIN;
2078 break;
2079 #endif
2081 case 0x8e: /* -> PHASE_MSGOUT */
2082 /* SELECTION ->MESSAGE OUT */
2083 host->scsi.phase = PHASE_MSGOUT;
2084 acornscsi_buildmessages(host);
2085 acornscsi_sendmessage(host);
2086 break;
2088 /* these should not happen */
2089 case 0x85: /* target disconnected */
2090 acornscsi_done(host, &host->SCpnt, DID_ERROR);
2091 break;
2093 default:
2094 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
2095 host->host->host_no, acornscsi_target(host), ssr);
2096 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2097 acornscsi_abortcmd(host, host->SCpnt->tag);
2099 return INTR_PROCESSING;
2101 case PHASE_MSGOUT: /* STATE: connected & sent IDENTIFY message */
2103 * SCSI standard says that MESSAGE OUT phases can be followed by a
2104 * DATA phase, STATUS phase, MESSAGE IN phase or COMMAND phase
2106 switch (ssr) {
2107 case 0x8a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2108 case 0x1a: /* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2109 /* MESSAGE OUT -> COMMAND */
2110 acornscsi_sendcommand(host);
2111 break;
2113 case 0x8b: /* -> PHASE_STATUS */
2114 case 0x1b: /* -> PHASE_STATUS */
2115 /* MESSAGE OUT -> STATUS */
2116 acornscsi_readstatusbyte(host);
2117 host->scsi.phase = PHASE_STATUSIN;
2118 break;
2120 case 0x8e: /* -> PHASE_MSGOUT */
2121 /* MESSAGE_OUT(MESSAGE_IN) ->MESSAGE OUT */
2122 acornscsi_sendmessage(host);
2123 break;
2125 case 0x4f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2126 case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2127 /* MESSAGE OUT -> MESSAGE IN */
2128 acornscsi_message(host);
2129 break;
2131 default:
2132 printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
2133 host->host->host_no, acornscsi_target(host), ssr);
2134 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2136 return INTR_PROCESSING;
2138 case PHASE_COMMAND: /* STATE: connected & command sent */
2139 switch (ssr) {
2140 case 0x18: /* -> PHASE_DATAOUT */
2141 /* COMMAND -> DATA OUT */
2142 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2143 acornscsi_abortcmd(host, host->SCpnt->tag);
2144 acornscsi_dma_setup(host, DMA_OUT);
2145 if (!acornscsi_starttransfer(host))
2146 acornscsi_abortcmd(host, host->SCpnt->tag);
2147 host->scsi.phase = PHASE_DATAOUT;
2148 return INTR_IDLE;
2150 case 0x19: /* -> PHASE_DATAIN */
2151 /* COMMAND -> DATA IN */
2152 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2153 acornscsi_abortcmd(host, host->SCpnt->tag);
2154 acornscsi_dma_setup(host, DMA_IN);
2155 if (!acornscsi_starttransfer(host))
2156 acornscsi_abortcmd(host, host->SCpnt->tag);
2157 host->scsi.phase = PHASE_DATAIN;
2158 return INTR_IDLE;
2160 case 0x1b: /* -> PHASE_STATUS */
2161 /* COMMAND -> STATUS */
2162 acornscsi_readstatusbyte(host);
2163 host->scsi.phase = PHASE_STATUSIN;
2164 break;
2166 case 0x1e: /* -> PHASE_MSGOUT */
2167 /* COMMAND -> MESSAGE OUT */
2168 acornscsi_sendmessage(host);
2169 break;
2171 case 0x1f: /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2172 /* COMMAND -> MESSAGE IN */
2173 acornscsi_message(host);
2174 break;
2176 default:
2177 printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2178 host->host->host_no, acornscsi_target(host), ssr);
2179 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2181 return INTR_PROCESSING;
2183 case PHASE_DISCONNECT: /* STATE: connected, received DISCONNECT msg */
2184 if (ssr == 0x85) { /* -> PHASE_IDLE */
2185 host->scsi.disconnectable = 1;
2186 host->scsi.reconnected.tag = 0;
2187 host->scsi.phase = PHASE_IDLE;
2188 host->stats.disconnects += 1;
2189 } else {
2190 printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2191 host->host->host_no, acornscsi_target(host), ssr);
2192 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2194 return INTR_NEXT_COMMAND;
2196 case PHASE_IDLE: /* STATE: disconnected */
2197 if (ssr == 0x81) /* -> PHASE_RECONNECTED or PHASE_ABORTED */
2198 acornscsi_reconnect(host);
2199 else {
2200 printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2201 host->host->host_no, acornscsi_target(host), ssr);
2202 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2204 return INTR_PROCESSING;
2206 case PHASE_RECONNECTED: /* STATE: device reconnected to initiator */
2208 * Command reconnected - if MESGIN, get message - it may be
2209 * the tag. If not, get command out of disconnected queue
2212 * If we reconnected and we're not in MESSAGE IN phase after IDENTIFY,
2213 * reconnect I_T_L command
2215 if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
2216 return INTR_IDLE;
2217 ADD_STATUS(host->SCpnt->target, ssr, host->scsi.phase, in_irq);
2218 switch (ssr) {
2219 case 0x88: /* data out phase */
2220 /* -> PHASE_DATAOUT */
2221 /* MESSAGE IN -> DATA OUT */
2222 acornscsi_dma_setup(host, DMA_OUT);
2223 if (!acornscsi_starttransfer(host))
2224 acornscsi_abortcmd(host, host->SCpnt->tag);
2225 host->scsi.phase = PHASE_DATAOUT;
2226 return INTR_IDLE;
2228 case 0x89: /* data in phase */
2229 /* -> PHASE_DATAIN */
2230 /* MESSAGE IN -> DATA IN */
2231 acornscsi_dma_setup(host, DMA_IN);
2232 if (!acornscsi_starttransfer(host))
2233 acornscsi_abortcmd(host, host->SCpnt->tag);
2234 host->scsi.phase = PHASE_DATAIN;
2235 return INTR_IDLE;
2237 case 0x8a: /* command out */
2238 /* MESSAGE IN -> COMMAND */
2239 acornscsi_sendcommand(host);/* -> PHASE_COMMAND, PHASE_COMMANDPAUSED */
2240 break;
2242 case 0x8b: /* status in */
2243 /* -> PHASE_STATUSIN */
2244 /* MESSAGE IN -> STATUS */
2245 acornscsi_readstatusbyte(host);
2246 host->scsi.phase = PHASE_STATUSIN;
2247 break;
2249 case 0x8e: /* message out */
2250 /* -> PHASE_MSGOUT */
2251 /* MESSAGE IN -> MESSAGE OUT */
2252 acornscsi_sendmessage(host);
2253 break;
2255 case 0x8f: /* message in */
2256 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2257 break;
2259 default:
2260 printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2261 host->host->host_no, acornscsi_target(host), ssr);
2262 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2264 return INTR_PROCESSING;
2266 case PHASE_DATAIN: /* STATE: transferred data in */
2268 * This is simple - if we disconnect then the DMA address & count is
2269 * correct.
2271 switch (ssr) {
2272 case 0x19: /* -> PHASE_DATAIN */
2273 case 0x89: /* -> PHASE_DATAIN */
2274 acornscsi_abortcmd(host, host->SCpnt->tag);
2275 return INTR_IDLE;
2277 case 0x1b: /* -> PHASE_STATUSIN */
2278 case 0x4b: /* -> PHASE_STATUSIN */
2279 case 0x8b: /* -> PHASE_STATUSIN */
2280 /* DATA IN -> STATUS */
2281 host->scsi.SCp.scsi_xferred = host->SCpnt->request_bufflen -
2282 acornscsi_sbic_xfcount(host);
2283 acornscsi_dma_stop(host);
2284 acornscsi_readstatusbyte(host);
2285 host->scsi.phase = PHASE_STATUSIN;
2286 break;
2288 case 0x1e: /* -> PHASE_MSGOUT */
2289 case 0x4e: /* -> PHASE_MSGOUT */
2290 case 0x8e: /* -> PHASE_MSGOUT */
2291 /* DATA IN -> MESSAGE OUT */
2292 host->scsi.SCp.scsi_xferred = host->SCpnt->request_bufflen -
2293 acornscsi_sbic_xfcount(host);
2294 acornscsi_dma_stop(host);
2295 acornscsi_sendmessage(host);
2296 break;
2298 case 0x1f: /* message in */
2299 case 0x4f: /* message in */
2300 case 0x8f: /* message in */
2301 /* DATA IN -> MESSAGE IN */
2302 host->scsi.SCp.scsi_xferred = host->SCpnt->request_bufflen -
2303 acornscsi_sbic_xfcount(host);
2304 acornscsi_dma_stop(host);
2305 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2306 break;
2308 default:
2309 printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2310 host->host->host_no, acornscsi_target(host), ssr);
2311 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2313 return INTR_PROCESSING;
2315 case PHASE_DATAOUT: /* STATE: transferred data out */
2317 * This is more complicated - if we disconnect, the DMA could be 12
2318 * bytes ahead of us. We need to correct this.
2320 switch (ssr) {
2321 case 0x18: /* -> PHASE_DATAOUT */
2322 case 0x88: /* -> PHASE_DATAOUT */
2323 acornscsi_abortcmd(host, host->SCpnt->tag);
2324 return INTR_IDLE;
2326 case 0x1b: /* -> PHASE_STATUSIN */
2327 case 0x4b: /* -> PHASE_STATUSIN */
2328 case 0x8b: /* -> PHASE_STATUSIN */
2329 /* DATA OUT -> STATUS */
2330 host->scsi.SCp.scsi_xferred = host->SCpnt->request_bufflen -
2331 acornscsi_sbic_xfcount(host);
2332 acornscsi_dma_stop(host);
2333 acornscsi_dma_adjust(host);
2334 acornscsi_readstatusbyte(host);
2335 host->scsi.phase = PHASE_STATUSIN;
2336 break;
2338 case 0x1e: /* -> PHASE_MSGOUT */
2339 case 0x4e: /* -> PHASE_MSGOUT */
2340 case 0x8e: /* -> PHASE_MSGOUT */
2341 /* DATA OUT -> MESSAGE OUT */
2342 host->scsi.SCp.scsi_xferred = host->SCpnt->request_bufflen -
2343 acornscsi_sbic_xfcount(host);
2344 acornscsi_dma_stop(host);
2345 acornscsi_dma_adjust(host);
2346 acornscsi_sendmessage(host);
2347 break;
2349 case 0x1f: /* message in */
2350 case 0x4f: /* message in */
2351 case 0x8f: /* message in */
2352 /* DATA OUT -> MESSAGE IN */
2353 host->scsi.SCp.scsi_xferred = host->SCpnt->request_bufflen -
2354 acornscsi_sbic_xfcount(host);
2355 acornscsi_dma_stop(host);
2356 acornscsi_dma_adjust(host);
2357 acornscsi_message(host); /* -> PHASE_MSGIN, PHASE_DISCONNECT */
2358 break;
2360 default:
2361 printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2362 host->host->host_no, acornscsi_target(host), ssr);
2363 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2365 return INTR_PROCESSING;
2367 case PHASE_STATUSIN: /* STATE: status in complete */
2368 switch (ssr) {
2369 case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2370 case 0x8f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2371 /* STATUS -> MESSAGE IN */
2372 acornscsi_message(host);
2373 break;
2375 case 0x1e: /* -> PHASE_MSGOUT */
2376 case 0x8e: /* -> PHASE_MSGOUT */
2377 /* STATUS -> MESSAGE OUT */
2378 acornscsi_sendmessage(host);
2379 break;
2381 default:
2382 printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2383 host->host->host_no, acornscsi_target(host), ssr);
2384 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2386 return INTR_PROCESSING;
2388 case PHASE_MSGIN: /* STATE: message in */
2389 switch (ssr) {
2390 case 0x1e: /* -> PHASE_MSGOUT */
2391 case 0x4e: /* -> PHASE_MSGOUT */
2392 case 0x8e: /* -> PHASE_MSGOUT */
2393 /* MESSAGE IN -> MESSAGE OUT */
2394 acornscsi_sendmessage(host);
2395 break;
2397 case 0x1f: /* -> PHASE_MSGIN, PHASE_DONE, PHASE_DISCONNECT */
2398 case 0x2f:
2399 case 0x4f:
2400 case 0x8f:
2401 acornscsi_message(host);
2402 break;
2404 case 0x85:
2405 printk("scsi%d.%c: strange message in disconnection\n",
2406 host->host->host_no, acornscsi_target(host));
2407 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2408 acornscsi_done(host, &host->SCpnt, DID_ERROR);
2409 break;
2411 default:
2412 printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2413 host->host->host_no, acornscsi_target(host), ssr);
2414 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2416 return INTR_PROCESSING;
2418 case PHASE_DONE: /* STATE: received status & message */
2419 switch (ssr) {
2420 case 0x85: /* -> PHASE_IDLE */
2421 acornscsi_done(host, &host->SCpnt, DID_OK);
2422 return INTR_NEXT_COMMAND;
2424 case 0x1e:
2425 case 0x8e:
2426 acornscsi_sendmessage(host);
2427 break;
2429 default:
2430 printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2431 host->host->host_no, acornscsi_target(host), ssr);
2432 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2434 return INTR_PROCESSING;
2436 case PHASE_ABORTED:
2437 switch (ssr) {
2438 case 0x85:
2439 if (host->SCpnt)
2440 acornscsi_done(host, &host->SCpnt, DID_ABORT);
2441 else {
2442 clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
2443 host->busyluns);
2444 host->scsi.phase = PHASE_IDLE;
2446 return INTR_NEXT_COMMAND;
2448 case 0x1e:
2449 case 0x2e:
2450 case 0x4e:
2451 case 0x8e:
2452 acornscsi_sendmessage(host);
2453 break;
2455 default:
2456 printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2457 host->host->host_no, acornscsi_target(host), ssr);
2458 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2460 return INTR_PROCESSING;
2462 default:
2463 printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2464 host->host->host_no, acornscsi_target(host), ssr);
2465 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->target : 8);
2467 return INTR_PROCESSING;
2471 * Prototype: void acornscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
2472 * Purpose : handle interrupts from Acorn SCSI card
2473 * Params : irq - interrupt number
2474 * dev_id - device specific data (AS_Host structure)
2475 * regs - processor registers when interrupt occurred
2477 static
2478 void acornscsi_intr(int irq, void *dev_id, struct pt_regs *regs)
2480 AS_Host *host = (AS_Host *)dev_id;
2481 intr_ret_t ret;
2482 int iostatus;
2483 int in_irq = 0;
2485 if (host->scsi.interrupt)
2486 printk("scsi%d: interrupt re-entered\n", host->host->host_no);
2487 host->scsi.interrupt = 1;
2489 do {
2490 ret = INTR_IDLE;
2492 iostatus = inb(host->card.io_intr);
2494 if (iostatus & 2) {
2495 acornscsi_dma_intr(host);
2496 iostatus = inb(host->card.io_intr);
2499 if (iostatus & 8)
2500 ret = acornscsi_sbicintr(host, in_irq);
2503 * If we have a transfer pending, start it.
2504 * Only start it if the interface has already started transferring
2505 * it's data
2507 if (host->dma.xfer_required)
2508 acornscsi_dma_xfer(host);
2510 if (ret == INTR_NEXT_COMMAND)
2511 ret = acornscsi_kick(host);
2513 in_irq = 1;
2514 } while (ret != INTR_IDLE);
2516 host->scsi.interrupt = 0;
2519 /*=============================================================================================
2520 * Interfaces between interrupt handler and rest of scsi code
2524 * Function : acornscsi_queuecmd(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
2525 * Purpose : queues a SCSI command
2526 * Params : cmd - SCSI command
2527 * done - function called on completion, with pointer to command descriptor
2528 * Returns : 0, or < 0 on error.
2530 int acornscsi_queuecmd(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2532 AS_Host *host = (AS_Host *)SCpnt->host->hostdata;
2534 if (!done) {
2535 /* there should be some way of rejecting errors like this without panicing... */
2536 panic("scsi%d: queuecommand called with NULL done function [cmd=%p]",
2537 SCpnt->host->host_no, SCpnt);
2538 return -EINVAL;
2541 #if (DEBUG & DEBUG_NO_WRITE)
2542 if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->target))) {
2543 printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2544 SCpnt->host->host_no, '0' + SCpnt->target);
2545 SCpnt->result = DID_NO_CONNECT << 16;
2546 done(SCpnt);
2547 return 0;
2549 #endif
2551 SCpnt->scsi_done = done;
2552 SCpnt->host_scribble = NULL;
2553 SCpnt->result = 0;
2554 SCpnt->tag = 0;
2555 SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
2556 SCpnt->SCp.sent_command = 0;
2557 SCpnt->SCp.scsi_xferred = 0;
2558 SCpnt->SCp.Status = 0;
2559 SCpnt->SCp.Message = 0;
2561 if (SCpnt->use_sg) {
2562 SCpnt->SCp.buffer = (struct scatterlist *) SCpnt->buffer;
2563 SCpnt->SCp.buffers_residual = SCpnt->use_sg - 1;
2564 SCpnt->SCp.ptr = (char *) SCpnt->SCp.buffer->address;
2565 SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
2566 } else {
2567 SCpnt->SCp.buffer = NULL;
2568 SCpnt->SCp.buffers_residual = 0;
2569 SCpnt->SCp.ptr = (char *) SCpnt->request_buffer;
2570 SCpnt->SCp.this_residual = SCpnt->request_bufflen;
2573 host->stats.queues += 1;
2576 unsigned long flags;
2578 if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
2579 SCpnt->result = DID_ERROR << 16;
2580 done(SCpnt);
2581 return 0;
2583 save_flags_cli(flags);
2584 if (host->scsi.phase == PHASE_IDLE)
2585 acornscsi_kick(host);
2586 restore_flags(flags);
2588 return 0;
2592 * Prototype: void acornscsi_reportstatus(Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
2593 * Purpose : pass a result to *SCpntp1, and check if *SCpntp1 = *SCpntp2
2594 * Params : SCpntp1 - pointer to command to return
2595 * SCpntp2 - pointer to command to check
2596 * result - result to pass back to mid-level done function
2597 * Returns : *SCpntp2 = NULL if *SCpntp1 is the same command structure as *SCpntp2.
2599 static inline
2600 void acornscsi_reportstatus(Scsi_Cmnd **SCpntp1, Scsi_Cmnd **SCpntp2, int result)
2602 Scsi_Cmnd *SCpnt = *SCpntp1;
2604 if (SCpnt) {
2605 *SCpntp1 = NULL;
2607 SCpnt->result = result;
2608 SCpnt->scsi_done(SCpnt);
2611 if (SCpnt == *SCpntp2)
2612 *SCpntp2 = NULL;
2615 enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
2618 * Prototype: enum res acornscsi_do_abort(Scsi_Cmnd *SCpnt)
2619 * Purpose : abort a command on this host
2620 * Params : SCpnt - command to abort
2621 * Returns : our abort status
2623 static enum res_abort
2624 acornscsi_do_abort(AS_Host *host, Scsi_Cmnd *SCpnt)
2626 enum res_abort res = res_not_running;
2628 if (queue_removecmd(&host->queues.issue, SCpnt)) {
2630 * The command was on the issue queue, and has not been
2631 * issued yet. We can remove the command from the queue,
2632 * and acknowledge the abort. Neither the devices nor the
2633 * interface know about the command.
2635 //#if (DEBUG & DEBUG_ABORT)
2636 printk("on issue queue ");
2637 //#endif
2638 res = res_success;
2639 } else if (queue_removecmd(&host->queues.disconnected, SCpnt)) {
2641 * The command was on the disconnected queue. Simply
2642 * acknowledge the abort condition, and when the target
2643 * reconnects, we will give it an ABORT message. The
2644 * target should then disconnect, and we will clear
2645 * the busylun bit.
2647 //#if (DEBUG & DEBUG_ABORT)
2648 printk("on disconnected queue ");
2649 //#endif
2650 res = res_success;
2651 } else if (host->SCpnt == SCpnt) {
2652 unsigned long flags;
2654 //#if (DEBUG & DEBUG_ABORT)
2655 printk("executing ");
2656 //#endif
2658 save_flags(flags);
2659 cli();
2660 switch (host->scsi.phase) {
2662 * If the interface is idle, and the command is 'disconnectable',
2663 * then it is the same as on the disconnected queue. We simply
2664 * remove all traces of the command. When the target reconnects,
2665 * we will give it an ABORT message since the command could not
2666 * be found. When the target finally disconnects, we will clear
2667 * the busylun bit.
2669 case PHASE_IDLE:
2670 if (host->scsi.disconnectable) {
2671 host->scsi.disconnectable = 0;
2672 host->SCpnt = NULL;
2673 res = res_success;
2675 break;
2678 * If the command has connected and done nothing further,
2679 * simply force a disconnect. We also need to clear the
2680 * busylun bit.
2682 case PHASE_CONNECTED:
2683 sbic_arm_write(host->scsi.io_port, CMND, CMND_DISCONNECT);
2684 host->SCpnt = NULL;
2685 res = res_success_clear;
2686 break;
2688 default:
2689 acornscsi_abortcmd(host, host->SCpnt->tag);
2690 res = res_snooze;
2692 restore_flags(flags);
2693 } else if (host->origSCpnt == SCpnt) {
2695 * The command will be executed next, but a command
2696 * is currently using the interface. This is similar to
2697 * being on the issue queue, except the busylun bit has
2698 * been set.
2700 host->origSCpnt = NULL;
2701 //#if (DEBUG & DEBUG_ABORT)
2702 printk("waiting for execution ");
2703 //#endif
2704 res = res_success_clear;
2705 } else
2706 printk("unknown ");
2708 return res;
2712 * Prototype: int acornscsi_abort(Scsi_Cmnd *SCpnt)
2713 * Purpose : abort a command on this host
2714 * Params : SCpnt - command to abort
2715 * Returns : one of SCSI_ABORT_ macros
2717 int acornscsi_abort(Scsi_Cmnd *SCpnt)
2719 AS_Host *host = (AS_Host *) SCpnt->host->hostdata;
2720 int result;
2722 host->stats.aborts += 1;
2724 #if (DEBUG & DEBUG_ABORT)
2726 int asr, ssr;
2727 asr = sbic_arm_read(host->scsi.io_port, ASR);
2728 ssr = sbic_arm_read(host->scsi.io_port, SSR);
2730 printk(KERN_WARNING "acornscsi_abort: ");
2731 print_sbic_status(asr, ssr, host->scsi.phase);
2732 acornscsi_dumplog(host, SCpnt->target);
2734 #endif
2736 printk("scsi%d: ", host->host->host_no);
2738 switch (acornscsi_do_abort(host, SCpnt)) {
2740 * We managed to find the command and cleared it out.
2741 * We do not expect the command to be executing on the
2742 * target, but we have set the busylun bit.
2744 case res_success_clear:
2745 //#if (DEBUG & DEBUG_ABORT)
2746 printk("clear ");
2747 //#endif
2748 clear_bit(SCpnt->target * 8 + SCpnt->lun, host->busyluns);
2751 * We found the command, and cleared it out. Either
2752 * the command is still known to be executing on the
2753 * target, or the busylun bit is not set.
2755 case res_success:
2756 //#if (DEBUG & DEBUG_ABORT)
2757 printk("success\n");
2758 //#endif
2759 SCpnt->result = DID_ABORT << 16;
2760 SCpnt->scsi_done(SCpnt);
2761 result = SCSI_ABORT_SUCCESS;
2762 break;
2765 * We did find the command, but unfortunately we couldn't
2766 * unhook it from ourselves. Wait some more, and if it
2767 * still doesn't complete, reset the interface.
2769 case res_snooze:
2770 //#if (DEBUG & DEBUG_ABORT)
2771 printk("snooze\n");
2772 //#endif
2773 result = SCSI_ABORT_SNOOZE;
2774 break;
2777 * The command could not be found (either because it completed,
2778 * or it got dropped.
2780 default:
2781 case res_not_running:
2782 acornscsi_dumplog(host, SCpnt->target);
2783 #if (DEBUG & DEBUG_ABORT)
2784 result = SCSI_ABORT_SNOOZE;
2785 #else
2786 result = SCSI_ABORT_NOT_RUNNING;
2787 #endif
2788 //#if (DEBUG & DEBUG_ABORT)
2789 printk("not running\n");
2790 //#endif
2791 break;
2794 return result;
2798 * Prototype: int acornscsi_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
2799 * Purpose : reset a command on this host/reset this host
2800 * Params : SCpnt - command causing reset
2801 * result - what type of reset to perform
2802 * Returns : one of SCSI_RESET_ macros
2804 int acornscsi_reset(Scsi_Cmnd *SCpnt, unsigned int reset_flags)
2806 AS_Host *host = (AS_Host *)SCpnt->host->hostdata;
2807 Scsi_Cmnd *SCptr;
2809 host->stats.resets += 1;
2811 #if (DEBUG & DEBUG_RESET)
2813 int asr, ssr;
2815 asr = sbic_arm_read(host->scsi.io_port, ASR);
2816 ssr = sbic_arm_read(host->scsi.io_port, SSR);
2818 printk(KERN_WARNING "acornscsi_reset: ");
2819 print_sbic_status(asr, ssr, host->scsi.phase);
2820 acornscsi_dumplog(host, SCpnt->target);
2822 #endif
2824 acornscsi_dma_stop(host);
2826 SCptr = host->SCpnt;
2829 * do hard reset. This resets all devices on this host, and so we
2830 * must set the reset status on all commands.
2832 acornscsi_resetcard(host);
2835 * report reset on commands current connected/disconnected
2837 acornscsi_reportstatus(&host->SCpnt, &SCptr, DID_RESET);
2839 while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
2840 acornscsi_reportstatus(&SCptr, &SCpnt, DID_RESET);
2842 if (SCpnt) {
2843 SCpnt->result = DID_RESET << 16;
2844 SCpnt->scsi_done(SCpnt);
2847 return SCSI_RESET_BUS_RESET | SCSI_RESET_HOST_RESET | SCSI_RESET_SUCCESS;
2850 /*==============================================================================================
2851 * initialisation & miscellaneous support
2853 static struct expansion_card *ecs[MAX_ECARDS];
2856 * Prototype: void acornscsi_init(AS_Host *host)
2857 * Purpose : initialise the AS_Host structure for one interface & setup hardware
2858 * Params : host - host to setup
2860 static
2861 void acornscsi_init(AS_Host *host)
2863 memset(&host->stats, 0, sizeof (host->stats));
2864 queue_initialise(&host->queues.issue);
2865 queue_initialise(&host->queues.disconnected);
2866 msgqueue_initialise(&host->scsi.msgs);
2868 acornscsi_resetcard(host);
2871 int acornscsi_detect(Scsi_Host_Template * tpnt)
2873 static const card_ids acornscsi_cids[] = { ACORNSCSI_LIST, { 0xffff, 0xffff } };
2874 int i, count = 0;
2875 struct Scsi_Host *instance;
2876 AS_Host *host;
2878 tpnt->proc_dir = &proc_scsi_acornscsi;
2880 for (i = 0; i < MAX_ECARDS; i++)
2881 ecs[i] = NULL;
2883 ecard_startfind();
2885 while(1) {
2886 ecs[count] = ecard_find(0, acornscsi_cids);
2887 if (!ecs[count])
2888 break;
2890 if (ecs[count]->irq == 0xff) {
2891 printk("scsi: WD33C93 does not have IRQ enabled - ignoring\n");
2892 continue;
2895 ecard_claim(ecs[count]); /* Must claim here - card produces irq on reset */
2897 instance = scsi_register(tpnt, sizeof(AS_Host));
2898 host = (AS_Host *)instance->hostdata;
2900 instance->io_port = ecard_address(ecs[count], ECARD_MEMC, 0);
2901 instance->irq = ecs[count]->irq;
2903 host->host = instance;
2904 host->scsi.io_port = ioaddr(instance->io_port + 0x800);
2905 host->scsi.irq = instance->irq;
2906 host->card.io_intr = POD_SPACE(instance->io_port) + 0x800;
2907 host->card.io_page = POD_SPACE(instance->io_port) + 0xc00;
2908 host->card.io_ram = ioaddr(instance->io_port);
2909 host->dma.io_port = instance->io_port + 0xc00;
2910 host->dma.io_intr_clear = POD_SPACE(instance->io_port) + 0x800;
2912 ecs[count]->irqaddr = (char *)ioaddr(host->card.io_intr);
2913 ecs[count]->irqmask = 0x0a;
2915 request_region(instance->io_port + 0x800, 2, "acornscsi(sbic)");
2916 request_region(host->card.io_intr, 1, "acornscsi(intr)");
2917 request_region(host->card.io_page, 1, "acornscsi(page)");
2918 #ifdef USE_DMAC
2919 request_region(host->dma.io_port, 256, "acornscsi(dmac)");
2920 #endif
2921 request_region(instance->io_port, 2048, "acornscsi(ram)");
2923 if (request_irq(host->scsi.irq, acornscsi_intr, SA_INTERRUPT, "acornscsi", host)) {
2924 printk(KERN_CRIT "scsi%d: IRQ%d not free, interrupts disabled\n",
2925 instance->host_no, host->scsi.irq);
2926 host->scsi.irq = NO_IRQ;
2929 acornscsi_init(host);
2931 ++count;
2933 return count;
2937 * Function: int acornscsi_release(struct Scsi_Host *host)
2938 * Purpose : release all resources used by this adapter
2939 * Params : host - driver structure to release
2940 * Returns : nothing of any consequence
2942 int acornscsi_release(struct Scsi_Host *instance)
2944 AS_Host *host = (AS_Host *)instance->hostdata;
2945 int i;
2948 * Put card into RESET state
2950 outb(0x80, host->card.io_page);
2952 if (host->scsi.irq != NO_IRQ)
2953 free_irq(host->scsi.irq, host);
2955 release_region(instance->io_port + 0x800, 2);
2956 release_region(host->card.io_intr, 1);
2957 release_region(host->card.io_page, 1);
2958 release_region(host->dma.io_port, 256);
2959 release_region(instance->io_port, 2048);
2961 for (i = 0; i < MAX_ECARDS; i++)
2962 if (ecs[i] && instance->io_port == ecard_address(ecs[i], ECARD_MEMC, 0))
2963 ecard_release(ecs[i]);
2965 msgqueue_free(&host->scsi.msgs);
2966 queue_free(&host->queues.disconnected);
2967 queue_free(&host->queues.issue);
2969 return 0;
2973 * Function: char *acornscsi_info(struct Scsi_Host *host)
2974 * Purpose : return a string describing this interface
2975 * Params : host - host to give information on
2976 * Returns : a constant string
2978 const
2979 char *acornscsi_info(struct Scsi_Host *host)
2981 static char string[100], *p;
2983 p = string;
2985 p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
2986 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2987 " SYNC"
2988 #endif
2989 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
2990 " TAG"
2991 #endif
2992 #ifdef CONFIG_SCSI_ACORNSCSI_LINK
2993 " LINK"
2994 #endif
2995 #if (DEBUG & DEBUG_NO_WRITE)
2996 " NOWRITE ("NO_WRITE_STR")"
2997 #endif
2998 , host->hostt->name, host->io_port, host->irq,
2999 VER_MAJOR, VER_MINOR, VER_PATCH);
3000 return string;
3003 int acornscsi_proc_info(char *buffer, char **start, off_t offset,
3004 int length, int host_no, int inout)
3006 int pos, begin = 0, devidx;
3007 struct Scsi_Host *instance = scsi_hostlist;
3008 Scsi_Device *scd;
3009 AS_Host *host;
3010 char *p = buffer;
3012 for (instance = scsi_hostlist;
3013 instance && instance->host_no != host_no;
3014 instance = instance->next);
3016 if (inout == 1 || !instance)
3017 return -EINVAL;
3019 host = (AS_Host *)instance->hostdata;
3021 p += sprintf(p, "AcornSCSI driver v%d.%d.%d"
3022 #ifdef CONFIG_SCSI_ACORNSCSI_SYNC
3023 " SYNC"
3024 #endif
3025 #ifdef CONFIG_SCSI_ACORNSCSI_TAGGED_QUEUE
3026 " TAG"
3027 #endif
3028 #ifdef CONFIG_SCSI_ACORNSCSI_LINK
3029 " LINK"
3030 #endif
3031 #if (DEBUG & DEBUG_NO_WRITE)
3032 " NOWRITE ("NO_WRITE_STR")"
3033 #endif
3034 "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
3036 p += sprintf(p, "SBIC: WD33C93A Address: %08X IRQ : %d\n",
3037 host->scsi.io_port, host->scsi.irq);
3038 #ifdef USE_DMAC
3039 p += sprintf(p, "DMAC: uPC71071 Address: %08X IRQ : %d\n\n",
3040 host->dma.io_port, host->scsi.irq);
3041 #endif
3043 p += sprintf(p, "Statistics:\n"
3044 "Queued commands: %-10u Issued commands: %-10u\n"
3045 "Done commands : %-10u Reads : %-10u\n"
3046 "Writes : %-10u Others : %-10u\n"
3047 "Disconnects : %-10u Aborts : %-10u\n"
3048 "Resets : %-10u\n\nLast phases:",
3049 host->stats.queues, host->stats.removes,
3050 host->stats.fins, host->stats.reads,
3051 host->stats.writes, host->stats.miscs,
3052 host->stats.disconnects, host->stats.aborts,
3053 host->stats.resets);
3055 for (devidx = 0; devidx < 9; devidx ++) {
3056 unsigned int statptr, prev;
3058 p += sprintf(p, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
3059 statptr = host->status_ptr[devidx] - 10;
3061 if ((signed int)statptr < 0)
3062 statptr += STATUS_BUFFER_SIZE;
3064 prev = host->status[devidx][statptr].when;
3066 for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
3067 if (host->status[devidx][statptr].when) {
3068 p += sprintf(p, "%c%02X:%02X+%2ld",
3069 host->status[devidx][statptr].irq ? '-' : ' ',
3070 host->status[devidx][statptr].ph,
3071 host->status[devidx][statptr].ssr,
3072 (host->status[devidx][statptr].when - prev) < 100 ?
3073 (host->status[devidx][statptr].when - prev) : 99);
3074 prev = host->status[devidx][statptr].when;
3079 p += sprintf(p, "\nAttached devices:%s\n", instance->host_queue ? "" : " none");
3081 for (scd = instance->host_queue; scd; scd = scd->next) {
3082 int len;
3084 proc_print_scsidevice(scd, p, &len, 0);
3085 p += len;
3087 p += sprintf(p, "Extensions: ");
3089 if (scd->tagged_supported)
3090 p += sprintf(p, "TAG %sabled [%d] ",
3091 scd->tagged_queue ? "en" : "dis", scd->current_tag);
3092 p += sprintf(p, "\nTransfers: ");
3093 if (host->device[scd->id].sync_xfer & 15)
3094 p += sprintf(p, "sync, offset %d, %d ns\n",
3095 host->device[scd->id].sync_xfer & 15,
3096 acornscsi_getperiod(host->device[scd->id].sync_xfer));
3097 else
3098 p += sprintf(p, "async\n");
3100 pos = p - buffer;
3101 if (pos + begin < offset) {
3102 begin += pos;
3103 p = buffer;
3105 pos = p - buffer;
3106 if (pos + begin > offset + length)
3107 break;
3110 pos = p - buffer;
3112 *start = buffer + (offset - begin);
3113 pos -= offset - begin;
3115 if (pos > length)
3116 pos = length;
3118 return pos;
3121 #ifdef MODULE
3123 Scsi_Host_Template driver_template = ACORNSCSI_3;
3125 #include "../../scsi/scsi_module.c"
3126 #endif