summaryrefslogtreecommitdiff
path: root/sys/arch/vax/uba/tmscp.c
blob: ef490d93e3d6d3a6ae40b9eb46438a5d87be50a9 (plain)
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
/*	$NetBSD: tmscp.c,v 1.12 1996/04/08 18:37:30 ragge Exp $ */

/*-
 * Copyright (c) 1991 The Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the University of
 *	California, Berkeley and its contributors.
 * 4. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	@(#)tmscp.c	7.16 (Berkeley) 5/9/91
 */

/*
 * sccsid = "@(#)tmscp.c	1.24	(ULTRIX)	1/21/86";
 */

/************************************************************************
 *									*
 *        Licensed from Digital Equipment Corporation 			*
 *                       Copyright (c) 					*
 *               Digital Equipment Corporation				*
 *                   Maynard, Massachusetts 				*
 *                         1985, 1986 					*
 *                    All rights reserved. 				*
 *									*
 *        The Information in this software is subject to change 	*
 *   without notice and should not be construed as a commitment 	*
 *   by  Digital  Equipment  Corporation.   Digital   makes  no 	*
 *   representations about the suitability of this software for 	*
 *   any purpose.  It is supplied "As Is" without expressed  or 	*
 *   implied  warranty. 						*
 *									*
 *        If the Regents of the University of California or its 	*
 *   licensees modify the software in a manner creating  		*
 *   diriviative copyright rights, appropriate copyright  		*
 *   legends may be placed on  the drivative work in addition  		*
 *   to that set forth above. 						*
 *									*
 ************************************************************************
 * 
 * tmscp.c - TMSCP (TK50/TU81) tape device driver
 * 
 * Modification History:
 *
 * 06-Jan-86 - afd
 *	Changed the probe routine to use DELAY (not TODR).  This now
 *	works for MicroVAXen as well.  This eliminates the busy-wait
 *	for MicroVAXen so a dead TK50 controller will not hang autoconf.
 *
 * 06-Dec-85 - afd
 *	Fixed a bug in density selection.  The "set unit characteristics"
 *	command to select density, was clearing the "unit flags" field
 *	where the CACHE bit was for TU81-E.  Now the unit's "format" and
 *	"unitflgs" are saved in tms_info struct.  And are used on STUNT
 *	commands.
 *
 * 19-Oct-85 - afd
 *	Added support to the open routine to allow drives to be opened
 *	for low density (800 or 1600 bpi) use.  When the slave routine
 *	initiates a "get-unit-char" cmd, the format menu for the unit
 *	is saved in the tms_info structure. The format menu is used in the
 *	start routine to select the proper low density.
 *
 * 02-Oct-85 - afd
 *	When a tmscp-type controller is initializing, it is possible for
 *	the sa reg to become 0 between states.  Thus the init code in
 *	the interrupt routine had to be modified to reflect this.
 *
 * 21-Sep-85 - afd
 *	The TK50 declares a serious exception when a tape mark is encountered.
 *	This causes problems to dd (& other UN*X utilities).  So a flag
 *	is set in the rsp() routine when a tape mark is encountered.  If
 *	this flag is set, the start() routine appends the Clear Serious
 *	Exception modifier to the next command.
 *
 * 03-Sep-85 -- jaw
 *	messed up previous edit..
 *
 * 29-Aug-85 - jaw
 *	fixed bugs in 8200 and 750 buffered datapath handling.
 *
 * 06-Aug-85 - afd
 *   1. When repositioning records or files, the count of items skipped
 *	does NOT HAVE to be returned by controllers (& the TU81 doesn't).
 *	So tmscprsp() had to be modified to stop reporting
 *	residual count errors on reposition commands.
 *
 *   2. Fixed bug in the open routine which allowed multiple opens.
 *
 * 18-Jul-85 - afd
 *   1. Need to return status when mt status (or corresponding ioctl) is done.
 *	Save resid, flags, endcode & status in tmscprsp() routine (except on
 *	clear serious exception no-op).  Return these fields when status
 *	ioctl is done (in tmscpcommand()).  How they are returned:
 *		mt_resid = resid
 *		mt_dsreg = flags|endcode
 *		mt_erreg = status
 *
 *   2. Added latent support for enabling/disabling caching.  This is
 *	handled along with all other ioctl commands.
 *
 *   3. Need to issue a no-op on unrecognized ioctl in tmscpstart(), since
 *	we have already commited to issuing a command at that point.
 *
 *   4. In tmscprsp() routine if encode is 0200 (invalid command issued);
 *	We need to: Unlink the buffer from the I/O wait queue,
 *	and signal iodone, so the higher level command can exit!
 *	Just as if it were a valid command.
 *
 * 11-jul-85 -- jaw
 *	fix bua/bda map registers.
 *
 * 19-Jun-85 -- jaw
 *	VAX8200 name change.
 *
 * 06-Jun-85 - jaw
 *	fixes for 8200.
 *
 * 9-Apr-85 - afd
 *	Added timeout code to the probe routine, so if the controller
 *	fails to init in 10 seconds we return failed status.
 *
 * 13-Mar-85 -jaw
 *	Changes for support of the VAX8200 were merged in.
 *
 * 27-Feb-85 -tresvik
 *	Changes for support of the VAX8600 were merged in.
 *
 */

#define NTMS 2 /* XXX - This is _wery_ kludgy! /ragge */

#include "tmscp.h"
#if NTMSCP > 0

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/conf.h>
#include <sys/errno.h>
#include <sys/file.h>
#include <sys/map.h>
#include <sys/ioctl.h>
#include <sys/syslog.h>
#include <sys/mtio.h>
/* #include <sys/cmap.h> */
#include <sys/uio.h>
#include <sys/proc.h>
#include <sys/tprintf.h>
#include <sys/proc.h>

#include <machine/pte.h>
#include <machine/cpu.h>
#include <machine/mtpr.h>
#include <machine/sid.h>

#include <vax/uba/ubareg.h>
#include <vax/uba/ubavar.h>

#define TENSEC	(1000)
#define	TMS_PRI	LOG_INFO

#define NRSPL2  3               /* log2 number of response packets */
#define NCMDL2  3               /* log2 number of command packets */
#define NRSP    (1<<NRSPL2)
#define NCMD    (1<<NCMDL2)

#include <vax/uba/tmscpreg.h>
#include <vax/vax/tmscpinf.h>
#include <vax/vax/mscpvar.h>

int	tmscp_match __P((struct device *, void *, void *));
void	tmscp_attach __P((struct device *, struct device *, void *));
void	tmscpstrategy __P((struct buf *));

struct	cfdriver tmscp_cd = {
	NULL, "tmscp", DV_DULL
};

struct	cfattach tmscp_ca = {
	sizeof(struct device), tmscp_match, tmscp_attach
};

/* Software state per controller */

struct tmscp_softc {
	short   sc_state;       /* state of controller */
	short   sc_mapped;      /* Unibus map allocated for tmscp struct? */
	int     sc_ubainfo;     /* Unibus mapping info */
	struct	tmscp *sc_tmscp;   /* Unibus address of tmscp struct */
	int     sc_ivec;        /* interrupt vector address */
	short   sc_credits;     /* transfer credits */
	short   sc_lastcmd;     /* pointer into command ring */
	short   sc_lastrsp;     /* pointer into response ring */
	short   sc_ipl;		/* interrupt priority (Q-bus) */
} tmscp_softc[NTMSCP];

struct tmscp {
	struct tmscpca	tmscp_ca;         /* communications area */
	struct mscp	tmscp_rsp[NRSP];  /* response packets */
	struct mscp	tmscp_cmd[NCMD];  /* command packets */
} tmscp[NTMSCP];

int	tmscpprobe __P((caddr_t, int, struct uba_ctlr *, struct  uba_softc *));
int	tmscpslave __P((struct uba_device *, caddr_t));
int	tmscpinit __P((int));
void	tmscpattach __P((struct uba_device *));
void	tmscpintr __P((int));
void	tmscprsp __P((struct uba_ctlr *, struct tmscp *,
	    struct tmscp_softc *, int));
void	tmscpstart __P((struct uba_ctlr *));
void	tmscpcommand __P((dev_t, int, int));
struct mscp *tmscpgetcp __P((struct uba_ctlr *));
void	errinfo __P((int));
int	tmscpcmd __P((int, struct tmscp *, struct tmscpdevice *));

int	tmscpopen __P((dev_t, int, int, struct proc *p));
int	tmscpclose __P((dev_t, int, int, struct proc *p));
void	tmscpstrategy __P((struct buf *));
int	tmscpread __P((dev_t, struct uio *));
int	tmscpwrite __P((dev_t, struct uio *));
int	tmscpdump __P((dev_t, daddr_t, caddr_t, size_t));
int	tmscpioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
void	tmscpreset __P((int));

/*
 * Per drive-unit info
 */
struct tms_info {
	daddr_t		tms_dsize;	/* Max user size from online pkt */
	unsigned	tms_type;	/* Drive type int field  */
	int		tms_resid;	/* residual from last xfer */
	u_char		tms_endcode;	/* last command endcode */
	u_char		tms_flags;	/* last command end flags */
	unsigned	tms_status;	/* Command status from last command */
	char		tms_openf;	/* lock against multiple opens */
	char		tms_lastiow;	/* last op was a write */
	char		tms_serex;	/* set when serious exception occurs */
	char		tms_clserex;	/* set when serex being cleared by no-op */
	short		tms_fmtmenu;	/* the unit's format (density) menu */
	short		tms_unitflgs;	/* unit flag parameters */
	short		tms_format;	/* the unit's current format (density) */
	tpr_t 		tms_tpr;	/* tprintf handle */
} tms_info[NTMS];

void	tmserror __P((struct uba_ctlr *, struct mslg *));


struct uba_ctlr *tmscpminfo[NTMSCP];
struct uba_device *tmsdinfo[NTMS];
/* 
 * ifdef other tmscp devices here if they allow more than 1 unit/controller
 */
struct uba_device *tmscpip[NTMSCP][1];
struct buf ctmscpbuf[NTMSCP];		/* internal cmd buffer (for ioctls) */
struct buf tmsutab[NTMS];		/* Drive queue */
struct buf tmscpwtab[NTMSCP];		/* I/O wait queue, per controller */
int    tmscpmicro[NTMSCP];		/* to store microcode level */
short  utoctlr[NTMS];			/* Slave unit to controller mapping */
					/* filled in by the slave routine */

/* Bits in minor device */
#define	TMSUNIT(dev)	(minor(dev)&03)
#define	T_HIDENSITY	010

/* Slave unit to controller mapping */
#define TMSCPCTLR(dev)	(utoctlr[TMSUNIT(dev)])

/*
 * Internal (ioctl) command codes (these must also be declared in the
 * tmscpioctl routine).  These correspond to ioctls in mtio.h
 */
#define TMS_WRITM	0		/* write tape mark */
#define TMS_FSF		1		/* forward space file */
#define TMS_BSF		2		/* backward space file */
#define TMS_FSR		3		/* forward space record */
#define TMS_BSR		4		/* backward space record */
#define TMS_REW		5		/* rewind tape */
#define TMS_OFFL	6		/* rewind tape & mark unit offline */
#define TMS_SENSE	7		/* noop - do a get unit status */
#define TMS_CACHE	8		/* enable cache */
#define TMS_NOCACHE	9		/* disable cache */
/* These go last: after all real mt cmds, just bump the numbers up */
#define TMS_CSE		10		/* clear serious exception */
#define TMS_LOWDENSITY	11		/* set unit to low density */
#define TMS_HIDENSITY	12		/* set unit to high density */

/*
 * Controller states
 */
#define S_IDLE  0               /* hasn't been initialized */
#define S_STEP1 1               /* doing step 1 init */
#define S_STEP2 2               /* doing step 2 init */
#define S_STEP3 3               /* doing step 3 init */
#define S_SCHAR 4               /* doing "set controller characteristics" */
#define S_RUN   5               /* running */

int     tmscperror = 0;		/* causes hex dump of packets */
int	tmscp_cp_wait = 0;	/* Something to wait on for command */
				/* packets and or credits. */
extern	int	hz;		/* Should find the right include */

#ifdef	DEBUG
#define printd if (tmscpdebug) printf
int tmscpdebug = 1;
#define	printd10 if(tmscpdebug >= 10) printf
#endif 

#define DRVNAME "tms"
#define CTRLNAME "tmscp"

u_short tmscpstd[] = { 0174504, 0 };
struct  uba_driver tmscpdriver =
{ tmscpprobe, tmscpslave, tmscpattach, 0, tmscpstd, DRVNAME, tmsdinfo, CTRLNAME
, tmscpminfo, 0};

#define b_qsize         b_resid         /* queue size per drive, in tmsutab */
#define b_ubinfo        b_resid         /* Unibus mapping info, per buffer */


/*************************************************************************/

#define DELAYTEN 1000
extern	struct cfdriver uba_cd;

/*
 * Unfortunately qbgetpri can't be used because the TK50 doesn't flip the
 * TMSCP_STEP2 flag in the tmscpsa register until after the pending interrupt
 * has been acknowledged by the cpu. If you are at spl6(), the TMSCP_STEP2
 * flag never gets set and you return (0).
 */
int
tmscpprobe(reg, ctlr, um, uh)
	caddr_t reg;		/* address of the IP register */
	int ctlr;		/* index of controller in the tmscp_softc array */
	struct uba_ctlr *um;
	struct  uba_softc *uh;
{
	/* register int br, cvec; MUST be 1st (r11 & r10): IPL and intr vec */
	register struct tmscp_softc *sc = &tmscp_softc[ctlr];
				/* ptr to software controller structure */
	volatile struct tmscpdevice *tmscpaddr;
	int count;		/* for probe delay time out */
	struct	uba_softc *ubasc;

#	ifdef lint
	br = 0; cvec = br; br = cvec; reg = reg;
	tmscpreset(0); tmscpintr(0);
#	endif

	tmscpminfo[ctlr] = um;
	tmscpaddr = (struct tmscpdevice *) reg;
	/* 
	 * Set host-settable interrupt vector.
	 * Assign 0 to the ip register to start the tmscp-device initialization.
	 * The device is not really initialized at this point, this is just to
	 * find out if the device exists.
	 */
	ubasc = uba_cd.cd_devs[0]; /* XXX */
	sc->sc_ivec = (ubasc->uh_lastiv -= 4);
	tmscpaddr->tmscpip = 0;

	count=0;
	while(count < DELAYTEN)
		{	/* wait for at most 10 secs */
		if((tmscpaddr->tmscpsa & TMSCP_STEP1) != 0)
			break;
		DELAY(10000);
		count=count+1;
		}
	if (count == DELAYTEN)
		return(0);		

	tmscpaddr->tmscpsa = TMSCP_ERR|(NCMDL2<<11)|(NRSPL2<<8)|TMSCP_IE|(sc->sc_ivec/4);

	count=0;
	while(count < DELAYTEN)
		{
		if((tmscpaddr->tmscpsa & TMSCP_STEP2) != 0)
			break;
		DELAY(10000);
		count = count+1;
		}
	if (count == DELAYTEN)
		return(0);

#ifdef QBA
	sc->sc_ipl = 0x15;
#endif
	return(sizeof (struct tmscpdevice));
}

/*
 * Try to find a slave (a drive) on the controller.
 * If the controller is not in the run state, call init to initialize it.
 */
int
tmscpslave (ui, reg)
	struct uba_device *ui;	/* ptr to the uba device structure */
	caddr_t reg;		/* addr of the device controller */
{
	register struct uba_ctlr *um = tmscpminfo[ui->ui_ctlr];
	volatile struct tmscp_softc *sc = &tmscp_softc[ui->ui_ctlr];
	volatile struct tms_info *tms = &tms_info[ui->ui_unit];
	volatile struct tmscpdevice *tmscpaddr;	/* ptr to IP & SA */
	volatile struct mscp *mp;
	volatile int i;			/* Something to write into to start */
					/* the tmscp polling */

#	ifdef lint
	reg = reg;
#	endif
	tmscpaddr = (struct tmscpdevice *)um->um_addr;
	/* 
	 * If its not in the run state, start the initialization process
	 * (tmscpintr will complete it);  if the initialization doesn't start;
	 * then return.
	 */
	if(sc->sc_state != S_RUN)
		{
#		ifdef DEBUG
		printd("tmscpslave: ctlr not running: calling init \n");
#		endif	
		if(!tmscpinit(ui->ui_ctlr))
	    		return(0);
		}
	/*
	 * Wait for the controller to come into the run state or go idle.
	 * If it goes idle return.
	 */
#	ifdef DEBUG
	i=1;
#	endif	
	while(sc->sc_state != S_RUN && sc->sc_state != S_IDLE)
#		ifdef DEBUG
		if (tmscpaddr->tmscpsa & TMSCP_ERR && i)
	    		{
	    		printd("tmscp-device: fatal error (%o)\n", tmscpaddr->tmscpsa&0xffff);
	    		i=0;
	   		 }
#		endif	
		;	/* wait */
	if(sc->sc_state == S_IDLE)
		{	/* The tmscp device failed to initialize */
		printf("tmscp controller failed to init\n");
		return(0);
		}
	/* The controller is up so see if the drive is there */
	if(0 == (mp = tmscpgetcp(um)))
		{
		printf("tmscp can't get command packet\n");
		return(0);
		}
	/* Need to determine the drive type for generic driver */
	mp->mscp_opcode = M_OP_GTUNT;	/* This should give us the device type */
	mp->mscp_unit = ui->ui_slave;
	mp->mscp_cmdref = (long) ui->ui_slave;
	tms->tms_status = 0;	/* set to zero */
	tmscpip[ui->ui_ctlr][ui->ui_slave] = ui;
	*((long *) mp->mscp_dscptr ) |= TMSCP_OWN | TMSCP_INT;/* maybe we should poll*/
	i = tmscpaddr->tmscpip;
#ifdef lint
	i = i;
#endif
	while(!tms->tms_status)
		;				/* Wait for some status */
#	ifdef DEBUG
	printd("tmscpslave: status = %o\n",tms->tms_status & M_ST_MASK);
#	endif
	tmscpip[ui->ui_ctlr][ui->ui_slave] = 0;
	if(!tms->tms_type)			/* packet from a GTUNT */
		return(0);			/* Failed No such drive */
	else
		return(1);			/* Got it and it is there */
}


/* 
 * Set ui flags to zero to show device is not online & set tmscpip.
 * Unit to Controller mapping is set up here.
 * Open routine will issue the online command, later.
 */
void
tmscpattach (ui)
	register struct uba_device *ui;		/* ptr to unibus dev struct */
{

	ui->ui_flags = 0;
	tmscpip[ui->ui_ctlr][ui->ui_slave] = ui;
#	ifdef DEBUG
	/* 
	 * Check to see if the drive is available.
	 * If not then just print debug.
	 */
	if(tms_info[ui->ui_unit].tms_status != M_ST_AVLBL)
		printd("tmscpattach: unavailable \n");
#	endif	
	utoctlr[ui->ui_unit] = ui->ui_ctlr;
}


/*
 * TMSCP interrupt routine.
 */
void
tmscpintr(d)
	int d;
{
	volatile struct uba_ctlr *um = tmscpminfo[d];
	volatile struct tmscpdevice *tmscpaddr =
		(struct tmscpdevice *)um->um_addr;
	struct buf *bp;
	volatile int i;
	volatile struct tmscp_softc *sc = &tmscp_softc[d];
	struct tmscp *tm = &tmscp[d];
	struct tmscp *ttm;
	volatile struct mscp *mp;

#	ifdef DEBUG
	printd10("tmscpintr: state %d, tmscpsa %o\n", sc->sc_state, tmscpaddr->tmscpsa);
#	endif	

#ifdef QBA
	if (cpunumber == VAX_78032)
		splx(sc->sc_ipl);
#endif
	/*
	 * How the interrupt is handled depends on the state of the controller.
	 */
	switch (sc->sc_state) {

	case S_IDLE:
		printf("tmscp%d: random interrupt ignored\n", d);
		return;

	/* Controller was in step 1 last, see if its gone to step 2 */
	case S_STEP1:
#		define STEP1MASK 0174377
#		define STEP1GOOD (TMSCP_STEP2|TMSCP_IE|(NCMDL2<<3)|NRSPL2)
		for (i = 0; i < 150; i++)
			{
			if ((tmscpaddr->tmscpsa&STEP1MASK) != STEP1GOOD)
				{ /* still in step 1 (wait 1/100 sec) */
				DELAY(10000);
#				ifdef DEBUG
				printd("still in step 1, delaying\n");
#				endif DEBUG
				}
			else
				break;
			}
		if (i > 149)
			{
			sc->sc_state = S_IDLE;
			printf("failed to initialize, in step1: sa 0x%x", tmscpaddr->tmscpsa);
			wakeup((caddr_t)um);
			return;
			}
		tmscpaddr->tmscpsa = ((int)&sc->sc_tmscp->tmscp_ca.ca_ringbase)
			| ((cpunumber == VAX_780 || cpunumber == VAX_8600) ?
			 TMSCP_PI : 0);
		sc->sc_state = S_STEP2;
		return;

	/* Controller was in step 2 last, see if its gone to step 3 */
	case S_STEP2:
#		define STEP2MASK 0174377
#		define STEP2GOOD (TMSCP_STEP3|TMSCP_IE|(sc->sc_ivec/4))
		for (i = 0; i < 150; i++)
			{
			if ((tmscpaddr->tmscpsa&STEP2MASK) != STEP2GOOD)
				{ /* still in step 2 (wait 1/100 sec) */
				DELAY(10000);
#				ifdef DEBUG
				printd("still in step 2, delaying\n");
#				endif DEBUG
				}
			else
				break;
			}
		if (i > 149)
			{
			sc->sc_state = S_IDLE;
			printf("failed to initialize, in step2: sa 0x%x", tmscpaddr->tmscpsa);
			wakeup((caddr_t)um);
			return;
			}
		tmscpaddr->tmscpsa = ((int)&sc->sc_tmscp->tmscp_ca.ca_ringbase)>>16;
		sc->sc_state = S_STEP3;
		return;

	/* Controller was in step 3 last, see if its gone to step 4 */
	case S_STEP3:
#		define STEP3MASK 0174000
#		define STEP3GOOD TMSCP_STEP4
		for (i = 0; i < 150; i++)
			{
			if ((tmscpaddr->tmscpsa&STEP3MASK) != STEP3GOOD)
				{ /* still in step 3 (wait 1/100 sec) */
				DELAY(10000);
#				ifdef DEBUG
				printd("still in step 3, delaying\n");
#				endif DEBUG
				}
			else
				break;
			}
		if (i > 149)
			{
			sc->sc_state = S_IDLE;
			printf("failed to initialize, in step3: sa 0x%x", tmscpaddr->tmscpsa);
			wakeup((caddr_t)um);
			return;
			}
		/*
		 * Get microcode version and model number of controller;
		 * Signal initialization complete (_GO) (to the controller);
		 *    ask for Last Fail response if tmscperror is set;
		 * Set state to "set controller characteristics".
		 */
		tmscpmicro[d] = tmscpaddr->tmscpsa;
		tmscpaddr->tmscpsa = TMSCP_GO | (tmscperror? TMSCP_LF : 0);
		sc->sc_state = S_SCHAR;
#		ifdef DEBUG
		printd("tmscpintr: completed state %d \n", sc->sc_state);
		printd("tmscp%d Version %d model %d\n",d,tmscpmicro[d]&0xF,
			(tmscpmicro[d]>>4) & 0xF);
#		endif

	    /*
	     * Initialize the data structures (response and command queues).
	     */
	    ttm = sc->sc_tmscp;
	    for (i = 0; i < NRSP; i++)
		    {
		    tm->tmscp_ca.ca_rspdsc[i] = TMSCP_OWN | TMSCP_INT | 
			   (long)&ttm->tmscp_rsp[i].mscp_cmdref;
		    tm->tmscp_rsp[i].mscp_dscptr = &tm->tmscp_ca.ca_rspdsc[i];
		    tm->tmscp_rsp[i].mscp_header.tmscp_msglen = mscp_msglen;
		    }
	    for (i = 0; i < NCMD; i++)
		    {
		    tm->tmscp_ca.ca_cmddsc[i] = TMSCP_INT | 
					(long)&ttm->tmscp_cmd[i].mscp_cmdref;
		    tm->tmscp_cmd[i].mscp_dscptr = &tm->tmscp_ca.ca_cmddsc[i];
		    tm->tmscp_cmd[i].mscp_header.tmscp_msglen = mscp_msglen;
		    tm->tmscp_cmd[i].mscp_header.tmscp_vcid = 1;
		    }
	    bp = &tmscpwtab[d];
	    bp->b_actf = NULL;
	    sc->sc_lastcmd = 1;
	    sc->sc_lastrsp = 0;
	    mp = &tmscp[d].tmscp_cmd[0];
	    mp->mscp_unit = mp->mscp_modifier = 0;
	    mp->mscp_flags = 0;
	    mp->mscp_version = 0;
	    mp->mscp_cntflgs = M_CF_ATTN|M_CF_MISC|M_CF_THIS;
	    /*
	     * A host time out value of 0 means that the controller will not
	     * time out.  This is ok for the TK50.
	     */
	    mp->mscp_hsttmo = 0;
	    mp->mscp_time = 0;
	    mp->mscp_cntdep = 0;
	    mp->mscp_opcode = M_OP_STCON;
	    *((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
	    i = tmscpaddr->tmscpip;      /* initiate polling */
	    return;

	case S_SCHAR:
	case S_RUN:
		break;

	default:
	    printf("tmscp%d: interrupt in unknown state %d ignored\n",d,sc->sc_state);
	    return;
	}	/* end switch */

	/*
	 * The controller state is S_SCHAR or S_RUN
	 */

	/*
	 * If the error bit is set in the SA register then print an error
	 * message and reinitialize the controller.
	 */
	if (tmscpaddr->tmscpsa&TMSCP_ERR)
		{
		printf("tmscp%d: fatal error (%o)\n", d, tmscpaddr->tmscpsa&0xffff);
		tmscpaddr->tmscpip = 0;
		wakeup((caddr_t)um);
		}
	/*
	 * Check for a buffer purge request. (Won't happen w/ TK50 on Q22 bus)
	 */
	if (tm->tmscp_ca.ca_bdp)
		{
		UBAPURGE(um->um_hd->uh_uba, tm->tmscp_ca.ca_bdp);
		tm->tmscp_ca.ca_bdp = 0;
		tmscpaddr->tmscpsa = 0;      /* signal purge complete */
		}

	/*
	 * Check for response ring transition.
	 */
	if (tm->tmscp_ca.ca_rspint)
		{
		tm->tmscp_ca.ca_rspint = 0;
		for (i = sc->sc_lastrsp;; i++)
			{
			i %= NRSP;
			if (tm->tmscp_ca.ca_rspdsc[i]&TMSCP_OWN)
			    break;
			tmscprsp((struct uba_ctlr *)um, tm,
			    (struct tmscp_softc *)sc, i);
			tm->tmscp_ca.ca_rspdsc[i] |= TMSCP_OWN;
			}
		sc->sc_lastrsp = i;
		}

	/*
	 * Check for command ring transition.
	 */
	if (tm->tmscp_ca.ca_cmdint)
		{
#		ifdef DEBUG
		printd("tmscpintr: command ring transition\n");
#		endif		
		tm->tmscp_ca.ca_cmdint = 0;
		}
    	if(tmscp_cp_wait)
		wakeup((caddr_t)&tmscp_cp_wait);
    	(void) tmscpstart((struct uba_ctlr *)um);
}


/*
 * Open a tmscp device and set the unit online.  If the controller is not 
 * in the run state, call init to initialize the tmscp controller first.
 */

/* ARGSUSED */
int
tmscpopen(dev, flag, type, p)
	dev_t dev;
	int flag, type;
	struct proc *p;
{
	register int unit;
	register struct uba_device *ui;
	register struct tmscp_softc *sc;
	register struct tms_info *tms;
	register volatile struct mscp *mp;
	register struct uba_ctlr *um;
	volatile struct tmscpdevice *tmscpaddr;
	volatile int i;
	int s;
	
	unit = TMSUNIT(dev);
#	ifdef DEBUG
	printd("tmscpopen unit %d\n",unit);
	if(tmscpdebug)DELAY(10000);
#	endif
	if (unit >= NTMS || (ui = tmsdinfo[unit]) == 0 || ui->ui_alive == 0)
		return (ENXIO);
	tms = &tms_info[ui->ui_unit];
	if (tms->tms_openf)
		return (EBUSY);
	sc = &tmscp_softc[ui->ui_ctlr];
	tms->tms_openf = 1;
	tms->tms_tpr = tprintf_open(curproc);
	s = splbio();
	if (sc->sc_state != S_RUN)
		{
		if (sc->sc_state == S_IDLE)
			if(!tmscpinit(ui->ui_ctlr))
				{
				printf("tmscp controller failed to init\n");
				(void) splx(s);
				tms->tms_openf = 0;
				return(ENXIO);
				}
		/* 
		 * Wait for initialization to complete
		 */
		timeout(wakeup,(caddr_t)ui->ui_mi,11*hz);	/* to be sure*/
		sleep((caddr_t)ui->ui_mi, 0);
		if (sc->sc_state != S_RUN)
			{
			(void) splx(s);
			tms->tms_openf = 0;
			return (EIO);
			}
		}
	/*
	 * Check to see if the device is really there.
	 * this code was taken from Fred Canters 11 driver
	 */
	um = ui->ui_mi;
	tmscpaddr = (struct tmscpdevice *) um->um_addr;
	(void) splx(s);
	if(ui->ui_flags == 0)
		{
		s = splbio();
		while(0 ==(mp = tmscpgetcp(um)))
			{
			tmscp_cp_wait++;
			sleep((caddr_t)&tmscp_cp_wait,PSWP+1);
			tmscp_cp_wait--;
			}
		(void) splx(s);
		mp->mscp_opcode = M_OP_ONLIN;
		mp->mscp_unit = ui->ui_slave;
		mp->mscp_cmdref = (long) & tms->tms_type;
					    /* need to sleep on something */
#		ifdef DEBUG
		printd("tmscpopen: bring unit %d online\n",ui->ui_unit);
#		endif	
		*((long *) mp->mscp_dscptr ) |= TMSCP_OWN | TMSCP_INT;
		i = tmscpaddr->tmscpip;
#ifdef lint
		i = i;
#endif
		/* 
		 * To make sure we wake up, timeout in 240 seconds.
		 * Wakeup in tmscprsp routine.
		 * 240 seconds (4 minutes) is necessary since a rewind
		 * can take a few minutes.
		 */
		timeout(wakeup,(caddr_t) mp->mscp_cmdref,240 * hz);
		sleep((caddr_t) mp->mscp_cmdref,PSWP+1);
		}
	if(ui->ui_flags == 0) {
		tms->tms_openf = 0;
		return(ENXIO);  /* Didn't go online */
	}
	tms->tms_lastiow = 0;
	/*
	 * If the high density device is not specified, set unit to low
	 * density.  This is done as an "internal" ioctl command so
	 * that the command setup and response handling
	 * is done thru "regular" command routines.
	 */
	if ((minor(dev) & T_HIDENSITY) == 0)
		tmscpcommand(dev, TMS_LOWDENSITY, 1);
	else
		tmscpcommand(dev, TMS_HIDENSITY, 1);
	return (0);
}


/*
 * Close tape device.
 *
 * If tape was open for writing or last operation was
 * a write, then write two EOF's and backspace over the last one.
 * Unless this is a non-rewinding special file, rewind the tape.
 *
 * NOTE:
 *	We want to be sure that any serious exception is cleared on the
 *	close. A Clear Serious Exception (CSE) modifier is always done on
 *	the rewind command.  For the non-rewind case we check to see if the
 *	"serex" field is set in the softc struct; if it is then issue a noop
 *	command with the CSE modifier.
 * Make the tape available to others, by clearing openf flag.
 */
int
tmscpclose(dev, flag, type, p)
	register dev_t dev;
	register flag, type;
	struct proc *p;
{
	register struct tms_info *tms;
	register struct uba_device *ui;

	ui = tmsdinfo[TMSUNIT(dev)];
#	ifdef DEBUG
	printd("tmscpclose: ctlr =  %d\n",TMSCPCTLR(dev));
	printd("tmscpclose: unit = %d\n",TMSUNIT(dev));
	if(tmscpdebug)DELAY(10000);
#	endif
	tms = &tms_info[ui->ui_unit];
	if (flag == FWRITE || ((flag&FWRITE) && tms->tms_lastiow))
		{
		/*	   device, command, count */
		tmscpcommand (dev, TMS_WRITM, 1);
		tmscpcommand (dev, TMS_WRITM, 1);
		tmscpcommand (dev, TMS_BSR, 1);
		}
	if ((minor(dev)&T_NOREWIND) == 0)
		/*
		 * Don't hang waiting for rewind complete.
		 */
		tmscpcommand(dev, TMS_REW, 0);
	else
		if (tms->tms_serex)
			{
#			ifdef DEBUG
			printd("tmscpclose: clearing serex\n");
			if(tmscpdebug)DELAY(10000);
#			endif
			tmscpcommand(dev, TMS_CSE, 1);
			}
	tprintf_close(tms->tms_tpr);
	tms->tms_openf = 0;
	return (0);
}


/*
 * Execute a command on the tape drive a specified number of times.
 * This routine sets up a buffer and calls the strategy routine which
 * links the buffer onto the drive's buffer queue.
 * The start routine will take care of creating a tmscp command packet
 * with the command.  The start routine is called by the strategy or the
 * interrupt routine.
 */
void
tmscpcommand (dev, com, count)
	register dev_t dev;
	int com, count;
{
	register struct uba_device *ui;
	register struct buf *bp;
	register int s;
	int unit = TMSUNIT(dev);

	ui = tmsdinfo[unit];
	bp = &ctmscpbuf[ui->ui_ctlr];

	s = splbio();
	while (bp->b_flags&B_BUSY)
		{
		/*
		 * This special check is because B_BUSY never
		 * gets cleared in the non-waiting rewind case.
		 */
		if (bp->b_bcount == 0 && (bp->b_flags&B_DONE))
			break;
		bp->b_flags |= B_WANTED;
		sleep((caddr_t)bp, PRIBIO);
		}
	bp->b_flags = B_BUSY|B_READ;
	splx(s);
	/*
	 * Load the buffer.  The b_count field gets used to hold the command
	 * count.  the b_resid field gets used to hold the command mneumonic.
	 * These 2 fields are "known" to be "safe" to use for this purpose.
	 * (Most other drivers also use these fields in this way.)
	 */
	bp->b_dev = dev;
	bp->b_bcount = count;
	bp->b_resid = com;
	bp->b_blkno = 0;
	tmscpstrategy(bp);
	/*
	 * In case of rewind from close, don't wait.
	 * This is the only case where count can be 0.
	 */
	if (count == 0)
		return;
	iowait(bp);
	if (bp->b_flags&B_WANTED)
		wakeup((caddr_t)bp);
	bp->b_flags &= B_ERROR;
}

/*
 * Find an unused command packet
 */
struct mscp *
tmscpgetcp(um)
	struct uba_ctlr *um;
{
	register volatile struct mscp *mp;
	register volatile struct tmscpca *cp;
	register struct tmscp_softc *sc;
	register int i;
	int	s;

	s = splbio();
	cp = &tmscp[um->um_ctlr].tmscp_ca;
	sc = &tmscp_softc[um->um_ctlr];
	/*
	 * If no credits, can't issue any commands
	 * until some outstanding commands complete.
	 */
	i = sc->sc_lastcmd;
#  	ifdef DEBUG
	printd10("tmscpgetcp: %d credits remain\n", sc->sc_credits);
#	endif
	if(((cp->ca_cmddsc[i]&(TMSCP_OWN|TMSCP_INT))==TMSCP_INT) &&
	  (sc->sc_credits >= 2))
		{
		sc->sc_credits--;       /* This commits to issuing a command */
		cp->ca_cmddsc[i] &= ~TMSCP_INT;
		mp = &tmscp[um->um_ctlr].tmscp_cmd[i];
		mp->mscp_unit = mp->mscp_modifier = 0;
		mp->mscp_opcode = mp->mscp_flags = 0;
		mp->mscp_bytecnt = mp->mscp_buffer = 0;
		sc->sc_lastcmd = (i + 1) % NCMD;
		(void) splx(s);
		return((struct mscp *)mp);
		}
	(void) splx(s);
	return(NULL);
}


/*
 * Initialize a TMSCP device.  Set up UBA mapping registers,
 * initialize data structures, and start hardware
 * initialization sequence.
 */
int
tmscpinit (d)
	int d;			/* index to the controller */
{
	register struct tmscp_softc *sc;
	register struct tmscp *t;
	volatile struct tmscpdevice *tmscpaddr;
	struct uba_ctlr *um;

	sc = &tmscp_softc[d];
	um = tmscpminfo[d];
	um->um_tab.b_active++;
	t = &tmscp[d];
	tmscpaddr = (struct tmscpdevice *)um->um_addr;
	if (sc->sc_mapped == 0)
		{
		/*
		 * Map the communications area and command
		 * and response packets into Unibus address
		 * space.
		 */
		sc->sc_ubainfo = uballoc(um->um_ubanum, (caddr_t)t, sizeof (struct tmscp), 0);
		sc->sc_tmscp = (struct tmscp *)(UBAI_ADDR(sc->sc_ubainfo));
		sc->sc_mapped = 1;
		}

	/*
	 * Start the hardware initialization sequence.
	 */
	tmscpaddr->tmscpip = 0;              /* start initialization */

	while((tmscpaddr->tmscpsa & TMSCP_STEP1) == 0)
		{
#		ifdef DEBUG
		printd("tmscpinit: tmscpsa = 0%o\n",tmscpaddr->tmscpsa);
		DELAY(100000);
#		endif
		if(tmscpaddr->tmscpsa & TMSCP_ERR)
			return(0);	/* CHECK */
		}
	tmscpaddr->tmscpsa=TMSCP_ERR|(NCMDL2<<11)|(NRSPL2<<8)|TMSCP_IE|(sc->sc_ivec/4);
	/*
	 * Initialization continues in the interrupt routine.
	 */
	sc->sc_state = S_STEP1;
	sc->sc_credits = 0;
	return(1);
}


/*
 * Start I/O operation
 * This code is convoluted.  The majority of it was copied from the uda driver.
 */
void
tmscpstart(um)
	register struct uba_ctlr *um;
{
	register struct buf *bp, *dp;
	register volatile struct mscp *mp;
	register struct tmscp_softc *sc;
	register struct tms_info *tms;
	register struct uba_device *ui;
	volatile struct tmscpdevice *tmscpaddr;
	volatile struct tmscp *tm = &tmscp[um->um_ctlr];
	volatile int i;
	int tempi;
	char ioctl;		/* flag: set true if its an IOCTL command */

	sc = &tmscp_softc[um->um_ctlr];
	
    for(;;)
	{
	if ((dp = um->um_tab.b_actf) == NULL)
		{
		/*
		 * Release unneeded UBA resources and return
		 * (drive was inactive)
		 */
		um->um_tab.b_active = 0;
		break;
		}
	if ((bp = dp->b_actf) == NULL)
		{
		/*
		 * No more requests for this drive, remove
		 * from controller queue and look at next drive.
		 * We know we're at the head of the controller queue.
		 */
		dp->b_active = 0;
		um->um_tab.b_actf = dp->b_hash.le_next;
		continue;		/* Need to check for loop */
		}
	um->um_tab.b_active++;
	tmscpaddr = (struct tmscpdevice *)um->um_addr;
	ui = tmsdinfo[(TMSUNIT(bp->b_dev))];
	tms = &tms_info[ui->ui_unit];
	if ((tmscpaddr->tmscpsa&TMSCP_ERR) || sc->sc_state != S_RUN)
		{
		tprintf(tms->tms_tpr,
		    "tms%d: hard error bn%d\n",
		    minor(bp->b_dev)&03, bp->b_blkno);
		log(TMS_PRI, "tmscp%d: sa 0%o, state %d\n",um->um_ctlr,
				tmscpaddr->tmscpsa&0xffff, sc->sc_state);
		(void)tmscpinit(um->um_ctlr);
		/* SHOULD REQUEUE OUTSTANDING REQUESTS, LIKE TMSCPRESET */
		break;
		}
	/*
	 * Default is that last command was NOT a write command;
	 * if a write command is done it will be detected in tmscprsp.
	 */
	tms->tms_lastiow = 0;
	if (ui->ui_flags == 0)
		{        /* not online */
		if ((mp = tmscpgetcp(um)) == NULL)
			break;
		mp->mscp_opcode = M_OP_ONLIN;
		mp->mscp_unit = ui->ui_slave;
		dp->b_active = 2;
		um->um_tab.b_actf = dp->b_hash.le_next; /* remove from controller q */
		*((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
		if (tmscpaddr->tmscpsa&TMSCP_ERR)
			printf("tmscp%d fatal error (0%o)\n",um->um_ctlr,
					tmscpaddr->tmscpsa&0xffff);
		i = tmscpaddr->tmscpip;
		continue;
		}
	switch (cpunumber) {

	case VAX_8600:
	case VAX_780:
		i = UBA_NEEDBDP|UBA_CANTWAIT;
		break;
	case VAX_750:
		i = um->um_ubinfo|UBA_HAVEBDP|UBA_CANTWAIT;
		break;
	case VAX_730:
	case VAX_78032:
		i = UBA_CANTWAIT;
		break;
	}   /* end switch (cpunumber) */
	/*
	 * If command is an ioctl command then set the ioctl flag for later use.
	 * If not (i.e. it is a read or write) then attempt
	 * to set up a buffer pointer.
	 */
	ioctl = 0;
	if (bp == &ctmscpbuf[um->um_ctlr])
		ioctl = 1;
	else
		if ((i = ubasetup(um->um_ubanum, bp, i)) == 0)
			{
			if(dp->b_qsize != 0)
				break; /* When a command completes and */
				     /* frees a bdp tmscpstart will be called */
			if ((mp = tmscpgetcp(um)) == NULL)
				break;
#			ifdef DEBUG
			printd("tmscpstart: GTUNT %d ubasetup = %d\n",ui->ui_unit, i);
			if(tmscpdebug)DELAY(10000);
#			endif
			mp->mscp_opcode = M_OP_GTUNT;
			mp->mscp_unit = ui->ui_slave;
			*((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
			if (tmscpaddr->tmscpsa&TMSCP_ERR)
				printf("tmscp%d: fatal error (0%o)\n",um->um_ctlr,
					    tmscpaddr->tmscpsa&0xffff);
			i = tmscpaddr->tmscpip;	/* initiate polling */
			break;
			}
#	if defined(VAX750)
	if (cpunumber == VAX_750)
		tempi = i & 0xfffffff;			/* mask off bdp */
	else
#	endif
		tempi = i;
	if ((mp = tmscpgetcp(um)) == NULL)
		{
		if (!ioctl)		/* only need to release if NOT ioctl */
			ubarelse(um->um_ubanum,&tempi);
		break;
		}
	mp->mscp_cmdref = (long)bp;		/* pointer to get back */
	mp->mscp_unit = ui->ui_slave;
	/*
	 * If its an ioctl-type command then set up the appropriate
	 * tmscp command;  by doing a switch on the "b_resid" field where
	 * the command mneumonic is stored.
	 */
	if (ioctl)
		{
#		ifdef DEBUG
		printd("tmscpstart: doing ioctl cmd %d\n", bp->b_resid);
#		endif
		/*
		 * The reccnt and tmkcnt fields are set to zero by the getcp
		 * routine (as bytecnt and buffer fields).  Thus reccnt and
		 * tmkcnt are only modified here if they need to be set to
		 * a non-zero value.
		 */
		switch ((int)bp->b_resid) {

		case TMS_WRITM:
			mp->mscp_opcode = M_OP_WRITM;
			break;
		case TMS_FSF:
			mp->mscp_opcode = M_OP_REPOS;
			mp->mscp_tmkcnt = bp->b_bcount;
			break;
		case TMS_BSF:
			mp->mscp_opcode = M_OP_REPOS;
			mp->mscp_modifier = M_MD_REVRS;
			mp->mscp_tmkcnt = bp->b_bcount;
			break;
		case TMS_FSR:
			mp->mscp_opcode = M_OP_REPOS;
			mp->mscp_modifier = M_MD_OBJCT;
			mp->mscp_reccnt = bp->b_bcount;
			break;
		case TMS_BSR:
			mp->mscp_opcode = M_OP_REPOS;
			mp->mscp_modifier = M_MD_REVRS | M_MD_OBJCT;
			mp->mscp_reccnt = bp->b_bcount;
			break;
		/*
		 * Clear serious exception is done for Rewind & Available cmds
		 */
		case TMS_REW:
			mp->mscp_opcode = M_OP_REPOS;
			mp->mscp_modifier = M_MD_REWND | M_MD_CLSEX;
			if (bp->b_bcount == 0)
				mp->mscp_modifier |= M_MD_IMMED;
			tms->tms_serex = 0;
			break;
		case TMS_OFFL:
			mp->mscp_opcode = M_OP_AVAIL;
			mp->mscp_modifier = M_MD_UNLOD | M_MD_CLSEX;
			tms->tms_serex = 0;
			break;
		case TMS_SENSE:
			mp->mscp_opcode = M_OP_GTUNT;
			break;
		case TMS_CACHE:
			mp->mscp_opcode = M_OP_STUNT;
			tms->tms_unitflgs |= M_UF_WBKNV;
			mp->mscp_unitflgs = tms->tms_unitflgs;
			mp->mscp_format = tms->tms_format;
			/* default device dependant parameters */
			mp->mscp_mediaid = 0;
			break;
		case TMS_NOCACHE:
			mp->mscp_opcode = M_OP_STUNT;
			tms->tms_unitflgs &= ~(M_UF_WBKNV);
			mp->mscp_unitflgs = tms->tms_unitflgs;
			mp->mscp_format = tms->tms_format;
			/* default device dependant parameters */
			mp->mscp_mediaid = 0;
			break;
		case TMS_CSE:
			/*
			 * This is a no-op command. It performs a 
			 * clear serious exception only.  (Done on a
			 * non-rewinding close after a serious exception.)
			 */
			mp->mscp_opcode = M_OP_REPOS;
			mp->mscp_modifier = M_MD_CLSEX;
			tms->tms_serex = 0;
			tms->tms_clserex = 1;
			break;
		case TMS_LOWDENSITY:
			/*
			 * Set the unit to low density
			 */
			mp->mscp_opcode = M_OP_STUNT;
			mp->mscp_unitflgs = tms->tms_unitflgs;
			mp->mscp_mediaid = 0;	/* default device dependant parameters */
			if ((tms->tms_fmtmenu & M_TF_800) != 0)
				mp->mscp_format = M_TF_800;
			else
				mp->mscp_format = M_TF_PE & tms->tms_fmtmenu;
			tms->tms_format = mp->mscp_format;
			break;
		case TMS_HIDENSITY:
			/*
			 * Set the unit to high density (format == 0)
			 */
			mp->mscp_opcode = M_OP_STUNT;
			mp->mscp_unitflgs = tms->tms_unitflgs;
			mp->mscp_mediaid = 0;	/* default device dependant parameters */
			mp->mscp_format = 0;
			tms->tms_format = 0;
			break;
		default:
			printf("Bad ioctl on tms unit %d\n", ui->ui_unit);
			/* Need a no-op. Reposition no amount */
			mp->mscp_opcode = M_OP_REPOS;
			break;
		}   /* end switch (bp->b_resid) */
		}
	else    /* Its a read/write command (not an ioctl) */
		{
		mp->mscp_opcode = bp->b_flags&B_READ ? M_OP_READ : M_OP_WRITE;
		mp->mscp_bytecnt = bp->b_bcount;
		mp->mscp_buffer = UBAI_ADDR(i) | (UBAI_BDP(i) << 24);

		bp->b_ubinfo = tempi;			/* save mapping info */
		}
	if (tms->tms_serex == 2)			/* if tape mark read */
		{
		mp->mscp_modifier |= M_MD_CLSEX;	/*  clear serious exc */
		tms->tms_serex = 0;
		}
	*((long *)mp->mscp_dscptr) |= TMSCP_OWN|TMSCP_INT;
#	ifdef DEBUG
	printd("tmscpstart: opcode 0%o mod %o unit %d cnt %d\n",mp->mscp_opcode,mp->mscp_modifier,mp->mscp_unit,mp->mscp_bytecnt);
	if(tmscpdebug)DELAY(100000);
#	endif
	i = tmscpaddr->tmscpip;              /* initiate polling */
	dp->b_qsize++;
	/*
	 * Move drive to the end of the controller queue
	 */
	if (dp->b_hash.le_next != NULL)
		{
		um->um_tab.b_actf = dp->b_hash.le_next;
		MSCP_APPEND(dp, &um->um_tab, b_hash.le_next);
		}
	/*
	 * Move buffer to I/O wait queue
	 */
	dp->b_actf = bp->b_actf;
	dp = &tmscpwtab[um->um_ctlr];
	MSCP_APPEND(bp, dp, b_actf);
	if (tmscpaddr->tmscpsa&TMSCP_ERR)
		{
		printf("tmscp%d: fatal error (0%o)\n", um->um_ctlr, tmscpaddr->tmscpsa&0xffff);
		(void)tmscpinit(um->um_ctlr);
		break;
		}
    }   /* end for */
    /*
     * Check for response ring transitions lost in the
     * Race condition
     */
    for (i = sc->sc_lastrsp;; i++)
	    {
	    i %= NRSP;
	    if (tm->tmscp_ca.ca_rspdsc[i]&TMSCP_OWN)
		    break;
	    tmscprsp(um, (struct tmscp *)tm, sc, i);
	    tm->tmscp_ca.ca_rspdsc[i] |= TMSCP_OWN;
	    }
    sc->sc_lastrsp = i;
}


/*
 * Process a response packet
 */
void
tmscprsp(um, tm, sc, i)
	register struct uba_ctlr *um;
	struct tmscp *tm;
	register struct tmscp_softc *sc;
	int i;
{
	register volatile struct mscp *mp;
	register struct tms_info *tms;
	struct uba_device *ui;
	struct buf *dp, *bp;
	int st;
	struct  uba_softc *ubasc;

	mp = &tm->tmscp_rsp[i];
	mp->mscp_header.tmscp_msglen = mscp_msglen;
	sc->sc_credits += mp->mscp_header.tmscp_credits & 0xf;  /* low 4 bits */
	if ((mp->mscp_header.tmscp_credits & 0xf0) > 0x10)	/* Check */
		return;
#	ifdef DEBUG
	printd("tmscprsp, opcode 0%o status 0%o\n",mp->mscp_opcode,mp->mscp_status&M_ST_MASK);
#	endif	
	/*
	 * If it's an error log message (datagram),
	 * pass it on for more extensive processing.
	 */
	if ((mp->mscp_header.tmscp_credits & 0xf0) == 0x10)
		{	/* check */
		tmserror(um, (struct mslg *)mp);
		return;
		}
	st = mp->mscp_status&M_ST_MASK;
	/*
	 * The controller interrupts as drive 0.
	 * This means that you must check for controller interrupts
	 * before you check to see if there is a drive 0.
	 */
	if((M_OP_STCON|M_OP_END) == mp->mscp_opcode)
		{
		if (st == M_ST_SUCC)
			{
#		  	ifdef DEBUG
			printd("ctlr has %d credits\n", mp->mscp_header.tmscp_credits & 0xf);
			printd("ctlr timeout = %d\n", mp->mscp_cnttmo);
#			endif
			sc->sc_state = S_RUN;
			}
		else
			sc->sc_state = S_IDLE;
		um->um_tab.b_active = 0;
		wakeup((caddr_t)um);
		return;
		}
	if (mp->mscp_unit >= NTMS)
		return;
	if ((ui = tmscpip[um->um_ctlr][mp->mscp_unit]) == 0)
		return;
	tms = &tms_info[ui->ui_unit];
	/*
	 * Save endcode, endflags, and status for mtioctl get unit status.
	 * NOTE: Don't do this on Clear serious exception (reposition no-op);
	 *    which is done on close since this would
	 *    overwrite the real status we want.
	 */
	if (tms->tms_clserex != 1)
		{
		tms->tms_endcode = mp->mscp_opcode;
		tms->tms_flags = mp->mscp_flags;
		tms->tms_status = st;
		}
	else tms->tms_clserex = 0;

	switch (mp->mscp_opcode) {
	case M_OP_ONLIN|M_OP_END:
		tms->tms_type = mp->mscp_mediaid;
		dp = &tmsutab[ui->ui_unit];
		if (st == M_ST_SUCC)
			{
			/*
			 * Link the drive onto the controller queue
			 */
			MSCP_APPEND(dp, &um->um_tab, b_hash.le_next);
			ui->ui_flags = 1;       /* mark it online */
			tms->tms_dsize=(daddr_t)mp->mscp_maxwrt;
#			ifdef DEBUG
			printd("tmscprsp: unit %d online\n", mp->mscp_unit);
#			endif			
			/* 
			 * This define decodes the Media type identifier
			 */
#	        	define F_to_C(x,i)     ( ((x)->mscp_mediaid) >> (i*5+7) & 0x1f ? ( ( (((x)->mscp_mediaid) >>( i*5 + 7)) & 0x1f) + 'A' - 1): ' ')
#			ifdef DEBUG
			printd("tmscprsp: unit %d online %x %c%c %c%c%c%d\n"
				,mp->mscp_unit, mp->mscp_mediaid ,F_to_C(mp,4)
				,F_to_C(mp,3), F_to_C(mp,2)
				,F_to_C(mp,1), F_to_C(mp,0), mp->mscp_mediaid & 0x7f);
#			endif				
			dp->b_active = 1;
			}	/* end if st == M_ST_SUCC */
		else 
			{
			if ((bp = dp->b_actf))
				tprintf(tms->tms_tpr,
				    "tms%d: hard error bn%d: OFFLINE\n",
				    minor(bp->b_dev)&03, bp->b_blkno);
			else
				tprintf(tms->tms_tpr,
				    "tms%d: hard error: OFFLINE\n",
				    ui->ui_unit);
			while ((bp = dp->b_actf))
				{
				dp->b_actf = bp->b_actf;
				bp->b_flags |= B_ERROR;
				iodone(bp);
				}
			}
		if(mp->mscp_cmdref!=NULL)
			/* Seems to get lost sometimes in uda */
			wakeup((caddr_t)mp->mscp_cmdref);
		break;
	/*
	 * The AVAILABLE ATTENTION message occurs when the
	 * unit becomes available after loading,
	 * marking the unit offline (ui_flags = 0) will force an
	 * online command prior to using the unit.
	 */
	case M_OP_AVATN:
		ui->ui_flags = 0;
		tms->tms_type = mp->mscp_mediaid;
		break;
	case M_OP_END:
		/*
		 * An endcode without an opcode (0200) is an invalid command.
		 * The mscp specification states that this would be a protocol
		 * type error, such as illegal opcodes. The mscp spec. also
		 * states that parameter error type of invalid commands should
		 * return the normal end message for the command. This does not appear
		 * to be the case. An invalid logical block number returned an endcode
		 * of 0200 instead of the 0241 (read) that was expected.
		 */
			
		printf("tmscp%d: invalid cmd, endcode = %o, status=%o\n",
			um->um_ctlr, mp->mscp_opcode, st);
		bp = (struct buf *)mp->mscp_cmdref;
		/*
		 * Unlink buffer from I/O wait queue.
		 * And signal iodone, so the higher level command can exit!
		 *
		 */
		dp = &tmscpwtab[um->um_ctlr];
		while (dp && dp->b_actf != bp)
			dp = dp->b_actf;
		if (dp == NULL)
			panic("tmscp: don't work1!");
		dp->b_actf = bp->b_actf;
		dp = &tmsutab[ui->ui_unit];
		dp->b_qsize--;
		iodone(bp);
		break;
	case M_OP_WRITE|M_OP_END:
		/* mark the last io op as a write */
		tms->tms_lastiow = 1;
	case M_OP_READ|M_OP_END:
	case M_OP_WRITM|M_OP_END:
	case M_OP_REPOS|M_OP_END:
	case M_OP_STUNT|M_OP_END:
	/*
	 * The AVAILABLE message occurs when the mt ioctl "rewoffl" is
	 * issued.  For the ioctl, "rewoffl", a tmscp AVAILABLE command is
	 * done with the UNLOAD modifier.  This performs a rewind, followed
	 * by marking the unit offline.  So mark the unit offline
	 * software wise as well (ui_flags = 0 and 
	 * tms->tms_openf = 0).
	 */
	case M_OP_AVAIL|M_OP_END:
#		ifdef DEBUG
		printd("tmscprsp: position = %d\n", mp->mscp_lbn);
#		endif
		bp = (struct buf *)mp->mscp_cmdref;
		/*
		 * Only need to release buffer if the command was read or write.
		 * No ubasetup was done in "tmscpstart" if it was an ioctl cmd.
		 */
		if (mp->mscp_opcode == (M_OP_READ|M_OP_END) || 
		    mp->mscp_opcode == (M_OP_WRITE|M_OP_END))
			ubarelse(um->um_ubanum, (int *)&bp->b_ubinfo);
		/*
		 * Unlink buffer from I/O wait queue.
		 */
		dp = &tmscpwtab[um->um_ctlr];
		while (dp && dp->b_actf != bp)
			dp = dp->b_actf;
		if (dp == NULL)
			panic("tmscp: don't work2!");
		dp->b_actf = bp->b_actf;
#		if defined(VAX750)
		ubasc = uba_cd.cd_devs[um->um_ubanum];
		if (cpunumber == VAX_750) { 
		    if ((tmscpwtab[um->um_ctlr].b_actf == NULL) &&
					(um->um_ubinfo != 0)) {
			ubarelse(um->um_ubanum, &um->um_ubinfo);
		    }
		    else {
			if (mp->mscp_opcode == (M_OP_READ|M_OP_END) ||
		    	    mp->mscp_opcode == (M_OP_WRITE|M_OP_END))
				UBAPURGE(ubasc->uh_uba,(um->um_ubinfo >>28) & 0x0f);
		    }
		}
#		endif
		dp = &tmsutab[ui->ui_unit];
		dp->b_qsize--;
		if (st == M_ST_OFFLN || st == M_ST_AVLBL)
			{
			ui->ui_flags = 0;       /* mark unit offline */
			tms->tms_openf = 0;
			tms->tms_type = mp->mscp_mediaid;
			/*
			 * Link the buffer onto the front of the drive queue
			 */
			bp->b_actf = dp->b_actf;
			dp->b_actf = bp;
			/*
			 * Link the drive onto the controller queue
			 */
			if (dp->b_active == 0)
				{
				MSCP_APPEND(dp, &um->um_tab, b_hash.le_next);
				dp->b_active = 1;
				}
#			if defined(VAX750)
			if (cpunumber == VAX_750 && um->um_ubinfo == 0)
				um->um_ubinfo = uballoc(um->um_ubanum, (caddr_t)0, 0, UBA_NEEDBDP);
#			endif
			return;
			}
		if (st != M_ST_SUCC)
			{
			if (mp->mscp_flags & M_EF_SEREX)
				tms->tms_serex = 1;
			if (st != M_ST_TAPEM)
				{
				tprintf(tms->tms_tpr,
				    "tms%d: hard error bn%d\n",
				    minor(bp->b_dev)&03, bp->b_blkno);
				errinfo(st);		/* produces more info */
#				ifdef DEBUG
				printd("tmscprsp: error; status sub-code = 0%o, flags = 0%o\n",
					(mp->mscp_status & 177740)>>5, mp->mscp_flags);
#				endif
				bp->b_flags |= B_ERROR;
				}
			else
				/* Hit a tape mark - Set serex flag to
				 * a special value so we can clear the
				 * serious exception on the next command.
				 */
				tms->tms_serex = 2;
			}
		/*
		 * The tmscp spec states that controllers do not have to
		 * report the number of records or files skipped.  So on
		 * reposition commands we go strictly by cmd status.
		 */
		if (mp->mscp_opcode != (M_OP_REPOS|M_OP_END))
			bp->b_resid = bp->b_bcount - mp->mscp_bytecnt;
		else
			bp->b_resid = 0;
		tms->tms_resid = bp->b_resid;
		iodone(bp);
		break;

	case M_OP_GTUNT|M_OP_END:
#		ifdef DEBUG
		printd("tmscprsp: GTUNT end packet status = 0%o\n",st);
		printd("tmscprsp: unit %d mediaid %x %c%c %c%c%c%d %x %x t=%d\n"
		    ,mp->mscp_unit, mp->mscp_mediaid
		    ,F_to_C(mp,4),F_to_C(mp,3),F_to_C(mp,2)
		    ,F_to_C(mp,1),F_to_C(mp,0)
		    ,mp->mscp_mediaid & 0x7f
		    ,mp->mscp_unitid.val[0]
		    ,mp->mscp_unitid.val[1]
		    ,mp->mscp_format);
#		endif		
		tms->tms_type = mp->mscp_mediaid;
		tms->tms_fmtmenu = mp->mscp_fmtmenu;
		tms->tms_unitflgs = mp->mscp_unitflgs;
		break;

	default:
		printf("tmscp unknown packet\n");
		tmserror(um, (struct mslg *)mp);
	}	/* end switch mp->mscp_opcode */
}


/* 
 * Give a meaningful error when the mscp_status field returns an error code.
 */

void
errinfo(st)
	int st;			/* the status code */
{
	switch(st) {
	case M_ST_ICMD:
		printf("invalid command\n");
		break;
	case M_ST_ABRTD:
		printf("command aborted\n");
		break;
	case M_ST_OFFLN:
		printf("unit offline\n");
		break;
	case M_ST_WRTPR:
		printf("unit write protected\n");
		break;
	case M_ST_COMP:
		printf("compare error\n");
		break;
	case M_ST_DATA:
		printf("data error\n");
		break;
	case M_ST_HSTBF:
		printf("host buffer access error\n");
		break;
	case M_ST_CNTLR:
		printf("controller error\n");
		break;
	case M_ST_DRIVE:
		printf("drive error\n");
		break;
	case M_ST_FMTER:
		printf("formatter error\n");
		break;
	case M_ST_BOT:
		printf("BOT encountered\n");
		break;
	case M_ST_TAPEM:
		printf("tape mark encountered\n");
		break;
	case M_ST_RDTRN:
		printf("record data truncated\n");
		break;
	case M_ST_PLOST:
		printf("position lost\n");
		break;
	case M_ST_SEX:
		printf("serious exception\n");
		break;
	case M_ST_LED:
		printf("LEOT detected\n");
		break;
	}
}


/*
 * Manage buffers and perform block mode read and write operations.
 */
void
tmscpstrategy (bp)
	register struct buf *bp;
{
	register struct uba_device *ui;
	register struct uba_ctlr *um;
	register struct buf *dp;
	register int unit = TMSUNIT(bp->b_dev);
	int s;

	if (unit >= NTMS)
		{
#		ifdef DEBUG
		printd ("tmscpstrategy: bad unit # %d\n",unit);
#		endif
		bp->b_flags |= B_ERROR;
		iodone(bp);
		return;
		}
	ui = tmsdinfo[unit];
	um = ui->ui_mi;
	if (ui == 0 || ui->ui_alive == 0)
		{
		bp->b_flags |= B_ERROR;
		iodone(bp);
		return;
		}
	s = splbio();
	/*
	 * Link the buffer onto the drive queue
	 */
	dp = &tmsutab[ui->ui_unit];
	MSCP_APPEND(bp, dp, b_actf);
	/*
	 * Link the drive onto the controller queue
	 */
	if (dp->b_active == 0)
		{
		MSCP_APPEND(dp, &um->um_tab, b_hash.le_next);
		dp->b_active = 1;
		}
	/*
	 * If the controller is not active, start it.
	 */
	if (um->um_tab.b_active == 0)
		{
#		if defined(VAX750)
		if (cpunumber == VAX_750
				 && tmscpwtab[um->um_ctlr].b_actf == NULL)
			{
			if (um->um_ubinfo != 0)
				log(TMS_PRI, "tmscpstrategy: ubinfo 0x%x\n",
				    um->um_ubinfo);
			else
				um->um_ubinfo = uballoc(um->um_ubanum, (caddr_t)0, 0, UBA_NEEDBDP);
			}
#		endif
#		ifdef DEBUG
		printd10("tmscpstrategy: Controller not active, starting it\n");
#		endif
		(void) tmscpstart(um);
		}
	splx(s);
	return;
}

int
tmscpread(dev, uio)
	dev_t dev;
	struct uio *uio;
{

	return (physio(tmscpstrategy, NULL, dev, B_READ, minphys, uio));
}

int
tmscpwrite(dev, uio)
	dev_t dev;
	struct uio *uio;
{

	return (physio(tmscpstrategy, NULL, dev, B_WRITE, minphys, uio));
}

#define DBSIZE 32

#define ca_Rspdsc       ca_rspdsc[0]
#define ca_Cmddsc       ca_rspdsc[1]
#define tmscp_Rsp       tmscp_rsp[0]
#define tmscp_Cmd       tmscp_cmd[0]

struct  tmscp     tmscpd[NTMSCP];

int
tmscpdump(dev, blkno, va, size)
	dev_t dev;
	daddr_t blkno;
	caddr_t va;
	size_t size;
{
#ifdef notyet
	volatile struct tmscpdevice *tmscpaddr;
	volatile struct tmscp *tmscp_ubaddr;
	char *start;
	int num, blk, unit;
	register struct uba_regs *uba;
	register struct uba_device *ui;
	register volatile struct tmscp *tmscpp;
	register struct pte *io;
	register int i;

	unit = minor(dev) & 03;
	if (unit >= NTMS)
		return (ENXIO);
#	define phys(cast, addr) ((cast)((int)addr & 0x7fffffff))
	ui = phys(struct uba_device *, tmsdinfo[unit]);
	if (ui->ui_alive == 0)
		return (ENXIO);
	uba = phys(struct uba_softc *, ui->ui_hd)->uh_physuba;
	ubainit(uba);
	tmscpaddr = (struct tmscpdevice *)ui->ui_physaddr;
	DELAY(2000000);
	tmscpp = phys(struct tmscp *, &tmscpd[ui->ui_ctlr]);

	num = btoc(sizeof(struct tmscp)) + 1;
	io = (struct pte *)&uba->uba_map[NUBMREG-num];
	for(i = 0; i<num; i++)
		*(int *)io++ = UBAMR_MRV|(btop(tmscpp)+i);
	tmscp_ubaddr = (struct tmscp *)(((int)tmscpp & PGOFSET)|((NUBMREG-num)<<9));

	tmscpaddr->tmscpip = 0;
	while ((tmscpaddr->tmscpsa & TMSCP_STEP1) == 0)
		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
	tmscpaddr->tmscpsa = TMSCP_ERR;
	while ((tmscpaddr->tmscpsa & TMSCP_STEP2) == 0)
		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
	tmscpaddr->tmscpsa = (short)((int)&tmscp_ubaddr->tmscp_ca.ca_ringbase);
	while ((tmscpaddr->tmscpsa & TMSCP_STEP3) == 0)
		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
	tmscpaddr->tmscpsa = (short)(((int)&tmscp_ubaddr->tmscp_ca.ca_ringbase) >> 16);
	while ((tmscpaddr->tmscpsa & TMSCP_STEP4) == 0)
		if(tmscpaddr->tmscpsa & TMSCP_ERR) return(EFAULT);
	tmscpaddr->tmscpsa = TMSCP_GO;
	tmscpp->tmscp_ca.ca_Rspdsc = (long)&tmscp_ubaddr->tmscp_Rsp.mscp_cmdref;
	tmscpp->tmscp_ca.ca_Cmddsc = (long)&tmscp_ubaddr->tmscp_Cmd.mscp_cmdref;
	tmscpp->tmscp_Cmd.mscp_header.tmscp_vcid = 1;	/* for tape */
	tmscpp->tmscp_Cmd.mscp_cntflgs = 0;
	tmscpp->tmscp_Cmd.mscp_version = 0;
	if (tmscpcmd(M_OP_STCON, tmscpp, tmscpaddr) == 0) {
		return(EFAULT);
	}
	tmscpp->tmscp_Cmd.mscp_unit = ui->ui_slave;
	if (tmscpcmd(M_OP_ONLIN, tmscpp, tmscpaddr) == 0) {
		return(EFAULT);
	}

	num = maxfree;
	start = 0;
	while (num > 0)
		{
		blk = num > DBSIZE ? DBSIZE : num;
		io = (struct pte *)uba->uba_map;
		for (i = 0; i < blk; i++)
			*(int *)io++ = (btop(start)+i) | UBAMR_MRV;
		*(int *)io = 0;
		tmscpp->tmscp_Cmd.mscp_lbn = btop(start);
		tmscpp->tmscp_Cmd.mscp_unit = ui->ui_slave;
		tmscpp->tmscp_Cmd.mscp_bytecnt = blk*NBPG;
#		ifdef	MVAX
		if( cpu == MVAX_I )
			tmscpp->tmscp_Cmd.mscp_buffer = (long) start;
		else
#		endif 	MVAX
			tmscpp->tmscp_Cmd.mscp_buffer = 0;
		if (tmscpcmd(M_OP_WRITE, tmscpp, tmscpaddr) == 0)
			return(EIO);
		start += blk*NBPG;
		num -= blk;
		}
#endif
	return (0);
}


/*
 * Perform a standalone tmscp command.  This routine is only used by tmscpdump.
 */

int
tmscpcmd(op, tmscpp, tmscpaddr)
	int op;
	struct tmscp *tmscpp;
	struct tmscpdevice *tmscpaddr;
{
	volatile int i;


	tmscpp->tmscp_Cmd.mscp_opcode = op;
	tmscpp->tmscp_Rsp.mscp_header.tmscp_msglen = mscp_msglen;
	tmscpp->tmscp_Cmd.mscp_header.tmscp_msglen = mscp_msglen;
	tmscpp->tmscp_ca.ca_Rspdsc |= TMSCP_OWN|TMSCP_INT;
	tmscpp->tmscp_ca.ca_Cmddsc |= TMSCP_OWN|TMSCP_INT;
	if (tmscpaddr->tmscpsa&TMSCP_ERR)
		printf("tmscp fatal error (0%o)\n", tmscpaddr->tmscpsa&0xffff);
	i = tmscpaddr->tmscpip;
#ifdef lint
	i = i;
#endif
	for (;;)
		{
		if (tmscpp->tmscp_ca.ca_cmdint)
			tmscpp->tmscp_ca.ca_cmdint = 0;
		if (tmscpp->tmscp_ca.ca_rspint)
			break;
		}
	tmscpp->tmscp_ca.ca_rspint = 0;
	if (tmscpp->tmscp_Rsp.mscp_opcode != (op|M_OP_END) ||
	    (tmscpp->tmscp_Rsp.mscp_status&M_ST_MASK) != M_ST_SUCC)
		{
		printf("error: com %d opc 0x%x stat 0x%x\ndump ", op,
			tmscpp->tmscp_Rsp.mscp_opcode, tmscpp->tmscp_Rsp.mscp_status);
		return(0);
		}
	return(1);
}

/*
 * Catch ioctl commands, and call the "command" routine to do them.
 */

/* ARGSUSED */
int
tmscpioctl(dev, cmd, data, flag, p)
	dev_t dev;
	u_long cmd;
	caddr_t data;
	int flag;
	struct proc *p;
{
	register struct buf *bp = &ctmscpbuf[TMSCPCTLR(dev)];
	register callcount;	/* number of times to call cmd routine */
	register struct uba_device *ui;
	register struct tms_info *tms;
	int fcount;		/* number of files (or records) to space */
	int error = 0;
	register struct mtop *mtop;	/* mag tape cmd op to perform */
	register struct mtget *mtget;	/* mag tape struct to get info in */

	/* we depend of the values and order of the TMS ioctl codes here */
	static tmsops[] =
	 {TMS_WRITM,TMS_FSF,TMS_BSF,TMS_FSR,TMS_BSR,TMS_REW,TMS_OFFL,TMS_SENSE,
	  TMS_CACHE,TMS_NOCACHE};

	switch (cmd) {
	case MTIOCTOP:	/* tape operation */
		mtop = (struct mtop *)data;
		switch (mtop->mt_op) {

		case MTWEOF:
			callcount = mtop->mt_count;
			fcount = 1;
			break;
		case MTFSF: case MTBSF:
		case MTFSR: case MTBSR:
			callcount = 1;
			fcount = mtop->mt_count;
			break;
		case MTREW: case MTOFFL: case MTNOP:
		case MTCACHE: case MTNOCACHE:
			callcount = 1;
			fcount = 1;		/* wait for this rewind */
			break;
		default:
			return (ENXIO);
		}	/* end switch mtop->mt_op */

		if (callcount <= 0 || fcount <= 0)
			return (EINVAL);
		while (--callcount >= 0)
			{
			tmscpcommand(dev, tmsops[mtop->mt_op], fcount);
			if ((mtop->mt_op == MTFSR || mtop->mt_op == MTBSR) &&
			    bp->b_resid)
				return (EIO);
			if (bp->b_flags & B_ERROR)	/* like hitting BOT */
				break;
			}
		if (bp->b_flags&B_ERROR)
			if ((error = bp->b_error)==0)
				return (EIO);
		return (error);

	case MTIOCGET:
		/*
		 * Return status info associated with the particular UNIT.
		 */
		ui = tmsdinfo[TMSUNIT(dev)];
		tms = &tms_info[ui->ui_unit];
		mtget = (struct mtget *)data;
		mtget->mt_type = MT_ISTMSCP;
		mtget->mt_dsreg = tms->tms_flags << 8;
		mtget->mt_dsreg |= tms->tms_endcode;
		mtget->mt_erreg = tms->tms_status;
		mtget->mt_resid = tms->tms_resid;
		break;

	default:
		return (ENXIO);
	}
	return (0);
}


/*
 * Reset (for raw mode use only).
 */
void
tmscpreset (uban)
	int uban;
{
	register struct uba_ctlr *um;
	register struct uba_device *ui;
	register struct buf *bp, *dp;
	register int unit;
	struct buf *nbp;
	int d;

	for (d = 0; d < NTMSCP; d++)
		{
		if ((um = tmscpminfo[d]) == 0 || um->um_ubanum != uban ||
		    um->um_alive == 0)
			continue;
		printf(" tmscp%d", d);
		um->um_tab.b_active = 0;
		um->um_tab.b_actf = 0;
		tmscp_softc[d].sc_state = S_IDLE;
		tmscp_softc[d].sc_mapped = 0;
		for (unit = 0; unit < NTMS; unit++)
			{
			if ((ui = tmsdinfo[unit]) == 0)
				continue;
			if (ui->ui_alive == 0 || ui->ui_mi != um)
				continue;
			tmsutab[unit].b_active = 0;
			tmsutab[unit].b_qsize = 0;
			}
		for (bp = tmscpwtab[d].b_actf; bp; bp = nbp)
			{
			nbp = bp->b_actf;
			bp->b_ubinfo = 0;
			/*
			 * Link the buffer onto the drive queue
			 */
			dp = &tmsutab[TMSUNIT(bp->b_dev)];
			MSCP_APPEND(bp, dp, b_actf);
			/*
			 * Link the drive onto the controller queue
			 */
			if (dp->b_active == 0)
				{
				MSCP_APPEND(dp, &um->um_tab, b_hash.le_next);
				dp->b_active = 1;
				}
			}
		(void)tmscpinit(d);
		}
}


/*
 * Process an error log message
 *
 * Only minimal decoding is done, only "useful"
 * information is printed.  Eventually should
 * send message to an error logger.
 */
void
tmserror(um, mp)
	register struct uba_ctlr *um;
	register struct mslg *mp;
{
	register i;

#	ifdef DEBUG
	printd("tmserror:\n");
#	endif
	if(!(mp->mslg_flags & (M_LF_SUCC | M_LF_CONT)))
		log(TMS_PRI, "tmscp%d: %s error, ", um->um_ctlr,
		mp->mslg_flags & ( M_LF_SUCC | M_LF_CONT ) ? "soft" : "hard");

	switch (mp->mslg_format) {

	case M_FM_CNTERR:
		log(TMS_PRI, "controller error, event 0%o\n", mp->mslg_event);
		break;
	case M_FM_BUSADDR:
		log(TMS_PRI, "host memory access error, event 0%o, addr 0%o\n",
		    mp->mslg_event,
		    (unsigned int)(mp->mslg_unitid & 0xffffffff));
		break;
	case M_FM_TAPETRN:
		log(TMS_PRI, "tape transfer error, unit %d, grp 0x%x, event 0%o\n",
			mp->mslg_unit, mp->mslg_group, mp->mslg_event);
		break;
	case M_FM_STIERR:
		log(TMS_PRI, "STI error, unit %d, event 0%o\n",
			mp->mslg_unit, mp->mslg_event);
#ifdef notdef
		/* too painful to do with log() */
		for(i = 0; i < 62;i++)
			mprintf("\t0x%x",mp->mslg_stiunsucc[i] & 0xff);
		mprintf("\n");
#endif
		break;
	case M_FM_STIDEL:
		log(TMS_PRI, "STI Drive Error Log, unit %d, event 0%o\n",
			mp->mslg_unit, mp->mslg_event);
		break;
	case M_FM_STIFEL:
		log(TMS_PRI, "STI Formatter Error Log, unit %d, event 0%o\n",
			mp->mslg_unit, mp->mslg_event);
		break;
	default:
		log(TMS_PRI, "unknown error, unit %d, format 0%o, event 0%o\n",
			mp->mslg_unit, mp->mslg_format, mp->mslg_event);
	}

	if (tmscperror)
		{
		register long *p = (long *)mp;

		for (i = 0; i < mp->mslg_header.tmscp_msglen; i += sizeof(*p))
			printf("%x ", (unsigned int)*p++);
		printf("\n");
		}
}

int
tmscp_match(parent, match, aux)
        struct device *parent;
        void *match, *aux;
{
        return 0;
}

void
tmscp_attach(parent, self, aux)
        struct device *parent, *self;
        void *aux;
{
}

#endif