-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathattach.c
More file actions
950 lines (843 loc) · 27.2 KB
/
attach.c
File metadata and controls
950 lines (843 loc) · 27.2 KB
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
//
// Copyright 2022-2025 Stefan Reinauer & Chris Hooper
//
// 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.
//
#ifdef DEBUG_ATTACH
#define USE_SERIAL_OUTPUT
#endif
#include "port.h"
#include "printf.h"
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
#include <libraries/expansionbase.h>
#include <devices/trackdisk.h>
#include <clib/expansion_protos.h>
#include <clib/exec_protos.h>
#include <clib/alib_protos.h>
#include <inline/expansion.h>
#include <exec/io.h>
#include <proto/dos.h>
#include <proto/expansion.h>
#include <exec/memory.h>
#include <exec/interrupts.h>
#include <exec/execbase.h>
#include <exec/errors.h>
#include <exec/lists.h>
#include <dos/dostags.h>
#include <devices/scsidisk.h>
#include "device.h"
#include "scsi_all.h"
#include "scsi_spc.h"
#include "scsipiconf.h"
#include "sd.h"
#include "sys_queue.h"
#include "siopreg.h"
#include "siopvar.h"
#include "attach.h"
#include "battmem.h"
#include "ndkcompat.h"
#include "a4091.h"
#include "util/a4092flash/flash.h"
#include "util/a4092flash/nvram_flash.h"
#include "mfg.h"
/*
* NewMinList
* ----------
* Exec V45+ contains the NewMinList function
* But this is not available in Kickstart 3.1
* So we undefine the NDK function and implement it ourselves.
*/
#undef NewMinList
void NewMinList(struct MinList * list)
{
list->mlh_Tail = NULL;
list->mlh_Head = (struct MinNode *)&list->mlh_Tail;
list->mlh_TailPred = (struct MinNode *)list;
}
void scsipi_free_all_xs(struct scsipi_channel *chan);
extern struct ExecBase *SysBase;
extern int romboot;
struct ExpansionBase *ExpansionBase;
/*
* irq_handler_core
* ----------------
* This is the actual interrupt handler. It checks whether the interrupt
* register of the SCSI chip indicates there is something to do, and if
* so also captures the SCSI Status 0 and DMA Status, and then wakes the
* service task to go process them.
*/
__attribute__((noinline))
static int
irq_handler_core(a4091_save_t *save)
{
struct siop_softc *sc = save->as_device_private;
siop_regmap_p rp;
uint8_t istat;
uint32_t reg;
if (sc->sc_flags & SIOP_INTSOFF)
return (0); /* interrupts are not active */
rp = sc->sc_siopp;
istat = rp->siop_istat;
if ((istat & (SIOP_ISTAT_SIP | SIOP_ISTAT_DIP)) == 0)
return (0);
save->as_irq_count++;
/*
* Save Interrupt Status, SCSI Status 0, and DMA Status.
*
* SCSI Status 0 and DMA Status are read in the same transaction
* to ensure that interrupts clear properly and that SCSI interrupts
* captured in ISTAT are not missed. See DMA Status (DSTAT) register
* documentation.
*/
sc->sc_istat |= istat;
reg = *ADDR32((uintptr_t) &rp->siop_sstat2);
sc->sc_sstat0 = reg >> 8;
sc->sc_dstat = reg;
#if defined(ARCH_720) || defined(ARCH_770)
sc->sc_sist = rp->siop_sist;
#endif
if (save->as_svc_task != NULL)
Signal(save->as_svc_task, BIT(save->as_irq_signal));
return (!!(save->as_irq_count & 0xf));
}
/*
* irq_handler
* -----------
* Simple trampoline function to ensure that the last statement
* sets the Z flag appropriately. It would be best to rewrite
* this code in assembly to reduce instruction count. Maybe change
* to "tst d0" if the called function wants to return anything
* other than 0.
*/
static LONG __saveds
irq_handler(register a4091_save_t *save asm("a1"))
{
return (irq_handler_core(save));
}
void *
device_private(device_t dev)
{
(void)dev;
/* CDH: HACK - This should be per device but it is global */
return (asave->as_device_private);
}
#ifdef ENABLE_QUICKINTS
#include "quickints.c"
/*
* a4091_quick_irq
* -----------------------
* Assembly wrapper for quick interrupts that calls the existing
* irq_handler() C function. This reuses all the existing interrupt
* handling logic.
*/
void __stdargs a4091_quick_irq(void);
asm (
" .globl _a4091_quick_irq \n"
"_a4091_quick_irq: \n"
" move.l d0,-(sp) \n"
" move.w 0xdff01c,d0 \n" // Interrupt Enable State
" btst.l #14,d0 \n" // Check if pending disable
" bne.s RealInterrupt \n"
"ExitInt: \n"
" move.l (sp)+,d0 \n"
" rte \n"
"RealInterrupt: \n"
" movem.l d1/a0-a1,-(sp) \n" // Save registers for C ABI
" move.l _asave,a1 \n" // Load pointer to a4091_save
" jsr _irq_handler \n" // Call C handler (a1 passed in register)
" movem.l (sp)+,d1/a0-a1 \n" // Restore registers
" bra.s ExitInt \n" // Return from interrupt
);
/*
* a4091_add_quick_irq_handler
* ---------------------------
* Set up quick interrupt handling for the A4091 card.
*/
static int
a4091_add_quick_irq_handler(uint32_t a4091_base)
{
ULONG intnum;
struct Task *task = FindTask(NULL);
if (task == NULL)
return (ERROR_OPEN_FAIL);
asave->as_SysBase = SysBase;
asave->as_svc_task = task;
asave->as_irq_count = 0;
asave->as_irq_signal = AllocSignal(-1);
// Obtain a quick interrupt vector
intnum = ObtainQuickVector(a4091_quick_irq);
if (intnum == 0) {
printf("Failed to obtain quick interrupt vector\n");
FreeSignal(asave->as_irq_signal);
return (ERROR_NO_FREE_STORE);
}
asave->quick_vec_num = intnum;
// Program the A4091 to use this quick interrupt vector
*ADDR8(a4091_base + A4091_OFFSET_QUICKINT) = (uint8_t)intnum;
printf("Quick interrupt vector %"PRIu32" installed at A4091\n", intnum);
return (0);
}
/*
* a4091_remove_quick_irq_handler
* ------------------------------
* Remove quick interrupt handling and restore the vector.
*/
static void
a4091_remove_quick_irq_handler(void)
{
if (asave->quick_vec_num != 0) {
printf("Removing quick ISR handler (%d irqs)\n", asave->as_irq_count);
asave->as_exiting = 1;
ReleaseQuickVector(asave->quick_vec_num);
/* Reset quick interrupt in hardware by writing to upper half of INTVEC range */
*ADDR8(asave->as_addr + (A4091_OFFSET_QUICKINT | (1<<17))) = 0x26;
asave->quick_vec_num = 0;
FreeSignal(asave->as_irq_signal);
}
}
#endif
static int
a4091_add_local_irq_handler(void)
{
struct Task *task = FindTask(NULL);
if (task == NULL)
return (ERROR_OPEN_FAIL);
asave->as_SysBase = SysBase;
asave->as_svc_task = task;
asave->as_irq_count = 0;
asave->as_irq_signal = AllocSignal(-1);
asave->as_isr = AllocMem(sizeof (*asave->as_isr),
MEMF_CLEAR | MEMF_PUBLIC);
if (asave->as_isr == NULL) {
printf("AllocMem failed\n");
return (ERROR_NO_MEMORY);
}
asave->as_isr->is_Node.ln_Type = NT_INTERRUPT;
asave->as_isr->is_Node.ln_Pri = A4091_INTPRI;
asave->as_isr->is_Node.ln_Name = real_device_name; // a4091.device
asave->as_isr->is_Data = asave;
#pragma GCC diagnostic push
#if GCC_VERSION > 60500
#pragma GCC diagnostic ignored "-Wcast-function-type"
#endif
asave->as_isr->is_Code = (void (*)()) irq_handler;
#pragma GCC diagnostic pop
printf("Add IRQ=%d pri=%d isr=%p asave=%p\n",
A4091_IRQ, A4091_INTPRI, &asave->as_isr, asave);
AddIntServer(A4091_IRQ, asave->as_isr);
return (0);
}
static void
a4091_remove_local_irq_handler(void)
{
if (asave->as_isr != NULL) {
struct Interrupt *as_isr = asave->as_isr;
printf("Removing ISR handler (%d irqs)\n", asave->as_irq_count);
asave->as_exiting = 1;
asave->as_isr = NULL;
RemIntServer(A4091_IRQ, as_isr);
FreeMem(as_isr, sizeof (*asave->as_isr));
FreeSignal(asave->as_irq_signal);
}
}
#if defined(DRIVER_A4091) || defined(DRIVER_A4092) || defined(DRIVER_A4770)
/*
* a4091_find
* ----------
* Locates the next A4091 in the system (by autoconfig order) which has
* not yet been claimed by a driver. If one is not found, the first
* device is chosen (reused).
*/
static uint32_t
a4091_find(UBYTE *boardnum)
{
struct ConfigDev *cdev = NULL;
uint32_t as_addr = 0; /* Default to not found */
int count = 0;
if ((ExpansionBase = (struct ExpansionBase *)OpenLibrary("expansion.library", 0)) == 0) {
printf("Can't open expansion.library.\n");
return (0);
}
if (romboot){
/*
* ROM code needs to be using GetCurrentBinding() rather
* than FindConfigDev() to get the current board.
*/
ULONG res;
struct CurrentBinding cb;
res = GetCurrentBinding(&cb, sizeof (cb));
printf("gcb=%"PRIu32" fn='%s' ps='%s'\n", res,
(char *)cb.cb_FileName ?: "", (char *)cb.cb_ProductString ?: "");
if (!res)
return (0);
if (cb.cb_ConfigDev != NULL) {
struct ConfigDev *cd = cb.cb_ConfigDev;
cdev = cd;
as_addr = (uint32_t) (cdev->cd_BoardAddr);
do {
printf("configdev %p board=%08x flags=%02x configme=%x driver=%p\n",
cd, (uint32_t) cd->cd_BoardAddr, cd->cd_Flags, CDB_CONFIGME, cd->cd_Driver);
cd = cd->cd_NextCD;
} while (cd != NULL);
}
} else {
do {
cdev = FindConfigDev(cdev, ZORRO_MFG_ID, ZORRO_PROD_ID);
if ((cdev != NULL) && (cdev->cd_Flags & CDB_CONFIGME)) {
cdev->cd_Flags &= ~CDB_CONFIGME;
as_addr = (uint32_t) (cdev->cd_BoardAddr);
*boardnum = count;
break;
}
count++;
} while (cdev != NULL);
if (cdev == NULL) {
cdev = FindConfigDev(cdev, ZORRO_MFG_ID, ZORRO_PROD_ID);
if (cdev != NULL) {
/* Just take the first board found */
as_addr = (uint32_t) (cdev->cd_BoardAddr);
*boardnum = 0;
}
}
}
#if defined(FLASH_PARALLEL) || defined(FLASH_SPI)
UBYTE manufId,devId;
ULONG sectorSize;
ULONG flashSize;
if (flash_init(&manufId,&devId,(void *)as_addr,&flashSize,§orSize)) {
if(!flash_read_nvram(NVRAM_OFFSET, &asave->nvram.nv)) {
/* Initialize hardware DIP register from cached NVRAM */
*(volatile uint8_t *)(as_addr + HW_OFFSET_SWITCHES) =
asave->nvram.nv.settings.switch_flags;
}
/* Start clean */
asave->nvram.os_dirty = 0;
asave->nvram.switch_dirty = 0;
}
#endif
CloseLibrary((struct Library *)ExpansionBase);
asave->as_addr = as_addr;
asave->as_cd = cdev;
return (as_addr);
}
#else
/*
* a4000t_find
* ----------
* Locates the next A4091 in the system (by autoconfig order) which has
* not yet been claimed by a driver. If one is not found, the first
* device is chosen (reused).
*/
static uint32_t
a4000t_find(UBYTE *boardnum)
{
*boardnum=0;
asave->as_addr = HW_SCSI_BASE;
asave->as_cd = NULL;
return asave->as_addr;
}
#endif
uint8_t get_dip_switches(void)
{
uint8_t switches;
#if defined(DRIVER_A4000T) || defined(DRIVER_A4000T770)
switches = *(volatile uint32_t *)(asave->as_addr + HW_OFFSET_SWITCHES);
#else
switches = *(volatile uint8_t *)(asave->as_addr + HW_OFFSET_SWITCHES);
#endif
return switches;
}
static uint8_t
get_sc_host_id(const struct siop_softc *sc, uint8_t dip_switches)
{
#ifdef DRIVER_A4000T770
if (sc != NULL && sc->sc_siopp != NULL)
return (~sc->sc_siopp->siop_gpreg) & 0x0f;
#else
(void)sc;
#endif
return dip_switches & 0x07;
}
static uint8_t
get_sc_lun_count(uint8_t dip_switches)
{
#ifdef DRIVER_A4000T770
return (dip_switches & BIT(2)) ? 1 : 8;
#else
return (dip_switches & BIT(7)) ? 1 : 8;
#endif
}
static uint8_t
get_sc_target_count(uint8_t dip_switches)
{
#ifdef DRIVER_A4000T770
return (dip_switches & BIT(0)) ? 8 : 16;
#elif defined(DRIVER_A4770)
(void)dip_switches;
return 16;
#else
(void)dip_switches;
return 8;
#endif
}
#ifdef DRIVER_A4000T770
static int
get_sc_ultra_enabled(uint8_t dip_switches)
{
return ((dip_switches & BIT(1)) == 0);
}
#endif
uint8_t
get_host_id(void)
{
uint8_t dip_switches = 0xff;
struct siop_softc *sc = NULL;
if (asave != NULL) {
dip_switches = get_dip_switches();
sc = asave->as_device_private;
}
return get_sc_host_id(sc, dip_switches);
}
uint8_t
get_lun_count(void)
{
uint8_t dip_switches = 0xff;
if (asave != NULL)
dip_switches = get_dip_switches();
return get_sc_lun_count(dip_switches);
}
uint8_t
get_target_count(void)
{
uint8_t dip_switches = 0xff;
if (asave != NULL)
dip_switches = get_dip_switches();
return get_sc_target_count(dip_switches);
}
static void
a4091_release(uint32_t as_addr)
{
if (asave->as_addr != as_addr)
printf("Releasing wrong card.\n");
#if HW_IS_ZORRO3
asave->as_cd->cd_Flags |= CDB_CONFIGME;
#endif
}
static int
a4091_validate(uint32_t dev_base)
{
uint32_t temp;
uint32_t scratch;
uint32_t patt = 0xf0e7c3a5;
uint32_t next;
uint rot;
uint fail = 0;
siop_regmap_p rp = (siop_regmap_p) (dev_base + HW_OFFSET_REGISTERS);
#if HW_IS_ZORRO3
if ((dev_base < 0x10000000) || (dev_base >= 0xf0000000) ||
(dev_base & 0x00ffffff)) {
printf("Invalid device base %x\n", dev_base);
return (ERROR_BAD_BOARD);
}
#endif
#if !HW_IS_ZORRO3
rp->siop_dcntl = SIOP_DCNTL_EA;
#endif
#if 0
rp->siop_istat |= SIOP_ISTAT_RST; /* reset chip */
delay(20000);
rp->siop_istat &= ~SIOP_ISTAT_RST;
delay(20000);
#endif
/*
* Validate device connectivity by writing the temp and scratch
* registers.
*/
#if defined(NCR53C770)
#define siop_scratch siop_scratcha
#endif
/*
* Create write pointer offset for the cache write-allocate workaround.
* A4091/A4092 use the +0x40 write shadows; A4000T and 53C770 boards
* expose the TEMP/SCRATCH write shadows at +0x80.
*/
#if defined(DRIVER_A4091) || defined(DRIVER_A4092)
siop_regmap_p rp_write = (siop_regmap_p)((char *)rp + 0x40);
#elif defined(DRIVER_A4770) || defined(DRIVER_A4000T) || \
defined(DRIVER_A4000T770)
siop_regmap_p rp_write = (siop_regmap_p)((char *)rp + 0x80);
#endif
scratch = rp->siop_scratch;
temp = rp->siop_temp;
for (rot = 0; rot < 32; rot++, patt = next) {
uint32_t got_scratch;
uint32_t got_temp;
next = ((patt & 0x7fffffff) << 1) | (patt >> 31);
rp_write->siop_scratch = patt;
rp_write->siop_temp = next;
/*
* The cache line flushes below serve two purposes:
* 1) 68040 cache might still be on for the Zorro region during
* pre-boot init. This can cause the wrong values to be read
* from the scratch and test registers the writes to the
* shadow registers do not update the cached values of the
* primary registers.
* 2) 68030 cache write-allocate mode bug work-around. This is
* where even if the cache is disabled for a region, the CPU
* in rare cases might allocate a cache line on write.
* This is actually not needed for 68030 because this code
* does not write to the same register address as what it
* reads (shadow registers of the 53C710 in the A4091 are
* used for the write accesses).
*/
CacheClearE((void *)(&rp->siop_scratch), 4, CACRF_ClearD);
CacheClearE((void *)(&rp->siop_temp), 4, CACRF_ClearD);
got_scratch = rp->siop_scratch;
got_temp = rp->siop_temp;
if ((got_scratch != patt) ||
(got_temp != next)) {
printf(XSTR(DEVNAME)" FAIL");
if (got_scratch != patt) {
printf(" scratch %08x != %08x [%08x]",
got_scratch, patt, got_scratch ^ patt);
panic("Hardware test failure\n SCRATCH %08lx != %08lx [%08lx]",
got_scratch, patt, got_scratch ^ patt);
}
if (got_temp != next) {
printf(" temp %08x != %08x [%08x]",
got_temp, next, got_temp ^ next);
panic("Hardware test failure\n TEMP %08lx != %08lx [%08lx]",
got_temp, next, got_temp ^ next);
}
printf("\n");
fail++;
break;
}
}
rp_write->siop_scratch = scratch;
rp_write->siop_temp = temp;
return (fail ? ERROR_BAD_BOARD : 0);
}
int
init_chan(device_t self, UBYTE *boardnum)
{
struct siop_softc *sc = device_private(self);
struct scsipi_adapter *adapt = &sc->sc_adapter;
struct scsipi_channel *chan = &sc->sc_channel;
uint32_t dev_base;
uint8_t dip_switches;
int rc;
#if defined(DRIVER_A4091) || defined(DRIVER_A4092) || defined(DRIVER_A4770)
dev_base = a4091_find(boardnum);
#else
dev_base = a4000t_find(boardnum);
#endif
if (dev_base == 0) {
printf(XSTR(DEVNAME)": board #%u not found\n",*boardnum);
return (ERROR_NO_BOARD);
}
printf(XSTR(DEVNAME)": board #%u found at 0x%x\n", *boardnum, dev_base);
if ((rc = a4091_validate(dev_base)))
return (rc);
memset(sc, 0, sizeof (*sc));
dip_switches = get_dip_switches();
printf("DIP switches = %02x\n", dip_switches);
Load_BattMem();
#if defined(FLASH_PARALLEL) || defined(FLASH_SPI)
mfg_read();
#endif
sc->sc_dev = self;
sc->sc_siopp = (siop_regmap_p)((char *)dev_base + HW_OFFSET_REGISTERS);
sc->sc_clock_freq = HW_SCLK_CLOCK_FREQ; /* Physical SCLK input */
#ifdef NCR53C710
sc->sc_ctest7 = SIOP_CTEST7_CDIS; // Disable burst
#elif NCR53C770
sc->sc_ctest0 = SIOP_CTEST0_CDIS; // Disable burst
#else
#error "Unsupported SCSI Host Controller"
#endif
#if defined(DRIVER_A4000T) || defined(DRIVER_A4000T770)
sc->sc_dcntl = SIOP_DCNTL_EA; /* A4000T: Connect _SLACK/_STERM */
#else
sc->sc_dcntl = 0; /* Zorro cards */
#endif
TAILQ_INIT(&sc->ready_list);
TAILQ_INIT(&sc->nexus_list);
TAILQ_INIT(&sc->free_list);
/*
* Fill in the scsipi_adapter.
*/
memset(adapt, 0, sizeof (*adapt));
adapt->adapt_dev = self;
adapt->adapt_nchannels = 1;
adapt->adapt_openings = 7;
#ifdef NCR53C710
adapt->adapt_request = siop_scsipi_request;
#elif NCR53C770
adapt->adapt_request = siopng_scsipi_request;
#else
#error ""
#endif
adapt->adapt_asave = asave;
/*
* Fill in the scsipi_channel.
*/
memset(chan, 0, sizeof (*chan));
chan->chan_adapter = adapt;
chan->chan_ntargets = get_sc_target_count(dip_switches);
chan->chan_nluns = get_sc_lun_count(dip_switches);
chan->chan_id = get_sc_host_id(sc, dip_switches); // SCSI ID from board straps
TAILQ_INIT(&chan->chan_queue);
TAILQ_INIT(&chan->chan_complete);
asave->as_callout_head = &callout_head;
#ifdef DRIVER_A4770
/* Bring up the A4770 as wide but non-Ultra until board policy is exposed. */
sc->sc_flags |= SIOP_FORCE_NON_ULTRA;
#endif
#ifdef DRIVER_A4000T770
if ((dip_switches & BIT(0)) != 0)
sc->sc_flags |= SIOP_FORCE_NARROW;
if (!get_sc_ultra_enabled(dip_switches))
sc->sc_flags |= SIOP_FORCE_NON_ULTRA;
#endif
if ((dip_switches & BIT(5)) == 0) {
/* Need to disable synchronous SCSI */
sc->sc_nosync = ~0;
}
/*
* Rear-access DIP switches
#ifdef DRIVER_A4000T770
* SW 8 Off Termination High On Handled by hardware
* SW 7 Off Termination Low On Handled by hardware
* SW 6 Off Synchronous SCSI Mode sc->sc_nosync
* SW 5 Off Short Spinup ms.slowSpinup
* SW 4 Off SCSI-2 Fast Bus Mode NOT SUPPORTED YET
* SW 3 Off SCSI LUNs Enabled chan->chan_nluns
* SW 2 Off Ultra transfers enabled Board-wide policy
* SW 1 Off Wide SCSI enabled chan->chan_ntargets
*
* Host ID comes from GPREG, not from SW 1..3.
#else
* SW 8 Off SCSI LUNs Enabled chan->chan_nluns
* SW 7 Off Internal Termination On Handled by hardware
* SW 6 Off Synchronous SCSI Mode sc->sc_nosync
* SW 5 Off Short Spinup ms.slowSpinup
* SW 4 Off SCSI-2 Fast Bus Mode NOT SUPPORTED YET
* SW 3 Off ADR2=1 chan->chan_id
* SW 2 Off ADR1=1 chan->chan_id
* SW 1 Off ADR0=1 Controller Host ID=7 chan->chan_id
#endif
*/
scsipi_channel_init(chan);
#ifdef ENABLE_QUICKINTS
// Use quick interrupts if enabled in battmem, otherwise use normal interrupts
if (asave->quick_int)
rc = a4091_add_quick_irq_handler(dev_base);
else
#endif
rc = a4091_add_local_irq_handler();
if (rc != 0)
return (rc);
Signal(asave->as_svc_task, BIT(asave->as_irq_signal));
#ifdef NCR53C710
siopinitialize(sc);
#elif NCR53C770
siopnginitialize(sc);
#else
#error "Need to define NCR53C710 or NCR53C770"
#endif
return (0);
}
void
deinit_chan(device_t self)
{
struct siop_softc *sc = device_private(self);
struct scsipi_channel *chan = &sc->sc_channel;
#ifdef NCR53C710
siopshutdown(chan);
#elif NCR53C770
siopngshutdown(chan);
#else
#error "Need to define NCR53C710 or NCR53C770"
#endif
#ifdef ENABLE_QUICKINTS
// Remove quick or normal interrupt based on what was installed
if (asave->quick_int && asave->quick_vec_num != 0)
a4091_remove_quick_irq_handler();
else
#endif
a4091_remove_local_irq_handler();
a4091_release((uint32_t) sc->sc_siopp - 0x00800000);
}
struct scsipi_periph *
scsipi_alloc_periph(int flags)
{
struct scsipi_periph *periph;
uint i;
(void)flags;
periph = AllocMem(sizeof (*periph), MEMF_PUBLIC | MEMF_CLEAR);
if (periph == NULL)
return (NULL);
#if 0
/*
* Start with one command opening. The periph driver
* will grow this if it knows it can take advantage of it.
*/
periph->periph_openings = 1;
#endif
for (i = 0; i < PERIPH_NTAGWORDS; i++)
periph->periph_freetags[i] = 0xffffffff;
#if 0
/* Not tracked for AmigaOS */
TAILQ_INIT(&periph->periph_xferq);
#endif
return (periph);
}
void
scsipi_free_periph(struct scsipi_periph *periph)
{
FreeMem(periph, sizeof (*periph));
}
int scsi_probe_device(struct scsipi_channel *chan, int target, int lun, struct scsipi_periph *periph, int *failed);
int
attach(device_t self, uint scsi_target, struct scsipi_periph **periph_p,
uint flags)
{
struct siop_softc *sc = device_private(self);
struct scsipi_channel *chan = &sc->sc_channel;
struct scsipi_periph *periph;
int target, lun;
int rc;
int failed = 0;
(void)flags;
decode_unit_number(scsi_target, &target, &lun);
// Check for NCR53c770 wide SCSI limits: 16 targets (0-15), 8 LUNs (0-7)
if (target >= 16 || lun >= 8)
return (ERROR_OPEN_FAIL);
if (target == chan->chan_id)
return (ERROR_SELF_UNIT);
periph = scsipi_alloc_periph(0);
*periph_p = periph;
if (periph == NULL)
return (ERROR_NO_MEMORY);
printf("attach(%p, %d)\n", periph, scsi_target);
periph->periph_openings = 4; // Max # of outstanding commands
periph->periph_target = target; // SCSI target ID
periph->periph_lun = lun; // SCSI LUN
#ifdef DEBUG_SCSIPI
periph->periph_dbflags = SCSIPI_DEBUG_FLAGS; // Full debugging
#else
periph->periph_dbflags = 0;
#endif
periph->periph_changenum = 1;
periph->periph_channel = chan;
periph->periph_changeint = NULL;
NewMinList(&periph->periph_changeintlist);
rc = scsi_probe_device(chan, target, lun, periph, &failed);
printf("scsi_probe_device(%d.%d) cont=%d failed=%d\n",
target, lun, rc, failed);
#ifndef USE_SERIAL_OUTPUT
(void) rc;
#endif
if (failed) {
scsipi_free_periph(periph);
return (failed);
}
scsipi_insert_periph(chan, periph);
/* Check if device supports disconnect/reconnect */
if (asave->allow_disc) {
struct {
struct scsi_mode_parameter_header_6 hdr;
struct scsi_disconnect_reconnect_page drp;
} modepage;
int ms_rc = scsipi_mode_sense(periph, SMS_DBD,
SCSI_DISCONNECT_RECONNECT_PAGE,
&modepage.hdr,
sizeof(modepage),
XS_CTL_SIMPLE_TAG,
0, /* retries */
5000); /* timeout ms */
if (ms_rc == 0 &&
(modepage.drp.pg_code & 0x3f) == SCSI_DISCONNECT_RECONNECT_PAGE) {
/* Device supports disconnect-reconnect mode page */
printf(" Target %d: disconnect/reconnect enabled\n", target);
#if defined(ARCH_710)
siop_allow_disc[target] = 3;
#elif defined(ARCH_720) || defined(ARCH_770)
siopng_allow_disc[target] = 3;
#endif
} else {
printf(" Target %d: disconnect/reconnect disabled\n", target);
}
}
#if 0
/* Might be needed for A3000 / A2091 / A590 */
scsipi_set_xfer_mode(chan, target, 1);
#endif
return (0);
}
ULONG
calculate_unit_number(int target, int lun)
{
if (target > 7 || lun > 7) {
// Phase V wide SCSI scheme for IDs/LUNs > 7
return lun * 10 * 1000 + target * 10 + HD_WIDESCSI;
} else {
// Traditional scheme for IDs/LUNs <= 7
return target + lun * 10;
}
}
void
decode_unit_number(ULONG unit_num, int *target, int *lun)
{
if ((unit_num % 10) == HD_WIDESCSI) {
// Phase V wide SCSI scheme
*target = (unit_num / 10) % 1000;
*lun = (unit_num / (10 * 1000)) % 1000;
} else {
// Traditional scheme
*target = unit_num % 10;
*lun = unit_num / 10;
}
}
void
detach(struct scsipi_periph *periph)
{
printf("detach(%p, %d)\n",
periph, calculate_unit_number(periph->periph_target, periph->periph_lun));
if (periph != NULL) {
int timeout = 6; // Seconds
struct scsipi_channel *chan = periph->periph_channel;
while (periph->periph_sent > 0) {
/* Need to wait for outstanding commands to complete */
timeout -= irq_and_timer_handler();
if (timeout == 0) {
printf("Detach timeout waiting for periph to quiesce\n");
return;
}
}
scsipi_remove_periph(chan, periph);
scsipi_free_periph(periph);
}
}
int
periph_still_attached(void)
{
uint i;
struct siop_softc *sc = asave->as_device_private;
struct scsipi_channel *chan = &sc->sc_channel;
for (i = 0; i < SCSIPI_CHAN_PERIPH_BUCKETS; i++)
if (LIST_FIRST(&chan->chan_periphtab[i]) != NULL) {
return (1);
}
return (0);
}