Home | History | Annotate | Line # | Download | only in vsa
hdc9224.c revision 1.39
      1 /*	$NetBSD: hdc9224.c,v 1.39 2007/03/04 19:21:56 christos 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/cdefs.h>
     54 __KERNEL_RCSID(0, "$NetBSD: hdc9224.c,v 1.39 2007/03/04 19:21:56 christos Exp $");
     55 
     56 #include <sys/param.h>
     57 #include <sys/systm.h>
     58 #include <sys/kernel.h>
     59 #include <sys/conf.h>
     60 #include <sys/file.h>
     61 #include <sys/stat.h>
     62 #include <sys/ioctl.h>
     63 #include <sys/buf.h>
     64 #include <sys/bufq.h>
     65 #include <sys/proc.h>
     66 #include <sys/user.h>
     67 #include <sys/device.h>
     68 #include <sys/disklabel.h>
     69 #include <sys/disk.h>
     70 #include <sys/syslog.h>
     71 #include <sys/reboot.h>
     72 
     73 #include <uvm/uvm_extern.h>
     74 
     75 #include <ufs/ufs/dinode.h> /* For BBSIZE */
     76 #include <ufs/ffs/fs.h>
     77 
     78 #include <machine/pte.h>
     79 #include <machine/sid.h>
     80 #include <machine/cpu.h>
     81 #include <machine/uvax.h>
     82 #include <machine/ka410.h>
     83 #include <machine/vsbus.h>
     84 #include <machine/rpb.h>
     85 #include <machine/scb.h>
     86 
     87 #include <dev/mscp/mscp.h> /* For DEC disk encoding */
     88 
     89 #include <vax/vsa/hdc9224.h>
     90 
     91 #include "ioconf.h"
     92 #include "locators.h"
     93 
     94 
     95 /*
     96  * on-disk geometry block
     97  */
     98 #define _aP	__attribute__ ((packed))	/* force byte-alignment */
     99 struct rdgeom {
    100 	char mbz[10];		/* 10 bytes of zero */
    101 	long xbn_count _aP;	/* number of XBNs */
    102 	long dbn_count _aP;	/* number of DBNs */
    103 	long lbn_count _aP;	/* number of LBNs (Logical-Block-Numbers) */
    104 	long rbn_count _aP;	/* number of RBNs (Replacement-Block-Numbers) */
    105 	short nspt;		/* number of sectors per track */
    106 	short ntracks;		/* number of tracks */
    107 	short ncylinders;	/* number of cylinders */
    108 	short precomp;		/* first cylinder for write precompensation */
    109 	short reduced;		/* first cylinder for reduced write current */
    110 	short seek_rate;	/* seek rate or zero for buffered seeks */
    111 	short crc_eec;		/* 0 if CRC, 1 if ECC is being used */
    112 	short rct;		/* "replacement control table" (RCT) */
    113 	short rct_ncopies;	/* number of copies of the RCT */
    114 	long	media_id _aP;	/* media identifier */
    115 	short interleave;	/* sector-to-sector interleave */
    116 	short headskew;		/* head-to-head skew */
    117 	short cylskew;		/* cylinder-to-cylinder skew */
    118 	short gap0_size;	/* size of GAP 0 in the MFM format */
    119 	short gap1_size;	/* size of GAP 1 in the MFM format */
    120 	short gap2_size;	/* size of GAP 2 in the MFM format */
    121 	short gap3_size;	/* size of GAP 3 in the MFM format */
    122 	short sync_value;	/* sync value used when formatting */
    123 	char	reserved[32];	/* reserved for use by the RQDX formatter */
    124 	short serial_number;	/* serial number */
    125 #if 0	/* we don't need these 412 useless bytes ... */
    126 	char	fill[412-2];	/* Filler bytes to the end of the block */
    127 	short checksum;	/* checksum over the XBN */
    128 #endif
    129 };
    130 
    131 /*
    132  * Software status
    133  */
    134 struct	rdsoftc {
    135 	struct device sc_dev;		/* must be here! (pseudo-OOP:) */
    136 	struct disk sc_disk;		/* disklabel etc. */
    137 	struct rdgeom sc_xbn;		/* on-disk geometry information */
    138 	int sc_drive;		/* physical unit number */
    139 };
    140 
    141 struct	hdcsoftc {
    142 	struct device sc_dev;		/* must be here (pseudo-OOP:) */
    143 	struct evcnt sc_intrcnt;
    144 	struct vsbus_dma sc_vd;
    145 	vaddr_t sc_regs;		/* register addresses */
    146 	struct bufq_state *sc_q;
    147 	struct buf *sc_active;
    148 	struct hdc9224_UDCreg sc_creg;	/* (command) registers to be written */
    149 	struct hdc9224_UDCreg sc_sreg;	/* (status) registers being read */
    150 	void *	sc_dmabase;		/* */
    151 	int	sc_dmasize;
    152 	void *sc_bufaddr;		/* Current in-core address */
    153 	int sc_diskblk;			/* Current block on disk */
    154 	int sc_bytecnt;			/* How much left to transfer */
    155 	int sc_xfer;			/* Current transfer size */
    156 	int sc_retries;
    157 	volatile u_char sc_status;	/* last status from interrupt */
    158 	char sc_intbit;
    159 };
    160 
    161 struct hdc_attach_args {
    162 	int ha_drive;
    163 };
    164 
    165 /*
    166  * prototypes for (almost) all the internal routines
    167  */
    168 static	int hdcmatch(struct device *, struct cfdata *, void *);
    169 static	void hdcattach(struct device *, struct device *, void *);
    170 static	int hdcprint(void *, const char *);
    171 static	int rdmatch(struct device *, struct cfdata *, void *);
    172 static	void rdattach(struct device *, struct device *, void *);
    173 static	void hdcintr(void *);
    174 static	int hdc_command(struct hdcsoftc *, int);
    175 static	void rd_readgeom(struct hdcsoftc *, struct rdsoftc *);
    176 #ifdef RDDEBUG
    177 static	void hdc_printgeom( struct rdgeom *);
    178 #endif
    179 static	void hdc_writeregs(struct hdcsoftc *);
    180 static	void hdcstart(struct hdcsoftc *, struct buf *);
    181 static	int hdc_rdselect(struct hdcsoftc *, int);
    182 static	void rdmakelabel(struct disklabel *, struct rdgeom *);
    183 static	void hdc_writeregs(struct hdcsoftc *);
    184 static	void hdc_readregs(struct hdcsoftc *);
    185 static	void hdc_qstart(void *);
    186 
    187 CFATTACH_DECL(hdc, sizeof(struct hdcsoftc),
    188     hdcmatch, hdcattach, NULL, NULL);
    189 
    190 CFATTACH_DECL(rd, sizeof(struct rdsoftc),
    191     rdmatch, rdattach, NULL, NULL);
    192 
    193 dev_type_open(rdopen);
    194 dev_type_close(rdclose);
    195 dev_type_read(rdread);
    196 dev_type_write(rdwrite);
    197 dev_type_ioctl(rdioctl);
    198 dev_type_strategy(rdstrategy);
    199 dev_type_size(rdsize);
    200 
    201 const struct bdevsw rd_bdevsw = {
    202 	rdopen, rdclose, rdstrategy, rdioctl, nulldump, rdsize, D_DISK
    203 };
    204 
    205 const struct cdevsw rd_cdevsw = {
    206 	rdopen, rdclose, rdread, rdwrite, rdioctl,
    207 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    208 };
    209 
    210 /* At least 0.7 uS between register accesses */
    211 static int rd_dmasize, inq = 0;
    212 static int u;
    213 #define	WAIT	__asm("movl %0,%0;movl %0,%0;movl %0,%0; movl %0,%0" :: "m"(u))
    214 
    215 #define	HDC_WREG(x)	*(volatile char *)(sc->sc_regs) = (x)
    216 #define	HDC_RREG	*(volatile char *)(sc->sc_regs)
    217 #define	HDC_WCMD(x)	*(volatile char *)(sc->sc_regs + 4) = (x)
    218 #define	HDC_RSTAT	*(volatile char *)(sc->sc_regs + 4)
    219 
    220 /*
    221  * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
    222  * thus we probe for the existence of the controller and reset it.
    223  * NB: we can't initialize the controller yet, since space for hdcsoftc
    224  *     is not yet allocated. Thus we do this in hdcattach()...
    225  */
    226 int
    227 hdcmatch(struct device *parent, struct cfdata *cf, void *aux)
    228 {
    229 	struct vsbus_attach_args *va = aux;
    230 	volatile char *hdc_csr = (char *)va->va_addr;
    231 	int i;
    232 
    233 	u = 8; /* !!! - GCC */
    234 
    235 	if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46
    236 	    || vax_boardtype == VAX_BTYP_48 || vax_boardtype == VAX_BTYP_53)
    237 		return 0;
    238 
    239 	hdc_csr[4] = DKC_CMD_RESET; /* reset chip */
    240 	for (i = 0; i < 1000; i++) {
    241 		DELAY(1000);
    242 		if (hdc_csr[4] & DKC_ST_DONE)
    243 			break;
    244 	}
    245 	if (i == 100)
    246 		return 0; /* No response to reset */
    247 
    248 	hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM;
    249 	WAIT;
    250 	hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE;
    251 	WAIT;
    252 	hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */
    253 	DELAY(1000);
    254 	return (1);
    255 }
    256 
    257 int
    258 hdcprint(void *aux, const char *name)
    259 {
    260 	struct hdc_attach_args *ha = aux;
    261 
    262 	if (name)
    263 		aprint_normal ("RD?? at %s drive %d", name, ha->ha_drive);
    264 	return UNCONF;
    265 }
    266 
    267 /*
    268  * hdc_attach() probes for all possible devices
    269  */
    270 void
    271 hdcattach(struct device *parent, struct device *self, void *aux)
    272 {
    273 	struct vsbus_attach_args *va = aux;
    274 	struct hdcsoftc *sc = (void *)self;
    275 	struct hdc_attach_args ha;
    276 	int status, i;
    277 
    278 	printf ("\n");
    279 	/*
    280 	 * Get interrupt vector, enable instrumentation.
    281 	 */
    282 	scb_vecalloc(va->va_cvec, hdcintr, sc, SCB_ISTACK, &sc->sc_intrcnt);
    283 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    284 	    self->dv_xname, "intr");
    285 
    286 	sc->sc_regs = vax_map_physmem(va->va_paddr, 1);
    287 	sc->sc_dmabase = (void *)va->va_dmaaddr;
    288 	sc->sc_dmasize = va->va_dmasize;
    289 	sc->sc_intbit = va->va_maskno;
    290 	rd_dmasize = min(MAXPHYS, sc->sc_dmasize); /* Used in rd_minphys */
    291 
    292 	sc->sc_vd.vd_go = hdc_qstart;
    293 	sc->sc_vd.vd_arg = sc;
    294 	/*
    295 	 * Reset controller.
    296 	 */
    297 	HDC_WCMD(DKC_CMD_RESET);
    298 	DELAY(1000);
    299 	status = HDC_RSTAT;
    300 	if (status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
    301 		printf("%s: RESET failed,  status 0x%x\n",
    302 			sc->sc_dev.dv_xname, status);
    303 		return;
    304 	}
    305 	bufq_alloc(&sc->sc_q, "disksort", BUFQ_SORT_CYLINDER);
    306 
    307 	/*
    308 	 * now probe for all possible hard drives
    309 	 */
    310 	for (i = 0; i < 4; i++) {
    311 		if (i == 2) /* Floppy, needs special handling */
    312 			continue;
    313 		HDC_WCMD(DKC_CMD_DRSELECT | i);
    314 		DELAY(1000);
    315 		status = HDC_RSTAT;
    316 		ha.ha_drive = i;
    317 		if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS)
    318 			config_found(self, (void *)&ha, hdcprint);
    319 	}
    320 }
    321 
    322 /*
    323  * rdmatch() probes for the existence of a RD-type disk/floppy
    324  */
    325 int
    326 rdmatch(parent, cf, aux)
    327 	struct device *parent;
    328 	struct cfdata *cf;
    329 	void *aux;
    330 {
    331 	struct hdc_attach_args *ha = aux;
    332 
    333 	if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT &&
    334 	    cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive)
    335 		return 0;
    336 
    337 	if (ha->ha_drive == 2) /* Always floppy, not supported */
    338 		return 0;
    339 
    340 	return 1;
    341 }
    342 
    343 void
    344 rdattach(struct device *parent, struct device *self, void *aux)
    345 {
    346 	struct hdcsoftc *sc = (void*)parent;
    347 	struct rdsoftc *rd = (void*)self;
    348 	struct hdc_attach_args *ha = aux;
    349 	struct disklabel *dl;
    350 	const char *msg;
    351 
    352 	rd->sc_drive = ha->ha_drive;
    353 	/*
    354 	 * Initialize and attach the disk structure.
    355 	 */
    356 	rd->sc_disk.dk_name = rd->sc_dev.dv_xname;
    357 	disk_attach(&rd->sc_disk);
    358 
    359 	/*
    360 	 * if it's not a floppy then evaluate the on-disk geometry.
    361 	 * if necessary correct the label...
    362 	 */
    363 	rd_readgeom(sc, rd);
    364 	disk_printtype(rd->sc_drive, rd->sc_xbn.media_id);
    365 	dl = rd->sc_disk.dk_label;
    366 	rdmakelabel(dl, &rd->sc_xbn);
    367 	printf("%s", rd->sc_dev.dv_xname);
    368 	msg = readdisklabel(MAKEDISKDEV(cdevsw_lookup_major(&rd_cdevsw),
    369 					device_unit(&rd->sc_dev), RAW_PART),
    370 			    rdstrategy, dl, NULL);
    371 	if (msg)
    372 		printf(": %s", msg);
    373 	printf(": size %d sectors\n", dl->d_secperunit);
    374 #ifdef RDDEBUG
    375 	hdc_printgeom(&rd->sc_xbn);
    376 #endif
    377 }
    378 
    379 void
    380 hdcintr(void *arg)
    381 {
    382 	struct hdcsoftc *sc = arg;
    383 	struct buf *bp;
    384 
    385 	sc->sc_status = HDC_RSTAT;
    386 	if (sc->sc_active == 0)
    387 		return; /* Complain? */
    388 
    389 	if ((sc->sc_status & (DKC_ST_INTPEND|DKC_ST_DONE)) !=
    390 	    (DKC_ST_INTPEND|DKC_ST_DONE))
    391 		return; /* Why spurious ints sometimes??? */
    392 
    393 	bp = sc->sc_active;
    394 	sc->sc_active = 0;
    395 	if ((sc->sc_status & DKC_ST_TERMCOD) != DKC_TC_SUCCESS) {
    396 		int i;
    397 		u_char *g = (u_char *)&sc->sc_sreg;
    398 
    399 		if (sc->sc_retries++ < 3) { /* Allow 3 retries */
    400 			hdcstart(sc, bp);
    401 			return;
    402 		}
    403 		printf("%s: failed, status 0x%x\n",
    404 		    sc->sc_dev.dv_xname, sc->sc_status);
    405 		hdc_readregs(sc);
    406 		for (i = 0; i < 10; i++)
    407 			printf("%i: %x\n", i, g[i]);
    408 		bp->b_flags |= B_ERROR;
    409 		bp->b_error = ENXIO;
    410 		bp->b_resid = bp->b_bcount;
    411 		biodone(bp);
    412 		vsbus_dma_intr();
    413 		return;
    414 	}
    415 
    416 	if (bp->b_flags & B_READ) {
    417 		vsbus_copytoproc(bp->b_proc, sc->sc_dmabase, sc->sc_bufaddr,
    418 		    sc->sc_xfer);
    419 	}
    420 	sc->sc_diskblk += (sc->sc_xfer/DEV_BSIZE);
    421 	sc->sc_bytecnt -= sc->sc_xfer;
    422 	sc->sc_bufaddr = (char *)sc->sc_bufaddr + sc->sc_xfer;
    423 
    424 	if (sc->sc_bytecnt == 0) { /* Finished transfer */
    425 		biodone(bp);
    426 		vsbus_dma_intr();
    427 	} else
    428 		hdcstart(sc, bp);
    429 }
    430 
    431 /*
    432  *
    433  */
    434 void
    435 rdstrategy(struct buf *bp)
    436 {
    437 	struct rdsoftc *rd;
    438 	struct hdcsoftc *sc;
    439 	struct disklabel *lp;
    440 	int unit, s;
    441 
    442 	unit = DISKUNIT(bp->b_dev);
    443 	if (unit > rd_cd.cd_ndevs || (rd = rd_cd.cd_devs[unit]) == NULL) {
    444 		bp->b_error = ENXIO;
    445 		bp->b_flags |= B_ERROR;
    446 		goto done;
    447 	}
    448 	sc = (void *)device_parent(&rd->sc_dev);
    449 
    450 	lp = rd->sc_disk.dk_label;
    451 	if ((bounds_check_with_label(&rd->sc_disk, bp, 1)) <= 0)
    452 		goto done;
    453 
    454 	if (bp->b_bcount == 0)
    455 		goto done;
    456 
    457 	bp->b_rawblkno =
    458 	    bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    459 	bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl;
    460 
    461 	s = splbio();
    462 	BUFQ_PUT(sc->sc_q, bp);
    463 	if (inq == 0) {
    464 		inq = 1;
    465 		vsbus_dma_start(&sc->sc_vd);
    466 	}
    467 	splx(s);
    468 	return;
    469 
    470 done:	biodone(bp);
    471 }
    472 
    473 void
    474 hdc_qstart(void *arg)
    475 {
    476 	struct hdcsoftc *sc = arg;
    477 
    478 	inq = 0;
    479 
    480 	hdcstart(sc, 0);
    481 	if (BUFQ_PEEK(sc->sc_q)) {
    482 		vsbus_dma_start(&sc->sc_vd); /* More to go */
    483 		inq = 1;
    484 	}
    485 }
    486 
    487 void
    488 hdcstart(struct hdcsoftc *sc, struct buf *ob)
    489 {
    490 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    491 	struct disklabel *lp;
    492 	struct rdsoftc *rd;
    493 	struct buf *bp;
    494 	int cn, sn, tn, bn, blks;
    495 	volatile char ch;
    496 
    497 	if (sc->sc_active)
    498 		return; /* Already doing something */
    499 
    500 
    501 	if (ob == 0) {
    502 		bp = BUFQ_GET(sc->sc_q);
    503 		if (bp == NULL)
    504 			return; /* Nothing to do */
    505 		sc->sc_bufaddr = bp->b_data;
    506 		sc->sc_diskblk = bp->b_rawblkno;
    507 		sc->sc_bytecnt = bp->b_bcount;
    508 		sc->sc_retries = 0;
    509 		bp->b_resid = 0;
    510 	} else
    511 		bp = ob;
    512 
    513 	rd = rd_cd.cd_devs[DISKUNIT(bp->b_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(dev_t dev, int flag, int fmt, struct lwp *l)
    633 {
    634 	struct rdsoftc *rd;
    635 	int unit, part;
    636 
    637 	unit = DISKUNIT(dev);
    638 	if (unit >= rd_cd.cd_ndevs)
    639 		return ENXIO;
    640 	rd = rd_cd.cd_devs[unit];
    641 	if (rd == 0)
    642 		return ENXIO;
    643 
    644 	part = DISKPART(dev);
    645 	if (part >= rd->sc_disk.dk_label->d_npartitions)
    646 		return ENXIO;
    647 
    648 	switch (fmt) {
    649 	case S_IFCHR:
    650 		rd->sc_disk.dk_copenmask |= (1 << part);
    651 		break;
    652 	case S_IFBLK:
    653 		rd->sc_disk.dk_bopenmask |= (1 << part);
    654 		break;
    655 	}
    656 	rd->sc_disk.dk_openmask =
    657 	    rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
    658 
    659 	return 0;
    660 }
    661 
    662 /*
    663  *
    664  */
    665 int
    666 rdclose(dev_t dev, int flag, int fmt, struct lwp *l)
    667 {
    668 	struct rdsoftc *rd;
    669 	int part;
    670 
    671 	rd = rd_cd.cd_devs[DISKUNIT(dev)];
    672 	part = DISKPART(dev);
    673 
    674 	switch (fmt) {
    675 	case S_IFCHR:
    676 		rd->sc_disk.dk_copenmask &= ~(1 << part);
    677 		break;
    678 	case S_IFBLK:
    679 		rd->sc_disk.dk_bopenmask &= ~(1 << part);
    680 		break;
    681 	}
    682 	rd->sc_disk.dk_openmask =
    683 	    rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask;
    684 
    685 	return (0);
    686 }
    687 
    688 /*
    689  *
    690  */
    691 int
    692 rdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
    693 {
    694 	struct rdsoftc *rd = rd_cd.cd_devs[DISKUNIT(dev)];
    695 	struct disklabel *lp = rd->sc_disk.dk_label;
    696 	int err = 0;
    697 
    698 	switch (cmd) {
    699 	case DIOCGDINFO:
    700 		bcopy(lp, addr, sizeof (struct disklabel));
    701 		break;
    702 
    703 	case DIOCGPART:
    704 		((struct partinfo *)addr)->disklab = lp;
    705 		((struct partinfo *)addr)->part =
    706 		  &lp->d_partitions[DISKPART(dev)];
    707 		break;
    708 
    709 	case DIOCWDINFO:
    710 	case DIOCSDINFO:
    711 		if ((flag & FWRITE) == 0)
    712 			return EBADF;
    713 		else
    714 			err = (cmd == DIOCSDINFO ?
    715 			    setdisklabel(lp, (struct disklabel *)addr, 0, 0) :
    716 			    writedisklabel(dev, rdstrategy, lp, 0));
    717 		break;
    718 
    719 	case DIOCGDEFLABEL:
    720 		bzero(lp, sizeof(struct disklabel));
    721 		rdmakelabel(lp, &rd->sc_xbn);
    722 		break;
    723 
    724 	case DIOCWLABEL:
    725 		if ((flag & FWRITE) == 0)
    726 			err = EBADF;
    727 		break;
    728 
    729 	default:
    730 		err = ENOTTY;
    731 	}
    732 	return err;
    733 }
    734 
    735 /*
    736  *
    737  */
    738 int
    739 rdread(dev_t dev, struct uio *uio, int flag)
    740 {
    741 	return (physio (rdstrategy, NULL, dev, B_READ, minphys, uio));
    742 }
    743 
    744 /*
    745  *
    746  */
    747 int
    748 rdwrite(dev_t dev, struct uio *uio, int flag)
    749 {
    750 	return (physio (rdstrategy, NULL, dev, B_WRITE, minphys, uio));
    751 }
    752 
    753 /*
    754  * we have to wait 0.7 usec between two accesses to any of the
    755  * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
    756  * instruction. Thus the loop-overhead will be enough...
    757  */
    758 static void
    759 hdc_readregs(struct hdcsoftc *sc)
    760 {
    761 	int i;
    762 	char *p;
    763 
    764 	HDC_WCMD(DKC_CMD_SETREGPTR);
    765 	WAIT;
    766 	p = (void*)&sc->sc_sreg;
    767 	for (i=0; i<10; i++) {
    768 		*p++ = HDC_RREG;	/* dkc_reg auto-increments */
    769 		WAIT;
    770 	}
    771 }
    772 
    773 static void
    774 hdc_writeregs(struct hdcsoftc *sc)
    775 {
    776 	int i;
    777 	char *p;
    778 
    779 	HDC_WCMD(DKC_CMD_SETREGPTR);
    780 	p = (void*)&sc->sc_creg;
    781 	for (i=0; i<10; i++) {
    782 		HDC_WREG(*p++);	/* dkc_reg auto-increments */
    783 		WAIT;
    784 	}
    785 }
    786 
    787 /*
    788  * hdc_command() issues a command and polls the intreq-register
    789  * to find when command has completed
    790  */
    791 int
    792 hdc_command(struct hdcsoftc *sc, int cmd)
    793 {
    794 	hdc_writeregs(sc);		/* write the prepared registers */
    795 	HDC_WCMD(cmd);
    796 	WAIT;
    797 	return (0);
    798 }
    799 
    800 int
    801 hdc_rdselect(struct hdcsoftc *sc, int unit)
    802 {
    803 	struct hdc9224_UDCreg *p = &sc->sc_creg;
    804 	int error;
    805 
    806 	/*
    807 	 * bring "creg" in some known-to-work state and
    808 	 * select the drive with the DRIVE SELECT command.
    809 	 */
    810 	bzero(p, sizeof(struct hdc9224_UDCreg));
    811 
    812 	p->udc_rtcnt = UDC_RC_HDD_READ;
    813 	p->udc_mode  = UDC_MD_HDD;
    814 	p->udc_term  = UDC_TC_HDD;
    815 
    816 	error = hdc_command(sc, DKC_CMD_DRSEL_HDD | unit);
    817 
    818 	return (error);
    819 }
    820 
    821 void
    822 rdmakelabel(struct disklabel *dl, struct rdgeom *g)
    823 {
    824 	int n, p = 0;
    825 
    826 	dl->d_bbsize = BBSIZE;
    827 	dl->d_sbsize = SBLOCKSIZE;
    828 	dl->d_typename[p++] = MSCP_MID_CHAR(2, g->media_id);
    829 	dl->d_typename[p++] = MSCP_MID_CHAR(1, g->media_id);
    830 	if (MSCP_MID_ECH(0, g->media_id))
    831 		dl->d_typename[p++] = MSCP_MID_CHAR(0, g->media_id);
    832 	n = MSCP_MID_NUM(g->media_id);
    833 	if (n > 99) {
    834 		dl->d_typename[p++] = '1';
    835 		n -= 100;
    836 	}
    837 	if (n > 9) {
    838 		dl->d_typename[p++] = (n / 10) + '0';
    839 		n %= 10;
    840 	}
    841 	dl->d_typename[p++] = n + '0';
    842 	dl->d_typename[p] = 0;
    843 	dl->d_type = DTYPE_MSCP; /* XXX - what to use here??? */
    844 	dl->d_rpm = 3600;
    845 	dl->d_secsize = DEV_BSIZE;
    846 
    847 	dl->d_secperunit = g->lbn_count;
    848 	dl->d_nsectors = g->nspt;
    849 	dl->d_ntracks = g->ntracks;
    850 	dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks;
    851 	dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl;
    852 
    853 	dl->d_npartitions = MAXPARTITIONS;
    854 	dl->d_partitions[0].p_size = dl->d_partitions[2].p_size =
    855 	    dl->d_secperunit;
    856 	dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0;
    857 	dl->d_interleave = dl->d_headswitch = 1;
    858 	dl->d_magic = dl->d_magic2 = DISKMAGIC;
    859 	dl->d_checksum = dkcksum(dl);
    860 }
    861