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