-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsiop.c
2209 lines (2067 loc) · 69.4 KB
/
siop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: siop.c,v 1.71 2022/04/07 19:33:37 andvar Exp $ */
/*
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Van Jacobson of Lawrence Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)siop.c 7.5 (Berkeley) 5/4/91
*/
/*
* Copyright (c) 1994 Michael L. Hitch
*
* This code is derived from software contributed to Berkeley by
* Van Jacobson of Lawrence Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @(#)siop.c 7.5 (Berkeley) 5/4/91
*/
/*
* AMIGA 53C710 scsi adaptor driver
*/
#ifdef DEBUG_SIOP
#define USE_SERIAL_OUTPUT
#endif
#include "port.h"
#include "port_bsd.h"
// #include "opt_ddb.h"
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: siop.c,v 1.71 2022/04/07 19:33:37 andvar Exp $");
#include <sys/param.h>
#if 0
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/device.h>
#include <sys/disklabel.h>
#include <sys/buf.h>
#include <sys/malloc.h>
#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <machine/cpu.h>
#ifdef __m68k__
#include <m68k/cacheops.h>
#else
#define DCIAS(pa)
#endif
#include <amiga/amiga/custom.h>
#endif
#include "scsi_all.h"
#include "scsipiconf.h"
#include <string.h>
#include <stdlib.h>
#include "sys_queue.h"
#include "siopreg.h"
#include "siopvar.h"
#include <stdio.h>
/*
* SCSI delays
* In u-seconds, primarily for state changes on the SPC.
*/
#define SCSI_CMD_WAIT 500000 /* wait per step of 'immediate' cmds */
#define SCSI_DATA_WAIT 500000 /* wait per data in/out step */
#define SCSI_INIT_WAIT 500000 /* wait per step (both) during init */
void siop_select(struct siop_softc *);
void siopabort(struct siop_softc *, siop_regmap_p, const char *);
void sioperror(struct siop_softc *, siop_regmap_p, u_char);
void siopstart(struct siop_softc *);
int siop_checkintr(struct siop_softc *, u_char, u_char, u_char, int *);
void siopreset(struct siop_softc *);
void siopsetdelay(int);
void siop_scsidone(struct siop_acb *, int);
void siop_timeout(void *);
void siop_sched(struct siop_softc *);
void siop_poll(struct siop_softc *, struct siop_acb *);
void siopintr(struct siop_softc *);
void scsi_period_to_siop(struct siop_softc *, int);
void siop_start(struct siop_softc *, int, int, u_char *, int, u_char *, int);
#ifdef DEBUG_SIOP
void siop_dump_acb(struct siop_acb *);
#endif
/* 53C710 script */
#include "siop_script.out"
/* default to not inhibit sync negotiation on any drive */
u_char siop_inhibit_sync[8] = { 0, 0, 0, 0, 0, 0, 0 }; /* initialize, so patchable */
#ifndef PORT_AMIGA
u_char siop_allow_disc[8] = {3, 3, 3, 3, 3, 3, 3, 3};
#else
u_char siop_allow_disc[8];
#endif
int siop_no_disc = 0; // Disable Synchronous SCSI when this flag is set
int siop_no_dma = 0; // Disable 53C710 DMA when this flag is set
/*
* siop_reset_delay must provide sufficient time for all targets
* on the bus to recover following a bus reset.
*/
const int siop_reset_delay = 250; /* delay after reset, in milliseconds */
#if 0
int siop_cmd_wait = SCSI_CMD_WAIT;
int siop_data_wait = SCSI_DATA_WAIT;
int siop_init_wait = SCSI_INIT_WAIT;
#endif
#ifdef DEBUG_SYNC
#define P_NS(x) ((x) / 4)
/*
* sync period transfer lookup - only valid for 66 MHz clock
*/
static struct {
unsigned char p; /* period from sync request message */
unsigned char r; /* siop_period << 4 | sbcl */
} sync_tab[] = {
{P_NS(60), 0<<4 | 1},
{P_NS(76), 1<<4 | 1},
{P_NS(92), 2<<4 | 1},
{P_NS(92), 0<<4 | 2},
{P_NS(108), 3<<4 | 1},
{P_NS(116), 1<<4 | 2},
{P_NS(120), 4<<4 | 1},
{P_NS(120), 0<<4 | 3},
{P_NS(136), 5<<4 | 1},
{P_NS(140), 2<<4 | 2},
{P_NS(152), 6<<4 | 1},
{P_NS(152), 1<<4 | 3},
{P_NS(164), 3<<4 | 2},
{P_NS(168), 7<<4 | 1},
{P_NS(180), 2<<4 | 3},
{P_NS(184), 4<<4 | 2},
{P_NS(208), 5<<4 | 2},
{P_NS(212), 3<<4 | 3},
{P_NS(232), 6<<4 | 2},
{P_NS(240), 4<<4 | 3},
{P_NS(256), 7<<4 | 2},
{P_NS(272), 5<<4 | 3},
{P_NS(300), 6<<4 | 3},
{P_NS(332), 7<<4 | 3}
};
#endif
#ifdef DEBUG_SYNC
int siopsync_debug = 1;
#endif
#ifdef DEBUG
/*
* 0x01 - full debug
* 0x02 - DMA chaining
* 0x04 - siopintr
* 0x08 - phase mismatch
* 0x10 - <not used>
* 0x20 - panic on unhandled exceptions
* 0x100 - disconnect/reselect
*/
int siop_debug = 0x1ff;
// int siopsync_debug = 0;
int siopdma_hits = 0;
int siopdma_misses = 0;
int siopchain_ints = 0;
int siopstarts = 0;
int siopints = 0;
int siopphmm = 0;
#define SIOP_TRACE_SIZE 128
#define SIOP_TRACE(a,b,c,d) \
siop_trbuf[siop_trix] = (a); \
siop_trbuf[siop_trix+1] = (b); \
siop_trbuf[siop_trix+2] = (c); \
siop_trbuf[siop_trix+3] = (d); \
siop_trix = (siop_trix + 4) & (SIOP_TRACE_SIZE - 1);
u_char siop_trbuf[SIOP_TRACE_SIZE];
int siop_trix;
void siop_dump(struct siop_softc *);
void siop_dump_trace(void);
#else
#define SIOP_TRACE(a,b,c,d)
#endif
#ifndef PORT_AMIGA
/*
* default minphys routine for siop based controllers
*/
void
siop_minphys(struct buf *bp)
{
/*
* No max transfer at this level.
*/
minphys(bp);
}
#endif
/*
* used by specific siop controller
*
*/
void
siop_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
void *arg)
{
struct scsipi_xfer *xs;
#ifdef DIAGNOSTIC
struct scsipi_periph *periph;
#endif
struct siop_acb *acb;
struct siop_softc *sc = device_private(chan->chan_adapter->adapt_dev);
int flags, s;
switch (req) {
case ADAPTER_REQ_RUN_XFER:
xs = arg;
#ifdef DIAGNOSTIC
periph = xs->xs_periph;
#endif
flags = xs->xs_control;
#ifndef PORT_AMIGA
/* XXXX ?? */
if (flags & XS_CTL_DATA_UIO)
panic("siop: scsi data uio requested");
#endif
/* XXXX ?? */
if (sc->sc_nexus && flags & XS_CTL_POLL)
/* panic("siop_scsicmd: busy");*/
printf("siop_scsicmd: busy\n");
s = bsd_splbio();
acb = sc->free_list.tqh_first;
if (acb) {
TAILQ_REMOVE(&sc->free_list, acb, chain);
}
bsd_splx(s);
/*
* This should never happen as we track the resources
* in the mid-layer.
*/
if (acb == NULL) {
#ifdef DIAGNOSTIC
scsipi_printaddr(periph);
printf("unable to allocate acb\n");
panic("siop_scsipi_request");
#endif
#ifdef PORT_AMIGA
panic("siop_scsipi_request: no free ACB");
#endif
}
acb->flags = ACB_ACTIVE;
acb->xs = xs;
#ifdef PORT_AMIGA
CopyMem(xs->cmd, &acb->cmd, xs->cmdlen);
#else
memcpy(&acb->cmd, xs->cmd, xs->cmdlen);
#endif
acb->clen = xs->cmdlen;
acb->daddr = xs->data;
acb->dleft = xs->datalen;
s = bsd_splbio();
TAILQ_INSERT_TAIL(&sc->ready_list, acb, chain);
if (sc->sc_nexus == NULL)
siop_sched(sc);
bsd_splx(s);
if (flags & XS_CTL_POLL || siop_no_dma)
siop_poll(sc, acb);
return;
case ADAPTER_REQ_GROW_RESOURCES:
return;
case ADAPTER_REQ_SET_XFER_MODE:
return;
}
}
void
siop_poll(struct siop_softc *sc, struct siop_acb *acb)
{
siop_regmap_p rp = sc->sc_siopp;
struct scsipi_xfer *xs = acb->xs;
int i;
int status;
u_char istat;
u_char dstat;
u_char sstat0;
int s;
int to;
s = bsd_splbio();
to = xs->timeout / 1000; // to is in seconds
if (sc->nexus_list.tqh_first)
printf("%s: siop_poll called with disconnected device\n",
device_xname(sc->sc_dev));
for (;;) {
/* use cmd_wait values? */
#define WAIT_ITERS 1000 // Decrement to once per second
i = WAIT_ITERS;
/* XXX spl0(); */
while (((istat = rp->siop_istat) &
(SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0) {
if (--i <= 0) {
#ifdef DEBUG
printf ("waiting: tgt %d cmd %02x sbcl %02x dsp %lx (+%lx) dcmd %lx ds %p timeout %d\n",
xs->xs_periph->periph_target, acb->cmd.opcode,
rp->siop_sbcl, rp->siop_dsp,
rp->siop_dsp - sc->sc_scriptspa,
*((volatile long *)&rp->siop_dcmd), &acb->ds, acb->xs->timeout);
#endif
i = WAIT_ITERS;
--to;
if (to <= 0) {
#ifdef PORT_AMIGA
bsd_splbio();
#endif
siopreset(sc);
return;
}
}
delay(1000); // 1 ms
}
#ifdef PORT_AMIGA
/*
* Work around the caution specified in 53C710 documentation for
* Register 0C (0F) DMA Status (DSTAT), where when performing
* consecutive 8-bit reads of the DSTAT and SSTAT0 registers,
* a delay equivalent to 12 BCLK periods must be inserted to
* ensure the interrupts clear properly.
*/
uint32_t reg = *ADDR32((uintptr_t) &rp->siop_sstat2);
sstat0 = reg >> 8;
dstat = reg;
#else
sstat0 = rp->siop_sstat0;
dstat = rp->siop_dstat;
#endif
if (siop_checkintr(sc, istat, dstat, sstat0, &status)) {
if (acb != sc->sc_nexus)
printf("%s: siop_poll disconnected device completed\n",
device_xname(sc->sc_dev));
else if ((sc->sc_flags & SIOP_INTDEFER) == 0) {
sc->sc_flags &= ~SIOP_INTSOFF;
rp->siop_sien = sc->sc_sien;
rp->siop_dien = sc->sc_dien;
}
siop_scsidone(sc->sc_nexus, status);
}
if (xs->xs_status & XS_STS_DONE)
break;
}
bsd_splx(s);
}
/*
* start next command that's ready
*/
void
siop_sched(struct siop_softc *sc)
{
struct scsipi_periph *periph;
struct siop_acb *acb;
int i;
#ifdef DEBUG
if (sc->sc_nexus) {
printf("%s: siop_sched- nexus %p/%d ready %p/%d\n",
device_xname(sc->sc_dev), sc->sc_nexus,
sc->sc_nexus->xs->xs_periph->periph_target,
sc->ready_list.tqh_first,
sc->ready_list.tqh_first->xs->xs_periph->periph_target);
return;
}
#endif
for (acb = sc->ready_list.tqh_first; acb; acb = acb->chain.tqe_next) {
periph = acb->xs->xs_periph;
i = periph->periph_target;
if(!(sc->sc_tinfo[i].lubusy & (1 << periph->periph_lun))) {
struct siop_tinfo *ti = &sc->sc_tinfo[i];
TAILQ_REMOVE(&sc->ready_list, acb, chain);
sc->sc_nexus = acb;
periph = acb->xs->xs_periph;
ti = &sc->sc_tinfo[periph->periph_target];
ti->lubusy |= (1 << periph->periph_lun);
break;
}
}
// XXX: Might need to kick the queue processing here if it's not running
if (acb == NULL) {
#ifdef DEBUG
printf("%s: siop_sched didn't find ready command\n",
device_xname(sc->sc_dev));
#endif
return;
}
if (acb->xs->xs_control & XS_CTL_RESET)
siopreset(sc);
#ifdef PORT_AMIGA
/*
* Setup timeout callout for every issued transaction on the channel.
*
* This differs from the original NetBSD driver where a callout is
* only set for the first transaction active on the bus. In that case,
* if the first one finishes, but following transactions do not finish,
* a driver hang will occur, since there is nothing to terminate another
* active command.
*/
callout_reset(&acb->xs->xs_callout,
mstohz(acb->xs->timeout) + 1, siop_timeout, acb);
#endif
#if 0
acb->cmd.bytes[0] |= slp->scsipi_scsi.lun << 5; /* XXXX */
#endif
++sc->sc_active;
siop_select(sc);
}
void
siop_scsidone(struct siop_acb *acb, int stat)
{
struct scsipi_xfer *xs;
struct scsipi_periph *periph;
struct siop_softc *sc;
int dosched = 0;
if (acb == NULL || (xs = acb->xs) == NULL) {
printf("siop_scsidone: NULL acb %p or scsipi_xfer\n", acb);
#ifdef DIAGNOSTIC
printf("siop_scsidone: NULL acb or scsipi_xfer\n");
#if defined(DEBUG) && defined(DDB)
Debugger();
#endif
#endif
return;
}
callout_stop(&xs->xs_callout);
periph = xs->xs_periph;
sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
xs->status = stat;
xs->resid = 0; /* XXXX */
if (xs->error == XS_NOERROR) {
if (stat == SCSI_CHECK || stat == SCSI_BUSY)
xs->error = XS_BUSY;
}
/*
* Remove the ACB from whatever queue it's on. We have to do a bit of
* a hack to figure out which queue it's on. Note that it is *not*
* necessary to cdr down the ready queue, but we must cdr down the
* nexus queue and see if it's there, so we can mark the unit as no
* longer busy. This code is sickening, but it works.
*/
if (acb == sc->sc_nexus) {
sc->sc_nexus = NULL;
sc->sc_tinfo[periph->periph_target].lubusy &=
~(1<<periph->periph_lun);
if (sc->ready_list.tqh_first)
dosched = 1; /* start next command */
--sc->sc_active;
SIOP_TRACE('d','a',stat,0)
} else if (sc->ready_list.tqh_last == &acb->chain.tqe_next) {
TAILQ_REMOVE(&sc->ready_list, acb, chain);
SIOP_TRACE('d','r',stat,0)
} else {
register struct siop_acb *acb2;
for (acb2 = sc->nexus_list.tqh_first; acb2;
acb2 = acb2->chain.tqe_next)
if (acb2 == acb) {
TAILQ_REMOVE(&sc->nexus_list, acb, chain);
sc->sc_tinfo[periph->periph_target].lubusy
&= ~(1<<periph->periph_lun);
--sc->sc_active;
break;
}
if (acb2)
;
else if (acb->chain.tqe_next) {
TAILQ_REMOVE(&sc->ready_list, acb, chain);
--sc->sc_active;
} else {
printf("%s: can't find matching acb\n",
device_xname(sc->sc_dev));
#ifdef DDB
/* Debugger(); */
#endif
}
SIOP_TRACE('d','n',stat,0);
}
#ifdef PORT_AMIGA
/*
* Call CachePostDMA here once for the whole buffer, even if it took multiple CachePreDMA calls with DMA_Continue flag
*
* Source: The MuLib Programmer’s Manual Page 40-41
* http://aminet.net/package/docs/misc/MuManual
*
*/
if (acb->iob_buf != NULL && acb->iob_len != 0) {
CachePostDMA(&acb->iob_buf, (LONG *)&acb->iob_len, 0);
}
#endif
/* Put it on the free list. */
acb->flags = ACB_FREE;
TAILQ_INSERT_HEAD(&sc->free_list, acb, chain);
sc->sc_tinfo[periph->periph_target].cmds++;
scsipi_done(xs);
#ifdef PORT_AMIGA
if (sc->sc_channel.chan_flags & SCSIPI_CHAN_RESET_PEND) {
/*
* If reset is pending and there is no current I/O active on
* the channel, then go ahead and reset the channel now.
*/
if ((sc->sc_nexus == NULL) && (sc->nexus_list.tqh_first == NULL)) {
siopreset(sc);
/* Tell scsipi completion thread to restart the queue */
sc->sc_channel.chan_tflags |= SCSIPI_CHANT_KICK;
}
}
#endif
if (dosched && sc->sc_nexus == NULL)
siop_sched(sc);
}
void
siopabort(register struct siop_softc *sc, siop_regmap_p rp, const char *where)
{
#ifdef fix_this
int i;
#endif
printf ("%s: abort %s: dstat %02x, sstat0 %02x sbcl %02x\n",
device_xname(sc->sc_dev),
where, rp->siop_dstat, rp->siop_sstat0, rp->siop_sbcl);
if (sc->sc_active > 0) {
#ifdef TODO
SET_SBIC_cmd (rp, SBIC_CMD_ABORT);
WAIT_CIP (rp);
GET_SBIC_asr (rp, asr);
if (asr & (SBIC_ASR_BSY|SBIC_ASR_LCI))
{
/* ok, get more drastic.. */
SET_SBIC_cmd (rp, SBIC_CMD_RESET);
delay(25);
SBIC_WAIT(rp, SBIC_ASR_INT, 0);
GET_SBIC_csr (rp, csr); /* clears interrupt also */
return;
}
do
{
SBIC_WAIT (rp, SBIC_ASR_INT, 0);
GET_SBIC_csr (rp, csr);
}
while ((csr != SBIC_CSR_DISC) && (csr != SBIC_CSR_DISC_1)
&& (csr != SBIC_CSR_CMD_INVALID));
#endif
/* lets just hope it worked.. */
#ifdef fix_this
for (i = 0; i < 2; ++i) {
if (sc->sc_iob[i].sc_xs && &sc->sc_iob[i] !=
sc->sc_cur) {
printf ("siopabort: cleanup!\n");
sc->sc_iob[i].sc_xs = NULL;
}
}
#endif /* fix_this */
/* sc->sc_active = 0; */
}
}
void
siopinitialize(struct siop_softc *sc)
{
int i;
u_int inhibit_sync;
#ifndef PORT_AMIGA
extern u_long scsi_nosync;
extern int shift_nosync;
#endif
/*
* Need to check that scripts is on a long word boundary
* Also should verify that dev doesn't span non-contiguous
* physical pages.
*/
sc->sc_scriptspa = kvtop((void *)__UNCONST(scripts));
/*
* malloc sc_acb to ensure that DS is on a long word boundary.
*/
#ifdef PORT_AMIGA
sc->sc_acb = AllocMem(sizeof(struct siop_acb) * SIOP_NACB,
MEMF_CLEAR | MEMF_PUBLIC);
#else
sc->sc_acb = malloc(sizeof(struct siop_acb) * SIOP_NACB);
#endif
sc->sc_tcp[1] = 1000 / sc->sc_clock_freq;
sc->sc_tcp[2] = 1500 / sc->sc_clock_freq;
sc->sc_tcp[3] = 2000 / sc->sc_clock_freq;
sc->sc_minsync = sc->sc_tcp[1]; /* in 4ns units */
if (sc->sc_minsync < 25)
sc->sc_minsync = 25;
if (sc->sc_clock_freq <= 25) {
sc->sc_dcntl |= 0x80; /* SCLK/1 */
sc->sc_tcp[0] = sc->sc_tcp[1];
} else if (sc->sc_clock_freq <= 37) {
sc->sc_dcntl |= 0x40; /* SCLK/1.5 */
sc->sc_tcp[0] = sc->sc_tcp[2];
} else if (sc->sc_clock_freq <= 50) {
sc->sc_dcntl |= 0x00; /* SCLK/2 */
sc->sc_tcp[0] = sc->sc_tcp[3];
} else {
sc->sc_dcntl |= 0xc0; /* SCLK/3 */
sc->sc_tcp[0] = 3000 / sc->sc_clock_freq;
}
#ifdef PORT_AMIGA
if (sc->sc_nodisconnect) {
/* sc->sc_nodisconnect can be used to prevent SCSI target disconnect */
for (i = 0; i < 8; ++i)
if (sc->sc_nodisconnect & (1 << i))
siop_allow_disc[i] = 0;
else
siop_allow_disc[i] = 3;
}
#endif
if (sc->sc_nosync) {
#ifdef PORT_AMIGA
inhibit_sync = sc->sc_nosync & 0xff;
#else
inhibit_sync = (scsi_nosync >> shift_nosync) & 0xff;
shift_nosync += 8;
#endif
#ifdef DEBUG
if (inhibit_sync)
printf("%s: Inhibiting synchronous transfer %02x\n",
device_xname(sc->sc_dev), inhibit_sync);
#endif
for (i = 0; i < 8; ++i)
if (inhibit_sync & (1 << i))
siop_inhibit_sync[i] = 1;
}
siopreset(sc);
}
#ifdef PORT_AMIGA
void
siopshutdown(struct scsipi_channel *chan)
{
struct siop_softc *sc = device_private(chan->chan_adapter->adapt_dev);
siopreset(sc);
scsipi_free_all_xs(chan);
FreeMem(sc->sc_acb, sizeof(struct siop_acb) * SIOP_NACB);
}
#endif
void
siop_timeout(void *arg)
{
struct siop_acb *acb;
struct scsipi_periph *periph;
struct siop_softc *sc;
int s;
acb = arg;
periph = acb->xs->xs_periph;
sc = device_private(periph->periph_channel->chan_adapter->adapt_dev);
scsipi_printaddr(periph);
printf("timed out\n");
s = bsd_splbio();
#ifdef PORT_AMIGA
/*
* To prevent clobbering transactions on this channel to other targets
* which have not timed out, we will:
* 1) Mark the channel as pending reset.
* 2) Complete this transaction as XS_TIMEOUT.
* 3) siop_scsidone() will take care of resetting the channel when
* there are no more transactions pending for the channel,
*/
sc->sc_channel.chan_flags |= SCSIPI_CHAN_RESET_PEND;
printf("XS_TIMEOUT %p %p\n", acb, acb->xs);
acb->xs->error = XS_TIMEOUT;
siop_scsidone(acb, acb->stat[0]);
#else
acb->xs->error = XS_TIMEOUT;
siopreset(sc);
#endif
bsd_splx(s);
}
void
siopreset(struct siop_softc *sc)
{
siop_regmap_p rp;
u_int i, s;
u_char dummy;
struct siop_acb *acb;
rp = sc->sc_siopp;
if (sc->sc_flags & SIOP_ALIVE)
siopabort(sc, rp, "reset");
printf("%s: ", device_xname(sc->sc_dev)); /* XXXX */
s = bsd_splbio();
/*
* Reset the chip
* XXX - is this really needed?
*/
rp->siop_istat |= SIOP_ISTAT_ABRT; /* abort current script */
rp->siop_istat |= SIOP_ISTAT_RST; /* reset chip */
rp->siop_istat &= ~SIOP_ISTAT_RST;
/*
* Reset SCSI bus (do we really want this?)
*/
rp->siop_sien = 0;
rp->siop_scntl1 |= SIOP_SCNTL1_RST;
delay(1);
rp->siop_scntl1 &= ~SIOP_SCNTL1_RST;
/*
* Set up various chip parameters
*/
rp->siop_scntl0 = SIOP_ARB_FULL | SIOP_SCNTL0_EPC | SIOP_SCNTL0_EPG;
rp->siop_scntl1 = SIOP_SCNTL1_ESR;
rp->siop_dcntl = sc->sc_dcntl;
#ifdef PORT_AMIGA
rp->siop_dmode = 0xe0; /* burst length = 8, drive FC2 */
#else
rp->siop_dmode = 0x80; /* burst length = 4 */
#endif
rp->siop_sien = 0x00; /* don't enable interrupts yet */
rp->siop_dien = 0x00; /* don't enable interrupts yet */
rp->siop_scid = 1 << sc->sc_channel.chan_id;
rp->siop_dwt = 0x00;
rp->siop_ctest0 |= SIOP_CTEST0_BTD | SIOP_CTEST0_EAN;
rp->siop_ctest7 |= sc->sc_ctest7;
#ifdef PORT_AMIGA
/*
* Set SC0 to an output to allow possible board work-around for 53C710
* errata where a SCSI interrupt occurs while the 53C710 is requesting
* the bus. It will release the request after it being granted, causing
* reduced bus utilization due to 7M clock arbitration.
*/
rp->siop_ctest8 |= SIOP_CTEST8_SM;
#endif
// rp->siop_ctest0 |= SIOP_CTEST0_ERF; // Set only for <= 5M transfer rate
/* will need to re-negotiate sync xfers */
memset(&sc->sc_sync, 0, sizeof (sc->sc_sync));
i = rp->siop_istat;
#ifdef PORT_AMIGA
if (i & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP))
dummy = *ADDR32((uintptr_t) &rp->siop_sstat2);
#else
if (i & SIOP_ISTAT_SIP)
dummy = rp->siop_sstat0;
if (i & SIOP_ISTAT_DIP)
dummy = rp->siop_dstat;
#endif
__USE(dummy);
sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
bsd_splx (s);
delay (siop_reset_delay * 1000);
#ifdef PORT_AMIGA
siopintr(sc);
#endif
printf("siop id %d reset V%d\n", sc->sc_channel.chan_id,
rp->siop_ctest8 >> 4);
if ((sc->sc_flags & SIOP_ALIVE) == 0) {
TAILQ_INIT(&sc->ready_list);
TAILQ_INIT(&sc->nexus_list);
TAILQ_INIT(&sc->free_list);
sc->sc_nexus = NULL;
acb = sc->sc_acb;
memset(acb, 0, sizeof(struct siop_acb) * SIOP_NACB);
for (i = 0; i < SIOP_NACB; i++) {
TAILQ_INSERT_TAIL(&sc->free_list, acb, chain);
acb++;
}
memset(sc->sc_tinfo, 0, sizeof(sc->sc_tinfo));
} else {
if (sc->sc_nexus != NULL) {
sc->sc_nexus->xs->error = XS_RESET;
siop_scsidone(sc->sc_nexus, sc->sc_nexus->stat[0]);
}
while ((acb = sc->nexus_list.tqh_first) > 0) {
acb->xs->error = XS_RESET;
siop_scsidone(acb, acb->stat[0]);
}
}
#ifdef PORT_AMIGA
sc->sc_channel.chan_flags &= ~SCSIPI_CHAN_RESET_PEND;
#endif
sc->sc_flags |= SIOP_ALIVE;
sc->sc_flags &= ~(SIOP_INTDEFER|SIOP_INTSOFF);
/* enable SCSI and DMA interrupts */
sc->sc_sien = SIOP_SIEN_M_A | SIOP_SIEN_STO | /*SIOP_SIEN_SEL |*/ SIOP_SIEN_SGE |
SIOP_SIEN_UDC | SIOP_SIEN_RST | SIOP_SIEN_PAR;
sc->sc_dien = SIOP_DIEN_BF | SIOP_DIEN_ABRT | SIOP_DIEN_SIR |
/*SIOP_DIEN_WTD |*/ SIOP_DIEN_IID;
rp->siop_sien = sc->sc_sien;
rp->siop_dien = sc->sc_dien;
}
/*
* Setup Data Storage for 53C710 and start SCRIPTS processing
*/
void
siop_start(struct siop_softc *sc, int target, int lun, u_char *cbuf, int clen,
u_char *buf, int len)
{
siop_regmap_p rp = sc->sc_siopp;
int nchain;
#ifdef PORT_AMIGA
int count;
ULONG tcount;
#else
int count, tcount;
#endif
char *addr, *dmaend;
struct siop_acb *acb = sc->sc_nexus;
#ifdef DEBUG
int i;
#endif
if (acb == NULL) {
printf("siop_start: NULL acb!\n");
return;
}
#if 0
printf("siop_start %d.%d acb=%p xs=%p retries=%d\n", target, lun, acb, acb->xs, acb->xs ? acb->xs->xs_retries : -1);
#endif
#ifdef DEBUG
if (siop_debug & 0x100 && rp->siop_sbcl & SIOP_BSY) {
printf ("ACK! siop was busy: rp %p script %p dsa %p active %ld\n",
rp, &scripts, &acb->ds, sc->sc_active);
printf ("istat %02x sfbr %02x lcrc %02x sien %02x dien %02x\n",
rp->siop_istat, rp->siop_sfbr, rp->siop_lcrc,
rp->siop_sien, rp->siop_dien);
#ifdef DDB
/*Debugger();*/
#endif
}
#endif
acb->msgout[0] = MSG_IDENTIFY | lun;
if (siop_allow_disc[target] & 2 ||
(siop_allow_disc[target] && len == 0))
acb->msgout[0] = MSG_IDENTIFY_DR | lun;
acb->status = 0;
acb->stat[0] = -1;
acb->msg[0] = -1;
acb->ds.scsi_addr = (0x10000 << target) | (sc->sc_sync[target].sxfer << 8);
acb->ds.idlen = 1;
acb->ds.idbuf = (char *) kvtop(&acb->msgout[0]);
acb->ds.cmdlen = clen;
acb->ds.cmdbuf = (char *) kvtop(cbuf);
acb->ds.stslen = 1;
acb->ds.stsbuf = (char *) kvtop(&acb->stat[0]);
acb->ds.msglen = 1;
acb->ds.msgbuf = (char *) kvtop(&acb->msg[0]);
acb->msg[1] = -1;
acb->ds.msginlen = 1;
acb->ds.extmsglen = 1;
acb->ds.synmsglen = 3;
acb->ds.msginbuf = (char *) kvtop(&acb->msg[1]);
acb->ds.extmsgbuf = (char *) kvtop(&acb->msg[2]);
acb->ds.synmsgbuf = (char *) kvtop(&acb->msg[3]);
memset(&acb->ds.chain, 0, sizeof (acb->ds.chain));
/*
* Negotiate wide is the initial negotiation state; since the 53c710
* doesn't do wide transfers, just begin the synchronous transfer
* negotiation here.
*/
if (sc->sc_sync[target].state == NEG_WIDE) {
if (siop_inhibit_sync[target]) {
sc->sc_sync[target].state = NEG_DONE;
sc->sc_sync[target].sbcl = 0;
sc->sc_sync[target].sxfer = 0;
#ifdef DEBUG_SYNC
if (siopsync_debug)
printf ("Forcing target %d asynchronous\n", target);
#endif
}
else {
acb->msg[2] = -1;
acb->msgout[1] = MSG_EXT_MESSAGE;
acb->msgout[2] = 3;
acb->msgout[3] = MSG_SYNC_REQ;
#ifdef MAXTOR_SYNC_KLUDGE
acb->msgout[4] = 50 / 4; /* ask for ridiculous period */
#else
acb->msgout[4] = sc->sc_minsync;
#endif
acb->msgout[5] = SIOP_MAX_OFFSET;
acb->ds.idlen = 6;
sc->sc_sync[target].state = NEG_WAITS;
#ifdef DEBUG_SYNC