Home | History | Annotate | Line # | Download | only in ata
wd.c revision 1.63
      1  1.11  deraadt /*
      2  1.41  mycroft  * Copyright (c) 1994 Charles Hannum.
      3   1.1      cgd  * Copyright (c) 1990 The Regents of the University of California.
      4   1.1      cgd  * All rights reserved.
      5   1.1      cgd  *
      6   1.1      cgd  * This code is derived from software contributed to Berkeley by
      7   1.1      cgd  * William Jolitz.
      8   1.1      cgd  *
      9   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     10   1.1      cgd  * modification, are permitted provided that the following conditions
     11   1.1      cgd  * are met:
     12   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     14   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     16   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     17   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     18   1.1      cgd  *    must display the following acknowledgement:
     19   1.1      cgd  *	This product includes software developed by the University of
     20   1.1      cgd  *	California, Berkeley and its contributors.
     21   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     22   1.1      cgd  *    may be used to endorse or promote products derived from this software
     23   1.1      cgd  *    without specific prior written permission.
     24   1.1      cgd  *
     25   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35   1.1      cgd  * SUCH DAMAGE.
     36   1.1      cgd  *
     37  1.15      cgd  *	from: @(#)wd.c	7.2 (Berkeley) 5/9/91
     38  1.63  mycroft  *	$Id: wd.c,v 1.63 1994/03/07 05:54:44 mycroft Exp $
     39   1.1      cgd  */
     40   1.1      cgd 
     41  1.46  mycroft #define	INSTRUMENT	/* instrumentation stuff by Brad Parker */
     42   1.1      cgd 
     43   1.1      cgd #include "wd.h"
     44  1.43  mycroft #if NWDC > 0
     45   1.1      cgd 
     46  1.29  mycroft #include <sys/param.h>
     47  1.29  mycroft #include <sys/dkbad.h>
     48  1.29  mycroft #include <sys/systm.h>
     49  1.43  mycroft #include <sys/kernel.h>
     50  1.29  mycroft #include <sys/conf.h>
     51  1.29  mycroft #include <sys/file.h>
     52  1.29  mycroft #include <sys/stat.h>
     53  1.29  mycroft #include <sys/ioctl.h>
     54  1.29  mycroft #include <sys/disklabel.h>
     55  1.29  mycroft #include <sys/buf.h>
     56  1.29  mycroft #include <sys/uio.h>
     57  1.29  mycroft #include <sys/malloc.h>
     58  1.29  mycroft #include <sys/syslog.h>
     59   1.3  deraadt #ifdef INSTRUMENT
     60  1.29  mycroft #include <sys/dkstat.h>
     61   1.3  deraadt #endif
     62  1.29  mycroft 
     63  1.29  mycroft #include <vm/vm.h>
     64  1.29  mycroft 
     65  1.29  mycroft #include <machine/cpu.h>
     66  1.32  mycroft #include <machine/cpufunc.h>
     67  1.29  mycroft #include <machine/pio.h>
     68  1.29  mycroft 
     69  1.29  mycroft #include <i386/isa/isa.h>
     70  1.29  mycroft #include <i386/isa/isa_device.h>
     71  1.29  mycroft #include <i386/isa/icu.h>
     72  1.29  mycroft #include <i386/isa/wdreg.h>
     73   1.1      cgd 
     74  1.41  mycroft #define WDCNDELAY	100000	/* delay = 100us; so 10s for a controller state change */
     75  1.41  mycroft #define WDCDELAY	100
     76  1.19  deraadt 
     77  1.51  mycroft #if 0
     78  1.41  mycroft /* if you enable this, it will report any delays more than 100us * N long */
     79  1.51  mycroft #define WDCNDELAY_DEBUG	100
     80  1.51  mycroft #endif
     81  1.23  deraadt 
     82  1.21  deraadt #define	WDIORETRIES	5	/* number of retries before giving up */
     83   1.1      cgd 
     84  1.51  mycroft #define WDUNIT(dev)	((minor(dev) & 0xf8) >> 3)
     85  1.51  mycroft #define WDPART(dev)	((minor(dev) & 0x07)     )
     86  1.37  mycroft #define makewddev(maj, unit, part)	(makedev(maj, ((unit << 3) + part)))
     87   1.1      cgd #define WDRAW	3		/* 'd' partition isn't a partition! */
     88   1.1      cgd 
     89   1.1      cgd #define b_cylin	b_resid		/* cylinder number for doing IO to */
     90   1.1      cgd 				/* shares an entry in the buf struct */
     91   1.1      cgd 
     92   1.1      cgd /*
     93   1.1      cgd  * Drive states.  Used to initialize drive.
     94   1.1      cgd  */
     95   1.1      cgd #define	CLOSED		0		/* disk is closed. */
     96   1.1      cgd #define	WANTOPEN	1		/* open requested, not started */
     97   1.1      cgd #define	RECAL		2		/* doing restore */
     98   1.1      cgd #define	OPEN		3		/* done with open */
     99   1.1      cgd 
    100   1.1      cgd /*
    101  1.37  mycroft  * Drive status.
    102   1.1      cgd  */
    103   1.1      cgd struct	disk {
    104   1.1      cgd 	long	dk_bc;		/* byte count left */
    105   1.3  deraadt 	long	dk_bct;		/* total byte count left */
    106   1.1      cgd 	short	dk_skip;	/* blocks already transferred */
    107   1.3  deraadt 	short	dk_skipm;	/* blocks already transferred for multi */
    108   1.3  deraadt 	char	dk_ctrlr;	/* physical controller number */
    109   1.1      cgd 	char	dk_unit;	/* physical unit number */
    110   1.3  deraadt 	char	dk_lunit;	/* logical unit number */
    111   1.1      cgd 	char	dk_state;	/* control state */
    112  1.55  mycroft 	int	dk_timeout;	/* timeout counter */
    113   1.1      cgd 	u_char	dk_status;	/* copy of status reg. */
    114   1.1      cgd 	u_char	dk_error;	/* copy of error reg. */
    115  1.60  mycroft 	u_short	dk_port;	/* i/o port base */
    116   1.1      cgd 
    117   1.3  deraadt 	u_long  dk_copenpart;   /* character units open on this drive */
    118   1.3  deraadt 	u_long  dk_bopenpart;   /* block units open on this drive */
    119   1.3  deraadt 	u_long  dk_openpart;    /* all units open on this drive */
    120   1.1      cgd 	short	dk_wlabel;	/* label writable? */
    121   1.1      cgd 	short	dk_flags;	/* drive characteistics found */
    122   1.1      cgd #define	DKFL_SINGLE	0x00004	 /* sector at a time mode */
    123   1.1      cgd #define	DKFL_ERROR	0x00008	 /* processing a disk error */
    124   1.1      cgd #define	DKFL_BSDLABEL	0x00010	 /* has a BSD disk label */
    125   1.1      cgd #define	DKFL_BADSECT	0x00020	 /* has a bad144 badsector table */
    126   1.1      cgd #define	DKFL_WRITEPROT	0x00040	 /* manual unit write protect */
    127   1.1      cgd 	struct wdparams dk_params; /* ESDI/IDE drive/controller parameters */
    128   1.1      cgd 	struct disklabel dk_dd;	/* device configuration data */
    129  1.14  deraadt 	struct cpu_disklabel dk_cpd;
    130   1.3  deraadt 	long	dk_badsect[127];	/* 126 plus trailing -1 marker */
    131   1.1      cgd };
    132   1.1      cgd 
    133   1.9  deraadt struct board {
    134   1.9  deraadt 	short dkc_port;
    135   1.9  deraadt };
    136   1.9  deraadt 
    137   1.9  deraadt struct	board	wdcontroller[NWDC];
    138   1.3  deraadt struct	disk	*wddrives[NWD];		/* table of units */
    139   1.3  deraadt struct	buf	wdtab[NWDC];		/* various per-controller info */
    140   1.3  deraadt struct	buf	wdutab[NWD];		/* head of queue per drive */
    141   1.3  deraadt struct	buf	rwdbuf[NWD];		/* buffers for raw IO */
    142   1.3  deraadt long	wdxfer[NWD];			/* count of transfers */
    143   1.3  deraadt 
    144   1.3  deraadt int wdprobe(), wdattach();
    145   1.3  deraadt 
    146   1.3  deraadt struct	isa_driver wdcdriver = {
    147   1.3  deraadt 	wdprobe, wdattach, "wdc",
    148   1.1      cgd };
    149   1.1      cgd 
    150  1.55  mycroft static void wdustart __P((struct disk *));
    151  1.55  mycroft static void wdstart __P((int));
    152  1.60  mycroft static int wdcommand __P((struct disk *, int, int, int, int, int));
    153  1.55  mycroft static int wdcontrol __P((struct buf *));
    154  1.55  mycroft static int wdsetctlr __P((struct disk *));
    155  1.55  mycroft static int wdgetctlr __P((struct disk *));
    156  1.55  mycroft static void bad144intern __P((struct disk *));
    157  1.55  mycroft static int wdreset __P((struct disk *, int));
    158  1.60  mycroft static void wdtimeout __P((caddr_t));
    159  1.63  mycroft void wddisksort __P((struct buf *, struct buf *));
    160  1.63  mycroft static void wderror __P((struct disk *, struct buf *, char *));
    161  1.63  mycroft int wdwait __P((struct disk *, int));
    162  1.63  mycroft #define	wait_for_drq(d)		wdwait(d, WDCS_DRQ)
    163  1.63  mycroft #define	wait_for_ready(d)	wdwait(d, WDCS_READY | WDCS_SEEKCMPLT)
    164  1.63  mycroft #define	wait_for_unbusy(d)	wdwait(d, 0)
    165   1.1      cgd 
    166   1.1      cgd /*
    167   1.1      cgd  * Probe for controller.
    168   1.1      cgd  */
    169   1.1      cgd int
    170  1.55  mycroft wdprobe(isa_dev)
    171  1.55  mycroft 	struct isa_device *isa_dev;
    172   1.1      cgd {
    173   1.1      cgd 	struct disk *du;
    174  1.60  mycroft 	u_short wdc;
    175   1.1      cgd 
    176  1.55  mycroft 	if (isa_dev->id_unit >= NWDC)
    177  1.37  mycroft 		return 0;
    178   1.1      cgd 
    179  1.37  mycroft 	du = (struct disk *)malloc(sizeof(struct disk), M_TEMP, M_NOWAIT);
    180   1.3  deraadt 	bzero(du, sizeof(struct disk));
    181   1.3  deraadt 
    182  1.55  mycroft 	du->dk_ctrlr = isa_dev->id_unit;
    183   1.3  deraadt 	du->dk_unit = 0;
    184   1.3  deraadt 	du->dk_lunit = 0;
    185  1.55  mycroft 	wdcontroller[isa_dev->id_unit].dkc_port = isa_dev->id_iobase;
    186   1.1      cgd 
    187  1.55  mycroft 	wdc = du->dk_port = isa_dev->id_iobase;
    188   1.3  deraadt 
    189   1.1      cgd 	/* check if we have registers that work */
    190   1.3  deraadt 	outb(wdc+wd_error, 0x5a);	/* error register not writable */
    191   1.3  deraadt 	outb(wdc+wd_cyl_lo, 0xa5);	/* but all of cyllo are implemented */
    192  1.37  mycroft 	if (inb(wdc+wd_error) == 0x5a || inb(wdc+wd_cyl_lo) != 0xa5)
    193   1.1      cgd 		goto nodevice;
    194   1.1      cgd 
    195  1.51  mycroft 	wdreset(du, 0);
    196   1.1      cgd 
    197   1.1      cgd 	/* execute a controller only command */
    198  1.63  mycroft 	if (wdcommand(du, 0, 0, 0, 0, WDCC_DIAGNOSE) != 0)
    199   1.1      cgd 		goto nodevice;
    200   1.1      cgd 
    201   1.3  deraadt 	free(du, M_TEMP);
    202  1.37  mycroft 	return 8;
    203   1.1      cgd 
    204   1.1      cgd nodevice:
    205   1.1      cgd 	free(du, M_TEMP);
    206  1.37  mycroft 	return 0;
    207   1.1      cgd }
    208   1.1      cgd 
    209   1.1      cgd /*
    210   1.9  deraadt  * Called for the controller too
    211   1.1      cgd  * Attach each drive if possible.
    212   1.1      cgd  */
    213   1.1      cgd int
    214  1.55  mycroft wdattach(isa_dev)
    215  1.55  mycroft 	struct isa_device *isa_dev;
    216   1.1      cgd {
    217   1.3  deraadt 	int unit, lunit;
    218   1.3  deraadt 	struct disk *du;
    219  1.56  mycroft 	int i, blank;
    220   1.3  deraadt 
    221  1.55  mycroft 	if (isa_dev->id_masunit == -1)
    222  1.37  mycroft 		return 0;
    223  1.55  mycroft 	if (isa_dev->id_masunit >= NWDC)
    224  1.37  mycroft 		return 0;
    225   1.3  deraadt 
    226  1.55  mycroft 	lunit = isa_dev->id_unit;
    227  1.11  deraadt 	if (lunit == -1) {
    228  1.55  mycroft 		printf("wdc%d: cannot support unit ?\n", isa_dev->id_masunit);
    229  1.11  deraadt 		return 0;
    230  1.11  deraadt 	}
    231   1.9  deraadt 	if (lunit >= NWD)
    232  1.37  mycroft 		return 0;
    233  1.55  mycroft 	unit = isa_dev->id_physid;
    234   1.3  deraadt 
    235  1.37  mycroft 	du = wddrives[lunit] =
    236  1.37  mycroft 	    (struct disk *)malloc(sizeof(struct disk), M_TEMP, M_NOWAIT);
    237   1.9  deraadt 	bzero(du, sizeof(struct disk));
    238   1.9  deraadt 	bzero(&wdutab[lunit], sizeof(struct buf));
    239   1.9  deraadt 	bzero(&rwdbuf[lunit], sizeof(struct buf));
    240   1.9  deraadt 	wdxfer[lunit] = 0;
    241  1.55  mycroft 	du->dk_ctrlr = isa_dev->id_masunit;
    242   1.9  deraadt 	du->dk_unit = unit;
    243   1.9  deraadt 	du->dk_lunit = lunit;
    244  1.55  mycroft 	du->dk_port = wdcontroller[isa_dev->id_masunit].dkc_port;
    245   1.9  deraadt 
    246  1.56  mycroft 	if (wdgetctlr(du) != 0) {
    247  1.37  mycroft 		/*printf("wd%d at wdc%d slave %d -- error\n", lunit,
    248  1.55  mycroft 		    isa_dev->id_masunit, unit);*/
    249   1.9  deraadt 		wddrives[lunit] = 0;
    250   1.9  deraadt 		free(du, M_TEMP);
    251   1.9  deraadt 		return 0;
    252   1.1      cgd 	}
    253  1.56  mycroft 
    254  1.56  mycroft 	printf("wd%d at wdc%d targ %d: ", isa_dev->id_unit,
    255  1.56  mycroft 	    isa_dev->id_masunit, isa_dev->id_physid);
    256  1.56  mycroft 	if (du->dk_params.wdp_heads == 0)
    257  1.56  mycroft 		printf("(unknown size) <");
    258  1.56  mycroft 	else
    259  1.56  mycroft 		printf("%dMB %d cyl, %d head, %d sec <",
    260  1.56  mycroft 		    du->dk_dd.d_ncylinders * du->dk_dd.d_secpercyl /
    261  1.56  mycroft 		    (1048576 / DEV_BSIZE),
    262  1.56  mycroft 		    du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks,
    263  1.56  mycroft 		    du->dk_dd.d_nsectors);
    264  1.56  mycroft 	for (i = blank = 0; i < sizeof(du->dk_params.wdp_model); i++) {
    265  1.56  mycroft 		char c = du->dk_params.wdp_model[i];
    266  1.56  mycroft 		if (c == '\0')
    267  1.56  mycroft 			break;
    268  1.56  mycroft 		if (c != ' ') {
    269  1.56  mycroft 			if (blank)
    270  1.56  mycroft 				printf(" %c", c);
    271  1.56  mycroft 			else
    272  1.56  mycroft 				printf("%c", c);
    273  1.56  mycroft 			blank = 0;
    274  1.56  mycroft 		} else
    275  1.56  mycroft 			blank = 1;
    276  1.56  mycroft 	}
    277  1.56  mycroft 	printf(">\n");
    278  1.56  mycroft 
    279  1.56  mycroft 	wdtimeout((caddr_t)du);
    280  1.56  mycroft 
    281   1.9  deraadt 	return 1;
    282   1.1      cgd }
    283   1.1      cgd 
    284   1.1      cgd /* Read/write routine for a buffer.  Finds the proper unit, range checks
    285   1.1      cgd  * arguments, and schedules the transfer.  Does not wait for the transfer
    286   1.1      cgd  * to complete.  Multi-page transfers are supported.  All I/O requests must
    287   1.1      cgd  * be a multiple of a sector in length.
    288   1.1      cgd  */
    289   1.1      cgd int
    290  1.55  mycroft wdstrategy(bp)
    291  1.55  mycroft 	struct buf *bp;
    292   1.1      cgd {
    293  1.55  mycroft 	struct buf *dp;
    294   1.1      cgd 	struct disk *du;	/* Disk unit to do the IO.	*/
    295  1.51  mycroft 	int lunit = WDUNIT(bp->b_dev);
    296  1.37  mycroft 	int s;
    297   1.3  deraadt 
    298   1.1      cgd 	/* valid unit, controller, and request?  */
    299   1.3  deraadt 	if (lunit >= NWD || bp->b_blkno < 0 ||
    300  1.37  mycroft 	    howmany(bp->b_bcount, DEV_BSIZE) >= (1 << NBBY) ||
    301   1.3  deraadt 	    (du = wddrives[lunit]) == 0) {
    302   1.1      cgd 		bp->b_error = EINVAL;
    303   1.1      cgd 		bp->b_flags |= B_ERROR;
    304   1.1      cgd 		goto done;
    305   1.1      cgd 	}
    306   1.3  deraadt 
    307   1.1      cgd 	/* "soft" write protect check */
    308   1.1      cgd 	if ((du->dk_flags & DKFL_WRITEPROT) && (bp->b_flags & B_READ) == 0) {
    309   1.1      cgd 		bp->b_error = EROFS;
    310   1.1      cgd 		bp->b_flags |= B_ERROR;
    311   1.1      cgd 		goto done;
    312   1.1      cgd 	}
    313   1.3  deraadt 
    314   1.1      cgd 	/* have partitions and want to use them? */
    315  1.51  mycroft 	if ((du->dk_flags & DKFL_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW) {
    316   1.1      cgd 		/*
    317   1.1      cgd 		 * do bounds checking, adjust transfer. if error, process.
    318   1.1      cgd 		 * if end of partition, just return
    319   1.1      cgd 		 */
    320   1.1      cgd 		if (bounds_check_with_label(bp, &du->dk_dd, du->dk_wlabel) <= 0)
    321   1.1      cgd 			goto done;
    322   1.1      cgd 		/* otherwise, process transfer request */
    323   1.1      cgd 	}
    324   1.3  deraadt 
    325  1.38  mycroft 	/* don't bother */
    326  1.38  mycroft 	bp->b_cylin = 0;
    327  1.38  mycroft 
    328   1.1      cgd 	/* queue transfer on drive, activate drive and controller if idle */
    329   1.3  deraadt 	dp = &wdutab[lunit];
    330   1.1      cgd 	s = splbio();
    331  1.49  mycroft 	wddisksort(dp, bp);
    332   1.1      cgd 	if (dp->b_active == 0)
    333   1.1      cgd 		wdustart(du);		/* start drive */
    334   1.3  deraadt 	if (wdtab[du->dk_ctrlr].b_active == 0)
    335   1.3  deraadt 		wdstart(du->dk_ctrlr);		/* start controller */
    336   1.1      cgd 	splx(s);
    337   1.3  deraadt 	return 0;
    338   1.3  deraadt 
    339   1.1      cgd done:
    340   1.1      cgd 	/* toss transfer, we're done early */
    341   1.1      cgd 	biodone(bp);
    342   1.1      cgd }
    343   1.1      cgd 
    344   1.1      cgd /*
    345  1.49  mycroft  * Need to skip over multitransfer bufs.
    346  1.49  mycroft  */
    347  1.49  mycroft void
    348  1.55  mycroft wddisksort(dp, bp)
    349  1.55  mycroft 	struct buf *dp, *bp;
    350  1.49  mycroft {
    351  1.55  mycroft 	struct buf *ap;
    352  1.49  mycroft 
    353  1.49  mycroft 	while ((ap = dp->b_actf) && ap->b_flags & B_XXX)
    354  1.49  mycroft 		dp = ap;
    355  1.49  mycroft 	disksort(dp, bp);
    356  1.49  mycroft }
    357  1.49  mycroft 
    358  1.49  mycroft /*
    359   1.1      cgd  * Routine to queue a command to the controller.  The unit's
    360   1.1      cgd  * request is linked into the active list for the controller.
    361   1.1      cgd  * If the controller is idle, the transfer is started.
    362   1.1      cgd  */
    363   1.1      cgd static void
    364  1.55  mycroft wdustart(du)
    365  1.55  mycroft 	struct disk *du;
    366   1.1      cgd {
    367  1.55  mycroft 	struct buf *dp = &wdutab[du->dk_lunit];
    368   1.3  deraadt 	int ctrlr = du->dk_ctrlr;
    369   1.3  deraadt 
    370   1.1      cgd 	/* unit already active? */
    371   1.1      cgd 	if (dp->b_active)
    372   1.1      cgd 		return;
    373   1.3  deraadt 
    374   1.1      cgd 	/* anything to start? */
    375  1.53  mycroft 	if (dp->b_actf == NULL)
    376   1.1      cgd 		return;
    377   1.3  deraadt 
    378   1.1      cgd 	/* link onto controller queue */
    379   1.1      cgd 	dp->b_forw = NULL;
    380  1.51  mycroft 	if (wdtab[ctrlr].b_forw == NULL)
    381  1.51  mycroft 		wdtab[ctrlr].b_forw = dp;
    382   1.1      cgd 	else
    383   1.3  deraadt 		wdtab[ctrlr].b_actl->b_forw = dp;
    384   1.3  deraadt 	wdtab[ctrlr].b_actl = dp;
    385   1.3  deraadt 
    386   1.1      cgd 	/* mark the drive unit as busy */
    387   1.1      cgd 	dp->b_active = 1;
    388   1.1      cgd }
    389   1.1      cgd 
    390   1.1      cgd /*
    391   1.1      cgd  * Controller startup routine.  This does the calculation, and starts
    392   1.1      cgd  * a single-sector read or write operation.  Called to start a transfer,
    393   1.1      cgd  * or from the interrupt routine to continue a multi-sector transfer.
    394   1.1      cgd  * RESTRICTIONS:
    395   1.1      cgd  * 1.	The transfer length must be an exact multiple of the sector size.
    396   1.1      cgd  */
    397   1.1      cgd static void
    398  1.55  mycroft wdstart(ctrlr)
    399  1.55  mycroft 	int ctrlr;
    400   1.1      cgd {
    401   1.1      cgd 	register struct disk *du;	/* disk unit for IO */
    402   1.1      cgd 	register struct buf *bp;
    403   1.1      cgd 	struct disklabel *lp;
    404   1.1      cgd 	struct buf *dp;
    405  1.54  mycroft 	long blknum, cylin, head, sector;
    406  1.60  mycroft 	long secpertrk, secpercyl;
    407   1.3  deraadt 	int xfrblknum;
    408  1.60  mycroft 	int lunit;
    409   1.3  deraadt 
    410   1.1      cgd loop:
    411   1.1      cgd 	/* is there a drive for the controller to do a transfer with? */
    412  1.51  mycroft 	dp = wdtab[ctrlr].b_forw;
    413  1.53  mycroft 	if (dp == NULL) {
    414  1.53  mycroft 		wdtab[ctrlr].b_active = 0;
    415   1.1      cgd 		return;
    416  1.53  mycroft 	}
    417   1.3  deraadt 
    418   1.1      cgd 	/* is there a transfer to this drive ? if so, link it on
    419   1.1      cgd 	   the controller's queue */
    420   1.1      cgd 	bp = dp->b_actf;
    421   1.1      cgd 	if (bp == NULL) {
    422  1.53  mycroft 		dp->b_active = 0;
    423  1.51  mycroft 		wdtab[ctrlr].b_forw = dp->b_forw;
    424   1.1      cgd 		goto loop;
    425   1.1      cgd 	}
    426   1.3  deraadt 
    427   1.1      cgd 	/* obtain controller and drive information */
    428  1.51  mycroft 	lunit = WDUNIT(bp->b_dev);
    429   1.3  deraadt 	du = wddrives[lunit];
    430  1.51  mycroft 
    431  1.51  mycroft 	/* clear any pending timeout, just in case */
    432  1.55  mycroft 	du->dk_timeout = 0;
    433   1.3  deraadt 
    434   1.1      cgd 	/* if not really a transfer, do control operations specially */
    435   1.1      cgd 	if (du->dk_state < OPEN) {
    436   1.1      cgd 		(void) wdcontrol(bp);
    437   1.1      cgd 		return;
    438   1.1      cgd 	}
    439   1.3  deraadt 
    440   1.1      cgd 	/* calculate transfer details */
    441   1.1      cgd 	blknum = bp->b_blkno + du->dk_skip;
    442  1.44  mycroft #ifdef WDDEBUG
    443   1.1      cgd 	if (du->dk_skip == 0)
    444   1.3  deraadt 		printf("\nwdstart %d: %s %d@%d; map ", lunit,
    445  1.37  mycroft 		    (bp->b_flags & B_READ) ? "read" : "write", bp->b_bcount,
    446  1.37  mycroft 		    blknum);
    447   1.1      cgd 	else
    448   1.3  deraadt 		printf(" %d)%x", du->dk_skip, inb(du->dk_port+wd_altsts));
    449   1.1      cgd #endif
    450  1.37  mycroft 	if (du->dk_skip == 0)
    451   1.1      cgd 		du->dk_bc = bp->b_bcount;
    452   1.3  deraadt 	if (du->dk_skipm == 0) {
    453   1.3  deraadt 		struct buf *oldbp, *nextbp;
    454   1.3  deraadt 		oldbp = bp;
    455  1.33  mycroft 		nextbp = bp->b_actf;
    456   1.3  deraadt 		du->dk_bct = du->dk_bc;
    457   1.3  deraadt 		oldbp->b_flags |= B_XXX;
    458  1.44  mycroft 		while (nextbp && (oldbp->b_flags & DKFL_SINGLE) == 0 &&
    459  1.44  mycroft 		    oldbp->b_dev == nextbp->b_dev && nextbp->b_blkno ==
    460  1.44  mycroft 		    (oldbp->b_blkno + (oldbp->b_bcount / DEV_BSIZE)) &&
    461  1.37  mycroft 		    (oldbp->b_flags & B_READ) == (nextbp->b_flags & B_READ)) {
    462  1.37  mycroft 			if ((du->dk_bct + nextbp->b_bcount) / DEV_BSIZE >= 240)
    463   1.3  deraadt 				break;
    464   1.3  deraadt 			du->dk_bct += nextbp->b_bcount;
    465  1.49  mycroft 			nextbp->b_flags |= B_XXX;
    466   1.3  deraadt 			oldbp = nextbp;
    467  1.33  mycroft 			nextbp = nextbp->b_actf;
    468   1.3  deraadt 		}
    469   1.3  deraadt 	}
    470   1.3  deraadt 
    471   1.1      cgd 	lp = &du->dk_dd;
    472   1.1      cgd 	secpertrk = lp->d_nsectors;
    473   1.1      cgd 	secpercyl = lp->d_secpercyl;
    474  1.51  mycroft 	if ((du->dk_flags & DKFL_BSDLABEL) != 0 && WDPART(bp->b_dev) != WDRAW)
    475  1.51  mycroft 		blknum += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
    476   1.1      cgd 	cylin = blknum / secpercyl;
    477   1.1      cgd 	head = (blknum % secpercyl) / secpertrk;
    478   1.1      cgd 	sector = blknum % secpertrk;
    479   1.3  deraadt 
    480   1.3  deraadt 	/* Check for bad sectors if we have them, and not formatting */
    481   1.3  deraadt 	/* Only do this in single-sector mode, or when starting a */
    482   1.3  deraadt 	/* multiple-sector transfer. */
    483  1.37  mycroft 	if ((du->dk_flags & DKFL_BADSECT) &&
    484  1.44  mycroft #ifdef B_FORMAT
    485  1.37  mycroft 	    (bp->b_flags & B_FORMAT) == 0 &&
    486   1.3  deraadt #endif
    487  1.44  mycroft 	    (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE))) {
    488   1.1      cgd 
    489   1.3  deraadt 		long blkchk, blkend, blknew;
    490   1.3  deraadt 		int i;
    491   1.3  deraadt 
    492   1.3  deraadt 		blkend = blknum + howmany(du->dk_bct, DEV_BSIZE) - 1;
    493   1.3  deraadt 		for (i = 0; (blkchk = du->dk_badsect[i]) != -1; i++) {
    494  1.37  mycroft 			if (blkchk > blkend)
    495   1.3  deraadt 				break;	/* transfer is completely OK; done */
    496  1.37  mycroft 			if (blkchk == blknum) {
    497  1.37  mycroft 				blknew =
    498  1.37  mycroft 				    lp->d_secperunit - lp->d_nsectors - i - 1;
    499   1.3  deraadt 				cylin = blknew / secpercyl;
    500   1.3  deraadt 				head = (blknew % secpercyl) / secpertrk;
    501   1.3  deraadt 				sector = blknew % secpertrk;
    502   1.3  deraadt 				du->dk_flags |= DKFL_SINGLE;
    503   1.3  deraadt 				/* found and replaced first blk of transfer; done */
    504   1.3  deraadt 				break;
    505   1.3  deraadt 			} else if (blkchk > blknum) {
    506   1.3  deraadt 				du->dk_flags |= DKFL_SINGLE;
    507   1.3  deraadt 				break;	/* bad block inside transfer; done */
    508   1.3  deraadt 			}
    509   1.3  deraadt 		}
    510   1.3  deraadt 	}
    511  1.37  mycroft 	if (du->dk_flags & DKFL_SINGLE) {
    512   1.3  deraadt 		du->dk_bct = du->dk_bc;
    513   1.3  deraadt 		du->dk_skipm = du->dk_skip;
    514   1.3  deraadt 	}
    515   1.3  deraadt 
    516   1.3  deraadt #ifdef WDDEBUG
    517  1.51  mycroft 	printf("c%d h%d s%d ", cylin, head, sector);
    518   1.1      cgd #endif
    519   1.3  deraadt 
    520  1.49  mycroft 	sector++;	/* sectors begin with 1, not 0 */
    521   1.3  deraadt 
    522   1.3  deraadt 	wdtab[ctrlr].b_active = 1;		/* mark controller active */
    523   1.3  deraadt 
    524   1.3  deraadt #ifdef INSTRUMENT
    525   1.3  deraadt 	/* instrumentation */
    526   1.3  deraadt 	if (du->dk_unit >= 0 && du->dk_skip == 0) {
    527   1.3  deraadt 		dk_busy |= 1 << du->dk_lunit;
    528   1.3  deraadt 		dk_wds[du->dk_lunit] += bp->b_bcount >> 6;
    529   1.3  deraadt 	}
    530   1.3  deraadt 	if (du->dk_unit >= 0 && du->dk_skipm == 0) {
    531   1.3  deraadt 		++dk_seek[du->dk_lunit];
    532   1.3  deraadt 		++dk_xfer[du->dk_lunit];
    533   1.3  deraadt 	}
    534   1.3  deraadt #endif
    535   1.1      cgd 
    536  1.19  deraadt retry:
    537   1.1      cgd 	/* if starting a multisector transfer, or doing single transfers */
    538   1.3  deraadt 	if (du->dk_skipm == 0 || (du->dk_flags & DKFL_SINGLE)) {
    539  1.60  mycroft 		int command, count;
    540  1.51  mycroft 
    541   1.3  deraadt 		if (wdtab[ctrlr].b_errcnt && (bp->b_flags & B_READ) == 0) {
    542   1.1      cgd 			du->dk_bc += DEV_BSIZE;
    543   1.3  deraadt 			du->dk_bct += DEV_BSIZE;
    544   1.3  deraadt 		}
    545   1.1      cgd 
    546  1.37  mycroft #ifdef B_FORMAT
    547   1.1      cgd 		if (bp->b_flags & B_FORMAT) {
    548  1.60  mycroft 			sector = lp->d_gap3;
    549  1.60  mycroft 			count = lp->d_nsectors;
    550  1.60  mycroft 			command = WDCC_FORMAT;
    551   1.1      cgd 		} else {
    552   1.3  deraadt 			if (du->dk_flags & DKFL_SINGLE)
    553  1.60  mycroft 				count = 1;
    554   1.3  deraadt 			else
    555  1.60  mycroft 				count = howmany(du->dk_bct, DEV_BSIZE);
    556  1.60  mycroft 			command =
    557  1.60  mycroft 			    (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    558   1.3  deraadt 		}
    559   1.3  deraadt #else
    560   1.1      cgd 		if (du->dk_flags & DKFL_SINGLE)
    561  1.60  mycroft 			count = 1;
    562   1.1      cgd 		else
    563  1.60  mycroft 			count = howmany(du->dk_bct, DEV_BSIZE);
    564  1.60  mycroft 		command = (bp->b_flags & B_READ) ? WDCC_READ : WDCC_WRITE;
    565   1.1      cgd #endif
    566   1.3  deraadt 
    567   1.1      cgd 		/* initiate command! */
    568  1.63  mycroft 		while (wdcommand(du, cylin, head, sector, count, command) < 0) {
    569  1.63  mycroft 			wderror(du, NULL,
    570  1.63  mycroft 			    "wdstart: timeout waiting for unbusy");
    571  1.51  mycroft 			wdreset(du, 1);
    572  1.51  mycroft 		}
    573  1.44  mycroft #ifdef WDDEBUG
    574  1.37  mycroft 		printf("sector %d cylin %d head %d addr %x sts %x\n", sector,
    575  1.63  mycroft 		    cylin, head, bp->b_un.b_addr, inb(du->dk_port+wd_altsts));
    576   1.1      cgd #endif
    577   1.1      cgd 	}
    578   1.3  deraadt 
    579  1.60  mycroft 	du->dk_timeout = 2;
    580  1.60  mycroft 
    581   1.1      cgd 	/* if this is a read operation, just go away until it's done.	*/
    582  1.60  mycroft 	if (bp->b_flags & B_READ)
    583   1.3  deraadt 		return;
    584   1.3  deraadt 
    585  1.51  mycroft 	if (wait_for_drq(du) < 0) {
    586  1.63  mycroft 		/* XXX seems wrong */
    587  1.51  mycroft 		wdreset(du, 1);
    588  1.19  deraadt 		goto retry;
    589  1.19  deraadt 	}
    590  1.60  mycroft 
    591   1.1      cgd 	/* then send it! */
    592  1.63  mycroft 	outsw(du->dk_port+wd_data, bp->b_un.b_addr + du->dk_skip * DEV_BSIZE,
    593  1.44  mycroft 	    DEV_BSIZE / sizeof(short));
    594  1.51  mycroft 
    595   1.1      cgd 	du->dk_bc -= DEV_BSIZE;
    596   1.3  deraadt 	du->dk_bct -= DEV_BSIZE;
    597   1.1      cgd }
    598   1.1      cgd 
    599   1.1      cgd /* Interrupt routine for the controller.  Acknowledge the interrupt, check for
    600   1.1      cgd  * errors on the current operation, mark it done if necessary, and start
    601   1.1      cgd  * the next request.  Also check for a partially done transfer, and
    602   1.1      cgd  * continue with the next chunk if so.
    603   1.1      cgd  */
    604   1.1      cgd void
    605  1.55  mycroft wdintr(ctrlr)
    606  1.55  mycroft 	int ctrlr;
    607   1.1      cgd {
    608   1.1      cgd 	register struct	disk *du;
    609   1.1      cgd 	register struct buf *bp, *dp;
    610  1.59  mycroft 
    611  1.59  mycroft 	/* clear the pending interrupt */
    612  1.59  mycroft 	(void) inb(wdcontroller[ctrlr].dkc_port+wd_status);
    613  1.51  mycroft 
    614   1.3  deraadt 	if (!wdtab[ctrlr].b_active) {
    615   1.3  deraadt 		printf("wdc%d: extra interrupt\n", ctrlr);
    616   1.1      cgd 		return;
    617   1.1      cgd 	}
    618   1.3  deraadt 
    619  1.51  mycroft 	dp = wdtab[ctrlr].b_forw;
    620   1.1      cgd 	bp = dp->b_actf;
    621  1.51  mycroft 	du = wddrives[WDUNIT(bp->b_dev)];
    622  1.55  mycroft 	du->dk_timeout = 0;
    623   1.3  deraadt 
    624  1.44  mycroft #ifdef WDDEBUG
    625   1.3  deraadt 	printf("I%d ", ctrlr);
    626   1.1      cgd #endif
    627  1.13  deraadt 
    628  1.63  mycroft 	if (wait_for_unbusy(du) < 0) {
    629  1.63  mycroft 		wderror(du, NULL, "wdintr: timeout waiting for unbusy");
    630  1.63  mycroft 		du->dk_status |= WDCS_ERR;	/* XXX */
    631  1.27      cgd 	}
    632   1.3  deraadt 
    633   1.1      cgd 	/* is it not a transfer, but a control operation? */
    634   1.1      cgd 	if (du->dk_state < OPEN) {
    635  1.47  mycroft 		wdtab[ctrlr].b_active = 0;
    636  1.62  mycroft 		switch (wdcontrol(bp)) {
    637  1.62  mycroft 		case -1:
    638  1.62  mycroft 			goto done;
    639  1.62  mycroft 		case 1:
    640   1.3  deraadt 			wdstart(ctrlr);
    641  1.62  mycroft 			break;
    642  1.62  mycroft 		}
    643   1.1      cgd 		return;
    644   1.1      cgd 	}
    645   1.3  deraadt 
    646   1.1      cgd 	/* have we an error? */
    647  1.63  mycroft 	if (du->dk_status & (WDCS_ERR | WDCS_ECCCOR)) {
    648  1.51  mycroft 	lose:
    649  1.44  mycroft #ifdef WDDEBUG
    650  1.63  mycroft 		wderror(du, NULL, "wdintr");
    651   1.1      cgd #endif
    652  1.37  mycroft 		if ((du->dk_flags & DKFL_SINGLE) == 0) {
    653  1.37  mycroft 			du->dk_flags |= DKFL_ERROR;
    654   1.1      cgd 			goto outt;
    655   1.1      cgd 		}
    656   1.1      cgd #ifdef B_FORMAT
    657   1.1      cgd 		if (bp->b_flags & B_FORMAT) {
    658  1.46  mycroft 			bp->b_error = EIO;
    659   1.1      cgd 			bp->b_flags |= B_ERROR;
    660   1.1      cgd 			goto done;
    661   1.1      cgd 		}
    662   1.1      cgd #endif
    663   1.3  deraadt 
    664   1.1      cgd 		/* error or error correction? */
    665  1.63  mycroft 		if (du->dk_status & WDCS_ERR) {
    666  1.37  mycroft 			if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
    667   1.3  deraadt 				wdtab[ctrlr].b_active = 0;
    668  1.37  mycroft 			else {
    669  1.63  mycroft 				wderror(du, bp, "hard error");
    670  1.49  mycroft 				bp->b_error = EIO;
    671   1.1      cgd 				bp->b_flags |= B_ERROR;	/* flag the error */
    672   1.1      cgd 			}
    673  1.62  mycroft 		} else
    674  1.63  mycroft 			wderror(du, bp, "soft ecc");
    675   1.1      cgd 	}
    676   1.3  deraadt 
    677   1.1      cgd 	/*
    678   1.1      cgd 	 * If this was a successful read operation, fetch the data.
    679   1.1      cgd 	 */
    680  1.37  mycroft 	if (((bp->b_flags & (B_READ | B_ERROR)) == B_READ) &&
    681  1.37  mycroft 	    wdtab[ctrlr].b_active) {
    682  1.51  mycroft 		int chk;
    683   1.3  deraadt 
    684   1.1      cgd 		chk = min(DEV_BSIZE / sizeof(short), du->dk_bc / sizeof(short));
    685   1.3  deraadt 
    686   1.1      cgd 		/* ready to receive data? */
    687  1.63  mycroft 		if (wait_for_drq(du) != 0) {
    688  1.63  mycroft 			wderror(du, NULL, "wdintr: read error detected late");
    689  1.51  mycroft 			goto lose;
    690  1.27      cgd 		}
    691  1.27      cgd 
    692   1.1      cgd 		/* suck in data */
    693  1.63  mycroft 		insw(du->dk_port+wd_data,
    694  1.60  mycroft 		    bp->b_un.b_addr + du->dk_skip * DEV_BSIZE, chk);
    695   1.1      cgd 		du->dk_bc -= chk * sizeof(short);
    696   1.3  deraadt 		du->dk_bct -= chk * sizeof(short);
    697   1.3  deraadt 
    698   1.1      cgd 		/* for obselete fractional sector reads */
    699   1.3  deraadt 		while (chk++ < (DEV_BSIZE / sizeof(short)))
    700  1.63  mycroft 			(void) inw(du->dk_port+wd_data);
    701   1.1      cgd 	}
    702   1.3  deraadt 
    703   1.3  deraadt 	wdxfer[du->dk_lunit]++;
    704  1.46  mycroft outt:
    705   1.3  deraadt 	if (wdtab[ctrlr].b_active) {
    706   1.3  deraadt #ifdef INSTRUMENT
    707   1.3  deraadt 		if (du->dk_unit >= 0)
    708  1.37  mycroft 			dk_busy &= ~(1 << du->dk_unit);
    709   1.3  deraadt #endif
    710   1.1      cgd 		if ((bp->b_flags & B_ERROR) == 0) {
    711  1.37  mycroft 			du->dk_skip++;	/* Add to succ. sect */
    712  1.37  mycroft 			du->dk_skipm++;	/* Add to succ. sect for multitransfer */
    713  1.62  mycroft 			if (wdtab[ctrlr].b_errcnt)
    714  1.63  mycroft 				wderror(du, bp, "soft error");
    715   1.3  deraadt 			wdtab[ctrlr].b_errcnt = 0;
    716   1.3  deraadt 
    717   1.1      cgd 			/* see if more to transfer */
    718   1.1      cgd 			if (du->dk_bc > 0 && (du->dk_flags & DKFL_ERROR) == 0) {
    719  1.49  mycroft 				wdtab[ctrlr].b_active = 0;
    720  1.48  mycroft 				wdstart(ctrlr);
    721  1.48  mycroft 				return;	/* next chunk is started */
    722  1.37  mycroft 			} else if ((du->dk_flags & (DKFL_SINGLE | DKFL_ERROR)) == DKFL_ERROR) {
    723   1.1      cgd 				du->dk_skip = 0;
    724   1.3  deraadt 				du->dk_skipm = 0;
    725   1.1      cgd 				du->dk_flags &= ~DKFL_ERROR;
    726  1.60  mycroft 				du->dk_flags |= DKFL_SINGLE;
    727  1.51  mycroft 				wdtab[ctrlr].b_active = 0;
    728   1.3  deraadt 				wdstart(ctrlr);
    729  1.51  mycroft 				return;	/* redo xfer sector by sector */
    730   1.1      cgd 			}
    731   1.1      cgd 		}
    732   1.1      cgd 
    733  1.62  mycroft 	done:
    734   1.1      cgd 		/* done with this transfer, with or without error */
    735   1.1      cgd 		du->dk_flags &= ~DKFL_SINGLE;
    736   1.3  deraadt 		wdtab[ctrlr].b_errcnt = 0;
    737  1.37  mycroft 		if (du->dk_bct == 0) {
    738  1.51  mycroft 			wdtab[ctrlr].b_forw = dp->b_forw;
    739   1.3  deraadt 			du->dk_skipm = 0;
    740   1.3  deraadt 			dp->b_active = 0;
    741   1.3  deraadt 		}
    742  1.50  mycroft 		bp->b_resid = bp->b_bcount - du->dk_skip * DEV_BSIZE;
    743  1.50  mycroft 		bp->b_flags &= ~B_XXX;
    744  1.50  mycroft 		du->dk_skip = 0;
    745  1.33  mycroft 		dp->b_actf = bp->b_actf;
    746   1.1      cgd 		dp->b_errcnt = 0;
    747   1.1      cgd 		biodone(bp);
    748   1.1      cgd 	}
    749  1.49  mycroft 
    750  1.49  mycroft 	/* controller now idle */
    751  1.46  mycroft 	wdtab[ctrlr].b_active = 0;
    752  1.46  mycroft 
    753   1.1      cgd 	/* anything more on drive queue? */
    754   1.3  deraadt 	if (dp->b_actf && du->dk_bct == 0)
    755   1.1      cgd 		wdustart(du);
    756   1.3  deraadt 
    757   1.1      cgd 	/* anything more for controller to do? */
    758  1.51  mycroft 	if (wdtab[ctrlr].b_forw)
    759   1.3  deraadt 		wdstart(ctrlr);
    760   1.1      cgd }
    761   1.1      cgd 
    762   1.1      cgd /*
    763   1.1      cgd  * Initialize a drive.
    764   1.1      cgd  */
    765   1.1      cgd int
    766  1.55  mycroft wdopen(dev, flag, fmt, p)
    767  1.55  mycroft 	dev_t dev;
    768  1.55  mycroft 	int flag;
    769  1.55  mycroft 	int fmt;
    770  1.55  mycroft 	struct proc *p;
    771   1.1      cgd {
    772  1.54  mycroft 	int lunit;
    773  1.54  mycroft 	struct disk *du;
    774  1.51  mycroft 	int part = WDPART(dev), mask = 1 << part;
    775   1.3  deraadt 	struct partition *pp;
    776   1.1      cgd 	char *msg;
    777   1.3  deraadt 
    778  1.51  mycroft 	lunit = WDUNIT(dev);
    779   1.3  deraadt 	if (lunit >= NWD)
    780  1.37  mycroft 		return ENXIO;
    781   1.3  deraadt 	du = wddrives[lunit];
    782   1.3  deraadt 	if (du == 0)
    783  1.37  mycroft 		return ENXIO;
    784   1.3  deraadt 
    785   1.1      cgd 	if ((du->dk_flags & DKFL_BSDLABEL) == 0) {
    786   1.1      cgd 		du->dk_flags |= DKFL_WRITEPROT;
    787   1.3  deraadt 		wdutab[lunit].b_actf = NULL;
    788   1.1      cgd 
    789   1.1      cgd 		/*
    790   1.1      cgd 		 * Use the default sizes until we've read the label,
    791   1.1      cgd 		 * or longer if there isn't one there.
    792   1.1      cgd 		 */
    793   1.1      cgd 		bzero(&du->dk_dd, sizeof(du->dk_dd));
    794   1.1      cgd 		du->dk_dd.d_type = DTYPE_ST506;
    795   1.1      cgd 		du->dk_dd.d_ncylinders = 1024;
    796   1.1      cgd 		du->dk_dd.d_secsize = DEV_BSIZE;
    797   1.1      cgd 		du->dk_dd.d_ntracks = 8;
    798   1.1      cgd 		du->dk_dd.d_nsectors = 17;
    799   1.1      cgd 		du->dk_dd.d_secpercyl = 17*8;
    800   1.3  deraadt 		du->dk_dd.d_secperunit = 17*8*1024;
    801   1.1      cgd 		du->dk_state = WANTOPEN;
    802   1.1      cgd 
    803   1.3  deraadt 		/* read label using "raw" partition */
    804  1.55  mycroft #ifdef notdef
    805  1.55  mycroft 		/* wdsetctlr(du); */	/* Maybe do this TIH */
    806  1.51  mycroft 		msg = readdisklabel(makewddev(major(dev), WDUNIT(dev), WDRAW),
    807  1.37  mycroft 		    wdstrategy, &du->dk_dd, &du->dk_cpd);
    808  1.55  mycroft 		wdsetctlr(du);
    809   1.3  deraadt #endif
    810  1.51  mycroft 		if (msg = readdisklabel(makewddev(major(dev), WDUNIT(dev),
    811  1.42  mycroft 		    WDRAW), wdstrategy, &du->dk_dd, &du->dk_cpd)) {
    812  1.62  mycroft 			log(LOG_WARNING, "wd%d: cannot find label (%s)\n",
    813  1.62  mycroft 			    lunit, msg);
    814  1.62  mycroft 			if (part != WDRAW)
    815  1.46  mycroft 				return EINVAL;	/* XXX needs translation */
    816   1.1      cgd 		} else {
    817  1.55  mycroft 			wdsetctlr(du);
    818   1.1      cgd 			du->dk_flags |= DKFL_BSDLABEL;
    819   1.1      cgd 			du->dk_flags &= ~DKFL_WRITEPROT;
    820   1.1      cgd 			if (du->dk_dd.d_flags & D_BADSECT)
    821   1.1      cgd 				du->dk_flags |= DKFL_BADSECT;
    822   1.1      cgd 		}
    823   1.3  deraadt 	}
    824   1.3  deraadt 
    825   1.3  deraadt 	if (du->dk_flags & DKFL_BADSECT)
    826   1.3  deraadt 		bad144intern(du);
    827   1.1      cgd 
    828   1.3  deraadt 	/*
    829  1.60  mycroft 	 * Warn if a partition is opened that overlaps another partition which
    830  1.60  mycroft 	 * is open unless one is the "raw" partition (whole disk).
    831   1.3  deraadt 	 */
    832  1.46  mycroft 	if ((du->dk_openpart & mask) == 0 && part != WDRAW) {
    833  1.46  mycroft 		int start, end;
    834   1.3  deraadt 
    835   1.3  deraadt 		pp = &du->dk_dd.d_partitions[part];
    836   1.3  deraadt 		start = pp->p_offset;
    837   1.3  deraadt 		end = pp->p_offset + pp->p_size;
    838   1.3  deraadt 		for (pp = du->dk_dd.d_partitions;
    839  1.44  mycroft 		    pp < &du->dk_dd.d_partitions[du->dk_dd.d_npartitions];
    840  1.44  mycroft 		    pp++) {
    841  1.44  mycroft 			if (pp->p_offset + pp->p_size <= start ||
    842  1.44  mycroft 			    pp->p_offset >= end)
    843   1.3  deraadt 				continue;
    844   1.3  deraadt 			if (pp - du->dk_dd.d_partitions == WDRAW)
    845   1.3  deraadt 				continue;
    846   1.3  deraadt 			if (du->dk_openpart & (1 << (pp - du->dk_dd.d_partitions)))
    847   1.3  deraadt 				log(LOG_WARNING,
    848  1.37  mycroft 				    "wd%d%c: overlaps open partition (%c)\n",
    849  1.37  mycroft 				    lunit, part + 'a',
    850  1.37  mycroft 				    pp - du->dk_dd.d_partitions + 'a');
    851   1.3  deraadt 		}
    852   1.1      cgd 	}
    853  1.37  mycroft 	if (part >= du->dk_dd.d_npartitions && part != WDRAW)
    854  1.37  mycroft 		return ENXIO;
    855   1.3  deraadt 
    856   1.1      cgd 	/* insure only one open at a time */
    857   1.3  deraadt 	du->dk_openpart |= mask;
    858   1.3  deraadt 	switch (fmt) {
    859   1.3  deraadt 	case S_IFCHR:
    860   1.3  deraadt 		du->dk_copenpart |= mask;
    861   1.3  deraadt 		break;
    862   1.3  deraadt 	case S_IFBLK:
    863   1.3  deraadt 		du->dk_bopenpart |= mask;
    864   1.3  deraadt 		break;
    865   1.3  deraadt 	}
    866  1.60  mycroft 
    867  1.37  mycroft 	return 0;
    868   1.1      cgd }
    869   1.1      cgd 
    870   1.1      cgd /*
    871   1.1      cgd  * Implement operations other than read/write.
    872   1.1      cgd  * Called from wdstart or wdintr during opens and formats.
    873   1.1      cgd  * Uses finite-state-machine to track progress of operation in progress.
    874   1.1      cgd  * Returns 0 if operation still in progress, 1 if completed.
    875   1.1      cgd  */
    876   1.1      cgd static int
    877  1.55  mycroft wdcontrol(bp)
    878  1.55  mycroft 	struct buf *bp;
    879   1.1      cgd {
    880  1.54  mycroft 	struct disk *du;
    881  1.63  mycroft 	int ctrlr;
    882   1.3  deraadt 
    883  1.51  mycroft 	du = wddrives[WDUNIT(bp->b_dev)];
    884   1.3  deraadt 	ctrlr = du->dk_ctrlr;
    885   1.3  deraadt 
    886   1.1      cgd 	switch (du->dk_state) {
    887   1.1      cgd 	case WANTOPEN:			/* set SDH, step rate, do restore */
    888  1.46  mycroft 	tryagainrecal:
    889  1.60  mycroft 		wdgetctlr(du);		/* XXX Is this necessary? */
    890  1.60  mycroft 		wdtab[ctrlr].b_active = 1;
    891  1.63  mycroft 		if (wdcommand(du, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0) {
    892  1.63  mycroft 			wderror(du, 0, "wdcontrol: wdcommand failed");
    893  1.63  mycroft 			goto lose;
    894  1.21  deraadt 		}
    895   1.3  deraadt 		du->dk_state = RECAL;
    896  1.37  mycroft 		return 0;
    897  1.55  mycroft 
    898   1.1      cgd 	case RECAL:
    899  1.63  mycroft 		if (du->dk_status & WDCS_ERR || wdsetctlr(du) != 0) {
    900  1.63  mycroft 			wderror(du, NULL, "wdcontrol: recal failed");
    901  1.63  mycroft 		lose:
    902  1.63  mycroft 			if (du->dk_status & WDCS_ERR)
    903  1.63  mycroft 				wdreset(du, 1);
    904  1.60  mycroft 			du->dk_state = WANTOPEN;
    905  1.21  deraadt 			if (++wdtab[ctrlr].b_errcnt < WDIORETRIES)
    906   1.1      cgd 				goto tryagainrecal;
    907   1.1      cgd 			bp->b_error = ENXIO;	/* XXX needs translation */
    908  1.60  mycroft 			bp->b_flags |= B_ERROR;
    909  1.62  mycroft 			return -1;
    910   1.1      cgd 		}
    911   1.3  deraadt 		wdtab[ctrlr].b_errcnt = 0;
    912   1.1      cgd 		du->dk_state = OPEN;
    913   1.1      cgd 		/*
    914  1.63  mycroft 		 * The rest of the initialization can be done by normal means.
    915   1.1      cgd 		 */
    916  1.37  mycroft 		return 1;
    917   1.1      cgd 	}
    918   1.1      cgd 
    919  1.60  mycroft #ifdef DIAGNOSTIC
    920  1.60  mycroft 	panic("wdcontrol: impossible");
    921  1.60  mycroft #endif
    922   1.1      cgd }
    923   1.1      cgd 
    924   1.1      cgd /*
    925   1.1      cgd  * send a command and wait uninterruptibly until controller is finished.
    926   1.1      cgd  * return -1 if controller busy for too long, otherwise
    927   1.1      cgd  * return status. intended for brief controller commands at critical points.
    928   1.1      cgd  * assumes interrupts are blocked.
    929   1.1      cgd  */
    930   1.1      cgd static int
    931  1.60  mycroft wdcommand(du, cylin, head, sector, count, cmd)
    932  1.55  mycroft 	struct disk *du;
    933  1.60  mycroft 	int cylin, head, sector, count;
    934  1.55  mycroft 	int cmd;
    935   1.3  deraadt {
    936  1.60  mycroft 	int stat;
    937  1.60  mycroft 	u_short wdc;
    938   1.3  deraadt 
    939  1.21  deraadt 	/* controller ready for command? */
    940  1.51  mycroft 	if (wait_for_unbusy(du) < 0)
    941  1.40  mycroft 		return -1;
    942  1.60  mycroft 
    943  1.60  mycroft 	/* select drive */
    944  1.60  mycroft 	wdc = du->dk_port;
    945  1.60  mycroft 	outb(wdc+wd_sdh, WDSD_IBM | (du->dk_unit << 4) | head);
    946  1.60  mycroft 	if (cmd == WDCC_DIAGNOSE || cmd == WDCC_IDC)
    947  1.60  mycroft 		stat = wait_for_unbusy(du);
    948  1.60  mycroft 	else
    949  1.63  mycroft 		stat = wdwait(du, WDCS_READY);
    950  1.60  mycroft 	if (stat < 0)
    951  1.60  mycroft 		return -1;
    952   1.3  deraadt 
    953  1.60  mycroft 	/* load parameters */
    954  1.60  mycroft 	outb(wdc+wd_precomp, du->dk_dd.d_precompcyl / 4);
    955  1.60  mycroft 	outb(wdc+wd_cyl_lo, cylin);
    956  1.60  mycroft 	outb(wdc+wd_cyl_hi, cylin >> 8);
    957  1.60  mycroft 	outb(wdc+wd_sector, sector);
    958  1.60  mycroft 	outb(wdc+wd_seccnt, count);
    959  1.60  mycroft 
    960   1.1      cgd 	/* send command, await results */
    961   1.1      cgd 	outb(wdc+wd_command, cmd);
    962  1.40  mycroft 
    963  1.51  mycroft 	switch (cmd) {
    964  1.51  mycroft 	default:
    965  1.51  mycroft #if 0
    966  1.51  mycroft 	case WDCC_FORMAT:
    967  1.51  mycroft 	case WDCC_RESTORE | WD_STEP:
    968  1.60  mycroft 	case WDCC_DIAGNOSE:
    969  1.51  mycroft #endif
    970  1.51  mycroft 		return wait_for_unbusy(du);
    971  1.52  mycroft 	case WDCC_READ:
    972  1.52  mycroft 	case WDCC_WRITE:
    973  1.52  mycroft 		return 0;
    974  1.54  mycroft 	case WDCC_IDC:
    975  1.63  mycroft 		return wdwait(du, WDCS_READY);
    976  1.51  mycroft 	case WDCC_READP:
    977  1.51  mycroft 		return wait_for_drq(du);
    978  1.51  mycroft 	}
    979   1.1      cgd }
    980   1.1      cgd 
    981   1.1      cgd /*
    982   1.1      cgd  * issue IDC to drive to tell it just what geometry it is to be.
    983   1.1      cgd  */
    984   1.1      cgd static int
    985  1.55  mycroft wdsetctlr(du)
    986  1.55  mycroft 	struct disk *du;
    987   1.3  deraadt {
    988  1.63  mycroft 
    989  1.44  mycroft #ifdef WDDEBUG
    990   1.3  deraadt 	printf("wd(%d,%d) C%dH%dS%d\n", du->dk_ctrlr, du->dk_unit,
    991  1.37  mycroft 	    du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks, du->dk_dd.d_nsectors);
    992  1.44  mycroft #endif
    993   1.3  deraadt 
    994  1.63  mycroft 	if (wdcommand(du, du->dk_dd.d_ncylinders, du->dk_dd.d_ntracks - 1, 0,
    995  1.63  mycroft 	    du->dk_dd.d_nsectors, WDCC_IDC) != 0) {
    996  1.63  mycroft 		wderror(du, NULL, "wdsetctlr: failed");
    997  1.51  mycroft 		return -1;
    998  1.57  mycroft 	}
    999  1.60  mycroft 	return 0;
   1000   1.1      cgd }
   1001   1.1      cgd 
   1002   1.1      cgd /*
   1003   1.1      cgd  * issue READP to drive to ask it what it is.
   1004   1.1      cgd  */
   1005   1.1      cgd static int
   1006  1.55  mycroft wdgetctlr(du)
   1007  1.55  mycroft 	struct disk *du;
   1008   1.3  deraadt {
   1009  1.63  mycroft 	int i;
   1010   1.1      cgd 	char tb[DEV_BSIZE];
   1011   1.1      cgd 	struct wdparams *wp;
   1012   1.3  deraadt 
   1013  1.63  mycroft 	if (wdcommand(du, 0, 0, 0, 0, WDCC_READP) != 0) {
   1014  1.60  mycroft 		/*
   1015  1.60  mycroft 		 * If WDCC_READP fails then we might have an old drive
   1016  1.60  mycroft 		 * so we try a seek to 0; if that passes then the
   1017  1.60  mycroft 		 * drive is there but it's OLD AND KRUSTY.
   1018  1.60  mycroft 		 */
   1019  1.63  mycroft 		if (wdcommand(du, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0)
   1020  1.60  mycroft 			return -1;
   1021  1.60  mycroft 
   1022  1.60  mycroft 		strncpy(du->dk_dd.d_typename, "ST506",
   1023  1.60  mycroft 		    sizeof du->dk_dd.d_typename);
   1024  1.60  mycroft 		strncpy(du->dk_params.wdp_model, "Unknown Type",
   1025  1.60  mycroft 		    sizeof du->dk_params.wdp_model);
   1026  1.60  mycroft 		du->dk_dd.d_type = DTYPE_ST506;
   1027  1.60  mycroft 	} else {
   1028  1.13  deraadt 		/* obtain parameters */
   1029  1.13  deraadt 		wp = &du->dk_params;
   1030  1.60  mycroft 		insw(du->dk_port+wd_data, tb, sizeof(tb) / sizeof(short));
   1031  1.13  deraadt 		bcopy(tb, wp, sizeof(struct wdparams));
   1032  1.13  deraadt 
   1033  1.13  deraadt 		/* shuffle string byte order */
   1034  1.37  mycroft 		for (i = 0; i < sizeof(wp->wdp_model); i += 2) {
   1035  1.13  deraadt 			u_short *p;
   1036  1.37  mycroft 			p = (u_short *)(wp->wdp_model + i);
   1037  1.13  deraadt 			*p = ntohs(*p);
   1038  1.13  deraadt 		}
   1039  1.13  deraadt 
   1040  1.37  mycroft 		strncpy(du->dk_dd.d_typename, "ESDI/IDE",
   1041  1.37  mycroft 		    sizeof du->dk_dd.d_typename);
   1042  1.13  deraadt 		du->dk_dd.d_type = DTYPE_ESDI;
   1043  1.13  deraadt 		bcopy(wp->wdp_model+20, du->dk_dd.d_packname, 14-1);
   1044  1.13  deraadt 
   1045  1.13  deraadt 		/* update disklabel given drive information */
   1046  1.44  mycroft 		du->dk_dd.d_ncylinders =
   1047  1.44  mycroft 		    wp->wdp_fixedcyl + wp->wdp_removcyl /*+- 1*/;
   1048  1.13  deraadt 		du->dk_dd.d_ntracks = wp->wdp_heads;
   1049  1.13  deraadt 		du->dk_dd.d_nsectors = wp->wdp_sectors;
   1050  1.44  mycroft 		du->dk_dd.d_secpercyl =
   1051  1.44  mycroft 		    du->dk_dd.d_ntracks * du->dk_dd.d_nsectors;
   1052  1.44  mycroft 		du->dk_dd.d_partitions[1].p_size =
   1053  1.44  mycroft 		    du->dk_dd.d_secpercyl * wp->wdp_sectors;
   1054  1.13  deraadt 		du->dk_dd.d_partitions[1].p_offset = 0;
   1055   1.8  deraadt 	}
   1056  1.13  deraadt 
   1057  1.13  deraadt #if 0
   1058  1.37  mycroft 	printf("gc %x cyl %d trk %d sec %d type %d sz %d model %s\n",
   1059  1.37  mycroft 	    wp->wdp_config, wp->wdp_fixedcyl + wp->wdp_removcyl, wp->wdp_heads,
   1060  1.37  mycroft 	    wp->wdp_sectors, wp->wdp_cntype, wp->wdp_cnsbsz, wp->wdp_model);
   1061  1.13  deraadt #endif
   1062  1.13  deraadt 
   1063   1.1      cgd 	/* better ... */
   1064   1.1      cgd 	du->dk_dd.d_subtype |= DSTYPE_GEOMETRY;
   1065   1.3  deraadt 
   1066   1.1      cgd 	/* XXX sometimes possibly needed */
   1067  1.60  mycroft 	(void) inb(du->dk_port+wd_status);
   1068  1.63  mycroft 
   1069  1.37  mycroft 	return 0;
   1070   1.1      cgd }
   1071   1.1      cgd 
   1072   1.1      cgd int
   1073  1.55  mycroft wdclose(dev, flag, fmt)
   1074  1.55  mycroft 	dev_t dev;
   1075  1.55  mycroft 	int flag;
   1076  1.55  mycroft 	int fmt;
   1077   1.1      cgd {
   1078  1.60  mycroft 	struct disk *du = wddrives[WDUNIT(dev)];
   1079  1.51  mycroft 	int part = WDPART(dev), mask = 1 << part;
   1080   1.3  deraadt 
   1081   1.3  deraadt 	du->dk_openpart &= ~mask;
   1082   1.3  deraadt 	switch (fmt) {
   1083   1.3  deraadt 	case S_IFCHR:
   1084   1.3  deraadt 		du->dk_copenpart &= ~mask;
   1085   1.3  deraadt 		break;
   1086   1.3  deraadt 	case S_IFBLK:
   1087   1.3  deraadt 		du->dk_bopenpart &= ~mask;
   1088   1.3  deraadt 		break;
   1089   1.3  deraadt 	}
   1090  1.60  mycroft 
   1091  1.37  mycroft 	return 0;
   1092   1.1      cgd }
   1093   1.1      cgd 
   1094   1.1      cgd int
   1095  1.55  mycroft wdioctl(dev, cmd, addr, flag, p)
   1096  1.55  mycroft 	dev_t dev;
   1097  1.55  mycroft 	int cmd;
   1098  1.55  mycroft 	caddr_t addr;
   1099  1.55  mycroft 	int flag;
   1100  1.55  mycroft 	struct proc *p;
   1101   1.1      cgd {
   1102  1.51  mycroft 	int lunit = WDUNIT(dev);
   1103  1.54  mycroft 	struct disk *du = wddrives[lunit];
   1104  1.54  mycroft 	int error;
   1105   1.3  deraadt 
   1106   1.1      cgd 	switch (cmd) {
   1107   1.1      cgd 	case DIOCSBAD:
   1108   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1109  1.54  mycroft 			return EBADF;
   1110  1.54  mycroft 		du->dk_cpd.bad = *(struct dkbad *)addr;
   1111  1.54  mycroft 		bad144intern(du);
   1112  1.54  mycroft 		return 0;
   1113   1.1      cgd 
   1114   1.1      cgd 	case DIOCGDINFO:
   1115   1.1      cgd 		*(struct disklabel *)addr = du->dk_dd;
   1116  1.54  mycroft 		return 0;
   1117   1.3  deraadt 
   1118   1.3  deraadt 	case DIOCGPART:
   1119   1.3  deraadt 		((struct partinfo *)addr)->disklab = &du->dk_dd;
   1120   1.3  deraadt 		((struct partinfo *)addr)->part =
   1121  1.51  mycroft 		    &du->dk_dd.d_partitions[WDPART(dev)];
   1122  1.54  mycroft 		return 0;
   1123   1.3  deraadt 
   1124   1.3  deraadt 	case DIOCSDINFO:
   1125   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1126  1.54  mycroft 			return EBADF;
   1127  1.54  mycroft 		error = setdisklabel(&du->dk_dd,
   1128  1.54  mycroft 		    (struct disklabel *)addr,
   1129  1.54  mycroft 		    /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart : */0,
   1130  1.54  mycroft 		    &du->dk_cpd);
   1131   1.3  deraadt 		if (error == 0) {
   1132   1.1      cgd 			du->dk_flags |= DKFL_BSDLABEL;
   1133  1.55  mycroft 			wdsetctlr(du);
   1134   1.1      cgd 		}
   1135  1.54  mycroft 		return error;
   1136   1.3  deraadt 
   1137  1.44  mycroft 	case DIOCWLABEL:
   1138   1.1      cgd 		du->dk_flags &= ~DKFL_WRITEPROT;
   1139   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1140  1.54  mycroft 			return EBADF;
   1141  1.54  mycroft 		du->dk_wlabel = *(int *)addr;
   1142  1.54  mycroft 		return 0;
   1143   1.3  deraadt 
   1144  1.44  mycroft 	case DIOCWDINFO:
   1145   1.1      cgd 		du->dk_flags &= ~DKFL_WRITEPROT;
   1146   1.3  deraadt 		if ((flag & FWRITE) == 0)
   1147  1.54  mycroft 			return EBADF;
   1148  1.54  mycroft 		error = setdisklabel(&du->dk_dd,
   1149  1.37  mycroft 		    (struct disklabel *)addr,
   1150  1.44  mycroft 		    /*(du->dk_flags & DKFL_BSDLABEL) ? du->dk_openpart :*/0,
   1151  1.54  mycroft 		    &du->dk_cpd);
   1152  1.54  mycroft 		if (error == 0) {
   1153   1.3  deraadt 			int wlab;
   1154   1.3  deraadt 
   1155   1.1      cgd 			du->dk_flags |= DKFL_BSDLABEL;
   1156  1.55  mycroft 			wdsetctlr(du);
   1157   1.3  deraadt 
   1158   1.3  deraadt 			/* simulate opening partition 0 so write succeeds */
   1159  1.13  deraadt 			du->dk_openpart |= (1 << 0);	    /* XXX */
   1160   1.3  deraadt 			wlab = du->dk_wlabel;
   1161   1.3  deraadt 			du->dk_wlabel = 1;
   1162  1.60  mycroft 			error = writedisklabel(dev, wdstrategy, &du->dk_dd,
   1163  1.60  mycroft 			    &du->dk_cpd);
   1164   1.3  deraadt 			du->dk_openpart = du->dk_copenpart | du->dk_bopenpart;
   1165   1.3  deraadt 			du->dk_wlabel = wlab;
   1166   1.3  deraadt 		}
   1167  1.54  mycroft 		return error;
   1168   1.3  deraadt 
   1169   1.1      cgd #ifdef notyet
   1170   1.1      cgd 	case DIOCGDINFOP:
   1171  1.37  mycroft 		*(struct disklabel **)addr = &du->dk_dd;
   1172  1.54  mycroft 		return 0;
   1173   1.3  deraadt 
   1174   1.1      cgd 	case DIOCWFORMAT:
   1175   1.1      cgd 		if ((flag & FWRITE) == 0)
   1176  1.54  mycroft 			return EBADF;
   1177  1.54  mycroft 	{
   1178  1.54  mycroft 		register struct format_op *fop;
   1179  1.54  mycroft 		struct iovec aiov;
   1180  1.54  mycroft 		struct uio auio;
   1181   1.3  deraadt 
   1182  1.54  mycroft 		fop = (struct format_op *)addr;
   1183  1.54  mycroft 		aiov.iov_base = fop->df_buf;
   1184  1.54  mycroft 		aiov.iov_len = fop->df_count;
   1185  1.54  mycroft 		auio.uio_iov = &aiov;
   1186  1.54  mycroft 		auio.uio_iovcnt = 1;
   1187  1.54  mycroft 		auio.uio_resid = fop->df_count;
   1188  1.54  mycroft 		auio.uio_segflg = 0;
   1189  1.54  mycroft 		auio.uio_offset =
   1190  1.54  mycroft 		    fop->df_startblk * du->dk_dd.d_secsize;
   1191  1.54  mycroft 		error = physio(wdformat, &rwdbuf[lunit], dev, B_WRITE,
   1192  1.54  mycroft 		    minphys, &auio);
   1193  1.54  mycroft 		fop->df_count -= auio.uio_resid;
   1194  1.54  mycroft 		fop->df_reg[0] = du->dk_status;
   1195  1.54  mycroft 		fop->df_reg[1] = du->dk_error;
   1196  1.54  mycroft 		return error;
   1197  1.54  mycroft 	}
   1198   1.1      cgd #endif
   1199   1.3  deraadt 
   1200   1.1      cgd 	default:
   1201  1.54  mycroft 		return ENOTTY;
   1202   1.1      cgd 	}
   1203  1.54  mycroft 
   1204  1.54  mycroft #ifdef DIAGNOSTIC
   1205  1.54  mycroft 	panic("wdioctl: impossible");
   1206  1.54  mycroft #endif
   1207   1.1      cgd }
   1208   1.1      cgd 
   1209  1.44  mycroft #ifdef B_FORMAT
   1210   1.1      cgd int
   1211   1.1      cgd wdformat(struct buf *bp)
   1212   1.1      cgd {
   1213  1.44  mycroft 
   1214   1.1      cgd 	bp->b_flags |= B_FORMAT;
   1215  1.37  mycroft 	return wdstrategy(bp);
   1216   1.1      cgd }
   1217   1.1      cgd #endif
   1218   1.1      cgd 
   1219   1.1      cgd int
   1220  1.55  mycroft wdsize(dev)
   1221  1.55  mycroft 	dev_t dev;
   1222   1.1      cgd {
   1223  1.51  mycroft 	int lunit = WDUNIT(dev), part = WDPART(dev);
   1224   1.1      cgd 	struct disk *du;
   1225   1.3  deraadt 
   1226   1.3  deraadt 	if (lunit >= NWD)
   1227  1.37  mycroft 		return -1;
   1228  1.46  mycroft 	du = wddrives[lunit];
   1229  1.46  mycroft 	if (du == 0)
   1230  1.37  mycroft 		return -1;
   1231   1.3  deraadt 
   1232   1.3  deraadt 	if (du->dk_state < OPEN || (du->dk_flags & DKFL_BSDLABEL) == 0) {
   1233   1.3  deraadt 		int val;
   1234  1.44  mycroft 		val = wdopen(makewddev(major(dev), lunit, WDRAW), FREAD,
   1235  1.44  mycroft 		    S_IFBLK, 0);
   1236  1.55  mycroft 		/* XXX Clear the open flag? */
   1237   1.3  deraadt 		if (val != 0)
   1238  1.37  mycroft 			return -1;
   1239   1.3  deraadt 	}
   1240   1.3  deraadt 
   1241  1.37  mycroft 	if ((du->dk_flags & (DKFL_WRITEPROT | DKFL_BSDLABEL)) != DKFL_BSDLABEL)
   1242  1.37  mycroft 		return -1;
   1243  1.46  mycroft 
   1244  1.46  mycroft 	return (int)du->dk_dd.d_partitions[part].p_size;
   1245   1.1      cgd }
   1246   1.1      cgd 
   1247   1.3  deraadt /* dump core after a system crash */
   1248   1.1      cgd int
   1249  1.55  mycroft wddump(dev)
   1250  1.55  mycroft 	dev_t dev;
   1251   1.1      cgd {
   1252   1.1      cgd 	register struct disk *du;	/* disk unit to do the IO */
   1253  1.46  mycroft 	long num;			/* number of sectors to write */
   1254  1.60  mycroft 	int ctrlr, lunit, part;
   1255  1.46  mycroft 	long blkoff, blknum;
   1256  1.63  mycroft 	long cylin, head, sector;
   1257  1.60  mycroft 	long secpertrk, secpercyl, nblocks;
   1258   1.1      cgd 	char *addr;
   1259  1.46  mycroft 	static wddoingadump = 0;
   1260   1.1      cgd 	extern caddr_t CADDR1;
   1261  1.30       ws 	extern struct pte *CMAP1;
   1262  1.30       ws 
   1263  1.37  mycroft 	addr = (char *)0;		/* starting address */
   1264   1.3  deraadt 
   1265  1.16  deraadt #if DO_NOT_KNOW_HOW
   1266  1.16  deraadt 	/* toss any characters present prior to dump, ie. non-blocking getc */
   1267  1.16  deraadt 	while (cngetc())
   1268   1.1      cgd 		;
   1269  1.16  deraadt #endif
   1270  1.60  mycroft 
   1271   1.1      cgd 	/* size of memory to dump */
   1272  1.51  mycroft 	lunit = WDUNIT(dev);
   1273  1.51  mycroft 	part = WDPART(dev);	/* file system */
   1274   1.1      cgd 	/* check for acceptable drive number */
   1275   1.3  deraadt 	if (lunit >= NWD)
   1276  1.37  mycroft 		return ENXIO;
   1277   1.3  deraadt 	du = wddrives[lunit];
   1278   1.1      cgd 	/* was it ever initialized ? */
   1279  1.46  mycroft 	if (du == 0 || du->dk_state < OPEN || du->dk_flags & DKFL_WRITEPROT)
   1280  1.37  mycroft 		return ENXIO;
   1281  1.46  mycroft 
   1282   1.3  deraadt 	ctrlr = du->dk_ctrlr;
   1283   1.3  deraadt 
   1284   1.1      cgd 	/* Convert to disk sectors */
   1285  1.28  mycroft 	num = ctob(physmem) / du->dk_dd.d_secsize;
   1286   1.3  deraadt 
   1287   1.1      cgd 	secpertrk = du->dk_dd.d_nsectors;
   1288   1.1      cgd 	secpercyl = du->dk_dd.d_secpercyl;
   1289   1.1      cgd 	nblocks = du->dk_dd.d_partitions[part].p_size;
   1290   1.1      cgd 	blkoff = du->dk_dd.d_partitions[part].p_offset;
   1291   1.3  deraadt 
   1292  1.60  mycroft 	/*printf("part %x, nblocks %d, dumplo %d, num %d\n", part, nblocks,
   1293  1.51  mycroft 	    dumplo, num);*/
   1294  1.60  mycroft 
   1295   1.1      cgd 	/* check transfer bounds against partition size */
   1296  1.46  mycroft 	if (dumplo < 0 || dumplo + num > nblocks)
   1297  1.37  mycroft 		return EINVAL;
   1298  1.60  mycroft 
   1299  1.60  mycroft 	/* check if controller active */
   1300  1.60  mycroft 	/*if (wdtab[ctrlr].b_active)
   1301  1.60  mycroft 		return EFAULT;*/
   1302  1.60  mycroft 	if (wddoingadump)
   1303  1.60  mycroft 		return EFAULT;
   1304  1.60  mycroft 
   1305   1.3  deraadt 	/* mark controller active for if we panic during the dump */
   1306  1.46  mycroft 	/*wdtab[ctrlr].b_active = 1;*/
   1307   1.3  deraadt 	wddoingadump = 1;
   1308  1.60  mycroft 
   1309  1.60  mycroft 	/* Recalibrate. */
   1310  1.63  mycroft 	if (wdcommand(du, 0, 0, 0, 0, WDCC_RESTORE | WD_STEP) != 0 ||
   1311  1.63  mycroft 	    wdsetctlr(du) != 0) {
   1312  1.63  mycroft 		wderror(du, NULL, "wddump: recal failed");
   1313  1.51  mycroft 		return EIO;
   1314  1.63  mycroft 	}
   1315   1.3  deraadt 
   1316   1.1      cgd 	blknum = dumplo + blkoff;
   1317   1.1      cgd 	while (num > 0) {
   1318   1.1      cgd 		/* compute disk address */
   1319   1.1      cgd 		cylin = blknum / secpercyl;
   1320   1.1      cgd 		head = (blknum % secpercyl) / secpertrk;
   1321   1.1      cgd 		sector = blknum % secpertrk;
   1322   1.3  deraadt 
   1323   1.3  deraadt 		if (du->dk_flags & DKFL_BADSECT) {
   1324   1.3  deraadt 			long newblk;
   1325   1.3  deraadt 			int i;
   1326   1.3  deraadt 
   1327   1.3  deraadt 			for (i = 0; du->dk_badsect[i] != -1; i++) {
   1328   1.3  deraadt 				if (blknum < du->dk_badsect[i]) {
   1329  1.45  mycroft 					/* sorted list, passed our block by */
   1330  1.44  mycroft 					break;
   1331   1.3  deraadt 				} else if (blknum == du->dk_badsect[i]) {
   1332   1.3  deraadt 					newblk = du->dk_dd.d_secperunit -
   1333   1.3  deraadt 						du->dk_dd.d_nsectors - i - 1;
   1334   1.3  deraadt 					cylin = newblk / secpercyl;
   1335   1.3  deraadt 					head = (newblk % secpercyl) / secpertrk;
   1336   1.3  deraadt 					sector = newblk % secpertrk;
   1337  1.44  mycroft 					/* found and replaced; done */
   1338   1.3  deraadt 					break;
   1339   1.3  deraadt 				}
   1340   1.1      cgd 			}
   1341   1.3  deraadt 		}
   1342   1.1      cgd 		sector++;		/* origin 1 */
   1343   1.3  deraadt 
   1344   1.1      cgd #ifdef notdef
   1345   1.1      cgd 		/* lets just talk about this first...*/
   1346  1.60  mycroft 		printf("cylin %d, head %d, sector %d, addr 0x%x", cylin, head,
   1347  1.60  mycroft 		    sector, addr);
   1348   1.1      cgd #endif
   1349  1.63  mycroft 		if (wdcommand(du, cylin, head, sector, 1, WDCC_WRITE) != 0) {
   1350  1.63  mycroft 			wderror(du, NULL, "wddump: wdcommand failed");
   1351  1.63  mycroft 			return EIO;
   1352  1.63  mycroft 		}
   1353  1.63  mycroft 		if (wait_for_drq(du) != 0) {
   1354  1.63  mycroft 			wderror(du, NULL, "wddump: timeout waiting for drq");
   1355  1.51  mycroft 			return EIO;
   1356  1.63  mycroft 		}
   1357   1.3  deraadt 
   1358  1.46  mycroft #ifdef notdef	/* cannot use this since this address was mapped differently */
   1359  1.46  mycroft 		pmap_enter(kernel_pmap, CADDR1, trunc_page(addr), VM_PROT_READ, TRUE);
   1360  1.46  mycroft #else
   1361  1.46  mycroft 		*(int *)CMAP1 = PG_V | PG_KW | ctob((long)addr);
   1362  1.46  mycroft 		tlbflush();
   1363  1.46  mycroft #endif
   1364  1.46  mycroft 
   1365  1.63  mycroft 		outsw(du->dk_port+wd_data, CADDR1 + ((int)addr & PGOFSET),
   1366  1.51  mycroft 		    DEV_BSIZE / sizeof(short));
   1367   1.3  deraadt 
   1368  1.13  deraadt 		/* Check data request (should be done). */
   1369  1.63  mycroft 		if (wait_for_ready(du) != 0) {
   1370  1.63  mycroft 			wderror(du, NULL, "wddump: timeout waiting for ready");
   1371  1.63  mycroft 			return EIO;
   1372  1.63  mycroft 		}
   1373  1.63  mycroft 		if (du->dk_status & WDCS_DRQ) {
   1374  1.63  mycroft 			wderror(du, NULL, "wddump: extra drq");
   1375  1.37  mycroft 			return EIO;
   1376  1.63  mycroft 		}
   1377   1.3  deraadt 
   1378  1.46  mycroft 		if ((unsigned)addr % 1048576 == 0)
   1379  1.46  mycroft 			printf("%d ", num / (1048576 / DEV_BSIZE));
   1380   1.1      cgd 
   1381   1.1      cgd 		/* update block count */
   1382   1.1      cgd 		num--;
   1383   1.3  deraadt 		blknum++;
   1384  1.46  mycroft 		(int)addr += DEV_BSIZE;
   1385   1.3  deraadt 
   1386  1.16  deraadt #if DO_NOT_KNOW_HOW
   1387  1.16  deraadt 		/* operator aborting dump? non-blocking getc() */
   1388  1.16  deraadt 		if (cngetc())
   1389  1.37  mycroft 			return EINTR;
   1390  1.16  deraadt #endif
   1391   1.1      cgd 	}
   1392  1.37  mycroft 	return 0;
   1393   1.1      cgd }
   1394   1.3  deraadt 
   1395   1.3  deraadt /*
   1396   1.3  deraadt  * Internalize the bad sector table.
   1397   1.3  deraadt  */
   1398   1.3  deraadt void
   1399  1.55  mycroft bad144intern(du)
   1400  1.55  mycroft 	struct disk *du;
   1401   1.3  deraadt {
   1402   1.3  deraadt 	int i;
   1403  1.55  mycroft 
   1404  1.55  mycroft 	if ((du->dk_flags & DKFL_BADSECT) == 0)
   1405  1.55  mycroft 		return;
   1406  1.55  mycroft 
   1407  1.55  mycroft 	for (i = 0; i < 127; i++)
   1408  1.55  mycroft 		du->dk_badsect[i] = -1;
   1409  1.55  mycroft 	for (i = 0; i < 126; i++) {
   1410  1.55  mycroft 		if (du->dk_cpd.bad.bt_bad[i].bt_cyl == 0xffff)
   1411  1.55  mycroft 			break;
   1412  1.55  mycroft 		du->dk_badsect[i] =
   1413  1.55  mycroft 		    du->dk_cpd.bad.bt_bad[i].bt_cyl * du->dk_dd.d_secpercyl +
   1414  1.55  mycroft 		    (du->dk_cpd.bad.bt_bad[i].bt_trksec >> 8) *
   1415  1.55  mycroft 			du->dk_dd.d_nsectors +
   1416  1.55  mycroft 		    (du->dk_cpd.bad.bt_bad[i].bt_trksec & 0x00ff);
   1417   1.3  deraadt 	}
   1418   1.3  deraadt }
   1419   1.3  deraadt 
   1420  1.27      cgd static int
   1421  1.55  mycroft wdreset(du, err)
   1422  1.55  mycroft 	struct disk *du;
   1423  1.55  mycroft 	int err;
   1424  1.21  deraadt {
   1425  1.63  mycroft 	u_short wdc = du->dk_port;
   1426  1.21  deraadt 
   1427  1.37  mycroft 	if (err)
   1428  1.51  mycroft 		printf("wdc%d: busy too long, resetting\n", du->dk_ctrlr);
   1429  1.21  deraadt 
   1430  1.21  deraadt 	/* reset the device  */
   1431  1.37  mycroft 	outb(wdc+wd_ctlr, WDCTL_RST | WDCTL_IDS);
   1432  1.61  mycroft 	delay(1000);
   1433  1.55  mycroft 	outb(wdc+wd_ctlr, WDCTL_IDS);
   1434  1.61  mycroft 	delay(1000);
   1435  1.55  mycroft 	(void) inb(wdc+wd_error);
   1436  1.21  deraadt 	outb(wdc+wd_ctlr, WDCTL_4BIT);
   1437  1.21  deraadt 
   1438  1.51  mycroft 	if (wait_for_unbusy(du) < 0)
   1439  1.51  mycroft 		printf("wdc%d: failed to reset controller\n", du->dk_ctrlr);
   1440  1.40  mycroft }
   1441  1.40  mycroft 
   1442  1.40  mycroft int
   1443  1.63  mycroft wdwait(du, mask)
   1444  1.51  mycroft 	struct disk *du;
   1445  1.49  mycroft 	int mask;
   1446  1.40  mycroft {
   1447  1.63  mycroft 	u_short wdc = du->dk_port;
   1448  1.40  mycroft 	int timeout = 0;
   1449  1.63  mycroft 	u_char status;
   1450  1.40  mycroft 
   1451  1.40  mycroft 	for (;;) {
   1452  1.63  mycroft 		du->dk_status = status = inb(wdc+wd_altsts);
   1453  1.63  mycroft 		if ((status & WDCS_BUSY) == 0 && (status & mask) == mask)
   1454  1.54  mycroft 			break;
   1455  1.40  mycroft 		if (++timeout > WDCNDELAY)
   1456  1.40  mycroft 			return -1;
   1457  1.61  mycroft 		delay(WDCDELAY);
   1458  1.21  deraadt 	}
   1459  1.63  mycroft 	if (status & WDCS_ERR)
   1460  1.63  mycroft 		du->dk_error = inb(wdc+wd_error);
   1461  1.23  deraadt #ifdef WDCNDELAY_DEBUG
   1462  1.37  mycroft 	if (timeout > WDCNDELAY_DEBUG)
   1463  1.63  mycroft 		printf("wdc%d: busy-wait took %dus\n", du->dk_ctrlr,
   1464  1.51  mycroft 		    WDCDELAY * timeout);
   1465  1.23  deraadt #endif
   1466  1.63  mycroft 	return status & WDCS_ERR;
   1467  1.27      cgd }
   1468  1.27      cgd 
   1469  1.60  mycroft static void
   1470  1.55  mycroft wdtimeout(arg)
   1471  1.55  mycroft 	caddr_t arg;
   1472  1.27      cgd {
   1473  1.55  mycroft 	int s = splbio();
   1474  1.55  mycroft 	struct disk *du = (void *)arg;
   1475  1.27      cgd 
   1476  1.55  mycroft 	if (du->dk_timeout && --du->dk_timeout == 0) {
   1477  1.63  mycroft 		wderror(du, NULL, "lost interrupt");
   1478  1.55  mycroft 		wdreset(du, 0);
   1479  1.60  mycroft 		/* XXX Need to recalibrate and upload geometry. */
   1480  1.37  mycroft 		du->dk_skip = 0;
   1481  1.37  mycroft 		du->dk_flags |= DKFL_SINGLE;
   1482  1.60  mycroft 		wdstart(du->dk_ctrlr);
   1483  1.27      cgd 	}
   1484  1.55  mycroft 	timeout((timeout_t)wdtimeout, arg, hz);
   1485  1.55  mycroft 	splx(s);
   1486  1.63  mycroft }
   1487  1.63  mycroft 
   1488  1.63  mycroft static void
   1489  1.63  mycroft wderror(du, bp, msg)
   1490  1.63  mycroft 	struct disk *du;
   1491  1.63  mycroft 	struct buf *bp;
   1492  1.63  mycroft 	char *msg;
   1493  1.63  mycroft {
   1494  1.63  mycroft 	if (bp)
   1495  1.63  mycroft 		diskerr(bp, "wd", msg, LOG_PRINTF, du->dk_skip, &du->dk_dd);
   1496  1.63  mycroft 	else
   1497  1.63  mycroft 		printf("wd%d: %s: status %b error %b\n", du->dk_lunit, msg,
   1498  1.63  mycroft 		    du->dk_status, WDCS_BITS, du->dk_error, WDERR_BITS);
   1499  1.21  deraadt }
   1500  1.43  mycroft 
   1501  1.43  mycroft #endif /* NWDC > 0 */
   1502