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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
|
/*
* Mach Operating System
* Copyright (c) 1991,1990,1989 Carnegie Mellon University
* All Rights Reserved.
*
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie the
* rights to redistribute these changes.
*/
/*
* File: scsi_5380_hdw.c
* Author: Alessandro Forin, Carnegie Mellon University
* Date: 4/91
*
* Bottom layer of the SCSI driver: chip-dependent functions
*
* This file contains the code that is specific to the NCR 5380
* SCSI chip (Host Bus Adapter in SCSI parlance): probing, start
* operation, and interrupt routine.
*/
/*
* This layer works based on small simple 'scripts' that are installed
* at the start of the command and drive the chip to completion.
* The idea comes from the specs of the NCR 53C700 'script' processor.
*
* There are various reasons for this, mainly
* - Performance: identify the common (successful) path, and follow it;
* at interrupt time no code is needed to find the current status
* - Code size: it should be easy to compact common operations
* - Adaptability: the code skeleton should adapt to different chips without
* terrible complications.
* - Error handling: and it is easy to modify the actions performed
* by the scripts to cope with strange but well identified sequences
*
*/
#include <sci.h>
#if NSCI > 0
#include <platforms.h>
#include <mach/std_types.h>
#include <sys/types.h>
#include <chips/busses.h>
#include <scsi/compat_30.h>
#include <machine/machspl.h>
#include <sys/syslog.h>
#include <scsi/scsi.h>
#include <scsi/scsi2.h>
#include <scsi/scsi_defs.h>
#ifdef VAXSTATION
#define PAD(n) char n[3]
#endif
#include <scsi/adapters/scsi_5380.h>
#ifdef PAD
typedef struct {
volatile unsigned char sci_data; /* r: Current data */
/*#define sci_odata sci_data /* w: Out data */
PAD(pad0);
volatile unsigned char sci_icmd; /* rw: Initiator command */
PAD(pad1);
volatile unsigned char sci_mode; /* rw: Mode */
PAD(pad2);
volatile unsigned char sci_tcmd; /* rw: Target command */
PAD(pad3);
volatile unsigned char sci_bus_csr; /* r: Bus Status */
/*#define sci_sel_enb sci_bus_csr /* w: Select enable */
PAD(pad4);
volatile unsigned char sci_csr; /* r: Status */
/*#define sci_dma_send sci_csr /* w: Start dma send data */
PAD(pad5);
volatile unsigned char sci_idata; /* r: Input data */
/*#define sci_trecv sci_idata /* w: Start dma receive, target */
PAD(pad6);
volatile unsigned char sci_iack; /* r: Interrupt Acknowledge */
/*#define sci_irecv sci_iack /* w: Start dma receive, initiator */
PAD(pad7);
} sci_padded_regmap_t;
#else
typedef sci_regmap_t sci_padded_regmap_t;
#endif
#ifdef VAXSTATION
#define check_memory(addr,dow) ((dow) ? wbadaddr(addr,4) : badaddr(addr,4))
/* vax3100 */
#include <chips/vs42x_rb.h>
#define STC_5380_A VAX3100_STC_5380_A
#define STC_5380_B VAX3100_STC_5380_B
#define STC_DMAREG_OFF VAX3100_STC_DMAREG_OFF
static int mem; /* mem++ seems to take approx 0.34 usecs */
#define delay_1p2_us() {mem++;mem++;mem++;mem++;}
#define my_scsi_id(ctlr) (ka3100_scsi_id((ctlr)))
#endif /* VAXSTATION */
#ifndef STC_5380_A /* cross compile check */
typedef struct {
int sci_dma_dir, sci_dma_adr;
} *sci_dmaregs_t;
#define STC_DMAREG_OFF 0
#define SCI_DMA_DIR_WRITE 0
#define SCI_DMA_DIR_READ 1
#define STC_5380_A 0
#define STC_5380_B 0x100
#define SCI_RAM_SIZE 0x10000
#endif
/*
* The 5380 can't tell you the scsi ID it uses, so
* unless there is another way use the defaults
*/
#ifndef my_scsi_id
#define my_scsi_id(ctlr) (scsi_initiator_id[(ctlr)])
#endif
/*
* Statically partition the DMA buffer between targets.
* This way we will eventually be able to attach/detach
* drives on-fly. And 18k/target is enough.
*/
#define PER_TGT_DMA_SIZE ((SCI_RAM_SIZE/7) & ~(sizeof(int)-1))
/*
* Round to 4k to make debug easier
*/
#define PER_TGT_BUFF_SIZE ((PER_TGT_DMA_SIZE >> 12) << 12)
#define PER_TGT_BURST_SIZE (PER_TGT_BUFF_SIZE>>1)
/*
* Macros to make certain things a little more readable
*/
#define SCI_ACK(ptr,phase) (ptr)->sci_tcmd = (phase)
#define SCI_CLR_INTR(regs) {register int temp = regs->sci_iack;}
/*
* A script has a two parts: a pre-condition and an action.
* The first triggers error handling if not satisfied and in
* our case it is formed by the current bus phase and connected
* condition as per bus status bits. The action part is just a
* function pointer, invoked in a standard way. The script
* pointer is advanced only if the action routine returns TRUE.
* See sci_intr() for how and where this is all done.
*/
typedef struct script {
int condition; /* expected state at interrupt */
int (*action)(); /* action routine */
} *script_t;
#define SCRIPT_MATCH(cs,bs) (((bs)&SCI_BUS_BSY)|SCI_CUR_PHASE((bs)))
#define SCI_PHASE_DISC 0x0 /* sort of .. */
/* forward decls of script actions */
boolean_t
sci_dosynch(), /* negotiate synch xfer */
sci_dma_in(), /* get data from target via dma */
sci_dma_out(), /* send data to target via dma */
sci_get_status(), /* get status from target */
sci_end_transaction(), /* all come to an end */
sci_msg_in(), /* get disconnect message(s) */
sci_disconnected(); /* current target disconnected */
/* forward decls of error handlers */
boolean_t
sci_err_generic(), /* generic error handler */
sci_err_disconn(), /* when a target disconnects */
gimmeabreak(); /* drop into the debugger */
int sci_reset_scsibus();
boolean_t sci_probe_target();
scsi_ret_t sci_select_target();
#ifdef VAXSTATION
/*
* This should be somewhere else, and it was a
* mistake to share this buffer across SCSIs.
*/
struct dmabuffer {
volatile char *base;
char *sbrk;
} dmab[1];
volatile char *
sci_buffer_base(unit)
{
return dmab[unit].base;
}
sci_buffer_init(dmar, ram)
sci_dmaregs_t dmar;
volatile char *ram;
{
dmar->sci_dma_rammode = SCI_RAM_EXPMODE;
dmab[0].base = dmab[0].sbrk = (char *) ram;
blkclr((char *) ram, SCI_RAM_SIZE);
}
char *
sci_buffer_sbrk(size)
{
char *ret = dmab[0].sbrk;
dmab[0].sbrk += size;
if ((dmab[0].sbrk - dmab[0].base) > SCI_RAM_SIZE)
panic("scialloc");
return ret;
}
#endif /* VAXSTATION */
/*
* State descriptor for this layer. There is one such structure
* per (enabled) 5380 interface
*/
struct sci_softc {
watchdog_t wd;
sci_padded_regmap_t *regs; /* 5380 registers */
sci_dmaregs_t dmar; /* DMA controller registers */
volatile char *buff; /* DMA buffer memory (I/O space) */
script_t script;
int (*error_handler)();
int in_count; /* amnt we expect to receive */
int out_count; /* amnt we are going to ship */
volatile char state;
#define SCI_STATE_BUSY 0x01 /* selecting or currently connected */
#define SCI_STATE_TARGET 0x04 /* currently selected as target */
#define SCI_STATE_COLLISION 0x08 /* lost selection attempt */
#define SCI_STATE_DMA_IN 0x10 /* tgt --> initiator xfer */
unsigned char ntargets; /* how many alive on this scsibus */
unsigned char done;
unsigned char extra_byte;
scsi_softc_t *sc;
target_info_t *active_target;
target_info_t *next_target; /* trying to seize bus */
queue_head_t waiting_targets;/* other targets competing for bus */
} sci_softc_data[NSCI];
typedef struct sci_softc *sci_softc_t;
sci_softc_t sci_softc[NSCI];
/*
* Definition of the controller for the auto-configuration program.
*/
int sci_probe(), scsi_slave(), sci_go(), sci_intr();
void scsi_attach();
vm_offset_t sci_std[NSCI] = { 0 };
struct bus_device *sci_dinfo[NSCI*8];
struct bus_ctlr *sci_minfo[NSCI];
struct bus_driver sci_driver =
{ sci_probe, scsi_slave, scsi_attach, sci_go, sci_std, "rz", sci_dinfo,
"sci", sci_minfo, BUS_INTR_B4_PROBE};
/*
* Scripts
*/
struct script
sci_script_data_in[] = {
{ SCSI_PHASE_DATAI|SCI_BUS_BSY, sci_dma_in},
{ SCSI_PHASE_STATUS|SCI_BUS_BSY, sci_get_status},
{ SCSI_PHASE_MSG_IN|SCI_BUS_BSY, sci_end_transaction}
},
sci_script_data_out[] = {
{ SCSI_PHASE_DATAO|SCI_BUS_BSY, sci_dma_out},
{ SCSI_PHASE_STATUS|SCI_BUS_BSY, sci_get_status},
{ SCSI_PHASE_MSG_IN|SCI_BUS_BSY, sci_end_transaction}
},
sci_script_cmd[] = {
{ SCSI_PHASE_STATUS|SCI_BUS_BSY, sci_get_status},
{ SCSI_PHASE_MSG_IN|SCI_BUS_BSY, sci_end_transaction}
},
/* Synchronous transfer neg(oti)ation */
sci_script_try_synch[] = {
{ SCSI_PHASE_MSG_OUT|SCI_BUS_BSY, sci_dosynch}
},
/* Disconnect sequence */
sci_script_disconnect[] = {
{ SCI_PHASE_DISC, sci_disconnected}
};
#define u_min(a,b) (((a) < (b)) ? (a) : (b))
#define DEBUG
#ifdef DEBUG
sci_state(base)
vm_offset_t base;
{
sci_padded_regmap_t *regs;
sci_dmaregs_t dmar;
extern char *sci;
unsigned dmadr;
int cnt, i;
if (base == 0)
base = (vm_offset_t)sci;
for (i = 0; i < 2; i++) {
regs = (sci_padded_regmap_t*) (base +
(i ? STC_5380_B : STC_5380_A));
dmar = (sci_dmaregs_t) ((char*)regs + STC_DMAREG_OFF);
SCI_DMADR_GET(dmar,dmadr);
SCI_TC_GET(dmar,cnt);
db_printf("scsi%d: ph %x (sb %x), mode %x, tph %x, csr %x, cmd %x, ",
i,
(unsigned) SCI_CUR_PHASE(regs->sci_bus_csr),
(unsigned) regs->sci_bus_csr,
(unsigned) regs->sci_mode,
(unsigned) regs->sci_tcmd,
(unsigned) regs->sci_csr,
(unsigned) regs->sci_icmd);
db_printf("dma%c %x @ %x\n",
(dmar->sci_dma_dir) ? 'I' : 'O', cnt, dmadr);
}
return 0;
}
sci_target_state(tgt)
target_info_t *tgt;
{
if (tgt == 0)
tgt = sci_softc[0]->active_target;
if (tgt == 0)
return 0;
db_printf("fl %x dma %x+%x cmd %x id %x per %x off %x ior %x ret %x\n",
tgt->flags, tgt->dma_ptr, tgt->transient_state.dma_offset,
tgt->cmd_ptr, tgt->target_id, tgt->sync_period, tgt->sync_offset,
tgt->ior, tgt->done);
if (tgt->flags & TGT_DISCONNECTED){
script_t spt;
spt = tgt->transient_state.script;
db_printf("disconnected at ");
db_printsym(spt,1);
db_printf(": %x ", spt->condition);
db_printsym(spt->action,1);
db_printf(", ");
db_printsym(tgt->transient_state.handler, 1);
db_printf("\n");
}
return 0;
}
sci_all_targets(unit)
{
int i;
target_info_t *tgt;
for (i = 0; i < 8; i++) {
tgt = sci_softc[unit]->sc->target[i];
if (tgt)
sci_target_state(tgt);
}
}
sci_script_state(unit)
{
script_t spt = sci_softc[unit]->script;
if (spt == 0) return 0;
db_printsym(spt,1);
db_printf(": %x ", spt->condition);
db_printsym(spt->action,1);
db_printf(", ");
db_printsym(sci_softc[unit]->error_handler, 1);
return 0;
}
#define PRINT(x) if (scsi_debug) printf x
#define TRMAX 200
int tr[TRMAX+3];
int trpt, trpthi;
#define TR(x) tr[trpt++] = x
#define TRWRAP trpthi = trpt; trpt = 0;
#define TRCHECK if (trpt > TRMAX) {TRWRAP}
#define TRACE
#ifdef TRACE
#define LOGSIZE 256
int sci_logpt;
char sci_log[LOGSIZE];
#define MAXLOG_VALUE 0x24
struct {
char *name;
unsigned int count;
} logtbl[MAXLOG_VALUE];
static LOG(e,f)
char *f;
{
sci_log[sci_logpt++] = (e);
if (sci_logpt == LOGSIZE) sci_logpt = 0;
if ((e) < MAXLOG_VALUE) {
logtbl[(e)].name = (f);
logtbl[(e)].count++;
}
}
sci_print_log(skip)
int skip;
{
register int i, j;
register unsigned char c;
for (i = 0, j = sci_logpt; i < LOGSIZE; i++) {
c = sci_log[j];
if (++j == LOGSIZE) j = 0;
if (skip-- > 0)
continue;
if (c < MAXLOG_VALUE)
db_printf(" %s", logtbl[c].name);
else
db_printf("-%d", c & 0x7f);
}
db_printf("\n");
return 0;
}
sci_print_stat()
{
register int i;
register char *p;
for (i = 0; i < MAXLOG_VALUE; i++) {
if (p = logtbl[i].name)
printf("%d %s\n", logtbl[i].count, p);
}
}
#else /* TRACE */
#define LOG(e,f)
#endif /* TRACE */
#else /* DEBUG */
#define PRINT(x)
#define LOG(e,f)
#define TR(x)
#define TRCHECK
#define TRWRAP
#endif /* DEBUG */
/*
* Probe/Slave/Attach functions
*/
/*
* Probe routine:
* Should find out (a) if the controller is
* present and (b) which/where slaves are present.
*
* Implementation:
* Send an identify msg to each possible target on the bus
* except of course ourselves.
*/
sci_probe(reg, ui)
char *reg;
struct bus_ctlr *ui;
{
int unit = ui->unit;
sci_softc_t sci = &sci_softc_data[unit];
int target_id, i;
scsi_softc_t *sc;
register sci_padded_regmap_t *regs;
spl_t s;
boolean_t did_banner = FALSE;
char *cmd_ptr;
static char *here = "sci_probe";
/*
* We are only called if the chip is there,
* but make sure anyways..
*/
regs = (sci_padded_regmap_t *) (reg);
if (check_memory(regs, 0))
return 0;
#if notyet
/* Mappable version side */
SCI_probe(reg, ui);
#endif
/*
* Initialize hw descriptor
*/
sci_softc[unit] = sci;
sci->regs = regs;
sci->dmar = (sci_dmaregs_t)(reg + STC_DMAREG_OFF);
sci->buff = sci_buffer_base(0);
queue_init(&sci->waiting_targets);
sc = scsi_master_alloc(unit, sci);
sci->sc = sc;
sc->go = sci_go;
sc->probe = sci_probe_target;
sc->watchdog = scsi_watchdog;
sci->wd.reset = sci_reset_scsibus;
#ifdef MACH_KERNEL
sc->max_dma_data = -1; /* unlimited */
#else
sc->max_dma_data = scsi_per_target_virtual;
#endif
scsi_might_disconnect[unit] = 0; /* still true */
/*
* Reset chip
*/
s = splbio();
sci_reset(sci, TRUE);
SCI_CLR_INTR(regs);
/*
* Our SCSI id on the bus.
*/
sc->initiator_id = my_scsi_id(unit);
printf("%s%d: my SCSI id is %d", ui->name, unit, sc->initiator_id);
/*
* For all possible targets, see if there is one and allocate
* a descriptor for it if it is there.
*/
cmd_ptr = sci_buffer_sbrk(0);
for (target_id = 0; target_id < 8; target_id++) {
register unsigned csr, dsr;
scsi_status_byte_t status;
/* except of course ourselves */
if (target_id == sc->initiator_id)
continue;
if (sci_select_target( regs, sc->initiator_id, target_id, FALSE) == SCSI_RET_DEVICE_DOWN) {
SCI_CLR_INTR(regs);
continue;
}
printf(",%s%d", did_banner++ ? " " : " target(s) at ",
target_id);
/* should be command phase here: we selected wo ATN! */
while (SCI_CUR_PHASE(regs->sci_bus_csr) != SCSI_PHASE_CMD)
;
SCI_ACK(regs,SCSI_PHASE_CMD);
/* build command in dma area */
{
unsigned char *p = (unsigned char*) cmd_ptr;
p[0] = SCSI_CMD_TEST_UNIT_READY;
p[1] =
p[2] =
p[3] =
p[4] =
p[5] = 0;
}
sci_data_out(regs, SCSI_PHASE_CMD, 6, cmd_ptr);
while (SCI_CUR_PHASE(regs->sci_bus_csr) != SCSI_PHASE_STATUS)
;
SCI_ACK(regs,SCSI_PHASE_STATUS);
sci_data_in(regs, SCSI_PHASE_STATUS, 1, &status.bits);
if (status.st.scsi_status_code != SCSI_ST_GOOD)
scsi_error( 0, SCSI_ERR_STATUS, status.bits, 0);
/* get cmd_complete message */
while (SCI_CUR_PHASE(regs->sci_bus_csr) != SCSI_PHASE_MSG_IN)
;
SCI_ACK(regs,SCSI_PHASE_MSG_IN);
sci_data_in(regs, SCSI_PHASE_MSG_IN, 1, &i);
/* check disconnected, clear all intr bits */
while (regs->sci_bus_csr & SCI_BUS_BSY)
;
SCI_ACK(regs,SCI_PHASE_DISC);
SCI_CLR_INTR(regs);
/* ... */
/*
* Found a target
*/
sci->ntargets++;
{
register target_info_t *tgt;
tgt = scsi_slave_alloc(unit, target_id, sci);
/* "virtual" address for our use */
tgt->cmd_ptr = sci_buffer_sbrk(PER_TGT_DMA_SIZE);
/* "physical" address for dma engine */
tgt->dma_ptr = (char*)(tgt->cmd_ptr - sci->buff);
#ifdef MACH_KERNEL
#else /*MACH_KERNEL*/
fdma_init(&tgt->fdma, scsi_per_target_virtual);
#endif /*MACH_KERNEL*/
}
}
printf(".\n");
splx(s);
return 1;
}
boolean_t
sci_probe_target(tgt, ior)
target_info_t *tgt;
io_req_t ior;
{
sci_softc_t sci = sci_softc[tgt->masterno];
boolean_t newlywed;
newlywed = (tgt->cmd_ptr == 0);
if (newlywed) {
/* desc was allocated afresh */
/* "virtual" address for our use */
tgt->cmd_ptr = sci_buffer_sbrk(PER_TGT_DMA_SIZE);
/* "physical" address for dma engine */
tgt->dma_ptr = (char*)(tgt->cmd_ptr - sci->buff);
#ifdef MACH_KERNEL
#else /*MACH_KERNEL*/
fdma_init(&tgt->fdma, scsi_per_target_virtual);
#endif /*MACH_KERNEL*/
}
if (scsi_inquiry(tgt, SCSI_INQ_STD_DATA) == SCSI_RET_DEVICE_DOWN)
return FALSE;
tgt->flags = TGT_ALIVE;
return TRUE;
}
static sci_wait(preg, until)
volatile unsigned char *preg;
{
int timeo = 1000000;
/* read it over to avoid bus glitches */
while ( ((*preg & until) != until) ||
((*preg & until) != until) ||
((*preg & until) != until)) {
delay(1);
if (!timeo--) {
printf("sci_wait TIMEO with x%x\n", *preg);
break;
}
}
return *preg;
}
scsi_ret_t
sci_select_target(regs, myid, id, with_atn)
register sci_padded_regmap_t *regs;
unsigned char myid, id;
boolean_t with_atn;
{
register unsigned char bid, icmd;
scsi_ret_t ret = SCSI_RET_RETRY;
if ((regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
(regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
(regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
return ret;
/* for our purposes.. */
myid = 1 << myid;
id = 1 << id;
regs->sci_sel_enb = myid; /* if not there already */
regs->sci_odata = myid;
regs->sci_mode |= SCI_MODE_ARB;
/* AIP might not set if BSY went true after we checked */
for (bid = 0; bid < 20; bid++) /* 20usec circa */
if (regs->sci_icmd & SCI_ICMD_AIP)
break;
if ((regs->sci_icmd & SCI_ICMD_AIP) == 0) {
goto lost;
}
delay(2); /* 2.2us arb delay */
if (regs->sci_icmd & SCI_ICMD_LST) {
goto lost;
}
regs->sci_mode &= ~SCI_MODE_PAR_CHK;
bid = regs->sci_data;
if ((bid & ~myid) > myid) {
goto lost;
}
if (regs->sci_icmd & SCI_ICMD_LST) {
goto lost;
}
/* Won arbitration, enter selection phase now */
icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
icmd |= (with_atn ? (SCI_ICMD_SEL|SCI_ICMD_ATN) : SCI_ICMD_SEL);
regs->sci_icmd = icmd;
if (regs->sci_icmd & SCI_ICMD_LST) {
goto nosel;
}
/* XXX a target that violates specs might still drive the bus XXX */
/* XXX should put our id out, and after the delay check nothi XXX */
/* XXX ng else is out there. XXX */
delay_1p2_us();
regs->sci_sel_enb = 0;
regs->sci_odata = myid | id;
icmd |= SCI_ICMD_BSY|SCI_ICMD_DATA;
regs->sci_icmd = icmd;
regs->sci_mode &= ~SCI_MODE_ARB; /* 2 deskew delays, too */
icmd &= ~SCI_ICMD_BSY;
regs->sci_icmd = icmd;
/* bus settle delay, 400ns */
delay(0); /* too much ? */
regs->sci_mode |= SCI_MODE_PAR_CHK;
{
register int timeo = 2500;/* 250 msecs in 100 usecs chunks */
while ((regs->sci_bus_csr & SCI_BUS_BSY) == 0)
if (--timeo > 0)
delay(100);
else {
goto nodev;
}
}
icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL);
regs->sci_icmd = icmd;
/* regs->sci_sel_enb = myid;*/ /* looks like we should NOT have it */
return SCSI_RET_SUCCESS;
nodev:
ret = SCSI_RET_DEVICE_DOWN;
regs->sci_sel_enb = myid;
nosel:
icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_SEL|SCI_ICMD_ATN);
regs->sci_icmd = icmd;
lost:
bid = regs->sci_mode;
bid &= ~SCI_MODE_ARB;
bid |= SCI_MODE_PAR_CHK;
regs->sci_mode = bid;
return ret;
}
sci_data_out(regs, phase, count, data)
register sci_padded_regmap_t *regs;
unsigned char *data;
{
register unsigned char icmd;
/* ..checks.. */
icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
loop:
if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
return count;
while ( ((regs->sci_bus_csr & SCI_BUS_REQ) == 0) &&
((regs->sci_bus_csr & SCI_BUS_REQ) == 0) &&
((regs->sci_bus_csr & SCI_BUS_REQ) == 0))
;
icmd |= SCI_ICMD_DATA;
regs->sci_icmd = icmd;
regs->sci_odata = *data++;
icmd |= SCI_ICMD_ACK;
regs->sci_icmd = icmd;
icmd &= ~(SCI_ICMD_DATA|SCI_ICMD_ACK);
while ( (regs->sci_bus_csr & SCI_BUS_REQ) &&
(regs->sci_bus_csr & SCI_BUS_REQ) &&
(regs->sci_bus_csr & SCI_BUS_REQ))
;
regs->sci_icmd = icmd;
if (--count > 0)
goto loop;
return 0;
}
sci_data_in(regs, phase, count, data)
register sci_padded_regmap_t *regs;
unsigned char *data;
{
register unsigned char icmd;
/* ..checks.. */
icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
loop:
if (SCI_CUR_PHASE(regs->sci_bus_csr) != phase)
return count;
while ( ((regs->sci_bus_csr & SCI_BUS_REQ) == 0) &&
((regs->sci_bus_csr & SCI_BUS_REQ) == 0) &&
((regs->sci_bus_csr & SCI_BUS_REQ) == 0))
;
*data++ = regs->sci_data;
icmd |= SCI_ICMD_ACK;
regs->sci_icmd = icmd;
icmd &= ~SCI_ICMD_ACK;
while ( (regs->sci_bus_csr & SCI_BUS_REQ) &&
(regs->sci_bus_csr & SCI_BUS_REQ) &&
(regs->sci_bus_csr & SCI_BUS_REQ))
;
regs->sci_icmd = icmd;
if (--count > 0)
goto loop;
return 0;
}
sci_reset(sci, quickly)
sci_softc_t sci;
boolean_t quickly;
{
register sci_padded_regmap_t *regs = sci->regs;
register sci_dmaregs_t dma = sci->dmar;
int dummy;
regs->sci_icmd = SCI_ICMD_TEST; /* don't drive outputs */
regs->sci_icmd = SCI_ICMD_TEST|SCI_ICMD_RST;
delay(25);
regs->sci_icmd = 0;
regs->sci_mode = SCI_MODE_PAR_CHK|SCI_MODE_PERR_IE;
regs->sci_tcmd = SCI_PHASE_DISC; /* make sure we do not miss transition */
regs->sci_sel_enb = 0;
/* idle the dma controller */
dma->sci_dma_adr = 0;
dma->sci_dma_dir = SCI_DMA_DIR_WRITE;
SCI_TC_PUT(dma,0);
/* clear interrupt (two might be queued?) */
SCI_CLR_INTR(regs);
SCI_CLR_INTR(regs);
if (quickly)
return;
/*
* reset the scsi bus, the interrupt routine does the rest
* or you can call sci_bus_reset().
*/
regs->sci_icmd = SCI_ICMD_RST;
}
/*
* Operational functions
*/
/*
* Start a SCSI command on a target
*/
sci_go(tgt, cmd_count, in_count, cmd_only)
target_info_t *tgt;
boolean_t cmd_only;
{
sci_softc_t sci;
register spl_t s;
boolean_t disconn;
script_t scp;
boolean_t (*handler)();
LOG(1,"go");
sci = (sci_softc_t)tgt->hw_state;
/*
* We cannot do real DMA.
*/
#ifdef MACH_KERNEL
#else /*MACH_KERNEL*/
if (tgt->ior)
fdma_map(&tgt->fdma, tgt->ior);
#endif /*MACH_KERNEL*/
if ((tgt->cur_cmd == SCSI_CMD_WRITE) ||
(tgt->cur_cmd == SCSI_CMD_LONG_WRITE)){
io_req_t ior = tgt->ior;
register int len = ior->io_count;
tgt->transient_state.out_count = len;
if (len > PER_TGT_BUFF_SIZE)
len = PER_TGT_BUFF_SIZE;
bcopy( ior->io_data,
tgt->cmd_ptr + cmd_count,
len);
tgt->transient_state.copy_count = len;
/* avoid leaks */
if (len < tgt->block_size) {
bzero( tgt->cmd_ptr + cmd_count + len,
tgt->block_size - len);
tgt->transient_state.out_count = tgt->block_size;
}
} else {
tgt->transient_state.out_count = 0;
tgt->transient_state.copy_count = 0;
}
tgt->transient_state.cmd_count = cmd_count;
disconn = BGET(scsi_might_disconnect,tgt->masterno,tgt->target_id);
disconn = disconn && (sci->ntargets > 1);
disconn |= BGET(scsi_should_disconnect,tgt->masterno,tgt->target_id);
/*
* Setup target state
*/
tgt->done = SCSI_RET_IN_PROGRESS;
handler = (disconn) ? sci_err_disconn : sci_err_generic;
switch (tgt->cur_cmd) {
case SCSI_CMD_READ:
case SCSI_CMD_LONG_READ:
LOG(0x13,"readop");
scp = sci_script_data_in;
break;
case SCSI_CMD_WRITE:
case SCSI_CMD_LONG_WRITE:
LOG(0x14,"writeop");
scp = sci_script_data_out;
break;
case SCSI_CMD_INQUIRY:
/* This is likely the first thing out:
do the synch neg if so */
if (!cmd_only && ((tgt->flags&TGT_DID_SYNCH)==0)) {
scp = sci_script_try_synch;
tgt->flags |= TGT_TRY_SYNCH;
break;
}
case SCSI_CMD_REQUEST_SENSE:
case SCSI_CMD_MODE_SENSE:
case SCSI_CMD_RECEIVE_DIAG_RESULTS:
case SCSI_CMD_READ_CAPACITY:
case SCSI_CMD_READ_BLOCK_LIMITS:
case SCSI_CMD_READ_TOC:
case SCSI_CMD_READ_SUBCH:
case SCSI_CMD_READ_HEADER:
case 0xc4: /* despised: SCSI_CMD_DEC_PLAYBACK_STATUS */
case 0xdd: /* despised: SCSI_CMD_NEC_READ_SUBCH_Q */
case 0xde: /* despised: SCSI_CMD_NEC_READ_TOC */
scp = sci_script_data_in;
LOG(0x1c,"cmdop");
LOG(0x80+tgt->cur_cmd,0);
break;
case SCSI_CMD_MODE_SELECT:
case SCSI_CMD_REASSIGN_BLOCKS:
case SCSI_CMD_FORMAT_UNIT:
case 0xc9: /* vendor-spec: SCSI_CMD_DEC_PLAYBACK_CONTROL */
tgt->transient_state.cmd_count = sizeof_scsi_command(tgt->cur_cmd);
tgt->transient_state.out_count =
cmd_count - tgt->transient_state.cmd_count;
scp = sci_script_data_out;
LOG(0x1c,"cmdop");
LOG(0x80+tgt->cur_cmd,0);
break;
case SCSI_CMD_TEST_UNIT_READY:
/*
* Do the synch negotiation here, unless prohibited
* or done already
*/
if (tgt->flags & TGT_DID_SYNCH) {
scp = sci_script_cmd;
} else {
scp = sci_script_try_synch;
tgt->flags |= TGT_TRY_SYNCH;
cmd_only = FALSE;
}
LOG(0x1c,"cmdop");
LOG(0x80+tgt->cur_cmd,0);
break;
default:
LOG(0x1c,"cmdop");
LOG(0x80+tgt->cur_cmd,0);
scp = sci_script_cmd;
}
tgt->transient_state.script = scp;
tgt->transient_state.handler = handler;
tgt->transient_state.identify = (cmd_only) ? 0xff :
(disconn ? SCSI_IDENTIFY|SCSI_IFY_ENABLE_DISCONNECT :
SCSI_IDENTIFY);
if (in_count)
tgt->transient_state.in_count =
(in_count < tgt->block_size) ? tgt->block_size : in_count;
else
tgt->transient_state.in_count = 0;
tgt->transient_state.dma_offset = 0;
/*
* See if another target is currently selected on
* this SCSI bus, e.g. lock the sci structure.
* Note that it is the strategy routine's job
* to serialize ops on the same target as appropriate.
* XXX here and everywhere, locks!
*/
/*
* Protection viz reconnections makes it tricky.
*/
/* s = splbio();*/
s = splhigh();
if (sci->wd.nactive++ == 0)
sci->wd.watchdog_state = SCSI_WD_ACTIVE;
if (sci->state & SCI_STATE_BUSY) {
/*
* Queue up this target, note that this takes care
* of proper FIFO scheduling of the scsi-bus.
*/
LOG(3,"enqueue");
enqueue_tail(&sci->waiting_targets, (queue_entry_t) tgt);
} else {
/*
* It is down to at most two contenders now,
* we will treat reconnections same as selections
* and let the scsi-bus arbitration process decide.
*/
sci->state |= SCI_STATE_BUSY;
sci->next_target = tgt;
sci_attempt_selection(sci);
/*
* Note that we might still lose arbitration..
*/
}
splx(s);
}
sci_attempt_selection(sci)
sci_softc_t sci;
{
target_info_t *tgt;
register int out_count;
sci_padded_regmap_t *regs;
sci_dmaregs_t dmar;
register int cmd;
boolean_t ok;
scsi_ret_t ret;
regs = sci->regs;
dmar = sci->dmar;
tgt = sci->next_target;
LOG(4,"select");
LOG(0x80+tgt->target_id,0);
/*
* Init bus state variables and set registers.
*/
sci->active_target = tgt;
/* reselection pending ? */
if ((regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
(regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)) &&
(regs->sci_bus_csr & (SCI_BUS_BSY|SCI_BUS_SEL)))
return;
sci->script = tgt->transient_state.script;
sci->error_handler = tgt->transient_state.handler;
sci->done = SCSI_RET_IN_PROGRESS;
sci->in_count = 0;
sci->out_count = 0;
sci->extra_byte = 0;
/*
* This is a bit involved, but the bottom line is we want to
* know after we selected with or w/o ATN if the selection
* went well (ret) and if it is (ok) to send the command.
*/
ok = TRUE;
if (tgt->flags & TGT_DID_SYNCH) {
if (tgt->transient_state.identify == 0xff) {
/* Select w/o ATN */
ret = sci_select_target(regs, sci->sc->initiator_id,
tgt->target_id, FALSE);
} else {
/* Select with ATN */
ret = sci_select_target(regs, sci->sc->initiator_id,
tgt->target_id, TRUE);
if (ret == SCSI_RET_SUCCESS) {
register unsigned char icmd;
while (SCI_CUR_PHASE(regs->sci_bus_csr) != SCSI_PHASE_MSG_OUT)
;
icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
icmd &= ~SCI_ICMD_ATN;
regs->sci_icmd = icmd;
SCI_ACK(regs,SCSI_PHASE_MSG_OUT);
ok = (sci_data_out(regs, SCSI_PHASE_MSG_OUT,
1, &tgt->transient_state.identify) == 0);
}
}
} else if (tgt->flags & TGT_TRY_SYNCH) {
/* Select with ATN, do the synch xfer neg */
ret = sci_select_target(regs, sci->sc->initiator_id,
tgt->target_id, TRUE);
if (ret == SCSI_RET_SUCCESS) {
while (SCI_CUR_PHASE(regs->sci_bus_csr) != SCSI_PHASE_MSG_OUT)
;
ok = sci_dosynch( sci, regs->sci_csr, regs->sci_bus_csr);
}
} else {
ret = sci_select_target(regs, sci->sc->initiator_id,
tgt->target_id, FALSE);
}
if (ret == SCSI_RET_DEVICE_DOWN) {
sci->done = ret;
sci_end(sci, regs->sci_csr, regs->sci_bus_csr);
return;
}
if ((ret != SCSI_RET_SUCCESS) || !ok)
return;
/* time this out or do it via dma !! */
while (SCI_CUR_PHASE(regs->sci_bus_csr) != SCSI_PHASE_CMD)
;
/* set dma pointer and counter to xfer command */
out_count = tgt->transient_state.cmd_count;
#if 0
SCI_ACK(regs,SCSI_PHASE_CMD);
sci_data_out(regs,SCSI_PHASE_CMD,out_count,tgt->cmd_ptr);
regs->sci_mode = SCI_MODE_PAR_CHK|SCI_MODE_DMA|SCI_MODE_MONBSY;
#else
SCI_DMADR_PUT(dmar,tgt->dma_ptr);
delay_1p2_us();
SCI_TC_PUT(dmar,out_count);
dmar->sci_dma_dir = SCI_DMA_DIR_WRITE;
SCI_ACK(regs,SCSI_PHASE_CMD);
SCI_CLR_INTR(regs);
regs->sci_mode = SCI_MODE_PAR_CHK|SCI_MODE_DMA|SCI_MODE_MONBSY;
regs->sci_icmd = SCI_ICMD_DATA;
regs->sci_dma_send = 1;
#endif
}
/*
* Interrupt routine
* Take interrupts from the chip
*
* Implementation:
* Move along the current command's script if
* all is well, invoke error handler if not.
*/
sci_intr(unit)
{
register sci_softc_t sci;
register script_t scp;
register unsigned csr, bs, cmd;
register sci_padded_regmap_t *regs;
boolean_t try_match;
#if notyet
extern boolean_t rz_use_mapped_interface;
if (rz_use_mapped_interface)
return SCI_intr(unit);
#endif
LOG(5,"\n\tintr");
sci = sci_softc[unit];
regs = sci->regs;
/* ack interrupt */
csr = regs->sci_csr;
bs = regs->sci_bus_csr;
cmd = regs->sci_icmd;
TR(regs->sci_mode);
SCI_CLR_INTR(regs);
TR(csr);
TR(bs);
TR(cmd);
TRCHECK;
if (cmd & SCI_ICMD_RST){
sci_bus_reset(sci);
return;
}
/* we got an interrupt allright */
if (sci->active_target)
sci->wd.watchdog_state = SCSI_WD_ACTIVE;
/* drop spurious calls */
if ((csr & SCI_CSR_INT) == 0) {
LOG(2,"SPURIOUS");
return;
}
/* Note: reselect has I/O asserted, select has not */
if ((sci->state & SCI_STATE_TARGET) ||
((bs & (SCI_BUS_BSY|SCI_BUS_SEL|SCI_BUS_IO)) == SCI_BUS_SEL)) {
sci_target_intr(sci,csr,bs);
return;
}
scp = sci->script;
/* Race: disconnecting, we get the disconnected notification
(csr sez BSY dropped) at the same time a reselect is active */
if ((csr & SCI_CSR_DISC) &&
scp && (scp->condition == SCI_PHASE_DISC)) {
(void) (*scp->action)(sci, csr, bs);
/* takes care of calling reconnect if necessary */
return;
}
/* check who got the bus */
if ((scp == 0) || (cmd & SCI_ICMD_LST) ||
((bs & (SCI_BUS_BSY|SCI_BUS_SEL|SCI_BUS_IO)) == (SCI_BUS_SEL|SCI_BUS_IO))) {
sci_reconnect(sci, csr, bs);
return;
}
if (SCRIPT_MATCH(csr,bs) != scp->condition) {
if (try_match = (*sci->error_handler)(sci, csr, bs)) {
csr = regs->sci_csr;
bs = regs->sci_bus_csr;
}
} else
try_match = TRUE;
/* might have been side effected */
scp = sci->script;
if (try_match && (SCRIPT_MATCH(csr,bs) == scp->condition)) {
/*
* Perform the appropriate operation,
* then proceed
*/
if ((*scp->action)(sci, csr, bs)) {
/* might have been side effected */
scp = sci->script;
sci->script = scp + 1;
}
}
}
sci_target_intr(sci)
register sci_softc_t sci;
{
panic("SCI: TARGET MODE !!!\n");
}
/*
* All the many little things that the interrupt
* routine might switch to
*/
boolean_t
sci_end_transaction( sci, csr, bs)
register sci_softc_t sci;
{
register sci_padded_regmap_t *regs = sci->regs;
char cmc;
LOG(0x1f,"end_t");
/* Stop dma, no interrupt on disconnect */
regs->sci_icmd = 0;
regs->sci_mode &= ~(SCI_MODE_DMA|SCI_MODE_MONBSY|SCI_MODE_DMA_IE);
/* dmar->sci_dma_dir = SCI_DMA_DIR_WRITE;/* make sure we steal not */
SCI_ACK(regs,SCSI_PHASE_MSG_IN);
regs->sci_sel_enb = (1 << sci->sc->initiator_id);
sci_data_in(regs, SCSI_PHASE_MSG_IN, 1, &cmc);
if (cmc != SCSI_COMMAND_COMPLETE)
printf("{T%x}", cmc);
/* check disconnected, clear all intr bits */
while (regs->sci_bus_csr & SCI_BUS_BSY)
;
SCI_CLR_INTR(regs);
SCI_ACK(regs,SCI_PHASE_DISC);
if (!sci_end(sci, csr, bs)) {
SCI_CLR_INTR(regs);
(void) sci_reconnect(sci, csr, bs);
}
return FALSE;
}
boolean_t
sci_end( sci, csr, bs)
register sci_softc_t sci;
{
register target_info_t *tgt;
register io_req_t ior;
register sci_padded_regmap_t *regs = sci->regs;
boolean_t reconn_pending;
LOG(6,"end");
tgt = sci->active_target;
if ((tgt->done = sci->done) == SCSI_RET_IN_PROGRESS)
tgt->done = SCSI_RET_SUCCESS;
sci->script = 0;
if (sci->wd.nactive-- == 1)
sci->wd.watchdog_state = SCSI_WD_INACTIVE;
/* check reconnection not pending */
bs = regs->sci_bus_csr;
reconn_pending = ((bs & (SCI_BUS_BSY|SCI_BUS_SEL|SCI_BUS_IO)) == (SCI_BUS_SEL|SCI_BUS_IO));
if (!reconn_pending) {
sci_release_bus(sci);
} else {
sci->active_target = 0;
/* sci->state &= ~SCI_STATE_BUSY; later */
}
if (ior = tgt->ior) {
#ifdef MACH_KERNEL
#else /*MACH_KERNEL*/
fdma_unmap(&tgt->fdma, ior);
#endif /*MACH_KERNEL*/
LOG(0xA,"ops->restart");
(*tgt->dev_ops->restart)( tgt, TRUE);
if (reconn_pending)
sci->state &= ~SCI_STATE_BUSY;
}
return (!reconn_pending);
}
boolean_t
sci_release_bus(sci)
register sci_softc_t sci;
{
boolean_t ret = FALSE;
LOG(9,"release");
sci->script = 0;
if (sci->state & SCI_STATE_COLLISION) {
LOG(0xB,"collided");
sci->state &= ~SCI_STATE_COLLISION;
sci_attempt_selection(sci);
} else if (queue_empty(&sci->waiting_targets)) {
sci->state &= ~SCI_STATE_BUSY;
sci->active_target = 0;
ret = TRUE;
} else {
LOG(0xC,"dequeue");
sci->next_target = (target_info_t *)
dequeue_head(&sci->waiting_targets);
sci_attempt_selection(sci);
}
return ret;
}
boolean_t
sci_get_status( sci, csr, bs)
register sci_softc_t sci;
{
register sci_padded_regmap_t *regs = sci->regs;
register sci_dmaregs_t dmar = sci->dmar;
scsi2_status_byte_t status;
register target_info_t *tgt;
unsigned int len, mode;
LOG(0xD,"get_status");
TRWRAP;
/* Stop dma */
regs->sci_icmd = 0;
mode = regs->sci_mode;
regs->sci_mode = (mode & ~(SCI_MODE_DMA|SCI_MODE_DMA_IE));
dmar->sci_dma_dir = SCI_DMA_DIR_WRITE;/* make sure we steal not */
sci->state &= ~SCI_STATE_DMA_IN;
tgt = sci->active_target;
if (len = sci->in_count) {
register int count;
SCI_TC_GET(dmar,count);
if ((tgt->cur_cmd != SCSI_CMD_READ) &&
(tgt->cur_cmd != SCSI_CMD_LONG_READ)){
len -= count;
} else {
if (count) {
#if 0
this is incorrect and besides..
tgt->ior->io_residual = count;
#endif
len -= count;
}
sci_copyin( tgt, tgt->transient_state.dma_offset,
len, 0, 0);
}
}
/* to get the phase mismatch intr */
regs->sci_mode = mode;
SCI_ACK(regs,SCSI_PHASE_STATUS);
sci_data_in(regs, SCSI_PHASE_STATUS, 1, &status.bits);
SCI_TC_PUT(dmar,0);
if (status.st.scsi_status_code != SCSI_ST_GOOD) {
scsi_error(sci->active_target, SCSI_ERR_STATUS, status.bits, 0);
sci->done = (status.st.scsi_status_code == SCSI_ST_BUSY) ?
SCSI_RET_RETRY : SCSI_RET_NEED_SENSE;
} else
sci->done = SCSI_RET_SUCCESS;
return TRUE;
}
boolean_t
sci_dma_in( sci, csr, bs)
register sci_softc_t sci;
{
register target_info_t *tgt;
register sci_padded_regmap_t *regs = sci->regs;
register sci_dmaregs_t dmar = sci->dmar;
char *dma_ptr;
register int count;
boolean_t advance_script = TRUE;
LOG(0xE,"dma_in");
/*
* Problem: the 5380 pipelines xfers between the scsibus and
* itself and between itself and the DMA engine --> halting ?
* In the dmaout direction all is ok, except that (see NCR notes)
* the EOP interrupt is generated before the pipe is empty.
* In the dmain direction (here) the interrupt comes when
* one too many bytes have been xferred on chip!
*
* More specifically, IF we asked for count blindly and we had
* more than count bytes coming (double buffering) we would endup
* actually xferring count+1 from the scsibus, but only count
* to memory [hopefully the last byte sits in the sci_datai reg].
* This could be helped, except most times count is an exact multiple
* of the sector size which is where disks disconnect....
*
* INSTEAD, we recognize here that we expect more than count bytes
* coming and set the DMA count to count-1 but keep sci->in_count
* above to count. This will be wrong if the target disconnects
* amidst, but we can cure it.
*
* The places where this has an effect are marked by "EXTRA_BYTE"
*/
tgt = sci->active_target;
sci->state |= SCI_STATE_DMA_IN;
/* ought to stop dma to start another */
regs->sci_mode &= ~ (SCI_MODE_DMA|SCI_MODE_DMA_IE);
regs->sci_icmd = 0;
if (sci->in_count == 0) {
/*
* Got nothing yet: either just sent the command
* or just reconnected
*/
register int avail;
count = tgt->transient_state.in_count;
count = u_min(count, (PER_TGT_BURST_SIZE));
avail = PER_TGT_BUFF_SIZE - tgt->transient_state.dma_offset;
count = u_min(count, avail);
/* common case of 8k-or-less read ? */
advance_script = (tgt->transient_state.in_count == count);
} else {
/*
* We received some data.
*/
register int offset, xferred, eb;
unsigned char extrab = regs->sci_idata; /* EXTRA_BYTE */
SCI_TC_GET(dmar,xferred);
assert(xferred == 0);
if (scsi_debug) {
printf("{B %x %x %x (%x)}",
sci->in_count, xferred, sci->extra_byte, extrab);
}
/* ++EXTRA_BYTE */
xferred = sci->in_count - xferred;
eb = sci->extra_byte;
/* --EXTRA_BYTE */
assert(xferred > 0);
tgt->transient_state.in_count -= xferred;
assert(tgt->transient_state.in_count > 0);
offset = tgt->transient_state.dma_offset;
tgt->transient_state.dma_offset += xferred;
count = u_min(tgt->transient_state.in_count, (PER_TGT_BURST_SIZE));
if (tgt->transient_state.dma_offset == PER_TGT_BUFF_SIZE) {
tgt->transient_state.dma_offset = 0;
} else {
register int avail;
avail = PER_TGT_BUFF_SIZE - tgt->transient_state.dma_offset;
count = u_min(count, avail);
}
advance_script = (tgt->transient_state.in_count == count);
/* get some more */
dma_ptr = tgt->dma_ptr + tgt->transient_state.dma_offset;
sci->in_count = count;
/* ++EXTRA_BYTE */
if (!advance_script) {
sci->extra_byte = 1; /* that's the cure.. */
count--;
} else
sci->extra_byte = 0;
/* --EXTRA_BYTE */
SCI_TC_PUT(dmar,count);
/* regs->sci_icmd = 0;*/
SCI_DMADR_PUT(dmar,dma_ptr);
delay_1p2_us();
SCI_ACK(regs,SCSI_PHASE_DATAI);
SCI_CLR_INTR(regs);
regs->sci_mode |= (advance_script ? SCI_MODE_DMA
: (SCI_MODE_DMA|SCI_MODE_DMA_IE));
dmar->sci_dma_dir = SCI_DMA_DIR_READ;
regs->sci_irecv = 1;
/* copy what we got */
sci_copyin( tgt, offset, xferred, eb, extrab);
/* last chunk ? */
return advance_script;
}
sci->in_count = count;
dma_ptr = tgt->dma_ptr + tgt->transient_state.dma_offset;
/* ++EXTRA_BYTE */
if (!advance_script) {
sci->extra_byte = 1; /* that's the cure.. */
count--;
} else
sci->extra_byte = 0;
/* --EXTRA_BYTE */
SCI_TC_PUT(dmar,count);
/* regs->sci_icmd = 0;*/
SCI_DMADR_PUT(dmar,dma_ptr);
delay_1p2_us();
SCI_ACK(regs,SCSI_PHASE_DATAI);
SCI_CLR_INTR(regs);
regs->sci_mode |= (advance_script ? SCI_MODE_DMA
: (SCI_MODE_DMA|SCI_MODE_DMA_IE));
dmar->sci_dma_dir = SCI_DMA_DIR_READ;
regs->sci_irecv = 1;
return advance_script;
}
/* send data to target. Called in three different ways:
(a) to start transfer (b) to restart a bigger-than-8k
transfer (c) after reconnection
*/
int sci_delay = 1;
boolean_t
sci_dma_out( sci, csr, bs)
register sci_softc_t sci;
{
register sci_padded_regmap_t *regs = sci->regs;
register sci_dmaregs_t dmar = sci->dmar;
register char *dma_ptr;
register target_info_t *tgt;
boolean_t advance_script = TRUE;
int count = sci->out_count;
spl_t s;
register int tmp;
LOG(0xF,"dma_out");
tgt = sci->active_target;
sci->state &= ~SCI_STATE_DMA_IN;
if (sci->out_count == 0) {
/*
* Nothing committed: either just sent the
* command or reconnected
*/
register int remains;
/* ought to stop dma to start another */
regs->sci_mode &= ~ (SCI_MODE_DMA|SCI_MODE_DMA_IE);
dmar->sci_dma_dir = SCI_DMA_DIR_READ;/*hold it */
regs->sci_icmd = SCI_ICMD_DATA;
SCI_ACK(regs,SCSI_PHASE_DATAO);
count = tgt->transient_state.out_count;
count = u_min(count, (PER_TGT_BURST_SIZE));
remains = PER_TGT_BUFF_SIZE - tgt->transient_state.dma_offset;
count = u_min(count, remains);
/* common case of 8k-or-less write ? */
advance_script = (tgt->transient_state.out_count == count);
} else {
/*
* We sent some data.
* Also, take care of bogus interrupts
*/
register int offset, xferred;
if (sci_delay & 1) delay(1000);
/* ought to stop dma to start another */
regs->sci_mode &= ~ (SCI_MODE_DMA|SCI_MODE_DMA_IE);
dmar->sci_dma_dir = SCI_DMA_DIR_READ;/*hold it */
/* regs->sci_icmd = SCI_ICMD_DATA; */
SCI_TC_GET(dmar,xferred);
if (xferred) printf("{A %x}", xferred);
xferred = sci->out_count - xferred;
assert(xferred > 0);
tgt->transient_state.out_count -= xferred;
assert(tgt->transient_state.out_count > 0);
offset = tgt->transient_state.dma_offset;
tgt->transient_state.dma_offset += xferred;
count = u_min(tgt->transient_state.out_count, (PER_TGT_BURST_SIZE));
if (tgt->transient_state.dma_offset == PER_TGT_BUFF_SIZE) {
tgt->transient_state.dma_offset = 0;
} else {
register int remains;
remains = PER_TGT_BUFF_SIZE - tgt->transient_state.dma_offset;
count = u_min(count, remains);
}
/* last chunk ? */
if (tgt->transient_state.out_count == count)
goto quickie;
/* ship some more */
dma_ptr = tgt->dma_ptr +
tgt->transient_state.cmd_count + tgt->transient_state.dma_offset;
sci->out_count = count;
/*
* Mistery: sometimes the first byte
* of an 8k chunk is missing from the tape, it must
* be that somehow touching the 5380 registers
* after the dma engine is ready screws up: false DRQ?
*/
s = splhigh();
SCI_TC_PUT(dmar,count);
/* SCI_CLR_INTR(regs);*/
regs->sci_mode = SCI_MODE_PAR_CHK | SCI_MODE_DMA |
SCI_MODE_MONBSY | SCI_MODE_DMA_IE;
/* regs->sci_icmd = SCI_ICMD_DATA;*/
dmar->sci_dma_dir = SCI_DMA_DIR_WRITE;
SCI_DMADR_PUT(dmar,dma_ptr);
delay_1p2_us();
regs->sci_dma_send = 1;
splx(s);
/* copy some more data */
sci_copyout(tgt, offset, xferred);
return FALSE;
}
quickie:
sci->out_count = count;
dma_ptr = tgt->dma_ptr +
tgt->transient_state.cmd_count + tgt->transient_state.dma_offset;
tmp = (advance_script ?
SCI_MODE_PAR_CHK|SCI_MODE_DMA|SCI_MODE_MONBSY:
SCI_MODE_PAR_CHK|SCI_MODE_DMA|SCI_MODE_MONBSY|SCI_MODE_DMA_IE);
s = splhigh();
SCI_TC_PUT(dmar,count);
/* SCI_CLR_INTR(regs);*/
regs->sci_mode = tmp;
/* regs->sci_icmd = SCI_ICMD_DATA;*/
SCI_DMADR_PUT(dmar,dma_ptr);
delay_1p2_us();
dmar->sci_dma_dir = SCI_DMA_DIR_WRITE;
regs->sci_dma_send = 1;
splx(s);
return advance_script;
}
/* disconnect-reconnect ops */
/* get the message in via dma */
boolean_t
sci_msg_in(sci, csr, bs)
register sci_softc_t sci;
{
register target_info_t *tgt;
char *dma_ptr;
register sci_padded_regmap_t *regs = sci->regs;
register sci_dmaregs_t dmar = sci->dmar;
LOG(0x15,"msg_in");
tgt = sci->active_target;
dma_ptr = tgt->dma_ptr;
/* We would clobber the data for READs */
if (sci->state & SCI_STATE_DMA_IN) {
register int offset;
offset = tgt->transient_state.cmd_count + tgt->transient_state.dma_offset;
dma_ptr += offset;
}
/* ought to stop dma to start another */
regs->sci_mode &= ~ (SCI_MODE_DMA|SCI_MODE_DMA_IE);
regs->sci_icmd = 0;
/* We only really expect two bytes */
SCI_TC_PUT(dmar,sizeof(scsi_command_group_0));
/* regs->sci_icmd = 0*/
SCI_DMADR_PUT(dmar,dma_ptr);
delay_1p2_us();
SCI_ACK(regs,SCSI_PHASE_MSG_IN);
SCI_CLR_INTR(regs);
regs->sci_mode |= SCI_MODE_DMA;
dmar->sci_dma_dir = SCI_DMA_DIR_READ;
regs->sci_irecv = 1;
return TRUE;
}
/* check the message is indeed a DISCONNECT */
boolean_t
sci_disconnect(sci, csr, bs)
register sci_softc_t sci;
{
register int len;
boolean_t ok = FALSE;
register sci_dmaregs_t dmar = sci->dmar;
register char *msgs;
unsigned int offset;
SCI_TC_GET(dmar,len);
len = sizeof(scsi_command_group_0) - len;
PRINT(("{G%d}",len));
/* wherever it was, take it from there */
SCI_DMADR_GET(dmar,offset);
msgs = (char*)sci->buff + offset - len;
if ((len == 0) || (len > 2))
ok = FALSE;
else {
/* A SDP message preceeds it in non-completed READs */
ok = ((msgs[0] == SCSI_DISCONNECT) || /* completed op */
((msgs[0] == SCSI_SAVE_DATA_POINTER) && /* incomplete */
(msgs[1] == SCSI_DISCONNECT)));
}
if (!ok)
printf("[tgt %d bad msg (%d): %x]",
sci->active_target->target_id, len, *msgs);
return TRUE;
}
/* save all relevant data, free the BUS */
boolean_t
sci_disconnected(sci, csr, bs)
register sci_softc_t sci;
{
register target_info_t *tgt;
sci_padded_regmap_t *regs = sci->regs;
regs->sci_mode &= ~(SCI_MODE_MONBSY|SCI_MODE_DMA);
SCI_CLR_INTR(regs);/*retriggered by MONBSY cuz intr routine did CLR */
SCI_ACK(regs,SCI_PHASE_DISC);
LOG(0x16,"disconnected");
sci_disconnect(sci,csr,bs);
tgt = sci->active_target;
tgt->flags |= TGT_DISCONNECTED;
tgt->transient_state.handler = sci->error_handler;
/* the rest has been saved in sci_err_disconn() */
PRINT(("{D%d}", tgt->target_id));
sci_release_bus(sci);
return FALSE;
}
/* get reconnect message, restore BUS */
boolean_t
sci_reconnect(sci, csr, bs)
register sci_softc_t sci;
{
register target_info_t *tgt;
sci_padded_regmap_t *regs;
register int id;
int msg;
LOG(0x17,"reconnect");
if (sci->wd.nactive == 0) {
LOG(2,"SPURIOUS");
return FALSE;
}
regs = sci->regs;
regs->sci_mode &= ~SCI_MODE_PAR_CHK;
id = regs->sci_data;/*parity!*/
regs->sci_mode |= SCI_MODE_PAR_CHK;
/* xxx check our id is in there */
id &= ~(1 << sci->sc->initiator_id);
{
register int i;
for (i = 0; i < 8; i++)
if (id & (1 << i)) break;
if (i == 8) {printf("{P%x}", id);return;}
id = i;
}
regs->sci_icmd = SCI_ICMD_BSY;
while (regs->sci_bus_csr & SCI_BUS_SEL)
;
regs->sci_icmd = 0;
delay_1p2_us();
while ( ((regs->sci_bus_csr & SCI_BUS_BSY) == 0) &&
((regs->sci_bus_csr & SCI_BUS_BSY) == 0) &&
((regs->sci_bus_csr & SCI_BUS_BSY) == 0))
;
regs->sci_mode |= SCI_MODE_MONBSY;
/* Now should wait for correct phase: REQ signals it */
while ( ((regs->sci_bus_csr & SCI_BUS_REQ) == 0) &&
((regs->sci_bus_csr & SCI_BUS_REQ) == 0) &&
((regs->sci_bus_csr & SCI_BUS_REQ) == 0))
;
/*
* See if this reconnection collided with a selection attempt
*/
if (sci->state & SCI_STATE_BUSY)
sci->state |= SCI_STATE_COLLISION;
sci->state |= SCI_STATE_BUSY;
/* Get identify msg */
bs = regs->sci_bus_csr;
if (SCI_CUR_PHASE(bs) != SCSI_PHASE_MSG_IN) gimmeabreak();
SCI_ACK(regs,SCSI_PHASE_MSG_IN);
msg = 0;
sci_data_in(regs, SCSI_PHASE_MSG_IN, 1, &msg);
regs->sci_mode = SCI_MODE_PAR_CHK|SCI_MODE_DMA|SCI_MODE_MONBSY;
regs->sci_sel_enb = 0;
if (msg != SCSI_IDENTIFY)
printf("{I%x %x}", id, msg);
tgt = sci->sc->target[id];
if (id > 7 || tgt == 0) panic("sci_reconnect");
PRINT(("{R%d}", id));
if (sci->state & SCI_STATE_COLLISION)
PRINT(("[B %d-%d]", sci->active_target->target_id, id));
LOG(0x80+id,0);
sci->active_target = tgt;
tgt->flags &= ~TGT_DISCONNECTED;
sci->script = tgt->transient_state.script;
sci->error_handler = tgt->transient_state.handler;
sci->in_count = 0;
sci->out_count = 0;
/* Should get a phase mismatch when tgt changes phase */
return TRUE;
}
/* do the synch negotiation */
boolean_t
sci_dosynch( sci, csr, bs)
register sci_softc_t sci;
{
/*
* Phase is MSG_OUT here, cmd has not been xferred
*/
int len;
register target_info_t *tgt;
register sci_padded_regmap_t *regs = sci->regs;
unsigned char off, icmd;
register unsigned char *p;
regs->sci_mode |= SCI_MODE_MONBSY;
LOG(0x11,"dosync");
/* ATN still asserted */
SCI_ACK(regs,SCSI_PHASE_MSG_OUT);
tgt = sci->active_target;
tgt->flags |= TGT_DID_SYNCH; /* only one chance */
tgt->flags &= ~TGT_TRY_SYNCH;
p = (unsigned char *)tgt->cmd_ptr + tgt->transient_state.cmd_count +
tgt->transient_state.dma_offset;
p[0] = SCSI_IDENTIFY;
p[1] = SCSI_EXTENDED_MESSAGE;
p[2] = 3;
p[3] = SCSI_SYNC_XFER_REQUEST;
/* We cannot run synchronous */
#define sci_to_scsi_period(x) 0xff
#define scsi_period_to_sci(x) (x)
off = 0;
p[4] = sci_to_scsi_period(sci_min_period);
p[5] = off;
/* xfer all but last byte with ATN set */
sci_data_out(regs, SCSI_PHASE_MSG_OUT,
sizeof(scsi_synch_xfer_req_t), p);
icmd = regs->sci_icmd & ~(SCI_ICMD_DIFF|SCI_ICMD_TEST);
icmd &= ~SCI_ICMD_ATN;
regs->sci_icmd = icmd;
sci_data_out(regs, SCSI_PHASE_MSG_OUT, 1,
&p[sizeof(scsi_synch_xfer_req_t)]);
/* wait for phase change */
while (regs->sci_csr & SCI_CSR_PHASE_MATCH)
;
bs = regs->sci_bus_csr;
/* The standard sez there nothing else the target can do but.. */
if (SCI_CUR_PHASE(bs) != SCSI_PHASE_MSG_IN)
panic("sci_dosync");/* XXX put offline */
msgin:
/* ack */
SCI_ACK(regs,SCSI_PHASE_MSG_IN);
/* get answer */
len = sizeof(scsi_synch_xfer_req_t);
len = sci_data_in(regs, SCSI_PHASE_MSG_IN, len, p);
/* do not cancel the phase mismatch interrupt ! */
/* look at the answer and see if we like it */
if (len || (p[0] != SCSI_EXTENDED_MESSAGE)) {
/* did not like it at all */
printf(" did not like SYNCH xfer ");
} else {
/* will NOT do synch */
printf(" but we cannot do SYNCH xfer ");
tgt->sync_period = scsi_period_to_sci(p[3]);
tgt->sync_offset = p[4];
/* sanity */
if (tgt->sync_offset != 0)
printf(" ?OFFSET %x? ", tgt->sync_offset);
}
/* wait for phase change */
while (regs->sci_csr & SCI_CSR_PHASE_MATCH)
;
bs = regs->sci_bus_csr;
/* phase should be command now */
/* continue with simple command script */
sci->error_handler = sci_err_generic;
sci->script = sci_script_cmd;
if (SCI_CUR_PHASE(bs) == SCSI_PHASE_CMD )
return TRUE;
/* sci->script++;*/
if (SCI_CUR_PHASE(bs) == SCSI_PHASE_STATUS )
return TRUE; /* intr is pending */
sci->script++;
if (SCI_CUR_PHASE(bs) == SCSI_PHASE_MSG_IN )
return TRUE;
if ((bs & SCI_BUS_BSY) == 0) /* uhu? disconnected */
return sci_end_transaction(sci, regs->sci_csr, regs->sci_bus_csr);
panic("sci_dosynch");
return FALSE;
}
/*
* The bus was reset
*/
sci_bus_reset(sci)
register sci_softc_t sci;
{
register target_info_t *tgt;
register sci_padded_regmap_t *regs = sci->regs;
int i;
LOG(0x21,"bus_reset");
/*
* Clear bus descriptor
*/
sci->script = 0;
sci->error_handler = 0;
sci->active_target = 0;
sci->next_target = 0;
sci->state = 0;
queue_init(&sci->waiting_targets);
sci->wd.nactive = 0;
sci_reset(sci, TRUE);
printf("sci%d: (%d) bus reset ", sci->sc->masterno, ++sci->wd.reset_count);
delay(scsi_delay_after_reset); /* some targets take long to reset */
if (sci->sc == 0) /* sanity */
return;
scsi_bus_was_reset(sci->sc);
}
/*
* Error handlers
*/
/*
* Generic, default handler
*/
boolean_t
sci_err_generic(sci, csr, bs)
register sci_softc_t sci;
{
register int cond = sci->script->condition;
LOG(0x10,"err_generic");
if (SCI_CUR_PHASE(bs) == SCSI_PHASE_STATUS)
return sci_err_to_status(sci, csr, bs);
gimmeabreak();
return FALSE;
}
/*
* Handle generic errors that are reported as
* an unexpected change to STATUS phase
*/
sci_err_to_status(sci, csr, bs)
register sci_softc_t sci;
{
script_t scp = sci->script;
LOG(0x20,"err_tostatus");
while (SCSI_PHASE(scp->condition) != SCSI_PHASE_STATUS)
scp++;
sci->script = scp;
#if 0
/*
* Normally, we would already be able to say the command
* is in error, e.g. the tape had a filemark or something.
* But in case we do disconnected mode WRITEs, it is quite
* common that the following happens:
* dma_out -> disconnect -> reconnect
* and our script might expect at this point that the dma
* had to be restarted (it didn't know it was completed
* because the tape record is shorter than we asked for).
* And in any event.. it is both correct and cleaner to
* declare error iff the STATUS byte says so.
*/
sci->done = SCSI_RET_NEED_SENSE;
#endif
return TRUE;
}
/*
* Watch for a disconnection
*/
boolean_t
sci_err_disconn(sci, csr, bs)
register sci_softc_t sci;
{
register sci_padded_regmap_t *regs;
register sci_dmaregs_t dmar = sci->dmar;
register target_info_t *tgt;
int count;
LOG(0x18,"err_disconn");
if (SCI_CUR_PHASE(bs) != SCSI_PHASE_MSG_IN)
return sci_err_generic(sci, csr, bs);
regs = sci->regs;
tgt = sci->active_target;
switch (SCSI_PHASE(sci->script->condition)) {
case SCSI_PHASE_DATAO:
LOG(0x1b,"+DATAO");
if (sci_delay & 1) delay(1000);
/* Stop dma */
regs->sci_icmd = 0;
regs->sci_mode &= ~(SCI_MODE_DMA|SCI_MODE_DMA_IE);
dmar->sci_dma_dir = SCI_DMA_DIR_READ;/* make sure we steal not */
if (sci->out_count) {
register int xferred, offset;
SCI_TC_GET(dmar,xferred);
if (scsi_debug)
printf("{O %x %x}", xferred, sci->out_count);
/* 5380 prefetches */
xferred = sci->out_count - xferred - 1;
/* assert(xferred > 0);*/
tgt->transient_state.out_count -= xferred;
assert(tgt->transient_state.out_count > 0);
offset = tgt->transient_state.dma_offset;
tgt->transient_state.dma_offset += xferred;
if (tgt->transient_state.dma_offset >= PER_TGT_BUFF_SIZE)
tgt->transient_state.dma_offset = 0;
sci_copyout( tgt, offset, xferred);
}
tgt->transient_state.script = sci_script_data_out;
break;
case SCSI_PHASE_DATAI:
LOG(0x19,"+DATAI");
/* Stop dma */
regs->sci_icmd = 0;
regs->sci_mode &= ~(SCI_MODE_DMA|SCI_MODE_DMA_IE);
dmar->sci_dma_dir = SCI_DMA_DIR_WRITE;/* make sure we steal not */
if (sci->in_count) {
register int offset, xferred;
/* unsigned char extrab = regs->sci_idata;*/
SCI_TC_GET(dmar,xferred);
/* ++EXTRA_BYTE */
if (scsi_debug)
printf("{A %x %x %x}", xferred, sci->in_count, sci->extra_byte);
xferred = sci->in_count - xferred - sci->extra_byte;
/* ++EXTRA_BYTE */
assert(xferred > 0);
tgt->transient_state.in_count -= xferred;
assert(tgt->transient_state.in_count > 0);
offset = tgt->transient_state.dma_offset;
tgt->transient_state.dma_offset += xferred;
if (tgt->transient_state.dma_offset >= PER_TGT_BUFF_SIZE)
tgt->transient_state.dma_offset = 0;
/* copy what we got */
sci_copyin( tgt, offset, xferred, 0, 0/*extrab*/);
}
tgt->transient_state.script = sci_script_data_in;
break;
case SCSI_PHASE_STATUS:
/* will have to restart dma */
SCI_TC_GET(dmar,count);
if (sci->state & SCI_STATE_DMA_IN) {
register int offset, xferred;
/* unsigned char extrab = regs->sci_idata;*/
LOG(0x1a,"+STATUS+R");
/* Stop dma */
regs->sci_icmd = 0;
regs->sci_mode &= ~(SCI_MODE_DMA|SCI_MODE_DMA_IE);
dmar->sci_dma_dir = SCI_DMA_DIR_WRITE;/* make sure we steal not */
/* ++EXTRA_BYTE */
if (scsi_debug)
printf("{A %x %x %x}", count, sci->in_count, sci->extra_byte);
xferred = sci->in_count - count - sci->extra_byte;
/* ++EXTRA_BYTE */
assert(xferred > 0);
tgt->transient_state.in_count -= xferred;
/* assert(tgt->transient_state.in_count > 0);*/
offset = tgt->transient_state.dma_offset;
tgt->transient_state.dma_offset += xferred;
if (tgt->transient_state.dma_offset >= PER_TGT_BUFF_SIZE)
tgt->transient_state.dma_offset = 0;
/* copy what we got */
sci_copyin( tgt, offset, xferred, 0, 0/*/extrab*/);
tgt->transient_state.script = sci_script_data_in;
if (tgt->transient_state.in_count == 0)
tgt->transient_state.script++;
} else {
LOG(0x1d,"+STATUS+W");
if (sci_delay & 1) delay(1000);
/* Stop dma */
regs->sci_icmd = 0;
regs->sci_mode &= ~(SCI_MODE_DMA|SCI_MODE_DMA_IE);
dmar->sci_dma_dir = SCI_DMA_DIR_READ;/* make sure we steal not */
if (scsi_debug)
printf("{O %x %x}", count, sci->out_count);
if ((count == 0) && (tgt->transient_state.out_count == sci->out_count)) {
/* all done */
tgt->transient_state.script = &sci_script_data_out[1];
tgt->transient_state.out_count = 0;
} else {
register int xferred, offset;
/* how much we xferred */
xferred = sci->out_count - count - 1;/*prefetch*/
tgt->transient_state.out_count -= xferred;
assert(tgt->transient_state.out_count > 0);
offset = tgt->transient_state.dma_offset;
tgt->transient_state.dma_offset += xferred;
if (tgt->transient_state.dma_offset >= PER_TGT_BUFF_SIZE)
tgt->transient_state.dma_offset = 0;
sci_copyout( tgt, offset, xferred);
tgt->transient_state.script = sci_script_data_out;
}
sci->out_count = 0;
}
break;
default:
gimmeabreak();
}
sci->extra_byte = 0;
/* SCI_ACK(regs,SCSI_PHASE_MSG_IN); later */
(void) sci_msg_in(sci,csr,bs);
regs->sci_sel_enb = (1 << sci->sc->initiator_id);
sci->script = sci_script_disconnect;
return FALSE;
}
/*
* Watchdog
*
*/
sci_reset_scsibus(sci)
register sci_softc_t sci;
{
register target_info_t *tgt = sci->active_target;
if (tgt) {
int cnt;
SCI_TC_GET(sci->dmar,cnt);
log( LOG_KERN,
"Target %d was active, cmd x%x in x%x out x%x Sin x%x Sou x%x dmalen x%x\n",
tgt->target_id, tgt->cur_cmd,
tgt->transient_state.in_count, tgt->transient_state.out_count,
sci->in_count, sci->out_count, cnt);
}
sci->regs->sci_icmd = SCI_ICMD_RST;
delay(25);
}
/*
* Copy routines
*/
/*static*/
sci_copyin(tgt, offset, len, isaobb, obb)
register target_info_t *tgt;
unsigned char obb;
{
register char *from, *to;
register int count;
count = tgt->transient_state.copy_count;
from = tgt->cmd_ptr + offset;
to = tgt->ior->io_data + count;
tgt->transient_state.copy_count = count + len;
bcopy( from, to, len);
/* check for last, poor little odd byte */
if (isaobb) {
to += len;
to[-1] = obb;
}
}
/*static*/
sci_copyout( tgt, offset, len)
register target_info_t *tgt;
{
register char *from, *to;
register int count, olen;
unsigned char c;
char *p;
count = tgt->ior->io_count - tgt->transient_state.copy_count;
if (count > 0) {
len = u_min(count, len);
offset += tgt->transient_state.cmd_count;
count = tgt->transient_state.copy_count;
tgt->transient_state.copy_count = count + len;
from = tgt->ior->io_data + count;
to = tgt->cmd_ptr + offset;
bcopy(from, to, len);
}
}
#endif /*NSCI > 0*/
|