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