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