Home | History | Annotate | Line # | Download | only in dev
rd.c revision 1.107
      1  1.107   tsutsui /*	$NetBSD: rd.c,v 1.107 2021/07/05 14:51:23 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.107   tsutsui __KERNEL_RCSID(0, "$NetBSD: rd.c,v 1.107 2021/07/05 14:51:23 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.8   mycroft 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.14   thorpej 	{ RD7946AID,	0,	"7945A",	NRD7945ABPT,
    190   1.14   thorpej 	  NRD7945ATRK,	968,	 108416 },
    191   1.14   thorpej 
    192   1.14   thorpej 	{ RD9134DID,	1,	"9134D",	NRD9134DBPT,
    193   1.14   thorpej 	  NRD9134DTRK,	303,	  29088 },
    194   1.14   thorpej 
    195   1.14   thorpej 	{ RD9134LID,	1,	"9122S",	NRD9122SBPT,
    196   1.14   thorpej 	  NRD9122STRK,	77,	   1232 },
    197   1.14   thorpej 
    198   1.14   thorpej 	{ RD7912PID,	0,	"7912P",	NRD7912PBPT,
    199   1.14   thorpej 	  NRD7912PTRK,	572,	 128128 },
    200   1.14   thorpej 
    201   1.14   thorpej 	{ RD7914PID,	0,	"7914P",	NRD7914PBPT,
    202   1.14   thorpej 	  NRD7914PTRK,	1152,	 258048 },
    203   1.14   thorpej 
    204   1.14   thorpej 	{ RD7958AID,	0,	"7958A",	NRD7958ABPT,
    205   1.14   thorpej 	  NRD7958ATRK,	1013,	 255276 },
    206   1.14   thorpej 
    207   1.14   thorpej 	{ RD7957AID,	0,	"7957A",	NRD7957ABPT,
    208   1.14   thorpej 	  NRD7957ATRK,	1036,	 159544 },
    209   1.14   thorpej 
    210   1.14   thorpej 	{ RD7933HID,	0,	"7933H",	NRD7933HBPT,
    211   1.14   thorpej 	  NRD7933HTRK,	1321,	 789958 },
    212   1.14   thorpej 
    213   1.14   thorpej 	{ RD9134LID,	1,	"9134L",	NRD9134LBPT,
    214   1.14   thorpej 	  NRD9134LTRK,	973,	  77840 },
    215   1.14   thorpej 
    216   1.14   thorpej 	{ RD7936HID,	0,	"7936H",	NRD7936HBPT,
    217   1.14   thorpej 	  NRD7936HTRK,	698,	 600978 },
    218   1.14   thorpej 
    219   1.14   thorpej 	{ RD7937HID,	0,	"7937H",	NRD7937HBPT,
    220   1.14   thorpej 	  NRD7937HTRK,	698,	1116102 },
    221   1.14   thorpej 
    222   1.14   thorpej 	{ RD7914CTID,	0,	"7914CT",	NRD7914PBPT,
    223   1.14   thorpej 	  NRD7914PTRK,	1152,	 258048 },
    224   1.14   thorpej 
    225   1.14   thorpej 	{ RD7946AID,	0,	"7946A",	NRD7945ABPT,
    226   1.14   thorpej 	  NRD7945ATRK,	968,	 108416 },
    227   1.14   thorpej 
    228   1.14   thorpej 	{ RD9134LID,	1,	"9122D",	NRD9122SBPT,
    229   1.14   thorpej 	  NRD9122STRK,	77,	   1232 },
    230   1.14   thorpej 
    231   1.14   thorpej 	{ RD7957BID,	0,	"7957B",	NRD7957BBPT,
    232   1.14   thorpej 	  NRD7957BTRK,	1269,	 159894 },
    233   1.14   thorpej 
    234   1.14   thorpej 	{ RD7958BID,	0,	"7958B",	NRD7958BBPT,
    235   1.14   thorpej 	  NRD7958BTRK,	786,	 297108 },
    236   1.14   thorpej 
    237   1.14   thorpej 	{ RD7959BID,	0,	"7959B",	NRD7959BBPT,
    238   1.14   thorpej 	  NRD7959BTRK,	1572,	 594216 },
    239   1.14   thorpej 
    240   1.14   thorpej 	{ RD2200AID,	0,	"2200A",	NRD2200ABPT,
    241   1.14   thorpej 	  NRD2200ATRK,	1449,	 654948 },
    242   1.14   thorpej 
    243   1.14   thorpej 	{ RD2203AID,	0,	"2203A",	NRD2203ABPT,
    244   1.14   thorpej 	  NRD2203ATRK,	1449,	1309896 }
    245    1.1       cgd };
    246   1.85   tsutsui static const int numrdidentinfo = __arraycount(rdidentinfo);
    247    1.1       cgd 
    248   1.93   tsutsui static int	rdident(device_t, struct rd_softc *,
    249   1.63   thorpej 		    struct hpibbus_attach_args *);
    250   1.63   thorpej static void	rdreset(struct rd_softc *);
    251   1.63   thorpej static void	rdustart(struct rd_softc *);
    252   1.63   thorpej static int	rdgetinfo(dev_t);
    253   1.63   thorpej static void	rdrestart(void *);
    254   1.63   thorpej static struct buf *rdfinish(struct rd_softc *, struct buf *);
    255   1.63   thorpej 
    256   1.63   thorpej static void	rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
    257   1.63   thorpej static void	rdrestart(void *);
    258   1.63   thorpej static void	rdustart(struct rd_softc *);
    259   1.63   thorpej static struct buf *rdfinish(struct rd_softc *, struct buf *);
    260   1.63   thorpej static void	rdstart(void *);
    261   1.63   thorpej static void	rdgo(void *);
    262   1.63   thorpej static void	rdintr(void *);
    263   1.63   thorpej static int	rdstatus(struct rd_softc *);
    264   1.63   thorpej static int	rderror(int);
    265   1.29    scottr #ifdef DEBUG
    266   1.66       chs static void	rdprinterr(const char *, short, const char **);
    267   1.29    scottr #endif
    268   1.29    scottr 
    269   1.85   tsutsui static int	rdmatch(device_t, cfdata_t, void *);
    270   1.85   tsutsui static void	rdattach(device_t, device_t, void *);
    271   1.29    scottr 
    272   1.85   tsutsui CFATTACH_DECL_NEW(rd, sizeof(struct rd_softc),
    273   1.53   thorpej     rdmatch, rdattach, NULL, NULL);
    274   1.29    scottr 
    275   1.63   thorpej static dev_type_open(rdopen);
    276   1.63   thorpej static dev_type_close(rdclose);
    277   1.63   thorpej static dev_type_read(rdread);
    278   1.63   thorpej static dev_type_write(rdwrite);
    279   1.63   thorpej static dev_type_ioctl(rdioctl);
    280   1.63   thorpej static dev_type_strategy(rdstrategy);
    281   1.63   thorpej static dev_type_dump(rddump);
    282   1.63   thorpej static dev_type_size(rdsize);
    283   1.51   gehenna 
    284   1.51   gehenna const struct bdevsw rd_bdevsw = {
    285   1.94  dholland 	.d_open = rdopen,
    286   1.94  dholland 	.d_close = rdclose,
    287   1.94  dholland 	.d_strategy = rdstrategy,
    288   1.94  dholland 	.d_ioctl = rdioctl,
    289   1.94  dholland 	.d_dump = rddump,
    290   1.94  dholland 	.d_psize = rdsize,
    291   1.96  dholland 	.d_discard = nodiscard,
    292   1.94  dholland 	.d_flag = D_DISK
    293   1.51   gehenna };
    294   1.51   gehenna 
    295   1.51   gehenna const struct cdevsw rd_cdevsw = {
    296   1.94  dholland 	.d_open = rdopen,
    297   1.94  dholland 	.d_close = rdclose,
    298   1.94  dholland 	.d_read = rdread,
    299   1.94  dholland 	.d_write = rdwrite,
    300   1.94  dholland 	.d_ioctl = rdioctl,
    301   1.94  dholland 	.d_stop = nostop,
    302   1.94  dholland 	.d_tty = notty,
    303   1.94  dholland 	.d_poll = nopoll,
    304   1.94  dholland 	.d_mmap = nommap,
    305   1.94  dholland 	.d_kqfilter = nokqfilter,
    306   1.97  dholland 	.d_discard = nodiscard,
    307   1.94  dholland 	.d_flag = D_DISK
    308   1.51   gehenna };
    309   1.29    scottr 
    310   1.63   thorpej static int
    311   1.85   tsutsui rdmatch(device_t parent, cfdata_t cf, void *aux)
    312    1.1       cgd {
    313   1.27   thorpej 	struct hpibbus_attach_args *ha = aux;
    314    1.1       cgd 
    315  1.105   tsutsui 	return rdident(parent, NULL, ha);
    316   1.27   thorpej }
    317   1.17   thorpej 
    318   1.63   thorpej static void
    319   1.85   tsutsui rdattach(device_t parent, device_t self, void *aux)
    320   1.27   thorpej {
    321   1.85   tsutsui 	struct rd_softc *sc = device_private(self);
    322   1.27   thorpej 	struct hpibbus_attach_args *ha = aux;
    323  1.106   tsutsui 	int id;
    324  1.106   tsutsui 	char pbuf[9];
    325   1.27   thorpej 
    326   1.85   tsutsui 	sc->sc_dev = self;
    327   1.68      yamt 	bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
    328   1.37   thorpej 
    329   1.27   thorpej 	if (rdident(parent, sc, ha) == 0) {
    330   1.85   tsutsui 		aprint_error(": didn't respond to describe command!\n");
    331   1.27   thorpej 		return;
    332   1.27   thorpej 	}
    333   1.17   thorpej 
    334   1.17   thorpej 	/*
    335  1.106   tsutsui 	 * XXX We use DEV_BSIZE instead of the sector size value pulled
    336  1.106   tsutsui 	 * XXX off the driver because all of this code assumes 512 byte
    337  1.106   tsutsui 	 * XXX blocks.  ICK!
    338  1.106   tsutsui 	 */
    339  1.106   tsutsui 	id = sc->sc_type;
    340  1.106   tsutsui 	aprint_normal(": %s\n", rdidentinfo[id].ri_desc);
    341  1.106   tsutsui 	format_bytes(pbuf, sizeof(pbuf),
    342  1.106   tsutsui 	    rdidentinfo[id].ri_nblocks * DEV_BSIZE);
    343  1.106   tsutsui 	aprint_normal_dev(sc->sc_dev, "%s, %d cyl, %d head, %d sec,"
    344  1.106   tsutsui 	    " %d bytes/block x %u blocks\n",
    345  1.106   tsutsui 	    pbuf, rdidentinfo[id].ri_ncyl, rdidentinfo[id].ri_ntpc,
    346  1.106   tsutsui 	    rdidentinfo[id].ri_nbpt,
    347  1.106   tsutsui 	    DEV_BSIZE, rdidentinfo[id].ri_nblocks);
    348  1.106   tsutsui 
    349  1.106   tsutsui 	/*
    350   1.17   thorpej 	 * Initialize and attach the disk structure.
    351   1.17   thorpej 	 */
    352   1.46  gmcgarry 	memset(&sc->sc_dkdev, 0, sizeof(sc->sc_dkdev));
    353   1.85   tsutsui 	disk_init(&sc->sc_dkdev, device_xname(sc->sc_dev), NULL);
    354   1.27   thorpej 	disk_attach(&sc->sc_dkdev);
    355   1.27   thorpej 
    356   1.27   thorpej 	sc->sc_slave = ha->ha_slave;
    357   1.27   thorpej 	sc->sc_punit = ha->ha_punit;
    358   1.27   thorpej 
    359   1.81        he 	callout_init(&sc->sc_restart_ch, 0);
    360   1.41   thorpej 
    361   1.27   thorpej 	/* Initialize the hpib job queue entry */
    362   1.27   thorpej 	sc->sc_hq.hq_softc = sc;
    363   1.27   thorpej 	sc->sc_hq.hq_slave = sc->sc_slave;
    364   1.27   thorpej 	sc->sc_hq.hq_start = rdstart;
    365   1.27   thorpej 	sc->sc_hq.hq_go = rdgo;
    366   1.27   thorpej 	sc->sc_hq.hq_intr = rdintr;
    367   1.15   thorpej 
    368   1.27   thorpej 	sc->sc_flags = RDF_ALIVE;
    369    1.1       cgd #ifdef DEBUG
    370    1.1       cgd 	/* always report errors */
    371    1.1       cgd 	if (rddebug & RDB_ERROR)
    372    1.1       cgd 		rderrthresh = 0;
    373    1.1       cgd #endif
    374   1.44        he 	/*
    375   1.44        he 	 * attach the device into the random source list
    376   1.44        he 	 */
    377   1.85   tsutsui 	rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
    378   1.98       tls 	    RND_TYPE_DISK, RND_FLAG_DEFAULT);
    379    1.1       cgd }
    380    1.1       cgd 
    381   1.63   thorpej static int
    382   1.85   tsutsui rdident(device_t parent, struct rd_softc *sc, struct hpibbus_attach_args *ha)
    383    1.1       cgd {
    384  1.107   tsutsui 	struct cs80_describe desc;
    385    1.1       cgd 	u_char stat, cmd[3];
    386  1.106   tsutsui 	char name[7];
    387   1.27   thorpej 	int i, id, n, ctlr, slave;
    388   1.27   thorpej 
    389   1.72   thorpej 	ctlr = device_unit(parent);
    390   1.27   thorpej 	slave = ha->ha_slave;
    391    1.1       cgd 
    392   1.27   thorpej 	/* Verify that we have a CS80 device. */
    393   1.27   thorpej 	if ((ha->ha_id & 0x200) == 0)
    394   1.78   tsutsui 		return 0;
    395   1.27   thorpej 
    396   1.27   thorpej 	/* Is it one of the disks we support? */
    397   1.27   thorpej 	for (id = 0; id < numrdidentinfo; id++)
    398  1.105   tsutsui 		if (ha->ha_id == rdidentinfo[id].ri_hwid &&
    399  1.105   tsutsui 		    ha->ha_punit <= rdidentinfo[id].ri_maxunum)
    400   1.27   thorpej 			break;
    401  1.105   tsutsui 	if (id == numrdidentinfo)
    402   1.78   tsutsui 		return 0;
    403    1.1       cgd 
    404    1.1       cgd 	/*
    405   1.27   thorpej 	 * If we're just probing for the device, that's all the
    406   1.27   thorpej 	 * work we need to do.
    407    1.1       cgd 	 */
    408   1.27   thorpej 	if (sc == NULL)
    409   1.78   tsutsui 		return 1;
    410    1.1       cgd 
    411    1.1       cgd 	/*
    412   1.27   thorpej 	 * Reset device and collect description
    413    1.1       cgd 	 */
    414   1.27   thorpej 	rdreset(sc);
    415   1.27   thorpej 	cmd[0] = C_SUNIT(ha->ha_punit);
    416    1.1       cgd 	cmd[1] = C_SVOL(0);
    417    1.1       cgd 	cmd[2] = C_DESC;
    418    1.1       cgd 	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
    419  1.107   tsutsui 	hpibrecv(ctlr, slave, C_EXEC, &desc, sizeof(desc));
    420    1.1       cgd 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    421   1.62   tsutsui 	memset(name, 0, sizeof(name));
    422   1.27   thorpej 	if (stat == 0) {
    423  1.107   tsutsui 		n = desc.d_name;
    424    1.1       cgd 		for (i = 5; i >= 0; i--) {
    425    1.1       cgd 			name[i] = (n & 0xf) + '0';
    426    1.1       cgd 			n >>= 4;
    427    1.1       cgd 		}
    428    1.1       cgd 	}
    429   1.27   thorpej 
    430    1.1       cgd #ifdef DEBUG
    431    1.1       cgd 	if (rddebug & RDB_IDENT) {
    432  1.104   tsutsui 		aprint_normal("\n");
    433  1.104   tsutsui 		aprint_normal_dev(sc->sc_dev, "id: 0x%04x, name: %x ('%s')\n",
    434  1.107   tsutsui 		    ha->ha_id, desc.d_name, name);
    435  1.104   tsutsui 		aprint_normal("  iuw %x, maxxfr %d, ctype %d\n",
    436  1.107   tsutsui 		    desc.d_iuw, desc.d_cmaxxfr, desc.d_ctype);
    437  1.104   tsutsui 		aprint_normal("  utype %d, bps %d, blkbuf %d, burst %d,"
    438   1.85   tsutsui 		    " blktime %d\n",
    439  1.107   tsutsui 		    desc.d_utype, desc.d_sectsize,
    440  1.107   tsutsui 		    desc.d_blkbuf, desc.d_burstsize, desc.d_blocktime);
    441  1.104   tsutsui 		aprint_normal("  avxfr %d, ort %d, atp %d, maxint %d, fv %x"
    442   1.85   tsutsui 		    ", rv %x\n",
    443  1.107   tsutsui 		    desc.d_uavexfr, desc.d_retry, desc.d_access,
    444  1.107   tsutsui 		    desc.d_maxint, desc.d_fvbyte, desc.d_rvbyte);
    445  1.104   tsutsui 		aprint_normal("  maxcyl/head/sect %d/%d/%d, maxvsect %d,"
    446   1.85   tsutsui 		    " inter %d\n",
    447  1.107   tsutsui 		    desc.d_maxcyl, desc.d_maxhead, desc.d_maxsect,
    448  1.107   tsutsui 		    desc.d_maxvsectl, desc.d_interleave);
    449   1.85   tsutsui 		aprint_normal("%s", device_xname(sc->sc_dev));
    450    1.1       cgd 	}
    451    1.1       cgd #endif
    452   1.27   thorpej 
    453    1.1       cgd 	/*
    454    1.1       cgd 	 * Take care of a couple of anomolies:
    455    1.1       cgd 	 * 1. 7945A and 7946A both return same HW id
    456    1.1       cgd 	 * 2. 9122S and 9134D both return same HW id
    457    1.1       cgd 	 * 3. 9122D and 9134L both return same HW id
    458    1.1       cgd 	 */
    459   1.27   thorpej 	switch (ha->ha_id) {
    460    1.1       cgd 	case RD7946AID:
    461   1.46  gmcgarry 		if (memcmp(name, "079450", 6) == 0)
    462    1.1       cgd 			id = RD7945A;
    463    1.1       cgd 		else
    464    1.1       cgd 			id = RD7946A;
    465    1.1       cgd 		break;
    466    1.1       cgd 
    467    1.1       cgd 	case RD9134LID:
    468   1.46  gmcgarry 		if (memcmp(name, "091340", 6) == 0)
    469    1.1       cgd 			id = RD9134L;
    470    1.1       cgd 		else
    471    1.1       cgd 			id = RD9122D;
    472    1.1       cgd 		break;
    473    1.1       cgd 
    474    1.1       cgd 	case RD9134DID:
    475   1.46  gmcgarry 		if (memcmp(name, "091220", 6) == 0)
    476    1.1       cgd 			id = RD9122S;
    477    1.1       cgd 		else
    478    1.1       cgd 			id = RD9134D;
    479    1.1       cgd 		break;
    480    1.1       cgd 	}
    481   1.27   thorpej 
    482   1.27   thorpej 	sc->sc_type = id;
    483   1.27   thorpej 
    484   1.78   tsutsui 	return 1;
    485    1.1       cgd }
    486    1.1       cgd 
    487   1.63   thorpej static void
    488   1.85   tsutsui rdreset(struct rd_softc *sc)
    489    1.1       cgd {
    490   1.85   tsutsui 	int ctlr = device_unit(device_parent(sc->sc_dev));
    491   1.85   tsutsui 	int slave = sc->sc_slave;
    492    1.1       cgd 	u_char stat;
    493    1.1       cgd 
    494   1.85   tsutsui 	sc->sc_clear.c_unit = C_SUNIT(sc->sc_punit);
    495   1.85   tsutsui 	sc->sc_clear.c_cmd = C_CLEAR;
    496   1.85   tsutsui 	hpibsend(ctlr, slave, C_TCMD, &sc->sc_clear, sizeof(sc->sc_clear));
    497   1.27   thorpej 	hpibswait(ctlr, slave);
    498   1.27   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    499   1.27   thorpej 
    500   1.85   tsutsui 	sc->sc_src.c_unit = C_SUNIT(RDCTLR);
    501   1.85   tsutsui 	sc->sc_src.c_nop = C_NOP;
    502   1.85   tsutsui 	sc->sc_src.c_cmd = C_SREL;
    503   1.85   tsutsui 	sc->sc_src.c_param = C_REL;
    504   1.85   tsutsui 	hpibsend(ctlr, slave, C_CMD, &sc->sc_src, sizeof(sc->sc_src));
    505   1.27   thorpej 	hpibswait(ctlr, slave);
    506   1.27   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    507   1.27   thorpej 
    508   1.85   tsutsui 	sc->sc_ssmc.c_unit = C_SUNIT(sc->sc_punit);
    509   1.85   tsutsui 	sc->sc_ssmc.c_cmd = C_SSM;
    510   1.85   tsutsui 	sc->sc_ssmc.c_refm = REF_MASK;
    511   1.85   tsutsui 	sc->sc_ssmc.c_fefm = FEF_MASK;
    512   1.85   tsutsui 	sc->sc_ssmc.c_aefm = AEF_MASK;
    513   1.85   tsutsui 	sc->sc_ssmc.c_iefm = IEF_MASK;
    514   1.85   tsutsui 	hpibsend(ctlr, slave, C_CMD, &sc->sc_ssmc, sizeof(sc->sc_ssmc));
    515   1.27   thorpej 	hpibswait(ctlr, slave);
    516   1.27   thorpej 	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
    517    1.1       cgd #ifdef DEBUG
    518   1.85   tsutsui 	sc->sc_stats.rdresets++;
    519    1.1       cgd #endif
    520    1.1       cgd }
    521    1.1       cgd 
    522    1.8   mycroft /*
    523   1.71       wiz  * Read or construct a disklabel
    524    1.8   mycroft  */
    525   1.63   thorpej static int
    526   1.63   thorpej rdgetinfo(dev_t dev)
    527    1.8   mycroft {
    528   1.87    cegger 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
    529   1.85   tsutsui 	struct disklabel *lp = sc->sc_dkdev.dk_label;
    530   1.28    scottr 	struct partition *pi;
    531   1.57       dsl 	const char *msg;
    532    1.8   mycroft 
    533    1.8   mycroft 	/*
    534    1.8   mycroft 	 * Set some default values to use while reading the label
    535    1.8   mycroft 	 * or to use if there isn't a label.
    536    1.8   mycroft 	 */
    537   1.79  christos 	memset((void *)lp, 0, sizeof *lp);
    538   1.85   tsutsui 	rdgetdefaultlabel(sc, lp);
    539    1.8   mycroft 
    540    1.8   mycroft 	/*
    541    1.8   mycroft 	 * Now try to read the disklabel
    542    1.8   mycroft 	 */
    543   1.16   thorpej 	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
    544    1.8   mycroft 	if (msg == NULL)
    545   1.78   tsutsui 		return 0;
    546    1.8   mycroft 
    547    1.8   mycroft 	pi = lp->d_partitions;
    548   1.85   tsutsui 	printf("%s: WARNING: %s\n", device_xname(sc->sc_dev), msg);
    549   1.64   thorpej 
    550   1.85   tsutsui 	pi[2].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
    551    1.9   mycroft 	/* XXX reset other info since readdisklabel screws with it */
    552    1.9   mycroft 	lp->d_npartitions = 3;
    553    1.9   mycroft 	pi[0].p_size = 0;
    554   1.64   thorpej 
    555   1.78   tsutsui 	return 0;
    556    1.8   mycroft }
    557    1.8   mycroft 
    558   1.63   thorpej static int
    559   1.69  christos rdopen(dev_t dev, int flags, int mode, struct lwp *l)
    560    1.1       cgd {
    561   1.85   tsutsui 	struct rd_softc *sc;
    562   1.26   thorpej 	int error, mask, part;
    563    1.1       cgd 
    564   1.87    cegger 	sc = device_lookup_private(&rd_cd, rdunit(dev));
    565   1.87    cegger 	if (sc == NULL)
    566   1.87    cegger 		return ENXIO;
    567   1.87    cegger 
    568   1.88        he 	if ((sc->sc_flags & RDF_ALIVE) == 0)
    569   1.78   tsutsui 		return ENXIO;
    570    1.8   mycroft 
    571    1.8   mycroft 	/*
    572    1.8   mycroft 	 * Wait for any pending opens/closes to complete
    573    1.8   mycroft 	 */
    574   1.85   tsutsui 	while (sc->sc_flags & (RDF_OPENING|RDF_CLOSING))
    575   1.85   tsutsui 		(void) tsleep(sc, PRIBIO, "rdopen", 0);
    576    1.8   mycroft 
    577    1.8   mycroft 	/*
    578    1.8   mycroft 	 * On first open, get label and partition info.
    579    1.8   mycroft 	 * We may block reading the label, so be careful
    580    1.8   mycroft 	 * to stop any other opens.
    581    1.8   mycroft 	 */
    582   1.85   tsutsui 	if (sc->sc_dkdev.dk_openmask == 0) {
    583   1.85   tsutsui 		sc->sc_flags |= RDF_OPENING;
    584    1.8   mycroft 		error = rdgetinfo(dev);
    585   1.85   tsutsui 		sc->sc_flags &= ~RDF_OPENING;
    586   1.85   tsutsui 		wakeup((void *)sc);
    587    1.8   mycroft 		if (error)
    588   1.78   tsutsui 			return error;
    589    1.8   mycroft 	}
    590    1.8   mycroft 
    591   1.26   thorpej 	part = rdpart(dev);
    592   1.26   thorpej 	mask = 1 << part;
    593   1.26   thorpej 
    594   1.26   thorpej 	/* Check that the partition exists. */
    595   1.26   thorpej 	if (part != RAW_PART &&
    596   1.85   tsutsui 	    (part > sc->sc_dkdev.dk_label->d_npartitions ||
    597   1.85   tsutsui 	     sc->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
    598   1.78   tsutsui 		return ENXIO;
    599   1.26   thorpej 
    600   1.26   thorpej 	/* Ensure only one open at a time. */
    601   1.26   thorpej 	switch (mode) {
    602   1.26   thorpej 	case S_IFCHR:
    603   1.85   tsutsui 		sc->sc_dkdev.dk_copenmask |= mask;
    604   1.26   thorpej 		break;
    605   1.26   thorpej 	case S_IFBLK:
    606   1.85   tsutsui 		sc->sc_dkdev.dk_bopenmask |= mask;
    607   1.26   thorpej 		break;
    608   1.26   thorpej 	}
    609   1.85   tsutsui 	sc->sc_dkdev.dk_openmask =
    610   1.85   tsutsui 	    sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
    611   1.26   thorpej 
    612   1.78   tsutsui 	return 0;
    613    1.8   mycroft }
    614    1.8   mycroft 
    615   1.63   thorpej static int
    616   1.69  christos rdclose(dev_t dev, int flag, int mode, struct lwp *l)
    617    1.8   mycroft {
    618   1.87    cegger 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
    619   1.85   tsutsui 	struct disk *dk = &sc->sc_dkdev;
    620    1.8   mycroft 	int mask, s;
    621    1.8   mycroft 
    622    1.8   mycroft 	mask = 1 << rdpart(dev);
    623    1.8   mycroft 	if (mode == S_IFCHR)
    624   1.17   thorpej 		dk->dk_copenmask &= ~mask;
    625    1.8   mycroft 	else
    626   1.17   thorpej 		dk->dk_bopenmask &= ~mask;
    627   1.17   thorpej 	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
    628    1.8   mycroft 	/*
    629    1.8   mycroft 	 * On last close, we wait for all activity to cease since
    630  1.102   msaitoh 	 * the label/partition info will become invalid.  Since we
    631    1.8   mycroft 	 * might sleep, we must block any opens while we are here.
    632    1.8   mycroft 	 * Note we don't have to about other closes since we know
    633    1.8   mycroft 	 * we are the last one.
    634    1.8   mycroft 	 */
    635   1.17   thorpej 	if (dk->dk_openmask == 0) {
    636   1.85   tsutsui 		sc->sc_flags |= RDF_CLOSING;
    637    1.8   mycroft 		s = splbio();
    638   1.85   tsutsui 		while (sc->sc_active) {
    639   1.85   tsutsui 			sc->sc_flags |= RDF_WANTED;
    640   1.85   tsutsui 			(void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
    641    1.8   mycroft 		}
    642    1.8   mycroft 		splx(s);
    643   1.85   tsutsui 		sc->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
    644   1.85   tsutsui 		wakeup((void *)sc);
    645    1.8   mycroft 	}
    646   1.78   tsutsui 	return 0;
    647    1.1       cgd }
    648    1.1       cgd 
    649   1.63   thorpej static void
    650   1.63   thorpej rdstrategy(struct buf *bp)
    651    1.1       cgd {
    652   1.87    cegger 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(bp->b_dev));
    653   1.28    scottr 	struct partition *pinfo;
    654   1.28    scottr 	daddr_t bn;
    655   1.28    scottr 	int sz, s;
    656   1.26   thorpej 	int offset;
    657    1.1       cgd 
    658    1.1       cgd #ifdef DEBUG
    659    1.1       cgd 	if (rddebug & RDB_FOLLOW)
    660   1.91  uebayasi 		printf("rdstrategy(%p): dev %"PRIx64", bn %llx, bcount %x, %c\n",
    661    1.1       cgd 		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
    662    1.1       cgd 		       (bp->b_flags & B_READ) ? 'R' : 'W');
    663    1.1       cgd #endif
    664    1.1       cgd 	bn = bp->b_blkno;
    665    1.1       cgd 	sz = howmany(bp->b_bcount, DEV_BSIZE);
    666   1.85   tsutsui 	pinfo = &sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
    667   1.26   thorpej 
    668   1.26   thorpej 	/* Don't perform partition translation on RAW_PART. */
    669   1.26   thorpej 	offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
    670   1.26   thorpej 
    671   1.26   thorpej 	if (rdpart(bp->b_dev) != RAW_PART) {
    672   1.26   thorpej 		/*
    673   1.26   thorpej 		 * XXX This block of code belongs in
    674   1.26   thorpej 		 * XXX bounds_check_with_label()
    675   1.26   thorpej 		 */
    676   1.26   thorpej 
    677   1.26   thorpej 		if (bn < 0 || bn + sz > pinfo->p_size) {
    678   1.26   thorpej 			sz = pinfo->p_size - bn;
    679   1.26   thorpej 			if (sz == 0) {
    680   1.26   thorpej 				bp->b_resid = bp->b_bcount;
    681   1.26   thorpej 				goto done;
    682   1.26   thorpej 			}
    683   1.26   thorpej 			if (sz < 0) {
    684   1.26   thorpej 				bp->b_error = EINVAL;
    685   1.82        ad 				goto done;
    686   1.26   thorpej 			}
    687   1.26   thorpej 			bp->b_bcount = dbtob(sz);
    688    1.1       cgd 		}
    689   1.26   thorpej 		/*
    690   1.26   thorpej 		 * Check for write to write protected label
    691   1.26   thorpej 		 */
    692   1.26   thorpej 		if (bn + offset <= LABELSECTOR &&
    693   1.26   thorpej #if LABELSECTOR != 0
    694   1.26   thorpej 		    bn + offset + sz > LABELSECTOR &&
    695   1.26   thorpej #endif
    696   1.85   tsutsui 		    !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
    697   1.26   thorpej 			bp->b_error = EROFS;
    698   1.82        ad 			goto done;
    699    1.1       cgd 		}
    700    1.8   mycroft 	}
    701   1.39   thorpej 	bp->b_rawblkno = bn + offset;
    702    1.1       cgd 	s = splbio();
    703   1.89      yamt 	bufq_put(sc->sc_tab, bp);
    704   1.85   tsutsui 	if (sc->sc_active == 0) {
    705   1.85   tsutsui 		sc->sc_active = 1;
    706   1.85   tsutsui 		rdustart(sc);
    707    1.1       cgd 	}
    708    1.1       cgd 	splx(s);
    709    1.1       cgd 	return;
    710    1.1       cgd done:
    711    1.1       cgd 	biodone(bp);
    712    1.1       cgd }
    713    1.1       cgd 
    714    1.1       cgd /*
    715    1.1       cgd  * Called from timeout() when handling maintenance releases
    716    1.1       cgd  */
    717   1.63   thorpej static void
    718   1.63   thorpej rdrestart(void *arg)
    719    1.1       cgd {
    720    1.1       cgd 	int s = splbio();
    721   1.27   thorpej 	rdustart((struct rd_softc *)arg);
    722    1.1       cgd 	splx(s);
    723    1.1       cgd }
    724    1.1       cgd 
    725   1.63   thorpej static void
    726   1.85   tsutsui rdustart(struct rd_softc *sc)
    727    1.1       cgd {
    728   1.28    scottr 	struct buf *bp;
    729    1.1       cgd 
    730   1.89      yamt 	bp = bufq_peek(sc->sc_tab);
    731   1.85   tsutsui 	sc->sc_addr = bp->b_data;
    732   1.85   tsutsui 	sc->sc_resid = bp->b_bcount;
    733   1.85   tsutsui 	if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
    734   1.85   tsutsui 		rdstart(sc);
    735    1.1       cgd }
    736    1.1       cgd 
    737   1.63   thorpej static struct buf *
    738   1.85   tsutsui rdfinish(struct rd_softc *sc, struct buf *bp)
    739    1.8   mycroft {
    740    1.8   mycroft 
    741   1.85   tsutsui 	sc->sc_errcnt = 0;
    742   1.89      yamt 	(void)bufq_get(sc->sc_tab);
    743    1.8   mycroft 	bp->b_resid = 0;
    744    1.8   mycroft 	biodone(bp);
    745   1.85   tsutsui 	hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
    746   1.89      yamt 	if ((bp = bufq_peek(sc->sc_tab)) != NULL)
    747   1.78   tsutsui 		return bp;
    748   1.85   tsutsui 	sc->sc_active = 0;
    749   1.85   tsutsui 	if (sc->sc_flags & RDF_WANTED) {
    750   1.85   tsutsui 		sc->sc_flags &= ~RDF_WANTED;
    751   1.85   tsutsui 		wakeup((void *)&sc->sc_tab);
    752    1.8   mycroft 	}
    753   1.78   tsutsui 	return NULL;
    754    1.8   mycroft }
    755    1.8   mycroft 
    756   1.63   thorpej static void
    757   1.63   thorpej rdstart(void *arg)
    758    1.1       cgd {
    759   1.85   tsutsui 	struct rd_softc *sc = arg;
    760   1.89      yamt 	struct buf *bp = bufq_peek(sc->sc_tab);
    761   1.95  christos 	int ctlr, slave;
    762   1.27   thorpej 
    763   1.85   tsutsui 	ctlr = device_unit(device_parent(sc->sc_dev));
    764   1.85   tsutsui 	slave = sc->sc_slave;
    765    1.1       cgd 
    766    1.1       cgd again:
    767    1.1       cgd #ifdef DEBUG
    768    1.1       cgd 	if (rddebug & RDB_FOLLOW)
    769   1.85   tsutsui 		printf("rdstart(%s): bp %p, %c\n", device_xname(sc->sc_dev), bp,
    770   1.85   tsutsui 		    (bp->b_flags & B_READ) ? 'R' : 'W');
    771    1.1       cgd #endif
    772   1.85   tsutsui 	sc->sc_flags |= RDF_SEEK;
    773   1.85   tsutsui 	sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
    774   1.85   tsutsui 	sc->sc_ioc.c_volume = C_SVOL(0);
    775   1.85   tsutsui 	sc->sc_ioc.c_saddr = C_SADDR;
    776   1.85   tsutsui 	sc->sc_ioc.c_hiaddr = 0;
    777   1.85   tsutsui 	sc->sc_ioc.c_addr = RDBTOS(bp->b_rawblkno);
    778   1.85   tsutsui 	sc->sc_ioc.c_nop2 = C_NOP;
    779   1.85   tsutsui 	sc->sc_ioc.c_slen = C_SLEN;
    780   1.85   tsutsui 	sc->sc_ioc.c_len = sc->sc_resid;
    781   1.85   tsutsui 	sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
    782    1.1       cgd #ifdef DEBUG
    783    1.1       cgd 	if (rddebug & RDB_IO)
    784   1.29    scottr 		printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
    785   1.27   thorpej 		       ctlr, slave, C_CMD,
    786   1.85   tsutsui 		       &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc) - 2);
    787    1.1       cgd #endif
    788   1.85   tsutsui 	if (hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
    789   1.85   tsutsui 		     sizeof(sc->sc_ioc) - 2) == sizeof(sc->sc_ioc) - 2) {
    790   1.17   thorpej 
    791   1.17   thorpej 		/* Instrumentation. */
    792   1.85   tsutsui 		disk_busy(&sc->sc_dkdev);
    793   1.85   tsutsui 		iostat_seek(sc->sc_dkdev.dk_stats);
    794   1.17   thorpej 
    795    1.1       cgd #ifdef DEBUG
    796    1.1       cgd 		if (rddebug & RDB_IO)
    797   1.27   thorpej 			printf("rdstart: hpibawait(%x)\n", ctlr);
    798    1.1       cgd #endif
    799   1.27   thorpej 		hpibawait(ctlr);
    800    1.1       cgd 		return;
    801    1.1       cgd 	}
    802    1.1       cgd 	/*
    803    1.1       cgd 	 * Experience has shown that the hpibwait in this hpibsend will
    804    1.1       cgd 	 * occasionally timeout.  It appears to occur mostly on old 7914
    805    1.1       cgd 	 * drives with full maintenance tracks.  We should probably
    806    1.1       cgd 	 * integrate this with the backoff code in rderror.
    807    1.1       cgd 	 */
    808    1.1       cgd #ifdef DEBUG
    809    1.1       cgd 	if (rddebug & RDB_ERROR)
    810   1.66       chs 		printf("%s: rdstart: cmd %x adr %lx blk %lld len %d ecnt %d\n",
    811   1.85   tsutsui 		    device_xname(sc->sc_dev),
    812   1.85   tsutsui 		    sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
    813   1.85   tsutsui 		    bp->b_blkno, sc->sc_resid, sc->sc_errcnt);
    814   1.85   tsutsui 	sc->sc_stats.rdretries++;
    815   1.85   tsutsui #endif
    816   1.85   tsutsui 	sc->sc_flags &= ~RDF_SEEK;
    817   1.85   tsutsui 	rdreset(sc);
    818   1.85   tsutsui 	if (sc->sc_errcnt++ < RDRETRY)
    819    1.1       cgd 		goto again;
    820   1.56  gmcgarry 	printf("%s: rdstart err: cmd 0x%x sect %ld blk %" PRId64 " len %d\n",
    821   1.85   tsutsui 	    device_xname(sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
    822   1.85   tsutsui 	    bp->b_blkno, sc->sc_resid);
    823    1.1       cgd 	bp->b_error = EIO;
    824   1.85   tsutsui 	bp = rdfinish(sc, bp);
    825    1.8   mycroft 	if (bp) {
    826   1.85   tsutsui 		sc->sc_addr = bp->b_data;
    827   1.85   tsutsui 		sc->sc_resid = bp->b_bcount;
    828   1.85   tsutsui 		if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
    829    1.8   mycroft 			goto again;
    830    1.1       cgd 	}
    831    1.1       cgd }
    832    1.1       cgd 
    833   1.63   thorpej static void
    834   1.63   thorpej rdgo(void *arg)
    835    1.1       cgd {
    836   1.85   tsutsui 	struct rd_softc *sc = arg;
    837   1.89      yamt 	struct buf *bp = bufq_peek(sc->sc_tab);
    838   1.27   thorpej 	int rw, ctlr, slave;
    839   1.27   thorpej 
    840   1.85   tsutsui 	ctlr = device_unit(device_parent(sc->sc_dev));
    841   1.85   tsutsui 	slave = sc->sc_slave;
    842   1.13   thorpej 
    843   1.13   thorpej 	rw = bp->b_flags & B_READ;
    844    1.1       cgd 
    845   1.17   thorpej 	/* Instrumentation. */
    846   1.85   tsutsui 	disk_busy(&sc->sc_dkdev);
    847   1.17   thorpej 
    848    1.8   mycroft #ifdef USELEDS
    849   1.31   thorpej 	ledcontrol(0, 0, LED_DISK);
    850    1.8   mycroft #endif
    851   1.85   tsutsui 	hpibgo(ctlr, slave, C_EXEC, sc->sc_addr, sc->sc_resid, rw, rw != 0);
    852    1.1       cgd }
    853    1.1       cgd 
    854   1.27   thorpej /* ARGSUSED */
    855   1.63   thorpej static void
    856   1.63   thorpej rdintr(void *arg)
    857    1.1       cgd {
    858   1.85   tsutsui 	struct rd_softc *sc = arg;
    859   1.85   tsutsui 	int unit = device_unit(sc->sc_dev);
    860   1.89      yamt 	struct buf *bp = bufq_peek(sc->sc_tab);
    861    1.1       cgd 	u_char stat = 13;	/* in case hpibrecv fails */
    862   1.27   thorpej 	int rv, restart, ctlr, slave;
    863   1.27   thorpej 
    864   1.85   tsutsui 	ctlr = device_unit(device_parent(sc->sc_dev));
    865   1.85   tsutsui 	slave = sc->sc_slave;
    866   1.27   thorpej 
    867    1.1       cgd #ifdef DEBUG
    868    1.1       cgd 	if (rddebug & RDB_FOLLOW)
    869   1.29    scottr 		printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
    870   1.85   tsutsui 		    (bp->b_flags & B_READ) ? 'R' : 'W', sc->sc_flags);
    871    1.1       cgd 	if (bp == NULL) {
    872   1.85   tsutsui 		printf("%s: bp == NULL\n", device_xname(sc->sc_dev));
    873    1.1       cgd 		return;
    874    1.1       cgd 	}
    875    1.1       cgd #endif
    876   1.85   tsutsui 	disk_unbusy(&sc->sc_dkdev, (bp->b_bcount - bp->b_resid),
    877   1.55       mrg 	    (bp->b_flags & B_READ));
    878   1.17   thorpej 
    879   1.85   tsutsui 	if (sc->sc_flags & RDF_SEEK) {
    880   1.85   tsutsui 		sc->sc_flags &= ~RDF_SEEK;
    881   1.27   thorpej 		if (hpibustart(ctlr))
    882   1.85   tsutsui 			rdgo(sc);
    883    1.1       cgd 		return;
    884    1.1       cgd 	}
    885   1.85   tsutsui 	if ((sc->sc_flags & RDF_SWAIT) == 0) {
    886    1.1       cgd #ifdef DEBUG
    887   1.85   tsutsui 		sc->sc_stats.rdpolltries++;
    888    1.1       cgd #endif
    889   1.27   thorpej 		if (hpibpptest(ctlr, slave) == 0) {
    890    1.1       cgd #ifdef DEBUG
    891   1.85   tsutsui 			sc->sc_stats.rdpollwaits++;
    892    1.1       cgd #endif
    893   1.17   thorpej 
    894   1.17   thorpej 			/* Instrumentation. */
    895   1.85   tsutsui 			disk_busy(&sc->sc_dkdev);
    896   1.85   tsutsui 			sc->sc_flags |= RDF_SWAIT;
    897   1.27   thorpej 			hpibawait(ctlr);
    898    1.1       cgd 			return;
    899    1.1       cgd 		}
    900    1.1       cgd 	} else
    901   1.85   tsutsui 		sc->sc_flags &= ~RDF_SWAIT;
    902   1.27   thorpej 	rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
    903    1.1       cgd 	if (rv != 1 || stat) {
    904    1.1       cgd #ifdef DEBUG
    905    1.1       cgd 		if (rddebug & RDB_ERROR)
    906   1.24  christos 			printf("rdintr: recv failed or bad stat %d\n", stat);
    907    1.1       cgd #endif
    908    1.1       cgd 		restart = rderror(unit);
    909    1.1       cgd #ifdef DEBUG
    910   1.85   tsutsui 		sc->sc_stats.rdretries++;
    911    1.1       cgd #endif
    912   1.85   tsutsui 		if (sc->sc_errcnt++ < RDRETRY) {
    913    1.1       cgd 			if (restart)
    914   1.85   tsutsui 				rdstart(sc);
    915    1.1       cgd 			return;
    916    1.1       cgd 		}
    917    1.1       cgd 		bp->b_error = EIO;
    918    1.1       cgd 	}
    919   1.85   tsutsui 	if (rdfinish(sc, bp))
    920   1.85   tsutsui 		rdustart(sc);
    921   1.85   tsutsui 	rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
    922    1.1       cgd }
    923    1.1       cgd 
    924   1.63   thorpej static int
    925   1.85   tsutsui rdstatus(struct rd_softc *sc)
    926    1.1       cgd {
    927   1.28    scottr 	int c, s;
    928    1.1       cgd 	u_char stat;
    929    1.1       cgd 	int rv;
    930    1.1       cgd 
    931   1.85   tsutsui 	c = device_unit(device_parent(sc->sc_dev));
    932   1.85   tsutsui 	s = sc->sc_slave;
    933   1.85   tsutsui 	sc->sc_rsc.c_unit = C_SUNIT(sc->sc_punit);
    934   1.85   tsutsui 	sc->sc_rsc.c_sram = C_SRAM;
    935   1.85   tsutsui 	sc->sc_rsc.c_ram = C_RAM;
    936   1.85   tsutsui 	sc->sc_rsc.c_cmd = C_STATUS;
    937   1.85   tsutsui 	memset((void *)&sc->sc_stat, 0, sizeof(sc->sc_stat));
    938   1.85   tsutsui 	rv = hpibsend(c, s, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
    939   1.85   tsutsui 	if (rv != sizeof(sc->sc_rsc)) {
    940    1.1       cgd #ifdef DEBUG
    941    1.1       cgd 		if (rddebug & RDB_STATUS)
    942   1.24  christos 			printf("rdstatus: send C_CMD failed %d != %d\n",
    943   1.85   tsutsui 			    rv, sizeof(sc->sc_rsc));
    944    1.1       cgd #endif
    945   1.78   tsutsui 		return 1;
    946    1.1       cgd 	}
    947   1.85   tsutsui 	rv = hpibrecv(c, s, C_EXEC, &sc->sc_stat, sizeof(sc->sc_stat));
    948   1.85   tsutsui 	if (rv != sizeof(sc->sc_stat)) {
    949    1.1       cgd #ifdef DEBUG
    950    1.1       cgd 		if (rddebug & RDB_STATUS)
    951   1.24  christos 			printf("rdstatus: send C_EXEC failed %d != %d\n",
    952   1.85   tsutsui 			    rv, sizeof(sc->sc_stat));
    953    1.1       cgd #endif
    954   1.78   tsutsui 		return 1;
    955    1.1       cgd 	}
    956    1.1       cgd 	rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
    957    1.1       cgd 	if (rv != 1 || stat) {
    958    1.1       cgd #ifdef DEBUG
    959    1.1       cgd 		if (rddebug & RDB_STATUS)
    960   1.24  christos 			printf("rdstatus: recv failed %d or bad stat %d\n",
    961   1.78   tsutsui 			    rv, stat);
    962    1.1       cgd #endif
    963   1.78   tsutsui 		return 1;
    964    1.1       cgd 	}
    965   1.78   tsutsui 	return 0;
    966    1.1       cgd }
    967    1.1       cgd 
    968    1.1       cgd /*
    969    1.1       cgd  * Deal with errors.
    970    1.1       cgd  * Returns 1 if request should be restarted,
    971    1.1       cgd  * 0 if we should just quietly give up.
    972    1.1       cgd  */
    973   1.63   thorpej static int
    974   1.63   thorpej rderror(int unit)
    975    1.1       cgd {
    976   1.87    cegger 	struct rd_softc *sc = device_lookup_private(&rd_cd,unit);
    977   1.28    scottr 	struct rd_stat *sp;
    978    1.1       cgd 	struct buf *bp;
    979    1.1       cgd 	daddr_t hwbn, pbn;
    980   1.63   thorpej 	char *hexstr(int, int); /* XXX */
    981    1.1       cgd 
    982   1.85   tsutsui 	if (rdstatus(sc)) {
    983    1.1       cgd #ifdef DEBUG
    984   1.85   tsutsui 		printf("%s: couldn't get status\n", device_xname(sc->sc_dev));
    985    1.1       cgd #endif
    986   1.85   tsutsui 		rdreset(sc);
    987   1.78   tsutsui 		return 1;
    988    1.1       cgd 	}
    989   1.85   tsutsui 	sp = &sc->sc_stat;
    990    1.1       cgd 	if (sp->c_fef & FEF_REXMT)
    991   1.78   tsutsui 		return 1;
    992    1.1       cgd 	if (sp->c_fef & FEF_PF) {
    993   1.85   tsutsui 		rdreset(sc);
    994   1.78   tsutsui 		return 1;
    995    1.1       cgd 	}
    996    1.1       cgd 	/*
    997    1.1       cgd 	 * Unit requests release for internal maintenance.
    998    1.1       cgd 	 * We just delay awhile and try again later.  Use expontially
    999    1.1       cgd 	 * increasing backoff ala ethernet drivers since we don't really
   1000    1.1       cgd 	 * know how long the maintenance will take.  With RDWAITC and
   1001    1.1       cgd 	 * RDRETRY as defined, the range is 1 to 32 seconds.
   1002    1.1       cgd 	 */
   1003    1.1       cgd 	if (sp->c_fef & FEF_IMR) {
   1004    1.1       cgd 		extern int hz;
   1005   1.85   tsutsui 		int rdtimo = RDWAITC << sc->sc_errcnt;
   1006    1.1       cgd #ifdef DEBUG
   1007   1.24  christos 		printf("%s: internal maintenance, %d second timeout\n",
   1008   1.85   tsutsui 		    device_xname(sc->sc_dev), rdtimo);
   1009   1.85   tsutsui 		sc->sc_stats.rdtimeouts++;
   1010    1.1       cgd #endif
   1011   1.85   tsutsui 		hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
   1012   1.85   tsutsui 		callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
   1013   1.78   tsutsui 		return 0;
   1014    1.1       cgd 	}
   1015    1.1       cgd 	/*
   1016    1.1       cgd 	 * Only report error if we have reached the error reporting
   1017    1.1       cgd 	 * threshhold.  By default, this will only report after the
   1018    1.1       cgd 	 * retry limit has been exceeded.
   1019    1.1       cgd 	 */
   1020   1.85   tsutsui 	if (sc->sc_errcnt < rderrthresh)
   1021   1.78   tsutsui 		return 1;
   1022    1.1       cgd 
   1023    1.1       cgd 	/*
   1024   1.45       wiz 	 * First conjure up the block number at which the error occurred.
   1025    1.1       cgd 	 * Note that not all errors report a block number, in that case
   1026    1.1       cgd 	 * we just use b_blkno.
   1027   1.76   tsutsui 	 */
   1028   1.89      yamt 	bp = bufq_peek(sc->sc_tab);
   1029   1.85   tsutsui 	pbn = sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
   1030    1.1       cgd 	if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
   1031    1.1       cgd 	    (sp->c_ief & IEF_RRMASK)) {
   1032    1.1       cgd 		hwbn = RDBTOS(pbn + bp->b_blkno);
   1033    1.1       cgd 		pbn = bp->b_blkno;
   1034    1.1       cgd 	} else {
   1035    1.1       cgd 		hwbn = sp->c_blk;
   1036    1.1       cgd 		pbn = RDSTOB(hwbn) - pbn;
   1037    1.1       cgd 	}
   1038    1.1       cgd 	/*
   1039    1.1       cgd 	 * Now output a generic message suitable for badsect.
   1040    1.1       cgd 	 * Note that we don't use harderr cuz it just prints
   1041    1.1       cgd 	 * out b_blkno which is just the beginning block number
   1042   1.45       wiz 	 * of the transfer, not necessary where the error occurred.
   1043    1.1       cgd 	 */
   1044   1.85   tsutsui 	printf("%s%c: hard error sn%" PRId64 "\n", device_xname(sc->sc_dev),
   1045   1.27   thorpej 	    'a'+rdpart(bp->b_dev), pbn);
   1046    1.1       cgd 	/*
   1047    1.1       cgd 	 * Now report the status as returned by the hardware with
   1048    1.1       cgd 	 * attempt at interpretation (unless debugging).
   1049    1.1       cgd 	 */
   1050   1.85   tsutsui 	printf("%s %s error:", device_xname(sc->sc_dev),
   1051   1.27   thorpej 	    (bp->b_flags & B_READ) ? "read" : "write");
   1052    1.1       cgd #ifdef DEBUG
   1053    1.1       cgd 	if (rddebug & RDB_ERROR) {
   1054    1.1       cgd 		/* status info */
   1055   1.24  christos 		printf("\n    volume: %d, unit: %d\n",
   1056   1.78   tsutsui 		    (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
   1057    1.1       cgd 		rdprinterr("reject", sp->c_ref, err_reject);
   1058    1.1       cgd 		rdprinterr("fault", sp->c_fef, err_fault);
   1059    1.1       cgd 		rdprinterr("access", sp->c_aef, err_access);
   1060    1.1       cgd 		rdprinterr("info", sp->c_ief, err_info);
   1061   1.66       chs 		printf("    block: %lld, P1-P10: ", hwbn);
   1062   1.30   thorpej 		printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1063   1.30   thorpej 		printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1064   1.30   thorpej 		printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1065    1.1       cgd 		/* command */
   1066   1.24  christos 		printf("    ioc: ");
   1067   1.85   tsutsui 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_pad);
   1068   1.85   tsutsui 		printf("0x%x", *(u_short *)&sc->sc_ioc.c_hiaddr);
   1069   1.85   tsutsui 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_addr);
   1070   1.85   tsutsui 		printf("0x%x", *(u_short *)&sc->sc_ioc.c_nop2);
   1071   1.85   tsutsui 		printf("0x%x", *(u_int *)&sc->sc_ioc.c_len);
   1072   1.85   tsutsui 		printf("0x%x\n", *(u_short *)&sc->sc_ioc.c_cmd);
   1073   1.78   tsutsui 		return 1;
   1074    1.1       cgd 	}
   1075    1.1       cgd #endif
   1076   1.24  christos 	printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
   1077   1.78   tsutsui 	    (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
   1078   1.78   tsutsui 	    sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
   1079   1.24  christos 	printf("P1-P10: ");
   1080   1.30   thorpej 	printf("0x%x", *(u_int *)&sp->c_raw[0]);
   1081   1.30   thorpej 	printf("0x%x", *(u_int *)&sp->c_raw[4]);
   1082   1.30   thorpej 	printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
   1083   1.78   tsutsui 	return 1;
   1084   1.12   thorpej }
   1085   1.12   thorpej 
   1086   1.63   thorpej static int
   1087   1.63   thorpej rdread(dev_t dev, struct uio *uio, int flags)
   1088   1.12   thorpej {
   1089   1.12   thorpej 
   1090   1.78   tsutsui 	return physio(rdstrategy, NULL, dev, B_READ, minphys, uio);
   1091   1.12   thorpej }
   1092   1.12   thorpej 
   1093   1.63   thorpej static int
   1094   1.63   thorpej rdwrite(dev_t dev, struct uio *uio, int flags)
   1095   1.12   thorpej {
   1096   1.12   thorpej 
   1097   1.78   tsutsui 	return physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio);
   1098    1.1       cgd }
   1099    1.1       cgd 
   1100   1.63   thorpej static int
   1101   1.79  christos rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
   1102    1.1       cgd {
   1103   1.87    cegger 	struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
   1104   1.28    scottr 	struct disklabel *lp = sc->sc_dkdev.dk_label;
   1105    1.8   mycroft 	int error, flags;
   1106    1.8   mycroft 
   1107   1.99  christos 	error = disk_ioctl(&sc->sc_dkdev, rdpart(dev), cmd, data, flag, l);
   1108   1.99  christos 	if (error != EPASSTHROUGH)
   1109   1.99  christos 		return error;
   1110   1.99  christos 
   1111    1.8   mycroft 	switch (cmd) {
   1112    1.8   mycroft 	case DIOCWLABEL:
   1113    1.8   mycroft 		if ((flag & FWRITE) == 0)
   1114   1.78   tsutsui 			return EBADF;
   1115    1.8   mycroft 		if (*(int *)data)
   1116    1.8   mycroft 			sc->sc_flags |= RDF_WLABEL;
   1117    1.8   mycroft 		else
   1118    1.8   mycroft 			sc->sc_flags &= ~RDF_WLABEL;
   1119   1.78   tsutsui 		return 0;
   1120    1.8   mycroft 
   1121    1.8   mycroft 	case DIOCSDINFO:
   1122    1.8   mycroft 		if ((flag & FWRITE) == 0)
   1123   1.78   tsutsui 			return EBADF;
   1124   1.78   tsutsui 		return setdisklabel(lp, (struct disklabel *)data,
   1125   1.78   tsutsui 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
   1126   1.78   tsutsui 		    NULL);
   1127    1.8   mycroft 
   1128    1.8   mycroft 	case DIOCWDINFO:
   1129    1.8   mycroft 		if ((flag & FWRITE) == 0)
   1130   1.78   tsutsui 			return EBADF;
   1131    1.8   mycroft 		error = setdisklabel(lp, (struct disklabel *)data,
   1132   1.78   tsutsui 		    (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
   1133   1.78   tsutsui 		    NULL);
   1134    1.8   mycroft 		if (error)
   1135   1.78   tsutsui 			return error;
   1136    1.8   mycroft 		flags = sc->sc_flags;
   1137    1.8   mycroft 		sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
   1138   1.78   tsutsui 		error = writedisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
   1139    1.8   mycroft 		sc->sc_flags = flags;
   1140   1.78   tsutsui 		return error;
   1141   1.47  gmcgarry 
   1142   1.47  gmcgarry 	case DIOCGDEFLABEL:
   1143   1.47  gmcgarry 		rdgetdefaultlabel(sc, (struct disklabel *)data);
   1144   1.78   tsutsui 		return 0;
   1145    1.8   mycroft 	}
   1146   1.78   tsutsui 	return EINVAL;
   1147   1.47  gmcgarry }
   1148   1.47  gmcgarry 
   1149   1.63   thorpej static void
   1150   1.63   thorpej rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
   1151   1.47  gmcgarry {
   1152   1.47  gmcgarry 	int type = sc->sc_type;
   1153   1.47  gmcgarry 
   1154   1.79  christos 	memset((void *)lp, 0, sizeof(struct disklabel));
   1155   1.47  gmcgarry 
   1156  1.100  christos 	lp->d_type = DKTYPE_HPIB;
   1157   1.49  gmcgarry 	lp->d_secsize = DEV_BSIZE;
   1158   1.49  gmcgarry 	lp->d_nsectors = rdidentinfo[type].ri_nbpt;
   1159   1.47  gmcgarry 	lp->d_ntracks = rdidentinfo[type].ri_ntpc;
   1160   1.47  gmcgarry 	lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
   1161   1.49  gmcgarry 	lp->d_secperunit = rdidentinfo[type].ri_nblocks;
   1162   1.47  gmcgarry 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   1163   1.62   tsutsui 
   1164   1.77   tsutsui 	strlcpy(lp->d_typename, rdidentinfo[type].ri_desc,
   1165   1.77   tsutsui 	    sizeof(lp->d_typename));
   1166   1.77   tsutsui 	strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
   1167   1.47  gmcgarry 	lp->d_rpm = 3000;
   1168   1.47  gmcgarry 	lp->d_interleave = 1;
   1169   1.47  gmcgarry 	lp->d_flags = 0;
   1170   1.47  gmcgarry 
   1171   1.47  gmcgarry 	lp->d_partitions[RAW_PART].p_offset = 0;
   1172   1.47  gmcgarry 	lp->d_partitions[RAW_PART].p_size =
   1173   1.47  gmcgarry 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   1174   1.47  gmcgarry 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   1175   1.47  gmcgarry 	lp->d_npartitions = RAW_PART + 1;
   1176   1.47  gmcgarry 
   1177   1.47  gmcgarry 	lp->d_magic = DISKMAGIC;
   1178   1.47  gmcgarry 	lp->d_magic2 = DISKMAGIC;
   1179   1.47  gmcgarry 	lp->d_checksum = dkcksum(lp);
   1180    1.1       cgd }
   1181    1.1       cgd 
   1182    1.1       cgd int
   1183   1.63   thorpej rdsize(dev_t dev)
   1184    1.1       cgd {
   1185   1.85   tsutsui 	struct rd_softc *sc;
   1186    1.8   mycroft 	int psize, didopen = 0;
   1187    1.1       cgd 
   1188   1.87    cegger 	sc = device_lookup_private(&rd_cd, rdunit(dev));
   1189   1.87    cegger 	if (sc == NULL)
   1190   1.87    cegger 		return ENXIO;
   1191   1.87    cegger 
   1192   1.88        he 	if ((sc->sc_flags & RDF_ALIVE) == 0)
   1193   1.87    cegger 		return ENXIO;
   1194    1.8   mycroft 
   1195    1.8   mycroft 	/*
   1196    1.8   mycroft 	 * We get called very early on (via swapconf)
   1197    1.8   mycroft 	 * without the device being open so we may need
   1198    1.8   mycroft 	 * to handle it here.
   1199    1.8   mycroft 	 */
   1200   1.85   tsutsui 	if (sc->sc_dkdev.dk_openmask == 0) {
   1201    1.8   mycroft 		if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
   1202   1.78   tsutsui 			return -1;
   1203    1.8   mycroft 		didopen = 1;
   1204    1.8   mycroft 	}
   1205   1.85   tsutsui 	psize = sc->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
   1206   1.85   tsutsui 	    (sc->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
   1207    1.8   mycroft 	if (didopen)
   1208   1.78   tsutsui 		(void)rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
   1209   1.78   tsutsui 	return psize;
   1210    1.1       cgd }
   1211    1.1       cgd 
   1212    1.1       cgd #ifdef DEBUG
   1213   1.63   thorpej static void
   1214   1.63   thorpej rdprinterr(const char *str, short err, const char **tab)
   1215    1.1       cgd {
   1216   1.28    scottr 	int i;
   1217    1.1       cgd 	int printed;
   1218    1.1       cgd 
   1219    1.1       cgd 	if (err == 0)
   1220    1.1       cgd 		return;
   1221   1.29    scottr 	printf("    %s error %d field:", str, err);
   1222    1.1       cgd 	printed = 0;
   1223    1.1       cgd 	for (i = 0; i < 16; i++)
   1224    1.1       cgd 		if (err & (0x8000 >> i))
   1225   1.24  christos 			printf("%s%s", printed++ ? " + " : " ", tab[i]);
   1226   1.24  christos 	printf("\n");
   1227    1.1       cgd }
   1228    1.1       cgd #endif
   1229    1.1       cgd 
   1230   1.22   thorpej static int rddoingadump;	/* simple mutex */
   1231   1.22   thorpej 
   1232    1.1       cgd /*
   1233   1.58       wiz  * Non-interrupt driven, non-DMA dump routine.
   1234    1.1       cgd  */
   1235   1.63   thorpej static int
   1236   1.79  christos rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
   1237   1.22   thorpej {
   1238   1.22   thorpej 	int sectorsize;		/* size of a disk sector */
   1239   1.22   thorpej 	int nsects;		/* number of sectors in partition */
   1240   1.22   thorpej 	int sectoff;		/* sector offset of partition */
   1241   1.22   thorpej 	int totwrt;		/* total number of sectors left to write */
   1242   1.22   thorpej 	int nwrt;		/* current number of sectors to write */
   1243   1.95  christos 	int part;
   1244   1.27   thorpej 	int ctlr, slave;
   1245   1.85   tsutsui 	struct rd_softc *sc;
   1246   1.22   thorpej 	struct disklabel *lp;
   1247    1.1       cgd 	char stat;
   1248    1.1       cgd 
   1249   1.22   thorpej 	/* Check for recursive dump; if so, punt. */
   1250   1.22   thorpej 	if (rddoingadump)
   1251   1.78   tsutsui 		return EFAULT;
   1252   1.22   thorpej 	rddoingadump = 1;
   1253   1.22   thorpej 
   1254   1.22   thorpej 	/* Decompose unit and partition. */
   1255   1.22   thorpej 	part = rdpart(dev);
   1256   1.22   thorpej 
   1257   1.22   thorpej 	/* Make sure dump device is ok. */
   1258   1.87    cegger 	sc = device_lookup_private(&rd_cd, rdunit(dev));
   1259   1.87    cegger 	if (sc == NULL)
   1260   1.87    cegger 		return ENXIO;
   1261   1.87    cegger 
   1262   1.88        he 	if ((sc->sc_flags & RDF_ALIVE) == 0)
   1263   1.78   tsutsui 		return ENXIO;
   1264   1.27   thorpej 
   1265   1.85   tsutsui 	ctlr = device_unit(device_parent(sc->sc_dev));
   1266   1.85   tsutsui 	slave = sc->sc_slave;
   1267   1.22   thorpej 
   1268   1.22   thorpej 	/*
   1269   1.22   thorpej 	 * Convert to disk sectors.  Request must be a multiple of size.
   1270   1.22   thorpej 	 */
   1271   1.85   tsutsui 	lp = sc->sc_dkdev.dk_label;
   1272   1.22   thorpej 	sectorsize = lp->d_secsize;
   1273   1.22   thorpej 	if ((size % sectorsize) != 0)
   1274   1.78   tsutsui 		return EFAULT;
   1275   1.22   thorpej 	totwrt = size / sectorsize;
   1276   1.22   thorpej 	blkno = dbtob(blkno) / sectorsize;	/* blkno in DEV_BSIZE units */
   1277   1.22   thorpej 
   1278   1.22   thorpej 	nsects = lp->d_partitions[part].p_size;
   1279   1.22   thorpej 	sectoff = lp->d_partitions[part].p_offset;
   1280   1.22   thorpej 
   1281   1.22   thorpej 	/* Check transfer bounds against partition size. */
   1282   1.22   thorpej 	if ((blkno < 0) || (blkno + totwrt) > nsects)
   1283   1.78   tsutsui 		return EINVAL;
   1284   1.22   thorpej 
   1285   1.22   thorpej 	/* Offset block number to start of partition. */
   1286   1.22   thorpej 	blkno += sectoff;
   1287   1.22   thorpej 
   1288   1.22   thorpej 	while (totwrt > 0) {
   1289   1.22   thorpej 		nwrt = totwrt;		/* XXX */
   1290   1.22   thorpej #ifndef RD_DUMP_NOT_TRUSTED
   1291   1.22   thorpej 		/*
   1292   1.22   thorpej 		 * Fill out and send HPIB command.
   1293   1.22   thorpej 		 */
   1294   1.85   tsutsui 		sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
   1295   1.85   tsutsui 		sc->sc_ioc.c_volume = C_SVOL(0);
   1296   1.85   tsutsui 		sc->sc_ioc.c_saddr = C_SADDR;
   1297   1.85   tsutsui 		sc->sc_ioc.c_hiaddr = 0;
   1298   1.85   tsutsui 		sc->sc_ioc.c_addr = RDBTOS(blkno);
   1299   1.85   tsutsui 		sc->sc_ioc.c_nop2 = C_NOP;
   1300   1.85   tsutsui 		sc->sc_ioc.c_slen = C_SLEN;
   1301   1.85   tsutsui 		sc->sc_ioc.c_len = nwrt * sectorsize;
   1302   1.85   tsutsui 		sc->sc_ioc.c_cmd = C_WRITE;
   1303   1.85   tsutsui 		hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
   1304   1.85   tsutsui 		    sizeof(sc->sc_ioc) - 2);
   1305   1.27   thorpej 		if (hpibswait(ctlr, slave))
   1306   1.78   tsutsui 			return EIO;
   1307   1.22   thorpej 
   1308   1.22   thorpej 		/*
   1309   1.22   thorpej 		 * Send the data.
   1310   1.22   thorpej 		 */
   1311   1.27   thorpej 		hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
   1312   1.27   thorpej 		(void) hpibswait(ctlr, slave);
   1313   1.27   thorpej 		hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
   1314    1.8   mycroft 		if (stat)
   1315   1.78   tsutsui 			return EIO;
   1316   1.22   thorpej #else /* RD_DUMP_NOT_TRUSTED */
   1317   1.22   thorpej 		/* Let's just talk about this first... */
   1318   1.85   tsutsui 		printf("%s: dump addr %p, blk %d\n", device_xname(sc->sc_dev),
   1319   1.22   thorpej 		    va, blkno);
   1320   1.22   thorpej 		delay(500 * 1000);	/* half a second */
   1321   1.22   thorpej #endif /* RD_DUMP_NOT_TRUSTED */
   1322   1.22   thorpej 
   1323   1.22   thorpej 		/* update block count */
   1324   1.22   thorpej 		totwrt -= nwrt;
   1325   1.22   thorpej 		blkno += nwrt;
   1326   1.80   tsutsui 		va = (char *)va + sectorsize * nwrt;
   1327    1.1       cgd 	}
   1328   1.22   thorpej 	rddoingadump = 0;
   1329   1.78   tsutsui 	return 0;
   1330    1.1       cgd }
   1331