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