Home | History | Annotate | Line # | Download | only in mscp
mscp_disk.c revision 1.52
      1 /*	$NetBSD: mscp_disk.c,v 1.52 2006/03/29 04:47:13 thorpej 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.52 2006/03/29 04:47:13 thorpej 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 	if ((error = lockmgr(&ra->ra_disk.dk_openlock, LK_EXCLUSIVE,
    249 			     NULL)) != 0)
    250 		return (error);
    251 
    252 	/*
    253 	 * If there are wedges, and this is not RAW_PART, then we
    254 	 * need to fail.
    255 	 */
    256 	if (ra->ra_disk.dk_nwedges != 0 && part != RAW_PART) {
    257 		error = EBUSY;
    258 		goto bad1;
    259 	}
    260 
    261 	/*
    262 	 * If this is the first open; we must first try to put
    263 	 * the disk online (and read the label).
    264 	 */
    265 	if (ra->ra_state == DK_CLOSED) {
    266 		if (ra_putonline(ra) == MSCP_FAILED) {
    267 			error = ENXIO;
    268 			goto bad1;
    269 		}
    270 	}
    271 
    272 	/* If the disk has no label; allow writing everywhere */
    273 	if (ra->ra_havelabel == 0)
    274 		ra->ra_wlabel = 1;
    275 
    276 	if (part >= ra->ra_disk.dk_label->d_npartitions) {
    277 		error = ENXIO;
    278 		goto bad1;
    279 	}
    280 
    281 	/*
    282 	 * Wait for the state to settle
    283 	 */
    284 #if notyet
    285 	while (ra->ra_state != DK_OPEN)
    286 		if ((error = tsleep((caddr_t)ra, (PZERO + 1) | PCATCH,
    287 		    devopn, 0))) {
    288 			splx(s);
    289 			return (error);
    290 		}
    291 #endif
    292 
    293 	mask = 1 << part;
    294 
    295 	switch (fmt) {
    296 	case S_IFCHR:
    297 		ra->ra_disk.dk_copenmask |= mask;
    298 		break;
    299 	case S_IFBLK:
    300 		ra->ra_disk.dk_bopenmask |= mask;
    301 		break;
    302 	}
    303 	ra->ra_disk.dk_openmask |= mask;
    304 	(void) lockmgr(&ra->ra_disk.dk_openlock, LK_RELEASE, NULL);
    305 	return 0;
    306 
    307  bad1:
    308 	(void) lockmgr(&ra->ra_disk.dk_openlock, LK_RELEASE, NULL);
    309 	return (error);
    310 }
    311 
    312 /* ARGSUSED */
    313 int
    314 raclose(dev, flags, fmt, l)
    315 	dev_t dev;
    316 	int flags, fmt;
    317 	struct	lwp *l;
    318 {
    319 	int unit = DISKUNIT(dev);
    320 	struct ra_softc *ra = ra_cd.cd_devs[unit];
    321 	int error, mask = (1 << DISKPART(dev));
    322 
    323 	if ((error = lockmgr(&ra->ra_disk.dk_openlock, LK_EXCLUSIVE,
    324 			     NULL)) != 0)
    325 		return (error);
    326 
    327 	switch (fmt) {
    328 	case S_IFCHR:
    329 		ra->ra_disk.dk_copenmask &= ~mask;
    330 		break;
    331 	case S_IFBLK:
    332 		ra->ra_disk.dk_bopenmask &= ~mask;
    333 		break;
    334 	}
    335 	ra->ra_disk.dk_openmask =
    336 	    ra->ra_disk.dk_copenmask | ra->ra_disk.dk_bopenmask;
    337 
    338 	/*
    339 	 * Should wait for I/O to complete on this partition even if
    340 	 * others are open, but wait for work on blkflush().
    341 	 */
    342 #if notyet
    343 	if (ra->ra_openpart == 0) {
    344 		s = spluba();
    345 		while (BUFQ_PEEK(udautab[unit]) != NULL)
    346 			(void) tsleep(&udautab[unit], PZERO - 1,
    347 			    "raclose", 0);
    348 		splx(s);
    349 		ra->ra_state = CLOSED;
    350 		ra->ra_wlabel = 0;
    351 	}
    352 #endif
    353 	(void) lockmgr(&ra->ra_disk.dk_openlock, LK_RELEASE, NULL);
    354 	return (0);
    355 }
    356 
    357 /*
    358  * Queue a transfer request, and if possible, hand it to the controller.
    359  */
    360 void
    361 rastrategy(bp)
    362 	struct buf *bp;
    363 {
    364 	int unit;
    365 	struct ra_softc *ra;
    366 	int b;
    367 
    368 	/*
    369 	 * Make sure this is a reasonable drive to use.
    370 	 */
    371 	unit = DISKUNIT(bp->b_dev);
    372 	if (unit > ra_cd.cd_ndevs || (ra = ra_cd.cd_devs[unit]) == NULL) {
    373 		bp->b_error = ENXIO;
    374 		bp->b_flags |= B_ERROR;
    375 		goto done;
    376 	}
    377 	/*
    378 	 * If drive is open `raw' or reading label, let it at it.
    379 	 */
    380 	if (ra->ra_state == DK_RDLABEL) {
    381 	        /* Make some statistics... /bqt */
    382 	        b = splbio();
    383 	        disk_busy(&ra->ra_disk);
    384 		splx(b);
    385 		mscp_strategy(bp, device_parent(&ra->ra_dev));
    386 		return;
    387 	}
    388 
    389 	/* If disk is not online, try to put it online */
    390 	if (ra->ra_state == DK_CLOSED)
    391 		if (ra_putonline(ra) == MSCP_FAILED) {
    392 			bp->b_flags |= B_ERROR;
    393 			bp->b_error = EIO;
    394 			goto done;
    395 		}
    396 
    397 	/*
    398 	 * Determine the size of the transfer, and make sure it is
    399 	 * within the boundaries of the partition.
    400 	 */
    401 	if (bounds_check_with_label(&ra->ra_disk, bp, ra->ra_wlabel) <= 0)
    402 		goto done;
    403 
    404 	/* Make some statistics... /bqt */
    405 	b = splbio();
    406 	disk_busy(&ra->ra_disk);
    407 	splx(b);
    408 	mscp_strategy(bp, device_parent(&ra->ra_dev));
    409 	return;
    410 
    411 done:
    412 	biodone(bp);
    413 }
    414 
    415 int
    416 raread(dev, uio, flags)
    417 	dev_t dev;
    418 	struct uio *uio;
    419 	int flags;
    420 {
    421 
    422 	return (physio(rastrategy, NULL, dev, B_READ, minphys, uio));
    423 }
    424 
    425 int
    426 rawrite(dev, uio, flags)
    427 	dev_t dev;
    428 	struct uio *uio;
    429 	int flags;
    430 {
    431 
    432 	return (physio(rastrategy, NULL, dev, B_WRITE, minphys, uio));
    433 }
    434 
    435 /*
    436  * I/O controls.
    437  */
    438 int
    439 raioctl(dev, cmd, data, flag, l)
    440 	dev_t dev;
    441 	u_long cmd;
    442 	caddr_t data;
    443 	int flag;
    444 	struct lwp *l;
    445 {
    446 	int unit = DISKUNIT(dev);
    447 	struct disklabel *lp, *tp;
    448 	struct ra_softc *ra = ra_cd.cd_devs[unit];
    449 	int error = 0;
    450 #ifdef __HAVE_OLD_DISKLABEL
    451 	struct disklabel newlabel;
    452 #endif
    453 
    454 	lp = ra->ra_disk.dk_label;
    455 
    456 	switch (cmd) {
    457 
    458 	case DIOCGDINFO:
    459 		bcopy(lp, data, sizeof (struct disklabel));
    460 		break;
    461 #ifdef __HAVE_OLD_DISKLABEL
    462 	case ODIOCGDINFO:
    463 		bcopy(lp, &newlabel, sizeof disklabel);
    464 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    465 			return ENOTTY;
    466 		bcopy(&newlabel, data, sizeof (struct olddisklabel));
    467 		break;
    468 #endif
    469 
    470 	case DIOCGPART:
    471 		((struct partinfo *)data)->disklab = lp;
    472 		((struct partinfo *)data)->part =
    473 		    &lp->d_partitions[DISKPART(dev)];
    474 		break;
    475 
    476 	case DIOCWDINFO:
    477 	case DIOCSDINFO:
    478 #ifdef __HAVE_OLD_DISKLABEL
    479 	case ODIOCWDINFO:
    480 	case ODIOCSDINFO:
    481 		if (cmd == ODIOCSDINFO || xfer == ODIOCWDINFO) {
    482 			memset(&newlabel, 0, sizeof newlabel);
    483 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    484 			tp = &newlabel;
    485 		} else
    486 #endif
    487 		tp = (struct disklabel *)data;
    488 
    489 		if ((flag & FWRITE) == 0)
    490 			error = EBADF;
    491 		else {
    492 			if ((error = lockmgr(&ra->ra_disk.dk_openlock,
    493 					     LK_EXCLUSIVE, NULL)) != 0)
    494 				break;
    495 			error = setdisklabel(lp, tp, 0, 0);
    496 			if ((error == 0) && (cmd == DIOCWDINFO
    497 #ifdef __HAVE_OLD_DISKLABEL
    498 			    || cmd == ODIOCWDINFO
    499 #else
    500 			    )) {
    501 #endif
    502 				ra->ra_wlabel = 1;
    503 				error = writedisklabel(dev, rastrategy, lp,0);
    504 				ra->ra_wlabel = 0;
    505 			}
    506 			(void) lockmgr(&ra->ra_disk.dk_openlock,
    507 				       LK_RELEASE, NULL);
    508 		}
    509 		break;
    510 
    511 	case DIOCWLABEL:
    512 		if ((flag & FWRITE) == 0)
    513 			error = EBADF;
    514 		else
    515 			ra->ra_wlabel = 1;
    516 		break;
    517 
    518 	case DIOCGDEFLABEL:
    519 #ifdef __HAVE_OLD_DISKLABEL
    520 	case ODIOCGDEFLABEL:
    521 		if (cmd == ODIOCGDEFLABEL)
    522 			tp = &newlabel;
    523 		else
    524 #else
    525 		tp = (struct disklabel *)data;
    526 #endif
    527 		bzero(tp, sizeof(struct disklabel));
    528 		tp->d_secsize = lp->d_secsize;
    529 		tp->d_nsectors = lp->d_nsectors;
    530 		tp->d_ntracks = lp->d_ntracks;
    531 		tp->d_ncylinders = lp->d_ncylinders;
    532 		tp->d_secpercyl = lp->d_secpercyl;
    533 		tp->d_secperunit = lp->d_secperunit;
    534 		tp->d_type = DTYPE_MSCP;
    535 		tp->d_rpm = 3600;
    536 		rrmakelabel(tp, ra->ra_mediaid);
    537 #ifdef __HAVE_OLD_DISKLABEL
    538 		if (cmd == ODIOCGDEFLABEL) {
    539 			if (tp->d_npartitions > OLDMAXPARTITIONS)
    540 				return ENOTTY;
    541 			memcpy(data, tp, sizeof (struct olddisklabel));
    542 		}
    543 #endif
    544 		break;
    545 
    546 	case DIOCAWEDGE:
    547 	    {
    548 	    	struct dkwedge_info *dkw = (void *) data;
    549 
    550 		if ((flag & FWRITE) == 0)
    551 			return (EBADF);
    552 
    553 		/* If the ioctl happens here, the parent is us. */
    554 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
    555 		return (dkwedge_add(dkw));
    556 	    }
    557 
    558 	case DIOCDWEDGE:
    559 	    {
    560 	    	struct dkwedge_info *dkw = (void *) data;
    561 
    562 		if ((flag & FWRITE) == 0)
    563 			return (EBADF);
    564 
    565 		/* If the ioctl happens here, the parent is us. */
    566 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
    567 		return (dkwedge_del(dkw));
    568 	    }
    569 
    570 	case DIOCLWEDGES:
    571 	    {
    572 	    	struct dkwedge_list *dkwl = (void *) data;
    573 
    574 		return (dkwedge_list(&ra->ra_disk, dkwl, l));
    575 	    }
    576 
    577 	default:
    578 		error = ENOTTY;
    579 		break;
    580 	}
    581 	return (error);
    582 }
    583 
    584 
    585 int
    586 radump(dev, blkno, va, size)
    587 	dev_t	dev;
    588 	daddr_t blkno;
    589 	caddr_t va;
    590 	size_t	size;
    591 {
    592 	return ENXIO;
    593 }
    594 
    595 /*
    596  * Return the size of a partition, if known, or -1 if not.
    597  */
    598 int
    599 rasize(dev)
    600 	dev_t dev;
    601 {
    602 	int unit = DISKUNIT(dev);
    603 	struct ra_softc *ra;
    604 
    605 	if (unit >= ra_cd.cd_ndevs || ra_cd.cd_devs[unit] == 0)
    606 		return -1;
    607 
    608 	ra = ra_cd.cd_devs[unit];
    609 
    610 	if (ra->ra_state == DK_CLOSED)
    611 		if (ra_putonline(ra) == MSCP_FAILED)
    612 			return -1;
    613 
    614 	return ra->ra_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
    615 	    (ra->ra_disk.dk_label->d_secsize / DEV_BSIZE);
    616 }
    617 
    618 #endif /* NRA */
    619 
    620 #if NRX
    621 
    622 int	rxmatch(struct device *, struct cfdata *, void *);
    623 
    624 CFATTACH_DECL(rx, sizeof(struct rx_softc),
    625     rxmatch, rxattach, NULL, NULL);
    626 
    627 dev_type_open(rxopen);
    628 dev_type_read(rxread);
    629 dev_type_write(rxwrite);
    630 dev_type_ioctl(rxioctl);
    631 dev_type_strategy(rxstrategy);
    632 dev_type_dump(rxdump);
    633 dev_type_size(rxsize);
    634 
    635 const struct bdevsw rx_bdevsw = {
    636 	rxopen, nullclose, rxstrategy, rxioctl, rxdump, rxsize, D_DISK
    637 };
    638 
    639 const struct cdevsw rx_cdevsw = {
    640 	rxopen, nullclose, rxread, rxwrite, rxioctl,
    641 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    642 };
    643 
    644 static struct dkdriver rxdkdriver = {
    645 	rxstrategy, minphys
    646 };
    647 
    648 /*
    649  * More driver definitions, for generic MSCP code.
    650  */
    651 
    652 int
    653 rxmatch(parent, cf, aux)
    654 	struct	device *parent;
    655 	struct	cfdata *cf;
    656 	void	*aux;
    657 {
    658 	struct	drive_attach_args *da = aux;
    659 	struct	mscp *mp = da->da_mp;
    660 
    661 	if ((da->da_typ & MSCPBUS_DISK) == 0)
    662 		return 0;
    663 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
    664 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
    665 		return 0;
    666 	/*
    667 	 * Check if this disk is a floppy; then configure it.
    668 	 * Seems to be a safe way to test it per Chris Torek.
    669 	 */
    670 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
    671 		return 1;
    672 	return 0;
    673 }
    674 
    675 #endif /* NRX */
    676 
    677 /*
    678  * The attach routine only checks and prints drive type.
    679  * Bringing the disk online is done when the disk is accessed
    680  * the first time.
    681  */
    682 void
    683 rxattach(parent, self, aux)
    684 	struct	device *parent, *self;
    685 	void	*aux;
    686 {
    687 	struct	rx_softc *rx = (void *)self;
    688 	struct	drive_attach_args *da = aux;
    689 	struct	mscp *mp = da->da_mp;
    690 	struct	mscp_softc *mi = (void *)parent;
    691 	struct	disklabel *dl;
    692 
    693 	rx->ra_mediaid = mp->mscp_guse.guse_mediaid;
    694 	rx->ra_state = DK_CLOSED;
    695 	rx->ra_hwunit = mp->mscp_unit;
    696 	mi->mi_dp[mp->mscp_unit] = self;
    697 
    698 	rx->ra_disk.dk_name = rx->ra_dev.dv_xname;
    699 #if NRX
    700 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
    701 		rx->ra_disk.dk_driver = &rxdkdriver;
    702 #endif
    703 #if NRA
    704 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@')
    705 		rx->ra_disk.dk_driver = &radkdriver;
    706 #endif
    707 	disk_attach((struct disk *)&rx->ra_disk);
    708 
    709 	/* Fill in what we know. The actual size is gotten later */
    710 	dl = rx->ra_disk.dk_label;
    711 
    712 	dl->d_secsize = DEV_BSIZE;
    713 	dl->d_nsectors = mp->mscp_guse.guse_nspt;
    714 	dl->d_ntracks = mp->mscp_guse.guse_ngpc * mp->mscp_guse.guse_group;
    715 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
    716 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
    717 #ifdef DEBUG
    718 	printf("%s: nspt %d group %d ngpc %d rct %d nrpt %d nrct %d\n",
    719 	    self->dv_xname, mp->mscp_guse.guse_nspt, mp->mscp_guse.guse_group,
    720 	    mp->mscp_guse.guse_ngpc, mp->mscp_guse.guse_rctsize,
    721 	    mp->mscp_guse.guse_nrpt, mp->mscp_guse.guse_nrct);
    722 #endif
    723 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@') {
    724 		/*
    725 		 * XXX We should try to discover wedges here, but
    726 		 * XXX that would mean being able to do I/O.  Should
    727 		 * XXX use config_defer() here.
    728 		 */
    729 	}
    730 }
    731 
    732 /*
    733  * (Try to) put the drive online. This is done the first time the
    734  * drive is opened, or if it har fallen offline.
    735  */
    736 int
    737 rx_putonline(rx)
    738 	struct rx_softc *rx;
    739 {
    740 	struct	mscp *mp;
    741 	struct	mscp_softc *mi =
    742 	    (struct mscp_softc *)device_parent(&rx->ra_dev);
    743 	volatile int i;
    744 
    745 	rx->ra_state = DK_CLOSED;
    746 	mp = mscp_getcp(mi, MSCP_WAIT);
    747 	mp->mscp_opcode = M_OP_ONLINE;
    748 	mp->mscp_unit = rx->ra_hwunit;
    749 	mp->mscp_cmdref = 1;
    750 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
    751 
    752 	/* Poll away */
    753 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
    754 	if (tsleep(&rx->ra_state, PRIBIO, "rxonline", 100*100))
    755 		rx->ra_state = DK_CLOSED;
    756 
    757 	if (rx->ra_state == DK_CLOSED)
    758 		return MSCP_FAILED;
    759 
    760 	return MSCP_DONE;
    761 }
    762 
    763 #if NRX
    764 
    765 /*
    766  * Open a drive.
    767  */
    768 /*ARGSUSED*/
    769 int
    770 rxopen(dev, flag, fmt, l)
    771 	dev_t dev;
    772 	int flag, fmt;
    773 	struct	lwp *l;
    774 {
    775 	struct rx_softc *rx;
    776 	int unit;
    777 
    778 	/*
    779 	 * Make sure this is a reasonable open request.
    780 	 */
    781 	unit = DISKUNIT(dev);
    782 	if (unit >= rx_cd.cd_ndevs)
    783 		return ENXIO;
    784 	rx = rx_cd.cd_devs[unit];
    785 	if (rx == 0)
    786 		return ENXIO;
    787 
    788 	/*
    789 	 * If this is the first open; we must first try to put
    790 	 * the disk online (and read the label).
    791 	 */
    792 	if (rx->ra_state == DK_CLOSED)
    793 		if (rx_putonline(rx) == MSCP_FAILED)
    794 			return ENXIO;
    795 
    796 	return 0;
    797 }
    798 
    799 /*
    800  * Queue a transfer request, and if possible, hand it to the controller.
    801  *
    802  * This routine is broken into two so that the internal version
    803  * udastrat1() can be called by the (nonexistent, as yet) bad block
    804  * revectoring routine.
    805  */
    806 void
    807 rxstrategy(bp)
    808 	struct buf *bp;
    809 {
    810 	int unit;
    811 	struct rx_softc *rx;
    812 	int b;
    813 
    814 	/*
    815 	 * Make sure this is a reasonable drive to use.
    816 	 */
    817 	unit = DISKUNIT(bp->b_dev);
    818 	if (unit > rx_cd.cd_ndevs || (rx = rx_cd.cd_devs[unit]) == NULL) {
    819 		bp->b_error = ENXIO;
    820 		bp->b_flags |= B_ERROR;
    821 		goto done;
    822 	}
    823 
    824 	/* If disk is not online, try to put it online */
    825 	if (rx->ra_state == DK_CLOSED)
    826 		if (rx_putonline(rx) == MSCP_FAILED) {
    827 			bp->b_flags |= B_ERROR;
    828 			bp->b_error = EIO;
    829 			goto done;
    830 		}
    831 
    832 	/*
    833 	 * Determine the size of the transfer, and make sure it is
    834 	 * within the boundaries of the partition.
    835 	 */
    836 	if (bp->b_blkno >= rx->ra_disk.dk_label->d_secperunit) {
    837 		bp->b_resid = bp->b_bcount;
    838 		goto done;
    839 	}
    840 
    841 	/* Make some statistics... /bqt */
    842 	b = splbio();
    843 	disk_busy(&rx->ra_disk);
    844 	splx(b);
    845 	mscp_strategy(bp, device_parent(&rx->ra_dev));
    846 	return;
    847 
    848 done:
    849 	biodone(bp);
    850 }
    851 
    852 int
    853 rxread(dev, uio, flag)
    854 	dev_t dev;
    855 	struct uio *uio;
    856 	int flag;
    857 {
    858 
    859 	return (physio(rxstrategy, NULL, dev, B_READ, minphys, uio));
    860 }
    861 
    862 int
    863 rxwrite(dev, uio, flag)
    864 	dev_t dev;
    865 	struct uio *uio;
    866 	int flag;
    867 {
    868 
    869 	return (physio(rxstrategy, NULL, dev, B_WRITE, minphys, uio));
    870 }
    871 
    872 /*
    873  * I/O controls.
    874  */
    875 int
    876 rxioctl(dev, cmd, data, flag, l)
    877 	dev_t dev;
    878 	u_long cmd;
    879 	caddr_t data;
    880 	int flag;
    881 	struct lwp *l;
    882 {
    883 	int unit = DISKUNIT(dev);
    884 	struct disklabel *lp;
    885 	struct rx_softc *rx = rx_cd.cd_devs[unit];
    886 	int error = 0;
    887 
    888 	lp = rx->ra_disk.dk_label;
    889 
    890 	switch (cmd) {
    891 
    892 	case DIOCGDINFO:
    893 		bcopy(lp, data, sizeof (struct disklabel));
    894 		break;
    895 
    896 	case DIOCGPART:
    897 		((struct partinfo *)data)->disklab = lp;
    898 		((struct partinfo *)data)->part =
    899 		    &lp->d_partitions[DISKPART(dev)];
    900 		break;
    901 
    902 
    903 	case DIOCWDINFO:
    904 	case DIOCSDINFO:
    905 	case DIOCWLABEL:
    906 		break;
    907 
    908 	default:
    909 		error = ENOTTY;
    910 		break;
    911 	}
    912 	return (error);
    913 }
    914 
    915 int
    916 rxdump(dev, blkno, va, size)
    917 	dev_t dev;
    918 	daddr_t blkno;
    919 	caddr_t va;
    920 	size_t size;
    921 {
    922 
    923 	/* Not likely. */
    924 	return ENXIO;
    925 }
    926 
    927 int
    928 rxsize(dev)
    929 	dev_t dev;
    930 {
    931 
    932 	return -1;
    933 }
    934 
    935 #endif /* NRX */
    936 
    937 void	rrdgram(struct device *, struct mscp *, struct mscp_softc *);
    938 void	rriodone(struct device *, struct buf *);
    939 int	rronline(struct device *, struct mscp *);
    940 int	rrgotstatus(struct device *, struct mscp *);
    941 void	rrreplace(struct device *, struct mscp *);
    942 int	rrioerror(struct device *, struct mscp *, struct buf *);
    943 void	rrfillin(struct buf *, struct mscp *);
    944 void	rrbb(struct device *, struct mscp *, struct buf *);
    945 
    946 
    947 struct	mscp_device ra_device = {
    948 	rrdgram,
    949 	rriodone,
    950 	rronline,
    951 	rrgotstatus,
    952 	rrreplace,
    953 	rrioerror,
    954 	rrbb,
    955 	rrfillin,
    956 };
    957 
    958 /*
    959  * Handle an error datagram.
    960  * This can come from an unconfigured drive as well.
    961  */
    962 void
    963 rrdgram(usc, mp, mi)
    964 	struct device *usc;
    965 	struct mscp *mp;
    966 	struct mscp_softc *mi;
    967 {
    968 	if (mscp_decodeerror(usc == NULL?"unconf disk" : usc->dv_xname, mp, mi))
    969 		return;
    970 	/*
    971 	 * SDI status information bytes 10 and 11 are the microprocessor
    972 	 * error code and front panel code respectively.  These vary per
    973 	 * drive type and are printed purely for field service information.
    974 	 */
    975 	if (mp->mscp_format == M_FM_SDI)
    976 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
    977 			mp->mscp_erd.erd_sdistat[10],
    978 			mp->mscp_erd.erd_sdistat[11]);
    979 }
    980 
    981 void
    982 rriodone(usc, bp)
    983 	struct device *usc;
    984 	struct buf *bp;
    985 {
    986 	struct ra_softc *ra;
    987 	int unit;
    988 
    989 	/* We assume that this is a reasonable drive. ra_strategy should
    990 	   already have verified it. Thus, no checks here... /bqt */
    991 	unit = DISKUNIT(bp->b_dev);
    992 #if NRA
    993 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
    994 		ra = ra_cd.cd_devs[unit];
    995 	else
    996 #endif
    997 #if NRX
    998 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
    999 		ra = rx_cd.cd_devs[unit];
   1000 	else
   1001 #endif
   1002 		panic("rriodone: unexpected major %d unit %d",
   1003 		    major(bp->b_dev), unit);
   1004 	disk_unbusy(&ra->ra_disk, bp->b_bcount, (bp->b_flags & B_READ));
   1005 
   1006 	biodone(bp);
   1007 }
   1008 
   1009 /*
   1010  * A drive came on line.  Check its type and size.  Return DONE if
   1011  * we think the drive is truly on line.	 In any case, awaken anyone
   1012  * sleeping on the drive on-line-ness.
   1013  */
   1014 int
   1015 rronline(usc, mp)
   1016 	struct device *usc;
   1017 	struct mscp *mp;
   1018 {
   1019 	struct rx_softc *rx = (struct rx_softc *)usc;
   1020 	struct disklabel *dl;
   1021 
   1022 	wakeup((caddr_t)&rx->ra_state);
   1023 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
   1024 		printf("%s: attempt to bring on line failed: ", usc->dv_xname);
   1025 		mscp_printevent(mp);
   1026 		return (MSCP_FAILED);
   1027 	}
   1028 
   1029 	rx->ra_state = DK_OPEN;
   1030 
   1031 	dl = rx->ra_disk.dk_label;
   1032 	dl->d_secperunit = (daddr_t)mp->mscp_onle.onle_unitsize;
   1033 
   1034 	if (dl->d_secpercyl) {
   1035 		dl->d_ncylinders = dl->d_secperunit/dl->d_secpercyl;
   1036 		dl->d_type = DTYPE_MSCP;
   1037 		dl->d_rpm = 3600;
   1038 	} else {
   1039 		dl->d_type = DTYPE_FLOPPY;
   1040 		dl->d_rpm = 300;
   1041 	}
   1042 	rrmakelabel(dl, rx->ra_mediaid);
   1043 
   1044 	return (MSCP_DONE);
   1045 }
   1046 
   1047 void
   1048 rrmakelabel(dl, type)
   1049 	struct disklabel *dl;
   1050 	long type;
   1051 {
   1052 	int n, p = 0;
   1053 
   1054 	dl->d_bbsize = BBSIZE;
   1055 	dl->d_sbsize = SBLOCKSIZE;
   1056 
   1057 	/* Create the disk name for disklabel. Phew... */
   1058 	dl->d_typename[p++] = MSCP_MID_CHAR(2, type);
   1059 	dl->d_typename[p++] = MSCP_MID_CHAR(1, type);
   1060 	if (MSCP_MID_ECH(0, type))
   1061 		dl->d_typename[p++] = MSCP_MID_CHAR(0, type);
   1062 	n = MSCP_MID_NUM(type);
   1063 	if (n > 99) {
   1064 		dl->d_typename[p++] = '1';
   1065 		n -= 100;
   1066 	}
   1067 	if (n > 9) {
   1068 		dl->d_typename[p++] = (n / 10) + '0';
   1069 		n %= 10;
   1070 	}
   1071 	dl->d_typename[p++] = n + '0';
   1072 	dl->d_typename[p] = 0;
   1073 	dl->d_npartitions = MAXPARTITIONS;
   1074 	dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
   1075 	    dl->d_secperunit;
   1076 	dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
   1077 	dl->d_interleave = dl->d_headswitch = 1;
   1078 	dl->d_magic = dl->d_magic2 = DISKMAGIC;
   1079 	dl->d_checksum = dkcksum(dl);
   1080 }
   1081 
   1082 /*
   1083  * We got some (configured) unit's status.  Return DONE if it succeeded.
   1084  */
   1085 int
   1086 rrgotstatus(usc, mp)
   1087 	struct device *usc;
   1088 	struct mscp *mp;
   1089 {
   1090 	if ((mp->mscp_status & M_ST_MASK) != M_ST_SUCCESS) {
   1091 		printf("%s: attempt to get status failed: ", usc->dv_xname);
   1092 		mscp_printevent(mp);
   1093 		return (MSCP_FAILED);
   1094 	}
   1095 	/* record for (future) bad block forwarding and whatever else */
   1096 #ifdef notyet
   1097 	uda_rasave(ui->ui_unit, mp, 1);
   1098 #endif
   1099 	return (MSCP_DONE);
   1100 }
   1101 
   1102 /*
   1103  * A replace operation finished.
   1104  */
   1105 /*ARGSUSED*/
   1106 void
   1107 rrreplace(usc, mp)
   1108 	struct device *usc;
   1109 	struct mscp *mp;
   1110 {
   1111 
   1112 	panic("udareplace");
   1113 }
   1114 
   1115 /*
   1116  * A transfer failed.  We get a chance to fix or restart it.
   1117  * Need to write the bad block forwaring code first....
   1118  */
   1119 /*ARGSUSED*/
   1120 int
   1121 rrioerror(usc, mp, bp)
   1122 	struct device *usc;
   1123 	struct mscp *mp;
   1124 	struct buf *bp;
   1125 {
   1126 	struct ra_softc *ra = (void *)usc;
   1127 	int code = mp->mscp_event;
   1128 
   1129 	switch (code & M_ST_MASK) {
   1130 	/* The unit has fallen offline. Try to figure out why. */
   1131 	case M_ST_OFFLINE:
   1132 		bp->b_flags |= B_ERROR;
   1133 		bp->b_error = EIO;
   1134 		ra->ra_state = DK_CLOSED;
   1135 		if (code & M_OFFLINE_UNMOUNTED)
   1136 			printf("%s: not mounted/spun down\n", usc->dv_xname);
   1137 		if (code & M_OFFLINE_DUPLICATE)
   1138 			printf("%s: duplicate unit number!!!\n", usc->dv_xname);
   1139 		return MSCP_DONE;
   1140 
   1141 	case M_ST_AVAILABLE:
   1142 		ra->ra_state = DK_CLOSED; /* Force another online */
   1143 		return MSCP_DONE;
   1144 
   1145 	default:
   1146 		printf("%s:", usc->dv_xname);
   1147 		break;
   1148 	}
   1149 	return (MSCP_FAILED);
   1150 }
   1151 
   1152 /*
   1153  * Fill in disk addresses in a mscp packet waiting for transfer.
   1154  */
   1155 void
   1156 rrfillin(bp, mp)
   1157 	struct buf *bp;
   1158 	struct mscp *mp;
   1159 {
   1160 	struct rx_softc *rx = 0; /* Wall */
   1161 	struct disklabel *lp;
   1162 	int unit = DISKUNIT(bp->b_dev);
   1163 	int part = DISKPART(bp->b_dev);
   1164 
   1165 #if NRA
   1166 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
   1167 		rx = ra_cd.cd_devs[unit];
   1168 #endif
   1169 #if NRX
   1170 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
   1171 		rx = rx_cd.cd_devs[unit];
   1172 #endif
   1173 	lp = rx->ra_disk.dk_label;
   1174 
   1175 	mp->mscp_seq.seq_lbn = lp->d_partitions[part].p_offset + bp->b_blkno;
   1176 	mp->mscp_unit = rx->ra_hwunit;
   1177 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
   1178 }
   1179 
   1180 /*
   1181  * A bad block related operation finished.
   1182  */
   1183 /*ARGSUSED*/
   1184 void
   1185 rrbb(usc, mp, bp)
   1186 	struct device *usc;
   1187 	struct mscp *mp;
   1188 	struct buf *bp;
   1189 {
   1190 
   1191 	panic("udabb");
   1192 }
   1193