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