rd.c revision 1.30.6.4 1 /* $NetBSD: rd.c,v 1.30.6.4 2017/12/03 11:37:01 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1996-2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1988 University of Utah.
34 * Copyright (c) 1982, 1990, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This code is derived from software contributed to Berkeley by
38 * the Systems Programming Group of the University of Utah Computer
39 * Science Department.
40 *
41 * Redistribution and use in source and binary forms, with or without
42 * modification, are permitted provided that the following conditions
43 * are met:
44 * 1. Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * 2. Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in the
48 * documentation and/or other materials provided with the distribution.
49 * 3. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * from: Utah $Hdr: rd.c 1.44 92/12/26$
66 *
67 * @(#)rd.c 8.2 (Berkeley) 5/19/94
68 */
69
70 /*
71 * CS80/SS80 disk driver
72 */
73
74 #include <sys/cdefs.h>
75 __KERNEL_RCSID(0, "$NetBSD: rd.c,v 1.30.6.4 2017/12/03 11:37:01 jdolecek Exp $");
76
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/buf.h>
80 #include <sys/bufq.h>
81 #include <sys/callout.h>
82 #include <sys/conf.h>
83 #include <sys/device.h>
84 #include <sys/disk.h>
85 #include <sys/disklabel.h>
86 #include <sys/endian.h>
87 #include <sys/fcntl.h>
88 #include <sys/ioctl.h>
89 #include <sys/proc.h>
90 #include <sys/stat.h>
91
92 #include <sys/rndsource.h>
93
94 #include <dev/gpib/gpibvar.h>
95 #include <dev/gpib/cs80busvar.h>
96
97 #include <dev/gpib/rdreg.h>
98
99 #include "ioconf.h"
100
101 #ifdef DEBUG
102 int rddebug = 0xff;
103 #define RDB_FOLLOW 0x01
104 #define RDB_STATUS 0x02
105 #define RDB_IDENT 0x04
106 #define RDB_IO 0x08
107 #define RDB_ASYNC 0x10
108 #define RDB_ERROR 0x80
109 #define DPRINTF(mask, str) if (rddebug & (mask)) printf str
110 #else
111 #define DPRINTF(mask, str) /* nothing */
112 #endif
113
114 struct rd_softc {
115 device_t sc_dev;
116 gpib_chipset_tag_t sc_ic;
117 gpib_handle_t sc_hdl;
118
119 struct disk sc_dk;
120
121 int sc_slave; /* GPIB slave */
122 int sc_punit; /* physical unit on slave */
123
124 int sc_flags;
125 #define RDF_ALIVE 0x01
126 #define RDF_SEEK 0x02
127 #define RDF_SWAIT 0x04
128 #define RDF_OPENING 0x08
129 #define RDF_CLOSING 0x10
130 #define RDF_WANTED 0x20
131 #define RDF_WLABEL 0x40
132
133 u_int16_t sc_type;
134 u_int8_t *sc_addr;
135 int sc_resid;
136 struct rd_iocmd sc_ioc;
137 struct bufq_state *sc_tab;
138 int sc_active;
139 int sc_errcnt;
140
141 struct callout sc_restart_ch;
142
143 krndsource_t rnd_source;
144 };
145
146 #define RDUNIT(dev) DISKUNIT(dev)
147 #define RDPART(dev) DISKPART(dev)
148 #define RDMAKEDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
149 #define RDLABELDEV(dev) (RDMAKEDEV(major(dev), RDUNIT(dev), RAW_PART))
150
151 #define RDRETRY 5
152 #define RDWAITC 1 /* min time for timeout in seconds */
153
154 int rderrthresh = RDRETRY-1; /* when to start reporting errors */
155
156 /*
157 * Misc. HW description, indexed by sc_type.
158 * Used for mapping 256-byte sectors for 512-byte sectors
159 */
160 const struct rdidentinfo {
161 u_int16_t ri_hwid; /* 2 byte HW id */
162 u_int16_t ri_maxunum; /* maximum allowed unit number */
163 const char *ri_desc; /* drive type description */
164 int ri_nbpt; /* DEV_BSIZE blocks per track */
165 int ri_ntpc; /* tracks per cylinder */
166 int ri_ncyl; /* cylinders per unit */
167 int ri_nblocks; /* DEV_BSIZE blocks on disk */
168 } rdidentinfo[] = {
169 { RD7946AID, 0, "7945A", NRD7945ABPT,
170 NRD7945ATRK, 968, 108416 },
171
172 { RD9134DID, 1, "9134D", NRD9134DBPT,
173 NRD9134DTRK, 303, 29088 },
174
175 { RD9134LID, 1, "9122S", NRD9122SBPT,
176 NRD9122STRK, 77, 1232 },
177
178 { RD7912PID, 0, "7912P", NRD7912PBPT,
179 NRD7912PTRK, 572, 128128 },
180
181 { RD7914PID, 0, "7914P", NRD7914PBPT,
182 NRD7914PTRK, 1152, 258048 },
183
184 { RD7958AID, 0, "7958A", NRD7958ABPT,
185 NRD7958ATRK, 1013, 255276 },
186
187 { RD7957AID, 0, "7957A", NRD7957ABPT,
188 NRD7957ATRK, 1036, 159544 },
189
190 { RD7933HID, 0, "7933H", NRD7933HBPT,
191 NRD7933HTRK, 1321, 789958 },
192
193 { RD9134LID, 1, "9134L", NRD9134LBPT,
194 NRD9134LTRK, 973, 77840 },
195
196 { RD7936HID, 0, "7936H", NRD7936HBPT,
197 NRD7936HTRK, 698, 600978 },
198
199 { RD7937HID, 0, "7937H", NRD7937HBPT,
200 NRD7937HTRK, 698, 1116102 },
201
202 { RD7914CTID, 0, "7914CT", NRD7914PBPT,
203 NRD7914PTRK, 1152, 258048 },
204
205 { RD7946AID, 0, "7946A", NRD7945ABPT,
206 NRD7945ATRK, 968, 108416 },
207
208 { RD9134LID, 1, "9122D", NRD9122SBPT,
209 NRD9122STRK, 77, 1232 },
210
211 { RD7957BID, 0, "7957B", NRD7957BBPT,
212 NRD7957BTRK, 1269, 159894 },
213
214 { RD7958BID, 0, "7958B", NRD7958BBPT,
215 NRD7958BTRK, 786, 297108 },
216
217 { RD7959BID, 0, "7959B", NRD7959BBPT,
218 NRD7959BTRK, 1572, 594216 },
219
220 { RD2200AID, 0, "2200A", NRD2200ABPT,
221 NRD2200ATRK, 1449, 654948 },
222
223 { RD2203AID, 0, "2203A", NRD2203ABPT,
224 NRD2203ATRK, 1449, 1309896 }
225 };
226 int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
227
228 int rdlookup(int, int, int);
229 int rdgetinfo(struct rd_softc *);
230 void rdrestart(void *);
231 struct buf *rdfinish(struct rd_softc *, struct buf *);
232
233 void rdgetcompatlabel(struct rd_softc *, struct disklabel *);
234 void rdgetdefaultlabel(struct rd_softc *, struct disklabel *);
235 void rdrestart(void *);
236 void rdustart(struct rd_softc *);
237 struct buf *rdfinish(struct rd_softc *, struct buf *);
238 void rdcallback(void *, int);
239 void rdstart(struct rd_softc *);
240 void rdintr(struct rd_softc *);
241 int rderror(struct rd_softc *);
242
243 int rdmatch(device_t, cfdata_t, void *);
244 void rdattach(device_t, device_t, void *);
245
246 CFATTACH_DECL_NEW(rd, sizeof(struct rd_softc),
247 rdmatch, rdattach, NULL, NULL);
248
249
250 dev_type_open(rdopen);
251 dev_type_close(rdclose);
252 dev_type_read(rdread);
253 dev_type_write(rdwrite);
254 dev_type_ioctl(rdioctl);
255 dev_type_strategy(rdstrategy);
256 dev_type_dump(rddump);
257 dev_type_size(rdsize);
258
259 const struct bdevsw rd_bdevsw = {
260 .d_open = rdopen,
261 .d_close = rdclose,
262 .d_strategy = rdstrategy,
263 .d_ioctl = rdioctl,
264 .d_dump = rddump,
265 .d_psize = rdsize,
266 .d_discard = nodiscard,
267 .d_flag = D_DISK
268 };
269
270 const struct cdevsw rd_cdevsw = {
271 .d_open = rdopen,
272 .d_close = rdclose,
273 .d_read = rdread,
274 .d_write = rdwrite,
275 .d_ioctl = rdioctl,
276 .d_stop = nostop,
277 .d_tty = notty,
278 .d_poll = nopoll,
279 .d_mmap = nommap,
280 .d_kqfilter = nokqfilter,
281 .d_discard = nodiscard,
282 .d_flag = D_DISK
283 };
284
285 extern struct cfdriver rd_cd;
286
287 static void
288 rdminphys(struct buf *bp)
289 {
290 struct rd_softc *sc = device_lookup_private(&rd_cd, RDUNIT(bp->b_dev));
291
292 long xmax;
293 xmax = rd->sc_dev->dv_maxphys;
294 if (bp->b_bcount > xmax)
295 bp->b_bcount = xmax;
296 }
297
298 const struct dkdriver rd_dkdriver = { rdstrategy, rdminphys };
299
300 int
301 rdlookup(int id, int slave, int punit)
302 {
303 int i;
304
305 for (i = 0; i < numrdidentinfo; i++) {
306 if (rdidentinfo[i].ri_hwid == id)
307 break;
308 }
309 if (i == numrdidentinfo || punit > rdidentinfo[i].ri_maxunum)
310 return (-1);
311 return (i);
312 }
313
314 int
315 rdmatch(device_t parent, cfdata_t match, void *aux)
316 {
317 struct cs80bus_attach_args *ca = aux;
318
319 if (rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit) < 0)
320 return (0);
321 return (1);
322 }
323
324 void
325 rdattach(device_t parent, device_t self, void *aux)
326 {
327 struct rd_softc *sc = device_private(self);
328 struct cs80bus_attach_args *ca = aux;
329 struct cs80_description csd;
330 char name[7];
331 int type, i, n;
332
333 sc->sc_dev = self;
334 sc->sc_ic = ca->ca_ic;
335 sc->sc_slave = ca->ca_slave;
336 sc->sc_punit = ca->ca_punit;
337
338 if ((type = rdlookup(ca->ca_id, ca->ca_slave, ca->ca_punit)) < 0)
339 return;
340
341 if (cs80reset(parent, sc->sc_slave, sc->sc_punit)) {
342 aprint_normal("\n");
343 aprint_error_dev(sc->sc_dev, "can't reset device\n");
344 return;
345 }
346
347 if (cs80describe(parent, sc->sc_slave, sc->sc_punit, &csd)) {
348 aprint_normal("\n");
349 aprint_error_dev(sc->sc_dev,
350 "didn't respond to describe command\n");
351 return;
352 }
353 memset(name, 0, sizeof(name));
354 for (i=0, n=0; i<3; i++) {
355 name[n++] = (csd.d_name[i] >> 4) + '0';
356 name[n++] = (csd.d_name[i] & 0x0f) + '0';
357 }
358
359 #ifdef DEBUG
360 if (rddebug & RDB_IDENT) {
361 printf("\n%s: name: ('%s')\n",
362 device_xname(sc->sc_dev), name);
363 printf(" iuw %x, maxxfr %d, ctype %d\n",
364 csd.d_iuw, csd.d_cmaxxfr, csd.d_ctype);
365 printf(" utype %d, bps %d, blkbuf %d, burst %d, blktime %d\n",
366 csd.d_utype, csd.d_sectsize,
367 csd.d_blkbuf, csd.d_burstsize, csd.d_blocktime);
368 printf(" avxfr %d, ort %d, atp %d, maxint %d, fv %x, rv %x\n",
369 csd.d_uavexfr, csd.d_retry, csd.d_access,
370 csd.d_maxint, csd.d_fvbyte, csd.d_rvbyte);
371 printf(" maxcyl/head/sect %d/%d/%d, maxvsect %d, inter %d\n",
372 csd.d_maxcylhead >> 8, csd.d_maxcylhead & 0xff,
373 csd.d_maxsect, csd.d_maxvsectl, csd.d_interleave);
374 printf("%s", device_xname(sc->sc_dev));
375 }
376 #endif
377
378 /*
379 * Take care of a couple of anomolies:
380 * 1. 7945A and 7946A both return same HW id
381 * 2. 9122S and 9134D both return same HW id
382 * 3. 9122D and 9134L both return same HW id
383 */
384 switch (ca->ca_id) {
385 case RD7946AID:
386 if (memcmp(name, "079450", 6) == 0)
387 type = RD7945A;
388 else
389 type = RD7946A;
390 break;
391
392 case RD9134LID:
393 if (memcmp(name, "091340", 6) == 0)
394 type = RD9134L;
395 else
396 type = RD9122D;
397 break;
398
399 case RD9134DID:
400 if (memcmp(name, "091220", 6) == 0)
401 type = RD9122S;
402 else
403 type = RD9134D;
404 break;
405 }
406
407 sc->sc_type = type;
408
409 /*
410 * XXX We use DEV_BSIZE instead of the sector size value pulled
411 * XXX off the driver because all of this code assumes 512 byte
412 * XXX blocks. ICK!
413 */
414 printf(": %s\n", rdidentinfo[type].ri_desc);
415 printf("%s: %d cylinders, %d heads, %d blocks, %d bytes/block\n",
416 device_xname(sc->sc_dev), rdidentinfo[type].ri_ncyl,
417 rdidentinfo[type].ri_ntpc, rdidentinfo[type].ri_nblocks,
418 DEV_BSIZE);
419
420 bufq_alloc(&sc->sc_tab, "fcfs", 0);
421
422 /*
423 * Initialize and attach the disk structure.
424 */
425 memset(&sc->sc_dk, 0, sizeof(sc->sc_dk));
426 disk_init(&sc->sc_dk, device_xname(sc->sc_dev), rd_dkdriver);
427 disk_attach(&sc->sc_dk);
428
429 callout_init(&sc->sc_restart_ch, 0);
430
431 if (gpibregister(sc->sc_ic, sc->sc_slave, rdcallback, sc,
432 &sc->sc_hdl)) {
433 aprint_error_dev(sc->sc_dev, "can't register callback\n");
434 return;
435 }
436
437 sc->sc_flags = RDF_ALIVE;
438 #ifdef DEBUG
439 /* always report errors */
440 if (rddebug & RDB_ERROR)
441 rderrthresh = 0;
442 #endif
443 /*
444 * attach the device into the random source list
445 */
446 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
447 RND_TYPE_DISK, RND_FLAG_DEFAULT);
448 }
449
450 /*
451 * Read or construct a disklabel
452 */
453 int
454 rdgetinfo(struct rd_softc *sc)
455 {
456 struct disklabel *lp = sc->sc_dk.dk_label;
457 struct partition *pi;
458 const char *msg;
459
460 memset(sc->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
461
462 rdgetdefaultlabel(sc, lp);
463
464 /*
465 * Call the generic disklabel extraction routine
466 */
467 msg = readdisklabel(RDMAKEDEV(0, device_unit(sc->sc_dev), RAW_PART),
468 rdstrategy, lp, NULL);
469 if (msg == NULL)
470 return (0);
471
472 pi = lp->d_partitions;
473 printf("%s: WARNING: %s\n", device_xname(sc->sc_dev), msg);
474
475 pi[RAW_PART].p_size = rdidentinfo[sc->sc_type].ri_nblocks;
476 lp->d_npartitions = RAW_PART+1;
477 pi[0].p_size = 0;
478
479 return (0);
480 }
481
482 int
483 rdopen(dev_t dev, int flags, int mode, struct lwp *l)
484 {
485 struct rd_softc *sc;
486 int error, mask, part;
487
488 sc = device_lookup_private(&rd_cd, RDUNIT(dev));
489 if (sc == NULL || (sc->sc_flags & RDF_ALIVE) ==0)
490 return (ENXIO);
491
492 /*
493 * Wait for any pending opens/closes to complete
494 */
495 while (sc->sc_flags & (RDF_OPENING | RDF_CLOSING))
496 (void) tsleep(sc, PRIBIO, "rdopen", 0);
497
498 /*
499 * On first open, get label and partition info.
500 * We may block reading the label, so be careful
501 * to stop any other opens.
502 */
503 if (sc->sc_dk.dk_openmask == 0) {
504 sc->sc_flags |= RDF_OPENING;
505 error = rdgetinfo(sc);
506 sc->sc_flags &= ~RDF_OPENING;
507 wakeup((void *)sc);
508 if (error)
509 return (error);
510 }
511
512 part = RDPART(dev);
513 mask = 1 << part;
514
515 /* Check that the partition exists. */
516 if (part != RAW_PART && (part > sc->sc_dk.dk_label->d_npartitions ||
517 sc->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED))
518 return (ENXIO);
519
520 /* Ensure only one open at a time. */
521 switch (mode) {
522 case S_IFCHR:
523 sc->sc_dk.dk_copenmask |= mask;
524 break;
525 case S_IFBLK:
526 sc->sc_dk.dk_bopenmask |= mask;
527 break;
528 }
529 sc->sc_dk.dk_openmask =
530 sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
531
532 return (0);
533 }
534
535 int
536 rdclose(dev_t dev, int flag, int mode, struct lwp *l)
537 {
538 struct rd_softc *sc;
539 struct disk *dk;
540 int mask, s;
541
542 sc = device_lookup_private(&rd_cd, RDUNIT(dev));
543 if (sc == NULL)
544 return (ENXIO);
545
546 dk = &sc->sc_dk;
547
548 mask = 1 << RDPART(dev);
549 if (mode == S_IFCHR)
550 dk->dk_copenmask &= ~mask;
551 else
552 dk->dk_bopenmask &= ~mask;
553 dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
554 /*
555 * On last close, we wait for all activity to cease since
556 * the label/parition info will become invalid. Since we
557 * might sleep, we must block any opens while we are here.
558 * Note we don't have to about other closes since we know
559 * we are the last one.
560 */
561 if (dk->dk_openmask == 0) {
562 sc->sc_flags |= RDF_CLOSING;
563 s = splbio();
564 while (sc->sc_active) {
565 sc->sc_flags |= RDF_WANTED;
566 (void) tsleep(&sc->sc_tab, PRIBIO, "rdclose", 0);
567 }
568 splx(s);
569 sc->sc_flags &= ~(RDF_CLOSING | RDF_WLABEL);
570 wakeup((void *)sc);
571 }
572 return (0);
573 }
574
575 void
576 rdstrategy(struct buf *bp)
577 {
578 struct rd_softc *sc;
579 struct partition *pinfo;
580 daddr_t bn;
581 int sz, s;
582 int offset;
583
584 sc = device_lookup_private(&rd_cd, RDUNIT(bp->b_dev));
585
586 DPRINTF(RDB_FOLLOW,
587 ("rdstrategy(%p): dev %" PRIx64 ", bn %" PRId64 ", bcount %d, %c\n",
588 bp, bp->b_dev, bp->b_blkno, bp->b_bcount,
589 (bp->b_flags & B_READ) ? 'R' : 'W'));
590
591 bn = bp->b_blkno;
592 sz = howmany(bp->b_bcount, DEV_BSIZE);
593 pinfo = &sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)];
594
595 /* Don't perform partition translation on RAW_PART. */
596 offset = (RDPART(bp->b_dev) == RAW_PART) ? 0 : pinfo->p_offset;
597
598 if (RDPART(bp->b_dev) != RAW_PART) {
599 /*
600 * XXX This block of code belongs in
601 * XXX bounds_check_with_label()
602 */
603
604 if (bn < 0 || bn + sz > pinfo->p_size) {
605 sz = pinfo->p_size - bn;
606 if (sz == 0) {
607 bp->b_resid = bp->b_bcount;
608 goto done;
609 }
610 if (sz < 0) {
611 bp->b_error = EINVAL;
612 goto done;
613 }
614 bp->b_bcount = dbtob(sz);
615 }
616 /*
617 * Check for write to write protected label
618 */
619 if (bn + offset <= LABELSECTOR &&
620 #if LABELSECTOR != 0
621 bn + offset + sz > LABELSECTOR &&
622 #endif
623 !(bp->b_flags & B_READ) && !(sc->sc_flags & RDF_WLABEL)) {
624 bp->b_error = EROFS;
625 goto done;
626 }
627 }
628 bp->b_rawblkno = bn + offset;
629 s = splbio();
630 bufq_put(sc->sc_tab, bp);
631 if (sc->sc_active == 0) {
632 sc->sc_active = 1;
633 rdustart(sc);
634 }
635 splx(s);
636 return;
637 done:
638 biodone(bp);
639 }
640
641 /*
642 * Called from timeout() when handling maintenance releases
643 * callout from timeouts
644 */
645 void
646 rdrestart(void *arg)
647 {
648 int s = splbio();
649 rdustart((struct rd_softc *)arg);
650 splx(s);
651 }
652
653
654 /* called by rdstrategy() to start a block transfer */
655 /* called by rdrestart() when handingly timeouts */
656 /* called by rdintr() */
657 void
658 rdustart(struct rd_softc *sc)
659 {
660 struct buf *bp;
661
662 bp = bufq_peek(sc->sc_tab);
663 sc->sc_addr = bp->b_data;
664 sc->sc_resid = bp->b_bcount;
665 if (gpibrequest(sc->sc_ic, sc->sc_hdl))
666 rdstart(sc);
667 }
668
669 struct buf *
670 rdfinish(struct rd_softc *sc, struct buf *bp)
671 {
672
673 sc->sc_errcnt = 0;
674 (void)bufq_get(sc->sc_tab);
675 bp->b_resid = 0;
676 biodone(bp);
677 gpibrelease(sc->sc_ic, sc->sc_hdl);
678 if ((bp = bufq_peek(sc->sc_tab)) != NULL)
679 return (bp);
680 sc->sc_active = 0;
681 if (sc->sc_flags & RDF_WANTED) {
682 sc->sc_flags &= ~RDF_WANTED;
683 wakeup((void *)&sc->sc_tab);
684 }
685 return (NULL);
686 }
687
688 void
689 rdcallback(void *v, int action)
690 {
691 struct rd_softc *sc = v;
692
693 DPRINTF(RDB_FOLLOW, ("rdcallback: v=%p, action=%d\n", v, action));
694
695 switch (action) {
696 case GPIBCBF_START:
697 rdstart(sc);
698 break;
699 case GPIBCBF_INTR:
700 rdintr(sc);
701 break;
702 #ifdef DEBUG
703 default:
704 DPRINTF(RDB_ERROR, ("rdcallback: unknown action %d\n",
705 action));
706 break;
707 #endif
708 }
709 }
710
711
712 /* called from rdustart() to start a transfer */
713 /* called from gpib interface as the initiator */
714 void
715 rdstart(struct rd_softc *sc)
716 {
717 struct buf *bp = bufq_peek(sc->sc_tab);
718 int slave, punit;
719
720 slave = sc->sc_slave;
721 punit = sc->sc_punit;
722
723 DPRINTF(RDB_FOLLOW, ("rdstart(%s): bp %p, %c\n",
724 device_xname(sc->sc_dev), bp, (bp->b_flags & B_READ) ? 'R' : 'W'));
725
726 again:
727
728 sc->sc_flags |= RDF_SEEK;
729 sc->sc_ioc.c_unit = CS80CMD_SUNIT(punit);
730 sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
731 sc->sc_ioc.c_saddr = CS80CMD_SADDR;
732 sc->sc_ioc.c_hiaddr = htobe16(0);
733 sc->sc_ioc.c_addr = htobe32(RDBTOS(bp->b_rawblkno));
734 sc->sc_ioc.c_nop2 = CS80CMD_NOP;
735 sc->sc_ioc.c_slen = CS80CMD_SLEN;
736 sc->sc_ioc.c_len = htobe32(sc->sc_resid);
737 sc->sc_ioc.c_cmd = bp->b_flags & B_READ ? CS80CMD_READ : CS80CMD_WRITE;
738
739 if (gpibsend(sc->sc_ic, slave, CS80CMD_SCMD, &sc->sc_ioc.c_unit,
740 sizeof(sc->sc_ioc)-1) == sizeof(sc->sc_ioc)-1) {
741 /* Instrumentation. */
742 disk_busy(&sc->sc_dk);
743 iostat_seek(sc->sc_dk.dk_stats);
744 gpibawait(sc->sc_ic);
745 return;
746 }
747 /*
748 * Experience has shown that the gpibwait in this gpibsend will
749 * occasionally timeout. It appears to occur mostly on old 7914
750 * drives with full maintenance tracks. We should probably
751 * integrate this with the backoff code in rderror.
752 */
753
754 DPRINTF(RDB_ERROR,
755 ("rdstart: cmd %x adr %ul blk %" PRId64 " len %d ecnt %d\n",
756 sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr, bp->b_blkno, sc->sc_resid,
757 sc->sc_errcnt));
758
759 sc->sc_flags &= ~RDF_SEEK;
760 cs80reset(device_parent(sc->sc_dev), slave, punit);
761 if (sc->sc_errcnt++ < RDRETRY)
762 goto again;
763 printf("%s: rdstart err: cmd 0x%x sect %uld blk %" PRId64 " len %d\n",
764 device_xname(sc->sc_dev), sc->sc_ioc.c_cmd, sc->sc_ioc.c_addr,
765 bp->b_blkno, sc->sc_resid);
766 bp->b_error = EIO;
767 bp = rdfinish(sc, bp);
768 if (bp) {
769 sc->sc_addr = bp->b_data;
770 sc->sc_resid = bp->b_bcount;
771 if (gpibrequest(sc->sc_ic, sc->sc_hdl))
772 goto again;
773 }
774 }
775
776 void
777 rdintr(struct rd_softc *sc)
778 {
779 struct buf *bp;
780 u_int8_t stat = 13; /* in case gpibrecv fails */
781 int rv, dir, restart, slave;
782
783 slave = sc->sc_slave;
784 bp = bufq_peek(sc->sc_tab);
785
786 DPRINTF(RDB_FOLLOW, ("rdintr(%s): bp %p, %c, flags %x\n",
787 device_xname(sc->sc_dev), bp, (bp->b_flags & B_READ) ? 'R' : 'W',
788 sc->sc_flags));
789
790 disk_unbusy(&sc->sc_dk, (bp->b_bcount - bp->b_resid),
791 (bp->b_flags & B_READ));
792
793 if (sc->sc_flags & RDF_SEEK) {
794 sc->sc_flags &= ~RDF_SEEK;
795 dir = (bp->b_flags & B_READ ? GPIB_READ : GPIB_WRITE);
796 gpibxfer(sc->sc_ic, slave, CS80CMD_EXEC, sc->sc_addr,
797 sc->sc_resid, dir, dir == GPIB_READ);
798 disk_busy(&sc->sc_dk);
799 return;
800 }
801 if ((sc->sc_flags & RDF_SWAIT) == 0) {
802 if (gpibpptest(sc->sc_ic, slave) == 0) {
803 /* Instrumentation. */
804 disk_busy(&sc->sc_dk);
805 sc->sc_flags |= RDF_SWAIT;
806 gpibawait(sc->sc_ic);
807 return;
808 }
809 } else
810 sc->sc_flags &= ~RDF_SWAIT;
811 rv = gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
812 if (rv != 1 || stat) {
813 DPRINTF(RDB_ERROR,
814 ("rdintr: receive failed (rv=%d) or bad stat %d\n", rv,
815 stat));
816 restart = rderror(sc);
817 if (sc->sc_errcnt++ < RDRETRY) {
818 if (restart)
819 rdstart(sc);
820 return;
821 }
822 bp->b_error = EIO;
823 }
824 if (rdfinish(sc, bp) != NULL)
825 rdustart(sc);
826 rnd_add_uint32(&sc->rnd_source, bp->b_blkno);
827 }
828
829 /*
830 * Deal with errors.
831 * Returns 1 if request should be restarted,
832 * 0 if we should just quietly give up.
833 */
834 int
835 rderror(struct rd_softc *sc)
836 {
837 struct cs80_stat css;
838 struct buf *bp;
839 daddr_t hwbn, pbn;
840
841 DPRINTF(RDB_FOLLOW, ("rderror: sc=%p\n", sc));
842
843 if (cs80status(device_parent(sc->sc_dev), sc->sc_slave,
844 sc->sc_punit, &css)) {
845 cs80reset(device_parent(sc->sc_dev), sc->sc_slave,
846 sc->sc_punit);
847 return (1);
848 }
849 #ifdef DEBUG
850 if (rddebug & RDB_ERROR) { /* status info */
851 printf("\n volume: %d, unit: %d\n",
852 (css.c_vu>>4)&0xF, css.c_vu&0xF);
853 printf(" reject 0x%x\n", css.c_ref);
854 printf(" fault 0x%x\n", css.c_fef);
855 printf(" access 0x%x\n", css.c_aef);
856 printf(" info 0x%x\n", css.c_ief);
857 printf(" block, P1-P10: ");
858 printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
859 printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
860 printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
861 }
862 #endif
863 if (css.c_fef & FEF_REXMT)
864 return (1);
865 if (css.c_fef & FEF_PF) {
866 cs80reset(device_parent(sc->sc_dev), sc->sc_slave,
867 sc->sc_punit);
868 return (1);
869 }
870 /*
871 * Unit requests release for internal maintenance.
872 * We just delay awhile and try again later. Use expontially
873 * increasing backoff ala ethernet drivers since we don't really
874 * know how long the maintenance will take. With RDWAITC and
875 * RDRETRY as defined, the range is 1 to 32 seconds.
876 */
877 if (css.c_fef & FEF_IMR) {
878 extern int hz;
879 int rdtimo = RDWAITC << sc->sc_errcnt;
880 DPRINTF(RDB_STATUS,
881 ("%s: internal maintenance, %d-second timeout\n",
882 device_xname(sc->sc_dev), rdtimo));
883 gpibrelease(sc->sc_ic, sc->sc_hdl);
884 callout_reset(&sc->sc_restart_ch, rdtimo * hz, rdrestart, sc);
885 return (0);
886 }
887 /*
888 * Only report error if we have reached the error reporting
889 * threshhold. By default, this will only report after the
890 * retry limit has been exceeded.
891 */
892 if (sc->sc_errcnt < rderrthresh)
893 return (1);
894
895 /*
896 * First conjure up the block number at which the error occurred.
897 */
898 bp = bufq_peek(sc->sc_tab);
899 pbn = sc->sc_dk.dk_label->d_partitions[RDPART(bp->b_dev)].p_offset;
900 if ((css.c_fef & FEF_CU) || (css.c_fef & FEF_DR) ||
901 (css.c_ief & IEF_RRMASK)) {
902 /*
903 * Not all errors report a block number, just use b_blkno.
904 */
905 hwbn = RDBTOS(pbn + bp->b_blkno);
906 pbn = bp->b_blkno;
907 } else {
908 hwbn = css.c_blk;
909 pbn = RDSTOB(hwbn) - pbn;
910 }
911 #ifdef DEBUG
912 if (rddebug & RDB_ERROR) { /* status info */
913 printf("\n volume: %d, unit: %d\n",
914 (css.c_vu>>4)&0xF, css.c_vu&0xF);
915 printf(" reject 0x%x\n", css.c_ref);
916 printf(" fault 0x%x\n", css.c_fef);
917 printf(" access 0x%x\n", css.c_aef);
918 printf(" info 0x%x\n", css.c_ief);
919 printf(" block, P1-P10: ");
920 printf(" block: %" PRId64 ", P1-P10: ", hwbn);
921 printf("0x%x", *(u_int32_t *)&css.c_raw[0]);
922 printf("0x%x", *(u_int32_t *)&css.c_raw[4]);
923 printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
924 }
925 #endif
926 #ifdef DEBUG
927 if (rddebug & RDB_ERROR) { /* command */
928 printf(" ioc: ");
929 printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_pad);
930 printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_hiaddr);
931 printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_addr);
932 printf("0x%x", *(u_int16_t *)&sc->sc_ioc.c_nop2);
933 printf("0x%x", *(u_int32_t *)&sc->sc_ioc.c_len);
934 printf("0x%x\n", *(u_int16_t *)&sc->sc_ioc.c_cmd);
935 return (1);
936 }
937 #endif
938 /*
939 * Now output a generic message suitable for badsect.
940 * Note that we don't use harderr because it just prints
941 * out b_blkno which is just the beginning block number
942 * of the transfer, not necessary where the error occurred.
943 */
944 printf("%s%c: hard error, sector number %" PRId64 "\n",
945 device_xname(sc->sc_dev), 'a'+RDPART(bp->b_dev), pbn);
946 /*
947 * Now report the status as returned by the hardware with
948 * attempt at interpretation.
949 */
950 printf("%s %s error:", device_xname(sc->sc_dev),
951 (bp->b_flags & B_READ) ? "read" : "write");
952 printf(" unit %d, volume %d R0x%x F0x%x A0x%x I0x%x\n",
953 css.c_vu&0xF, (css.c_vu>>4)&0xF,
954 css.c_ref, css.c_fef, css.c_aef, css.c_ief);
955 printf("P1-P10: ");
956 printf("0x%x ", *(u_int32_t *)&css.c_raw[0]);
957 printf("0x%x ", *(u_int32_t *)&css.c_raw[4]);
958 printf("0x%x\n", *(u_int16_t *)&css.c_raw[8]);
959
960 return (1);
961 }
962
963 int
964 rdread(dev_t dev, struct uio *uio, int flags)
965 {
966
967 return (physio(rdstrategy, NULL, dev, B_READ, minphys, uio));
968 }
969
970 int
971 rdwrite(dev_t dev, struct uio *uio, int flags)
972 {
973
974 return (physio(rdstrategy, NULL, dev, B_WRITE, minphys, uio));
975 }
976
977 int
978 rdioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
979 {
980 struct rd_softc *sc;
981 struct disklabel *lp;
982 int error, flags;
983
984 sc = device_lookup_private(&rd_cd, RDUNIT(dev));
985 if (sc == NULL)
986 return (ENXIO);
987 lp = sc->sc_dk.dk_label;
988
989 DPRINTF(RDB_FOLLOW, ("rdioctl: sc=%p\n", sc));
990
991 error = disk_ioctl(&sc->sc_dk, dev, cmd, data, flag, l);
992 if (error != EPASSTHROUGH)
993 return error;
994
995 switch (cmd) {
996 case DIOCWLABEL:
997 if ((flag & FWRITE) == 0)
998 return (EBADF);
999 if (*(int *)data)
1000 sc->sc_flags |= RDF_WLABEL;
1001 else
1002 sc->sc_flags &= ~RDF_WLABEL;
1003 return (0);
1004
1005 case DIOCSDINFO:
1006 if ((flag & FWRITE) == 0)
1007 return (EBADF);
1008 return (setdisklabel(lp, (struct disklabel *)data,
1009 (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
1010 (struct cpu_disklabel *)0));
1011
1012 case DIOCWDINFO:
1013 if ((flag & FWRITE) == 0)
1014 return (EBADF);
1015 error = setdisklabel(lp, (struct disklabel *)data,
1016 (sc->sc_flags & RDF_WLABEL) ? 0 : sc->sc_dk.dk_openmask,
1017 (struct cpu_disklabel *)0);
1018 if (error)
1019 return (error);
1020 flags = sc->sc_flags;
1021 sc->sc_flags = RDF_ALIVE | RDF_WLABEL;
1022 error = writedisklabel(RDLABELDEV(dev), rdstrategy, lp,
1023 (struct cpu_disklabel *)0);
1024 sc->sc_flags = flags;
1025 return (error);
1026
1027 case DIOCGDEFLABEL:
1028 rdgetdefaultlabel(sc, (struct disklabel *)data);
1029 return (0);
1030 }
1031 return (EINVAL);
1032 }
1033
1034 void
1035 rdgetdefaultlabel(struct rd_softc *sc, struct disklabel *lp)
1036 {
1037 int type = sc->sc_type;
1038
1039 memset((void *)lp, 0, sizeof(struct disklabel));
1040
1041 lp->d_type = DKTYPE_HPIB /* DKTYPE_GPIB */;
1042 lp->d_secsize = DEV_BSIZE;
1043 lp->d_nsectors = rdidentinfo[type].ri_nbpt;
1044 lp->d_ntracks = rdidentinfo[type].ri_ntpc;
1045 lp->d_ncylinders = rdidentinfo[type].ri_ncyl;
1046 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1047 lp->d_secperunit = lp->d_ncylinders * lp->d_secpercyl;
1048
1049 strncpy(lp->d_typename, rdidentinfo[type].ri_desc, 16);
1050 strncpy(lp->d_packname, "fictitious", 16);
1051 lp->d_rpm = 3000;
1052 lp->d_interleave = 1;
1053 lp->d_flags = 0;
1054
1055 lp->d_partitions[RAW_PART].p_offset = 0;
1056 lp->d_partitions[RAW_PART].p_size =
1057 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1058 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1059 lp->d_npartitions = RAW_PART + 1;
1060
1061 lp->d_magic = DISKMAGIC;
1062 lp->d_magic2 = DISKMAGIC;
1063 lp->d_checksum = dkcksum(lp);
1064 }
1065
1066 int
1067 rdsize(dev_t dev)
1068 {
1069 struct rd_softc *sc;
1070 int psize, didopen = 0;
1071
1072 sc = device_lookup_private(&rd_cd, RDUNIT(dev));
1073 if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
1074 return (-1);
1075
1076 /*
1077 * We get called very early on (via swapconf)
1078 * without the device being open so we may need
1079 * to handle it here.
1080 */
1081 if (sc->sc_dk.dk_openmask == 0) {
1082 if (rdopen(dev, FREAD | FWRITE, S_IFBLK, NULL))
1083 return (-1);
1084 didopen = 1;
1085 }
1086 psize = sc->sc_dk.dk_label->d_partitions[RDPART(dev)].p_size *
1087 (sc->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1088 if (didopen)
1089 (void) rdclose(dev, FREAD | FWRITE, S_IFBLK, NULL);
1090 return (psize);
1091 }
1092
1093
1094 static int rddoingadump; /* simple mutex */
1095
1096 /*
1097 * Non-interrupt driven, non-dma dump routine.
1098 */
1099 int
1100 rddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1101 {
1102 struct rd_softc *sc;
1103 int sectorsize; /* size of a disk sector */
1104 int nsects; /* number of sectors in partition */
1105 int sectoff; /* sector offset of partition */
1106 int totwrt; /* total number of sectors left to write */
1107 int nwrt; /* current number of sectors to write */
1108 int slave;
1109 struct disklabel *lp;
1110 u_int8_t stat;
1111
1112 /* Check for recursive dump; if so, punt. */
1113 if (rddoingadump)
1114 return (EFAULT);
1115 rddoingadump = 1;
1116
1117 sc = device_lookup_private(&rd_cd, RDUNIT(dev));
1118 if (sc == NULL || (sc->sc_flags & RDF_ALIVE) == 0)
1119 return (ENXIO);
1120
1121 DPRINTF(RDB_FOLLOW, ("rddump: sc=%p\n", sc));
1122
1123 slave = sc->sc_slave;
1124
1125 /*
1126 * Convert to disk sectors. Request must be a multiple of size.
1127 */
1128 lp = sc->sc_dk.dk_label;
1129 sectorsize = lp->d_secsize;
1130 if ((size % sectorsize) != 0)
1131 return (EFAULT);
1132 totwrt = size / sectorsize;
1133 blkno = dbtob(blkno) / sectorsize; /* blkno in DEV_BSIZE units */
1134
1135 nsects = lp->d_partitions[RDPART(dev)].p_size;
1136 sectoff = lp->d_partitions[RDPART(dev)].p_offset;
1137
1138 /* Check transfer bounds against partition size. */
1139 if ((blkno < 0) || (blkno + totwrt) > nsects)
1140 return (EINVAL);
1141
1142 /* Offset block number to start of partition. */
1143 blkno += sectoff;
1144
1145 while (totwrt > 0) {
1146 nwrt = totwrt; /* XXX */
1147 #ifndef RD_DUMP_NOT_TRUSTED
1148 /*
1149 * Fill out and send GPIB command.
1150 */
1151 sc->sc_ioc.c_unit = CS80CMD_SUNIT(sc->sc_punit);
1152 sc->sc_ioc.c_volume = CS80CMD_SVOL(0);
1153 sc->sc_ioc.c_saddr = CS80CMD_SADDR;
1154 sc->sc_ioc.c_hiaddr = 0;
1155 sc->sc_ioc.c_addr = RDBTOS(blkno);
1156 sc->sc_ioc.c_nop2 = CS80CMD_NOP;
1157 sc->sc_ioc.c_slen = CS80CMD_SLEN;
1158 sc->sc_ioc.c_len = nwrt * sectorsize;
1159 sc->sc_ioc.c_cmd = CS80CMD_WRITE;
1160 (void) gpibsend(sc->sc_ic, slave, CS80CMD_SCMD,
1161 &sc->sc_ioc.c_unit, sizeof(sc->sc_ioc)-3);
1162 if (gpibswait(sc->sc_ic, slave))
1163 return (EIO);
1164 /*
1165 * Send the data.
1166 */
1167 (void) gpibsend(sc->sc_ic, slave, CS80CMD_EXEC, va,
1168 nwrt * sectorsize);
1169 (void) gpibswait(sc->sc_ic, slave);
1170 (void) gpibrecv(sc->sc_ic, slave, CS80CMD_QSTAT, &stat, 1);
1171 if (stat)
1172 return (EIO);
1173 #else /* RD_DUMP_NOT_TRUSTED */
1174 /* Let's just talk about this first... */
1175 printf("%s: dump addr %p, blk %d\n", device_xname(sc->sc_dev),
1176 va, blkno);
1177 delay(500 * 1000); /* half a second */
1178 #endif /* RD_DUMP_NOT_TRUSTED */
1179
1180 /* update block count */
1181 totwrt -= nwrt;
1182 blkno += nwrt;
1183 va = (char *)va + sectorsize * nwrt;
1184 }
1185 rddoingadump = 0;
1186 return (0);
1187 }
1188