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