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