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 clear_bit(SCpnt->device->id * 8 +
845 (u8)(SCpnt->device->lun & 0x7), host->busyluns);
846
847 scsi_done(SCpnt);
848 } else
849 printk("scsi%d: null command in acornscsi_done", host->host->host_no);
850
851 host->scsi.phase = PHASE_IDLE;
852}
853
854
855
856
857
858
859
860
861static
862void acornscsi_data_updateptr(AS_Host *host, struct scsi_pointer *SCp, unsigned int length)
863{
864 SCp->ptr += length;
865 SCp->this_residual -= length;
866
867 if (SCp->this_residual == 0 && next_SCp(SCp) == 0)
868 host->dma.xfer_done = 1;
869}
870
871
872
873
874
875
876
877
878
879
880
881static
882void acornscsi_data_read(AS_Host *host, char *ptr,
883 unsigned int start_addr, unsigned int length)
884{
885 extern void __acornscsi_in(void __iomem *, char *buf, int len);
886 unsigned int page, offset, len = length;
887
888 page = (start_addr >> 12);
889 offset = start_addr & ((1 << 12) - 1);
890
891 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
892
893 while (len > 0) {
894 unsigned int this_len;
895
896 if (len + offset > (1 << 12))
897 this_len = (1 << 12) - offset;
898 else
899 this_len = len;
900
901 __acornscsi_in(host->base + (offset << 1), ptr, this_len);
902
903 offset += this_len;
904 ptr += this_len;
905 len -= this_len;
906
907 if (offset == (1 << 12)) {
908 offset = 0;
909 page ++;
910 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
911 }
912 }
913 writeb(host->card.page_reg, host->fast + PAGE_REG);
914}
915
916
917
918
919
920
921
922
923
924
925
926static
927void acornscsi_data_write(AS_Host *host, char *ptr,
928 unsigned int start_addr, unsigned int length)
929{
930 extern void __acornscsi_out(void __iomem *, char *buf, int len);
931 unsigned int page, offset, len = length;
932
933 page = (start_addr >> 12);
934 offset = start_addr & ((1 << 12) - 1);
935
936 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
937
938 while (len > 0) {
939 unsigned int this_len;
940
941 if (len + offset > (1 << 12))
942 this_len = (1 << 12) - offset;
943 else
944 this_len = len;
945
946 __acornscsi_out(host->base + (offset << 1), ptr, this_len);
947
948 offset += this_len;
949 ptr += this_len;
950 len -= this_len;
951
952 if (offset == (1 << 12)) {
953 offset = 0;
954 page ++;
955 writeb((page & 0x3f) | host->card.page_reg, host->fast + PAGE_REG);
956 }
957 }
958 writeb(host->card.page_reg, host->fast + PAGE_REG);
959}
960
961
962
963
964#ifdef USE_DMAC
965
966
967
968
969
970
971
972static inline
973void acornscsi_dma_stop(AS_Host *host)
974{
975 dmac_write(host, DMAC_MASKREG, MASK_ON);
976 dmac_clearintr(host);
977
978#if (DEBUG & DEBUG_DMA)
979 DBG(host->SCpnt, acornscsi_dumpdma(host, "stop"));
980#endif
981}
982
983
984
985
986
987
988
989
990
991static
992void acornscsi_dma_setup(AS_Host *host, dmadir_t direction)
993{
994 unsigned int address, length, mode;
995
996 host->dma.direction = direction;
997
998 dmac_write(host, DMAC_MASKREG, MASK_ON);
999
1000 if (direction == DMA_OUT) {
1001#if (DEBUG & DEBUG_NO_WRITE)
1002 if (NO_WRITE & (1 << host->SCpnt->device->id)) {
1003 printk(KERN_CRIT "scsi%d.%c: I can't handle DMA_OUT!\n",
1004 host->host->host_no, acornscsi_target(host));
1005 return;
1006 }
1007#endif
1008 mode = DMAC_WRITE;
1009 } else
1010 mode = DMAC_READ;
1011
1012
1013
1014
1015 length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1016 if (length) {
1017 host->dma.start_addr = address = host->dma.free_addr;
1018 host->dma.free_addr = (host->dma.free_addr + length) &
1019 (DMAC_BUFFER_SIZE - 1);
1020
1021
1022
1023
1024 if (direction == DMA_OUT)
1025 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1026 length);
1027
1028 length -= 1;
1029 dmac_write(host, DMAC_TXCNTLO, length);
1030 dmac_write(host, DMAC_TXCNTHI, length >> 8);
1031 dmac_write(host, DMAC_TXADRLO, address);
1032 dmac_write(host, DMAC_TXADRMD, address >> 8);
1033 dmac_write(host, DMAC_TXADRHI, 0);
1034 dmac_write(host, DMAC_MODECON, mode);
1035 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1036
1037#if (DEBUG & DEBUG_DMA)
1038 DBG(host->SCpnt, acornscsi_dumpdma(host, "strt"));
1039#endif
1040 host->dma.xfer_setup = 1;
1041 }
1042}
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052static
1053void acornscsi_dma_cleanup(AS_Host *host)
1054{
1055 dmac_write(host, DMAC_MASKREG, MASK_ON);
1056 dmac_clearintr(host);
1057
1058
1059
1060
1061 if (host->dma.xfer_required) {
1062 host->dma.xfer_required = 0;
1063 if (host->dma.direction == DMA_IN)
1064 acornscsi_data_read(host, host->dma.xfer_ptr,
1065 host->dma.xfer_start, host->dma.xfer_length);
1066 }
1067
1068
1069
1070
1071 if (host->dma.xfer_setup) {
1072 unsigned int transferred;
1073
1074 host->dma.xfer_setup = 0;
1075
1076#if (DEBUG & DEBUG_DMA)
1077 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupi"));
1078#endif
1079
1080
1081
1082
1083 transferred = dmac_address(host) - host->dma.start_addr;
1084 host->dma.transferred += transferred;
1085
1086 if (host->dma.direction == DMA_IN)
1087 acornscsi_data_read(host, host->scsi.SCp.ptr,
1088 host->dma.start_addr, transferred);
1089
1090
1091
1092
1093 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1094#if (DEBUG & DEBUG_DMA)
1095 DBG(host->SCpnt, acornscsi_dumpdma(host, "cupo"));
1096#endif
1097 }
1098}
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110static
1111void acornscsi_dma_intr(AS_Host *host)
1112{
1113 unsigned int address, length, transferred;
1114
1115#if (DEBUG & DEBUG_DMA)
1116 DBG(host->SCpnt, acornscsi_dumpdma(host, "inti"));
1117#endif
1118
1119 dmac_write(host, DMAC_MASKREG, MASK_ON);
1120 dmac_clearintr(host);
1121
1122
1123
1124
1125 transferred = dmac_address(host) - host->dma.start_addr;
1126 host->dma.transferred += transferred;
1127
1128
1129
1130
1131 if (host->dma.direction == DMA_IN) {
1132 host->dma.xfer_start = host->dma.start_addr;
1133 host->dma.xfer_length = transferred;
1134 host->dma.xfer_ptr = host->scsi.SCp.ptr;
1135 host->dma.xfer_required = 1;
1136 }
1137
1138 acornscsi_data_updateptr(host, &host->scsi.SCp, transferred);
1139
1140
1141
1142
1143 length = min_t(unsigned int, host->scsi.SCp.this_residual, DMAC_BUFFER_SIZE / 2);
1144 if (length) {
1145 host->dma.start_addr = address = host->dma.free_addr;
1146 host->dma.free_addr = (host->dma.free_addr + length) &
1147 (DMAC_BUFFER_SIZE - 1);
1148
1149
1150
1151
1152 if (host->dma.direction == DMA_OUT)
1153 acornscsi_data_write(host, host->scsi.SCp.ptr, host->dma.start_addr,
1154 length);
1155
1156 length -= 1;
1157 dmac_write(host, DMAC_TXCNTLO, length);
1158 dmac_write(host, DMAC_TXCNTHI, length >> 8);
1159 dmac_write(host, DMAC_TXADRLO, address);
1160 dmac_write(host, DMAC_TXADRMD, address >> 8);
1161 dmac_write(host, DMAC_TXADRHI, 0);
1162 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1163
1164#if (DEBUG & DEBUG_DMA)
1165 DBG(host->SCpnt, acornscsi_dumpdma(host, "into"));
1166#endif
1167 } else {
1168 host->dma.xfer_setup = 0;
1169#if 0
1170
1171
1172
1173
1174
1175
1176 if (dmac_read(host, DMAC_STATUS) & STATUS_RQ0) {
1177 acornscsi_abortcmd(host);
1178
1179 dmac_write(host, DMAC_TXCNTLO, 0);
1180 dmac_write(host, DMAC_TXCNTHI, 0);
1181 dmac_write(host, DMAC_TXADRLO, 0);
1182 dmac_write(host, DMAC_TXADRMD, 0);
1183 dmac_write(host, DMAC_TXADRHI, 0);
1184 dmac_write(host, DMAC_MASKREG, MASK_OFF);
1185 }
1186#endif
1187 }
1188}
1189
1190
1191
1192
1193
1194
1195static
1196void acornscsi_dma_xfer(AS_Host *host)
1197{
1198 host->dma.xfer_required = 0;
1199
1200 if (host->dma.direction == DMA_IN)
1201 acornscsi_data_read(host, host->dma.xfer_ptr,
1202 host->dma.xfer_start, host->dma.xfer_length);
1203}
1204
1205
1206
1207
1208
1209
1210
1211static
1212void acornscsi_dma_adjust(AS_Host *host)
1213{
1214 if (host->dma.xfer_setup) {
1215 signed long transferred;
1216#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1217 DBG(host->SCpnt, acornscsi_dumpdma(host, "adji"));
1218#endif
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230 transferred = host->scsi.SCp.scsi_xferred - host->dma.transferred;
1231 if (transferred < 0)
1232 printk("scsi%d.%c: Ack! DMA write correction %ld < 0!\n",
1233 host->host->host_no, acornscsi_target(host), transferred);
1234 else if (transferred == 0)
1235 host->dma.xfer_setup = 0;
1236 else {
1237 transferred += host->dma.start_addr;
1238 dmac_write(host, DMAC_TXADRLO, transferred);
1239 dmac_write(host, DMAC_TXADRMD, transferred >> 8);
1240 dmac_write(host, DMAC_TXADRHI, transferred >> 16);
1241#if (DEBUG & (DEBUG_DMA|DEBUG_WRITE))
1242 DBG(host->SCpnt, acornscsi_dumpdma(host, "adjo"));
1243#endif
1244 }
1245 }
1246}
1247#endif
1248
1249
1250
1251
1252static int
1253acornscsi_write_pio(AS_Host *host, char *bytes, int *ptr, int len, unsigned int max_timeout)
1254{
1255 unsigned int asr, timeout = max_timeout;
1256 int my_ptr = *ptr;
1257
1258 while (my_ptr < len) {
1259 asr = sbic_arm_read(host, SBIC_ASR);
1260
1261 if (asr & ASR_DBR) {
1262 timeout = max_timeout;
1263
1264 sbic_arm_write(host, SBIC_DATA, bytes[my_ptr++]);
1265 } else if (asr & ASR_INT)
1266 break;
1267 else if (--timeout == 0)
1268 break;
1269 udelay(1);
1270 }
1271
1272 *ptr = my_ptr;
1273
1274 return (timeout == 0) ? -1 : 0;
1275}
1276
1277
1278
1279
1280
1281
1282static void
1283acornscsi_sendcommand(AS_Host *host)
1284{
1285 struct scsi_cmnd *SCpnt = host->SCpnt;
1286
1287 sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1288 sbic_arm_writenext(host, 0);
1289 sbic_arm_writenext(host, SCpnt->cmd_len - host->scsi.SCp.sent_command);
1290
1291 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1292
1293 if (acornscsi_write_pio(host, SCpnt->cmnd,
1294 (int *)&host->scsi.SCp.sent_command, SCpnt->cmd_len, 1000000))
1295 printk("scsi%d: timeout while sending command\n", host->host->host_no);
1296
1297 host->scsi.phase = PHASE_COMMAND;
1298}
1299
1300static
1301void acornscsi_sendmessage(AS_Host *host)
1302{
1303 unsigned int message_length = msgqueue_msglength(&host->scsi.msgs);
1304 unsigned int msgnr;
1305 struct message *msg;
1306
1307#if (DEBUG & DEBUG_MESSAGES)
1308 printk("scsi%d.%c: sending message ",
1309 host->host->host_no, acornscsi_target(host));
1310#endif
1311
1312 switch (message_length) {
1313 case 0:
1314 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1315
1316 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 1");
1317
1318 sbic_arm_write(host, SBIC_DATA, NOP);
1319
1320 host->scsi.last_message = NOP;
1321#if (DEBUG & DEBUG_MESSAGES)
1322 printk("NOP");
1323#endif
1324 break;
1325
1326 case 1:
1327 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1328 msg = msgqueue_getmsg(&host->scsi.msgs, 0);
1329
1330 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "sending message 2");
1331
1332 sbic_arm_write(host, SBIC_DATA, msg->msg[0]);
1333
1334 host->scsi.last_message = msg->msg[0];
1335#if (DEBUG & DEBUG_MESSAGES)
1336 spi_print_msg(msg->msg);
1337#endif
1338 break;
1339
1340 default:
1341
1342
1343
1344
1345
1346
1347
1348
1349 sbic_arm_write(host, SBIC_TRANSCNTH, 0);
1350 sbic_arm_writenext(host, 0);
1351 sbic_arm_writenext(host, message_length);
1352 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1353
1354 msgnr = 0;
1355 while ((msg = msgqueue_getmsg(&host->scsi.msgs, msgnr++)) != NULL) {
1356 unsigned int i;
1357#if (DEBUG & DEBUG_MESSAGES)
1358 spi_print_msg(msg);
1359#endif
1360 i = 0;
1361 if (acornscsi_write_pio(host, msg->msg, &i, msg->length, 1000000))
1362 printk("scsi%d: timeout while sending message\n", host->host->host_no);
1363
1364 host->scsi.last_message = msg->msg[0];
1365 if (msg->msg[0] == EXTENDED_MESSAGE)
1366 host->scsi.last_message |= msg->msg[2] << 8;
1367
1368 if (i != msg->length)
1369 break;
1370 }
1371 break;
1372 }
1373#if (DEBUG & DEBUG_MESSAGES)
1374 printk("\n");
1375#endif
1376}
1377
1378
1379
1380
1381
1382
1383static
1384void acornscsi_readstatusbyte(AS_Host *host)
1385{
1386 acornscsi_sbic_issuecmd(host, CMND_XFERINFO|CMND_SBT);
1387 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "reading status byte");
1388 host->scsi.SCp.Status = sbic_arm_read(host, SBIC_DATA);
1389}
1390
1391
1392
1393
1394
1395
1396static
1397unsigned char acornscsi_readmessagebyte(AS_Host *host)
1398{
1399 unsigned char message;
1400
1401 acornscsi_sbic_issuecmd(host, CMND_XFERINFO | CMND_SBT);
1402
1403 acornscsi_sbic_wait(host, ASR_DBR, ASR_DBR, 1000, "for message byte");
1404
1405 message = sbic_arm_read(host, SBIC_DATA);
1406
1407
1408 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after message byte");
1409
1410 sbic_arm_read(host, SBIC_SSR);
1411
1412 return message;
1413}
1414
1415
1416
1417
1418
1419
1420static
1421void acornscsi_message(AS_Host *host)
1422{
1423 unsigned char message[16];
1424 unsigned int msgidx = 0, msglen = 1;
1425
1426 do {
1427 message[msgidx] = acornscsi_readmessagebyte(host);
1428
1429 switch (msgidx) {
1430 case 0:
1431 if (message[0] == EXTENDED_MESSAGE ||
1432 (message[0] >= 0x20 && message[0] <= 0x2f))
1433 msglen = 2;
1434 break;
1435
1436 case 1:
1437 if (message[0] == EXTENDED_MESSAGE)
1438 msglen += message[msgidx];
1439 break;
1440 }
1441 msgidx += 1;
1442 if (msgidx < msglen) {
1443 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1444
1445
1446 acornscsi_sbic_wait(host, ASR_INT, ASR_INT, 1000, "for interrupt after negate ack");
1447 sbic_arm_read(host, SBIC_SSR);
1448 }
1449 } while (msgidx < msglen);
1450
1451#if (DEBUG & DEBUG_MESSAGES)
1452 printk("scsi%d.%c: message in: ",
1453 host->host->host_no, acornscsi_target(host));
1454 spi_print_msg(message);
1455 printk("\n");
1456#endif
1457
1458 if (host->scsi.phase == PHASE_RECONNECTED) {
1459
1460
1461
1462
1463
1464
1465 if (message[0] == SIMPLE_QUEUE_TAG)
1466 host->scsi.reconnected.tag = message[1];
1467 if (acornscsi_reconnect_finish(host))
1468 host->scsi.phase = PHASE_MSGIN;
1469 }
1470
1471 switch (message[0]) {
1472 case ABORT_TASK_SET:
1473 case ABORT_TASK:
1474 case COMMAND_COMPLETE:
1475 if (host->scsi.phase != PHASE_STATUSIN) {
1476 printk(KERN_ERR "scsi%d.%c: command complete following non-status in phase?\n",
1477 host->host->host_no, acornscsi_target(host));
1478 acornscsi_dumplog(host, host->SCpnt->device->id);
1479 }
1480 host->scsi.phase = PHASE_DONE;
1481 host->scsi.SCp.Message = message[0];
1482 break;
1483
1484 case SAVE_POINTERS:
1485
1486
1487
1488
1489
1490
1491 acornscsi_dma_cleanup(host);
1492 host->SCpnt->SCp = host->scsi.SCp;
1493 host->SCpnt->SCp.sent_command = 0;
1494 host->scsi.phase = PHASE_MSGIN;
1495 break;
1496
1497 case RESTORE_POINTERS:
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507 acornscsi_dma_cleanup(host);
1508 host->scsi.SCp = host->SCpnt->SCp;
1509 host->scsi.phase = PHASE_MSGIN;
1510 break;
1511
1512 case DISCONNECT:
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522 acornscsi_dma_cleanup(host);
1523 host->scsi.phase = PHASE_DISCONNECT;
1524 break;
1525
1526 case MESSAGE_REJECT:
1527#if 0
1528
1529
1530
1531
1532
1533
1534 if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST)
1535 host->device[host->SCpnt->device->id].sync_state = SYNC_NEGOCIATE;
1536#endif
1537
1538
1539
1540
1541 if (msgqueue_msglength(&host->scsi.msgs))
1542 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1543
1544 switch (host->scsi.last_message) {
1545 case EXTENDED_MESSAGE | (EXTENDED_SDTR << 8):
1546
1547
1548
1549 printk(KERN_NOTICE "scsi%d.%c: Using asynchronous transfer\n",
1550 host->host->host_no, acornscsi_target(host));
1551 host->device[host->SCpnt->device->id].sync_xfer = SYNCHTRANSFER_2DBA;
1552 host->device[host->SCpnt->device->id].sync_state = SYNC_ASYNCHRONOUS;
1553 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1554 break;
1555
1556 default:
1557 break;
1558 }
1559 break;
1560
1561 case SIMPLE_QUEUE_TAG:
1562
1563 printk("scsi%d.%c: reconnect queue tag %02X\n",
1564 host->host->host_no, acornscsi_target(host),
1565 message[1]);
1566 break;
1567
1568 case EXTENDED_MESSAGE:
1569 switch (message[2]) {
1570#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1571 case EXTENDED_SDTR:
1572 if (host->device[host->SCpnt->device->id].sync_state == SYNC_SENT_REQUEST) {
1573
1574
1575
1576
1577
1578
1579 host->device[host->SCpnt->device->id].sync_state = SYNC_COMPLETED;
1580 printk(KERN_NOTICE "scsi%d.%c: Using synchronous transfer, offset %d, %d ns\n",
1581 host->host->host_no, acornscsi_target(host),
1582 message[4], message[3] * 4);
1583 host->device[host->SCpnt->device->id].sync_xfer =
1584 calc_sync_xfer(message[3] * 4, message[4]);
1585 } else {
1586 unsigned char period, length;
1587
1588
1589
1590
1591 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1592 period = max_t(unsigned int, message[3], sdtr_period / 4);
1593 length = min_t(unsigned int, message[4], sdtr_size);
1594 msgqueue_addmsg(&host->scsi.msgs, 5, EXTENDED_MESSAGE, 3,
1595 EXTENDED_SDTR, period, length);
1596 host->device[host->SCpnt->device->id].sync_xfer =
1597 calc_sync_xfer(period * 4, length);
1598 }
1599 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1600 break;
1601#else
1602
1603
1604
1605#endif
1606
1607 case EXTENDED_WDTR:
1608
1609
1610
1611 default:
1612 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1613 msgqueue_flush(&host->scsi.msgs);
1614 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1615 break;
1616 }
1617 break;
1618
1619 default:
1620 printk(KERN_ERR "scsi%d.%c: unrecognised message %02X, rejecting\n",
1621 host->host->host_no, acornscsi_target(host),
1622 message[0]);
1623 acornscsi_sbic_issuecmd(host, CMND_ASSERTATN);
1624 msgqueue_flush(&host->scsi.msgs);
1625 msgqueue_addmsg(&host->scsi.msgs, 1, MESSAGE_REJECT);
1626 host->scsi.phase = PHASE_MSGIN;
1627 break;
1628 }
1629 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1630}
1631
1632
1633
1634
1635
1636
1637static
1638void acornscsi_buildmessages(AS_Host *host)
1639{
1640#if 0
1641
1642 if (cmd_reset) {
1643 msgqueue_addmsg(&host->scsi.msgs, 1, BUS_DEVICE_RESET);
1644 return;
1645 }
1646#endif
1647
1648 msgqueue_addmsg(&host->scsi.msgs, 1,
1649 IDENTIFY(host->device[host->SCpnt->device->id].disconnect_ok,
1650 host->SCpnt->device->lun));
1651
1652#if 0
1653
1654 if (cmd_aborted) {
1655 acornscsi_abortcmd(host);
1656 return;
1657 }
1658#endif
1659
1660
1661#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
1662 if (host->device[host->SCpnt->device->id].sync_state == SYNC_NEGOCIATE) {
1663 host->device[host->SCpnt->device->id].sync_state = SYNC_SENT_REQUEST;
1664 msgqueue_addmsg(&host->scsi.msgs, 5,
1665 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1666 sdtr_period / 4, sdtr_size);
1667 }
1668#endif
1669}
1670
1671
1672
1673
1674
1675
1676
1677static
1678int acornscsi_starttransfer(AS_Host *host)
1679{
1680 int residual;
1681
1682 if (!host->scsi.SCp.ptr ) {
1683 printk(KERN_ERR "scsi%d.%c: null buffer passed to acornscsi_starttransfer\n",
1684 host->host->host_no, acornscsi_target(host));
1685 return 0;
1686 }
1687
1688 residual = scsi_bufflen(host->SCpnt) - host->scsi.SCp.scsi_xferred;
1689
1690 sbic_arm_write(host, SBIC_SYNCHTRANSFER, host->device[host->SCpnt->device->id].sync_xfer);
1691 sbic_arm_writenext(host, residual >> 16);
1692 sbic_arm_writenext(host, residual >> 8);
1693 sbic_arm_writenext(host, residual);
1694 acornscsi_sbic_issuecmd(host, CMND_XFERINFO);
1695 return 1;
1696}
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709static
1710int acornscsi_reconnect(AS_Host *host)
1711{
1712 unsigned int target, lun, ok = 0;
1713
1714 target = sbic_arm_read(host, SBIC_SOURCEID);
1715
1716 if (!(target & 8))
1717 printk(KERN_ERR "scsi%d: invalid source id after reselection "
1718 "- device fault?\n",
1719 host->host->host_no);
1720
1721 target &= 7;
1722
1723 if (host->SCpnt && !host->scsi.disconnectable) {
1724 printk(KERN_ERR "scsi%d.%d: reconnected while command in "
1725 "progress to target %d?\n",
1726 host->host->host_no, target, host->SCpnt->device->id);
1727 host->SCpnt = NULL;
1728 }
1729
1730 lun = sbic_arm_read(host, SBIC_DATA) & 7;
1731
1732 host->scsi.reconnected.target = target;
1733 host->scsi.reconnected.lun = lun;
1734 host->scsi.reconnected.tag = 0;
1735
1736 if (host->scsi.disconnectable && host->SCpnt &&
1737 host->SCpnt->device->id == target && host->SCpnt->device->lun == lun)
1738 ok = 1;
1739
1740 if (!ok && queue_probetgtlun(&host->queues.disconnected, target, lun))
1741 ok = 1;
1742
1743 ADD_STATUS(target, 0x81, host->scsi.phase, 0);
1744
1745 if (ok) {
1746 host->scsi.phase = PHASE_RECONNECTED;
1747 } else {
1748
1749 printk(KERN_ERR "scsi%d.%c: reselected with no command "
1750 "to reconnect with\n",
1751 host->host->host_no, '0' + target);
1752 acornscsi_dumplog(host, target);
1753 acornscsi_abortcmd(host);
1754 if (host->SCpnt) {
1755 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1756 host->SCpnt = NULL;
1757 }
1758 }
1759 acornscsi_sbic_issuecmd(host, CMND_NEGATEACK);
1760 return !ok;
1761}
1762
1763
1764
1765
1766
1767
1768
1769static
1770int acornscsi_reconnect_finish(AS_Host *host)
1771{
1772 if (host->scsi.disconnectable && host->SCpnt) {
1773 host->scsi.disconnectable = 0;
1774 if (host->SCpnt->device->id == host->scsi.reconnected.target &&
1775 host->SCpnt->device->lun == host->scsi.reconnected.lun &&
1776 scsi_cmd_to_rq(host->SCpnt)->tag == host->scsi.reconnected.tag) {
1777#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1778 DBG(host->SCpnt, printk("scsi%d.%c: reconnected",
1779 host->host->host_no, acornscsi_target(host)));
1780#endif
1781 } else {
1782 queue_add_cmd_tail(&host->queues.disconnected, host->SCpnt);
1783#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1784 DBG(host->SCpnt, printk("scsi%d.%c: had to move command "
1785 "to disconnected queue\n",
1786 host->host->host_no, acornscsi_target(host)));
1787#endif
1788 host->SCpnt = NULL;
1789 }
1790 }
1791 if (!host->SCpnt) {
1792 host->SCpnt = queue_remove_tgtluntag(&host->queues.disconnected,
1793 host->scsi.reconnected.target,
1794 host->scsi.reconnected.lun,
1795 host->scsi.reconnected.tag);
1796#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1797 DBG(host->SCpnt, printk("scsi%d.%c: had to get command",
1798 host->host->host_no, acornscsi_target(host)));
1799#endif
1800 }
1801
1802 if (!host->SCpnt)
1803 acornscsi_abortcmd(host);
1804 else {
1805
1806
1807
1808 host->scsi.SCp = host->SCpnt->SCp;
1809#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1810 printk(", data pointers: [%p, %X]",
1811 host->scsi.SCp.ptr, host->scsi.SCp.this_residual);
1812#endif
1813 }
1814#if (DEBUG & (DEBUG_QUEUES|DEBUG_DISCON))
1815 printk("\n");
1816#endif
1817
1818 host->dma.transferred = host->scsi.SCp.scsi_xferred;
1819
1820 return host->SCpnt != NULL;
1821}
1822
1823
1824
1825
1826
1827
1828static
1829void acornscsi_disconnect_unexpected(AS_Host *host)
1830{
1831 printk(KERN_ERR "scsi%d.%c: unexpected disconnect\n",
1832 host->host->host_no, acornscsi_target(host));
1833#if (DEBUG & DEBUG_ABORT)
1834 acornscsi_dumplog(host, 8);
1835#endif
1836
1837 acornscsi_done(host, &host->SCpnt, DID_ERROR);
1838}
1839
1840
1841
1842
1843
1844
1845static
1846void acornscsi_abortcmd(AS_Host *host)
1847{
1848 host->scsi.phase = PHASE_ABORTED;
1849 sbic_arm_write(host, SBIC_CMND, CMND_ASSERTATN);
1850
1851 msgqueue_flush(&host->scsi.msgs);
1852 msgqueue_addmsg(&host->scsi.msgs, 1, ABORT);
1853}
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866static
1867intr_ret_t acornscsi_sbicintr(AS_Host *host, int in_irq)
1868{
1869 unsigned int asr, ssr;
1870
1871 asr = sbic_arm_read(host, SBIC_ASR);
1872 if (!(asr & ASR_INT))
1873 return INTR_IDLE;
1874
1875 ssr = sbic_arm_read(host, SBIC_SSR);
1876
1877#if (DEBUG & DEBUG_PHASES)
1878 print_sbic_status(asr, ssr, host->scsi.phase);
1879#endif
1880
1881 ADD_STATUS(8, ssr, host->scsi.phase, in_irq);
1882
1883 if (host->SCpnt && !host->scsi.disconnectable)
1884 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
1885
1886 switch (ssr) {
1887 case 0x00:
1888 printk(KERN_ERR "scsi%d: reset in standard mode but wanted advanced mode.\n",
1889 host->host->host_no);
1890
1891 sbic_arm_write(host, SBIC_OWNID, OWNID_EAF | host->host->this_id);
1892 sbic_arm_write(host, SBIC_CMND, CMND_RESET);
1893 return INTR_IDLE;
1894
1895 case 0x01:
1896 sbic_arm_write(host, SBIC_CTRL, INIT_SBICDMA | CTRL_IDI);
1897 sbic_arm_write(host, SBIC_TIMEOUT, TIMEOUT_TIME);
1898 sbic_arm_write(host, SBIC_SYNCHTRANSFER, SYNCHTRANSFER_2DBA);
1899 sbic_arm_write(host, SBIC_SOURCEID, SOURCEID_ER | SOURCEID_DSP);
1900 msgqueue_flush(&host->scsi.msgs);
1901 return INTR_IDLE;
1902
1903 case 0x41:
1904 acornscsi_disconnect_unexpected(host);
1905 return INTR_NEXT_COMMAND;
1906 }
1907
1908 switch (host->scsi.phase) {
1909 case PHASE_CONNECTING:
1910 switch (ssr) {
1911 case 0x11:
1912
1913 host->scsi.phase = PHASE_CONNECTED;
1914 msgqueue_flush(&host->scsi.msgs);
1915 host->dma.transferred = host->scsi.SCp.scsi_xferred;
1916
1917 asr = sbic_arm_read(host, SBIC_ASR);
1918 if (!(asr & ASR_INT))
1919 break;
1920 ssr = sbic_arm_read(host, SBIC_SSR);
1921 ADD_STATUS(8, ssr, host->scsi.phase, 1);
1922 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, 1);
1923 goto connected;
1924
1925 case 0x42:
1926
1927 acornscsi_done(host, &host->SCpnt, DID_NO_CONNECT);
1928 return INTR_NEXT_COMMAND;
1929
1930 case 0x81:
1931
1932 host->origSCpnt = host->SCpnt;
1933 host->SCpnt = NULL;
1934 msgqueue_flush(&host->scsi.msgs);
1935 acornscsi_reconnect(host);
1936 break;
1937
1938 default:
1939 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTING, SSR %02X?\n",
1940 host->host->host_no, acornscsi_target(host), ssr);
1941 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
1942 acornscsi_abortcmd(host);
1943 }
1944 return INTR_PROCESSING;
1945
1946 connected:
1947 case PHASE_CONNECTED:
1948 switch (ssr) {
1949#ifdef NONSTANDARD
1950 case 0x8a:
1951
1952 acornscsi_sendcommand(host);
1953 break;
1954
1955 case 0x8b:
1956
1957 acornscsi_readstatusbyte(host);
1958 host->scsi.phase = PHASE_STATUSIN;
1959 break;
1960#endif
1961
1962 case 0x8e:
1963
1964 host->scsi.phase = PHASE_MSGOUT;
1965 acornscsi_buildmessages(host);
1966 acornscsi_sendmessage(host);
1967 break;
1968
1969
1970 case 0x85:
1971 acornscsi_done(host, &host->SCpnt, DID_ERROR);
1972 break;
1973
1974 default:
1975 printk(KERN_ERR "scsi%d.%c: PHASE_CONNECTED, SSR %02X?\n",
1976 host->host->host_no, acornscsi_target(host), ssr);
1977 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
1978 acornscsi_abortcmd(host);
1979 }
1980 return INTR_PROCESSING;
1981
1982 case PHASE_MSGOUT:
1983
1984
1985
1986
1987 switch (ssr) {
1988 case 0x8a:
1989 case 0x1a:
1990
1991 acornscsi_sendcommand(host);
1992 break;
1993
1994 case 0x8b:
1995 case 0x1b:
1996
1997 acornscsi_readstatusbyte(host);
1998 host->scsi.phase = PHASE_STATUSIN;
1999 break;
2000
2001 case 0x8e:
2002
2003 acornscsi_sendmessage(host);
2004 break;
2005
2006 case 0x4f:
2007 case 0x1f:
2008
2009 acornscsi_message(host);
2010 break;
2011
2012 default:
2013 printk(KERN_ERR "scsi%d.%c: PHASE_MSGOUT, SSR %02X?\n",
2014 host->host->host_no, acornscsi_target(host), ssr);
2015 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2016 }
2017 return INTR_PROCESSING;
2018
2019 case PHASE_COMMAND:
2020 switch (ssr) {
2021 case 0x18:
2022
2023 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2024 acornscsi_abortcmd(host);
2025 acornscsi_dma_setup(host, DMA_OUT);
2026 if (!acornscsi_starttransfer(host))
2027 acornscsi_abortcmd(host);
2028 host->scsi.phase = PHASE_DATAOUT;
2029 return INTR_IDLE;
2030
2031 case 0x19:
2032
2033 if (host->scsi.SCp.sent_command != host->SCpnt->cmd_len)
2034 acornscsi_abortcmd(host);
2035 acornscsi_dma_setup(host, DMA_IN);
2036 if (!acornscsi_starttransfer(host))
2037 acornscsi_abortcmd(host);
2038 host->scsi.phase = PHASE_DATAIN;
2039 return INTR_IDLE;
2040
2041 case 0x1b:
2042
2043 acornscsi_readstatusbyte(host);
2044 host->scsi.phase = PHASE_STATUSIN;
2045 break;
2046
2047 case 0x1e:
2048
2049 acornscsi_sendmessage(host);
2050 break;
2051
2052 case 0x1f:
2053
2054 acornscsi_message(host);
2055 break;
2056
2057 default:
2058 printk(KERN_ERR "scsi%d.%c: PHASE_COMMAND, SSR %02X?\n",
2059 host->host->host_no, acornscsi_target(host), ssr);
2060 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2061 }
2062 return INTR_PROCESSING;
2063
2064 case PHASE_DISCONNECT:
2065 if (ssr == 0x85) {
2066 host->scsi.disconnectable = 1;
2067 host->scsi.reconnected.tag = 0;
2068 host->scsi.phase = PHASE_IDLE;
2069 host->stats.disconnects += 1;
2070 } else {
2071 printk(KERN_ERR "scsi%d.%c: PHASE_DISCONNECT, SSR %02X instead of disconnect?\n",
2072 host->host->host_no, acornscsi_target(host), ssr);
2073 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2074 }
2075 return INTR_NEXT_COMMAND;
2076
2077 case PHASE_IDLE:
2078 if (ssr == 0x81)
2079 acornscsi_reconnect(host);
2080 else {
2081 printk(KERN_ERR "scsi%d.%c: PHASE_IDLE, SSR %02X while idle?\n",
2082 host->host->host_no, acornscsi_target(host), ssr);
2083 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2084 }
2085 return INTR_PROCESSING;
2086
2087 case PHASE_RECONNECTED:
2088
2089
2090
2091
2092
2093
2094
2095
2096 if (ssr != 0x8f && !acornscsi_reconnect_finish(host))
2097 return INTR_IDLE;
2098 ADD_STATUS(host->SCpnt->device->id, ssr, host->scsi.phase, in_irq);
2099 switch (ssr) {
2100 case 0x88:
2101
2102
2103 acornscsi_dma_setup(host, DMA_OUT);
2104 if (!acornscsi_starttransfer(host))
2105 acornscsi_abortcmd(host);
2106 host->scsi.phase = PHASE_DATAOUT;
2107 return INTR_IDLE;
2108
2109 case 0x89:
2110
2111
2112 acornscsi_dma_setup(host, DMA_IN);
2113 if (!acornscsi_starttransfer(host))
2114 acornscsi_abortcmd(host);
2115 host->scsi.phase = PHASE_DATAIN;
2116 return INTR_IDLE;
2117
2118 case 0x8a:
2119
2120 acornscsi_sendcommand(host);
2121 break;
2122
2123 case 0x8b:
2124
2125
2126 acornscsi_readstatusbyte(host);
2127 host->scsi.phase = PHASE_STATUSIN;
2128 break;
2129
2130 case 0x8e:
2131
2132
2133 acornscsi_sendmessage(host);
2134 break;
2135
2136 case 0x8f:
2137 acornscsi_message(host);
2138 break;
2139
2140 default:
2141 printk(KERN_ERR "scsi%d.%c: PHASE_RECONNECTED, SSR %02X after reconnect?\n",
2142 host->host->host_no, acornscsi_target(host), ssr);
2143 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2144 }
2145 return INTR_PROCESSING;
2146
2147 case PHASE_DATAIN:
2148
2149
2150
2151
2152 switch (ssr) {
2153 case 0x19:
2154 case 0x89:
2155 acornscsi_abortcmd(host);
2156 return INTR_IDLE;
2157
2158 case 0x1b:
2159 case 0x4b:
2160 case 0x8b:
2161
2162 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2163 acornscsi_sbic_xfcount(host);
2164 acornscsi_dma_stop(host);
2165 acornscsi_readstatusbyte(host);
2166 host->scsi.phase = PHASE_STATUSIN;
2167 break;
2168
2169 case 0x1e:
2170 case 0x4e:
2171 case 0x8e:
2172
2173 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2174 acornscsi_sbic_xfcount(host);
2175 acornscsi_dma_stop(host);
2176 acornscsi_sendmessage(host);
2177 break;
2178
2179 case 0x1f:
2180 case 0x4f:
2181 case 0x8f:
2182
2183 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2184 acornscsi_sbic_xfcount(host);
2185 acornscsi_dma_stop(host);
2186 acornscsi_message(host);
2187 break;
2188
2189 default:
2190 printk(KERN_ERR "scsi%d.%c: PHASE_DATAIN, SSR %02X?\n",
2191 host->host->host_no, acornscsi_target(host), ssr);
2192 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2193 }
2194 return INTR_PROCESSING;
2195
2196 case PHASE_DATAOUT:
2197
2198
2199
2200
2201 switch (ssr) {
2202 case 0x18:
2203 case 0x88:
2204 acornscsi_abortcmd(host);
2205 return INTR_IDLE;
2206
2207 case 0x1b:
2208 case 0x4b:
2209 case 0x8b:
2210
2211 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2212 acornscsi_sbic_xfcount(host);
2213 acornscsi_dma_stop(host);
2214 acornscsi_dma_adjust(host);
2215 acornscsi_readstatusbyte(host);
2216 host->scsi.phase = PHASE_STATUSIN;
2217 break;
2218
2219 case 0x1e:
2220 case 0x4e:
2221 case 0x8e:
2222
2223 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2224 acornscsi_sbic_xfcount(host);
2225 acornscsi_dma_stop(host);
2226 acornscsi_dma_adjust(host);
2227 acornscsi_sendmessage(host);
2228 break;
2229
2230 case 0x1f:
2231 case 0x4f:
2232 case 0x8f:
2233
2234 host->scsi.SCp.scsi_xferred = scsi_bufflen(host->SCpnt) -
2235 acornscsi_sbic_xfcount(host);
2236 acornscsi_dma_stop(host);
2237 acornscsi_dma_adjust(host);
2238 acornscsi_message(host);
2239 break;
2240
2241 default:
2242 printk(KERN_ERR "scsi%d.%c: PHASE_DATAOUT, SSR %02X?\n",
2243 host->host->host_no, acornscsi_target(host), ssr);
2244 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2245 }
2246 return INTR_PROCESSING;
2247
2248 case PHASE_STATUSIN:
2249 switch (ssr) {
2250 case 0x1f:
2251 case 0x8f:
2252
2253 acornscsi_message(host);
2254 break;
2255
2256 case 0x1e:
2257 case 0x8e:
2258
2259 acornscsi_sendmessage(host);
2260 break;
2261
2262 default:
2263 printk(KERN_ERR "scsi%d.%c: PHASE_STATUSIN, SSR %02X instead of MESSAGE_IN?\n",
2264 host->host->host_no, acornscsi_target(host), ssr);
2265 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2266 }
2267 return INTR_PROCESSING;
2268
2269 case PHASE_MSGIN:
2270 switch (ssr) {
2271 case 0x1e:
2272 case 0x4e:
2273 case 0x8e:
2274
2275 acornscsi_sendmessage(host);
2276 break;
2277
2278 case 0x1f:
2279 case 0x2f:
2280 case 0x4f:
2281 case 0x8f:
2282 acornscsi_message(host);
2283 break;
2284
2285 case 0x85:
2286 printk("scsi%d.%c: strange message in disconnection\n",
2287 host->host->host_no, acornscsi_target(host));
2288 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2289 acornscsi_done(host, &host->SCpnt, DID_ERROR);
2290 break;
2291
2292 default:
2293 printk(KERN_ERR "scsi%d.%c: PHASE_MSGIN, SSR %02X after message in?\n",
2294 host->host->host_no, acornscsi_target(host), ssr);
2295 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2296 }
2297 return INTR_PROCESSING;
2298
2299 case PHASE_DONE:
2300 switch (ssr) {
2301 case 0x85:
2302 acornscsi_done(host, &host->SCpnt, DID_OK);
2303 return INTR_NEXT_COMMAND;
2304
2305 case 0x1e:
2306 case 0x8e:
2307 acornscsi_sendmessage(host);
2308 break;
2309
2310 default:
2311 printk(KERN_ERR "scsi%d.%c: PHASE_DONE, SSR %02X instead of disconnect?\n",
2312 host->host->host_no, acornscsi_target(host), ssr);
2313 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2314 }
2315 return INTR_PROCESSING;
2316
2317 case PHASE_ABORTED:
2318 switch (ssr) {
2319 case 0x85:
2320 if (host->SCpnt)
2321 acornscsi_done(host, &host->SCpnt, DID_ABORT);
2322 else {
2323 clear_bit(host->scsi.reconnected.target * 8 + host->scsi.reconnected.lun,
2324 host->busyluns);
2325 host->scsi.phase = PHASE_IDLE;
2326 }
2327 return INTR_NEXT_COMMAND;
2328
2329 case 0x1e:
2330 case 0x2e:
2331 case 0x4e:
2332 case 0x8e:
2333 acornscsi_sendmessage(host);
2334 break;
2335
2336 default:
2337 printk(KERN_ERR "scsi%d.%c: PHASE_ABORTED, SSR %02X?\n",
2338 host->host->host_no, acornscsi_target(host), ssr);
2339 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2340 }
2341 return INTR_PROCESSING;
2342
2343 default:
2344 printk(KERN_ERR "scsi%d.%c: unknown driver phase %d\n",
2345 host->host->host_no, acornscsi_target(host), ssr);
2346 acornscsi_dumplog(host, host->SCpnt ? host->SCpnt->device->id : 8);
2347 }
2348 return INTR_PROCESSING;
2349}
2350
2351
2352
2353
2354
2355
2356
2357static irqreturn_t
2358acornscsi_intr(int irq, void *dev_id)
2359{
2360 AS_Host *host = (AS_Host *)dev_id;
2361 intr_ret_t ret;
2362 int iostatus;
2363 int in_irq = 0;
2364
2365 do {
2366 ret = INTR_IDLE;
2367
2368 iostatus = readb(host->fast + INT_REG);
2369
2370 if (iostatus & 2) {
2371 acornscsi_dma_intr(host);
2372 iostatus = readb(host->fast + INT_REG);
2373 }
2374
2375 if (iostatus & 8)
2376 ret = acornscsi_sbicintr(host, in_irq);
2377
2378
2379
2380
2381
2382
2383 if (host->dma.xfer_required)
2384 acornscsi_dma_xfer(host);
2385
2386 if (ret == INTR_NEXT_COMMAND)
2387 ret = acornscsi_kick(host);
2388
2389 in_irq = 1;
2390 } while (ret != INTR_IDLE);
2391
2392 return IRQ_HANDLED;
2393}
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405static int acornscsi_queuecmd_lck(struct scsi_cmnd *SCpnt)
2406{
2407 void (*done)(struct scsi_cmnd *) = scsi_done;
2408 AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2409
2410#if (DEBUG & DEBUG_NO_WRITE)
2411 if (acornscsi_cmdtype(SCpnt->cmnd[0]) == CMD_WRITE && (NO_WRITE & (1 << SCpnt->device->id))) {
2412 printk(KERN_CRIT "scsi%d.%c: WRITE attempted with NO_WRITE flag set\n",
2413 host->host->host_no, '0' + SCpnt->device->id);
2414 set_host_byte(SCpnt, DID_NO_CONNECT);
2415 done(SCpnt);
2416 return 0;
2417 }
2418#endif
2419
2420 SCpnt->host_scribble = NULL;
2421 SCpnt->result = 0;
2422 SCpnt->SCp.phase = (int)acornscsi_datadirection(SCpnt->cmnd[0]);
2423 SCpnt->SCp.sent_command = 0;
2424 SCpnt->SCp.scsi_xferred = 0;
2425
2426 init_SCp(SCpnt);
2427
2428 host->stats.queues += 1;
2429
2430 {
2431 unsigned long flags;
2432
2433 if (!queue_add_cmd_ordered(&host->queues.issue, SCpnt)) {
2434 set_host_byte(SCpnt, DID_ERROR);
2435 done(SCpnt);
2436 return 0;
2437 }
2438 local_irq_save(flags);
2439 if (host->scsi.phase == PHASE_IDLE)
2440 acornscsi_kick(host);
2441 local_irq_restore(flags);
2442 }
2443 return 0;
2444}
2445
2446DEF_SCSI_QCMD(acornscsi_queuecmd)
2447
2448enum res_abort { res_not_running, res_success, res_success_clear, res_snooze };
2449
2450
2451
2452
2453
2454
2455
2456static enum res_abort acornscsi_do_abort(AS_Host *host, struct scsi_cmnd *SCpnt)
2457{
2458 enum res_abort res = res_not_running;
2459
2460 if (queue_remove_cmd(&host->queues.issue, SCpnt)) {
2461
2462
2463
2464
2465
2466
2467
2468 printk("on issue queue ");
2469
2470 res = res_success;
2471 } else if (queue_remove_cmd(&host->queues.disconnected, SCpnt)) {
2472
2473
2474
2475
2476
2477
2478
2479
2480 printk("on disconnected queue ");
2481
2482 res = res_success;
2483 } else if (host->SCpnt == SCpnt) {
2484 unsigned long flags;
2485
2486
2487 printk("executing ");
2488
2489
2490 local_irq_save(flags);
2491 switch (host->scsi.phase) {
2492
2493
2494
2495
2496
2497
2498
2499
2500 case PHASE_IDLE:
2501 if (host->scsi.disconnectable) {
2502 host->scsi.disconnectable = 0;
2503 host->SCpnt = NULL;
2504 res = res_success;
2505 }
2506 break;
2507
2508
2509
2510
2511
2512
2513 case PHASE_CONNECTED:
2514 sbic_arm_write(host, SBIC_CMND, CMND_DISCONNECT);
2515 host->SCpnt = NULL;
2516 res = res_success_clear;
2517 break;
2518
2519 default:
2520 acornscsi_abortcmd(host);
2521 res = res_snooze;
2522 }
2523 local_irq_restore(flags);
2524 } else if (host->origSCpnt == SCpnt) {
2525
2526
2527
2528
2529
2530
2531 host->origSCpnt = NULL;
2532
2533 printk("waiting for execution ");
2534
2535 res = res_success_clear;
2536 } else
2537 printk("unknown ");
2538
2539 return res;
2540}
2541
2542
2543
2544
2545
2546
2547
2548int acornscsi_abort(struct scsi_cmnd *SCpnt)
2549{
2550 AS_Host *host = (AS_Host *) SCpnt->device->host->hostdata;
2551 int result;
2552
2553 host->stats.aborts += 1;
2554
2555#if (DEBUG & DEBUG_ABORT)
2556 {
2557 int asr, ssr;
2558 asr = sbic_arm_read(host, SBIC_ASR);
2559 ssr = sbic_arm_read(host, SBIC_SSR);
2560
2561 printk(KERN_WARNING "acornscsi_abort: ");
2562 print_sbic_status(asr, ssr, host->scsi.phase);
2563 acornscsi_dumplog(host, SCpnt->device->id);
2564 }
2565#endif
2566
2567 printk("scsi%d: ", host->host->host_no);
2568
2569 switch (acornscsi_do_abort(host, SCpnt)) {
2570
2571
2572
2573
2574
2575 case res_success_clear:
2576
2577 printk("clear ");
2578
2579 clear_bit(SCpnt->device->id * 8 +
2580 (u8)(SCpnt->device->lun & 0x7), host->busyluns);
2581 fallthrough;
2582
2583
2584
2585
2586
2587
2588 case res_success:
2589
2590 printk("success\n");
2591
2592 result = SUCCESS;
2593 break;
2594
2595
2596
2597
2598
2599
2600 case res_snooze:
2601
2602 printk("snooze\n");
2603
2604 result = FAILED;
2605 break;
2606
2607
2608
2609
2610
2611 default:
2612 case res_not_running:
2613 acornscsi_dumplog(host, SCpnt->device->id);
2614 result = FAILED;
2615
2616 printk("not running\n");
2617
2618 break;
2619 }
2620
2621 return result;
2622}
2623
2624
2625
2626
2627
2628
2629
2630int acornscsi_host_reset(struct scsi_cmnd *SCpnt)
2631{
2632 AS_Host *host = (AS_Host *)SCpnt->device->host->hostdata;
2633 struct scsi_cmnd *SCptr;
2634
2635 host->stats.resets += 1;
2636
2637#if (DEBUG & DEBUG_RESET)
2638 {
2639 int asr, ssr, devidx;
2640
2641 asr = sbic_arm_read(host, SBIC_ASR);
2642 ssr = sbic_arm_read(host, SBIC_SSR);
2643
2644 printk(KERN_WARNING "acornscsi_reset: ");
2645 print_sbic_status(asr, ssr, host->scsi.phase);
2646 for (devidx = 0; devidx < 9; devidx++)
2647 acornscsi_dumplog(host, devidx);
2648 }
2649#endif
2650
2651 acornscsi_dma_stop(host);
2652
2653
2654
2655
2656
2657 acornscsi_resetcard(host);
2658
2659 while ((SCptr = queue_remove(&host->queues.disconnected)) != NULL)
2660 ;
2661
2662 return SUCCESS;
2663}
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675const
2676char *acornscsi_info(struct Scsi_Host *host)
2677{
2678 static char string[100], *p;
2679
2680 p = string;
2681
2682 p += sprintf(string, "%s at port %08lX irq %d v%d.%d.%d"
2683#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2684 " SYNC"
2685#endif
2686#if (DEBUG & DEBUG_NO_WRITE)
2687 " NOWRITE (" __stringify(NO_WRITE) ")"
2688#endif
2689 , host->hostt->name, host->io_port, host->irq,
2690 VER_MAJOR, VER_MINOR, VER_PATCH);
2691 return string;
2692}
2693
2694static int acornscsi_show_info(struct seq_file *m, struct Scsi_Host *instance)
2695{
2696 int devidx;
2697 struct scsi_device *scd;
2698 AS_Host *host;
2699
2700 host = (AS_Host *)instance->hostdata;
2701
2702 seq_printf(m, "AcornSCSI driver v%d.%d.%d"
2703#ifdef CONFIG_SCSI_ACORNSCSI_SYNC
2704 " SYNC"
2705#endif
2706#if (DEBUG & DEBUG_NO_WRITE)
2707 " NOWRITE (" __stringify(NO_WRITE) ")"
2708#endif
2709 "\n\n", VER_MAJOR, VER_MINOR, VER_PATCH);
2710
2711 seq_printf(m, "SBIC: WD33C93A Address: %p IRQ : %d\n",
2712 host->base + SBIC_REGIDX, host->scsi.irq);
2713#ifdef USE_DMAC
2714 seq_printf(m, "DMAC: uPC71071 Address: %p IRQ : %d\n\n",
2715 host->base + DMAC_OFFSET, host->scsi.irq);
2716#endif
2717
2718 seq_printf(m, "Statistics:\n"
2719 "Queued commands: %-10u Issued commands: %-10u\n"
2720 "Done commands : %-10u Reads : %-10u\n"
2721 "Writes : %-10u Others : %-10u\n"
2722 "Disconnects : %-10u Aborts : %-10u\n"
2723 "Resets : %-10u\n\nLast phases:",
2724 host->stats.queues, host->stats.removes,
2725 host->stats.fins, host->stats.reads,
2726 host->stats.writes, host->stats.miscs,
2727 host->stats.disconnects, host->stats.aborts,
2728 host->stats.resets);
2729
2730 for (devidx = 0; devidx < 9; devidx ++) {
2731 unsigned int statptr, prev;
2732
2733 seq_printf(m, "\n%c:", devidx == 8 ? 'H' : ('0' + devidx));
2734 statptr = host->status_ptr[devidx] - 10;
2735
2736 if ((signed int)statptr < 0)
2737 statptr += STATUS_BUFFER_SIZE;
2738
2739 prev = host->status[devidx][statptr].when;
2740
2741 for (; statptr != host->status_ptr[devidx]; statptr = (statptr + 1) & (STATUS_BUFFER_SIZE - 1)) {
2742 if (host->status[devidx][statptr].when) {
2743 seq_printf(m, "%c%02X:%02X+%2ld",
2744 host->status[devidx][statptr].irq ? '-' : ' ',
2745 host->status[devidx][statptr].ph,
2746 host->status[devidx][statptr].ssr,
2747 (host->status[devidx][statptr].when - prev) < 100 ?
2748 (host->status[devidx][statptr].when - prev) : 99);
2749 prev = host->status[devidx][statptr].when;
2750 }
2751 }
2752 }
2753
2754 seq_printf(m, "\nAttached devices:\n");
2755
2756 shost_for_each_device(scd, instance) {
2757 seq_printf(m, "Device/Lun TaggedQ Sync\n");
2758 seq_printf(m, " %d/%llu ", scd->id, scd->lun);
2759 if (scd->tagged_supported)
2760 seq_printf(m, "%3sabled ",
2761 scd->simple_tags ? "en" : "dis");
2762 else
2763 seq_printf(m, "unsupported ");
2764
2765 if (host->device[scd->id].sync_xfer & 15)
2766 seq_printf(m, "offset %d, %d ns\n",
2767 host->device[scd->id].sync_xfer & 15,
2768 acornscsi_getperiod(host->device[scd->id].sync_xfer));
2769 else
2770 seq_printf(m, "async\n");
2771
2772 }
2773 return 0;
2774}
2775
2776static struct scsi_host_template acornscsi_template = {
2777 .module = THIS_MODULE,
2778 .show_info = acornscsi_show_info,
2779 .name = "AcornSCSI",
2780 .info = acornscsi_info,
2781 .queuecommand = acornscsi_queuecmd,
2782 .eh_abort_handler = acornscsi_abort,
2783 .eh_host_reset_handler = acornscsi_host_reset,
2784 .can_queue = 16,
2785 .this_id = 7,
2786 .sg_tablesize = SG_ALL,
2787 .cmd_per_lun = 2,
2788 .dma_boundary = PAGE_SIZE - 1,
2789 .proc_name = "acornscsi",
2790};
2791
2792static int acornscsi_probe(struct expansion_card *ec, const struct ecard_id *id)
2793{
2794 struct Scsi_Host *host;
2795 AS_Host *ashost;
2796 int ret;
2797
2798 ret = ecard_request_resources(ec);
2799 if (ret)
2800 goto out;
2801
2802 host = scsi_host_alloc(&acornscsi_template, sizeof(AS_Host));
2803 if (!host) {
2804 ret = -ENOMEM;
2805 goto out_release;
2806 }
2807
2808 ashost = (AS_Host *)host->hostdata;
2809
2810 ashost->base = ecardm_iomap(ec, ECARD_RES_MEMC, 0, 0);
2811 ashost->fast = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
2812 if (!ashost->base || !ashost->fast) {
2813 ret = -ENOMEM;
2814 goto out_put;
2815 }
2816
2817 host->irq = ec->irq;
2818 ashost->host = host;
2819 ashost->scsi.irq = host->irq;
2820
2821 ec->irqaddr = ashost->fast + INT_REG;
2822 ec->irqmask = 0x0a;
2823
2824 ret = request_irq(host->irq, acornscsi_intr, 0, "acornscsi", ashost);
2825 if (ret) {
2826 printk(KERN_CRIT "scsi%d: IRQ%d not free: %d\n",
2827 host->host_no, ashost->scsi.irq, ret);
2828 goto out_put;
2829 }
2830
2831 memset(&ashost->stats, 0, sizeof (ashost->stats));
2832 queue_initialise(&ashost->queues.issue);
2833 queue_initialise(&ashost->queues.disconnected);
2834 msgqueue_initialise(&ashost->scsi.msgs);
2835
2836 acornscsi_resetcard(ashost);
2837
2838 ret = scsi_add_host(host, &ec->dev);
2839 if (ret)
2840 goto out_irq;
2841
2842 scsi_scan_host(host);
2843 goto out;
2844
2845 out_irq:
2846 free_irq(host->irq, ashost);
2847 msgqueue_free(&ashost->scsi.msgs);
2848 queue_free(&ashost->queues.disconnected);
2849 queue_free(&ashost->queues.issue);
2850 out_put:
2851 ecardm_iounmap(ec, ashost->fast);
2852 ecardm_iounmap(ec, ashost->base);
2853 scsi_host_put(host);
2854 out_release:
2855 ecard_release_resources(ec);
2856 out:
2857 return ret;
2858}
2859
2860static void acornscsi_remove(struct expansion_card *ec)
2861{
2862 struct Scsi_Host *host = ecard_get_drvdata(ec);
2863 AS_Host *ashost = (AS_Host *)host->hostdata;
2864
2865 ecard_set_drvdata(ec, NULL);
2866 scsi_remove_host(host);
2867
2868
2869
2870
2871 writeb(0x80, ashost->fast + PAGE_REG);
2872
2873 free_irq(host->irq, ashost);
2874
2875 msgqueue_free(&ashost->scsi.msgs);
2876 queue_free(&ashost->queues.disconnected);
2877 queue_free(&ashost->queues.issue);
2878 ecardm_iounmap(ec, ashost->fast);
2879 ecardm_iounmap(ec, ashost->base);
2880 scsi_host_put(host);
2881 ecard_release_resources(ec);
2882}
2883
2884static const struct ecard_id acornscsi_cids[] = {
2885 { MANU_ACORN, PROD_ACORN_SCSI },
2886 { 0xffff, 0xffff },
2887};
2888
2889static struct ecard_driver acornscsi_driver = {
2890 .probe = acornscsi_probe,
2891 .remove = acornscsi_remove,
2892 .id_table = acornscsi_cids,
2893 .drv = {
2894 .name = "acornscsi",
2895 },
2896};
2897
2898static int __init acornscsi_init(void)
2899{
2900 return ecard_register_driver(&acornscsi_driver);
2901}
2902
2903static void __exit acornscsi_exit(void)
2904{
2905 ecard_remove_driver(&acornscsi_driver);
2906}
2907
2908module_init(acornscsi_init);
2909module_exit(acornscsi_exit);
2910
2911MODULE_AUTHOR("Russell King");
2912MODULE_DESCRIPTION("AcornSCSI driver");
2913MODULE_LICENSE("GPL");
2914