summaryrefslogtreecommitdiff
path: root/sys/arch/mac68k/dev/ncr5380.c
blob: e387ccdebf512eede39e697cd53e7f89fb80ca65 (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
/*	$OpenBSD: ncr5380.c,v 1.9 1996/06/23 16:08:23 briggs Exp $	*/
/*	$NetBSD: ncr5380.c,v 1.31 1996/06/23 15:02:58 briggs Exp $	*/

/*
 * Copyright (c) 1995 Leo Weppelman.
 * 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 Leo Weppelman.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * Bit mask of targets you want debugging to be shown
 */
u_char	dbg_target_mask = 0x7f;

/*
 * Set bit for target when parity checking must be disabled.
 * My (LWP) Maxtor 7245S  seems to generate parity errors on about 50%
 * of all transfers while the data is correct!?
 */
u_char	ncr5380_no_parchk = 0xff;

/*
 * Flags to allow binpatching of which devices to allow or disallow
 * issuance of linked commands.  disallowance is favored over allowance,
 * so if the same bit is set in either, the driver will not issue linked
 * commands to the corresponding target.
 * SCSI-2 devices can report whether or not they will accept linked
 * commands.  If that reports that a device supports linked commands,
 * then it is treated as if the corresponding bit in ncr5380_allow_linked
 * is set.
 */
u_char	ncr5380_allow_linked = 0x80;
u_char	ncr5380_disallow_linked = 0x80;

/*
 * This is the default sense-command we send.
 */
static	u_char	sense_cmd[] = {
		REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense_data), 0
};

/*
 * True if the main co-routine is running
 */
static volatile int	main_running = 0;

/*
 * Mask of targets selected
 */
static u_char	busy;

static void	ncr5380_minphys __P((struct buf *bp));
static int	ncr5380_scsi_cmd __P((struct scsi_xfer *xs));
static void	ncr5380_show_scsi_cmd __P((struct scsi_xfer *xs));

struct scsi_adapter ncr5380_switch = {
	ncr5380_scsi_cmd,		/* scsi_cmd()			*/
	ncr5380_minphys,		/* scsi_minphys()		*/
	0,				/* open_target_lu()		*/
	0				/* close_target_lu()		*/
};

struct scsi_device ncr5380_dev = {
	NULL,		/* use default error handler		*/
	NULL,		/* do not have a start functio		*/
	NULL,		/* have no async handler		*/
	NULL		/* Use default done routine		*/
};


static SC_REQ	req_queue[NREQ];
static SC_REQ	*free_head = NULL;	/* Free request structures	*/


/*
 * Inline functions:
 */

/*
 * Determine the size of a SCSI command.
 */
extern __inline__ int command_size(opcode)
u_char	opcode;
{
	switch ((opcode >> 4) & 0xf) {
		case 0:
		case 1:
			return (6);
		case 2:
		case 3:
			return (10);
	}
	return (12);
}


/*
 * Wait for request-line to become active. When it doesn't return 0.
 * Otherwise return != 0.
 * The timeouts in the 'wait_req_*' functions are arbitrary and rather
 * large. In 99% of the invocations nearly no timeout is needed but in
 * some cases (especially when using my tapedrive, a Tandberg 3600) the
 * device is busy internally and the first SCSI-phase will be delayed.
 *
 * -- A sleeping Fujitsu M2512 is even worse; try 2.5 sec     -hf 20 Jun
 */
extern __inline__ int wait_req_true(void)
{
	int	timeout = 2500000;

	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
		delay(1);
	return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
}

/*
 * Wait for request-line to become inactive. When it doesn't return 0.
 * Otherwise return != 0.
 */
extern __inline__ int wait_req_false(void)
{
	int	timeout = 2500000;

	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
		delay(1);
	return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
}

extern __inline__ void ack_message()
{
	SET_5380_REG(NCR5380_ICOM, 0);
}

extern __inline__ void nack_message(SC_REQ *reqp, u_char msg)
{
	SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
	reqp->msgout = msg;
}

extern __inline__ void finish_req(SC_REQ *reqp)
{
	int			sps;
	struct scsi_xfer	*xs = reqp->xs;

#ifdef REAL_DMA
	/*
	 * If we bounced, free the bounce buffer
	 */
	if (reqp->dr_flag & DRIVER_BOUNCING) 
		free_bounceb(reqp->bounceb);
#endif /* REAL_DMA */
#ifdef DBG_REQ
	if (dbg_target_mask & (1 << reqp->targ_id))
		show_request(reqp, "DONE");
#endif
#ifdef DBG_ERR_RET
	if (reqp->xs->error != 0)
		show_request(reqp, "ERR_RET");
#endif
	/*
	 * Return request to free-q
	 */
	sps = splbio();
	reqp->next = free_head;
	free_head  = reqp;
	splx(sps);

	xs->flags |= ITSDONE;
	if (!(reqp->dr_flag & DRIVER_LINKCHK))
		scsi_done(xs);
}

/*
 * Auto config stuff....
 */
int	ncr_cprint __P((void *auxp, char *));
void	ncr_attach __P((struct device *, struct device *, void *));
int	ncr_match __P((struct device *, void *, void *));

/*
 * Tricks to make driver-name configurable
 */
#define CFNAME(n)	__CONCAT(n,_cd)
#define CANAME(n)	__CONCAT(n,_ca)
#define CFSTRING(n)	__STRING(n)

struct cfattach CANAME(DRNAME) = {
	sizeof(struct ncr_softc), ncr_match, ncr_attach
};

struct cfdriver CFNAME(DRNAME) = {
	NULL, CFSTRING(DRNAME), DV_DULL
};

int
ncr_match(pdp, match, auxp)
struct device	*pdp;
void		*match, *auxp;
{
	return (machine_match(pdp, match, auxp, &CFNAME(DRNAME)));
}

void
ncr_attach(pdp, dp, auxp)
struct device	*pdp, *dp;
void		*auxp;
{
	struct ncr_softc	*sc;
	int			i;

	sc = (struct ncr_softc *)dp;

	sc->sc_link.adapter_softc   = sc;
	sc->sc_link.adapter_target  = 7;
	sc->sc_link.adapter         = &ncr5380_switch;
	sc->sc_link.device          = &ncr5380_dev;
	sc->sc_link.openings        = NREQ - 1;

	/*
	 * bitmasks
	 */
	sc->sc_noselatn = 0;
	sc->sc_selected = 0;

	/*
	 * Initialize machine-type specific things...
	 */
	scsi_mach_init(sc);
	printf("\n");

	/*
	 * Initialize request queue freelist.
	 */
	for (i = 0; i < NREQ; i++) {
		req_queue[i].next = free_head;
		free_head = &req_queue[i];
	}

	/*
	 * Initialize the host adapter
	 */
	scsi_idisable();
	ENABLE_NCR5380(sc);
	SET_5380_REG(NCR5380_ICOM, 0);
	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
	SET_5380_REG(NCR5380_TCOM, 0);
	SET_5380_REG(NCR5380_IDSTAT, 0);
	scsi_ienable();

	/*
	 * attach all scsi units on us
	 */
	config_found(dp, &sc->sc_link, ncr_cprint);
}

/*
 * print diag if name is NULL else just extra
 */
int
ncr_cprint(auxp, name)
void	*auxp;
char	*name;
{
	if (name == NULL)
		return (UNCONF);
	return (QUIET);
}
/*
 * End of auto config stuff....
 */

/*
 * Carry out a request from the high level driver.
 */
static int
ncr5380_scsi_cmd(struct scsi_xfer *xs)
{
	int	sps;
	SC_REQ	*reqp, *link, *tmp;
	int	flags = xs->flags;

	/*
	 * We do not queue RESET commands
	 */
	if (flags & SCSI_RESET) {
		scsi_reset_verbose(xs->sc_link->adapter_softc,
				   "Got reset-command");
		return (COMPLETE);
	}

	/*
	 * Get a request block
	 */
	sps = splbio();
	if ((reqp = free_head) == 0) {
		splx(sps);
		return (TRY_AGAIN_LATER);
	}
	free_head  = reqp->next;
	reqp->next = NULL;
	splx(sps);

	/*
	 * Initialize our private fields
	 */
	reqp->dr_flag   = (flags & SCSI_POLL) ? DRIVER_NOINT : 0;
	reqp->phase     = NR_PHASE;
	reqp->msgout    = MSG_NOOP;
	reqp->status    = SCSGOOD;
	reqp->message   = 0xff;
	reqp->link      = NULL;
	reqp->xs        = xs;
	reqp->targ_id   = xs->sc_link->target;
	reqp->targ_lun  = xs->sc_link->lun;
	reqp->xdata_ptr = (u_char*)xs->data;
	reqp->xdata_len = xs->datalen;
	memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic));
	reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;

	/*
	 * Sanity check on flags...
	 */
	if (flags & ITSDONE) {
		ncr_tprint(reqp, "scsi_cmd: command already done.....\n");
		xs->flags &= ~ITSDONE;
	}
	if (!(flags & INUSE)) {
		ncr_tprint(reqp, "scsi_cmd: command not in use.....\n");
		xs->flags |= INUSE;
	}

#ifdef REAL_DMA
	/*
	 * Check if DMA can be used on this request
	 */
	if (scsi_dmaok(reqp))
		reqp->dr_flag |= DRIVER_DMAOK;
#endif /* REAL_DMA */

	/*
	 * Insert the command into the issue queue. Note that 'REQUEST SENSE'
	 * commands are inserted at the head of the queue since any command
	 * will clear the existing contingent allegience condition and the sense
	 * data is only valid while the condition exists.
	 * When possible, link the command to a previous command to the same
	 * target. This is not very sensible when AUTO_SENSE is not defined!
	 * Interrupts are disabled while we are fiddling with the issue-queue.
	 */
	sps = splbio();
	link = NULL;
	if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) {
		reqp->next = issue_q;
		issue_q    = reqp;
	}
	else {
		tmp  = issue_q;
		do {
		    if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
				link = tmp;
		} while (tmp->next && (tmp = tmp->next));
		tmp->next = reqp;
#ifdef AUTO_SENSE
		if (link && (   (xs->sc_link->inquiry_flags & SID_Linked)
			     || ((1<<reqp->targ_id) & ncr5380_allow_linked))
			 && !((1<<reqp->targ_id) & ncr5380_disallow_linked)) {
			link->link = reqp;
			link->xcmd.bytes[link->xs->cmdlen-2] |= 1;
		}
#endif
	}
	splx(sps);

#ifdef DBG_REQ
	if (dbg_target_mask & (1 << reqp->targ_id))
		show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ?
								"HEAD":"TAIL");
#endif

	run_main(xs->sc_link->adapter_softc);

	if (xs->flags & (SCSI_POLL|ITSDONE))
		return (COMPLETE); /* We're booting or run_main has completed */
	return (SUCCESSFULLY_QUEUED);
}

static void
ncr5380_minphys(struct buf *bp)
{
    if (bp->b_bcount > MIN_PHYS)
	bp->b_bcount = MIN_PHYS;
    minphys(bp);
}
#undef MIN_PHYS

static void
ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
{
	u_char	*b = (u_char *) xs->cmd;
	int	i  = 0;

	if (!(xs->flags & SCSI_RESET)) {
		printf("(%d:%d:%d,0x%x)-", xs->sc_link->scsibus,
		    xs->sc_link->target, xs->sc_link->lun, xs->sc_link->flags);
		while (i < xs->cmdlen) {
			if (i)
				printf(",");
			printf("%x",b[i++]);
		}
		printf("-\n");
	}
	else {
		printf("(%d:%d:%d)-RESET-\n",
		    xs->sc_link->scsibus,xs->sc_link->target, xs->sc_link->lun);
	}
}

/*
 * The body of the driver.
 */
static void
scsi_main(sc)
struct ncr_softc *sc;
{
	SC_REQ	*req, *prev;
	int	itype;
	int	sps;

	/*
	 * While running in the driver SCSI-interrupts are disabled.
	 */
	scsi_idisable();
	ENABLE_NCR5380(sc);

	PID("scsi_main1");
	for (;;) {
	    sps = splbio();
	    if (!connected) {
		/*
		 * Check if it is fair keep any exclusive access to DMA
		 * claimed. If not, stop queueing new jobs so the discon_q
		 * will be eventually drained and DMA can be given up.
		 */
		if (!fair_to_keep_dma())
			goto main_exit;

		/*
		 * Search through the issue-queue for a command
		 * destined for a target that isn't busy.
		 */
		prev = NULL;
		for (req=issue_q; req != NULL; prev = req, req = req->next) {
			if (!(busy & (1 << req->targ_id))) {
				/*
				 * Found one, remove it from the issue queue
				 */
				if (prev == NULL) 
					issue_q = req->next;
				else prev->next = req->next;
				req->next = NULL;
				break;
			}
		}

		/*
		 * When a request has just ended, we get here before an other
		 * device detects that the bus is free and that it can
		 * reconnect. The problem is that when this happens, we always
		 * baffle the device because our (initiator) id is higher. This
		 * can cause a sort of starvation on slow devices. So we check
		 * for a pending reselection here.
		 * Note that 'connected' will be non-null if the reselection
		 * succeeds.
		 */
		if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
						== (SC_S_SEL|SC_S_IO)){
			if (req != NULL) {
				req->next = issue_q; 
				issue_q = req;
			}
			splx(sps);

			reselect(sc);
			scsi_clr_ipend();
			goto connected;
		}

		/*
		 * The host is not connected and there is no request
		 * pending, exit.
		 */
		if (req == NULL) {
			PID("scsi_main2");
			goto main_exit;
		}

		/*
		 * Re-enable interrupts before handling the request.
		 */
		splx(sps);

#ifdef DBG_REQ
		if (dbg_target_mask & (1 << req->targ_id))
			show_request(req, "TARGET");
#endif
		/*
		 * We found a request. Try to connect to the target. If the
		 * initiator fails arbitration, the command is put back in the
		 * issue queue.
		 */
		if (scsi_select(req, 0)) {
			sps = splbio();
			req->next = issue_q; 
			issue_q = req;
			splx(sps);
#ifdef DBG_REQ
			if (dbg_target_mask & (1 << req->targ_id))
				ncr_tprint(req, "Select failed\n");
#endif
		}
	    }
	    else splx(sps);
connected:
	    if (connected) {
		/*
		 * If the host is currently connected but a 'real-dma' transfer
		 * is in progress, the 'end-of-dma' interrupt restarts main.
		 * So quit.
		 */
		sps = splbio();
		if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
			PID("scsi_main3");
			goto main_exit;
		}
		splx(sps);

		/*
		 * Let the target guide us through the bus-phases
		 */
		while (information_transfer(sc) == -1)
			;
	    }
	}
	/* NEVER TO REACH HERE */
	panic("ncr5380-SCSI: not designed to come here");

main_exit:
	/*
	 * We enter here with interrupts disabled. We are about to exit main
	 * so interrupts should be re-enabled. Because interrupts are edge
	 * triggered, we could already have missed the interrupt. Therefore
	 * we check the IRQ-line here and re-enter when we really missed a
	 * valid interrupt.
	 */
	PID("scsi_main4");
	scsi_ienable();

	/*
	 * If we're not currently connected, enable reselection
	 * interrupts.
	 */
	if (!connected)
		SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);

	if (scsi_ipending()) {
		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
			scsi_idisable();
			splx(sps);

			if (itype == INTR_RESEL)
				reselect(sc);
#ifdef REAL_DMA
			else dma_ready();
#else
			else {
				if (pdma_ready())
					goto connected;
				panic("Got DMA interrupt without DMA");
			}
#endif
			scsi_clr_ipend();
			goto connected;
		}
	}
	reconsider_dma();

	main_running = 0;
	splx(sps);
	PID("scsi_main5");
}

#ifdef REAL_DMA
/*
 * The SCSI-DMA interrupt.
 * This interrupt can only be triggered when running in non-polled DMA
 * mode. When DMA is not active, it will be silently ignored, it is usually
 * to late because the EOP interrupt of the controller happens just a tiny
 * bit earlier. It might become usefull when scatter/gather is implemented,
 * because in that case only part of the DATAIN/DATAOUT transfer is taken
 * out of a single buffer.
 */
static void
ncr_dma_intr(sc)
struct ncr_softc *sc;
{
	SC_REQ	*reqp;
	int	dma_done;

	PID("ncr_dma_intr");
	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
		scsi_idisable();
		if (!(dma_done = dma_ready())) {
			transfer_dma(reqp, reqp->phase, 0);
			return;
		}
		run_main(sc);
	}
}
#endif /* REAL_DMA */

/*
 * The SCSI-controller interrupt. This interrupt occurs on reselections and
 * at the end of non-polled DMA-interrupts. It is assumed to be called from
 * the machine-dependent hardware interrupt.
 */
static void
ncr_ctrl_intr(sc)
struct ncr_softc *sc;
{
	int	itype;

	while (scsi_ipending()) {
		scsi_idisable();
		if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
			if (itype == INTR_RESEL)
				reselect(sc);
			else {
#ifdef REAL_DMA
			    int	dma_done;
			    if (!(dma_done = dma_ready())) {
				transfer_dma(connected, connected->phase, 0);
				return;
			    }
#else
			    if (pdma_ready())
				return;
			    panic("Got DMA interrupt without DMA\n");
#endif
			}
			scsi_clr_ipend();
		}
		run_main(sc);
		return;
	}
	PID("ncr_ctrl_intr1");
}

/*
 * Initiate a connection path between the host and the target. The function
 * first goes into arbitration for the SCSI-bus. When this succeeds, the target
 * is selected and an 'IDENTIFY' message is send.
 * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
 * the target does not respond (to either selection or 'MESSAGE OUT') the
 * 'done' function is executed.
 * The result code given by the driver can be influenced by setting 'code'
 * to a non-zero value. This is the case when 'select' is called by abort.
 */
static int
scsi_select(reqp, code)
SC_REQ	*reqp;
int	code;
{
	u_char			tmp[1];
	u_char			phase;
	u_long			cnt;
	int			sps;
	u_int8_t		atn_flag;
	u_int8_t		targ_bit;
	struct ncr_softc	*sc;

	sc = reqp->xs->sc_link->adapter_softc;
	DBG_SELPRINT ("Starting arbitration\n", 0);
	PID("scsi_select1");

	sps = splbio();

	/*
	 * Prevent a race condition here. If a reslection interrupt occurred
	 * between the decision to pick a new request and the call to select,
	 * we abort the selection.
	 * Interrupts are lowered when the 5380 is setup to arbitrate for the
	 * bus.
	 */
	if (connected) {
		splx(sps);
		PID("scsi_select2");
		return (-1);
	}

	/*
	 * Set phase bits to 0, otherwise the 5380 won't drive the bus during
	 * selection.
	 */
	SET_5380_REG(NCR5380_TCOM, 0);
	SET_5380_REG(NCR5380_ICOM, 0);

	/*
	 * Arbitrate for the bus.
	 */
	SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
	SET_5380_REG(NCR5380_MODE, SC_ARBIT);
 
	splx(sps);

	cnt = 10;
	while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
		delay(1);

	if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
		SET_5380_REG(NCR5380_ICOM, 0);
		DBG_SELPRINT ("Arbitration lost, bus not free\n",0);
		PID("scsi_select3");
		return (-1);
	}

	/* The arbitration delay is 2.2 usecs */
	delay(3);

	/*
	 * Check the result of the arbitration. If we failed, return -1.
	 */
	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
		SET_5380_REG(NCR5380_ICOM, 0);
		PID("scsi_select4");
		return (-1);
	}

	/*
	 * The spec requires that we should read the data register to
	 * check for higher id's and check the SC_LA again.
	 */
	tmp[0] = GET_5380_REG(NCR5380_DATA);
	if (tmp[0] & ~((SC_HOST_ID << 1) - 1)) {
		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
		SET_5380_REG(NCR5380_ICOM, 0);
		DBG_SELPRINT ("Arbitration lost, higher id present\n",0);
		PID("scsi_select5");
		return (-1);
	}
	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
		SET_5380_REG(NCR5380_ICOM, 0);
		DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
		PID("scsi_select6");
		return (-1);
	}
	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
	if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
		SET_5380_REG(NCR5380_ICOM, 0);
		DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
		PID("scsi_select7");
		return (-1);
	}
	/* Bus settle delay + Bus clear delay = 1.2 usecs */
	delay(2);
	DBG_SELPRINT ("Arbitration complete\n", 0);

	/*
	 * Now that we won the arbitration, start the selection.
	 */
	targ_bit = 1 << reqp->targ_id;
	SET_5380_REG(NCR5380_DATA, SC_HOST_ID | targ_bit);

	if (sc->sc_noselatn & targ_bit)
		atn_flag = 0;
	else
		atn_flag = SC_A_ATN;

	/*
	 * Raise ATN while SEL is true before BSY goes false from arbitration,
	 * since this is the only way to guarantee that we'll get a MESSAGE OUT
	 * phase immediately after the selection.
	 */
	SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | atn_flag | SC_ADTB);
	SET_5380_REG(NCR5380_MODE, IMODE_BASE);

	/*
	 * Turn off reselection interrupts
	 */
	SET_5380_REG(NCR5380_IDSTAT, 0);

	/*
	 * Reset BSY. The delay following it, surpresses a glitch in the
	 * 5380 which causes us to see our own BSY signal instead of that of
	 * the target.
	 */
	SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag | SC_ADTB);
	delay(1);

	/*
	 * Wait for the target to react, the specs call for a timeout of
	 * 250 ms.
	 */
	cnt = 25000;
	while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
		delay(10);

	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
		/*
		 * There is no reaction from the target, start the selection
		 * timeout procedure. We release the databus but keep SEL
		 * asserted. After that we wait a 'selection abort time' (200
		 * usecs) and 2 deskew delays (90 ns) and check BSY again.
		 * When BSY is asserted, we assume the selection succeeded,
		 * otherwise we release the bus.
		 */
		SET_5380_REG(NCR5380_ICOM, SC_A_SEL | atn_flag);
		delay(201);
		if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
			SET_5380_REG(NCR5380_ICOM, 0);
			reqp->xs->error      = code ? code : XS_SELTIMEOUT;
			DBG_SELPRINT ("Target %d not responding to sel\n",
								reqp->targ_id);
			finish_req(reqp);
			PID("scsi_select8");
			return (0);
		}
	}
	SET_5380_REG(NCR5380_ICOM, atn_flag);

	DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);

	/*
	 * The SCSI-interrupts are disabled while a request is being handled.
	 */
	scsi_idisable();

	/*
	 * If we did not request ATN, then don't try to send IDENTIFY.
	 */
	if (atn_flag == 0) {
		reqp->phase = PH_CMD;
		goto identify_failed;
	}

	/*
	 * Here we prepare to send an 'IDENTIFY' message.
	 * Allow disconnect only when interrups are allowed.
	 */
	tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
			(reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
	cnt    = 1;
	phase  = PH_MSGOUT;

	/*
	 * Since we followed the SCSI-spec and raised ATN while SEL was true
	 * but before BSY was false during the selection, a 'MESSAGE OUT'
	 * phase should follow.  Unfortunately, this does not happen on
	 * all targets (Asante ethernet devices, for example), so we must 
	 * check the actual mode if the message transfer fails--if the
	 * new phase is PH_CMD and has never been successfully selected
	 * w/ATN in the past, then we assume that it is an old device
	 * that doesn't support select w/ATN.
	 */
	if (transfer_pio(&phase, tmp, &cnt, 0) || cnt) {

		if ((phase == PH_CMD) && !(sc->sc_selected & targ_bit)) {
			DBG_SELPRINT ("Target %d: not responding to ATN.\n",
							reqp->targ_id);
			sc->sc_noselatn |= targ_bit;
			reqp->phase = PH_CMD;
			goto identify_failed;
		}

		DBG_SELPRINT ("Target %d: failed to send identify\n",
							reqp->targ_id);
		/*
		 * Try to disconnect from the target.  We cannot leave
		 * it just hanging here.
		 */
		if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
			u_long	len   = 1;
			u_char	phase = PH_MSGOUT;
			u_char	msg   = MSG_ABORT;

			transfer_pio(&phase, &msg, &len, 0);
		}
		else scsi_reset_verbose(sc, "Connected to unidentified target");

		SET_5380_REG(NCR5380_ICOM, 0);
		reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
		finish_req(reqp);
		PID("scsi_select9");
		return (0);
	}
	reqp->phase = PH_MSGOUT;

identify_failed:
	sc->sc_selected |= targ_bit;

#ifdef notyet /* LWP: Do we need timeouts in the driver? */
	/*
	 * Command is connected, start timer ticking.
	 */
	ccb_p->xtimeout = ccb_p->timeout + Lbolt;
#endif

	connected  = reqp;
	busy      |= targ_bit;
	PID("scsi_select10");
	return (0);
}

/*
 * Return codes:
 *	 0: Job has finished or disconnected, find something else
 *	-1: keep on calling information_transfer() from scsi_main()
 */
static int
information_transfer(sc)
struct ncr_softc *sc;
{
	SC_REQ	*reqp = connected;
	u_char	tmp, phase;
	u_long	len;

	PID("info_transf1");
	/*
	 * Clear pending interrupts from 5380-chip.
	 */
	scsi_clr_ipend();

	/*
	 * The SCSI-spec requires BSY to be true while connected to a target,
	 * loosing it means we lost the target...
	 * Also REQ needs to be asserted here to indicate that the bus-phase
	 * is valid. When the target does not supply REQ within a 'reasonable'
	 * amount of time, it's probably lost in it's own maze of twisting
	 * passages, we have to reset the bus to free it.
	 */
	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
		wait_req_true();
	tmp = GET_5380_REG(NCR5380_IDSTAT);


	if ((tmp & (SC_S_BSY|SC_S_REQ)) != (SC_S_BSY|SC_S_REQ)) {
		busy           &= ~(1 << reqp->targ_id);
		connected       = NULL;
		reqp->xs->error = XS_TIMEOUT;
		finish_req(reqp);
		if (!(tmp & SC_S_REQ))
			scsi_reset_verbose(sc,
					   "Timeout waiting for phase-change");
		PID("info_transf2");
		return (0);
	}

	phase = (tmp >> 2) & 7;
	if (phase != reqp->phase) {
		reqp->phase = phase;
		DBG_INFPRINT(show_phase, reqp, phase);
	}
	else {
		/*
		 * Same data-phase. If same error give up
		 */
		if ((reqp->msgout == MSG_ABORT)
		     && ((phase == PH_DATAOUT) || (phase == PH_DATAIN))) {
			busy     &= ~(1 << reqp->targ_id);
			connected = NULL;
			finish_req(reqp);
			scsi_reset_verbose(sc, "Failure to abort command");
			return (0);
		}
	}

	switch (phase) {
	    case PH_DATAOUT:
#ifdef DBG_NOWRITE
		ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
		reqp->msgout = MSG_ABORT;
		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
		return (-1);
#endif /* DBG_NOWRITE */
		/*
		 * If this is the first write using DMA, fill
		 * the bounce buffer.
		 */
		if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
		    if (reqp->dr_flag & DRIVER_BOUNCING)
			bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
		}

	   case PH_DATAIN:
		if (reqp->xdata_len <= 0) {
			/*
			 * Target keeps requesting data. Try to get into
			 * message-out phase by feeding/taking 100 byte.
			 */
			ncr_tprint(reqp, "Target requests too much data\n");
			reqp->msgout = MSG_ABORT;
			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
			reach_msg_out(sc, 100);
			return (-1);
		}
#ifdef REAL_DMA
		if (reqp->dr_flag & DRIVER_DMAOK) {
			int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
			transfer_dma(reqp, phase, poll);
			if (!poll)
				return (0);
		}
		else
#endif
		{
			PID("info_transf3");
			len = reqp->xdata_len;
#ifdef USE_PDMA
			if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
				return (0);
#else
			transfer_pio(&phase, reqp->xdata_ptr, &len, 0);
#endif
			reqp->xdata_ptr += reqp->xdata_len - len;
			reqp->xdata_len  = len;
		}
		return (-1);
	   case PH_MSGIN:
		/*
		 * We only expect single byte messages here.
		 */
		len = 1;
		transfer_pio(&phase, &tmp, &len, 1);
		reqp->message = tmp;
		return (handle_message(reqp, tmp));
	   case PH_MSGOUT:
		len = 1;
		transfer_pio(&phase, &reqp->msgout, &len, 0);
		if (reqp->msgout == MSG_ABORT) {
			busy     &= ~(1 << reqp->targ_id);
			connected = NULL;
			if (!reqp->xs->error)
				reqp->xs->error = XS_DRIVER_STUFFUP;
			finish_req(reqp);
			PID("info_transf4");
			return (0);
		}
		reqp->msgout = MSG_NOOP;
		return (-1);
	   case PH_CMD :
		len = command_size(reqp->xcmd.opcode);
		transfer_pio(&phase, (u_char *)&reqp->xcmd, &len, 0);
		PID("info_transf5");
		return (-1);
	   case PH_STATUS:
		len = 1;
		transfer_pio(&phase, &tmp, &len, 0);
		reqp->status = tmp;
		PID("info_transf6");
		return (-1);
	   default :
		ncr_tprint(reqp, "Unknown phase\n");
	}
	PID("info_transf7");
	return (-1);
}

/*
 * Handle the message 'msg' send to us by the target.
 * Return values:
 *	 0 : The current command has completed.
 *	-1 : Get on to the next phase.
 */
static int
handle_message(reqp, msg)
SC_REQ	*reqp;
u_int	msg;
{
	int	sps;
	SC_REQ	*prev, *req;

	PID("hmessage1");
	switch (msg) {
		/*
		 * Linking lets us reduce the time required to get
		 * the next command to the device, skipping the arbitration
		 * and selection time. In the current implementation,
		 * we merely have to start the next command pointed
		 * to by 'next_link'.
		 */
		case MSG_LINK_CMD_COMPLETE:
		case MSG_LINK_CMD_COMPLETEF:
			if (reqp->link == NULL) {
				ncr_tprint(reqp, "No link for linked command");
				nack_message(reqp, MSG_ABORT);
				PID("hmessage2");
				return (-1);
			}
			ack_message();
			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
				reqp->xs->resid = reqp->xdata_len;
				reqp->xs->error = 0;
			}

#ifdef AUTO_SENSE
			if (check_autosense(reqp, 1) == -1)
				return (-1);
#endif /* AUTO_SENSE */

#ifdef DBG_REQ
			if (dbg_target_mask & (1 << reqp->targ_id))
				show_request(reqp->link, "LINK");
#endif
			connected = reqp->link;

			/*
			 * Unlink the 'linked' request from the issue_q
			 */
			sps  = splbio();
			prev = NULL;
			req  = issue_q;
			for (; req != NULL; prev = req, req = req->next) {
				if (req == connected)
					break;
			}
			if (req == NULL)
				panic("Inconsistent issue_q");
			if (prev == NULL)
				issue_q = req->next;
			else prev->next = req->next;
			req->next = NULL;
			splx(sps);

			finish_req(reqp);
			PID("hmessage3");
			return (-1);
		case MSG_ABORT:
		case MSG_CMDCOMPLETE:
			ack_message();
			connected = NULL;	
			busy     &= ~(1 << reqp->targ_id);
			if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
				reqp->xs->resid = reqp->xdata_len;
				reqp->xs->error = 0;
			}

#ifdef AUTO_SENSE
			if (check_autosense(reqp, 0) == -1) {
				PID("hmessage4");
				return (0);
			}
#endif /* AUTO_SENSE */

			finish_req(reqp);
			PID("hmessage5");
			return (0);
		case MSG_MESSAGE_REJECT:
			ack_message();
			PID("hmessage6");
			return (-1);
		case MSG_DISCONNECT:
			ack_message();
#ifdef DBG_REQ
			if (dbg_target_mask & (1 << reqp->targ_id))
				show_request(reqp, "DISCON");
#endif
			sps = splbio();
			connected  = NULL;
			reqp->next = discon_q;
			discon_q   = reqp;
			splx(sps);
			PID("hmessage7");
			return (0);
		case MSG_SAVEDATAPOINTER:
		case MSG_RESTOREPOINTERS:
			/*
			 * We save pointers implicitely at disconnect.
			 * So we can ignore these messages.
			 */
			ack_message();
			PID("hmessage8");
			return (-1);
		case MSG_EXTENDED:
			nack_message(reqp, MSG_MESSAGE_REJECT);
			PID("hmessage9");
			return (-1);
		default: 
			if ((msg & 0x80) && !(msg & 0x18)) {	/* IDENTIFY */
				PID("hmessage10");
				ack_message();
				return (0);
			} else {
				ncr_tprint(reqp,
					   "Unknown message %x.  Rejecting.\n",
					   msg);
				nack_message(reqp, MSG_MESSAGE_REJECT);
			}
			return (-1);
	}
	PID("hmessage11");
	return (-1);
}

/*
 * Handle reselection. If a valid reconnection occurs, connected
 * points at the reconnected command. The command is removed from the
 * disconnected queue.
 */
static void
reselect(sc)
struct ncr_softc *sc;
{
	u_char	phase;
	u_long	len;
	u_char	msg;
	u_char	target_mask;
	int	abort = 0;
	SC_REQ	*tmp, *prev;

	PID("reselect1");
	target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;

	/*
	 * At this point, we have detected that our SCSI-id is on the bus,
	 * SEL is true and BSY was false for at least one bus settle
	 * delay (400 ns.).
	 * We must assert BSY ourselves, until the target drops the SEL signal.
	 * The SCSI-spec specifies no maximum time for this, so we have to
	 * choose something long enough to suit all targets.
	 */
	SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
	len = 250000;
	while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
		delay(1);
		len--;
	}
	if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
		/* Damn SEL isn't dropping */
		scsi_reset_verbose(sc, "Target won't drop SEL during Reselect");
		return;
	}

	SET_5380_REG(NCR5380_ICOM, 0);
	
	/*
	 * Check if the reselection is still valid. Check twice because
	 * of possible line glitches - cheaper than delay(1) and we need
	 * only a few nanoseconds.
	 */
	if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
	    if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
		ncr_aprint(sc, "Stepped into the reselection timeout\n");
		return;
	    }
	}

	/*
	 * Get the expected identify message.
	 */
	phase = PH_MSGIN;
	len   = 1;
	transfer_pio(&phase, &msg, &len, 0);
	if (len || !MSG_ISIDENTIFY(msg)) {
		ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
		abort = 1;
		tmp = NULL;
	}
	else {
	    /*
	     * Find the command reconnecting
	     */
	    for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
		if (target_mask == (1 << tmp->targ_id)) {
			if (prev)
				prev->next = tmp->next;
			else discon_q = tmp->next;
			tmp->next = NULL;
			break;
		}
	    }
	    if (tmp == NULL) {
		ncr_aprint(sc, "No disconnected job for targetmask %x\n",
								target_mask);
		abort = 1;
	    }
	}
	if (abort) {
		msg   = MSG_ABORT;
		len   = 1;
		phase = PH_MSGOUT;

		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
		if (transfer_pio(&phase, &msg, &len, 0) || len)
			scsi_reset_verbose(sc, "Failure to abort reselection");
	}
	else {
		connected = tmp;
#ifdef DBG_REQ
		if (dbg_target_mask & (1 << tmp->targ_id))
			show_request(tmp, "RECON");
#endif
	}
	PID("reselect2");
}

/*
 * Transfer data in a given phase using programmed I/O.
 * Returns -1 when a different phase is entered without transferring the
 * maximum number of bytes, 0 if all bytes transferred or exit is in the same
 * phase.
 */
static int
transfer_pio(phase, data, len, dont_drop_ack)
u_char	*phase;
u_char	*data;
u_long	*len;
int	dont_drop_ack;
{
	u_int	cnt = *len;
	u_char	ph  = *phase;
	u_char	tmp, new_icom;

	DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
	PID("tpio1");
	SET_5380_REG(NCR5380_TCOM, ph);
	do {
	    if (!wait_req_true()) {
		DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
		break;
	    }
	    if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
		DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
		break;
	    }
	    if (PH_IN(ph)) {
		*data++ = GET_5380_REG(NCR5380_DATA);
		SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
		if ((cnt == 1) && dont_drop_ack)
			new_icom = SC_A_ACK;
		else new_icom = 0;
	    }
	    else {
		SET_5380_REG(NCR5380_DATA, *data++);

		/*
		 * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
		 * the initiator should drop ATN on the last byte of the
		 * message phase after REQ has been asserted for the handshake
		 * but before the initiator raises ACK.
		 */
		if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
			SET_5380_REG(NCR5380_ICOM, SC_ADTB);
			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
			new_icom = 0;
		}
		else {
			SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
			new_icom = SC_A_ATN;
		}
	    }
	    if (!wait_req_false()) {
		DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
		break;
	    }
	    SET_5380_REG(NCR5380_ICOM, new_icom);

	} while (--cnt);

	if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
		*phase = (tmp >> 2) & 7;
	else *phase = NR_PHASE;
	*len = cnt;
	DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
								*phase, cnt);
	PID("tpio2");
	if (!cnt || (*phase == ph))
		return (0);
	return (-1);
}

#ifdef REAL_DMA
/*
 * Start a DMA-transfer on the device using the current pointers.
 * If 'poll' is true, the function busy-waits until DMA has completed.
 */
static void
transfer_dma(reqp, phase, poll)
SC_REQ	*reqp;
u_int	phase;
int	poll;
{
	int	dma_done;
	u_char	mbase = 0;
	int	sps;

again:
	PID("tdma1");

	/*
	 * We should be in phase, otherwise we are not allowed to
	 * drive the bus.
	 */
	SET_5380_REG(NCR5380_TCOM, phase);

	/*
	 * Defer interrupts until DMA is fully running.
	 */
	sps = splbio();

	/*
	 * Clear pending interrupts and parity errors.
	 */
	scsi_clr_ipend();

	if (!poll) {
		/*
		 * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
		 * to the interrupts we want enabled.
		 */
		scsi_ienable();
		reqp->dr_flag |= DRIVER_IN_DMA;
		mbase = SC_E_EOPI | SC_MON_BSY;
	}
	else scsi_idisable();
	mbase |=  IMODE_BASE | SC_M_DMA;
	scsi_dma_setup(reqp, phase, mbase);

	splx(sps);

	if (poll) {
		/*
		 * On polled-dma transfers, we wait here until the
		 * 'end-of-dma' condition occurs.
		 */
		poll_edma(reqp);
		if (!(dma_done = dma_ready()))
			goto again;
	}
	PID("tdma2");
}

/*
 * Check results of a DMA data-transfer.
 */
static int
dma_ready()
{
	SC_REQ	*reqp = connected;
	int	dmstat, is_edma;
	long	bytes_left, bytes_done;

	is_edma = get_dma_result(reqp, &bytes_left);
	dmstat  = GET_5380_REG(NCR5380_DMSTAT);

	/*
	 * Check if the call is sensible and not caused by any spurious
	 * interrupt.
	 */
	if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
		     && (dmstat & SC_PHS_MTCH) ) {
		ncr_tprint(reqp, "dma_ready: spurious call "
				 "(dm:%x,last_hit: %s)\n",
#ifdef DBG_PID
					dmstat, last_hit[DBG_PID-1]);
#else
					dmstat, "unknown");
#endif
		return (0);
	}

	/*
	 * Clear all (pending) interrupts.
	 */
	scsi_clr_ipend();

	/*
	 * Update various transfer-pointers/lengths
	 */
	bytes_done = reqp->dm_cur->dm_count - bytes_left;

	if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
		/*
		 * Copy the bytes read until now from the bounce buffer
		 * to the 'real' destination. Flush the data-cache
		 * before copying.
		 */
		PCIA();
		bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
		reqp->bouncerp += bytes_done;
	}

	reqp->xdata_ptr  = &reqp->xdata_ptr[bytes_done];	/* XXX */
	reqp->xdata_len -= bytes_done;				/* XXX */
	if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
		reqp->dm_cur++;
	else reqp->dm_cur->dm_addr += bytes_done;

	if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
		if (!(ncr5380_no_parchk & (1 << reqp->targ_id))) {
			ncr_tprint(reqp, "parity error in data-phase\n");
			reqp->xs->error = XS_TIMEOUT;
		}
	}

	/*
	 * DMA mode should always be reset even when we will continue with the
	 * next chain. It is also essential to clear the MON_BUSY because
	 * when LOST_BUSY is unexpectedly set, we will not be able to drive
	 * the bus....
	 */
	SET_5380_REG(NCR5380_MODE, IMODE_BASE);


	if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
		 || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {

		/*
		 * Tell interrupt functions DMA mode has ended.
		 */
		reqp->dr_flag &= ~DRIVER_IN_DMA;

		/*
		 * Clear mode and icom
		 */
		SET_5380_REG(NCR5380_MODE, IMODE_BASE);
		SET_5380_REG(NCR5380_ICOM, 0);

		if (dmstat & SC_BSY_ERR) {
			if (!reqp->xs->error)
				reqp->xs->error = XS_TIMEOUT;
			finish_req(reqp);
			PID("dma_ready1");
			return (1);
		}

		if (reqp->xs->error != 0) {
ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
			reqp->msgout = MSG_ABORT;
			SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
		}
		PID("dma_ready2");
		return (1);
	}
	return (0);
}
#endif /* REAL_DMA */

static int
check_autosense(reqp, linked)
SC_REQ	*reqp;
int	linked;
{
	int	sps;

	/*
	 * If we not executing an auto-sense and the status code
	 * is request-sense, we automatically issue a request
	 * sense command.
	 */
	PID("cautos1");
	if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
		switch (reqp->status & SCSMASK) {
		    case SCSCHKC:
			bcopy(sense_cmd, &reqp->xcmd, sizeof(sense_cmd));
			reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
			reqp->xdata_len = sizeof(reqp->xs->sense);
			reqp->dr_flag  |= DRIVER_AUTOSEN;
			reqp->dr_flag  &= ~DRIVER_DMAOK;
			if (!linked) {
				sps = splbio();
				reqp->next = issue_q;
				issue_q    = reqp;
				splx(sps);
			}
			else reqp->xcmd.bytes[sizeof(sense_cmd)-2] |= 1;

#ifdef DBG_REQ
			bzero(reqp->xdata_ptr, reqp->xdata_len);
			if (dbg_target_mask & (1 << reqp->targ_id))
				show_request(reqp, "AUTO-SENSE");
#endif
			PID("cautos2");
			return (-1);
		    case SCSBUSY:
			reqp->xs->error = XS_BUSY;
			return (0);
		}
	}
	else {
		/*
		 * An auto-sense has finished
		 */
		if ((reqp->status & SCSMASK) != SCSGOOD)
			reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
		else reqp->xs->error = XS_SENSE;
		reqp->status = SCSCHKC;
	}
	PID("cautos3");
	return (0);
}

static int
reach_msg_out(sc, len)
struct ncr_softc *sc;
u_long		 len;
{
	u_char	phase;
	u_char	data;
	u_long	n = len;

	ncr_aprint(sc, "Trying to reach Message-out phase\n");
	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
		phase = (phase >> 2) & 7;
	else return (-1);
	ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
	if (phase == PH_MSGOUT)
		return (0);

	SET_5380_REG(NCR5380_TCOM, phase);

	do {
		if (!wait_req_true())
			break;
		if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
			break;
		if (PH_IN(phase)) {
			data = GET_5380_REG(NCR5380_DATA);
			SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
		}
		else {
			SET_5380_REG(NCR5380_DATA, 0);
			SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
		}
		if (!wait_req_false())
			break;
		SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
	} while (--n);

	if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
		phase = (phase >> 2) & 7;
		if (phase == PH_MSGOUT) {
			ncr_aprint(sc, "Message-out phase reached after "
					"%ld bytes.\n", len - n);
			return (0);
		}
	}
	return (-1);
}

void
scsi_reset()
{
	SC_REQ	*tmp, *next;
	int	sps;

	PID("scsi_reset1");
	sps = splbio();
	SET_5380_REG(NCR5380_ICOM, SC_A_RST);
	delay(100);
	SET_5380_REG(NCR5380_ICOM, 0);
	scsi_clr_ipend();

	/*
	 * None of the jobs in the discon_q will ever be reconnected,
	 * notify this to the higher level code.
	 */
	for (tmp = discon_q; tmp ;) {
		next = tmp->next;
		tmp->next = NULL;
		tmp->xs->error = XS_TIMEOUT;
		busy &= ~(1 << tmp->targ_id);
		finish_req(tmp);
		tmp = next;
	}
	discon_q = NULL;

	/*
	 * The current job will never finish either.
	 * The problem is that we can't finish the job because an instance
	 * of main is running on it. Our best guess is that the job is currently
	 * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
	 * the job because it detects BSY-loss.
	 */
	if ((tmp = connected) != NULL) {
		if (tmp->dr_flag & DRIVER_IN_DMA) {
			tmp->xs->error = XS_DRIVER_STUFFUP;
#ifdef REAL_DMA
			dma_ready();
#endif
		}
	}
	splx(sps);
	PID("scsi_reset2");

	/*
	 * Give the attached devices some time to handle the reset. This
	 * value is arbitrary but should be relatively long.
	 */
	delay(100000);
}

static void
scsi_reset_verbose(sc, why)
struct ncr_softc *sc;
const char	 *why;
{
	ncr_aprint(sc, "Resetting SCSI-bus (%s)\n", why);

	scsi_reset();
}

/*
 * Check validity of the IRQ set by the 5380. If the interrupt is valid,
 * the appropriate action is carried out (reselection or DMA ready) and
 * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
 * and INTR_SPURIOUS is returned.
 */
static int
check_intr(sc)
struct ncr_softc *sc;
{
	SC_REQ	*reqp;

	if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
							==(SC_S_SEL|SC_S_IO))
		return (INTR_RESEL);
	else {
		if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
			reqp->dr_flag &= ~DRIVER_IN_DMA;
			return (INTR_DMA);
		}
	}
	scsi_clr_ipend();
	printf("-->");
	scsi_show();
	ncr_aprint(sc, "Spurious interrupt.\n");
	return (INTR_SPURIOUS);
}

#ifdef REAL_DMA
/*
 * Check if DMA can be used for this request. This function also builds
 * the dma-chain.
 */
static int
scsi_dmaok(reqp)
SC_REQ	*reqp;
{
	u_long			phy_buf;
	u_long			phy_len;
	void			*req_addr;
	u_long			req_len;
	struct dma_chain	*dm;

	/*
	 * Initialize locals and requests' DMA-chain.
	 */
	req_len        = reqp->xdata_len;
	req_addr       = (void*)reqp->xdata_ptr;
	dm             = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
	dm->dm_count   = dm->dm_addr = 0;
	reqp->dr_flag &= ~DRIVER_BOUNCING;

	/*
	 * Do not accept zero length DMA.
	 */
	if (req_len == 0)
		return (0);

	/*
	 * LWP: I think that this restriction is not strictly nessecary.
	 */
	if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
		return (0);

	/*
	 * Build the DMA-chain.
	 */
	dm->dm_addr = phy_buf = kvtop(req_addr);
	while (req_len) {
		if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
			phy_len = req_len;

		req_addr     += phy_len;
		req_len      -= phy_len;
		dm->dm_count += phy_len;

		if (req_len) {
			u_long	tmp = kvtop(req_addr);

			if ((phy_buf + phy_len) != tmp) {
			    if (wrong_dma_range(reqp, dm)) {
				if (reqp->dr_flag & DRIVER_BOUNCING)
					goto bounceit;
				return (0);
			    }

			    if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
				ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
				return (0);
			    }
			    dm->dm_count = 0;
			    dm->dm_addr  = tmp;
			}
			phy_buf = tmp;
		}
	}
        if (wrong_dma_range(reqp, dm)) {
		if (reqp->dr_flag & DRIVER_BOUNCING)
			goto bounceit;
		return (0);
	}
	reqp->dm_last = dm;
	return (1);

bounceit:
	if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
		/*
		 * If we can't get a bounce buffer, forget DMA
		 */
		reqp->dr_flag &= ~DRIVER_BOUNCING;
		return(0);
	}
	/*
	 * Initialize a single DMA-range containing the bounced request
	 */
	dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
	dm->dm_addr    = kvtop(reqp->bounceb);
	dm->dm_count   = reqp->xdata_len;
	reqp->bouncerp = reqp->bounceb;

	return (1);
}
#endif /* REAL_DMA */

static void
run_main(sc)
struct ncr_softc *sc;
{
	int	sps = splbio();

	if (!main_running) {
		/*
		 * If shared resources are required, claim them
		 * before entering 'scsi_main'. If we can't get them
		 * now, assume 'run_main' will be called when the resource
		 * becomes available.
		 */
		if (!claimed_dma()) {
			splx(sps);
			return;
		}
		main_running = 1;
		splx(sps);
		scsi_main(sc);
	}
	else splx(sps);
}

/*
 * Prefix message with full target info.
 */
static void
ncr_tprint(SC_REQ *reqp, char *fmt, ...)
{
	va_list	ap;

	va_start(ap, fmt);
	sc_print_addr(reqp->xs->sc_link);
	printf("%:", fmt, ap);
	va_end(ap);
}

/*
 * Prefix message with adapter info.
 */
static void
ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
{
	va_list	ap;

	va_start(ap, fmt);
	printf("%s : %:", sc->sc_dev.dv_xname, fmt, ap);
	va_end(ap);
}
/****************************************************************************
 *		Start Debugging Functions				    *
 ****************************************************************************/
static void
show_data_sense(xs)
struct scsi_xfer	*xs;
{
	u_char	*p1, *p2;
	int	i;
	int	sz;

	p1 = (u_char *) xs->cmd;
	p2 = (u_char *)&xs->sense;
	if(*p2 == 0)
		return;	/* No(n)sense */
	printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
	for (i = 0; i < sz; i++)
		printf("%x ", p1[i]);
	printf("\nsense: ");
	for (i = 0; i < sizeof(xs->sense); i++)
		printf("%x ", p2[i]);
	printf("\n");
}

static void
show_request(reqp, qtxt)
SC_REQ	*reqp;
char	*qtxt;
{
	printf("REQ-%s: %d %p[%ld] cmd[0]=%x S=%x M=%x R=%x resid=%d dr_flag=%x %s\n",
			qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
			reqp->xcmd.opcode, reqp->status, reqp->message,
			reqp->xs->error, reqp->xs->resid, reqp->dr_flag,
			reqp->link ? "L":"");
	if (reqp->status == SCSCHKC)
		show_data_sense(reqp->xs);
}

static char *sig_names[] = {
	"PAR", "SEL", "I/O", "C/D", "MSG", "REQ", "BSY", "RST",
	"ACK", "ATN", "LBSY", "PMATCH", "IRQ", "EPAR", "DREQ", "EDMA"
};

static void
show_signals(dmstat, idstat)
u_char	dmstat, idstat;
{
	u_short	tmp, mask;
	int	j, need_pipe;

	tmp = idstat | ((dmstat & 3) << 8);
	printf("Bus signals (%02x/%02x): ", idstat, dmstat & 3);
	for (mask = 1, j = need_pipe = 0; mask <= tmp; mask <<= 1, j++) {
		if (tmp & mask)
			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
	}
	printf("\nDma status (%02x): ", dmstat);
	for (mask = 4, j = 10, need_pipe = 0; mask <= dmstat; mask <<= 1, j++) {
		if (dmstat & mask)
			printf("%s%s", need_pipe++ ? "|" : "", sig_names[j]);
	}
	printf("\n");
}

void
scsi_show()
{
	SC_REQ	*tmp;
	int	sps = splhigh();
	u_char	idstat, dmstat;
#ifdef	DBG_PID
	int	i;
#endif

	printf("scsi_show: scsi_main is%s running\n",
		main_running ? "" : " not");
	for (tmp = issue_q; tmp; tmp = tmp->next)
		show_request(tmp, "ISSUED");
	for (tmp = discon_q; tmp; tmp = tmp->next)
		show_request(tmp, "DISCONNECTED");
	if (connected)
		show_request(connected, "CONNECTED");
	idstat = GET_5380_REG(NCR5380_IDSTAT);
	dmstat = GET_5380_REG(NCR5380_DMSTAT);
	show_signals(dmstat, idstat);
	if (connected)
		printf("phase = %d, ", connected->phase);
	printf("busy:%x, spl:%04x\n", busy, sps);
#ifdef	DBG_PID
	for (i=0; i<DBG_PID; i++)
		printf("\t%d\t%s\n", i, last_hit[i]);
#endif

	splx(sps);
}