Home | History | Annotate | Line # | Download | only in mscp
mscp_disk.c revision 1.56.10.2
      1 /*	$NetBSD: mscp_disk.c,v 1.56.10.2 2007/07/29 12:15:44 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.56.10.2 2007/07/29 12:15:44 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 		goto done;
    369 	}
    370 	/*
    371 	 * If drive is open `raw' or reading label, let it at it.
    372 	 */
    373 	if (ra->ra_state == DK_RDLABEL) {
    374 	        /* Make some statistics... /bqt */
    375 	        b = splbio();
    376 	        disk_busy(&ra->ra_disk);
    377 		splx(b);
    378 		mscp_strategy(bp, device_parent(&ra->ra_dev));
    379 		return;
    380 	}
    381 
    382 	/* If disk is not online, try to put it online */
    383 	if (ra->ra_state == DK_CLOSED)
    384 		if (ra_putonline(ra) == MSCP_FAILED) {
    385 			bp->b_error = EIO;
    386 			goto done;
    387 		}
    388 
    389 	/*
    390 	 * Determine the size of the transfer, and make sure it is
    391 	 * within the boundaries of the partition.
    392 	 */
    393 	if (bounds_check_with_label(&ra->ra_disk, bp, ra->ra_wlabel) <= 0)
    394 		goto done;
    395 
    396 	/* Make some statistics... /bqt */
    397 	b = splbio();
    398 	disk_busy(&ra->ra_disk);
    399 	splx(b);
    400 	mscp_strategy(bp, device_parent(&ra->ra_dev));
    401 	return;
    402 
    403 done:
    404 	biodone(bp);
    405 }
    406 
    407 int
    408 raread(dev, uio, flags)
    409 	dev_t dev;
    410 	struct uio *uio;
    411 	int flags;
    412 {
    413 
    414 	return (physio(rastrategy, NULL, dev, B_READ, minphys, uio));
    415 }
    416 
    417 int
    418 rawrite(dev, uio, flags)
    419 	dev_t dev;
    420 	struct uio *uio;
    421 	int flags;
    422 {
    423 
    424 	return (physio(rastrategy, NULL, dev, B_WRITE, minphys, uio));
    425 }
    426 
    427 /*
    428  * I/O controls.
    429  */
    430 int
    431 raioctl(dev, cmd, data, flag, l)
    432 	dev_t dev;
    433 	u_long cmd;
    434 	void *data;
    435 	int flag;
    436 	struct lwp *l;
    437 {
    438 	int unit = DISKUNIT(dev);
    439 	struct disklabel *lp, *tp;
    440 	struct ra_softc *ra = ra_cd.cd_devs[unit];
    441 	int error = 0;
    442 #ifdef __HAVE_OLD_DISKLABEL
    443 	struct disklabel newlabel;
    444 #endif
    445 
    446 	lp = ra->ra_disk.dk_label;
    447 
    448 	switch (cmd) {
    449 
    450 	case DIOCGDINFO:
    451 		bcopy(lp, data, sizeof (struct disklabel));
    452 		break;
    453 #ifdef __HAVE_OLD_DISKLABEL
    454 	case ODIOCGDINFO:
    455 		bcopy(lp, &newlabel, sizeof disklabel);
    456 		if (newlabel.d_npartitions > OLDMAXPARTITIONS)
    457 			return ENOTTY;
    458 		bcopy(&newlabel, data, sizeof (struct olddisklabel));
    459 		break;
    460 #endif
    461 
    462 	case DIOCGPART:
    463 		((struct partinfo *)data)->disklab = lp;
    464 		((struct partinfo *)data)->part =
    465 		    &lp->d_partitions[DISKPART(dev)];
    466 		break;
    467 
    468 	case DIOCWDINFO:
    469 	case DIOCSDINFO:
    470 #ifdef __HAVE_OLD_DISKLABEL
    471 	case ODIOCWDINFO:
    472 	case ODIOCSDINFO:
    473 		if (cmd == ODIOCSDINFO || xfer == ODIOCWDINFO) {
    474 			memset(&newlabel, 0, sizeof newlabel);
    475 			memcpy(&newlabel, data, sizeof (struct olddisklabel));
    476 			tp = &newlabel;
    477 		} else
    478 #endif
    479 		tp = (struct disklabel *)data;
    480 
    481 		if ((flag & FWRITE) == 0)
    482 			error = EBADF;
    483 		else {
    484 			mutex_enter(&ra->ra_disk.dk_openlock);
    485 			error = setdisklabel(lp, tp, 0, 0);
    486 			if ((error == 0) && (cmd == DIOCWDINFO
    487 #ifdef __HAVE_OLD_DISKLABEL
    488 			    || cmd == ODIOCWDINFO
    489 #else
    490 			    )) {
    491 #endif
    492 				ra->ra_wlabel = 1;
    493 				error = writedisklabel(dev, rastrategy, lp,0);
    494 				ra->ra_wlabel = 0;
    495 			}
    496 			mutex_exit(&ra->ra_disk.dk_openlock);
    497 		}
    498 		break;
    499 
    500 	case DIOCWLABEL:
    501 		if ((flag & FWRITE) == 0)
    502 			error = EBADF;
    503 		else
    504 			ra->ra_wlabel = 1;
    505 		break;
    506 
    507 	case DIOCGDEFLABEL:
    508 #ifdef __HAVE_OLD_DISKLABEL
    509 	case ODIOCGDEFLABEL:
    510 		if (cmd == ODIOCGDEFLABEL)
    511 			tp = &newlabel;
    512 		else
    513 #else
    514 		tp = (struct disklabel *)data;
    515 #endif
    516 		bzero(tp, sizeof(struct disklabel));
    517 		tp->d_secsize = lp->d_secsize;
    518 		tp->d_nsectors = lp->d_nsectors;
    519 		tp->d_ntracks = lp->d_ntracks;
    520 		tp->d_ncylinders = lp->d_ncylinders;
    521 		tp->d_secpercyl = lp->d_secpercyl;
    522 		tp->d_secperunit = lp->d_secperunit;
    523 		tp->d_type = DTYPE_MSCP;
    524 		tp->d_rpm = 3600;
    525 		rrmakelabel(tp, ra->ra_mediaid);
    526 #ifdef __HAVE_OLD_DISKLABEL
    527 		if (cmd == ODIOCGDEFLABEL) {
    528 			if (tp->d_npartitions > OLDMAXPARTITIONS)
    529 				return ENOTTY;
    530 			memcpy(data, tp, sizeof (struct olddisklabel));
    531 		}
    532 #endif
    533 		break;
    534 
    535 	case DIOCAWEDGE:
    536 	    {
    537 	    	struct dkwedge_info *dkw = (void *) data;
    538 
    539 		if ((flag & FWRITE) == 0)
    540 			return (EBADF);
    541 
    542 		/* If the ioctl happens here, the parent is us. */
    543 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
    544 		return (dkwedge_add(dkw));
    545 	    }
    546 
    547 	case DIOCDWEDGE:
    548 	    {
    549 	    	struct dkwedge_info *dkw = (void *) data;
    550 
    551 		if ((flag & FWRITE) == 0)
    552 			return (EBADF);
    553 
    554 		/* If the ioctl happens here, the parent is us. */
    555 		strcpy(dkw->dkw_parent, ra->ra_dev.dv_xname);
    556 		return (dkwedge_del(dkw));
    557 	    }
    558 
    559 	case DIOCLWEDGES:
    560 	    {
    561 	    	struct dkwedge_list *dkwl = (void *) data;
    562 
    563 		return (dkwedge_list(&ra->ra_disk, dkwl, l));
    564 	    }
    565 
    566 	default:
    567 		error = ENOTTY;
    568 		break;
    569 	}
    570 	return (error);
    571 }
    572 
    573 
    574 int
    575 radump(dev, blkno, va, size)
    576 	dev_t	dev;
    577 	daddr_t blkno;
    578 	void *va;
    579 	size_t	size;
    580 {
    581 	return ENXIO;
    582 }
    583 
    584 /*
    585  * Return the size of a partition, if known, or -1 if not.
    586  */
    587 int
    588 rasize(dev)
    589 	dev_t dev;
    590 {
    591 	int unit = DISKUNIT(dev);
    592 	struct ra_softc *ra;
    593 
    594 	if (unit >= ra_cd.cd_ndevs || ra_cd.cd_devs[unit] == 0)
    595 		return -1;
    596 
    597 	ra = ra_cd.cd_devs[unit];
    598 
    599 	if (ra->ra_state == DK_CLOSED)
    600 		if (ra_putonline(ra) == MSCP_FAILED)
    601 			return -1;
    602 
    603 	return ra->ra_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
    604 	    (ra->ra_disk.dk_label->d_secsize / DEV_BSIZE);
    605 }
    606 
    607 #endif /* NRA */
    608 
    609 #if NRX
    610 
    611 int	rxmatch(struct device *, struct cfdata *, void *);
    612 
    613 CFATTACH_DECL(rx, sizeof(struct rx_softc),
    614     rxmatch, rxattach, NULL, NULL);
    615 
    616 dev_type_open(rxopen);
    617 dev_type_read(rxread);
    618 dev_type_write(rxwrite);
    619 dev_type_ioctl(rxioctl);
    620 dev_type_strategy(rxstrategy);
    621 dev_type_dump(rxdump);
    622 dev_type_size(rxsize);
    623 
    624 const struct bdevsw rx_bdevsw = {
    625 	rxopen, nullclose, rxstrategy, rxioctl, rxdump, rxsize, D_DISK
    626 };
    627 
    628 const struct cdevsw rx_cdevsw = {
    629 	rxopen, nullclose, rxread, rxwrite, rxioctl,
    630 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    631 };
    632 
    633 static struct dkdriver rxdkdriver = {
    634 	rxstrategy, minphys
    635 };
    636 
    637 /*
    638  * More driver definitions, for generic MSCP code.
    639  */
    640 
    641 int
    642 rxmatch(parent, cf, aux)
    643 	struct	device *parent;
    644 	struct	cfdata *cf;
    645 	void	*aux;
    646 {
    647 	struct	drive_attach_args *da = aux;
    648 	struct	mscp *mp = da->da_mp;
    649 
    650 	if ((da->da_typ & MSCPBUS_DISK) == 0)
    651 		return 0;
    652 	if (cf->cf_loc[MSCPBUSCF_DRIVE] != MSCPBUSCF_DRIVE_DEFAULT &&
    653 	    cf->cf_loc[MSCPBUSCF_DRIVE] != mp->mscp_unit)
    654 		return 0;
    655 	/*
    656 	 * Check if this disk is a floppy; then configure it.
    657 	 * Seems to be a safe way to test it per Chris Torek.
    658 	 */
    659 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
    660 		return 1;
    661 	return 0;
    662 }
    663 
    664 #endif /* NRX */
    665 
    666 /*
    667  * The attach routine only checks and prints drive type.
    668  * Bringing the disk online is done when the disk is accessed
    669  * the first time.
    670  */
    671 void
    672 rxattach(parent, self, aux)
    673 	struct	device *parent, *self;
    674 	void	*aux;
    675 {
    676 	struct	rx_softc *rx = device_private(self);
    677 	struct	drive_attach_args *da = aux;
    678 	struct	mscp *mp = da->da_mp;
    679 	struct	mscp_softc *mi = (void *)parent;
    680 	struct	disklabel *dl;
    681 
    682 	rx->ra_mediaid = mp->mscp_guse.guse_mediaid;
    683 	rx->ra_state = DK_CLOSED;
    684 	rx->ra_hwunit = mp->mscp_unit;
    685 	mi->mi_dp[mp->mscp_unit] = self;
    686 
    687 	rx->ra_disk.dk_name = rx->ra_dev.dv_xname;
    688 #if NRX
    689 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) == 'X' - '@')
    690 		rx->ra_disk.dk_driver = &rxdkdriver;
    691 #endif
    692 #if NRA
    693 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@')
    694 		rx->ra_disk.dk_driver = &radkdriver;
    695 #endif
    696 	disk_attach((struct disk *)&rx->ra_disk);
    697 
    698 	/* Fill in what we know. The actual size is gotten later */
    699 	dl = rx->ra_disk.dk_label;
    700 
    701 	dl->d_secsize = DEV_BSIZE;
    702 	dl->d_nsectors = mp->mscp_guse.guse_nspt;
    703 	dl->d_ntracks = mp->mscp_guse.guse_ngpc * mp->mscp_guse.guse_group;
    704 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
    705 	disk_printtype(mp->mscp_unit, mp->mscp_guse.guse_mediaid);
    706 #ifdef DEBUG
    707 	printf("%s: nspt %d group %d ngpc %d rct %d nrpt %d nrct %d\n",
    708 	    self->dv_xname, mp->mscp_guse.guse_nspt, mp->mscp_guse.guse_group,
    709 	    mp->mscp_guse.guse_ngpc, mp->mscp_guse.guse_rctsize,
    710 	    mp->mscp_guse.guse_nrpt, mp->mscp_guse.guse_nrct);
    711 #endif
    712 	if (MSCP_MID_ECH(1, mp->mscp_guse.guse_mediaid) != 'X' - '@') {
    713 		/*
    714 		 * XXX We should try to discover wedges here, but
    715 		 * XXX that would mean being able to do I/O.  Should
    716 		 * XXX use config_defer() here.
    717 		 */
    718 	}
    719 }
    720 
    721 /*
    722  * (Try to) put the drive online. This is done the first time the
    723  * drive is opened, or if it har fallen offline.
    724  */
    725 int
    726 rx_putonline(rx)
    727 	struct rx_softc *rx;
    728 {
    729 	struct	mscp *mp;
    730 	struct	mscp_softc *mi =
    731 	    (struct mscp_softc *)device_parent(&rx->ra_dev);
    732 	volatile int i;
    733 
    734 	rx->ra_state = DK_CLOSED;
    735 	mp = mscp_getcp(mi, MSCP_WAIT);
    736 	mp->mscp_opcode = M_OP_ONLINE;
    737 	mp->mscp_unit = rx->ra_hwunit;
    738 	mp->mscp_cmdref = 1;
    739 	*mp->mscp_addr |= MSCP_OWN | MSCP_INT;
    740 
    741 	/* Poll away */
    742 	i = bus_space_read_2(mi->mi_iot, mi->mi_iph, 0);
    743 	if (tsleep(&rx->ra_state, PRIBIO, "rxonline", 100*100))
    744 		rx->ra_state = DK_CLOSED;
    745 
    746 	if (rx->ra_state == DK_CLOSED)
    747 		return MSCP_FAILED;
    748 
    749 	return MSCP_DONE;
    750 }
    751 
    752 #if NRX
    753 
    754 /*
    755  * Open a drive.
    756  */
    757 /*ARGSUSED*/
    758 int
    759 rxopen(dev, flag, fmt, l)
    760 	dev_t dev;
    761 	int flag, fmt;
    762 	struct	lwp *l;
    763 {
    764 	struct rx_softc *rx;
    765 	int unit;
    766 
    767 	/*
    768 	 * Make sure this is a reasonable open request.
    769 	 */
    770 	unit = DISKUNIT(dev);
    771 	if (unit >= rx_cd.cd_ndevs)
    772 		return ENXIO;
    773 	rx = rx_cd.cd_devs[unit];
    774 	if (rx == 0)
    775 		return ENXIO;
    776 
    777 	/*
    778 	 * If this is the first open; we must first try to put
    779 	 * the disk online (and read the label).
    780 	 */
    781 	if (rx->ra_state == DK_CLOSED)
    782 		if (rx_putonline(rx) == MSCP_FAILED)
    783 			return ENXIO;
    784 
    785 	return 0;
    786 }
    787 
    788 /*
    789  * Queue a transfer request, and if possible, hand it to the controller.
    790  *
    791  * This routine is broken into two so that the internal version
    792  * udastrat1() can be called by the (nonexistent, as yet) bad block
    793  * revectoring routine.
    794  */
    795 void
    796 rxstrategy(bp)
    797 	struct buf *bp;
    798 {
    799 	int unit;
    800 	struct rx_softc *rx;
    801 	int b;
    802 
    803 	/*
    804 	 * Make sure this is a reasonable drive to use.
    805 	 */
    806 	unit = DISKUNIT(bp->b_dev);
    807 	if (unit > rx_cd.cd_ndevs || (rx = rx_cd.cd_devs[unit]) == NULL) {
    808 		bp->b_error = ENXIO;
    809 		goto done;
    810 	}
    811 
    812 	/* If disk is not online, try to put it online */
    813 	if (rx->ra_state == DK_CLOSED)
    814 		if (rx_putonline(rx) == MSCP_FAILED) {
    815 			bp->b_error = EIO;
    816 			goto done;
    817 		}
    818 
    819 	/*
    820 	 * Determine the size of the transfer, and make sure it is
    821 	 * within the boundaries of the partition.
    822 	 */
    823 	if (bp->b_blkno >= rx->ra_disk.dk_label->d_secperunit) {
    824 		bp->b_resid = bp->b_bcount;
    825 		goto done;
    826 	}
    827 
    828 	/* Make some statistics... /bqt */
    829 	b = splbio();
    830 	disk_busy(&rx->ra_disk);
    831 	splx(b);
    832 	mscp_strategy(bp, device_parent(&rx->ra_dev));
    833 	return;
    834 
    835 done:
    836 	biodone(bp);
    837 }
    838 
    839 int
    840 rxread(dev, uio, flag)
    841 	dev_t dev;
    842 	struct uio *uio;
    843 	int flag;
    844 {
    845 
    846 	return (physio(rxstrategy, NULL, dev, B_READ, minphys, uio));
    847 }
    848 
    849 int
    850 rxwrite(dev, uio, flag)
    851 	dev_t dev;
    852 	struct uio *uio;
    853 	int flag;
    854 {
    855 
    856 	return (physio(rxstrategy, NULL, dev, B_WRITE, minphys, uio));
    857 }
    858 
    859 /*
    860  * I/O controls.
    861  */
    862 int
    863 rxioctl(dev, cmd, data, flag, l)
    864 	dev_t dev;
    865 	u_long cmd;
    866 	void *data;
    867 	int flag;
    868 	struct lwp *l;
    869 {
    870 	int unit = DISKUNIT(dev);
    871 	struct disklabel *lp;
    872 	struct rx_softc *rx = rx_cd.cd_devs[unit];
    873 	int error = 0;
    874 
    875 	lp = rx->ra_disk.dk_label;
    876 
    877 	switch (cmd) {
    878 
    879 	case DIOCGDINFO:
    880 		bcopy(lp, data, sizeof (struct disklabel));
    881 		break;
    882 
    883 	case DIOCGPART:
    884 		((struct partinfo *)data)->disklab = lp;
    885 		((struct partinfo *)data)->part =
    886 		    &lp->d_partitions[DISKPART(dev)];
    887 		break;
    888 
    889 
    890 	case DIOCWDINFO:
    891 	case DIOCSDINFO:
    892 	case DIOCWLABEL:
    893 		break;
    894 
    895 	default:
    896 		error = ENOTTY;
    897 		break;
    898 	}
    899 	return (error);
    900 }
    901 
    902 int
    903 rxdump(dev, blkno, va, size)
    904 	dev_t dev;
    905 	daddr_t blkno;
    906 	void *va;
    907 	size_t size;
    908 {
    909 
    910 	/* Not likely. */
    911 	return ENXIO;
    912 }
    913 
    914 int
    915 rxsize(dev)
    916 	dev_t dev;
    917 {
    918 
    919 	return -1;
    920 }
    921 
    922 #endif /* NRX */
    923 
    924 void	rrdgram(struct device *, struct mscp *, struct mscp_softc *);
    925 void	rriodone(struct device *, struct buf *);
    926 int	rronline(struct device *, struct mscp *);
    927 int	rrgotstatus(struct device *, struct mscp *);
    928 void	rrreplace(struct device *, struct mscp *);
    929 int	rrioerror(struct device *, struct mscp *, struct buf *);
    930 void	rrfillin(struct buf *, struct mscp *);
    931 void	rrbb(struct device *, struct mscp *, struct buf *);
    932 
    933 
    934 struct	mscp_device ra_device = {
    935 	rrdgram,
    936 	rriodone,
    937 	rronline,
    938 	rrgotstatus,
    939 	rrreplace,
    940 	rrioerror,
    941 	rrbb,
    942 	rrfillin,
    943 };
    944 
    945 /*
    946  * Handle an error datagram.
    947  * This can come from an unconfigured drive as well.
    948  */
    949 void
    950 rrdgram(usc, mp, mi)
    951 	struct device *usc;
    952 	struct mscp *mp;
    953 	struct mscp_softc *mi;
    954 {
    955 	if (mscp_decodeerror(usc == NULL?"unconf disk" : usc->dv_xname, mp, mi))
    956 		return;
    957 	/*
    958 	 * SDI status information bytes 10 and 11 are the microprocessor
    959 	 * error code and front panel code respectively.  These vary per
    960 	 * drive type and are printed purely for field service information.
    961 	 */
    962 	if (mp->mscp_format == M_FM_SDI)
    963 		printf("\tsdi uproc error code 0x%x, front panel code 0x%x\n",
    964 			mp->mscp_erd.erd_sdistat[10],
    965 			mp->mscp_erd.erd_sdistat[11]);
    966 }
    967 
    968 void
    969 rriodone(usc, bp)
    970 	struct device *usc;
    971 	struct buf *bp;
    972 {
    973 	struct ra_softc *ra;
    974 	int unit;
    975 
    976 	/* We assume that this is a reasonable drive. ra_strategy should
    977 	   already have verified it. Thus, no checks here... /bqt */
    978 	unit = DISKUNIT(bp->b_dev);
    979 #if NRA
    980 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
    981 		ra = ra_cd.cd_devs[unit];
    982 	else
    983 #endif
    984 #if NRX
    985 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
    986 		ra = rx_cd.cd_devs[unit];
    987 	else
    988 #endif
    989 		panic("rriodone: unexpected major %d unit %d",
    990 		    major(bp->b_dev), 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((void *)&rx->ra_state);
   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_error = EIO;
   1120 		ra->ra_state = DK_CLOSED;
   1121 		if (code & M_OFFLINE_UNMOUNTED)
   1122 			printf("%s: not mounted/spun down\n", usc->dv_xname);
   1123 		if (code & M_OFFLINE_DUPLICATE)
   1124 			printf("%s: duplicate unit number!!!\n", usc->dv_xname);
   1125 		return MSCP_DONE;
   1126 
   1127 	case M_ST_AVAILABLE:
   1128 		ra->ra_state = DK_CLOSED; /* Force another online */
   1129 		return MSCP_DONE;
   1130 
   1131 	default:
   1132 		printf("%s:", usc->dv_xname);
   1133 		break;
   1134 	}
   1135 	return (MSCP_FAILED);
   1136 }
   1137 
   1138 /*
   1139  * Fill in disk addresses in a mscp packet waiting for transfer.
   1140  */
   1141 void
   1142 rrfillin(bp, mp)
   1143 	struct buf *bp;
   1144 	struct mscp *mp;
   1145 {
   1146 	struct rx_softc *rx = 0; /* Wall */
   1147 	struct disklabel *lp;
   1148 	int unit = DISKUNIT(bp->b_dev);
   1149 	int part = DISKPART(bp->b_dev);
   1150 
   1151 #if NRA
   1152 	if (cdevsw_lookup(bp->b_dev) == &ra_cdevsw)
   1153 		rx = ra_cd.cd_devs[unit];
   1154 #endif
   1155 #if NRX
   1156 	if (cdevsw_lookup(bp->b_dev) == &rx_cdevsw)
   1157 		rx = rx_cd.cd_devs[unit];
   1158 #endif
   1159 	lp = rx->ra_disk.dk_label;
   1160 
   1161 	mp->mscp_seq.seq_lbn = lp->d_partitions[part].p_offset + bp->b_blkno;
   1162 	mp->mscp_unit = rx->ra_hwunit;
   1163 	mp->mscp_seq.seq_bytecount = bp->b_bcount;
   1164 }
   1165 
   1166 /*
   1167  * A bad block related operation finished.
   1168  */
   1169 /*ARGSUSED*/
   1170 void
   1171 rrbb(usc, mp, bp)
   1172 	struct device *usc;
   1173 	struct mscp *mp;
   1174 	struct buf *bp;
   1175 {
   1176 
   1177 	panic("udabb");
   1178 }
   1179