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