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