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