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