Home | History | Annotate | Line # | Download | only in vsa
hdc9224.c revision 1.16.2.1
      1 /*	$NetBSD: hdc9224.c,v 1.16.2.1 2001/10/10 11:56:45 fvdl Exp $ */
      2 /*
      3  * Copyright (c) 1996 Ludd, University of Lule}, Sweden.
      4  * All rights reserved.
      5  *
      6  * This code is derived from software contributed to Ludd by Bertram Barth.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed at Ludd, University of
     19  *	Lule}, Sweden and its contributors.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * with much help from (in alphabetical order):
     37  *	Jeremy
     38  *	Roger Ivie
     39  *	Rick Macklem
     40  *	Mike Young
     41  *
     42  * Rewritten by Ragge 25 Jun 2000. New features:
     43  *	- Uses interrupts instead of polling to signal ready.
     44  *	- Can cooperate with the SCSI routines WRT. the DMA area.
     45  *
     46  * TODO:
     47  *	- Floppy support missing.
     48  *	- Bad block forwarding missing.
     49  *	- Statistics collection.
     50  */
     51 #undef	RDDEBUG
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/conf.h>
     57 #include <sys/file.h>
     58 #include <sys/stat.h>
     59 #include <sys/ioctl.h>
     60 #include <sys/buf.h>
     61 #include <sys/proc.h>
     62 #include <sys/user.h>
     63 #include <sys/map.h>
     64 #include <sys/device.h>
     65 #include <sys/dkstat.h>
     66 #include <sys/disklabel.h>
     67 #include <sys/disk.h>
     68 #include <sys/syslog.h>
     69 #include <sys/reboot.h>
     70 #include <sys/vnode.h>
     71 
     72 #include <uvm/uvm_extern.h>
     73 
     74 #include <ufs/ufs/dinode.h> /* For BBSIZE */
     75 #include <ufs/ffs/fs.h>
     76 
     77 #include <machine/pte.h>
     78 #include <machine/sid.h>
     79 #include <machine/cpu.h>
     80 #include <machine/uvax.h>
     81 #include <machine/ka410.h>
     82 #include <machine/vsbus.h>
     83 #include <machine/rpb.h>
     84 #include <machine/scb.h>
     85 
     86 #include <dev/mscp/mscp.h> /* For DEC disk encoding */
     87 
     88 #include <vax/vsa/hdc9224.h>
     89 
     90 #include "ioconf.h"
     91 #include "locators.h"
     92 
     93 
     94 /*
     95  * on-disk geometry block
     96  */
     97 #define _aP	__attribute__ ((packed))	/* force byte-alignment */
     98 struct rdgeom {
     99 	char mbz[10];		/* 10 bytes of zero */
    100 	long xbn_count _aP;	/* number of XBNs */
    101 	long dbn_count _aP;	/* number of DBNs */
    102 	long lbn_count _aP;	/* number of LBNs (Logical-Block-Numbers) */
    103 	long rbn_count _aP;	/* number of RBNs (Replacement-Block-Numbers) */
    104 	short nspt;		/* number of sectors per track */
    105 	short ntracks;		/* number of tracks */
    106 	short ncylinders;	/* number of cylinders */
    107 	short precomp;		/* first cylinder for write precompensation */
    108 	short reduced;		/* first cylinder for reduced write current */
    109 	short seek_rate;	/* seek rate or zero for buffered seeks */
    110 	short crc_eec;		/* 0 if CRC, 1 if ECC is being used */
    111 	short rct;		/* "replacement control table" (RCT) */
    112 	short rct_ncopies;	/* number of copies of the RCT */
    113 	long	media_id _aP;	/* media identifier */
    114 	short interleave;	/* sector-to-sector interleave */
    115 	short headskew;		/* head-to-head skew */
    116 	short cylskew;		/* cylinder-to-cylinder skew */
    117 	short gap0_size;	/* size of GAP 0 in the MFM format */
    118 	short gap1_size;	/* size of GAP 1 in the MFM format */
    119 	short gap2_size;	/* size of GAP 2 in the MFM format */
    120 	short gap3_size;	/* size of GAP 3 in the MFM format */
    121 	short sync_value;	/* sync value used when formatting */
    122 	char	reserved[32];	/* reserved for use by the RQDX formatter */
    123 	short serial_number;	/* serial number */
    124 #if 0	/* we don't need these 412 useless bytes ... */
    125 	char	fill[412-2];	/* Filler bytes to the end of the block */
    126 	short checksum;	/* checksum over the XBN */
    127 #endif
    128 };
    129 
    130 /*
    131  * Software status
    132  */
    133 struct	rdsoftc {
    134 	struct device sc_dev;		/* must be here! (pseudo-OOP:) */
    135 	struct disk sc_disk;		/* disklabel etc. */
    136 	struct rdgeom sc_xbn;		/* on-disk geometry information */
    137 	int sc_drive;		/* physical unit number */
    138 };
    139 
    140 struct	hdcsoftc {
    141 	struct device sc_dev;		/* must be here (pseudo-OOP:) */
    142 	struct evcnt sc_intrcnt;
    143 	struct vsbus_dma sc_vd;
    144 	vaddr_t sc_regs;		/* register addresses */
    145 	struct buf_queue sc_q;
    146 	struct buf *sc_active;
    147 	struct hdc9224_UDCreg sc_creg;	/* (command) registers to be written */
    148 	struct hdc9224_UDCreg sc_sreg;	/* (status) registers being read */
    149 	caddr_t	sc_dmabase;		/* */
    150 	int	sc_dmasize;
    151 	caddr_t sc_bufaddr;		/* Current in-core address */
    152 	int sc_diskblk;			/* Current block on disk */
    153 	int sc_bytecnt;			/* How much left to transfer */
    154 	int sc_xfer;			/* Current transfer size */
    155 	int sc_retries;
    156 	volatile u_char sc_status;	/* last status from interrupt */
    157 	char sc_intbit;
    158 };
    159 
    160 struct hdc_attach_args {
    161 	int ha_drive;
    162 };
    163 
    164 /*
    165  * prototypes for (almost) all the internal routines
    166  */
    167 static	int hdcmatch(struct device *, struct cfdata *, void *);
    168 static	void hdcattach(struct device *, struct device *, void *);
    169 static	int hdcprint(void *, const char *);
    170 static	int rdmatch(struct device *, struct cfdata *, void *);
    171 static	void rdattach(struct device *, struct device *, void *);
    172 static	void hdcintr(void *);
    173 static	int hdc_command(struct hdcsoftc *, int);
    174 static	void rd_readgeom(struct hdcsoftc *, struct rdsoftc *);
    175 #ifdef RDDEBUG
    176 static	void hdc_printgeom( struct rdgeom *);
    177 #endif
    178 static	void hdc_writeregs(struct hdcsoftc *);
    179 static	void hdcstart(struct hdcsoftc *, struct buf *);
    180 static	int hdc_rdselect(struct hdcsoftc *, int);
    181 static	void rdmakelabel(struct disklabel *, struct rdgeom *);
    182 static	void hdc_writeregs(struct hdcsoftc *);
    183 static	void hdc_readregs(struct hdcsoftc *);
    184 static	void hdc_qstart(void *);
    185 
    186 bdev_decl(rd);
    187 cdev_decl(rd);
    188 
    189 struct	cfattach hdc_ca = {
    190 	sizeof(struct hdcsoftc), hdcmatch, hdcattach
    191 };
    192 
    193 struct	cfattach rd_ca = {
    194 	sizeof(struct rdsoftc), rdmatch, rdattach
    195 };
    196 
    197 
    198 /* At least 0.7 uS between register accesses */
    199 static int rd_dmasize, inq = 0;
    200 static int u;
    201 #define	WAIT	asm("movl _u,_u;movl _u,_u;movl _u,_u; movl _u,_u")
    202 
    203 #define	HDC_WREG(x)	*(volatile char *)(sc->sc_regs) = (x)
    204 #define	HDC_RREG	*(volatile char *)(sc->sc_regs)
    205 #define	HDC_WCMD(x)	*(volatile char *)(sc->sc_regs + 4) = (x)
    206 #define	HDC_RSTAT	*(volatile char *)(sc->sc_regs + 4)
    207 
    208 /*
    209  * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
    210  * thus we probe for the existence of the controller and reset it.
    211  * NB: we can't initialize the controller yet, since space for hdcsoftc
    212  *     is not yet allocated. Thus we do this in hdcattach()...
    213  */
    214 int
    215 hdcmatch(struct device *parent, struct cfdata *cf, void *aux)
    216 {
    217 	struct vsbus_attach_args *va = aux;
    218 	volatile char *hdc_csr = (char *)va->va_addr;
    219 	int i;
    220 
    221 	u = 8; /* !!! - GCC */
    222 
    223 	if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46
    224 	    || vax_boardtype == VAX_BTYP_48 || vax_boardtype == VAX_BTYP_53)
    225 		return 0;
    226 
    227 	hdc_csr[4] = DKC_CMD_RESET; /* reset chip */
    228 	for (i = 0; i < 1000; i++) {
    229 		DELAY(1000);
    230 		if (hdc_csr[4] & DKC_ST_DONE)
    231 			break;
    232 	}
    233 	if (i == 100)
    234 		return 0; /* No response to reset */
    235 
    236 	hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM;
    237 	WAIT;
    238 	hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE;
    239 	WAIT;
    240 	hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */
    241 	DELAY(1000);
    242 	return (1);
    243 }
    244 
    245 int
    246 hdcprint(void *aux, const char *name)
    247 {
    248 	struct hdc_attach_args *ha = aux;
    249 
    250 	if (name)
    251 		printf ("RD?? at %s drive %d", name, ha->ha_drive);
    252 	return UNCONF;
    253 }
    254 
    255 /*
    256  * hdc_attach() probes for all possible devices
    257  */
    258 void
    259 hdcattach(struct device *parent, struct device *self, void *aux)
    260 {
    261 	struct vsbus_attach_args *va = aux;
    262 	struct hdcsoftc *sc = (void *)self;
    263 	struct hdc_attach_args ha;
    264 	int status, i;
    265 
    266 	printf ("\n");
    267 	/*
    268 	 * Get interrupt vector, enable instrumentation.
    269 	 */
    270 	scb_vecalloc(va->va_cvec, hdcintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
    271 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    272 	    self->dv_xname, "intr");
    273 
    274 	sc->sc_regs = vax_map_physmem(va->va_paddr, 1);
    275 	sc->sc_dmabase = (caddr_t)va->va_dmaaddr;
    276 	sc->sc_dmasize = va->va_dmasize;
    277 	sc->sc_intbit = va->va_maskno;
    278 	rd_dmasize = min(MAXPHYS, sc->sc_dmasize); /* Used in rd_minphys */
    279 
    280 	sc->sc_vd.vd_go = hdc_qstart;
    281 	sc->sc_vd.vd_arg = sc;
    282 	/*
    283 	 * Reset controller.
    284 	 */
    285 	HDC_WCMD(DKC_CMD_RESET);
    286 	DELAY(1000);
    287 	status = HDC_RSTAT;
    288 	if (status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
    289 		printf("%s: RESET failed,  status 0x%x\n",
    290 			sc->sc_dev.dv_xname, status);
    291 		return;
    292 	}
    293 	BUFQ_INIT(&sc->sc_q);
    294 
    295 	/*
    296 	 * now probe for all possible hard drives
    297 	 */
    298 	for (i = 0; i < 4; i++) {
    299 		if (i == 2) /* Floppy, needs special handling */
    300 			continue;
    301 		HDC_WCMD(DKC_CMD_DRSELECT | i);
    302 		DELAY(1000);
    303 		status = HDC_RSTAT;
    304 		ha.ha_drive = i;
    305 		if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS)
    306 			config_found(self, (void *)&ha, hdcprint);
    307 	}
    308 }
    309 
    310 /*
    311  * rdmatch() probes for the existence of a RD-type disk/floppy
    312  */
    313 int
    314 rdmatch(parent, cf, aux)
    315 	struct device *parent;
    316 	struct cfdata *cf;
    317 	void *aux;
    318 {
    319 	struct hdc_attach_args *ha = aux;
    320 
    321 	if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT &&
    322 	    cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive)
    323 		return 0;
    324 
    325 	if (ha->ha_drive == 2) /* Always floppy, not supported */
    326 		return 0;
    327 
    328 	return 1;
    329 }
    330 
    331 #define	RDMAJOR 19
    332 
    333 void
    334 rdattach(struct device *parent, struct device *self, void *aux)
    335 {
    336 	struct hdcsoftc *sc = (void*)parent;
    337 	struct rdsoftc *rd = (void*)self;
    338 	struct hdc_attach_args *ha = aux;
    339 	struct disklabel *dl;
    340 	char *msg;
    341 	struct vnode vn;		/* XXX */
    342 	struct specinfo si;		/* XXX */
    343 
    344 	rd->sc_drive = ha->ha_drive;
    345 	/*
    346 	 * Initialize and attach the disk structure.
    347 	 */
    348 	rd->sc_disk.dk_name = rd->sc_dev.dv_xname;
    349 	disk_attach(&rd->sc_disk);
    350 
    351 	/*
    352 	 * if it's not a floppy then evaluate the on-disk geometry.
    353 	 * if necessary correct the label...
    354 	 */
    355 	rd_readgeom(sc, rd);
    356 	disk_printtype(rd->sc_drive, rd->sc_xbn.media_id);
    357 	dl = rd->sc_disk.dk_label;
    358 	rdmakelabel(dl, &rd->sc_xbn);
    359 	printf("%s", rd->sc_dev.dv_xname);
    360 	vn.v_type = VBLK;
    361 	vn.v_specinfo = &si;
    362 	vn.v_rdev = MAKEDISKDEV(RDMAJOR, rd->sc_dev.dv_unit, RAW_PART);
    363 	vn.v_devcookie = rd;
    364 	msg = readdisklabel(&vn, rdstrategy, dl, NULL);
    365 	if (msg)
    366 		printf(": %s", msg);
    367 	printf(": size %d sectors\n", dl->d_secperunit);
    368 #ifdef RDDEBUG
    369 	hdc_printgeom(&rd->sc_xbn);
    370 #endif
    371 }
    372 
    373 void
    374 hdcintr(void *arg)
    375 {
    376 	struct hdcsoftc *sc = arg;
    377 	struct buf *bp;
    378 
    379 	sc->sc_status = HDC_RSTAT;
    380 	if (sc->sc_active == 0)
    381 		return; /* Complain? */
    382 
    383 	if ((sc->sc_status & (DKC_ST_INTPEND|DKC_ST_DONE)) !=
    384 	    (DKC_ST_INTPEND|DKC_ST_DONE))
    385 		return; /* Why spurious ints sometimes??? */
    386 
    387 	bp = sc->sc_active;
    388 	sc->sc_active = 0;
    389 	if ((sc->sc_status & DKC_ST_TERMCOD) != DKC_TC_SUCCESS) {
    390 		int i;
    391 		u_char *g = (u_char *)&sc->sc_sreg;
    392 
    393 		if (sc->sc_retries++ < 3) { /* Allow 3 retries */
    394 			hdcstart(sc, bp);
    395 			return;
    396 		}
    397 		printf("%s: failed, status 0x%x\n",
    398 		    sc->sc_dev.dv_xname, sc->sc_status);
    399 		hdc_readregs(sc);
    400 		for (i = 0; i < 10; i++)
    401 			printf("%i: %x\n", i, g[i]);
    402 		bp->b_flags |= B_ERROR;
    403 		bp->b_error = ENXIO;
    404 		bp->b_resid = bp->b_bcount;
    405 		biodone(bp);
    406 		vsbus_dma_intr();
    407 		return;
    408 	}
    409 
    410 	if (bp->b_flags & B_READ) {
    411 		vsbus_copytoproc(bp->b_proc, sc->sc_dmabase, sc->sc_bufaddr,
    412 		    sc->sc_xfer);
    413 	}
    414 	sc->sc_diskblk += (sc->sc_xfer/DEV_BSIZE);
    415 	sc->sc_bytecnt -= sc->sc_xfer;
    416 	sc->sc_bufaddr += sc->sc_xfer;
    417 
    418 	if (sc->sc_bytecnt == 0) { /* Finished transfer */
    419 		biodone(bp);
    420 		vsbus_dma_intr();
    421 	} else
    422 		hdcstart(sc, bp);
    423 }
    424 
    425 /*
    426  *
    427  */
    428 void
    429 rdstrategy(struct buf *bp)
    430 {
    431 	struct rdsoftc *rd;
    432 	struct hdcsoftc *sc;
    433 	struct disklabel *lp;
    434 	int unit, s, part;
    435 
    436 	unit = DISKUNIT(vdev_rdev(bp->b_devvp));
    437 	if (unit > rd_cd.cd_ndevs || (rd = rd_cd.cd_devs[unit]) == NULL) {
    438 		bp->b_error = ENXIO;
    439 		bp->b_flags |= B_ERROR;
    440 		goto done;
    441 	}
    442 	sc = (void *)rd->sc_dev.dv_parent;
    443 
    444 	lp = rd->sc_disk.dk_label;
    445 	if ((bounds_check_with_label(bp, lp, 1)) <= 0)
    446 		goto done;
    447 
    448 	if (bp->b_bcount == 0)
    449 		goto done;
    450 
    451 	part = (bp->b_flags & B_DKLABEL) ? RAW_PART:
    452 	    DISKPART(vdev_rdev(bp->b_devvp));
    453 
    454 	bp->b_rawblkno =
    455 	    bp->b_blkno + lp->d_partitions[part].p_offset;
    456 	bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
    457 
    458 	s = splbio();
    459 	disksort_cylinder(&sc->sc_q, bp);
    460 	if (inq == 0) {
    461 		inq = 1;
    462 		vsbus_dma_start(&sc->sc_vd);
    463 	}
    464 	splx(s);
    465 	return;
    466 
    467 done:	biodone(bp);
    468 }
    469 
    470 void
    471 hdc_qstart(void *arg)
    472 {
    473 	struct hdcsoftc *sc = arg;
    474 
    475 	inq = 0;
    476 
    477 	hdcstart(sc, 0);
    478 	if (BUFQ_FIRST(&sc->sc_q)) {
    479 		vsbus_dma_start(&sc->sc_vd); /* More to go */
    480 		inq = 1;
    481 	}
    482 }
    483 
    484 void
    485 hdcstart(struct hdcsoftc *sc, struct buf *ob)
    486 {
    487 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    488 	struct disklabel *lp;
    489 	struct rdsoftc *rd;
    490 	struct buf *bp;
    491 	int cn, sn, tn, bn, blks;
    492 	volatile char ch;
    493 	dev_t dev;
    494 
    495 	if (sc->sc_active)
    496 		return; /* Already doing something */
    497 
    498 
    499 	if (ob == 0) {
    500 		bp = BUFQ_FIRST(&sc->sc_q);
    501 		if (bp == NULL)
    502 			return; /* Nothing to do */
    503 		BUFQ_REMOVE(&sc->sc_q, bp);
    504 		sc->sc_bufaddr = bp->b_data;
    505 		sc->sc_diskblk = bp->b_rawblkno;
    506 		sc->sc_bytecnt = bp->b_bcount;
    507 		sc->sc_retries = 0;
    508 		bp->b_resid = 0;
    509 	} else
    510 		bp = ob;
    511 
    512 	dev = vdev_rdev(bp->b_devvp);
    513 	rd = rd_cd.cd_devs[DISKUNIT(dev)];
    514 	hdc_rdselect(sc, rd->sc_drive);
    515 	sc->sc_active = bp;
    516 
    517 	bn = sc->sc_diskblk;
    518 	lp = rd->sc_disk.dk_label;
    519         if (bn) {
    520                 cn = bn / lp->d_secpercyl;
    521                 sn = bn % lp->d_secpercyl;
    522                 tn = sn / lp->d_nsectors;
    523                 sn = sn % lp->d_nsectors;
    524         } else
    525                 cn = sn = tn = 0;
    526 
    527 	cn++; /* first cylinder is reserved */
    528 
    529 	bzero(p, sizeof(struct hdc9224_UDCreg));
    530 
    531 	/*
    532 	 * Tricky thing: the controller do itself only increase the sector
    533 	 * number, not the track or cylinder number. Therefore the driver
    534 	 * is not allowed to have transfers that crosses track boundaries.
    535 	 */
    536 	blks = sc->sc_bytecnt/DEV_BSIZE;
    537 	if ((sn + blks) > lp->d_nsectors)
    538 		blks = lp->d_nsectors - sn;
    539 
    540 	p->udc_dsect = sn;
    541 	p->udc_dcyl = cn & 0xff;
    542 	p->udc_dhead = ((cn >> 4) & 0x70) | tn;
    543 	p->udc_scnt = blks;
    544 
    545 	p->udc_rtcnt = UDC_RC_RTRYCNT;
    546 	p->udc_mode = UDC_MD_HDD;
    547 	p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWRFLT;
    548 	hdc_writeregs(sc);
    549 
    550 	/* Count up vars */
    551 	sc->sc_xfer = blks * DEV_BSIZE;
    552 
    553 	ch = HDC_RSTAT; /* Avoid pending interrupts */
    554 	WAIT;
    555 	vsbus_clrintr(sc->sc_intbit); /* Clear pending int's */
    556 
    557 	if (bp->b_flags & B_READ) {
    558 		HDC_WCMD(DKC_CMD_READ_HDD);
    559 	} else {
    560 		vsbus_copyfromproc(bp->b_proc, sc->sc_bufaddr, sc->sc_dmabase,
    561 		    sc->sc_xfer);
    562 		HDC_WCMD(DKC_CMD_WRITE_HDD);
    563 	}
    564 }
    565 
    566 void
    567 rd_readgeom(struct hdcsoftc *sc, struct rdsoftc *rd)
    568 {
    569 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    570 
    571 	hdc_rdselect(sc, rd->sc_drive);		/* select drive right now */
    572 
    573 	bzero(p, sizeof(struct hdc9224_UDCreg));
    574 
    575 	p->udc_scnt  = 1;
    576 	p->udc_rtcnt = UDC_RC_RTRYCNT;
    577 	p->udc_mode  = UDC_MD_HDD;
    578 	p->udc_term  = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWPROT;
    579 	hdc_writeregs(sc);
    580 	sc->sc_status = 0;
    581 	HDC_WCMD(DKC_CMD_READ_HDD|2);
    582 	while ((sc->sc_status & DKC_ST_INTPEND) == 0)
    583 		;
    584 	bcopy(sc->sc_dmabase, &rd->sc_xbn, sizeof(struct rdgeom));
    585 }
    586 
    587 #ifdef RDDEBUG
    588 /*
    589  * display the contents of the on-disk geometry structure
    590  */
    591 void
    592 hdc_printgeom(p)
    593 	struct rdgeom *p;
    594 {
    595 	printf ("**DiskData**	 XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n",
    596 		p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
    597 	printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
    598 		p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
    599 	printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
    600 		p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
    601 	printf ("media-ID: %lx, interleave: %d, headskew: %d, cylskew: %d\n",
    602 		p->media_id, p->interleave, p->headskew, p->cylskew);
    603 	printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
    604 		p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
    605 		p->sync_value);
    606 }
    607 #endif
    608 
    609 /*
    610  * Return the size of a partition, if known, or -1 if not.
    611  */
    612 int
    613 rdsize(dev_t dev)
    614 {
    615 	struct rdsoftc *rd;
    616 	int unit = DISKUNIT(dev);
    617 	int size;
    618 
    619 	if (unit >= rd_cd.cd_ndevs || rd_cd.cd_devs[unit] == 0)
    620 		return -1;
    621 	rd = rd_cd.cd_devs[unit];
    622 	size = rd->sc_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
    623 	    (rd->sc_disk.dk_label->d_secsize / DEV_BSIZE);
    624 
    625 	return (size);
    626 }
    627 
    628 /*
    629  *
    630  */
    631 int
    632 rdopen(struct vnode *devvp, int flag, int fmt, struct proc *p)
    633 {
    634 	struct rdsoftc *rd;
    635 	int unit, part;
    636 	dev_t dev;
    637 
    638 	dev = vdev_rdev(devvp);
    639 	unit = DISKUNIT(dev);
    640 	if (unit >= rd_cd.cd_ndevs)
    641 		return ENXIO;
    642 	rd = rd_cd.cd_devs[unit];
    643 	if (rd == 0)
    644 		return ENXIO;
    645 
    646 	part = DISKPART(dev);
    647 	if (part >= rd->sc_disk.dk_label->d_npartitions)
    648 		return ENXIO;
    649 
    650 	vdev_setprivdata(devvp, rd);
    651 
    652 	switch (fmt) {
    653 	case S_IFCHR:
    654 		rd->sc_disk.dk_copenmask |= (1 << part);
    655 		break;
    656 	case S_IFBLK:
    657 		rd->sc_disk.dk_bopenmask |= (1 << part);
    658 		break;
    659 	}
    660 	rd->sc_disk.dk_openmask =
    661 	    rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
    662 
    663 	return 0;
    664 }
    665 
    666 /*
    667  *
    668  */
    669 int
    670 rdclose(struct vnode *devvp, int flag, int fmt, struct proc *p)
    671 {
    672 	struct rdsoftc *rd;
    673 	int part;
    674 
    675 	rd = vdev_privdata(devvp);
    676 	part = DISKPART(vdev_rdev(devvp));
    677 
    678 	switch (fmt) {
    679 	case S_IFCHR:
    680 		rd->sc_disk.dk_copenmask &= ~(1 << part);
    681 		break;
    682 	case S_IFBLK:
    683 		rd->sc_disk.dk_bopenmask &= ~(1 << part);
    684 		break;
    685 	}
    686 	rd->sc_disk.dk_openmask =
    687 	    rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
    688 
    689 	return (0);
    690 }
    691 
    692 /*
    693  *
    694  */
    695 int
    696 rdioctl(struct vnode *devvp, u_long cmd, caddr_t addr, int flag, struct proc *p)
    697 {
    698 	struct rdsoftc *rd = vdev_privdata(devvp);
    699 	dev_t dev = vdev_rdev(devvp);
    700 	struct disklabel *lp = rd->sc_disk.dk_label;
    701 	int err = 0;
    702 
    703 	switch (cmd) {
    704 	case DIOCGDINFO:
    705 		bcopy(lp, addr, sizeof (struct disklabel));
    706 		break;
    707 
    708 	case DIOCGPART:
    709 		((struct partinfo *)addr)->disklab = lp;
    710 		((struct partinfo *)addr)->part =
    711 		  &lp->d_partitions[DISKPART(dev)];
    712 		break;
    713 
    714 	case DIOCWDINFO:
    715 	case DIOCSDINFO:
    716 		if ((flag & FWRITE) == 0)
    717 			return EBADF;
    718 		else
    719 			err = (cmd == DIOCSDINFO ?
    720 			    setdisklabel(lp, (struct disklabel *)addr, 0, 0) :
    721 			    writedisklabel(devvp, rdstrategy, lp, 0));
    722 		break;
    723 
    724 	case DIOCGDEFLABEL:
    725 		bzero(lp, sizeof(struct disklabel));
    726 		rdmakelabel(lp, &rd->sc_xbn);
    727 		break;
    728 
    729 	case DIOCWLABEL:
    730 		if ((flag & FWRITE) == 0)
    731 			err = EBADF;
    732 		break;
    733 
    734 	default:
    735 		err = ENOTTY;
    736 	}
    737 	return err;
    738 }
    739 
    740 /*
    741  *
    742  */
    743 int
    744 rdread(struct vnode *devvp, struct uio *uio, int flag)
    745 {
    746 	return (physio (rdstrategy, NULL, devvp, B_READ, minphys, uio));
    747 }
    748 
    749 /*
    750  *
    751  */
    752 int
    753 rdwrite(struct vnode *devvp, struct uio *uio, int flag)
    754 {
    755 	return (physio (rdstrategy, NULL, devvp, B_WRITE, minphys, uio));
    756 }
    757 
    758 /*
    759  *
    760  */
    761 int
    762 rddump(dev_t dev, daddr_t daddr, caddr_t addr, size_t size)
    763 {
    764 	return 0;
    765 }
    766 
    767 /*
    768  * we have to wait 0.7 usec between two accesses to any of the
    769  * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
    770  * instruction. Thus the loop-overhead will be enough...
    771  */
    772 static void
    773 hdc_readregs(struct hdcsoftc *sc)
    774 {
    775 	int i;
    776 	char *p;
    777 
    778 	HDC_WCMD(DKC_CMD_SETREGPTR);
    779 	WAIT;
    780 	p = (void*)&sc->sc_sreg;
    781 	for (i=0; i<10; i++) {
    782 		*p++ = HDC_RREG;	/* dkc_reg auto-increments */
    783 		WAIT;
    784 	}
    785 }
    786 
    787 static void
    788 hdc_writeregs(struct hdcsoftc *sc)
    789 {
    790 	int i;
    791 	char *p;
    792 
    793 	HDC_WCMD(DKC_CMD_SETREGPTR);
    794 	p = (void*)&sc->sc_creg;
    795 	for (i=0; i<10; i++) {
    796 		HDC_WREG(*p++);	/* dkc_reg auto-increments */
    797 		WAIT;
    798 	}
    799 }
    800 
    801 /*
    802  * hdc_command() issues a command and polls the intreq-register
    803  * to find when command has completed
    804  */
    805 int
    806 hdc_command(struct hdcsoftc *sc, int cmd)
    807 {
    808 	hdc_writeregs(sc);		/* write the prepared registers */
    809 	HDC_WCMD(cmd);
    810 	WAIT;
    811 	return (0);
    812 }
    813 
    814 int
    815 hdc_rdselect(struct hdcsoftc *sc, int unit)
    816 {
    817 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    818 	int error;
    819 
    820 	/*
    821 	 * bring "creg" in some known-to-work state and
    822 	 * select the drive with the DRIVE SELECT command.
    823 	 */
    824 	bzero(p, sizeof(struct hdc9224_UDCreg));
    825 
    826 	p->udc_rtcnt = UDC_RC_HDD_READ;
    827 	p->udc_mode  = UDC_MD_HDD;
    828 	p->udc_term  = UDC_TC_HDD;
    829 
    830 	error = hdc_command(sc, DKC_CMD_DRSEL_HDD | unit);
    831 
    832 	return (error);
    833 }
    834 
    835 void
    836 rdmakelabel(struct disklabel *dl, struct rdgeom *g)
    837 {
    838 	int n, p = 0;
    839 
    840 	dl->d_bbsize = BBSIZE;
    841 	dl->d_sbsize = SBSIZE;
    842 	dl->d_typename[p++] = MSCP_MID_CHAR(2, g->media_id);
    843 	dl->d_typename[p++] = MSCP_MID_CHAR(1, g->media_id);
    844 	if (MSCP_MID_ECH(0, g->media_id))
    845 		dl->d_typename[p++] = MSCP_MID_CHAR(0, g->media_id);
    846 	n = MSCP_MID_NUM(g->media_id);
    847 	if (n > 99) {
    848 		dl->d_typename[p++] = '1';
    849 		n -= 100;
    850 	}
    851 	if (n > 9) {
    852 		dl->d_typename[p++] = (n / 10) + '0';
    853 		n %= 10;
    854 	}
    855 	dl->d_typename[p++] = n + '0';
    856 	dl->d_typename[p] = 0;
    857 	dl->d_type = DTYPE_MSCP; /* XXX - what to use here??? */
    858 	dl->d_rpm = 3600;
    859 	dl->d_secsize = DEV_BSIZE;
    860 
    861 	dl->d_secperunit = g->lbn_count;
    862 	dl->d_nsectors = g->nspt;
    863 	dl->d_ntracks = g->ntracks;
    864 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
    865 	dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
    866 
    867 	dl->d_npartitions = MAXPARTITIONS;
    868 	dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
    869 	    dl->d_secperunit;
    870 	dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
    871 	dl->d_interleave = dl->d_headswitch = 1;
    872 	dl->d_magic = dl->d_magic2 = DISKMAGIC;
    873 	dl->d_checksum = dkcksum(dl);
    874 }
    875