* Fix some cases where NULL was used but 0 was meant (and vice versa).
[games.git] / sys / dev / disk / nsp / nsp.c
1 /*      $FreeBSD: src/sys/dev/nsp/nsp.c,v 1.1.2.6 2001/12/17 13:30:18 non Exp $ */
2 /*      $DragonFly: src/sys/dev/disk/nsp/nsp.c,v 1.13 2008/06/05 18:06:31 swildner Exp $        */
3 /*      $NecBSD: nsp.c,v 1.21.12.6 2001/06/29 06:27:52 honda Exp $      */
4 /*      $NetBSD$        */
5
6 #define NSP_DEBUG
7 #define NSP_STATICS
8 #define NSP_IO_CONTROL_FLAGS \
9         (NSP_READ_SUSPEND_IO | NSP_WRITE_SUSPEND_IO | \
10          NSP_READ_FIFO_INTERRUPTS | NSP_WRITE_FIFO_INTERRUPTS | \
11          NSP_USE_MEMIO | NSP_WAIT_FOR_SELECT)
12
13 /*
14  *  Copyright (c) 1998, 1999, 2000, 2001
15  *      NetBSD/pc98 porting staff. All rights reserved.
16  *
17  *  Copyright (c) 1998, 1999, 2000, 2001
18  *      Naofumi HONDA. All rights reserved.
19  * 
20  *  Redistribution and use in source and binary forms, with or without
21  *  modification, are permitted provided that the following conditions
22  *  are met:
23  *  1. Redistributions of source code must retain the above copyright
24  *     notice, this list of conditions and the following disclaimer.
25  *  2. Redistributions in binary form must reproduce the above copyright
26  *     notice, this list of conditions and the following disclaimer in the
27  *     documentation and/or other materials provided with the distribution.
28  *  3. The name of the author may not be used to endorse or promote products
29  *     derived from this software without specific prior written permission.
30  * 
31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
35  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  */
43 #include "opt_ddb.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/buf.h>
49 #include <sys/queue.h>
50 #include <sys/malloc.h>
51 #include <sys/errno.h>
52 #include <sys/bus.h>
53 #include <sys/thread2.h>
54
55 #include <machine/clock.h>
56 #include <machine/cpu.h>
57
58 #include <bus/cam/scsi/scsi_low.h>
59 #include "nspreg.h"
60 #include "nspvar.h"
61
62 /***************************************************
63  * USER SETTINGS
64  ***************************************************/
65 /* DEVICE CONFIGURATION FLAGS (MINOR)
66  *
67  * 0x01   DISCONECT OFF
68  * 0x02   PARITY LINE OFF
69  * 0x04   IDENTIFY MSG OFF ( = single lun)
70  * 0x08   SYNC TRANSFER OFF
71  */
72
73 /***************************************************
74  * PARAMS
75  ***************************************************/
76 #define NSP_NTARGETS    8
77 #define NSP_NLUNS       8
78
79 #define NSP_MAX_DATA_SIZE       (64 * 1024)
80 #define NSP_SELTIMEOUT          (200)
81 #define NSP_DELAY_MAX           (2 * 1000 * 1000)
82 #define NSP_DELAY_INTERVAL      (1)
83 #define NSP_TIMER_1MS           (1000 / 51)
84
85 /***************************************************
86  * DEBUG
87  ***************************************************/
88 #ifdef  NSP_DEBUG
89 int nsp_debug;
90 #endif  /* NSP_DEBUG */
91
92 #ifdef  NSP_STATICS
93 struct nsp_statics {
94         int arbit_conflict_1;
95         int arbit_conflict_2;
96         int device_data_write;
97         int device_busy;
98         int disconnect;
99         int reselect;
100         int data_phase_bypass;
101 } nsp_statics;
102 #endif  /* NSP_STATICS */
103
104 /***************************************************
105  * IO control
106  ***************************************************/
107 #define NSP_READ_SUSPEND_IO             0x0001
108 #define NSP_WRITE_SUSPEND_IO            0x0002
109 #define NSP_USE_MEMIO                   0x0004
110 #define NSP_READ_FIFO_INTERRUPTS        0x0010
111 #define NSP_WRITE_FIFO_INTERRUPTS       0x0020
112 #define NSP_WAIT_FOR_SELECT             0x0100
113
114 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
115 int nsp_read_suspend_bytes = DEV_BSIZE;
116 int nsp_write_suspend_bytes = DEV_BSIZE;
117 int nsp_read_interrupt_bytes = 4096;
118 int nsp_write_interrupt_bytes = 4096;
119
120 /***************************************************
121  * DEVICE STRUCTURE
122  ***************************************************/
123 extern struct cfdriver nsp_cd;
124
125 /**************************************************************
126  * DECLARE
127  **************************************************************/
128 #define NSP_FIFO_ON     1
129 #define NSP_FIFO_OFF    0
130 static void nsp_pio_read (struct nsp_softc *, int);
131 static void nsp_pio_write (struct nsp_softc *, int);
132 static int nsp_xfer (struct nsp_softc *, u_int8_t *, int, int, int);
133 static int nsp_msg (struct nsp_softc *, struct targ_info *, u_int);
134 static int nsp_reselected (struct nsp_softc *);
135 static int nsp_disconnected (struct nsp_softc *, struct targ_info *);
136 static void nsp_pdma_end (struct nsp_softc *, struct targ_info *);
137 static void nsphw_init (struct nsp_softc *);
138 static int nsp_target_nexus_establish (struct nsp_softc *);
139 static int nsp_lun_nexus_establish (struct nsp_softc *);
140 static int nsp_ccb_nexus_establish (struct nsp_softc *);
141 static int nsp_world_start (struct nsp_softc *, int);
142 static int nsphw_start_selection (struct nsp_softc *sc, struct slccb *);
143 static void nsphw_bus_reset (struct nsp_softc *);
144 static void nsphw_attention (struct nsp_softc *);
145 static u_int nsp_fifo_count (struct nsp_softc *);
146 static u_int nsp_request_count (struct nsp_softc *);
147 static int nsp_negate_signal (struct nsp_softc *, u_int8_t, u_char *);
148 static int nsp_expect_signal (struct nsp_softc *, u_int8_t, u_int8_t);
149 static void nsp_start_timer (struct nsp_softc *, int);
150 static void nsp_setup_fifo (struct nsp_softc *, int, int, int);
151 static int nsp_targ_init (struct nsp_softc *, struct targ_info *, int);
152 static void nsphw_selection_done_and_expect_msgout (struct nsp_softc *);
153 static void nsp_data_padding (struct nsp_softc *, int, u_int);
154 static int nsp_timeout (struct nsp_softc *);
155 static int nsp_read_fifo (struct nsp_softc *, int);
156 static int nsp_write_fifo (struct nsp_softc *, int);
157 static int nsp_phase_match (struct nsp_softc *, u_int8_t, u_int8_t);
158 static int nsp_wait_interrupt (struct nsp_softc *);
159
160 struct scsi_low_funcs nspfuncs = {
161         SC_LOW_INIT_T nsp_world_start,
162         SC_LOW_BUSRST_T nsphw_bus_reset,
163         SC_LOW_TARG_INIT_T nsp_targ_init,
164         SC_LOW_LUN_INIT_T NULL,
165
166         SC_LOW_SELECT_T nsphw_start_selection,
167         SC_LOW_NEXUS_T nsp_lun_nexus_establish,
168         SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
169
170         SC_LOW_ATTEN_T nsphw_attention,
171         SC_LOW_MSG_T nsp_msg,
172
173         SC_LOW_TIMEOUT_T nsp_timeout,
174         SC_LOW_POLL_T nspintr,
175
176         NULL,
177 };
178
179 /****************************************************
180  * hwfuncs
181  ****************************************************/
182 static __inline u_int8_t nsp_cr_read_1 (bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs);
183 static __inline void nsp_cr_write_1 (bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs, u_int8_t va);
184
185 static __inline u_int8_t
186 nsp_cr_read_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs)
187 {
188         
189         bus_space_write_1(bst, bsh, nsp_idxr, ofs);
190         return bus_space_read_1(bst, bsh, nsp_datar);
191 }
192
193 static __inline void 
194 nsp_cr_write_1(bus_space_tag_t bst, bus_space_handle_t bsh, bus_addr_t ofs,
195                u_int8_t va)
196 {
197
198         bus_space_write_1(bst, bsh, nsp_idxr, ofs);
199         bus_space_write_1(bst, bsh, nsp_datar, va);
200 }
201         
202 static int
203 nsp_expect_signal(struct nsp_softc *sc, u_int8_t curphase, u_int8_t mask)
204 {
205         struct scsi_low_softc *slp = &sc->sc_sclow;
206         bus_space_tag_t bst = sc->sc_iot;
207         bus_space_handle_t bsh = sc->sc_ioh;
208         int wc;
209         u_int8_t ph, isrc;
210
211         for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
212         {
213                 ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
214                 if (ph == (u_int8_t) -1)
215                         return -1;
216
217                 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
218                 if (isrc & IRQSR_SCSI)
219                         return 0;
220
221                 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
222                         return 1;
223
224                 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
225         }
226
227         kprintf("%s: nsp_expect_signal timeout\n", slp->sl_xname);
228         return -1;
229 }
230
231 static void
232 nsphw_init(struct nsp_softc *sc)
233 {
234         bus_space_tag_t bst = sc->sc_iot;
235         bus_space_handle_t bsh = sc->sc_ioh;
236
237         /* block all interrupts */
238         bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
239
240         /* setup SCSI interface */
241         bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_IFSEL);
242
243         nsp_cr_write_1(bst, bsh, NSPR_SCIENR, 0);
244
245         nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
246         nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_iclkdiv);
247
248         nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
249         nsp_cr_write_1(bst, bsh, NSPR_PARITYR, sc->sc_parr);
250         nsp_cr_write_1(bst, bsh, NSPR_PTCLRR,
251                        PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
252
253         /* setup fifo asic */
254         bus_space_write_1(bst, bsh, nsp_ifselr, IFSELR_REGSEL);
255         nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, 0);
256         if ((nsp_cr_read_1(bst, bsh, NSPR_OCR) & OCR_TERMPWRS) == 0)
257                 nsp_cr_write_1(bst, bsh, NSPR_TERMPWRC, TERMPWRC_POWON);
258
259         nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_IO8);
260         nsp_cr_write_1(bst, bsh, NSPR_CLKDIVR, sc->sc_clkdiv);
261         nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
262         nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
263
264         nsp_cr_write_1(bst, bsh, NSPR_SYNCR, 0);
265         nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, 0);
266
267         /* enable interrupts and ack them */
268         nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
269         bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
270
271         nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
272 }
273
274 /****************************************************
275  * scsi low interface
276  ****************************************************/
277 static void
278 nsphw_attention(struct nsp_softc *sc)
279 {
280         bus_space_tag_t bst = sc->sc_iot;
281         bus_space_handle_t bsh = sc->sc_ioh;
282         u_int8_t cr;
283
284         cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR)/*  & ~SCBUSCR_ACK */;
285         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
286         SCSI_LOW_DELAY(10);
287 }
288
289 static void
290 nsphw_bus_reset(struct nsp_softc *sc)
291 {
292         bus_space_tag_t bst = sc->sc_iot;
293         bus_space_handle_t bsh = sc->sc_ioh;
294         int i;
295
296         bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_ALLMASK);
297
298         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_RST);
299         SCSI_LOW_DELAY(100 * 1000);     /* 100ms */
300         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
301         for (i = 0; i < 5; i ++)
302                 (void) nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
303
304         bus_space_write_1(bst, bsh, nsp_irqcr, IRQSR_MASK);
305 }
306
307 static void
308 nsphw_selection_done_and_expect_msgout(struct nsp_softc *sc)
309 {
310         struct scsi_low_softc *slp = &sc->sc_sclow;
311         bus_space_tag_t bst = sc->sc_iot;
312         bus_space_handle_t bsh = sc->sc_ioh;
313
314         /* clear ack counter */
315         sc->sc_cnt = 0;
316         nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
317                         PTCLRR_REQ | PTCLRR_HOST);
318
319         /* deassert sel and assert atten */
320         sc->sc_seltout = 0;
321         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, sc->sc_busc);
322         SCSI_LOW_DELAY(1);
323         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 
324                         sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
325         SCSI_LOW_ASSERT_ATN(slp);
326 }
327
328 static int
329 nsphw_start_selection(struct nsp_softc *sc, struct slccb *cb)
330 {
331         struct scsi_low_softc *slp = &sc->sc_sclow;
332         bus_space_tag_t bst = sc->sc_iot;
333         bus_space_handle_t bsh = sc->sc_ioh;
334         struct targ_info *ti = cb->ti;
335         u_int8_t arbs, ph;
336         int wc;
337
338         wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
339         sc->sc_dataout_timeout = 0;
340
341         /* check bus free */
342         crit_enter();
343         ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
344         if (ph != SCBUSMON_FREE)
345         {
346                 crit_exit();
347 #ifdef  NSP_STATICS
348                 nsp_statics.arbit_conflict_1 ++;
349 #endif  /* NSP_STATICS */
350                 return SCSI_LOW_START_FAIL;
351         }
352
353         /* start arbitration */
354         nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_EXEC);
355         crit_exit();
356
357         SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
358         do 
359         {
360                 /* XXX: what a stupid chip! */
361                 arbs = nsp_cr_read_1(bst, bsh, NSPR_ARBITS);
362                 SCSI_LOW_DELAY(1);
363         } 
364         while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
365
366         if ((arbs & ARBITS_WIN) == 0)
367         {
368                 nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
369 #ifdef  NSP_STATICS
370                 nsp_statics.arbit_conflict_2 ++;
371 #endif  /* NSP_STATICS */
372                 return SCSI_LOW_START_FAIL;
373         }
374
375         /* assert select line */
376         SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
377         scsi_low_arbit_win(slp);
378
379         crit_enter();
380         SCSI_LOW_DELAY(3);
381         nsp_cr_write_1(bst, bsh, NSPR_DATA,
382                        sc->sc_idbit | (1 << ti->ti_id));
383         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
384                         SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
385         SCSI_LOW_DELAY(3);
386         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, SCBUSCR_SEL |
387                         SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
388         nsp_cr_write_1(bst, bsh, NSPR_ARBITS, ARBITS_CLR);
389         SCSI_LOW_DELAY(3);
390         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
391                        SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
392         SCSI_LOW_DELAY(1);
393
394         if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
395         {
396 #define NSP_FIRST_SEL_WAIT      300
397 #define NSP_SEL_CHECK_INTERVAL  10
398
399                 /* wait for a selection response */
400                 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
401                      wc ++)
402                 {
403                         ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
404                         if ((ph & SCBUSMON_BSY) == 0)
405                         {
406                                 SCSI_LOW_DELAY(NSP_SEL_CHECK_INTERVAL);
407                                 continue;
408                         }
409
410                         SCSI_LOW_DELAY(1);
411                         ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
412                         if ((ph & SCBUSMON_BSY) != 0)
413                         {
414                                 nsphw_selection_done_and_expect_msgout(sc);
415                                 crit_exit();
416
417                                 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
418                                 return SCSI_LOW_START_OK;
419                         }
420                 }
421         }
422         crit_exit();
423
424         /* check a selection timeout */
425         nsp_start_timer(sc, NSP_TIMER_1MS);
426         sc->sc_seltout = 1;
427         return SCSI_LOW_START_OK;
428 }
429
430 static int
431 nsp_world_start(struct nsp_softc *sc, int fdone)
432 {
433         struct scsi_low_softc *slp = &sc->sc_sclow;
434
435         sc->sc_cnt = 0;
436         sc->sc_seltout = 0;
437
438         if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
439                 sc->sc_busc = SCBUSCR_ATN;
440         else
441                 sc->sc_busc = 0;
442
443         if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
444                 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
445         else
446                 sc->sc_parr = 0;
447
448         sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
449
450         nsphw_init(sc);
451         scsi_low_bus_reset(slp);
452
453         SOFT_INTR_REQUIRED(slp);
454         return 0;
455 }
456
457 struct ncp_synch_data {
458         u_int min_period;
459         u_int max_period;
460         u_int chip_period;
461         u_int ack_width;
462 };
463
464 static struct ncp_synch_data ncp_sync_data_40M[] = {
465         {0x0c,0x0c,0x1,0},      /* 20MB  50ns*/
466         {0x19,0x19,0x3,1},      /* 10MB  100ns*/ 
467         {0x1a,0x25,0x5,2},      /* 7.5MB 150ns*/ 
468         {0x26,0x32,0x7,3},      /* 5MB   200ns*/
469         {0x0, 0, 0, 0}
470 };
471
472 static struct ncp_synch_data ncp_sync_data_20M[] = {
473         {0x19,0x19,0x1,0},      /* 10MB  100ns*/ 
474         {0x1a,0x25,0x2,0},      /* 7.5MB 150ns*/ 
475         {0x26,0x32,0x3,1},      /* 5MB   200ns*/
476         {0x0, 0, 0, 0}
477 };
478
479 static int
480 nsp_msg(struct nsp_softc *sc, struct targ_info *ti, u_int msg)
481 {
482         bus_space_tag_t bst = sc->sc_iot;
483         bus_space_handle_t bsh = sc->sc_ioh;
484         struct ncp_synch_data *sdp;
485         struct nsp_targ_info *nti = (void *) ti;
486         u_int period, offset;
487         int i, error;
488
489         if ((msg & SCSI_LOW_MSG_WIDE) != 0)
490         {
491                 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
492                 {
493                         ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
494                         return EINVAL;
495                 }
496                 return 0;
497         }
498
499         if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
500                 return 0;
501
502         period = ti->ti_maxsynch.period;
503         offset = ti->ti_maxsynch.offset;
504         if (sc->sc_iclkdiv == CLKDIVR_20M)
505                 sdp = &ncp_sync_data_20M[0];
506         else
507                 sdp = &ncp_sync_data_40M[0];
508
509         for (i = 0; sdp->max_period != 0; i ++, sdp ++)
510         {
511                 if (period >= sdp->min_period && period <= sdp->max_period)
512                         break;
513         }
514
515         if (period != 0 && sdp->max_period == 0)
516         {
517                 /*
518                  * NO proper period/offset found,
519                  * Retry neg with the target.
520                  */
521                 ti->ti_maxsynch.period = 0;
522                 ti->ti_maxsynch.offset = 0;
523                 nti->nti_reg_syncr = 0;
524                 nti->nti_reg_ackwidth = 0;
525                 error = EINVAL;
526         }
527         else
528         {
529                 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
530                                       (offset & SYNCR_OFFM);
531                 nti->nti_reg_ackwidth = sdp->ack_width;
532                 error = 0;
533         }
534
535         nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
536         nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
537         return error;
538 }
539
540 static int
541 nsp_targ_init(struct nsp_softc *sc, struct targ_info *ti, int action)
542 {
543         struct nsp_targ_info *nti = (void *) ti;
544
545         if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
546         {
547                 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
548                 ti->ti_maxsynch.period = 100 / 4;
549                 ti->ti_maxsynch.offset = 15;
550                 nti->nti_reg_syncr = 0;
551                 nti->nti_reg_ackwidth = 0;
552         }
553         return 0;
554 }       
555
556 static void
557 nsp_start_timer(struct nsp_softc *sc, int time)
558 {
559         bus_space_tag_t bst = sc->sc_iot;
560         bus_space_handle_t bsh = sc->sc_ioh;
561
562         sc->sc_timer = time;
563         nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, time);
564 }
565
566 /**************************************************************
567  * General probe attach
568  **************************************************************/
569 int
570 nspprobesubr(bus_space_tag_t iot, bus_space_handle_t ioh, u_int dvcfg)
571 {
572         u_int8_t regv;
573
574         regv = bus_space_read_1(iot, ioh, nsp_fifosr);
575         if (regv < 0x11 || regv >= 0x20)
576                 return 0;
577         return 1;
578 }
579
580 int
581 nspprint(void *aux, const char *name)
582 {
583
584         if (name != NULL)
585                 kprintf("%s: scsibus ", name);
586         return UNCONF;
587 }
588
589 void
590 nspattachsubr(struct nsp_softc *sc)
591 {
592         struct scsi_low_softc *slp = &sc->sc_sclow;
593
594         kprintf("\n");
595
596         sc->sc_idbit = (1 << slp->sl_hostid);
597         slp->sl_flags |= HW_READ_PADDING;
598         slp->sl_funcs = &nspfuncs;
599         sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
600
601         (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
602                                sizeof(struct nsp_targ_info), 0);
603 }
604
605 /**************************************************************
606  * PDMA functions
607  **************************************************************/
608 static u_int
609 nsp_fifo_count(struct nsp_softc *sc)
610 {
611         bus_space_tag_t bst = sc->sc_iot;
612         bus_space_handle_t bsh = sc->sc_ioh;
613         u_int count;
614
615         nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
616         count = bus_space_read_1(bst, bsh, nsp_datar);
617         count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
618         count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
619         return count;
620 }
621
622 static u_int
623 nsp_request_count(struct nsp_softc *sc)
624 {
625         bus_space_tag_t bst = sc->sc_iot;
626         bus_space_handle_t bsh = sc->sc_ioh;
627         u_int count;
628
629         nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
630         count = bus_space_read_1(bst, bsh, nsp_datar);
631         count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 8);
632         count += (((u_int) bus_space_read_1(bst, bsh, nsp_datar)) << 16);
633         return count;
634 }
635
636 static void
637 nsp_setup_fifo(struct nsp_softc *sc, int on, int direction, int datalen)
638 {
639         u_int8_t xfermode;
640
641         sc->sc_suspendio = 0;
642         if (on == NSP_FIFO_OFF)
643         {
644                 xfermode = XFERMR_IO8;
645                 goto out;
646         }
647
648         /* check if suspend io OK ? */
649         if (datalen > 0)
650         {
651                 if (direction == SCSI_LOW_READ)
652                 {
653                         if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
654                             (datalen % nsp_read_suspend_bytes) == 0)
655                                 sc->sc_suspendio = nsp_read_suspend_bytes;
656                 }
657                 else
658                 {
659                         if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
660                             (datalen % nsp_write_suspend_bytes) == 0)
661                                 sc->sc_suspendio = nsp_write_suspend_bytes;
662                 }
663         }
664
665         /* determine a transfer type */
666         if (datalen < DEV_BSIZE || (datalen & 3) != 0)
667         {
668                 if (sc->sc_memh != 0 &&
669                     (nsp_io_control & NSP_USE_MEMIO) != 0)
670                         xfermode = XFERMR_XEN | XFERMR_MEM8;
671                 else
672                         xfermode = XFERMR_XEN | XFERMR_IO8;
673         }
674         else
675         {
676                 if (sc->sc_memh != 0 &&
677                     (nsp_io_control & NSP_USE_MEMIO) != 0)
678                         xfermode = XFERMR_XEN | XFERMR_MEM32;
679                 else
680                         xfermode = XFERMR_XEN | XFERMR_IO32;
681
682                 if (sc->sc_suspendio > 0)
683                         xfermode |= XFERMR_FIFOEN;
684         }
685
686 out:
687         sc->sc_xfermr = xfermode;
688         nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_XFERMR, sc->sc_xfermr);
689 }
690
691 static void
692 nsp_pdma_end(struct nsp_softc *sc, struct targ_info *ti)
693 {
694         struct scsi_low_softc *slp = &sc->sc_sclow;
695         struct slccb *cb = slp->sl_Qnexus;
696         u_int len = 0, cnt;
697
698         sc->sc_dataout_timeout = 0;
699         slp->sl_flags &= ~HW_PDMASTART;
700         nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
701         if ((sc->sc_icr & SCIENR_FIFO) != 0)
702         {
703                 sc->sc_icr &= ~SCIENR_FIFO;
704                 nsp_cr_write_1(sc->sc_iot, sc->sc_ioh, NSPR_SCIENR, sc->sc_icr);
705         }
706
707         if (cb == NULL)
708         {
709                 slp->sl_error |= PDMAERR;
710                 return;
711         }
712
713         if (ti->ti_phase == PH_DATA)
714         {
715                 cnt = nsp_fifo_count(sc);
716                 if (slp->sl_scp.scp_direction  == SCSI_LOW_WRITE)
717                 {
718                         len = sc->sc_cnt - cnt;
719                         if (sc->sc_cnt >= cnt &&
720                             slp->sl_scp.scp_datalen + len <= 
721                             cb->ccb_scp.scp_datalen)
722                         {
723                                 slp->sl_scp.scp_data -= len;
724                                 slp->sl_scp.scp_datalen += len;
725                         }
726                         else
727                         {
728                                 slp->sl_error |= PDMAERR;
729                                 kprintf("%s len %x >= datalen %x\n",
730                                         slp->sl_xname,
731                                         len, slp->sl_scp.scp_datalen);
732                         }
733                 }
734                 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
735                 {
736                         if (sc->sc_cnt != cnt ||
737                             sc->sc_cnt > cb->ccb_scp.scp_datalen)
738                         {
739                                 slp->sl_error |= PDMAERR;
740                                 kprintf("%s: data read count error %x != %x (%x)\n",
741                                         slp->sl_xname, sc->sc_cnt, cnt,
742                                         cb->ccb_scp.scp_datalen);
743                         }
744                 }
745                 sc->sc_cnt = cnt;
746                 scsi_low_data_finish(slp);
747         }
748         else
749         {
750
751                 kprintf("%s data phase miss\n", slp->sl_xname);
752                 slp->sl_error |= PDMAERR;
753         }
754 }
755
756 #define RFIFO_CRIT      64
757 #define WFIFO_CRIT      32
758
759 static void
760 nsp_data_padding(struct nsp_softc *sc, int direction, u_int count)
761 {
762         bus_space_tag_t bst = sc->sc_iot;
763         bus_space_handle_t bsh = sc->sc_ioh;
764
765         if (count > NSP_MAX_DATA_SIZE)
766                 count = NSP_MAX_DATA_SIZE;
767
768         nsp_cr_write_1(bst, bsh, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
769         if (direction == SCSI_LOW_READ)
770         {
771                 while (count -- > 0)
772                         (void) bus_space_read_1(bst, bsh, nsp_fifodr);
773         }
774         else
775         {
776                 while (count -- > 0)
777                         (void) bus_space_write_1(bst, bsh, nsp_fifodr, 0);
778         }
779         nsp_cr_write_1(bst, bsh, NSPR_XFERMR, sc->sc_xfermr);
780 }
781
782 static int
783 nsp_read_fifo(struct nsp_softc *sc, int suspendio)
784 {
785         struct scsi_low_softc *slp = &sc->sc_sclow;
786         bus_space_tag_t bst = sc->sc_iot;
787         bus_space_handle_t bsh = sc->sc_ioh;
788         u_int res;
789
790         res = nsp_fifo_count(sc);
791         if (res == sc->sc_cnt)
792                 return 0;
793
794 #ifdef  NSP_DEBUG
795         if (res < sc->sc_cnt || res == (u_int) -1)
796         {
797                 kprintf("%s: strange fifo ack count 0x%x < 0x%x\n", 
798                         slp->sl_xname, res, sc->sc_cnt);
799                 return 0;
800         }
801 #endif  /* NSP_DEBUG */
802
803         res = res - sc->sc_cnt;
804         if (res > slp->sl_scp.scp_datalen)
805         {
806                 if ((slp->sl_error & PDMAERR) == 0)
807                 {
808                         kprintf("%s: data overrun 0x%x > 0x%x\n",
809                                 slp->sl_xname, res, slp->sl_scp.scp_datalen);
810                 }
811
812                 slp->sl_error |= PDMAERR;
813                 slp->sl_scp.scp_datalen = 0;
814
815                 if ((slp->sl_flags & HW_READ_PADDING) == 0)
816                 {
817                         kprintf("%s: read padding required\n", slp->sl_xname);
818                         return 0;
819                 }
820
821                 nsp_data_padding(sc, SCSI_LOW_READ, res);
822                 sc->sc_cnt += res;
823                 return 1;       /* padding start */
824         }
825
826         if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
827                 res = suspendio;
828
829         if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
830         {
831                 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
832                 {
833                         res &= ~3;
834                         bus_space_read_region_4(sc->sc_memt, sc->sc_memh, 0, 
835                                 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
836                 }
837                 else
838                 {
839                         bus_space_read_region_1(sc->sc_memt, sc->sc_memh, 0, 
840                                 (u_int8_t *) slp->sl_scp.scp_data, res);
841                 }
842         }
843         else
844         {
845                 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
846                 {
847                         res &= ~3;
848                         bus_space_read_multi_4(bst, bsh, nsp_fifodr,
849                                 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
850                 }
851                 else 
852                 {
853                         bus_space_read_multi_1(bst, bsh, nsp_fifodr,
854                                 (u_int8_t *) slp->sl_scp.scp_data, res);
855                 }
856         }
857
858         if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
859         {
860                 nsp_cr_write_1(bst, bsh, NSPR_PARITYR, 
861                                PARITYR_ENABLE | PARITYR_CLEAR);
862                 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
863         }
864
865         slp->sl_scp.scp_data += res;
866         slp->sl_scp.scp_datalen -= res;
867         sc->sc_cnt += res;
868         return 0;
869 }
870
871 static int
872 nsp_write_fifo(struct nsp_softc *sc, int suspendio)
873 {
874         struct scsi_low_softc *slp = &sc->sc_sclow;
875         bus_space_tag_t bst = sc->sc_iot;
876         bus_space_handle_t bsh = sc->sc_ioh;
877         u_int res;
878         u_int8_t stat;
879
880         if (suspendio > 0)
881         {
882 #ifdef  NSP_DEBUG
883                 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
884                 {
885                         kprintf("%s: strange write length 0x%x\n",
886                                 slp->sl_xname, slp->sl_scp.scp_datalen);
887                 }
888 #endif  /* NSP_DEBUG */
889                 res = slp->sl_scp.scp_datalen % suspendio;
890                 if (res == 0)
891                 {
892                         res = suspendio;
893                 }
894         }
895         else
896         {
897                 res = WFIFO_CRIT;
898         }
899
900         if (res > slp->sl_scp.scp_datalen)
901                 res = slp->sl_scp.scp_datalen;
902
903         /* XXX: reconfirm! */
904         stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
905         if (stat != PHASE_DATAOUT)
906                 return 0;
907
908         if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
909         {
910                 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
911                 {
912                         bus_space_write_region_4(sc->sc_memt, sc->sc_memh, 0,
913                                 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
914                 }
915                 else
916                 {
917                         bus_space_write_region_1(sc->sc_memt, sc->sc_memh, 0,
918                                 (u_int8_t *) slp->sl_scp.scp_data, res);
919                 }
920         }
921         else
922         {
923                 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
924                 {
925                         bus_space_write_multi_4(bst, bsh, nsp_fifodr,
926                                 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
927                 }
928                 else
929                 {
930                         bus_space_write_multi_1(bst, bsh, nsp_fifodr,
931                                 (u_int8_t *) slp->sl_scp.scp_data, res);
932                 }
933         }
934
935         slp->sl_scp.scp_datalen -= res;
936         slp->sl_scp.scp_data += res;
937         sc->sc_cnt += res;
938         return 0;
939 }
940
941 static int
942 nsp_wait_interrupt(struct nsp_softc *sc)
943 {
944         bus_space_tag_t bst = sc->sc_iot;
945         bus_space_handle_t bsh = sc->sc_ioh;
946         int tout;
947         u_int8_t isrc;
948
949         for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
950         {
951                 isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
952                 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
953                 {
954                         if ((isrc & IRQSR_FIFO) != 0)
955                         {
956                                 bus_space_write_1(bst, bsh,
957                                         nsp_irqcr, IRQCR_FIFOCL);
958                         }
959                         return 1;
960                 }
961                 SCSI_LOW_DELAY(1);
962         }
963         return 0;
964 }
965
966 static void
967 nsp_pio_read(struct nsp_softc *sc, int suspendio)
968 {
969         struct scsi_low_softc *slp = &sc->sc_sclow;
970         bus_space_tag_t bst = sc->sc_iot;
971         bus_space_handle_t bsh = sc->sc_ioh;
972         int tout, padding, datalen;
973         u_int8_t stat, fstat;
974
975         padding = 0;
976         tout = sc->sc_tmaxcnt;
977         slp->sl_flags |= HW_PDMASTART;
978         datalen = slp->sl_scp.scp_datalen;
979
980 ReadLoop:
981         while (1)
982         {
983                 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
984                 if (stat == (u_int8_t) -1)
985                         return;
986
987                 /* out of data phase */
988                 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
989                 {
990                         nsp_read_fifo(sc, 0);
991                         return;
992                 }
993
994                 /* data phase */
995                 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
996                 if ((fstat & FIFOSR_FULLEMP) != 0)
997                 {
998                         if ((sc->sc_icr & SCIENR_FIFO) != 0)
999                         {
1000                                 bus_space_write_1(bst, bsh, nsp_irqcr, 
1001                                                   IRQCR_FIFOCL);
1002                         }
1003
1004                         if (suspendio > 0)
1005                         {
1006                                 padding |= nsp_read_fifo(sc, suspendio);
1007                         }
1008                         else
1009                         {
1010                                 padding |= nsp_read_fifo(sc, 0);
1011                         }
1012
1013                         if ((sc->sc_icr & SCIENR_FIFO) != 0)
1014                                 break;
1015                 }
1016                 else
1017                 {
1018                         if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1019                                 return;
1020
1021                         if ((sc->sc_icr & SCIENR_FIFO) != 0)
1022                                 break;
1023
1024                         SCSI_LOW_DELAY(1);
1025                 }
1026
1027                 if ((-- tout) <= 0)
1028                 {
1029                         kprintf("%s: nsp_pio_read: timeout\n", slp->sl_xname);
1030                         return;
1031                 }
1032         }
1033
1034
1035         if (slp->sl_scp.scp_datalen > 0 &&
1036             slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1037         {
1038                 if (nsp_wait_interrupt(sc) != 0)
1039                         goto ReadLoop;
1040         }
1041 }
1042
1043 static void
1044 nsp_pio_write(struct nsp_softc *sc, int suspendio)
1045 {
1046         struct scsi_low_softc *slp = &sc->sc_sclow;
1047         bus_space_tag_t bst = sc->sc_iot;
1048         bus_space_handle_t bsh = sc->sc_ioh;
1049         u_int rcount, acount;
1050         int tout, datalen;
1051         u_int8_t stat, fstat;
1052
1053         tout = sc->sc_tmaxcnt;
1054         slp->sl_flags |= HW_PDMASTART;
1055         datalen = slp->sl_scp.scp_datalen;
1056
1057 WriteLoop:
1058         while (1)
1059         {
1060                 stat = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1061                 if (stat != PHASE_DATAOUT)
1062                         return;
1063
1064                 if (slp->sl_scp.scp_datalen <= 0)
1065                 {
1066                         if (sc->sc_dataout_timeout == 0)
1067                                 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1068                         return;
1069                 }
1070
1071                 fstat = bus_space_read_1(bst, bsh, nsp_fifosr);
1072                 if ((fstat & FIFOSR_FULLEMP) != 0)
1073                 {
1074                         if ((sc->sc_icr & SCIENR_FIFO) != 0)
1075                         {
1076                                 bus_space_write_1(bst, bsh, nsp_irqcr,
1077                                                   IRQCR_FIFOCL);
1078                         }
1079
1080                         if (suspendio > 0)
1081                         {
1082                                 /* XXX:IMPORTANT:
1083                                  * To avoid timeout of pcmcia bus
1084                                  * (not scsi bus!), we should check
1085                                  * the scsi device sends us request
1086                                  * signals, which means the scsi device
1087                                  * is ready to recieve data without
1088                                  * heavy delays. 
1089                                  */
1090                                 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1091                                 {
1092                                         /* Step I:
1093                                          * fill the nsp fifo, and waiting for
1094                                          * the fifo empty.
1095                                          */
1096                                         nsp_write_fifo(sc, 0);
1097                                 }
1098                                 else
1099                                 {
1100                                         /* Step II:
1101                                          * check the request singals.
1102                                          */
1103                                         acount = nsp_fifo_count(sc);
1104                                         rcount = nsp_request_count(sc);
1105                                         if (rcount <= acount)
1106                                         {
1107                                                 nsp_write_fifo(sc, 0);
1108 #ifdef  NSP_STATICS
1109                                                 nsp_statics.device_busy ++;
1110 #endif  /* NSP_STATICS */
1111                                         }
1112                                         else
1113                                         {
1114                                                 nsp_write_fifo(sc, suspendio);
1115 #ifdef  NSP_STATICS
1116                                                 nsp_statics.device_data_write ++;
1117 #endif  /* NSP_STATICS */
1118                                         }
1119                                 }
1120                         }
1121                         else
1122                         {
1123                                 nsp_write_fifo(sc, 0);
1124                         }
1125
1126                         if ((sc->sc_icr & SCIENR_FIFO) != 0)
1127                                 break;
1128                 }
1129                 else
1130                 {
1131                         if ((sc->sc_icr & SCIENR_FIFO) != 0)
1132                                 break;
1133
1134                         SCSI_LOW_DELAY(1);
1135                 }
1136
1137                 if ((-- tout) <= 0)
1138                 {
1139                         kprintf("%s: nsp_pio_write: timeout\n", slp->sl_xname);
1140                         return;
1141                 }
1142         }
1143
1144         if (slp->sl_scp.scp_datalen > 0 &&
1145             slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1146         {
1147                 if (nsp_wait_interrupt(sc) != 0)
1148                         goto WriteLoop;
1149         }
1150 }
1151
1152 static int
1153 nsp_negate_signal(struct nsp_softc *sc, u_int8_t mask, u_char *s)
1154 {
1155         struct scsi_low_softc *slp = &sc->sc_sclow;
1156         bus_space_tag_t bst = sc->sc_iot;
1157         bus_space_handle_t bsh = sc->sc_ioh;
1158         int wc;
1159         u_int8_t regv;
1160
1161         for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1162         {
1163                 regv = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1164                 if (regv == (u_int8_t) -1)
1165                         return -1;
1166                 if ((regv & mask) == 0)
1167                         return 1;
1168                 SCSI_LOW_DELAY(NSP_DELAY_INTERVAL);
1169         }
1170
1171         kprintf("%s: %s nsp_negate_signal timeout\n", slp->sl_xname, s);
1172         return -1;
1173 }
1174
1175 static int
1176 nsp_xfer(struct nsp_softc *sc, u_int8_t *buf, int len, int phase,
1177          int clear_atn)
1178 {
1179         bus_space_tag_t bst = sc->sc_iot;
1180         bus_space_handle_t bsh = sc->sc_ioh;
1181         int ptr, rv;
1182
1183         for (ptr = 0; len > 0; len --, ptr ++)
1184         {
1185                 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1186                 if (rv <= 0)
1187                         goto out;
1188
1189                 if (len == 1 && clear_atn != 0)
1190                 {
1191                         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR,
1192                                        SCBUSCR_ADIR | SCBUSCR_ACKEN);
1193                         SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1194                 }
1195
1196                 if (phase & SCBUSMON_IO)
1197                 {
1198                         buf[ptr] = nsp_cr_read_1(bst, bsh, NSPR_DATAACK);
1199                 }
1200                 else
1201                 {
1202                         nsp_cr_write_1(bst, bsh, NSPR_DATAACK, buf[ptr]);
1203                 }
1204                 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1205         }
1206
1207 out:
1208         return len;
1209 }
1210
1211 /**************************************************************
1212  * disconnect & reselect (HW low)
1213  **************************************************************/
1214 static int
1215 nsp_reselected(struct nsp_softc *sc)
1216 {
1217         struct scsi_low_softc *slp = &sc->sc_sclow;
1218         bus_space_tag_t bst = sc->sc_iot;
1219         bus_space_handle_t bsh = sc->sc_ioh;
1220         struct targ_info *ti;
1221         u_int sid;
1222         u_int8_t cr;
1223
1224         sid = (u_int) nsp_cr_read_1(bst, bsh, NSPR_RESELR);
1225         sid &= ~sc->sc_idbit;
1226         sid = ffs(sid) - 1;
1227         if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1228                 return EJUSTRETURN;
1229
1230         nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1231
1232         cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1233         cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1234         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1235         cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1236         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1237
1238 #ifdef  NSP_STATICS
1239         nsp_statics.reselect ++;
1240 #endif  /* NSP_STATCIS */
1241         return EJUSTRETURN;
1242 }
1243
1244 static int
1245 nsp_disconnected(struct nsp_softc *sc, struct targ_info *ti)
1246 {
1247         struct scsi_low_softc *slp = &sc->sc_sclow;
1248         bus_space_tag_t bst = sc->sc_iot;
1249         bus_space_handle_t bsh = sc->sc_ioh;
1250
1251         nsp_cr_write_1(bst, bsh, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1252                         PTCLRR_REQ | PTCLRR_HOST);
1253         if ((sc->sc_icr & SCIENR_FIFO) != 0)
1254         {
1255                 sc->sc_icr &= ~SCIENR_FIFO;
1256                 nsp_cr_write_1(bst, bsh, NSPR_SCIENR, sc->sc_icr);
1257         }
1258         sc->sc_cnt = 0;
1259         sc->sc_dataout_timeout = 0;
1260 #ifdef  NSP_STATICS
1261         nsp_statics.disconnect ++;
1262 #endif  /* NSP_STATICS */
1263         scsi_low_disconnected(slp, ti);
1264         return 1;
1265 }
1266
1267 /**************************************************************
1268  * SEQUENCER
1269  **************************************************************/
1270 static void nsp_error (struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1271
1272 static void
1273 nsp_error(struct nsp_softc *sc, u_char *s, u_int8_t isrc, u_int8_t ph,
1274           u_int8_t irqphs)
1275 {
1276         struct scsi_low_softc *slp = &sc->sc_sclow;
1277
1278         kprintf("%s: %s\n", slp->sl_xname, s);
1279         kprintf("%s: isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1280                slp->sl_xname, (u_int) isrc, (u_int) ph, (u_int) irqphs);
1281 }
1282
1283 static int
1284 nsp_target_nexus_establish(struct nsp_softc *sc)
1285 {
1286         struct scsi_low_softc *slp = &sc->sc_sclow;
1287         bus_space_tag_t bst = sc->sc_iot;
1288         bus_space_handle_t bsh = sc->sc_ioh;
1289         struct targ_info *ti = slp->sl_Tnexus;
1290         struct nsp_targ_info *nti = (void *) ti;
1291
1292         /* setup synch transfer registers */
1293         nsp_cr_write_1(bst, bsh, NSPR_SYNCR, nti->nti_reg_syncr);
1294         nsp_cr_write_1(bst, bsh, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1295
1296         /* setup pdma fifo (minimum) */
1297         nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1298         return 0;
1299 }
1300
1301 static int
1302 nsp_lun_nexus_establish(struct nsp_softc *sc)
1303 {
1304
1305         return 0;
1306 }
1307
1308 static int
1309 nsp_ccb_nexus_establish(struct nsp_softc *sc)
1310 {
1311         struct scsi_low_softc *slp = &sc->sc_sclow;
1312         struct slccb *cb = slp->sl_Qnexus;
1313
1314         sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1315
1316         /* setup pdma fifo */
1317         nsp_setup_fifo(sc, NSP_FIFO_ON, 
1318                        slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1319
1320         if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1321         {
1322                 if (sc->sc_suspendio > 0 &&
1323                     (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1324                 {
1325                         sc->sc_icr |= SCIENR_FIFO;
1326                         nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1327                                        NSPR_SCIENR, sc->sc_icr);
1328                 }
1329         }
1330         else 
1331         {
1332                 if (sc->sc_suspendio > 0 &&
1333                     (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1334                 {
1335                         sc->sc_icr |= SCIENR_FIFO;
1336                         nsp_cr_write_1(sc->sc_iot, sc->sc_ioh,
1337                                        NSPR_SCIENR, sc->sc_icr);
1338                 }
1339         }
1340         return 0;
1341 }
1342
1343 static int
1344 nsp_phase_match(struct nsp_softc *sc, u_int8_t phase, u_int8_t stat)
1345 {
1346         struct scsi_low_softc *slp = &sc->sc_sclow;
1347
1348         if ((stat & SCBUSMON_PHMASK) != phase)
1349         {
1350                 kprintf("%s: phase mismatch 0x%x != 0x%x\n",
1351                         slp->sl_xname, (u_int) phase, (u_int) stat);
1352                 return EINVAL;
1353         }
1354
1355         if ((stat & SCBUSMON_REQ) == 0)
1356                 return EINVAL;
1357
1358         return 0;
1359 }
1360
1361 int
1362 nspintr(void *arg)
1363 {
1364         struct nsp_softc *sc = arg;
1365         struct scsi_low_softc *slp = &sc->sc_sclow;
1366         bus_space_tag_t bst = sc->sc_iot;
1367         bus_space_handle_t bsh = sc->sc_ioh;
1368         struct targ_info *ti;
1369         struct buf *bp;
1370         u_int derror, flags;
1371         int len, rv;
1372         u_int8_t isrc, ph, irqphs, cr, regv;
1373
1374         /*******************************************
1375          * interrupt check
1376          *******************************************/
1377         if (slp->sl_flags & HW_INACTIVE)
1378                 return 0;
1379
1380         bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_IRQDIS);
1381         isrc = bus_space_read_1(bst, bsh, nsp_irqsr);
1382         if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1383         {
1384                 bus_space_write_1(bst, bsh, nsp_irqcr, 0);
1385                 return 0;
1386         }
1387
1388         /* XXX: IMPORTANT
1389          * Do not read an irqphs register if no scsi phase interrupt.
1390          * Unless, you should lose a scsi phase interrupt.
1391          */
1392         ph = nsp_cr_read_1(bst, bsh, NSPR_SCBUSMON);
1393         if ((isrc & IRQSR_SCSI) != 0)
1394         {
1395                 irqphs = nsp_cr_read_1(bst, bsh, NSPR_IRQPHS);
1396         }
1397         else
1398                 irqphs = 0;
1399
1400         /*
1401          * timer interrupt handler (scsi vs timer interrupts)
1402          */
1403         if (sc->sc_timer != 0)
1404         {
1405                 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1406                 nsp_cr_write_1(bst, bsh, NSPR_TIMERCNT, 0);
1407                 sc->sc_timer = 0;
1408         }
1409         
1410         /* check a timer interrupt */
1411         regv = 0;
1412         if ((isrc & IRQSR_TIMER) != 0)
1413         {
1414                 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1415                 {
1416                         bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_TIMERCL);
1417                         return 1;
1418                 }
1419                 regv |= IRQCR_TIMERCL;
1420         }
1421
1422         /* check a fifo interrupt */
1423         if ((isrc & IRQSR_FIFO) != 0)
1424         {
1425                 regv |= IRQCR_FIFOCL;
1426         }
1427
1428         /* OK. enable all interrupts */
1429         bus_space_write_1(bst, bsh, nsp_irqcr, regv);
1430
1431         /*******************************************
1432          * debug section
1433          *******************************************/
1434 #ifdef  NSP_DEBUG
1435         if (nsp_debug)
1436         {
1437                 nsp_error(sc, "current status", isrc, ph, irqphs);
1438                 scsi_low_print(slp, NULL);
1439 #ifdef  DDB
1440                 if (nsp_debug > 1)
1441                         SCSI_LOW_DEBUGGER("nsp");
1442 #endif  /* DDB */
1443         }
1444 #endif  /* NSP_DEBUG */
1445
1446         /*******************************************
1447          * Parse hardware SCSI irq reasons register
1448          *******************************************/
1449         if ((isrc & IRQSR_SCSI) != 0)
1450         {
1451                 if ((irqphs & IRQPHS_RST) != 0)
1452                 {
1453                         scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT, 
1454                                          "bus reset (power off?)");
1455                         return 1;
1456                 }
1457
1458                 if ((irqphs & IRQPHS_RSEL) != 0)
1459                 {
1460                         bus_space_write_1(bst, bsh, nsp_irqcr, IRQCR_RESCL);
1461                         if (nsp_reselected(sc) == EJUSTRETURN)
1462                                 return 1;
1463                 }
1464
1465                 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1466                         return 1; 
1467         }
1468
1469         /*******************************************
1470          * nexus check
1471          *******************************************/
1472         if ((ti = slp->sl_Tnexus) == NULL)
1473         {
1474                 /* unknown scsi phase changes */
1475                 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1476                 return 0;
1477         }
1478
1479         /*******************************************
1480          * aribitration & selection
1481          *******************************************/
1482         switch (ti->ti_phase)
1483         {
1484         case PH_SELSTART:
1485                 if ((ph & SCBUSMON_BSY) == 0)
1486                 {
1487                         if (sc->sc_seltout >= NSP_SELTIMEOUT)
1488                         {
1489                                 sc->sc_seltout = 0;
1490                                 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, 0);
1491                                 return nsp_disconnected(sc, ti);
1492                         }
1493                         sc->sc_seltout ++;
1494                         nsp_start_timer(sc, NSP_TIMER_1MS);
1495                         return 1;
1496                 }
1497
1498                 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1499                 nsphw_selection_done_and_expect_msgout(sc);
1500                 return 1;
1501
1502         case PH_SELECTED:
1503                 if ((isrc & IRQSR_SCSI) == 0)
1504                         return 1;
1505
1506                 nsp_target_nexus_establish(sc);
1507                 break;
1508
1509         case PH_RESEL:
1510                 if ((isrc & IRQSR_SCSI) == 0)
1511                         return 1;
1512
1513                 nsp_target_nexus_establish(sc);
1514                 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1515                 {
1516                         kprintf("%s: unexpected phase after reselect\n",
1517                                slp->sl_xname);
1518                         slp->sl_error |= FATALIO;
1519                         scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1520                         return 1;
1521                 }
1522                 break;
1523
1524         case PH_DATA:
1525                 if ((isrc & IRQSR_SCSI) != 0)
1526                         break;
1527                 if ((isrc & IRQSR_FIFO) != 0)
1528                 {
1529                         if (NSP_IS_PHASE_DATA(ph) == 0)
1530                                 return 1;
1531                         irqphs = (ph & IRQPHS_PHMASK);
1532                         break;
1533                 }
1534                 return 1;
1535
1536         default:
1537                 if ((isrc & IRQSR_SCSI) == 0)
1538                         return 1;
1539                 break;
1540         }
1541
1542         /*******************************************
1543          * data phase control 
1544          *******************************************/
1545         if (slp->sl_flags & HW_PDMASTART)
1546         {
1547                 if ((isrc & IRQSR_SCSI) != 0 &&
1548                      NSP_IS_IRQPHS_DATA(irqphs) == 0)
1549                 {
1550                         if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1551                                 nsp_pio_read(sc, 0);
1552                         nsp_pdma_end(sc, ti);
1553                 }
1554         }
1555
1556         /*******************************************
1557          * scsi seq
1558          *******************************************/
1559         if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1560                 return nsp_disconnected(sc, ti);
1561
1562         /* check unexpected bus free state */
1563         if (ph == 0)
1564         {
1565                 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1566                 return nsp_disconnected(sc, ti);
1567         }
1568                 
1569         /* check normal scsi phase */
1570         switch (irqphs & IRQPHS_PHMASK)
1571         {
1572         case IRQPHS_CMD:
1573                 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1574                         return 1;
1575
1576                 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1577                 if (scsi_low_cmd(slp, ti) != 0)
1578                 {
1579                         scsi_low_attention(slp);
1580                 }
1581
1582                 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR);
1583                 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1584                         nsp_cr_write_1(bst, bsh, NSPR_CMDDR,
1585                                        slp->sl_scp.scp_cmd[len]);
1586
1587                 nsp_cr_write_1(bst, bsh, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1588                 break;
1589
1590         case IRQPHS_DATAOUT:
1591                 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1592                 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1593                 {
1594                         scsi_low_attention(slp);
1595                 }
1596         
1597                 nsp_pio_write(sc, sc->sc_suspendio);
1598                 break;
1599
1600         case IRQPHS_DATAIN:
1601                 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1602                 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1603                 {
1604                         scsi_low_attention(slp);
1605                 }
1606
1607                 nsp_pio_read(sc, sc->sc_suspendio);
1608                 break;
1609
1610         case IRQPHS_STATUS:
1611                 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1612                         return 1;
1613
1614                 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1615                 regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1616                 if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1617                 {
1618                         nsp_cr_write_1(bst, bsh, NSPR_PARITYR, 
1619                                        PARITYR_ENABLE | PARITYR_CLEAR);
1620                         derror = SCSI_LOW_DATA_PE;
1621                 }
1622                 else
1623                         derror = 0;
1624
1625                 /* assert ACK */
1626                 cr = SCBUSCR_ACK | nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR);
1627                 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1628
1629                 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1630                 {
1631                         scsi_low_attention(slp);
1632                 }
1633
1634                 /* check REQ nagated */
1635                 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1636
1637                 /* deassert ACK */
1638                 cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1639                 nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1640                 break;
1641
1642         case IRQPHS_MSGOUT:
1643                 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1644                         return 1;
1645
1646 #ifdef  NSP_MSGOUT_SERIALIZE
1647                 /*
1648                  * XXX: NSP QUIRK
1649                  * NSP invoke interrupts only in the case of scsi phase changes,
1650                  * therefore we should poll the scsi phase here to catch 
1651                  * the next "msg out" if exists (no scsi phase changes).
1652                  */
1653                 rv = len = 16;
1654                 do {
1655                         SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1656                         flags = (ti->ti_ophase != ti->ti_phase) ? 
1657                                         SCSI_LOW_MSGOUT_INIT : 0;
1658                         len = scsi_low_msgout(slp, ti, flags);
1659
1660                         if (len > 1 && slp->sl_atten == 0)
1661                         {
1662                                 scsi_low_attention(slp);
1663                         }
1664
1665                         if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1666                                      slp->sl_clear_atten) != 0)
1667                         {
1668                                 slp->sl_error |= FATALIO;
1669                                 nsp_error(sc, "MSGOUT: xfer short",
1670                                                     isrc, ph, irqphs);
1671                         }
1672
1673                         /* catch a next signal */
1674                         rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1675                 }
1676                 while (rv > 0 && len -- > 0);
1677
1678 #else   /* !NSP_MSGOUT_SERIALIZE */
1679                 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1680                 flags = SCSI_LOW_MSGOUT_UNIFY;
1681                 if (ti->ti_ophase != ti->ti_phase)
1682                         flags |= SCSI_LOW_MSGOUT_INIT;
1683                 len = scsi_low_msgout(slp, ti, flags);
1684
1685                 if (len > 1 && slp->sl_atten == 0)
1686                 {
1687                         scsi_low_attention(slp);
1688                 }
1689
1690                 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1691                              slp->sl_clear_atten) != 0)
1692                 {
1693                         nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1694                 }
1695
1696 #endif  /* !NSP_MSGOUT_SERIALIZE */
1697                 break;
1698
1699         case IRQPHS_MSGIN:
1700                 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1701                         return 1;
1702
1703                 /*
1704                  * XXX: NSP QUIRK
1705                  * NSP invoke interrupts only in the case of scsi phase changes,
1706                  * therefore we should poll the scsi phase here to catch 
1707                  * the next "msg in" if exists (no scsi phase changes).
1708                  */
1709                 rv = len = 16;
1710                 do {
1711                         SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1712
1713                         /* read a data */
1714                         regv = nsp_cr_read_1(bst, bsh, NSPR_DATA);
1715                         if (nsp_cr_read_1(bst, bsh, NSPR_PARITYR) & PARITYR_PE)
1716                         {
1717                                 nsp_cr_write_1(bst, bsh,
1718                                                NSPR_PARITYR, 
1719                                                PARITYR_ENABLE | PARITYR_CLEAR);
1720                                 derror = SCSI_LOW_DATA_PE;
1721                         }
1722                         else
1723                         {
1724                                 derror = 0;
1725                         }
1726
1727                         /* assert ack */
1728                         cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) | SCBUSCR_ACK;
1729                         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1730
1731                         if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1732                         {
1733                                 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1734                                 {
1735                                         scsi_low_attention(slp);
1736                                 }
1737                         }
1738
1739                         /* check REQ nagated */
1740                         nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1741
1742                         /* deassert ack */
1743                         cr = nsp_cr_read_1(bst, bsh, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1744                         nsp_cr_write_1(bst, bsh, NSPR_SCBUSCR, cr);
1745
1746                         /* catch a next signal */
1747                         rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1748                 } 
1749                 while (rv > 0 && len -- > 0);
1750                 break;
1751
1752         default:
1753                 slp->sl_error |= FATALIO;
1754                 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1755                 break;
1756         }
1757
1758         return 1;
1759
1760 #if     0
1761 timerout:
1762         nsp_start_timer(sc, NSP_TIMER_1MS);
1763         return 0;
1764 #endif
1765 }
1766
1767 static int
1768 nsp_timeout(struct nsp_softc *sc)
1769 {
1770         struct scsi_low_softc *slp = &sc->sc_sclow;
1771         bus_space_tag_t iot = sc->sc_iot;
1772         bus_space_handle_t ioh = sc->sc_ioh;
1773         int tout;
1774         u_int8_t ph, regv;
1775
1776         if (slp->sl_Tnexus == NULL)
1777                 return 0;
1778
1779         ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1780         switch (ph & SCBUSMON_PHMASK)
1781         {
1782         case PHASE_DATAOUT:
1783                 if (sc->sc_dataout_timeout == 0)
1784                         break;
1785
1786                 /* check a fifo empty */
1787                 regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1788                 if ((regv & FIFOSR_FULLEMP) == 0)
1789                         break;
1790                 bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1791
1792                 /* check still requested */
1793                 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1794                 if ((ph & SCBUSMON_REQ) == 0)
1795                         break;
1796                 /* check timeout */
1797                 if ((-- sc->sc_dataout_timeout) > 0)
1798                         break;  
1799
1800                 slp->sl_error |= PDMAERR;
1801                 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1802                 {
1803                         kprintf("%s: write padding required\n", slp->sl_xname);
1804                         break;
1805                 }
1806
1807                 tout = NSP_DELAY_MAX;
1808                 while (tout -- > 0)
1809                 {
1810                         ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1811                         if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1812                                 break;
1813                         regv = bus_space_read_1(iot, ioh, nsp_fifosr);
1814                         if ((regv & FIFOSR_FULLEMP) == 0)
1815                         {
1816                                 SCSI_LOW_DELAY(1);
1817                                 continue;
1818                         }
1819
1820                         bus_space_write_1(iot, ioh, nsp_irqcr, IRQCR_FIFOCL);
1821                         nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1822                 }
1823                 ph = nsp_cr_read_1(iot, ioh, NSPR_SCBUSMON);
1824                 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1825                         sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1826                 break;
1827
1828         default:
1829                 break;
1830         }
1831         return 0;
1832 }