1 /* $NecBSD: nsp.c,v 1.21.12.6 2001/06/29 06:27:52 honda Exp $ */
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)
12 * SPDX-License-Identifier: BSD-3-Clause
14 * Copyright (c) 1998, 1999, 2000, 2001
15 * NetBSD/pc98 porting staff. All rights reserved.
17 * Copyright (c) 1998, 1999, 2000, 2001
18 * Naofumi HONDA. All rights reserved.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. The name of the author may not be used to endorse or promote products
29 * derived from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
35 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
52 #include <sys/queue.h>
53 #include <sys/malloc.h>
54 #include <sys/errno.h>
57 #include <machine/cpu.h>
58 #include <machine/bus.h>
60 #include <cam/scsi/scsi_low.h>
61 #include <dev/nsp/nspreg.h>
62 #include <dev/nsp/nspvar.h>
64 /***************************************************
66 ***************************************************/
67 /* DEVICE CONFIGURATION FLAGS (MINOR)
70 * 0x02 PARITY LINE OFF
71 * 0x04 IDENTIFY MSG OFF ( = single lun)
72 * 0x08 SYNC TRANSFER OFF
75 /***************************************************
77 ***************************************************/
78 #define NSP_NTARGETS 8
81 #define NSP_MAX_DATA_SIZE (64 * 1024)
82 #define NSP_SELTIMEOUT (200)
83 #define NSP_DELAY_MAX (2 * 1000 * 1000)
84 #define NSP_DELAY_INTERVAL (1)
85 #define NSP_TIMER_1MS (1000 / 51)
87 /***************************************************
89 ***************************************************/
92 #endif /* NSP_DEBUG */
98 int device_data_write;
102 int data_phase_bypass;
104 #endif /* NSP_STATICS */
106 /***************************************************
108 ***************************************************/
109 #define NSP_READ_SUSPEND_IO 0x0001
110 #define NSP_WRITE_SUSPEND_IO 0x0002
111 #define NSP_USE_MEMIO 0x0004
112 #define NSP_READ_FIFO_INTERRUPTS 0x0010
113 #define NSP_WRITE_FIFO_INTERRUPTS 0x0020
114 #define NSP_WAIT_FOR_SELECT 0x0100
116 u_int nsp_io_control = NSP_IO_CONTROL_FLAGS;
117 int nsp_read_suspend_bytes = DEV_BSIZE;
118 int nsp_write_suspend_bytes = DEV_BSIZE;
119 int nsp_read_interrupt_bytes = 4096;
120 int nsp_write_interrupt_bytes = 4096;
122 /***************************************************
124 ***************************************************/
125 extern struct cfdriver nsp_cd;
127 /**************************************************************
129 **************************************************************/
130 #define NSP_FIFO_ON 1
131 #define NSP_FIFO_OFF 0
132 static void nsp_pio_read(struct nsp_softc *, int);
133 static void nsp_pio_write(struct nsp_softc *, int);
134 static int nsp_xfer(struct nsp_softc *, u_int8_t *, int, int, int);
135 static int nsp_msg(struct nsp_softc *, struct targ_info *, u_int);
136 static int nsp_reselected(struct nsp_softc *);
137 static int nsp_disconnected(struct nsp_softc *, struct targ_info *);
138 static void nsp_pdma_end(struct nsp_softc *, struct targ_info *);
139 static void nsphw_init(struct nsp_softc *);
140 static int nsp_target_nexus_establish(struct nsp_softc *);
141 static int nsp_lun_nexus_establish(struct nsp_softc *);
142 static int nsp_ccb_nexus_establish(struct nsp_softc *);
143 static int nsp_world_start(struct nsp_softc *, int);
144 static int nsphw_start_selection(struct nsp_softc *sc, struct slccb *);
145 static void nsphw_bus_reset(struct nsp_softc *);
146 static void nsphw_attention(struct nsp_softc *);
147 static u_int nsp_fifo_count(struct nsp_softc *);
148 static u_int nsp_request_count(struct nsp_softc *);
149 static int nsp_negate_signal(struct nsp_softc *, u_int8_t, u_char *);
150 static int nsp_expect_signal(struct nsp_softc *, u_int8_t, u_int8_t);
151 static void nsp_start_timer(struct nsp_softc *, int);
152 static void nsp_setup_fifo(struct nsp_softc *, int, int, int);
153 static int nsp_targ_init(struct nsp_softc *, struct targ_info *, int);
154 static void nsphw_selection_done_and_expect_msgout(struct nsp_softc *);
155 static void nsp_data_padding(struct nsp_softc *, int, u_int);
156 static int nsp_timeout(struct nsp_softc *);
157 static int nsp_read_fifo(struct nsp_softc *, int);
158 static int nsp_write_fifo(struct nsp_softc *, int);
159 static int nsp_phase_match(struct nsp_softc *, u_int8_t, u_int8_t);
160 static int nsp_wait_interrupt(struct nsp_softc *);
162 struct scsi_low_funcs nspfuncs = {
163 SC_LOW_INIT_T nsp_world_start,
164 SC_LOW_BUSRST_T nsphw_bus_reset,
165 SC_LOW_TARG_INIT_T nsp_targ_init,
166 SC_LOW_LUN_INIT_T NULL,
168 SC_LOW_SELECT_T nsphw_start_selection,
169 SC_LOW_NEXUS_T nsp_lun_nexus_establish,
170 SC_LOW_NEXUS_T nsp_ccb_nexus_establish,
172 SC_LOW_ATTEN_T nsphw_attention,
173 SC_LOW_MSG_T nsp_msg,
175 SC_LOW_TIMEOUT_T nsp_timeout,
176 SC_LOW_POLL_T nspintr,
181 /****************************************************
183 ****************************************************/
184 static __inline uint8_t
185 nsp_cr_read_1(struct resource *res, bus_addr_t ofs)
188 bus_write_1(res, nsp_idxr, ofs);
189 return bus_read_1(res, nsp_datar);
193 nsp_cr_write_1(struct resource *res, bus_addr_t ofs, uint8_t va)
196 bus_write_1(res, nsp_idxr, ofs);
197 bus_write_1(res, nsp_datar, va);
201 nsp_expect_signal(struct nsp_softc *sc, u_int8_t curphase, u_int8_t mask)
203 struct scsi_low_softc *slp = &sc->sc_sclow;
207 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
209 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
210 if (ph == (u_int8_t) -1)
213 isrc = bus_read_1(sc->port_res, nsp_irqsr);
214 if (isrc & IRQSR_SCSI)
217 if ((ph & mask) != 0 && (ph & SCBUSMON_PHMASK) == curphase)
220 DELAY(NSP_DELAY_INTERVAL);
223 device_printf(slp->sl_dev, "nsp_expect_signal timeout\n");
229 struct nsp_softc *sc;
232 /* block all interrupts */
233 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_ALLMASK);
235 /* setup SCSI interface */
236 bus_write_1(sc->port_res, nsp_ifselr, IFSELR_IFSEL);
238 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, 0);
240 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, XFERMR_IO8);
241 nsp_cr_write_1(sc->port_res, NSPR_CLKDIVR, sc->sc_iclkdiv);
243 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
244 nsp_cr_write_1(sc->port_res, NSPR_PARITYR, sc->sc_parr);
245 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR,
246 PTCLRR_ACK | PTCLRR_REQ | PTCLRR_HOST | PTCLRR_RSS);
248 /* setup fifo asic */
249 bus_write_1(sc->port_res, nsp_ifselr, IFSELR_REGSEL);
250 nsp_cr_write_1(sc->port_res, NSPR_TERMPWRC, 0);
251 if ((nsp_cr_read_1(sc->port_res, NSPR_OCR) & OCR_TERMPWRS) == 0)
252 nsp_cr_write_1(sc->port_res, NSPR_TERMPWRC, TERMPWRC_POWON);
254 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, XFERMR_IO8);
255 nsp_cr_write_1(sc->port_res, NSPR_CLKDIVR, sc->sc_clkdiv);
256 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
257 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
259 nsp_cr_write_1(sc->port_res, NSPR_SYNCR, 0);
260 nsp_cr_write_1(sc->port_res, NSPR_ACKWIDTH, 0);
262 /* enable interrupts and ack them */
263 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
264 bus_write_1(sc->port_res, nsp_irqcr, IRQSR_MASK);
266 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
269 /****************************************************
271 ****************************************************/
274 struct nsp_softc *sc;
278 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR)/* & ~SCBUSCR_ACK */;
279 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr | SCBUSCR_ATN);
285 struct nsp_softc *sc;
289 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_ALLMASK);
291 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, SCBUSCR_RST);
292 DELAY(100 * 1000); /* 100ms */
293 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, 0);
294 for (i = 0; i < 5; i ++)
295 (void) nsp_cr_read_1(sc->port_res, NSPR_IRQPHS);
297 bus_write_1(sc->port_res, nsp_irqcr, IRQSR_MASK);
301 nsphw_selection_done_and_expect_msgout(sc)
302 struct nsp_softc *sc;
304 struct scsi_low_softc *slp = &sc->sc_sclow;
306 /* clear ack counter */
308 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
309 PTCLRR_REQ | PTCLRR_HOST);
311 /* deassert sel and assert atten */
313 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, sc->sc_busc);
315 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
316 sc->sc_busc | SCBUSCR_ADIR | SCBUSCR_ACKEN);
317 SCSI_LOW_ASSERT_ATN(slp);
321 nsphw_start_selection(sc, cb)
322 struct nsp_softc *sc;
325 struct scsi_low_softc *slp = &sc->sc_sclow;
326 struct targ_info *ti = cb->ti;
327 register u_int8_t arbs, ph;
330 wc = sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
331 sc->sc_dataout_timeout = 0;
334 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
335 if (ph != SCBUSMON_FREE)
338 nsp_statics.arbit_conflict_1 ++;
339 #endif /* NSP_STATICS */
340 return SCSI_LOW_START_FAIL;
343 /* start arbitration */
344 nsp_cr_write_1(sc->port_res, NSPR_ARBITS, ARBITS_EXEC);
346 SCSI_LOW_SETUP_PHASE(ti, PH_ARBSTART);
349 /* XXX: what a stupid chip! */
350 arbs = nsp_cr_read_1(sc->port_res, NSPR_ARBITS);
353 while ((arbs & (ARBITS_WIN | ARBITS_FAIL)) == 0 && wc -- > 0);
355 if ((arbs & ARBITS_WIN) == 0)
357 nsp_cr_write_1(sc->port_res, NSPR_ARBITS, ARBITS_CLR);
359 nsp_statics.arbit_conflict_2 ++;
360 #endif /* NSP_STATICS */
361 return SCSI_LOW_START_FAIL;
364 /* assert select line */
365 SCSI_LOW_SETUP_PHASE(ti, PH_SELSTART);
366 scsi_low_arbit_win(slp);
369 nsp_cr_write_1(sc->port_res, NSPR_DATA,
370 sc->sc_idbit | (1 << ti->ti_id));
371 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
372 SCBUSCR_SEL | SCBUSCR_BSY | sc->sc_busc);
374 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, SCBUSCR_SEL |
375 SCBUSCR_BSY | SCBUSCR_DOUT | sc->sc_busc);
376 nsp_cr_write_1(sc->port_res, NSPR_ARBITS, ARBITS_CLR);
378 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
379 SCBUSCR_SEL | SCBUSCR_DOUT | sc->sc_busc);
382 if ((nsp_io_control & NSP_WAIT_FOR_SELECT) != 0)
384 #define NSP_FIRST_SEL_WAIT 300
385 #define NSP_SEL_CHECK_INTERVAL 10
387 /* wait for a selection response */
388 for (wc = 0; wc < NSP_FIRST_SEL_WAIT / NSP_SEL_CHECK_INTERVAL;
391 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
392 if ((ph & SCBUSMON_BSY) == 0)
394 DELAY(NSP_SEL_CHECK_INTERVAL);
399 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
400 if ((ph & SCBUSMON_BSY) != 0)
402 nsphw_selection_done_and_expect_msgout(sc);
404 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
405 return SCSI_LOW_START_OK;
410 /* check a selection timeout */
411 nsp_start_timer(sc, NSP_TIMER_1MS);
413 return SCSI_LOW_START_OK;
417 nsp_world_start(sc, fdone)
418 struct nsp_softc *sc;
421 struct scsi_low_softc *slp = &sc->sc_sclow;
426 if ((slp->sl_cfgflags & CFG_NOATTEN) == 0)
427 sc->sc_busc = SCBUSCR_ATN;
431 if ((slp->sl_cfgflags & CFG_NOPARITY) == 0)
432 sc->sc_parr = PARITYR_ENABLE | PARITYR_CLEAR;
436 sc->sc_icr = (SCIENR_SCCHG | SCIENR_RESEL | SCIENR_RST);
439 scsi_low_bus_reset(slp);
444 struct ncp_synch_data {
451 static struct ncp_synch_data ncp_sync_data_40M[] = {
452 {0x0c,0x0c,0x1,0}, /* 20MB 50ns*/
453 {0x19,0x19,0x3,1}, /* 10MB 100ns*/
454 {0x1a,0x25,0x5,2}, /* 7.5MB 150ns*/
455 {0x26,0x32,0x7,3}, /* 5MB 200ns*/
459 static struct ncp_synch_data ncp_sync_data_20M[] = {
460 {0x19,0x19,0x1,0}, /* 10MB 100ns*/
461 {0x1a,0x25,0x2,0}, /* 7.5MB 150ns*/
462 {0x26,0x32,0x3,1}, /* 5MB 200ns*/
468 struct nsp_softc *sc;
469 struct targ_info *ti;
472 struct ncp_synch_data *sdp;
473 struct nsp_targ_info *nti = (void *) ti;
474 u_int period, offset;
477 if ((msg & SCSI_LOW_MSG_WIDE) != 0)
479 if (ti->ti_width != SCSI_LOW_BUS_WIDTH_8)
481 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
487 if ((msg & SCSI_LOW_MSG_SYNCH) == 0)
490 period = ti->ti_maxsynch.period;
491 offset = ti->ti_maxsynch.offset;
492 if (sc->sc_iclkdiv == CLKDIVR_20M)
493 sdp = &ncp_sync_data_20M[0];
495 sdp = &ncp_sync_data_40M[0];
497 for (i = 0; sdp->max_period != 0; i ++, sdp ++)
499 if (period >= sdp->min_period && period <= sdp->max_period)
503 if (period != 0 && sdp->max_period == 0)
506 * NO proper period/offset found,
507 * Retry neg with the target.
509 ti->ti_maxsynch.period = 0;
510 ti->ti_maxsynch.offset = 0;
511 nti->nti_reg_syncr = 0;
512 nti->nti_reg_ackwidth = 0;
517 nti->nti_reg_syncr = (sdp->chip_period << SYNCR_PERS) |
518 (offset & SYNCR_OFFM);
519 nti->nti_reg_ackwidth = sdp->ack_width;
523 nsp_cr_write_1(sc->port_res, NSPR_SYNCR, nti->nti_reg_syncr);
524 nsp_cr_write_1(sc->port_res, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
529 nsp_targ_init(sc, ti, action)
530 struct nsp_softc *sc;
531 struct targ_info *ti;
534 struct nsp_targ_info *nti = (void *) ti;
536 if (action == SCSI_LOW_INFO_ALLOC || action == SCSI_LOW_INFO_REVOKE)
538 ti->ti_width = SCSI_LOW_BUS_WIDTH_8;
539 ti->ti_maxsynch.period = 100 / 4;
540 ti->ti_maxsynch.offset = 15;
541 nti->nti_reg_syncr = 0;
542 nti->nti_reg_ackwidth = 0;
548 nsp_start_timer(sc, time)
549 struct nsp_softc *sc;
554 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, time);
557 /**************************************************************
558 * General probe attach
559 **************************************************************/
561 nspprobesubr(struct resource *res, u_int dvcfg)
565 regv = bus_read_1(res, nsp_fifosr);
566 if (regv < 0x11 || regv >= 0x20)
573 struct nsp_softc *sc;
575 struct scsi_low_softc *slp = &sc->sc_sclow;
577 sc->sc_idbit = (1 << slp->sl_hostid);
578 slp->sl_flags |= HW_READ_PADDING;
579 slp->sl_funcs = &nspfuncs;
580 sc->sc_tmaxcnt = SCSI_LOW_MIN_TOUT * 1000 * 1000; /* default */
582 (void) scsi_low_attach(slp, 0, NSP_NTARGETS, NSP_NLUNS,
583 sizeof(struct nsp_targ_info), 0);
586 /**************************************************************
588 **************************************************************/
591 struct nsp_softc *sc;
595 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_RSS_ACK | PTCLRR_PT);
596 count = bus_read_1(sc->port_res, nsp_datar);
597 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 8);
598 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 16);
603 nsp_request_count(sc)
604 struct nsp_softc *sc;
608 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_RSS_REQ | PTCLRR_PT);
609 count = bus_read_1(sc->port_res, nsp_datar);
610 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 8);
611 count += (((u_int) bus_read_1(sc->port_res, nsp_datar)) << 16);
616 nsp_setup_fifo(sc, on, direction, datalen)
617 struct nsp_softc *sc;
624 sc->sc_suspendio = 0;
625 if (on == NSP_FIFO_OFF)
627 xfermode = XFERMR_IO8;
631 /* check if suspend io OK ? */
634 if (direction == SCSI_LOW_READ)
636 if ((nsp_io_control & NSP_READ_SUSPEND_IO) != 0 &&
637 (datalen % nsp_read_suspend_bytes) == 0)
638 sc->sc_suspendio = nsp_read_suspend_bytes;
642 if ((nsp_io_control & NSP_WRITE_SUSPEND_IO) != 0 &&
643 (datalen % nsp_write_suspend_bytes) == 0)
644 sc->sc_suspendio = nsp_write_suspend_bytes;
648 /* determine a transfer type */
649 if (datalen < DEV_BSIZE || (datalen & 3) != 0)
651 if (sc->mem_res != NULL &&
652 (nsp_io_control & NSP_USE_MEMIO) != 0)
653 xfermode = XFERMR_XEN | XFERMR_MEM8;
655 xfermode = XFERMR_XEN | XFERMR_IO8;
659 if (sc->mem_res != NULL &&
660 (nsp_io_control & NSP_USE_MEMIO) != 0)
661 xfermode = XFERMR_XEN | XFERMR_MEM32;
663 xfermode = XFERMR_XEN | XFERMR_IO32;
665 if (sc->sc_suspendio > 0)
666 xfermode |= XFERMR_FIFOEN;
670 sc->sc_xfermr = xfermode;
671 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, sc->sc_xfermr);
676 struct nsp_softc *sc;
677 struct targ_info *ti;
679 struct scsi_low_softc *slp = &sc->sc_sclow;
680 struct slccb *cb = slp->sl_Qnexus;
683 sc->sc_dataout_timeout = 0;
684 slp->sl_flags &= ~HW_PDMASTART;
685 nsp_setup_fifo(sc, NSP_FIFO_OFF, SCSI_LOW_READ, 0);
686 if ((sc->sc_icr & SCIENR_FIFO) != 0)
688 sc->sc_icr &= ~SCIENR_FIFO;
689 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
694 slp->sl_error |= PDMAERR;
698 if (ti->ti_phase == PH_DATA)
700 cnt = nsp_fifo_count(sc);
701 if (slp->sl_scp.scp_direction == SCSI_LOW_WRITE)
703 len = sc->sc_cnt - cnt;
704 if (sc->sc_cnt >= cnt &&
705 slp->sl_scp.scp_datalen + len <=
706 cb->ccb_scp.scp_datalen)
708 slp->sl_scp.scp_data -= len;
709 slp->sl_scp.scp_datalen += len;
713 slp->sl_error |= PDMAERR;
714 device_printf(slp->sl_dev,
715 "len %x >= datalen %x\n",
716 len, slp->sl_scp.scp_datalen);
719 else if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
721 if (sc->sc_cnt != cnt ||
722 sc->sc_cnt > cb->ccb_scp.scp_datalen)
724 slp->sl_error |= PDMAERR;
725 device_printf(slp->sl_dev,
726 "data read count error %x != %x (%x)\n",
728 cb->ccb_scp.scp_datalen);
732 scsi_low_data_finish(slp);
737 device_printf(slp->sl_dev, "data phase miss\n");
738 slp->sl_error |= PDMAERR;
742 #define RFIFO_CRIT 64
743 #define WFIFO_CRIT 32
746 nsp_data_padding(sc, direction, count)
747 struct nsp_softc *sc;
752 if (count > NSP_MAX_DATA_SIZE)
753 count = NSP_MAX_DATA_SIZE;
755 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, XFERMR_XEN | XFERMR_IO8);
756 if (direction == SCSI_LOW_READ)
759 (void) bus_read_1(sc->port_res, nsp_fifodr);
764 (void) bus_write_1(sc->port_res, nsp_fifodr, 0);
766 nsp_cr_write_1(sc->port_res, NSPR_XFERMR, sc->sc_xfermr);
770 nsp_read_fifo(sc, suspendio)
771 struct nsp_softc *sc;
774 struct scsi_low_softc *slp = &sc->sc_sclow;
777 res = nsp_fifo_count(sc);
778 if (res == sc->sc_cnt)
782 if (res < sc->sc_cnt || res == (u_int) -1)
784 device_printf(slp->sl_dev,
785 "strange fifo ack count 0x%x < 0x%x\n", res, sc->sc_cnt);
788 #endif /* NSP_DEBUG */
790 res = res - sc->sc_cnt;
791 if (res > slp->sl_scp.scp_datalen)
793 if ((slp->sl_error & PDMAERR) == 0)
795 device_printf(slp->sl_dev, "data overrun 0x%x > 0x%x\n",
796 res, slp->sl_scp.scp_datalen);
799 slp->sl_error |= PDMAERR;
800 slp->sl_scp.scp_datalen = 0;
802 if ((slp->sl_flags & HW_READ_PADDING) == 0)
804 device_printf(slp->sl_dev, "read padding required\n");
808 nsp_data_padding(sc, SCSI_LOW_READ, res);
810 return 1; /* padding start */
813 if (suspendio > 0 && slp->sl_scp.scp_datalen >= suspendio)
816 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
818 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
821 bus_read_region_4(sc->mem_res, 0,
822 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
826 bus_read_region_1(sc->mem_res, 0,
827 (u_int8_t *) slp->sl_scp.scp_data, res);
832 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
835 bus_read_multi_4(sc->port_res, nsp_fifodr,
836 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
840 bus_read_multi_1(sc->port_res, nsp_fifodr,
841 (u_int8_t *) slp->sl_scp.scp_data, res);
845 if (nsp_cr_read_1(sc->port_res, NSPR_PARITYR) & PARITYR_PE)
847 nsp_cr_write_1(sc->port_res, NSPR_PARITYR,
848 PARITYR_ENABLE | PARITYR_CLEAR);
849 scsi_low_assert_msg(slp, slp->sl_Tnexus, SCSI_LOW_MSG_ERROR, 1);
852 slp->sl_scp.scp_data += res;
853 slp->sl_scp.scp_datalen -= res;
859 nsp_write_fifo(sc, suspendio)
860 struct nsp_softc *sc;
863 struct scsi_low_softc *slp = &sc->sc_sclow;
865 register u_int8_t stat;
870 if ((slp->sl_scp.scp_datalen % WFIFO_CRIT) != 0)
872 device_printf(slp->sl_dev,
873 "strange write length 0x%x\n",
874 slp->sl_scp.scp_datalen);
876 #endif /* NSP_DEBUG */
877 res = slp->sl_scp.scp_datalen % suspendio;
888 if (res > slp->sl_scp.scp_datalen)
889 res = slp->sl_scp.scp_datalen;
891 /* XXX: reconfirm! */
892 stat = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
893 if (stat != PHASE_DATAOUT)
896 if ((sc->sc_xfermr & (XFERMR_MEM32 | XFERMR_MEM8)) != 0)
898 if ((sc->sc_xfermr & XFERMR_MEM32) != 0)
900 bus_write_region_4(sc->mem_res, 0,
901 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
905 bus_write_region_1(sc->mem_res, 0,
906 (u_int8_t *) slp->sl_scp.scp_data, res);
911 if ((sc->sc_xfermr & XFERMR_IO32) != 0)
913 bus_write_multi_4(sc->port_res, nsp_fifodr,
914 (u_int32_t *) slp->sl_scp.scp_data, res >> 2);
918 bus_write_multi_1(sc->port_res, nsp_fifodr,
919 (u_int8_t *) slp->sl_scp.scp_data, res);
923 slp->sl_scp.scp_datalen -= res;
924 slp->sl_scp.scp_data += res;
930 nsp_wait_interrupt(sc)
931 struct nsp_softc *sc;
934 register u_int8_t isrc;
936 for (tout = 0; tout < DEV_BSIZE / 10; tout ++)
938 isrc = bus_read_1(sc->port_res, nsp_irqsr);
939 if ((isrc & (IRQSR_SCSI | IRQSR_FIFO)) != 0)
941 if ((isrc & IRQSR_FIFO) != 0)
943 bus_write_1(sc->port_res,
944 nsp_irqcr, IRQCR_FIFOCL);
954 nsp_pio_read(sc, suspendio)
955 struct nsp_softc *sc;
958 struct scsi_low_softc *slp = &sc->sc_sclow;
959 int tout, padding, datalen;
960 register u_int8_t stat, fstat;
963 tout = sc->sc_tmaxcnt;
964 slp->sl_flags |= HW_PDMASTART;
965 datalen = slp->sl_scp.scp_datalen;
970 stat = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
971 if (stat == (u_int8_t) -1)
974 /* out of data phase */
975 if ((stat & SCBUSMON_PHMASK) != PHASE_DATAIN)
977 nsp_read_fifo(sc, 0);
982 fstat = bus_read_1(sc->port_res, nsp_fifosr);
983 if ((fstat & FIFOSR_FULLEMP) != 0)
985 if ((sc->sc_icr & SCIENR_FIFO) != 0)
987 bus_write_1(sc->port_res, nsp_irqcr,
993 padding |= nsp_read_fifo(sc, suspendio);
997 padding |= nsp_read_fifo(sc, 0);
1000 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1005 if (padding == 0 && slp->sl_scp.scp_datalen <= 0)
1008 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1016 device_printf(slp->sl_dev, "nsp_pio_read: timeout\n");
1022 if (slp->sl_scp.scp_datalen > 0 &&
1023 slp->sl_scp.scp_datalen > datalen - nsp_read_interrupt_bytes)
1025 if (nsp_wait_interrupt(sc) != 0)
1031 nsp_pio_write(sc, suspendio)
1032 struct nsp_softc *sc;
1035 struct scsi_low_softc *slp = &sc->sc_sclow;
1036 u_int rcount, acount;
1038 register u_int8_t stat, fstat;
1040 tout = sc->sc_tmaxcnt;
1041 slp->sl_flags |= HW_PDMASTART;
1042 datalen = slp->sl_scp.scp_datalen;
1047 stat = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON) & SCBUSMON_PHMASK;
1048 if (stat != PHASE_DATAOUT)
1051 if (slp->sl_scp.scp_datalen <= 0)
1053 if (sc->sc_dataout_timeout == 0)
1054 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;
1058 fstat = bus_read_1(sc->port_res, nsp_fifosr);
1059 if ((fstat & FIFOSR_FULLEMP) != 0)
1061 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1063 bus_write_1(sc->port_res, nsp_irqcr,
1070 * To avoid timeout of pcmcia bus
1071 * (not scsi bus!), we should check
1072 * the scsi device sends us request
1073 * signals, which means the scsi device
1074 * is ready to receive data without
1077 if ((slp->sl_scp.scp_datalen % suspendio) == 0)
1080 * fill the nsp fifo, and waiting for
1083 nsp_write_fifo(sc, 0);
1088 * check the request singals.
1090 acount = nsp_fifo_count(sc);
1091 rcount = nsp_request_count(sc);
1092 if (rcount <= acount)
1094 nsp_write_fifo(sc, 0);
1096 nsp_statics.device_busy ++;
1097 #endif /* NSP_STATICS */
1101 nsp_write_fifo(sc, suspendio);
1103 nsp_statics.device_data_write ++;
1104 #endif /* NSP_STATICS */
1110 nsp_write_fifo(sc, 0);
1113 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1118 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1126 device_printf(slp->sl_dev, "nsp_pio_write: timeout\n");
1131 if (slp->sl_scp.scp_datalen > 0 &&
1132 slp->sl_scp.scp_datalen > datalen - nsp_write_interrupt_bytes)
1134 if (nsp_wait_interrupt(sc) != 0)
1140 nsp_negate_signal(struct nsp_softc *sc, u_int8_t mask, u_char *s)
1142 struct scsi_low_softc *slp = &sc->sc_sclow;
1146 for (wc = 0; wc < NSP_DELAY_MAX / NSP_DELAY_INTERVAL; wc ++)
1148 regv = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1149 if (regv == (u_int8_t) -1)
1151 if ((regv & mask) == 0)
1153 DELAY(NSP_DELAY_INTERVAL);
1156 device_printf(slp->sl_dev, "%s nsp_negate_signal timeout\n", s);
1161 nsp_xfer(sc, buf, len, phase, clear_atn)
1162 struct nsp_softc *sc;
1170 for (ptr = 0; len > 0; len --, ptr ++)
1172 rv = nsp_expect_signal(sc, phase, SCBUSMON_REQ);
1176 if (len == 1 && clear_atn != 0)
1178 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR,
1179 SCBUSCR_ADIR | SCBUSCR_ACKEN);
1180 SCSI_LOW_DEASSERT_ATN(&sc->sc_sclow);
1183 if (phase & SCBUSMON_IO)
1185 buf[ptr] = nsp_cr_read_1(sc->port_res, NSPR_DATAACK);
1189 nsp_cr_write_1(sc->port_res, NSPR_DATAACK, buf[ptr]);
1191 nsp_negate_signal(sc, SCBUSMON_ACK, "xfer<ACK>");
1198 /**************************************************************
1199 * disconnect & reselect (HW low)
1200 **************************************************************/
1203 struct nsp_softc *sc;
1205 struct scsi_low_softc *slp = &sc->sc_sclow;
1206 struct targ_info *ti;
1210 sid = (u_int) nsp_cr_read_1(sc->port_res, NSPR_RESELR);
1211 sid &= ~sc->sc_idbit;
1213 if ((ti = scsi_low_reselected(slp, sid)) == NULL)
1216 nsp_negate_signal(sc, SCBUSMON_SEL, "reselect<SEL>");
1218 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR);
1219 cr &= ~(SCBUSCR_BSY | SCBUSCR_ATN);
1220 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1221 cr |= SCBUSCR_ADIR | SCBUSCR_ACKEN;
1222 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1225 nsp_statics.reselect ++;
1226 #endif /* NSP_STATCIS */
1231 nsp_disconnected(sc, ti)
1232 struct nsp_softc *sc;
1233 struct targ_info *ti;
1235 struct scsi_low_softc *slp = &sc->sc_sclow;
1237 nsp_cr_write_1(sc->port_res, NSPR_PTCLRR, PTCLRR_PT | PTCLRR_ACK |
1238 PTCLRR_REQ | PTCLRR_HOST);
1239 if ((sc->sc_icr & SCIENR_FIFO) != 0)
1241 sc->sc_icr &= ~SCIENR_FIFO;
1242 nsp_cr_write_1(sc->port_res, NSPR_SCIENR, sc->sc_icr);
1245 sc->sc_dataout_timeout = 0;
1247 nsp_statics.disconnect ++;
1248 #endif /* NSP_STATICS */
1249 scsi_low_disconnected(slp, ti);
1253 /**************************************************************
1255 **************************************************************/
1256 static void nsp_error(struct nsp_softc *, u_char *, u_int8_t, u_int8_t, u_int8_t);
1259 nsp_error(struct nsp_softc * sc, u_char *s, u_int8_t isrc, u_int8_t ph,
1262 struct scsi_low_softc *slp = &sc->sc_sclow;
1264 device_printf(slp->sl_dev, "%s\n", s);
1265 device_printf(slp->sl_dev, "isrc 0x%x scmon 0x%x irqphs 0x%x\n",
1266 (u_int) isrc, (u_int) ph, (u_int) irqphs);
1270 nsp_target_nexus_establish(sc)
1271 struct nsp_softc *sc;
1273 struct scsi_low_softc *slp = &sc->sc_sclow;
1274 struct targ_info *ti = slp->sl_Tnexus;
1275 struct nsp_targ_info *nti = (void *) ti;
1277 /* setup synch transfer registers */
1278 nsp_cr_write_1(sc->port_res, NSPR_SYNCR, nti->nti_reg_syncr);
1279 nsp_cr_write_1(sc->port_res, NSPR_ACKWIDTH, nti->nti_reg_ackwidth);
1281 /* setup pdma fifo (minimum) */
1282 nsp_setup_fifo(sc, NSP_FIFO_ON, SCSI_LOW_READ, 0);
1287 nsp_lun_nexus_establish(sc)
1288 struct nsp_softc *sc;
1295 nsp_ccb_nexus_establish(sc)
1296 struct nsp_softc *sc;
1298 struct scsi_low_softc *slp = &sc->sc_sclow;
1299 struct slccb *cb = slp->sl_Qnexus;
1301 sc->sc_tmaxcnt = cb->ccb_tcmax * 1000 * 1000;
1303 /* setup pdma fifo */
1304 nsp_setup_fifo(sc, NSP_FIFO_ON,
1305 slp->sl_scp.scp_direction, slp->sl_scp.scp_datalen);
1307 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1309 if (sc->sc_suspendio > 0 &&
1310 (nsp_io_control & NSP_READ_FIFO_INTERRUPTS) != 0)
1312 sc->sc_icr |= SCIENR_FIFO;
1313 nsp_cr_write_1(sc->port_res,
1314 NSPR_SCIENR, sc->sc_icr);
1319 if (sc->sc_suspendio > 0 &&
1320 (nsp_io_control & NSP_WRITE_FIFO_INTERRUPTS) != 0)
1322 sc->sc_icr |= SCIENR_FIFO;
1323 nsp_cr_write_1(sc->port_res,
1324 NSPR_SCIENR, sc->sc_icr);
1331 nsp_phase_match(struct nsp_softc *sc, u_int8_t phase, u_int8_t stat)
1333 struct scsi_low_softc *slp = &sc->sc_sclow;
1335 if ((stat & SCBUSMON_PHMASK) != phase)
1337 device_printf(slp->sl_dev, "phase mismatch 0x%x != 0x%x\n",
1338 (u_int) phase, (u_int) stat);
1342 if ((stat & SCBUSMON_REQ) == 0)
1352 struct nsp_softc *sc = arg;
1353 struct scsi_low_softc *slp = &sc->sc_sclow;
1354 struct targ_info *ti;
1356 u_int derror, flags;
1358 u_int8_t isrc, ph, irqphs, cr, regv;
1360 /*******************************************
1362 *******************************************/
1363 if (slp->sl_flags & HW_INACTIVE)
1366 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_IRQDIS);
1367 isrc = bus_read_1(sc->port_res, nsp_irqsr);
1368 if (isrc == (u_int8_t) -1 || (isrc & IRQSR_MASK) == 0)
1370 bus_write_1(sc->port_res, nsp_irqcr, 0);
1375 * Do not read an irqphs register if no scsi phase interrupt.
1376 * Unless, you should lose a scsi phase interrupt.
1378 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1379 if ((isrc & IRQSR_SCSI) != 0)
1381 irqphs = nsp_cr_read_1(sc->port_res, NSPR_IRQPHS);
1387 * timer interrupt handler (scsi vs timer interrupts)
1389 if (sc->sc_timer != 0)
1391 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
1392 nsp_cr_write_1(sc->port_res, NSPR_TIMERCNT, 0);
1396 /* check a timer interrupt */
1398 if ((isrc & IRQSR_TIMER) != 0)
1400 if ((isrc & IRQSR_MASK) == IRQSR_TIMER && sc->sc_seltout == 0)
1402 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_TIMERCL);
1405 regv |= IRQCR_TIMERCL;
1408 /* check a fifo interrupt */
1409 if ((isrc & IRQSR_FIFO) != 0)
1411 regv |= IRQCR_FIFOCL;
1414 /* OK. enable all interrupts */
1415 bus_write_1(sc->port_res, nsp_irqcr, regv);
1417 /*******************************************
1419 *******************************************/
1423 nsp_error(sc, "current status", isrc, ph, irqphs);
1424 scsi_low_print(slp, NULL);
1427 kdb_enter(KDB_WHY_CAM, "nsp");
1430 #endif /* NSP_DEBUG */
1432 /*******************************************
1433 * Parse hardware SCSI irq reasons register
1434 *******************************************/
1435 if ((isrc & IRQSR_SCSI) != 0)
1437 if ((irqphs & IRQPHS_RST) != 0)
1439 scsi_low_restart(slp, SCSI_LOW_RESTART_SOFT,
1440 "bus reset (power off?)");
1444 if ((irqphs & IRQPHS_RSEL) != 0)
1446 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_RESCL);
1447 if (nsp_reselected(sc) == EJUSTRETURN)
1451 if ((irqphs & (IRQPHS_PCHG | IRQPHS_LBF)) == 0)
1455 /*******************************************
1457 *******************************************/
1458 if ((ti = slp->sl_Tnexus) == NULL)
1460 /* unknown scsi phase changes */
1461 nsp_error(sc, "unknown scsi phase changes", isrc, ph, irqphs);
1465 /*******************************************
1466 * aribitration & selection
1467 *******************************************/
1468 switch (ti->ti_phase)
1471 if ((ph & SCBUSMON_BSY) == 0)
1473 if (sc->sc_seltout >= NSP_SELTIMEOUT)
1476 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, 0);
1477 return nsp_disconnected(sc, ti);
1480 nsp_start_timer(sc, NSP_TIMER_1MS);
1484 SCSI_LOW_SETUP_PHASE(ti, PH_SELECTED);
1485 nsphw_selection_done_and_expect_msgout(sc);
1489 if ((isrc & IRQSR_SCSI) == 0)
1492 nsp_target_nexus_establish(sc);
1496 if ((isrc & IRQSR_SCSI) == 0)
1499 nsp_target_nexus_establish(sc);
1500 if ((ph & SCBUSMON_PHMASK) != PHASE_MSGIN)
1502 device_printf(slp->sl_dev,
1503 "unexpected phase after reselect\n");
1504 slp->sl_error |= FATALIO;
1505 scsi_low_assert_msg(slp, ti, SCSI_LOW_MSG_ABORT, 1);
1511 if ((isrc & IRQSR_SCSI) != 0)
1513 if ((isrc & IRQSR_FIFO) != 0)
1515 if (NSP_IS_PHASE_DATA(ph) == 0)
1517 irqphs = (ph & IRQPHS_PHMASK);
1523 if ((isrc & IRQSR_SCSI) == 0)
1528 /*******************************************
1529 * data phase control
1530 *******************************************/
1531 if (slp->sl_flags & HW_PDMASTART)
1533 if ((isrc & IRQSR_SCSI) != 0 &&
1534 NSP_IS_IRQPHS_DATA(irqphs) == 0)
1536 if (slp->sl_scp.scp_direction == SCSI_LOW_READ)
1537 nsp_pio_read(sc, 0);
1538 nsp_pdma_end(sc, ti);
1542 /*******************************************
1544 *******************************************/
1545 if (slp->sl_msgphase != 0 && (irqphs & IRQPHS_LBF) != 0)
1546 return nsp_disconnected(sc, ti);
1548 /* check unexpected bus free state */
1551 nsp_error(sc, "unexpected bus free", isrc, ph, irqphs);
1552 return nsp_disconnected(sc, ti);
1555 /* check normal scsi phase */
1556 switch (irqphs & IRQPHS_PHMASK)
1559 if (nsp_phase_match(sc, PHASE_CMD, ph) != 0)
1562 SCSI_LOW_SETUP_PHASE(ti, PH_CMD);
1563 if (scsi_low_cmd(slp, ti) != 0)
1565 scsi_low_attention(slp);
1568 nsp_cr_write_1(sc->port_res, NSPR_CMDCR, CMDCR_PTCLR);
1569 for (len = 0; len < slp->sl_scp.scp_cmdlen; len ++)
1570 nsp_cr_write_1(sc->port_res, NSPR_CMDDR,
1571 slp->sl_scp.scp_cmd[len]);
1573 nsp_cr_write_1(sc->port_res, NSPR_CMDCR, CMDCR_PTCLR | CMDCR_EXEC);
1576 case IRQPHS_DATAOUT:
1577 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1578 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_WRITE) != 0)
1580 scsi_low_attention(slp);
1583 nsp_pio_write(sc, sc->sc_suspendio);
1587 SCSI_LOW_SETUP_PHASE(ti, PH_DATA);
1588 if (scsi_low_data(slp, ti, &bp, SCSI_LOW_READ) != 0)
1590 scsi_low_attention(slp);
1593 nsp_pio_read(sc, sc->sc_suspendio);
1597 if (nsp_phase_match(sc, PHASE_STATUS, ph) != 0)
1600 SCSI_LOW_SETUP_PHASE(ti, PH_STAT);
1601 regv = nsp_cr_read_1(sc->port_res, NSPR_DATA);
1602 if (nsp_cr_read_1(sc->port_res, NSPR_PARITYR) & PARITYR_PE)
1604 nsp_cr_write_1(sc->port_res, NSPR_PARITYR,
1605 PARITYR_ENABLE | PARITYR_CLEAR);
1606 derror = SCSI_LOW_DATA_PE;
1612 cr = SCBUSCR_ACK | nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR);
1613 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1615 if (scsi_low_statusin(slp, ti, derror | regv) != 0)
1617 scsi_low_attention(slp);
1620 /* check REQ nagated */
1621 nsp_negate_signal(sc, SCBUSMON_REQ, "statin<REQ>");
1624 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1625 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1629 if (nsp_phase_match(sc, PHASE_MSGOUT, ph) != 0)
1632 #ifdef NSP_MSGOUT_SERIALIZE
1635 * NSP invoke interrupts only in the case of scsi phase changes,
1636 * therefore we should poll the scsi phase here to catch
1637 * the next "msg out" if exists (no scsi phase changes).
1641 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1642 flags = (ti->ti_ophase != ti->ti_phase) ?
1643 SCSI_LOW_MSGOUT_INIT : 0;
1644 len = scsi_low_msgout(slp, ti, flags);
1646 if (len > 1 && slp->sl_atten == 0)
1648 scsi_low_attention(slp);
1651 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1652 slp->sl_clear_atten) != 0)
1654 slp->sl_error |= FATALIO;
1655 nsp_error(sc, "MSGOUT: xfer short",
1659 /* catch a next signal */
1660 rv = nsp_expect_signal(sc, PHASE_MSGOUT, SCBUSMON_REQ);
1662 while (rv > 0 && len -- > 0);
1664 #else /* !NSP_MSGOUT_SERIALIZE */
1665 SCSI_LOW_SETUP_PHASE(ti, PH_MSGOUT);
1666 flags = SCSI_LOW_MSGOUT_UNIFY;
1667 if (ti->ti_ophase != ti->ti_phase)
1668 flags |= SCSI_LOW_MSGOUT_INIT;
1669 len = scsi_low_msgout(slp, ti, flags);
1671 if (len > 1 && slp->sl_atten == 0)
1673 scsi_low_attention(slp);
1676 if (nsp_xfer(sc, ti->ti_msgoutstr, len, PHASE_MSGOUT,
1677 slp->sl_clear_atten) != 0)
1679 nsp_error(sc, "MSGOUT: xfer short", isrc, ph, irqphs);
1682 #endif /* !NSP_MSGOUT_SERIALIZE */
1686 if (nsp_phase_match(sc, PHASE_MSGIN, ph) != 0)
1691 * NSP invoke interrupts only in the case of scsi phase changes,
1692 * therefore we should poll the scsi phase here to catch
1693 * the next "msg in" if exists (no scsi phase changes).
1697 SCSI_LOW_SETUP_PHASE(ti, PH_MSGIN);
1700 regv = nsp_cr_read_1(sc->port_res, NSPR_DATA);
1701 if (nsp_cr_read_1(sc->port_res, NSPR_PARITYR) & PARITYR_PE)
1703 nsp_cr_write_1(sc->port_res,
1705 PARITYR_ENABLE | PARITYR_CLEAR);
1706 derror = SCSI_LOW_DATA_PE;
1714 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR) | SCBUSCR_ACK;
1715 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1717 if (scsi_low_msgin(slp, ti, regv | derror) == 0)
1719 if (scsi_low_is_msgout_continue(ti, 0) != 0)
1721 scsi_low_attention(slp);
1725 /* check REQ nagated */
1726 nsp_negate_signal(sc, SCBUSMON_REQ, "msgin<REQ>");
1729 cr = nsp_cr_read_1(sc->port_res, NSPR_SCBUSCR) & (~SCBUSCR_ACK);
1730 nsp_cr_write_1(sc->port_res, NSPR_SCBUSCR, cr);
1732 /* catch a next signal */
1733 rv = nsp_expect_signal(sc, PHASE_MSGIN, SCBUSMON_REQ);
1735 while (rv > 0 && len -- > 0);
1739 slp->sl_error |= FATALIO;
1740 nsp_error(sc, "unknown scsi phase", isrc, ph, irqphs);
1748 nsp_start_timer(sc, NSP_TIMER_1MS);
1755 struct nsp_softc *sc;
1757 struct scsi_low_softc *slp = &sc->sc_sclow;
1761 if (slp->sl_Tnexus == NULL)
1764 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1765 switch (ph & SCBUSMON_PHMASK)
1768 if (sc->sc_dataout_timeout == 0)
1771 /* check a fifo empty */
1772 regv = bus_read_1(sc->port_res, nsp_fifosr);
1773 if ((regv & FIFOSR_FULLEMP) == 0)
1775 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_FIFOCL);
1777 /* check still requested */
1778 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1779 if ((ph & SCBUSMON_REQ) == 0)
1782 if ((-- sc->sc_dataout_timeout) > 0)
1785 slp->sl_error |= PDMAERR;
1786 if ((slp->sl_flags & HW_WRITE_PADDING) == 0)
1788 device_printf(slp->sl_dev, "write padding required\n");
1792 tout = NSP_DELAY_MAX;
1795 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1796 if ((ph & SCBUSMON_PHMASK) != PHASE_DATAOUT)
1798 regv = bus_read_1(sc->port_res, nsp_fifosr);
1799 if ((regv & FIFOSR_FULLEMP) == 0)
1805 bus_write_1(sc->port_res, nsp_irqcr, IRQCR_FIFOCL);
1806 nsp_data_padding(sc, SCSI_LOW_WRITE, 32);
1808 ph = nsp_cr_read_1(sc->port_res, NSPR_SCBUSMON);
1809 if ((ph & SCBUSMON_PHMASK) == PHASE_DATAOUT)
1810 sc->sc_dataout_timeout = SCSI_LOW_TIMEOUT_HZ;