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