rd.c revision 1.112 1 /* $NetBSD: rd.c,v 1.112 2022/11/23 18:53:22 tsutsui 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.112 2022/11/23 18:53:22 tsutsui 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/rndsource.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 #define RDB_FOLLOW 0x01
176 #define RDB_STATUS 0x02
177 #define RDB_IDENT 0x04
178 #define RDB_IO 0x08
179 #define RDB_ASYNC 0x10
180 #define RDB_ERROR 0x80
181 int rddebug = RDB_ERROR | RDB_IDENT;
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 [RD7945A] = {
190 .ri_hwid = RD7946AID,
191 .ri_desc = "7945A",
192 .ri_nbpt = NRD7945ABPT,
193 .ri_ntpc = NRD7945ATRK,
194 .ri_ncyl = NRD7945ACYL,
195 .ri_nblocks = NRD7945ABLK
196 },
197
198 [RD9134D] = {
199 .ri_hwid = RD9134DID,
200 .ri_desc = "9134D",
201 .ri_nbpt = NRD9134DBPT,
202 .ri_ntpc = NRD9134DTRK,
203 .ri_ncyl = NRD9134DCYL,
204 .ri_nblocks = NRD9134DBLK
205 },
206
207 [RD9122S] = {
208 .ri_hwid = RD9134LID,
209 .ri_desc = "9122S",
210 .ri_nbpt = NRD9122SBPT,
211 .ri_ntpc = NRD9122STRK,
212 .ri_ncyl = NRD9122SCYL,
213 .ri_nblocks = NRD9122SBLK
214 },
215
216 [RD7912P] = {
217 .ri_hwid = RD7912PID,
218 .ri_desc = "7912P",
219 .ri_nbpt = NRD7912PBPT,
220 .ri_ntpc = NRD7912PTRK,
221 .ri_ncyl = NRD7912PCYL,
222 .ri_nblocks = NRD7912PBLK
223 },
224
225 [RD7914P] = {
226 .ri_hwid = RD7914PID,
227 .ri_desc = "7914P",
228 .ri_nbpt = NRD7914PBPT,
229 .ri_ntpc = NRD7914PTRK,
230 .ri_ncyl = NRD7914PCYL,
231 .ri_nblocks = NRD7914PBLK
232 },
233
234 [RD7958A] = {
235 .ri_hwid = RD7958AID,
236 .ri_desc = "7958A",
237 .ri_nbpt = NRD7958ABPT,
238 .ri_ntpc = NRD7958ATRK,
239 .ri_ncyl = NRD7958ACYL,
240 .ri_nblocks = NRD7958ABLK
241 },
242
243 [RD7957A] = {
244 .ri_hwid = RD7957AID,
245 .ri_desc = "7957A",
246 .ri_nbpt = NRD7957ABPT,
247 .ri_ntpc = NRD7957ATRK,
248 .ri_ncyl = NRD7957ACYL,
249 .ri_nblocks = NRD7957ABLK
250 },
251
252 [RD7933H] = {
253 .ri_hwid = RD7933HID,
254 .ri_desc = "7933H",
255 .ri_nbpt = NRD7933HBPT,
256 .ri_ntpc = NRD7933HTRK,
257 .ri_ncyl = NRD7933HCYL,
258 .ri_nblocks = NRD7933HBLK
259 },
260
261 [RD9134L] = {
262 .ri_hwid = RD9134LID,
263 .ri_desc = "9134L",
264 .ri_nbpt = NRD9134LBPT,
265 .ri_ntpc = NRD9134LTRK,
266 .ri_ncyl = NRD9134LCYL,
267 .ri_nblocks = NRD9134LBLK
268 },
269
270 [RD7936H] = {
271 .ri_hwid = RD7936HID,
272 .ri_desc = "7936H",
273 .ri_nbpt = NRD7936HBPT,
274 .ri_ntpc = NRD7936HTRK,
275 .ri_ncyl = NRD7936HCYL,
276 .ri_nblocks = NRD7936HBLK
277 },
278
279 [RD7937H] = {
280 .ri_hwid = RD7937HID,
281 .ri_desc = "7937H",
282 .ri_nbpt = NRD7937HBPT,
283 .ri_ntpc = NRD7937HTRK,
284 .ri_ncyl = NRD7937HCYL,
285 .ri_nblocks = NRD7937HBLK
286 },
287
288 [RD7914CT] = {
289 .ri_hwid = RD7914CTID,
290 .ri_desc = "7914CT",
291 .ri_nbpt = NRD7914PBPT,
292 .ri_ntpc = NRD7914PTRK,
293 .ri_ncyl = NRD7914PCYL,
294 .ri_nblocks = NRD7914PBLK
295 },
296
297 [RD7946A] = {
298 .ri_hwid = RD7946AID,
299 .ri_desc = "7946A",
300 .ri_nbpt = NRD7945ABPT,
301 .ri_ntpc = NRD7945ATRK,
302 .ri_ncyl = NRD7945ACYL,
303 .ri_nblocks = NRD7945ABLK
304 },
305
306 [RD9122D] = {
307 .ri_hwid = RD9134LID,
308 .ri_desc = "9122D",
309 .ri_nbpt = NRD9122SBPT,
310 .ri_ntpc = NRD9122STRK,
311 .ri_ncyl = NRD9122SCYL,
312 .ri_nblocks = NRD9122SBLK
313 },
314
315 [RD7957B] = {
316 .ri_hwid = RD7957BID,
317 .ri_desc = "7957B",
318 .ri_nbpt = NRD7957BBPT,
319 .ri_ntpc = NRD7957BTRK,
320 .ri_ncyl = NRD7957BCYL,
321 .ri_nblocks = NRD7957BBLK
322 },
323
324 [RD7958B] = {
325 .ri_hwid = RD7958BID,
326 .ri_desc = "7958B",
327 .ri_nbpt = NRD7958BBPT,
328 .ri_ntpc = NRD7958BTRK,
329 .ri_ncyl = NRD7958BCYL,
330 .ri_nblocks = NRD7958BBLK
331 },
332
333 [RD7959B] = {
334 .ri_hwid = RD7959BID,
335 .ri_desc = "7959B",
336 .ri_nbpt = NRD7959BBPT,
337 .ri_ntpc = NRD7959BTRK,
338 .ri_ncyl = NRD7959BCYL,
339 .ri_nblocks = NRD7959BBLK
340 },
341
342 [RD2200A] = {
343 .ri_hwid = RD2200AID,
344 .ri_desc = "2200A",
345 .ri_nbpt = NRD2200ABPT,
346 .ri_ntpc = NRD2200ATRK,
347 .ri_ncyl = NRD2200ACYL,
348 .ri_nblocks = NRD2200ABLK
349 },
350
351 [RD2203A] = {
352 .ri_hwid = RD2203AID,
353 .ri_desc = "2203A",
354 .ri_nbpt = NRD2203ABPT,
355 .ri_ntpc = NRD2203ATRK,
356 .ri_ncyl = NRD2203ACYL,
357 .ri_nblocks = NRD2203ABLK
358 },
359
360 [RD2202A] = {
361 .ri_hwid = RD2202AID,
362 .ri_desc = "2202A",
363 .ri_nbpt = NRD2202ABPT,
364 .ri_ntpc = NRD2202ATRK,
365 .ri_ncyl = NRD2202ACYL,
366 .ri_nblocks = NRD2202ABLK
367 },
368
369 [RD7908A] = {
370 .ri_hwid = RD7908AID,
371 .ri_desc = "7908A",
372 .ri_nbpt = NRD7908ABPT,
373 .ri_ntpc = NRD7908ATRK,
374 .ri_ncyl = NRD7908ACYL,
375 .ri_nblocks = NRD7908ABLK
376 },
377
378 [RD7911A] = {
379 .ri_hwid = RD7911AID,
380 .ri_desc = "7911A",
381 .ri_nbpt = NRD7911ABPT,
382 .ri_ntpc = NRD7911ATRK,
383 .ri_ncyl = NRD7911ACYL,
384 .ri_nblocks = NRD7911ABLK
385 },
386
387 [RD7941A] = {
388 .ri_hwid = RD7946AID,
389 .ri_desc = "7941A",
390 .ri_nbpt = NRD7941ABPT,
391 .ri_ntpc = NRD7941ATRK,
392 .ri_ncyl = NRD7941ACYL,
393 .ri_nblocks = NRD7941ABLK
394 }
395 };
396 static const int numrdidentinfo = __arraycount(rdidentinfo);
397
398 struct rdname2id {
399 const char *rn_name;
400 int rn_id;
401 };
402 static const struct rdname2id rdname2id[] = {
403 { RD7945ANAME, RD7945A },
404 { RD9134DNAME, RD9134D },
405 { RD7912PNAME, RD7912P },
406 { RD7914PNAME, RD7914P },
407 { RD7958ANAME, RD7958A },
408 { RD7957ANAME, RD7957A },
409 { RD7933HNAME, RD7933H },
410 { RD9134LNAME, RD9134L },
411 { RD7936HNAME, RD7936H },
412 { RD7937HNAME, RD7937H },
413 { RD7914CTNAME, RD7914CT },
414 { RD9122DNAME, RD9122D },
415 { RD7957BNAME, RD7957B },
416 { RD7958BNAME, RD7958B },
417 { RD7959BNAME, RD7959B },
418 { RD2200ANAME, RD2200A },
419 { RD2203ANAME, RD2203A },
420 { RD2202ANAME, RD2202A },
421 { RD7908ANAME, RD7908A },
422 { RD7911ANAME, RD7911A },
423 { RD7941ANAME, RD7941A }
424 };
425 static const int numrdname2id = __arraycount(rdname2id);
426
427 static int rdident(device_t, struct rd_softc *,
428 struct hpibbus_attach_args *);
429 static void rdreset(struct rd_softc *);
430 static void rdreset_unit(int, int, int);
431 static void rdustart(struct rd_softc *);
432 static int rdgetinfo(dev_t);
433 static void rdrestart(void *);
434 static struct buf *rdfinish(struct rd_softc *, struct buf *);
435
436 static void rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
437 static void rdrestart(void *);
438 static void rdustart(struct rd_softc *);
439 static struct buf *rdfinish(struct rd_softc *, struct buf *);
440 static void rdstart(void *);
441 static void rdgo(void *);
442 static void rdintr(void *);
443 static int rdstatus(struct rd_softc *);
444 static int rderror(int);
445 #ifdef DEBUG
446 static void rdprinterr(const char *, short, const char **);
447 #endif
448
449 static int rdmatch(device_t, cfdata_t, void *);
450 static void rdattach(device_t, device_t, void *);
451
452 CFATTACH_DECL_NEW(rd, sizeof(struct rd_softc),
453 rdmatch, rdattach, NULL, NULL);
454
455 static dev_type_open(rdopen);
456 static dev_type_close(rdclose);
457 static dev_type_read(rdread);
458 static dev_type_write(rdwrite);
459 static dev_type_ioctl(rdioctl);
460 static dev_type_strategy(rdstrategy);
461 static dev_type_dump(rddump);
462 static dev_type_size(rdsize);
463
464 const struct bdevsw rd_bdevsw = {
465 .d_open = rdopen,
466 .d_close = rdclose,
467 .d_strategy = rdstrategy,
468 .d_ioctl = rdioctl,
469 .d_dump = rddump,
470 .d_psize = rdsize,
471 .d_discard = nodiscard,
472 .d_flag = D_DISK
473 };
474
475 const struct cdevsw rd_cdevsw = {
476 .d_open = rdopen,
477 .d_close = rdclose,
478 .d_read = rdread,
479 .d_write = rdwrite,
480 .d_ioctl = rdioctl,
481 .d_stop = nostop,
482 .d_tty = notty,
483 .d_poll = nopoll,
484 .d_mmap = nommap,
485 .d_kqfilter = nokqfilter,
486 .d_discard = nodiscard,
487 .d_flag = D_DISK
488 };
489
490 static int
491 rdmatch(device_t parent, cfdata_t cf, void *aux)
492 {
493 struct hpibbus_attach_args *ha = aux;
494
495 return rdident(parent, NULL, ha);
496 }
497
498 static void
499 rdattach(device_t parent, device_t self, void *aux)
500 {
501 struct rd_softc *sc = device_private(self);
502 struct hpibbus_attach_args *ha = aux;
503 int id;
504 char pbuf[9];
505
506 sc->sc_dev = self;
507 bufq_alloc(&sc->sc_tab, "disksort", BUFQ_SORT_RAWBLOCK);
508
509 if (rdident(parent, sc, ha) == 0) {
510 aprint_error(": didn't respond to describe command!\n");
511 return;
512 }
513
514 /*
515 * XXX We use DEV_BSIZE instead of the sector size value pulled
516 * XXX off the driver because all of this code assumes 512 byte
517 * XXX blocks. ICK!
518 */
519 id = sc->sc_type;
520 aprint_normal(": %s\n", rdidentinfo[id].ri_desc);
521 format_bytes(pbuf, sizeof(pbuf),
522 rdidentinfo[id].ri_nblocks * DEV_BSIZE);
523 aprint_normal_dev(sc->sc_dev, "%s, %d cyl, %d head, %d sec,"
524 " %d bytes/block x %u blocks\n",
525 pbuf, rdidentinfo[id].ri_ncyl, rdidentinfo[id].ri_ntpc,
526 rdidentinfo[id].ri_nbpt,
527 DEV_BSIZE, rdidentinfo[id].ri_nblocks);
528
529 /*
530 * Initialize and attach the disk structure.
531 */
532 memset(&sc->sc_dkdev, 0, sizeof(sc->sc_dkdev));
533 disk_init(&sc->sc_dkdev, device_xname(sc->sc_dev), NULL);
534 disk_attach(&sc->sc_dkdev);
535
536 sc->sc_slave = ha->ha_slave;
537 sc->sc_punit = ha->ha_punit;
538
539 callout_init(&sc->sc_restart_ch, 0);
540
541 /* Initialize the hpib job queue entry */
542 sc->sc_hq.hq_softc = sc;
543 sc->sc_hq.hq_slave = sc->sc_slave;
544 sc->sc_hq.hq_start = rdstart;
545 sc->sc_hq.hq_go = rdgo;
546 sc->sc_hq.hq_intr = rdintr;
547
548 sc->sc_flags = RDF_ALIVE;
549 #ifdef DEBUG
550 /* always report errors */
551 if (rddebug & RDB_ERROR)
552 rderrthresh = 0;
553 #endif
554 /*
555 * attach the device into the random source list
556 */
557 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
558 RND_TYPE_DISK, RND_FLAG_DEFAULT);
559 }
560
561 static int
562 rdident(device_t parent, struct rd_softc *sc, struct hpibbus_attach_args *ha)
563 {
564 struct cs80_describe desc;
565 u_char stat, cmd[3];
566 char name[7];
567 int i, id, n, ctlr, slave;
568
569 ctlr = device_unit(parent);
570 slave = ha->ha_slave;
571
572 /* Verify that we have a CS80 device. */
573 if ((ha->ha_id & 0x200) == 0)
574 return 0;
575
576 /* Is it one of the disks we support? */
577 for (id = 0; id < numrdidentinfo; id++)
578 if (ha->ha_id == rdidentinfo[id].ri_hwid)
579 break;
580 if (id == numrdidentinfo)
581 return 0;
582
583 /*
584 * The supported dvice ID is probed.
585 * Check if the specified physical unit is actually supported
586 * by brandnew HP-IB emulator devices like HPDisk and HPDrive etc.
587 */
588 /*
589 * Reset device and collect description
590 */
591 memset(&desc, 0, sizeof(desc));
592 stat = 0;
593 rdreset_unit(ctlr, slave, ha->ha_punit);
594 cmd[0] = C_SUNIT(ha->ha_punit);
595 cmd[1] = C_SVOL(0);
596 cmd[2] = C_DESC;
597 hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
598 hpibrecv(ctlr, slave, C_EXEC, &desc, sizeof(desc));
599 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
600
601 if (stat != 0 || desc.d_name == 0) {
602 /*
603 * No valid response from the specified punit.
604 *
605 * Note it looks HPDisk responds to commands against
606 * supported but not-configured punits at 1 to 3.
607 */
608 return 0;
609 }
610
611 /*
612 * If we're just probing for the device, that's all the
613 * work we need to do.
614 */
615 if (sc == NULL)
616 return 1;
617
618 memset(name, 0, sizeof(name));
619 n = desc.d_name;
620 for (i = 5; i >= 0; i--) {
621 name[i] = (n & 0xf) + '0';
622 n >>= 4;
623 }
624
625 #ifdef DEBUG
626 if (rddebug & RDB_IDENT) {
627 aprint_normal("\n");
628 aprint_normal_dev(sc->sc_dev, "id: 0x%04x, name: %x ('%s')\n",
629 ha->ha_id, desc.d_name, name);
630 aprint_normal(" iuw %x, maxxfr %d, ctype %d\n",
631 desc.d_iuw, desc.d_cmaxxfr, desc.d_ctype);
632 aprint_normal(" utype %d, bps %d, blkbuf %d, burst %d,"
633 " blktime %d\n",
634 desc.d_utype, desc.d_sectsize,
635 desc.d_blkbuf, desc.d_burstsize, desc.d_blocktime);
636 aprint_normal(" avxfr %d, ort %d, atp %d, maxint %d, fv %x"
637 ", rv %x\n",
638 desc.d_uavexfr, desc.d_retry, desc.d_access,
639 desc.d_maxint, desc.d_fvbyte, desc.d_rvbyte);
640 aprint_normal(" maxcyl/head/sect %d/%d/%d, maxvsect %d,"
641 " inter %d\n",
642 desc.d_maxcyl, desc.d_maxhead, desc.d_maxsect,
643 desc.d_maxvsectl, desc.d_interleave);
644 aprint_normal("%s", device_xname(sc->sc_dev));
645 }
646 #endif
647
648 /*
649 * Take care of a couple of anomolies:
650 * 1. 7945A, 7946A, and 7941A all return same HW id
651 * 2. 9122S and 9134D both return same HW id
652 * 3. 9122D and 9134L both return same HW id
653 */
654 switch (ha->ha_id) {
655 case RD7946AID:
656 if (memcmp(name, RD7945ANAME, RDNAMELEN) == 0)
657 id = RD7945A;
658 else if (memcmp(name, RD7941ANAME, RDNAMELEN) == 0)
659 id = RD7941A;
660 else
661 id = RD7946A;
662 break;
663
664 case RD9134LID:
665 if (memcmp(name, RD9134LNAME, RDNAMELEN) == 0)
666 id = RD9134L;
667 else
668 id = RD9122D;
669 break;
670
671 case RD9134DID:
672 if (memcmp(name, RD9122SNAME, RDNAMELEN) == 0)
673 id = RD9122S;
674 else
675 id = RD9134D;
676 break;
677 }
678
679 /*
680 * HPDisk can have independent physical units that are not
681 * corresponding to device IDs.
682 * To handle this, we have to check names in the drive description
683 * data for punit >= 1.
684 */
685 if (ha->ha_punit >= 1) {
686 for (i = 0; i < numrdname2id; i++) {
687 if (memcmp(name, rdname2id[i].rn_name,
688 RDNAMELEN) == 0) {
689 id = rdname2id[i].rn_id;
690 break;
691 }
692 }
693 }
694
695 sc->sc_type = id;
696
697 return 1;
698 }
699
700 static void
701 rdreset(struct rd_softc *sc)
702 {
703 int ctlr, slave, punit;
704
705 ctlr = device_unit(device_parent(sc->sc_dev));
706 slave = sc->sc_slave;
707 punit = sc->sc_punit;
708 rdreset_unit(ctlr, slave, punit);
709 #ifdef DEBUG
710 sc->sc_stats.rdresets++;
711 #endif
712 }
713
714 static void
715 rdreset_unit(int ctlr, int slave, int punit)
716 {
717 struct rd_ssmcmd ssmc;
718 struct rd_srcmd src;
719 struct rd_clearcmd clear;
720 u_char stat;
721
722 clear.c_unit = C_SUNIT(punit);
723 clear.c_cmd = C_CLEAR;
724 hpibsend(ctlr, slave, C_TCMD, &clear, sizeof(clear));
725 hpibswait(ctlr, slave);
726 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
727
728 src.c_unit = C_SUNIT(RDCTLR);
729 src.c_nop = C_NOP;
730 src.c_cmd = C_SREL;
731 src.c_param = C_REL;
732 hpibsend(ctlr, slave, C_CMD, &src, sizeof(src));
733 hpibswait(ctlr, slave);
734 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
735
736 ssmc.c_unit = C_SUNIT(punit);
737 ssmc.c_cmd = C_SSM;
738 ssmc.c_refm = REF_MASK;
739 ssmc.c_fefm = FEF_MASK;
740 ssmc.c_aefm = AEF_MASK;
741 ssmc.c_iefm = IEF_MASK;
742 hpibsend(ctlr, slave, C_CMD, &ssmc, sizeof(ssmc));
743 hpibswait(ctlr, slave);
744 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
745 }
746
747 /*
748 * Read or construct a disklabel
749 */
750 static int
751 rdgetinfo(dev_t dev)
752 {
753 struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
754 struct disklabel *lp = sc->sc_dkdev.dk_label;
755 struct partition *pi;
756 const char *msg;
757
758 /*
759 * Set some default values to use while reading the label
760 * or to use if there isn't a label.
761 */
762 memset((void *)lp, 0, sizeof *lp);
763 rdgetdefaultlabel(sc, lp);
764
765 /*
766 * Now try to read the disklabel
767 */
768 msg = readdisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
769 if (msg == NULL)
770 return 0;
771
772 pi = lp->d_partitions;
773 printf("%s: WARNING: %s\n", device_xname(sc->sc_dev), msg);
774
775 pi[2].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
776 /* XXX reset other info since readdisklabel screws with it */
777 lp->d_npartitions = 3;
778 pi[0].p_size = 0;
779
780 return 0;
781 }
782
783 static int
784 rdopen(dev_t dev, int flags, int mode, struct lwp *l)
785 {
786 struct rd_softc *sc;
787 int error, mask, part;
788
789 sc = device_lookup_private(&rd_cd, rdunit(dev));
790 if (sc == NULL)
791 return ENXIO;
792
793 if ((sc->sc_flags & RDF_ALIVE) == 0)
794 return ENXIO;
795
796 /*
797 * Wait for any pending opens/closes to complete
798 */
799 while (sc->sc_flags & (RDF_OPENING|RDF_CLOSING))
800 (void) tsleep(sc, PRIBIO, "rdopen", 0);
801
802 /*
803 * On first open, get label and partition info.
804 * We may block reading the label, so be careful
805 * to stop any other opens.
806 */
807 if (sc->sc_dkdev.dk_openmask == 0) {
808 sc->sc_flags |= RDF_OPENING;
809 error = rdgetinfo(dev);
810 sc->sc_flags &= ~RDF_OPENING;
811 wakeup((void *)sc);
812 if (error)
813 return error;
814 }
815
816 part = rdpart(dev);
817 mask = 1 << part;
818
819 /* Check that the partition exists. */
820 if (part != RAW_PART &&
821 (part > sc->sc_dkdev.dk_label->d_npartitions ||
822 sc->sc_dkdev.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
823 return ENXIO;
824
825 /* Ensure only one open at a time. */
826 switch (mode) {
827 case S_IFCHR:
828 sc->sc_dkdev.dk_copenmask |= mask;
829 break;
830 case S_IFBLK:
831 sc->sc_dkdev.dk_bopenmask |= mask;
832 break;
833 }
834 sc->sc_dkdev.dk_openmask =
835 sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
836
837 return 0;
838 }
839
840 static int
841 rdclose(dev_t dev, int flag, int mode, struct lwp *l)
842 {
843 struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
844 struct disk *dk = &sc->sc_dkdev;
845 int mask, s;
846
847 mask = 1 << rdpart(dev);
848 if (mode == S_IFCHR)
849 dk->dk_copenmask &= ~mask;
850 else
851 dk->dk_bopenmask &= ~mask;
852 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
853 /*
854 * On last close, we wait for all activity to cease since
855 * the label/partition info will become invalid. Since we
856 * might sleep, we must block any opens while we are here.
857 * Note we don't have to about other closes since we know
858 * we are the last one.
859 */
860 if (dk->dk_openmask == 0) {
861 sc->sc_flags |= RDF_CLOSING;
862 s = splbio();
863 while (sc->sc_active) {
864 sc->sc_flags |= RDF_WANTED;
865 (void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
866 }
867 splx(s);
868 sc->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
869 wakeup((void *)sc);
870 }
871 return 0;
872 }
873
874 static void
875 rdstrategy(struct buf *bp)
876 {
877 struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(bp->b_dev));
878 struct partition *pinfo;
879 daddr_t bn;
880 int sz, s;
881 int offset;
882
883 #ifdef DEBUG
884 if (rddebug & RDB_FOLLOW)
885 printf("rdstrategy(%p): dev %"PRIx64", bn %llx, bcount %x, %c\n",
886 bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
887 (bp->b_flags & B_READ) ? 'R' : 'W');
888 #endif
889 bn = bp->b_blkno;
890 sz = howmany(bp->b_bcount, DEV_BSIZE);
891 pinfo = &sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)];
892
893 /* Don't perform partition translation on RAW_PART. */
894 offset = (rdpart(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
895
896 if (rdpart(bp->b_dev) != RAW_PART) {
897 /*
898 * XXX This block of code belongs in
899 * XXX bounds_check_with_label()
900 */
901
902 if (bn < 0 || bn + sz > pinfo->p_size) {
903 sz = pinfo->p_size - bn;
904 if (sz == 0) {
905 bp->b_resid = bp->b_bcount;
906 goto done;
907 }
908 if (sz < 0) {
909 bp->b_error = EINVAL;
910 goto done;
911 }
912 bp->b_bcount = dbtob(sz);
913 }
914 /*
915 * Check for write to write protected label
916 */
917 if (bn + offset <= LABELSECTOR &&
918 #if LABELSECTOR != 0
919 bn + offset + sz > LABELSECTOR &&
920 #endif
921 !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
922 bp->b_error = EROFS;
923 goto done;
924 }
925 }
926 bp->b_rawblkno = bn + offset;
927 s = splbio();
928 bufq_put(sc->sc_tab, bp);
929 if (sc->sc_active == 0) {
930 sc->sc_active = 1;
931 rdustart(sc);
932 }
933 splx(s);
934 return;
935 done:
936 biodone(bp);
937 }
938
939 /*
940 * Called from timeout() when handling maintenance releases
941 */
942 static void
943 rdrestart(void *arg)
944 {
945 int s = splbio();
946 rdustart((struct rd_softc *)arg);
947 splx(s);
948 }
949
950 static void
951 rdustart(struct rd_softc *sc)
952 {
953 struct buf *bp;
954
955 bp = bufq_peek(sc->sc_tab);
956 sc->sc_addr = bp->b_data;
957 sc->sc_resid = bp->b_bcount;
958 if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
959 rdstart(sc);
960 }
961
962 static struct buf *
963 rdfinish(struct rd_softc *sc, struct buf *bp)
964 {
965
966 sc->sc_errcnt = 0;
967 (void)bufq_get(sc->sc_tab);
968 bp->b_resid = 0;
969 biodone(bp);
970 hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
971 if ((bp = bufq_peek(sc->sc_tab)) != NULL)
972 return bp;
973 sc->sc_active = 0;
974 if (sc->sc_flags & RDF_WANTED) {
975 sc->sc_flags &= ~RDF_WANTED;
976 wakeup((void *)&sc->sc_tab);
977 }
978 return NULL;
979 }
980
981 static void
982 rdstart(void *arg)
983 {
984 struct rd_softc *sc = arg;
985 struct buf *bp = bufq_peek(sc->sc_tab);
986 int ctlr, slave;
987
988 ctlr = device_unit(device_parent(sc->sc_dev));
989 slave = sc->sc_slave;
990
991 again:
992 #ifdef DEBUG
993 if (rddebug & RDB_FOLLOW)
994 printf("rdstart(%s): bp %p, %c\n", device_xname(sc->sc_dev), bp,
995 (bp->b_flags & B_READ) ? 'R' : 'W');
996 #endif
997 sc->sc_flags |= RDF_SEEK;
998 sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
999 sc->sc_ioc.c_volume = C_SVOL(0);
1000 sc->sc_ioc.c_saddr = C_SADDR;
1001 sc->sc_ioc.c_hiaddr = 0;
1002 sc->sc_ioc.c_addr = RDBTOS(bp->b_rawblkno);
1003 sc->sc_ioc.c_nop2 = C_NOP;
1004 sc->sc_ioc.c_slen = C_SLEN;
1005 sc->sc_ioc.c_len = sc->sc_resid;
1006 sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
1007 #ifdef DEBUG
1008 if (rddebug & RDB_IO)
1009 printf("rdstart: hpibsend(%x, %x, %x, %p, %x)\n",
1010 ctlr, slave, C_CMD,
1011 &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc) - 2);
1012 #endif
1013 if (hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
1014 sizeof(sc->sc_ioc) - 2) == sizeof(sc->sc_ioc) - 2) {
1015
1016 /* Instrumentation. */
1017 disk_busy(&sc->sc_dkdev);
1018 iostat_seek(sc->sc_dkdev.dk_stats);
1019
1020 #ifdef DEBUG
1021 if (rddebug & RDB_IO)
1022 printf("rdstart: hpibawait(%x)\n", ctlr);
1023 #endif
1024 hpibawait(ctlr);
1025 return;
1026 }
1027 /*
1028 * Experience has shown that the hpibwait in this hpibsend will
1029 * occasionally timeout. It appears to occur mostly on old 7914
1030 * drives with full maintenance tracks. We should probably
1031 * integrate this with the backoff code in rderror.
1032 */
1033 #ifdef DEBUG
1034 if (rddebug & RDB_ERROR)
1035 printf("%s: rdstart: cmd %x adr %lx blk %lld len %d ecnt %d\n",
1036 device_xname(sc->sc_dev),
1037 sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
1038 bp->b_blkno, sc->sc_resid, sc->sc_errcnt);
1039 sc->sc_stats.rdretries++;
1040 #endif
1041 sc->sc_flags &= ~RDF_SEEK;
1042 rdreset(sc);
1043 if (sc->sc_errcnt++ < RDRETRY)
1044 goto again;
1045 printf("%s: rdstart err: cmd 0x%x sect %ld blk %" PRId64 " len %d\n",
1046 device_xname(sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
1047 bp->b_blkno, sc->sc_resid);
1048 bp->b_error = EIO;
1049 bp = rdfinish(sc, bp);
1050 if (bp) {
1051 sc->sc_addr = bp->b_data;
1052 sc->sc_resid = bp->b_bcount;
1053 if (hpibreq(device_parent(sc->sc_dev), &sc->sc_hq))
1054 goto again;
1055 }
1056 }
1057
1058 static void
1059 rdgo(void *arg)
1060 {
1061 struct rd_softc *sc = arg;
1062 struct buf *bp = bufq_peek(sc->sc_tab);
1063 int rw, ctlr, slave;
1064
1065 ctlr = device_unit(device_parent(sc->sc_dev));
1066 slave = sc->sc_slave;
1067
1068 rw = bp->b_flags & B_READ;
1069
1070 /* Instrumentation. */
1071 disk_busy(&sc->sc_dkdev);
1072
1073 #ifdef USELEDS
1074 ledcontrol(0, 0, LED_DISK);
1075 #endif
1076 hpibgo(ctlr, slave, C_EXEC, sc->sc_addr, sc->sc_resid, rw, rw != 0);
1077 }
1078
1079 /* ARGSUSED */
1080 static void
1081 rdintr(void *arg)
1082 {
1083 struct rd_softc *sc = arg;
1084 int unit = device_unit(sc->sc_dev);
1085 struct buf *bp = bufq_peek(sc->sc_tab);
1086 u_char stat = 13; /* in case hpibrecv fails */
1087 int rv, restart, ctlr, slave;
1088
1089 ctlr = device_unit(device_parent(sc->sc_dev));
1090 slave = sc->sc_slave;
1091
1092 #ifdef DEBUG
1093 if (rddebug & RDB_FOLLOW)
1094 printf("rdintr(%d): bp %p, %c, flags %x\n", unit, bp,
1095 (bp->b_flags & B_READ) ? 'R' : 'W', sc->sc_flags);
1096 if (bp == NULL) {
1097 printf("%s: bp == NULL\n", device_xname(sc->sc_dev));
1098 return;
1099 }
1100 #endif
1101 disk_unbusy(&sc->sc_dkdev, (bp->b_bcount - bp->b_resid),
1102 (bp->b_flags & B_READ));
1103
1104 if (sc->sc_flags & RDF_SEEK) {
1105 sc->sc_flags &= ~RDF_SEEK;
1106 if (hpibustart(ctlr))
1107 rdgo(sc);
1108 return;
1109 }
1110 if ((sc->sc_flags & RDF_SWAIT) == 0) {
1111 #ifdef DEBUG
1112 sc->sc_stats.rdpolltries++;
1113 #endif
1114 if (hpibpptest(ctlr, slave) == 0) {
1115 #ifdef DEBUG
1116 sc->sc_stats.rdpollwaits++;
1117 #endif
1118
1119 /* Instrumentation. */
1120 disk_busy(&sc->sc_dkdev);
1121 sc->sc_flags |= RDF_SWAIT;
1122 hpibawait(ctlr);
1123 return;
1124 }
1125 } else
1126 sc->sc_flags &= ~RDF_SWAIT;
1127 rv = hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
1128 if (rv != 1 || stat) {
1129 #ifdef DEBUG
1130 if (rddebug & RDB_ERROR)
1131 printf("rdintr: recv failed or bad stat %d\n", stat);
1132 #endif
1133 restart = rderror(unit);
1134 #ifdef DEBUG
1135 sc->sc_stats.rdretries++;
1136 #endif
1137 if (sc->sc_errcnt++ < RDRETRY) {
1138 if (restart)
1139 rdstart(sc);
1140 return;
1141 }
1142 bp->b_error = EIO;
1143 }
1144 if (rdfinish(sc, bp))
1145 rdustart(sc);
1146 rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
1147 }
1148
1149 static int
1150 rdstatus(struct rd_softc *sc)
1151 {
1152 int c, s;
1153 u_char stat;
1154 int rv;
1155
1156 c = device_unit(device_parent(sc->sc_dev));
1157 s = sc->sc_slave;
1158 sc->sc_rsc.c_unit = C_SUNIT(sc->sc_punit);
1159 sc->sc_rsc.c_sram = C_SRAM;
1160 sc->sc_rsc.c_ram = C_RAM;
1161 sc->sc_rsc.c_cmd = C_STATUS;
1162 memset((void *)&sc->sc_stat, 0, sizeof(sc->sc_stat));
1163 rv = hpibsend(c, s, C_CMD, &sc->sc_rsc, sizeof(sc->sc_rsc));
1164 if (rv != sizeof(sc->sc_rsc)) {
1165 #ifdef DEBUG
1166 if (rddebug & RDB_STATUS)
1167 printf("rdstatus: send C_CMD failed %d != %d\n",
1168 rv, sizeof(sc->sc_rsc));
1169 #endif
1170 return 1;
1171 }
1172 rv = hpibrecv(c, s, C_EXEC, &sc->sc_stat, sizeof(sc->sc_stat));
1173 if (rv != sizeof(sc->sc_stat)) {
1174 #ifdef DEBUG
1175 if (rddebug & RDB_STATUS)
1176 printf("rdstatus: send C_EXEC failed %d != %d\n",
1177 rv, sizeof(sc->sc_stat));
1178 #endif
1179 return 1;
1180 }
1181 rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
1182 if (rv != 1 || stat) {
1183 #ifdef DEBUG
1184 if (rddebug & RDB_STATUS)
1185 printf("rdstatus: recv failed %d or bad stat %d\n",
1186 rv, stat);
1187 #endif
1188 return 1;
1189 }
1190 return 0;
1191 }
1192
1193 /*
1194 * Deal with errors.
1195 * Returns 1 if request should be restarted,
1196 * 0 if we should just quietly give up.
1197 */
1198 static int
1199 rderror(int unit)
1200 {
1201 struct rd_softc *sc = device_lookup_private(&rd_cd,unit);
1202 struct rd_stat *sp;
1203 struct buf *bp;
1204 daddr_t hwbn, pbn;
1205 char *hexstr(int, int); /* XXX */
1206
1207 if (rdstatus(sc)) {
1208 #ifdef DEBUG
1209 printf("%s: couldn't get status\n", device_xname(sc->sc_dev));
1210 #endif
1211 rdreset(sc);
1212 return 1;
1213 }
1214 sp = &sc->sc_stat;
1215 if (sp->c_fef & FEF_REXMT)
1216 return 1;
1217 if (sp->c_fef & FEF_PF) {
1218 rdreset(sc);
1219 return 1;
1220 }
1221 /*
1222 * Unit requests release for internal maintenance.
1223 * We just delay awhile and try again later. Use expontially
1224 * increasing backoff ala ethernet drivers since we don't really
1225 * know how long the maintenance will take. With RDWAITC and
1226 * RDRETRY as defined, the range is 1 to 32 seconds.
1227 */
1228 if (sp->c_fef & FEF_IMR) {
1229 extern int hz;
1230 int rdtimo = RDWAITC << sc->sc_errcnt;
1231 #ifdef DEBUG
1232 printf("%s: internal maintenance, %d second timeout\n",
1233 device_xname(sc->sc_dev), rdtimo);
1234 sc->sc_stats.rdtimeouts++;
1235 #endif
1236 hpibfree(device_parent(sc->sc_dev), &sc->sc_hq);
1237 callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
1238 return 0;
1239 }
1240 /*
1241 * Only report error if we have reached the error reporting
1242 * threshold. By default, this will only report after the
1243 * retry limit has been exceeded.
1244 */
1245 if (sc->sc_errcnt < rderrthresh)
1246 return 1;
1247
1248 /*
1249 * First conjure up the block number at which the error occurred.
1250 * Note that not all errors report a block number, in that case
1251 * we just use b_blkno.
1252 */
1253 bp = bufq_peek(sc->sc_tab);
1254 pbn = sc->sc_dkdev.dk_label->d_partitions[rdpart(bp->b_dev)].p_offset;
1255 if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
1256 (sp->c_ief & IEF_RRMASK)) {
1257 hwbn = RDBTOS(pbn + bp->b_blkno);
1258 pbn = bp->b_blkno;
1259 } else {
1260 hwbn = sp->c_blk;
1261 pbn = RDSTOB(hwbn) - pbn;
1262 }
1263 /*
1264 * Now output a generic message suitable for badsect.
1265 * Note that we don't use harderr cuz it just prints
1266 * out b_blkno which is just the beginning block number
1267 * of the transfer, not necessary where the error occurred.
1268 */
1269 printf("%s%c: hard error sn%" PRId64 "\n", device_xname(sc->sc_dev),
1270 'a'+rdpart(bp->b_dev), pbn);
1271 /*
1272 * Now report the status as returned by the hardware with
1273 * attempt at interpretation (unless debugging).
1274 */
1275 printf("%s %s error:", device_xname(sc->sc_dev),
1276 (bp->b_flags & B_READ) ? "read" : "write");
1277 #ifdef DEBUG
1278 if (rddebug & RDB_ERROR) {
1279 /* status info */
1280 printf("\n volume: %d, unit: %d\n",
1281 (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
1282 rdprinterr("reject", sp->c_ref, err_reject);
1283 rdprinterr("fault", sp->c_fef, err_fault);
1284 rdprinterr("access", sp->c_aef, err_access);
1285 rdprinterr("info", sp->c_ief, err_info);
1286 printf(" block: %lld, P1-P10: ", hwbn);
1287 printf("0x%x", *(u_int *)&sp->c_raw[0]);
1288 printf("0x%x", *(u_int *)&sp->c_raw[4]);
1289 printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1290 /* command */
1291 printf(" ioc: ");
1292 printf("0x%x", *(u_int *)&sc->sc_ioc.c_pad);
1293 printf("0x%x", *(u_short *)&sc->sc_ioc.c_hiaddr);
1294 printf("0x%x", *(u_int *)&sc->sc_ioc.c_addr);
1295 printf("0x%x", *(u_short *)&sc->sc_ioc.c_nop2);
1296 printf("0x%x", *(u_int *)&sc->sc_ioc.c_len);
1297 printf("0x%x\n", *(u_short *)&sc->sc_ioc.c_cmd);
1298 return 1;
1299 }
1300 #endif
1301 printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
1302 (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
1303 sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
1304 printf("P1-P10: ");
1305 printf("0x%x", *(u_int *)&sp->c_raw[0]);
1306 printf("0x%x", *(u_int *)&sp->c_raw[4]);
1307 printf("0x%x\n", *(u_short *)&sp->c_raw[8]);
1308 return 1;
1309 }
1310
1311 static int
1312 rdread(dev_t dev, struct uio *uio, int flags)
1313 {
1314
1315 return physio(rdstrategy, NULL, dev, B_READ, minphys, uio);
1316 }
1317
1318 static int
1319 rdwrite(dev_t dev, struct uio *uio, int flags)
1320 {
1321
1322 return physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio);
1323 }
1324
1325 static int
1326 rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
1327 {
1328 struct rd_softc *sc = device_lookup_private(&rd_cd, rdunit(dev));
1329 struct disklabel *lp = sc->sc_dkdev.dk_label;
1330 int error, flags;
1331
1332 error = disk_ioctl(&sc->sc_dkdev, rdpart(dev), cmd, data, flag, l);
1333 if (error != EPASSTHROUGH)
1334 return error;
1335
1336 switch (cmd) {
1337 case DIOCWLABEL:
1338 if ((flag & FWRITE) == 0)
1339 return EBADF;
1340 if (*(int *)data)
1341 sc->sc_flags |= RDF_WLABEL;
1342 else
1343 sc->sc_flags &= ~RDF_WLABEL;
1344 return 0;
1345
1346 case DIOCSDINFO:
1347 if ((flag & FWRITE) == 0)
1348 return EBADF;
1349 return setdisklabel(lp, (struct disklabel *)data,
1350 (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
1351 NULL);
1352
1353 case DIOCWDINFO:
1354 if ((flag & FWRITE) == 0)
1355 return EBADF;
1356 error = setdisklabel(lp, (struct disklabel *)data,
1357 (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dkdev.dk_openmask,
1358 NULL);
1359 if (error)
1360 return error;
1361 flags = sc->sc_flags;
1362 sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1363 error = writedisklabel(rdlabdev(dev), rdstrategy, lp, NULL);
1364 sc->sc_flags = flags;
1365 return error;
1366
1367 case DIOCGDEFLABEL:
1368 rdgetdefaultlabel(sc, (struct disklabel *)data);
1369 return 0;
1370 }
1371 return EINVAL;
1372 }
1373
1374 static void
1375 rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
1376 {
1377 int type = sc->sc_type;
1378
1379 memset((void *)lp, 0, sizeof(struct disklabel));
1380
1381 lp->d_type = DKTYPE_HPIB;
1382 lp->d_secsize = DEV_BSIZE;
1383 lp->d_nsectors = rdidentinfo[type].ri_nbpt;
1384 lp->d_ntracks = rdidentinfo[type].ri_ntpc;
1385 lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
1386 lp->d_secperunit = rdidentinfo[type].ri_nblocks;
1387 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1388
1389 strlcpy(lp->d_typename, rdidentinfo[type].ri_desc,
1390 sizeof(lp->d_typename));
1391 strlcpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
1392 lp->d_rpm = 3000;
1393 lp->d_interleave = 1;
1394 lp->d_flags = 0;
1395
1396 lp->d_partitions[RAW_PART].p_offset = 0;
1397 lp->d_partitions[RAW_PART].p_size =
1398 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1399 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1400 lp->d_npartitions = RAW_PART + 1;
1401
1402 lp->d_magic = DISKMAGIC;
1403 lp->d_magic2 = DISKMAGIC;
1404 lp->d_checksum = dkcksum(lp);
1405 }
1406
1407 int
1408 rdsize(dev_t dev)
1409 {
1410 struct rd_softc *sc;
1411 int psize, didopen = 0;
1412
1413 sc = device_lookup_private(&rd_cd, rdunit(dev));
1414 if (sc == NULL)
1415 return ENXIO;
1416
1417 if ((sc->sc_flags & RDF_ALIVE) == 0)
1418 return ENXIO;
1419
1420 /*
1421 * We get called very early on (via swapconf)
1422 * without the device being open so we may need
1423 * to handle it here.
1424 */
1425 if (sc->sc_dkdev.dk_openmask == 0) {
1426 if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1427 return -1;
1428 didopen = 1;
1429 }
1430 psize = sc->sc_dkdev.dk_label->d_partitions[rdpart(dev)].p_size *
1431 (sc->sc_dkdev.dk_label->d_secsize / DEV_BSIZE);
1432 if (didopen)
1433 (void)rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1434 return psize;
1435 }
1436
1437 #ifdef DEBUG
1438 static void
1439 rdprinterr(const char *str, short err, const char **tab)
1440 {
1441 int i;
1442 int printed;
1443
1444 if (err == 0)
1445 return;
1446 printf(" %s error %d field:", str, err);
1447 printed = 0;
1448 for (i = 0; i < 16; i++)
1449 if (err & (0x8000 >> i))
1450 printf("%s%s", printed++ ? " + " : " ", tab[i]);
1451 printf("\n");
1452 }
1453 #endif
1454
1455 static int rddoingadump; /* simple mutex */
1456
1457 /*
1458 * Non-interrupt driven, non-DMA dump routine.
1459 */
1460 static int
1461 rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1462 {
1463 int sectorsize; /* size of a disk sector */
1464 int nsects; /* number of sectors in partition */
1465 int sectoff; /* sector offset of partition */
1466 int totwrt; /* total number of sectors left to write */
1467 int nwrt; /* current number of sectors to write */
1468 int part;
1469 int ctlr, slave;
1470 struct rd_softc *sc;
1471 struct disklabel *lp;
1472 char stat;
1473
1474 /* Check for recursive dump; if so, punt. */
1475 if (rddoingadump)
1476 return EFAULT;
1477 rddoingadump = 1;
1478
1479 /* Decompose unit and partition. */
1480 part = rdpart(dev);
1481
1482 /* Make sure dump device is ok. */
1483 sc = device_lookup_private(&rd_cd, rdunit(dev));
1484 if (sc == NULL)
1485 return ENXIO;
1486
1487 if ((sc->sc_flags & RDF_ALIVE) == 0)
1488 return ENXIO;
1489
1490 ctlr = device_unit(device_parent(sc->sc_dev));
1491 slave = sc->sc_slave;
1492
1493 /*
1494 * Convert to disk sectors. Request must be a multiple of size.
1495 */
1496 lp = sc->sc_dkdev.dk_label;
1497 sectorsize = lp->d_secsize;
1498 if ((size % sectorsize) != 0)
1499 return EFAULT;
1500 totwrt = size / sectorsize;
1501 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1502
1503 nsects = lp->d_partitions[part].p_size;
1504 sectoff = lp->d_partitions[part].p_offset;
1505
1506 /* Check transfer bounds against partition size. */
1507 if ((blkno < 0) || (blkno + totwrt) > nsects)
1508 return EINVAL;
1509
1510 /* Offset block number to start of partition. */
1511 blkno += sectoff;
1512
1513 while (totwrt > 0) {
1514 nwrt = totwrt; /* XXX */
1515 #ifndef RD_DUMP_NOT_TRUSTED
1516 /*
1517 * Fill out and send HPIB command.
1518 */
1519 sc->sc_ioc.c_unit = C_SUNIT(sc->sc_punit);
1520 sc->sc_ioc.c_volume = C_SVOL(0);
1521 sc->sc_ioc.c_saddr = C_SADDR;
1522 sc->sc_ioc.c_hiaddr = 0;
1523 sc->sc_ioc.c_addr = RDBTOS(blkno);
1524 sc->sc_ioc.c_nop2 = C_NOP;
1525 sc->sc_ioc.c_slen = C_SLEN;
1526 sc->sc_ioc.c_len = nwrt * sectorsize;
1527 sc->sc_ioc.c_cmd = C_WRITE;
1528 hpibsend(ctlr, slave, C_CMD, &sc->sc_ioc.c_unit,
1529 sizeof(sc->sc_ioc) - 2);
1530 if (hpibswait(ctlr, slave))
1531 return EIO;
1532
1533 /*
1534 * Send the data.
1535 */
1536 hpibsend(ctlr, slave, C_EXEC, va, nwrt * sectorsize);
1537 (void) hpibswait(ctlr, slave);
1538 hpibrecv(ctlr, slave, C_QSTAT, &stat, 1);
1539 if (stat)
1540 return EIO;
1541 #else /* RD_DUMP_NOT_TRUSTED */
1542 /* Let's just talk about this first... */
1543 printf("%s: dump addr %p, blk %d\n", device_xname(sc->sc_dev),
1544 va, blkno);
1545 delay(500 * 1000); /* half a second */
1546 #endif /* RD_DUMP_NOT_TRUSTED */
1547
1548 /* update block count */
1549 totwrt -= nwrt;
1550 blkno += nwrt;
1551 va = (char *)va + sectorsize * nwrt;
1552 }
1553 rddoingadump = 0;
1554 return 0;
1555 }
1556