Home | History | Annotate | Line # | Download | only in mba
mba.c revision 1.20
      1 /*	$NetBSD: mba.c,v 1.20 2000/06/04 06:16:55 matt 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 #include "opt_vax750.h"
     62 
     63 struct	mbaunit mbaunit[] = {
     64 	{MBADT_RP04,	"rp04", MB_RP},
     65 	{MBADT_RP05,	"rp05", MB_RP},
     66 	{MBADT_RP06,	"rp06", MB_RP},
     67 	{MBADT_RP07,	"rp07", MB_RP},
     68 	{MBADT_RM02,	"rm02", MB_RP},
     69 	{MBADT_RM03,	"rm03", MB_RP},
     70 	{MBADT_RM05,	"rm05", MB_RP},
     71 	{MBADT_RM80,	"rm80", MB_RP},
     72 	{0,		0,	0}
     73 };
     74 
     75 int	mbamatch __P((struct device *, struct cfdata *, void *));
     76 void	mbaattach __P((struct device *, struct device *, void *));
     77 void	mbaintr __P((void *));
     78 int	mbaprint __P((void *, const char *));
     79 void	mbaqueue __P((struct mba_device *));
     80 void	mbastart __P((struct mba_softc *));
     81 void	mbamapregs __P((struct mba_softc *));
     82 
     83 struct	cfattach mba_cmi_ca = {
     84 	sizeof(struct mba_softc), mbamatch, mbaattach
     85 };
     86 
     87 struct	cfattach mba_sbi_ca = {
     88 	sizeof(struct mba_softc), mbamatch, mbaattach
     89 };
     90 
     91 extern	struct cfdriver mba_cd;
     92 
     93 /*
     94  * Look if this is a massbuss adapter.
     95  */
     96 int
     97 mbamatch(parent, cf, aux)
     98 	struct	device *parent;
     99 	struct  cfdata *cf;
    100 	void	*aux;
    101 {
    102 	struct	sbi_attach_args *sa = (struct sbi_attach_args *)aux;
    103 
    104 	if ((cf->cf_loc[0] != sa->nexnum) && (cf->cf_loc[0] > -1 ))
    105 		return 0;
    106 
    107 	if (sa->type == NEX_MBA)
    108 		return 1;
    109 
    110 	return 0;
    111 }
    112 
    113 /*
    114  * Attach the found massbuss adapter. Setup its interrupt vectors,
    115  * reset it and go searching for drives on it.
    116  */
    117 void
    118 mbaattach(parent, self, aux)
    119 	struct	device *parent, *self;
    120 	void	*aux;
    121 {
    122 	struct	mba_softc *sc = (void *)self;
    123 	struct	sbi_attach_args *sa = (struct sbi_attach_args *)aux;
    124 	volatile struct	mba_regs *mbar = (struct mba_regs *)sa->nexaddr;
    125 	struct	mba_attach_args ma;
    126 	int	i, j;
    127 
    128 	printf("\n");
    129 	/*
    130 	 * Set up interrupt vectors for this MBA.
    131 	 */
    132 	sc->sc_dsp = idsptch;
    133 	sc->sc_dsp.pushlarg = sc;
    134 	sc->sc_dsp.hoppaddr = mbaintr;
    135 	sc->sc_dsp.ev = &sc->sc_intrcnt;
    136 	scb->scb_nexvec[0][sa->nexnum] = scb->scb_nexvec[1][sa->nexnum] =
    137 	    scb->scb_nexvec[2][sa->nexnum] = scb->scb_nexvec[3][sa->nexnum] =
    138 	    &sc->sc_dsp;
    139 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
    140 
    141 	sc->sc_physnr = sa->nexnum - 8; /* MBA's have TR between 8 - 11... */
    142 #if VAX750
    143 	if (vax_cputype == VAX_750)
    144 		sc->sc_physnr += 4;	/* ...but not on 11/750 */
    145 #endif
    146 	sc->sc_first = 0;
    147 	sc->sc_last = (void *)&sc->sc_first;
    148 	sc->sc_mbareg = (struct mba_regs *)mbar;
    149 	mbar->mba_cr = MBACR_INIT;	/* Reset adapter */
    150 	mbar->mba_cr = MBACR_IE;	/* Enable interrupts */
    151 
    152 	for (i = 0; i < MAXMBADEV; i++) {
    153 		sc->sc_state = SC_AUTOCONF;
    154 		if ((mbar->mba_md[i].md_ds & MBADS_DPR) == 0)
    155 			continue;
    156 		/* We have a drive, ok. */
    157 		ma.unit = i;
    158 		ma.type = mbar->mba_md[i].md_dt & 0777;
    159 		j = 0;
    160 		while (mbaunit[j++].nr)
    161 			if (mbaunit[j].nr == ma.type)
    162 				break;
    163 		ma.devtyp = mbaunit[j].devtyp;
    164 		ma.name = mbaunit[j].name;
    165 		config_found(&sc->sc_dev, (void *)&ma, mbaprint);
    166 	}
    167 }
    168 
    169 /*
    170  * We got an interrupt. Check type of interrupt and call the specific
    171  * device interrupt handling routine.
    172  */
    173 void
    174 mbaintr(mba)
    175 	void	*mba;
    176 {
    177 	struct	mba_softc *sc = mba;
    178 	volatile struct	mba_regs *mr = sc->sc_mbareg;
    179 	struct	mba_device *md;
    180 	struct	buf *bp;
    181 	int	itype, attn, anr;
    182 
    183 	itype = mr->mba_sr;
    184 	mr->mba_sr = itype;	/* Write back to clear bits */
    185 
    186 	attn = mr->mba_md[0].md_as & 0xff;
    187 	mr->mba_md[0].md_as = attn;
    188 
    189 	if (sc->sc_state == SC_AUTOCONF)
    190 		return;	/* During autoconfig */
    191 
    192 	md = sc->sc_first;
    193 	bp = BUFQ_FIRST(&md->md_q);
    194 	/*
    195 	 * A data-transfer interrupt. Current operation is finished,
    196 	 * call that device's finish routine to see what to do next.
    197 	 */
    198 	if (sc->sc_state == SC_ACTIVE) {
    199 
    200 		sc->sc_state = SC_IDLE;
    201 		switch ((*md->md_finish)(md, itype, &attn)) {
    202 
    203 		case XFER_FINISH:
    204 			/*
    205 			 * Transfer is finished. Take buffer of drive
    206 			 * queue, and take drive of adapter queue.
    207 			 * If more to transfer, start the adapter again
    208 			 * by calling mbastart().
    209 			 */
    210 			BUFQ_REMOVE(&md->md_q, bp);
    211 			sc->sc_first = md->md_back;
    212 			md->md_back = 0;
    213 			if (sc->sc_first == 0)
    214 				sc->sc_last = (void *)&sc->sc_first;
    215 
    216 			if (BUFQ_FIRST(&md->md_q) != NULL) {
    217 				sc->sc_last->md_back = md;
    218 				sc->sc_last = md;
    219 			}
    220 
    221 			bp->b_resid = 0;
    222 			biodone(bp);
    223 			if (sc->sc_first)
    224 				mbastart(sc);
    225 			break;
    226 
    227 		case XFER_RESTART:
    228 			/*
    229 			 * Something went wrong with the transfer. Try again.
    230 			 */
    231 			mbastart(sc);
    232 			break;
    233 		}
    234 	}
    235 
    236 	while (attn) {
    237 		anr = ffs(attn) - 1;
    238 		attn &= ~(1 << anr);
    239 		if (sc->sc_md[anr]->md_attn == 0)
    240 			panic("Should check for new MBA device %d", anr);
    241 		(*sc->sc_md[anr]->md_attn)(sc->sc_md[anr]);
    242 	}
    243 }
    244 
    245 int
    246 mbaprint(aux, mbaname)
    247 	void	*aux;
    248 	const char	*mbaname;
    249 {
    250 	struct  mba_attach_args *ma = aux;
    251 
    252 	if (mbaname) {
    253 		if (ma->name)
    254 			printf("%s", ma->name);
    255 		else
    256 			printf("device type %o", ma->type);
    257 		printf(" at %s", mbaname);
    258 	}
    259 	printf(" drive %d", ma->unit);
    260 	return (ma->name ? UNCONF : UNSUPP);
    261 }
    262 
    263 /*
    264  * A device calls mbaqueue() when it wants to get on the adapter queue.
    265  * Called at splbio(). If the adapter is inactive, start it.
    266  */
    267 void
    268 mbaqueue(md)
    269 	struct	mba_device *md;
    270 {
    271 	struct	mba_softc *sc = md->md_mba;
    272 	int	i = (int)sc->sc_first;
    273 
    274 	sc->sc_last->md_back = md;
    275 	sc->sc_last = md;
    276 
    277 	if (i == 0)
    278 		mbastart(sc);
    279 }
    280 
    281 /*
    282  * Start activity on (idling) adapter. Calls mbamapregs() to setup
    283  * for dma transfer, then the unit-specific start routine.
    284  */
    285 void
    286 mbastart(sc)
    287 	struct	mba_softc *sc;
    288 {
    289 	struct	mba_device *md = sc->sc_first;
    290 	volatile struct	mba_regs *mr = sc->sc_mbareg;
    291 	struct	buf *bp = BUFQ_FIRST(&md->md_q);
    292 
    293 	disk_reallymapin(BUFQ_FIRST(&md->md_q), sc->sc_mbareg->mba_map,
    294 	    0, PG_V);
    295 
    296 	sc->sc_state = SC_ACTIVE;
    297 	mr->mba_var = ((u_int)bp->b_data & VAX_PGOFSET);
    298 	mr->mba_bc = (~bp->b_bcount) + 1;
    299 	(*md->md_start)(md);		/* machine-dependent start */
    300 }
    301