Home | History | Annotate | Line # | Download | only in vsa
hdc9224.c revision 1.10
      1 /*	$NetBSD: hdc9224.c,v 1.10 2000/06/25 16:00:44 ragge 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 
     71 #include <vm/vm.h>
     72 #include <vm/vm_kern.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 	volatile u_char sc_status;	/* last status from interrupt */
    156 };
    157 
    158 struct hdc_attach_args {
    159 	int ha_drive;
    160 };
    161 
    162 /*
    163  * prototypes for (almost) all the internal routines
    164  */
    165 static	int hdcmatch(struct device *, struct cfdata *, void *);
    166 static	void hdcattach(struct device *, struct device *, void *);
    167 static	int hdcprint(void *, const char *);
    168 static	int rdmatch(struct device *, struct cfdata *, void *);
    169 static	void rdattach(struct device *, struct device *, void *);
    170 static	void hdcintr(void *);
    171 static	int hdc_command(struct hdcsoftc *, int);
    172 static	void rd_readgeom(struct hdcsoftc *, struct rdsoftc *);
    173 #ifdef RDDEBUG
    174 static	void hdc_printgeom( struct rdgeom *);
    175 #endif
    176 static	void hdc_writeregs(struct hdcsoftc *);
    177 static	void hdcstart(struct hdcsoftc *, struct buf *);
    178 static	int hdc_rdselect(struct hdcsoftc *, int);
    179 static	void rdmakelabel(struct disklabel *, struct rdgeom *);
    180 static	void hdc_writeregs(struct hdcsoftc *);
    181 static	void hdc_readregs(struct hdcsoftc *);
    182 static	void hdc_qstart(void *);
    183 
    184 bdev_decl(rd);
    185 cdev_decl(rd);
    186 
    187 struct	cfattach hdc_ca = {
    188 	sizeof(struct hdcsoftc), hdcmatch, hdcattach
    189 };
    190 
    191 struct	cfattach rd_ca = {
    192 	sizeof(struct rdsoftc), rdmatch, rdattach
    193 };
    194 
    195 
    196 /* At least 0.7 uS between register accesses */
    197 static int rd_dmasize, inq = 0;
    198 static int u;
    199 #define	WAIT	asm("movl _u,_u;movl _u,_u;movl _u,_u; movl _u,_u")
    200 
    201 #define	HDC_WREG(x)	*(volatile char *)(sc->sc_regs) = (x)
    202 #define	HDC_RREG	*(volatile char *)(sc->sc_regs)
    203 #define	HDC_WCMD(x)	*(volatile char *)(sc->sc_regs + 4) = (x)
    204 #define	HDC_RSTAT	*(volatile char *)(sc->sc_regs + 4)
    205 
    206 /*
    207  * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
    208  * thus we probe for the existence of the controller and reset it.
    209  * NB: we can't initialize the controller yet, since space for hdcsoftc
    210  *     is not yet allocated. Thus we do this in hdcattach()...
    211  */
    212 int
    213 hdcmatch(struct device *parent, struct cfdata *cf, void *aux)
    214 {
    215 	struct vsbus_attach_args *va = aux;
    216 	volatile char *hdc_csr = (char *)va->va_addr;
    217 	int i;
    218 
    219 	u = 8; /* !!! - GCC */
    220 
    221 	if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46
    222 	    || vax_boardtype == VAX_BTYP_48)
    223 		return 0;
    224 
    225 	hdc_csr[4] = DKC_CMD_RESET; /* reset chip */
    226 	for (i = 0; i < 1000; i++) {
    227 		DELAY(1000);
    228 		if (hdc_csr[4] & DKC_ST_DONE)
    229 			break;
    230 	}
    231 	if (i == 100)
    232 		return 0; /* No response to reset */
    233 
    234 	hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM;
    235 	WAIT;
    236 	hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE;
    237 	WAIT;
    238 	hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */
    239 	DELAY(1000);
    240 	return (1);
    241 }
    242 
    243 int
    244 hdcprint(void *aux, const char *name)
    245 {
    246 	struct hdc_attach_args *ha = aux;
    247 
    248 	if (name)
    249 		printf ("RD?? at %s drive %d", name, ha->ha_drive);
    250 	return UNCONF;
    251 }
    252 
    253 /*
    254  * hdc_attach() probes for all possible devices
    255  */
    256 void
    257 hdcattach(struct device *parent, struct device *self, void *aux)
    258 {
    259 	struct vsbus_attach_args *va = aux;
    260 	struct hdcsoftc *sc = (void *)self;
    261 	struct hdc_attach_args ha;
    262 	int status, i;
    263 
    264 	printf ("\n");
    265 	/*
    266 	 * Get interrupt vector, enable instrumentation.
    267 	 */
    268 	scb_vecalloc(va->va_cvec, hdcintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
    269 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    270 	    self->dv_xname, "intr");
    271 
    272 	sc->sc_regs = vax_map_physmem(va->va_paddr, 1);
    273 	sc->sc_dmabase = (caddr_t)va->va_dmaaddr;
    274 	sc->sc_dmasize = va->va_dmasize;
    275 	rd_dmasize = min(MAXPHYS, sc->sc_dmasize); /* Used in rd_minphys */
    276 
    277 	sc->sc_vd.vd_go = hdc_qstart;
    278 	sc->sc_vd.vd_arg = sc;
    279 	/*
    280 	 * Reset controller.
    281 	 */
    282 	HDC_WCMD(DKC_CMD_RESET);
    283 	DELAY(1000);
    284 	status = HDC_RSTAT;
    285 	if (status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
    286 		printf("%s: RESET failed,  status 0x%x\n",
    287 			sc->sc_dev.dv_xname, status);
    288 		return;
    289 	}
    290 	BUFQ_INIT(&sc->sc_q);
    291 
    292 	/*
    293 	 * now probe for all possible hard drives
    294 	 */
    295 	for (i = 0; i < 4; i++) {
    296 		if (i == 2) /* Floppy, needs special handling */
    297 			continue;
    298 		HDC_WCMD(DKC_CMD_DRSELECT | i);
    299 		DELAY(1000);
    300 		status = HDC_RSTAT;
    301 		ha.ha_drive = i;
    302 		if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS)
    303 			config_found(self, (void *)&ha, hdcprint);
    304 	}
    305 }
    306 
    307 /*
    308  * rdmatch() probes for the existence of a RD-type disk/floppy
    309  */
    310 int
    311 rdmatch(parent, cf, aux)
    312 	struct device *parent;
    313 	struct cfdata *cf;
    314 	void *aux;
    315 {
    316 	struct hdc_attach_args *ha = aux;
    317 
    318 	if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT &&
    319 	    cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive)
    320 		return 0;
    321 
    322 	if (ha->ha_drive == 2) /* Always floppy, not supported */
    323 		return 0;
    324 
    325 	return 1;
    326 }
    327 
    328 #define	RDMAJOR 19
    329 
    330 void
    331 rdattach(struct device *parent, struct device *self, void *aux)
    332 {
    333 	struct hdcsoftc *sc = (void*)parent;
    334 	struct rdsoftc *rd = (void*)self;
    335 	struct hdc_attach_args *ha = aux;
    336 	struct disklabel *dl;
    337 	char *msg;
    338 
    339 	rd->sc_drive = ha->ha_drive;
    340 	/*
    341 	 * Initialize and attach the disk structure.
    342 	 */
    343 	rd->sc_disk.dk_name = rd->sc_dev.dv_xname;
    344 	disk_attach(&rd->sc_disk);
    345 
    346 	/*
    347 	 * if it's not a floppy then evaluate the on-disk geometry.
    348 	 * if neccessary correct the label...
    349 	 */
    350 	rd_readgeom(sc, rd);
    351 	disk_printtype(rd->sc_drive, rd->sc_xbn.media_id);
    352 	dl = rd->sc_disk.dk_label;
    353 	rdmakelabel(dl, &rd->sc_xbn);
    354 	printf("%s", rd->sc_dev.dv_xname);
    355 	msg = readdisklabel(MAKEDISKDEV(RDMAJOR, rd->sc_dev.dv_unit, RAW_PART),
    356 	    rdstrategy, dl, NULL);
    357 	if (msg)
    358 		printf(": %s", msg);
    359 	printf(": size %d sectors\n", dl->d_secperunit);
    360 #ifdef RDDEBUG
    361 	hdc_printgeom(&rd->sc_xbn);
    362 #endif
    363 }
    364 
    365 void
    366 hdcintr(void *arg)
    367 {
    368 	struct hdcsoftc *sc = arg;
    369 	struct buf *bp;
    370 
    371 	sc->sc_status = HDC_RSTAT;
    372 	if (sc->sc_active == 0)
    373 		return; /* Complain? */
    374 
    375 	if ((sc->sc_status & (DKC_ST_INTPEND|DKC_ST_DONE)) !=
    376 	    (DKC_ST_INTPEND|DKC_ST_DONE))
    377 		return; /* Why spurious ints sometimes??? */
    378 
    379 	bp = sc->sc_active;
    380 	sc->sc_active = 0;
    381 	if ((sc->sc_status & DKC_ST_TERMCOD) != DKC_TC_SUCCESS) {
    382 		int i;
    383 		u_char *g = (u_char *)&sc->sc_sreg;
    384 		printf("%s: failed, status 0x%x\n",
    385 		    sc->sc_dev.dv_xname, sc->sc_status);
    386 		hdc_readregs(sc);
    387 		for (i = 0; i < 10; i++)
    388 			printf("%i: %x\n", i, g[i]);
    389 		bp->b_flags |= B_ERROR;
    390 		bp->b_error = ENXIO;
    391 		bp->b_resid = bp->b_bcount;
    392 		biodone(bp);
    393 		vsbus_dma_intr();
    394 		return;
    395 	}
    396 
    397 	if (bp->b_flags & B_READ) {
    398 		vsbus_copytoproc(bp->b_proc, sc->sc_dmabase, sc->sc_bufaddr,
    399 		    sc->sc_xfer);
    400 	}
    401 	sc->sc_bufaddr += sc->sc_xfer;
    402 
    403 	if (sc->sc_bytecnt == 0) { /* Finished transfer */
    404 		biodone(bp);
    405 		vsbus_dma_intr();
    406 	} else
    407 		hdcstart(sc, bp);
    408 }
    409 
    410 /*
    411  *
    412  */
    413 void
    414 rdstrategy(struct buf *bp)
    415 {
    416 	struct rdsoftc *rd;
    417 	struct hdcsoftc *sc;
    418 	struct disklabel *lp;
    419 	int unit, s;
    420 
    421 	unit = DISKUNIT(bp->b_dev);
    422 	if (unit > rd_cd.cd_ndevs || (rd = rd_cd.cd_devs[unit]) == NULL) {
    423 		bp->b_error = ENXIO;
    424 		bp->b_flags |= B_ERROR;
    425 		goto done;
    426 	}
    427 	sc = (void *)rd->sc_dev.dv_parent;
    428 
    429 	lp = rd->sc_disk.dk_label;
    430 	if ((bounds_check_with_label(bp, lp, 1)) <= 0)
    431 		goto done;
    432 
    433 	if (bp->b_bcount == 0)
    434 		goto done;
    435 
    436 	bp->b_rawblkno =
    437 	    bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    438 	bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
    439 
    440 	s = splimp();
    441 	disksort_cylinder(&sc->sc_q, bp);
    442 	if (inq == 0) {
    443 		inq = 1;
    444 		vsbus_dma_start(&sc->sc_vd);
    445 	}
    446 	splx(s);
    447 	return;
    448 
    449 done:	biodone(bp);
    450 }
    451 
    452 void
    453 hdc_qstart(void *arg)
    454 {
    455 	struct hdcsoftc *sc = arg;
    456 
    457 	inq = 0;
    458 
    459 	hdcstart(sc, 0);
    460 	if (BUFQ_FIRST(&sc->sc_q)) {
    461 		vsbus_dma_start(&sc->sc_vd); /* More to go */
    462 		inq = 1;
    463 	}
    464 }
    465 
    466 void
    467 hdcstart(struct hdcsoftc *sc, struct buf *ob)
    468 {
    469 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    470 	struct disklabel *lp;
    471 	struct rdsoftc *rd;
    472 	struct buf *bp;
    473 	int cn, sn, tn, bn, blks;
    474 
    475 	if (sc->sc_active)
    476 		return; /* Already doing something */
    477 
    478 
    479 	if (ob == 0) {
    480 		bp = BUFQ_FIRST(&sc->sc_q);
    481 		if (bp == NULL)
    482 			return; /* Nothing to do */
    483 		BUFQ_REMOVE(&sc->sc_q, bp);
    484 		sc->sc_bufaddr = bp->b_data;
    485 		sc->sc_diskblk = bp->b_rawblkno;
    486 		sc->sc_bytecnt = bp->b_bcount;
    487 		bp->b_resid = 0;
    488 	} else
    489 		bp = ob;
    490 
    491 	rd = rd_cd.cd_devs[DISKUNIT(bp->b_dev)];
    492 	hdc_rdselect(sc, rd->sc_drive);
    493 	sc->sc_active = bp;
    494 
    495 	bn = sc->sc_diskblk;
    496 	lp = rd->sc_disk.dk_label;
    497         if (bn) {
    498                 cn = bn / lp->d_secpercyl;
    499                 sn = bn % lp->d_secpercyl;
    500                 tn = sn / lp->d_nsectors;
    501                 sn = sn % lp->d_nsectors;
    502         } else
    503                 cn = sn = tn = 0;
    504 
    505 	cn++; /* first cylinder is reserved */
    506 
    507 	bzero(p, sizeof(struct hdc9224_UDCreg));
    508 
    509 	/*
    510 	 * Tricky thing: the controller do itself only increase the sector
    511 	 * number, not the track or cylinder number. Therefore the driver
    512 	 * is not allowed to have transfers that crosses track boundaries.
    513 	 */
    514 	blks = sc->sc_bytecnt/DEV_BSIZE;
    515 	if ((sn + blks) > lp->d_nsectors)
    516 		blks = lp->d_nsectors - sn;
    517 
    518 	p->udc_dsect = sn;
    519 	p->udc_dcyl = cn & 0xff;
    520 	p->udc_dhead = ((cn >> 4) & 0x70) | tn;
    521 	p->udc_scnt = blks;
    522 
    523 	p->udc_rtcnt = UDC_RC_RTRYCNT;
    524 	p->udc_mode = UDC_MD_HDD;
    525 	p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWRFLT;
    526 	hdc_writeregs(sc);
    527 
    528 	/* Count up vars */
    529 	sc->sc_xfer = blks * DEV_BSIZE;
    530 	sc->sc_diskblk += blks;
    531 	sc->sc_bytecnt -= sc->sc_xfer;
    532 
    533 	if (bp->b_flags & B_READ) {
    534 		HDC_WCMD(DKC_CMD_READ_HDD);
    535 	} else {
    536 		vsbus_copyfromproc(bp->b_proc, sc->sc_bufaddr, sc->sc_dmabase,
    537 		    sc->sc_xfer);
    538 		HDC_WCMD(DKC_CMD_WRITE_HDD);
    539 	}
    540 }
    541 
    542 void
    543 rd_readgeom(struct hdcsoftc *sc, struct rdsoftc *rd)
    544 {
    545 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    546 
    547 	hdc_rdselect(sc, rd->sc_drive);		/* select drive right now */
    548 
    549 	bzero(p, sizeof(struct hdc9224_UDCreg));
    550 
    551 	p->udc_scnt  = 1;
    552 	p->udc_rtcnt = UDC_RC_RTRYCNT;
    553 	p->udc_mode  = UDC_MD_HDD;
    554 	p->udc_term  = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWPROT;
    555 	hdc_writeregs(sc);
    556 	sc->sc_status = 0;
    557 	HDC_WCMD(DKC_CMD_READ_HDD|2);
    558 	while ((sc->sc_status & DKC_ST_INTPEND) == 0)
    559 		;
    560 	bcopy(sc->sc_dmabase, &rd->sc_xbn, sizeof(struct rdgeom));
    561 }
    562 
    563 #ifdef RDDEBUG
    564 /*
    565  * display the contents of the on-disk geometry structure
    566  */
    567 void
    568 hdc_printgeom(p)
    569 	struct rdgeom *p;
    570 {
    571 	printf ("**DiskData**	 XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n",
    572 		p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
    573 	printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
    574 		p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
    575 	printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
    576 		p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
    577 	printf ("media-ID: %lx, interleave: %d, headskew: %d, cylskew: %d\n",
    578 		p->media_id, p->interleave, p->headskew, p->cylskew);
    579 	printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
    580 		p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
    581 		p->sync_value);
    582 }
    583 #endif
    584 
    585 /*
    586  * Return the size of a partition, if known, or -1 if not.
    587  */
    588 int
    589 rdsize(dev_t dev)
    590 {
    591 	struct rdsoftc *rd;
    592 	int unit = DISKUNIT(dev);
    593 	int size;
    594 
    595 	if (unit >= rd_cd.cd_ndevs || rd_cd.cd_devs[unit] == 0)
    596 		return -1;
    597 	rd = rd_cd.cd_devs[unit];
    598 	size = rd->sc_disk.dk_label->d_partitions[DISKPART(dev)].p_size *
    599 	    (rd->sc_disk.dk_label->d_secsize / DEV_BSIZE);
    600 
    601 	return (size);
    602 }
    603 
    604 /*
    605  *
    606  */
    607 int
    608 rdopen(dev_t dev, int flag, int fmt, struct proc *p)
    609 {
    610 	struct rdsoftc *rd;
    611 	int unit, part;
    612 
    613 	unit = DISKUNIT(dev);
    614 	if (unit >= rd_cd.cd_ndevs)
    615 		return ENXIO;
    616 	rd = rd_cd.cd_devs[unit];
    617 	if (rd == 0)
    618 		return ENXIO;
    619 
    620 	part = DISKPART(dev);
    621 	if (part >= rd->sc_disk.dk_label->d_npartitions)
    622 		return ENXIO;
    623 
    624 	switch (fmt) {
    625 	case S_IFCHR:
    626 		rd->sc_disk.dk_copenmask |= (1 << part);
    627 		break;
    628 	case S_IFBLK:
    629 		rd->sc_disk.dk_bopenmask |= (1 << part);
    630 		break;
    631 	}
    632 	rd->sc_disk.dk_openmask =
    633 	    rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
    634 
    635 	return 0;
    636 }
    637 
    638 /*
    639  *
    640  */
    641 int
    642 rdclose(dev_t dev, int flag, int fmt, struct proc *p)
    643 {
    644 	struct rdsoftc *rd;
    645 	int part;
    646 
    647 	rd = rd_cd.cd_devs[DISKUNIT(dev)];
    648 	part = DISKPART(dev);
    649 
    650 	switch (fmt) {
    651 	case S_IFCHR:
    652 		rd->sc_disk.dk_copenmask &= ~(1 << part);
    653 		break;
    654 	case S_IFBLK:
    655 		rd->sc_disk.dk_bopenmask &= ~(1 << part);
    656 		break;
    657 	}
    658 	rd->sc_disk.dk_openmask =
    659 	    rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
    660 
    661 	return (0);
    662 }
    663 
    664 /*
    665  *
    666  */
    667 int
    668 rdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p)
    669 {
    670 	struct rdsoftc *rd = rd_cd.cd_devs[DISKUNIT(dev)];
    671 	struct disklabel *lp = rd->sc_disk.dk_label;
    672 	int err = 0;
    673 
    674 	switch (cmd) {
    675 	case DIOCGDINFO:
    676 		bcopy(lp, addr, sizeof (struct disklabel));
    677 		break;
    678 
    679 	case DIOCGPART:
    680 		((struct partinfo *)addr)->disklab = lp;
    681 		((struct partinfo *)addr)->part =
    682 		  &lp->d_partitions[DISKPART(dev)];
    683 		break;
    684 
    685 	case DIOCWDINFO:
    686 	case DIOCSDINFO:
    687 		if ((flag & FWRITE) == 0)
    688 			return EBADF;
    689 		else
    690 			err = (cmd == DIOCSDINFO ?
    691 			    setdisklabel(lp, (struct disklabel *)addr, 0, 0) :
    692 			    writedisklabel(dev, rdstrategy, lp, 0));
    693 		break;
    694 
    695 	case DIOCGDEFLABEL:
    696 		bzero(lp, sizeof(struct disklabel));
    697 		rdmakelabel(lp, &rd->sc_xbn);
    698 		break;
    699 
    700 	case DIOCWLABEL:
    701 		if ((flag & FWRITE) == 0)
    702 			err = EBADF;
    703 		break;
    704 
    705 	default:
    706 		err = ENOTTY;
    707 	}
    708 	return err;
    709 }
    710 
    711 /*
    712  *
    713  */
    714 int
    715 rdread(dev_t dev, struct uio *uio, int flag)
    716 {
    717 	return (physio (rdstrategy, NULL, dev, B_READ, minphys, uio));
    718 }
    719 
    720 /*
    721  *
    722  */
    723 int
    724 rdwrite(dev_t dev, struct uio *uio, int flag)
    725 {
    726 	return (physio (rdstrategy, NULL, dev, B_WRITE, minphys, uio));
    727 }
    728 
    729 /*
    730  *
    731  */
    732 int
    733 rddump(dev_t dev, daddr_t daddr, caddr_t addr, size_t size)
    734 {
    735 	return 0;
    736 }
    737 
    738 /*
    739  * we have to wait 0.7 usec between two accesses to any of the
    740  * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
    741  * instruction. Thus the loop-overhead will be enough...
    742  */
    743 static void
    744 hdc_readregs(struct hdcsoftc *sc)
    745 {
    746 	int i;
    747 	char *p;
    748 
    749 	HDC_WCMD(DKC_CMD_SETREGPTR);
    750 	WAIT;
    751 	p = (void*)&sc->sc_sreg;
    752 	for (i=0; i<10; i++) {
    753 		*p++ = HDC_RREG;	/* dkc_reg auto-increments */
    754 		WAIT;
    755 	}
    756 }
    757 
    758 static void
    759 hdc_writeregs(struct hdcsoftc *sc)
    760 {
    761 	int i;
    762 	char *p;
    763 
    764 	HDC_WCMD(DKC_CMD_SETREGPTR);
    765 	p = (void*)&sc->sc_creg;
    766 	for (i=0; i<10; i++) {
    767 		HDC_WREG(*p++);	/* dkc_reg auto-increments */
    768 		WAIT;
    769 	}
    770 }
    771 
    772 /*
    773  * hdc_command() issues a command and polls the intreq-register
    774  * to find when command has completed
    775  */
    776 int
    777 hdc_command(struct hdcsoftc *sc, int cmd)
    778 {
    779 	hdc_writeregs(sc);		/* write the prepared registers */
    780 	HDC_WCMD(cmd);
    781 	WAIT;
    782 	return (0);
    783 }
    784 
    785 int
    786 hdc_rdselect(struct hdcsoftc *sc, int unit)
    787 {
    788 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    789 	int error;
    790 
    791 	/*
    792 	 * bring "creg" in some known-to-work state and
    793 	 * select the drive with the DRIVE SELECT command.
    794 	 */
    795 	bzero(p, sizeof(struct hdc9224_UDCreg));
    796 
    797 	p->udc_rtcnt = UDC_RC_HDD_READ;
    798 	p->udc_mode  = UDC_MD_HDD;
    799 	p->udc_term  = UDC_TC_HDD;
    800 
    801 	error = hdc_command(sc, DKC_CMD_DRSEL_HDD | unit);
    802 
    803 	return (error);
    804 }
    805 
    806 void
    807 rdmakelabel(struct disklabel *dl, struct rdgeom *g)
    808 {
    809 	int n, p = 0;
    810 
    811 	dl->d_bbsize = BBSIZE;
    812 	dl->d_sbsize = SBSIZE;
    813 	dl->d_typename[p++] = MSCP_MID_CHAR(2, g->media_id);
    814 	dl->d_typename[p++] = MSCP_MID_CHAR(1, g->media_id);
    815 	if (MSCP_MID_ECH(0, g->media_id))
    816 		dl->d_typename[p++] = MSCP_MID_CHAR(0, g->media_id);
    817 	n = MSCP_MID_NUM(g->media_id);
    818 	if (n > 99) {
    819 		dl->d_typename[p++] = '1';
    820 		n -= 100;
    821 	}
    822 	if (n > 9) {
    823 		dl->d_typename[p++] = (n / 10) + '0';
    824 		n %= 10;
    825 	}
    826 	dl->d_typename[p++] = n + '0';
    827 	dl->d_typename[p] = 0;
    828 	dl->d_type = DTYPE_MSCP; /* XXX - what to use here??? */
    829 	dl->d_rpm = 3600;
    830 	dl->d_secsize = DEV_BSIZE;
    831 
    832 	dl->d_secperunit = g->lbn_count;
    833 	dl->d_nsectors = g->nspt;
    834 	dl->d_ntracks = g->ntracks;
    835 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
    836 	dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
    837 
    838 	dl->d_npartitions = MAXPARTITIONS;
    839 	dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
    840 	    dl->d_secperunit;
    841 	dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
    842 	dl->d_interleave = dl->d_headswitch = 1;
    843 	dl->d_magic = dl->d_magic2 = DISKMAGIC;
    844 	dl->d_checksum = dkcksum(dl);
    845 }
    846