More spl_* removal from dev/disk/, replacing them with critical sections.
[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.8 2005/06/06 21:48:16 eirikn 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(sc, bcv)
195         struct stg_softc *sc;
196         u_int8_t bcv;
197 {
198
199         bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, bcv);
200         sc->sc_busimg = bcv;
201 }
202
203 static int
204 stghw_check(sc)
205         struct stg_softc *sc;
206 {
207         struct scsi_low_softc *slp = &sc->sc_sclow;
208         bus_space_tag_t iot = sc->sc_iot;
209         bus_space_handle_t ioh = sc->sc_ioh;
210         u_int fcbsize, fcb;
211         u_int16_t lsb, msb;
212
213         lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
214         msb = bus_space_read_1(iot, ioh, tmc_idmsb);
215         switch (msb << 8 | lsb)
216         {
217                 case 0x6127:
218                         /* TMCCHIP_1800 not supported. (it's my policy) */
219                         sc->sc_chip = TMCCHIP_1800;
220                         return EINVAL;
221
222                 case 0x60e9:
223                         if (bus_space_read_1(iot, ioh, tmc_cfg2) & 0x02)
224                         {
225                                 sc->sc_chip = TMCCHIP_18C30;
226                                 sc->sc_fsz = TMC18C30_FIFOSZ;
227                                 fcbsize = TMC18C30_FCBSZ;
228                         }
229                         else
230                         {
231                                 sc->sc_chip = TMCCHIP_18C50;
232                                 sc->sc_fsz = TMC18C50_FIFOSZ;
233                                 fcbsize = TMC18C50_FCBSZ;
234                         }
235                         break;
236
237                 default:
238                         sc->sc_chip = TMCCHIP_UNK;
239                         return ENODEV;
240         }
241
242         sc->sc_fcRinit = FCTL_INTEN;
243         sc->sc_fcWinit = FCTL_PARENB | FCTL_INTEN;
244
245         if (slp->sl_cfgflags & CFG_NOATTEN)
246                 sc->sc_imsg = 0;
247         else
248                 sc->sc_imsg = BCTL_ATN;
249         sc->sc_busc = BCTL_BUSEN;
250
251         sc->sc_wthold = fcbsize + 256;
252         sc->sc_rthold = fcbsize - 256;
253         sc->sc_maxwsize = sc->sc_fsz;
254
255         fcb = fcbsize / (sc->sc_fsz / 16);
256         sc->sc_icinit = ICTL_CD | ICTL_SEL | ICTL_ARBIT | fcb;
257         return 0;
258 }
259
260 static void
261 stghw_init(sc)
262         struct stg_softc *sc;
263 {
264         bus_space_tag_t iot = sc->sc_iot;
265         bus_space_handle_t ioh = sc->sc_ioh;
266
267         bus_space_write_1(iot, ioh, tmc_ictl, 0);
268         stghw_bcr_write_1(sc, BCTL_BUSFREE);
269         bus_space_write_1(iot, ioh, tmc_fctl,
270                           sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
271         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
272         bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
273
274         bus_space_write_1(iot, ioh, tmc_ssctl, 0);
275 }
276
277 static int
278 stg_targ_init(sc, ti, action)
279         struct stg_softc *sc;
280         struct targ_info *ti;
281         int action;
282 {
283         struct stg_targ_info *sti = (void *) ti;
284
285         if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
286         {
287                 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
288                 ti->ti_maxsynch.period = 0;
289                 ti->ti_maxsynch.offset = 0;
290                 sti->sti_reg_synch = 0;
291         }
292         return 0;
293 }       
294
295 /****************************************************
296  * scsi low interface
297  ****************************************************/
298 static void
299 stghw_attention(sc)
300         struct stg_softc *sc;
301 {
302
303         sc->sc_busc |= BCTL_ATN;
304         sc->sc_busimg |= BCTL_ATN;
305         bus_space_write_1(sc->sc_iot, sc->sc_ioh, tmc_bctl, sc->sc_busimg);
306         SCSI_LOW_DELAY(10);
307 }
308
309 static void
310 stghw_bus_reset(sc)
311         struct stg_softc *sc;
312 {
313         bus_space_tag_t iot = sc->sc_iot;
314         bus_space_handle_t ioh = sc->sc_ioh;
315
316         bus_space_write_1(iot, ioh, tmc_ictl, 0);
317         bus_space_write_1(iot, ioh, tmc_fctl, 0);
318         stghw_bcr_write_1(sc, BCTL_RST);
319         SCSI_LOW_DELAY(100000);
320         stghw_bcr_write_1(sc, BCTL_BUSFREE);
321 }
322
323 static int
324 stghw_start_selection(sc, cb)
325         struct stg_softc *sc;
326         struct slccb *cb;
327 {
328         bus_space_tag_t iot = sc->sc_iot;
329         bus_space_handle_t ioh = sc->sc_ioh;
330         struct targ_info *ti = cb->ti;
331         u_int8_t stat;
332
333         sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
334         sc->sc_dataout_timeout = 0;
335         sc->sc_ubf_timeout = 0;
336         stghw_bcr_write_1(sc, BCTL_BUSFREE);
337         bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
338
339         crit_enter();
340         stat = bus_space_read_1(iot, ioh, tmc_astat);
341         if ((stat & ASTAT_INT) != 0)
342         {
343                 crit_exit();
344                 return SCSI_LOW_START_FAIL;
345         }
346
347         bus_space_write_1(iot, ioh, tmc_scsiid, sc->sc_idbit);
348         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_ARBIT);
349         crit_exit();
350
351         SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
352         return SCSI_LOW_START_OK;
353 }
354
355 static int
356 stg_world_start(sc, fdone)
357         struct stg_softc *sc;
358         int fdone;
359 {
360         struct scsi_low_softc *slp = &sc->sc_sclow;
361         int error;
362
363         if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
364                 sc->sc_fcRinit |= FCTL_PARENB;
365         else
366                 sc->sc_fcRinit &= ~FCTL_PARENB;
367
368         if ((error = stghw_check(sc)) != 0)
369                 return error;
370
371         stghw_init(sc);
372         scsi_low_bus_reset(slp);
373         stghw_init(sc);
374
375         SOFT_INTR_REQUIRED(slp);
376         return 0;
377 }
378
379 static int
380 stg_msg(sc, ti, msg)
381         struct stg_softc *sc;
382         struct targ_info *ti;
383         u_int msg;
384 {
385         bus_space_tag_t iot = sc->sc_iot;
386         bus_space_handle_t ioh = sc->sc_ioh;
387         struct stg_targ_info *sti = (void *) ti;
388         u_int period, offset;
389
390         if ((msg & SCSI_LOW_MSG_WIDE) != 0)
391         {
392                 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
393                 {
394                         ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
395                         return EINVAL;
396                 }
397                 return 0;
398         }
399
400         if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
401                 return 0;
402
403         period = ti->ti_maxsynch.period;
404         offset = ti->ti_maxsynch.offset;
405         period = period << 2;
406         if (period >= 200)
407         {
408                 sti->sti_reg_synch = (period - 200) / 50;
409                 if (period % 50)
410                         sti->sti_reg_synch ++;
411                 sti->sti_reg_synch |= SSCTL_SYNCHEN;
412         }
413         else if (period >= 100)
414         {
415                 sti->sti_reg_synch = (period - 100) / 50;
416                 if (period % 50)
417                         sti->sti_reg_synch ++;
418                 sti->sti_reg_synch |= SSCTL_SYNCHEN | SSCTL_FSYNCHEN;
419         }
420         bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
421         return 0;
422 }
423
424 /**************************************************************
425  * General probe attach
426  **************************************************************/
427 int
428 stgprobesubr(iot, ioh, dvcfg)
429         bus_space_tag_t iot;
430         bus_space_handle_t ioh;
431         u_int dvcfg;
432 {
433         u_int16_t lsb, msb;
434
435         lsb = bus_space_read_1(iot, ioh, tmc_idlsb);
436         msb = bus_space_read_1(iot, ioh, tmc_idmsb);
437         switch (msb << 8 | lsb)
438         {
439                 default:
440                         return 0;
441                 case 0x6127:
442                         /* not support! */
443                         return 0;
444                 case 0x60e9:
445                         return 1;
446         }
447         return 0;
448 }
449
450 int
451 stgprint(aux, name)
452         void *aux;
453         const char *name;
454 {
455
456         if (name != NULL)
457                 printf("%s: scsibus ", name);
458         return UNCONF;
459 }
460
461 void
462 stgattachsubr(sc)
463         struct stg_softc *sc;
464 {
465         struct scsi_low_softc *slp = &sc->sc_sclow;
466
467         printf("\n");
468
469         sc->sc_idbit = (1 << slp->sl_hostid); 
470         slp->sl_funcs = &stgfuncs;
471         sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
472
473         slp->sl_flags |= HW_READ_PADDING;
474         slp->sl_cfgflags |= CFG_ASYNC;  /* XXX */
475
476         (void) scsi_low_attach(slp, 0, STG_NTARGETS, STG_NLUNS,
477                                 sizeof(struct stg_targ_info), 0);
478 }
479
480 /**************************************************************
481  * PDMA functions
482  **************************************************************/
483 static __inline void
484 stg_pdma_end(sc, ti)
485         struct stg_softc *sc;
486         struct targ_info *ti;
487 {
488         struct scsi_low_softc *slp = &sc->sc_sclow;
489         bus_space_tag_t iot = sc->sc_iot;
490         bus_space_handle_t ioh = sc->sc_ioh;
491         struct slccb *cb = slp->sl_Qnexus;
492         u_int len, tres;
493
494         slp->sl_flags &= ~HW_PDMASTART;
495         sc->sc_icinit &= ~ICTL_FIFO;
496         sc->sc_dataout_timeout = 0;
497
498         if (cb == NULL)
499         {
500                 slp->sl_error |= PDMAERR;
501                 goto out;
502         }
503
504         if (ti->ti_phase == PH_DATA)
505         {
506                 len = bus_space_read_2(iot, ioh, tmc_fdcnt);
507                 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
508                 {
509                         if (len != 0)
510                         {
511                                 tres = len + slp->sl_scp.scp_datalen;
512                                 if (tres <= (u_int) cb->ccb_scp.scp_datalen)
513                                 {
514                                         slp->sl_scp.scp_data -= len;
515                                         slp->sl_scp.scp_datalen = tres;
516                                 }
517                                 else
518                                 {
519                                         slp->sl_error |= PDMAERR;
520                                         printf("%s len %x >= datalen %x\n",
521                                                 slp->sl_xname,
522                                                 len, slp->sl_scp.scp_datalen);
523                                 }
524                         }
525                 }
526                 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
527                 {
528                         if (len != 0)
529                         {
530                                 slp->sl_error |= PDMAERR;
531                                 printf("%s: len %x left in fifo\n",
532                                         slp->sl_xname, len);
533                         }
534                 }
535                 scsi_low_data_finish(slp);
536         }
537         else
538         {
539
540                 printf("%s data phase miss\n", slp->sl_xname);
541                 slp->sl_error |= PDMAERR;
542         }
543
544 out:
545         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
546 }
547
548 static void
549 stg_pio_read(sc, ti, thold)
550         struct stg_softc *sc;
551         struct targ_info *ti;
552         u_int thold;
553 {
554         struct scsi_low_softc *slp = &sc->sc_sclow;
555         bus_space_tag_t iot = sc->sc_iot;
556         bus_space_handle_t ioh = sc->sc_ioh;
557         struct sc_p *sp = &slp->sl_scp;
558         int tout;
559         u_int res;
560         u_int8_t stat;
561
562         if ((slp->sl_flags & HW_PDMASTART) == 0)
563         {
564                 bus_space_write_1(iot, ioh, tmc_fctl,
565                                   sc->sc_fcRinit | FCTL_FIFOEN);
566                 slp->sl_flags |= HW_PDMASTART;
567         }
568
569         tout = sc->sc_tmaxcnt;
570         while (tout -- > 0)
571         {
572                 if (thold > 0)
573                 {
574                         crit_enter();
575                         res = bus_space_read_2(iot, ioh, tmc_fdcnt);
576                         if (res < thold)
577                         {
578                                 bus_space_write_1(iot, ioh, tmc_ictl,
579                                                   sc->sc_icinit);
580                                 crit_exit();
581                                 break;
582                         }
583                         crit_exit();
584                 }
585                 else
586                 {
587                         stat = bus_space_read_1(iot, ioh, tmc_bstat);
588                         res = bus_space_read_2(iot, ioh, tmc_fdcnt);
589                         if (res == 0)
590                         {
591                                 if ((stat & PHASE_MASK) != DATA_IN_PHASE)
592                                         break;
593                                 if (sp->scp_datalen <= 0)
594                                         break;
595                                 SCSI_LOW_DELAY(1);
596                                 continue;
597                         }
598                 }
599
600                 /* The assumtion res != 0 is valid here */
601                 if (res > sp->scp_datalen)
602                 {
603                         if (res == (u_int) -1)
604                                 break;
605
606                         slp->sl_error |= PDMAERR;
607                         if ((slp->sl_flags & HW_READ_PADDING) == 0)
608                         {
609                                 printf("%s: read padding required\n",
610                                         slp->sl_xname);
611                                 break;
612                         }
613
614                         sp->scp_datalen = 0;
615                         if (res > STG_MAX_DATA_SIZE)
616                                 res = STG_MAX_DATA_SIZE;
617                         while (res -- > 0)
618                         {
619                                 (void) bus_space_read_1(iot, ioh, tmc_rfifo);
620                         }
621                         continue;
622                 }
623
624                 sp->scp_datalen -= res;
625                 if (res & 1)
626                 {
627                         *sp->scp_data = bus_space_read_1(iot, ioh, tmc_rfifo);
628                         sp->scp_data ++;
629                         res --;
630                 }
631
632                 bus_space_read_multi_2(iot, ioh, tmc_rfifo,
633                                        (u_int16_t *) sp->scp_data, res >> 1);
634                 sp->scp_data += res;
635         }
636
637         if (tout <= 0)
638                 printf("%s: pio read timeout\n", slp->sl_xname);
639 }
640
641 static void
642 stg_pio_write(sc, ti, thold)
643         struct stg_softc *sc;
644         struct targ_info *ti;
645         u_int thold;
646 {
647         struct scsi_low_softc *slp = &sc->sc_sclow;
648         bus_space_tag_t iot = sc->sc_iot;
649         bus_space_handle_t ioh = sc->sc_ioh;
650         struct sc_p *sp = &slp->sl_scp;
651         u_int res;
652         int s, tout;
653         u_int8_t stat;
654
655         if ((slp->sl_flags & HW_PDMASTART) == 0)
656         {
657                 stat = sc->sc_fcWinit | FCTL_FIFOEN | FCTL_FIFOW;
658                 bus_space_write_1(iot, ioh, tmc_fctl, stat | FCTL_CLRFIFO);
659                 bus_space_write_1(iot, ioh, tmc_fctl, stat);
660                 slp->sl_flags |= HW_PDMASTART;
661         }
662
663         tout = sc->sc_tmaxcnt;
664         while (tout -- > 0)
665         {
666                 stat = bus_space_read_1(iot, ioh, tmc_bstat);
667                 if ((stat & PHASE_MASK) != DATA_OUT_PHASE)
668                         break;
669
670                 if (sp->scp_datalen <= 0)
671                 {
672                         if (sc->sc_dataout_timeout == 0)
673                                 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
674                         break;
675                 }
676
677                 if (thold > 0)
678                 {
679                         crit_enter();
680                         res = bus_space_read_2(iot, ioh, tmc_fdcnt);
681                         if (res > thold)
682                         {
683                                 bus_space_write_1(iot, ioh, tmc_ictl,
684                                                   sc->sc_icinit);
685                                 crit_exit();
686                                 break;
687                         }
688                         crit_exit();
689                 }
690                 else
691                 {
692                         res = bus_space_read_2(iot, ioh, tmc_fdcnt);
693                         if (res > sc->sc_maxwsize / 2)
694                         {
695                                 SCSI_LOW_DELAY(1);
696                                 continue;
697                         }
698                 }
699                         
700                 if (res == (u_int) -1)
701                         break;
702                 res = sc->sc_maxwsize - res;
703                 if (res > sp->scp_datalen)
704                         res = sp->scp_datalen;
705
706                 sp->scp_datalen -= res;
707                 if ((res & 0x1) != 0)
708                 {
709                         bus_space_write_1(iot, ioh, tmc_wfifo, *sp->scp_data);
710                         sp->scp_data ++;
711                         res --;
712                 }
713
714                 bus_space_write_multi_2(iot, ioh, tmc_wfifo, 
715                                         (u_int16_t *) sp->scp_data, res >> 1);
716                 sp->scp_data += res;
717         }
718
719         if (tout <= 0)
720                 printf("%s: pio write timeout\n", slp->sl_xname);
721 }
722
723 static int
724 stg_negate_signal(sc, mask, s)
725         struct stg_softc *sc;
726         u_int8_t mask;
727         u_char *s;
728 {
729         struct scsi_low_softc *slp = &sc->sc_sclow;
730         bus_space_tag_t bst = sc->sc_iot;
731         bus_space_handle_t bsh = sc->sc_ioh;
732         int wc;
733         u_int8_t regv;
734
735         for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
736         {
737                 regv = bus_space_read_1(bst, bsh, tmc_bstat);
738                 if (regv == (u_int8_t) -1)
739                         return -1;
740                 if ((regv & mask) == 0)
741                         return 1;
742
743                 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
744         }
745
746         printf("%s: %s stg_negate_signal timeout\n", slp->sl_xname, s);
747         return -1;
748 }
749
750 static int
751 stg_expect_signal(sc, phase, mask)
752         struct stg_softc *sc;
753         u_int8_t phase, mask;
754 {
755         struct scsi_low_softc *slp = &sc->sc_sclow;
756         bus_space_tag_t bst = sc->sc_iot;
757         bus_space_handle_t bsh = sc->sc_ioh;
758         int wc;
759         u_int8_t ph;
760
761         phase &= PHASE_MASK;
762         for (wc = 0; wc < STG_DELAY_MAX / STG_DELAY_INTERVAL; wc ++)
763         {
764                 ph = bus_space_read_1(bst, bsh, tmc_bstat);
765                 if (ph == (u_int8_t) -1)
766                         return -1;
767                 if ((ph & PHASE_MASK) != phase)
768                         return 0;
769                 if ((ph & mask) != 0)
770                         return 1;
771
772                 SCSI_LOW_DELAY(STG_DELAY_INTERVAL);
773         }
774
775         printf("%s: stg_expect_signal timeout\n", slp->sl_xname);
776         return -1;
777 }
778
779 static int
780 stg_xfer(sc, buf, len, phase, clear_atn)
781         struct stg_softc *sc;
782         u_int8_t *buf;
783         int len;
784         int phase;
785         int clear_atn;
786 {
787         bus_space_tag_t iot = sc->sc_iot;
788         bus_space_handle_t ioh = sc->sc_ioh;
789         int rv, ptr;
790
791         if (phase & BSTAT_IO)
792                 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
793         else
794                 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
795
796         for (ptr = 0; len > 0; len --)
797         {
798                 rv = stg_expect_signal(sc, phase, BSTAT_REQ);
799                 if (rv <= 0)
800                         goto bad;
801
802                 if (len == 1 && clear_atn != 0)
803                 {
804                         sc->sc_busc &= ~BCTL_ATN;
805                         stghw_bcr_write_1(sc, sc->sc_busc);
806                         SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
807                 }
808
809                 if (phase & BSTAT_IO)
810                 {
811                         buf[ptr ++] = bus_space_read_1(iot, ioh, tmc_rdata);
812                 }
813                 else
814                 {
815                         bus_space_write_1(iot, ioh, tmc_wdata, buf[ptr ++]);
816                 }
817
818                 stg_negate_signal(sc, BSTAT_ACK, "xfer<ACK>");
819         }
820
821 bad:
822         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
823         return len;
824 }
825
826 /**************************************************************
827  * disconnect & reselect (HW low)
828  **************************************************************/
829 static int
830 stg_reselected(sc)
831         struct stg_softc *sc;
832 {
833         struct scsi_low_softc *slp = &sc->sc_sclow;
834         bus_space_tag_t iot = sc->sc_iot;
835         bus_space_handle_t ioh = sc->sc_ioh;
836         int tout;
837         u_int sid;
838         u_int8_t regv;
839
840         if (slp->sl_selid != NULL)
841         {
842                 /* XXX:
843                  * Selection vs Reselection conflicts.
844                  */
845                 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
846                 stghw_bcr_write_1(sc, BCTL_BUSFREE);
847         }
848         else if (slp->sl_Tnexus != NULL)
849         {
850                 printf("%s: unexpected termination\n", slp->sl_xname);
851                 stg_disconnected(sc, slp->sl_Tnexus);
852         }
853
854         /* XXX:
855          * We should ack the reselection as soon as possible,
856          * because the target would abort the current reselection seq 
857          * due to reselection timeout.
858          */
859         tout = STG_DELAY_SELECT_POLLING_MAX;
860         while (tout -- > 0)
861         {
862                 regv = bus_space_read_1(iot, ioh, tmc_bstat);
863                 if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) == 
864                             (BSTAT_IO | BSTAT_SEL))
865                 {
866                         SCSI_LOW_DELAY(1);
867                         regv = bus_space_read_1(iot, ioh, tmc_bstat);
868                         if ((regv & (BSTAT_IO | BSTAT_SEL | BSTAT_BSY)) == 
869                                     (BSTAT_IO | BSTAT_SEL))
870                                 goto reselect_start;
871                 }
872                 SCSI_LOW_DELAY(1);
873         }
874         printf("%s: reselction timeout I\n", slp->sl_xname);
875         return EJUSTRETURN;
876         
877 reselect_start:
878         sid = (u_int) bus_space_read_1(iot, ioh, tmc_scsiid);
879         if ((sid & sc->sc_idbit) == 0)
880         {
881                 /* not us */
882                 return EJUSTRETURN;
883         }
884
885         bus_space_write_1(iot, ioh, tmc_fctl, 
886                             sc->sc_fcRinit | FCTL_CLRFIFO | FCTL_CLRINT);
887         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
888         stghw_bcr_write_1(sc, sc->sc_busc | BCTL_BSY);
889
890         while (tout -- > 0)
891         {
892                 regv = bus_space_read_1(iot, ioh, tmc_bstat);
893                 if ((regv & (BSTAT_SEL | BSTAT_BSY)) == BSTAT_BSY)
894                         goto reselected;
895                 SCSI_LOW_DELAY(1);
896         }
897         printf("%s: reselction timeout II\n", slp->sl_xname);
898         return EJUSTRETURN;
899
900 reselected:
901         sid &= ~sc->sc_idbit;
902         sid = ffs(sid) - 1;
903         if (scsi_low_reselected(slp, sid) == NULL)
904                 return EJUSTRETURN;
905
906 #ifdef  STG_STATICS
907         stg_statics.reselect ++;
908 #endif  /* STG_STATICS */
909         return EJUSTRETURN;
910 }
911
912 static int
913 stg_disconnected(sc, ti)
914         struct stg_softc *sc;
915         struct targ_info *ti;
916 {
917         struct scsi_low_softc *slp = &sc->sc_sclow;
918         bus_space_tag_t iot = sc->sc_iot;
919         bus_space_handle_t ioh = sc->sc_ioh;
920
921         /* clear bus status & fifo */
922         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
923         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
924         stghw_bcr_write_1(sc, BCTL_BUSFREE);
925         sc->sc_icinit &= ~ICTL_FIFO;
926         sc->sc_busc &= ~BCTL_ATN;
927         sc->sc_dataout_timeout = 0;
928         sc->sc_ubf_timeout = 0;
929
930 #ifdef  STG_STATICS
931         stg_statics.disconnect ++;
932 #endif  /* STG_STATICS */
933         scsi_low_disconnected(slp, ti);
934         return 1;
935 }
936
937 /**************************************************************
938  * SEQUENCER
939  **************************************************************/
940 static int
941 stg_target_nexus_establish(sc)
942         struct stg_softc *sc;
943 {
944         struct scsi_low_softc *slp = &sc->sc_sclow;
945         bus_space_tag_t iot = sc->sc_iot;
946         bus_space_handle_t ioh = sc->sc_ioh;
947         struct targ_info *ti = slp->sl_Tnexus;
948         struct stg_targ_info *sti = (void *) ti;
949
950         bus_space_write_1(iot, ioh, tmc_ssctl, sti->sti_reg_synch);
951         if ((stg_io_control & STG_FIFO_INTERRUPTS) != 0)
952         {
953                 sc->sc_icinit |= ICTL_FIFO;
954         }
955         return 0;
956 }
957
958 static int
959 stg_lun_nexus_establish(sc)
960         struct stg_softc *sc;
961 {
962
963         return 0;
964 }
965
966 static int
967 stg_ccb_nexus_establish(sc)
968         struct stg_softc *sc;
969 {
970         struct scsi_low_softc *slp = &sc->sc_sclow;
971         struct slccb *cb = slp->sl_Qnexus;
972
973         sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
974         return 0;
975 }
976
977 #define STGHW_SELECT_INTERVAL   10
978
979 static int
980 stghw_select_targ_wait(sc, mu)
981         struct stg_softc *sc;
982         int mu;
983 {
984         bus_space_tag_t iot = sc->sc_iot;
985         bus_space_handle_t ioh = sc->sc_ioh;
986
987         mu = mu / STGHW_SELECT_INTERVAL;
988         while (mu -- > 0)
989         {
990                 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) == 0)
991                 {
992                         SCSI_LOW_DELAY(STGHW_SELECT_INTERVAL);
993                         continue;
994                 }
995                 SCSI_LOW_DELAY(1);
996                 if ((bus_space_read_1(iot, ioh, tmc_bstat) & BSTAT_BSY) != 0)
997                 {
998                         return 0;
999                 }
1000         }
1001         return ENXIO;
1002 }
1003
1004 static void
1005 stg_selection_done_and_expect_msgout(sc)
1006         struct stg_softc *sc;
1007 {
1008         struct scsi_low_softc *slp = &sc->sc_sclow;
1009         bus_space_tag_t iot = sc->sc_iot;
1010         bus_space_handle_t ioh = sc->sc_ioh;
1011
1012         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit | FCTL_CLRFIFO);
1013         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1014         stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc);
1015         SCSI_LOW_ASSERT_ATN(slp);
1016 }
1017
1018 int
1019 stgintr(arg)
1020         void *arg;
1021 {
1022         struct stg_softc *sc = arg;
1023         struct scsi_low_softc *slp = &sc->sc_sclow;
1024         bus_space_tag_t iot = sc->sc_iot;
1025         bus_space_handle_t ioh = sc->sc_ioh;
1026         struct targ_info *ti;
1027         struct physio_proc *pp;
1028         struct buf *bp;
1029         u_int derror, flags;
1030         int len;
1031         u_int8_t status, astatus, regv;
1032
1033         /*******************************************
1034          * interrupt check
1035          *******************************************/
1036         if (slp->sl_flags & HW_INACTIVE)
1037                 return 0;
1038
1039         astatus = bus_space_read_1(iot, ioh, tmc_astat);
1040         status = bus_space_read_1(iot, ioh, tmc_bstat);
1041
1042         if ((astatus & ASTAT_STATMASK) == 0 || astatus == (u_int8_t) -1)
1043                 return 0;
1044
1045         bus_space_write_1(iot, ioh, tmc_ictl, 0);
1046         if (astatus & ASTAT_SCSIRST)
1047         {
1048                 bus_space_write_1(iot, ioh, tmc_fctl,
1049                                   sc->sc_fcRinit | FCTL_CLRFIFO);
1050                 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1051                 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1052
1053                 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT, 
1054                                  "bus reset (power off?)");
1055                 return 1;
1056         }
1057
1058         /*******************************************
1059          * debug section
1060          *******************************************/
1061 #ifdef  STG_DEBUG
1062         if (stg_debug)
1063         {
1064                 scsi_low_print(slp, NULL);
1065                 printf("%s: st %x ist %x\n\n", slp->sl_xname,
1066                        status, astatus);
1067 #ifdef  DDB
1068                 if (stg_debug > 1)
1069                         SCSI_LOW_DEBUGGER("stg");
1070 #endif  /* DDB */
1071         }
1072 #endif  /* STG_DEBUG */
1073
1074         /*******************************************
1075          * reselection & nexus
1076          *******************************************/
1077         if ((status & RESEL_PHASE_MASK)== PHASE_RESELECTED)
1078         {
1079                 if (stg_reselected(sc) == EJUSTRETURN)
1080                         goto out;
1081         }
1082
1083         if ((ti = slp->sl_Tnexus) == NULL)
1084                 return 0;
1085
1086         derror = 0;
1087         if ((astatus & ASTAT_PARERR) != 0 && ti->ti_phase != PH_ARBSTART &&
1088             (sc->sc_fcRinit & FCTL_PARENB) != 0)
1089         {
1090                 slp->sl_error |= PARITYERR;
1091                 derror = SCSI_LOW_DATA_PE;
1092                 if ((status & PHASE_MASK) == MESSAGE_IN_PHASE)
1093                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_PARITY, 0);
1094                 else
1095                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ERROR, 1);
1096         }
1097
1098         /*******************************************
1099          * aribitration & selection
1100          *******************************************/
1101         switch (ti->ti_phase)
1102         {
1103         case PH_ARBSTART:
1104                 if ((astatus & ASTAT_ARBIT) == 0)
1105                 {
1106 #ifdef  STG_STATICS
1107                         stg_statics.arbit_fail_0 ++;
1108 #endif  /* STG_STATICS */
1109                         goto arb_fail;
1110                 }
1111
1112                 status = bus_space_read_1(iot, ioh, tmc_bstat);
1113                 if ((status & BSTAT_IO) != 0)
1114                 {
1115                         /* XXX:
1116                          * Selection vs Reselection conflicts.
1117                          */
1118 #ifdef  STG_STATICS
1119                         stg_statics.arbit_fail_1 ++;
1120 #endif  /* STG_STATICS */
1121 arb_fail:
1122                         bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1123                         stghw_bcr_write_1(sc, BCTL_BUSFREE);
1124                         scsi_low_arbit_fail(slp, slp->sl_Qnexus);
1125                         goto out;
1126                 }
1127
1128                 /*
1129                  * selection assert start.
1130                  */
1131                 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
1132                 scsi_low_arbit_win(slp);
1133
1134                 crit_enter();
1135                 bus_space_write_1(iot, ioh, tmc_scsiid,
1136                                   sc->sc_idbit | (1 << ti->ti_id));
1137                 stghw_bcr_write_1(sc, sc->sc_imsg | sc->sc_busc | BCTL_SEL);
1138                 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcWinit);
1139                 if ((stg_io_control & STG_WAIT_FOR_SELECT) != 0)
1140                 {
1141                         /* selection abort delay 200 + 100 micro sec */
1142                         if (stghw_select_targ_wait(sc, 300) == 0)
1143                         {
1144                                 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1145                                 stg_selection_done_and_expect_msgout(sc);
1146                         }       
1147                 }
1148                 crit_exit();
1149                 goto out;
1150
1151         case PH_SELSTART:
1152                 if ((status & BSTAT_BSY) == 0)
1153                 {
1154                         /* selection timeout delay 250 ms */
1155                         if (stghw_select_targ_wait(sc, 250 * 1000) != 0)
1156                         {
1157                                 stg_disconnected(sc, ti);
1158                                 goto out;
1159                         }
1160                 }
1161
1162                 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1163                 stg_selection_done_and_expect_msgout(sc);
1164                 goto out;
1165
1166         case PH_SELECTED:
1167                 if ((status & BSTAT_REQ) == 0)
1168                         goto out;
1169                 stg_target_nexus_establish(sc);
1170                 break;
1171
1172         case PH_RESEL:
1173                 if ((status & BSTAT_REQ) == 0)
1174                         goto out;
1175
1176                 /* clear a busy line */
1177                 bus_space_write_1(iot, ioh, tmc_fctl, sc->sc_fcRinit);
1178                 stghw_bcr_write_1(sc, sc->sc_busc);
1179                 stg_target_nexus_establish(sc);
1180                 if ((status & PHASE_MASK) != MESSAGE_IN_PHASE)
1181                 {
1182                         printf("%s: unexpected phase after reselect\n",
1183                                slp->sl_xname);
1184                         slp->sl_error |= FATALIO;
1185                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1186                         goto out;
1187                 }
1188                 break;
1189         }
1190
1191         /*******************************************
1192          * data phase
1193          *******************************************/
1194         if ((slp->sl_flags & HW_PDMASTART) && STG_IS_PHASE_DATA(status) == 0)
1195         {
1196                 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1197                         stg_pio_read(sc, ti, 0);
1198
1199                 stg_pdma_end(sc, ti);
1200         }
1201
1202         /*******************************************
1203          * scsi seq
1204          *******************************************/
1205         switch (status & PHASE_MASK)
1206         {
1207         case COMMAND_PHASE:
1208                 if (stg_expect_signal(sc, COMMAND_PHASE, BSTAT_REQ) <= 0)
1209                         break;
1210
1211                 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1212                 if (scsi_low_cmd(slp, ti) != 0)
1213                 {
1214                         scsi_low_attention(slp);
1215                 }
1216
1217                 if (stg_xfer(sc, slp->sl_scp.scp_cmd, slp->sl_scp.scp_cmdlen,
1218                              COMMAND_PHASE, 0) != 0)
1219                 {
1220                         printf("%s: CMDOUT short\n", slp->sl_xname);
1221                 }
1222                 break;
1223
1224         case DATA_OUT_PHASE:
1225                 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1226                 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1227                 {
1228                         scsi_low_attention(slp);
1229                 }
1230
1231                 pp = physio_proc_enter(bp);
1232                 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1233                         stg_pio_write(sc, ti, sc->sc_wthold);
1234                 else
1235                         stg_pio_write(sc, ti, 0);
1236                 physio_proc_leave(pp);
1237                 break;
1238
1239         case DATA_IN_PHASE:
1240                 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1241                 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1242                 {
1243                         scsi_low_attention(slp);
1244                 }
1245
1246                 pp = physio_proc_enter(bp);
1247                 if ((sc->sc_icinit & ICTL_FIFO) != 0)
1248                         stg_pio_read(sc, ti, sc->sc_rthold);
1249                 else
1250                         stg_pio_read(sc, ti, 0);
1251                 physio_proc_leave(pp);
1252                 break;
1253
1254         case STATUS_PHASE:
1255                 regv = stg_expect_signal(sc, STATUS_PHASE, BSTAT_REQ);
1256                 if (regv <= 0)
1257                         break;
1258
1259                 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1260                 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1261                 if (scsi_low_statusin(slp, ti, regv | derror) != 0)
1262                 {
1263                         scsi_low_attention(slp);
1264                 }
1265                 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1266                 {
1267                         printf("%s: STATIN: data mismatch\n", slp->sl_xname);
1268                 }
1269                 stg_negate_signal(sc, BSTAT_ACK, "statin<ACK>");
1270                 break;
1271
1272         case MESSAGE_OUT_PHASE:
1273                 if (stg_expect_signal(sc, MESSAGE_OUT_PHASE, BSTAT_REQ) <= 0)
1274                         break;
1275
1276                 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1277                 flags = (ti->ti_ophase != ti->ti_phase) ? 
1278                                 SCSI_LOW_MSGOUT_INIT : 0;
1279                 len = scsi_low_msgout(slp, ti, flags);
1280
1281                 if (len > 1 && slp->sl_atten == 0)
1282                 {
1283                         scsi_low_attention(slp);
1284                 }
1285
1286                 if (stg_xfer(sc, ti->ti_msgoutstr, len, MESSAGE_OUT_PHASE,
1287                              slp->sl_clear_atten) != 0)
1288                 {
1289                         printf("%s: MSGOUT short\n", slp->sl_xname);
1290                 }
1291                 else
1292                 {
1293                         if (slp->sl_msgphase >= MSGPH_ABORT) 
1294                         {
1295                                 stg_disconnected(sc, ti);
1296                         }
1297                 }
1298                 break;
1299
1300         case MESSAGE_IN_PHASE:
1301                 /* confirm phase and req signal */
1302                 if (stg_expect_signal(sc, MESSAGE_IN_PHASE, BSTAT_REQ) <= 0)
1303                         break;
1304
1305                 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1306
1307                 /* read data with NOACK */
1308                 regv = bus_space_read_1(iot, ioh, tmc_sdna);
1309
1310                 if (scsi_low_msgin(slp, ti, derror | regv) == 0)
1311                 {
1312                         if (scsi_low_is_msgout_continue(ti, 0) != 0)
1313                         {
1314                                 scsi_low_attention(slp);
1315                         }
1316                 }
1317
1318                 /* read data with ACK */
1319                 if (regv != bus_space_read_1(iot, ioh, tmc_rdata))
1320                 {
1321                         printf("%s: MSGIN: data mismatch\n", slp->sl_xname);
1322                 }
1323
1324                 /* wait for the ack negated */
1325                 stg_negate_signal(sc, BSTAT_ACK, "msgin<ACK>");
1326
1327                 if (slp->sl_msgphase != 0 && slp->sl_msgphase < MSGPH_ABORT)
1328                 {
1329                         stg_disconnected(sc, ti);
1330                 }
1331                 break;
1332
1333         case BUSFREE_PHASE:
1334                 printf("%s: unexpected disconnect\n", slp->sl_xname);
1335                 stg_disconnected(sc, ti);
1336                 break;
1337
1338         default:
1339                 slp->sl_error |= FATALIO;
1340                 printf("%s: unknown phase bus %x intr %x\n",
1341                         slp->sl_xname, status, astatus);
1342                 break;
1343         }
1344
1345 out:
1346         bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1347         return 1;
1348 }
1349
1350 static int
1351 stg_timeout(sc)
1352         struct stg_softc *sc;
1353 {
1354         struct scsi_low_softc *slp = &sc->sc_sclow;
1355         bus_space_tag_t iot = sc->sc_iot;
1356         bus_space_handle_t ioh = sc->sc_ioh;
1357         int tout, count;
1358         u_int8_t status;
1359
1360         if (slp->sl_Tnexus == NULL)
1361                 return 0;
1362
1363         status = bus_space_read_1(iot, ioh, tmc_bstat);
1364         if ((status & PHASE_MASK) == 0)
1365         {
1366                 if (sc->sc_ubf_timeout ++ == 0)
1367                         return 0;
1368
1369                 printf("%s: unexpected bus free detected\n", slp->sl_xname);
1370                 slp->sl_error |= FATALIO;
1371                 scsi_low_print(slp, slp->sl_Tnexus);
1372                 stg_disconnected(sc, slp->sl_Tnexus);
1373                 return 0;
1374         }
1375
1376         switch (status & PHASE_MASK)
1377         {
1378         case DATA_OUT_PHASE:
1379                 if (sc->sc_dataout_timeout == 0)
1380                         break;
1381                 if ((status & BSTAT_REQ) == 0)
1382                         break;
1383                 if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1384                         break;
1385                 if ((-- sc->sc_dataout_timeout) > 0)
1386                         break;  
1387
1388                 slp->sl_error |= PDMAERR;
1389                 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1390                 {
1391                         printf("%s: write padding required\n",
1392                                 slp->sl_xname);
1393                         break;
1394                 }       
1395
1396                 bus_space_write_1(iot, ioh, tmc_ictl, 0);
1397
1398                 tout = STG_DELAY_MAX;
1399                 while (tout --)
1400                 {
1401                         status = bus_space_read_1(iot, ioh, tmc_bstat);
1402                         if ((status & PHASE_MASK) != DATA_OUT_PHASE)
1403                                 break;
1404
1405                         if (bus_space_read_2(iot, ioh, tmc_fdcnt) != 0)
1406                         {
1407                                 SCSI_LOW_DELAY(1);
1408                                 continue;
1409                         }
1410
1411                         for (count = sc->sc_maxwsize; count > 0; count --)
1412                                 bus_space_write_1(iot, ioh, tmc_wfifo, 0);
1413                 }
1414
1415                 status = bus_space_read_1(iot, ioh, tmc_bstat);
1416                 if ((status & PHASE_MASK) == DATA_OUT_PHASE)
1417                         sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1418
1419                 bus_space_write_1(iot, ioh, tmc_ictl, sc->sc_icinit);
1420                 break;
1421
1422         default:
1423                 break;
1424         }
1425         return 0;
1426 }