mba.c revision 1.7 1 /* $NetBSD: mba.c,v 1.7 1996/08/20 14:15:04 ragge Exp $ */
2 /*
3 * Copyright (c) 1994, 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 * Simple massbus drive routine.
35 * TODO:
36 * Autoconfig new devices 'on the fly'.
37 * More intelligent way to handle different interrupts.
38 */
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/queue.h>
44 #include <sys/buf.h>
45 #include <sys/proc.h>
46
47 #include <vm/vm.h>
48 #include <vm/vm_kern.h>
49
50 #include <machine/trap.h>
51 #include <machine/scb.h>
52 #include <machine/nexus.h>
53 #include <machine/pte.h>
54 #include <machine/pcb.h>
55 #include <machine/sid.h>
56
57 #include <vax/mba/mbareg.h>
58 #include <vax/mba/mbavar.h>
59
60 struct mbaunit mbaunit[] = {
61 {MBADT_RP04, "rp04", MB_RP},
62 {MBADT_RP05, "rp05", MB_RP},
63 {MBADT_RP06, "rp06", MB_RP},
64 {MBADT_RP07, "rp07", MB_RP},
65 {MBADT_RM02, "rm02", MB_RP},
66 {MBADT_RM03, "rm03", MB_RP},
67 {MBADT_RM05, "rm05", MB_RP},
68 {MBADT_RM80, "rm80", MB_RP},
69 {0, 0, 0}
70 };
71
72 int mbamatch __P((struct device *, void *, void *));
73 void mbaattach __P((struct device *, struct device *, void *));
74 void mbaintr __P((int));
75 int mbaprint __P((void *, char *));
76 void mbaqueue __P((struct mba_device *));
77 void mbastart __P((struct mba_softc *));
78 void mbamapregs __P((struct mba_softc *));
79
80 struct cfdriver mba_cd = {
81 NULL, "mba", DV_DULL
82 };
83
84 struct cfattach mba_cmi_ca = {
85 sizeof(struct mba_softc), mbamatch, mbaattach
86 };
87
88 struct cfattach mba_sbi_ca = {
89 sizeof(struct mba_softc), mbamatch, mbaattach
90 };
91
92 /*
93 * Look if this is a massbuss adapter.
94 */
95 int
96 mbamatch(parent, match, aux)
97 struct device *parent;
98 void *match, *aux;
99 {
100 struct sbi_attach_args *sa = (struct sbi_attach_args *)aux;
101 struct cfdata *cf = match;
102
103 if ((cf->cf_loc[0] != sa->nexnum) && (cf->cf_loc[0] > -1 ))
104 return 0;
105
106 if (sa->type == NEX_MBA)
107 return 1;
108
109 return 0;
110 }
111
112 /*
113 * Attach the found massbuss adapter. Setup its interrupt vectors,
114 * reset it and go searching for drives on it.
115 */
116 void
117 mbaattach(parent, self, aux)
118 struct device *parent, *self;
119 void *aux;
120 {
121 struct mba_softc *sc = (void *)self;
122 struct sbi_attach_args *sa = (struct sbi_attach_args *)aux;
123 volatile struct mba_regs *mbar = (struct mba_regs *)sa->nexaddr;
124 struct mba_attach_args ma;
125 extern struct ivec_dsp idsptch;
126 int i, j;
127
128 printf("\n");
129 /*
130 * Set up interrupt vectors for this MBA.
131 */
132 bcopy(&idsptch, &sc->sc_dsp, sizeof(struct ivec_dsp));
133 scb->scb_nexvec[0][sa->nexnum] = scb->scb_nexvec[1][sa->nexnum] =
134 scb->scb_nexvec[2][sa->nexnum] = scb->scb_nexvec[3][sa->nexnum] =
135 &sc->sc_dsp;
136 sc->sc_dsp.pushlarg = sc->sc_dev.dv_unit;
137 sc->sc_dsp.hoppaddr = mbaintr;
138
139 sc->sc_physnr = sa->nexnum - 8; /* MBA's have TR between 8 - 11... */
140 #ifdef VAX750
141 if (vax_cputype == VAX_750)
142 sc->sc_physnr += 4; /* ...but not on 11/750 */
143 #endif
144 sc->sc_first = 0;
145 sc->sc_last = (void *)&sc->sc_first;
146 sc->sc_mbareg = (struct mba_regs *)mbar;
147 mbar->mba_cr = MBACR_INIT; /* Reset adapter */
148 mbar->mba_cr = MBACR_IE; /* Enable interrupts */
149
150 for (i = 0; i < MAXMBADEV; i++) {
151 sc->sc_state = SC_AUTOCONF;
152 if ((mbar->mba_md[i].md_ds & MBADS_DPR) == 0)
153 continue;
154 /* We have a drive, ok. */
155 ma.unit = i;
156 ma.type = mbar->mba_md[i].md_dt & 0777;
157 j = 0;
158 while (mbaunit[j++].nr)
159 if (mbaunit[j].nr == ma.type)
160 break;
161 ma.devtyp = mbaunit[j].devtyp;
162 ma.name = mbaunit[j].name;
163 config_found(&sc->sc_dev, (void *)&ma, mbaprint);
164 }
165 }
166
167 /*
168 * We got an interrupt. Check type of interrupt and call the specific
169 * device interrupt handling routine.
170 */
171 void
172 mbaintr(mba)
173 int mba;
174 {
175 struct mba_softc *sc = mba_cd.cd_devs[mba];
176 volatile struct mba_regs *mr = sc->sc_mbareg;
177 struct mba_device *md;
178 struct buf *bp;
179 int itype, attn, anr;
180
181 itype = mr->mba_sr;
182 mr->mba_sr = itype; /* Write back to clear bits */
183
184 attn = mr->mba_md[0].md_as & 0xff;
185 mr->mba_md[0].md_as = attn;
186
187 if (sc->sc_state == SC_AUTOCONF)
188 return; /* During autoconfig */
189
190 md = sc->sc_first;
191 bp = md->md_q.b_actf;
192 /*
193 * A data-transfer interrupt. Current operation is finished,
194 * call that device's finish routine to see what to do next.
195 */
196 if (sc->sc_state == SC_ACTIVE) {
197
198 sc->sc_state = SC_IDLE;
199 switch ((*md->md_finish)(md, itype, &attn)) {
200
201 case XFER_FINISH:
202 /*
203 * Transfer is finished. Take buffer of drive
204 * queue, and take drive of adapter queue.
205 * If more to transfer, start the adapter again
206 * by calling mbastart().
207 */
208 md->md_q.b_actf = bp->b_actf;
209 sc->sc_first = md->md_back;
210 md->md_back = 0;
211 if (sc->sc_first == 0)
212 sc->sc_last = (void *)&sc->sc_first;
213
214 if (md->md_q.b_actf) {
215 sc->sc_last->md_back = md;
216 sc->sc_last = md;
217 }
218
219 bp->b_resid = 0;
220 biodone(bp);
221 if (sc->sc_first)
222 mbastart(sc);
223 break;
224
225 case XFER_RESTART:
226 /*
227 * Something went wrong with the transfer. Try again.
228 */
229 mbastart(sc);
230 break;
231 }
232 }
233
234 while (attn) {
235 anr = ffs(attn) - 1;
236 attn &= ~(1 << anr);
237 if (sc->sc_md[anr]->md_attn == 0)
238 panic("Should check for new MBA device %d", anr);
239 (*sc->sc_md[anr]->md_attn)(sc->sc_md[anr]);
240 }
241 }
242
243 int
244 mbaprint(aux, mbaname)
245 void *aux;
246 char *mbaname;
247 {
248 struct mba_attach_args *ma = aux;
249
250 if (mbaname) {
251 if (ma->name)
252 printf("%s", ma->name);
253 else
254 printf("device type %o", ma->type);
255 printf(" at %s", mbaname);
256 }
257 printf(" drive %d", ma->unit);
258 return (ma->name ? UNCONF : UNSUPP);
259 }
260
261 /*
262 * A device calls mbaqueue() when it wants to get on the adapter queue.
263 * Called at splbio(). If the adapter is inactive, start it.
264 */
265 void
266 mbaqueue(md)
267 struct mba_device *md;
268 {
269 struct mba_softc *sc = md->md_mba;
270 int i = (int)sc->sc_first;
271
272 sc->sc_last->md_back = md;
273 sc->sc_last = md;
274
275 if (i == 0)
276 mbastart(sc);
277 }
278
279 /*
280 * Start activity on (idling) adapter. Calls mbamapregs() to setup
281 * for dma transfer, then the unit-specific start routine.
282 */
283 void
284 mbastart(sc)
285 struct mba_softc *sc;
286 {
287 struct mba_device *md = sc->sc_first;
288 volatile struct mba_regs *mr = sc->sc_mbareg;
289 struct buf *bp = md->md_q.b_actf;
290
291 disk_reallymapin(md->md_q.b_actf, sc->sc_mbareg->mba_map, 0, PG_V);
292
293 sc->sc_state = SC_ACTIVE;
294 mr->mba_var = ((u_int)bp->b_un.b_addr & PGOFSET);
295 mr->mba_bc = (~bp->b_bcount) + 1;
296 (*md->md_start)(md); /* machine-dependent start */
297 }
298