Home | History | Annotate | Line # | Download | only in vsa
hdc9224.c revision 1.9
      1 /*	$NetBSD: hdc9224.c,v 1.9 1998/04/13 12:17:31 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 #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, struct 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, struct cfdata *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, cf, aux)
    222 	struct device *parent;
    223 	struct cfdata *cf;
    224 	void *aux;
    225 {
    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, cf, aux)
    326 	struct device *parent;
    327 	struct cfdata *cf;
    328 	void *aux;
    329 {
    330 	struct hdcsoftc *hdc = (void*)parent;
    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, %ld MB, %ld 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 int hdc_strategy(struct hdcsoftc *, struct rdsoftc *, int, int, int, int, char *);
    400 /*
    401  * Read/write routine for a buffer.  For now we poll the controller,
    402  * thus this routine waits for the transfer to complete.
    403  */
    404 void
    405 rdstrategy(bp)
    406 	struct buf *bp;
    407 {
    408 	struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(bp->b_dev)];
    409 	struct hdcsoftc *hdc = (void *)rd->sc_dev.dv_parent;
    410 	struct partition *p;
    411 	int blkno;
    412 
    413 	trace (("rdstrategy(#%d/%d)\n", bp->b_blkno, bp->b_bcount));
    414 
    415 	/* XXX		should make some checks... */
    416 
    417 	/*
    418 	 * If it's a null transfer, return immediatly
    419 	 */
    420 	if (bp->b_bcount == 0)
    421 		goto done;
    422 
    423 	/*
    424 	 * what follows now should not be here but in rdstart...
    425 	 */
    426 	/*------------------------------*/
    427 	blkno = bp->b_blkno / (rd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    428 	p = &rd->sc_dk.dk_label->d_partitions[HDCPART(bp->b_dev)];
    429 	blkno += p->p_offset;
    430 
    431 	/* nblks = howmany(bp->b_bcount, sd->sc_dk.dk_label->d_secsize); */
    432 
    433 	if (hdc_strategy(hdc, rd, HDCUNIT(bp->b_dev),
    434 			 ((bp->b_flags & B_READ) ? F_READ : F_WRITE),
    435 			 blkno, bp->b_bcount, bp->b_data) == 0)
    436 		goto done;
    437 	/*------------------------------*/
    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 void hdc_mid2str(long, char *);
    586 #ifdef DEBUG
    587 void hdc_printgeom( struct rdgeom *);
    588 /*
    589  * display the contents of the on-disk geometry structure
    590  */
    591 void
    592 hdc_printgeom(p)
    593 	struct rdgeom *p;
    594 {
    595 	char dname[8];
    596 	hdc_mid2str(p->media_id, dname);
    597 
    598 	printf ("**DiskData**	 XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n",
    599 		p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count);
    600 	printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n",
    601 		p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced);
    602 	printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n",
    603 		p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies);
    604 	printf ("media-ID: %s, interleave: %d, headskew: %d, cylskew: %d\n",
    605 		dname, p->interleave, p->headskew, p->cylskew);
    606 	printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n",
    607 		p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size,
    608 		p->sync_value);
    609 }
    610 #endif
    611 
    612 /*
    613  * Convert media_id to string/name (encoding is documented in mscp.h)
    614  */
    615 void
    616 hdc_mid2str(media_id, name)
    617 	long media_id;
    618 	char *name;
    619 {
    620 	struct {			/* For RD32 this struct holds: */
    621 		u_long mt:7;		/* number in name: 0x20 == 32 */
    622 		u_long a2:5;		/* ' ' encoded as 0x0 */
    623 		u_long a1:5;		/* 'D' encoded with base '@' */
    624 		u_long a0:5;		/* 'R' encoded with base '@' */
    625 		u_long d1:5;		/* 'U' encoded with base '@' */
    626 		u_long d0:5;		/* 'D' encoded with base '@' */
    627 	} *p = (void*)&media_id;
    628 
    629 #define MIDCHR(x)	(x ? x + '@' : ' ')
    630 
    631 	sprintf (name, "%c%c%d", MIDCHR(p->a0), MIDCHR(p->a1), p->mt);
    632 }
    633 
    634 int
    635 hdc_getdata(hdc, rd, unit)
    636 	struct hdcsoftc *hdc;
    637 	struct rdsoftc *rd;
    638 	int unit;
    639 {
    640 	struct disklabel *lp = rd->sc_dk.dk_label;
    641 	struct rdparams *rp = &rd->sc_param;
    642 	int res;
    643 
    644 	trace (("hdc_getdata(%d)\n", unit));
    645 
    646 	bzero(rd->sc_dk.dk_label, sizeof(struct disklabel));
    647 	bzero(rd->sc_dk.dk_cpulabel, sizeof(struct cpu_disklabel));
    648 
    649 	if (unit == 2) {
    650 		lp->d_secsize = DEV_BSIZE;
    651 		lp->d_ntracks = 2;
    652 		lp->d_nsectors = 15;
    653 		lp->d_ncylinders = 80;
    654 		lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    655 
    656 		return (0);
    657 	}
    658 
    659 	res = hdc_strategy(hdc, rd, unit, F_READ, -1, 4096, hdc_iobuf);
    660 	bcopy (hdc_iobuf, &rd->sc_xbn, sizeof(struct rdgeom));
    661 #ifdef DEBUG
    662 	hdc_printgeom(&rd->sc_xbn);
    663 #endif
    664 	lp->d_secsize = DEV_BSIZE;
    665 	lp->d_ntracks = rd->sc_xbn.ntracks;
    666 	lp->d_nsectors = rd->sc_xbn.nspt;
    667 	lp->d_ncylinders = rd->sc_xbn.ncylinders;
    668 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    669 
    670 	rp->cylinders = rd->sc_xbn.ncylinders;
    671 	rp->heads = rd->sc_xbn.ntracks;
    672 	rp->sectors = rd->sc_xbn.nspt;
    673 	rp->diskblks = rp->cylinders * rp->heads * rp->sectors;
    674 	rp->disklbns = rd->sc_xbn.lbn_count;
    675 	rp->blksize = DEV_BSIZE;
    676 	rp->diskbytes = rp->disklbns * rp->blksize;
    677 	hdc_mid2str(rd->sc_xbn.media_id, rp->diskname);
    678 
    679 	return (0);
    680 }
    681 
    682 int
    683 hdc_getlabel(hdc, rd, unit)
    684 	struct hdcsoftc *hdc;
    685 	struct rdsoftc *rd;
    686 	int unit;
    687 {
    688 	struct disklabel *lp = rd->sc_dk.dk_label;
    689 	struct disklabel *xp = (void*)(hdc_iobuf + 64);
    690 	int res;
    691 
    692 	trace (("hdc_getlabel(%d)\n", unit));
    693 
    694 #define LBL_CHECK(x)	if (xp->x != lp->x) {			\
    695 			  printf ("%d-->%d\n", xp->x, lp->x);	\
    696 			  xp->x = lp->x;			\
    697 			}
    698 	res = hdc_strategy(hdc, rd, unit, F_READ, 0, DEV_BSIZE, hdc_iobuf);
    699 	LBL_CHECK(d_secsize);
    700 	LBL_CHECK(d_ntracks);
    701 	LBL_CHECK(d_nsectors);
    702 	LBL_CHECK(d_ncylinders);
    703 	LBL_CHECK(d_secpercyl);
    704 	bcopy(xp, lp, sizeof(struct disklabel));
    705 
    706 	return (0);
    707 }
    708 
    709 bdev_decl(hdc);
    710 int hdcsize(dev_t);
    711 /*
    712  * Return the size of a partition, if known, or -1 if not.
    713  */
    714 int
    715 hdcsize(dev)
    716 	dev_t dev;
    717 {
    718 	int unit = HDCUNIT(dev);
    719 	int part = HDCPART(dev);
    720 	struct rdsoftc *rd = rd_cd.cd_devs[unit];
    721 	int size;
    722 
    723 	trace (("hdcsize(%x == %d/%d)\n", dev, unit, part));
    724 
    725 	if (hdcopen(dev, 0, S_IFBLK, 0) != 0)
    726 		return (-1);
    727 #if 0
    728 	if (rd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    729 		size = -1;
    730 	else
    731 #endif
    732 		size = rd->sc_dk.dk_label->d_partitions[part].p_size;
    733 	if (hdcclose(dev, 0, S_IFBLK, 0) != 0)
    734 		return (-1);
    735 	debug (("hdcsize: size=%d\n", size));
    736 	return (size);
    737 }
    738 
    739 /*
    740  *
    741  */
    742 int
    743 hdcopen (dev, flag, fmt, p)
    744 	dev_t dev;
    745 	int flag;
    746 	int fmt;
    747 	struct proc *p;
    748 {
    749 	int unit = HDCUNIT(dev);
    750 	struct hdcsoftc *hdc;
    751 	struct rdsoftc *rd;
    752 
    753 	trace (("hdcopen(0x%x = %d/%d)\n", dev, unit, part));
    754 
    755 	if (unit >= rd_cd.cd_ndevs) {
    756 		printf ("hdcopen: invalid unit %d\n", unit);
    757 		return ENXIO;
    758 	}
    759 	rd = rd_cd.cd_devs[unit];
    760 	if (!rd) {
    761 		printf("hdcopen: null-pointer in rdsoftc.\n");
    762 		return (ENXIO);
    763 	}
    764 	hdc = (void *)rd->sc_dev.dv_parent;
    765 
    766 	/* XXX here's much more to do! XXX */
    767 
    768 	hdc_getdata (hdc, rd, unit);
    769 	hdc_getlabel (hdc, rd, unit);
    770 
    771 	return (0);
    772 }
    773 
    774 /*
    775  *
    776  */
    777 int
    778 hdcclose (dev, flag, type, p)
    779 	dev_t dev;
    780 	int flag, type;
    781 	struct proc *p;
    782 {
    783 	trace (("hdcclose()\n"));
    784 	return (0);
    785 }
    786 
    787 /*
    788  *
    789  */
    790 void
    791 hdcstrategy(bp)
    792 	register struct buf *bp;
    793 {
    794 	trace (("hdcstrategy()\n"));
    795 	rdstrategy(bp);
    796 	debug (("hdcstrategy done.\n"));
    797 }
    798 
    799 /*
    800  *
    801  */
    802 int
    803 hdcioctl(dev, cmd, data, flag, p)
    804 	dev_t dev;
    805 	u_long cmd;
    806 	caddr_t data;	/* aka: addr */
    807 	int flag;
    808 	struct proc *p;
    809 {
    810 	struct rdsoftc *rd = rd_cd.cd_devs[HDCUNIT(dev)];
    811 	int error;
    812 
    813 	trace (("hdcioctl(%x, %x)\n", dev, cmd));
    814 
    815 	/*
    816 	 * If the device is not valid.. abandon ship
    817 	 */
    818 	/* XXX */
    819 
    820 	switch (cmd) {
    821 	case DIOCGDINFO:
    822 		*(struct disklabel *)data = *(rd->sc_dk.dk_label);
    823 		return (0);
    824 
    825 	case DIOCGPART:
    826 		((struct partinfo *)data)->disklab = rd->sc_dk.dk_label;
    827 		((struct partinfo *)data)->part =
    828 		  &rd->sc_dk.dk_label->d_partitions[HDCPART(dev)];
    829 		return (0);
    830 
    831 	case DIOCWDINFO:
    832 	case DIOCSDINFO:
    833 /* XXX
    834 		if ((flag & FWRITE) == 0)
    835 			return EBADF;
    836 
    837 		if ((error = sdlock(sd)) != 0)
    838 			return error;
    839 		sd->flags |= SDF_LABELLING;
    840 */
    841 		error = setdisklabel(rd->sc_dk.dk_label,
    842 		     (struct disklabel *)data, 0, rd->sc_dk.dk_cpulabel);
    843 		if (error == 0) {
    844 			if (cmd == DIOCWDINFO)
    845 				error = writedisklabel(HDCLABELDEV(dev),
    846 					rdstrategy, rd->sc_dk.dk_label,
    847 					rd->sc_dk.dk_cpulabel);
    848 		}
    849 /* XXX
    850 		sd->flags &= ~SDF_LABELLING;
    851 		sdunlock(sd);
    852 */
    853 		return (error);
    854 
    855 	case DIOCWLABEL:
    856 		if ((flag & FWRITE) == 0)
    857 			return (EBADF);
    858 /* XXX
    859 		if (*(int *)data)
    860 			sd->flags |= SDF_WLABEL;
    861 		else
    862 			sd->flags &= ~SDF_WLABEL;
    863 */
    864 		return (0);
    865 
    866 	default:
    867 		if (HDCPART(dev) != RAW_PART)
    868 			return ENOTTY;
    869 		printf ("IOCTL %lx not implemented.\n", cmd);
    870 		return (-1);
    871 	}
    872 }
    873 
    874 cdev_decl(hdc);
    875 
    876 /*
    877  *
    878  */
    879 int
    880 hdcread (dev, uio, flag)
    881 	dev_t dev;
    882 	struct uio *uio;
    883 	int flag;
    884 {
    885 	trace (("hdcread()\n"));
    886 	return (physio (hdcstrategy, NULL, dev, B_READ, minphys, uio));
    887 }
    888 
    889 /*
    890  *
    891  */
    892 int
    893 hdcwrite (dev, uio, flag)
    894 	dev_t dev;
    895 	struct uio *uio;
    896 	int flag;
    897 {
    898 	trace (("hdcwrite()\n"));
    899 	return (physio (hdcstrategy, NULL, dev, B_WRITE, minphys, uio));
    900 }
    901 
    902 /*
    903  *
    904  */
    905 int
    906 hdcdump(dev, daddr, addr, size)
    907 	dev_t dev;
    908 	daddr_t daddr;
    909 	caddr_t addr;
    910 	size_t size;
    911 {
    912 	trace (("hdcdump (%x)\n", dev));
    913 	return 0;
    914 }
    915 
    916 void hdc_readregs (struct hdcsoftc *);
    917 /*
    918  * we have to wait 0.7 usec between two accesses to any of the
    919  * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one
    920  * instruction. Thus the loop-overhead will be enough...
    921  */
    922 void
    923 hdc_readregs(sc)
    924 	struct hdcsoftc *sc;
    925 {
    926 	int i;
    927 	char *p;
    928 
    929 	trace(("hdc_readregs()\n"));
    930 
    931 	sc->sc_dkc->dkc_cmd = 0x40;	/* set internal counter to zero */
    932 	p = (void*)&sc->sc_sreg;
    933 	for (i=0; i<10; i++)
    934 		*p++ = sc->sc_dkc->dkc_reg;	/* dkc_reg auto-increments */
    935 }
    936 
    937 void hdc_writeregs( struct hdcsoftc *);
    938 
    939 void
    940 hdc_writeregs(sc)
    941 	struct hdcsoftc *sc;
    942 {
    943 	int i;
    944 	char *p;
    945 
    946 	trace(("hdc_writeregs()\n"));
    947 
    948 	sc->sc_dkc->dkc_cmd = 0x40;	/* set internal counter to zero */
    949 	p = (void*)&sc->sc_creg;
    950 	for (i=0; i<10; i++)
    951 		sc->sc_dkc->dkc_reg = *p++;	/* dkc_reg auto-increments */
    952 }
    953 
    954 /*
    955  * hdc_command() issues a command and polls the intreq-register
    956  * to find when command has completed
    957  */
    958 int
    959 hdc_command(sc, cmd)
    960 	struct hdcsoftc *sc;
    961 	int cmd;
    962 {
    963 	volatile u_char *intreq = (void*)uvax_phys2virt(KA410_INTREQ);
    964 	volatile u_char *intclr = (void*)uvax_phys2virt(KA410_INTCLR);
    965 	int i, c;
    966 
    967 	trace (("hdc_command(%x)\n", cmd));
    968 	debug (("intr-state: %x %x %x\n", *intreq, *intclr, *intmsk));
    969 
    970 	if (!haveLock) {
    971 	  vsbus_lockDMA(sc->sc_cfargs);
    972 	  haveLock = 1;
    973 	}
    974 
    975 	hdc_writeregs(sc);		/* write the prepared registers */
    976 	*intclr = INTR_DC;		/* clear any old interrupt */
    977 	sc->sc_dkc->dkc_cmd = cmd;	/* issue the command */
    978 	for (i=0; i<MAX_WAIT; i++) {
    979 		if ((c = *intreq) & INTR_DC)
    980 			break;
    981 	}
    982 	if ((c & INTR_DC) == 0) {
    983 		printf ("hdc_command: timeout in command 0x%x\n", cmd);
    984 	}
    985 	hdc_readregs(sc);		/* read the status registers */
    986 	sc->sc_status = sc->sc_dkc->dkc_stat;
    987 
    988 	if (!keepLock) {
    989 	  vsbus_unlockDMA(sc->sc_cfargs);
    990 	  haveLock = 0;
    991 	}
    992 
    993 	if (sc->sc_status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
    994 		printf ("command 0x%x completed with status 0x%x\n",
    995 			cmd, sc->sc_status);
    996 		return (-1);
    997 	}
    998 	return (0);
    999 }
   1000 
   1001 /*
   1002  * writing zero into the command-register will reset the controller.
   1003  * This will not interrupt data-transfer commands!
   1004  * Also no interrupt is generated, thus we don't use hdc_command()
   1005  */
   1006 int
   1007 hdc_reset(sc)
   1008 	struct hdcsoftc *sc;
   1009 {
   1010 	trace (("hdc_reset()\n"));
   1011 
   1012 	sc->sc_dkc->dkc_cmd = DKC_CMD_RESET;	/* issue RESET command */
   1013 	hdc_readregs(sc);			/* read the status registers */
   1014 	sc->sc_status = sc->sc_dkc->dkc_stat;
   1015 	if (sc->sc_status != (DKC_ST_DONE|DKC_TC_SUCCESS)) {
   1016 		printf ("RESET command completed with status 0x%x\n",
   1017 			sc->sc_status);
   1018 		return (-1);
   1019 	}
   1020 	return (0);
   1021 }
   1022 
   1023 int hdc_rxselect(struct hdcsoftc *, int);
   1024 
   1025 int
   1026 hdc_rxselect(sc, unit)
   1027 	struct hdcsoftc *sc;
   1028 	int unit;
   1029 {
   1030 	register struct hdc9224_UDCreg *p = &sc->sc_creg;
   1031 	register struct hdc9224_UDCreg *q = &sc->sc_sreg;
   1032 	int error;
   1033 
   1034 	/*
   1035 	 * bring command-regs in some known-to-work state and
   1036 	 * select the drive with the DRIVE SELECT command.
   1037 	 */
   1038 	p->udc_dma7  = 0;
   1039 	p->udc_dma15 = 0;
   1040 	p->udc_dma23 = 0;
   1041 	p->udc_dsect = 1;	/* sectors are numbered 1..15 !!! */
   1042 	p->udc_dhead = 0;
   1043 	p->udc_dcyl  = 0;
   1044 	p->udc_scnt  = 0;
   1045 
   1046 	p->udc_rtcnt = UDC_RC_RX33READ;
   1047 	p->udc_mode  = UDC_MD_RX33;
   1048 	p->udc_term  = UDC_TC_FDD;
   1049 
   1050 	/*
   1051 	 * this is ...
   1052 	 */
   1053 	error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
   1054 
   1055 	if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == 0)) {
   1056 	  printf("\nfloppy-drive not ready (new floppy inserted?)\n\n");
   1057 	  p->udc_rtcnt &= ~UDC_RC_INVRDY;	/* clear INVRDY-flag */
   1058 	  error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
   1059 	  if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == 0)) {
   1060 	    printf("diskette not ready(1): %x/%x\n", error, q->udc_dstat);
   1061 	    printf("floppy-drive offline?\n");
   1062 	    return (-1);
   1063 	  }
   1064 
   1065 	  if (q->udc_dstat & UDC_DS_TRK00)		    /* if track-0 */
   1066 	    error = hdc_command(sc, DKC_CMD_STEPIN_FDD);   /* step inwards */
   1067 	  else						    /* else */
   1068 	    error = hdc_command(sc, DKC_CMD_STEPOUT_FDD);  /* step outwards */
   1069 
   1070 	  if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == UDC_DS_READY)) {
   1071 	    printf("diskette not ready(2): %x/%x\n", error, q->udc_dstat);
   1072 	    printf("No floppy inserted or drive offline\n");
   1073 	    /* return (-1); */
   1074 	  }
   1075 
   1076 	  p->udc_rtcnt |= UDC_RC_INVRDY;
   1077 	  error = hdc_command(sc, DKC_CMD_DRSEL_RX33 | unit);
   1078 	  if ((error != 0) || ((q->udc_dstat & UDC_DS_READY) == 0)) {
   1079 	    printf("diskette not ready(3): %x/%x\n", error, q->udc_dstat);
   1080 	    printf("no floppy inserted or floppy-door open\n");
   1081 	    return(-1);
   1082 	  }
   1083 	  printf("floppy-drive reselected.\n");
   1084 	}
   1085 	if (error)
   1086 		error = hdc_command (sc, DKC_CMD_DRSEL_RX33 | unit);
   1087 
   1088 	return (error);
   1089 }
   1090 
   1091 int hdc_rdselect (struct hdcsoftc *, int);
   1092 
   1093 int
   1094 hdc_rdselect(sc, unit)
   1095 	struct hdcsoftc *sc;
   1096 	int unit;
   1097 {
   1098 	register struct hdc9224_UDCreg *p = &sc->sc_creg;
   1099 	int error;
   1100 
   1101 	/*
   1102 	 * bring "creg" in some known-to-work state and
   1103 	 * select the drive with the DRIVE SELECT command.
   1104 	 */
   1105 	p->udc_dma7  = 0;
   1106 	p->udc_dma15 = 0;
   1107 	p->udc_dma23 = 0;
   1108 	p->udc_dsect = 0;		/* sectors are numbered 0..16 */
   1109 	p->udc_dhead = 0;
   1110 	p->udc_dcyl  = 0;
   1111 	p->udc_scnt  = 0;
   1112 
   1113 	p->udc_rtcnt = UDC_RC_HDD_READ;
   1114 	p->udc_mode  = UDC_MD_HDD;
   1115 	p->udc_term  = UDC_TC_HDD;
   1116 
   1117 	error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
   1118 	if (error)
   1119 		error = hdc_command (sc, DKC_CMD_DRSEL_HDD | unit);
   1120 
   1121 	return (error);
   1122 }
   1123 
   1124 /*
   1125  * bring command-regs into some known-to-work state and select
   1126  * the drive with the DRIVE SELECT command.
   1127  */
   1128 int
   1129 hdc_select(sc, unit)
   1130 	struct hdcsoftc *sc;
   1131 	int unit;
   1132 {
   1133 	int error;
   1134 
   1135 	trace (("hdc_select(%x,%d)\n", sc, unit));
   1136 
   1137 	switch (unit) {
   1138 	case 0:
   1139 	case 1:
   1140 		error = hdc_rdselect(sc, unit);
   1141 		break;
   1142 	case 2:
   1143 		error = hdc_rxselect(sc, unit);
   1144 		/* bertram: delay ??? XXX */
   1145 		break;
   1146 	default:
   1147 		printf("invalid unit %d in hdc_select()\n", unit);
   1148 		error = -1;
   1149 	}
   1150 
   1151 	return (error);
   1152 }
   1153 #endif	/* NHDC > 0 */
   1154