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