Home | History | Annotate | Line # | Download | only in dev
scsi_1185.c revision 1.22
      1  1.22    martin /*	$NetBSD: scsi_1185.c,v 1.22 2016/03/26 17:14:38 martin Exp $	*/
      2   1.3    tsubai 
      3   1.1    tsubai /*
      4   1.1    tsubai  * Copyright (c) 1992, 1993
      5   1.1    tsubai  *	The Regents of the University of California.  All rights reserved.
      6   1.1    tsubai  *
      7   1.1    tsubai  * This code is derived from software contributed to Berkeley by
      8   1.1    tsubai  * Sony Corp. and Kazumasa Utashiro of Software Research Associates, Inc.
      9   1.1    tsubai  *
     10   1.1    tsubai  * Redistribution and use in source and binary forms, with or without
     11   1.1    tsubai  * modification, are permitted provided that the following conditions
     12   1.1    tsubai  * are met:
     13   1.1    tsubai  * 1. Redistributions of source code must retain the above copyright
     14   1.1    tsubai  *    notice, this list of conditions and the following disclaimer.
     15   1.1    tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    tsubai  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    tsubai  *    documentation and/or other materials provided with the distribution.
     18  1.13       agc  * 3. Neither the name of the University nor the names of its contributors
     19   1.1    tsubai  *    may be used to endorse or promote products derived from this software
     20   1.1    tsubai  *    without specific prior written permission.
     21   1.1    tsubai  *
     22   1.1    tsubai  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23   1.1    tsubai  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24   1.1    tsubai  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25   1.1    tsubai  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26   1.1    tsubai  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27   1.1    tsubai  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28   1.1    tsubai  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29   1.1    tsubai  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30   1.1    tsubai  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31   1.1    tsubai  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1    tsubai  * SUCH DAMAGE.
     33   1.1    tsubai  *
     34   1.1    tsubai  * from: $Hdr: scsi_1185.c,v 4.300 91/06/09 06:22:20 root Rel41 $ SONY
     35   1.1    tsubai  *
     36   1.1    tsubai  *	@(#)scsi_1185.c	8.1 (Berkeley) 6/11/93
     37   1.1    tsubai  */
     38   1.1    tsubai 
     39   1.1    tsubai /*
     40   1.1    tsubai  * Copyright (c) 1989- by SONY Corporation.
     41   1.4    tsubai  *
     42   1.1    tsubai  *	scsi_1185.c
     43   1.1    tsubai  *
     44   1.1    tsubai  *	CXD1185Q
     45   1.1    tsubai  *	SCSI bus low level common routines
     46  1.14       wiz  *				for one CPU machine
     47   1.4    tsubai  *
     48   1.1    tsubai  * MODIFY HISTORY:
     49   1.1    tsubai  *
     50   1.1    tsubai  *	DMAC_WAIT	--- DMAC_0266 wo tukau-baai, DMAC mata-wa SCSI-chip ni
     51   1.1    tsubai  *				tuzukete access suru-baai,
     52   1.1    tsubai  *				kanarazu wait wo ireru-beshi !
     53   1.1    tsubai  */
     54  1.12     lukem 
     55  1.12     lukem #include <sys/cdefs.h>
     56  1.22    martin __KERNEL_RCSID(0, "$NetBSD: scsi_1185.c,v 1.22 2016/03/26 17:14:38 martin Exp $");
     57   1.1    tsubai 
     58  1.20      matt #define	__INTR_PRIVATE
     59   1.1    tsubai #include <sys/param.h>
     60   1.1    tsubai #include <sys/systm.h>
     61   1.4    tsubai #include <sys/device.h>
     62  1.20      matt #include <sys/intr.h>
     63   1.4    tsubai 
     64  1.10   thorpej #include <uvm/uvm_extern.h>
     65  1.10   thorpej 
     66   1.4    tsubai #include <dev/scsipi/scsi_all.h>
     67   1.4    tsubai #include <dev/scsipi/scsipi_all.h>
     68   1.4    tsubai #include <dev/scsipi/scsiconf.h>
     69   1.1    tsubai 
     70   1.1    tsubai #include <machine/cpu.h>
     71   1.4    tsubai #include <machine/intr.h>
     72   1.4    tsubai #include <machine/machConst.h>
     73   1.1    tsubai 
     74   1.8   thorpej #include <mips/cache.h>
     75   1.8   thorpej 
     76   1.2   thorpej #include <newsmips/dev/screg_1185.h>
     77   1.4    tsubai #include <newsmips/dev/scsireg.h>
     78   1.1    tsubai 
     79  1.19   tsutsui #include "ioconf.h"
     80  1.19   tsutsui 
     81   1.4    tsubai #if defined(news3400)
     82   1.2   thorpej # include <newsmips/dev/dmac_0448.h>
     83   1.1    tsubai # ifndef NDMACMAP
     84   1.4    tsubai # define NDMACMAP 144
     85   1.1    tsubai # endif
     86   1.1    tsubai #endif
     87   1.1    tsubai 
     88   1.1    tsubai #define ABORT_SYNCTR_MES_FROM_TARGET
     89   1.1    tsubai #define SCSI_1185AQ
     90   1.1    tsubai #define RESET_RECOVER
     91   1.1    tsubai #define DMAC_MAP_INIT			/* for nws-3700 parity error */
     92   1.1    tsubai #define APAD_ALWAYS_ON
     93   1.1    tsubai 
     94   1.4    tsubai #define CHECK_LOOP_CNT	60
     95   1.4    tsubai #define RSL_LOOP_CNT	60
     96   1.1    tsubai 
     97   1.1    tsubai #ifndef DMAC_MAP_INIT
     98   1.1    tsubai # define MAP_OVER_ACCESS		/* for nws-3700 parity error */
     99   1.1    tsubai #endif
    100   1.1    tsubai 
    101   1.1    tsubai #undef	CHECK_MRQ
    102   1.1    tsubai 
    103   1.1    tsubai #ifdef NOT_SUPPORT_SYNCTR
    104   1.4    tsubai # define MAX_OFFSET_BYTES 0
    105   1.1    tsubai #else
    106   1.4    tsubai # define MAX_OFFSET_BYTES MAX_OFFSET
    107   1.1    tsubai #endif
    108   1.1    tsubai 
    109   1.1    tsubai #define	act_point	spoint
    110   1.1    tsubai #define	act_trcnt	stcnt
    111   1.1    tsubai #define	act_tag		stag
    112   1.1    tsubai #define	act_offset	soffset
    113   1.1    tsubai 
    114   1.4    tsubai #define	splscsi splsc
    115   1.1    tsubai 
    116   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
    117  1.17   tsutsui #define nops(x)		{ int __i; for (__i = 0; __i < (x); __i++) ; }
    118   1.1    tsubai #define	DMAC_WAIT0	;
    119   1.1    tsubai #else
    120   1.1    tsubai #define	DMAC_WAIT0	DMAC_WAIT
    121   1.1    tsubai #endif
    122   1.1    tsubai 
    123   1.1    tsubai #ifdef DMAC_MAP_INIT
    124   1.4    tsubai static int dmac_map_init = 0;
    125   1.1    tsubai #endif
    126   1.1    tsubai 
    127   1.1    tsubai /*
    128   1.1    tsubai  *	command flag status
    129   1.1    tsubai  */
    130   1.1    tsubai #define	CF_SET		1
    131   1.1    tsubai #define	CF_SEND		2
    132   1.1    tsubai #define	CF_ENOUGH	3
    133   1.1    tsubai #define	CF_EXEC		4
    134   1.1    tsubai 
    135   1.4    tsubai #define	SEL_TIMEOUT_VALUE 0x7a
    136   1.1    tsubai 
    137  1.15   tsutsui void sc_send(struct sc_scb *, int, int);
    138  1.15   tsutsui int scintr(void);
    139  1.15   tsutsui void scsi_hardreset(void);
    140  1.15   tsutsui void scsi_chipreset(struct sc_softc *);
    141  1.15   tsutsui void scsi_softreset(struct sc_softc *);
    142  1.15   tsutsui int sc_busy(struct sc_softc *, int);
    143  1.15   tsutsui 
    144  1.15   tsutsui static int WAIT_STATR_BITCLR(int);
    145  1.15   tsutsui static int WAIT_STATR_BITSET(int);
    146  1.15   tsutsui static void SET_CMD(struct sc_softc *, int);
    147  1.15   tsutsui static void SET_CNT(int);
    148  1.15   tsutsui static int GET_CNT(void);
    149  1.19   tsutsui static void GET_INTR(uint8_t *, uint8_t *);
    150  1.15   tsutsui static void sc_start(struct sc_softc *);
    151  1.15   tsutsui static void sc_resel(struct sc_softc *);
    152  1.15   tsutsui static void sc_discon(struct sc_softc *);
    153  1.15   tsutsui static void sc_pmatch(struct sc_softc *);
    154  1.15   tsutsui static void flush_fifo(struct sc_softc *);
    155  1.15   tsutsui static void sc_cout(struct sc_softc *, struct sc_chan_stat *);
    156  1.15   tsutsui static void sc_min(struct sc_softc *, struct sc_chan_stat *);
    157  1.15   tsutsui static void sc_mout(struct sc_softc *, struct sc_chan_stat *);
    158  1.15   tsutsui static void sc_sin(struct sc_softc *, volatile struct sc_chan_stat *);
    159  1.15   tsutsui static void sc_dio(struct sc_softc *, volatile struct sc_chan_stat *);
    160  1.15   tsutsui static void sc_dio_pad(struct sc_softc *, volatile struct sc_chan_stat *);
    161  1.15   tsutsui static void print_scsi_stat(struct sc_softc *);
    162  1.15   tsutsui static void append_wb(struct sc_softc *, struct sc_chan_stat *);
    163  1.15   tsutsui static struct sc_chan_stat *get_wb_chan(struct sc_softc *);
    164  1.15   tsutsui static int release_wb(struct sc_softc *);
    165  1.15   tsutsui static void adjust_transfer(struct sc_softc *, struct sc_chan_stat *);
    166  1.15   tsutsui static void clean_k2dcache(struct sc_scb *);
    167   1.1    tsubai 
    168  1.15   tsutsui extern void sc_done(struct sc_scb *);
    169  1.15   tsutsui extern paddr_t kvtophys(vaddr_t);
    170   1.1    tsubai 
    171   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
    172  1.19   tsutsui #define dma_reset(x) do {					\
    173  1.17   tsutsui 	int __s = splscsi();					\
    174   1.3    tsubai 	dmac_gsel = (x); dmac_cctl = DM_RST; dmac_cctl = 0;	\
    175  1.17   tsutsui 	splx(__s);						\
    176  1.19   tsutsui } while (/* CONSTCOND */ 0)
    177   1.1    tsubai #endif
    178   1.1    tsubai 
    179   1.1    tsubai int
    180  1.15   tsutsui WAIT_STATR_BITCLR(int bitmask)
    181   1.1    tsubai {
    182  1.15   tsutsui 	int iloop;
    183  1.19   tsutsui 	uint8_t dummy;
    184   1.1    tsubai 
    185   1.1    tsubai 	iloop = 0;
    186   1.1    tsubai 	do {
    187   1.1    tsubai 		dummy = sc_statr;
    188   1.1    tsubai 		DMAC_WAIT0;
    189   1.1    tsubai 		if (iloop++ > CHECK_LOOP_CNT)
    190  1.15   tsutsui 			return -1;
    191   1.1    tsubai 	} while (dummy & bitmask);
    192  1.15   tsutsui 	return 0;
    193   1.1    tsubai }
    194   1.1    tsubai 
    195   1.1    tsubai int
    196  1.15   tsutsui WAIT_STATR_BITSET(int bitmask)
    197   1.1    tsubai {
    198  1.15   tsutsui 	int iloop;
    199  1.19   tsutsui 	uint8_t dummy;
    200   1.1    tsubai 
    201   1.1    tsubai 	iloop = 0;
    202   1.1    tsubai 	do {
    203   1.1    tsubai 		dummy = sc_statr;
    204   1.1    tsubai 		DMAC_WAIT0;
    205   1.1    tsubai 		if (iloop++ > CHECK_LOOP_CNT)
    206  1.15   tsutsui 			return -1;
    207   1.1    tsubai 	} while ((dummy & bitmask) == 0);
    208  1.15   tsutsui 	return 0;
    209   1.1    tsubai }
    210   1.1    tsubai 
    211   1.1    tsubai void
    212  1.15   tsutsui SET_CMD(struct sc_softc *sc, int CMD)
    213   1.1    tsubai {
    214  1.15   tsutsui 
    215  1.15   tsutsui 	(void)WAIT_STATR_BITCLR(R0_CIP);
    216  1.15   tsutsui 	sc->lastcmd = CMD;
    217  1.15   tsutsui 	sc_comr = CMD;
    218   1.1    tsubai 	DMAC_WAIT0;
    219   1.1    tsubai }
    220   1.1    tsubai 
    221   1.1    tsubai void
    222  1.15   tsutsui SET_CNT(int COUNT)
    223   1.1    tsubai {
    224  1.15   tsutsui 
    225  1.15   tsutsui 	sc_tclow = COUNT & 0xff;
    226   1.1    tsubai 	DMAC_WAIT0;
    227  1.15   tsutsui 	sc_tcmid = (COUNT >> 8) & 0xff;
    228   1.1    tsubai 	DMAC_WAIT0;
    229  1.15   tsutsui 	sc_tchi = (COUNT >> 16) & 0xff;
    230   1.1    tsubai 	DMAC_WAIT0;
    231   1.1    tsubai }
    232   1.1    tsubai 
    233   1.1    tsubai int
    234  1.15   tsutsui GET_CNT(void)
    235   1.1    tsubai {
    236  1.19   tsutsui 	int COUNT;
    237   1.1    tsubai 
    238   1.1    tsubai 	COUNT = sc_tclow;
    239   1.1    tsubai 	DMAC_WAIT0;
    240   1.1    tsubai 	COUNT += (sc_tcmid << 8) & 0xff00;
    241   1.1    tsubai 	DMAC_WAIT0;
    242   1.1    tsubai 	COUNT += (sc_tchi << 16) & 0xff0000;
    243   1.1    tsubai 	DMAC_WAIT0;
    244  1.15   tsutsui 	return COUNT;
    245   1.1    tsubai }
    246   1.1    tsubai 
    247   1.1    tsubai void
    248  1.19   tsutsui GET_INTR(uint8_t *DATA1, uint8_t *DATA2)
    249   1.1    tsubai {
    250  1.15   tsutsui 
    251  1.15   tsutsui 	(void)WAIT_STATR_BITCLR(R0_CIP);
    252   1.1    tsubai 	while (sc_statr & R0_MIRQ) {
    253   1.1    tsubai 		DMAC_WAIT0;
    254   1.1    tsubai 		*DATA1 |= sc_intrq1;
    255   1.1    tsubai 		DMAC_WAIT0;
    256   1.1    tsubai 		*DATA2 |= sc_intrq2;
    257   1.1    tsubai 		DMAC_WAIT0;
    258   1.1    tsubai 	}
    259   1.1    tsubai }
    260   1.1    tsubai 
    261   1.1    tsubai 
    262   1.1    tsubai void
    263  1.15   tsutsui sc_send(struct sc_scb *scb, int chan, int ie)
    264   1.4    tsubai {
    265   1.4    tsubai 	struct sc_softc *sc = scb->scb_softc;
    266   1.4    tsubai 	struct sc_chan_stat *cs;
    267   1.4    tsubai 	struct scsipi_xfer *xs;
    268   1.4    tsubai 	int i;
    269  1.19   tsutsui 	uint8_t *p;
    270   1.4    tsubai 
    271   1.4    tsubai 	cs = &sc->chan_stat[chan];
    272   1.4    tsubai 	xs = scb->xs;
    273   1.4    tsubai 
    274  1.19   tsutsui 	p = (uint8_t *)xs->cmd;
    275   1.4    tsubai 	if (cs->scb != NULL) {
    276   1.4    tsubai 		printf("SCSI%d: sc_send() NOT NULL cs->sc\n", chan);
    277  1.16  christos 		printf("ie=0x%x scb=%p cs->sc=%p\n", ie, scb, cs->scb);
    278   1.4    tsubai 		printf("cdb=");
    279   1.4    tsubai 		for (i = 0; i < 6; i++)
    280   1.4    tsubai 			printf(" 0x%x", *p++);
    281   1.4    tsubai 		printf("\n");
    282   1.4    tsubai 		panic("SCSI soft error");
    283   1.1    tsubai 		/*NOTREACHED*/
    284   1.1    tsubai 	}
    285   1.1    tsubai 
    286   1.4    tsubai 	if (p[0] == SCOP_RESET && p[1] == SCOP_RESET) {
    287   1.1    tsubai 		/*
    288   1.1    tsubai 		 * SCSI bus reset command procedure
    289   1.1    tsubai 		 *	(vender unique by Sony Corp.)
    290   1.1    tsubai 		 */
    291   1.1    tsubai #ifdef SCSI_1185AQ
    292   1.4    tsubai 		if (sc_idenr & 0x08)
    293   1.4    tsubai 			sc->scsi_1185AQ = 1;
    294   1.4    tsubai 		else
    295   1.4    tsubai 			sc->scsi_1185AQ = 0;
    296   1.1    tsubai #endif
    297   1.4    tsubai 		cs->scb = scb;
    298   1.1    tsubai 		scsi_hardreset();
    299   1.4    tsubai 		scb->istatus = INST_EP;
    300   1.4    tsubai 		cs->scb = NULL;
    301   1.4    tsubai 		sc_done(scb);
    302   1.1    tsubai 		return;
    303   1.1    tsubai 	}
    304   1.1    tsubai 
    305   1.4    tsubai 	if (scb->sc_map && (scb->sc_map->mp_pages > 0)) {
    306   1.1    tsubai 		/*
    307   1.1    tsubai 		 * use map table
    308   1.1    tsubai 		 */
    309   1.4    tsubai 		scb->sc_coffset = scb->sc_map->mp_offset & PGOFSET;
    310   1.4    tsubai 		if (scb->sc_map->mp_pages > NSCMAP) {
    311   1.1    tsubai 			printf("SCSI%d: map table overflow\n", chan);
    312   1.4    tsubai 			scb->istatus = INST_EP|INST_LB|INST_PRE;
    313   1.1    tsubai 			return;
    314   1.1    tsubai 		}
    315   1.1    tsubai 	} else {
    316   1.1    tsubai 		/*
    317   1.1    tsubai 		 * no use map table
    318   1.1    tsubai 		 */
    319   1.4    tsubai 		scb->sc_coffset = (u_int)scb->sc_cpoint & PGOFSET;
    320   1.1    tsubai 	}
    321   1.4    tsubai 	scb->sc_ctag = 0;
    322   1.1    tsubai 
    323   1.4    tsubai 	cs->scb = scb;
    324   1.1    tsubai 	cs->comflg = OFF;
    325   1.1    tsubai 
    326   1.1    tsubai 	cs->intr_flg = ie;
    327   1.1    tsubai 	cs->chan_num = chan;
    328   1.4    tsubai 	sc->perr_flag[chan] = 0;
    329   1.4    tsubai 	sc->mout_flag[chan] = 0;
    330   1.4    tsubai 	sc->min_cnt[chan] = 0;
    331   1.4    tsubai 
    332   1.4    tsubai 	sc->sel_stat[chan] = SEL_WAIT;
    333   1.4    tsubai 	append_wb(sc, cs);
    334   1.4    tsubai 	sc_start(sc);
    335   1.1    tsubai }
    336   1.1    tsubai 
    337   1.1    tsubai /*
    338   1.1    tsubai  *	SCSI start up routine
    339   1.1    tsubai  */
    340   1.1    tsubai void
    341  1.15   tsutsui sc_start(struct sc_softc *sc)
    342   1.1    tsubai {
    343   1.4    tsubai 	struct sc_chan_stat *cs;
    344  1.19   tsutsui 	int chan, s;
    345  1.19   tsutsui 	uint8_t dummy;
    346   1.1    tsubai 
    347   1.4    tsubai 	s = splscsi();
    348   1.4    tsubai 	cs = get_wb_chan(sc);
    349   1.4    tsubai 	if ((cs == NULL) || (sc->ipc >= 0))
    350   1.1    tsubai 		goto sc_start_exit;
    351   1.4    tsubai 	chan = cs->chan_num;
    352   1.4    tsubai 	if (sc->sel_stat[chan] != SEL_WAIT) {
    353   1.1    tsubai 		/*
    354   1.1    tsubai 		 * already started
    355   1.1    tsubai 		 */
    356   1.1    tsubai 		goto sc_start_exit;
    357   1.1    tsubai 	}
    358   1.4    tsubai 	sc->sel_stat[chan] = SEL_START;
    359   1.1    tsubai 
    360   1.1    tsubai 	dummy = sc_cmonr;
    361   1.1    tsubai 	DMAC_WAIT0;
    362   1.1    tsubai 	if (dummy & (R4_MBSY|R4_MSEL)) {
    363   1.4    tsubai 		sc->sel_stat[chan] = SEL_WAIT;
    364   1.1    tsubai 		goto sc_start_exit;
    365   1.1    tsubai 	}
    366   1.1    tsubai 
    367   1.1    tsubai 	/*
    368   1.1    tsubai 	 *	send SELECT with ATN command
    369   1.1    tsubai 	 */
    370   1.4    tsubai 	sc->dma_stat = OFF;
    371   1.4    tsubai 	sc->pad_start = 0;
    372   1.1    tsubai 	dummy = sc_statr;
    373   1.1    tsubai 	DMAC_WAIT0;
    374   1.1    tsubai 	if (dummy & R0_CIP) {
    375   1.4    tsubai 		sc->sel_stat[chan] = SEL_WAIT;
    376   1.1    tsubai 		goto sc_start_exit;
    377   1.1    tsubai 	}
    378   1.1    tsubai 	sc_idenr = (chan << SC_TG_SHIFT) | SC_OWNID;
    379   1.1    tsubai 	DMAC_WAIT0;
    380   1.1    tsubai #ifdef SCSI_1185AQ
    381   1.4    tsubai 	if (sc->scsi_1185AQ)
    382   1.1    tsubai 		sc_intok1 = Ra_STO|Ra_ARBF;
    383   1.1    tsubai 	else
    384   1.1    tsubai 		sc_intok1 = Ra_STO|Ra_RSL|Ra_ARBF;
    385   1.1    tsubai #else
    386   1.1    tsubai 	sc_intok1 = Ra_STO|Ra_RSL|Ra_ARBF;
    387   1.1    tsubai #endif
    388   1.1    tsubai 	DMAC_WAIT0;
    389   1.1    tsubai 	/*
    390   1.1    tsubai 	 * BUGFIX for signal reflection on BSY
    391   1.1    tsubai 	 *	!Rb_DCNT
    392   1.1    tsubai 	 */
    393   1.1    tsubai 	sc_intok2 = Rb_FNC|Rb_SRST|Rb_PHC|Rb_SPE;
    394   1.1    tsubai 	DMAC_WAIT0;
    395   1.1    tsubai 
    396   1.1    tsubai 	dummy = sc_cmonr;
    397   1.1    tsubai 	DMAC_WAIT0;
    398   1.1    tsubai 	if (dummy & (R4_MBSY|R4_MSEL)) {
    399   1.4    tsubai 		sc->sel_stat[chan] = SEL_WAIT;
    400   1.1    tsubai 		goto sc_start_exit;
    401   1.1    tsubai 	}
    402   1.4    tsubai 	SET_CMD(sc, SCMD_SEL_ATN);
    403   1.1    tsubai 
    404   1.1    tsubai sc_start_exit:
    405   1.1    tsubai 	splx(s);
    406   1.1    tsubai }
    407   1.1    tsubai 
    408   1.1    tsubai /*
    409   1.1    tsubai  *	SCSI interrupt service routine
    410   1.1    tsubai  */
    411   1.1    tsubai int
    412  1.15   tsutsui scintr(void)
    413   1.1    tsubai {
    414  1.15   tsutsui 	int iloop;
    415  1.19   tsutsui 	int chan;
    416  1.19   tsutsui 	uint8_t dummy;
    417   1.4    tsubai 	struct sc_softc *sc;
    418   1.4    tsubai 	struct sc_chan_stat *cs;
    419  1.19   tsutsui 	uint8_t s_int1, s_int2;
    420   1.1    tsubai 
    421  1.19   tsutsui 	sc = device_lookup_private(&sc_cd, 0);		/* XXX */
    422   1.4    tsubai 
    423   1.1    tsubai scintr_loop:
    424   1.1    tsubai 
    425   1.1    tsubai #if defined(CHECK_MRQ) && defined(news3400)
    426   1.1    tsubai 	while (dmac_gstat & CH_MRQ(CH_SCSI))
    427   1.1    tsubai 		DMAC_WAIT;
    428   1.1    tsubai #endif
    429   1.1    tsubai 
    430   1.1    tsubai 	for (iloop = 0; iloop < 100; iloop++) {
    431   1.1    tsubai 		dummy = sc_statr;
    432   1.1    tsubai 		DMAC_WAIT;
    433   1.1    tsubai 		if ((dummy & R0_CIP) == 0)
    434   1.1    tsubai 			break;
    435   1.1    tsubai 	}
    436   1.1    tsubai 
    437   1.1    tsubai 	/*
    438   1.1    tsubai 	 * get SCSI interrupt request
    439   1.1    tsubai 	 */
    440   1.1    tsubai 	while (sc_statr & R0_MIRQ) {
    441   1.1    tsubai 		DMAC_WAIT0;
    442   1.1    tsubai 		s_int1 = sc_intrq1;
    443   1.1    tsubai 		DMAC_WAIT0;
    444   1.1    tsubai 		s_int2 = sc_intrq2;
    445   1.1    tsubai 		DMAC_WAIT0;
    446   1.4    tsubai 		sc->int_stat1 |= s_int1;
    447   1.4    tsubai 		sc->int_stat2 |= s_int2;
    448   1.1    tsubai 	}
    449   1.1    tsubai 
    450   1.4    tsubai 	if (sc->int_stat2 & R3_SRST) {
    451   1.1    tsubai 		/*
    452   1.1    tsubai 		 * RST signal is drived
    453   1.1    tsubai 		 */
    454   1.4    tsubai 		sc->int_stat2 &= ~R3_SRST;
    455   1.4    tsubai 		scsi_softreset(sc);
    456   1.1    tsubai 		goto scintr_exit;
    457   1.1    tsubai 	}
    458   1.1    tsubai 
    459   1.4    tsubai 	if ((sc->ipc < 0) && (sc->wrc <= 0) && (sc->wbc <= 0)) {
    460   1.4    tsubai 		sc->int_stat1 = 0;
    461   1.4    tsubai 		sc->int_stat2 = 0;
    462   1.1    tsubai 		goto scintr_exit;
    463   1.1    tsubai 	}
    464   1.1    tsubai 
    465   1.4    tsubai 	cs = get_wb_chan(sc);
    466  1.19   tsutsui 	if (cs)
    467  1.19   tsutsui 		chan = cs->chan_num;
    468   1.4    tsubai 
    469   1.4    tsubai 	if (cs && (sc->sel_stat[chan] == SEL_START) &&
    470   1.4    tsubai 		(sc->lastcmd == SCMD_SEL_ATN)) {
    471   1.1    tsubai 		/*
    472   1.1    tsubai 		 *	Check the result of SELECTION command
    473   1.1    tsubai 		 */
    474   1.4    tsubai 		if (sc->int_stat1 & R2_RSL) {
    475   1.1    tsubai 			/*
    476   1.1    tsubai 			 * RESELECTION occur
    477   1.1    tsubai 			 */
    478   1.4    tsubai 			if (sc->wrc > 0) {
    479   1.4    tsubai 				sc->sel_stat[chan] = SEL_RSLD;
    480   1.1    tsubai 			} else {
    481   1.1    tsubai 				/*
    482   1.1    tsubai 				 * Ghost RESELECTION ???
    483   1.1    tsubai 				 */
    484   1.4    tsubai 				sc->int_stat1 &= ~R2_RSL;
    485   1.1    tsubai 			}
    486   1.1    tsubai 		}
    487   1.4    tsubai 		if (sc->int_stat1 & R2_ARBF) {
    488   1.1    tsubai 			/*
    489   1.1    tsubai 			 * ARBITRATION fault
    490   1.1    tsubai 			 */
    491   1.4    tsubai 			sc->int_stat1 &= ~R2_ARBF;
    492   1.4    tsubai 			sc->sel_stat[chan] = SEL_ARBF;
    493   1.1    tsubai 		}
    494   1.4    tsubai 		if (sc->int_stat1 & R2_STO) {
    495   1.1    tsubai 			/*
    496   1.1    tsubai 			 * SELECTION timeout
    497   1.1    tsubai 			 */
    498   1.4    tsubai 			sc->int_stat1 &= ~R2_STO;
    499  1.15   tsutsui 			if ((sc->int_stat2&(R3_PHC|R3_RMSG)) !=
    500  1.15   tsutsui 			     (R3_PHC|R3_RMSG)) {
    501   1.4    tsubai 				sc->ipc = chan;
    502   1.4    tsubai 				sc->ip = &sc->chan_stat[chan];
    503   1.4    tsubai 				sc->sel_stat[chan] = SEL_TIMEOUT;
    504   1.4    tsubai 				sc->chan_stat[chan].scb->istatus
    505   1.1    tsubai 					= INST_EP|INST_TO;
    506   1.4    tsubai 				release_wb(sc);
    507   1.1    tsubai 			}
    508   1.1    tsubai 		}
    509   1.1    tsubai 
    510   1.1    tsubai 		/*
    511   1.1    tsubai 		 *	SELECTION command done
    512   1.1    tsubai 		 */
    513   1.4    tsubai 		switch (sc->sel_stat[chan]) {
    514   1.1    tsubai 
    515   1.1    tsubai 		case SEL_START:
    516   1.4    tsubai 			if ((sc->int_stat2 & R3_FNC) == 0)
    517   1.1    tsubai 				break;
    518   1.1    tsubai 			/*
    519   1.1    tsubai 			 * SELECTION success
    520   1.1    tsubai 			 */
    521   1.1    tsubai 			sc_intok2 = Rb_FNC|Rb_DCNT|Rb_SRST|Rb_PHC|Rb_SPE;
    522   1.4    tsubai 			sc->ipc = chan;
    523   1.4    tsubai 			sc->ip = &sc->chan_stat[chan];
    524   1.4    tsubai 			sc->ip->scb->istatus |= INST_IP;
    525   1.4    tsubai 			sc->dma_stat = OFF;
    526   1.4    tsubai 			sc->pad_start = 0;
    527   1.4    tsubai 			sc->sel_stat[chan] = SEL_SUCCESS;
    528   1.4    tsubai 			release_wb(sc);
    529   1.1    tsubai #ifndef NOT_SUPPORT_SYNCTR
    530   1.4    tsubai 			sc_syncr = sc->sync_tr[chan];
    531   1.1    tsubai 			DMAC_WAIT0;
    532   1.1    tsubai #endif
    533   1.1    tsubai 			DMAC_WAIT0;
    534   1.1    tsubai 			break;
    535   1.1    tsubai 
    536   1.1    tsubai 		case SEL_TIMEOUT:
    537   1.1    tsubai 			/*
    538   1.1    tsubai 			 * SELECTION time out
    539   1.1    tsubai 			 */
    540   1.4    tsubai 			sc_discon(sc);
    541   1.1    tsubai 			goto scintr_exit;
    542   1.1    tsubai 
    543   1.1    tsubai 		/* case SEL_RSLD: */
    544   1.1    tsubai 		/* case SEL_ARBF: */
    545   1.1    tsubai 		default:
    546   1.1    tsubai 			/*
    547   1.1    tsubai 			 * SELECTION failed
    548   1.1    tsubai 			 */
    549   1.4    tsubai 			sc->sel_stat[chan] = SEL_WAIT;
    550   1.1    tsubai 			break;
    551   1.1    tsubai 		}
    552   1.4    tsubai 		if ((sc->int_stat1 & R2_RSL) == 0)
    553   1.4    tsubai 			sc->int_stat2 &= ~R3_FNC;
    554   1.1    tsubai 	}
    555   1.1    tsubai 
    556   1.4    tsubai 	if (sc->ip != NULL) {
    557   1.1    tsubai 		/*
    558   1.1    tsubai 		 * check In Process channel's request
    559   1.1    tsubai 		 */
    560   1.4    tsubai 		if (sc->dma_stat != OFF) {
    561   1.1    tsubai 			/*
    562   1.1    tsubai 			 * adjust pointer & counter
    563   1.1    tsubai 			 */
    564   1.4    tsubai 			adjust_transfer(sc, sc->ip);
    565   1.1    tsubai 		}
    566   1.4    tsubai 		if (sc->int_stat2 & R3_SPE) {
    567  1.15   tsutsui 			int volatile statr;
    568  1.15   tsutsui 			int volatile cmonr;
    569   1.1    tsubai 
    570   1.1    tsubai 			statr = sc_statr;
    571  1.21  christos 			__USE(statr);
    572   1.1    tsubai 			DMAC_WAIT0;
    573   1.1    tsubai 			cmonr = sc_cmonr;
    574  1.21  christos 			__USE(cmonr);
    575   1.4    tsubai 			sc->int_stat2 &= ~R3_SPE;
    576   1.4    tsubai 			sc->perr_flag[sc->ip->chan_num] = 1;
    577   1.1    tsubai 		}
    578   1.1    tsubai 	}
    579   1.1    tsubai 
    580   1.4    tsubai 	if (sc->int_stat2 & R3_DCNT) {
    581   1.1    tsubai 		/*
    582   1.1    tsubai 		 * Bus Free
    583   1.1    tsubai 		 */
    584   1.4    tsubai 		sc_discon(sc);
    585   1.4    tsubai 		sc->int_stat2 &= ~R3_DCNT;
    586   1.1    tsubai 	}
    587   1.1    tsubai 
    588   1.4    tsubai 	if ((sc->ipc >= 0) && (sc->sel_stat[sc->ipc] == SEL_RSL_WAIT)) {
    589   1.4    tsubai 		sc->sel_stat[sc->ipc] = SEL_RSLD;
    590   1.4    tsubai 		sc->ipc = -1;
    591   1.4    tsubai 		sc->int_stat1 |= R2_RSL;
    592   1.1    tsubai 	}
    593   1.4    tsubai 	if (sc->int_stat1 & R2_RSL) {
    594   1.1    tsubai 		/*
    595   1.1    tsubai 		 * Reselection
    596   1.1    tsubai 		 */
    597   1.4    tsubai 		sc_resel(sc);
    598   1.4    tsubai 		sc->int_stat1 &= ~R2_RSL;
    599   1.4    tsubai 		if (sc->sel_stat[sc->ipc] == SEL_RSL_WAIT)
    600   1.1    tsubai 			goto scintr_exit;
    601   1.1    tsubai 	}
    602   1.1    tsubai 
    603   1.1    tsubai 
    604   1.4    tsubai 	if ((sc->ipc >= 0) && (sc->ipc != SC_OWNID) &&
    605   1.4    tsubai 	    (sc->sel_stat[sc->ipc] == SEL_SUCCESS)) {
    606   1.4    tsubai 		if (sc->int_stat2 & R3_PHC) {
    607   1.1    tsubai 			/*
    608   1.1    tsubai 			 * Phase change
    609   1.1    tsubai 			 */
    610   1.4    tsubai 			sc->int_stat2 &= ~(R3_PHC|R3_RMSG);
    611   1.4    tsubai 			sc_pmatch(sc);
    612   1.4    tsubai 		} else if (sc->int_stat2 & R3_RMSG) {
    613   1.1    tsubai 			/*
    614   1.1    tsubai 			 * message Phase
    615   1.1    tsubai 			 */
    616   1.4    tsubai 			if (sc->min_flag > 0) {
    617   1.4    tsubai 				sc->int_stat2 &= ~(R3_PHC|R3_RMSG);
    618   1.4    tsubai 				sc_pmatch(sc);
    619   1.1    tsubai 			}
    620   1.1    tsubai 		}
    621   1.4    tsubai 		else if (sc->dma_stat != OFF) {
    622   1.1    tsubai 			dummy = sc_cmonr;
    623   1.1    tsubai 			DMAC_WAIT0;
    624   1.1    tsubai 			if ((dummy & (R4_MMSG|R4_MCD|R4_MREQ)) == R4_MREQ) {
    625   1.1    tsubai 				/*
    626   1.1    tsubai 				 * still DATA transfer phase
    627   1.1    tsubai 				 */
    628   1.4    tsubai 				sc_dio_pad(sc, sc->ip);
    629   1.1    tsubai 			}
    630   1.1    tsubai 		}
    631   1.4    tsubai 		else if (sc->ip->comflg == CF_SEND) {
    632   1.1    tsubai 			dummy = sc_cmonr;
    633   1.1    tsubai 			DMAC_WAIT0;
    634   1.1    tsubai 			if ((dummy & SC_PMASK) == COM_OUT) {
    635   1.1    tsubai 				/*
    636   1.1    tsubai 				 * command out phase
    637   1.1    tsubai 				 */
    638   1.4    tsubai 				sc_cout(sc, sc->ip);
    639   1.1    tsubai 			}
    640   1.1    tsubai 		}
    641   1.1    tsubai 	} else {
    642   1.4    tsubai 		if (sc->int_stat2 & (R3_PHC|R3_RMSG))
    643   1.1    tsubai 			goto scintr_exit;
    644   1.1    tsubai 	}
    645   1.1    tsubai 
    646   1.4    tsubai 	if ((sc->int_stat1 & (R2_STO|R2_RSL|R2_ARBF))
    647   1.4    tsubai 	    || (sc->int_stat2 & (R3_DCNT|R3_SRST|R3_PHC|R3_SPE))) {
    648   1.1    tsubai 		/*
    649   1.1    tsubai 		 * still remain intrq
    650   1.1    tsubai 		 */
    651   1.1    tsubai 		goto scintr_loop;
    652   1.1    tsubai 	}
    653   1.1    tsubai 
    654   1.1    tsubai scintr_exit:
    655  1.15   tsutsui 	return 1;
    656   1.1    tsubai }
    657   1.1    tsubai 
    658   1.1    tsubai /*
    659   1.1    tsubai  *	SCSI bus reset routine
    660   1.1    tsubai  *		scsi_hardreset() is occered a reset interrupt.
    661   1.1    tsubai  *		And call scsi_softreset().
    662   1.1    tsubai  */
    663   1.1    tsubai void
    664  1.15   tsutsui scsi_hardreset(void)
    665   1.1    tsubai {
    666  1.15   tsutsui 	int s;
    667   1.1    tsubai #ifdef DMAC_MAP_INIT
    668  1.15   tsutsui 	int i;
    669   1.1    tsubai #endif
    670   1.4    tsubai 	struct sc_softc *sc;
    671   1.1    tsubai 
    672  1.19   tsutsui 	sc = device_lookup_private(&sc_cd, 0);	/* XXX */
    673   1.1    tsubai 	s = splscsi();
    674   1.1    tsubai 
    675   1.4    tsubai 	scsi_chipreset(sc);
    676   1.1    tsubai 	DMAC_WAIT0;
    677   1.4    tsubai 	sc->int_stat1 = 0;
    678   1.4    tsubai 	sc->int_stat2 = 0;
    679   1.4    tsubai 	SET_CMD(sc, SCMD_AST_RST);			/* assert RST signal */
    680   1.1    tsubai 
    681   1.1    tsubai #ifdef DMAC_MAP_INIT
    682   1.1    tsubai 	if (dmac_map_init == 0) {
    683   1.1    tsubai 		dmac_map_init++;
    684   1.1    tsubai 		for (i = 0; i < NDMACMAP; i++) {
    685   1.9   thorpej # if defined(__mips__) && defined(CPU_SINGLE)
    686   1.1    tsubai 			dmac_gsel = CH_SCSI;
    687  1.19   tsutsui 			dmac_ctag = (uint8_t)i;
    688  1.19   tsutsui 			dmac_cmap = (uint16_t)0;
    689   1.1    tsubai # endif
    690   1.1    tsubai 		}
    691   1.1    tsubai 	}
    692   1.1    tsubai #endif
    693   1.1    tsubai 	/*cxd1185_init();*/
    694   1.1    tsubai 	splx(s);
    695   1.1    tsubai }
    696   1.1    tsubai 
    697   1.1    tsubai /*
    698   1.1    tsubai  * I/O port (sc_ioptr) bit assign
    699  1.11   tsutsui  *
    700   1.1    tsubai  *	Rf_PRT3		-	<reserved>
    701   1.1    tsubai  *	Rf_PRT2		-	<reserved>
    702   1.1    tsubai  *	Rf_PRT1		out	Floppy Disk Density control
    703   1.1    tsubai  *	Rf_PRT0		out	Floppy Disk Eject control
    704   1.1    tsubai  */
    705   1.1    tsubai 
    706   1.1    tsubai void
    707  1.15   tsutsui scsi_chipreset(struct sc_softc *sc)
    708   1.1    tsubai {
    709  1.15   tsutsui 	int s;
    710  1.19   tsutsui 	uint8_t save_ioptr;
    711   1.1    tsubai 
    712   1.1    tsubai 	s = splscsi();
    713   1.1    tsubai 
    714   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
    715   1.1    tsubai 	dmac_gsel = CH_SCSI;
    716   1.1    tsubai 	dmac_cwid = 4;				/* initialize DMAC SCSI chan */
    717  1.19   tsutsui 	*(volatile uint8_t *)PINTEN |= DMA_INTEN;
    718   1.1    tsubai 	dma_reset(CH_SCSI);
    719   1.1    tsubai #endif
    720   1.1    tsubai 	sc_envir = 0;				/* 1/4 clock */
    721   1.1    tsubai 	DMAC_WAIT0;
    722   1.1    tsubai 	save_ioptr = sc_ioptr;
    723   1.1    tsubai 	DMAC_WAIT0;
    724   1.4    tsubai 	sc->lastcmd = SCMD_CHIP_RST;
    725   1.1    tsubai 	sc_comr = SCMD_CHIP_RST;		/* reset chip */
    726   1.1    tsubai 	DMAC_WAIT;
    727  1.15   tsutsui 	(void)WAIT_STATR_BITCLR(R0_CIP);
    728   1.1    tsubai 	/*
    729   1.1    tsubai 	 * SCMD_CHIP_RST command reset all register
    730   1.1    tsubai 	 *				except sc_statr<7:6> & sc_cmonr.
    731   1.1    tsubai 	 * So, bit R0_MIRQ & R3_FNC will be not set.
    732   1.1    tsubai 	 */
    733   1.1    tsubai 	sc_idenr = SC_OWNID;
    734   1.1    tsubai 	DMAC_WAIT0;
    735   1.1    tsubai 
    736   1.1    tsubai 	sc_intok1 = Ra_STO|Ra_RSL|Ra_ARBF;
    737   1.1    tsubai 	DMAC_WAIT0;
    738   1.1    tsubai 	sc_intok2 = Rb_FNC|Rb_SRST|Rb_PHC|Rb_SPE|Rb_RMSG;
    739   1.1    tsubai 	DMAC_WAIT0;
    740   1.1    tsubai 
    741   1.1    tsubai 	sc_ioptr = save_ioptr;
    742   1.1    tsubai 	DMAC_WAIT;
    743   1.1    tsubai 
    744   1.1    tsubai 	sc_moder = Rc_TMSL;			/* RST drive time = 25.5 us */
    745   1.1    tsubai 	DMAC_WAIT0;
    746   1.1    tsubai 	sc_timer = 0x2;
    747   1.1    tsubai 	DMAC_WAIT0;
    748   1.1    tsubai 
    749   1.1    tsubai 	sc_moder = Rc_SPHI;			/* selection timeout = 252 ms */
    750   1.1    tsubai 	DMAC_WAIT0;
    751   1.1    tsubai 	sc_timer = SEL_TIMEOUT_VALUE;
    752   1.1    tsubai 	DMAC_WAIT0;
    753   1.1    tsubai 
    754   1.1    tsubai #ifdef SCSI_1185AQ
    755   1.4    tsubai 	if (sc->scsi_1185AQ)
    756   1.4    tsubai 		SET_CMD(sc, SCMD_ENB_SEL);		/* enable reselection */
    757   1.1    tsubai #endif
    758   1.1    tsubai 
    759   1.4    tsubai 	sc->int_stat1 &= ~R2_RSL;		/* ignore RSL inter request */
    760   1.1    tsubai 
    761   1.1    tsubai 	splx(s);
    762   1.1    tsubai }
    763   1.1    tsubai 
    764   1.1    tsubai void
    765  1.15   tsutsui scsi_softreset(struct sc_softc *sc)
    766   1.1    tsubai {
    767  1.19   tsutsui 	struct sc_chan_stat *cs;
    768   1.4    tsubai 	int i;
    769  1.15   tsutsui 	/* int (*handler)(); */
    770   1.1    tsubai 
    771   1.4    tsubai 	sc->wbq_actf = NULL;
    772   1.4    tsubai 	sc->wbq_actl = NULL;
    773   1.4    tsubai 	sc->wbc = 0;
    774   1.4    tsubai 	sc->wrc = 0;
    775   1.4    tsubai 	sc->ip = NULL;
    776   1.4    tsubai 	sc->ipc = -1;
    777   1.4    tsubai 	sc->dma_stat = OFF;
    778   1.4    tsubai 	sc->pad_start = 0;
    779   1.1    tsubai 
    780   1.1    tsubai 	for (i = 0; i < NTARGET; ++i) {
    781   1.1    tsubai 		if (i == SC_OWNID)
    782   1.1    tsubai 			continue;
    783   1.4    tsubai 		cs = &sc->chan_stat[i];
    784   1.1    tsubai 		cs->wb_next = NULL;
    785   1.1    tsubai #ifndef NOT_SUPPORT_SYNCTR
    786   1.4    tsubai 		sc->sync_tr[i] = 0;		/* asynchronous mode */
    787   1.1    tsubai #endif
    788   1.4    tsubai 		sc->sel_stat[i] = SEL_WAIT;
    789   1.4    tsubai 		if (cs->scb != NULL) {
    790   1.4    tsubai 			struct sc_scb *scb = cs->scb;
    791   1.4    tsubai 
    792   1.4    tsubai 			if ((cs->scb->istatus & INST_EP) == 0)
    793   1.4    tsubai 				cs->scb->istatus = (INST_EP|INST_HE);
    794   1.4    tsubai 			cs->scb = NULL;
    795   1.9   thorpej #ifdef __mips__
    796   1.4    tsubai 			clean_k2dcache(scb);
    797   1.4    tsubai #endif
    798   1.4    tsubai 			if (cs->intr_flg == SCSI_INTEN) {
    799   1.4    tsubai 				intrcnt[SCSI_INTR]++;
    800   1.1    tsubai #if 0
    801   1.4    tsubai 				handler = scintsw[i].sci_inthandler;
    802   1.4    tsubai 				if (handler)
    803   1.4    tsubai 					(*handler)(scintsw[i].sci_ctlr);
    804   1.1    tsubai #endif
    805   1.1    tsubai 			}
    806   1.4    tsubai 			sc_done(scb);
    807   1.1    tsubai 		}
    808   1.1    tsubai 	}
    809   1.1    tsubai }
    810   1.1    tsubai 
    811   1.1    tsubai /*
    812   1.1    tsubai  *	RESELECTION interrupt service routine
    813   1.1    tsubai  *		( RESELECTION phase )
    814   1.1    tsubai  */
    815   1.1    tsubai void
    816  1.15   tsutsui sc_resel(struct sc_softc *sc)
    817   1.1    tsubai {
    818  1.15   tsutsui 	struct sc_chan_stat *cs;
    819  1.19   tsutsui 	uint8_t chan;
    820  1.19   tsutsui 	uint8_t statr;
    821  1.15   tsutsui 	int iloop;
    822   1.1    tsubai 
    823   1.4    tsubai 	sc->min_flag = 0;
    824   1.1    tsubai 	chan = (sc_idenr & R6_SID_MASK) >> SC_TG_SHIFT;
    825   1.1    tsubai 
    826   1.1    tsubai 	if (chan == SC_OWNID)
    827   1.1    tsubai 		return;
    828   1.1    tsubai 
    829   1.1    tsubai 	statr = sc_statr;
    830   1.1    tsubai 	DMAC_WAIT0;
    831   1.1    tsubai 	if (statr & R0_CIP) {
    832   1.4    tsubai 		if (sc->lastcmd == SCMD_SEL_ATN) {
    833   1.1    tsubai 			/*
    834   1.1    tsubai 			 * SELECTION command dead lock ?
    835   1.1    tsubai 			 *	save interrupt request
    836   1.1    tsubai 			 */
    837   1.1    tsubai 			while (sc_statr & R0_MIRQ) {
    838   1.1    tsubai 				DMAC_WAIT0;
    839   1.4    tsubai 				sc->int_stat1 |= sc_intrq1;
    840   1.1    tsubai 				DMAC_WAIT0;
    841   1.4    tsubai 				sc->int_stat2 |= sc_intrq2;
    842   1.1    tsubai 				DMAC_WAIT0;
    843   1.1    tsubai 			}
    844   1.4    tsubai 			scsi_chipreset(sc);
    845   1.1    tsubai 		}
    846   1.1    tsubai 	}
    847   1.1    tsubai 
    848   1.4    tsubai 	cs = &sc->chan_stat[chan];
    849   1.4    tsubai 	if (cs->scb == NULL) {
    850   1.1    tsubai 		scsi_hardreset();
    851   1.1    tsubai 		return;
    852   1.1    tsubai 	}
    853   1.4    tsubai 	if ((cs->scb->istatus & INST_WR) == 0) {
    854   1.1    tsubai 		scsi_hardreset();
    855   1.1    tsubai 		return;
    856   1.1    tsubai 	}
    857   1.1    tsubai 
    858   1.4    tsubai 	if (sc->ipc >= 0) {
    859   1.1    tsubai 		scsi_hardreset();
    860   1.1    tsubai 		return;
    861   1.1    tsubai 	}
    862   1.1    tsubai 
    863   1.4    tsubai 	sc->ip = cs;
    864   1.4    tsubai 	sc->ipc = chan;
    865   1.1    tsubai 
    866   1.1    tsubai 	sc_intok2 = Rb_FNC|Rb_DCNT|Rb_SRST|Rb_PHC|Rb_SPE;
    867   1.1    tsubai 	DMAC_WAIT0;
    868   1.1    tsubai 
    869   1.1    tsubai 	iloop = 0;
    870   1.4    tsubai 	while ((sc->int_stat2 & R3_FNC) == 0) {
    871   1.1    tsubai 		/*
    872   1.1    tsubai 		 * Max 6 usec wait
    873   1.1    tsubai 		 */
    874   1.1    tsubai 		if (iloop++ > RSL_LOOP_CNT) {
    875   1.4    tsubai 			sc->sel_stat[chan] = SEL_RSL_WAIT;
    876   1.1    tsubai 			return;
    877   1.1    tsubai 		}
    878   1.4    tsubai 		GET_INTR(&sc->int_stat1, &sc->int_stat2);
    879   1.1    tsubai 	}
    880   1.4    tsubai 	sc->int_stat2 &= ~R3_FNC;
    881  1.11   tsutsui 
    882   1.4    tsubai 	sc->sel_stat[chan] = SEL_SUCCESS;
    883   1.1    tsubai 
    884   1.4    tsubai 	sc->wrc--;
    885   1.4    tsubai 	sc->dma_stat = OFF;
    886   1.4    tsubai 	sc->pad_start = 0;
    887   1.4    tsubai 	cs->scb->istatus |= INST_IP;
    888   1.4    tsubai 	cs->scb->istatus &= ~INST_WR;
    889   1.1    tsubai 
    890   1.1    tsubai #ifndef NOT_SUPPORT_SYNCTR
    891   1.4    tsubai 	sc_syncr = sc->sync_tr[chan];
    892   1.1    tsubai 	DMAC_WAIT0;
    893   1.1    tsubai #endif
    894   1.1    tsubai }
    895   1.1    tsubai 
    896   1.1    tsubai /*
    897   1.1    tsubai  *	DISCONNECT interrupt service routine
    898   1.1    tsubai  *		( Target disconnect / job done )
    899   1.1    tsubai  */
    900   1.1    tsubai void
    901  1.15   tsutsui sc_discon(struct sc_softc *sc)
    902   1.1    tsubai {
    903  1.19   tsutsui 	struct sc_chan_stat *cs;
    904  1.15   tsutsui 	/* int (*handler)(); */
    905  1.19   tsutsui 	uint8_t dummy;
    906   1.1    tsubai 
    907   1.1    tsubai 	/*
    908   1.7       wiz 	 * Signal reflection on BSY has occurred.
    909   1.1    tsubai 	 *	Not Bus Free Phase, ignore.
    910   1.1    tsubai 	 *
    911   1.1    tsubai 	 *	But, CXD1185Q reset INIT bit of sc_statr.
    912   1.1    tsubai 	 *	So, can't issue Transfer Information command.
    913  1.11   tsutsui 	 *
    914   1.1    tsubai 	 *	What shall we do ?  Bus reset ?
    915   1.1    tsubai 	 */
    916   1.4    tsubai 	if ((sc->int_stat2 & R3_DCNT) && ((sc_intok2 & Rb_DCNT) == 0))
    917   1.1    tsubai 		return;
    918   1.1    tsubai 
    919   1.1    tsubai 	sc_intok2 = Rb_FNC|Rb_SRST|Rb_PHC|Rb_SPE;
    920   1.1    tsubai 	DMAC_WAIT0;
    921   1.1    tsubai 
    922   1.4    tsubai 	sc->min_flag = 0;
    923   1.1    tsubai 	dummy = sc_cmonr;
    924   1.1    tsubai 	DMAC_WAIT0;
    925   1.1    tsubai 	if (dummy & R4_MATN) {
    926   1.4    tsubai 		SET_CMD(sc, SCMD_NGT_ATN);
    927   1.1    tsubai 		(void) WAIT_STATR_BITSET(R0_MIRQ);
    928   1.4    tsubai 		GET_INTR(&sc->int_stat1, &sc->int_stat2); /* clear interrupt */
    929   1.1    tsubai 	}
    930   1.1    tsubai 
    931   1.4    tsubai 	if ((sc->int_stat1 & R2_RSL) == 0)
    932   1.4    tsubai 		sc->int_stat2 &= ~R3_FNC;
    933   1.1    tsubai 
    934   1.4    tsubai 	cs = sc->ip;
    935   1.4    tsubai 	if ((cs == NULL) || (sc->ipc < 0))
    936   1.1    tsubai 		goto sc_discon_exit;
    937   1.1    tsubai 
    938   1.4    tsubai 	if ((sc->sel_stat[cs->chan_num] != SEL_SUCCESS)
    939   1.4    tsubai 			&& (sc->sel_stat[cs->chan_num] != SEL_TIMEOUT))
    940  1.19   tsutsui 		printf("%s: eh!\n", __func__);
    941   1.1    tsubai 
    942   1.1    tsubai 	/*
    943   1.1    tsubai 	 * indicate abnormal terminate
    944   1.1    tsubai 	 */
    945   1.4    tsubai 	if ((cs->scb->istatus & (INST_EP|INST_WR)) == 0)
    946   1.4    tsubai 		cs->scb->istatus |= (INST_EP|INST_PRE|INST_LB);
    947   1.1    tsubai 
    948   1.4    tsubai 	cs->scb->istatus &= ~INST_IP;
    949   1.4    tsubai 	sc->dma_stat = OFF;
    950   1.4    tsubai 	sc->pad_start = 0;
    951   1.4    tsubai 	sc->ip = NULL;
    952   1.4    tsubai 	sc->ipc = -1;
    953   1.4    tsubai 
    954   1.4    tsubai 	if ((cs->scb->istatus & INST_WR) == 0) {
    955   1.4    tsubai 		struct sc_scb *scb = cs->scb;
    956   1.4    tsubai 
    957   1.4    tsubai 		if (sc->perr_flag[cs->chan_num] > 0)
    958   1.4    tsubai 			cs->scb->istatus |= INST_EP|INST_PRE;
    959   1.4    tsubai 		cs->scb = NULL;
    960   1.9   thorpej #ifdef __mips__
    961   1.4    tsubai 		clean_k2dcache(scb);
    962   1.4    tsubai #endif
    963   1.4    tsubai 		if (cs->intr_flg == SCSI_INTEN) {
    964   1.4    tsubai 			intrcnt[SCSI_INTR]++;
    965   1.1    tsubai #if 0
    966   1.4    tsubai 			handler = scintsw[cs->chan_num].sci_inthandler;
    967   1.4    tsubai 			if (handler)
    968   1.4    tsubai 				(*handler)(scintsw[cs->chan_num].sci_ctlr);
    969   1.1    tsubai #endif
    970   1.1    tsubai 		}
    971   1.4    tsubai 		sc_done(scb);
    972   1.1    tsubai 	}
    973   1.1    tsubai 
    974   1.1    tsubai sc_discon_exit:
    975   1.4    tsubai 	sc_start(sc);
    976   1.1    tsubai }
    977   1.1    tsubai 
    978   1.1    tsubai /*
    979   1.1    tsubai  *	SCSI phase match interrupt service routine
    980   1.1    tsubai  */
    981   1.1    tsubai void
    982  1.15   tsutsui sc_pmatch(struct sc_softc *sc)
    983   1.1    tsubai {
    984   1.4    tsubai 	struct sc_chan_stat *cs;
    985  1.19   tsutsui 	uint8_t phase;
    986  1.19   tsutsui 	uint8_t phase2;
    987  1.19   tsutsui 	uint8_t cmonr;
    988   1.1    tsubai 
    989   1.4    tsubai 	sc->int_stat2 &= ~R3_FNC;			/* XXXXXXXX */
    990   1.1    tsubai 
    991   1.4    tsubai 	cs = sc->ip;
    992   1.1    tsubai 	if (cs == NULL)
    993   1.1    tsubai 		return;
    994   1.1    tsubai 
    995   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
    996   1.1    tsubai 	dma_reset(CH_SCSI);
    997   1.4    tsubai #endif
    998   1.1    tsubai 	phase = sc_cmonr & SC_PMASK;
    999   1.1    tsubai 	DMAC_WAIT0;
   1000   1.1    tsubai 	for (;;) {
   1001   1.1    tsubai 		phase2 = phase;
   1002   1.1    tsubai 		cmonr = sc_cmonr;
   1003   1.1    tsubai 		DMAC_WAIT0;
   1004   1.1    tsubai 		phase = cmonr & SC_PMASK;
   1005   1.1    tsubai 		if (phase == phase2) {
   1006   1.1    tsubai 			if ((phase == DAT_IN) || (phase == DAT_OUT))
   1007   1.1    tsubai 				break;
   1008   1.1    tsubai 			else if (cmonr & R4_MREQ)
   1009   1.1    tsubai 				break;
   1010   1.1    tsubai 		}
   1011   1.1    tsubai 	}
   1012   1.1    tsubai 
   1013   1.1    tsubai 
   1014   1.4    tsubai 	sc->dma_stat = OFF;
   1015   1.4    tsubai 	sc->pad_start = 0;
   1016   1.1    tsubai 
   1017   1.1    tsubai 	if (phase == COM_OUT) {
   1018   1.4    tsubai 		sc->min_flag = 0;
   1019   1.1    tsubai 		if (cs->comflg != CF_SEND)
   1020   1.1    tsubai 			cs->comflg = CF_SET;
   1021   1.4    tsubai 		sc_cout(sc, cs);
   1022   1.1    tsubai 	} else {
   1023   1.1    tsubai 		cs->comflg = CF_ENOUGH;
   1024   1.1    tsubai 		sc_intok2 &= ~Rb_FNC;
   1025   1.1    tsubai 		if (phase == MES_IN) {
   1026   1.4    tsubai 			sc->min_flag++;
   1027   1.4    tsubai 			sc_min(sc, cs);
   1028   1.1    tsubai 		} else {
   1029   1.4    tsubai 			sc->min_flag = 0;
   1030   1.1    tsubai 
   1031   1.1    tsubai 			switch (phase) {
   1032   1.1    tsubai 
   1033   1.1    tsubai 			case MES_OUT:
   1034   1.4    tsubai 				sc_mout(sc, cs);
   1035   1.1    tsubai 				break;
   1036   1.1    tsubai 
   1037   1.1    tsubai 			case DAT_IN:
   1038   1.1    tsubai 			case DAT_OUT:
   1039   1.4    tsubai 				sc_dio(sc, cs);
   1040   1.1    tsubai 				break;
   1041   1.1    tsubai 
   1042   1.1    tsubai 			case STAT_IN:
   1043   1.4    tsubai 				sc_sin(sc, cs);
   1044   1.1    tsubai 				break;
   1045   1.1    tsubai 
   1046   1.1    tsubai 			default:
   1047   1.1    tsubai 				printf("SCSI%d: unknown phase\n", cs->chan_num);
   1048   1.1    tsubai 				break;
   1049   1.1    tsubai 			}
   1050   1.1    tsubai 		}
   1051   1.1    tsubai 	}
   1052   1.1    tsubai }
   1053   1.1    tsubai 
   1054   1.1    tsubai 
   1055   1.1    tsubai void
   1056  1.15   tsutsui flush_fifo(struct sc_softc *sc)
   1057   1.1    tsubai {
   1058  1.19   tsutsui 	uint8_t dummy;
   1059  1.19   tsutsui 	uint8_t tmp;
   1060  1.19   tsutsui 	uint8_t tmp0;
   1061   1.1    tsubai 
   1062   1.1    tsubai 	dummy = sc_ffstr;
   1063   1.1    tsubai 	DMAC_WAIT0;
   1064   1.1    tsubai 	if (dummy & R5_FIFOREM) {
   1065   1.1    tsubai 		/*
   1066   1.1    tsubai 		 * flush FIFO
   1067   1.1    tsubai 		 */
   1068   1.4    tsubai 		SET_CMD(sc, SCMD_FLSH_FIFO);
   1069   1.1    tsubai 		tmp = 0;
   1070   1.1    tsubai 		do {
   1071   1.1    tsubai 			do {
   1072   1.1    tsubai 				dummy = sc_statr;
   1073   1.1    tsubai 				DMAC_WAIT0;
   1074   1.1    tsubai 			} while (dummy & R0_CIP);
   1075   1.1    tsubai 			GET_INTR(&tmp0, &tmp); /* clear interrupt */
   1076   1.1    tsubai 		} while ((tmp & R3_FNC) == 0);
   1077   1.1    tsubai 	}
   1078   1.1    tsubai }
   1079   1.1    tsubai 
   1080   1.1    tsubai /*
   1081   1.1    tsubai  *	SCSI command send routine
   1082   1.1    tsubai  */
   1083   1.1    tsubai void
   1084  1.15   tsutsui sc_cout(struct sc_softc *sc, struct sc_chan_stat *cs)
   1085   1.1    tsubai {
   1086  1.15   tsutsui 	int iloop;
   1087  1.15   tsutsui 	int cdb_bytes;
   1088  1.19   tsutsui 	uint8_t dummy;
   1089  1.19   tsutsui 	uint8_t statr;
   1090   1.4    tsubai 	struct scsipi_xfer *xs;
   1091   1.1    tsubai 
   1092   1.1    tsubai 	if (cs->comflg == CF_SET) {
   1093   1.4    tsubai 		struct sc_scb *scb = cs->scb;
   1094   1.4    tsubai 
   1095   1.1    tsubai 		cs->comflg = CF_SEND;
   1096   1.1    tsubai 
   1097   1.4    tsubai 		flush_fifo(sc);
   1098   1.1    tsubai 
   1099   1.4    tsubai 		xs = scb->xs;
   1100   1.4    tsubai 		cdb_bytes = xs->cmdlen;
   1101   1.4    tsubai 
   1102   1.4    tsubai 		switch (xs->cmd->opcode & CMD_TYPEMASK) {
   1103   1.1    tsubai 		case CMD_T0:
   1104   1.1    tsubai 		case CMD_T1:
   1105   1.1    tsubai 		case CMD_T5:
   1106   1.1    tsubai 			break;
   1107   1.1    tsubai 
   1108   1.1    tsubai 		default:
   1109   1.1    tsubai 			cdb_bytes = 6;
   1110   1.1    tsubai 			sc_intok2 |= Rb_FNC;
   1111   1.1    tsubai 			break;
   1112   1.1    tsubai 		}
   1113   1.1    tsubai 
   1114   1.1    tsubai 		/*
   1115   1.1    tsubai 		 * set Active pointers
   1116   1.1    tsubai 		 */
   1117   1.4    tsubai 		sc->act_cmd_pointer = (char *)xs->cmd;
   1118   1.4    tsubai 		cs->act_trcnt = scb->sc_ctrnscnt;
   1119   1.4    tsubai 		cs->act_point = scb->sc_cpoint;
   1120   1.4    tsubai 		cs->act_tag = scb->sc_ctag;
   1121   1.4    tsubai 		cs->act_offset = scb->sc_coffset;
   1122   1.1    tsubai 
   1123   1.1    tsubai 	} else {
   1124   1.1    tsubai 		cdb_bytes = 1;
   1125   1.1    tsubai 		iloop = 0;
   1126   1.1    tsubai 		do {
   1127   1.1    tsubai 			dummy = sc_cmonr;
   1128   1.1    tsubai 			DMAC_WAIT0;
   1129   1.1    tsubai 			if ((dummy & SC_PMASK) != COM_OUT)
   1130   1.1    tsubai 				return;
   1131   1.1    tsubai 			statr = sc_statr;
   1132   1.1    tsubai 			DMAC_WAIT0;
   1133   1.1    tsubai 			if (statr & R0_MIRQ)
   1134   1.1    tsubai 				return;
   1135   1.1    tsubai 		} while ((dummy & R4_MREQ) == 0);
   1136   1.1    tsubai 		statr = sc_statr;
   1137   1.1    tsubai 		DMAC_WAIT0;
   1138   1.1    tsubai 		if (statr & R0_MIRQ)
   1139   1.1    tsubai 			return;
   1140   1.1    tsubai 	}
   1141   1.1    tsubai 
   1142   1.1    tsubai 
   1143   1.1    tsubai 	SET_CNT(cdb_bytes);
   1144   1.4    tsubai 	SET_CMD(sc, SCMD_TR_INFO|R0_TRBE);
   1145   1.1    tsubai 
   1146   1.1    tsubai 	for (iloop = 0; iloop < cdb_bytes; iloop++) {
   1147   1.1    tsubai 		do {
   1148   1.1    tsubai 			dummy = sc_cmonr;
   1149   1.1    tsubai 			DMAC_WAIT0;
   1150   1.1    tsubai 			if ((dummy & SC_PMASK) != COM_OUT)
   1151   1.1    tsubai 				return;
   1152   1.1    tsubai 		} while ((dummy & R4_MREQ) == 0);
   1153   1.1    tsubai 		statr = sc_statr;
   1154   1.1    tsubai 		DMAC_WAIT0;
   1155   1.1    tsubai 		if (statr & R0_MIRQ)
   1156   1.1    tsubai 			return;
   1157   1.4    tsubai 		sc_datr = *sc->act_cmd_pointer++;
   1158   1.1    tsubai 		do {
   1159   1.1    tsubai 			dummy = sc_cmonr;
   1160   1.1    tsubai 			DMAC_WAIT0;
   1161   1.1    tsubai 		} while ((dummy & R4_MACK) != 0);
   1162   1.1    tsubai 	}
   1163   1.1    tsubai }
   1164   1.1    tsubai 
   1165   1.1    tsubai #define GET_MIN_COUNT	127
   1166   1.1    tsubai 
   1167   1.1    tsubai /*
   1168   1.1    tsubai  *	SCSI message accept routine
   1169   1.1    tsubai  */
   1170   1.1    tsubai void
   1171  1.15   tsutsui sc_min(struct sc_softc *sc, struct sc_chan_stat *cs)
   1172   1.1    tsubai {
   1173   1.4    tsubai 	struct sc_scb *scb = cs->scb;
   1174   1.4    tsubai 	struct scsipi_xfer *xs = scb->xs;
   1175  1.19   tsutsui 	uint8_t dummy;
   1176   1.1    tsubai 
   1177   1.1    tsubai 	sc_intok2 = Rb_FNC|Rb_DCNT|Rb_SRST|Rb_PHC|Rb_SPE|Rb_RMSG;
   1178   1.1    tsubai 	DMAC_WAIT0;
   1179   1.1    tsubai 
   1180   1.4    tsubai 	if (sc->min_flag == 1)
   1181   1.4    tsubai 		flush_fifo(sc);
   1182   1.1    tsubai 
   1183   1.1    tsubai 	dummy = sc_cmonr;
   1184   1.1    tsubai 	DMAC_WAIT0;
   1185   1.1    tsubai 	if ((dummy & R4_MREQ) == 0) {
   1186   1.1    tsubai 		printf("sc_min: !REQ cmonr=%x\n", dummy);
   1187   1.4    tsubai 		print_scsi_stat(sc);
   1188   1.1    tsubai 		scsi_hardreset();
   1189   1.1    tsubai 		return;
   1190   1.1    tsubai 	}
   1191   1.1    tsubai 
   1192   1.1    tsubai /*  retry_cmd_issue: */
   1193   1.4    tsubai 	sc->int_stat2 &= ~R3_FNC;
   1194   1.4    tsubai 	SET_CMD(sc, SCMD_TR_INFO);
   1195   1.1    tsubai 	do {
   1196   1.1    tsubai 		do {
   1197   1.1    tsubai 			dummy = sc_statr;
   1198   1.1    tsubai 			DMAC_WAIT0;
   1199   1.1    tsubai 		} while (dummy & R0_CIP);
   1200  1.19   tsutsui 		GET_INTR(&sc->int_stat1, &sc->int_stat2); /* clear interrupt */
   1201   1.4    tsubai 	} while ((sc->int_stat2 & R3_FNC) == 0);
   1202   1.4    tsubai 	sc->int_stat2 &= ~R3_FNC;
   1203   1.1    tsubai 
   1204   1.1    tsubai 	dummy = sc_ffstr;
   1205   1.1    tsubai 	if (dummy & R5_FIE) {
   1206   1.1    tsubai 		DMAC_WAIT;
   1207   1.1    tsubai 		dummy = sc_ffstr;
   1208   1.1    tsubai 		DMAC_WAIT0;
   1209   1.1    tsubai 		if (dummy & R5_FIE) {
   1210   1.1    tsubai 			dummy = sc_statr;
   1211   1.1    tsubai 			DMAC_WAIT0;
   1212   1.1    tsubai 			if ((dummy & R0_INIT) == 0) {
   1213   1.1    tsubai 				/*
   1214   1.1    tsubai 				 * CXD1185 detect BSY false
   1215   1.1    tsubai 				 */
   1216   1.1    tsubai 				scsi_hardreset();
   1217   1.1    tsubai 				return;
   1218   1.1    tsubai 			}
   1219   1.1    tsubai 		}
   1220   1.1    tsubai 	}
   1221   1.1    tsubai 	dummy = sc_datr;				/* get message byte */
   1222   1.1    tsubai 	DMAC_WAIT0;
   1223   1.1    tsubai 
   1224   1.4    tsubai 	if (sc->min_cnt[cs->chan_num] == 0) {
   1225   1.4    tsubai 		scb->message = scb->identify;
   1226   1.1    tsubai 		if (dummy == MSG_EXTND) {
   1227   1.1    tsubai 			/* Extended Message */
   1228   1.4    tsubai 			sc->min_cnt[cs->chan_num] = GET_MIN_COUNT;
   1229   1.4    tsubai 			sc->min_point[cs->chan_num] = scb->msgbuf;
   1230  1.19   tsutsui 			memset(scb->msgbuf, 0, 8);
   1231   1.4    tsubai 			*sc->min_point[cs->chan_num]++ = dummy;
   1232   1.1    tsubai 		} else {
   1233   1.1    tsubai 			switch ((dummy & MSG_IDENT)? MSG_IDENT : dummy) {
   1234   1.1    tsubai 
   1235   1.1    tsubai 			case MSG_CCOMP:
   1236   1.4    tsubai 				scb->istatus |= INST_EP;
   1237   1.1    tsubai 				break;
   1238   1.1    tsubai 
   1239   1.1    tsubai 			case MSG_MREJ:
   1240   1.1    tsubai #ifndef NOT_SUPPORT_SYNCTR
   1241   1.4    tsubai 				if (sc->mout_flag[cs->chan_num] == MOUT_SYNC_TR)
   1242   1.4    tsubai 					sc->sync_tr[cs->chan_num] = 0;
   1243   1.1    tsubai #endif
   1244   1.1    tsubai 				break;
   1245   1.1    tsubai 
   1246   1.1    tsubai 			case MSG_IDENT:
   1247   1.1    tsubai 			case MSG_RDP:
   1248   1.4    tsubai 
   1249   1.4    tsubai 				sc->dma_stat = OFF;
   1250   1.4    tsubai 				sc->pad_start = 0;
   1251   1.1    tsubai 				cs->comflg = OFF;
   1252   1.1    tsubai 				/*
   1253   1.4    tsubai 			 	 * restore the saved value to Active pointers
   1254   1.4    tsubai 			 	 */
   1255   1.4    tsubai 				sc->act_cmd_pointer = (char *)xs->cmd;
   1256   1.4    tsubai 				cs->act_trcnt = scb->sc_ctrnscnt;
   1257   1.4    tsubai 				cs->act_point = scb->sc_cpoint;
   1258   1.4    tsubai 				cs->act_tag = scb->sc_ctag;
   1259   1.4    tsubai 				cs->act_offset = scb->sc_coffset;
   1260   1.1    tsubai 				break;
   1261   1.1    tsubai 
   1262   1.1    tsubai 			case MSG_SDP:
   1263   1.1    tsubai 				/*
   1264   1.1    tsubai 				 * save Active pointers
   1265   1.1    tsubai 				 */
   1266   1.4    tsubai 				scb->sc_ctrnscnt = cs->act_trcnt;
   1267   1.4    tsubai 				scb->sc_ctag = cs->act_tag;
   1268   1.4    tsubai 				scb->sc_coffset = cs->act_offset;
   1269   1.4    tsubai 				scb->sc_cpoint = cs->act_point;
   1270   1.1    tsubai 				break;
   1271   1.1    tsubai 
   1272   1.1    tsubai 			case MSG_DCNT:
   1273   1.4    tsubai 				scb->istatus |= INST_WR;
   1274   1.4    tsubai 				sc->wrc++;
   1275   1.1    tsubai 				break;
   1276   1.1    tsubai 
   1277   1.1    tsubai 			default:
   1278   1.4    tsubai 				scb->message = MSG_MREJ;
   1279   1.4    tsubai 				SET_CMD(sc, SCMD_AST_ATN);
   1280   1.1    tsubai 				printf("SCSI%d:sc_min() Unknown mes=0x%x, \n",
   1281   1.1    tsubai 					cs->chan_num, dummy);
   1282   1.1    tsubai 			}
   1283   1.1    tsubai 		}
   1284   1.1    tsubai 	} else {
   1285   1.4    tsubai 		*sc->min_point[cs->chan_num]++ = dummy;
   1286   1.4    tsubai 		if (sc->min_cnt[cs->chan_num] == GET_MIN_COUNT)
   1287   1.4    tsubai 			sc->min_cnt[cs->chan_num] = dummy;
   1288   1.1    tsubai 		else
   1289   1.4    tsubai 			sc->min_cnt[cs->chan_num]--;
   1290   1.4    tsubai 		if (sc->min_cnt[cs->chan_num] <= 0) {
   1291   1.1    tsubai #ifdef ABORT_SYNCTR_MES_FROM_TARGET
   1292   1.4    tsubai 			if ((scb->msgbuf[2] == 0x01) &&
   1293   1.4    tsubai 			    (sc->mout_flag[cs->chan_num] == MOUT_SYNC_TR)) {
   1294   1.1    tsubai #else
   1295   1.4    tsubai 			if (scb->msgbuf[2] == 0x01) {
   1296   1.1    tsubai #endif
   1297  1.15   tsutsui 				int i;
   1298   1.1    tsubai 				/*
   1299   1.1    tsubai 				 * receive Synchronous transfer message reply
   1300   1.1    tsubai 				 *	calculate transfer period val
   1301   1.1    tsubai 				 *	tpm * 4/1000 us = 4/16 * (tpv + 1)
   1302   1.1    tsubai 				 */
   1303   1.1    tsubai #define	TPM2TPV(tpm)	(((tpm)*16 + 999) / 1000 - 1)
   1304   1.1    tsubai #ifndef NOT_SUPPORT_SYNCTR
   1305   1.4    tsubai 				i = scb->msgbuf[3];	/* get tpm */
   1306   1.1    tsubai 				i = TPM2TPV(i) << 4;
   1307   1.4    tsubai 				if (scb->msgbuf[4] == 0)
   1308   1.4    tsubai 					sc->sync_tr[cs->chan_num] = 0;
   1309   1.1    tsubai 				else
   1310   1.4    tsubai 					sc->sync_tr[cs->chan_num] =
   1311   1.4    tsubai 						i | scb->msgbuf[4];
   1312   1.1    tsubai #endif /* !NOT_SUPPORT_SYNCTR */
   1313   1.1    tsubai 			} else {
   1314   1.4    tsubai 				scb->message = MSG_MREJ;
   1315   1.4    tsubai 				SET_CMD(sc, SCMD_AST_ATN);	/* assert ATN */
   1316   1.1    tsubai 			}
   1317   1.1    tsubai 		}
   1318   1.1    tsubai 	}
   1319   1.4    tsubai 	SET_CMD(sc, SCMD_NGT_ACK);
   1320   1.1    tsubai }
   1321   1.1    tsubai 
   1322   1.1    tsubai /*
   1323   1.1    tsubai  *	SCSI message send routine
   1324   1.1    tsubai  */
   1325   1.1    tsubai void
   1326  1.15   tsutsui sc_mout(struct sc_softc *sc, struct sc_chan_stat *cs)
   1327   1.1    tsubai {
   1328  1.15   tsutsui 	struct sc_scb *scb = cs->scb;
   1329  1.15   tsutsui 	u_char *mp;
   1330  1.15   tsutsui 	int cnt;
   1331  1.15   tsutsui 	int iloop;
   1332  1.19   tsutsui 	uint8_t dummy;
   1333  1.19   tsutsui 	uint8_t tmp;
   1334  1.19   tsutsui 	uint8_t tmp0;
   1335   1.1    tsubai 
   1336   1.4    tsubai 	flush_fifo(sc);
   1337   1.1    tsubai 
   1338   1.4    tsubai 	if (sc->mout_flag[cs->chan_num] == 0) {
   1339   1.4    tsubai 		sc->mout_flag[cs->chan_num] = MOUT_IDENTIFY;
   1340   1.4    tsubai 		if (scb->message != 0) {
   1341   1.1    tsubai 			sc_intok2 = Rb_FNC|Rb_DCNT|Rb_SRST|Rb_PHC|Rb_SPE|Rb_RMSG;
   1342   1.1    tsubai 			DMAC_WAIT0;
   1343   1.4    tsubai 			if ((scb->message == MSG_EXTND)
   1344   1.4    tsubai 					&& (scb->msgbuf[2] == 0x01)) {
   1345   1.1    tsubai 				cnt = 5;
   1346   1.4    tsubai 				mp = scb->msgbuf;
   1347   1.4    tsubai 				scb->msgbuf[3] = MIN_TP;
   1348   1.4    tsubai 				if (scb->msgbuf[4] > MAX_OFFSET_BYTES)
   1349   1.4    tsubai 					scb->msgbuf[4] = MAX_OFFSET_BYTES;
   1350   1.4    tsubai 				sc->mout_flag[cs->chan_num] = MOUT_SYNC_TR;
   1351   1.1    tsubai 			} else {
   1352   1.1    tsubai 				cnt = 1;
   1353   1.4    tsubai 				mp = &scb->message;
   1354   1.1    tsubai 			}
   1355   1.1    tsubai 
   1356   1.1    tsubai 			SET_CNT(cnt);
   1357   1.4    tsubai 			SET_CMD(sc, SCMD_TR_INFO|R0_TRBE);
   1358   1.4    tsubai 			sc_datr = scb->identify;
   1359   1.1    tsubai 			DMAC_WAIT0;
   1360   1.1    tsubai 			for (iloop = 1; iloop < cnt; iloop++) {
   1361   1.1    tsubai 				sc_datr = *mp++;
   1362   1.1    tsubai 				DMAC_WAIT;
   1363   1.1    tsubai 			}
   1364   1.1    tsubai 			do {
   1365   1.1    tsubai 				dummy = sc_cmonr;
   1366   1.1    tsubai 				DMAC_WAIT0;
   1367   1.1    tsubai 				if ((dummy & R4_MBSY) == 0)
   1368   1.1    tsubai 					return;
   1369   1.1    tsubai 				dummy = sc_statr;
   1370   1.1    tsubai 				DMAC_WAIT0;
   1371   1.1    tsubai 			} while (dummy & R0_CIP);
   1372   1.1    tsubai 
   1373   1.1    tsubai 			tmp = 0;
   1374   1.1    tsubai 			GET_INTR(&tmp0, &tmp);		/* clear interrupt */
   1375   1.1    tsubai 			if ((tmp & R3_FNC) == 0) {
   1376   1.1    tsubai 				(void) WAIT_STATR_BITSET(R0_MIRQ);
   1377   1.1    tsubai 				GET_INTR(&tmp0, &tmp);	/* clear interrupt */
   1378   1.1    tsubai 			}
   1379   1.1    tsubai 
   1380   1.1    tsubai 			do {
   1381   1.1    tsubai 				dummy = sc_cmonr;
   1382   1.1    tsubai 				DMAC_WAIT0;
   1383   1.1    tsubai 				if ((dummy & R4_MBSY) == 0)
   1384   1.1    tsubai 					return;
   1385   1.1    tsubai 			} while ((dummy & R4_MREQ) == 0);
   1386   1.4    tsubai 			SET_CMD(sc, SCMD_NGT_ATN);
   1387  1.15   tsutsui 			(void)WAIT_STATR_BITCLR(R0_CIP);
   1388   1.1    tsubai 			GET_INTR(&tmp0, &tmp);		/* clear interrupt */
   1389   1.1    tsubai 
   1390   1.1    tsubai 			dummy = sc_cmonr;
   1391   1.1    tsubai 			DMAC_WAIT0;
   1392   1.1    tsubai 			if ((dummy & R4_MREQ) == 0) {
   1393   1.1    tsubai 				printf("sc_mout: !REQ cmonr=%x\n", dummy);
   1394   1.4    tsubai 				print_scsi_stat(sc);
   1395   1.1    tsubai 				scsi_hardreset();
   1396   1.1    tsubai 				return;
   1397   1.1    tsubai 			}
   1398   1.1    tsubai 
   1399   1.4    tsubai 			SET_CMD(sc, SCMD_TR_INFO);
   1400   1.1    tsubai 			sc_datr = *mp++;
   1401   1.1    tsubai 			DMAC_WAIT0;
   1402   1.1    tsubai 		} else {
   1403   1.1    tsubai 			dummy = sc_cmonr;
   1404   1.1    tsubai 			DMAC_WAIT0;
   1405   1.1    tsubai 			if (dummy & R4_MATN) {
   1406   1.4    tsubai 				SET_CMD(sc, SCMD_NGT_ATN);
   1407   1.1    tsubai 				(void) WAIT_STATR_BITCLR(R0_CIP);
   1408   1.1    tsubai 				GET_INTR(&tmp0, &tmp);	/* clear interrupt */
   1409   1.1    tsubai 			}
   1410   1.1    tsubai 
   1411   1.1    tsubai 			iloop = 0;
   1412   1.1    tsubai 			do {
   1413   1.1    tsubai 				dummy = sc_cmonr;
   1414   1.1    tsubai 				DMAC_WAIT0;
   1415   1.1    tsubai 				if (iloop++ > CHECK_LOOP_CNT)
   1416   1.1    tsubai 					break;
   1417   1.1    tsubai 			} while ((dummy & R4_MREQ) == 0);
   1418   1.4    tsubai 			SET_CMD(sc, SCMD_TR_INFO);
   1419   1.4    tsubai 			sc_datr = scb->identify;
   1420   1.1    tsubai 			DMAC_WAIT0;
   1421   1.1    tsubai 		}
   1422   1.1    tsubai 	} else {
   1423   1.1    tsubai 		dummy = sc_cmonr;
   1424   1.1    tsubai 		DMAC_WAIT0;
   1425   1.1    tsubai 		if (dummy & R4_MATN) {
   1426   1.4    tsubai 			SET_CMD(sc, SCMD_NGT_ATN);
   1427   1.1    tsubai 			(void) WAIT_STATR_BITCLR(R0_CIP);
   1428   1.1    tsubai 			GET_INTR(&tmp0, &tmp);		/* clear interrupt */
   1429   1.1    tsubai 		}
   1430   1.1    tsubai 
   1431   1.1    tsubai 		dummy = sc_cmonr;
   1432   1.1    tsubai 		DMAC_WAIT0;
   1433   1.1    tsubai 		if ((dummy & R4_MREQ) == 0) {
   1434   1.1    tsubai 			printf("sc_mout: !REQ cmonr=%x\n", dummy);
   1435   1.4    tsubai 			print_scsi_stat(sc);
   1436   1.1    tsubai 			scsi_hardreset();
   1437   1.1    tsubai 			return;
   1438   1.1    tsubai 		}
   1439   1.1    tsubai 
   1440   1.4    tsubai 		SET_CMD(sc, SCMD_TR_INFO);
   1441   1.4    tsubai 		sc_datr = scb->message;
   1442   1.1    tsubai 		DMAC_WAIT0;
   1443   1.1    tsubai 	}
   1444   1.1    tsubai }
   1445   1.1    tsubai 
   1446   1.1    tsubai /*
   1447   1.1    tsubai  *	SCSI status accept routine
   1448   1.1    tsubai  */
   1449   1.1    tsubai void
   1450  1.15   tsutsui sc_sin(struct sc_softc *sc, volatile struct sc_chan_stat *cs)
   1451   1.1    tsubai {
   1452  1.19   tsutsui 	uint8_t dummy;
   1453  1.15   tsutsui 	int iloop;
   1454   1.1    tsubai 
   1455   1.4    tsubai 	flush_fifo(sc);
   1456   1.1    tsubai 
   1457   1.1    tsubai 	dummy = sc_cmonr;
   1458   1.1    tsubai 	DMAC_WAIT0;
   1459   1.1    tsubai 	if ((dummy & R4_MREQ) == 0) {
   1460   1.1    tsubai 		printf("sc_sin: !REQ cmonr=%x\n", dummy);
   1461   1.4    tsubai 		print_scsi_stat(sc);
   1462   1.1    tsubai 		scsi_hardreset();
   1463   1.1    tsubai 		return;
   1464   1.1    tsubai 	}
   1465   1.1    tsubai 
   1466   1.1    tsubai 	sc_intok2 = Rb_FNC|Rb_DCNT|Rb_SRST|Rb_PHC|Rb_SPE|Rb_RMSG;
   1467   1.1    tsubai 	DMAC_WAIT0;
   1468   1.1    tsubai 
   1469   1.4    tsubai 	SET_CMD(sc, SCMD_TR_INFO);
   1470   1.1    tsubai 
   1471  1.15   tsutsui 	(void)WAIT_STATR_BITCLR(R0_CIP);
   1472   1.1    tsubai 
   1473   1.4    tsubai 	sc->int_stat2 &= ~R3_FNC;
   1474   1.1    tsubai 	iloop = 0;
   1475   1.1    tsubai 	do {
   1476   1.1    tsubai 		if (iloop++ > CHECK_LOOP_CNT)
   1477   1.1    tsubai 			break;
   1478   1.4    tsubai 		GET_INTR(&sc->int_stat1, &sc->int_stat2);	/* clear interrupt */
   1479   1.4    tsubai 	} while ((sc->int_stat2 & R3_FNC) == 0);
   1480   1.4    tsubai 	sc->int_stat2 &= ~R3_FNC;
   1481   1.1    tsubai 
   1482   1.4    tsubai 	cs->scb->tstatus = sc_datr;		/* get status byte */
   1483   1.1    tsubai 	DMAC_WAIT0;
   1484   1.1    tsubai }
   1485   1.1    tsubai 
   1486   1.1    tsubai /*
   1487   1.1    tsubai  *	SCSI data in/out routine
   1488   1.1    tsubai  */
   1489   1.1    tsubai void
   1490  1.15   tsutsui sc_dio(struct sc_softc *sc, volatile struct sc_chan_stat *cs)
   1491   1.1    tsubai {
   1492  1.19   tsutsui 	struct sc_scb *scb;
   1493  1.15   tsutsui 	int i;
   1494  1.15   tsutsui 	int pages;
   1495  1.19   tsutsui 	uint8_t tag;
   1496  1.19   tsutsui 	uint32_t pfn;
   1497  1.19   tsutsui 	uint8_t phase;
   1498   1.4    tsubai 	struct scsipi_xfer *xs;
   1499   1.1    tsubai 
   1500   1.4    tsubai 	scb = cs->scb;
   1501   1.4    tsubai 	xs = scb->xs;
   1502   1.1    tsubai 
   1503   1.1    tsubai 	sc_intok2 = Rb_FNC|Rb_DCNT|Rb_SRST|Rb_PHC|Rb_SPE;
   1504   1.1    tsubai 	DMAC_WAIT0;
   1505   1.1    tsubai 
   1506   1.1    tsubai 	if (cs->act_trcnt <= 0) {
   1507   1.4    tsubai 		sc_dio_pad(sc, cs);
   1508   1.1    tsubai 		return;
   1509   1.1    tsubai 	}
   1510   1.1    tsubai 
   1511   1.4    tsubai 	switch (xs->cmd->opcode) {
   1512   1.1    tsubai 
   1513   1.1    tsubai 	case SCOP_READ:
   1514   1.1    tsubai 	case SCOP_WRITE:
   1515   1.1    tsubai 	case SCOP_EREAD:
   1516   1.1    tsubai 	case SCOP_EWRITE:
   1517   1.4    tsubai 		i = (cs->act_trcnt + DEV_BSIZE -1) / DEV_BSIZE;
   1518   1.4    tsubai 		i *= DEV_BSIZE;
   1519   1.1    tsubai 		break;
   1520   1.1    tsubai 
   1521   1.1    tsubai 	default:
   1522   1.1    tsubai 		i = cs->act_trcnt;
   1523   1.1    tsubai 		break;
   1524   1.1    tsubai 	}
   1525   1.1    tsubai 
   1526   1.1    tsubai 	SET_CNT(i);
   1527   1.4    tsubai 	sc->pad_cnt[cs->chan_num] = i - cs->act_trcnt;
   1528   1.1    tsubai 
   1529   1.1    tsubai 	phase = sc_cmonr & SC_PMASK;
   1530   1.1    tsubai 	DMAC_WAIT0;
   1531   1.1    tsubai 	if (phase == DAT_IN) {
   1532   1.1    tsubai 		if (sc_syncr == OFF) {
   1533   1.1    tsubai 			DMAC_WAIT0;
   1534   1.4    tsubai 			flush_fifo(sc);
   1535   1.1    tsubai 		}
   1536   1.1    tsubai 	}
   1537   1.1    tsubai 
   1538   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
   1539   1.4    tsubai 	SET_CMD(sc, SCMD_TR_INFO|R0_DMA|R0_TRBE);
   1540   1.1    tsubai #endif
   1541   1.1    tsubai 
   1542   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
   1543   1.1    tsubai 	dmac_gsel = CH_SCSI;
   1544  1.19   tsutsui 	dmac_ctrcl = (uint8_t)(cs->act_trcnt & 0xff);
   1545  1.19   tsutsui 	dmac_ctrcm = (uint8_t)((cs->act_trcnt >> 8) & 0xff);
   1546  1.19   tsutsui 	dmac_ctrch = (uint8_t)((cs->act_trcnt >> 16) & 0x0f);
   1547  1.19   tsutsui 	dmac_cofsh = (uint8_t)((cs->act_offset >> 8) & 0xf);
   1548  1.19   tsutsui 	dmac_cofsl = (uint8_t)(cs->act_offset & 0xff);
   1549   1.1    tsubai #endif
   1550   1.1    tsubai 	tag = 0;
   1551   1.1    tsubai 
   1552   1.4    tsubai 	if (scb->sc_map && (scb->sc_map->mp_pages > 0)) {
   1553   1.1    tsubai 		/*
   1554   1.1    tsubai 		 * Set DMAC map entry from map table
   1555   1.1    tsubai 		 */
   1556   1.4    tsubai 		pages = scb->sc_map->mp_pages;
   1557   1.1    tsubai 		for (i = cs->act_tag; i < pages; i++) {
   1558   1.4    tsubai 			if ((pfn = scb->sc_map->mp_addr[i]) == 0)
   1559   1.1    tsubai 				panic("SCSI:sc_dma() zero entry");
   1560   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
   1561   1.1    tsubai 			dmac_gsel = CH_SCSI;
   1562  1.19   tsutsui 			dmac_ctag = (uint8_t)tag++;
   1563  1.19   tsutsui 			dmac_cmap = (uint16_t)pfn;
   1564   1.1    tsubai #endif
   1565   1.1    tsubai 		}
   1566   1.1    tsubai #ifdef MAP_OVER_ACCESS
   1567   1.9   thorpej # if defined(__mips__) && defined(CPU_SINGLE)
   1568   1.1    tsubai 		dmac_gsel = CH_SCSI;
   1569  1.19   tsutsui 		dmac_ctag = (uint8_t)tag++;
   1570  1.19   tsutsui 		dmac_cmap = (uint16_t)pfn;
   1571   1.1    tsubai # endif
   1572   1.1    tsubai #endif
   1573   1.1    tsubai 	} else {
   1574   1.1    tsubai 		/*
   1575   1.1    tsubai 		 * Set DMAC map entry from logical address
   1576   1.1    tsubai 		 */
   1577   1.5    tsubai 		pfn = kvtophys((vaddr_t)cs->act_point) >> PGSHIFT;
   1578   1.1    tsubai 		pages = (cs->act_trcnt >> PGSHIFT) + 2;
   1579   1.1    tsubai 		for (i = 0; i < pages; i++) {
   1580   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
   1581   1.1    tsubai 			dmac_gsel = CH_SCSI;
   1582  1.19   tsutsui 			dmac_ctag = (uint8_t)tag++;
   1583  1.19   tsutsui 			dmac_cmap = (uint8_t)pfn + i;
   1584   1.1    tsubai #endif
   1585   1.1    tsubai 		}
   1586   1.1    tsubai 	}
   1587   1.1    tsubai 
   1588   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
   1589   1.1    tsubai 	dmac_gsel = CH_SCSI;
   1590   1.1    tsubai 	dmac_ctag = 0;
   1591   1.1    tsubai #endif
   1592   1.1    tsubai 
   1593   1.1    tsubai 	if (phase == DAT_IN) {
   1594   1.4    tsubai 		sc->dma_stat = SC_DMAC_RD;
   1595   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
   1596   1.1    tsubai 		/*
   1597   1.1    tsubai 		 * auto pad flag is always on
   1598   1.1    tsubai 		 */
   1599   1.1    tsubai 		dmac_gsel = CH_SCSI;
   1600   1.1    tsubai 		dmac_cctl = DM_MODE|DM_APAD;
   1601   1.1    tsubai 		DMAC_WAIT;
   1602   1.1    tsubai 		dmac_cctl = DM_MODE|DM_APAD|DM_ENABLE;
   1603   1.1    tsubai 		DMAC_WAIT0;
   1604   1.1    tsubai #endif
   1605   1.1    tsubai 	}
   1606   1.1    tsubai 	else if (phase == DAT_OUT) {
   1607   1.4    tsubai 		sc->dma_stat = SC_DMAC_WR;
   1608   1.9   thorpej #if defined(__mips__) && defined(CPU_SINGLE)
   1609   1.1    tsubai 		dmac_gsel = CH_SCSI;
   1610   1.1    tsubai 		dmac_cctl = DM_APAD;
   1611   1.1    tsubai 		DMAC_WAIT;
   1612   1.1    tsubai 		dmac_cctl = DM_APAD|DM_ENABLE;
   1613   1.1    tsubai 		DMAC_WAIT0;
   1614   1.1    tsubai #endif
   1615   1.1    tsubai 						/* DMAC start on mem->I/O */
   1616   1.1    tsubai 	}
   1617   1.1    tsubai }
   1618   1.1    tsubai 
   1619   1.1    tsubai #define MAX_TR_CNT24	((1 << 24) -1)
   1620   1.1    tsubai void
   1621  1.15   tsutsui sc_dio_pad(struct sc_softc *sc, volatile struct sc_chan_stat *cs)
   1622   1.1    tsubai {
   1623  1.19   tsutsui 	uint8_t dummy;
   1624   1.1    tsubai 
   1625   1.1    tsubai 	if (cs->act_trcnt >= 0)
   1626   1.1    tsubai 		return;
   1627   1.4    tsubai 	sc->pad_start = 1;
   1628   1.1    tsubai 
   1629   1.1    tsubai 	SET_CNT(MAX_TR_CNT24);
   1630   1.4    tsubai 	SET_CMD(sc, SCMD_TR_PAD|R0_TRBE);
   1631   1.1    tsubai 	dummy = sc_cmonr & SC_PMASK;
   1632   1.1    tsubai 	DMAC_WAIT0;
   1633   1.1    tsubai 	if (dummy == DAT_IN)
   1634   1.1    tsubai 		dummy = sc_datr;		/* get data */
   1635   1.1    tsubai 	else
   1636   1.1    tsubai 		sc_datr = 0;			/* send data */
   1637   1.1    tsubai }
   1638   1.1    tsubai 
   1639   1.1    tsubai void
   1640  1.15   tsutsui print_scsi_stat(struct sc_softc *sc)
   1641   1.1    tsubai {
   1642  1.15   tsutsui 
   1643   1.4    tsubai 	printf("ipc=%d wrc=%d wbc=%d\n", sc->ipc, sc->wrc, sc->wbc);
   1644   1.1    tsubai }
   1645   1.1    tsubai 
   1646   1.1    tsubai /*
   1647   1.1    tsubai  *	return 0 if it was done.  Or retun TRUE if it is busy.
   1648   1.1    tsubai  */
   1649   1.1    tsubai int
   1650  1.15   tsutsui sc_busy(struct sc_softc *sc, int chan)
   1651   1.1    tsubai {
   1652  1.15   tsutsui 
   1653  1.15   tsutsui 	return (int)sc->chan_stat[chan].scb;
   1654   1.1    tsubai }
   1655   1.1    tsubai 
   1656   1.1    tsubai 
   1657   1.1    tsubai /*
   1658   1.1    tsubai  *	append channel into Waiting Bus_free queue
   1659   1.1    tsubai  */
   1660   1.1    tsubai void
   1661  1.15   tsutsui append_wb(struct sc_softc *sc, struct sc_chan_stat *cs)
   1662   1.1    tsubai {
   1663   1.4    tsubai 	int s;
   1664   1.1    tsubai 
   1665   1.1    tsubai 	s = splclock();			/* inhibit process switch */
   1666   1.4    tsubai 	if (sc->wbq_actf == NULL)
   1667   1.4    tsubai 		sc->wbq_actf = cs;
   1668   1.1    tsubai 	else
   1669   1.4    tsubai 		sc->wbq_actl->wb_next = cs;
   1670   1.4    tsubai 	sc->wbq_actl = cs;
   1671   1.4    tsubai 	cs->scb->istatus = INST_WAIT;
   1672   1.4    tsubai 	sc->wbc++;
   1673   1.1    tsubai 	splx(s);
   1674   1.1    tsubai }
   1675   1.1    tsubai 
   1676   1.1    tsubai /*
   1677   1.1    tsubai  *	get channel from Waiting Bus_free queue
   1678   1.1    tsubai  */
   1679   1.4    tsubai struct sc_chan_stat *
   1680  1.15   tsutsui get_wb_chan(struct sc_softc *sc)
   1681   1.1    tsubai {
   1682   1.4    tsubai 	struct sc_chan_stat *cs;
   1683   1.4    tsubai 	int s;
   1684   1.1    tsubai 
   1685   1.1    tsubai 	s = splclock();			/* inhibit process switch */
   1686   1.4    tsubai 	cs = sc->wbq_actf;
   1687   1.4    tsubai 	if (cs && cs->chan_num == SC_OWNID)	/* needed? */
   1688   1.4    tsubai 		cs = NULL;
   1689   1.1    tsubai 	splx(s);
   1690   1.4    tsubai 	return cs;
   1691   1.1    tsubai }
   1692   1.1    tsubai 
   1693   1.1    tsubai /*
   1694   1.1    tsubai  *	release channel from Waiting Bus_free queue
   1695   1.1    tsubai  */
   1696   1.1    tsubai int
   1697  1.15   tsutsui release_wb(struct sc_softc *sc)
   1698   1.1    tsubai {
   1699   1.4    tsubai 	struct sc_chan_stat *cs;
   1700   1.4    tsubai 	int error = 0;
   1701   1.4    tsubai 	int s;
   1702   1.1    tsubai 
   1703   1.1    tsubai 	s = splclock();			/* inhibit process switch */
   1704   1.4    tsubai 	if (sc->wbq_actf == NULL) {
   1705   1.1    tsubai 		error = -1;
   1706   1.1    tsubai 	} else {
   1707   1.4    tsubai 		cs = sc->wbq_actf;
   1708   1.4    tsubai 		sc->wbq_actf = cs->wb_next;
   1709   1.1    tsubai 		cs->wb_next = NULL;
   1710   1.4    tsubai 		if (sc->wbq_actl == cs)
   1711   1.4    tsubai 			sc->wbq_actl = NULL;
   1712   1.4    tsubai 		cs->scb->istatus &= ~INST_WAIT;
   1713   1.4    tsubai 		sc->wbc--;
   1714   1.1    tsubai 	}
   1715   1.1    tsubai 	splx(s);
   1716   1.4    tsubai 	return error;
   1717   1.1    tsubai }
   1718   1.1    tsubai 
   1719   1.1    tsubai void
   1720  1.15   tsutsui adjust_transfer(struct sc_softc *sc, struct sc_chan_stat *cs)
   1721   1.1    tsubai {
   1722   1.4    tsubai 	struct sc_scb *scb = cs->scb;
   1723  1.22    martin 	u_int remain_cnt = 0;
   1724   1.4    tsubai 	u_int offset, sent_byte;
   1725   1.1    tsubai 
   1726   1.4    tsubai 	if (sc->pad_start) {
   1727   1.4    tsubai 		sc->pad_start = 0;
   1728   1.1    tsubai 	} else {
   1729   1.9   thorpej # if defined(__mips__) && defined(CPU_SINGLE)
   1730   1.1    tsubai 		remain_cnt = GET_CNT();
   1731   1.4    tsubai 		remain_cnt -= sc->pad_cnt[cs->chan_num];
   1732   1.4    tsubai 		if (sc->dma_stat == SC_DMAC_WR) {
   1733   1.1    tsubai 			/*
   1734   1.1    tsubai 			 * adjust counter in the FIFO
   1735   1.1    tsubai 			 */
   1736   1.1    tsubai 			remain_cnt += sc_ffstr & R5_FIFOREM;
   1737   1.1    tsubai 		}
   1738   1.1    tsubai # endif
   1739   1.1    tsubai 	}
   1740   1.1    tsubai 
   1741   1.4    tsubai 	sent_byte = scb->sc_ctrnscnt - remain_cnt;
   1742   1.1    tsubai 	cs->act_trcnt = remain_cnt;
   1743   1.1    tsubai 
   1744   1.4    tsubai 	offset = scb->sc_coffset + sent_byte;
   1745   1.1    tsubai 	cs->act_tag += (offset >> PGSHIFT);
   1746   1.1    tsubai 	cs->act_offset = offset & PGOFSET;
   1747   1.4    tsubai 	if ((scb->sc_map == NULL) || (scb->sc_map->mp_pages <= 0))
   1748   1.1    tsubai 		cs->act_point += sent_byte;
   1749   1.1    tsubai }
   1750   1.3    tsubai 
   1751   1.9   thorpej #ifdef __mips__
   1752   1.3    tsubai static void
   1753  1.15   tsutsui clean_k2dcache(struct sc_scb *scb)
   1754   1.3    tsubai {
   1755   1.4    tsubai 	struct sc_map *sc_map = scb->sc_map;
   1756   1.5    tsubai 	paddr_t pa;
   1757   1.3    tsubai 	int i, pages;
   1758   1.3    tsubai 
   1759   1.5    tsubai 	pa = kvtophys((vaddr_t)scb->msgbuf);
   1760   1.8   thorpej 	mips_dcache_wbinv_range_index(MIPS_PHYS_TO_KSEG0(pa),
   1761   1.8   thorpej 	    sizeof(scb->msgbuf));
   1762   1.4    tsubai 
   1763   1.4    tsubai 	if (MACH_IS_USPACE(scb->sc_cpoint))
   1764   1.4    tsubai 		panic("clean_k2dcache: user address is not supported");
   1765   1.4    tsubai 
   1766   1.4    tsubai 	if (MACH_IS_CACHED(scb->sc_cpoint)) {
   1767   1.8   thorpej 		mips_dcache_wbinv_range_index((vaddr_t)scb->sc_cpoint,
   1768   1.8   thorpej 		    scb->sc_ctrnscnt);
   1769   1.3    tsubai 		return;
   1770   1.4    tsubai 	}
   1771   1.3    tsubai 
   1772   1.4    tsubai 	if (sc_map) {
   1773   1.4    tsubai 		pages = sc_map->mp_pages;
   1774   1.4    tsubai 		for (i = 0; i < pages; i++) {
   1775   1.4    tsubai 			pa = sc_map->mp_addr[i] << PGSHIFT;
   1776   1.8   thorpej 			mips_dcache_wbinv_range_index(MIPS_PHYS_TO_KSEG0(pa),
   1777  1.10   thorpej 			    PAGE_SIZE);
   1778   1.4    tsubai 		}
   1779   1.3    tsubai 	}
   1780   1.3    tsubai }
   1781   1.3    tsubai #endif
   1782