Home | History | Annotate | Line # | Download | only in bi
kdb.c revision 1.21
      1  1.21      matt /*	$NetBSD: kdb.c,v 1.21 2000/06/05 00:09:17 matt Exp $ */
      2   1.1     ragge /*
      3   1.1     ragge  * Copyright (c) 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.12     ragge  *	This product includes software developed at Ludd, University of
     17  1.12     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.1     ragge /*
     34   1.1     ragge  * KDB50 disk device driver
     35   1.1     ragge  */
     36   1.1     ragge /*
     37   1.1     ragge  * TODO
     38   1.1     ragge  *   Implement node reset routine.
     39   1.1     ragge  *   Nices hardware error handling.
     40   1.1     ragge  */
     41   1.1     ragge 
     42   1.1     ragge #include <sys/param.h>
     43   1.1     ragge #include <sys/kernel.h>
     44   1.1     ragge #include <sys/buf.h>
     45   1.1     ragge #include <sys/device.h>
     46   1.1     ragge #include <sys/proc.h>
     47  1.10     ragge #include <sys/user.h>
     48   1.1     ragge #include <sys/malloc.h>
     49  1.12     ragge #include <sys/systm.h>
     50   1.1     ragge 
     51   1.1     ragge #include <vm/vm.h>
     52   1.1     ragge #include <vm/vm_kern.h>
     53   1.1     ragge 
     54  1.17     ragge #ifdef __vax__
     55   1.1     ragge #include <machine/pte.h>
     56   1.1     ragge #include <machine/pcb.h>
     57  1.17     ragge #endif
     58  1.17     ragge #include <machine/bus.h>
     59   1.1     ragge 
     60  1.17     ragge #include <dev/bi/bireg.h>
     61  1.17     ragge #include <dev/bi/bivar.h>
     62  1.17     ragge #include <dev/bi/kdbreg.h>
     63  1.17     ragge 
     64  1.17     ragge #include <dev/mscp/mscp.h>
     65  1.17     ragge #include <dev/mscp/mscpreg.h>
     66  1.17     ragge #include <dev/mscp/mscpvar.h>
     67   1.1     ragge 
     68   1.7       jtk #include "locators.h"
     69   1.7       jtk 
     70  1.17     ragge #define KDB_WL(adr, val) bus_space_write_4(sc->sc_iot, sc->sc_ioh, adr, val)
     71  1.17     ragge #define KDB_RL(adr) bus_space_read_4(sc->sc_iot, sc->sc_ioh, adr)
     72  1.17     ragge #define KDB_RS(adr) bus_space_read_2(sc->sc_iot, sc->sc_ioh, adr)
     73  1.17     ragge 
     74  1.12     ragge #define	    b_forw  b_hash.le_next
     75   1.1     ragge /*
     76   1.1     ragge  * Software status, per controller.
     77   1.1     ragge  */
     78   1.1     ragge struct	kdb_softc {
     79   1.1     ragge 	struct	device sc_dev;		/* Autoconfig info */
     80  1.20      matt 	struct	evcnt sc_intrcnt;	/* Interrupt counting */
     81  1.17     ragge 	caddr_t	sc_kdb;			/* Struct for kdb communication */
     82   1.1     ragge 	struct	mscp_softc *sc_softc;	/* MSCP info (per mscpvar.h) */
     83  1.17     ragge 	bus_dma_tag_t sc_dmat;
     84  1.17     ragge 	bus_dmamap_t sc_cmap;		/* Control structures */
     85  1.17     ragge 	bus_space_tag_t sc_iot;
     86  1.17     ragge 	bus_space_handle_t sc_ioh;
     87   1.1     ragge };
     88   1.1     ragge 
     89  1.11     ragge int	kdbmatch __P((struct device *, struct cfdata *, void *));
     90   1.1     ragge void	kdbattach __P((struct device *, struct device *, void *));
     91   1.1     ragge void	kdbreset __P((int));
     92  1.17     ragge void	kdbintr __P((void *));
     93  1.17     ragge void	kdbctlrdone __P((struct device *));
     94   1.2       cgd int	kdbprint __P((void *, const char *));
     95   1.1     ragge void	kdbsaerror __P((struct device *, int));
     96  1.17     ragge void	kdbgo __P((struct device *, struct mscp_xi *));
     97   1.1     ragge 
     98   1.1     ragge struct	cfattach kdb_ca = {
     99   1.1     ragge 	sizeof(struct kdb_softc), kdbmatch, kdbattach
    100   1.1     ragge };
    101   1.9   thorpej 
    102   1.1     ragge /*
    103   1.1     ragge  * More driver definitions, for generic MSCP code.
    104   1.1     ragge  */
    105   1.1     ragge struct	mscp_ctlr kdb_mscp_ctlr = {
    106   1.1     ragge 	kdbctlrdone,
    107   1.1     ragge 	kdbgo,
    108   1.1     ragge 	kdbsaerror,
    109   1.1     ragge };
    110   1.1     ragge 
    111   1.1     ragge int
    112   1.1     ragge kdbprint(aux, name)
    113   1.1     ragge 	void	*aux;
    114   1.2       cgd 	const char	*name;
    115   1.1     ragge {
    116   1.1     ragge 	if (name)
    117   1.4  christos 		printf("%s: mscpbus", name);
    118   1.1     ragge 	return UNCONF;
    119   1.1     ragge }
    120   1.1     ragge 
    121   1.1     ragge /*
    122   1.1     ragge  * Poke at a supposed KDB to see if it is there.
    123   1.1     ragge  */
    124   1.1     ragge int
    125  1.11     ragge kdbmatch(parent, cf, aux)
    126   1.1     ragge 	struct	device *parent;
    127  1.12     ragge 	struct	cfdata *cf;
    128  1.11     ragge 	void	*aux;
    129   1.1     ragge {
    130   1.1     ragge 	struct bi_attach_args *ba = aux;
    131   1.1     ragge 
    132  1.17     ragge 	if (bus_space_read_2(ba->ba_iot, ba->ba_ioh, BIREG_DTYPE) != BIDT_KDB50)
    133  1.12     ragge 		return 0;
    134   1.1     ragge 
    135  1.12     ragge 	if (cf->cf_loc[BICF_NODE] != BICF_NODE_DEFAULT &&
    136   1.7       jtk 	    cf->cf_loc[BICF_NODE] != ba->ba_nodenr)
    137  1.12     ragge 		return 0;
    138   1.1     ragge 
    139  1.12     ragge 	return 1;
    140   1.1     ragge }
    141   1.1     ragge 
    142   1.1     ragge void
    143   1.1     ragge kdbattach(parent, self, aux)
    144   1.1     ragge 	struct device *parent, *self;
    145   1.1     ragge 	void *aux;
    146   1.1     ragge {
    147   1.1     ragge 	struct	kdb_softc *sc = (void *)self;
    148   1.1     ragge 	struct	bi_attach_args *ba = aux;
    149   1.1     ragge 	struct	mscp_attach_args ma;
    150   1.1     ragge 	volatile int i = 10000;
    151  1.17     ragge 	int error, rseg;
    152  1.17     ragge 	bus_dma_segment_t seg;
    153   1.1     ragge 
    154   1.4  christos 	printf("\n");
    155  1.20      matt 	bi_intr_establish(ba->ba_icookie, ba->ba_ivec,
    156  1.20      matt 		kdbintr, sc, &sc->sc_intrcnt);
    157  1.21      matt 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    158  1.21      matt 		sc->sc_dev.dv_xname, "intr");
    159   1.1     ragge 
    160  1.17     ragge 	sc->sc_iot = ba->ba_iot;
    161  1.17     ragge 	sc->sc_ioh = ba->ba_ioh;
    162  1.17     ragge 	sc->sc_dmat = ba->ba_dmat;
    163  1.17     ragge 
    164  1.17     ragge 	/*
    165  1.17     ragge 	 * Map the communication area and command and
    166  1.17     ragge 	 * response packets into Unibus space.
    167  1.17     ragge 	 */
    168  1.17     ragge 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mscp_pack),
    169  1.17     ragge 	    NBPG, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    170  1.17     ragge 		printf("Alloc ctrl area %d\n", error);
    171  1.17     ragge 		return;
    172  1.17     ragge 	}
    173  1.17     ragge 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    174  1.17     ragge 	    sizeof(struct mscp_pack), &sc->sc_kdb,
    175  1.17     ragge 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    176  1.17     ragge 		printf("Map ctrl area %d\n", error);
    177  1.17     ragge err:		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    178  1.17     ragge 		return;
    179  1.17     ragge 	}
    180  1.17     ragge 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct mscp_pack),
    181  1.17     ragge 	    1, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT, &sc->sc_cmap))) {
    182  1.17     ragge 		printf("Create DMA map %d\n", error);
    183  1.17     ragge err2:		bus_dmamem_unmap(sc->sc_dmat, sc->sc_kdb,
    184  1.17     ragge 		    sizeof(struct mscp_pack));
    185  1.17     ragge 		goto err;
    186  1.17     ragge 	}
    187  1.17     ragge 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
    188  1.17     ragge 	    sc->sc_kdb, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT))) {
    189  1.17     ragge 		printf("Load ctrl map %d\n", error);
    190  1.17     ragge 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
    191  1.17     ragge 		goto err2;
    192  1.17     ragge 	}
    193  1.17     ragge 	bzero(sc->sc_kdb, sizeof(struct mscp_pack));
    194   1.1     ragge 
    195   1.1     ragge 	ma.ma_mc = &kdb_mscp_ctlr;
    196   1.1     ragge 	ma.ma_type = MSCPBUS_DISK|MSCPBUS_KDB;
    197  1.17     ragge 	ma.ma_uda = (struct mscp_pack *)sc->sc_kdb;
    198   1.1     ragge 	ma.ma_softc = &sc->sc_softc;
    199  1.17     ragge 	ma.ma_iot = sc->sc_iot;
    200  1.17     ragge 	ma.ma_iph = sc->sc_ioh + KDB_IP;
    201  1.17     ragge 	ma.ma_sah = sc->sc_ioh + KDB_SA;
    202  1.17     ragge 	ma.ma_swh = sc->sc_ioh + KDB_SW;
    203  1.17     ragge 	ma.ma_dmat = sc->sc_dmat;
    204  1.17     ragge 	ma.ma_dmam = sc->sc_cmap;
    205  1.17     ragge 	ma.ma_ivec = ba->ba_ivec;
    206   1.1     ragge 	ma.ma_ctlrnr = ba->ba_nodenr;
    207  1.17     ragge 	ma.ma_adapnr = ba->ba_busnr;
    208  1.17     ragge 
    209  1.17     ragge 	KDB_WL(BIREG_VAXBICSR, KDB_RL(BIREG_VAXBICSR) | BICSR_NRST);
    210   1.1     ragge 	while (i--) /* Need delay??? */
    211   1.1     ragge 		;
    212  1.17     ragge 	KDB_WL(BIREG_INTRDES, ba->ba_intcpu); /* Interrupt on CPU # */
    213  1.17     ragge 	KDB_WL(BIREG_BCICSR, KDB_RL(BIREG_BCICSR) |
    214  1.17     ragge 	    BCI_STOPEN | BCI_IDENTEN | BCI_UINTEN | BCI_INTEN);
    215  1.17     ragge 	KDB_WL(BIREG_UINTRCSR, ba->ba_ivec);
    216   1.1     ragge 	config_found(&sc->sc_dev, &ma, kdbprint);
    217   1.1     ragge }
    218   1.1     ragge 
    219  1.17     ragge void
    220  1.17     ragge kdbgo(usc, mxi)
    221   1.1     ragge 	struct device *usc;
    222  1.17     ragge 	struct mscp_xi *mxi;
    223   1.1     ragge {
    224   1.1     ragge 	struct kdb_softc *sc = (void *)usc;
    225  1.17     ragge 	struct buf *bp = mxi->mxi_bp;
    226  1.17     ragge 	struct mscp *mp = mxi->mxi_mp;
    227  1.18   thorpej 	u_int32_t addr = (u_int32_t)bp->b_data;
    228  1.17     ragge 	u_int32_t mapaddr;
    229  1.17     ragge 	int err;
    230   1.1     ragge 
    231  1.12     ragge 	/*
    232  1.17     ragge 	 * The KDB50 wants to read VAX Page tables directly, therefore
    233  1.17     ragge 	 * the result from bus_dmamap_load() is uninteresting. (But it
    234  1.17     ragge 	 * should never fail!).
    235  1.17     ragge 	 *
    236  1.17     ragge 	 * On VAX, point to the corresponding page tables. (user/sys)
    237  1.17     ragge 	 * On other systems, do something else...
    238  1.17     ragge 	 */
    239  1.18   thorpej 	err = bus_dmamap_load(sc->sc_dmat, mxi->mxi_dmam, bp->b_data,
    240  1.17     ragge 	    bp->b_bcount, bp->b_proc, BUS_DMA_NOWAIT);
    241  1.17     ragge 
    242  1.17     ragge 	if (err) /* Shouldn't happen */
    243  1.17     ragge 		panic("kdbgo: bus_dmamap_load: error %d", err);
    244  1.17     ragge 
    245  1.17     ragge #ifdef __vax__
    246  1.17     ragge 	/*
    247  1.12     ragge 	 * Get a pointer to the pte pointing out the first virtual address.
    248  1.12     ragge 	 * Use different ways in kernel and user space.
    249  1.12     ragge 	 */
    250  1.12     ragge 	if ((bp->b_flags & B_PHYS) == 0) {
    251  1.17     ragge 		mapaddr = ((u_int32_t)kvtopte(addr)) & ~KERNBASE;
    252  1.12     ragge 	} else {
    253  1.17     ragge 		struct pcb *pcb;
    254  1.17     ragge 		u_int32_t eaddr;
    255  1.17     ragge 
    256  1.17     ragge 		/*
    257  1.17     ragge 		 * We check if the PTE's needed crosses a page boundary.
    258  1.17     ragge 		 * If they do; only transfer the amount of data that is
    259  1.17     ragge 		 * mapped by the first PTE page and led the system handle
    260  1.17     ragge 		 * the rest of the data.
    261  1.17     ragge 		 */
    262  1.12     ragge 		pcb = &bp->b_proc->p_addr->u_pcb;
    263  1.17     ragge 		mapaddr = (u_int32_t)uvtopte(addr, pcb);
    264  1.17     ragge 		eaddr = (u_int32_t)uvtopte(addr + (bp->b_bcount - 1), pcb);
    265  1.17     ragge 		if (trunc_page(mapaddr) != trunc_page(eaddr)) {
    266  1.17     ragge 			mp->mscp_seq.seq_bytecount =
    267  1.17     ragge 			    (((round_page(mapaddr) - mapaddr)/4) * 512);
    268  1.17     ragge 		}
    269  1.17     ragge 		mapaddr = kvtophys(mapaddr);
    270  1.12     ragge 	}
    271  1.17     ragge #else
    272  1.17     ragge #error Must write code to handle KDB50 on non-vax.
    273  1.17     ragge #endif
    274  1.12     ragge 
    275  1.17     ragge 	mp->mscp_seq.seq_mapbase = mapaddr;
    276  1.17     ragge 	mxi->mxi_dmam->dm_segs[0].ds_addr = (addr & 511) | KDB_MAP;
    277  1.17     ragge 	mscp_dgo(sc->sc_softc, mxi);
    278   1.1     ragge }
    279   1.1     ragge 
    280   1.1     ragge void
    281   1.1     ragge kdbsaerror(usc, doreset)
    282   1.1     ragge 	struct device *usc;
    283   1.1     ragge 	int doreset;
    284   1.1     ragge {
    285   1.1     ragge 	struct	kdb_softc *sc = (void *)usc;
    286   1.1     ragge 
    287  1.17     ragge 	if ((KDB_RS(KDB_SA) & MP_ERR) == 0)
    288   1.1     ragge 		return;
    289  1.17     ragge 	printf("%s: controller error, sa=0x%x\n", sc->sc_dev.dv_xname,
    290  1.17     ragge 	    KDB_RS(KDB_SA));
    291   1.1     ragge 	/* What to do now??? */
    292   1.1     ragge }
    293   1.1     ragge 
    294   1.1     ragge /*
    295   1.1     ragge  * Interrupt routine.  Depending on the state of the controller,
    296   1.1     ragge  * continue initialisation, or acknowledge command and response
    297   1.1     ragge  * interrupts, and process responses.
    298   1.1     ragge  */
    299   1.1     ragge void
    300  1.17     ragge kdbintr(void *arg)
    301   1.1     ragge {
    302  1.17     ragge 	struct kdb_softc *sc = arg;
    303   1.1     ragge 
    304  1.17     ragge 	if (KDB_RS(KDB_SA) & MP_ERR) {	/* ctlr fatal error */
    305   1.1     ragge 		kdbsaerror(&sc->sc_dev, 1);
    306   1.1     ragge 		return;
    307   1.1     ragge 	}
    308   1.1     ragge 	mscp_intr(sc->sc_softc);
    309   1.1     ragge }
    310   1.1     ragge 
    311   1.1     ragge #ifdef notyet
    312   1.1     ragge /*
    313   1.1     ragge  * The KDB50 has been reset.  Reinitialise the controller
    314   1.1     ragge  * and requeue outstanding I/O.
    315   1.1     ragge  */
    316   1.1     ragge void
    317   1.1     ragge kdbreset(ctlr)
    318   1.1     ragge 	int ctlr;
    319   1.1     ragge {
    320  1.17     ragge 	struct kdb_softc *sc;
    321   1.1     ragge 
    322   1.1     ragge 	sc = kdb_cd.cd_devs[ctlr];
    323   1.4  christos 	printf(" kdb%d", ctlr);
    324   1.1     ragge 
    325   1.1     ragge 
    326   1.1     ragge 	/* reset queues and requeue pending transfers */
    327   1.1     ragge 	mscp_requeue(sc->sc_softc);
    328   1.1     ragge 
    329   1.1     ragge 	/*
    330   1.1     ragge 	 * If it fails to initialise we will notice later and
    331   1.1     ragge 	 * try again (and again...).  Do not call kdbstart()
    332   1.1     ragge 	 * here; it will be done after the controller finishes
    333   1.1     ragge 	 * initialisation.
    334   1.1     ragge 	 */
    335   1.1     ragge 	if (kdbinit(sc))
    336   1.4  christos 		printf(" (hung)");
    337   1.1     ragge }
    338   1.1     ragge #endif
    339   1.1     ragge 
    340   1.1     ragge void
    341  1.17     ragge kdbctlrdone(usc)
    342   1.1     ragge 	struct device *usc;
    343   1.1     ragge {
    344   1.1     ragge }
    345