Home | History | Annotate | Line # | Download | only in vsa
hdc9224.c revision 1.5
      1 /*	$NetBSD: hdc9224.c,v 1.5 1997/03/09 15:55:59 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 
     43 /* #define DEBUG	/* */
     44 /* #define TRACE	/* */
     45 static int haveLock = 0;
     46 static int keepLock = 0;
     47 
     48 #define F_READ	11
     49 #define F_WRITE 12
     50 
     51 #define trace(x)
     52 #define debug(x)
     53 
     54 #include "hdc.h"
     55 #if NHDC > 0
     56 
     57 #include <sys/param.h>
     58 #include <sys/systm.h>
     59 #include <sys/kernel.h>
     60 #include <sys/conf.h>
     61 #include <sys/file.h>
     62 #include <sys/stat.h>
     63 #include <sys/ioctl.h>
     64 #include <sys/buf.h>
     65 #include <sys/proc.h>
     66 #include <sys/user.h>
     67 #include <sys/map.h>
     68 #include <sys/device.h>
     69 #include <sys/dkstat.h>
     70 #include <sys/disklabel.h>
     71 #include <sys/disk.h>
     72 #include <sys/syslog.h>
     73 
     74 #include <machine/pte.h>
     75 #include <machine/sid.h>
     76 #include <machine/cpu.h>
     77 #include <machine/uvax.h>
     78 #include <machine/ka410.h>
     79 #include <machine/vsbus.h>
     80 
     81 #include <vax/vsa/hdc9224.h>
     82 
     83 
     84 /*
     85  * some definitions
     86  */
     87 #define CTLRNAME  "hdc"
     88 #define UNITNAME  "rd"
     89 #define HDC_PRI	  LOG_INFO
     90 
     91 /* Bits in minor device */
     92 #define HDCUNIT(dev)	DISKUNIT(dev)
     93 #define HDCPART(dev)	DISKPART(dev)
     94 #define HDCCTLR(dev)	0
     95 #define HDCLABELDEV(dev)	(MAKEDISKDEV(major(dev),HDCUNIT(dev),RAW_PART))
     96 
     97 #define MAX_WAIT	(1000*1000)	/* # of loop-instructions in seconds */
     98 
     99 
    100 /*
    101  * on-disk geometry block
    102  */
    103 #define _aP	__attribute__ ((packed))	/* force byte-alignment */
    104 struct rdgeom {
    105   char mbz[10];		/* 10 bytes of zero */
    106   long xbn_count _aP;	/* number of XBNs */
    107   long dbn_count _aP;	/* number of DBNs */
    108   long lbn_count _aP;	/* number of LBNs (Logical-Block-Numbers) */
    109   long rbn_count _aP;	/* number of RBNs (Replacement-Block-Numbers) */
    110   short nspt;		/* number of sectors per track */
    111   short ntracks;	/* number of tracks */
    112   short ncylinders;	/* number of cylinders */
    113   short precomp;	/* first cylinder for write precompensation */
    114   short reduced;	/* first cylinder for reduced write current */
    115   short seek_rate;	/* seek rate or zero for buffered seeks */
    116   short crc_eec;	/* 0 if CRC is being used or 1 if ECC is being used */
    117   short rct;		/* "replacement control table" (RCT) */
    118   short rct_ncopies;	/* number of copies of the RCT */
    119   long	media_id _aP;	/* media identifier */
    120   short interleave;	/* sector-to-sector interleave */
    121   short headskew;	/* head-to-head skew */
    122   short cylskew;	/* cylinder-to-cylinder skew */
    123   short gap0_size;	/* size of GAP 0 in the MFM format */
    124   short gap1_size;	/* size of GAP 1 in the MFM format */
    125   short gap2_size;	/* size of GAP 2 in the MFM format */
    126   short gap3_size;	/* size of GAP 3 in the MFM format */
    127   short sync_value;	/* sync value used to start a track when formatting */
    128   char	reserved[32];	/* reserved for use by the RQDX1/2/3 formatter */
    129   short serial_number;	/* serial number */
    130 #if 0	/* we don't need these 412 useless bytes ... */
    131   char	fill[412-2];	/* Filler bytes to the end of the block */
    132   short checksum;	/* checksum over the XBN */
    133 #endif
    134 };
    135 
    136 /*
    137  * Software status
    138  */
    139 struct	rdsoftc {
    140 	struct device	sc_dev;		/* must be here! (pseudo-OOP:) */
    141 	struct disk	sc_dk;		/* disklabel etc. */
    142 	struct rdgeom	sc_xbn;		/* on-disk geometry information */
    143 	struct rdparams {
    144 		u_short cylinders;	/* number of cylinders */
    145 		u_char	heads;		/* number of heads (tracks) */
    146 		u_char	sectors;	/* number of sectors/track */
    147 		u_long	diskblks;	/* number of sectors/disk */
    148 		u_long	disklbns;	/* number of available sectors */
    149 		u_long	blksize;	/* number of bytes/sector */
    150 		u_long	diskbytes;	/* number of bytes/disk */
    151 		char	diskname[8];
    152 	} sc_param;
    153 	int	sc_drive;		/* physical unit number */
    154 	int	sc_flags;
    155 	int	sc_state;
    156 	int	sc_mode;
    157 };
    158 
    159 struct	hdcsoftc {
    160 	struct device sc_dev;		/* must be here (pseudo-OOP:) */
    161 	struct hdc9224_DKCreg *sc_dkc;	/* I/O address of the controller */
    162 	struct hdc9224_UDCreg sc_creg;	/* (command) registers to be written */
    163 	struct hdc9224_UDCreg sc_sreg;	/* (status) registers being read */
    164 	struct confargs *sc_cfargs;	/* remember args being probed with */
    165 	char	*sc_dmabase;		/* */
    166 	long	sc_dmasize;		/* */
    167 	long	sc_ioaddr;		/* unmapped I/O address */
    168 	long	sc_ivec;		/* interrupt vector address */
    169 	short	sc_ibit;		/* bit-value in interrupt register */
    170 	short	sc_status;		/* copy of status register */
    171 	short	sc_state;
    172 	short	sc_flags;
    173 	short	sc_errors;
    174 };
    175 
    176 /*
    177  * Device definition for (new) autoconfiguration.
    178  */
    179 int	hdcmatch  __P((struct device *parent, void *cfdata, void *aux));
    180 void	hdcattach __P((struct device *parent, struct device *self, void *aux));
    181 int	hdcprint  __P((void *aux, const char *name));
    182 
    183 struct	cfdriver hdc_cd = {
    184 	NULL, "hdc", DV_DULL
    185 };
    186 struct	cfattach hdc_ca = {
    187 	sizeof(struct hdcsoftc), hdcmatch, hdcattach
    188 };
    189 
    190 int	rdmatch __P((struct device *parent, void *cfdata, void *aux));
    191 void	rdattach __P((struct device *parent, struct device *self, void *aux));
    192 int	rdprint __P((void *aux, const char *name));
    193 void	rdstrategy __P((struct buf *bp));
    194 
    195 struct	cfdriver rd_cd = {
    196 	NULL, "rd", DV_DISK
    197 };
    198 struct	cfattach rd_ca = {
    199 	sizeof(struct rdsoftc), rdmatch, rdattach
    200 };
    201 
    202 struct dkdriver rddkdriver = { rdstrategy };
    203 
    204 /*
    205  * prototypes for (almost) all the internal routines
    206  */
    207 int hdc_reset	__P((struct hdcsoftc *sc));
    208 int hdc_select	__P((struct hdcsoftc *sc, int drive));
    209 int hdc_command __P((struct hdcsoftc *sc, int cmd));
    210 
    211 int hdc_getdata	 __P((struct hdcsoftc *hdc, struct rdsoftc *rd, int drive));
    212 int hdc_getlabel __P((struct hdcsoftc *hdc, struct rdsoftc *rd, int drive));
    213 
    214 void rdgetlabel __P((struct rdsoftc *sc));
    215 
    216 /*
    217  * new-config's hdcmatch() is similiar to old-config's hdcprobe(),
    218  * thus we probe for the existence of the controller and reset it.
    219  * NB: we can't initialize the controller yet, since space for hdcsoftc
    220  *     is not yet allocated. Thus we do this in hdcattach()...
    221  */
    222 int
    223 hdcmatch(parent, match, aux)
    224 	struct device *parent;
    225 	void *match, *aux;
    226 {
    227 	struct cfdata *cf = match;
    228 	struct confargs *ca = aux;
    229 
    230 	trace(("hdcmatch(0x%x, %d, %s)\n", parent, cf->cf_unit, ca->ca_name));
    231 
    232 	if (strcmp(ca->ca_name, "hdc") &&
    233 	    strcmp(ca->ca_name, "hdc9224") &&
    234 	    strcmp(ca->ca_name, "HDC9224"))
    235 		return (0);
    236 
    237 	/*
    238 	 * only(?) VS2000/KA410 has exactly one HDC9224 controller
    239 	 */
    240 	if (vax_boardtype != VAX_BTYP_410) {
    241 		printf ("unexpected boardtype 0x%x in hdcmatch()\n",
    242 			vax_boardtype);
    243 		return (0);
    244 	}
    245 	if (cf->cf_unit != 0)
    246 		return (0);
    247 
    248 	return (1);
    249 }
    250 
    251 struct hdc_attach_args {
    252 	int ha_drive;
    253 };
    254 
    255 int
    256 rdprint(aux, name)
    257 	void *aux;
    258 	const char *name;
    259 {
    260 	struct hdc_attach_args *ha = aux;
    261 
    262 	trace(("rdprint(%d, %s)\n", ha->ha_drive, name));
    263 
    264 	if (!name)
    265 		printf (" drive %d", ha->ha_drive);
    266 	return (QUIET);
    267 }
    268 
    269 /*
    270  * hdc_attach() probes for all possible devices
    271  */
    272 void
    273 hdcattach(parent, self, aux)
    274 	struct device *parent, *self;
    275 	void *aux;
    276 {
    277 	struct hdcsoftc *sc = (void*)self;
    278 	struct confargs *ca = aux;
    279 	struct hdc_attach_args ha;
    280 
    281 	trace(("hdcattach(0x%x, 0x%x, %s)\n", parent, self, ca->ca_name));
    282 
    283 	printf ("\n");
    284 	/*
    285 	 * first reset/initialize the controller
    286 	 */
    287 	sc->sc_cfargs = ca;
    288 
    289 	sc->sc_ioaddr = ca->ca_ioaddr;
    290 	sc->sc_dkc = (void*)uvax_phys2virt(sc->sc_ioaddr);
    291 	sc->sc_ibit = ca->ca_intbit;
    292 	sc->sc_ivec = ca->ca_intvec;
    293 	sc->sc_status = 0;
    294 	sc->sc_state = 0;
    295 	sc->sc_flags = 0;
    296 	sc->sc_errors = 0;
    297 
    298 	sc->sc_dkc     = (void*)uvax_phys2virt(KA410_DKC_BASE);
    299 	sc->sc_dmabase = (void*)uvax_phys2virt(KA410_DMA_BASE);
    300 	sc->sc_dmasize = KA410_DMA_SIZE;
    301 
    302 	if (hdc_reset(sc) != 0) {
    303 		delay(500*1000);	/* wait .5 seconds */
    304 		if (hdc_reset(sc) != 0)
    305 			printf ("problems with hdc_reset()...\n");
    306 	}
    307 
    308 	/*
    309 	 * now probe for all possible disks
    310 	 */
    311 	for (ha.ha_drive=0; ha.ha_drive<3; ha.ha_drive++)
    312 		(void)config_found(self, (void*)&ha, rdprint);
    313 
    314 #ifdef notyet
    315 	/*
    316 	 * now that probing is done, we can register and enable interrupts
    317 	 */
    318 	vsbus_intr_register(XXX);
    319 	vsbus_intr_enable(XXX);
    320 #endif
    321 }
    322 
    323 /*
    324  * rdmatch() probes for the existence of a RD-type disk/floppy
    325  */
    326 int
    327 rdmatch(parent, match, aux)
    328 	struct device *parent;
    329 	void *match, *aux;
    330 {
    331 	struct hdcsoftc *hdc = (void*)parent;
    332 	struct cfdata *cf = match;
    333 	struct hdc_attach_args *ha = aux;
    334 	int drive = ha->ha_drive;
    335 	int res;
    336 
    337 	trace(("rdmatch(%d, %d)\n", cf->cf_unit, drive));
    338 
    339 	if (cf->cf_unit != ha->ha_drive)
    340 		return (0);
    341 
    342 	switch (drive) {
    343 	case 0:
    344 	case 1:
    345 	case 2:
    346 		res = hdc_select(hdc, drive);
    347 		break;
    348 	default:
    349 		printf ("rdmatch: invalid unit-number %d\n", drive);
    350 		return (0);
    351 	}
    352 
    353 	debug (("cstat: %x dstat: %x\n", hdc->sc_sreg.udc_cstat,
    354 		hdc->sc_sreg.udc_dstat));
    355 	if (drive == 1)
    356 	  return (0);	/* XXX */
    357 
    358 	return (1);
    359 }
    360 
    361 void
    362 rdattach(parent, self, aux)
    363 	struct device *parent, *self;
    364 	void *aux;
    365 {
    366 	struct hdcsoftc *hdc = (void*)parent;
    367 	struct rdsoftc *rd = (void*)self;
    368 	struct hdc_attach_args *ha = aux;
    369 	struct rdparams *rp = &rd->sc_param;
    370 
    371 	trace(("rdattach(%d)\n", ha->ha_drive));
    372 
    373 	rd->sc_drive = ha->ha_drive;
    374 	/*
    375 	 * Initialize and attach the disk structure.
    376 	 */
    377 	rd->sc_dk.dk_driver = &rddkdriver;
    378 	rd->sc_dk.dk_name = rd->sc_dev.dv_xname;
    379 	disk_attach(&rd->sc_dk);
    380 	/*
    381 	 * if it's not a floppy then evaluate the on-disk geometry.
    382 	 * if neccessary correct the label...
    383 	 */
    384 	printf("\n%s: ", rd->sc_dev.dv_xname);
    385 	if (rd->sc_drive == 2) {
    386 		printf("floppy (RX33)\n");
    387 	}
    388 	else {
    389 		hdc_getdata(hdc, rd, rd->sc_drive);
    390 		printf("%s, %d MB, %d LBN, %d cyl, %d head, %d sect/track\n",
    391 		       rp->diskname, rp->diskblks/2048, rp->disklbns,
    392 		       rp->cylinders, rp->heads, rp->sectors);
    393 	}
    394 }
    395 
    396 /*
    397  * Read/write routine for a buffer.  For now we poll the controller,
    398  * thus this routine waits for the transfer to complete.
    399  */
    400 void
    401 rdstrategy(bp)
    402 	struct buf *bp;
    403 {
    404 	struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(bp->b_dev)];
    405 	struct hdcsoftc *hdc = (void *)rd->sc_dev.dv_parent;
    406 	struct partition *p;
    407 	int blkno, i, s;
    408 
    409 	trace (("rdstrategy(#%d/%d)\n", bp->b_blkno, bp->b_bcount));
    410 
    411 	/* XXX		should make some checks... */
    412 
    413 	/*
    414 	 * If it's a null transfer, return immediatly
    415 	 */
    416 	if (bp->b_bcount == 0)
    417 		goto done;
    418 
    419 	/*
    420 	 * what follows now should not be here but in rdstart...
    421 	 */
    422 	/*------------------------------*/
    423 	blkno = bp->b_blkno / (rd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    424 	p = &rd->sc_dk.dk_label->d_partitions[HDCPART(bp->b_dev)];
    425 	blkno += p->p_offset;
    426 
    427 	/* nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize); */
    428 
    429 	if (hdc_strategy(hdc, rd, HDCUNIT(bp->b_dev),
    430 			 ((bp->b_flags & B_READ) ? F_READ : F_WRITE),
    431 			 blkno, bp->b_bcount, bp->b_data) == 0)
    432 		goto done;
    433 	/*------------------------------*/
    434 bad:
    435 	bp->b_flags |= B_ERROR;
    436 done:
    437 	/*
    438 	 * Correctly set the buf to indicate a completed xfer
    439 	 */
    440 	bp->b_resid = 0;	/* ??? bertram */
    441 	biodone(bp);
    442 }
    443 
    444 int
    445 hdc_strategy(hdc, rd, unit, func, dblk, size, buf)
    446 	struct hdcsoftc *hdc;
    447 	struct rdsoftc *rd;
    448 	int unit;
    449 	int func;
    450 	int dblk;
    451 	int size;
    452 	char *buf;
    453 {
    454 	struct hdc9224_UDCreg *p = &hdc->sc_creg;
    455 	struct disklabel *lp = rd->sc_dk.dk_label;
    456 	int sect, head, cyl;
    457 	int scount;
    458 	int cmd, res = 0;
    459 
    460 	trace (("hdc_strategy(%d, %d, %d, %d, 0x%x)\n",
    461 		unit, func, dblk, size, buf));
    462 
    463 	hdc_select(hdc, unit);		/* select drive right now */
    464 
    465 	if (unit != 2 && dblk == -1) {	/* read the on-disk geometry */
    466 
    467 	  p->udc_dma7  = 0;
    468 	  p->udc_dma15 = 0;
    469 	  p->udc_dma23 = 0;
    470 
    471 	  p->udc_dsect = 0;
    472 	  p->udc_dhead = 0;
    473 	  p->udc_dcyl  = 0;
    474 
    475 	  p->udc_scnt  = size/512;
    476 	  p->udc_rtcnt = 0xF0;
    477 	  p->udc_mode  = 0xC0;
    478 	  p->udc_term  = 0xB4;
    479 
    480 	  vsbus_lockDMA(hdc->sc_cfargs);		/* bertram XXX */
    481 	  haveLock = 1;
    482 	  keepLock = 1;
    483 
    484 #ifdef PARANOID
    485 	  bzero (hdc->sc_dmabase, size);	/* clear disk buffer */
    486 #endif
    487 	  cmd = 0x5C | 0x03;			/* bypass bad sectors */
    488 	  cmd = 0x5C | 0x01;			/* terminate if bad sector */
    489 
    490 	  res = hdc_command (hdc, cmd);
    491 	  /* hold the locking ! */
    492 	  bcopy (hdc->sc_dmabase, buf, size);	/* copy to buf */
    493 	  /* now release the locking */
    494 
    495 	  vsbus_unlockDMA(hdc->sc_cfargs);
    496 	  haveLock = 0;
    497 	  keepLock = 0;
    498 
    499 	  return (res);
    500 	}
    501 
    502 	scount = size / 512;
    503 	while (scount) {
    504 	  /*
    505 	   * prepare drive/operation parameter
    506 	   */
    507 	  cyl  = dblk / lp->d_secpercyl;
    508 	  sect = dblk % lp->d_secpercyl;
    509 	  head = sect / lp->d_nsectors;
    510 	  sect = sect % lp->d_nsectors;
    511 	  if (unit == 2)
    512 		sect++;
    513 	  else
    514 		cyl++;		/* first cylinder is reserved */
    515 
    516 	  size = 512 * min(scount, lp->d_nsectors - sect);
    517 
    518 	  debug (("hdc_strategy: block #%d ==> s/t/c=%d/%d/%d (%d/%d)\n",
    519 		  dblk, sect, head, cyl, scount, size));
    520 
    521 	  /*
    522 	   * now initialize the register values ...
    523 	   */
    524 	  p->udc_dma7  = 0;
    525 	  p->udc_dma15 = 0;
    526 	  p->udc_dma23 = 0;
    527 
    528 	  p->udc_dsect = sect;
    529 	  head |= (cyl >> 4) & 0x70;
    530 	  p->udc_dhead = head;
    531 	  p->udc_dcyl  = cyl;
    532 
    533 	  p->udc_scnt  = size/512;
    534 
    535 	  if (unit == 2) {	/* floppy */
    536 	    p->udc_rtcnt = 0xF2;
    537 	    p->udc_mode	 = 0x81;	/* RX33 with RX50 media */
    538 	    p->udc_mode	 = 0x82;	/* RX33 with RX33 media */
    539 	    p->udc_term	 = 0xB4;
    540 	  } else {		 /* disk */
    541 	    p->udc_rtcnt = 0xF0;
    542 	    p->udc_mode	 = 0xC0;
    543 	    p->udc_term	 = 0xB4;
    544 	  }
    545 
    546 	  vsbus_lockDMA(hdc->sc_cfargs);
    547 	  haveLock = 1;
    548 	  keepLock = 1;
    549 
    550 	  if (func == F_WRITE) {
    551 	    bcopy (buf, hdc->sc_dmabase, size); /* copy from buf */
    552 	    cmd = 0xA0 | (unit==2 ? 1 : 0);
    553 	    res = hdc_command (hdc, cmd);
    554 	  }
    555 	  else {
    556 #ifdef PARANOID
    557 	    bzero (hdc->sc_dmabase, size);		/* clear disk buffer */
    558 #endif
    559 	    cmd = 0x5C | 0x03;	/* bypass bad sectors */
    560 	    cmd = 0x5C | 0x01;	/* terminate if bad sector */
    561 	    res = hdc_command (hdc, cmd);
    562 	    bcopy (hdc->sc_dmabase, buf, size); /* copy to buf */
    563 	  }
    564 
    565 	  vsbus_unlockDMA(hdc->sc_cfargs);
    566 	  haveLock = 0;
    567 	  keepLock = 0;
    568 
    569 	  scount -= size/512;
    570 	  dblk += size/512;
    571 	  buf += size;
    572 	}
    573 
    574 	if (unit != 2)		/* deselect drive, if not floppy */
    575 	  hdc_command (hdc, DKC_CMD_DRDESELECT);
    576 
    577 	return 0;
    578 }
    579 
    580 char hdc_iobuf[17*512];		/* we won't need more */
    581 
    582 #ifdef DEBUG
    583 /*
    584  * display the contents of the on-disk geometry structure
    585  */
    586 int
    587 hdc_printgeom(p)
    588 	struct rdgeom *p;
    589 {
    590 	char dname[8];
    591 	hdc_mid2str(p->media_id, dname);
    592 
    593 	printf ("**DiskData**	 XBNs: %d, DBNs: %d, LBNs: %d, RBNs: %d\n",
    594 		p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
    595 	printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
    596 		p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
    597 	printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
    598 		p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
    599 	printf ("media-ID: %s, interleave: %d, headskew: %d, cylskew: %d\n",
    600 		dname, p->interleave, p->headskew, p->cylskew);
    601 	printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
    602 		p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
    603 		p->sync_value);
    604 }
    605 #endif
    606 
    607 /*
    608  * Convert media_id to string/name (encoding is documented in mscp.h)
    609  */
    610 int
    611 hdc_mid2str(media_id, name)
    612 	long media_id;
    613 	char *name;
    614 {
    615 	struct {			/* For RD32 this struct holds: */
    616 		u_long mt:7;		/* number in name: 0x20 == 32 */
    617 		u_long a2:5;		/* ' ' encoded as 0x0 */
    618 		u_long a1:5;		/* 'D' encoded with base '@' */
    619 		u_long a0:5;		/* 'R' encoded with base '@' */
    620 		u_long d1:5;		/* 'U' encoded with base '@' */
    621 		u_long d0:5;		/* 'D' encoded with base '@' */
    622 	} *p = (void*)&media_id;
    623 
    624 #define MIDCHR(x)	(x ? x + '@' : ' ')
    625 
    626 	sprintf (name, "%c%c%d", MIDCHR(p->a0), MIDCHR(p->a1), p->mt);
    627 }
    628 
    629 int
    630 hdc_getdata(hdc, rd, unit)
    631 	struct hdcsoftc *hdc;
    632 	struct rdsoftc *rd;
    633 	int unit;
    634 {
    635 	struct disklabel *lp = rd->sc_dk.dk_label;
    636 	struct rdparams *rp = &rd->sc_param;
    637 	int res;
    638 
    639 	trace (("hdc_getdata(%d)\n", unit));
    640 
    641 	bzero(rd->sc_dk.dk_label, sizeof(struct disklabel));
    642 	bzero(rd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    643 
    644 	if (unit == 2) {
    645 		lp->d_secsize = DEV_BSIZE;
    646 		lp->d_ntracks = 2;
    647 		lp->d_nsectors = 15;
    648 		lp->d_ncylinders = 80;
    649 		lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    650 
    651 		return (0);
    652 	}
    653 
    654 	res = hdc_strategy(hdc, rd, unit, F_READ, -1, 4096, hdc_iobuf);
    655 	bcopy (hdc_iobuf, &rd->sc_xbn, sizeof(struct rdgeom));
    656 #ifdef DEBUG
    657 	hdc_printgeom(&rd->sc_xbn);
    658 #endif
    659 	lp->d_secsize = DEV_BSIZE;
    660 	lp->d_ntracks = rd->sc_xbn.ntracks;
    661 	lp->d_nsectors = rd->sc_xbn.nspt;
    662 	lp->d_ncylinders = rd->sc_xbn.ncylinders;
    663 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    664 
    665 	rp->cylinders = rd->sc_xbn.ncylinders;
    666 	rp->heads = rd->sc_xbn.ntracks;
    667 	rp->sectors = rd->sc_xbn.nspt;
    668 	rp->diskblks = rp->cylinders * rp->heads * rp->sectors;
    669 	rp->disklbns = rd->sc_xbn.lbn_count;
    670 	rp->blksize = DEV_BSIZE;
    671 	rp->diskbytes = rp->disklbns * rp->blksize;
    672 	hdc_mid2str(rd->sc_xbn.media_id, rp->diskname);
    673 
    674 	return (0);
    675 }
    676 
    677 int
    678 hdc_getlabel(hdc, rd, unit)
    679 	struct hdcsoftc *hdc;
    680 	struct rdsoftc *rd;
    681 	int unit;
    682 {
    683 	struct disklabel *lp = rd->sc_dk.dk_label;
    684 	struct disklabel *xp = (void*)(hdc_iobuf + 64);
    685 	int res;
    686 
    687 	trace (("hdc_getlabel(%d)\n", unit));
    688 
    689 #define LBL_CHECK(x)	if (xp->x != lp->x) {			\
    690 			  printf ("%d-->%d\n", xp->x, lp->x);	\
    691 			  xp->x = lp->x;			\
    692 			}
    693 	res = hdc_strategy(hdc, rd, unit, F_READ, 0, DEV_BSIZE, hdc_iobuf);
    694 	LBL_CHECK(d_secsize);
    695 	LBL_CHECK(d_ntracks);
    696 	LBL_CHECK(d_nsectors);
    697 	LBL_CHECK(d_ncylinders);
    698 	LBL_CHECK(d_secpercyl);
    699 	bcopy(xp, lp, sizeof(struct disklabel));
    700 
    701 	return (0);
    702 }
    703 
    704 /*
    705  * Return the size of a partition, if known, or -1 if not.
    706  */
    707 hdcsize(dev)
    708 	dev_t dev;
    709 {
    710 	int unit = HDCUNIT(dev);
    711 	int part = HDCPART(dev);
    712 	struct rdsoftc *rd = rd_cd.cd_devs[unit];
    713 	int size;
    714 
    715 	trace (("hdcsize(%x == %d/%d)\n", dev, unit, part));
    716 
    717 	if (hdcopen(dev, 0, S_IFBLK) != 0)
    718 		return (-1);
    719 #if 0
    720 	if (rd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    721 		size = -1;
    722 	else
    723 #endif
    724 		size = rd->sc_dk.dk_label->d_partitions[part].p_size;
    725 	if (hdcclose(dev, 0, S_IFBLK) != 0)
    726 		return (-1);
    727 	debug (("hdcsize: size=%d\n", size));
    728 	return (size);
    729 }
    730 
    731 /*
    732  *
    733  */
    734 int
    735 hdcopen (dev, flag, fmt)
    736 	dev_t dev;
    737 	int flag;
    738 	int fmt;
    739 {
    740 	int unit = HDCUNIT(dev);
    741 	int part = HDCPART(dev);
    742 	struct hdcsoftc *hdc;
    743 	struct rdsoftc *rd;
    744 	int res, error;
    745 
    746 	trace (("hdcopen(0x%x = %d/%d)\n", dev, unit, part));
    747 
    748 	if (unit >= rd_cd.cd_ndevs) {
    749 		printf ("hdcopen: invalid unit %d\n", unit);
    750 		return ENXIO;
    751 	}
    752 	rd = rd_cd.cd_devs[unit];
    753 	if (!rd) {
    754 		printf("hdcopen: null-pointer in rdsoftc.\n");
    755 		return (ENXIO);
    756 	}
    757 	hdc = (void *)rd->sc_dev.dv_parent;
    758 
    759 	/* XXX here's much more to do! XXX */
    760 
    761 	hdc_getdata (hdc, rd, unit);
    762 	hdc_getlabel (hdc, rd, unit);
    763 
    764 	return (0);
    765 }
    766 
    767 /*
    768  *
    769  */
    770 int
    771 hdcclose (dev, flag)
    772 	dev_t dev;
    773 	int flag;
    774 {
    775 	trace (("hdcclose()\n"));
    776 	return (0);
    777 }
    778 
    779 /*
    780  *
    781  */
    782 void
    783 hdcstrategy(bp)
    784 	register struct buf *bp;
    785 {
    786 	trace (("hdcstrategy()\n"));
    787 	rdstrategy(bp);
    788 	debug (("hdcstrategy done.\n"));
    789 }
    790 
    791 /*
    792  *
    793  */
    794 int
    795 hdcioctl(dev, cmd, data, flag, p)
    796 	dev_t dev;
    797 	int cmd;
    798 	caddr_t data;	/* aka: addr */
    799 	int flag;
    800 	struct proc *p;
    801 {
    802 	struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(dev)];
    803 	struct hdcsoftc *hdc = (void *)rd->sc_dev.dv_parent;
    804 	int error;
    805 
    806 	trace (("hdcioctl(%x, %x)\n", dev, cmd));
    807 
    808 	/*
    809 	 * If the device is not valid.. abandon ship
    810 	 */
    811 	/* XXX */
    812 
    813 	switch (cmd) {
    814 	case DIOCGDINFO:
    815 		*(struct disklabel *)data = *(rd->sc_dk.dk_label);
    816 		return (0);
    817 
    818 	case DIOCGPART:
    819 		((struct partinfo *)data)->disklab = rd->sc_dk.dk_label;
    820 		((struct partinfo *)data)->part =
    821 		  &rd->sc_dk.dk_label->d_partitions[HDCPART(dev)];
    822 		return (0);
    823 
    824 	case DIOCWDINFO:
    825 	case DIOCSDINFO:
    826 /* XXX
    827 		if ((flag & FWRITE) == 0)
    828 			return EBADF;
    829 
    830 		if ((error = sdlock(sd)) != 0)
    831 			return error;
    832 		sd->flags |= SDF_LABELLING;
    833 */
    834 		error = setdisklabel(rd->sc_dk.dk_label,
    835 		     (struct disklabel *)data, 0, rd->sc_dk.dk_cpulabel);
    836 		if (error == 0) {
    837 			if (cmd == DIOCWDINFO)
    838 				error = writedisklabel(HDCLABELDEV(dev),
    839 					rdstrategy, rd->sc_dk.dk_label,
    840 					rd->sc_dk.dk_cpulabel);
    841 		}
    842 /* XXX
    843 		sd->flags &= ~SDF_LABELLING;
    844 		sdunlock(sd);
    845 */
    846 		return (error);
    847 
    848 	case DIOCWLABEL:
    849 		if ((flag & FWRITE) == 0)
    850 			return (EBADF);
    851 /* XXX
    852 		if (*(int *)data)
    853 			sd->flags |= SDF_WLABEL;
    854 		else
    855 			sd->flags &= ~SDF_WLABEL;
    856 */
    857 		return (0);
    858 
    859 	default:
    860 		if (HDCPART(dev) != RAW_PART)
    861 			return ENOTTY;
    862 		printf ("IOCTL %x not implemented.\n", cmd);
    863 		return (-1);
    864 	}
    865 }
    866 
    867 /*
    868  *
    869  */
    870 int
    871 hdcintr()
    872 {
    873 	trace (("hdcintr()\n"));
    874 }
    875 
    876 /*
    877  *
    878  */
    879 int
    880 hdcread (dev, uio)
    881 	dev_t dev;
    882 	struct uio *uio;
    883 {
    884 	trace (("hdcread()\n"));
    885 	return (physio (hdcstrategy, NULL, dev, B_READ, minphys, uio));
    886 }
    887 
    888 /*
    889  *
    890  */
    891 int
    892 hdcwrite (dev, uio)
    893 	dev_t dev;
    894 	struct uio *uio;
    895 {
    896 	trace (("hdcwrite()\n"));
    897 	return (physio (hdcstrategy, NULL, dev, B_WRITE, minphys, uio));
    898 }
    899 
    900 /*
    901  *
    902  */
    903 int
    904 hdcdump(dev)
    905 	dev_t dev;
    906 {
    907 	trace (("hdcdump (%x)\n", dev));
    908 }
    909 
    910 /*
    911  * we have to wait 0.7 usec between two accesses to any of the
    912  * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
    913  * instruction. Thus the loop-overhead will be enough...
    914  */
    915 void
    916 hdc_readregs(sc)
    917 	struct hdcsoftc *sc;
    918 {
    919 	int i;
    920 	char *p;
    921 
    922 	trace(("hdc_readregs()\n"));
    923 
    924 	sc->sc_dkc->dkc_cmd = 0x40;	/* set internal counter to zero */
    925 	p = (void*)&sc->sc_sreg;
    926 	for (i=0; i<10; i++)
    927 		*p++ = sc->sc_dkc->dkc_reg;	/* dkc_reg auto-increments */
    928 }
    929 
    930 void
    931 hdc_writeregs(sc)
    932 	struct hdcsoftc *sc;
    933 {
    934 	int i;
    935 	char *p;
    936 
    937 	trace(("hdc_writeregs()\n"));
    938 
    939 	sc->sc_dkc->dkc_cmd = 0x40;	/* set internal counter to zero */
    940 	p = (void*)&sc->sc_creg;
    941 	for (i=0; i<10; i++)
    942 		sc->sc_dkc->dkc_reg = *p++;	/* dkc_reg auto-increments */
    943 }
    944 
    945 /*
    946  * hdc_command() issues a command and polls the intreq-register
    947  * to find when command has completed
    948  */
    949 int
    950 hdc_command(sc, cmd)
    951 	struct hdcsoftc *sc;
    952 	int cmd;
    953 {
    954 	volatile u_char *intreq = (void*)uvax_phys2virt(KA410_INTREQ);
    955 	volatile u_char *intclr = (void*)uvax_phys2virt(KA410_INTCLR);
    956 	volatile u_char *intmsk = (void*)uvax_phys2virt(KA410_INTMSK);
    957 	int i, c;
    958 
    959 	trace (("hdc_command(%x)\n", cmd));
    960 	debug (("intr-state: %x %x %x\n", *intreq, *intclr, *intmsk));
    961 
    962 	if (!haveLock) {
    963 	  vsbus_lockDMA(sc->sc_cfargs);
    964 	  haveLock = 1;
    965 	}
    966 
    967 	hdc_writeregs(sc);		/* write the prepared registers */
    968 	*intclr = INTR_DC;		/* clear any old interrupt */
    969 	sc->sc_dkc->dkc_cmd = cmd;	/* issue the command */
    970 	for (i=0; i<MAX_WAIT; i++) {
    971 		if ((c = *intreq) & INTR_DC)
    972 			break;
    973 	}
    974 	if ((c & INTR_DC) == 0) {
    975 		printf ("hdc_command: timeout in command 0x%x\n", cmd);
    976 	}
    977 	hdc_readregs(sc);		/* read the status registers */
    978 	sc->sc_status = sc->sc_dkc->dkc_stat;
    979 
    980 	if (!keepLock) {
    981 	  vsbus_unlockDMA(sc->sc_cfargs);
    982 	  haveLock = 0;
    983 	}
    984 
    985 	if (sc->sc_status != DKC_ST_DONE|DKC_TC_SUCCESS) {
    986 		printf ("command 0x%x completed with status 0x%x\n",
    987 			cmd, sc->sc_status);
    988 		return (-1);
    989 	}
    990 	return (0);
    991 }
    992 
    993 /*
    994  * writing zero into the command-register will reset the controller.
    995  * This will not interrupt data-transfer commands!
    996  * Also no interrupt is generated, thus we don't use hdc_command()
    997  */
    998 int
    999 hdc_reset(sc)
   1000 	struct hdcsoftc *sc;
   1001 {
   1002 	trace (("hdc_reset()\n"));
   1003 
   1004 	sc->sc_dkc->dkc_cmd = DKC_CMD_RESET;	/* issue RESET command */
   1005 	hdc_readregs(sc);			/* read the status registers */
   1006 	sc->sc_status = sc->sc_dkc->dkc_stat;
   1007 	if (sc->sc_status != DKC_ST_DONE|DKC_TC_SUCCESS) {
   1008 		printf ("RESET command completed with status 0x%x\n",
   1009 			sc->sc_status);
   1010 		return (-1);
   1011 	}
   1012 	return (0);
   1013 }
   1014 
   1015 int
   1016 hdc_rxselect(sc, unit)
   1017 	struct hdcsoftc *sc;
   1018 	int unit;
   1019 {
   1020 	register struct hdc9224_UDCreg *p = &sc->sc_creg;
   1021 	register struct hdc9224_UDCreg *q = &sc->sc_sreg;
   1022 	int error;
   1023 
   1024 	/*
   1025 	 * bring command-regs in some known-to-work state and
   1026 	 * select the drive with the DRIVE SELECT command.
   1027 	 */
   1028 	p->udc_dma7  = 0;
   1029 	p->udc_dma15 = 0;
   1030 	p->udc_dma23 = 0;
   1031 	p->udc_dsect = 1;	/* sectors are numbered 1..15 !!! */
   1032 	p->udc_dhead = 0;
   1033 	p->udc_dcyl  = 0;
   1034 	p->udc_scnt  = 0;
   1035 
   1036 	p->udc_rtcnt = UDC_RC_RX33READ;
   1037 	p->udc_mode  = UDC_MD_RX33;
   1038 	p->udc_term  = UDC_TC_FDD;
   1039 
   1040 	/*
   1041 	 * this is ...
   1042 	 */
   1043 	error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
   1044 
   1045 	if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 0)) {
   1046 	  printf("\nfloppy-drive not ready (new floppy inserted?)\n\n");
   1047 	  p->udc_rtcnt &= ~UDC_RC_INVRDY;	/* clear INVRDY-flag */
   1048 	  error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
   1049 	  if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 0)) {
   1050 	    printf("diskette not ready(1): %x/%x\n", error, q->udc_dstat);
   1051 	    printf("floppy-drive offline?\n");
   1052 	    return (-1);
   1053 	  }
   1054 
   1055 	  if (q->udc_dstat & UDC_DS_TRK00)		    /* if track-0 */
   1056 	    error = hdc_command(sc, DKC_CMD_STEPIN_FDD);   /* step inwards */
   1057 	  else						    /* else */
   1058 	    error = hdc_command(sc, DKC_CMD_STEPOUT_FDD);  /* step outwards */
   1059 
   1060 	  if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 1)) {
   1061 	    printf("diskette not ready(2): %x/%x\n", error, q->udc_dstat);
   1062 	    printf("No floppy inserted or drive offline\n");
   1063 	    /* return (-1); */
   1064 	  }
   1065 
   1066 	  p->udc_rtcnt |= UDC_RC_INVRDY;
   1067 	  error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
   1068 	  if ((error != 0) || (q->udc_dstat & UDC_DS_READY == 0)) {
   1069 	    printf("diskette not ready(3): %x/%x\n", error, q->udc_dstat);
   1070 	    printf("no floppy inserted or floppy-door open\n");
   1071 	    return(-1);
   1072 	  }
   1073 	  printf("floppy-drive reselected.\n");
   1074 	}
   1075 	if (error)
   1076 		error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
   1077 
   1078 	return (error);
   1079 }
   1080 
   1081 int
   1082 hdc_rdselect(sc, unit)
   1083 	struct hdcsoftc *sc;
   1084 	int unit;
   1085 {
   1086 	register struct hdc9224_UDCreg *p = &sc->sc_creg;
   1087 	register struct hdc9224_UDCreg *q = &sc->sc_sreg;
   1088 	int error;
   1089 
   1090 	/*
   1091 	 * bring "creg" in some known-to-work state and
   1092 	 * select the drive with the DRIVE SELECT command.
   1093 	 */
   1094 	p->udc_dma7  = 0;
   1095 	p->udc_dma15 = 0;
   1096 	p->udc_dma23 = 0;
   1097 	p->udc_dsect = 0;		/* sectors are numbered 0..16 */
   1098 	p->udc_dhead = 0;
   1099 	p->udc_dcyl  = 0;
   1100 	p->udc_scnt  = 0;
   1101 
   1102 	p->udc_rtcnt = UDC_RC_HDD_READ;
   1103 	p->udc_mode  = UDC_MD_HDD;
   1104 	p->udc_term  = UDC_TC_HDD;
   1105 
   1106 	error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
   1107 	if (error)
   1108 		error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
   1109 
   1110 	return (error);
   1111 }
   1112 
   1113 /*
   1114  * bring command-regs into some known-to-work state and select
   1115  * the drive with the DRIVE SELECT command.
   1116  */
   1117 int
   1118 hdc_select(sc, unit)
   1119 	struct hdcsoftc *sc;
   1120 	int unit;
   1121 {
   1122 	int error;
   1123 
   1124 	trace (("hdc_select(%x,%d)\n", sc, unit));
   1125 
   1126 	switch (unit) {
   1127 	case 0:
   1128 	case 1:
   1129 		error = hdc_rdselect(sc, unit);
   1130 		break;
   1131 	case 2:
   1132 		error = hdc_rxselect(sc, unit);
   1133 		/* bertram: delay ??? XXX */
   1134 		break;
   1135 	default:
   1136 		printf("invalid unit %d in hdc_select()\n", unit);
   1137 		error = -1;
   1138 	}
   1139 
   1140 	return (error);
   1141 }
   1142 
   1143 #endif	/* NHDC > 0 */
   1144