mba.c revision 1.21 1 /* $NetBSD: mba.c,v 1.21 2000/06/04 18:04:39 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/bus.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 #include <machine/cpu.h>
57
58 #include <vax/mba/mbareg.h>
59 #include <vax/mba/mbavar.h>
60
61 #include "locators.h"
62
63 struct mbaunit mbaunit[] = {
64 {MBADT_RP04, "rp04", MB_RP},
65 {MBADT_RP05, "rp05", MB_RP},
66 {MBADT_RP06, "rp06", MB_RP},
67 {MBADT_RP07, "rp07", MB_RP},
68 {MBADT_RM02, "rm02", MB_RP},
69 {MBADT_RM03, "rm03", MB_RP},
70 {MBADT_RM05, "rm05", MB_RP},
71 {MBADT_RM80, "rm80", MB_RP},
72 {0, 0, 0}
73 };
74
75 int mbamatch(struct device *, struct cfdata *, void *);
76 void mbaattach(struct device *, struct device *, void *);
77 void mbaintr(void *);
78 int mbaprint(void *, const char *);
79 void mbaqueue(struct mba_device *);
80 void mbastart(struct mba_softc *);
81 void mbamapregs(struct mba_softc *);
82
83 struct cfattach mba_cmi_ca = {
84 sizeof(struct mba_softc), mbamatch, mbaattach
85 };
86
87 struct cfattach mba_sbi_ca = {
88 sizeof(struct mba_softc), mbamatch, mbaattach
89 };
90
91 #define MBA_WCSR(reg, val) \
92 bus_space_write_4(sc->sc_iot, sc->sc_ioh, (reg), (val))
93 #define MBA_RCSR(reg) \
94 bus_space_read_4(sc->sc_iot, sc->sc_ioh, (reg))
95
96 /*
97 * Look if this is a massbuss adapter.
98 */
99 int
100 mbamatch(struct device *parent, struct cfdata *cf, void *aux)
101 {
102 struct sbi_attach_args *sa = (struct sbi_attach_args *)aux;
103
104 if (vax_cputype == VAX_750) {
105 if (cf->cf_loc[CMICF_TR] != CMICF_TR_DEFAULT &&
106 cf->cf_loc[CMICF_TR] != sa->sa_nexnum)
107 return 0;
108 } else {
109 if (cf->cf_loc[SBICF_TR] != SBICF_TR_DEFAULT &&
110 cf->cf_loc[SBICF_TR] != sa->sa_nexnum)
111 return 0;
112 }
113
114 if (sa->sa_type == NEX_MBA)
115 return 1;
116
117 return 0;
118 }
119
120 /*
121 * Attach the found massbuss adapter. Setup its interrupt vectors,
122 * reset it and go searching for drives on it.
123 */
124 void
125 mbaattach(struct device *parent, struct device *self, void *aux)
126 {
127 struct mba_softc *sc = (void *)self;
128 struct sbi_attach_args *sa = (struct sbi_attach_args *)aux;
129 struct mba_attach_args ma;
130 int i, j;
131
132 printf("\n");
133 sc->sc_iot = sa->sa_iot;
134 sc->sc_ioh = sa->sa_ioh;
135 /*
136 * Set up interrupt vectors for this MBA.
137 */
138 for (i = 14; i < 18; i++)
139 scb_vecalloc(vecnum(0, i, sa->sa_nexnum),
140 mbaintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
141 evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
142
143 sc->sc_first = 0;
144 sc->sc_last = (void *)&sc->sc_first;
145 MBA_WCSR(MBA_CR, MBACR_INIT); /* Reset adapter */
146 MBA_WCSR(MBA_CR, MBACR_IE); /* Enable interrupts */
147
148 for (i = 0; i < MAXMBADEV; i++) {
149 sc->sc_state = SC_AUTOCONF;
150 if ((MBA_RCSR(MUREG(i, MU_DS)) & MBADS_DPR) == 0)
151 continue;
152 /* We have a drive, ok. */
153 ma.ma_unit = i;
154 ma.ma_type = MBA_RCSR(MUREG(i, MU_DT)) & 0777;
155 j = 0;
156 while (mbaunit[j++].nr)
157 if (mbaunit[j].nr == ma.ma_type)
158 break;
159 ma.ma_devtyp = mbaunit[j].devtyp;
160 ma.ma_name = mbaunit[j].name;
161 ma.ma_iot = sc->sc_iot;
162 ma.ma_ioh = sc->sc_ioh + MUREG(i, 0);
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(void *mba)
173 {
174 struct mba_softc *sc = mba;
175 struct mba_device *md;
176 struct buf *bp;
177 int itype, attn, anr;
178
179 itype = MBA_RCSR(MBA_SR);
180 MBA_WCSR(MBA_SR, itype);
181
182 attn = MBA_RCSR(MUREG(0, MU_AS)) & 0xff;
183 MBA_WCSR(MUREG(0, MU_AS), attn);
184
185 if (sc->sc_state == SC_AUTOCONF)
186 return; /* During autoconfig */
187
188 md = sc->sc_first;
189 bp = BUFQ_FIRST(&md->md_q);
190 /*
191 * A data-transfer interrupt. Current operation is finished,
192 * call that device's finish routine to see what to do next.
193 */
194 if (sc->sc_state == SC_ACTIVE) {
195
196 sc->sc_state = SC_IDLE;
197 switch ((*md->md_finish)(md, itype, &attn)) {
198
199 case XFER_FINISH:
200 /*
201 * Transfer is finished. Take buffer of drive
202 * queue, and take drive of adapter queue.
203 * If more to transfer, start the adapter again
204 * by calling mbastart().
205 */
206 BUFQ_REMOVE(&md->md_q, bp);
207 sc->sc_first = md->md_back;
208 md->md_back = 0;
209 if (sc->sc_first == 0)
210 sc->sc_last = (void *)&sc->sc_first;
211
212 if (BUFQ_FIRST(&md->md_q) != NULL) {
213 sc->sc_last->md_back = md;
214 sc->sc_last = md;
215 }
216
217 bp->b_resid = 0;
218 biodone(bp);
219 if (sc->sc_first)
220 mbastart(sc);
221 break;
222
223 case XFER_RESTART:
224 /*
225 * Something went wrong with the transfer. Try again.
226 */
227 mbastart(sc);
228 break;
229 }
230 }
231
232 while (attn) {
233 anr = ffs(attn) - 1;
234 attn &= ~(1 << anr);
235 if (sc->sc_md[anr]->md_attn == 0)
236 panic("Should check for new MBA device %d", anr);
237 (*sc->sc_md[anr]->md_attn)(sc->sc_md[anr]);
238 }
239 }
240
241 int
242 mbaprint(void *aux, const char *mbaname)
243 {
244 struct mba_attach_args *ma = aux;
245
246 if (mbaname) {
247 if (ma->ma_name)
248 printf("%s", ma->ma_name);
249 else
250 printf("device type %o", ma->ma_type);
251 printf(" at %s", mbaname);
252 }
253 printf(" drive %d", ma->ma_unit);
254 return (ma->ma_name ? UNCONF : UNSUPP);
255 }
256
257 /*
258 * A device calls mbaqueue() when it wants to get on the adapter queue.
259 * Called at splbio(). If the adapter is inactive, start it.
260 */
261 void
262 mbaqueue(struct mba_device *md)
263 {
264 struct mba_softc *sc = md->md_mba;
265 int i = (int)sc->sc_first;
266
267 sc->sc_last->md_back = md;
268 sc->sc_last = md;
269
270 if (i == 0)
271 mbastart(sc);
272 }
273
274 /*
275 * Start activity on (idling) adapter. Calls mbamapregs() to setup
276 * for dma transfer, then the unit-specific start routine.
277 */
278 void
279 mbastart(struct mba_softc *sc)
280 {
281 struct mba_device *md = sc->sc_first;
282 struct buf *bp = BUFQ_FIRST(&md->md_q);
283
284 disk_reallymapin(bp, (void *)(sc->sc_ioh + MAPREG(0)), 0, PG_V);
285
286 sc->sc_state = SC_ACTIVE;
287 MBA_WCSR(MBA_VAR, ((u_int)bp->b_data & VAX_PGOFSET));
288 MBA_WCSR(MBA_BC, (~bp->b_bcount) + 1);
289 (*md->md_start)(md); /* machine-dependent start */
290 }
291