1 /* $NetBSD: hp.c,v 1.36 2003/12/29 16:23:58 pk 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 * Simple device driver routine for massbuss disks. 35 * TODO: 36 * Fix support for Standard DEC BAD144 bad block forwarding. 37 * Be able to to handle soft/hard transfer errors. 38 * Handle non-data transfer interrupts. 39 * Autoconfiguration of disk drives 'on the fly'. 40 * Handle disk media changes. 41 * Dual-port operations should be supported. 42 */ 43 44 #include <sys/cdefs.h> 45 __KERNEL_RCSID(0, "$NetBSD: hp.c,v 1.36 2003/12/29 16:23:58 pk Exp $"); 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/device.h> 50 #include <sys/disklabel.h> 51 #include <sys/disk.h> 52 #include <sys/dkio.h> 53 #include <sys/buf.h> 54 #include <sys/stat.h> 55 #include <sys/ioccom.h> 56 #include <sys/fcntl.h> 57 #include <sys/syslog.h> 58 #include <sys/reboot.h> 59 #include <sys/conf.h> 60 #include <sys/event.h> 61 62 #include <machine/bus.h> 63 #include <machine/trap.h> 64 #include <machine/pte.h> 65 #include <machine/mtpr.h> 66 #include <machine/cpu.h> 67 68 #include <vax/mba/mbavar.h> 69 #include <vax/mba/mbareg.h> 70 #include <vax/mba/hpreg.h> 71 72 #include "ioconf.h" 73 #include "locators.h" 74 75 struct hp_softc { 76 struct device sc_dev; 77 struct disk sc_disk; 78 bus_space_tag_t sc_iot; 79 bus_space_handle_t sc_ioh; 80 struct mba_device sc_md; /* Common struct used by mbaqueue. */ 81 int sc_wlabel; /* Disklabel area is writable */ 82 }; 83 84 int hpmatch(struct device *, struct cfdata *, void *); 85 void hpattach(struct device *, struct device *, void *); 86 void hpstart(struct mba_device *); 87 int hpattn(struct mba_device *); 88 enum xfer_action hpfinish(struct mba_device *, int, int *); 89 90 CFATTACH_DECL(hp, sizeof(struct hp_softc), 91 hpmatch, hpattach, NULL, NULL); 92 93 dev_type_open(hpopen); 94 dev_type_close(hpclose); 95 dev_type_read(hpread); 96 dev_type_write(hpwrite); 97 dev_type_ioctl(hpioctl); 98 dev_type_strategy(hpstrategy); 99 dev_type_size(hpsize); 100 101 const struct bdevsw hp_bdevsw = { 102 hpopen, hpclose, hpstrategy, hpioctl, nulldump, hpsize, D_DISK 103 }; 104 105 const struct cdevsw hp_cdevsw = { 106 hpopen, hpclose, hpread, hpwrite, hpioctl, 107 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 108 }; 109 110 #define HP_WCSR(reg, val) \ 111 bus_space_write_4(sc->sc_iot, sc->sc_ioh, (reg), (val)) 112 #define HP_RCSR(reg) \ 113 bus_space_read_4(sc->sc_iot, sc->sc_ioh, (reg)) 114 115 116 /* 117 * Check if this is a disk drive; done by checking type from mbaattach. 118 */ 119 int 120 hpmatch(struct device *parent, struct cfdata *cf, void *aux) 121 { 122 struct mba_attach_args *ma = aux; 123 124 if (cf->cf_loc[MBACF_DRIVE] != MBACF_DRIVE_DEFAULT && 125 cf->cf_loc[MBACF_DRIVE] != ma->ma_unit) 126 return 0; 127 128 if (ma->ma_devtyp != MB_RP) 129 return 0; 130 131 return 1; 132 } 133 134 /* 135 * Disk drive found; fake a disklabel and try to read the real one. 136 * If the on-disk label can't be read; we lose. 137 */ 138 void 139 hpattach(struct device *parent, struct device *self, void *aux) 140 { 141 struct hp_softc *sc = (void *)self; 142 struct mba_softc *ms = (void *)parent; 143 struct disklabel *dl; 144 struct mba_attach_args *ma = aux; 145 const char *msg; 146 147 sc->sc_iot = ma->ma_iot; 148 sc->sc_ioh = ma->ma_ioh; 149 /* 150 * Init the common struct for both the adapter and its slaves. 151 */ 152 bufq_alloc(&sc->sc_md.md_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER); 153 sc->sc_md.md_softc = (void *)sc; /* Pointer to this softc */ 154 sc->sc_md.md_mba = (void *)parent; /* Pointer to parent softc */ 155 sc->sc_md.md_start = hpstart; /* Disk start routine */ 156 sc->sc_md.md_attn = hpattn; /* Disk attention routine */ 157 sc->sc_md.md_finish = hpfinish; /* Disk xfer finish routine */ 158 159 ms->sc_md[ma->ma_unit] = &sc->sc_md; /* Per-unit backpointer */ 160 161 /* 162 * Init and attach the disk structure. 163 */ 164 sc->sc_disk.dk_name = sc->sc_dev.dv_xname; 165 disk_attach(&sc->sc_disk); 166 167 /* 168 * Fake a disklabel to be able to read in the real label. 169 */ 170 dl = sc->sc_disk.dk_label; 171 172 dl->d_secsize = DEV_BSIZE; 173 dl->d_ntracks = 1; 174 dl->d_nsectors = 32; 175 dl->d_secpercyl = 32; 176 177 /* 178 * Read in label. 179 */ 180 if ((msg = readdisklabel(makedev(0, self->dv_unit * 8), hpstrategy, 181 dl, NULL)) != NULL) 182 printf(": %s", msg); 183 printf(": %s, size = %d sectors\n", dl->d_typename, dl->d_secperunit); 184 } 185 186 187 void 188 hpstrategy(struct buf *bp) 189 { 190 struct hp_softc *sc; 191 struct buf *gp; 192 int unit, s, err; 193 struct disklabel *lp; 194 195 unit = DISKUNIT(bp->b_dev); 196 sc = hp_cd.cd_devs[unit]; 197 lp = sc->sc_disk.dk_label; 198 199 err = bounds_check_with_label(&sc->sc_disk, bp, sc->sc_wlabel); 200 if (err <= 0) 201 goto done; 202 203 bp->b_rawblkno = 204 bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset; 205 bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl; 206 207 s = splbio(); 208 209 gp = BUFQ_PEEK(&sc->sc_md.md_q); 210 BUFQ_PUT(&sc->sc_md.md_q, bp); 211 if (gp == 0) 212 mbaqueue(&sc->sc_md); 213 214 splx(s); 215 return; 216 217 done: 218 bp->b_resid = bp->b_bcount; 219 biodone(bp); 220 } 221 222 /* 223 * Start transfer on given disk. Called from mbastart(). 224 */ 225 void 226 hpstart(struct mba_device *md) 227 { 228 struct hp_softc *sc = md->md_softc; 229 struct disklabel *lp = sc->sc_disk.dk_label; 230 struct buf *bp = BUFQ_PEEK(&md->md_q); 231 unsigned bn, cn, sn, tn; 232 233 /* 234 * Collect statistics. 235 */ 236 disk_busy(&sc->sc_disk); 237 sc->sc_disk.dk_seek++; 238 239 bn = bp->b_rawblkno; 240 if (bn) { 241 cn = bn / lp->d_secpercyl; 242 sn = bn % lp->d_secpercyl; 243 tn = sn / lp->d_nsectors; 244 sn = sn % lp->d_nsectors; 245 } else 246 cn = sn = tn = 0; 247 248 HP_WCSR(HP_DC, cn); 249 HP_WCSR(HP_DA, (tn << 8) | sn); 250 if (bp->b_flags & B_READ) 251 HP_WCSR(HP_CS1, HPCS_READ); 252 else 253 HP_WCSR(HP_CS1, HPCS_WRITE); 254 } 255 256 int 257 hpopen(dev_t dev, int flag, int fmt, struct proc *p) 258 { 259 struct hp_softc *sc; 260 int unit, part; 261 262 unit = DISKUNIT(dev); 263 if (unit >= hp_cd.cd_ndevs) 264 return ENXIO; 265 sc = hp_cd.cd_devs[unit]; 266 if (sc == 0) 267 return ENXIO; 268 269 part = DISKPART(dev); 270 271 if (part >= sc->sc_disk.dk_label->d_npartitions) 272 return ENXIO; 273 274 switch (fmt) { 275 case S_IFCHR: 276 sc->sc_disk.dk_copenmask |= (1 << part); 277 break; 278 279 case S_IFBLK: 280 sc->sc_disk.dk_bopenmask |= (1 << part); 281 break; 282 } 283 sc->sc_disk.dk_openmask = 284 sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask; 285 286 return 0; 287 } 288 289 int 290 hpclose(dev_t dev, int flag, int fmt, struct proc *p) 291 { 292 struct hp_softc *sc; 293 int unit, part; 294 295 unit = DISKUNIT(dev); 296 sc = hp_cd.cd_devs[unit]; 297 298 part = DISKPART(dev); 299 300 switch (fmt) { 301 case S_IFCHR: 302 sc->sc_disk.dk_copenmask &= ~(1 << part); 303 break; 304 305 case S_IFBLK: 306 sc->sc_disk.dk_bopenmask &= ~(1 << part); 307 break; 308 } 309 sc->sc_disk.dk_openmask = 310 sc->sc_disk.dk_copenmask | sc->sc_disk.dk_bopenmask; 311 312 return 0; 313 } 314 315 int 316 hpioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 317 { 318 struct hp_softc *sc = hp_cd.cd_devs[DISKUNIT(dev)]; 319 struct disklabel *lp = sc->sc_disk.dk_label; 320 int error; 321 322 switch (cmd) { 323 case DIOCGDINFO: 324 bcopy(lp, addr, sizeof (struct disklabel)); 325 return 0; 326 327 case DIOCGPART: 328 ((struct partinfo *)addr)->disklab = lp; 329 ((struct partinfo *)addr)->part = 330 &lp->d_partitions[DISKPART(dev)]; 331 break; 332 333 case DIOCSDINFO: 334 if ((flag & FWRITE) == 0) 335 return EBADF; 336 337 return setdisklabel(lp, (struct disklabel *)addr, 0, 0); 338 339 case DIOCWDINFO: 340 if ((flag & FWRITE) == 0) 341 error = EBADF; 342 else { 343 sc->sc_wlabel = 1; 344 error = writedisklabel(dev, hpstrategy, lp, 0); 345 sc->sc_wlabel = 0; 346 } 347 return error; 348 case DIOCWLABEL: 349 if ((flag & FWRITE) == 0) 350 return EBADF; 351 sc->sc_wlabel = 1; 352 break; 353 354 default: 355 return ENOTTY; 356 } 357 return 0; 358 } 359 360 /* 361 * Called when a transfer is finished. Check if transfer went OK, 362 * Return info about what-to-do-now. 363 */ 364 enum xfer_action 365 hpfinish(struct mba_device *md, int mbasr, int *attn) 366 { 367 struct hp_softc *sc = md->md_softc; 368 struct buf *bp = BUFQ_PEEK(&md->md_q); 369 int er1, er2, bc; 370 unsigned byte; 371 372 er1 = HP_RCSR(HP_ER1); 373 er2 = HP_RCSR(HP_ER2); 374 HP_WCSR(HP_ER1, 0); 375 HP_WCSR(HP_ER2, 0); 376 377 hper1: 378 switch (ffs(er1) - 1) { 379 case -1: 380 HP_WCSR(HP_ER1, 0); 381 goto hper2; 382 383 case HPER1_DCK: /* Corrected? data read. Just notice. */ 384 bc = bus_space_read_4(md->md_mba->sc_iot, 385 md->md_mba->sc_ioh, MBA_BC); 386 byte = ~(bc >> 16); 387 diskerr(bp, hp_cd.cd_name, "soft ecc", LOG_PRINTF, 388 btodb(bp->b_bcount - byte), sc->sc_disk.dk_label); 389 er1 &= ~(1<<HPER1_DCK); 390 break; 391 392 default: 393 printf("drive error :%s er1 %x er2 %x\n", 394 sc->sc_dev.dv_xname, er1, er2); 395 HP_WCSR(HP_ER1, 0); 396 HP_WCSR(HP_ER2, 0); 397 goto hper2; 398 } 399 goto hper1; 400 401 hper2: 402 mbasr &= ~(MBASR_DTBUSY|MBASR_DTCMP|MBASR_ATTN); 403 if (mbasr) 404 printf("massbuss error :%s %x\n", 405 sc->sc_dev.dv_xname, mbasr); 406 407 BUFQ_PEEK(&md->md_q)->b_resid = 0; 408 disk_unbusy(&sc->sc_disk, BUFQ_PEEK(&md->md_q)->b_bcount, 409 (bp->b_flags & B_READ)); 410 return XFER_FINISH; 411 } 412 413 /* 414 * Non-data transfer interrupt; like volume change. 415 */ 416 int 417 hpattn(struct mba_device *md) 418 { 419 struct hp_softc *sc = md->md_softc; 420 int er1, er2; 421 422 er1 = HP_RCSR(HP_ER1); 423 er2 = HP_RCSR(HP_ER2); 424 425 printf("%s: Attention! er1 %x er2 %x\n", 426 sc->sc_dev.dv_xname, er1, er2); 427 return 0; 428 } 429 430 431 int 432 hpsize(dev_t dev) 433 { 434 int size, unit = DISKUNIT(dev); 435 struct hp_softc *sc; 436 437 if (unit >= hp_cd.cd_ndevs || hp_cd.cd_devs[unit] == 0) 438 return -1; 439 440 sc = hp_cd.cd_devs[unit]; 441 size = sc->sc_disk.dk_label->d_partitions[DISKPART(dev)].p_size * 442 (sc->sc_disk.dk_label->d_secsize / DEV_BSIZE); 443 444 return size; 445 } 446 447 int 448 hpread(dev_t dev, struct uio *uio, int ioflag) 449 { 450 return (physio(hpstrategy, NULL, dev, B_READ, minphys, uio)); 451 } 452 453 int 454 hpwrite(dev_t dev, struct uio *uio, int ioflag) 455 { 456 return (physio(hpstrategy, NULL, dev, B_WRITE, minphys, uio)); 457 } 458