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