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