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