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