Home | History | Annotate | Line # | Download | only in mscp
mscp_disk.c revision 1.55
      1 /*	$NetBSD: mscp_disk.c,v 1.55 2007/07/21 19:51:48 ad Exp $	*/
      2 /*
      3  * Copyright (c) 1988 Regents of the University of California.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Chris Torek.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	@(#)uda.c	7.32 (Berkeley) 2/13/91
     34  */
     35 
     36 /*
     37  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
     38  *
     39  * This code is derived from software contributed to Berkeley by
     40  * Chris Torek.
     41  *
     42  * Redistribution and use in source and binary forms, with or without
     43  * modification, are permitted provided that the following conditions
     44  * are met:
     45  * 1. Redistributions of source code must retain the above copyright
     46  *    notice, this list of conditions and the following disclaimer.
     47  * 2. Redistributions in binary form must reproduce the above copyright
     48  *    notice, this list of conditions and the following disclaimer in the
     49  *    documentation and/or other materials provided with the distribution.
     50  * 3. All advertising materials mentioning features or use of this software
     51  *    must display the following acknowledgement:
     52  *	This product includes software developed by the University of
     53  *	California, Berkeley and its contributors.
     54  * 4. Neither the name of the University nor the names of its contributors
     55  *    may be used to endorse or promote products derived from this software
     56  *    without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     59  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     60  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     61  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     62  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     63  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     64  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     66  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     67  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     68  * SUCH DAMAGE.
     69  *
     70  *	@(#)uda.c	7.32 (Berkeley) 2/13/91
     71  */
     72 
     73 /*
     74  * RA disk device driver
     75  * RX MSCP floppy disk device driver
     76  */
     77 
     78 /*
     79  * TODO
     80  *	write bad block forwarding code
     81  */
     82 
     83 #include <sys/cdefs.h>
     84 __KERNEL_RCSID(0, "$NetBSD: mscp_disk.c,v 1.55 2007/07/21 19:51:48 ad Exp $");
     85 
     86 #include <sys/param.h>
     87 #include <sys/buf.h>
     88 #include <sys/bufq.h>
     89 #include <sys/device.h>
     90 #include <sys/disk.h>
     91 #include <sys/disklabel.h>
     92 #include <sys/ioctl.h>
     93 #include <sys/stat.h>
     94 #include <sys/fcntl.h>
     95 #include <sys/reboot.h>
     96 #include <sys/proc.h>
     97 #include <sys/systm.h>
     98 #include <sys/conf.h>
     99 
    100 #include <ufs/ufs/dinode.h>
    101 #include <ufs/ffs/fs.h>
    102 
    103 #include <machine/bus.h>
    104 #include <machine/cpu.h>
    105 
    106 #include <dev/mscp/mscp.h>
    107 #include <dev/mscp/mscpreg.h>
    108 #include <dev/mscp/mscpvar.h>
    109 
    110 #include "locators.h"
    111 #include "ioconf.h"
    112 #include "ra.h"
    113 
    114 /*
    115  * Drive status, per drive
    116  */
    117 struct ra_softc {
    118 	struct	device ra_dev;	/* Autoconf struct */
    119 	struct	disk ra_disk;
    120 	int	ra_state;	/* open/closed state */
    121 	u_long	ra_mediaid;	/* media id */
    122 	int	ra_hwunit;	/* Hardware unit number */
    123 	int	ra_havelabel;	/* true if we have a label */
    124 	int	ra_wlabel;	/* label sector is currently writable */
    125 };
    126 
    127 #define rx_softc ra_softc
    128 
    129 void	rxattach(struct device *, struct device *, void *);
    130 int	rx_putonline(struct rx_softc *);
    131 void	rrmakelabel(struct disklabel *, long);
    132 
    133 #if NRA
    134 
    135 int	ramatch(struct device *, struct cfdata *, void *);
    136 void	raattach(struct device *, struct device *, void *);
    137 int	ra_putonline(struct ra_softc *);
    138 
    139 CFATTACH_DECL(ra, sizeof(struct ra_softc),
    140     ramatch, rxattach, NULL, NULL);
    141 
    142 dev_type_open(raopen);
    143 dev_type_close(raclose);
    144 dev_type_read(raread);
    145 dev_type_write(rawrite);
    146 dev_type_ioctl(raioctl);
    147 dev_type_strategy(rastrategy);
    148 dev_type_dump(radump);
    149 dev_type_size(rasize);
    150 
    151 const struct bdevsw ra_bdevsw = {
    152 	raopen, raclose, rastrategy, raioctl, radump, rasize, D_DISK
    153 };
    154 
    155 const struct cdevsw ra_cdevsw = {
    156 	raopen, raclose, raread, rawrite, raioctl,
    157 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    158 };
    159 
    160 static struct dkdriver radkdriver = {
    161 	rastrategy, minphys
    162 };
    163 
    164 /*
    165  * More driver definitions, for generic MSCP code.
    166  */
    167 
    168 int
    169 ramatch(parent, cf, aux)
    170 	struct	device *parent;
    171 	struct	cfdata *cf;
    172 	void	*aux;
    173 {
    174 	struct	drive_attach_args *da = aux;
    175 	struct	mscp *mp = da->da_mp;
    176 
    177 	if ((da->da_typ & MSCPBUS_DISK) == 0)
    178 		return 0;
    179 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
    180 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
    181 		return 0;
    182 	/*
    183 	 * Check if this disk is a floppy; then don't configure it.
    184 	 * Seems to be a safe way to test it per Chris Torek.
    185 	 */
    186 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
    187 		return 0;
    188 	return 1;
    189 }
    190 
    191 /*
    192  * (Try to) put the drive online. This is done the first time the
    193  * drive is opened, or if it har fallen offline.
    194  */
    195 int
    196 ra_putonline(ra)
    197 	struct ra_softc *ra;
    198 {
    199 	struct	disklabel *dl;
    200 	const char *msg;
    201 	int maj;
    202 
    203 	if (rx_putonline(ra) != MSCP_DONE)
    204 		return MSCP_FAILED;
    205 
    206 	dl = ra->ra_disk.dk_label;
    207 
    208 	ra->ra_state = DK_RDLABEL;
    209 	printf("%s", ra->ra_dev.dv_xname);
    210 	maj = cdevsw_lookup_major(&ra_cdevsw);
    211 	if ((msg = readdisklabel(MAKEDISKDEV(maj, device_unit(&ra->ra_dev),
    212 	    RAW_PART), rastrategy, dl, NULL)) != NULL)
    213 		printf(": %s", msg);
    214 	else {
    215 		ra->ra_havelabel = 1;
    216 		ra->ra_state = DK_OPEN;
    217 	}
    218 
    219 	printf(": size %d sectors\n", dl->d_secperunit);
    220 
    221 	return MSCP_DONE;
    222 }
    223 
    224 /*
    225  * Open a drive.
    226  */
    227 /*ARGSUSED*/
    228 int
    229 raopen(dev, flag, fmt, l)
    230 	dev_t dev;
    231 	int flag, fmt;
    232 	struct	lwp *l;
    233 {
    234 	struct ra_softc *ra;
    235 	int error, part, unit, mask;
    236 	/*
    237 	 * Make sure this is a reasonable open request.
    238 	 */
    239 	unit = DISKUNIT(dev);
    240 	if (unit >= ra_cd.cd_ndevs)
    241 		return ENXIO;
    242 	ra = ra_cd.cd_devs[unit];
    243 	if (ra == 0)
    244 		return ENXIO;
    245 
    246 	part = DISKPART(dev);
    247 
    248 	mutex_enter(&ra->ra_disk.dk_openlock);
    249 
    250 	/*
    251 	 * If there are wedges, and this is not RAW_PART, then we
    252 	 * need to fail.
    253 	 */
    254 	if (ra->ra_disk.dk_nwedges != 0 && part != RAW_PART) {
    255 		error = EBUSY;
    256 		goto bad1;
    257 	}
    258 
    259 	/*
    260 	 * If this is the first open; we must first try to put
    261 	 * the disk online (and read the label).
    262 	 */
    263 	if (ra->ra_state == DK_CLOSED) {
    264 		if (ra_putonline(ra) == MSCP_FAILED) {
    265 			error = ENXIO;
    266 			goto bad1;
    267 		}
    268 	}
    269 
    270 	/* If the disk has no label; allow writing everywhere */
    271 	if (ra->ra_havelabel == 0)
    272 		ra->ra_wlabel = 1;
    273 
    274 	if (part >= ra->ra_disk.dk_label->d_npartitions) {
    275 		error = ENXIO;
    276 		goto bad1;
    277 	}
    278 
    279 	/*
    280 	 * Wait for the state to settle
    281 	 */
    282 #if notyet
    283 	while (ra->ra_state != DK_OPEN)
    284 		if ((error = tsleep((void *)ra, (PZERO + 1) | PCATCH,
    285 		    devopn, 0))) {
    286 			splx(s);
    287 			return (error);
    288 		}
    289 #endif
    290 
    291 	mask = 1 << part;
    292 
    293 	switch (fmt) {
    294 	case S_IFCHR:
    295 		ra->ra_disk.dk_copenmask |= mask;
    296 		break;
    297 	case S_IFBLK:
    298 		ra->ra_disk.dk_bopenmask |= mask;
    299 		break;
    300 	}
    301 	ra->ra_disk.dk_openmask |= mask;
    302 	error = 0;
    303  bad1:
    304 	mutex_exit(&ra->ra_disk.dk_openlock);
    305 	return (error);
    306 }
    307 
    308 /* ARGSUSED */
    309 int
    310 raclose(dev, flags, fmt, l)
    311 	dev_t dev;
    312 	int flags, fmt;
    313 	struct	lwp *l;
    314 {
    315 	int unit = DISKUNIT(dev);
    316 	struct ra_softc *ra = ra_cd.cd_devs[unit];
    317 	int mask = (1 << DISKPART(dev));
    318 
    319 	mutex_enter(&ra->ra_disk.dk_openlock);
    320 
    321 	switch (fmt) {
    322 	case S_IFCHR:
    323 		ra->ra_disk.dk_copenmask &= ~mask;
    324 		break;
    325 	case S_IFBLK:
    326 		ra->ra_disk.dk_bopenmask &= ~mask;
    327 		break;
    328 	}
    329 	ra->ra_disk.dk_openmask =
    330 	    ra->ra_disk.dk_copenmask | ra->ra_disk.dk_bopenmask;
    331 
    332 	/*
    333 	 * Should wait for I/O to complete on this partition even if
    334 	 * others are open, but wait for work on blkflush().
    335 	 */
    336 #if notyet
    337 	if (ra->ra_openpart == 0) {
    338 		s = spluba();
    339 		while (BUFQ_PEEK(udautab[unit]) != NULL)
    340 			(void) tsleep(&udautab[unit], PZERO - 1,
    341 			    "raclose", 0);
    342 		splx(s);
    343 		ra->ra_state = CLOSED;
    344 		ra->ra_wlabel = 0;
    345 	}
    346 #endif
    347 	mutex_exit(&ra->ra_disk.dk_openlock);
    348 	return (0);
    349 }
    350 
    351 /*
    352  * Queue a transfer request, and if possible, hand it to the controller.
    353  */
    354 void
    355 rastrategy(bp)
    356 	struct buf *bp;
    357 {
    358 	int unit;
    359 	struct ra_softc *ra;
    360 	int b;
    361 
    362 	/*
    363 	 * Make sure this is a reasonable drive to use.
    364 	 */
    365 	unit = DISKUNIT(bp->b_dev);
    366 	if (unit > ra_cd.cd_ndevs || (ra = ra_cd.cd_devs[unit]) == NULL) {
    367 		bp->b_error = ENXIO;
    368 		bp->b_flags |= B_ERROR;
    369 		goto done;
    370 	}
    371 	/*
    372 	 * If drive is open `raw' or reading label, let it at it.
    373 	 */
    374 	if (ra->ra_state == DK_RDLABEL) {
    375 	        /* Make some statistics... /bqt */
    376 	        b = splbio();
    377 	        disk_busy(&ra->ra_disk);
    378 		splx(b);
    379 		mscp_strategy(bp, device_parent(&ra->ra_dev));
    380 		return;
    381 	}
    382 
    383 	/* If disk is not online, try to put it online */
    384 	if (ra->ra_state == DK_CLOSED)
    385 		if (ra_putonline(ra) == MSCP_FAILED) {
    386 			bp->b_flags |= B_ERROR;
    387 			bp->b_error = EIO;
    388 			goto done;
    389 		}
    390 
    391 	/*
    392 	 * Determine the size of the transfer, and make sure it is
    393 	 * within the boundaries of the partition.
    394 	 */
    395 	if (bounds_check_with_label(&ra->ra_disk, bp, ra->ra_wlabel) <= 0)
    396 		goto done;
    397 
    398 	/* Make some statistics... /bqt */
    399 	b = splbio();
    400 	disk_busy(&ra->ra_disk);
    401 	splx(b);
    402 	mscp_strategy(bp, device_parent(&ra->ra_dev));
    403 	return;
    404 
    405 done:
    406 	biodone(bp);
    407 }
    408 
    409 int
    410 raread(dev, uio, flags)
    411 	dev_t dev;
    412 	struct uio *uio;
    413 	int flags;
    414 {
    415 
    416 	return (physio(rastrategy, NULL, dev, B_READ, minphys, uio));
    417 }
    418 
    419 int
    420 rawrite(dev, uio, flags)
    421 	dev_t dev;
    422 	struct uio *uio;
    423 	int flags;
    424 {
    425 
    426 	return (physio(rastrategy, NULL, dev, B_WRITE, minphys, uio));
    427 }
    428 
    429 /*
    430  * I/O controls.
    431  */
    432 int
    433 raioctl(dev, cmd, data, flag, l)
    434 	dev_t dev;
    435 	u_long cmd;
    436 	void *data;
    437 	int flag;
    438 	struct lwp *l;
    439 {
    440 	int unit = DISKUNIT(dev);
    441 	struct disklabel *lp, *tp;
    442 	struct ra_softc *ra = ra_cd.cd_devs[unit];
    443 	int error = 0;
    444 #ifdef __HAVE_OLD_DISKLABEL
    445 	struct disklabel newlabel;
    446 #endif
    447 
    448 	lp = ra->ra_disk.dk_label;
    449 
    450 	switch (cmd) {
    451 
    452 	case DIOCGDINFO:
    453 		bcopy(lp, data, sizeof (struct disklabel));
    454 		break;
    455 #ifdef __HAVE_OLD_DISKLABEL
    456 	case ODIOCGDINFO:
    457 		bcopy(lp, &newlabel, sizeof disklabel);
    458 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    459 			return ENOTTY;
    460 		bcopy(&newlabel, data, sizeof (struct olddisklabel));
    461 		break;
    462 #endif
    463 
    464 	case DIOCGPART:
    465 		((struct partinfo *)data)->disklab = lp;
    466 		((struct partinfo *)data)->part =
    467 		    &lp->d_partitions[DISKPART(dev)];
    468 		break;
    469 
    470 	case DIOCWDINFO:
    471 	case DIOCSDINFO:
    472 #ifdef __HAVE_OLD_DISKLABEL
    473 	case ODIOCWDINFO:
    474 	case ODIOCSDINFO:
    475 		if (cmd == ODIOCSDINFO || xfer == ODIOCWDINFO) {
    476 			memset(&newlabel, 0, sizeof newlabel);
    477 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    478 			tp = &newlabel;
    479 		} else
    480 #endif
    481 		tp = (struct disklabel *)data;
    482 
    483 		if ((flag & FWRITE) == 0)
    484 			error = EBADF;
    485 		else {
    486 			mutex_enter(&ra->ra_disk.dk_openlock);
    487 			error = setdisklabel(lp, tp, 0, 0);
    488 			if ((error == 0) && (cmd == DIOCWDINFO
    489 #ifdef __HAVE_OLD_DISKLABEL
    490 			    || cmd == ODIOCWDINFO
    491 #else
    492 			    )) {
    493 #endif
    494 				ra->ra_wlabel = 1;
    495 				error = writedisklabel(dev, rastrategy, lp,0);
    496 				ra->ra_wlabel = 0;
    497 			}
    498 			mutex_exit(&ra->ra_disk.dk_openlock);
    499 		}
    500 		break;
    501 
    502 	case DIOCWLABEL:
    503 		if ((flag & FWRITE) == 0)
    504 			error = EBADF;
    505 		else
    506 			ra->ra_wlabel = 1;
    507 		break;
    508 
    509 	case DIOCGDEFLABEL:
    510 #ifdef __HAVE_OLD_DISKLABEL
    511 	case ODIOCGDEFLABEL:
    512 		if (cmd == ODIOCGDEFLABEL)
    513 			tp = &newlabel;
    514 		else
    515 #else
    516 		tp = (struct disklabel *)data;
    517 #endif
    518 		bzero(tp, sizeof(struct disklabel));
    519 		tp->d_secsize = lp->d_secsize;
    520 		tp->d_nsectors = lp->d_nsectors;
    521 		tp->d_ntracks = lp->d_ntracks;
    522 		tp->d_ncylinders = lp->d_ncylinders;
    523 		tp->d_secpercyl = lp->d_secpercyl;
    524 		tp->d_secperunit = lp->d_secperunit;
    525 		tp->d_type = DTYPE_MSCP;
    526 		tp->d_rpm = 3600;
    527 		rrmakelabel(tp, ra->ra_mediaid);
    528 #ifdef __HAVE_OLD_DISKLABEL
    529 		if (cmd == ODIOCGDEFLABEL) {
    530 			if (tp->d_npartitions > OLDMAXPARTITIONS)
    531 				return ENOTTY;
    532 			memcpy(data, tp, sizeof (struct olddisklabel));
    533 		}
    534 #endif
    535 		break;
    536 
    537 	case DIOCAWEDGE:
    538 	    {
    539 	    	struct dkwedge_info *dkw = (void *) data;
    540 
    541 		if ((flag & FWRITE) == 0)
    542 			return (EBADF);
    543 
    544 		/* If the ioctl happens here, the parent is us. */
    545 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
    546 		return (dkwedge_add(dkw));
    547 	    }
    548 
    549 	case DIOCDWEDGE:
    550 	    {
    551 	    	struct dkwedge_info *dkw = (void *) data;
    552 
    553 		if ((flag & FWRITE) == 0)
    554 			return (EBADF);
    555 
    556 		/* If the ioctl happens here, the parent is us. */
    557 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
    558 		return (dkwedge_del(dkw));
    559 	    }
    560 
    561 	case DIOCLWEDGES:
    562 	    {
    563 	    	struct dkwedge_list *dkwl = (void *) data;
    564 
    565 		return (dkwedge_list(&ra->ra_disk, dkwl, l));
    566 	    }
    567 
    568 	default:
    569 		error = ENOTTY;
    570 		break;
    571 	}
    572 	return (error);
    573 }
    574 
    575 
    576 int
    577 radump(dev, blkno, va, size)
    578 	dev_t	dev;
    579 	daddr_t blkno;
    580 	void *va;
    581 	size_t	size;
    582 {
    583 	return ENXIO;
    584 }
    585 
    586 /*
    587  * Return the size of a partition, if known, or -1 if not.
    588  */
    589 int
    590 rasize(dev)
    591 	dev_t dev;
    592 {
    593 	int unit = DISKUNIT(dev);
    594 	struct ra_softc *ra;
    595 
    596 	if (unit >= ra_cd.cd_ndevs || ra_cd.cd_devs[unit] == 0)
    597 		return -1;
    598 
    599 	ra = ra_cd.cd_devs[unit];
    600 
    601 	if (ra->ra_state == DK_CLOSED)
    602 		if (ra_putonline(ra) == MSCP_FAILED)
    603 			return -1;
    604 
    605 	return ra->ra_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
    606 	    (ra->ra_disk.dk_label->d_secsize / DEV_BSIZE);
    607 }
    608 
    609 #endif /* NRA */
    610 
    611 #if NRX
    612 
    613 int	rxmatch(struct device *, struct cfdata *, void *);
    614 
    615 CFATTACH_DECL(rx, sizeof(struct rx_softc),
    616     rxmatch, rxattach, NULL, NULL);
    617 
    618 dev_type_open(rxopen);
    619 dev_type_read(rxread);
    620 dev_type_write(rxwrite);
    621 dev_type_ioctl(rxioctl);
    622 dev_type_strategy(rxstrategy);
    623 dev_type_dump(rxdump);
    624 dev_type_size(rxsize);
    625 
    626 const struct bdevsw rx_bdevsw = {
    627 	rxopen, nullclose, rxstrategy, rxioctl, rxdump, rxsize, D_DISK
    628 };
    629 
    630 const struct cdevsw rx_cdevsw = {
    631 	rxopen, nullclose, rxread, rxwrite, rxioctl,
    632 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    633 };
    634 
    635 static struct dkdriver rxdkdriver = {
    636 	rxstrategy, minphys
    637 };
    638 
    639 /*
    640  * More driver definitions, for generic MSCP code.
    641  */
    642 
    643 int
    644 rxmatch(parent, cf, aux)
    645 	struct	device *parent;
    646 	struct	cfdata *cf;
    647 	void	*aux;
    648 {
    649 	struct	drive_attach_args *da = aux;
    650 	struct	mscp *mp = da->da_mp;
    651 
    652 	if ((da->da_typ & MSCPBUS_DISK) == 0)
    653 		return 0;
    654 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
    655 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
    656 		return 0;
    657 	/*
    658 	 * Check if this disk is a floppy; then configure it.
    659 	 * Seems to be a safe way to test it per Chris Torek.
    660 	 */
    661 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
    662 		return 1;
    663 	return 0;
    664 }
    665 
    666 #endif /* NRX */
    667 
    668 /*
    669  * The attach routine only checks and prints drive type.
    670  * Bringing the disk online is done when the disk is accessed
    671  * the first time.
    672  */
    673 void
    674 rxattach(parent, self, aux)
    675 	struct	device *parent, *self;
    676 	void	*aux;
    677 {
    678 	struct	rx_softc *rx = device_private(self);
    679 	struct	drive_attach_args *da = aux;
    680 	struct	mscp *mp = da->da_mp;
    681 	struct	mscp_softc *mi = (void *)parent;
    682 	struct	disklabel *dl;
    683 
    684 	rx->ra_mediaid = mp->mscp_guse.guse_mediaid;
    685 	rx->ra_state = DK_CLOSED;
    686 	rx->ra_hwunit = mp->mscp_unit;
    687 	mi->mi_dp[mp->mscp_unit] = self;
    688 
    689 	rx->ra_disk.dk_name = rx->ra_dev.dv_xname;
    690 #if NRX
    691 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
    692 		rx->ra_disk.dk_driver = &rxdkdriver;
    693 #endif
    694 #if NRA
    695 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@')
    696 		rx->ra_disk.dk_driver = &radkdriver;
    697 #endif
    698 	disk_attach((struct disk *)&rx->ra_disk);
    699 
    700 	/* Fill in what we know. The actual size is gotten later */
    701 	dl = rx->ra_disk.dk_label;
    702 
    703 	dl->d_secsize = DEV_BSIZE;
    704 	dl->d_nsectors = mp->mscp_guse.guse_nspt;
    705 	dl->d_ntracks = mp->mscp_guse.guse_ngpc * mp->mscp_guse.guse_group;
    706 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
    707 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
    708 #ifdef DEBUG
    709 	printf("%s: nspt %d group %d ngpc %d rct %d nrpt %d nrct %d\n",
    710 	    self->dv_xname, mp->mscp_guse.guse_nspt, mp->mscp_guse.guse_group,
    711 	    mp->mscp_guse.guse_ngpc, mp->mscp_guse.guse_rctsize,
    712 	    mp->mscp_guse.guse_nrpt, mp->mscp_guse.guse_nrct);
    713 #endif
    714 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@') {
    715 		/*
    716 		 * XXX We should try to discover wedges here, but
    717 		 * XXX that would mean being able to do I/O.  Should
    718 		 * XXX use config_defer() here.
    719 		 */
    720 	}
    721 }
    722 
    723 /*
    724  * (Try to) put the drive online. This is done the first time the
    725  * drive is opened, or if it har fallen offline.
    726  */
    727 int
    728 rx_putonline(rx)
    729 	struct rx_softc *rx;
    730 {
    731 	struct	mscp *mp;
    732 	struct	mscp_softc *mi =
    733 	    (struct mscp_softc *)device_parent(&rx->ra_dev);
    734 	volatile int i;
    735 
    736 	rx->ra_state = DK_CLOSED;
    737 	mp = mscp_getcp(mi, MSCP_WAIT);
    738 	mp->mscp_opcode = M_OP_ONLINE;
    739 	mp->mscp_unit = rx->ra_hwunit;
    740 	mp->mscp_cmdref = 1;
    741 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
    742 
    743 	/* Poll away */
    744 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
    745 	if (tsleep(&rx->ra_state, PRIBIO, "rxonline", 100*100))
    746 		rx->ra_state = DK_CLOSED;
    747 
    748 	if (rx->ra_state == DK_CLOSED)
    749 		return MSCP_FAILED;
    750 
    751 	return MSCP_DONE;
    752 }
    753 
    754 #if NRX
    755 
    756 /*
    757  * Open a drive.
    758  */
    759 /*ARGSUSED*/
    760 int
    761 rxopen(dev, flag, fmt, l)
    762 	dev_t dev;
    763 	int flag, fmt;
    764 	struct	lwp *l;
    765 {
    766 	struct rx_softc *rx;
    767 	int unit;
    768 
    769 	/*
    770 	 * Make sure this is a reasonable open request.
    771 	 */
    772 	unit = DISKUNIT(dev);
    773 	if (unit >= rx_cd.cd_ndevs)
    774 		return ENXIO;
    775 	rx = rx_cd.cd_devs[unit];
    776 	if (rx == 0)
    777 		return ENXIO;
    778 
    779 	/*
    780 	 * If this is the first open; we must first try to put
    781 	 * the disk online (and read the label).
    782 	 */
    783 	if (rx->ra_state == DK_CLOSED)
    784 		if (rx_putonline(rx) == MSCP_FAILED)
    785 			return ENXIO;
    786 
    787 	return 0;
    788 }
    789 
    790 /*
    791  * Queue a transfer request, and if possible, hand it to the controller.
    792  *
    793  * This routine is broken into two so that the internal version
    794  * udastrat1() can be called by the (nonexistent, as yet) bad block
    795  * revectoring routine.
    796  */
    797 void
    798 rxstrategy(bp)
    799 	struct buf *bp;
    800 {
    801 	int unit;
    802 	struct rx_softc *rx;
    803 	int b;
    804 
    805 	/*
    806 	 * Make sure this is a reasonable drive to use.
    807 	 */
    808 	unit = DISKUNIT(bp->b_dev);
    809 	if (unit > rx_cd.cd_ndevs || (rx = rx_cd.cd_devs[unit]) == NULL) {
    810 		bp->b_error = ENXIO;
    811 		bp->b_flags |= B_ERROR;
    812 		goto done;
    813 	}
    814 
    815 	/* If disk is not online, try to put it online */
    816 	if (rx->ra_state == DK_CLOSED)
    817 		if (rx_putonline(rx) == MSCP_FAILED) {
    818 			bp->b_flags |= B_ERROR;
    819 			bp->b_error = EIO;
    820 			goto done;
    821 		}
    822 
    823 	/*
    824 	 * Determine the size of the transfer, and make sure it is
    825 	 * within the boundaries of the partition.
    826 	 */
    827 	if (bp->b_blkno >= rx->ra_disk.dk_label->d_secperunit) {
    828 		bp->b_resid = bp->b_bcount;
    829 		goto done;
    830 	}
    831 
    832 	/* Make some statistics... /bqt */
    833 	b = splbio();
    834 	disk_busy(&rx->ra_disk);
    835 	splx(b);
    836 	mscp_strategy(bp, device_parent(&rx->ra_dev));
    837 	return;
    838 
    839 done:
    840 	biodone(bp);
    841 }
    842 
    843 int
    844 rxread(dev, uio, flag)
    845 	dev_t dev;
    846 	struct uio *uio;
    847 	int flag;
    848 {
    849 
    850 	return (physio(rxstrategy, NULL, dev, B_READ, minphys, uio));
    851 }
    852 
    853 int
    854 rxwrite(dev, uio, flag)
    855 	dev_t dev;
    856 	struct uio *uio;
    857 	int flag;
    858 {
    859 
    860 	return (physio(rxstrategy, NULL, dev, B_WRITE, minphys, uio));
    861 }
    862 
    863 /*
    864  * I/O controls.
    865  */
    866 int
    867 rxioctl(dev, cmd, data, flag, l)
    868 	dev_t dev;
    869 	u_long cmd;
    870 	void *data;
    871 	int flag;
    872 	struct lwp *l;
    873 {
    874 	int unit = DISKUNIT(dev);
    875 	struct disklabel *lp;
    876 	struct rx_softc *rx = rx_cd.cd_devs[unit];
    877 	int error = 0;
    878 
    879 	lp = rx->ra_disk.dk_label;
    880 
    881 	switch (cmd) {
    882 
    883 	case DIOCGDINFO:
    884 		bcopy(lp, data, sizeof (struct disklabel));
    885 		break;
    886 
    887 	case DIOCGPART:
    888 		((struct partinfo *)data)->disklab = lp;
    889 		((struct partinfo *)data)->part =
    890 		    &lp->d_partitions[DISKPART(dev)];
    891 		break;
    892 
    893 
    894 	case DIOCWDINFO:
    895 	case DIOCSDINFO:
    896 	case DIOCWLABEL:
    897 		break;
    898 
    899 	default:
    900 		error = ENOTTY;
    901 		break;
    902 	}
    903 	return (error);
    904 }
    905 
    906 int
    907 rxdump(dev, blkno, va, size)
    908 	dev_t dev;
    909 	daddr_t blkno;
    910 	void *va;
    911 	size_t size;
    912 {
    913 
    914 	/* Not likely. */
    915 	return ENXIO;
    916 }
    917 
    918 int
    919 rxsize(dev)
    920 	dev_t dev;
    921 {
    922 
    923 	return -1;
    924 }
    925 
    926 #endif /* NRX */
    927 
    928 void	rrdgram(struct device *, struct mscp *, struct mscp_softc *);
    929 void	rriodone(struct device *, struct buf *);
    930 int	rronline(struct device *, struct mscp *);
    931 int	rrgotstatus(struct device *, struct mscp *);
    932 void	rrreplace(struct device *, struct mscp *);
    933 int	rrioerror(struct device *, struct mscp *, struct buf *);
    934 void	rrfillin(struct buf *, struct mscp *);
    935 void	rrbb(struct device *, struct mscp *, struct buf *);
    936 
    937 
    938 struct	mscp_device ra_device = {
    939 	rrdgram,
    940 	rriodone,
    941 	rronline,
    942 	rrgotstatus,
    943 	rrreplace,
    944 	rrioerror,
    945 	rrbb,
    946 	rrfillin,
    947 };
    948 
    949 /*
    950  * Handle an error datagram.
    951  * This can come from an unconfigured drive as well.
    952  */
    953 void
    954 rrdgram(usc, mp, mi)
    955 	struct device *usc;
    956 	struct mscp *mp;
    957 	struct mscp_softc *mi;
    958 {
    959 	if (mscp_decodeerror(usc == NULL?"unconf disk" : usc->dv_xname, mp, mi))
    960 		return;
    961 	/*
    962 	 * SDI status information bytes 10 and 11 are the microprocessor
    963 	 * error code and front panel code respectively.  These vary per
    964 	 * drive type and are printed purely for field service information.
    965 	 */
    966 	if (mp->mscp_format == M_FM_SDI)
    967 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
    968 			mp->mscp_erd.erd_sdistat[10],
    969 			mp->mscp_erd.erd_sdistat[11]);
    970 }
    971 
    972 void
    973 rriodone(usc, bp)
    974 	struct device *usc;
    975 	struct buf *bp;
    976 {
    977 	struct ra_softc *ra;
    978 	int unit;
    979 
    980 	/* We assume that this is a reasonable drive. ra_strategy should
    981 	   already have verified it. Thus, no checks here... /bqt */
    982 	unit = DISKUNIT(bp->b_dev);
    983 #if NRA
    984 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
    985 		ra = ra_cd.cd_devs[unit];
    986 	else
    987 #endif
    988 #if NRX
    989 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
    990 		ra = rx_cd.cd_devs[unit];
    991 	else
    992 #endif
    993 		panic("rriodone: unexpected major %d unit %d",
    994 		    major(bp->b_dev), unit);
    995 	disk_unbusy(&ra->ra_disk, bp->b_bcount, (bp->b_flags & B_READ));
    996 
    997 	biodone(bp);
    998 }
    999 
   1000 /*
   1001  * A drive came on line.  Check its type and size.  Return DONE if
   1002  * we think the drive is truly on line.	 In any case, awaken anyone
   1003  * sleeping on the drive on-line-ness.
   1004  */
   1005 int
   1006 rronline(usc, mp)
   1007 	struct device *usc;
   1008 	struct mscp *mp;
   1009 {
   1010 	struct rx_softc *rx = (struct rx_softc *)usc;
   1011 	struct disklabel *dl;
   1012 
   1013 	wakeup((void *)&rx->ra_state);
   1014 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
   1015 		printf("%s: attempt to bring on line failed: ", usc->dv_xname);
   1016 		mscp_printevent(mp);
   1017 		return (MSCP_FAILED);
   1018 	}
   1019 
   1020 	rx->ra_state = DK_OPEN;
   1021 
   1022 	dl = rx->ra_disk.dk_label;
   1023 	dl->d_secperunit = (daddr_t)mp->mscp_onle.onle_unitsize;
   1024 
   1025 	if (dl->d_secpercyl) {
   1026 		dl->d_ncylinders = dl->d_secperunit/dl->d_secpercyl;
   1027 		dl->d_type = DTYPE_MSCP;
   1028 		dl->d_rpm = 3600;
   1029 	} else {
   1030 		dl->d_type = DTYPE_FLOPPY;
   1031 		dl->d_rpm = 300;
   1032 	}
   1033 	rrmakelabel(dl, rx->ra_mediaid);
   1034 
   1035 	return (MSCP_DONE);
   1036 }
   1037 
   1038 void
   1039 rrmakelabel(dl, type)
   1040 	struct disklabel *dl;
   1041 	long type;
   1042 {
   1043 	int n, p = 0;
   1044 
   1045 	dl->d_bbsize = BBSIZE;
   1046 	dl->d_sbsize = SBLOCKSIZE;
   1047 
   1048 	/* Create the disk name for disklabel. Phew... */
   1049 	dl->d_typename[p++] = MSCP_MID_CHAR(2, type);
   1050 	dl->d_typename[p++] = MSCP_MID_CHAR(1, type);
   1051 	if (MSCP_MID_ECH(0, type))
   1052 		dl->d_typename[p++] = MSCP_MID_CHAR(0, type);
   1053 	n = MSCP_MID_NUM(type);
   1054 	if (n > 99) {
   1055 		dl->d_typename[p++] = '1';
   1056 		n -= 100;
   1057 	}
   1058 	if (n > 9) {
   1059 		dl->d_typename[p++] = (n / 10) + '0';
   1060 		n %= 10;
   1061 	}
   1062 	dl->d_typename[p++] = n + '0';
   1063 	dl->d_typename[p] = 0;
   1064 	dl->d_npartitions = MAXPARTITIONS;
   1065 	dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
   1066 	    dl->d_secperunit;
   1067 	dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
   1068 	dl->d_interleave = dl->d_headswitch = 1;
   1069 	dl->d_magic = dl->d_magic2 = DISKMAGIC;
   1070 	dl->d_checksum = dkcksum(dl);
   1071 }
   1072 
   1073 /*
   1074  * We got some (configured) unit's status.  Return DONE if it succeeded.
   1075  */
   1076 int
   1077 rrgotstatus(usc, mp)
   1078 	struct device *usc;
   1079 	struct mscp *mp;
   1080 {
   1081 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
   1082 		printf("%s: attempt to get status failed: ", usc->dv_xname);
   1083 		mscp_printevent(mp);
   1084 		return (MSCP_FAILED);
   1085 	}
   1086 	/* record for (future) bad block forwarding and whatever else */
   1087 #ifdef notyet
   1088 	uda_rasave(ui->ui_unit, mp, 1);
   1089 #endif
   1090 	return (MSCP_DONE);
   1091 }
   1092 
   1093 /*
   1094  * A replace operation finished.
   1095  */
   1096 /*ARGSUSED*/
   1097 void
   1098 rrreplace(usc, mp)
   1099 	struct device *usc;
   1100 	struct mscp *mp;
   1101 {
   1102 
   1103 	panic("udareplace");
   1104 }
   1105 
   1106 /*
   1107  * A transfer failed.  We get a chance to fix or restart it.
   1108  * Need to write the bad block forwaring code first....
   1109  */
   1110 /*ARGSUSED*/
   1111 int
   1112 rrioerror(usc, mp, bp)
   1113 	struct device *usc;
   1114 	struct mscp *mp;
   1115 	struct buf *bp;
   1116 {
   1117 	struct ra_softc *ra = (void *)usc;
   1118 	int code = mp->mscp_event;
   1119 
   1120 	switch (code & M_ST_MASK) {
   1121 	/* The unit has fallen offline. Try to figure out why. */
   1122 	case M_ST_OFFLINE:
   1123 		bp->b_flags |= B_ERROR;
   1124 		bp->b_error = EIO;
   1125 		ra->ra_state = DK_CLOSED;
   1126 		if (code & M_OFFLINE_UNMOUNTED)
   1127 			printf("%s: not mounted/spun down\n", usc->dv_xname);
   1128 		if (code & M_OFFLINE_DUPLICATE)
   1129 			printf("%s: duplicate unit number!!!\n", usc->dv_xname);
   1130 		return MSCP_DONE;
   1131 
   1132 	case M_ST_AVAILABLE:
   1133 		ra->ra_state = DK_CLOSED; /* Force another online */
   1134 		return MSCP_DONE;
   1135 
   1136 	default:
   1137 		printf("%s:", usc->dv_xname);
   1138 		break;
   1139 	}
   1140 	return (MSCP_FAILED);
   1141 }
   1142 
   1143 /*
   1144  * Fill in disk addresses in a mscp packet waiting for transfer.
   1145  */
   1146 void
   1147 rrfillin(bp, mp)
   1148 	struct buf *bp;
   1149 	struct mscp *mp;
   1150 {
   1151 	struct rx_softc *rx = 0; /* Wall */
   1152 	struct disklabel *lp;
   1153 	int unit = DISKUNIT(bp->b_dev);
   1154 	int part = DISKPART(bp->b_dev);
   1155 
   1156 #if NRA
   1157 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
   1158 		rx = ra_cd.cd_devs[unit];
   1159 #endif
   1160 #if NRX
   1161 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
   1162 		rx = rx_cd.cd_devs[unit];
   1163 #endif
   1164 	lp = rx->ra_disk.dk_label;
   1165 
   1166 	mp->mscp_seq.seq_lbn = lp->d_partitions[part].p_offset + bp->b_blkno;
   1167 	mp->mscp_unit = rx->ra_hwunit;
   1168 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
   1169 }
   1170 
   1171 /*
   1172  * A bad block related operation finished.
   1173  */
   1174 /*ARGSUSED*/
   1175 void
   1176 rrbb(usc, mp, bp)
   1177 	struct device *usc;
   1178 	struct mscp *mp;
   1179 	struct buf *bp;
   1180 {
   1181 
   1182 	panic("udabb");
   1183 }
   1184