Home | History | Annotate | Line # | Download | only in mba
mba.c revision 1.14
      1 /*	$NetBSD: mba.c,v 1.14 1998/05/21 13:16:14 ragge Exp $ */
      2 /*
      3  * Copyright (c) 1994, 1996 Ludd, University of Lule}, Sweden.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *      This product includes software developed at Ludd, University of
     17  *      Lule}, Sweden and its contributors.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Simple massbus drive routine.
     35  * TODO:
     36  *  Autoconfig new devices 'on the fly'.
     37  *  More intelligent way to handle different interrupts.
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/device.h>
     43 #include <sys/queue.h>
     44 #include <sys/buf.h>
     45 #include <sys/proc.h>
     46 
     47 #include <vm/vm.h>
     48 #include <vm/vm_kern.h>
     49 
     50 #include <machine/trap.h>
     51 #include <machine/scb.h>
     52 #include <machine/nexus.h>
     53 #include <machine/pte.h>
     54 #include <machine/pcb.h>
     55 #include <machine/sid.h>
     56 #include <machine/cpu.h>
     57 
     58 #include <vax/mba/mbareg.h>
     59 #include <vax/mba/mbavar.h>
     60 
     61 struct	mbaunit mbaunit[] = {
     62 	{MBADT_RP04,	"rp04", MB_RP},
     63 	{MBADT_RP05,	"rp05", MB_RP},
     64 	{MBADT_RP06,	"rp06", MB_RP},
     65 	{MBADT_RP07,	"rp07", MB_RP},
     66 	{MBADT_RM02,	"rm02", MB_RP},
     67 	{MBADT_RM03,	"rm03", MB_RP},
     68 	{MBADT_RM05,	"rm05", MB_RP},
     69 	{MBADT_RM80,	"rm80", MB_RP},
     70 	{0,		0,	0}
     71 };
     72 
     73 int	mbamatch __P((struct device *, struct cfdata *, void *));
     74 void	mbaattach __P((struct device *, struct device *, void *));
     75 void	mbaintr __P((int));
     76 int	mbaprint __P((void *, const char *));
     77 void	mbaqueue __P((struct mba_device *));
     78 void	mbastart __P((struct mba_softc *));
     79 void	mbamapregs __P((struct mba_softc *));
     80 
     81 struct	cfattach mba_cmi_ca = {
     82 	sizeof(struct mba_softc), mbamatch, mbaattach
     83 };
     84 
     85 struct	cfattach mba_sbi_ca = {
     86 	sizeof(struct mba_softc), mbamatch, mbaattach
     87 };
     88 
     89 extern	struct cfdriver mba_cd;
     90 
     91 /*
     92  * Look if this is a massbuss adapter.
     93  */
     94 int
     95 mbamatch(parent, cf, aux)
     96 	struct	device *parent;
     97 	struct  cfdata *cf;
     98 	void	*aux;
     99 {
    100 	struct	sbi_attach_args *sa = (struct sbi_attach_args *)aux;
    101 
    102 	if ((cf->cf_loc[0] != sa->nexnum) && (cf->cf_loc[0] > -1 ))
    103 		return 0;
    104 
    105 	if (sa->type == NEX_MBA)
    106 		return 1;
    107 
    108 	return 0;
    109 }
    110 
    111 /*
    112  * Attach the found massbuss adapter. Setup its interrupt vectors,
    113  * reset it and go searching for drives on it.
    114  */
    115 void
    116 mbaattach(parent, self, aux)
    117 	struct	device *parent, *self;
    118 	void	*aux;
    119 {
    120 	struct	mba_softc *sc = (void *)self;
    121 	struct	sbi_attach_args *sa = (struct sbi_attach_args *)aux;
    122 	volatile struct	mba_regs *mbar = (struct mba_regs *)sa->nexaddr;
    123 	struct	mba_attach_args ma;
    124 	extern  struct  ivec_dsp idsptch;
    125 	int	i, j;
    126 
    127 	printf("\n");
    128 	/*
    129 	 * Set up interrupt vectors for this MBA.
    130 	 */
    131 	bcopy(&idsptch, &sc->sc_dsp, sizeof(struct ivec_dsp));
    132 	scb->scb_nexvec[0][sa->nexnum] = scb->scb_nexvec[1][sa->nexnum] =
    133 	    scb->scb_nexvec[2][sa->nexnum] = scb->scb_nexvec[3][sa->nexnum] =
    134 	    &sc->sc_dsp;
    135 	sc->sc_dsp.pushlarg = sc->sc_dev.dv_unit;
    136 	sc->sc_dsp.hoppaddr = mbaintr;
    137 
    138 	sc->sc_physnr = sa->nexnum - 8; /* MBA's have TR between 8 - 11... */
    139 #ifdef VAX750
    140 	if (vax_cputype == VAX_750)
    141 		sc->sc_physnr += 4;	/* ...but not on 11/750 */
    142 #endif
    143 	sc->sc_first = 0;
    144 	sc->sc_last = (void *)&sc->sc_first;
    145 	sc->sc_mbareg = (struct mba_regs *)mbar;
    146 	mbar->mba_cr = MBACR_INIT;	/* Reset adapter */
    147 	mbar->mba_cr = MBACR_IE;	/* Enable interrupts */
    148 
    149 	for (i = 0; i < MAXMBADEV; i++) {
    150 		sc->sc_state = SC_AUTOCONF;
    151 		if ((mbar->mba_md[i].md_ds & MBADS_DPR) == 0)
    152 			continue;
    153 		/* We have a drive, ok. */
    154 		ma.unit = i;
    155 		ma.type = mbar->mba_md[i].md_dt & 0777;
    156 		j = 0;
    157 		while (mbaunit[j++].nr)
    158 			if (mbaunit[j].nr == ma.type)
    159 				break;
    160 		ma.devtyp = mbaunit[j].devtyp;
    161 		ma.name = mbaunit[j].name;
    162 		config_found(&sc->sc_dev, (void *)&ma, mbaprint);
    163 	}
    164 }
    165 
    166 /*
    167  * We got an interrupt. Check type of interrupt and call the specific
    168  * device interrupt handling routine.
    169  */
    170 void
    171 mbaintr(mba)
    172 	int	mba;
    173 {
    174 	struct	mba_softc *sc = mba_cd.cd_devs[mba];
    175 	volatile struct	mba_regs *mr = sc->sc_mbareg;
    176 	struct	mba_device *md;
    177 	struct	buf *bp;
    178 	int	itype, attn, anr;
    179 
    180 	itype = mr->mba_sr;
    181 	mr->mba_sr = itype;	/* Write back to clear bits */
    182 
    183 	attn = mr->mba_md[0].md_as & 0xff;
    184 	mr->mba_md[0].md_as = attn;
    185 
    186 	if (sc->sc_state == SC_AUTOCONF)
    187 		return;	/* During autoconfig */
    188 
    189 	md = sc->sc_first;
    190 	bp = md->md_q.b_actf;
    191 	/*
    192 	 * A data-transfer interrupt. Current operation is finished,
    193 	 * call that device's finish routine to see what to do next.
    194 	 */
    195 	if (sc->sc_state == SC_ACTIVE) {
    196 
    197 		sc->sc_state = SC_IDLE;
    198 		switch ((*md->md_finish)(md, itype, &attn)) {
    199 
    200 		case XFER_FINISH:
    201 			/*
    202 			 * Transfer is finished. Take buffer of drive
    203 			 * queue, and take drive of adapter queue.
    204 			 * If more to transfer, start the adapter again
    205 			 * by calling mbastart().
    206 			 */
    207 			md->md_q.b_actf = bp->b_actf;
    208 			sc->sc_first = md->md_back;
    209 			md->md_back = 0;
    210 			if (sc->sc_first == 0)
    211 				sc->sc_last = (void *)&sc->sc_first;
    212 
    213 			if (md->md_q.b_actf) {
    214 				sc->sc_last->md_back = md;
    215 				sc->sc_last = md;
    216 			}
    217 
    218 			bp->b_resid = 0;
    219 			biodone(bp);
    220 			if (sc->sc_first)
    221 				mbastart(sc);
    222 			break;
    223 
    224 		case XFER_RESTART:
    225 			/*
    226 			 * Something went wrong with the transfer. Try again.
    227 			 */
    228 			mbastart(sc);
    229 			break;
    230 		}
    231 	}
    232 
    233 	while (attn) {
    234 		anr = ffs(attn) - 1;
    235 		attn &= ~(1 << anr);
    236 		if (sc->sc_md[anr]->md_attn == 0)
    237 			panic("Should check for new MBA device %d", anr);
    238 		(*sc->sc_md[anr]->md_attn)(sc->sc_md[anr]);
    239 	}
    240 }
    241 
    242 int
    243 mbaprint(aux, mbaname)
    244 	void	*aux;
    245 	const char	*mbaname;
    246 {
    247 	struct  mba_attach_args *ma = aux;
    248 
    249 	if (mbaname) {
    250 		if (ma->name)
    251 			printf("%s", ma->name);
    252 		else
    253 			printf("device type %o", ma->type);
    254 		printf(" at %s", mbaname);
    255 	}
    256 	printf(" drive %d", ma->unit);
    257 	return (ma->name ? UNCONF : UNSUPP);
    258 }
    259 
    260 /*
    261  * A device calls mbaqueue() when it wants to get on the adapter queue.
    262  * Called at splbio(). If the adapter is inactive, start it.
    263  */
    264 void
    265 mbaqueue(md)
    266 	struct	mba_device *md;
    267 {
    268 	struct	mba_softc *sc = md->md_mba;
    269 	int	i = (int)sc->sc_first;
    270 
    271 	sc->sc_last->md_back = md;
    272 	sc->sc_last = md;
    273 
    274 	if (i == 0)
    275 		mbastart(sc);
    276 }
    277 
    278 /*
    279  * Start activity on (idling) adapter. Calls mbamapregs() to setup
    280  * for dma transfer, then the unit-specific start routine.
    281  */
    282 void
    283 mbastart(sc)
    284 	struct	mba_softc *sc;
    285 {
    286 	struct	mba_device *md = sc->sc_first;
    287 	volatile struct	mba_regs *mr = sc->sc_mbareg;
    288 	struct	buf *bp = md->md_q.b_actf;
    289 
    290 	disk_reallymapin(md->md_q.b_actf, sc->sc_mbareg->mba_map, 0, PG_V);
    291 
    292 	sc->sc_state = SC_ACTIVE;
    293 	mr->mba_var = ((u_int)bp->b_un.b_addr & PGOFSET);
    294 	mr->mba_bc = (~bp->b_bcount) + 1;
    295 	(*md->md_start)(md);		/* machine-dependent start */
    296 }
    297