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