Home | History | Annotate | Line # | Download | only in dev
rd.c revision 1.37
      1  1.37   thorpej /*	$NetBSD: rd.c,v 1.37 2000/01/21 23:29:03 thorpej Exp $	*/
      2  1.35   thorpej 
      3  1.35   thorpej /*-
      4  1.35   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  1.35   thorpej  * All rights reserved.
      6  1.35   thorpej  *
      7  1.35   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.35   thorpej  * by Jason R. Thorpe.
      9  1.35   thorpej  *
     10  1.35   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.35   thorpej  * modification, are permitted provided that the following conditions
     12  1.35   thorpej  * are met:
     13  1.35   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.35   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.35   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.35   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.35   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.35   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.35   thorpej  *    must display the following acknowledgement:
     20  1.35   thorpej  *	This product includes software developed by the NetBSD
     21  1.35   thorpej  *	Foundation, Inc. and its contributors.
     22  1.35   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.35   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.35   thorpej  *    from this software without specific prior written permission.
     25  1.35   thorpej  *
     26  1.35   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.35   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.35   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.35   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.35   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.35   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.35   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.35   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.35   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.35   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.35   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.35   thorpej  */
     38  1.10       cgd 
     39   1.1       cgd /*
     40   1.1       cgd  * Copyright (c) 1988 University of Utah.
     41   1.8   mycroft  * Copyright (c) 1982, 1990, 1993
     42   1.8   mycroft  *	The Regents of the University of California.  All rights reserved.
     43   1.1       cgd  *
     44   1.1       cgd  * This code is derived from software contributed to Berkeley by
     45   1.1       cgd  * the Systems Programming Group of the University of Utah Computer
     46   1.1       cgd  * Science Department.
     47   1.1       cgd  *
     48   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     49   1.1       cgd  * modification, are permitted provided that the following conditions
     50   1.1       cgd  * are met:
     51   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     52   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     53   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     54   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     55   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     56   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     57   1.1       cgd  *    must display the following acknowledgement:
     58   1.1       cgd  *	This product includes software developed by the University of
     59   1.1       cgd  *	California, Berkeley and its contributors.
     60   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     61   1.1       cgd  *    may be used to endorse or promote products derived from this software
     62   1.1       cgd  *    without specific prior written permission.
     63   1.1       cgd  *
     64   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     65   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     66   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     67   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     68   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     69   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     70   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     71   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     72   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     73   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     74   1.1       cgd  * SUCH DAMAGE.
     75   1.1       cgd  *
     76   1.8   mycroft  * from: Utah $Hdr: rd.c 1.44 92/12/26$
     77   1.8   mycroft  *
     78  1.10       cgd  *	@(#)rd.c	8.2 (Berkeley) 5/19/94
     79   1.1       cgd  */
     80   1.1       cgd 
     81   1.1       cgd /*
     82   1.1       cgd  * CS80/SS80 disk driver
     83   1.1       cgd  */
     84   1.1       cgd 
     85   1.5   mycroft #include <sys/param.h>
     86   1.5   mycroft #include <sys/systm.h>
     87   1.5   mycroft #include <sys/buf.h>
     88  1.28    scottr #include <sys/conf.h>
     89  1.28    scottr #include <sys/device.h>
     90  1.28    scottr #include <sys/disk.h>
     91   1.5   mycroft #include <sys/disklabel.h>
     92  1.28    scottr #include <sys/fcntl.h>
     93   1.8   mycroft #include <sys/ioctl.h>
     94  1.28    scottr #include <sys/proc.h>
     95  1.28    scottr #include <sys/stat.h>
     96  1.27   thorpej 
     97  1.27   thorpej #include <hp300/dev/hpibvar.h>
     98   1.1       cgd 
     99   1.5   mycroft #include <hp300/dev/rdreg.h>
    100   1.8   mycroft #include <hp300/dev/rdvar.h>
    101   1.1       cgd 
    102  1.31   thorpej #include "opt_useleds.h"
    103  1.31   thorpej 
    104  1.31   thorpej #ifdef USELEDS
    105  1.31   thorpej #include <hp300/hp300/leds.h>
    106  1.31   thorpej #endif
    107  1.31   thorpej 
    108   1.8   mycroft int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
    109   1.1       cgd 
    110   1.1       cgd #ifdef DEBUG
    111   1.1       cgd /* error message tables */
    112   1.1       cgd char *err_reject[] = {
    113   1.1       cgd 	0, 0,
    114   1.1       cgd 	"channel parity error",		/* 0x2000 */
    115   1.1       cgd 	0, 0,
    116   1.1       cgd 	"illegal opcode",		/* 0x0400 */
    117   1.1       cgd 	"module addressing",		/* 0x0200 */
    118   1.1       cgd 	"address bounds",		/* 0x0100 */
    119   1.1       cgd 	"parameter bounds",		/* 0x0080 */
    120   1.1       cgd 	"illegal parameter",		/* 0x0040 */
    121   1.1       cgd 	"message sequence",		/* 0x0020 */
    122   1.1       cgd 	0,
    123   1.1       cgd 	"message length",		/* 0x0008 */
    124   1.1       cgd 	0, 0, 0
    125   1.1       cgd };
    126   1.1       cgd 
    127   1.1       cgd char *err_fault[] = {
    128   1.1       cgd 	0,
    129   1.1       cgd 	"cross unit",			/* 0x4000 */
    130   1.1       cgd 	0,
    131   1.1       cgd 	"controller fault",		/* 0x1000 */
    132   1.1       cgd 	0, 0,
    133   1.1       cgd 	"unit fault",			/* 0x0200 */
    134   1.1       cgd 	0,
    135   1.1       cgd 	"diagnostic result",		/* 0x0080 */
    136   1.1       cgd 	0,
    137   1.1       cgd 	"operator release request",	/* 0x0020 */
    138   1.1       cgd 	"diagnostic release request",	/* 0x0010 */
    139   1.1       cgd 	"internal maintenance release request",	/* 0x0008 */
    140   1.1       cgd 	0,
    141   1.1       cgd 	"power fail",			/* 0x0002 */
    142   1.1       cgd 	"retransmit"			/* 0x0001 */
    143   1.1       cgd };
    144   1.1       cgd 
    145   1.1       cgd char *err_access[] = {
    146   1.1       cgd 	"illegal parallel operation",	/* 0x8000 */
    147   1.1       cgd 	"uninitialized media",		/* 0x4000 */
    148   1.1       cgd 	"no spares available",		/* 0x2000 */
    149   1.1       cgd 	"not ready",			/* 0x1000 */
    150   1.1       cgd 	"write protect",		/* 0x0800 */
    151   1.1       cgd 	"no data found",		/* 0x0400 */
    152   1.1       cgd 	0, 0,
    153   1.1       cgd 	"unrecoverable data overflow",	/* 0x0080 */
    154   1.1       cgd 	"unrecoverable data",		/* 0x0040 */
    155   1.1       cgd 	0,
    156   1.1       cgd 	"end of file",			/* 0x0010 */
    157   1.1       cgd 	"end of volume",		/* 0x0008 */
    158   1.1       cgd 	0, 0, 0
    159   1.1       cgd };
    160   1.1       cgd 
    161   1.1       cgd char *err_info[] = {
    162   1.1       cgd 	"operator release request",	/* 0x8000 */
    163   1.1       cgd 	"diagnostic release request",	/* 0x4000 */
    164   1.1       cgd 	"internal maintenance release request",	/* 0x2000 */
    165   1.1       cgd 	"media wear",			/* 0x1000 */
    166   1.1       cgd 	"latency induced",		/* 0x0800 */
    167   1.1       cgd 	0, 0,
    168   1.1       cgd 	"auto sparing invoked",		/* 0x0100 */
    169   1.1       cgd 	0,
    170   1.1       cgd 	"recoverable data overflow",	/* 0x0040 */
    171   1.1       cgd 	"marginal data",		/* 0x0020 */
    172   1.1       cgd 	"recoverable data",		/* 0x0010 */
    173   1.1       cgd 	0,
    174   1.1       cgd 	"maintenance track overflow",	/* 0x0004 */
    175   1.1       cgd 	0, 0
    176   1.1       cgd };
    177   1.8   mycroft 
    178   1.8   mycroft int	rddebug = 0x80;
    179   1.8   mycroft #define RDB_FOLLOW	0x01
    180   1.8   mycroft #define RDB_STATUS	0x02
    181   1.8   mycroft #define RDB_IDENT	0x04
    182   1.8   mycroft #define RDB_IO		0x08
    183   1.8   mycroft #define RDB_ASYNC	0x10
    184   1.8   mycroft #define RDB_ERROR	0x80
    185   1.1       cgd #endif
    186   1.1       cgd 
    187   1.1       cgd /*
    188   1.8   mycroft  * Misc. HW description, indexed by sc_type.
    189   1.8   mycroft  * Nothing really critical here, could do without it.
    190   1.1       cgd  */
    191   1.8   mycroft struct rdidentinfo rdidentinfo[] = {
    192  1.14   thorpej 	{ RD7946AID,	0,	"7945A",	NRD7945ABPT,
    193  1.14   thorpej 	  NRD7945ATRK,	968,	 108416 },
    194  1.14   thorpej 
    195  1.14   thorpej 	{ RD9134DID,	1,	"9134D",	NRD9134DBPT,
    196  1.14   thorpej 	  NRD9134DTRK,	303,	  29088 },
    197  1.14   thorpej 
    198  1.14   thorpej 	{ RD9134LID,	1,	"9122S",	NRD9122SBPT,
    199  1.14   thorpej 	  NRD9122STRK,	77,	   1232 },
    200  1.14   thorpej 
    201  1.14   thorpej 	{ RD7912PID,	0,	"7912P",	NRD7912PBPT,
    202  1.14   thorpej 	  NRD7912PTRK,	572,	 128128 },
    203  1.14   thorpej 
    204  1.14   thorpej 	{ RD7914PID,	0,	"7914P",	NRD7914PBPT,
    205  1.14   thorpej 	  NRD7914PTRK,	1152,	 258048 },
    206  1.14   thorpej 
    207  1.14   thorpej 	{ RD7958AID,	0,	"7958A",	NRD7958ABPT,
    208  1.14   thorpej 	  NRD7958ATRK,	1013,	 255276 },
    209  1.14   thorpej 
    210  1.14   thorpej 	{ RD7957AID,	0,	"7957A",	NRD7957ABPT,
    211  1.14   thorpej 	  NRD7957ATRK,	1036,	 159544 },
    212  1.14   thorpej 
    213  1.14   thorpej 	{ RD7933HID,	0,	"7933H",	NRD7933HBPT,
    214  1.14   thorpej 	  NRD7933HTRK,	1321,	 789958 },
    215  1.14   thorpej 
    216  1.14   thorpej 	{ RD9134LID,	1,	"9134L",	NRD9134LBPT,
    217  1.14   thorpej 	  NRD9134LTRK,	973,	  77840 },
    218  1.14   thorpej 
    219  1.14   thorpej 	{ RD7936HID,	0,	"7936H",	NRD7936HBPT,
    220  1.14   thorpej 	  NRD7936HTRK,	698,	 600978 },
    221  1.14   thorpej 
    222  1.14   thorpej 	{ RD7937HID,	0,	"7937H",	NRD7937HBPT,
    223  1.14   thorpej 	  NRD7937HTRK,	698,	1116102 },
    224  1.14   thorpej 
    225  1.14   thorpej 	{ RD7914CTID,	0,	"7914CT",	NRD7914PBPT,
    226  1.14   thorpej 	  NRD7914PTRK,	1152,	 258048 },
    227  1.14   thorpej 
    228  1.14   thorpej 	{ RD7946AID,	0,	"7946A",	NRD7945ABPT,
    229  1.14   thorpej 	  NRD7945ATRK,	968,	 108416 },
    230  1.14   thorpej 
    231  1.14   thorpej 	{ RD9134LID,	1,	"9122D",	NRD9122SBPT,
    232  1.14   thorpej 	  NRD9122STRK,	77,	   1232 },
    233  1.14   thorpej 
    234  1.14   thorpej 	{ RD7957BID,	0,	"7957B",	NRD7957BBPT,
    235  1.14   thorpej 	  NRD7957BTRK,	1269,	 159894 },
    236  1.14   thorpej 
    237  1.14   thorpej 	{ RD7958BID,	0,	"7958B",	NRD7958BBPT,
    238  1.14   thorpej 	  NRD7958BTRK,	786,	 297108 },
    239  1.14   thorpej 
    240  1.14   thorpej 	{ RD7959BID,	0,	"7959B",	NRD7959BBPT,
    241  1.14   thorpej 	  NRD7959BTRK,	1572,	 594216 },
    242  1.14   thorpej 
    243  1.14   thorpej 	{ RD2200AID,	0,	"2200A",	NRD2200ABPT,
    244  1.14   thorpej 	  NRD2200ATRK,	1449,	 654948 },
    245  1.14   thorpej 
    246  1.14   thorpej 	{ RD2203AID,	0,	"2203A",	NRD2203ABPT,
    247  1.14   thorpej 	  NRD2203ATRK,	1449,	1309896 }
    248   1.1       cgd };
    249   1.8   mycroft int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
    250   1.1       cgd 
    251  1.29    scottr bdev_decl(rd);
    252  1.29    scottr cdev_decl(rd);
    253  1.29    scottr 
    254  1.29    scottr int	rdident __P((struct device *, struct rd_softc *,
    255  1.29    scottr 	    struct hpibbus_attach_args *));
    256  1.29    scottr void	rdreset __P((struct rd_softc *));
    257  1.29    scottr void	rdustart __P((struct rd_softc *));
    258  1.29    scottr int	rdgetinfo __P((dev_t));
    259  1.29    scottr void	rdrestart __P((void *));
    260  1.29    scottr struct buf *rdfinish __P((struct rd_softc *, struct buf *));
    261  1.29    scottr 
    262  1.29    scottr void	rdrestart __P((void *));
    263  1.29    scottr void	rdustart __P((struct rd_softc *));
    264  1.29    scottr struct buf *rdfinish __P((struct rd_softc *, struct buf *));
    265  1.29    scottr void	rdstart __P((void *));
    266  1.29    scottr void	rdgo __P((void *));
    267  1.29    scottr void	rdintr __P((void *));
    268  1.29    scottr int	rdstatus __P((struct rd_softc *));
    269  1.29    scottr int	rderror __P((int));
    270  1.29    scottr #ifdef DEBUG
    271  1.29    scottr void	rdprinterr __P((char *, short, char **));
    272  1.29    scottr #endif
    273  1.29    scottr 
    274  1.29    scottr int	rdmatch __P((struct device *, struct cfdata *, void *));
    275  1.29    scottr void	rdattach __P((struct device *, struct device *, void *));
    276  1.29    scottr 
    277  1.29    scottr struct cfattach rd_ca = {
    278  1.29    scottr 	sizeof(struct rd_softc), rdmatch, rdattach
    279  1.29    scottr };
    280  1.29    scottr 
    281  1.36   thorpej extern struct cfdriver rd_cd;
    282  1.29    scottr 
    283  1.15   thorpej int
    284  1.27   thorpej rdmatch(parent, match, aux)
    285  1.27   thorpej 	struct device *parent;
    286  1.27   thorpej 	struct cfdata *match;
    287  1.27   thorpej 	void *aux;
    288   1.1       cgd {
    289  1.27   thorpej 	struct hpibbus_attach_args *ha = aux;
    290   1.1       cgd 
    291  1.27   thorpej 	/*
    292  1.27   thorpej 	 * Set punit if operator specified one in the kernel
    293  1.27   thorpej 	 * configuration file.
    294  1.27   thorpej 	 */
    295  1.34    kleink 	if (match->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
    296  1.27   thorpej 	    match->hpibbuscf_punit < HPIB_NPUNITS)
    297  1.27   thorpej 		ha->ha_punit = match->hpibbuscf_punit;
    298  1.27   thorpej 
    299  1.27   thorpej 	if (rdident(parent, NULL, ha) == 0) {
    300  1.18   thorpej 		/*
    301  1.27   thorpej 		 * XXX Some aging HP-IB drives are slow to
    302  1.27   thorpej 		 * XXX respond; give them a chance to catch
    303  1.27   thorpej 		 * XXX up and probe them again.
    304  1.18   thorpej 		 */
    305  1.27   thorpej 		delay(10000);
    306  1.27   thorpej 		ha->ha_id = hpibid(parent->dv_unit, ha->ha_slave);
    307  1.27   thorpej 		return (rdident(parent, NULL, ha));
    308  1.18   thorpej 	}
    309  1.27   thorpej 	return (1);
    310  1.27   thorpej }
    311  1.17   thorpej 
    312  1.27   thorpej void
    313  1.27   thorpej rdattach(parent, self, aux)
    314  1.27   thorpej 	struct device *parent, *self;
    315  1.27   thorpej 	void *aux;
    316  1.27   thorpej {
    317  1.27   thorpej 	struct rd_softc *sc = (struct rd_softc *)self;
    318  1.27   thorpej 	struct hpibbus_attach_args *ha = aux;
    319  1.27   thorpej 
    320  1.37   thorpej 	BUFQ_INIT(&sc->sc_tab);
    321  1.37   thorpej 
    322  1.27   thorpej 	if (rdident(parent, sc, ha) == 0) {
    323  1.27   thorpej 		printf("\n%s: didn't respond to describe command!\n",
    324  1.27   thorpej 		    sc->sc_dev.dv_xname);
    325  1.27   thorpej 		return;
    326  1.27   thorpej 	}
    327  1.17   thorpej 
    328  1.17   thorpej 	/*
    329  1.17   thorpej 	 * Initialize and attach the disk structure.
    330  1.17   thorpej 	 */
    331  1.27   thorpej 	bzero(&sc->sc_dkdev, sizeof(sc->sc_dkdev));
    332  1.27   thorpej 	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
    333  1.27   thorpej 	disk_attach(&sc->sc_dkdev);
    334  1.27   thorpej 
    335  1.27   thorpej 	sc->sc_slave = ha->ha_slave;
    336  1.27   thorpej 	sc->sc_punit = ha->ha_punit;
    337  1.27   thorpej 
    338  1.27   thorpej 	/* Initialize the hpib job queue entry */
    339  1.27   thorpej 	sc->sc_hq.hq_softc = sc;
    340  1.27   thorpej 	sc->sc_hq.hq_slave = sc->sc_slave;
    341  1.27   thorpej 	sc->sc_hq.hq_start = rdstart;
    342  1.27   thorpej 	sc->sc_hq.hq_go = rdgo;
    343  1.27   thorpej 	sc->sc_hq.hq_intr = rdintr;
    344  1.15   thorpej 
    345  1.27   thorpej 	sc->sc_flags = RDF_ALIVE;
    346   1.1       cgd #ifdef DEBUG
    347   1.1       cgd 	/* always report errors */
    348   1.1       cgd 	if (rddebug & RDB_ERROR)
    349   1.1       cgd 		rderrthresh = 0;
    350   1.1       cgd #endif
    351   1.1       cgd }
    352   1.1       cgd 
    353  1.15   thorpej int
    354  1.27   thorpej rdident(parent, sc, ha)
    355  1.27   thorpej 	struct device *parent;
    356  1.27   thorpej 	struct rd_softc *sc;
    357  1.27   thorpej 	struct hpibbus_attach_args *ha;
    358   1.1       cgd {
    359  1.27   thorpej 	struct rd_describe *desc = sc != NULL ? &sc->sc_rddesc : NULL;
    360   1.1       cgd 	u_char stat, cmd[3];
    361   1.1       cgd 	char name[7];
    362  1.27   thorpej 	int i, id, n, ctlr, slave;
    363  1.27   thorpej 
    364  1.27   thorpej 	ctlr = parent->dv_unit;
    365  1.27   thorpej 	slave = ha->ha_slave;
    366   1.1       cgd 
    367  1.27   thorpej 	/* Verify that we have a CS80 device. */
    368  1.27   thorpej 	if ((ha->ha_id & 0x200) == 0)
    369  1.27   thorpej 		return (0);
    370  1.27   thorpej 
    371  1.27   thorpej 	/* Is it one of the disks we support? */
    372  1.27   thorpej 	for (id = 0; id < numrdidentinfo; id++)
    373  1.27   thorpej 		if (ha->ha_id == rdidentinfo[id].ri_hwid)
    374  1.27   thorpej 			break;
    375  1.27   thorpej 	if (id == numrdidentinfo || ha->ha_punit > rdidentinfo[id].ri_maxunum)
    376  1.27   thorpej 		return (0);
    377   1.1       cgd 
    378   1.1       cgd 	/*
    379  1.27   thorpej 	 * If we're just probing for the device, that's all the
    380  1.27   thorpej 	 * work we need to do.
    381   1.1       cgd 	 */
    382  1.27   thorpej 	if (sc == NULL)
    383  1.27   thorpej 		return (1);
    384   1.1       cgd 
    385   1.1       cgd 	/*
    386  1.27   thorpej 	 * Reset device and collect description
    387   1.1       cgd 	 */
    388  1.27   thorpej 	rdreset(sc);
    389  1.27   thorpej 	cmd[0] = C_SUNIT(ha->ha_punit);
    390   1.1       cgd 	cmd[1] = C_SVOL(0);
    391   1.1       cgd 	cmd[2] = C_DESC;
    392   1.1       cgd 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
    393  1.14   thorpej 	hpibrecv(ctlr, slave, C_EXEC, desc, 37);
    394   1.1       cgd 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    395  1.27   thorpej 	bzero(name, sizeof(name));
    396  1.27   thorpej 	if (stat == 0) {
    397  1.27   thorpej 		n = desc->d_name;
    398   1.1       cgd 		for (i = 5; i >= 0; i--) {
    399   1.1       cgd 			name[i] = (n & 0xf) + '0';
    400   1.1       cgd 			n >>= 4;
    401   1.1       cgd 		}
    402   1.1       cgd 	}
    403  1.27   thorpej 
    404   1.1       cgd #ifdef DEBUG
    405   1.1       cgd 	if (rddebug & RDB_IDENT) {
    406  1.27   thorpej 		printf("\n%s: name: %x ('%s')\n",
    407  1.27   thorpej 		    sc->sc_dev.dv_xname, desc->d_name, name);
    408  1.24  christos 		printf("  iuw %x, maxxfr %d, ctype %d\n",
    409  1.27   thorpej 		    desc->d_iuw, desc->d_cmaxxfr, desc->d_ctype);
    410  1.24  christos 		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
    411  1.27   thorpej 		    desc->d_utype, desc->d_sectsize,
    412  1.27   thorpej 		    desc->d_blkbuf, desc->d_burstsize, desc->d_blocktime);
    413  1.24  christos 		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
    414  1.27   thorpej 		    desc->d_uavexfr, desc->d_retry, desc->d_access,
    415  1.27   thorpej 		    desc->d_maxint, desc->d_fvbyte, desc->d_rvbyte);
    416  1.24  christos 		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
    417  1.27   thorpej 		    desc->d_maxcyl, desc->d_maxhead, desc->d_maxsect,
    418  1.27   thorpej 		    desc->d_maxvsectl, desc->d_interleave);
    419  1.27   thorpej 		printf("%s", sc->sc_dev.dv_xname);
    420   1.1       cgd 	}
    421   1.1       cgd #endif
    422  1.27   thorpej 
    423   1.1       cgd 	/*
    424   1.1       cgd 	 * Take care of a couple of anomolies:
    425   1.1       cgd 	 * 1. 7945A and 7946A both return same HW id
    426   1.1       cgd 	 * 2. 9122S and 9134D both return same HW id
    427   1.1       cgd 	 * 3. 9122D and 9134L both return same HW id
    428   1.1       cgd 	 */
    429  1.27   thorpej 	switch (ha->ha_id) {
    430   1.1       cgd 	case RD7946AID:
    431   1.1       cgd 		if (bcmp(name, "079450", 6) == 0)
    432   1.1       cgd 			id = RD7945A;
    433   1.1       cgd 		else
    434   1.1       cgd 			id = RD7946A;
    435   1.1       cgd 		break;
    436   1.1       cgd 
    437   1.1       cgd 	case RD9134LID:
    438   1.1       cgd 		if (bcmp(name, "091340", 6) == 0)
    439   1.1       cgd 			id = RD9134L;
    440   1.1       cgd 		else
    441   1.1       cgd 			id = RD9122D;
    442   1.1       cgd 		break;
    443   1.1       cgd 
    444   1.1       cgd 	case RD9134DID:
    445   1.1       cgd 		if (bcmp(name, "091220", 6) == 0)
    446   1.1       cgd 			id = RD9122S;
    447   1.1       cgd 		else
    448   1.1       cgd 			id = RD9134D;
    449   1.1       cgd 		break;
    450   1.1       cgd 	}
    451  1.27   thorpej 
    452  1.27   thorpej 	sc->sc_type = id;
    453  1.27   thorpej 
    454  1.14   thorpej 	/*
    455  1.14   thorpej 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
    456  1.27   thorpej 	 * XXX off the driver because all of this code assumes 512 byte
    457  1.27   thorpej 	 * XXX blocks.  ICK!
    458  1.14   thorpej 	 */
    459  1.27   thorpej 	printf(": %s\n", rdidentinfo[id].ri_desc);
    460  1.27   thorpej 	printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
    461  1.27   thorpej 	    sc->sc_dev.dv_xname, rdidentinfo[id].ri_ncyl,
    462  1.27   thorpej 	    rdidentinfo[id].ri_ntpc, rdidentinfo[id].ri_nblocks,
    463  1.27   thorpej 	    DEV_BSIZE);
    464  1.27   thorpej 
    465  1.27   thorpej 	return (1);
    466   1.1       cgd }
    467   1.1       cgd 
    468  1.27   thorpej void
    469  1.27   thorpej rdreset(rs)
    470  1.28    scottr 	struct rd_softc *rs;
    471   1.1       cgd {
    472  1.27   thorpej 	int ctlr = rs->sc_dev.dv_parent->dv_unit;
    473  1.27   thorpej 	int slave = rs->sc_slave;
    474   1.1       cgd 	u_char stat;
    475   1.1       cgd 
    476   1.1       cgd 	rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
    477   1.1       cgd 	rs->sc_clear.c_cmd = C_CLEAR;
    478  1.27   thorpej 	hpibsend(ctlr, slave, C_TCMD, &rs->sc_clear, sizeof(rs->sc_clear));
    479  1.27   thorpej 	hpibswait(ctlr, slave);
    480  1.27   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    481  1.27   thorpej 
    482   1.1       cgd 	rs->sc_src.c_unit = C_SUNIT(RDCTLR);
    483   1.1       cgd 	rs->sc_src.c_nop = C_NOP;
    484   1.1       cgd 	rs->sc_src.c_cmd = C_SREL;
    485   1.1       cgd 	rs->sc_src.c_param = C_REL;
    486  1.27   thorpej 	hpibsend(ctlr, slave, C_CMD, &rs->sc_src, sizeof(rs->sc_src));
    487  1.27   thorpej 	hpibswait(ctlr, slave);
    488  1.27   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    489  1.27   thorpej 
    490   1.1       cgd 	rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
    491   1.1       cgd 	rs->sc_ssmc.c_cmd = C_SSM;
    492   1.1       cgd 	rs->sc_ssmc.c_refm = REF_MASK;
    493   1.1       cgd 	rs->sc_ssmc.c_fefm = FEF_MASK;
    494   1.1       cgd 	rs->sc_ssmc.c_aefm = AEF_MASK;
    495   1.1       cgd 	rs->sc_ssmc.c_iefm = IEF_MASK;
    496  1.27   thorpej 	hpibsend(ctlr, slave, C_CMD, &rs->sc_ssmc, sizeof(rs->sc_ssmc));
    497  1.27   thorpej 	hpibswait(ctlr, slave);
    498  1.27   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    499   1.1       cgd #ifdef DEBUG
    500  1.27   thorpej 	rs->sc_stats.rdresets++;
    501   1.1       cgd #endif
    502   1.1       cgd }
    503   1.1       cgd 
    504   1.8   mycroft /*
    505   1.8   mycroft  * Read or constuct a disklabel
    506   1.8   mycroft  */
    507   1.8   mycroft int
    508   1.8   mycroft rdgetinfo(dev)
    509   1.8   mycroft 	dev_t dev;
    510   1.8   mycroft {
    511   1.8   mycroft 	int unit = rdunit(dev);
    512  1.27   thorpej 	struct rd_softc *rs = rd_cd.cd_devs[unit];
    513  1.28    scottr 	struct disklabel *lp = rs->sc_dkdev.dk_label;
    514  1.28    scottr 	struct partition *pi;
    515  1.28    scottr 	char *msg;
    516   1.8   mycroft 
    517   1.8   mycroft 	/*
    518   1.8   mycroft 	 * Set some default values to use while reading the label
    519   1.8   mycroft 	 * or to use if there isn't a label.
    520   1.8   mycroft 	 */
    521   1.8   mycroft 	bzero((caddr_t)lp, sizeof *lp);
    522   1.8   mycroft 	lp->d_type = DTYPE_HPIB;
    523   1.8   mycroft 	lp->d_secsize = DEV_BSIZE;
    524   1.8   mycroft 	lp->d_nsectors = 32;
    525   1.8   mycroft 	lp->d_ntracks = 20;
    526   1.8   mycroft 	lp->d_ncylinders = 1;
    527   1.8   mycroft 	lp->d_secpercyl = 32*20;
    528   1.8   mycroft 	lp->d_npartitions = 3;
    529   1.8   mycroft 	lp->d_partitions[2].p_offset = 0;
    530   1.8   mycroft 	lp->d_partitions[2].p_size = LABELSECTOR+1;
    531   1.8   mycroft 
    532   1.8   mycroft 	/*
    533   1.8   mycroft 	 * Now try to read the disklabel
    534   1.8   mycroft 	 */
    535  1.16   thorpej 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
    536   1.8   mycroft 	if (msg == NULL)
    537  1.28    scottr 		return (0);
    538   1.8   mycroft 
    539   1.8   mycroft 	pi = lp->d_partitions;
    540  1.27   thorpej 	printf("%s: WARNING: %s, ", rs->sc_dev.dv_xname, msg);
    541   1.8   mycroft #ifdef COMPAT_NOLABEL
    542  1.24  christos 	printf("using old default partitioning\n");
    543   1.8   mycroft 	rdmakedisklabel(unit, lp);
    544   1.8   mycroft #else
    545  1.24  christos 	printf("defining `c' partition as entire disk\n");
    546   1.8   mycroft 	pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
    547   1.9   mycroft 	/* XXX reset other info since readdisklabel screws with it */
    548   1.9   mycroft 	lp->d_npartitions = 3;
    549   1.9   mycroft 	pi[0].p_size = 0;
    550   1.8   mycroft #endif
    551   1.8   mycroft 	return(0);
    552   1.8   mycroft }
    553   1.8   mycroft 
    554   1.1       cgd int
    555   1.1       cgd rdopen(dev, flags, mode, p)
    556   1.1       cgd 	dev_t dev;
    557   1.1       cgd 	int flags, mode;
    558   1.1       cgd 	struct proc *p;
    559   1.1       cgd {
    560  1.28    scottr 	int unit = rdunit(dev);
    561  1.28    scottr 	struct rd_softc *rs;
    562  1.26   thorpej 	int error, mask, part;
    563   1.1       cgd 
    564  1.27   thorpej 	if (unit >= rd_cd.cd_ndevs ||
    565  1.27   thorpej 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
    566  1.27   thorpej 	    (rs->sc_flags & RDF_ALIVE) == 0)
    567  1.27   thorpej 		return (ENXIO);
    568   1.8   mycroft 
    569   1.8   mycroft 	/*
    570   1.8   mycroft 	 * Wait for any pending opens/closes to complete
    571   1.8   mycroft 	 */
    572   1.8   mycroft 	while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
    573   1.8   mycroft 		sleep((caddr_t)rs, PRIBIO);
    574   1.8   mycroft 
    575   1.8   mycroft 	/*
    576   1.8   mycroft 	 * On first open, get label and partition info.
    577   1.8   mycroft 	 * We may block reading the label, so be careful
    578   1.8   mycroft 	 * to stop any other opens.
    579   1.8   mycroft 	 */
    580  1.17   thorpej 	if (rs->sc_dkdev.dk_openmask == 0) {
    581   1.8   mycroft 		rs->sc_flags |= RDF_OPENING;
    582   1.8   mycroft 		error = rdgetinfo(dev);
    583   1.8   mycroft 		rs->sc_flags &= ~RDF_OPENING;
    584   1.8   mycroft 		wakeup((caddr_t)rs);
    585   1.8   mycroft 		if (error)
    586   1.8   mycroft 			return(error);
    587   1.8   mycroft 	}
    588   1.8   mycroft 
    589  1.26   thorpej 	part = rdpart(dev);
    590  1.26   thorpej 	mask = 1 << part;
    591  1.26   thorpej 
    592  1.26   thorpej 	/* Check that the partition exists. */
    593  1.26   thorpej 	if (part != RAW_PART &&
    594  1.26   thorpej 	    (part > rs->sc_dkdev.dk_label->d_npartitions ||
    595  1.26   thorpej 	     rs->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
    596  1.26   thorpej 		return (ENXIO);
    597  1.26   thorpej 
    598  1.26   thorpej 	/* Ensure only one open at a time. */
    599  1.26   thorpej 	switch (mode) {
    600  1.26   thorpej 	case S_IFCHR:
    601  1.17   thorpej 		rs->sc_dkdev.dk_copenmask |= mask;
    602  1.26   thorpej 		break;
    603  1.26   thorpej 	case S_IFBLK:
    604  1.17   thorpej 		rs->sc_dkdev.dk_bopenmask |= mask;
    605  1.26   thorpej 		break;
    606  1.26   thorpej 	}
    607  1.26   thorpej 	rs->sc_dkdev.dk_openmask =
    608  1.26   thorpej 	    rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
    609  1.26   thorpej 
    610   1.8   mycroft 	return(0);
    611   1.8   mycroft }
    612   1.8   mycroft 
    613   1.8   mycroft int
    614   1.8   mycroft rdclose(dev, flag, mode, p)
    615   1.8   mycroft 	dev_t dev;
    616   1.8   mycroft 	int flag, mode;
    617   1.8   mycroft 	struct proc *p;
    618   1.8   mycroft {
    619   1.8   mycroft 	int unit = rdunit(dev);
    620  1.27   thorpej 	struct rd_softc *rs = rd_cd.cd_devs[unit];
    621  1.28    scottr 	struct disk *dk = &rs->sc_dkdev;
    622   1.8   mycroft 	int mask, s;
    623   1.8   mycroft 
    624   1.8   mycroft 	mask = 1 << rdpart(dev);
    625   1.8   mycroft 	if (mode == S_IFCHR)
    626  1.17   thorpej 		dk->dk_copenmask &= ~mask;
    627   1.8   mycroft 	else
    628  1.17   thorpej 		dk->dk_bopenmask &= ~mask;
    629  1.17   thorpej 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    630   1.8   mycroft 	/*
    631   1.8   mycroft 	 * On last close, we wait for all activity to cease since
    632   1.8   mycroft 	 * the label/parition info will become invalid.  Since we
    633   1.8   mycroft 	 * might sleep, we must block any opens while we are here.
    634   1.8   mycroft 	 * Note we don't have to about other closes since we know
    635   1.8   mycroft 	 * we are the last one.
    636   1.8   mycroft 	 */
    637  1.17   thorpej 	if (dk->dk_openmask == 0) {
    638   1.8   mycroft 		rs->sc_flags |= RDF_CLOSING;
    639   1.8   mycroft 		s = splbio();
    640  1.37   thorpej 		while (rs->sc_active) {
    641   1.8   mycroft 			rs->sc_flags |= RDF_WANTED;
    642  1.27   thorpej 			sleep((caddr_t)&rs->sc_tab, PRIBIO);
    643   1.8   mycroft 		}
    644   1.8   mycroft 		splx(s);
    645   1.8   mycroft 		rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
    646   1.8   mycroft 		wakeup((caddr_t)rs);
    647   1.8   mycroft 	}
    648   1.1       cgd 	return(0);
    649   1.1       cgd }
    650   1.1       cgd 
    651   1.4   mycroft void
    652   1.1       cgd rdstrategy(bp)
    653  1.28    scottr 	struct buf *bp;
    654   1.1       cgd {
    655   1.8   mycroft 	int unit = rdunit(bp->b_dev);
    656  1.27   thorpej 	struct rd_softc *rs = rd_cd.cd_devs[unit];
    657  1.28    scottr 	struct partition *pinfo;
    658  1.28    scottr 	daddr_t bn;
    659  1.28    scottr 	int sz, s;
    660  1.26   thorpej 	int offset;
    661   1.1       cgd 
    662   1.1       cgd #ifdef DEBUG
    663   1.1       cgd 	if (rddebug & RDB_FOLLOW)
    664  1.29    scottr 		printf("rdstrategy(%p): dev %x, bn %x, bcount %lx, %c\n",
    665   1.1       cgd 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    666   1.1       cgd 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    667   1.1       cgd #endif
    668   1.1       cgd 	bn = bp->b_blkno;
    669   1.1       cgd 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    670  1.17   thorpej 	pinfo = &rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
    671  1.26   thorpej 
    672  1.26   thorpej 	/* Don't perform partition translation on RAW_PART. */
    673  1.26   thorpej 	offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
    674  1.26   thorpej 
    675  1.26   thorpej 	if (rdpart(bp->b_dev) != RAW_PART) {
    676  1.26   thorpej 		/*
    677  1.26   thorpej 		 * XXX This block of code belongs in
    678  1.26   thorpej 		 * XXX bounds_check_with_label()
    679  1.26   thorpej 		 */
    680  1.26   thorpej 
    681  1.26   thorpej 		if (bn < 0 || bn + sz > pinfo->p_size) {
    682  1.26   thorpej 			sz = pinfo->p_size - bn;
    683  1.26   thorpej 			if (sz == 0) {
    684  1.26   thorpej 				bp->b_resid = bp->b_bcount;
    685  1.26   thorpej 				goto done;
    686  1.26   thorpej 			}
    687  1.26   thorpej 			if (sz < 0) {
    688  1.26   thorpej 				bp->b_error = EINVAL;
    689  1.26   thorpej 				goto bad;
    690  1.26   thorpej 			}
    691  1.26   thorpej 			bp->b_bcount = dbtob(sz);
    692   1.1       cgd 		}
    693  1.26   thorpej 		/*
    694  1.26   thorpej 		 * Check for write to write protected label
    695  1.26   thorpej 		 */
    696  1.26   thorpej 		if (bn + offset <= LABELSECTOR &&
    697  1.26   thorpej #if LABELSECTOR != 0
    698  1.26   thorpej 		    bn + offset + sz > LABELSECTOR &&
    699  1.26   thorpej #endif
    700  1.26   thorpej 		    !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
    701  1.26   thorpej 			bp->b_error = EROFS;
    702   1.8   mycroft 			goto bad;
    703   1.1       cgd 		}
    704   1.8   mycroft 	}
    705  1.37   thorpej 	bp->b_cylinder = bn + offset;		/* XXX */
    706   1.1       cgd 	s = splbio();
    707  1.37   thorpej 	disksort_cylinder(&rs->sc_tab, bp);	/* XXX */
    708  1.37   thorpej 	if (rs->sc_active == 0) {
    709  1.37   thorpej 		rs->sc_active = 1;
    710  1.27   thorpej 		rdustart(rs);
    711   1.1       cgd 	}
    712   1.1       cgd 	splx(s);
    713   1.1       cgd 	return;
    714   1.8   mycroft bad:
    715   1.8   mycroft 	bp->b_flags |= B_ERROR;
    716   1.1       cgd done:
    717   1.1       cgd 	biodone(bp);
    718   1.1       cgd }
    719   1.1       cgd 
    720   1.1       cgd /*
    721   1.1       cgd  * Called from timeout() when handling maintenance releases
    722   1.1       cgd  */
    723   1.8   mycroft void
    724   1.6   mycroft rdrestart(arg)
    725   1.6   mycroft 	void *arg;
    726   1.1       cgd {
    727   1.1       cgd 	int s = splbio();
    728  1.27   thorpej 	rdustart((struct rd_softc *)arg);
    729   1.1       cgd 	splx(s);
    730   1.1       cgd }
    731   1.1       cgd 
    732  1.27   thorpej void
    733  1.27   thorpej rdustart(rs)
    734  1.27   thorpej 	struct rd_softc *rs;
    735   1.1       cgd {
    736  1.28    scottr 	struct buf *bp;
    737   1.1       cgd 
    738  1.37   thorpej 	bp = BUFQ_FIRST(&rs->sc_tab);
    739   1.1       cgd 	rs->sc_addr = bp->b_un.b_addr;
    740   1.1       cgd 	rs->sc_resid = bp->b_bcount;
    741  1.27   thorpej 	if (hpibreq(rs->sc_dev.dv_parent, &rs->sc_hq))
    742  1.27   thorpej 		rdstart(rs);
    743   1.1       cgd }
    744   1.1       cgd 
    745   1.8   mycroft struct buf *
    746  1.27   thorpej rdfinish(rs, bp)
    747  1.28    scottr 	struct rd_softc *rs;
    748  1.28    scottr 	struct buf *bp;
    749   1.8   mycroft {
    750   1.8   mycroft 
    751  1.37   thorpej 	rs->sc_errcnt = 0;
    752  1.37   thorpej 	BUFQ_REMOVE(&rs->sc_tab, bp);
    753   1.8   mycroft 	bp->b_resid = 0;
    754   1.8   mycroft 	biodone(bp);
    755  1.27   thorpej 	hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
    756  1.37   thorpej 	if ((bp = BUFQ_FIRST(&rs->sc_tab)) != NULL)
    757  1.37   thorpej 		return (bp);
    758  1.37   thorpej 	rs->sc_active = 0;
    759   1.8   mycroft 	if (rs->sc_flags & RDF_WANTED) {
    760   1.8   mycroft 		rs->sc_flags &= ~RDF_WANTED;
    761   1.8   mycroft 		wakeup((caddr_t)dp);
    762   1.8   mycroft 	}
    763  1.27   thorpej 	return (NULL);
    764   1.8   mycroft }
    765   1.8   mycroft 
    766  1.27   thorpej void
    767  1.27   thorpej rdstart(arg)
    768  1.27   thorpej 	void *arg;
    769   1.1       cgd {
    770  1.27   thorpej 	struct rd_softc *rs = arg;
    771  1.37   thorpej 	struct buf *bp = BUFQ_FIRST(&rs->sc_tab);
    772  1.28    scottr 	int part, ctlr, slave;
    773  1.27   thorpej 
    774  1.27   thorpej 	ctlr = rs->sc_dev.dv_parent->dv_unit;
    775  1.27   thorpej 	slave = rs->sc_slave;
    776   1.1       cgd 
    777   1.1       cgd again:
    778   1.1       cgd #ifdef DEBUG
    779   1.1       cgd 	if (rddebug & RDB_FOLLOW)
    780  1.29    scottr 		printf("rdstart(%s): bp %p, %c\n", rs->sc_dev.dv_xname, bp,
    781   1.1       cgd 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    782   1.1       cgd #endif
    783   1.1       cgd 	part = rdpart(bp->b_dev);
    784   1.1       cgd 	rs->sc_flags |= RDF_SEEK;
    785   1.1       cgd 	rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
    786   1.1       cgd 	rs->sc_ioc.c_volume = C_SVOL(0);
    787   1.1       cgd 	rs->sc_ioc.c_saddr = C_SADDR;
    788   1.1       cgd 	rs->sc_ioc.c_hiaddr = 0;
    789  1.37   thorpej 	rs->sc_ioc.c_addr = RDBTOS(bp->b_cylinder);	/* XXX */
    790   1.1       cgd 	rs->sc_ioc.c_nop2 = C_NOP;
    791   1.1       cgd 	rs->sc_ioc.c_slen = C_SLEN;
    792   1.1       cgd 	rs->sc_ioc.c_len = rs->sc_resid;
    793   1.1       cgd 	rs->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
    794   1.1       cgd #ifdef DEBUG
    795   1.1       cgd 	if (rddebug & RDB_IO)
    796  1.29    scottr 		printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
    797  1.27   thorpej 		       ctlr, slave, C_CMD,
    798   1.1       cgd 		       &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
    799   1.1       cgd #endif
    800  1.27   thorpej 	if (hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
    801   1.1       cgd 		     sizeof(rs->sc_ioc)-2) == sizeof(rs->sc_ioc)-2) {
    802  1.17   thorpej 
    803  1.17   thorpej 		/* Instrumentation. */
    804  1.17   thorpej 		disk_busy(&rs->sc_dkdev);
    805  1.17   thorpej 		rs->sc_dkdev.dk_seek++;
    806  1.17   thorpej 
    807   1.1       cgd #ifdef DEBUG
    808   1.1       cgd 		if (rddebug & RDB_IO)
    809  1.27   thorpej 			printf("rdstart: hpibawait(%x)\n", ctlr);
    810   1.1       cgd #endif
    811  1.27   thorpej 		hpibawait(ctlr);
    812   1.1       cgd 		return;
    813   1.1       cgd 	}
    814   1.1       cgd 	/*
    815   1.1       cgd 	 * Experience has shown that the hpibwait in this hpibsend will
    816   1.1       cgd 	 * occasionally timeout.  It appears to occur mostly on old 7914
    817   1.1       cgd 	 * drives with full maintenance tracks.  We should probably
    818   1.1       cgd 	 * integrate this with the backoff code in rderror.
    819   1.1       cgd 	 */
    820   1.1       cgd #ifdef DEBUG
    821   1.1       cgd 	if (rddebug & RDB_ERROR)
    822  1.29    scottr 		printf("%s: rdstart: cmd %x adr %lx blk %d len %d ecnt %ld\n",
    823  1.27   thorpej 		       rs->sc_dev.dv_xname, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
    824  1.37   thorpej 		       bp->b_blkno, rs->sc_resid, rs->sc_errcnt);
    825  1.27   thorpej 	rs->sc_stats.rdretries++;
    826   1.1       cgd #endif
    827   1.1       cgd 	rs->sc_flags &= ~RDF_SEEK;
    828  1.27   thorpej 	rdreset(rs);
    829  1.37   thorpej 	if (rs->sc_errcnt++ < RDRETRY)
    830   1.1       cgd 		goto again;
    831  1.28    scottr 	printf("%s: rdstart err: cmd 0x%x sect %ld blk %d len %d\n",
    832  1.27   thorpej 	       rs->sc_dev.dv_xname, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
    833   1.1       cgd 	       bp->b_blkno, rs->sc_resid);
    834   1.1       cgd 	bp->b_flags |= B_ERROR;
    835   1.1       cgd 	bp->b_error = EIO;
    836  1.27   thorpej 	bp = rdfinish(rs, bp);
    837   1.8   mycroft 	if (bp) {
    838   1.8   mycroft 		rs->sc_addr = bp->b_un.b_addr;
    839   1.8   mycroft 		rs->sc_resid = bp->b_bcount;
    840  1.27   thorpej 		if (hpibreq(rs->sc_dev.dv_parent, &rs->sc_hq))
    841   1.8   mycroft 			goto again;
    842   1.1       cgd 	}
    843   1.1       cgd }
    844   1.1       cgd 
    845  1.27   thorpej void
    846  1.27   thorpej rdgo(arg)
    847  1.27   thorpej 	void *arg;
    848   1.1       cgd {
    849  1.27   thorpej 	struct rd_softc *rs = arg;
    850  1.37   thorpej 	struct buf *bp = BUFQ_FIRST(&rs->sc_tab);
    851  1.27   thorpej 	int rw, ctlr, slave;
    852  1.27   thorpej 
    853  1.27   thorpej 	ctlr = rs->sc_dev.dv_parent->dv_unit;
    854  1.27   thorpej 	slave = rs->sc_slave;
    855  1.13   thorpej 
    856  1.13   thorpej 	rw = bp->b_flags & B_READ;
    857   1.1       cgd 
    858  1.17   thorpej 	/* Instrumentation. */
    859  1.17   thorpej 	disk_busy(&rs->sc_dkdev);
    860  1.17   thorpej 
    861   1.8   mycroft #ifdef USELEDS
    862  1.31   thorpej 	ledcontrol(0, 0, LED_DISK);
    863   1.8   mycroft #endif
    864  1.27   thorpej 	hpibgo(ctlr, slave, C_EXEC, rs->sc_addr, rs->sc_resid, rw, rw != 0);
    865   1.1       cgd }
    866   1.1       cgd 
    867  1.27   thorpej /* ARGSUSED */
    868  1.27   thorpej void
    869  1.20   thorpej rdintr(arg)
    870  1.20   thorpej 	void *arg;
    871   1.1       cgd {
    872  1.28    scottr 	struct rd_softc *rs = arg;
    873  1.27   thorpej 	int unit = rs->sc_dev.dv_unit;
    874  1.37   thorpej 	struct buf *bp = BUFQ_FIRST(&rs->sc_tab);
    875   1.1       cgd 	u_char stat = 13;	/* in case hpibrecv fails */
    876  1.27   thorpej 	int rv, restart, ctlr, slave;
    877  1.27   thorpej 
    878  1.27   thorpej 	ctlr = rs->sc_dev.dv_parent->dv_unit;
    879  1.27   thorpej 	slave = rs->sc_slave;
    880  1.27   thorpej 
    881   1.1       cgd #ifdef DEBUG
    882   1.1       cgd 	if (rddebug & RDB_FOLLOW)
    883  1.29    scottr 		printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
    884   1.1       cgd 		       (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
    885   1.1       cgd 	if (bp == NULL) {
    886  1.27   thorpej 		printf("%s: bp == NULL\n", rs->sc_dev.dv_xname);
    887   1.1       cgd 		return;
    888   1.1       cgd 	}
    889   1.1       cgd #endif
    890  1.17   thorpej 	disk_unbusy(&rs->sc_dkdev, (bp->b_bcount - bp->b_resid));
    891  1.17   thorpej 
    892   1.1       cgd 	if (rs->sc_flags & RDF_SEEK) {
    893   1.1       cgd 		rs->sc_flags &= ~RDF_SEEK;
    894  1.27   thorpej 		if (hpibustart(ctlr))
    895  1.27   thorpej 			rdgo(rs);
    896   1.1       cgd 		return;
    897   1.1       cgd 	}
    898   1.1       cgd 	if ((rs->sc_flags & RDF_SWAIT) == 0) {
    899   1.1       cgd #ifdef DEBUG
    900  1.27   thorpej 		rs->sc_stats.rdpolltries++;
    901   1.1       cgd #endif
    902  1.27   thorpej 		if (hpibpptest(ctlr, slave) == 0) {
    903   1.1       cgd #ifdef DEBUG
    904  1.27   thorpej 			rs->sc_stats.rdpollwaits++;
    905   1.1       cgd #endif
    906  1.17   thorpej 
    907  1.17   thorpej 			/* Instrumentation. */
    908  1.17   thorpej 			disk_busy(&rs->sc_dkdev);
    909   1.1       cgd 			rs->sc_flags |= RDF_SWAIT;
    910  1.27   thorpej 			hpibawait(ctlr);
    911   1.1       cgd 			return;
    912   1.1       cgd 		}
    913   1.1       cgd 	} else
    914   1.1       cgd 		rs->sc_flags &= ~RDF_SWAIT;
    915  1.27   thorpej 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
    916   1.1       cgd 	if (rv != 1 || stat) {
    917   1.1       cgd #ifdef DEBUG
    918   1.1       cgd 		if (rddebug & RDB_ERROR)
    919  1.24  christos 			printf("rdintr: recv failed or bad stat %d\n", stat);
    920   1.1       cgd #endif
    921   1.1       cgd 		restart = rderror(unit);
    922   1.1       cgd #ifdef DEBUG
    923  1.27   thorpej 		rs->sc_stats.rdretries++;
    924   1.1       cgd #endif
    925  1.37   thorpej 		if (rs->sc_errcnt++ < RDRETRY) {
    926   1.1       cgd 			if (restart)
    927  1.27   thorpej 				rdstart(rs);
    928   1.1       cgd 			return;
    929   1.1       cgd 		}
    930   1.1       cgd 		bp->b_flags |= B_ERROR;
    931   1.1       cgd 		bp->b_error = EIO;
    932   1.1       cgd 	}
    933  1.27   thorpej 	if (rdfinish(rs, bp))
    934  1.27   thorpej 		rdustart(rs);
    935   1.1       cgd }
    936   1.1       cgd 
    937  1.27   thorpej int
    938   1.1       cgd rdstatus(rs)
    939  1.28    scottr 	struct rd_softc *rs;
    940   1.1       cgd {
    941  1.28    scottr 	int c, s;
    942   1.1       cgd 	u_char stat;
    943   1.1       cgd 	int rv;
    944   1.1       cgd 
    945  1.27   thorpej 	c = rs->sc_dev.dv_parent->dv_unit;
    946  1.27   thorpej 	s = rs->sc_slave;
    947   1.1       cgd 	rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
    948   1.1       cgd 	rs->sc_rsc.c_sram = C_SRAM;
    949   1.1       cgd 	rs->sc_rsc.c_ram = C_RAM;
    950   1.1       cgd 	rs->sc_rsc.c_cmd = C_STATUS;
    951   1.1       cgd 	bzero((caddr_t)&rs->sc_stat, sizeof(rs->sc_stat));
    952   1.1       cgd 	rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
    953   1.1       cgd 	if (rv != sizeof(rs->sc_rsc)) {
    954   1.1       cgd #ifdef DEBUG
    955   1.1       cgd 		if (rddebug & RDB_STATUS)
    956  1.24  christos 			printf("rdstatus: send C_CMD failed %d != %d\n",
    957   1.1       cgd 			       rv, sizeof(rs->sc_rsc));
    958   1.1       cgd #endif
    959   1.1       cgd 		return(1);
    960   1.1       cgd 	}
    961   1.1       cgd 	rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
    962   1.1       cgd 	if (rv != sizeof(rs->sc_stat)) {
    963   1.1       cgd #ifdef DEBUG
    964   1.1       cgd 		if (rddebug & RDB_STATUS)
    965  1.24  christos 			printf("rdstatus: send C_EXEC failed %d != %d\n",
    966   1.1       cgd 			       rv, sizeof(rs->sc_stat));
    967   1.1       cgd #endif
    968   1.1       cgd 		return(1);
    969   1.1       cgd 	}
    970   1.1       cgd 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
    971   1.1       cgd 	if (rv != 1 || stat) {
    972   1.1       cgd #ifdef DEBUG
    973   1.1       cgd 		if (rddebug & RDB_STATUS)
    974  1.24  christos 			printf("rdstatus: recv failed %d or bad stat %d\n",
    975   1.1       cgd 			       rv, stat);
    976   1.1       cgd #endif
    977   1.1       cgd 		return(1);
    978   1.1       cgd 	}
    979   1.1       cgd 	return(0);
    980   1.1       cgd }
    981   1.1       cgd 
    982   1.1       cgd /*
    983   1.1       cgd  * Deal with errors.
    984   1.1       cgd  * Returns 1 if request should be restarted,
    985   1.1       cgd  * 0 if we should just quietly give up.
    986   1.1       cgd  */
    987  1.27   thorpej int
    988   1.1       cgd rderror(unit)
    989   1.1       cgd 	int unit;
    990   1.1       cgd {
    991  1.27   thorpej 	struct rd_softc *rs = rd_cd.cd_devs[unit];
    992  1.28    scottr 	struct rd_stat *sp;
    993   1.1       cgd 	struct buf *bp;
    994   1.1       cgd 	daddr_t hwbn, pbn;
    995  1.28    scottr 	char *hexstr __P((int, int)); /* XXX */
    996   1.1       cgd 
    997   1.1       cgd 	if (rdstatus(rs)) {
    998   1.1       cgd #ifdef DEBUG
    999  1.27   thorpej 		printf("%s: couldn't get status\n", rs->sc_dev.dv_xname);
   1000   1.1       cgd #endif
   1001  1.27   thorpej 		rdreset(rs);
   1002   1.1       cgd 		return(1);
   1003   1.1       cgd 	}
   1004   1.1       cgd 	sp = &rs->sc_stat;
   1005   1.1       cgd 	if (sp->c_fef & FEF_REXMT)
   1006   1.1       cgd 		return(1);
   1007   1.1       cgd 	if (sp->c_fef & FEF_PF) {
   1008  1.27   thorpej 		rdreset(rs);
   1009   1.1       cgd 		return(1);
   1010   1.1       cgd 	}
   1011   1.1       cgd 	/*
   1012   1.1       cgd 	 * Unit requests release for internal maintenance.
   1013   1.1       cgd 	 * We just delay awhile and try again later.  Use expontially
   1014   1.1       cgd 	 * increasing backoff ala ethernet drivers since we don't really
   1015   1.1       cgd 	 * know how long the maintenance will take.  With RDWAITC and
   1016   1.1       cgd 	 * RDRETRY as defined, the range is 1 to 32 seconds.
   1017   1.1       cgd 	 */
   1018   1.1       cgd 	if (sp->c_fef & FEF_IMR) {
   1019   1.1       cgd 		extern int hz;
   1020  1.37   thorpej 		int rdtimo = RDWAITC << rs->sc_errcnt;
   1021   1.1       cgd #ifdef DEBUG
   1022  1.24  christos 		printf("%s: internal maintenance, %d second timeout\n",
   1023  1.27   thorpej 		       rs->sc_dev.dv_xname, rdtimo);
   1024  1.27   thorpej 		rs->sc_stats.rdtimeouts++;
   1025   1.1       cgd #endif
   1026  1.27   thorpej 		hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
   1027  1.27   thorpej 		timeout(rdrestart, rs, rdtimo * hz);
   1028   1.1       cgd 		return(0);
   1029   1.1       cgd 	}
   1030   1.1       cgd 	/*
   1031   1.1       cgd 	 * Only report error if we have reached the error reporting
   1032   1.1       cgd 	 * threshhold.  By default, this will only report after the
   1033   1.1       cgd 	 * retry limit has been exceeded.
   1034   1.1       cgd 	 */
   1035  1.37   thorpej 	if (rs->sc_errcnt < rderrthresh)
   1036   1.1       cgd 		return(1);
   1037   1.1       cgd 
   1038   1.1       cgd 	/*
   1039   1.1       cgd 	 * First conjure up the block number at which the error occured.
   1040   1.1       cgd 	 * Note that not all errors report a block number, in that case
   1041   1.1       cgd 	 * we just use b_blkno.
   1042   1.1       cgd  	 */
   1043  1.37   thorpej 	bp = BUFQ_FIRST(&rs->sc_tab);
   1044  1.17   thorpej 	pbn = rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
   1045   1.1       cgd 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
   1046   1.1       cgd 	    (sp->c_ief & IEF_RRMASK)) {
   1047   1.1       cgd 		hwbn = RDBTOS(pbn + bp->b_blkno);
   1048   1.1       cgd 		pbn = bp->b_blkno;
   1049   1.1       cgd 	} else {
   1050   1.1       cgd 		hwbn = sp->c_blk;
   1051   1.1       cgd 		pbn = RDSTOB(hwbn) - pbn;
   1052   1.1       cgd 	}
   1053   1.1       cgd 	/*
   1054   1.1       cgd 	 * Now output a generic message suitable for badsect.
   1055   1.1       cgd 	 * Note that we don't use harderr cuz it just prints
   1056   1.1       cgd 	 * out b_blkno which is just the beginning block number
   1057   1.1       cgd 	 * of the transfer, not necessary where the error occured.
   1058   1.1       cgd 	 */
   1059  1.27   thorpej 	printf("%s%c: hard error sn%d\n", rs->sc_dev.dv_xname,
   1060  1.27   thorpej 	    'a'+rdpart(bp->b_dev), pbn);
   1061   1.1       cgd 	/*
   1062   1.1       cgd 	 * Now report the status as returned by the hardware with
   1063   1.1       cgd 	 * attempt at interpretation (unless debugging).
   1064   1.1       cgd 	 */
   1065  1.27   thorpej 	printf("%s %s error:", rs->sc_dev.dv_xname,
   1066  1.27   thorpej 	    (bp->b_flags & B_READ) ? "read" : "write");
   1067   1.1       cgd #ifdef DEBUG
   1068   1.1       cgd 	if (rddebug & RDB_ERROR) {
   1069   1.1       cgd 		/* status info */
   1070  1.24  christos 		printf("\n    volume: %d, unit: %d\n",
   1071   1.1       cgd 		       (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
   1072   1.1       cgd 		rdprinterr("reject", sp->c_ref, err_reject);
   1073   1.1       cgd 		rdprinterr("fault", sp->c_fef, err_fault);
   1074   1.1       cgd 		rdprinterr("access", sp->c_aef, err_access);
   1075   1.1       cgd 		rdprinterr("info", sp->c_ief, err_info);
   1076  1.24  christos 		printf("    block: %d, P1-P10: ", hwbn);
   1077  1.30   thorpej 		printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1078  1.30   thorpej 		printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1079  1.30   thorpej 		printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1080   1.1       cgd 		/* command */
   1081  1.24  christos 		printf("    ioc: ");
   1082  1.30   thorpej 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_pad);
   1083  1.30   thorpej 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_hiaddr);
   1084  1.30   thorpej 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_addr);
   1085  1.30   thorpej 		printf("0x%x", *(u_short *)&rs->sc_ioc.c_nop2);
   1086  1.30   thorpej 		printf("0x%x", *(u_int *)&rs->sc_ioc.c_len);
   1087  1.30   thorpej 		printf("0x%x\n", *(u_short *)&rs->sc_ioc.c_cmd);
   1088   1.1       cgd 		return(1);
   1089   1.1       cgd 	}
   1090   1.1       cgd #endif
   1091  1.24  christos 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
   1092   1.1       cgd 	       (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
   1093   1.1       cgd 	       sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
   1094  1.24  christos 	printf("P1-P10: ");
   1095  1.30   thorpej 	printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1096  1.30   thorpej 	printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1097  1.30   thorpej 	printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1098   1.1       cgd 	return(1);
   1099  1.12   thorpej }
   1100  1.12   thorpej 
   1101  1.12   thorpej int
   1102  1.12   thorpej rdread(dev, uio, flags)
   1103  1.12   thorpej 	dev_t dev;
   1104  1.12   thorpej 	struct uio *uio;
   1105  1.12   thorpej 	int flags;
   1106  1.12   thorpej {
   1107  1.12   thorpej 
   1108  1.12   thorpej 	return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
   1109  1.12   thorpej }
   1110  1.12   thorpej 
   1111  1.12   thorpej int
   1112  1.12   thorpej rdwrite(dev, uio, flags)
   1113  1.12   thorpej 	dev_t dev;
   1114  1.12   thorpej 	struct uio *uio;
   1115  1.12   thorpej 	int flags;
   1116  1.12   thorpej {
   1117  1.12   thorpej 
   1118  1.12   thorpej 	return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
   1119   1.1       cgd }
   1120   1.1       cgd 
   1121   1.1       cgd int
   1122   1.1       cgd rdioctl(dev, cmd, data, flag, p)
   1123   1.1       cgd 	dev_t dev;
   1124  1.27   thorpej 	u_long cmd;
   1125   1.1       cgd 	caddr_t data;
   1126   1.1       cgd 	int flag;
   1127   1.1       cgd 	struct proc *p;
   1128   1.1       cgd {
   1129   1.8   mycroft 	int unit = rdunit(dev);
   1130  1.27   thorpej 	struct rd_softc *sc = rd_cd.cd_devs[unit];
   1131  1.28    scottr 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1132   1.8   mycroft 	int error, flags;
   1133   1.8   mycroft 
   1134   1.8   mycroft 	switch (cmd) {
   1135   1.8   mycroft 	case DIOCGDINFO:
   1136   1.8   mycroft 		*(struct disklabel *)data = *lp;
   1137   1.8   mycroft 		return (0);
   1138   1.8   mycroft 
   1139   1.8   mycroft 	case DIOCGPART:
   1140   1.8   mycroft 		((struct partinfo *)data)->disklab = lp;
   1141   1.8   mycroft 		((struct partinfo *)data)->part =
   1142   1.8   mycroft 			&lp->d_partitions[rdpart(dev)];
   1143   1.8   mycroft 		return (0);
   1144   1.8   mycroft 
   1145   1.8   mycroft 	case DIOCWLABEL:
   1146   1.8   mycroft 		if ((flag & FWRITE) == 0)
   1147   1.8   mycroft 			return (EBADF);
   1148   1.8   mycroft 		if (*(int *)data)
   1149   1.8   mycroft 			sc->sc_flags |= RDF_WLABEL;
   1150   1.8   mycroft 		else
   1151   1.8   mycroft 			sc->sc_flags &= ~RDF_WLABEL;
   1152   1.8   mycroft 		return (0);
   1153   1.8   mycroft 
   1154   1.8   mycroft 	case DIOCSDINFO:
   1155   1.8   mycroft 		if ((flag & FWRITE) == 0)
   1156   1.8   mycroft 			return (EBADF);
   1157   1.8   mycroft 		return (setdisklabel(lp, (struct disklabel *)data,
   1158   1.8   mycroft 				     (sc->sc_flags & RDF_WLABEL) ? 0
   1159  1.17   thorpej 				     : sc->sc_dkdev.dk_openmask,
   1160   1.8   mycroft 				     (struct cpu_disklabel *)0));
   1161   1.8   mycroft 
   1162   1.8   mycroft 	case DIOCWDINFO:
   1163   1.8   mycroft 		if ((flag & FWRITE) == 0)
   1164   1.8   mycroft 			return (EBADF);
   1165   1.8   mycroft 		error = setdisklabel(lp, (struct disklabel *)data,
   1166   1.8   mycroft 				     (sc->sc_flags & RDF_WLABEL) ? 0
   1167  1.17   thorpej 				     : sc->sc_dkdev.dk_openmask,
   1168   1.8   mycroft 				     (struct cpu_disklabel *)0);
   1169   1.8   mycroft 		if (error)
   1170   1.8   mycroft 			return (error);
   1171   1.8   mycroft 		flags = sc->sc_flags;
   1172   1.8   mycroft 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
   1173   1.8   mycroft 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
   1174   1.8   mycroft 				       (struct cpu_disklabel *)0);
   1175   1.8   mycroft 		sc->sc_flags = flags;
   1176   1.8   mycroft 		return (error);
   1177   1.8   mycroft 	}
   1178   1.1       cgd 	return(EINVAL);
   1179   1.1       cgd }
   1180   1.1       cgd 
   1181   1.1       cgd int
   1182   1.1       cgd rdsize(dev)
   1183   1.1       cgd 	dev_t dev;
   1184   1.1       cgd {
   1185  1.28    scottr 	int unit = rdunit(dev);
   1186  1.27   thorpej 	struct rd_softc *rs;
   1187   1.8   mycroft 	int psize, didopen = 0;
   1188   1.1       cgd 
   1189  1.27   thorpej 	if (unit >= rd_cd.cd_ndevs ||
   1190  1.27   thorpej 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
   1191  1.27   thorpej 	    (rs->sc_flags & RDF_ALIVE) == 0)
   1192  1.27   thorpej 		return (-1);
   1193   1.8   mycroft 
   1194   1.8   mycroft 	/*
   1195   1.8   mycroft 	 * We get called very early on (via swapconf)
   1196   1.8   mycroft 	 * without the device being open so we may need
   1197   1.8   mycroft 	 * to handle it here.
   1198   1.8   mycroft 	 */
   1199  1.17   thorpej 	if (rs->sc_dkdev.dk_openmask == 0) {
   1200   1.8   mycroft 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
   1201   1.8   mycroft 			return(-1);
   1202   1.8   mycroft 		didopen = 1;
   1203   1.8   mycroft 	}
   1204  1.32   thorpej 	psize = rs->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
   1205  1.32   thorpej 	    (rs->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
   1206   1.8   mycroft 	if (didopen)
   1207   1.8   mycroft 		(void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
   1208   1.8   mycroft 	return (psize);
   1209   1.1       cgd }
   1210   1.1       cgd 
   1211   1.1       cgd #ifdef DEBUG
   1212  1.29    scottr void
   1213   1.1       cgd rdprinterr(str, err, tab)
   1214   1.1       cgd 	char *str;
   1215   1.1       cgd 	short err;
   1216  1.29    scottr 	char **tab;
   1217   1.1       cgd {
   1218  1.28    scottr 	int i;
   1219   1.1       cgd 	int printed;
   1220   1.1       cgd 
   1221   1.1       cgd 	if (err == 0)
   1222   1.1       cgd 		return;
   1223  1.29    scottr 	printf("    %s error %d field:", str, err);
   1224   1.1       cgd 	printed = 0;
   1225   1.1       cgd 	for (i = 0; i < 16; i++)
   1226   1.1       cgd 		if (err & (0x8000 >> i))
   1227  1.24  christos 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
   1228  1.24  christos 	printf("\n");
   1229   1.1       cgd }
   1230   1.1       cgd #endif
   1231   1.1       cgd 
   1232  1.22   thorpej static int rddoingadump;	/* simple mutex */
   1233  1.22   thorpej 
   1234   1.1       cgd /*
   1235   1.1       cgd  * Non-interrupt driven, non-dma dump routine.
   1236   1.1       cgd  */
   1237   1.1       cgd int
   1238  1.22   thorpej rddump(dev, blkno, va, size)
   1239   1.1       cgd 	dev_t dev;
   1240  1.22   thorpej 	daddr_t blkno;
   1241  1.22   thorpej 	caddr_t va;
   1242  1.22   thorpej 	size_t size;
   1243  1.22   thorpej {
   1244  1.22   thorpej 	int sectorsize;		/* size of a disk sector */
   1245  1.22   thorpej 	int nsects;		/* number of sectors in partition */
   1246  1.22   thorpej 	int sectoff;		/* sector offset of partition */
   1247  1.22   thorpej 	int totwrt;		/* total number of sectors left to write */
   1248  1.22   thorpej 	int nwrt;		/* current number of sectors to write */
   1249  1.22   thorpej 	int unit, part;
   1250  1.27   thorpej 	int ctlr, slave;
   1251  1.22   thorpej 	struct rd_softc *rs;
   1252  1.22   thorpej 	struct disklabel *lp;
   1253   1.1       cgd 	char stat;
   1254   1.1       cgd 
   1255  1.22   thorpej 	/* Check for recursive dump; if so, punt. */
   1256  1.22   thorpej 	if (rddoingadump)
   1257  1.22   thorpej 		return (EFAULT);
   1258  1.22   thorpej 	rddoingadump = 1;
   1259  1.22   thorpej 
   1260  1.22   thorpej 	/* Decompose unit and partition. */
   1261  1.22   thorpej 	unit = rdunit(dev);
   1262  1.22   thorpej 	part = rdpart(dev);
   1263  1.22   thorpej 
   1264  1.22   thorpej 	/* Make sure dump device is ok. */
   1265  1.27   thorpej 	if (unit >= rd_cd.cd_ndevs ||
   1266  1.27   thorpej 	    (rs = rd_cd.cd_devs[unit]) == NULL ||
   1267  1.27   thorpej 	    (rs->sc_flags & RDF_ALIVE) == 0)
   1268  1.22   thorpej 		return (ENXIO);
   1269  1.27   thorpej 
   1270  1.27   thorpej 	ctlr = rs->sc_dev.dv_parent->dv_unit;
   1271  1.27   thorpej 	slave = rs->sc_slave;
   1272  1.22   thorpej 
   1273  1.22   thorpej 	/*
   1274  1.22   thorpej 	 * Convert to disk sectors.  Request must be a multiple of size.
   1275  1.22   thorpej 	 */
   1276  1.22   thorpej 	lp = rs->sc_dkdev.dk_label;
   1277  1.22   thorpej 	sectorsize = lp->d_secsize;
   1278  1.22   thorpej 	if ((size % sectorsize) != 0)
   1279  1.22   thorpej 		return (EFAULT);
   1280  1.22   thorpej 	totwrt = size / sectorsize;
   1281  1.22   thorpej 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1282  1.22   thorpej 
   1283  1.22   thorpej 	nsects = lp->d_partitions[part].p_size;
   1284  1.22   thorpej 	sectoff = lp->d_partitions[part].p_offset;
   1285  1.22   thorpej 
   1286  1.22   thorpej 	/* Check transfer bounds against partition size. */
   1287  1.22   thorpej 	if ((blkno < 0) || (blkno + totwrt) > nsects)
   1288   1.8   mycroft 		return (EINVAL);
   1289  1.22   thorpej 
   1290  1.22   thorpej 	/* Offset block number to start of partition. */
   1291  1.22   thorpej 	blkno += sectoff;
   1292  1.22   thorpej 
   1293  1.22   thorpej 	while (totwrt > 0) {
   1294  1.22   thorpej 		nwrt = totwrt;		/* XXX */
   1295  1.22   thorpej #ifndef RD_DUMP_NOT_TRUSTED
   1296  1.22   thorpej 		/*
   1297  1.22   thorpej 		 * Fill out and send HPIB command.
   1298  1.22   thorpej 		 */
   1299   1.1       cgd 		rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
   1300   1.1       cgd 		rs->sc_ioc.c_volume = C_SVOL(0);
   1301   1.1       cgd 		rs->sc_ioc.c_saddr = C_SADDR;
   1302   1.1       cgd 		rs->sc_ioc.c_hiaddr = 0;
   1303  1.22   thorpej 		rs->sc_ioc.c_addr = RDBTOS(blkno);
   1304   1.1       cgd 		rs->sc_ioc.c_nop2 = C_NOP;
   1305   1.1       cgd 		rs->sc_ioc.c_slen = C_SLEN;
   1306  1.22   thorpej 		rs->sc_ioc.c_len = nwrt * sectorsize;
   1307   1.1       cgd 		rs->sc_ioc.c_cmd = C_WRITE;
   1308  1.27   thorpej 		hpibsend(ctlr, slave, C_CMD, &rs->sc_ioc.c_unit,
   1309  1.27   thorpej 		    sizeof(rs->sc_ioc)-2);
   1310  1.27   thorpej 		if (hpibswait(ctlr, slave))
   1311   1.1       cgd 			return (EIO);
   1312  1.22   thorpej 
   1313  1.22   thorpej 		/*
   1314  1.22   thorpej 		 * Send the data.
   1315  1.22   thorpej 		 */
   1316  1.27   thorpej 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
   1317  1.27   thorpej 		(void) hpibswait(ctlr, slave);
   1318  1.27   thorpej 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
   1319   1.8   mycroft 		if (stat)
   1320   1.1       cgd 			return (EIO);
   1321  1.22   thorpej #else /* RD_DUMP_NOT_TRUSTED */
   1322  1.22   thorpej 		/* Let's just talk about this first... */
   1323  1.27   thorpej 		printf("%s: dump addr %p, blk %d\n", sc->sc_dev.dv_xname,
   1324  1.22   thorpej 		    va, blkno);
   1325  1.22   thorpej 		delay(500 * 1000);	/* half a second */
   1326  1.22   thorpej #endif /* RD_DUMP_NOT_TRUSTED */
   1327  1.22   thorpej 
   1328  1.22   thorpej 		/* update block count */
   1329  1.22   thorpej 		totwrt -= nwrt;
   1330  1.22   thorpej 		blkno += nwrt;
   1331  1.22   thorpej 		va += sectorsize * nwrt;
   1332   1.1       cgd 	}
   1333  1.22   thorpej 	rddoingadump = 0;
   1334   1.1       cgd 	return (0);
   1335   1.1       cgd }
   1336