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