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