summaryrefslogtreecommitdiff
path: root/sys/arch/sparc/dev/esp.c
blob: 658e2c5f3f3816db56a1de34ab39ad9defaf0185 (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
/*
 * FOR NOW, YOU MAY NOT USE THIS DRIVER WITHOUT EXPLICITLY GRANTED
 * PERMISSION.
 */


/*	$NetBSD: esp.c,v 1.18 1995/02/01 12:37:24 pk Exp $ */

/*
 * Copyright (c) 1995 Theo de Raadt
 * 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 Theo de Raadt.
 * 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.
 */

/*
 * Based IN PART on aic6360 by Jarle Greipsland, an older esp driver
 * by Peter Galbavy, and work by Charles Hannum on a few other drivers.
 */

/*
 * todo:
 * fix & enable sync
 * confirm parity and bus failures do not lock up driver
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/queue.h>

#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <scsi/scsi_message.h>

#include <machine/cpu.h>
#include <machine/autoconf.h>
#include <sparc/dev/sbusvar.h>
#include <sparc/dev/dmavar.h>
#include <sparc/dev/dmareg.h>
#include <sparc/dev/espreg.h>
#include <sparc/dev/espvar.h>

int esp_debug = ESP_SHOWPHASE|ESP_SHOWMISC|ESP_SHOWTRAC|ESP_SHOWCMDS; /**/ 

#if 1
#define ESPD(x)	
#else
#define ESPD(x)	printf x
#endif

void	espattach	__P((struct device *, struct device *, void *));
int	espmatch	__P((struct device *, void *, void *));
int	espprint	__P((void *, char *));
void	espreadregs	__P((struct esp_softc *));
int	espgetbyte	__P((struct esp_softc *, int));
void	espselect	__P((struct esp_softc *));
void	esp_reset	__P((struct esp_softc *));
void	esp_init	__P((struct esp_softc *, int));
int	esp_scsi_cmd	__P((struct scsi_xfer *));
int	esp_poll	__P((struct esp_softc *, struct ecb *));
int	espphase	__P((struct esp_softc *));
void	esp_sched	__P((struct esp_softc *));
void	esp_done	__P((struct ecb *));
void	esp_msgin	__P((struct esp_softc *));
void	esp_makemsg	__P((struct esp_softc *));
void	esp_msgout	__P((struct esp_softc *));
int	espintr		__P((struct esp_softc *));
void	esp_timeout	__P((void *arg));

struct cfdriver espcd = {
	NULL, "esp", espmatch, espattach, DV_DULL, sizeof(struct esp_softc)
};

struct scsi_adapter esp_switch = {
	esp_scsi_cmd, minphys, NULL, NULL
};

struct scsi_device esp_dev = {
	NULL, NULL, NULL, NULL
};

/*
 * Does anyone actually use this, and what for ?
 */
int
espprint(aux, name)
	void *aux;
	char *name;
{
	return (UNCONF);
}


int
espmatch(parent, vcf, aux)
	struct device *parent;
	void *vcf, *aux;
{
	struct cfdata *cf = vcf;
	register struct confargs *ca = aux;
	register struct romaux *ra = &ca->ca_ra;

	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
		return (0);
	if (ca->ca_bustype == BUS_SBUS)
		return (1);
	ra->ra_len = NBPG;
	return (probeget(ra->ra_vaddr, 1) != -1);
}


/*
 * Attach this instance, and then all the sub-devices
 */
void
espattach(parent, self, aux)
	struct device *parent, *self;
	void *aux;
{
	register struct confargs *ca = aux;
	struct esp_softc *sc = (void *)self;
	struct espregs *espr;
	struct bootpath *bp;
	int freq;

	/*
	 * Make sure things are sane. I don't know if this is ever
	 * necessary, but it seem to be in all of Torek's code.
	 */
	if (ca->ca_ra.ra_nintr != 1) {
		printf(": expected 1 interrupt, got %d\n", ca->ca_ra.ra_nintr);
		return;
	}

	sc->sc_pri = ca->ca_ra.ra_intr[0].int_pri;
	printf(" pri %d", sc->sc_pri);

	/*
	 * Map my registers in, if they aren't already in virtual
	 * address space.
	 */
	if (ca->ca_ra.ra_vaddr)
		sc->sc_regs = (struct espregs *) ca->ca_ra.ra_vaddr;
	else {
		sc->sc_regs = (struct espregs *)
		    mapiodev(ca->ca_ra.ra_paddr, ca->ca_ra.ra_len,
		    ca->ca_bustype);
	}
	espr = sc->sc_regs;

	/* Other settings */
	sc->sc_node = ca->ca_ra.ra_node;
	if (ca->ca_bustype == BUS_SBUS) {
		sc->sc_id = getpropint(sc->sc_node, "initiator-id", 7);
		sc->sc_freq = getpropint(sc->sc_node, "clock-frequency", -1);
	} else {
		sc->sc_id = 7;
		sc->sc_freq = 24000000;
	}
	if (sc->sc_freq < 0)
		sc->sc_freq = ((struct sbus_softc *)
		    sc->sc_dev.dv_parent)->sc_clockfreq;

	freq = sc->sc_freq / 1000000;		/* gimme Mhz */

	/*
	 * This is the value used to start sync negotiations. For a
	 * 25Mhz clock, this gives us 40, or 160nS, or 6.25Mb/s. It
	 * is constant for each adapter. In turn, notice that the ESP
	 * register "SYNCTP" is = (250 / the negotiated period).
	 */
	sc->sc_minsync = 1000 / freq;

	/* 0 is actually 8, even though the register only has 3 bits */
	sc->sc_ccf = FREQTOCCF(freq) & 0x07;

	/* The value must not be 1 -- make it 2 */
	if (sc->sc_ccf == 1)
		sc->sc_ccf = 2;

	/*
	 * The recommended timeout is 250ms (1/4 seconds). This
	 * register is loaded with a value calculated as follows:
	 *
	 *		(timout period) x (CLK frequency)
	 *	reg = -------------------------------------
	 *		 8192 x (Clock Conversion Factor)
	 *
	 * We have the CCF from above. For a 25MHz clock this gives
	 * a constant of 153 (we round up).
	 */
	sc->sc_timeout = (sc->sc_freq / 4 / 8192 / sc->sc_ccf) + 1;

	/*
	 * find the corresponding DMA controller.
	 */
	sc->sc_dma = ((struct dma_softc *)getdevunit("dma",
	    sc->sc_dev.dv_unit));
	if (sc->sc_dma)
		sc->sc_dma->sc_esp = sc;

	sc->sc_cfg1 = sc->sc_id | ESPCFG1_PARENB;
	sc->sc_cfg2 = 0;
	sc->sc_cfg3 = 0;

	/*
	 * The ESP100 only has a cfg1 register. The ESP100A has it, but
	 * lacks the cfg3 register. Thus, we can tell which chip we have.
	 * XXX: what about the FAS100A?
	 */
	espr->espr_cfg2 = ESPCFG2_SCSI2 | ESPCFG2_RPE;
	if ((espr->espr_cfg2 & ~ESPCFG2_RSVD) != (ESPCFG2_SCSI2 | ESPCFG2_RPE)) {
		printf(": ESP100");
		sc->sc_rev = ESP100;
	} else {
		espr->espr_cfg2 = 0;
		espr->espr_cfg3 = 0;
		espr->espr_cfg3 = ESPCFG3_CDB | ESPCFG3_FCLK;
		if (espr->espr_cfg3 != (ESPCFG3_CDB | ESPCFG3_FCLK)) {
			printf(": ESP100A");
			/* XXX sc->sc_cfg2 = ESPCFG2_SCSI2 | ESPCFG2_FE; */
			sc->sc_rev = ESP100A;
		} else {
			espr->espr_cfg3 = 0;
			printf(": ESP200");
			sc->sc_rev = ESP200;
		}
	}

	sc->sc_state = 0;
	esp_init(sc, 1);

	printf(" %dMhz, target %d\n", freq, sc->sc_id);

#if defined(SUN4C) || defined(SUN4M)
	if (ca->ca_bustype == BUS_SBUS) {
		/* add to the sbus structures */
		sc->sc_sd.sd_reset = (void *) esp_reset;
		sbus_establish(&sc->sc_sd, &sc->sc_dev);

		/*
		 * If the device is in an SBUS slave slot, bail now.
		 */
		if (sbus_slavecheck(self, ca))
			return;
	}
#endif /* SUN4C || SUN4M */

	/* and the interupts */
	sc->sc_ih.ih_fun = (void *) espintr;
	sc->sc_ih.ih_arg = sc;
	intr_establish(sc->sc_pri, &sc->sc_ih);
	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);

	/*
	 * fill in the prototype scsi_link.
	 */
	sc->sc_link.adapter_softc = sc;
	sc->sc_link.adapter_target = sc->sc_id;
	sc->sc_link.adapter = &esp_switch;
	sc->sc_link.device = &esp_dev;
	sc->sc_link.openings = 2;

	/*
	 * If the boot path is "esp", and the numbers point to
	 * this controller, then advance the bootpath a step.
	 * XXX boot /sbus/esp/sd@3,0 passes in esp@0,0 -- which
	 * is almost always wrong, but match in that case anyways.
	 */
	bp = ca->ca_ra.ra_bp;
	switch (ca->ca_bustype) {
	case BUS_SBUS:
		if (bp != NULL && strcmp(bp->name, "esp") == 0 &&
		    ((bp->val[0]==ca->ca_slot &&
		     (bp->val[1]==ca->ca_offset || bp->val[1]==0)) ||
		    (bp->val[0]==-1 && bp->val[1]==sc->sc_dev.dv_unit)))
			bootpath_store(1, bp + 1);
		break;
	default:
		if (bp != NULL && strcmp(bp->name, "esp") == 0 &&
		    bp->val[0] == -1 && bp->val[1] == sc->sc_dev.dv_unit)
			bootpath_store(1, bp + 1);
		break;
	}

	/*
	 * Now try to attach all the sub-devices
	 */
	config_found(self, &sc->sc_link, espprint);

	bootpath_store(1, NULL);
}


/*
 * Warning: This inserts two commands into the ESP command fifo.  Because
 * there are only 2 entries in that fifo, you may need to put a DELAY(1)
 * after calling this, if you are going to issue another command soon.
 */
void
espflush(sc)
	struct esp_softc *sc;
{
	struct espregs *espr = sc->sc_regs;

	if (espr->espr_fflag & ESPFIFO_FF) {
		espr->espr_cmd = ESPCMD_FLUSH;
		espr->espr_cmd = ESPCMD_NOP;
	}
}

/*
 * Read the ESP registers, and save their contents for later use.
 * ESP_STAT, ESP_STEP & ESP_INTR are mostly zeroed out when reading
 * ESP_INTR - so make sure it is the last read.
 *
 * XXX: TDR: this logic seems unsound
 * I think that (from reading the docs) most bits in these registers
 * only make sense when the DMA CSR has an interrupt showing. So I have
 * coded this to not do anything if there is no interrupt or error
 * pending.
 */
void
espreadregs(sc)
	struct esp_softc *sc;
{
	struct espregs *espr = sc->sc_regs;

	/* they mean nothing if the is no pending interrupt ??? */
	if (!(dmapending(sc->sc_dma)))
		return;

	/* Only the stepo bits are of interest */
	sc->sc_espstep = espr->espr_step & ESPSTEP_MASK;
	sc->sc_espstat = espr->espr_stat;
	sc->sc_espintr = espr->espr_intr;

	ESP_MISC(("regs[intr=%02x,stat=%02x,step=%02x] ", sc->sc_espintr,
	    sc->sc_espstat, sc->sc_espstep));
}

/*
 * returns -1 if no byte is available because fifo is empty.
 */
int
espgetbyte(sc, dotrans)
	struct esp_softc *sc;
	int dotrans;
{
	struct espregs *espr = sc->sc_regs;

	if (espr->espr_fflag & ESPFIFO_FF)
		return espr->espr_fifo;
	return -1;
}

/*
 * Send a command to a target, as well as any required messages.
 * There are three cases. The first two cases are fairly simple..
 * 1) command alone
 * 	load command into fifo, and use ESPCMD_SELNATN
 * 2) MSG_IDENTIFY + command
 *	load message and command into fifo, and use ESPCMD_SELATN
 * 3) a bunch of messages + command
 *	load first message byte into fifo. Use ESPCMD_SELATNS. When the
 *	next interrupt occurs, load the remainer of the message into
 *	the fifo and use ESPCMD_TRANS. When the device is ready to
 *	receive the command, it will switch into COMMAND_PHASE, and
 *	at that point we will feed it the command.
 */
void
espselect(sc)
	struct esp_softc *sc;
{
	struct espregs *espr = sc->sc_regs;
	register struct ecb *ecb = sc->sc_nexus;
	register struct scsi_link *sc_link = ecb->xs->sc_link;
	struct esp_tinfo *ti = &sc->sc_tinfo[sc_link->target];
	u_char *cmd = (u_char *)&ecb->cmd;
	int loadcmd = 1;
	int outcmd, i;

	espr->espr_selid = sc_link->target;
	espr->espr_syncoff = ti->offset;
	espr->espr_synctp = ti->synctp;

	sc->sc_state = ESPS_SELECTING;

	if (ecb->xs->flags & SCSI_RESET)
		sc->sc_msgpriq = SEND_DEV_RESET;
	else if (ti->flags & DO_NEGOTIATE)
		sc->sc_msgpriq = SEND_SDTR;
	else
		sc->sc_msgpriq = SEND_IDENTIFY;

	if (sc->sc_msgpriq) {
		esp_makemsg(sc);

		ESPD(("OM["));
		for (i = 0; i < sc->sc_omlen; i++)
			ESPD(("%02x%c", sc->sc_omp[i],
			    (i == sc->sc_omlen-1) ? ']' : ' '));
		ESPD((" "));

		espr->espr_fifo = sc->sc_omp[0];	/* 1st msg byte only */
		if (sc->sc_omlen == 1) {
			outcmd = ESPCMD_SELATN;
		} else {
			outcmd = ESPCMD_SELATNS;
			/* and this will will load the rest of the msg bytes */
			sc->sc_state = ESPS_SELECTSTOP;
			loadcmd = 0;
		}
	} else
		outcmd = ESPCMD_SELNATN;

	ESPD(("P%d/%02x/%d ", (ecb->xs->flags & SCSI_POLL) ? 1 : 0, outcmd, loadcmd));

	if (loadcmd) {
		ESPD(("CMD["));
		for (i = 0; i < ecb->clen; i++)
			ESPD(("%02x%c", cmd[i],
			    (i == ecb->clen-1) ? ']' : ' '));
		ESPD((" "));

		/* load the command into the FIFO */
		for (i = 0; i < ecb->clen; i++)
			espr->espr_fifo = cmd[i];
	}

	espr->espr_cmd = outcmd;
	if (!(ecb->xs->flags & SCSI_POLL))
		sc->sc_flags |= ESPF_MAYDISCON;
}


/*
 * Reset the ESP and DMA chips. Stops any transactions dead in the water;
 * does not cause an interrupt.
 */
void 
esp_reset(sc)
	struct esp_softc *sc;
{
	struct espregs *espr = sc->sc_regs;

	dmareset(sc->sc_dma);		/* reset DMA first */

	espr->espr_cmd = ESPCMD_RSTCHIP;
	DELAY(5);
	espr->espr_cmd = ESPCMD_NOP;
	DELAY(5);

	/* do these backwards, and fall through */
	switch (sc->sc_rev) {
	case ESP200:
		espr->espr_cfg3 = sc->sc_cfg3;
	case ESP100A:
		espr->espr_cfg2 = sc->sc_cfg2;
	case ESP100:
		espr->espr_cfg1 = sc->sc_cfg1;
		espr->espr_ccf = sc->sc_ccf;
		espr->espr_syncoff = 0;
		espr->espr_timeout = sc->sc_timeout;
		break;
	}
}

/*
 * Initialize esp state machine
 */
void
esp_init(sc, doreset)
	struct esp_softc *sc;
	int doreset;
{
	struct espregs *espr = sc->sc_regs;
	struct ecb *ecb;
	int i;
	
	/*
	 * reset the chip to a known state
	 */
	esp_reset(sc);

	if (doreset) {
		espr->espr_cmd = ESPCMD_RSTSCSI;
		DELAY(500);
		/* cheat: we don't want the state machine to reset again.. */
		esp_reset(sc);
	}

	if (sc->sc_state == 0) {	/* First time through */
		TAILQ_INIT(&sc->ready_list);
		TAILQ_INIT(&sc->nexus_list);
		TAILQ_INIT(&sc->free_list);
		sc->sc_nexus = 0;
		ecb = sc->sc_ecb;
		bzero(ecb, sizeof(sc->sc_ecb));
		for (i = 0; i < sizeof(sc->sc_ecb) / sizeof(*ecb); i++) {
			TAILQ_INSERT_TAIL(&sc->free_list, ecb, chain);
			ecb++;
		}
		bzero(sc->sc_tinfo, sizeof(sc->sc_tinfo));
	} else {
		sc->sc_state = ESPS_IDLE;
		if (sc->sc_nexus != NULL) {
			sc->sc_nexus->xs->error = XS_DRIVER_STUFFUP;
			untimeout(esp_timeout, sc->sc_nexus);
			esp_done(sc->sc_nexus);
		}
		sc->sc_nexus = NULL;
		while (ecb = sc->nexus_list.tqh_first) {
			ecb->xs->error = XS_DRIVER_STUFFUP;
			untimeout(esp_timeout, ecb);
			esp_done(ecb);
		}
	}
	
	for (i = 0; i < 8; i++) {
		struct esp_tinfo *ti = &sc->sc_tinfo[i];

		ti->flags = DO_NEGOTIATE;
		ti->period = sc->sc_minsync;
		ti->synctp = 250 / ti->period;
		ti->offset = ESP_SYNC_REQOFF;
	}
	sc->sc_state = ESPS_IDLE;
}

/*
 * Start a SCSI-command: This function is called by the higher level
 * SCSI-driver to queue/run SCSI-commands.
 */
int 
esp_scsi_cmd(xs)
	struct scsi_xfer *xs;
{
	struct scsi_link *sc_link = xs->sc_link;
	struct esp_softc *sc = sc_link->adapter_softc;
	struct ecb *ecb;
	int	s;
	
	/*ESPD(("NS%08x/%08x/%d ", xs, xs->data, xs->datalen));*/

	/* XXX: set lun */
	xs->cmd->bytes[0] |= (sc_link->lun << SCSI_CMD_LUN_SHIFT);

	/* Get a esp command block */
	s = splbio();
	ecb = sc->free_list.tqh_first;
	if (ecb) {
		TAILQ_REMOVE(&sc->free_list, ecb, chain);
	}
	splx(s);
		
	if (ecb == NULL) {
		xs->error = XS_DRIVER_STUFFUP;
		return TRY_AGAIN_LATER;
	}

	/* Initialize ecb */
	ecb->flags = ECB_ACTIVE;
	ecb->xs = xs;
	bcopy(xs->cmd, &ecb->cmd, xs->cmdlen);
	ecb->clen = xs->cmdlen;
	ecb->daddr = xs->data;
	ecb->dleft = xs->datalen;
	ecb->stat = 0;
	
	s = splbio();
	TAILQ_INSERT_TAIL(&sc->ready_list, ecb, chain);
	if ((xs->flags & SCSI_POLL) == 0)
		timeout(esp_timeout, ecb, (xs->timeout * hz) / 1000);

	if (sc->sc_state == ESPS_IDLE)
		esp_sched(sc);
	splx(s);

	/* Not allowed to use interrupts, use polling instead */
	if (xs->flags & SCSI_POLL)
		return esp_poll(sc, ecb);
	return SUCCESSFULLY_QUEUED;
}

/*
 * Used when interrupt driven I/O isn't allowed, e.g. during boot.
 */
int
esp_poll(sc, ecb)
	struct esp_softc *sc;
	struct ecb *ecb;
{
	struct scsi_xfer *xs = ecb->xs;
	int	count = xs->timeout * 1000;

	ESP_TRACE(("esp_poll\n"));
	while (count) {
		if (dmapending(sc->sc_dma)) {
			/*
			 * We decrement the interrupt event counter to
			 * repair it... because this isn't a real interrupt.
			 */
			if (espintr(sc))
				--sc->sc_intrcnt.ev_count;
			continue;
		}
		if (xs->flags & ITSDONE)
			break;
		DELAY(1);
		count--;
	}

	if (count == 0) {
		ecb->xs->error = XS_TIMEOUT;
		sc_print_addr(ecb->xs->sc_link);
		printf("timed out\n");
		esp_reset(sc);
	}
	dmaenintr(sc->sc_dma);
	return COMPLETE;
}


/*
 * Notice that we do not read the live register on an ESP100. On the
 * ESP100A and above the FE (Feature Enable) bit in config 2 latches
 * the phase in the register so it is safe to read.
 */
int
espphase(sc)
	struct esp_softc *sc;
{

	if (sc->sc_rev > ESP100)
		return (sc->sc_regs->espr_stat & ESPSTAT_PHASE);
	return (sc->sc_espstat & ESPSTAT_PHASE);
}


/*
 * Schedule a scsi operation.  This has now been pulled out of the interrupt
 * handler so that we may call it from esp_scsi_cmd and esp_done.  This may
 * save us an unecessary interrupt just to get things going.  Should only be
 * called when state == ESPS_IDLE and at bio pl.
 */
void
esp_sched(sc)
	struct esp_softc *sc;
{
	struct scsi_link *sc_link;
	struct ecb *ecb;
	int t;
	
	ESP_TRACE(("esp_sched\n"));

	/*
	 * Find first ecb in ready queue that is for a target/lunit
	 * combinations that is not busy.
	 */
	for (ecb = sc->ready_list.tqh_first; ecb; ecb = ecb->chain.tqe_next) {
		caddr_t cmd = (caddr_t) &ecb->cmd;
		sc_link = ecb->xs->sc_link;
		t = sc_link->target;
		if (!(sc->sc_tinfo[t].lubusy & (1 << sc_link->lun))) {
			struct esp_tinfo *ti = &sc->sc_tinfo[ecb->xs->sc_link->target];

			TAILQ_REMOVE(&sc->ready_list, ecb, chain);
			sc->sc_nexus = ecb;
			sc->sc_flags = 0;
			sc_link = ecb->xs->sc_link;
			espselect(sc);
			ti = &sc->sc_tinfo[sc_link->target];
			sc->sc_dp = ecb->daddr;
			sc->sc_dleft = ecb->dleft;
			ti->lubusy |= (1<<sc_link->lun);
			break;
		} else
			ESP_MISC(("%d:%d busy\n", t, sc_link->lun));
	}
}

/*
 * POST PROCESSING OF SCSI_CMD (usually current)
 */
void
esp_done(ecb)
	struct ecb *ecb;
{
	struct scsi_xfer *xs = ecb->xs;
	struct scsi_link *sc_link = xs->sc_link;
	struct esp_softc *sc = sc_link->adapter_softc;

	ESP_TRACE(("esp_done "));

	/*
	 * Now, if we've come here with no error code, i.e. we've kept the 
	 * initial XS_NOERROR, and the status code signals that we should
	 * check sense, we'll need to set up a request sense cmd block and 
	 * push the command back into the ready queue *before* any other 
	 * commands for this target/lunit, else we lose the sense info.
	 * We don't support chk sense conditions for the request sense cmd.
	 */
	if (xs->error == XS_NOERROR) {
		if (ecb->flags & ECB_CHKSENSE)
			xs->error = XS_SENSE;
		else if ((ecb->stat & ST_MASK) == SCSI_CHECK) {
			struct scsi_sense *ss = (void *)&ecb->cmd;

			ESPD(("RS "));
			ESP_MISC(("requesting sense "));
			/* First, save the return values */
			xs->resid = ecb->dleft;
			xs->status = ecb->stat;
			/* Next, setup a request sense command block */
			bzero(ss, sizeof(*ss));
			ss->opcode = REQUEST_SENSE;
			ss->byte2 = sc_link->lun << SCSI_CMD_LUN_SHIFT;
			ss->length = sizeof(struct scsi_sense_data);
			ecb->clen = sizeof(*ss);
			ecb->daddr = (char *)&xs->sense;
			ecb->dleft = sizeof(struct scsi_sense_data);
			ecb->flags = ECB_ACTIVE|ECB_CHKSENSE;
			TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
			sc->sc_tinfo[sc_link->target].lubusy &=
			    ~(1<<sc_link->lun);
			sc->sc_tinfo[sc_link->target].senses++;
			/* found it */
			if (sc->sc_nexus != ecb)
				TAILQ_INSERT_HEAD(&sc->ready_list, ecb, chain);
			sc->sc_state = ESPS_IDLE;
			esp_sched(sc);
			return;
		} else
			xs->resid = ecb->dleft;
	}

	xs->flags |= ITSDONE;

#if ESP_DEBUG > 1
	if (esp_debug & ESP_SHOWMISC) {
		printf("err=0x%02x ", xs->error);
		if (xs->error == XS_SENSE)
			printf("sense=%02x\n", xs->sense.error_code);
	}
	if ((xs->resid || xs->error > XS_SENSE) && (esp_debug & ESP_SHOWMISC)) {
		if (xs->resid)
			printf("esp_done: resid=%d\n", xs->resid);
		if (xs->error)
			printf("esp_done: error=%d\n", xs->error);
	}
#endif

	/*
	 * Remove the ECB from whatever queue it's on.  We have to do a bit of
	 * a hack to figure out which queue it's on.  Note that it is *not*
	 * necessary to cdr down the ready queue, but we must cdr down the
	 * nexus queue and see if it's there, so we can mark the unit as no
	 * longer busy.  This code is sickening, but it works.
	 */
	if (ecb == sc->sc_nexus) {
		sc->sc_tinfo[sc_link->target].lubusy &= ~(1<<sc_link->lun);
		sc->sc_nexus = NULL;
		sc->sc_state = ESPS_IDLE;
		esp_sched(sc);
	} else if (sc->ready_list.tqh_last == &ecb->chain.tqe_next) {
		TAILQ_REMOVE(&sc->ready_list, ecb, chain);
	} else {
		register struct ecb *ecb2;

		for (ecb2 = sc->nexus_list.tqh_first; ecb2;
		    ecb2 = ecb2->chain.tqe_next)
			if (ecb2 == ecb)
				break;
		if (ecb2) {
			TAILQ_REMOVE(&sc->nexus_list, ecb, chain);
			sc->sc_tinfo[sc_link->target].lubusy &=
			    ~(1<<sc_link->lun);
		} else if (ecb->chain.tqe_next) {
			TAILQ_REMOVE(&sc->ready_list, ecb, chain);
		} else {
			printf("%s: can't find matching ecb\n",
			    sc->sc_dev.dv_xname);
			Debugger();
		}
	}
	/* Put it on the free list. */
	ecb->flags = ECB_FREE;
	TAILQ_INSERT_HEAD(&sc->free_list, ecb, chain);

	sc->sc_tinfo[sc_link->target].cmds++;
	ESPD(("DONE%d ", xs->resid));
	scsi_done(xs);
}

/*
 * INTERRUPT/PROTOCOL ENGINE
 */

/*
 * Schedule an outgoing message by prioritizing it, and asserting
 * attention on the bus. We can only do this when we are the initiator
 * else there will be an illegal command interrupt.
 */
#define esp_sched_msgout(m) \
	do { \
		sc->sc_regs->espr_cmd = ESPCMD_SETATN; \
		sc->sc_msgpriq |= (m); \
		ESPD(("Mq%02x ", sc->sc_msgpriq)); \
	} while (0)

#define IS1BYTEMSG(m) (((m) != 1 && ((unsigned)(m)) < 0x20) || (m) & 0x80)
#define IS2BYTEMSG(m) (((m) & 0xf0) == 0x20)
#define ISEXTMSG(m) ((m) == 1)

/*
 * Get an incoming message as initiator.
 *
 * The SCSI bus must already be in MESSAGE_IN_PHASE and there is a
 * byte in the FIFO
 */
void
esp_msgin(sc)
	register struct esp_softc *sc;
{
	struct espregs *espr = sc->sc_regs;
	struct ecb *ecb;
	int extlen;
	int x, i;
	
	ESP_TRACE(("esp_msgin "));

	/* is something wrong ? */
	if (sc->sc_phase != MESSAGE_IN_PHASE) {
		printf("%s: not MESSAGE_IN_PHASE\n", sc->sc_dev.dv_xname);
		return;
	}

	ESPD(("MSGIN:%d ", espr->espr_fflag & ESPFIFO_FF));

#ifdef fixme
	/*
	 * Prepare for a new message.  A message should (according
	 * to the SCSI standard) be transmitted in one single
	 * MESSAGE_IN_PHASE. If we have been in some other phase,
	 * then this is a new message.
	 */
	if (sc->sc_prevphase != MESSAGE_IN_PHASE) {
		sc->sc_flags &= ~ESPF_DROP_MSGI;
		if (sc->sc_imlen > 0) {
			printf("%s: message type %02x",
			    sc->sc_dev.dv_xname, sc->sc_imess[0]);
			if (!IS1BYTEMSG(sc->sc_imess[0]))
				printf(" %02x", sc->sc_imess[1]);
			printf(" was dropped\n");
		}
		sc->sc_imlen = 0;
	}
#endif

	/*
	 * Which target is reselecting us? (The ID bit really)
	 * On a reselection, there should be the reselecting target's
	 * id and an identify message in the fifo.
	 */
	if (sc->sc_state == ESPS_RESELECTED && sc->sc_imlen == 0) {
		x = espgetbyte(sc, 0);
		if (x == -1) {
			printf("%s: msgin reselection found fifo empty\n",
			    sc->sc_dev.dv_xname);
			return;
		}
		ESPD(("ID=%02x ", x));
		sc->sc_selid = (u_char)x & ~(1<<sc->sc_id);
		ESP_MISC(("selid=0x%02x ", sc->sc_selid));
	}

	/*
	 * If parity errors just dump everything on the floor
	 */
	if (sc->sc_espstat & ESPSTAT_PE) {
		printf("%s: SCSI bus parity error\n",
		    sc->sc_dev.dv_xname);
		esp_sched_msgout(SEND_PARITY_ERROR);
		DELAY(1);
		sc->sc_flags |= ESPF_DROP_MSGI;
	}

	/*
	 * If we're going to reject the message, don't bother storing
	 * the incoming bytes.  But still, we need to ACK them.
	 * XXX: the above comment might be true -- this is not being
	 * done though!
	 */
	if ((sc->sc_flags & ESPF_DROP_MSGI) == 0) {
		x = espgetbyte(sc, 1);
		if (x == -1) {
			printf("%s: msgin fifo empty at %d\n",
			    sc->sc_dev.dv_xname, sc->sc_imlen);
			if (sc->sc_espintr & ESPINTR_BS) {
				espr->espr_cmd = ESPCMD_TRANS;
				ESPD(("MSI:y "));
			} else
				ESPD(("MSI:n "));
			return;
		}
		ESPD(("{%02x} ", (u_char)x));
		sc->sc_imess[sc->sc_imlen] = (u_char)x;
		if (sc->sc_imlen >= ESP_MSGLEN_MAX) {
			esp_sched_msgout(SEND_REJECT);
			DELAY(1);
			sc->sc_flags |= ESPF_DROP_MSGI;
		} else {
			sc->sc_imlen++;
			/* 
			 * This testing is suboptimal, but most
			 * messages will be of the one byte variety, so
			 * it should not effect performance
			 * significantly.
			 */
			if (sc->sc_imlen == 1 && IS1BYTEMSG(sc->sc_imess[0]))
				goto gotit;
			if (sc->sc_imlen == 2 && IS2BYTEMSG(sc->sc_imess[0]))
				goto gotit;
			if (sc->sc_imlen >= 3 && ISEXTMSG(sc->sc_imess[0]) &&
			    sc->sc_imlen == sc->sc_imess[1] + 2)
				goto gotit;
		}
	}
	goto done;

gotit:
	ESPD(("MP["));
	for (i=0; i<sc->sc_imlen; i++)
		ESPD(("%02x%c", sc->sc_imess[i],
		    (i==sc->sc_imlen-1) ? ']' : ' '));
	ESPD((" "));

	sc->sc_imlen = 0;	/* message is fully received */

	ESP_MISC(("gotmsg "));
	/*
	 * Now we should have a complete message (1 byte, 2 byte
	 * and moderately long extended messages).  We only handle
	 * extended messages which total length is shorter than
	 * ESP_MSGLEN_MAX.  Longer messages will be amputated.
	 */
	if (sc->sc_state == ESPS_NEXUS ||
	    sc->sc_state == ESPS_DOINGMSGIN ||
	    sc->sc_state == ESPS_DOINGSTATUS) {
		struct esp_tinfo *ti;

		ecb = sc->sc_nexus;
		ti = &sc->sc_tinfo[ecb->xs->sc_link->target];

		sc->sc_state = ESPS_NEXUS;	/* where we go after this */

		switch (sc->sc_imess[0]) {
		case MSG_CMDCOMPLETE:
			ESP_MISC(("cmdcomplete "));
			if (!ecb) {
				esp_sched_msgout(SEND_ABORT);
				DELAY(1);
				printf("%s: CMDCOMPLETE but no command?\n",
				    sc->sc_dev.dv_xname);
				break;
			}
			if (sc->sc_dleft < 0) {
				sc_print_addr(ecb->xs->sc_link);
				printf("%d extra bytes\n", -sc->sc_dleft);
				ecb->dleft = 0;
			}
			ecb->xs->resid = ecb->dleft = sc->sc_dleft;
			ecb->flags |= ECB_DONE;
			break;
		case MSG_MESSAGE_REJECT:
			if (esp_debug & ESP_SHOWMISC)
				printf("%s targ %d: our msg rejected by target\n",
				    sc->sc_dev.dv_xname,
				    ecb->xs->sc_link->target);
			if (sc->sc_flags & ESPF_SYNCHNEGO) {
				/*
				 * Device doesn't even know what sync is
				 * (right?)
				 */
				ti->offset = 0;
				ti->period = sc->sc_minsync;
				ti->synctp = 250 / ti->period;
				sc->sc_flags &= ~ESPF_SYNCHNEGO;
				ti->flags &= ~DO_NEGOTIATE;
			}
			/* Not all targets understand INITIATOR_DETECTED_ERR */
			if (sc->sc_msgout == SEND_INIT_DET_ERR) {
				esp_sched_msgout(SEND_ABORT);
				DELAY(1);
			}
			break;
		case MSG_NOOP:
			break;
		case MSG_DISCONNECT:
			if (!ecb) {
				esp_sched_msgout(SEND_ABORT);
				DELAY(1);
				printf("%s: nothing to DISCONNECT\n",
				    sc->sc_dev.dv_xname);
				break;
			}
			ti->dconns++;
			TAILQ_INSERT_HEAD(&sc->nexus_list, ecb, chain);
			ecb = sc->sc_nexus = NULL;
			sc->sc_state = ESPS_EXPECTDISC;
			sc->sc_flags |= ESPF_MAYDISCON;
			break;
		case MSG_SAVEDATAPOINTER:
			if (!ecb) {
				esp_sched_msgout(SEND_ABORT);
				DELAY(1);
				printf("%s: no DATAPOINTERs to save\n",
				    sc->sc_dev.dv_xname);
				break;
			}
			ESPD(("DL%d/%d ", sc->sc_dleft, ecb->dleft));
			ecb->dleft = sc->sc_dleft;
			ecb->daddr = sc->sc_dp;
			break;
		case MSG_RESTOREPOINTERS:
			if (!ecb) {
				esp_sched_msgout(SEND_ABORT);
				DELAY(1);
				printf("%s: no DATAPOINTERs to restore\n",
				    sc->sc_dev.dv_xname);
				break;
			}
			sc->sc_dp = ecb->daddr;
			sc->sc_dleft = ecb->dleft;
			break;
		case MSG_EXTENDED:
			switch (sc->sc_imess[2]) {
			case MSG_EXT_SDTR:
				ti->period = sc->sc_imess[3];
				ti->offset = sc->sc_imess[4];
				sc->sc_flags &= ~ESPF_SYNCHNEGO;
				ti->flags &= ~DO_NEGOTIATE;
				if (ti->offset == 0 ||
				    ti->offset > ESP_SYNC_MAXOFF ||
				    ti->period < sc->sc_minsync) {
					ti->offset = 0;
					ti->period = sc->sc_minsync;
					ti->synctp = 250 / ti->period;
					break;
				}
				printf("%s targ %d: sync, offset %d, period %dnsec\n",
				    sc->sc_dev.dv_xname, ecb->xs->sc_link->target,
				    ti->offset, ti->period * 4);
				ti->synctp = 250 / ti->period;
				break;
			default:
				/* Extended messages we don't handle */
				esp_sched_msgout(SEND_REJECT);
				DELAY(1);
				break;
			}
			break;
		default:
			/* thanks for that ident... XXX: we should check it? */
			if (!MSG_ISIDENTIFY(sc->sc_imess[0])) {
				esp_sched_msgout(SEND_REJECT);
				DELAY(1);
			}
			break;
		}
	} else if (sc->sc_state == ESPS_RESELECTED) {
		struct scsi_link *sc_link;
		u_char lunit;

		if (MSG_ISIDENTIFY(sc->sc_imess[0])) { 	/* Identify? */
			ESP_MISC(("searching "));
			/*
			 * Search wait queue for disconnected cmd
			 * The list should be short, so I haven't bothered with
			 * any more sophisticated structures than a simple
			 * singly linked list. 
			 */
			lunit = sc->sc_imess[0] & 0x07;
			for (ecb = sc->nexus_list.tqh_first; ecb;
			    ecb = ecb->chain.tqe_next) {
				sc_link = ecb->xs->sc_link;
				if (sc_link->lun == lunit &&
				    sc->sc_selid == (1<<sc_link->target)) {
					TAILQ_REMOVE(&sc->nexus_list, ecb,
					    chain);
					break;
				}
			}

			if (!ecb) {
				/* Invalid reselection! */
				esp_sched_msgout(SEND_ABORT);
				DELAY(1);
				printf("esp: invalid reselect (idbit=0x%2x)\n",
				    sc->sc_selid);
			} else {
				/*
				 * Reestablish nexus:
				 * Setup driver data structures and
				 * do an implicit RESTORE POINTERS
				 */
				sc->sc_nexus = ecb;
				sc->sc_dp = ecb->daddr;
				sc->sc_dleft = ecb->dleft;
				sc->sc_tinfo[sc_link->target].lubusy |=
				    (1<<sc_link->lun);
				espr->espr_syncoff =
				    sc->sc_tinfo[sc_link->target].offset;
				espr->espr_synctp = 250 /
				    sc->sc_tinfo[sc_link->target].period;
				ESP_MISC(("... found ecb"));
				sc->sc_state = ESPS_NEXUS;
			}
		} else {
			printf("%s: bogus reselect (no IDENTIFY) %0x2x\n",
			    sc->sc_dev.dv_xname, sc->sc_selid);
			esp_sched_msgout(SEND_DEV_RESET);
			DELAY(1);
		}
	} else { /* Neither ESP_HASNEXUS nor ESP_RESELECTED! */
		printf("%s: unexpected message in; will send DEV_RESET\n",
		    sc->sc_dev.dv_xname);
		esp_sched_msgout(SEND_DEV_RESET);
		DELAY(1);
	}
done:
	espr->espr_cmd = ESPCMD_MSGOK;
	espr->espr_cmd = ESPCMD_NOP;
}


void
esp_makemsg(sc)
	register struct esp_softc *sc;
{
	struct esp_tinfo *ti;
	struct ecb *ecb = sc->sc_nexus;
	int i;

	sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
	ESPD(("MQ%02x/%02x ", sc->sc_msgpriq, sc->sc_msgout));

	sc->sc_omlen = 1;		/* "Default" message len */
	switch (sc->sc_msgout) {
	case SEND_SDTR:		/* implies an IDENTIFY message */
		sc->sc_flags |= ESPF_SYNCHNEGO;
		ti = &sc->sc_tinfo[ecb->xs->sc_link->target];
		sc->sc_omess[0] = MSG_IDENTIFY(ecb->xs->sc_link->lun,
		    !(ecb->xs->flags & SCSI_POLL));
		sc->sc_omess[1] = MSG_EXTENDED;
		sc->sc_omess[2] = 3;
		sc->sc_omess[3] = MSG_EXT_SDTR;
		sc->sc_omess[4] = ti->period;
		sc->sc_omess[5] = ti->offset;
		sc->sc_omlen = 6;
		/* Fallthrough! */
	case SEND_IDENTIFY:
		sc->sc_omess[0] = MSG_IDENTIFY(ecb->xs->sc_link->lun,
		    !(ecb->xs->flags & SCSI_POLL));
		break;
	case SEND_DEV_RESET:
		sc->sc_omess[0] = MSG_BUS_DEV_RESET;
		if (sc->sc_nexus)
			sc->sc_nexus->flags |= ECB_DONE;
		break;
	case SEND_PARITY_ERROR:
		sc->sc_omess[0] = MSG_PARITY_ERROR;
		break;
	case SEND_ABORT:
		sc->sc_omess[0] = MSG_ABORT;
		if (sc->sc_nexus)
			sc->sc_nexus->flags |= ECB_DONE;
		break;
	case SEND_INIT_DET_ERR:
		sc->sc_omess[0] = MSG_INITIATOR_DET_ERR;
		break;
	case SEND_REJECT:
		sc->sc_omess[0] = MSG_MESSAGE_REJECT;
		break;
	default:
		sc->sc_omess[0] = MSG_NOOP;
		break;
	}
	sc->sc_omp = sc->sc_omess;
}


/*
 * Send the highest priority, scheduled message
 */
void
esp_msgout(sc)
	register struct esp_softc *sc;
{
	struct espregs *espr = sc->sc_regs;
	struct esp_tinfo *ti;
	struct ecb *ecb;
	int i;

	/* Pick up highest priority message */
	sc->sc_msgout = sc->sc_msgpriq & -sc->sc_msgpriq;
	esp_makemsg(sc);

	for (i = 0; i < sc->sc_omlen; i++)
		espr->espr_fifo = sc->sc_omp[i];
	espr->espr_cmd = ESPCMD_TRANS;
}


void
esp_timeout(arg)
	void *arg;
{
	struct ecb *ecb = (struct ecb *)arg;
	struct esp_softc *sc;

	sc = ecb->xs->sc_link->adapter_softc;
	sc_print_addr(ecb->xs->sc_link);
	printf("timed out at %d %d\n", sc->sc_state, sc->sc_phase);

	ecb->xs->error = XS_TIMEOUT;
	esp_reset(sc);
	esp_done(ecb);
	printf("new ecb %08x\n", sc->sc_nexus);
}

/*
 * Whatever we do, we must generate an interrupt if we expect to go
 * to the next state.
 * Note: this increments the events even if called from esp_poll()
 */
int 
espintr(sc)
	register struct esp_softc *sc;
{
	struct espregs *espr = sc->sc_regs;
	register struct ecb *ecb = sc->sc_nexus;
	register struct scsi_link *sc_link;
	u_char *cmd;
	struct esp_tinfo *ti;
	int dmaintrwas, i;

	/*
	 * Revision 1 DMA's must have their interrupts disabled,
	 * otherwise we can get bus timeouts while reading ESP
	 * registers.
	 */
	if (sc->sc_dma->sc_rev == DMAREV_1)
		dmaintrwas = dmadisintr(sc->sc_dma);

	espreadregs(sc);

	/*
	 * If either of these two things is true, we caused an interrupt.
	 * If we didn't, let's get out of here.
	 */
	if ((sc->sc_espintr | dmapending(sc->sc_dma)) == 0) {
		if (sc->sc_dma->sc_rev == DMAREV_1 && dmaintrwas)
			dmaenintr(sc->sc_dma);
		return (0);
	}

	sc->sc_phase = espphase(sc);

	ESPD(("I%02x/%02x/%02x ", sc->sc_espintr, sc->sc_state, sc->sc_phase));

	if (sc->sc_espintr & ESPINTR_SBR) {
		espflush(sc);
		printf("%s: scsi bus reset\n", sc->sc_dev.dv_xname);
		esp_init(sc, 0);	/* Restart everything */
		/*
		 * No interrupt expected, since there are now no
		 * ecb's in progress.
		 */
		goto done;
	}

	if (sc->sc_espintr & ESPINTR_ILL) {
		printf("%s: illegal command %02x\n",
		    sc->sc_dev.dv_xname, espr->espr_cmd);
		if (sc->sc_state == ESPS_NEXUS) {
			ecb->xs->error = XS_DRIVER_STUFFUP;
			untimeout(esp_timeout, ecb);
			esp_done(ecb);
		}
		esp_reset(sc);
		esp_sched(sc);		/* start next command */
		goto done;
	}

	if (sc->sc_espstat & ESPSTAT_GE) {
		/* no target ? */
		espflush(sc);
		printf("%s: gross error\n", sc->sc_dev.dv_xname);
		if (sc->sc_state == ESPS_NEXUS) {
			ecb->xs->error = XS_DRIVER_STUFFUP;
			untimeout(esp_timeout, ecb);
			esp_done(ecb);
		}
		esp_reset(sc);
		esp_sched(sc);
		/* started next command, which will interrupt us */
		goto done;
	}

	/*
	 * The `bad interrupts' have already been checked.
	 */
states:
	switch (sc->sc_state) {
	case ESPS_IDLE:
		ESPD(("I "));

		sc->sc_nexus = NULL;

		if (sc->sc_espintr & ESPINTR_RESEL) {
			sc->sc_state = ESPS_RESELECTED;
			goto states;
		}
		esp_sched(sc);

		/*
		 * We will get an interrupt if esp_sched() queues
		 * anything to the scsi bus.
		 */
		goto done;
	case ESPS_SELECTING:
		/*
		 * For a simple select, we sent either
		 * ESPCMD_SELATN or ESPCMD_SELNATN
		 */
		ESPD(("S "));

		if (sc->sc_espintr & ESPINTR_DIS) {
			sc->sc_state = ESPS_NEXUS;	/* will cleanup */
			goto states;
		}

		if (sc->sc_espintr & ESPINTR_RESEL) {
			/*
			 * If we were trying to select a target,
			 * push our command back into the ready list.
			 */
			if (sc->sc_state == ESPS_SELECTING) {
				TAILQ_INSERT_HEAD(&sc->ready_list,
				    sc->sc_nexus, chain);
				ecb = sc->sc_nexus = NULL;
			}
			sc->sc_state = ESPS_RESELECTED;
			goto states;
		}

		if (sc->sc_espintr == (ESPINTR_FC|ESPINTR_BS)) {
			ESPD(("STEP%d ", sc->sc_espstep));
			if (sc->sc_espstep == 0) {
				/* ATN: cannot happen */
				/* NATN: target did not assert message phase */
				sc->sc_state = ESPS_IDLE;
				goto states;
			} else if (sc->sc_espstep == 2) {
				/* BOTH: target did not assert command phase */
				sc->sc_state = ESPS_IDLE;
				goto states;
			} else if (sc->sc_espstep == 3) {
				/* BOTH: changed phase during cmd transfer */
				ESPD(("FF%d ", espr->espr_fflag & ESPFIFO_FF));
				if (espr->espr_fflag & ESPFIFO_FF) {
					sc->sc_state = ESPS_IDLE;
					goto states;
				}
			}

			espr->espr_cmd = ESPCMD_NOP;	/* unlatch fifo counter */
			if (sc->sc_phase != DATA_IN_PHASE &&
			    sc->sc_tinfo[ecb->xs->sc_link->target].offset == 0) {
				DELAY(1);
				espflush(sc);
			}

			ecb = sc->sc_nexus;
			if (!ecb)
				panic("esp: not nexus at sc->sc_nexus");
			sc_link = ecb->xs->sc_link;
			ti = &sc->sc_tinfo[sc_link->target];
			sc->sc_flags = 0;

			/* Clearly we succeeded at sending the message */
			sc->sc_msgpriq &= ~sc->sc_msgout;
			sc->sc_msgout = 0;
			if (sc->sc_msgpriq)
				espr->espr_cmd = ESPCMD_SETATN;

			sc->sc_dp = ecb->daddr;	/* implicit RESTOREDATAPOINTERS */
			sc->sc_dleft = ecb->dleft;
			ti->lubusy |= (1<<sc_link->lun);

			sc->sc_state = ESPS_NEXUS;
		}
		break;		/* handle the phase */
	case ESPS_SELECTSTOP:
		/*
		 * We wanted to select with multiple message bytes.
		 * As a result, we needed to use SELATNS. One
		 * message byte has been sent at this point. We need
		 * to send the other bytes, then the target will either
		 * switch to command phase to fetch the command or
		 * send us a message.
		 */
		ESPD(("SS%d ", sc->sc_espstep));

		if (sc->sc_espintr & ESPINTR_DIS) {
			sc->sc_state = ESPS_NEXUS;
			goto states;
		}

		if (sc->sc_espintr & ESPINTR_RESEL) {
			/*
			 * If we were trying to select a target,
			 * push our command back into the ready list.
			 */
			if (sc->sc_state == ESPS_SELECTING) {
				TAILQ_INSERT_HEAD(&sc->ready_list,
				    sc->sc_nexus, chain);
				ecb = sc->sc_nexus = NULL;
			}
			sc->sc_state = ESPS_RESELECTED;
			goto states;
		}

		if (sc->sc_espintr & (ESPINTR_BS|ESPINTR_FC)) {
			int i;

			/*
			 * Shove the remainder of the message bytes
			 * into the fifo. After we send them, command
			 * phase will request the command...
			 */
			espflush(sc);
			for (i = 1; i < sc->sc_omlen; i++)
				espr->espr_fifo = sc->sc_omp[i];
			espr->espr_cmd = ESPCMD_TRANS;
			sc->sc_state = ESPS_MULTMSGEND;
			goto done;
		}
		goto done;
	case ESPS_MULTMSGEND:
		ESPD(("MME "));
		if (sc->sc_espintr & ESPINTR_DIS) {
			sc->sc_state = ESPS_NEXUS;
			goto states;
		}

		sc->sc_msgpriq &= ~sc->sc_msgout;
		sc->sc_msgout = 0;

		ESPD(("F%d ", espr->espr_fflag & ESPFIFO_FF));
		if (espr->espr_fflag & ESPFIFO_FF) {
			espflush(sc);
			DELAY(1);
		}

		/*
		 * If there are more messages, re-assert
		 * ATN so that we will enter MSGOUT phase
		 * again. Also, pause till that interrupt is
		 * done to see if the phase changes.
		 */
		if (sc->sc_msgpriq)
			espr->espr_cmd = ESPCMD_SETATN;
		sc->sc_state = ESPS_NEXUS;
		goto states;
	case ESPS_RESELECTED:
		ESPD(("RS "));

		if (sc->sc_phase != MESSAGE_IN_PHASE) {
			printf("%s: target didn't identify\n",
			    sc->sc_dev.dv_xname);
			esp_init(sc, 1);
			goto done;
		}
		esp_msgin(sc);
		if (sc->sc_state != ESPS_NEXUS) {
			/* IDENTIFY failed?! */
			printf("%s: identify failed\n", sc->sc_dev.dv_xname);
			esp_init(sc, 1);
			goto done;
		}

		/*
		 * We will get an interrupt because of the ESPCMD_MSGOK
		 * interrupt inside esp_msgin()
		 */
		goto done;
	case ESPS_DOINGDMA:
		/*
		 * Call if DMA is still active. If dmaintr() has finished,
		 * delay a while to let things settle, and then re-read the
		 * phase.
		 * 
		 * XXX: should check ESPINTR_DIS
		 */
		if (sc->sc_espstat & ESPSTAT_PE) {
			printf("%s: SCSI bus parity error\n",
			    sc->sc_dev.dv_xname);
			esp_sched_msgout(SEND_INIT_DET_ERR);
			/* XXX: anything else to do? */
		}

		if (sc->sc_espintr & ESPINTR_BS) {
			/*
			 * ESP says we have changed phase, or have
			 * finished transferring all the bytes. It
			 * doesn't matter which case, either way we
			 * will let the phase determine what happens
			 * next.
			 */
			ESPD(("DOINGDMA:BS:%02x/%02x ", sc->sc_espstat,
			    espr->espr_tcl));
			if (sc->sc_dma->sc_active) {
				dmaintr(sc->sc_dma, 0);
				if (sc->sc_dma->sc_active)
					goto done;
			}
		} else {
			ESPD(("DOINGDMA:cont "));
			if (sc->sc_dma->sc_active) {
				dmaintr(sc->sc_dma, 1);
				if (sc->sc_dma->sc_active)
					goto done;
			}
		}

		/* finished a DMA transaction */
		sc->sc_flags = 0;

		ecb->daddr = sc->sc_dp;		/* implicit SAVEDATAPOINTERS */
		ecb->dleft = sc->sc_dleft;

		/*
		 * ESP100 strangeness -- need to fetch a new phase --
		 * why?
		 */
		if (sc->sc_rev==ESP100 &&
		    sc->sc_tinfo[ecb->xs->sc_link->target].offset) {
			sc->sc_espstat = espr->espr_stat & ESPSTAT_PHASE;
			sc->sc_phase = espphase(sc);
			/* XXX should check if sync under/overran */
		}
		sc->sc_state = ESPS_NEXUS;
		break;		/* handle the phase */
	case ESPS_DOINGMSGOUT:
		ESPD(("DMO "));
		if (sc->sc_espintr & ESPINTR_DIS) {
			sc->sc_state = ESPS_NEXUS;	/* will cleanup */
			goto states;
		}

		if (sc->sc_espintr & ESPINTR_BS) {
			sc->sc_msgpriq &= ~sc->sc_msgout;
			sc->sc_msgout = 0;

			ESPD(("F%d ", espr->espr_fflag & ESPFIFO_FF));
			if (espr->espr_fflag & ESPFIFO_FF) {
				espflush(sc);
				DELAY(1);
			}

			/*
			 * If there are more messages, re-assert
			 * ATN so that we will enter MSGOUT phase
			 * again. Also, pause till that interrupt is
			 * done to see if the phase changes.
			 */
			if (sc->sc_msgpriq)
				espr->espr_cmd = ESPCMD_SETATN;
			break;
		}

#if 0
		if (sc->sc_espintr & ESPINTR_FC) {
			/*
			 * XXX: the book claims that an ESPINTR_BS is
			 * generated, not ESPINTR_FC. Check if that is
			 * true.
			 */
		}
#endif
		break;		/* handle the phase */
	case ESPS_DOINGMSGIN:
		/*
		 * We receive two interrupts per message byte here: one for
		 * the MSGOK command in esp_msgin(), and one for the TRANS
		 * command below.
		 */
		ESPD(("DMI "));

		if (sc->sc_espintr & ESPINTR_DIS) {
			sc->sc_state = ESPS_NEXUS;	/* will cleanup */
			goto states;
		}

		if (sc->sc_espstat & ESPSTAT_PE) {
			printf("%s: SCSI bus parity error\n",
			    sc->sc_dev.dv_xname);
			esp_sched_msgout(SEND_PARITY_ERROR);
			espflush(sc);
			sc->sc_state = ESPS_NEXUS;
			/* XXX: are we missing some fifo handling? */
			goto done;	/* will be interrupted for MSGOUT */
		}

		/*
		 * The two interrupts we receive per byte will alternate
		 * between entering the following two if() statements.
		 * XXX: just incase, are these two if()'s in the correct
		 * order?
		 */
		if (sc->sc_espintr & ESPINTR_BS) {
			/*
			 * interrupted by ESPCMD_MSGOK in esp_msgin()
			 */
			if (sc->sc_phase == MESSAGE_IN_PHASE)
				espr->espr_cmd = ESPCMD_TRANS;
			else {
				ESPD(("PC "));
				/*
				 * XXX: The phase has been changed on us. 
				 * This should lead us to discard the
				 * current (incomplete) message, perhaps
				 * assert an error(?), and go handle whatever
				 * new phase we are now in.
				 */
				break;
			}
			goto done;
		}
		if (sc->sc_espintr & ESPINTR_FC) {
			/*
			 * interrupted by ESPCMD_TRANS a few lines above.
			 */
			esp_msgin(sc);
			goto done;
		}
		goto done;
	case ESPS_DOINGSTATUS:
		if (sc->sc_espintr & ESPINTR_DIS) {
			sc->sc_state = ESPS_NEXUS;	/* will cleanup */
			goto states;
		}

		if (sc->sc_espintr & (ESPINTR_FC|ESPINTR_BS)) {
			i = espgetbyte(sc, 0);
			if (i == -1) {
				printf("%s: ESPS_DOINGSTATUS fifo empty\n",
				    sc->sc_dev.dv_xname);
				/* XXX: how do we cleanup from this error? */
				goto done;
			}
			if (sc->sc_espstat & ESPSTAT_PE) {
				printf("%s: SCSI bus parity error\n",
				    sc->sc_dev.dv_xname);
				/*
				 * Can't tell what the real status should
				 * be, so force a later check.
				 */
				i = SCSI_CHECK;
			}
			ecb->stat = (u_char)i;
			ESPD(("status=%02x ", (u_char)i));

			if (sc->sc_espintr & ESPINTR_FC) {
				/*
				 * XXX assumes a single byte message --
				 * and what about parity errors and other
				 * possible botches inside esp_msgin?
				 */
				esp_msgin(sc);
				sc->sc_state = ESPS_NEXUS;
			} else
				sc->sc_state = ESPS_DOINGMSGIN;
			goto done;
		}
		goto done;
	case ESPS_EXPECTDISC:
		/*
		 * We were told to expect a disconnection interrupt.
		 * When we get it, we can go back to other work.
		 */
		ESPD(("ED "));
		if (sc->sc_espintr & ESPINTR_DIS) {
			espflush(sc);
			DELAY(1);
			espr->espr_cmd = ESPCMD_ENSEL; /* within 250ms of disconnect */
			sc->sc_state = ESPS_IDLE;
		}
		goto states;
	case ESPS_NEXUS:
		ESPD(("NX "));

		if (sc->sc_espintr & ESPINTR_DIS) {
			espflush(sc);
			DELAY(1);
			espr->espr_cmd = ESPCMD_ENSEL; /* within 250ms of disconnect */
			sc->sc_msgpriq = sc->sc_msgout = 0;

			ESPD(("DD:"));
			if (ecb->flags & ECB_DONE) {
				ESPD(("D "));
				/* successfully finished a transaction */
				untimeout(esp_timeout, ecb);
				esp_done(ecb);
			} else if (sc->sc_flags & ESPF_MAYDISCON) {
				ESPD(("M "));
				/* legal discon/recon is happening */
				TAILQ_INSERT_HEAD(&sc->nexus_list,
				    ecb, chain);
				sc->sc_nexus = NULL;
				sc->sc_state = ESPS_IDLE;
				sc->sc_flags &= ~ESPF_MAYDISCON;
				goto states;
			} else {
				ESPD(("U "));
				/* unexpected disconnection! fail. */
				ecb->xs->error = XS_TIMEOUT;
				untimeout(esp_timeout, ecb);
				esp_done(ecb);
			}
			goto done;
		}
		break;		/* handle the phase */
	default:
		printf("%s: unknown state %d\n",
		    sc->sc_dev.dv_xname, sc->sc_state);
		panic("cannot continue");
	}

	ecb = sc->sc_nexus;

	switch (sc->sc_phase) {
	case MESSAGE_OUT_PHASE:
		/*
		 * ATN was asserted, and the bus changed into MSGOUT phase.
		 * Send a message.
		 */
		esp_msgout(sc);				/* cause intr */
		sc->sc_state = ESPS_DOINGMSGOUT;
		break;
	case COMMAND_PHASE:
		/*
		 * Apparently we need to repeat the previous command.
		 */
		espflush(sc);
		DELAY(1);
		cmd = (u_char *)&ecb->cmd;

		ESPD(("CMD["));
		for (i = 0; i < ecb->clen; i++)
			ESPD(("%02x%c", cmd[i],
			    (i == ecb->clen-1) ? ']' : ' '));
		ESPD((" "));

		/* Now the command into the FIFO */
		for (i = 0; i < ecb->clen; i++)
			espr->espr_fifo = cmd[i];

		espr->espr_cmd = ESPCMD_TRANS;		/* cause intr */
		sc->sc_state = ESPS_NEXUS;
		break;
	case DATA_OUT_PHASE:
		/*
		 * We can feed the device the data now.
		 */
		espflush(sc);
		DELAY(1);
		dmastart(sc->sc_dma, &sc->sc_dp, &sc->sc_dleft, 0,
		    ecb->xs->flags & SCSI_POLL);	/* cause intr */
		sc->sc_state = ESPS_DOINGDMA;
		break;
	case DATA_IN_PHASE:
		/*
		 * The device is ready to give us the data.
		 */
		dmadrain(sc->sc_dma);
		dmastart(sc->sc_dma, &sc->sc_dp, &sc->sc_dleft, 1,
		    ecb->xs->flags & SCSI_POLL);	/* cause intr */
		sc->sc_state = ESPS_DOINGDMA;
		break;
	case MESSAGE_IN_PHASE:
		espflush(sc);
		DELAY(1);
		espr->espr_cmd = ESPCMD_TRANS;		/* cause intr */
		sc->sc_state = ESPS_DOINGMSGIN;
		break;
	case STATUS_PHASE:
		espflush(sc);
		DELAY(1);
		espr->espr_cmd = ESPCMD_ICCS;		/* cause intr */
		sc->sc_state = ESPS_DOINGSTATUS;
		break;
	default:
		printf("%s: bogus bus phase %d\n",
		    sc->sc_dev.dv_xname);
		break;
	}

done:
	if (sc->sc_dma->sc_rev == DMAREV_1 && dmaintrwas)
		dmaenintr(sc->sc_dma);
	sc->sc_intrcnt.ev_count++;
	return (1);
}