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