xref: /netbsd-src/sys/dev/bi/kdb.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1*c7fb772bSthorpej /*	$NetBSD: kdb.c,v 1.49 2021/08/07 16:19:09 thorpej Exp $ */
2f5e89154Sragge /*
3f5e89154Sragge  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
4f5e89154Sragge  * All rights reserved.
5f5e89154Sragge  *
6f5e89154Sragge  * Redistribution and use in source and binary forms, with or without
7f5e89154Sragge  * modification, are permitted provided that the following conditions
8f5e89154Sragge  * are met:
9f5e89154Sragge  * 1. Redistributions of source code must retain the above copyright
10f5e89154Sragge  *    notice, this list of conditions and the following disclaimer.
11f5e89154Sragge  * 2. Redistributions in binary form must reproduce the above copyright
12f5e89154Sragge  *    notice, this list of conditions and the following disclaimer in the
13f5e89154Sragge  *    documentation and/or other materials provided with the distribution.
14f5e89154Sragge  * 3. All advertising materials mentioning features or use of this software
15f5e89154Sragge  *    must display the following acknowledgement:
16f5e89154Sragge  *	This product includes software developed at Ludd, University of
17f5e89154Sragge  *	Lule}, Sweden and its contributors.
18f5e89154Sragge  * 4. The name of the author may not be used to endorse or promote products
19f5e89154Sragge  *    derived from this software without specific prior written permission
20f5e89154Sragge  *
21f5e89154Sragge  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22f5e89154Sragge  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23f5e89154Sragge  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24f5e89154Sragge  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25f5e89154Sragge  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26f5e89154Sragge  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27f5e89154Sragge  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28f5e89154Sragge  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29f5e89154Sragge  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30f5e89154Sragge  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31f5e89154Sragge  */
32f5e89154Sragge 
33f5e89154Sragge /*
34f5e89154Sragge  * KDB50 disk device driver
35f5e89154Sragge  */
36f5e89154Sragge /*
37f5e89154Sragge  * TODO
38f5e89154Sragge  *   Implement node reset routine.
39f5e89154Sragge  *   Nices hardware error handling.
40f5e89154Sragge  */
41f5e89154Sragge 
42f61cbe74Slukem #include <sys/cdefs.h>
43*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: kdb.c,v 1.49 2021/08/07 16:19:09 thorpej Exp $");
44f61cbe74Slukem 
45f5e89154Sragge #include <sys/param.h>
46f5e89154Sragge #include <sys/kernel.h>
47f5e89154Sragge #include <sys/buf.h>
486ef865c9She #include <sys/bufq.h>
49f5e89154Sragge #include <sys/device.h>
50f5e89154Sragge #include <sys/proc.h>
51f5e89154Sragge #include <sys/malloc.h>
52f5e89154Sragge #include <sys/systm.h>
53cae5d5a7Sragge #include <sys/sched.h>
54f5e89154Sragge 
55b5e0d568Smrg #include <uvm/uvm_extern.h>
56f5e89154Sragge 
5730819cb8Sragge #ifdef __vax__
58f5e89154Sragge #include <machine/pte.h>
59f5e89154Sragge #include <machine/pcb.h>
6030819cb8Sragge #endif
61a2a38285Sad #include <sys/bus.h>
62f5e89154Sragge 
6330819cb8Sragge #include <dev/bi/bireg.h>
6430819cb8Sragge #include <dev/bi/bivar.h>
6530819cb8Sragge #include <dev/bi/kdbreg.h>
66f5e89154Sragge 
6730819cb8Sragge #include <dev/mscp/mscp.h>
6830819cb8Sragge #include <dev/mscp/mscpreg.h>
6930819cb8Sragge #include <dev/mscp/mscpvar.h>
70f5e89154Sragge 
71f5e89154Sragge #include "locators.h"
72f5e89154Sragge 
7330819cb8Sragge #define KDB_WL(adr, val) bus_space_write_4(sc->sc_iot, sc->sc_ioh, adr, val)
7430819cb8Sragge #define KDB_RL(adr) bus_space_read_4(sc->sc_iot, sc->sc_ioh, adr)
7530819cb8Sragge #define KDB_RS(adr) bus_space_read_2(sc->sc_iot, sc->sc_ioh, adr)
7630819cb8Sragge 
77f5e89154Sragge #define	    b_forw  b_hash.le_next
78f5e89154Sragge /*
79f5e89154Sragge  * Software status, per controller.
80f5e89154Sragge  */
81f5e89154Sragge struct	kdb_softc {
82dfba8166Smatt 	device_t sc_dev;		/* Autoconfig info */
830bd304e5Smatt 	struct evcnt sc_intrcnt;	/* Interrupt counting */
8453524e44Schristos 	void *sc_kdb;			/* Struct for kdb communication */
85f5e89154Sragge 	struct mscp_softc *sc_softc;	/* MSCP info (per mscpvar.h) */
8630819cb8Sragge 	bus_dma_tag_t sc_dmat;
8730819cb8Sragge 	bus_dmamap_t sc_cmap;		/* Control structures */
8830819cb8Sragge 	bus_space_tag_t sc_iot;
8930819cb8Sragge 	bus_space_handle_t sc_ioh;
90f5e89154Sragge };
91f5e89154Sragge 
92dfba8166Smatt int	kdbmatch(device_t, cfdata_t, void *);
93dfba8166Smatt void	kdbattach(device_t, device_t, void *);
9418db93c7Sperry void	kdbreset(int);
9518db93c7Sperry void	kdbintr(void *);
96dfba8166Smatt void	kdbctlrdone(device_t);
9718db93c7Sperry int	kdbprint(void *, const char *);
98dfba8166Smatt void	kdbsaerror(device_t, int);
99dfba8166Smatt void	kdbgo(device_t, struct mscp_xi *);
100f5e89154Sragge 
10153485c0aSjoerg CFATTACH_DECL_NEW(kdb, sizeof(struct kdb_softc),
102c9b3657cSthorpej     kdbmatch, kdbattach, NULL, NULL);
103f5e89154Sragge 
104f5e89154Sragge /*
105f5e89154Sragge  * More driver definitions, for generic MSCP code.
106f5e89154Sragge  */
107f5e89154Sragge struct	mscp_ctlr kdb_mscp_ctlr = {
108f5e89154Sragge 	kdbctlrdone,
109f5e89154Sragge 	kdbgo,
110f5e89154Sragge 	kdbsaerror,
111f5e89154Sragge };
112f5e89154Sragge 
113f5e89154Sragge int
kdbprint(void * aux,const char * name)114dfba8166Smatt kdbprint(void *aux, const char *name)
115f5e89154Sragge {
116f5e89154Sragge 	if (name)
11772a7af27Sthorpej 		aprint_normal("%s: mscpbus", name);
118f5e89154Sragge 	return UNCONF;
119f5e89154Sragge }
120f5e89154Sragge 
121f5e89154Sragge /*
122f5e89154Sragge  * Poke at a supposed KDB to see if it is there.
123f5e89154Sragge  */
124f5e89154Sragge int
kdbmatch(device_t parent,cfdata_t cf,void * aux)12553485c0aSjoerg kdbmatch(device_t parent, cfdata_t cf, void *aux)
126f5e89154Sragge {
127f5e89154Sragge 	struct bi_attach_args *ba = aux;
128f5e89154Sragge 
12930819cb8Sragge 	if (bus_space_read_2(ba->ba_iot, ba->ba_ioh, BIREG_DTYPE) != BIDT_KDB50)
130f5e89154Sragge 		return 0;
131f5e89154Sragge 
132f5e89154Sragge 	if (cf->cf_loc[BICF_NODE] != BICF_NODE_DEFAULT &&
133f5e89154Sragge 	    cf->cf_loc[BICF_NODE] != ba->ba_nodenr)
134f5e89154Sragge 		return 0;
135f5e89154Sragge 
136f5e89154Sragge 	return 1;
137f5e89154Sragge }
138f5e89154Sragge 
139f5e89154Sragge void
kdbattach(device_t parent,device_t self,void * aux)140dfba8166Smatt kdbattach(device_t parent, device_t self, void *aux)
141f5e89154Sragge {
142dfba8166Smatt 	struct kdb_softc *sc = device_private(self);
143f5e89154Sragge 	struct bi_attach_args *ba = aux;
144f5e89154Sragge 	struct mscp_attach_args ma;
145f5e89154Sragge 	volatile int i = 10000;
14630819cb8Sragge 	int error, rseg;
14730819cb8Sragge 	bus_dma_segment_t seg;
148f5e89154Sragge 
14953485c0aSjoerg 	sc->sc_dev = self;
15053485c0aSjoerg 
151f5e89154Sragge 	printf("\n");
1520bd304e5Smatt 	bi_intr_establish(ba->ba_icookie, ba->ba_ivec,
1530bd304e5Smatt 		kdbintr, sc, &sc->sc_intrcnt);
1542f85fe7aSmatt 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
155dfba8166Smatt 		device_xname(sc->sc_dev), "intr");
156f5e89154Sragge 
15730819cb8Sragge 	sc->sc_iot = ba->ba_iot;
15830819cb8Sragge 	sc->sc_ioh = ba->ba_ioh;
15930819cb8Sragge 	sc->sc_dmat = ba->ba_dmat;
16030819cb8Sragge 
16130819cb8Sragge 	/*
16230819cb8Sragge 	 * Map the communication area and command and
16330819cb8Sragge 	 * response packets into Unibus space.
16430819cb8Sragge 	 */
16530819cb8Sragge 	if ((error = bus_dmamem_alloc(sc->sc_dmat, sizeof(struct mscp_pack),
16624ab4adbSthorpej 	    PAGE_SIZE, 0, &seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
16730819cb8Sragge 		printf("Alloc ctrl area %d\n", error);
16830819cb8Sragge 		return;
16930819cb8Sragge 	}
17030819cb8Sragge 	if ((error = bus_dmamem_map(sc->sc_dmat, &seg, rseg,
17130819cb8Sragge 	    sizeof(struct mscp_pack), &sc->sc_kdb,
17230819cb8Sragge 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
17330819cb8Sragge 		printf("Map ctrl area %d\n", error);
17430819cb8Sragge err:		bus_dmamem_free(sc->sc_dmat, &seg, rseg);
17530819cb8Sragge 		return;
17630819cb8Sragge 	}
17730819cb8Sragge 	if ((error = bus_dmamap_create(sc->sc_dmat, sizeof(struct mscp_pack),
17830819cb8Sragge 	    1, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT, &sc->sc_cmap))) {
17930819cb8Sragge 		printf("Create DMA map %d\n", error);
18030819cb8Sragge err2:		bus_dmamem_unmap(sc->sc_dmat, sc->sc_kdb,
18130819cb8Sragge 		    sizeof(struct mscp_pack));
18230819cb8Sragge 		goto err;
18330819cb8Sragge 	}
18430819cb8Sragge 	if ((error = bus_dmamap_load(sc->sc_dmat, sc->sc_cmap,
18530819cb8Sragge 	    sc->sc_kdb, sizeof(struct mscp_pack), 0, BUS_DMA_NOWAIT))) {
18630819cb8Sragge 		printf("Load ctrl map %d\n", error);
18730819cb8Sragge 		bus_dmamap_destroy(sc->sc_dmat, sc->sc_cmap);
18830819cb8Sragge 		goto err2;
18930819cb8Sragge 	}
190c6207717Sthorpej 	memset(sc->sc_kdb, 0, sizeof(struct mscp_pack));
191f5e89154Sragge 
192f5e89154Sragge 	ma.ma_mc = &kdb_mscp_ctlr;
193f5e89154Sragge 	ma.ma_type = MSCPBUS_DISK|MSCPBUS_KDB;
19430819cb8Sragge 	ma.ma_uda = (struct mscp_pack *)sc->sc_kdb;
195f5e89154Sragge 	ma.ma_softc = &sc->sc_softc;
19630819cb8Sragge 	ma.ma_iot = sc->sc_iot;
19730819cb8Sragge 	ma.ma_iph = sc->sc_ioh + KDB_IP;
19830819cb8Sragge 	ma.ma_sah = sc->sc_ioh + KDB_SA;
19930819cb8Sragge 	ma.ma_swh = sc->sc_ioh + KDB_SW;
20030819cb8Sragge 	ma.ma_dmat = sc->sc_dmat;
20130819cb8Sragge 	ma.ma_dmam = sc->sc_cmap;
20230819cb8Sragge 	ma.ma_ivec = ba->ba_ivec;
203f5e89154Sragge 	ma.ma_ctlrnr = ba->ba_nodenr;
20430819cb8Sragge 	ma.ma_adapnr = ba->ba_busnr;
20530819cb8Sragge 
20630819cb8Sragge 	KDB_WL(BIREG_VAXBICSR, KDB_RL(BIREG_VAXBICSR) | BICSR_NRST);
207f5e89154Sragge 	while (i--) /* Need delay??? */
208f5e89154Sragge 		;
20930819cb8Sragge 	KDB_WL(BIREG_INTRDES, ba->ba_intcpu); /* Interrupt on CPU # */
21030819cb8Sragge 	KDB_WL(BIREG_BCICSR, KDB_RL(BIREG_BCICSR) |
21130819cb8Sragge 	    BCI_STOPEN | BCI_IDENTEN | BCI_UINTEN | BCI_INTEN);
21230819cb8Sragge 	KDB_WL(BIREG_UINTRCSR, ba->ba_ivec);
213*c7fb772bSthorpej 	config_found(sc->sc_dev, &ma, kdbprint, CFARGS_NONE);
214f5e89154Sragge }
215f5e89154Sragge 
21630819cb8Sragge void
kdbgo(device_t usc,struct mscp_xi * mxi)217dfba8166Smatt kdbgo(device_t usc, struct mscp_xi *mxi)
218f5e89154Sragge {
219dfba8166Smatt 	struct kdb_softc *sc = device_private(usc);
22030819cb8Sragge 	struct buf *bp = mxi->mxi_bp;
22130819cb8Sragge 	struct mscp *mp = mxi->mxi_mp;
222071aed40Sthorpej 	u_int32_t addr = (u_int32_t)bp->b_data;
22330819cb8Sragge 	u_int32_t mapaddr;
22430819cb8Sragge 	int err;
225f5e89154Sragge 
22630819cb8Sragge 	/*
22730819cb8Sragge 	 * The KDB50 wants to read VAX Page tables directly, therefore
22830819cb8Sragge 	 * the result from bus_dmamap_load() is uninteresting. (But it
22930819cb8Sragge 	 * should never fail!).
23030819cb8Sragge 	 *
23130819cb8Sragge 	 * On VAX, point to the corresponding page tables. (user/sys)
23230819cb8Sragge 	 * On other systems, do something else...
23330819cb8Sragge 	 */
234071aed40Sthorpej 	err = bus_dmamap_load(sc->sc_dmat, mxi->mxi_dmam, bp->b_data,
235cae5d5a7Sragge 	    bp->b_bcount, (bp->b_flags & B_PHYS ? bp->b_proc : 0),
236cae5d5a7Sragge 	    BUS_DMA_NOWAIT);
237f5e89154Sragge 
23830819cb8Sragge 	if (err) /* Shouldn't happen */
23930819cb8Sragge 		panic("kdbgo: bus_dmamap_load: error %d", err);
24030819cb8Sragge 
24130819cb8Sragge #ifdef __vax__
242f5e89154Sragge 	/*
243f5e89154Sragge 	 * Get a pointer to the pte pointing out the first virtual address.
244f5e89154Sragge 	 * Use different ways in kernel and user space.
245f5e89154Sragge 	 */
246f5e89154Sragge 	if ((bp->b_flags & B_PHYS) == 0) {
24730819cb8Sragge 		mapaddr = ((u_int32_t)kvtopte(addr)) & ~KERNBASE;
248f5e89154Sragge 	} else {
24930819cb8Sragge 
250808f08e9Schristos /* XXX: This code does not belong here! */
251808f08e9Schristos #define	UVTOPTE(addr, pmap) (((addr) < 0x40000000) ? \
2526a3911e9She     &(*pmap)->pm_p0br[PG_PFNUM(addr)] : &(*pmap)->pm_p1br[PG_PFNUM(addr)])
253b97e4c21Sthorpej 
254808f08e9Schristos 		pmap_t *pmap = &bp->b_proc->p_vmspace->vm_map.pmap;
2556a3911e9She 		u_int32_t eaddr = addr + (bp->b_bcount - 1);
256808f08e9Schristos 		u_int32_t emapaddr = (u_int32_t)UVTOPTE(eaddr, pmap);
257808f08e9Schristos 
258808f08e9Schristos 		mapaddr = (u_int32_t)UVTOPTE(addr, pmap);
259808f08e9Schristos 		if (trunc_page(mapaddr) != trunc_page(emapaddr)) {
26030819cb8Sragge 			mp->mscp_seq.seq_bytecount =
26130819cb8Sragge 			    (((round_page(mapaddr) - mapaddr)/4) * 512);
262f5e89154Sragge 		}
26330819cb8Sragge 		mapaddr = kvtophys(mapaddr);
26430819cb8Sragge 	}
26530819cb8Sragge #else
26630819cb8Sragge #error Must write code to handle KDB50 on non-vax.
26730819cb8Sragge #endif
268f5e89154Sragge 
26930819cb8Sragge 	mp->mscp_seq.seq_mapbase = mapaddr;
27030819cb8Sragge 	mxi->mxi_dmam->dm_segs[0].ds_addr = (addr & 511) | KDB_MAP;
27130819cb8Sragge 	mscp_dgo(sc->sc_softc, mxi);
272f5e89154Sragge }
273f5e89154Sragge 
274f5e89154Sragge void
kdbsaerror(device_t usc,int doreset)275dfba8166Smatt kdbsaerror(device_t usc, int doreset)
276f5e89154Sragge {
277dfba8166Smatt 	struct kdb_softc *sc = device_private(usc);
278f5e89154Sragge 
27930819cb8Sragge 	if ((KDB_RS(KDB_SA) & MP_ERR) == 0)
280f5e89154Sragge 		return;
281dfba8166Smatt 	printf("%s: controller error, sa=0x%x\n", device_xname(sc->sc_dev),
28230819cb8Sragge 	    KDB_RS(KDB_SA));
283f5e89154Sragge 	/* What to do now??? */
284f5e89154Sragge }
285f5e89154Sragge 
286f5e89154Sragge /*
287f5e89154Sragge  * Interrupt routine.  Depending on the state of the controller,
288f5e89154Sragge  * continue initialisation, or acknowledge command and response
289f5e89154Sragge  * interrupts, and process responses.
290f5e89154Sragge  */
291f5e89154Sragge void
kdbintr(void * arg)29230819cb8Sragge kdbintr(void *arg)
293f5e89154Sragge {
29430819cb8Sragge 	struct kdb_softc *sc = arg;
295f5e89154Sragge 
29630819cb8Sragge 	if (KDB_RS(KDB_SA) & MP_ERR) {	/* ctlr fatal error */
297dfba8166Smatt 		kdbsaerror(sc->sc_dev, 1);
298f5e89154Sragge 		return;
299f5e89154Sragge 	}
30064c05143Sad 	KERNEL_LOCK(1, NULL);
301f5e89154Sragge 	mscp_intr(sc->sc_softc);
30264c05143Sad 	KERNEL_UNLOCK_ONE(NULL);
303f5e89154Sragge }
304f5e89154Sragge 
305f5e89154Sragge void
kdbctlrdone(device_t usc)30653485c0aSjoerg kdbctlrdone(device_t usc)
307f5e89154Sragge {
308f5e89154Sragge }
309