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