Home | History | Annotate | Line # | Download | only in dev
rd.c revision 1.9
      1  1.1      cgd /*
      2  1.1      cgd  * Copyright (c) 1988 University of Utah.
      3  1.8  mycroft  * Copyright (c) 1982, 1990, 1993
      4  1.8  mycroft  *	The Regents of the University of California.  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  * the Systems Programming Group of the University of Utah Computer
      8  1.1      cgd  * Science Department.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  *
     38  1.8  mycroft  * from: Utah $Hdr: rd.c 1.44 92/12/26$
     39  1.8  mycroft  *
     40  1.9  mycroft  *	from: @(#)rd.c	8.2 (Berkeley) 5/19/94
     41  1.9  mycroft  *	$Id: rd.c,v 1.9 1994/05/27 17:18:50 mycroft Exp $
     42  1.1      cgd  */
     43  1.1      cgd 
     44  1.1      cgd /*
     45  1.1      cgd  * CS80/SS80 disk driver
     46  1.1      cgd  */
     47  1.1      cgd #include "rd.h"
     48  1.1      cgd #if NRD > 0
     49  1.1      cgd 
     50  1.5  mycroft #include <sys/param.h>
     51  1.5  mycroft #include <sys/systm.h>
     52  1.5  mycroft #include <sys/buf.h>
     53  1.5  mycroft #include <sys/stat.h>
     54  1.5  mycroft #include <sys/dkstat.h>
     55  1.5  mycroft #include <sys/disklabel.h>
     56  1.8  mycroft #include <sys/ioctl.h>
     57  1.8  mycroft #include <sys/fcntl.h>
     58  1.1      cgd 
     59  1.5  mycroft #include <hp300/dev/device.h>
     60  1.5  mycroft #include <hp300/dev/rdreg.h>
     61  1.8  mycroft #include <hp300/dev/rdvar.h>
     62  1.8  mycroft #ifdef USELEDS
     63  1.8  mycroft #include <hp300/hp300/led.h>
     64  1.8  mycroft #endif
     65  1.1      cgd 
     66  1.8  mycroft #include <vm/vm_param.h>
     67  1.8  mycroft #include <vm/lock.h>
     68  1.8  mycroft #include <vm/vm_prot.h>
     69  1.8  mycroft #include <vm/pmap.h>
     70  1.1      cgd 
     71  1.1      cgd int	rdinit(), rdstart(), rdgo(), rdintr();
     72  1.8  mycroft void	rdstrategy();
     73  1.1      cgd struct	driver rddriver = {
     74  1.1      cgd 	rdinit, "rd", rdstart, rdgo, rdintr,
     75  1.1      cgd };
     76  1.1      cgd 
     77  1.8  mycroft struct	rd_softc rd_softc[NRD];
     78  1.8  mycroft struct	buf rdtab[NRD];
     79  1.8  mycroft int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
     80  1.1      cgd 
     81  1.1      cgd #ifdef DEBUG
     82  1.1      cgd /* error message tables */
     83  1.1      cgd char *err_reject[] = {
     84  1.1      cgd 	0, 0,
     85  1.1      cgd 	"channel parity error",		/* 0x2000 */
     86  1.1      cgd 	0, 0,
     87  1.1      cgd 	"illegal opcode",		/* 0x0400 */
     88  1.1      cgd 	"module addressing",		/* 0x0200 */
     89  1.1      cgd 	"address bounds",		/* 0x0100 */
     90  1.1      cgd 	"parameter bounds",		/* 0x0080 */
     91  1.1      cgd 	"illegal parameter",		/* 0x0040 */
     92  1.1      cgd 	"message sequence",		/* 0x0020 */
     93  1.1      cgd 	0,
     94  1.1      cgd 	"message length",		/* 0x0008 */
     95  1.1      cgd 	0, 0, 0
     96  1.1      cgd };
     97  1.1      cgd 
     98  1.1      cgd char *err_fault[] = {
     99  1.1      cgd 	0,
    100  1.1      cgd 	"cross unit",			/* 0x4000 */
    101  1.1      cgd 	0,
    102  1.1      cgd 	"controller fault",		/* 0x1000 */
    103  1.1      cgd 	0, 0,
    104  1.1      cgd 	"unit fault",			/* 0x0200 */
    105  1.1      cgd 	0,
    106  1.1      cgd 	"diagnostic result",		/* 0x0080 */
    107  1.1      cgd 	0,
    108  1.1      cgd 	"operator release request",	/* 0x0020 */
    109  1.1      cgd 	"diagnostic release request",	/* 0x0010 */
    110  1.1      cgd 	"internal maintenance release request",	/* 0x0008 */
    111  1.1      cgd 	0,
    112  1.1      cgd 	"power fail",			/* 0x0002 */
    113  1.1      cgd 	"retransmit"			/* 0x0001 */
    114  1.1      cgd };
    115  1.1      cgd 
    116  1.1      cgd char *err_access[] = {
    117  1.1      cgd 	"illegal parallel operation",	/* 0x8000 */
    118  1.1      cgd 	"uninitialized media",		/* 0x4000 */
    119  1.1      cgd 	"no spares available",		/* 0x2000 */
    120  1.1      cgd 	"not ready",			/* 0x1000 */
    121  1.1      cgd 	"write protect",		/* 0x0800 */
    122  1.1      cgd 	"no data found",		/* 0x0400 */
    123  1.1      cgd 	0, 0,
    124  1.1      cgd 	"unrecoverable data overflow",	/* 0x0080 */
    125  1.1      cgd 	"unrecoverable data",		/* 0x0040 */
    126  1.1      cgd 	0,
    127  1.1      cgd 	"end of file",			/* 0x0010 */
    128  1.1      cgd 	"end of volume",		/* 0x0008 */
    129  1.1      cgd 	0, 0, 0
    130  1.1      cgd };
    131  1.1      cgd 
    132  1.1      cgd char *err_info[] = {
    133  1.1      cgd 	"operator release request",	/* 0x8000 */
    134  1.1      cgd 	"diagnostic release request",	/* 0x4000 */
    135  1.1      cgd 	"internal maintenance release request",	/* 0x2000 */
    136  1.1      cgd 	"media wear",			/* 0x1000 */
    137  1.1      cgd 	"latency induced",		/* 0x0800 */
    138  1.1      cgd 	0, 0,
    139  1.1      cgd 	"auto sparing invoked",		/* 0x0100 */
    140  1.1      cgd 	0,
    141  1.1      cgd 	"recoverable data overflow",	/* 0x0040 */
    142  1.1      cgd 	"marginal data",		/* 0x0020 */
    143  1.1      cgd 	"recoverable data",		/* 0x0010 */
    144  1.1      cgd 	0,
    145  1.1      cgd 	"maintenance track overflow",	/* 0x0004 */
    146  1.1      cgd 	0, 0
    147  1.1      cgd };
    148  1.8  mycroft 
    149  1.8  mycroft struct	rdstats rdstats[NRD];
    150  1.8  mycroft int	rddebug = 0x80;
    151  1.8  mycroft #define RDB_FOLLOW	0x01
    152  1.8  mycroft #define RDB_STATUS	0x02
    153  1.8  mycroft #define RDB_IDENT	0x04
    154  1.8  mycroft #define RDB_IO		0x08
    155  1.8  mycroft #define RDB_ASYNC	0x10
    156  1.8  mycroft #define RDB_ERROR	0x80
    157  1.1      cgd #endif
    158  1.1      cgd 
    159  1.1      cgd /*
    160  1.8  mycroft  * Misc. HW description, indexed by sc_type.
    161  1.8  mycroft  * Nothing really critical here, could do without it.
    162  1.1      cgd  */
    163  1.8  mycroft struct rdidentinfo rdidentinfo[] = {
    164  1.8  mycroft 	{ RD7946AID,	0,	"7945A",	 108416 },
    165  1.8  mycroft 	{ RD9134DID,	1,	"9134D",	  29088 },
    166  1.8  mycroft 	{ RD9134LID,	1,	"9122S",	   1232 },
    167  1.8  mycroft 	{ RD7912PID,	0,	"7912P",	 128128 },
    168  1.8  mycroft 	{ RD7914PID,	0,	"7914P",	 258048 },
    169  1.8  mycroft 	{ RD7958AID,	0,	"7958A",	 255276 },
    170  1.8  mycroft 	{ RD7957AID,	0,	"7957A",	 159544 },
    171  1.8  mycroft 	{ RD7933HID,	0,	"7933H",	 789958 },
    172  1.8  mycroft 	{ RD9134LID,	1,	"9134L",	  77840 },
    173  1.8  mycroft 	{ RD7936HID,	0,	"7936H",	 600978 },
    174  1.8  mycroft 	{ RD7937HID,	0,	"7937H",	1116102 },
    175  1.8  mycroft 	{ RD7914CTID,	0,	"7914CT",	 258048 },
    176  1.8  mycroft 	{ RD7946AID,	0,	"7946A",	 108416 },
    177  1.8  mycroft 	{ RD9134LID,	1,	"9122D",	   1232 },
    178  1.8  mycroft 	{ RD7957BID,	0,	"7957B",	 159894 },
    179  1.8  mycroft 	{ RD7958BID,	0,	"7958B",	 297108 },
    180  1.8  mycroft 	{ RD7959BID,	0,	"7959B",	 594216 },
    181  1.8  mycroft 	{ RD2200AID,	0,	"2200A",	 654948 },
    182  1.8  mycroft 	{ RD2203AID,	0,	"2203A",	1309896 }
    183  1.1      cgd };
    184  1.8  mycroft int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
    185  1.1      cgd 
    186  1.1      cgd rdinit(hd)
    187  1.1      cgd 	register struct hp_device *hd;
    188  1.1      cgd {
    189  1.1      cgd 	register struct rd_softc *rs = &rd_softc[hd->hp_unit];
    190  1.1      cgd 
    191  1.1      cgd 	rs->sc_hd = hd;
    192  1.1      cgd 	rs->sc_punit = rdpunit(hd->hp_flags);
    193  1.1      cgd 	rs->sc_type = rdident(rs, hd);
    194  1.1      cgd 	if (rs->sc_type < 0)
    195  1.1      cgd 		return(0);
    196  1.1      cgd 	rs->sc_dq.dq_ctlr = hd->hp_ctlr;
    197  1.1      cgd 	rs->sc_dq.dq_unit = hd->hp_unit;
    198  1.1      cgd 	rs->sc_dq.dq_slave = hd->hp_slave;
    199  1.1      cgd 	rs->sc_dq.dq_driver = &rddriver;
    200  1.1      cgd 	rs->sc_flags = RDF_ALIVE;
    201  1.1      cgd #ifdef DEBUG
    202  1.1      cgd 	/* always report errors */
    203  1.1      cgd 	if (rddebug & RDB_ERROR)
    204  1.1      cgd 		rderrthresh = 0;
    205  1.1      cgd #endif
    206  1.1      cgd 	return(1);
    207  1.1      cgd }
    208  1.1      cgd 
    209  1.1      cgd rdident(rs, hd)
    210  1.1      cgd 	struct rd_softc *rs;
    211  1.1      cgd 	struct hp_device *hd;
    212  1.1      cgd {
    213  1.1      cgd 	struct rd_describe desc;
    214  1.1      cgd 	u_char stat, cmd[3];
    215  1.1      cgd 	int unit, lunit;
    216  1.1      cgd 	char name[7];
    217  1.1      cgd 	register int ctlr, slave, id, i;
    218  1.1      cgd 
    219  1.1      cgd 	ctlr = hd->hp_ctlr;
    220  1.1      cgd 	slave = hd->hp_slave;
    221  1.1      cgd 	unit = rs->sc_punit;
    222  1.1      cgd 	lunit = hd->hp_unit;
    223  1.1      cgd 
    224  1.1      cgd 	/*
    225  1.1      cgd 	 * Grab device id and make sure:
    226  1.1      cgd 	 * 1. It is a CS80 device.
    227  1.1      cgd 	 * 2. It is one of the types we support.
    228  1.1      cgd 	 * 3. If it is a 7946, we are accessing the disk unit (0)
    229  1.1      cgd 	 */
    230  1.1      cgd 	id = hpibid(ctlr, slave);
    231  1.1      cgd #ifdef DEBUG
    232  1.1      cgd 	if (rddebug & RDB_IDENT)
    233  1.1      cgd 		printf("hpibid(%d, %d) -> %x\n", ctlr, slave, id);
    234  1.1      cgd #endif
    235  1.1      cgd 	if ((id & 0x200) == 0)
    236  1.1      cgd 		return(-1);
    237  1.8  mycroft 	for (i = 0; i < numrdidentinfo; i++)
    238  1.8  mycroft 		if (id == rdidentinfo[i].ri_hwid)
    239  1.1      cgd 			break;
    240  1.8  mycroft 	if (i == numrdidentinfo || unit > rdidentinfo[i].ri_maxunum)
    241  1.1      cgd 		return(-1);
    242  1.1      cgd 	id = i;
    243  1.1      cgd 
    244  1.1      cgd 	/*
    245  1.1      cgd 	 * Reset drive and collect device description.
    246  1.1      cgd 	 * Don't really use the description info right now but
    247  1.1      cgd 	 * might come in handy in the future (for disk labels).
    248  1.1      cgd 	 */
    249  1.1      cgd 	rdreset(rs, hd);
    250  1.1      cgd 	cmd[0] = C_SUNIT(unit);
    251  1.1      cgd 	cmd[1] = C_SVOL(0);
    252  1.1      cgd 	cmd[2] = C_DESC;
    253  1.1      cgd 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
    254  1.1      cgd 	hpibrecv(ctlr, slave, C_EXEC, &desc, 37);
    255  1.1      cgd 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    256  1.1      cgd 	bzero(name, sizeof(name));
    257  1.1      cgd 	if (!stat) {
    258  1.1      cgd 		register int n = desc.d_name;
    259  1.1      cgd 		for (i = 5; i >= 0; i--) {
    260  1.1      cgd 			name[i] = (n & 0xf) + '0';
    261  1.1      cgd 			n >>= 4;
    262  1.1      cgd 		}
    263  1.1      cgd 		/* use drive characteristics to calculate xfer rate */
    264  1.1      cgd 		rs->sc_wpms = 1000000 * (desc.d_sectsize/2) / desc.d_blocktime;
    265  1.1      cgd 	}
    266  1.1      cgd #ifdef DEBUG
    267  1.1      cgd 	if (rddebug & RDB_IDENT) {
    268  1.1      cgd 		printf("rd%d: name: %x ('%s')\n",
    269  1.1      cgd 		       lunit, desc.d_name, name);
    270  1.1      cgd 		printf("  iuw %x, maxxfr %d, ctype %d\n",
    271  1.1      cgd 		       desc.d_iuw, desc.d_cmaxxfr, desc.d_ctype);
    272  1.1      cgd 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
    273  1.1      cgd 		       desc.d_utype, desc.d_sectsize,
    274  1.1      cgd 		       desc.d_blkbuf, desc.d_burstsize, desc.d_blocktime);
    275  1.1      cgd 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
    276  1.1      cgd 		       desc.d_uavexfr, desc.d_retry, desc.d_access,
    277  1.1      cgd 		       desc.d_maxint, desc.d_fvbyte, desc.d_rvbyte);
    278  1.1      cgd 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
    279  1.1      cgd 		       desc.d_maxcyl, desc.d_maxhead, desc.d_maxsect,
    280  1.1      cgd 		       desc.d_maxvsectl, desc.d_interleave);
    281  1.1      cgd 	}
    282  1.1      cgd #endif
    283  1.1      cgd 	/*
    284  1.1      cgd 	 * Take care of a couple of anomolies:
    285  1.1      cgd 	 * 1. 7945A and 7946A both return same HW id
    286  1.1      cgd 	 * 2. 9122S and 9134D both return same HW id
    287  1.1      cgd 	 * 3. 9122D and 9134L both return same HW id
    288  1.1      cgd 	 */
    289  1.8  mycroft 	switch (rdidentinfo[id].ri_hwid) {
    290  1.1      cgd 	case RD7946AID:
    291  1.1      cgd 		if (bcmp(name, "079450", 6) == 0)
    292  1.1      cgd 			id = RD7945A;
    293  1.1      cgd 		else
    294  1.1      cgd 			id = RD7946A;
    295  1.1      cgd 		break;
    296  1.1      cgd 
    297  1.1      cgd 	case RD9134LID:
    298  1.1      cgd 		if (bcmp(name, "091340", 6) == 0)
    299  1.1      cgd 			id = RD9134L;
    300  1.1      cgd 		else
    301  1.1      cgd 			id = RD9122D;
    302  1.1      cgd 		break;
    303  1.1      cgd 
    304  1.1      cgd 	case RD9134DID:
    305  1.1      cgd 		if (bcmp(name, "091220", 6) == 0)
    306  1.1      cgd 			id = RD9122S;
    307  1.1      cgd 		else
    308  1.1      cgd 			id = RD9134D;
    309  1.1      cgd 		break;
    310  1.1      cgd 	}
    311  1.8  mycroft 	printf("rd%d: %s\n", lunit, rdidentinfo[id].ri_desc);
    312  1.1      cgd 	return(id);
    313  1.1      cgd }
    314  1.1      cgd 
    315  1.1      cgd rdreset(rs, hd)
    316  1.1      cgd 	register struct rd_softc *rs;
    317  1.1      cgd 	register struct hp_device *hd;
    318  1.1      cgd {
    319  1.1      cgd 	u_char stat;
    320  1.1      cgd 
    321  1.1      cgd 	rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
    322  1.1      cgd 	rs->sc_clear.c_cmd = C_CLEAR;
    323  1.1      cgd 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_TCMD, &rs->sc_clear,
    324  1.1      cgd 		sizeof(rs->sc_clear));
    325  1.1      cgd 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    326  1.1      cgd 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    327  1.1      cgd 	rs->sc_src.c_unit = C_SUNIT(RDCTLR);
    328  1.1      cgd 	rs->sc_src.c_nop = C_NOP;
    329  1.1      cgd 	rs->sc_src.c_cmd = C_SREL;
    330  1.1      cgd 	rs->sc_src.c_param = C_REL;
    331  1.1      cgd 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_src,
    332  1.1      cgd 		sizeof(rs->sc_src));
    333  1.1      cgd 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    334  1.1      cgd 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    335  1.1      cgd 	rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
    336  1.1      cgd 	rs->sc_ssmc.c_cmd = C_SSM;
    337  1.1      cgd 	rs->sc_ssmc.c_refm = REF_MASK;
    338  1.1      cgd 	rs->sc_ssmc.c_fefm = FEF_MASK;
    339  1.1      cgd 	rs->sc_ssmc.c_aefm = AEF_MASK;
    340  1.1      cgd 	rs->sc_ssmc.c_iefm = IEF_MASK;
    341  1.1      cgd 	hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_ssmc,
    342  1.1      cgd 		sizeof(rs->sc_ssmc));
    343  1.1      cgd 	hpibswait(hd->hp_ctlr, hd->hp_slave);
    344  1.1      cgd 	hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
    345  1.1      cgd #ifdef DEBUG
    346  1.1      cgd 	rdstats[hd->hp_unit].rdresets++;
    347  1.1      cgd #endif
    348  1.1      cgd }
    349  1.1      cgd 
    350  1.8  mycroft /*
    351  1.8  mycroft  * Read or constuct a disklabel
    352  1.8  mycroft  */
    353  1.8  mycroft int
    354  1.8  mycroft rdgetinfo(dev)
    355  1.8  mycroft 	dev_t dev;
    356  1.8  mycroft {
    357  1.8  mycroft 	int unit = rdunit(dev);
    358  1.8  mycroft 	register struct rd_softc *rs = &rd_softc[unit];
    359  1.8  mycroft 	register struct disklabel *lp = &rs->sc_info.ri_label;
    360  1.8  mycroft 	register struct partition *pi;
    361  1.8  mycroft 	char *msg, *readdisklabel();
    362  1.8  mycroft 
    363  1.8  mycroft 	/*
    364  1.8  mycroft 	 * Set some default values to use while reading the label
    365  1.8  mycroft 	 * or to use if there isn't a label.
    366  1.8  mycroft 	 */
    367  1.8  mycroft 	bzero((caddr_t)lp, sizeof *lp);
    368  1.8  mycroft 	lp->d_type = DTYPE_HPIB;
    369  1.8  mycroft 	lp->d_secsize = DEV_BSIZE;
    370  1.8  mycroft 	lp->d_nsectors = 32;
    371  1.8  mycroft 	lp->d_ntracks = 20;
    372  1.8  mycroft 	lp->d_ncylinders = 1;
    373  1.8  mycroft 	lp->d_secpercyl = 32*20;
    374  1.8  mycroft 	lp->d_npartitions = 3;
    375  1.8  mycroft 	lp->d_partitions[2].p_offset = 0;
    376  1.8  mycroft 	lp->d_partitions[2].p_size = LABELSECTOR+1;
    377  1.8  mycroft 
    378  1.8  mycroft 	/*
    379  1.8  mycroft 	 * Now try to read the disklabel
    380  1.8  mycroft 	 */
    381  1.8  mycroft 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp);
    382  1.8  mycroft 	if (msg == NULL)
    383  1.8  mycroft 		return(0);
    384  1.8  mycroft 
    385  1.8  mycroft 	pi = lp->d_partitions;
    386  1.8  mycroft 	printf("rd%d: WARNING: %s, ", unit, msg);
    387  1.8  mycroft #ifdef COMPAT_NOLABEL
    388  1.8  mycroft 	printf("using old default partitioning\n");
    389  1.8  mycroft 	rdmakedisklabel(unit, lp);
    390  1.8  mycroft #else
    391  1.8  mycroft 	printf("defining `c' partition as entire disk\n");
    392  1.8  mycroft 	pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
    393  1.9  mycroft 	/* XXX reset other info since readdisklabel screws with it */
    394  1.9  mycroft 	lp->d_npartitions = 3;
    395  1.9  mycroft 	pi[0].p_size = 0;
    396  1.8  mycroft #endif
    397  1.8  mycroft 	return(0);
    398  1.8  mycroft }
    399  1.8  mycroft 
    400  1.1      cgd int
    401  1.1      cgd rdopen(dev, flags, mode, p)
    402  1.1      cgd 	dev_t dev;
    403  1.1      cgd 	int flags, mode;
    404  1.1      cgd 	struct proc *p;
    405  1.1      cgd {
    406  1.1      cgd 	register int unit = rdunit(dev);
    407  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
    408  1.8  mycroft 	int error, mask;
    409  1.1      cgd 
    410  1.1      cgd 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
    411  1.1      cgd 		return(ENXIO);
    412  1.8  mycroft 
    413  1.8  mycroft 	/*
    414  1.8  mycroft 	 * Wait for any pending opens/closes to complete
    415  1.8  mycroft 	 */
    416  1.8  mycroft 	while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
    417  1.8  mycroft 		sleep((caddr_t)rs, PRIBIO);
    418  1.8  mycroft 
    419  1.8  mycroft 	/*
    420  1.8  mycroft 	 * On first open, get label and partition info.
    421  1.8  mycroft 	 * We may block reading the label, so be careful
    422  1.8  mycroft 	 * to stop any other opens.
    423  1.8  mycroft 	 */
    424  1.8  mycroft 	if (rs->sc_info.ri_open == 0) {
    425  1.8  mycroft 		rs->sc_flags |= RDF_OPENING;
    426  1.8  mycroft 		error = rdgetinfo(dev);
    427  1.8  mycroft 		rs->sc_flags &= ~RDF_OPENING;
    428  1.8  mycroft 		wakeup((caddr_t)rs);
    429  1.8  mycroft 		if (error)
    430  1.8  mycroft 			return(error);
    431  1.8  mycroft 	}
    432  1.1      cgd 	if (rs->sc_hd->hp_dk >= 0) {
    433  1.1      cgd 		/* guess at xfer rate based on 3600 rpm (60 rps) */
    434  1.1      cgd 		if (rs->sc_wpms == 0)
    435  1.8  mycroft 			rs->sc_wpms = 60 * rs->sc_info.ri_label.d_nsectors
    436  1.8  mycroft 				* DEV_BSIZE / 2;
    437  1.1      cgd 		dk_wpms[rs->sc_hd->hp_dk] = rs->sc_wpms;
    438  1.1      cgd 	}
    439  1.8  mycroft 
    440  1.8  mycroft 	mask = 1 << rdpart(dev);
    441  1.8  mycroft 	if (mode == S_IFCHR)
    442  1.8  mycroft 		rs->sc_info.ri_copen |= mask;
    443  1.8  mycroft 	else
    444  1.8  mycroft 		rs->sc_info.ri_bopen |= mask;
    445  1.8  mycroft 	rs->sc_info.ri_open |= mask;
    446  1.8  mycroft 	return(0);
    447  1.8  mycroft }
    448  1.8  mycroft 
    449  1.8  mycroft int
    450  1.8  mycroft rdclose(dev, flag, mode, p)
    451  1.8  mycroft 	dev_t dev;
    452  1.8  mycroft 	int flag, mode;
    453  1.8  mycroft 	struct proc *p;
    454  1.8  mycroft {
    455  1.8  mycroft 	int unit = rdunit(dev);
    456  1.8  mycroft 	register struct rd_softc *rs = &rd_softc[unit];
    457  1.8  mycroft 	register struct rdinfo *ri = &rs->sc_info;
    458  1.8  mycroft 	int mask, s;
    459  1.8  mycroft 
    460  1.8  mycroft 	mask = 1 << rdpart(dev);
    461  1.8  mycroft 	if (mode == S_IFCHR)
    462  1.8  mycroft 		ri->ri_copen &= ~mask;
    463  1.8  mycroft 	else
    464  1.8  mycroft 		ri->ri_bopen &= ~mask;
    465  1.8  mycroft 	ri->ri_open = ri->ri_bopen | ri->ri_copen;
    466  1.8  mycroft 	/*
    467  1.8  mycroft 	 * On last close, we wait for all activity to cease since
    468  1.8  mycroft 	 * the label/parition info will become invalid.  Since we
    469  1.8  mycroft 	 * might sleep, we must block any opens while we are here.
    470  1.8  mycroft 	 * Note we don't have to about other closes since we know
    471  1.8  mycroft 	 * we are the last one.
    472  1.8  mycroft 	 */
    473  1.8  mycroft 	if (ri->ri_open == 0) {
    474  1.8  mycroft 		rs->sc_flags |= RDF_CLOSING;
    475  1.8  mycroft 		s = splbio();
    476  1.8  mycroft 		while (rdtab[unit].b_active) {
    477  1.8  mycroft 			rs->sc_flags |= RDF_WANTED;
    478  1.8  mycroft 			sleep((caddr_t)&rdtab[unit], PRIBIO);
    479  1.8  mycroft 		}
    480  1.8  mycroft 		splx(s);
    481  1.8  mycroft 		rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
    482  1.8  mycroft 		wakeup((caddr_t)rs);
    483  1.8  mycroft 	}
    484  1.1      cgd 	return(0);
    485  1.1      cgd }
    486  1.1      cgd 
    487  1.4  mycroft void
    488  1.1      cgd rdstrategy(bp)
    489  1.1      cgd 	register struct buf *bp;
    490  1.1      cgd {
    491  1.8  mycroft 	int unit = rdunit(bp->b_dev);
    492  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
    493  1.1      cgd 	register struct buf *dp = &rdtab[unit];
    494  1.8  mycroft 	register struct partition *pinfo;
    495  1.1      cgd 	register daddr_t bn;
    496  1.1      cgd 	register int sz, s;
    497  1.1      cgd 
    498  1.1      cgd #ifdef DEBUG
    499  1.1      cgd 	if (rddebug & RDB_FOLLOW)
    500  1.1      cgd 		printf("rdstrategy(%x): dev %x, bn %x, bcount %x, %c\n",
    501  1.1      cgd 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    502  1.1      cgd 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    503  1.1      cgd #endif
    504  1.1      cgd 	bn = bp->b_blkno;
    505  1.1      cgd 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    506  1.8  mycroft 	pinfo = &rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)];
    507  1.8  mycroft 	if (bn < 0 || bn + sz > pinfo->p_size) {
    508  1.8  mycroft 		sz = pinfo->p_size - bn;
    509  1.1      cgd 		if (sz == 0) {
    510  1.1      cgd 			bp->b_resid = bp->b_bcount;
    511  1.1      cgd 			goto done;
    512  1.1      cgd 		}
    513  1.1      cgd 		if (sz < 0) {
    514  1.1      cgd 			bp->b_error = EINVAL;
    515  1.8  mycroft 			goto bad;
    516  1.1      cgd 		}
    517  1.1      cgd 		bp->b_bcount = dbtob(sz);
    518  1.1      cgd 	}
    519  1.8  mycroft 	/*
    520  1.8  mycroft 	 * Check for write to write protected label
    521  1.8  mycroft 	 */
    522  1.8  mycroft 	if (bn + pinfo->p_offset <= LABELSECTOR &&
    523  1.8  mycroft #if LABELSECTOR != 0
    524  1.8  mycroft 	    bn + pinfo->p_offset + sz > LABELSECTOR &&
    525  1.8  mycroft #endif
    526  1.8  mycroft 	    !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
    527  1.8  mycroft 		bp->b_error = EROFS;
    528  1.8  mycroft 		goto bad;
    529  1.8  mycroft 	}
    530  1.8  mycroft 	bp->b_cylin = bn + pinfo->p_offset;
    531  1.1      cgd 	s = splbio();
    532  1.1      cgd 	disksort(dp, bp);
    533  1.1      cgd 	if (dp->b_active == 0) {
    534  1.1      cgd 		dp->b_active = 1;
    535  1.1      cgd 		rdustart(unit);
    536  1.1      cgd 	}
    537  1.1      cgd 	splx(s);
    538  1.1      cgd 	return;
    539  1.8  mycroft bad:
    540  1.8  mycroft 	bp->b_flags |= B_ERROR;
    541  1.1      cgd done:
    542  1.1      cgd 	biodone(bp);
    543  1.1      cgd }
    544  1.1      cgd 
    545  1.1      cgd /*
    546  1.1      cgd  * Called from timeout() when handling maintenance releases
    547  1.1      cgd  */
    548  1.8  mycroft void
    549  1.6  mycroft rdrestart(arg)
    550  1.6  mycroft 	void *arg;
    551  1.1      cgd {
    552  1.1      cgd 	int s = splbio();
    553  1.6  mycroft 	rdustart((int)arg);
    554  1.1      cgd 	splx(s);
    555  1.1      cgd }
    556  1.1      cgd 
    557  1.1      cgd rdustart(unit)
    558  1.1      cgd 	register int unit;
    559  1.1      cgd {
    560  1.1      cgd 	register struct buf *bp;
    561  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
    562  1.1      cgd 
    563  1.1      cgd 	bp = rdtab[unit].b_actf;
    564  1.1      cgd 	rs->sc_addr = bp->b_un.b_addr;
    565  1.1      cgd 	rs->sc_resid = bp->b_bcount;
    566  1.1      cgd 	if (hpibreq(&rs->sc_dq))
    567  1.1      cgd 		rdstart(unit);
    568  1.1      cgd }
    569  1.1      cgd 
    570  1.8  mycroft struct buf *
    571  1.8  mycroft rdfinish(unit, rs, bp)
    572  1.8  mycroft 	int unit;
    573  1.8  mycroft 	register struct rd_softc *rs;
    574  1.8  mycroft 	register struct buf *bp;
    575  1.8  mycroft {
    576  1.8  mycroft 	register struct buf *dp = &rdtab[unit];
    577  1.8  mycroft 
    578  1.8  mycroft 	dp->b_errcnt = 0;
    579  1.8  mycroft 	dp->b_actf = bp->b_actf;
    580  1.8  mycroft 	bp->b_resid = 0;
    581  1.8  mycroft 	biodone(bp);
    582  1.8  mycroft 	hpibfree(&rs->sc_dq);
    583  1.8  mycroft 	if (dp->b_actf)
    584  1.8  mycroft 		return(dp->b_actf);
    585  1.8  mycroft 	dp->b_active = 0;
    586  1.8  mycroft 	if (rs->sc_flags & RDF_WANTED) {
    587  1.8  mycroft 		rs->sc_flags &= ~RDF_WANTED;
    588  1.8  mycroft 		wakeup((caddr_t)dp);
    589  1.8  mycroft 	}
    590  1.8  mycroft 	return(NULL);
    591  1.8  mycroft }
    592  1.8  mycroft 
    593  1.1      cgd rdstart(unit)
    594  1.1      cgd 	register int unit;
    595  1.1      cgd {
    596  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
    597  1.1      cgd 	register struct buf *bp = rdtab[unit].b_actf;
    598  1.1      cgd 	register struct hp_device *hp = rs->sc_hd;
    599  1.1      cgd 	register int part;
    600  1.1      cgd 
    601  1.1      cgd again:
    602  1.1      cgd #ifdef DEBUG
    603  1.1      cgd 	if (rddebug & RDB_FOLLOW)
    604  1.1      cgd 		printf("rdstart(%d): bp %x, %c\n", unit, bp,
    605  1.1      cgd 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    606  1.1      cgd #endif
    607  1.1      cgd 	part = rdpart(bp->b_dev);
    608  1.1      cgd 	rs->sc_flags |= RDF_SEEK;
    609  1.1      cgd 	rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
    610  1.1      cgd 	rs->sc_ioc.c_volume = C_SVOL(0);
    611  1.1      cgd 	rs->sc_ioc.c_saddr = C_SADDR;
    612  1.1      cgd 	rs->sc_ioc.c_hiaddr = 0;
    613  1.8  mycroft 	rs->sc_ioc.c_addr = RDBTOS(bp->b_cylin);
    614  1.1      cgd 	rs->sc_ioc.c_nop2 = C_NOP;
    615  1.1      cgd 	rs->sc_ioc.c_slen = C_SLEN;
    616  1.1      cgd 	rs->sc_ioc.c_len = rs->sc_resid;
    617  1.1      cgd 	rs->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
    618  1.1      cgd #ifdef DEBUG
    619  1.1      cgd 	if (rddebug & RDB_IO)
    620  1.1      cgd 		printf("rdstart: hpibsend(%x, %x, %x, %x, %x)\n",
    621  1.1      cgd 		       hp->hp_ctlr, hp->hp_slave, C_CMD,
    622  1.1      cgd 		       &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
    623  1.1      cgd #endif
    624  1.1      cgd 	if (hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD, &rs->sc_ioc.c_unit,
    625  1.1      cgd 		     sizeof(rs->sc_ioc)-2) == sizeof(rs->sc_ioc)-2) {
    626  1.1      cgd 		if (hp->hp_dk >= 0) {
    627  1.1      cgd 			dk_busy |= 1 << hp->hp_dk;
    628  1.1      cgd 			dk_seek[hp->hp_dk]++;
    629  1.1      cgd 		}
    630  1.1      cgd #ifdef DEBUG
    631  1.1      cgd 		if (rddebug & RDB_IO)
    632  1.1      cgd 			printf("rdstart: hpibawait(%x)\n", hp->hp_ctlr);
    633  1.1      cgd #endif
    634  1.1      cgd 		hpibawait(hp->hp_ctlr);
    635  1.1      cgd 		return;
    636  1.1      cgd 	}
    637  1.1      cgd 	/*
    638  1.1      cgd 	 * Experience has shown that the hpibwait in this hpibsend will
    639  1.1      cgd 	 * occasionally timeout.  It appears to occur mostly on old 7914
    640  1.1      cgd 	 * drives with full maintenance tracks.  We should probably
    641  1.1      cgd 	 * integrate this with the backoff code in rderror.
    642  1.1      cgd 	 */
    643  1.1      cgd #ifdef DEBUG
    644  1.1      cgd 	if (rddebug & RDB_ERROR)
    645  1.1      cgd 		printf("rd%d: rdstart: cmd %x adr %d blk %d len %d ecnt %d\n",
    646  1.1      cgd 		       unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
    647  1.1      cgd 		       bp->b_blkno, rs->sc_resid, rdtab[unit].b_errcnt);
    648  1.1      cgd 	rdstats[unit].rdretries++;
    649  1.1      cgd #endif
    650  1.1      cgd 	rs->sc_flags &= ~RDF_SEEK;
    651  1.1      cgd 	rdreset(rs, hp);
    652  1.1      cgd 	if (rdtab[unit].b_errcnt++ < RDRETRY)
    653  1.1      cgd 		goto again;
    654  1.1      cgd 	printf("rd%d: rdstart err: cmd 0x%x sect %d blk %d len %d\n",
    655  1.1      cgd 	       unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
    656  1.1      cgd 	       bp->b_blkno, rs->sc_resid);
    657  1.1      cgd 	bp->b_flags |= B_ERROR;
    658  1.1      cgd 	bp->b_error = EIO;
    659  1.8  mycroft 	bp = rdfinish(unit, rs, bp);
    660  1.8  mycroft 	if (bp) {
    661  1.8  mycroft 		rs->sc_addr = bp->b_un.b_addr;
    662  1.8  mycroft 		rs->sc_resid = bp->b_bcount;
    663  1.8  mycroft 		if (hpibreq(&rs->sc_dq))
    664  1.8  mycroft 			goto again;
    665  1.1      cgd 	}
    666  1.1      cgd }
    667  1.1      cgd 
    668  1.1      cgd rdgo(unit)
    669  1.1      cgd 	register int unit;
    670  1.1      cgd {
    671  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
    672  1.1      cgd 	register struct hp_device *hp = rs->sc_hd;
    673  1.1      cgd 	struct buf *bp = rdtab[unit].b_actf;
    674  1.1      cgd 
    675  1.1      cgd 	if (hp->hp_dk >= 0) {
    676  1.1      cgd 		dk_busy |= 1 << hp->hp_dk;
    677  1.1      cgd 		dk_xfer[hp->hp_dk]++;
    678  1.1      cgd 		dk_wds[hp->hp_dk] += rs->sc_resid >> 6;
    679  1.1      cgd 	}
    680  1.8  mycroft #ifdef USELEDS
    681  1.8  mycroft 	if (inledcontrol == 0)
    682  1.8  mycroft 		ledcontrol(0, 0, LED_DISK);
    683  1.8  mycroft #endif
    684  1.1      cgd 	hpibgo(hp->hp_ctlr, hp->hp_slave, C_EXEC,
    685  1.1      cgd 	       rs->sc_addr, rs->sc_resid, bp->b_flags & B_READ);
    686  1.1      cgd }
    687  1.1      cgd 
    688  1.1      cgd rdintr(unit)
    689  1.1      cgd 	register int unit;
    690  1.1      cgd {
    691  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
    692  1.1      cgd 	register struct buf *bp = rdtab[unit].b_actf;
    693  1.1      cgd 	register struct hp_device *hp = rs->sc_hd;
    694  1.1      cgd 	u_char stat = 13;	/* in case hpibrecv fails */
    695  1.1      cgd 	int rv, restart;
    696  1.1      cgd 
    697  1.1      cgd #ifdef DEBUG
    698  1.1      cgd 	if (rddebug & RDB_FOLLOW)
    699  1.1      cgd 		printf("rdintr(%d): bp %x, %c, flags %x\n", unit, bp,
    700  1.1      cgd 		       (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
    701  1.1      cgd 	if (bp == NULL) {
    702  1.1      cgd 		printf("rd%d: bp == NULL\n", unit);
    703  1.1      cgd 		return;
    704  1.1      cgd 	}
    705  1.1      cgd #endif
    706  1.1      cgd 	if (hp->hp_dk >= 0)
    707  1.1      cgd 		dk_busy &= ~(1 << hp->hp_dk);
    708  1.1      cgd 	if (rs->sc_flags & RDF_SEEK) {
    709  1.1      cgd 		rs->sc_flags &= ~RDF_SEEK;
    710  1.1      cgd 		if (hpibustart(hp->hp_ctlr))
    711  1.1      cgd 			rdgo(unit);
    712  1.1      cgd 		return;
    713  1.1      cgd 	}
    714  1.1      cgd 	if ((rs->sc_flags & RDF_SWAIT) == 0) {
    715  1.1      cgd #ifdef DEBUG
    716  1.1      cgd 		rdstats[unit].rdpolltries++;
    717  1.1      cgd #endif
    718  1.1      cgd 		if (hpibpptest(hp->hp_ctlr, hp->hp_slave) == 0) {
    719  1.1      cgd #ifdef DEBUG
    720  1.1      cgd 			rdstats[unit].rdpollwaits++;
    721  1.1      cgd #endif
    722  1.1      cgd 			if (hp->hp_dk >= 0)
    723  1.1      cgd 				dk_busy |= 1 << hp->hp_dk;
    724  1.1      cgd 			rs->sc_flags |= RDF_SWAIT;
    725  1.1      cgd 			hpibawait(hp->hp_ctlr);
    726  1.1      cgd 			return;
    727  1.1      cgd 		}
    728  1.1      cgd 	} else
    729  1.1      cgd 		rs->sc_flags &= ~RDF_SWAIT;
    730  1.1      cgd 	rv = hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
    731  1.1      cgd 	if (rv != 1 || stat) {
    732  1.1      cgd #ifdef DEBUG
    733  1.1      cgd 		if (rddebug & RDB_ERROR)
    734  1.1      cgd 			printf("rdintr: recv failed or bad stat %d\n", stat);
    735  1.1      cgd #endif
    736  1.1      cgd 		restart = rderror(unit);
    737  1.1      cgd #ifdef DEBUG
    738  1.1      cgd 		rdstats[unit].rdretries++;
    739  1.1      cgd #endif
    740  1.1      cgd 		if (rdtab[unit].b_errcnt++ < RDRETRY) {
    741  1.1      cgd 			if (restart)
    742  1.1      cgd 				rdstart(unit);
    743  1.1      cgd 			return;
    744  1.1      cgd 		}
    745  1.1      cgd 		bp->b_flags |= B_ERROR;
    746  1.1      cgd 		bp->b_error = EIO;
    747  1.1      cgd 	}
    748  1.8  mycroft 	if (rdfinish(unit, rs, bp))
    749  1.1      cgd 		rdustart(unit);
    750  1.1      cgd }
    751  1.1      cgd 
    752  1.1      cgd rdstatus(rs)
    753  1.1      cgd 	register struct rd_softc *rs;
    754  1.1      cgd {
    755  1.1      cgd 	register int c, s;
    756  1.1      cgd 	u_char stat;
    757  1.1      cgd 	int rv;
    758  1.1      cgd 
    759  1.1      cgd 	c = rs->sc_hd->hp_ctlr;
    760  1.1      cgd 	s = rs->sc_hd->hp_slave;
    761  1.1      cgd 	rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
    762  1.1      cgd 	rs->sc_rsc.c_sram = C_SRAM;
    763  1.1      cgd 	rs->sc_rsc.c_ram = C_RAM;
    764  1.1      cgd 	rs->sc_rsc.c_cmd = C_STATUS;
    765  1.1      cgd 	bzero((caddr_t)&rs->sc_stat, sizeof(rs->sc_stat));
    766  1.1      cgd 	rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
    767  1.1      cgd 	if (rv != sizeof(rs->sc_rsc)) {
    768  1.1      cgd #ifdef DEBUG
    769  1.1      cgd 		if (rddebug & RDB_STATUS)
    770  1.1      cgd 			printf("rdstatus: send C_CMD failed %d != %d\n",
    771  1.1      cgd 			       rv, sizeof(rs->sc_rsc));
    772  1.1      cgd #endif
    773  1.1      cgd 		return(1);
    774  1.1      cgd 	}
    775  1.1      cgd 	rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
    776  1.1      cgd 	if (rv != sizeof(rs->sc_stat)) {
    777  1.1      cgd #ifdef DEBUG
    778  1.1      cgd 		if (rddebug & RDB_STATUS)
    779  1.1      cgd 			printf("rdstatus: send C_EXEC failed %d != %d\n",
    780  1.1      cgd 			       rv, sizeof(rs->sc_stat));
    781  1.1      cgd #endif
    782  1.1      cgd 		return(1);
    783  1.1      cgd 	}
    784  1.1      cgd 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
    785  1.1      cgd 	if (rv != 1 || stat) {
    786  1.1      cgd #ifdef DEBUG
    787  1.1      cgd 		if (rddebug & RDB_STATUS)
    788  1.1      cgd 			printf("rdstatus: recv failed %d or bad stat %d\n",
    789  1.1      cgd 			       rv, stat);
    790  1.1      cgd #endif
    791  1.1      cgd 		return(1);
    792  1.1      cgd 	}
    793  1.1      cgd 	return(0);
    794  1.1      cgd }
    795  1.1      cgd 
    796  1.1      cgd /*
    797  1.1      cgd  * Deal with errors.
    798  1.1      cgd  * Returns 1 if request should be restarted,
    799  1.1      cgd  * 0 if we should just quietly give up.
    800  1.1      cgd  */
    801  1.1      cgd rderror(unit)
    802  1.1      cgd 	int unit;
    803  1.1      cgd {
    804  1.1      cgd 	struct rd_softc *rs = &rd_softc[unit];
    805  1.1      cgd 	register struct rd_stat *sp;
    806  1.1      cgd 	struct buf *bp;
    807  1.1      cgd 	daddr_t hwbn, pbn;
    808  1.1      cgd 
    809  1.1      cgd 	if (rdstatus(rs)) {
    810  1.1      cgd #ifdef DEBUG
    811  1.1      cgd 		printf("rd%d: couldn't get status\n", unit);
    812  1.1      cgd #endif
    813  1.1      cgd 		rdreset(rs, rs->sc_hd);
    814  1.1      cgd 		return(1);
    815  1.1      cgd 	}
    816  1.1      cgd 	sp = &rs->sc_stat;
    817  1.1      cgd 	if (sp->c_fef & FEF_REXMT)
    818  1.1      cgd 		return(1);
    819  1.1      cgd 	if (sp->c_fef & FEF_PF) {
    820  1.1      cgd 		rdreset(rs, rs->sc_hd);
    821  1.1      cgd 		return(1);
    822  1.1      cgd 	}
    823  1.1      cgd 	/*
    824  1.1      cgd 	 * Unit requests release for internal maintenance.
    825  1.1      cgd 	 * We just delay awhile and try again later.  Use expontially
    826  1.1      cgd 	 * increasing backoff ala ethernet drivers since we don't really
    827  1.1      cgd 	 * know how long the maintenance will take.  With RDWAITC and
    828  1.1      cgd 	 * RDRETRY as defined, the range is 1 to 32 seconds.
    829  1.1      cgd 	 */
    830  1.1      cgd 	if (sp->c_fef & FEF_IMR) {
    831  1.1      cgd 		extern int hz;
    832  1.1      cgd 		int rdtimo = RDWAITC << rdtab[unit].b_errcnt;
    833  1.1      cgd #ifdef DEBUG
    834  1.1      cgd 		printf("rd%d: internal maintenance, %d second timeout\n",
    835  1.1      cgd 		       unit, rdtimo);
    836  1.1      cgd 		rdstats[unit].rdtimeouts++;
    837  1.1      cgd #endif
    838  1.1      cgd 		hpibfree(&rs->sc_dq);
    839  1.8  mycroft 		timeout(rdrestart, (void *)unit, rdtimo * hz);
    840  1.1      cgd 		return(0);
    841  1.1      cgd 	}
    842  1.1      cgd 	/*
    843  1.1      cgd 	 * Only report error if we have reached the error reporting
    844  1.1      cgd 	 * threshhold.  By default, this will only report after the
    845  1.1      cgd 	 * retry limit has been exceeded.
    846  1.1      cgd 	 */
    847  1.1      cgd 	if (rdtab[unit].b_errcnt < rderrthresh)
    848  1.1      cgd 		return(1);
    849  1.1      cgd 
    850  1.1      cgd 	/*
    851  1.1      cgd 	 * First conjure up the block number at which the error occured.
    852  1.1      cgd 	 * Note that not all errors report a block number, in that case
    853  1.1      cgd 	 * we just use b_blkno.
    854  1.1      cgd  	 */
    855  1.1      cgd 	bp = rdtab[unit].b_actf;
    856  1.8  mycroft 	pbn = rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)].p_offset;
    857  1.1      cgd 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
    858  1.1      cgd 	    (sp->c_ief & IEF_RRMASK)) {
    859  1.1      cgd 		hwbn = RDBTOS(pbn + bp->b_blkno);
    860  1.1      cgd 		pbn = bp->b_blkno;
    861  1.1      cgd 	} else {
    862  1.1      cgd 		hwbn = sp->c_blk;
    863  1.1      cgd 		pbn = RDSTOB(hwbn) - pbn;
    864  1.1      cgd 	}
    865  1.1      cgd 	/*
    866  1.1      cgd 	 * Now output a generic message suitable for badsect.
    867  1.1      cgd 	 * Note that we don't use harderr cuz it just prints
    868  1.1      cgd 	 * out b_blkno which is just the beginning block number
    869  1.1      cgd 	 * of the transfer, not necessary where the error occured.
    870  1.1      cgd 	 */
    871  1.1      cgd 	printf("rd%d%c: hard error sn%d\n",
    872  1.1      cgd 	       rdunit(bp->b_dev), 'a'+rdpart(bp->b_dev), pbn);
    873  1.1      cgd 	/*
    874  1.1      cgd 	 * Now report the status as returned by the hardware with
    875  1.1      cgd 	 * attempt at interpretation (unless debugging).
    876  1.1      cgd 	 */
    877  1.1      cgd 	printf("rd%d %s error:",
    878  1.1      cgd 	       unit, (bp->b_flags & B_READ) ? "read" : "write");
    879  1.1      cgd #ifdef DEBUG
    880  1.1      cgd 	if (rddebug & RDB_ERROR) {
    881  1.1      cgd 		/* status info */
    882  1.1      cgd 		printf("\n    volume: %d, unit: %d\n",
    883  1.1      cgd 		       (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
    884  1.1      cgd 		rdprinterr("reject", sp->c_ref, err_reject);
    885  1.1      cgd 		rdprinterr("fault", sp->c_fef, err_fault);
    886  1.1      cgd 		rdprinterr("access", sp->c_aef, err_access);
    887  1.1      cgd 		rdprinterr("info", sp->c_ief, err_info);
    888  1.1      cgd 		printf("    block: %d, P1-P10: ", hwbn);
    889  1.1      cgd 		printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
    890  1.1      cgd 		printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
    891  1.1      cgd 		printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
    892  1.1      cgd 		/* command */
    893  1.1      cgd 		printf("    ioc: ");
    894  1.1      cgd 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_pad, 8));
    895  1.1      cgd 		printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_hiaddr, 4));
    896  1.1      cgd 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_addr, 8));
    897  1.1      cgd 		printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_nop2, 4));
    898  1.1      cgd 		printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_len, 8));
    899  1.1      cgd 		printf("%s\n", hexstr(*(u_short *)&rs->sc_ioc.c_cmd, 4));
    900  1.1      cgd 		return(1);
    901  1.1      cgd 	}
    902  1.1      cgd #endif
    903  1.1      cgd 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
    904  1.1      cgd 	       (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
    905  1.1      cgd 	       sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
    906  1.1      cgd 	printf("P1-P10: ");
    907  1.1      cgd 	printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
    908  1.1      cgd 	printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
    909  1.1      cgd 	printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
    910  1.1      cgd 	return(1);
    911  1.1      cgd }
    912  1.1      cgd 
    913  1.1      cgd int
    914  1.1      cgd rdioctl(dev, cmd, data, flag, p)
    915  1.1      cgd 	dev_t dev;
    916  1.1      cgd 	int cmd;
    917  1.1      cgd 	caddr_t data;
    918  1.1      cgd 	int flag;
    919  1.1      cgd 	struct proc *p;
    920  1.1      cgd {
    921  1.8  mycroft 	int unit = rdunit(dev);
    922  1.8  mycroft 	register struct rd_softc *sc = &rd_softc[unit];
    923  1.8  mycroft 	register struct disklabel *lp = &sc->sc_info.ri_label;
    924  1.8  mycroft 	int error, flags;
    925  1.8  mycroft 
    926  1.8  mycroft 	switch (cmd) {
    927  1.8  mycroft 	case DIOCGDINFO:
    928  1.8  mycroft 		*(struct disklabel *)data = *lp;
    929  1.8  mycroft 		return (0);
    930  1.8  mycroft 
    931  1.8  mycroft 	case DIOCGPART:
    932  1.8  mycroft 		((struct partinfo *)data)->disklab = lp;
    933  1.8  mycroft 		((struct partinfo *)data)->part =
    934  1.8  mycroft 			&lp->d_partitions[rdpart(dev)];
    935  1.8  mycroft 		return (0);
    936  1.8  mycroft 
    937  1.8  mycroft 	case DIOCWLABEL:
    938  1.8  mycroft 		if ((flag & FWRITE) == 0)
    939  1.8  mycroft 			return (EBADF);
    940  1.8  mycroft 		if (*(int *)data)
    941  1.8  mycroft 			sc->sc_flags |= RDF_WLABEL;
    942  1.8  mycroft 		else
    943  1.8  mycroft 			sc->sc_flags &= ~RDF_WLABEL;
    944  1.8  mycroft 		return (0);
    945  1.8  mycroft 
    946  1.8  mycroft 	case DIOCSDINFO:
    947  1.8  mycroft 		if ((flag & FWRITE) == 0)
    948  1.8  mycroft 			return (EBADF);
    949  1.8  mycroft 		return (setdisklabel(lp, (struct disklabel *)data,
    950  1.8  mycroft 				     (sc->sc_flags & RDF_WLABEL) ? 0
    951  1.8  mycroft 				     : sc->sc_info.ri_open,
    952  1.8  mycroft 				     (struct cpu_disklabel *)0));
    953  1.8  mycroft 
    954  1.8  mycroft 	case DIOCWDINFO:
    955  1.8  mycroft 		if ((flag & FWRITE) == 0)
    956  1.8  mycroft 			return (EBADF);
    957  1.8  mycroft 		error = setdisklabel(lp, (struct disklabel *)data,
    958  1.8  mycroft 				     (sc->sc_flags & RDF_WLABEL) ? 0
    959  1.8  mycroft 				     : sc->sc_info.ri_open,
    960  1.8  mycroft 				     (struct cpu_disklabel *)0);
    961  1.8  mycroft 		if (error)
    962  1.8  mycroft 			return (error);
    963  1.8  mycroft 		flags = sc->sc_flags;
    964  1.8  mycroft 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
    965  1.8  mycroft 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
    966  1.8  mycroft 				       (struct cpu_disklabel *)0);
    967  1.8  mycroft 		sc->sc_flags = flags;
    968  1.8  mycroft 		return (error);
    969  1.8  mycroft 	}
    970  1.1      cgd 	return(EINVAL);
    971  1.1      cgd }
    972  1.1      cgd 
    973  1.1      cgd int
    974  1.1      cgd rdsize(dev)
    975  1.1      cgd 	dev_t dev;
    976  1.1      cgd {
    977  1.1      cgd 	register int unit = rdunit(dev);
    978  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
    979  1.8  mycroft 	int psize, didopen = 0;
    980  1.1      cgd 
    981  1.1      cgd 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
    982  1.1      cgd 		return(-1);
    983  1.8  mycroft 
    984  1.8  mycroft 	/*
    985  1.8  mycroft 	 * We get called very early on (via swapconf)
    986  1.8  mycroft 	 * without the device being open so we may need
    987  1.8  mycroft 	 * to handle it here.
    988  1.8  mycroft 	 */
    989  1.8  mycroft 	if (rs->sc_info.ri_open == 0) {
    990  1.8  mycroft 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
    991  1.8  mycroft 			return(-1);
    992  1.8  mycroft 		didopen = 1;
    993  1.8  mycroft 	}
    994  1.8  mycroft 	psize = rs->sc_info.ri_label.d_partitions[rdpart(dev)].p_size;
    995  1.8  mycroft 	if (didopen)
    996  1.8  mycroft 		(void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
    997  1.8  mycroft 	return (psize);
    998  1.1      cgd }
    999  1.1      cgd 
   1000  1.1      cgd #ifdef DEBUG
   1001  1.1      cgd rdprinterr(str, err, tab)
   1002  1.1      cgd 	char *str;
   1003  1.1      cgd 	short err;
   1004  1.1      cgd 	char *tab[];
   1005  1.1      cgd {
   1006  1.1      cgd 	register int i;
   1007  1.1      cgd 	int printed;
   1008  1.1      cgd 
   1009  1.1      cgd 	if (err == 0)
   1010  1.1      cgd 		return;
   1011  1.1      cgd 	printf("    %s error field:", str, err);
   1012  1.1      cgd 	printed = 0;
   1013  1.1      cgd 	for (i = 0; i < 16; i++)
   1014  1.1      cgd 		if (err & (0x8000 >> i))
   1015  1.1      cgd 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
   1016  1.1      cgd 	printf("\n");
   1017  1.1      cgd }
   1018  1.1      cgd #endif
   1019  1.1      cgd 
   1020  1.1      cgd /*
   1021  1.1      cgd  * Non-interrupt driven, non-dma dump routine.
   1022  1.1      cgd  */
   1023  1.1      cgd int
   1024  1.1      cgd rddump(dev)
   1025  1.1      cgd 	dev_t dev;
   1026  1.1      cgd {
   1027  1.1      cgd 	int part = rdpart(dev);
   1028  1.1      cgd 	int unit = rdunit(dev);
   1029  1.1      cgd 	register struct rd_softc *rs = &rd_softc[unit];
   1030  1.1      cgd 	register struct hp_device *hp = rs->sc_hd;
   1031  1.8  mycroft 	register struct partition *pinfo;
   1032  1.1      cgd 	register daddr_t baddr;
   1033  1.1      cgd 	register int maddr, pages, i;
   1034  1.1      cgd 	char stat;
   1035  1.1      cgd 	extern int lowram, dumpsize;
   1036  1.1      cgd #ifdef DEBUG
   1037  1.1      cgd 	extern int pmapdebug;
   1038  1.1      cgd 	pmapdebug = 0;
   1039  1.1      cgd #endif
   1040  1.1      cgd 
   1041  1.1      cgd 	/* is drive ok? */
   1042  1.1      cgd 	if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
   1043  1.1      cgd 		return (ENXIO);
   1044  1.8  mycroft 	pinfo = &rs->sc_info.ri_label.d_partitions[part];
   1045  1.8  mycroft 	/* dump parameters in range? */
   1046  1.8  mycroft 	if (dumplo < 0 || dumplo >= pinfo->p_size ||
   1047  1.8  mycroft 	    pinfo->p_fstype != FS_SWAP)
   1048  1.8  mycroft 		return (EINVAL);
   1049  1.8  mycroft 	pages = dumpsize;
   1050  1.8  mycroft 	if (dumplo + ctod(pages) > pinfo->p_size)
   1051  1.8  mycroft 		pages = dtoc(pinfo->p_size - dumplo);
   1052  1.8  mycroft 	maddr = lowram;
   1053  1.8  mycroft 	baddr = dumplo + pinfo->p_offset;
   1054  1.1      cgd 	/* HPIB idle? */
   1055  1.1      cgd 	if (!hpibreq(&rs->sc_dq)) {
   1056  1.1      cgd 		hpibreset(hp->hp_ctlr);
   1057  1.1      cgd 		rdreset(rs, rs->sc_hd);
   1058  1.1      cgd 		printf("[ drive %d reset ] ", unit);
   1059  1.1      cgd 	}
   1060  1.1      cgd 	for (i = 0; i < pages; i++) {
   1061  1.1      cgd #define NPGMB	(1024*1024/NBPG)
   1062  1.1      cgd 		/* print out how many Mbs we have dumped */
   1063  1.1      cgd 		if (i && (i % NPGMB) == 0)
   1064  1.1      cgd 			printf("%d ", i / NPGMB);
   1065  1.1      cgd #undef NPBMG
   1066  1.1      cgd 		rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
   1067  1.1      cgd 		rs->sc_ioc.c_volume = C_SVOL(0);
   1068  1.1      cgd 		rs->sc_ioc.c_saddr = C_SADDR;
   1069  1.1      cgd 		rs->sc_ioc.c_hiaddr = 0;
   1070  1.1      cgd 		rs->sc_ioc.c_addr = RDBTOS(baddr);
   1071  1.1      cgd 		rs->sc_ioc.c_nop2 = C_NOP;
   1072  1.1      cgd 		rs->sc_ioc.c_slen = C_SLEN;
   1073  1.1      cgd 		rs->sc_ioc.c_len = NBPG;
   1074  1.1      cgd 		rs->sc_ioc.c_cmd = C_WRITE;
   1075  1.1      cgd 		hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD,
   1076  1.1      cgd 			 &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
   1077  1.8  mycroft 		if (hpibswait(hp->hp_ctlr, hp->hp_slave))
   1078  1.1      cgd 			return (EIO);
   1079  1.8  mycroft 		pmap_enter(kernel_pmap, (vm_offset_t)vmmap, maddr,
   1080  1.8  mycroft 		    VM_PROT_READ, TRUE);
   1081  1.1      cgd 		hpibsend(hp->hp_ctlr, hp->hp_slave, C_EXEC, vmmap, NBPG);
   1082  1.8  mycroft 		(void) hpibswait(hp->hp_ctlr, hp->hp_slave);
   1083  1.1      cgd 		hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
   1084  1.8  mycroft 		if (stat)
   1085  1.1      cgd 			return (EIO);
   1086  1.1      cgd 		maddr += NBPG;
   1087  1.1      cgd 		baddr += ctod(1);
   1088  1.1      cgd 	}
   1089  1.1      cgd 	return (0);
   1090  1.1      cgd }
   1091  1.1      cgd #endif
   1092