Home | History | Annotate | Line # | Download | only in bi
      1 /*	$NetBSD: kdb.c,v 1.49 2021/08/07 16:19:09 thorpej Exp $ */
      2 /*
      3  * Copyright (c) 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  * KDB50 disk device driver
     35  */
     36 /*
     37  * TODO
     38  *   Implement node reset routine.
     39  *   Nices hardware error handling.
     40  */
     41 
     42 #include <sys/cdefs.h>
     43 __KERNEL_RCSID(0, "$NetBSD: kdb.c,v 1.49 2021/08/07 16:19:09 thorpej Exp $");
     44 
     45 #include <sys/param.h>
     46 #include <sys/kernel.h>
     47 #include <sys/buf.h>
     48 #include <sys/bufq.h>
     49 #include <sys/device.h>
     50 #include <sys/proc.h>
     51 #include <sys/malloc.h>
     52 #include <sys/systm.h>
     53 #include <sys/sched.h>
     54 
     55 #include <uvm/uvm_extern.h>
     56 
     57 #ifdef __vax__
     58 #include <machine/pte.h>
     59 #include <machine/pcb.h>
     60 #endif
     61 #include <sys/bus.h>
     62 
     63 #include <dev/bi/bireg.h>
     64 #include <dev/bi/bivar.h>
     65 #include <dev/bi/kdbreg.h>
     66 
     67 #include <dev/mscp/mscp.h>
     68 #include <dev/mscp/mscpreg.h>
     69 #include <dev/mscp/mscpvar.h>
     70 
     71 #include "locators.h"
     72 
     73 #define KDB_WL(adr, val) bus_space_write_4(sc->sc_iot, sc->sc_ioh, adr, val)
     74 #define KDB_RL(adr) bus_space_read_4(sc->sc_iot, sc->sc_ioh, adr)
     75 #define KDB_RS(adr) bus_space_read_2(sc->sc_iot, sc->sc_ioh, adr)
     76 
     77 #define	    b_forw  b_hash.le_next
     78 /*
     79  * Software status, per controller.
     80  */
     81 struct	kdb_softc {
     82 	device_t sc_dev;		/* Autoconfig info */
     83 	struct evcnt sc_intrcnt;	/* Interrupt counting */
     84 	void *sc_kdb;			/* Struct for kdb communication */
     85 	struct mscp_softc *sc_softc;	/* MSCP info (per mscpvar.h) */
     86 	bus_dma_tag_t sc_dmat;
     87 	bus_dmamap_t sc_cmap;		/* Control structures */
     88 	bus_space_tag_t sc_iot;
     89 	bus_space_handle_t sc_ioh;
     90 };
     91 
     92 int	kdbmatch(device_t, cfdata_t, void *);
     93 void	kdbattach(device_t, device_t, void *);
     94 void	kdbreset(int);
     95 void	kdbintr(void *);
     96 void	kdbctlrdone(device_t);
     97 int	kdbprint(void *, const char *);
     98 void	kdbsaerror(device_t, int);
     99 void	kdbgo(device_t, struct mscp_xi *);
    100 
    101 CFATTACH_DECL_NEW(kdb, sizeof(struct kdb_softc),
    102     kdbmatch, kdbattach, NULL, NULL);
    103 
    104 /*
    105  * More driver definitions, for generic MSCP code.
    106  */
    107 struct	mscp_ctlr kdb_mscp_ctlr = {
    108 	kdbctlrdone,
    109 	kdbgo,
    110 	kdbsaerror,
    111 };
    112 
    113 int
    114 kdbprint(void *aux, const char *name)
    115 {
    116 	if (name)
    117 		aprint_normal("%s: mscpbus", name);
    118 	return UNCONF;
    119 }
    120 
    121 /*
    122  * Poke at a supposed KDB to see if it is there.
    123  */
    124 int
    125 kdbmatch(device_t parent, cfdata_t cf, void *aux)
    126 {
    127 	struct bi_attach_args *ba = aux;
    128 
    129 	if (bus_space_read_2(ba->ba_iot, ba->ba_ioh, BIREG_DTYPE) != BIDT_KDB50)
    130 		return 0;
    131 
    132 	if (cf->cf_loc[BICF_NODE] != BICF_NODE_DEFAULT &&
    133 	    cf->cf_loc[BICF_NODE] != ba->ba_nodenr)
    134 		return 0;
    135 
    136 	return 1;
    137 }
    138 
    139 void
    140 kdbattach(device_t parent, device_t self, void *aux)
    141 {
    142 	struct kdb_softc *sc = device_private(self);
    143 	struct bi_attach_args *ba = aux;
    144 	struct mscp_attach_args ma;
    145 	volatile int i = 10000;
    146 	int error, rseg;
    147 	bus_dma_segment_t seg;
    148 
    149 	sc->sc_dev = self;
    150 
    151 	printf("\n");
    152 	bi_intr_establish(ba->ba_icookie, ba->ba_ivec,
    153 		kdbintr, sc, &sc->sc_intrcnt);
    154 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    155 		device_xname(sc->sc_dev), "intr");
    156 
    157 	sc->sc_iot = ba->ba_iot;
    158 	sc->sc_ioh = ba->ba_ioh;
    159 	sc->sc_dmat = ba->ba_dmat;
    160 
    161 	/*
    162 	 * Map the communication area and command and
    163 	 * response packets into Unibus space.
    164 	 */
    165 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mscp_pack),
    166 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
    167 		printf("Alloc ctrl area %d\n", error);
    168 		return;
    169 	}
    170 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
    171 	    sizeof(struct mscp_pack), &sc->sc_kdb,
    172 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    173 		printf("Map ctrl area %d\n", error);
    174 err:		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
    175 		return;
    176 	}
    177 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct mscp_pack),
    178 	    1, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT, &sc->sc_cmap))) {
    179 		printf("Create DMA map %d\n", error);
    180 err2:		bus_dmamem_unmap(sc->sc_dmat, sc->sc_kdb,
    181 		    sizeof(struct mscp_pack));
    182 		goto err;
    183 	}
    184 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
    185 	    sc->sc_kdb, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT))) {
    186 		printf("Load ctrl map %d\n", error);
    187 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
    188 		goto err2;
    189 	}
    190 	memset(sc->sc_kdb, 0, sizeof(struct mscp_pack));
    191 
    192 	ma.ma_mc = &kdb_mscp_ctlr;
    193 	ma.ma_type = MSCPBUS_DISK|MSCPBUS_KDB;
    194 	ma.ma_uda = (struct mscp_pack *)sc->sc_kdb;
    195 	ma.ma_softc = &sc->sc_softc;
    196 	ma.ma_iot = sc->sc_iot;
    197 	ma.ma_iph = sc->sc_ioh + KDB_IP;
    198 	ma.ma_sah = sc->sc_ioh + KDB_SA;
    199 	ma.ma_swh = sc->sc_ioh + KDB_SW;
    200 	ma.ma_dmat = sc->sc_dmat;
    201 	ma.ma_dmam = sc->sc_cmap;
    202 	ma.ma_ivec = ba->ba_ivec;
    203 	ma.ma_ctlrnr = ba->ba_nodenr;
    204 	ma.ma_adapnr = ba->ba_busnr;
    205 
    206 	KDB_WL(BIREG_VAXBICSR, KDB_RL(BIREG_VAXBICSR) | BICSR_NRST);
    207 	while (i--) /* Need delay??? */
    208 		;
    209 	KDB_WL(BIREG_INTRDES, ba->ba_intcpu); /* Interrupt on CPU # */
    210 	KDB_WL(BIREG_BCICSR, KDB_RL(BIREG_BCICSR) |
    211 	    BCI_STOPEN | BCI_IDENTEN | BCI_UINTEN | BCI_INTEN);
    212 	KDB_WL(BIREG_UINTRCSR, ba->ba_ivec);
    213 	config_found(sc->sc_dev, &ma, kdbprint, CFARGS_NONE);
    214 }
    215 
    216 void
    217 kdbgo(device_t usc, struct mscp_xi *mxi)
    218 {
    219 	struct kdb_softc *sc = device_private(usc);
    220 	struct buf *bp = mxi->mxi_bp;
    221 	struct mscp *mp = mxi->mxi_mp;
    222 	u_int32_t addr = (u_int32_t)bp->b_data;
    223 	u_int32_t mapaddr;
    224 	int err;
    225 
    226 	/*
    227 	 * The KDB50 wants to read VAX Page tables directly, therefore
    228 	 * the result from bus_dmamap_load() is uninteresting. (But it
    229 	 * should never fail!).
    230 	 *
    231 	 * On VAX, point to the corresponding page tables. (user/sys)
    232 	 * On other systems, do something else...
    233 	 */
    234 	err = bus_dmamap_load(sc->sc_dmat, mxi->mxi_dmam, bp->b_data,
    235 	    bp->b_bcount, (bp->b_flags & B_PHYS ? bp->b_proc : 0),
    236 	    BUS_DMA_NOWAIT);
    237 
    238 	if (err) /* Shouldn't happen */
    239 		panic("kdbgo: bus_dmamap_load: error %d", err);
    240 
    241 #ifdef __vax__
    242 	/*
    243 	 * Get a pointer to the pte pointing out the first virtual address.
    244 	 * Use different ways in kernel and user space.
    245 	 */
    246 	if ((bp->b_flags & B_PHYS) == 0) {
    247 		mapaddr = ((u_int32_t)kvtopte(addr)) & ~KERNBASE;
    248 	} else {
    249 
    250 /* XXX: This code does not belong here! */
    251 #define	UVTOPTE(addr, pmap) (((addr) < 0x40000000) ? \
    252     &(*pmap)->pm_p0br[PG_PFNUM(addr)] : &(*pmap)->pm_p1br[PG_PFNUM(addr)])
    253 
    254 		pmap_t *pmap = &bp->b_proc->p_vmspace->vm_map.pmap;
    255 		u_int32_t eaddr = addr + (bp->b_bcount - 1);
    256 		u_int32_t emapaddr = (u_int32_t)UVTOPTE(eaddr, pmap);
    257 
    258 		mapaddr = (u_int32_t)UVTOPTE(addr, pmap);
    259 		if (trunc_page(mapaddr) != trunc_page(emapaddr)) {
    260 			mp->mscp_seq.seq_bytecount =
    261 			    (((round_page(mapaddr) - mapaddr)/4) * 512);
    262 		}
    263 		mapaddr = kvtophys(mapaddr);
    264 	}
    265 #else
    266 #error Must write code to handle KDB50 on non-vax.
    267 #endif
    268 
    269 	mp->mscp_seq.seq_mapbase = mapaddr;
    270 	mxi->mxi_dmam->dm_segs[0].ds_addr = (addr & 511) | KDB_MAP;
    271 	mscp_dgo(sc->sc_softc, mxi);
    272 }
    273 
    274 void
    275 kdbsaerror(device_t usc, int doreset)
    276 {
    277 	struct kdb_softc *sc = device_private(usc);
    278 
    279 	if ((KDB_RS(KDB_SA) & MP_ERR) == 0)
    280 		return;
    281 	printf("%s: controller error, sa=0x%x\n", device_xname(sc->sc_dev),
    282 	    KDB_RS(KDB_SA));
    283 	/* What to do now??? */
    284 }
    285 
    286 /*
    287  * Interrupt routine.  Depending on the state of the controller,
    288  * continue initialisation, or acknowledge command and response
    289  * interrupts, and process responses.
    290  */
    291 void
    292 kdbintr(void *arg)
    293 {
    294 	struct kdb_softc *sc = arg;
    295 
    296 	if (KDB_RS(KDB_SA) & MP_ERR) {	/* ctlr fatal error */
    297 		kdbsaerror(sc->sc_dev, 1);
    298 		return;
    299 	}
    300 	KERNEL_LOCK(1, NULL);
    301 	mscp_intr(sc->sc_softc);
    302 	KERNEL_UNLOCK_ONE(NULL);
    303 }
    304 
    305 void
    306 kdbctlrdone(device_t usc)
    307 {
    308 }
    309