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