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