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