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