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