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