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