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