tmc18c30.c revision 79697
1/*	$FreeBSD: head/sys/dev/stg/tmc18c30.c 79697 2001-07-14 00:38:51Z non $	*/
2/*	$NecBSD: tmc18c30.c,v 1.28.12.3 2001/06/19 04:35:48 honda Exp $	*/
3/*	$NetBSD$	*/
4
5#define	STG_DEBUG
6#define	STG_STATICS
7#define	STG_IO_CONTROL_FLAGS	(STG_FIFO_INTERRUPTS | STG_WAIT_FOR_SELECT)
8
9/*
10 * [NetBSD for NEC PC-98 series]
11 *  Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
12 *	NetBSD/pc98 porting staff. All rights reserved.
13 *  Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001
14 *	Naofumi HONDA. All rights reserved.
15 *  Copyright (c) 1996, 1997, 1998, 1999
16 *	Kouichi Matsuda. All rights reserved.
17 *
18 *  Redistribution and use in source and binary forms, with or without
19 *  modification, are permitted provided that the following conditions
20 *  are met:
21 *  1. Redistributions of source code must retain the above copyright
22 *     notice, this list of conditions and the following disclaimer.
23 *  2. Redistributions in binary form must reproduce the above copyright
24 *     notice, this list of conditions and the following disclaimer in the
25 *     documentation and/or other materials provided with the distribution.
26 *  3. The name of the author may not be used to endorse or promote products
27 *     derived from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
31 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
32 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
38 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGE.
40 */
41#include "opt_ddb.h"
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/kernel.h>
46#if defined(__FreeBSD__) && __FreeBSD_version >= 500001
47#include <sys/bio.h>
48#endif	/* __FreeBSD__ */
49#include <sys/buf.h>
50#include <sys/queue.h>
51#include <sys/malloc.h>
52#include <sys/errno.h>
53
54#ifdef __NetBSD__
55#include <sys/device.h>
56#include <machine/bus.h>
57#include <machine/intr.h>
58
59#include <dev/scsipi/scsi_all.h>
60#include <dev/scsipi/scsipi_all.h>
61#include <dev/scsipi/scsiconf.h>
62#include <dev/scsipi/scsi_disk.h>
63
64#include <machine/dvcfg.h>
65#include <machine/physio_proc.h>
66
67#include <i386/Cbus/dev/scsi_low.h>
68#include <i386/Cbus/dev/tmc18c30reg.h>
69#include <i386/Cbus/dev/tmc18c30var.h>
70#endif /* __NetBSD__ */
71
72#ifdef __FreeBSD__
73#include <machine/clock.h>
74#include <machine/cpu.h>
75#include <machine/bus_pio.h>
76#include <machine/bus.h>
77
78#include <machine/dvcfg.h>
79#include <machine/physio_proc.h>
80
81#include <cam/scsi/scsi_low.h>
82#include <dev/stg/tmc18c30reg.h>
83#include <dev/stg/tmc18c30var.h>
84
85#if __FreeBSD_version < 400001
86#include "stg.h"
87struct	stg_softc *stgdata[NSTG];
88#endif
89#endif /* __FreeBSD__ */
90
91/***************************************************
92 * USER SETTINGS
93 ***************************************************/
94/* DEVICE CONFIGURATION FLAGS (MINOR)
95 *
96 * 0x01   DISCONECT OFF
97 * 0x02   PARITY LINE OFF
98 * 0x04   IDENTIFY MSG OFF ( = single lun)
99 * 0x08   SYNC TRANSFER OFF
100 */
101/* #define	STG_SYNC_SUPPORT */	/* NOT YET but easy */
102
103/* For the 512 fifo type: change below */
104#define	TMC18C30_FIFOSZ	0x800
105#define	TMC18C30_FCBSZ	0x200
106#define	TMC18C50_FIFOSZ	0x2000
107#define	TMC18C50_FCBSZ	0x400
108
109#define	STG_MAX_DATA_SIZE	(64 * 1024)
110#define	STG_DELAY_MAX			(2 * 1000 * 1000)
111#define	STG_DELAY_INTERVAL		(1)
112#define	STG_DELAY_SELECT_POLLING_MAX	(5 * 1000 * 1000)
113
114/***************************************************
115 * PARAMS
116 ***************************************************/
117#define	STG_NTARGETS	8
118#define	STG_NLUNS	8
119
120/***************************************************
121 * DEBUG
122 ***************************************************/
123#ifdef	STG_DEBUG
124int stg_debug;
125#endif	/* STG_DEBUG */
126
127#ifdef	STG_STATICS
128struct stg_statics {
129	int arbit_fail_0;
130	int arbit_fail_1;
131	int disconnect;
132	int reselect;
133} stg_statics;
134#endif	/* STG_STATICS */
135
136/***************************************************
137 * IO control flags
138 ***************************************************/
139#define	STG_FIFO_INTERRUPTS	0x0001
140#define	STG_WAIT_FOR_SELECT	0x0100
141
142int stg_io_control = STG_IO_CONTROL_FLAGS;
143
144/***************************************************
145 * DEVICE STRUCTURE
146 ***************************************************/
147extern struct cfdriver stg_cd;
148
149/**************************************************************
150 * DECLARE
151 **************************************************************/
152/* static */
153static void stg_pio_read __P((struct stg_softc *, struct targ_info *, u_int));
154static void stg_pio_write __P((struct stg_softc *, struct targ_info *, u_int));
155static int stg_xfer __P((struct stg_softc *, u_int8_t *, int, int, int));
156static int stg_msg __P((struct stg_softc *, struct targ_info *, u_int));
157static int stg_reselected __P((struct stg_softc *));
158static int stg_disconnected __P((struct stg_softc *, struct targ_info *));
159static __inline void stg_pdma_end __P((struct stg_softc *, struct targ_info *));
160static int stghw_select_targ_wait __P((struct stg_softc *, int));
161static int stghw_check __P((struct stg_softc *));
162static void stghw_init __P((struct stg_softc *));
163static int stg_negate_signal __P((struct stg_softc *, u_int8_t, u_char *));
164static int stg_expect_signal __P((struct stg_softc *, u_int8_t, u_int8_t));
165static int stg_world_start __P((struct stg_softc *, int));
166static int stghw_start_selection __P((struct stg_softc *sc, struct slccb *));
167static void stghw_bus_reset __P((struct stg_softc *));
168static void stghw_attention __P((struct stg_softc *));
169static int stg_target_nexus_establish __P((struct stg_softc *));
170static int stg_lun_nexus_establish __P((struct stg_softc *));
171static int stg_ccb_nexus_establish __P((struct stg_softc *));
172static int stg_targ_init __P((struct stg_softc *, struct targ_info *, int));
173static __inline void stghw_bcr_write_1 __P((struct stg_softc *, u_int8_t));
174static int stg_timeout __P((struct stg_softc *));
175static void stg_selection_done_and_expect_msgout __P((struct stg_softc *));
176
177struct scsi_low_funcs stgfuncs = {
178	SC_LOW_INIT_T stg_world_start,
179	SC_LOW_BUSRST_T stghw_bus_reset,
180	SC_LOW_TARG_INIT_T stg_targ_init,
181	SC_LOW_LUN_INIT_T NULL,
182
183	SC_LOW_SELECT_T stghw_start_selection,
184	SC_LOW_NEXUS_T stg_lun_nexus_establish,
185	SC_LOW_NEXUS_T stg_ccb_nexus_establish,
186
187	SC_LOW_ATTEN_T stghw_attention,
188	SC_LOW_MSG_T stg_msg,
189
190	SC_LOW_TIMEOUT_T stg_timeout,
191	SC_LOW_POLL_T stgintr,
192
193	NULL,
194};
195
196/****************************************************
197 * hwfuncs
198 ****************************************************/
199static __inline void
200stghw_bcr_write_1(sc, bcv)
201	struct stg_softc *sc;
202	u_int8_t bcv;
203{
204
205	bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
206	sc->sc_busimg = bcv;
207}
208
209static int
210stghw_check(sc)
211	struct stg_softc *sc;
212{
213	struct scsi_low_softc *slp = &sc->sc_sclow;
214	bus_space_tag_t iot = sc->sc_iot;
215	bus_space_handle_t ioh = sc->sc_ioh;
216	u_int fcbsize, fcb;
217	u_int16_t lsb, msb;
218
219	lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
220	msb = bus_space_read_1(iot, ioh, tmc_idmsb);
221	switch (msb << 8 | lsb)
222	{
223		case 0x6127:
224			/* TMCCHIP_1800 not supported. (it's my policy) */
225			sc->sc_chip = TMCCHIP_1800;
226			return EINVAL;
227
228		case 0x60e9:
229			if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
230			{
231				sc->sc_chip = TMCCHIP_18C30;
232				sc->sc_fsz = TMC18C30_FIFOSZ;
233				fcbsize = TMC18C30_FCBSZ;
234			}
235			else
236			{
237				sc->sc_chip = TMCCHIP_18C50;
238				sc->sc_fsz = TMC18C50_FIFOSZ;
239				fcbsize = TMC18C50_FCBSZ;
240			}
241			break;
242
243		default:
244			sc->sc_chip = TMCCHIP_UNK;
245			return ENODEV;
246	}
247
248	sc->sc_fcRinit = FCTL_INTEN;
249	sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
250
251	if (slp->sl_cfgflags & CFG_NOATTEN)
252		sc->sc_imsg = 0;
253	else
254		sc->sc_imsg = BCTL_ATN;
255	sc->sc_busc = BCTL_BUSEN;
256
257	sc->sc_wthold = fcbsize + 256;
258	sc->sc_rthold = fcbsize - 256;
259	sc->sc_maxwsize = sc->sc_fsz;
260
261	fcb = fcbsize / (sc->sc_fsz / 16);
262	sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
263	return 0;
264}
265
266static void
267stghw_init(sc)
268	struct stg_softc *sc;
269{
270	bus_space_tag_t iot = sc->sc_iot;
271	bus_space_handle_t ioh = sc->sc_ioh;
272
273	bus_space_write_1(iot, ioh, tmc_ictl, 0);
274	stghw_bcr_write_1(sc, BCTL_BUSFREE);
275	bus_space_write_1(iot, ioh, tmc_fctl,
276			  sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
277	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
278	bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
279
280	bus_space_write_1(iot, ioh, tmc_ssctl, 0);
281}
282
283static int
284stg_targ_init(sc, ti, action)
285	struct stg_softc *sc;
286	struct targ_info *ti;
287	int action;
288{
289	struct stg_targ_info *sti = (void *) ti;
290
291	if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
292	{
293		ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
294		ti->ti_maxsynch.period = 0;
295		ti->ti_maxsynch.offset = 0;
296		sti->sti_reg_synch = 0;
297	}
298	return 0;
299}
300
301/****************************************************
302 * scsi low interface
303 ****************************************************/
304static void
305stghw_attention(sc)
306	struct stg_softc *sc;
307{
308
309	sc->sc_busc |= BCTL_ATN;
310	sc->sc_busimg |= BCTL_ATN;
311	bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
312	SCSI_LOW_DELAY(10);
313}
314
315static void
316stghw_bus_reset(sc)
317	struct stg_softc *sc;
318{
319	bus_space_tag_t iot = sc->sc_iot;
320	bus_space_handle_t ioh = sc->sc_ioh;
321
322	bus_space_write_1(iot, ioh, tmc_ictl, 0);
323	bus_space_write_1(iot, ioh, tmc_fctl, 0);
324	stghw_bcr_write_1(sc, BCTL_RST);
325	SCSI_LOW_DELAY(100000);
326	stghw_bcr_write_1(sc, BCTL_BUSFREE);
327}
328
329static int
330stghw_start_selection(sc, cb)
331	struct stg_softc *sc;
332	struct slccb *cb;
333{
334	bus_space_tag_t iot = sc->sc_iot;
335	bus_space_handle_t ioh = sc->sc_ioh;
336	struct targ_info *ti = cb->ti;
337	register u_int8_t stat;
338	int s;
339
340	sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
341	sc->sc_dataout_timeout = 0;
342	sc->sc_ubf_timeout = 0;
343	stghw_bcr_write_1(sc, BCTL_BUSFREE);
344	bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
345
346	s = splhigh();
347	stat = bus_space_read_1(iot, ioh, tmc_astat);
348	if ((stat & ASTAT_INT) != 0)
349	{
350		splx(s);
351		return SCSI_LOW_START_FAIL;
352	}
353
354	bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
355	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
356	splx(s);
357
358	SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
359	return SCSI_LOW_START_OK;
360}
361
362static int
363stg_world_start(sc, fdone)
364	struct stg_softc *sc;
365	int fdone;
366{
367	struct scsi_low_softc *slp = &sc->sc_sclow;
368	int error;
369
370	if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
371		sc->sc_fcRinit |= FCTL_PARENB;
372	else
373		sc->sc_fcRinit &= ~FCTL_PARENB;
374
375	if ((error = stghw_check(sc)) != 0)
376		return error;
377
378	stghw_init(sc);
379	scsi_low_bus_reset(slp);
380	stghw_init(sc);
381
382	SOFT_INTR_REQUIRED(slp);
383	return 0;
384}
385
386static int
387stg_msg(sc, ti, msg)
388	struct stg_softc *sc;
389	struct targ_info *ti;
390	u_int msg;
391{
392	bus_space_tag_t iot = sc->sc_iot;
393	bus_space_handle_t ioh = sc->sc_ioh;
394	struct stg_targ_info *sti = (void *) ti;
395	u_int period, offset;
396
397	if ((msg & SCSI_LOW_MSG_WIDE) != 0)
398	{
399		if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
400		{
401			ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
402			return EINVAL;
403		}
404		return 0;
405	}
406
407	if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
408		return 0;
409
410 	period = ti->ti_maxsynch.period;
411	offset = ti->ti_maxsynch.offset;
412	period = period << 2;
413	if (period >= 200)
414	{
415		sti->sti_reg_synch = (period - 200) / 50;
416		if (period % 50)
417			sti->sti_reg_synch ++;
418		sti->sti_reg_synch |= SSCTL_SYNCHEN;
419	}
420	else if (period >= 100)
421	{
422		sti->sti_reg_synch = (period - 100) / 50;
423		if (period % 50)
424			sti->sti_reg_synch ++;
425		sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
426	}
427	bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
428	return 0;
429}
430
431/**************************************************************
432 * General probe attach
433 **************************************************************/
434int
435stgprobesubr(iot, ioh, dvcfg)
436	bus_space_tag_t iot;
437	bus_space_handle_t ioh;
438	u_int dvcfg;
439{
440	u_int16_t lsb, msb;
441
442	lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
443	msb = bus_space_read_1(iot, ioh, tmc_idmsb);
444	switch (msb << 8 | lsb)
445	{
446		default:
447			return 0;
448		case 0x6127:
449			/* not support! */
450			return 0;
451		case 0x60e9:
452			return 1;
453	}
454	return 0;
455}
456
457int
458stgprint(aux, name)
459	void *aux;
460	const char *name;
461{
462
463	if (name != NULL)
464		printf("%s: scsibus ", name);
465	return UNCONF;
466}
467
468void
469stgattachsubr(sc)
470	struct stg_softc *sc;
471{
472	struct scsi_low_softc *slp = &sc->sc_sclow;
473
474	printf("\n");
475
476	sc->sc_idbit = (1 << slp->sl_hostid);
477	slp->sl_funcs = &stgfuncs;
478	sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
479
480	slp->sl_flags |= HW_READ_PADDING;
481	slp->sl_cfgflags |= CFG_ASYNC;	/* XXX */
482
483	(void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
484				sizeof(struct stg_targ_info), 0);
485}
486
487/**************************************************************
488 * PDMA functions
489 **************************************************************/
490static __inline void
491stg_pdma_end(sc, ti)
492	struct stg_softc *sc;
493	struct targ_info *ti;
494{
495	struct scsi_low_softc *slp = &sc->sc_sclow;
496	bus_space_tag_t iot = sc->sc_iot;
497	bus_space_handle_t ioh = sc->sc_ioh;
498	struct slccb *cb = slp->sl_Qnexus;
499	u_int len, tres;
500
501	slp->sl_flags &= ~HW_PDMASTART;
502	sc->sc_icinit &= ~ICTL_FIFO;
503	sc->sc_dataout_timeout = 0;
504
505	if (cb == NULL)
506	{
507		slp->sl_error |= PDMAERR;
508		goto out;
509	}
510
511	if (ti->ti_phase == PH_DATA)
512	{
513		len = bus_space_read_2(iot, ioh, tmc_fdcnt);
514		if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
515		{
516			if (len != 0)
517			{
518				tres = len + slp->sl_scp.scp_datalen;
519				if (tres <= (u_int) cb->ccb_scp.scp_datalen)
520				{
521					slp->sl_scp.scp_data -= len;
522					slp->sl_scp.scp_datalen = tres;
523				}
524				else
525				{
526					slp->sl_error |= PDMAERR;
527					printf("%s len %x >= datalen %x\n",
528						slp->sl_xname,
529						len, slp->sl_scp.scp_datalen);
530				}
531			}
532		}
533		else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
534		{
535			if (len != 0)
536			{
537				slp->sl_error |= PDMAERR;
538				printf("%s: len %x left in fifo\n",
539					slp->sl_xname, len);
540			}
541		}
542		scsi_low_data_finish(slp);
543	}
544	else
545	{
546
547		printf("%s data phase miss\n", slp->sl_xname);
548		slp->sl_error |= PDMAERR;
549	}
550
551out:
552	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
553}
554
555static void
556stg_pio_read(sc, ti, thold)
557	struct stg_softc *sc;
558	struct targ_info *ti;
559	u_int thold;
560{
561	struct scsi_low_softc *slp = &sc->sc_sclow;
562	bus_space_tag_t iot = sc->sc_iot;
563	bus_space_handle_t ioh = sc->sc_ioh;
564	struct sc_p *sp = &slp->sl_scp;
565	int s, tout;
566	u_int res;
567	u_int8_t stat;
568
569	if ((slp->sl_flags & HW_PDMASTART) == 0)
570	{
571		bus_space_write_1(iot, ioh, tmc_fctl,
572				  sc->sc_fcRinit | FCTL_FIFOEN);
573		slp->sl_flags |= HW_PDMASTART;
574	}
575
576	tout = sc->sc_tmaxcnt;
577	while (tout -- > 0)
578	{
579		if (thold > 0)
580		{
581			s = splhigh();
582			res = bus_space_read_2(iot, ioh, tmc_fdcnt);
583			if (res < thold)
584			{
585				bus_space_write_1(iot, ioh, tmc_ictl,
586						  sc->sc_icinit);
587				splx(s);
588				break;
589			}
590			splx(s);
591		}
592		else
593		{
594			stat = bus_space_read_1(iot, ioh, tmc_bstat);
595			res = bus_space_read_2(iot, ioh, tmc_fdcnt);
596			if (res == 0)
597			{
598				if ((stat & PHASE_MASK) != DATA_IN_PHASE)
599					break;
600				if (sp->scp_datalen <= 0)
601					break;
602				SCSI_LOW_DELAY(1);
603				continue;
604			}
605		}
606
607		/* The assumtion res != 0 is valid here */
608		if (res > sp->scp_datalen)
609		{
610			if (res == (u_int) -1)
611				break;
612
613			slp->sl_error |= PDMAERR;
614			if ((slp->sl_flags & HW_READ_PADDING) == 0)
615			{
616				printf("%s: read padding required\n",
617					slp->sl_xname);
618				break;
619			}
620
621			sp->scp_datalen = 0;
622			if (res > STG_MAX_DATA_SIZE)
623				res = STG_MAX_DATA_SIZE;
624			while (res -- > 0)
625			{
626				(void) bus_space_read_1(iot, ioh, tmc_rfifo);
627			}
628			continue;
629		}
630
631		sp->scp_datalen -= res;
632		if (res & 1)
633		{
634			*sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
635			sp->scp_data ++;
636			res --;
637		}
638
639		bus_space_read_multi_2(iot, ioh, tmc_rfifo,
640				       (u_int16_t *) sp->scp_data, res >> 1);
641		sp->scp_data += res;
642	}
643
644	if (tout <= 0)
645		printf("%s: pio read timeout\n", slp->sl_xname);
646}
647
648static void
649stg_pio_write(sc, ti, thold)
650	struct stg_softc *sc;
651	struct targ_info *ti;
652	u_int thold;
653{
654	struct scsi_low_softc *slp = &sc->sc_sclow;
655	bus_space_tag_t iot = sc->sc_iot;
656	bus_space_handle_t ioh = sc->sc_ioh;
657	struct sc_p *sp = &slp->sl_scp;
658	u_int res;
659	int s, tout;
660	register u_int8_t stat;
661
662	if ((slp->sl_flags & HW_PDMASTART) == 0)
663	{
664		stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
665		bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
666		bus_space_write_1(iot, ioh, tmc_fctl, stat);
667		slp->sl_flags |= HW_PDMASTART;
668	}
669
670	tout = sc->sc_tmaxcnt;
671	while (tout -- > 0)
672	{
673		stat = bus_space_read_1(iot, ioh, tmc_bstat);
674		if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
675			break;
676
677		if (sp->scp_datalen <= 0)
678		{
679			if (sc->sc_dataout_timeout == 0)
680				sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
681			break;
682		}
683
684		if (thold > 0)
685		{
686			s = splhigh();
687			res = bus_space_read_2(iot, ioh, tmc_fdcnt);
688			if (res > thold)
689			{
690				bus_space_write_1(iot, ioh, tmc_ictl,
691						  sc->sc_icinit);
692				splx(s);
693				break;
694			}
695			splx(s);
696		}
697		else
698		{
699			res = bus_space_read_2(iot, ioh, tmc_fdcnt);
700			if (res > sc->sc_maxwsize / 2)
701			{
702				SCSI_LOW_DELAY(1);
703				continue;
704			}
705		}
706
707		if (res == (u_int) -1)
708			break;
709		res = sc->sc_maxwsize - res;
710		if (res > sp->scp_datalen)
711			res = sp->scp_datalen;
712
713		sp->scp_datalen -= res;
714		if ((res & 0x1) != 0)
715		{
716			bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
717			sp->scp_data ++;
718			res --;
719		}
720
721		bus_space_write_multi_2(iot, ioh, tmc_wfifo,
722					(u_int16_t *) sp->scp_data, res >> 1);
723		sp->scp_data += res;
724	}
725
726	if (tout <= 0)
727		printf("%s: pio write timeout\n", slp->sl_xname);
728}
729
730static int
731stg_negate_signal(sc, mask, s)
732	struct stg_softc *sc;
733	u_int8_t mask;
734	u_char *s;
735{
736	struct scsi_low_softc *slp = &sc->sc_sclow;
737	bus_space_tag_t bst = sc->sc_iot;
738	bus_space_handle_t bsh = sc->sc_ioh;
739	int wc;
740	u_int8_t regv;
741
742	for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
743	{
744		regv = bus_space_read_1(bst, bsh, tmc_bstat);
745		if (regv == (u_int8_t) -1)
746			return -1;
747		if ((regv & mask) == 0)
748			return 1;
749
750		SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
751	}
752
753	printf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
754	return -1;
755}
756
757static int
758stg_expect_signal(sc, phase, mask)
759	struct stg_softc *sc;
760	u_int8_t phase, mask;
761{
762	struct scsi_low_softc *slp = &sc->sc_sclow;
763	bus_space_tag_t bst = sc->sc_iot;
764	bus_space_handle_t bsh = sc->sc_ioh;
765	int wc;
766	u_int8_t ph;
767
768	phase &= PHASE_MASK;
769	for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
770	{
771		ph = bus_space_read_1(bst, bsh, tmc_bstat);
772		if (ph == (u_int8_t) -1)
773			return -1;
774		if ((ph & PHASE_MASK) != phase)
775			return 0;
776		if ((ph & mask) != 0)
777			return 1;
778
779		SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
780	}
781
782	printf("%s: stg_expect_signal timeout\n", slp->sl_xname);
783	return -1;
784}
785
786static int
787stg_xfer(sc, buf, len, phase, clear_atn)
788	struct stg_softc *sc;
789	u_int8_t *buf;
790	int len;
791	int phase;
792	int clear_atn;
793{
794	bus_space_tag_t iot = sc->sc_iot;
795	bus_space_handle_t ioh = sc->sc_ioh;
796	int rv, ptr;
797
798	if (phase & BSTAT_IO)
799		bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
800	else
801		bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
802
803	for (ptr = 0; len > 0; len --)
804	{
805		rv = stg_expect_signal(sc, phase, BSTAT_REQ);
806		if (rv <= 0)
807			goto bad;
808
809		if (len == 1 && clear_atn != 0)
810		{
811			sc->sc_busc &= ~BCTL_ATN;
812			stghw_bcr_write_1(sc, sc->sc_busc);
813			SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
814		}
815
816		if (phase & BSTAT_IO)
817		{
818			buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
819		}
820		else
821		{
822			bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
823		}
824
825		stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
826	}
827
828bad:
829	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
830	return len;
831}
832
833/**************************************************************
834 * disconnect & reselect (HW low)
835 **************************************************************/
836static int
837stg_reselected(sc)
838	struct stg_softc *sc;
839{
840	struct scsi_low_softc *slp = &sc->sc_sclow;
841	bus_space_tag_t iot = sc->sc_iot;
842	bus_space_handle_t ioh = sc->sc_ioh;
843	int tout;
844	u_int sid;
845	u_int8_t regv;
846
847	if (slp->sl_selid != NULL)
848	{
849		/* XXX:
850		 * Selection vs Reselection conflicts.
851		 */
852		bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
853		stghw_bcr_write_1(sc, BCTL_BUSFREE);
854	}
855	else if (slp->sl_Tnexus != NULL)
856	{
857		printf("%s: unexpected termination\n", slp->sl_xname);
858		stg_disconnected(sc, slp->sl_Tnexus);
859	}
860
861	/* XXX:
862	 * We should ack the reselection as soon as possible,
863	 * becuase the target would abort the current reselection seq
864  	 * due to reselection timeout.
865	 */
866	tout = STG_DELAY_SELECT_POLLING_MAX;
867	while (tout -- > 0)
868	{
869		regv = bus_space_read_1(iot, ioh, tmc_bstat);
870		if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
871			    (BSTAT_IO | BSTAT_SEL))
872		{
873			SCSI_LOW_DELAY(1);
874			regv = bus_space_read_1(iot, ioh, tmc_bstat);
875			if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) ==
876				    (BSTAT_IO | BSTAT_SEL))
877				goto reselect_start;
878		}
879		SCSI_LOW_DELAY(1);
880	}
881	printf("%s: reselction timeout I\n", slp->sl_xname);
882	return EJUSTRETURN;
883
884reselect_start:
885	sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
886	if ((sid & sc->sc_idbit) == 0)
887	{
888		/* not us */
889		return EJUSTRETURN;
890	}
891
892	bus_space_write_1(iot, ioh, tmc_fctl,
893			    sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
894	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
895	stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
896
897	while (tout -- > 0)
898	{
899		regv = bus_space_read_1(iot, ioh, tmc_bstat);
900		if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
901			goto reselected;
902		SCSI_LOW_DELAY(1);
903	}
904	printf("%s: reselction timeout II\n", slp->sl_xname);
905	return EJUSTRETURN;
906
907reselected:
908	sid &= ~sc->sc_idbit;
909	sid = ffs(sid) - 1;
910	if (scsi_low_reselected(slp, sid) == NULL)
911		return EJUSTRETURN;
912
913#ifdef	STG_STATICS
914	stg_statics.reselect ++;
915#endif	/* STG_STATICS */
916	return EJUSTRETURN;
917}
918
919static int
920stg_disconnected(sc, ti)
921	struct stg_softc *sc;
922	struct targ_info *ti;
923{
924	struct scsi_low_softc *slp = &sc->sc_sclow;
925	bus_space_tag_t iot = sc->sc_iot;
926	bus_space_handle_t ioh = sc->sc_ioh;
927
928	/* clear bus status & fifo */
929	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
930	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
931	stghw_bcr_write_1(sc, BCTL_BUSFREE);
932	sc->sc_icinit &= ~ICTL_FIFO;
933	sc->sc_busc &= ~BCTL_ATN;
934	sc->sc_dataout_timeout = 0;
935	sc->sc_ubf_timeout = 0;
936
937#ifdef	STG_STATICS
938	stg_statics.disconnect ++;
939#endif	/* STG_STATICS */
940	scsi_low_disconnected(slp, ti);
941	return 1;
942}
943
944/**************************************************************
945 * SEQUENCER
946 **************************************************************/
947static int
948stg_target_nexus_establish(sc)
949	struct stg_softc *sc;
950{
951	struct scsi_low_softc *slp = &sc->sc_sclow;
952	bus_space_tag_t iot = sc->sc_iot;
953	bus_space_handle_t ioh = sc->sc_ioh;
954	struct targ_info *ti = slp->sl_Tnexus;
955	struct stg_targ_info *sti = (void *) ti;
956
957	bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
958	if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
959	{
960		sc->sc_icinit |= ICTL_FIFO;
961	}
962	return 0;
963}
964
965static int
966stg_lun_nexus_establish(sc)
967	struct stg_softc *sc;
968{
969
970	return 0;
971}
972
973static int
974stg_ccb_nexus_establish(sc)
975	struct stg_softc *sc;
976{
977	struct scsi_low_softc *slp = &sc->sc_sclow;
978	struct slccb *cb = slp->sl_Qnexus;
979
980	sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
981	return 0;
982}
983
984#define	STGHW_SELECT_INTERVAL	10
985
986static int
987stghw_select_targ_wait(sc, mu)
988	struct stg_softc *sc;
989	int mu;
990{
991	bus_space_tag_t iot = sc->sc_iot;
992	bus_space_handle_t ioh = sc->sc_ioh;
993
994	mu = mu / STGHW_SELECT_INTERVAL;
995	while (mu -- > 0)
996	{
997		if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
998		{
999			SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
1000			continue;
1001		}
1002		SCSI_LOW_DELAY(1);
1003		if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
1004		{
1005			return 0;
1006		}
1007	}
1008	return ENXIO;
1009}
1010
1011static void
1012stg_selection_done_and_expect_msgout(sc)
1013	struct stg_softc *sc;
1014{
1015	struct scsi_low_softc *slp = &sc->sc_sclow;
1016	bus_space_tag_t iot = sc->sc_iot;
1017	bus_space_handle_t ioh = sc->sc_ioh;
1018
1019	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
1020	bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1021	stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
1022	SCSI_LOW_ASSERT_ATN(slp);
1023}
1024
1025int
1026stgintr(arg)
1027	void *arg;
1028{
1029	struct stg_softc *sc = arg;
1030	struct scsi_low_softc *slp = &sc->sc_sclow;
1031	bus_space_tag_t iot = sc->sc_iot;
1032	bus_space_handle_t ioh = sc->sc_ioh;
1033	struct targ_info *ti;
1034	struct physio_proc *pp;
1035	struct buf *bp;
1036	u_int derror, flags;
1037	int len, s;
1038	u_int8_t status, astatus, regv;
1039
1040	/*******************************************
1041	 * interrupt check
1042	 *******************************************/
1043	if (slp->sl_flags & HW_INACTIVE)
1044		return 0;
1045
1046	astatus = bus_space_read_1(iot, ioh, tmc_astat);
1047	status = bus_space_read_1(iot, ioh, tmc_bstat);
1048
1049	if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
1050		return 0;
1051
1052	bus_space_write_1(iot, ioh, tmc_ictl, 0);
1053	if (astatus & ASTAT_SCSIRST)
1054	{
1055		bus_space_write_1(iot, ioh, tmc_fctl,
1056				  sc->sc_fcRinit | FCTL_CLRFIFO);
1057		bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1058		bus_space_write_1(iot, ioh, tmc_ictl, 0);
1059
1060		scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1061				 "bus reset (power off?)");
1062		return 1;
1063	}
1064
1065	/*******************************************
1066	 * debug section
1067	 *******************************************/
1068#ifdef	STG_DEBUG
1069	if (stg_debug)
1070	{
1071		scsi_low_print(slp, NULL);
1072		printf("%s: st %x ist %x\n\n", slp->sl_xname,
1073		       status, astatus);
1074#ifdef	DDB
1075		if (stg_debug > 1)
1076			SCSI_LOW_DEBUGGER("stg");
1077#endif	/* DDB */
1078	}
1079#endif	/* STG_DEBUG */
1080
1081	/*******************************************
1082	 * reselection & nexus
1083	 *******************************************/
1084	if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1085	{
1086		if (stg_reselected(sc) == EJUSTRETURN)
1087			goto out;
1088	}
1089
1090	if ((ti = slp->sl_Tnexus) == NULL)
1091		return 0;
1092
1093	derror = 0;
1094	if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1095	    (sc->sc_fcRinit & FCTL_PARENB) != 0)
1096	{
1097		slp->sl_error |= PARITYERR;
1098		derror = SCSI_LOW_DATA_PE;
1099		if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1100			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1101		else
1102			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1103	}
1104
1105	/*******************************************
1106	 * aribitration & selection
1107	 *******************************************/
1108	switch (ti->ti_phase)
1109	{
1110	case PH_ARBSTART:
1111		if ((astatus & ASTAT_ARBIT) == 0)
1112		{
1113#ifdef	STG_STATICS
1114			stg_statics.arbit_fail_0 ++;
1115#endif	/* STG_STATICS */
1116			goto arb_fail;
1117		}
1118
1119		status = bus_space_read_1(iot, ioh, tmc_bstat);
1120		if ((status & BSTAT_IO) != 0)
1121		{
1122			/* XXX:
1123			 * Selection vs Reselection conflicts.
1124			 */
1125#ifdef	STG_STATICS
1126			stg_statics.arbit_fail_1 ++;
1127#endif	/* STG_STATICS */
1128arb_fail:
1129			bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1130			stghw_bcr_write_1(sc, BCTL_BUSFREE);
1131			scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1132			goto out;
1133		}
1134
1135		/*
1136		 * selection assert start.
1137		 */
1138		SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1139		scsi_low_arbit_win(slp);
1140
1141		s = splhigh();
1142		bus_space_write_1(iot, ioh, tmc_scsiid,
1143				  sc->sc_idbit | (1 << ti->ti_id));
1144		stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1145		bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1146		if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1147		{
1148			/* selection abort delay 200 + 100 micro sec */
1149			if (stghw_select_targ_wait(sc, 300) == 0)
1150			{
1151				SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1152				stg_selection_done_and_expect_msgout(sc);
1153			}
1154		}
1155		splx(s);
1156		goto out;
1157
1158	case PH_SELSTART:
1159		if ((status & BSTAT_BSY) == 0)
1160		{
1161			/* selection timeout delay 250 ms */
1162			if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1163			{
1164				stg_disconnected(sc, ti);
1165				goto out;
1166			}
1167		}
1168
1169		SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1170		stg_selection_done_and_expect_msgout(sc);
1171		goto out;
1172
1173	case PH_SELECTED:
1174		if ((status & BSTAT_REQ) == 0)
1175			goto out;
1176		stg_target_nexus_establish(sc);
1177		break;
1178
1179	case PH_RESEL:
1180		if ((status & BSTAT_REQ) == 0)
1181			goto out;
1182
1183		/* clear a busy line */
1184		bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1185		stghw_bcr_write_1(sc, sc->sc_busc);
1186		stg_target_nexus_establish(sc);
1187		if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1188		{
1189			printf("%s: unexpected phase after reselect\n",
1190			       slp->sl_xname);
1191			slp->sl_error |= FATALIO;
1192			scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1193			goto out;
1194		}
1195		break;
1196	}
1197
1198	/*******************************************
1199	 * data phase
1200	 *******************************************/
1201	if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1202	{
1203		if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1204			stg_pio_read(sc, ti, 0);
1205
1206		stg_pdma_end(sc, ti);
1207	}
1208
1209	/*******************************************
1210	 * scsi seq
1211	 *******************************************/
1212	switch (status & PHASE_MASK)
1213	{
1214	case COMMAND_PHASE:
1215		if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1216			break;
1217
1218		SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1219		if (scsi_low_cmd(slp, ti) != 0)
1220		{
1221			scsi_low_attention(slp);
1222		}
1223
1224		if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1225			     COMMAND_PHASE, 0) != 0)
1226		{
1227			printf("%s: CMDOUT short\n", slp->sl_xname);
1228		}
1229		break;
1230
1231	case DATA_OUT_PHASE:
1232		SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1233		if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1234		{
1235			scsi_low_attention(slp);
1236		}
1237
1238		pp = physio_proc_enter(bp);
1239		if ((sc->sc_icinit & ICTL_FIFO) != 0)
1240			stg_pio_write(sc, ti, sc->sc_wthold);
1241		else
1242			stg_pio_write(sc, ti, 0);
1243		physio_proc_leave(pp);
1244		break;
1245
1246	case DATA_IN_PHASE:
1247		SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1248		if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1249		{
1250			scsi_low_attention(slp);
1251		}
1252
1253		pp = physio_proc_enter(bp);
1254		if ((sc->sc_icinit & ICTL_FIFO) != 0)
1255			stg_pio_read(sc, ti, sc->sc_rthold);
1256		else
1257			stg_pio_read(sc, ti, 0);
1258		physio_proc_leave(pp);
1259		break;
1260
1261	case STATUS_PHASE:
1262		regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1263		if (regv <= 0)
1264			break;
1265
1266		SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1267		regv = bus_space_read_1(iot, ioh, tmc_sdna);
1268		if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1269		{
1270			scsi_low_attention(slp);
1271		}
1272		if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1273		{
1274			printf("%s: STATIN: data mismatch\n", slp->sl_xname);
1275		}
1276		stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1277		break;
1278
1279	case MESSAGE_OUT_PHASE:
1280		if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1281			break;
1282
1283		SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1284		flags = (ti->ti_ophase != ti->ti_phase) ?
1285				SCSI_LOW_MSGOUT_INIT : 0;
1286		len = scsi_low_msgout(slp, ti, flags);
1287
1288		if (len > 1 && slp->sl_atten == 0)
1289		{
1290			scsi_low_attention(slp);
1291		}
1292
1293		if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1294			     slp->sl_clear_atten) != 0)
1295		{
1296			printf("%s: MSGOUT short\n", slp->sl_xname);
1297		}
1298		else
1299		{
1300			if (slp->sl_msgphase >= MSGPH_ABORT)
1301			{
1302				stg_disconnected(sc, ti);
1303			}
1304		}
1305		break;
1306
1307	case MESSAGE_IN_PHASE:
1308		/* confirm phase and req signal */
1309		if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1310			break;
1311
1312		SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1313
1314		/* read data with NOACK */
1315		regv = bus_space_read_1(iot, ioh, tmc_sdna);
1316
1317		if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1318		{
1319			if (scsi_low_is_msgout_continue(ti, 0) != 0)
1320			{
1321				scsi_low_attention(slp);
1322			}
1323		}
1324
1325		/* read data with ACK */
1326		if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1327		{
1328			printf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1329		}
1330
1331		/* wait for the ack negated */
1332		stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1333
1334		if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1335		{
1336			stg_disconnected(sc, ti);
1337		}
1338		break;
1339
1340	case BUSFREE_PHASE:
1341		printf("%s: unexpected disconnect\n", slp->sl_xname);
1342		stg_disconnected(sc, ti);
1343		break;
1344
1345	default:
1346		slp->sl_error |= FATALIO;
1347		printf("%s: unknown phase bus %x intr %x\n",
1348			slp->sl_xname, status, astatus);
1349		break;
1350	}
1351
1352out:
1353	bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1354	return 1;
1355}
1356
1357static int
1358stg_timeout(sc)
1359	struct stg_softc *sc;
1360{
1361	struct scsi_low_softc *slp = &sc->sc_sclow;
1362	bus_space_tag_t iot = sc->sc_iot;
1363	bus_space_handle_t ioh = sc->sc_ioh;
1364	int tout, count;
1365	u_int8_t status;
1366
1367	if (slp->sl_Tnexus == NULL)
1368		return 0;
1369
1370	status = bus_space_read_1(iot, ioh, tmc_bstat);
1371	if ((status & PHASE_MASK) == 0)
1372	{
1373		if (sc->sc_ubf_timeout ++ == 0)
1374			return 0;
1375
1376		printf("%s: unexpected bus free detected\n", slp->sl_xname);
1377		slp->sl_error |= FATALIO;
1378		scsi_low_print(slp, slp->sl_Tnexus);
1379		stg_disconnected(sc, slp->sl_Tnexus);
1380		return 0;
1381	}
1382
1383	switch (status & PHASE_MASK)
1384	{
1385	case DATA_OUT_PHASE:
1386		if (sc->sc_dataout_timeout == 0)
1387			break;
1388		if ((status & BSTAT_REQ) == 0)
1389			break;
1390		if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1391			break;
1392		if ((-- sc->sc_dataout_timeout) > 0)
1393			break;
1394
1395	        slp->sl_error |= PDMAERR;
1396		if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1397		{
1398			printf("%s: write padding required\n",
1399				slp->sl_xname);
1400			break;
1401		}
1402
1403		bus_space_write_1(iot, ioh, tmc_ictl, 0);
1404
1405		tout = STG_DELAY_MAX;
1406		while (tout --)
1407		{
1408			status = bus_space_read_1(iot, ioh, tmc_bstat);
1409			if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1410				break;
1411
1412			if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1413			{
1414				SCSI_LOW_DELAY(1);
1415				continue;
1416			}
1417
1418			for (count = sc->sc_maxwsize; count > 0; count --)
1419				bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1420		}
1421
1422		status = bus_space_read_1(iot, ioh, tmc_bstat);
1423		if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1424			sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1425
1426		bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1427		break;
1428
1429	default:
1430		break;
1431	}
1432	return 0;
1433}
1434