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