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