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