1 /* $NetBSD: mba.c,v 1.39 2010/12/14 23:38:30 matt 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/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: mba.c,v 1.39 2010/12/14 23:38:30 matt Exp $"); 42 43 #include <sys/param.h> 44 #include <sys/systm.h> 45 #include <sys/bus.h> 46 #include <sys/cpu.h> 47 #include <sys/device.h> 48 #include <sys/queue.h> 49 #include <sys/buf.h> 50 #include <sys/bufq.h> 51 #include <sys/proc.h> 52 53 #include <machine/scb.h> 54 #include <machine/nexus.h> 55 #include <machine/pte.h> 56 #include <machine/sid.h> 57 #include <machine/sid.h> 58 59 #include <vax/mba/mbareg.h> 60 #include <vax/mba/mbavar.h> 61 62 #include "locators.h" 63 64 const struct mbaunit mbaunit[] = { 65 {MBADT_RP04, "rp04", MB_RP}, 66 {MBADT_RP05, "rp05", MB_RP}, 67 {MBADT_RP06, "rp06", MB_RP}, 68 {MBADT_RP07, "rp07", MB_RP}, 69 {MBADT_RM02, "rm02", MB_RP}, 70 {MBADT_RM03, "rm03", MB_RP}, 71 {MBADT_RM05, "rm05", MB_RP}, 72 {MBADT_RM80, "rm80", MB_RP}, 73 {0, 0, 0} 74 }; 75 76 void mbaqueue(struct mba_device *); 77 78 static int mbamatch(device_t, cfdata_t, void *); 79 static void mbaattach(device_t, device_t, void *); 80 static void mbaintr(void *); 81 static int mbaprint(void *, const char *); 82 static void mbastart(struct mba_softc *); 83 84 CFATTACH_DECL_NEW(mba_cmi, sizeof(struct mba_softc), 85 mbamatch, mbaattach, NULL, NULL); 86 87 CFATTACH_DECL_NEW(mba_sbi, sizeof(struct mba_softc), 88 mbamatch, mbaattach, NULL, NULL); 89 90 #define MBA_WCSR(reg, val) \ 91 bus_space_write_4(sc->sc_iot, sc->sc_ioh, (reg), (val)) 92 #define MBA_RCSR(reg) \ 93 bus_space_read_4(sc->sc_iot, sc->sc_ioh, (reg)) 94 95 /* 96 * Look if this is a massbuss adapter. 97 */ 98 int 99 mbamatch(device_t parent, cfdata_t cf, void *aux) 100 { 101 struct sbi_attach_args * const sa = aux; 102 103 if (vax_cputype == VAX_750) { 104 if (cf->cf_loc[CMICF_TR] != CMICF_TR_DEFAULT && 105 cf->cf_loc[CMICF_TR] != sa->sa_nexnum) 106 return 0; 107 } else { 108 if (cf->cf_loc[SBICF_TR] != SBICF_TR_DEFAULT && 109 cf->cf_loc[SBICF_TR] != sa->sa_nexnum) 110 return 0; 111 } 112 113 if (sa->sa_type == NEX_MBA) 114 return 1; 115 116 return 0; 117 } 118 119 /* 120 * Attach the found massbuss adapter. Setup its interrupt vectors, 121 * reset it and go searching for drives on it. 122 */ 123 void 124 mbaattach(device_t parent, device_t self, void *aux) 125 { 126 struct mba_softc * const sc = device_private(self); 127 struct sbi_attach_args * const sa = aux; 128 struct mba_attach_args ma; 129 int i, j; 130 131 aprint_normal("\n"); 132 133 sc->sc_dev = self; 134 sc->sc_iot = sa->sa_iot; 135 sc->sc_ioh = sa->sa_ioh; 136 /* 137 * Set up interrupt vectors for this MBA. 138 */ 139 for (i = 0x14; i < 0x18; i++) 140 scb_vecalloc(vecnum(0, i, sa->sa_nexnum), 141 mbaintr, sc, SCB_ISTACK, &sc->sc_intrcnt); 142 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 143 device_xname(self), "intr"); 144 145 STAILQ_INIT(&sc->sc_xfers); 146 MBA_WCSR(MBA_CR, MBACR_INIT); /* Reset adapter */ 147 MBA_WCSR(MBA_CR, MBACR_IE); /* Enable interrupts */ 148 149 for (i = 0; i < MAXMBADEV; i++) { 150 sc->sc_state = SC_AUTOCONF; 151 if ((MBA_RCSR(MUREG(i, MU_DS)) & MBADS_DPR) == 0) 152 continue; 153 /* We have a drive, ok. */ 154 ma.ma_unit = i; 155 ma.ma_type = MBA_RCSR(MUREG(i, MU_DT)) & 0xf1ff; 156 for (j = 0; mbaunit[j].nr; j++) 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, &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 * const 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 = STAILQ_FIRST(&sc->sc_xfers); 189 bp = bufq_peek(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 sc->sc_state = SC_IDLE; 196 switch ((*md->md_finish)(md, itype, &attn)) { 197 198 case XFER_FINISH: 199 /* 200 * Transfer is finished. Take buffer of drive 201 * queue, and take drive of adapter queue. 202 * If more to transfer, start the adapter again 203 * by calling mbastart(). 204 */ 205 (void)bufq_get(md->md_q); 206 STAILQ_REMOVE_HEAD(&sc->sc_xfers, md_link); 207 if (bufq_peek(md->md_q) != NULL) { 208 STAILQ_INSERT_TAIL(&sc->sc_xfers, md, md_link); 209 } 210 211 bp->b_resid = 0; 212 biodone(bp); 213 if (!STAILQ_EMPTY(&sc->sc_xfers)) 214 mbastart(sc); 215 break; 216 217 case XFER_RESTART: 218 /* 219 * Something went wrong with the transfer. Try again. 220 */ 221 mbastart(sc); 222 break; 223 } 224 } 225 226 while (attn) { 227 anr = ffs(attn) - 1; 228 attn &= ~(1 << anr); 229 if (sc->sc_md[anr]->md_attn == 0) 230 panic("Should check for new MBA device %d", anr); 231 (*sc->sc_md[anr]->md_attn)(sc->sc_md[anr]); 232 } 233 } 234 235 int 236 mbaprint(void *aux, const char *mbaname) 237 { 238 struct mba_attach_args * const ma = aux; 239 240 if (mbaname) { 241 if (ma->ma_name) 242 aprint_normal("%s", ma->ma_name); 243 else 244 aprint_normal("device type %o", ma->ma_type); 245 aprint_normal(" at %s", mbaname); 246 } 247 aprint_normal(" drive %d", ma->ma_unit); 248 return (ma->ma_name ? UNCONF : UNSUPP); 249 } 250 251 /* 252 * A device calls mbaqueue() when it wants to get on the adapter queue. 253 * Called at splbio(). If the adapter is inactive, start it. 254 */ 255 void 256 mbaqueue(struct mba_device *md) 257 { 258 struct mba_softc * const sc = md->md_mba; 259 bool was_empty = STAILQ_EMPTY(&sc->sc_xfers); 260 261 STAILQ_INSERT_TAIL(&sc->sc_xfers, md, md_link); 262 263 if (was_empty) 264 mbastart(sc); 265 } 266 267 /* 268 * Start activity on (idling) adapter. Calls disk_reallymapin() to setup 269 * for DMA transfer, then the unit-specific start routine. 270 */ 271 void 272 mbastart(struct mba_softc *sc) 273 { 274 struct mba_device * const md = STAILQ_FIRST(&sc->sc_xfers); 275 struct buf *bp = bufq_peek(md->md_q); 276 277 disk_reallymapin(bp, (void *)(sc->sc_ioh + MAPREG(0)), 0, PG_V); 278 279 sc->sc_state = SC_ACTIVE; 280 MBA_WCSR(MBA_VAR, ((u_int)bp->b_data & VAX_PGOFSET)); 281 MBA_WCSR(MBA_BC, (~bp->b_bcount) + 1); 282 (*md->md_start)(md); /* machine-dependent start */ 283 } 284