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