rd.c revision 1.36
1/*	$NetBSD: rd.c,v 1.36 1998/01/12 18:31:06 thorpej 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
108int	rderrthresh = RDRETRY-1;	/* when to start reporting errors */
109
110#ifdef DEBUG
111/* error message tables */
112char *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
127char *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
145char *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
161char *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
178int	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 */
191struct 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};
249int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
250
251bdev_decl(rd);
252cdev_decl(rd);
253
254int	rdident __P((struct device *, struct rd_softc *,
255	    struct hpibbus_attach_args *));
256void	rdreset __P((struct rd_softc *));
257void	rdustart __P((struct rd_softc *));
258int	rdgetinfo __P((dev_t));
259void	rdrestart __P((void *));
260struct buf *rdfinish __P((struct rd_softc *, struct buf *));
261
262void	rdrestart __P((void *));
263void	rdustart __P((struct rd_softc *));
264struct buf *rdfinish __P((struct rd_softc *, struct buf *));
265void	rdstart __P((void *));
266void	rdgo __P((void *));
267void	rdintr __P((void *));
268int	rdstatus __P((struct rd_softc *));
269int	rderror __P((int));
270#ifdef DEBUG
271void	rdprinterr __P((char *, short, char **));
272#endif
273
274int	rdmatch __P((struct device *, struct cfdata *, void *));
275void	rdattach __P((struct device *, struct device *, void *));
276
277struct cfattach rd_ca = {
278	sizeof(struct rd_softc), rdmatch, rdattach
279};
280
281extern struct cfdriver rd_cd;
282
283int
284rdmatch(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
312void
313rdattach(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	if (rdident(parent, sc, ha) == 0) {
321		printf("\n%s: didn't respond to describe command!\n",
322		    sc->sc_dev.dv_xname);
323		return;
324	}
325
326	/*
327	 * Initialize and attach the disk structure.
328	 */
329	bzero(&sc->sc_dkdev, sizeof(sc->sc_dkdev));
330	sc->sc_dkdev.dk_name = sc->sc_dev.dv_xname;
331	disk_attach(&sc->sc_dkdev);
332
333	sc->sc_slave = ha->ha_slave;
334	sc->sc_punit = ha->ha_punit;
335
336	/* Initialize the hpib job queue entry */
337	sc->sc_hq.hq_softc = sc;
338	sc->sc_hq.hq_slave = sc->sc_slave;
339	sc->sc_hq.hq_start = rdstart;
340	sc->sc_hq.hq_go = rdgo;
341	sc->sc_hq.hq_intr = rdintr;
342
343	sc->sc_flags = RDF_ALIVE;
344#ifdef DEBUG
345	/* always report errors */
346	if (rddebug & RDB_ERROR)
347		rderrthresh = 0;
348#endif
349}
350
351int
352rdident(parent, sc, ha)
353	struct device *parent;
354	struct rd_softc *sc;
355	struct hpibbus_attach_args *ha;
356{
357	struct rd_describe *desc = sc != NULL ? &sc->sc_rddesc : NULL;
358	u_char stat, cmd[3];
359	char name[7];
360	int i, id, n, ctlr, slave;
361
362	ctlr = parent->dv_unit;
363	slave = ha->ha_slave;
364
365	/* Verify that we have a CS80 device. */
366	if ((ha->ha_id & 0x200) == 0)
367		return (0);
368
369	/* Is it one of the disks we support? */
370	for (id = 0; id < numrdidentinfo; id++)
371		if (ha->ha_id == rdidentinfo[id].ri_hwid)
372			break;
373	if (id == numrdidentinfo || ha->ha_punit > rdidentinfo[id].ri_maxunum)
374		return (0);
375
376	/*
377	 * If we're just probing for the device, that's all the
378	 * work we need to do.
379	 */
380	if (sc == NULL)
381		return (1);
382
383	/*
384	 * Reset device and collect description
385	 */
386	rdreset(sc);
387	cmd[0] = C_SUNIT(ha->ha_punit);
388	cmd[1] = C_SVOL(0);
389	cmd[2] = C_DESC;
390	hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
391	hpibrecv(ctlr, slave, C_EXEC, desc, 37);
392	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
393	bzero(name, sizeof(name));
394	if (stat == 0) {
395		n = desc->d_name;
396		for (i = 5; i >= 0; i--) {
397			name[i] = (n & 0xf) + '0';
398			n >>= 4;
399		}
400	}
401
402#ifdef DEBUG
403	if (rddebug & RDB_IDENT) {
404		printf("\n%s: name: %x ('%s')\n",
405		    sc->sc_dev.dv_xname, desc->d_name, name);
406		printf("  iuw %x, maxxfr %d, ctype %d\n",
407		    desc->d_iuw, desc->d_cmaxxfr, desc->d_ctype);
408		printf("  utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
409		    desc->d_utype, desc->d_sectsize,
410		    desc->d_blkbuf, desc->d_burstsize, desc->d_blocktime);
411		printf("  avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
412		    desc->d_uavexfr, desc->d_retry, desc->d_access,
413		    desc->d_maxint, desc->d_fvbyte, desc->d_rvbyte);
414		printf("  maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
415		    desc->d_maxcyl, desc->d_maxhead, desc->d_maxsect,
416		    desc->d_maxvsectl, desc->d_interleave);
417		printf("%s", sc->sc_dev.dv_xname);
418	}
419#endif
420
421	/*
422	 * Take care of a couple of anomolies:
423	 * 1. 7945A and 7946A both return same HW id
424	 * 2. 9122S and 9134D both return same HW id
425	 * 3. 9122D and 9134L both return same HW id
426	 */
427	switch (ha->ha_id) {
428	case RD7946AID:
429		if (bcmp(name, "079450", 6) == 0)
430			id = RD7945A;
431		else
432			id = RD7946A;
433		break;
434
435	case RD9134LID:
436		if (bcmp(name, "091340", 6) == 0)
437			id = RD9134L;
438		else
439			id = RD9122D;
440		break;
441
442	case RD9134DID:
443		if (bcmp(name, "091220", 6) == 0)
444			id = RD9122S;
445		else
446			id = RD9134D;
447		break;
448	}
449
450	sc->sc_type = id;
451
452	/*
453	 * XXX We use DEV_BSIZE instead of the sector size value pulled
454	 * XXX off the driver because all of this code assumes 512 byte
455	 * XXX blocks.  ICK!
456	 */
457	printf(": %s\n", rdidentinfo[id].ri_desc);
458	printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
459	    sc->sc_dev.dv_xname, rdidentinfo[id].ri_ncyl,
460	    rdidentinfo[id].ri_ntpc, rdidentinfo[id].ri_nblocks,
461	    DEV_BSIZE);
462
463	return (1);
464}
465
466void
467rdreset(rs)
468	struct rd_softc *rs;
469{
470	int ctlr = rs->sc_dev.dv_parent->dv_unit;
471	int slave = rs->sc_slave;
472	u_char stat;
473
474	rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
475	rs->sc_clear.c_cmd = C_CLEAR;
476	hpibsend(ctlr, slave, C_TCMD, &rs->sc_clear, sizeof(rs->sc_clear));
477	hpibswait(ctlr, slave);
478	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
479
480	rs->sc_src.c_unit = C_SUNIT(RDCTLR);
481	rs->sc_src.c_nop = C_NOP;
482	rs->sc_src.c_cmd = C_SREL;
483	rs->sc_src.c_param = C_REL;
484	hpibsend(ctlr, slave, C_CMD, &rs->sc_src, sizeof(rs->sc_src));
485	hpibswait(ctlr, slave);
486	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
487
488	rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
489	rs->sc_ssmc.c_cmd = C_SSM;
490	rs->sc_ssmc.c_refm = REF_MASK;
491	rs->sc_ssmc.c_fefm = FEF_MASK;
492	rs->sc_ssmc.c_aefm = AEF_MASK;
493	rs->sc_ssmc.c_iefm = IEF_MASK;
494	hpibsend(ctlr, slave, C_CMD, &rs->sc_ssmc, sizeof(rs->sc_ssmc));
495	hpibswait(ctlr, slave);
496	hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
497#ifdef DEBUG
498	rs->sc_stats.rdresets++;
499#endif
500}
501
502/*
503 * Read or constuct a disklabel
504 */
505int
506rdgetinfo(dev)
507	dev_t dev;
508{
509	int unit = rdunit(dev);
510	struct rd_softc *rs = rd_cd.cd_devs[unit];
511	struct disklabel *lp = rs->sc_dkdev.dk_label;
512	struct partition *pi;
513	char *msg;
514
515	/*
516	 * Set some default values to use while reading the label
517	 * or to use if there isn't a label.
518	 */
519	bzero((caddr_t)lp, sizeof *lp);
520	lp->d_type = DTYPE_HPIB;
521	lp->d_secsize = DEV_BSIZE;
522	lp->d_nsectors = 32;
523	lp->d_ntracks = 20;
524	lp->d_ncylinders = 1;
525	lp->d_secpercyl = 32*20;
526	lp->d_npartitions = 3;
527	lp->d_partitions[2].p_offset = 0;
528	lp->d_partitions[2].p_size = LABELSECTOR+1;
529
530	/*
531	 * Now try to read the disklabel
532	 */
533	msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
534	if (msg == NULL)
535		return (0);
536
537	pi = lp->d_partitions;
538	printf("%s: WARNING: %s, ", rs->sc_dev.dv_xname, msg);
539#ifdef COMPAT_NOLABEL
540	printf("using old default partitioning\n");
541	rdmakedisklabel(unit, lp);
542#else
543	printf("defining `c' partition as entire disk\n");
544	pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
545	/* XXX reset other info since readdisklabel screws with it */
546	lp->d_npartitions = 3;
547	pi[0].p_size = 0;
548#endif
549	return(0);
550}
551
552int
553rdopen(dev, flags, mode, p)
554	dev_t dev;
555	int flags, mode;
556	struct proc *p;
557{
558	int unit = rdunit(dev);
559	struct rd_softc *rs;
560	int error, mask, part;
561
562	if (unit >= rd_cd.cd_ndevs ||
563	    (rs = rd_cd.cd_devs[unit]) == NULL ||
564	    (rs->sc_flags & RDF_ALIVE) == 0)
565		return (ENXIO);
566
567	/*
568	 * Wait for any pending opens/closes to complete
569	 */
570	while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
571		sleep((caddr_t)rs, PRIBIO);
572
573	/*
574	 * On first open, get label and partition info.
575	 * We may block reading the label, so be careful
576	 * to stop any other opens.
577	 */
578	if (rs->sc_dkdev.dk_openmask == 0) {
579		rs->sc_flags |= RDF_OPENING;
580		error = rdgetinfo(dev);
581		rs->sc_flags &= ~RDF_OPENING;
582		wakeup((caddr_t)rs);
583		if (error)
584			return(error);
585	}
586
587	part = rdpart(dev);
588	mask = 1 << part;
589
590	/* Check that the partition exists. */
591	if (part != RAW_PART &&
592	    (part > rs->sc_dkdev.dk_label->d_npartitions ||
593	     rs->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
594		return (ENXIO);
595
596	/* Ensure only one open at a time. */
597	switch (mode) {
598	case S_IFCHR:
599		rs->sc_dkdev.dk_copenmask |= mask;
600		break;
601	case S_IFBLK:
602		rs->sc_dkdev.dk_bopenmask |= mask;
603		break;
604	}
605	rs->sc_dkdev.dk_openmask =
606	    rs->sc_dkdev.dk_copenmask | rs->sc_dkdev.dk_bopenmask;
607
608	return(0);
609}
610
611int
612rdclose(dev, flag, mode, p)
613	dev_t dev;
614	int flag, mode;
615	struct proc *p;
616{
617	int unit = rdunit(dev);
618	struct rd_softc *rs = rd_cd.cd_devs[unit];
619	struct disk *dk = &rs->sc_dkdev;
620	int mask, s;
621
622	mask = 1 << rdpart(dev);
623	if (mode == S_IFCHR)
624		dk->dk_copenmask &= ~mask;
625	else
626		dk->dk_bopenmask &= ~mask;
627	dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
628	/*
629	 * On last close, we wait for all activity to cease since
630	 * the label/parition info will become invalid.  Since we
631	 * might sleep, we must block any opens while we are here.
632	 * Note we don't have to about other closes since we know
633	 * we are the last one.
634	 */
635	if (dk->dk_openmask == 0) {
636		rs->sc_flags |= RDF_CLOSING;
637		s = splbio();
638		while (rs->sc_tab.b_active) {
639			rs->sc_flags |= RDF_WANTED;
640			sleep((caddr_t)&rs->sc_tab, PRIBIO);
641		}
642		splx(s);
643		rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
644		wakeup((caddr_t)rs);
645	}
646	return(0);
647}
648
649void
650rdstrategy(bp)
651	struct buf *bp;
652{
653	int unit = rdunit(bp->b_dev);
654	struct rd_softc *rs = rd_cd.cd_devs[unit];
655	struct buf *dp = &rs->sc_tab;
656	struct partition *pinfo;
657	daddr_t bn;
658	int sz, s;
659	int offset;
660
661#ifdef DEBUG
662	if (rddebug & RDB_FOLLOW)
663		printf("rdstrategy(%p): dev %x, bn %x, bcount %lx, %c\n",
664		       bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
665		       (bp->b_flags & B_READ) ? 'R' : 'W');
666#endif
667	bn = bp->b_blkno;
668	sz = howmany(bp->b_bcount, DEV_BSIZE);
669	pinfo = &rs->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
670
671	/* Don't perform partition translation on RAW_PART. */
672	offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
673
674	if (rdpart(bp->b_dev) != RAW_PART) {
675		/*
676		 * XXX This block of code belongs in
677		 * XXX bounds_check_with_label()
678		 */
679
680		if (bn < 0 || bn + sz > pinfo->p_size) {
681			sz = pinfo->p_size - bn;
682			if (sz == 0) {
683				bp->b_resid = bp->b_bcount;
684				goto done;
685			}
686			if (sz < 0) {
687				bp->b_error = EINVAL;
688				goto bad;
689			}
690			bp->b_bcount = dbtob(sz);
691		}
692		/*
693		 * Check for write to write protected label
694		 */
695		if (bn + offset <= LABELSECTOR &&
696#if LABELSECTOR != 0
697		    bn + offset + sz > LABELSECTOR &&
698#endif
699		    !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
700			bp->b_error = EROFS;
701			goto bad;
702		}
703	}
704	bp->b_cylin = bn + offset;
705	s = splbio();
706	disksort(dp, bp);
707	if (dp->b_active == 0) {
708		dp->b_active = 1;
709		rdustart(rs);
710	}
711	splx(s);
712	return;
713bad:
714	bp->b_flags |= B_ERROR;
715done:
716	biodone(bp);
717}
718
719/*
720 * Called from timeout() when handling maintenance releases
721 */
722void
723rdrestart(arg)
724	void *arg;
725{
726	int s = splbio();
727	rdustart((struct rd_softc *)arg);
728	splx(s);
729}
730
731void
732rdustart(rs)
733	struct rd_softc *rs;
734{
735	struct buf *bp;
736
737	bp = rs->sc_tab.b_actf;
738	rs->sc_addr = bp->b_un.b_addr;
739	rs->sc_resid = bp->b_bcount;
740	if (hpibreq(rs->sc_dev.dv_parent, &rs->sc_hq))
741		rdstart(rs);
742}
743
744struct buf *
745rdfinish(rs, bp)
746	struct rd_softc *rs;
747	struct buf *bp;
748{
749	struct buf *dp = &rs->sc_tab;
750
751	dp->b_errcnt = 0;
752	dp->b_actf = bp->b_actf;
753	bp->b_resid = 0;
754	biodone(bp);
755	hpibfree(rs->sc_dev.dv_parent, &rs->sc_hq);
756	if (dp->b_actf)
757		return (dp->b_actf);
758	dp->b_active = 0;
759	if (rs->sc_flags & RDF_WANTED) {
760		rs->sc_flags &= ~RDF_WANTED;
761		wakeup((caddr_t)dp);
762	}
763	return (NULL);
764}
765
766void
767rdstart(arg)
768	void *arg;
769{
770	struct rd_softc *rs = arg;
771	struct buf *bp = rs->sc_tab.b_actf;
772	int part, ctlr, slave;
773
774	ctlr = rs->sc_dev.dv_parent->dv_unit;
775	slave = rs->sc_slave;
776
777again:
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_cylin);
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_tab.b_errcnt);
825	rs->sc_stats.rdretries++;
826#endif
827	rs->sc_flags &= ~RDF_SEEK;
828	rdreset(rs);
829	if (rs->sc_tab.b_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
845void
846rdgo(arg)
847	void *arg;
848{
849	struct rd_softc *rs = arg;
850	struct buf *bp = rs->sc_tab.b_actf;
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 */
868void
869rdintr(arg)
870	void *arg;
871{
872	struct rd_softc *rs = arg;
873	int unit = rs->sc_dev.dv_unit;
874	struct buf *bp = rs->sc_tab.b_actf;
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_tab.b_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
937int
938rdstatus(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 */
987int
988rderror(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_tab.b_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_tab.b_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 = rs->sc_tab.b_actf;
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
1101int
1102rdread(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
1111int
1112rdwrite(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
1121int
1122rdioctl(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
1181int
1182rdsize(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
1212void
1213rdprinterr(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
1232static int rddoingadump;	/* simple mutex */
1233
1234/*
1235 * Non-interrupt driven, non-dma dump routine.
1236 */
1237int
1238rddump(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