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