rd.c revision 1.14 1 /* $NetBSD: rd.c,v 1.14 1995/11/19 19:07:18 thorpej 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/dkstat.h>
56 #include <sys/disklabel.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 rdinit(), rdstart(), rdgo(), rdintr();
73 void rdstrategy();
74 struct driver rddriver = {
75 rdinit, "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 rdinit(hd)
225 register struct hp_device *hd;
226 {
227 register struct rd_softc *rs = &rd_softc[hd->hp_unit];
228
229 rs->sc_hd = hd;
230 rs->sc_punit = rdpunit(hd->hp_flags);
231 rs->sc_type = rdident(rs, hd);
232 if (rs->sc_type < 0)
233 return(0);
234 rs->sc_dq.dq_ctlr = hd->hp_ctlr;
235 rs->sc_dq.dq_unit = hd->hp_unit;
236 rs->sc_dq.dq_slave = hd->hp_slave;
237 rs->sc_dq.dq_driver = &rddriver;
238 rs->sc_flags = RDF_ALIVE;
239 #ifdef DEBUG
240 /* always report errors */
241 if (rddebug & RDB_ERROR)
242 rderrthresh = 0;
243 #endif
244 return(1);
245 }
246
247 rdident(rs, hd)
248 struct rd_softc *rs;
249 struct hp_device *hd;
250 {
251 struct rd_describe *desc = &rs->sc_rddesc;
252 u_char stat, cmd[3];
253 int unit, lunit;
254 char name[7];
255 register int ctlr, slave, id, i;
256
257 ctlr = hd->hp_ctlr;
258 slave = hd->hp_slave;
259 unit = rs->sc_punit;
260 lunit = hd->hp_unit;
261
262 /*
263 * Grab device id and make sure:
264 * 1. It is a CS80 device.
265 * 2. It is one of the types we support.
266 * 3. If it is a 7946, we are accessing the disk unit (0)
267 */
268 id = hpibid(ctlr, slave);
269 #ifdef DEBUG
270 if (rddebug & RDB_IDENT)
271 printf("hpibid(%d, %d) -> %x\n", ctlr, slave, id);
272 #endif
273 if ((id & 0x200) == 0)
274 return(-1);
275 for (i = 0; i < numrdidentinfo; i++)
276 if (id == rdidentinfo[i].ri_hwid)
277 break;
278 if (i == numrdidentinfo || unit > rdidentinfo[i].ri_maxunum)
279 return(-1);
280 id = i;
281
282 /*
283 * Reset drive and collect device description.
284 * Don't really use the description info right now but
285 * might come in handy in the future (for disk labels).
286 */
287 rdreset(rs, hd);
288 cmd[0] = C_SUNIT(unit);
289 cmd[1] = C_SVOL(0);
290 cmd[2] = C_DESC;
291 hpibsend(ctlr, slave, C_CMD, cmd, sizeof(cmd));
292 hpibrecv(ctlr, slave, C_EXEC, desc, 37);
293 hpibrecv(ctlr, slave, C_QSTAT, &stat, sizeof(stat));
294 bzero(name, sizeof(name));
295 if (!stat) {
296 register int n = desc->d_name;
297 for (i = 5; i >= 0; i--) {
298 name[i] = (n & 0xf) + '0';
299 n >>= 4;
300 }
301 /* use drive characteristics to calculate xfer rate */
302 rs->sc_wpms = 1000000 * (desc->d_sectsize/2) /
303 desc->d_blocktime;
304 }
305 #ifdef DEBUG
306 if (rddebug & RDB_IDENT) {
307 printf("rd%d: name: %x ('%s')\n",
308 lunit, desc->d_name, name);
309 printf(" iuw %x, maxxfr %d, ctype %d\n",
310 desc->d_iuw, desc->d_cmaxxfr, desc->d_ctype);
311 printf(" utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
312 desc->d_utype, desc->d_sectsize,
313 desc->d_blkbuf, desc->d_burstsize, desc->d_blocktime);
314 printf(" avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
315 desc->d_uavexfr, desc->d_retry, desc->d_access,
316 desc->d_maxint, desc->d_fvbyte, desc->d_rvbyte);
317 printf(" maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
318 desc->d_maxcyl, desc->d_maxhead, desc->d_maxsect,
319 desc->d_maxvsectl, desc->d_interleave);
320 }
321 #endif
322 /*
323 * Take care of a couple of anomolies:
324 * 1. 7945A and 7946A both return same HW id
325 * 2. 9122S and 9134D both return same HW id
326 * 3. 9122D and 9134L both return same HW id
327 */
328 switch (rdidentinfo[id].ri_hwid) {
329 case RD7946AID:
330 if (bcmp(name, "079450", 6) == 0)
331 id = RD7945A;
332 else
333 id = RD7946A;
334 break;
335
336 case RD9134LID:
337 if (bcmp(name, "091340", 6) == 0)
338 id = RD9134L;
339 else
340 id = RD9122D;
341 break;
342
343 case RD9134DID:
344 if (bcmp(name, "091220", 6) == 0)
345 id = RD9122S;
346 else
347 id = RD9134D;
348 break;
349 }
350 /*
351 * XXX We use DEV_BSIZE instead of the sector size value pulled
352 * off the driver because all of this code assumes 512 byte
353 * blocks. ICK!
354 */
355 printf("rd%d: %s, %d cylinders, %d heads, %d blocks, %d bytes/block\n",
356 lunit, rdidentinfo[id].ri_desc, rdidentinfo[id].ri_ncyl,
357 rdidentinfo[id].ri_ntpc, rdidentinfo[id].ri_nblocks,
358 DEV_BSIZE);
359 return(id);
360 }
361
362 rdreset(rs, hd)
363 register struct rd_softc *rs;
364 register struct hp_device *hd;
365 {
366 u_char stat;
367
368 rs->sc_clear.c_unit = C_SUNIT(rs->sc_punit);
369 rs->sc_clear.c_cmd = C_CLEAR;
370 hpibsend(hd->hp_ctlr, hd->hp_slave, C_TCMD, &rs->sc_clear,
371 sizeof(rs->sc_clear));
372 hpibswait(hd->hp_ctlr, hd->hp_slave);
373 hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
374 rs->sc_src.c_unit = C_SUNIT(RDCTLR);
375 rs->sc_src.c_nop = C_NOP;
376 rs->sc_src.c_cmd = C_SREL;
377 rs->sc_src.c_param = C_REL;
378 hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_src,
379 sizeof(rs->sc_src));
380 hpibswait(hd->hp_ctlr, hd->hp_slave);
381 hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
382 rs->sc_ssmc.c_unit = C_SUNIT(rs->sc_punit);
383 rs->sc_ssmc.c_cmd = C_SSM;
384 rs->sc_ssmc.c_refm = REF_MASK;
385 rs->sc_ssmc.c_fefm = FEF_MASK;
386 rs->sc_ssmc.c_aefm = AEF_MASK;
387 rs->sc_ssmc.c_iefm = IEF_MASK;
388 hpibsend(hd->hp_ctlr, hd->hp_slave, C_CMD, &rs->sc_ssmc,
389 sizeof(rs->sc_ssmc));
390 hpibswait(hd->hp_ctlr, hd->hp_slave);
391 hpibrecv(hd->hp_ctlr, hd->hp_slave, C_QSTAT, &stat, sizeof(stat));
392 #ifdef DEBUG
393 rdstats[hd->hp_unit].rdresets++;
394 #endif
395 }
396
397 /*
398 * Read or constuct a disklabel
399 */
400 int
401 rdgetinfo(dev)
402 dev_t dev;
403 {
404 int unit = rdunit(dev);
405 register struct rd_softc *rs = &rd_softc[unit];
406 register struct disklabel *lp = &rs->sc_info.ri_label;
407 register struct partition *pi;
408 char *msg, *readdisklabel();
409
410 /*
411 * Set some default values to use while reading the label
412 * or to use if there isn't a label.
413 */
414 bzero((caddr_t)lp, sizeof *lp);
415 lp->d_type = DTYPE_HPIB;
416 lp->d_secsize = DEV_BSIZE;
417 lp->d_nsectors = 32;
418 lp->d_ntracks = 20;
419 lp->d_ncylinders = 1;
420 lp->d_secpercyl = 32*20;
421 lp->d_npartitions = 3;
422 lp->d_partitions[2].p_offset = 0;
423 lp->d_partitions[2].p_size = LABELSECTOR+1;
424
425 /*
426 * Now try to read the disklabel
427 */
428 msg = readdisklabel(rdlabdev(dev), rdstrategy, lp);
429 if (msg == NULL)
430 return(0);
431
432 pi = lp->d_partitions;
433 printf("rd%d: WARNING: %s, ", unit, msg);
434 #ifdef COMPAT_NOLABEL
435 printf("using old default partitioning\n");
436 rdmakedisklabel(unit, lp);
437 #else
438 printf("defining `c' partition as entire disk\n");
439 pi[2].p_size = rdidentinfo[rs->sc_type].ri_nblocks;
440 /* XXX reset other info since readdisklabel screws with it */
441 lp->d_npartitions = 3;
442 pi[0].p_size = 0;
443 #endif
444 return(0);
445 }
446
447 int
448 rdopen(dev, flags, mode, p)
449 dev_t dev;
450 int flags, mode;
451 struct proc *p;
452 {
453 register int unit = rdunit(dev);
454 register struct rd_softc *rs = &rd_softc[unit];
455 int error, mask;
456
457 if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
458 return(ENXIO);
459
460 /*
461 * Wait for any pending opens/closes to complete
462 */
463 while (rs->sc_flags & (RDF_OPENING|RDF_CLOSING))
464 sleep((caddr_t)rs, PRIBIO);
465
466 /*
467 * On first open, get label and partition info.
468 * We may block reading the label, so be careful
469 * to stop any other opens.
470 */
471 if (rs->sc_info.ri_open == 0) {
472 rs->sc_flags |= RDF_OPENING;
473 error = rdgetinfo(dev);
474 rs->sc_flags &= ~RDF_OPENING;
475 wakeup((caddr_t)rs);
476 if (error)
477 return(error);
478 }
479 if (rs->sc_hd->hp_dk >= 0) {
480 /* guess at xfer rate based on 3600 rpm (60 rps) */
481 if (rs->sc_wpms == 0)
482 rs->sc_wpms = 60 * rs->sc_info.ri_label.d_nsectors
483 * DEV_BSIZE / 2;
484 dk_wpms[rs->sc_hd->hp_dk] = rs->sc_wpms;
485 }
486
487 mask = 1 << rdpart(dev);
488 if (mode == S_IFCHR)
489 rs->sc_info.ri_copen |= mask;
490 else
491 rs->sc_info.ri_bopen |= mask;
492 rs->sc_info.ri_open |= mask;
493 return(0);
494 }
495
496 int
497 rdclose(dev, flag, mode, p)
498 dev_t dev;
499 int flag, mode;
500 struct proc *p;
501 {
502 int unit = rdunit(dev);
503 register struct rd_softc *rs = &rd_softc[unit];
504 register struct rdinfo *ri = &rs->sc_info;
505 int mask, s;
506
507 mask = 1 << rdpart(dev);
508 if (mode == S_IFCHR)
509 ri->ri_copen &= ~mask;
510 else
511 ri->ri_bopen &= ~mask;
512 ri->ri_open = ri->ri_bopen | ri->ri_copen;
513 /*
514 * On last close, we wait for all activity to cease since
515 * the label/parition info will become invalid. Since we
516 * might sleep, we must block any opens while we are here.
517 * Note we don't have to about other closes since we know
518 * we are the last one.
519 */
520 if (ri->ri_open == 0) {
521 rs->sc_flags |= RDF_CLOSING;
522 s = splbio();
523 while (rdtab[unit].b_active) {
524 rs->sc_flags |= RDF_WANTED;
525 sleep((caddr_t)&rdtab[unit], PRIBIO);
526 }
527 splx(s);
528 rs->sc_flags &= ~(RDF_CLOSING|RDF_WLABEL);
529 wakeup((caddr_t)rs);
530 }
531 return(0);
532 }
533
534 void
535 rdstrategy(bp)
536 register struct buf *bp;
537 {
538 int unit = rdunit(bp->b_dev);
539 register struct rd_softc *rs = &rd_softc[unit];
540 register struct buf *dp = &rdtab[unit];
541 register struct partition *pinfo;
542 register daddr_t bn;
543 register int sz, s;
544
545 #ifdef DEBUG
546 if (rddebug & RDB_FOLLOW)
547 printf("rdstrategy(%x): dev %x, bn %x, bcount %x, %c\n",
548 bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
549 (bp->b_flags & B_READ) ? 'R' : 'W');
550 #endif
551 bn = bp->b_blkno;
552 sz = howmany(bp->b_bcount, DEV_BSIZE);
553 pinfo = &rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)];
554 if (bn < 0 || bn + sz > pinfo->p_size) {
555 sz = pinfo->p_size - bn;
556 if (sz == 0) {
557 bp->b_resid = bp->b_bcount;
558 goto done;
559 }
560 if (sz < 0) {
561 bp->b_error = EINVAL;
562 goto bad;
563 }
564 bp->b_bcount = dbtob(sz);
565 }
566 /*
567 * Check for write to write protected label
568 */
569 if (bn + pinfo->p_offset <= LABELSECTOR &&
570 #if LABELSECTOR != 0
571 bn + pinfo->p_offset + sz > LABELSECTOR &&
572 #endif
573 !(bp->b_flags & B_READ) && !(rs->sc_flags & RDF_WLABEL)) {
574 bp->b_error = EROFS;
575 goto bad;
576 }
577 bp->b_cylin = bn + pinfo->p_offset;
578 s = splbio();
579 disksort(dp, bp);
580 if (dp->b_active == 0) {
581 dp->b_active = 1;
582 rdustart(unit);
583 }
584 splx(s);
585 return;
586 bad:
587 bp->b_flags |= B_ERROR;
588 done:
589 biodone(bp);
590 }
591
592 /*
593 * Called from timeout() when handling maintenance releases
594 */
595 void
596 rdrestart(arg)
597 void *arg;
598 {
599 int s = splbio();
600 rdustart((int)arg);
601 splx(s);
602 }
603
604 rdustart(unit)
605 register int unit;
606 {
607 register struct buf *bp;
608 register struct rd_softc *rs = &rd_softc[unit];
609
610 bp = rdtab[unit].b_actf;
611 rs->sc_addr = bp->b_un.b_addr;
612 rs->sc_resid = bp->b_bcount;
613 if (hpibreq(&rs->sc_dq))
614 rdstart(unit);
615 }
616
617 struct buf *
618 rdfinish(unit, rs, bp)
619 int unit;
620 register struct rd_softc *rs;
621 register struct buf *bp;
622 {
623 register struct buf *dp = &rdtab[unit];
624
625 dp->b_errcnt = 0;
626 dp->b_actf = bp->b_actf;
627 bp->b_resid = 0;
628 biodone(bp);
629 hpibfree(&rs->sc_dq);
630 if (dp->b_actf)
631 return(dp->b_actf);
632 dp->b_active = 0;
633 if (rs->sc_flags & RDF_WANTED) {
634 rs->sc_flags &= ~RDF_WANTED;
635 wakeup((caddr_t)dp);
636 }
637 return(NULL);
638 }
639
640 rdstart(unit)
641 register int unit;
642 {
643 register struct rd_softc *rs = &rd_softc[unit];
644 register struct buf *bp = rdtab[unit].b_actf;
645 register struct hp_device *hp = rs->sc_hd;
646 register int part;
647
648 again:
649 #ifdef DEBUG
650 if (rddebug & RDB_FOLLOW)
651 printf("rdstart(%d): bp %x, %c\n", unit, bp,
652 (bp->b_flags & B_READ) ? 'R' : 'W');
653 #endif
654 part = rdpart(bp->b_dev);
655 rs->sc_flags |= RDF_SEEK;
656 rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
657 rs->sc_ioc.c_volume = C_SVOL(0);
658 rs->sc_ioc.c_saddr = C_SADDR;
659 rs->sc_ioc.c_hiaddr = 0;
660 rs->sc_ioc.c_addr = RDBTOS(bp->b_cylin);
661 rs->sc_ioc.c_nop2 = C_NOP;
662 rs->sc_ioc.c_slen = C_SLEN;
663 rs->sc_ioc.c_len = rs->sc_resid;
664 rs->sc_ioc.c_cmd = bp->b_flags & B_READ ? C_READ : C_WRITE;
665 #ifdef DEBUG
666 if (rddebug & RDB_IO)
667 printf("rdstart: hpibsend(%x, %x, %x, %x, %x)\n",
668 hp->hp_ctlr, hp->hp_slave, C_CMD,
669 &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
670 #endif
671 if (hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD, &rs->sc_ioc.c_unit,
672 sizeof(rs->sc_ioc)-2) == sizeof(rs->sc_ioc)-2) {
673 if (hp->hp_dk >= 0) {
674 dk_busy |= 1 << hp->hp_dk;
675 dk_seek[hp->hp_dk]++;
676 }
677 #ifdef DEBUG
678 if (rddebug & RDB_IO)
679 printf("rdstart: hpibawait(%x)\n", hp->hp_ctlr);
680 #endif
681 hpibawait(hp->hp_ctlr);
682 return;
683 }
684 /*
685 * Experience has shown that the hpibwait in this hpibsend will
686 * occasionally timeout. It appears to occur mostly on old 7914
687 * drives with full maintenance tracks. We should probably
688 * integrate this with the backoff code in rderror.
689 */
690 #ifdef DEBUG
691 if (rddebug & RDB_ERROR)
692 printf("rd%d: rdstart: cmd %x adr %d blk %d len %d ecnt %d\n",
693 unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
694 bp->b_blkno, rs->sc_resid, rdtab[unit].b_errcnt);
695 rdstats[unit].rdretries++;
696 #endif
697 rs->sc_flags &= ~RDF_SEEK;
698 rdreset(rs, hp);
699 if (rdtab[unit].b_errcnt++ < RDRETRY)
700 goto again;
701 printf("rd%d: rdstart err: cmd 0x%x sect %d blk %d len %d\n",
702 unit, rs->sc_ioc.c_cmd, rs->sc_ioc.c_addr,
703 bp->b_blkno, rs->sc_resid);
704 bp->b_flags |= B_ERROR;
705 bp->b_error = EIO;
706 bp = rdfinish(unit, rs, bp);
707 if (bp) {
708 rs->sc_addr = bp->b_un.b_addr;
709 rs->sc_resid = bp->b_bcount;
710 if (hpibreq(&rs->sc_dq))
711 goto again;
712 }
713 }
714
715 rdgo(unit)
716 register int unit;
717 {
718 register struct rd_softc *rs = &rd_softc[unit];
719 register struct hp_device *hp = rs->sc_hd;
720 struct buf *bp = rdtab[unit].b_actf;
721 int rw;
722
723 rw = bp->b_flags & B_READ;
724
725 if (hp->hp_dk >= 0) {
726 dk_busy |= 1 << hp->hp_dk;
727 dk_xfer[hp->hp_dk]++;
728 dk_wds[hp->hp_dk] += rs->sc_resid >> 6;
729 }
730 #ifdef USELEDS
731 if (inledcontrol == 0)
732 ledcontrol(0, 0, LED_DISK);
733 #endif
734 hpibgo(hp->hp_ctlr, hp->hp_slave, C_EXEC,
735 rs->sc_addr, rs->sc_resid, rw, rw != 0);
736 }
737
738 rdintr(unit)
739 register int unit;
740 {
741 register struct rd_softc *rs = &rd_softc[unit];
742 register struct buf *bp = rdtab[unit].b_actf;
743 register struct hp_device *hp = rs->sc_hd;
744 u_char stat = 13; /* in case hpibrecv fails */
745 int rv, restart;
746
747 #ifdef DEBUG
748 if (rddebug & RDB_FOLLOW)
749 printf("rdintr(%d): bp %x, %c, flags %x\n", unit, bp,
750 (bp->b_flags & B_READ) ? 'R' : 'W', rs->sc_flags);
751 if (bp == NULL) {
752 printf("rd%d: bp == NULL\n", unit);
753 return;
754 }
755 #endif
756 if (hp->hp_dk >= 0)
757 dk_busy &= ~(1 << hp->hp_dk);
758 if (rs->sc_flags & RDF_SEEK) {
759 rs->sc_flags &= ~RDF_SEEK;
760 if (hpibustart(hp->hp_ctlr))
761 rdgo(unit);
762 return;
763 }
764 if ((rs->sc_flags & RDF_SWAIT) == 0) {
765 #ifdef DEBUG
766 rdstats[unit].rdpolltries++;
767 #endif
768 if (hpibpptest(hp->hp_ctlr, hp->hp_slave) == 0) {
769 #ifdef DEBUG
770 rdstats[unit].rdpollwaits++;
771 #endif
772 if (hp->hp_dk >= 0)
773 dk_busy |= 1 << hp->hp_dk;
774 rs->sc_flags |= RDF_SWAIT;
775 hpibawait(hp->hp_ctlr);
776 return;
777 }
778 } else
779 rs->sc_flags &= ~RDF_SWAIT;
780 rv = hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
781 if (rv != 1 || stat) {
782 #ifdef DEBUG
783 if (rddebug & RDB_ERROR)
784 printf("rdintr: recv failed or bad stat %d\n", stat);
785 #endif
786 restart = rderror(unit);
787 #ifdef DEBUG
788 rdstats[unit].rdretries++;
789 #endif
790 if (rdtab[unit].b_errcnt++ < RDRETRY) {
791 if (restart)
792 rdstart(unit);
793 return;
794 }
795 bp->b_flags |= B_ERROR;
796 bp->b_error = EIO;
797 }
798 if (rdfinish(unit, rs, bp))
799 rdustart(unit);
800 }
801
802 rdstatus(rs)
803 register struct rd_softc *rs;
804 {
805 register int c, s;
806 u_char stat;
807 int rv;
808
809 c = rs->sc_hd->hp_ctlr;
810 s = rs->sc_hd->hp_slave;
811 rs->sc_rsc.c_unit = C_SUNIT(rs->sc_punit);
812 rs->sc_rsc.c_sram = C_SRAM;
813 rs->sc_rsc.c_ram = C_RAM;
814 rs->sc_rsc.c_cmd = C_STATUS;
815 bzero((caddr_t)&rs->sc_stat, sizeof(rs->sc_stat));
816 rv = hpibsend(c, s, C_CMD, &rs->sc_rsc, sizeof(rs->sc_rsc));
817 if (rv != sizeof(rs->sc_rsc)) {
818 #ifdef DEBUG
819 if (rddebug & RDB_STATUS)
820 printf("rdstatus: send C_CMD failed %d != %d\n",
821 rv, sizeof(rs->sc_rsc));
822 #endif
823 return(1);
824 }
825 rv = hpibrecv(c, s, C_EXEC, &rs->sc_stat, sizeof(rs->sc_stat));
826 if (rv != sizeof(rs->sc_stat)) {
827 #ifdef DEBUG
828 if (rddebug & RDB_STATUS)
829 printf("rdstatus: send C_EXEC failed %d != %d\n",
830 rv, sizeof(rs->sc_stat));
831 #endif
832 return(1);
833 }
834 rv = hpibrecv(c, s, C_QSTAT, &stat, 1);
835 if (rv != 1 || stat) {
836 #ifdef DEBUG
837 if (rddebug & RDB_STATUS)
838 printf("rdstatus: recv failed %d or bad stat %d\n",
839 rv, stat);
840 #endif
841 return(1);
842 }
843 return(0);
844 }
845
846 /*
847 * Deal with errors.
848 * Returns 1 if request should be restarted,
849 * 0 if we should just quietly give up.
850 */
851 rderror(unit)
852 int unit;
853 {
854 struct rd_softc *rs = &rd_softc[unit];
855 register struct rd_stat *sp;
856 struct buf *bp;
857 daddr_t hwbn, pbn;
858
859 if (rdstatus(rs)) {
860 #ifdef DEBUG
861 printf("rd%d: couldn't get status\n", unit);
862 #endif
863 rdreset(rs, rs->sc_hd);
864 return(1);
865 }
866 sp = &rs->sc_stat;
867 if (sp->c_fef & FEF_REXMT)
868 return(1);
869 if (sp->c_fef & FEF_PF) {
870 rdreset(rs, rs->sc_hd);
871 return(1);
872 }
873 /*
874 * Unit requests release for internal maintenance.
875 * We just delay awhile and try again later. Use expontially
876 * increasing backoff ala ethernet drivers since we don't really
877 * know how long the maintenance will take. With RDWAITC and
878 * RDRETRY as defined, the range is 1 to 32 seconds.
879 */
880 if (sp->c_fef & FEF_IMR) {
881 extern int hz;
882 int rdtimo = RDWAITC << rdtab[unit].b_errcnt;
883 #ifdef DEBUG
884 printf("rd%d: internal maintenance, %d second timeout\n",
885 unit, rdtimo);
886 rdstats[unit].rdtimeouts++;
887 #endif
888 hpibfree(&rs->sc_dq);
889 timeout(rdrestart, (void *)unit, rdtimo * hz);
890 return(0);
891 }
892 /*
893 * Only report error if we have reached the error reporting
894 * threshhold. By default, this will only report after the
895 * retry limit has been exceeded.
896 */
897 if (rdtab[unit].b_errcnt < rderrthresh)
898 return(1);
899
900 /*
901 * First conjure up the block number at which the error occured.
902 * Note that not all errors report a block number, in that case
903 * we just use b_blkno.
904 */
905 bp = rdtab[unit].b_actf;
906 pbn = rs->sc_info.ri_label.d_partitions[rdpart(bp->b_dev)].p_offset;
907 if ((sp->c_fef & FEF_CU) || (sp->c_fef & FEF_DR) ||
908 (sp->c_ief & IEF_RRMASK)) {
909 hwbn = RDBTOS(pbn + bp->b_blkno);
910 pbn = bp->b_blkno;
911 } else {
912 hwbn = sp->c_blk;
913 pbn = RDSTOB(hwbn) - pbn;
914 }
915 /*
916 * Now output a generic message suitable for badsect.
917 * Note that we don't use harderr cuz it just prints
918 * out b_blkno which is just the beginning block number
919 * of the transfer, not necessary where the error occured.
920 */
921 printf("rd%d%c: hard error sn%d\n",
922 rdunit(bp->b_dev), 'a'+rdpart(bp->b_dev), pbn);
923 /*
924 * Now report the status as returned by the hardware with
925 * attempt at interpretation (unless debugging).
926 */
927 printf("rd%d %s error:",
928 unit, (bp->b_flags & B_READ) ? "read" : "write");
929 #ifdef DEBUG
930 if (rddebug & RDB_ERROR) {
931 /* status info */
932 printf("\n volume: %d, unit: %d\n",
933 (sp->c_vu>>4)&0xF, sp->c_vu&0xF);
934 rdprinterr("reject", sp->c_ref, err_reject);
935 rdprinterr("fault", sp->c_fef, err_fault);
936 rdprinterr("access", sp->c_aef, err_access);
937 rdprinterr("info", sp->c_ief, err_info);
938 printf(" block: %d, P1-P10: ", hwbn);
939 printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
940 printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
941 printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
942 /* command */
943 printf(" ioc: ");
944 printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_pad, 8));
945 printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_hiaddr, 4));
946 printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_addr, 8));
947 printf("%s", hexstr(*(u_short *)&rs->sc_ioc.c_nop2, 4));
948 printf("%s", hexstr(*(u_int *)&rs->sc_ioc.c_len, 8));
949 printf("%s\n", hexstr(*(u_short *)&rs->sc_ioc.c_cmd, 4));
950 return(1);
951 }
952 #endif
953 printf(" v%d u%d, R0x%x F0x%x A0x%x I0x%x\n",
954 (sp->c_vu>>4)&0xF, sp->c_vu&0xF,
955 sp->c_ref, sp->c_fef, sp->c_aef, sp->c_ief);
956 printf("P1-P10: ");
957 printf("%s", hexstr(*(u_int *)&sp->c_raw[0], 8));
958 printf("%s", hexstr(*(u_int *)&sp->c_raw[4], 8));
959 printf("%s\n", hexstr(*(u_short *)&sp->c_raw[8], 4));
960 return(1);
961 }
962
963 int
964 rdread(dev, uio, flags)
965 dev_t dev;
966 struct uio *uio;
967 int flags;
968 {
969
970 return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
971 }
972
973 int
974 rdwrite(dev, uio, flags)
975 dev_t dev;
976 struct uio *uio;
977 int flags;
978 {
979
980 return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
981 }
982
983 int
984 rdioctl(dev, cmd, data, flag, p)
985 dev_t dev;
986 int cmd;
987 caddr_t data;
988 int flag;
989 struct proc *p;
990 {
991 int unit = rdunit(dev);
992 register struct rd_softc *sc = &rd_softc[unit];
993 register struct disklabel *lp = &sc->sc_info.ri_label;
994 int error, flags;
995
996 switch (cmd) {
997 case DIOCGDINFO:
998 *(struct disklabel *)data = *lp;
999 return (0);
1000
1001 case DIOCGPART:
1002 ((struct partinfo *)data)->disklab = lp;
1003 ((struct partinfo *)data)->part =
1004 &lp->d_partitions[rdpart(dev)];
1005 return (0);
1006
1007 case DIOCWLABEL:
1008 if ((flag & FWRITE) == 0)
1009 return (EBADF);
1010 if (*(int *)data)
1011 sc->sc_flags |= RDF_WLABEL;
1012 else
1013 sc->sc_flags &= ~RDF_WLABEL;
1014 return (0);
1015
1016 case DIOCSDINFO:
1017 if ((flag & FWRITE) == 0)
1018 return (EBADF);
1019 return (setdisklabel(lp, (struct disklabel *)data,
1020 (sc->sc_flags & RDF_WLABEL) ? 0
1021 : sc->sc_info.ri_open,
1022 (struct cpu_disklabel *)0));
1023
1024 case DIOCWDINFO:
1025 if ((flag & FWRITE) == 0)
1026 return (EBADF);
1027 error = setdisklabel(lp, (struct disklabel *)data,
1028 (sc->sc_flags & RDF_WLABEL) ? 0
1029 : sc->sc_info.ri_open,
1030 (struct cpu_disklabel *)0);
1031 if (error)
1032 return (error);
1033 flags = sc->sc_flags;
1034 sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1035 error = writedisklabel(rdlabdev(dev), rdstrategy, lp,
1036 (struct cpu_disklabel *)0);
1037 sc->sc_flags = flags;
1038 return (error);
1039 }
1040 return(EINVAL);
1041 }
1042
1043 int
1044 rdsize(dev)
1045 dev_t dev;
1046 {
1047 register int unit = rdunit(dev);
1048 register struct rd_softc *rs = &rd_softc[unit];
1049 int psize, didopen = 0;
1050
1051 if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
1052 return(-1);
1053
1054 /*
1055 * We get called very early on (via swapconf)
1056 * without the device being open so we may need
1057 * to handle it here.
1058 */
1059 if (rs->sc_info.ri_open == 0) {
1060 if (rdopen(dev, FREAD|FWRITE, S_IFBLK, NULL))
1061 return(-1);
1062 didopen = 1;
1063 }
1064 psize = rs->sc_info.ri_label.d_partitions[rdpart(dev)].p_size;
1065 if (didopen)
1066 (void) rdclose(dev, FREAD|FWRITE, S_IFBLK, NULL);
1067 return (psize);
1068 }
1069
1070 #ifdef DEBUG
1071 rdprinterr(str, err, tab)
1072 char *str;
1073 short err;
1074 char *tab[];
1075 {
1076 register int i;
1077 int printed;
1078
1079 if (err == 0)
1080 return;
1081 printf(" %s error field:", str, err);
1082 printed = 0;
1083 for (i = 0; i < 16; i++)
1084 if (err & (0x8000 >> i))
1085 printf("%s%s", printed++ ? " + " : " ", tab[i]);
1086 printf("\n");
1087 }
1088 #endif
1089
1090 /*
1091 * Non-interrupt driven, non-dma dump routine.
1092 */
1093 int
1094 rddump(dev)
1095 dev_t dev;
1096 {
1097 int part = rdpart(dev);
1098 int unit = rdunit(dev);
1099 register struct rd_softc *rs = &rd_softc[unit];
1100 register struct hp_device *hp = rs->sc_hd;
1101 register struct partition *pinfo;
1102 register daddr_t baddr;
1103 register int maddr, pages, i;
1104 char stat;
1105 extern int lowram, dumpsize;
1106 #ifdef DEBUG
1107 extern int pmapdebug;
1108 pmapdebug = 0;
1109 #endif
1110
1111 /* is drive ok? */
1112 if (unit >= NRD || (rs->sc_flags & RDF_ALIVE) == 0)
1113 return (ENXIO);
1114 pinfo = &rs->sc_info.ri_label.d_partitions[part];
1115 /* dump parameters in range? */
1116 if (dumplo < 0 || dumplo >= pinfo->p_size ||
1117 pinfo->p_fstype != FS_SWAP)
1118 return (EINVAL);
1119 pages = dumpsize;
1120 if (dumplo + ctod(pages) > pinfo->p_size)
1121 pages = dtoc(pinfo->p_size - dumplo);
1122 maddr = lowram;
1123 baddr = dumplo + pinfo->p_offset;
1124 /* HPIB idle? */
1125 if (!hpibreq(&rs->sc_dq)) {
1126 hpibreset(hp->hp_ctlr);
1127 rdreset(rs, rs->sc_hd);
1128 printf("[ drive %d reset ] ", unit);
1129 }
1130 for (i = 0; i < pages; i++) {
1131 #define NPGMB (1024*1024/NBPG)
1132 /* print out how many Mbs we have dumped */
1133 if (i && (i % NPGMB) == 0)
1134 printf("%d ", i / NPGMB);
1135 #undef NPBMG
1136 rs->sc_ioc.c_unit = C_SUNIT(rs->sc_punit);
1137 rs->sc_ioc.c_volume = C_SVOL(0);
1138 rs->sc_ioc.c_saddr = C_SADDR;
1139 rs->sc_ioc.c_hiaddr = 0;
1140 rs->sc_ioc.c_addr = RDBTOS(baddr);
1141 rs->sc_ioc.c_nop2 = C_NOP;
1142 rs->sc_ioc.c_slen = C_SLEN;
1143 rs->sc_ioc.c_len = NBPG;
1144 rs->sc_ioc.c_cmd = C_WRITE;
1145 hpibsend(hp->hp_ctlr, hp->hp_slave, C_CMD,
1146 &rs->sc_ioc.c_unit, sizeof(rs->sc_ioc)-2);
1147 if (hpibswait(hp->hp_ctlr, hp->hp_slave))
1148 return (EIO);
1149 pmap_enter(pmap_kernel(), (vm_offset_t)vmmap, maddr,
1150 VM_PROT_READ, TRUE);
1151 hpibsend(hp->hp_ctlr, hp->hp_slave, C_EXEC, vmmap, NBPG);
1152 (void) hpibswait(hp->hp_ctlr, hp->hp_slave);
1153 hpibrecv(hp->hp_ctlr, hp->hp_slave, C_QSTAT, &stat, 1);
1154 if (stat)
1155 return (EIO);
1156 maddr += NBPG;
1157 baddr += ctod(1);
1158 }
1159 return (0);
1160 }
1161 #endif
1162