Home | History | Annotate | Line # | Download | only in mba
mba.c revision 1.32
      1 /*	$NetBSD: mba.c,v 1.32 2003/07/15 02:15:01 lukem 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/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: mba.c,v 1.32 2003/07/15 02:15:01 lukem Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/device.h>
     46 #include <sys/queue.h>
     47 #include <sys/buf.h>
     48 #include <sys/proc.h>
     49 
     50 #include <uvm/uvm_extern.h>
     51 
     52 #include <machine/bus.h>
     53 #include <machine/scb.h>
     54 #include <machine/nexus.h>
     55 #include <machine/pte.h>
     56 #include <machine/pcb.h>
     57 #include <machine/sid.h>
     58 #include <machine/cpu.h>
     59 
     60 #include <vax/mba/mbareg.h>
     61 #include <vax/mba/mbavar.h>
     62 
     63 #include "locators.h"
     64 
     65 struct	mbaunit mbaunit[] = {
     66 	{MBADT_RP04,	"rp04", MB_RP},
     67 	{MBADT_RP05,	"rp05", MB_RP},
     68 	{MBADT_RP06,	"rp06", MB_RP},
     69 	{MBADT_RP07,	"rp07", MB_RP},
     70 	{MBADT_RM02,	"rm02", MB_RP},
     71 	{MBADT_RM03,	"rm03", MB_RP},
     72 	{MBADT_RM05,	"rm05", MB_RP},
     73 	{MBADT_RM80,	"rm80", MB_RP},
     74 	{0,		0,	0}
     75 };
     76 
     77 int	mbamatch(struct device *, struct cfdata *, void *);
     78 void	mbaattach(struct device *, struct device *, void *);
     79 void	mbaintr(void *);
     80 int	mbaprint(void *, const char *);
     81 void	mbaqueue(struct mba_device *);
     82 void	mbastart(struct mba_softc *);
     83 void	mbamapregs(struct mba_softc *);
     84 
     85 CFATTACH_DECL(mba_cmi, sizeof(struct mba_softc),
     86     mbamatch, mbaattach, NULL, NULL);
     87 
     88 CFATTACH_DECL(mba_sbi, sizeof(struct mba_softc),
     89     mbamatch, mbaattach, NULL, NULL);
     90 
     91 #define	MBA_WCSR(reg, val) \
     92 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, (reg), (val))
     93 #define MBA_RCSR(reg) \
     94 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, (reg))
     95 
     96 /*
     97  * Look if this is a massbuss adapter.
     98  */
     99 int
    100 mbamatch(struct device *parent, struct cfdata *cf, void *aux)
    101 {
    102 	struct	sbi_attach_args *sa = (struct sbi_attach_args *)aux;
    103 
    104 	if (vax_cputype == VAX_750) {
    105 		if (cf->cf_loc[CMICF_TR] != CMICF_TR_DEFAULT &&
    106 		    cf->cf_loc[CMICF_TR] != sa->sa_nexnum)
    107 			return 0;
    108 	} else {
    109 		if (cf->cf_loc[SBICF_TR] != SBICF_TR_DEFAULT &&
    110 		    cf->cf_loc[SBICF_TR] != sa->sa_nexnum)
    111 			return 0;
    112 	}
    113 
    114 	if (sa->sa_type == NEX_MBA)
    115 		return 1;
    116 
    117 	return 0;
    118 }
    119 
    120 /*
    121  * Attach the found massbuss adapter. Setup its interrupt vectors,
    122  * reset it and go searching for drives on it.
    123  */
    124 void
    125 mbaattach(struct device *parent, struct device *self, void *aux)
    126 {
    127 	struct	mba_softc *sc = (void *)self;
    128 	struct	sbi_attach_args *sa = (struct sbi_attach_args *)aux;
    129 	struct	mba_attach_args ma;
    130 	int	i, j;
    131 
    132 	printf("\n");
    133 	sc->sc_iot = sa->sa_iot;
    134 	sc->sc_ioh = sa->sa_ioh;
    135 	/*
    136 	 * Set up interrupt vectors for this MBA.
    137 	 */
    138 	for (i = 0x14; i < 0x18; i++)
    139 		scb_vecalloc(vecnum(0, i, sa->sa_nexnum),
    140 		    mbaintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
    141 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    142 		self->dv_xname, "intr");
    143 
    144 	sc->sc_first = 0;
    145 	sc->sc_last = (void *)&sc->sc_first;
    146 	MBA_WCSR(MBA_CR, MBACR_INIT);	/* Reset adapter */
    147 	MBA_WCSR(MBA_CR, MBACR_IE);	/* Enable interrupts */
    148 
    149 	for (i = 0; i < MAXMBADEV; i++) {
    150 		sc->sc_state = SC_AUTOCONF;
    151 		if ((MBA_RCSR(MUREG(i, MU_DS)) & MBADS_DPR) == 0)
    152 			continue;
    153 		/* We have a drive, ok. */
    154 		ma.ma_unit = i;
    155 		ma.ma_type = MBA_RCSR(MUREG(i, MU_DT)) & 0777;
    156 		j = 0;
    157 		while (mbaunit[j++].nr)
    158 			if (mbaunit[j].nr == ma.ma_type)
    159 				break;
    160 		ma.ma_devtyp = mbaunit[j].devtyp;
    161 		ma.ma_name = mbaunit[j].name;
    162 		ma.ma_iot = sc->sc_iot;
    163 		ma.ma_ioh = sc->sc_ioh + MUREG(i, 0);
    164 		config_found(&sc->sc_dev, (void *)&ma, mbaprint);
    165 	}
    166 }
    167 
    168 /*
    169  * We got an interrupt. Check type of interrupt and call the specific
    170  * device interrupt handling routine.
    171  */
    172 void
    173 mbaintr(void *mba)
    174 {
    175 	struct	mba_softc *sc = mba;
    176 	struct	mba_device *md;
    177 	struct	buf *bp;
    178 	int	itype, attn, anr;
    179 
    180 	itype = MBA_RCSR(MBA_SR);
    181 	MBA_WCSR(MBA_SR, itype);
    182 
    183 	attn = MBA_RCSR(MUREG(0, MU_AS)) & 0xff;
    184 	MBA_WCSR(MUREG(0, MU_AS), attn);
    185 
    186 	if (sc->sc_state == SC_AUTOCONF)
    187 		return;	/* During autoconfig */
    188 
    189 	md = sc->sc_first;
    190 	bp = BUFQ_PEEK(&md->md_q);
    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 			(void)BUFQ_GET(&md->md_q);
    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 (BUFQ_PEEK(&md->md_q) != NULL) {
    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(void *aux, const char *mbaname)
    244 {
    245 	struct  mba_attach_args *ma = aux;
    246 
    247 	if (mbaname) {
    248 		if (ma->ma_name)
    249 			aprint_normal("%s", ma->ma_name);
    250 		else
    251 			aprint_normal("device type %o", ma->ma_type);
    252 		aprint_normal(" at %s", mbaname);
    253 	}
    254 	aprint_normal(" drive %d", ma->ma_unit);
    255 	return (ma->ma_name ? UNCONF : UNSUPP);
    256 }
    257 
    258 /*
    259  * A device calls mbaqueue() when it wants to get on the adapter queue.
    260  * Called at splbio(). If the adapter is inactive, start it.
    261  */
    262 void
    263 mbaqueue(struct mba_device *md)
    264 {
    265 	struct	mba_softc *sc = md->md_mba;
    266 	int	i = (int)sc->sc_first;
    267 
    268 	sc->sc_last->md_back = md;
    269 	sc->sc_last = md;
    270 
    271 	if (i == 0)
    272 		mbastart(sc);
    273 }
    274 
    275 /*
    276  * Start activity on (idling) adapter. Calls mbamapregs() to setup
    277  * for DMA transfer, then the unit-specific start routine.
    278  */
    279 void
    280 mbastart(struct mba_softc *sc)
    281 {
    282 	struct	mba_device *md = sc->sc_first;
    283 	struct	buf *bp = BUFQ_PEEK(&md->md_q);
    284 
    285 	disk_reallymapin(bp, (void *)(sc->sc_ioh + MAPREG(0)), 0, PG_V);
    286 
    287 	sc->sc_state = SC_ACTIVE;
    288 	MBA_WCSR(MBA_VAR, ((u_int)bp->b_data & VAX_PGOFSET));
    289 	MBA_WCSR(MBA_BC, (~bp->b_bcount) + 1);
    290 	(*md->md_start)(md);		/* machine-dependent start */
    291 }
    292