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