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