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