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