1 /* $NetBSD: hdc9224.c,v 1.30 2003/07/15 02:15:06 lukem Exp $ */ 2 /* 3 * Copyright (c) 1996 Ludd, University of Lule}, Sweden. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to Ludd by Bertram Barth. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed at Ludd, University of 19 * Lule}, Sweden and its contributors. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * with much help from (in alphabetical order): 37 * Jeremy 38 * Roger Ivie 39 * Rick Macklem 40 * Mike Young 41 * 42 * Rewritten by Ragge 25 Jun 2000. New features: 43 * - Uses interrupts instead of polling to signal ready. 44 * - Can cooperate with the SCSI routines WRT. the DMA area. 45 * 46 * TODO: 47 * - Floppy support missing. 48 * - Bad block forwarding missing. 49 * - Statistics collection. 50 */ 51 #undef RDDEBUG 52 53 #include <sys/cdefs.h> 54 __KERNEL_RCSID(0, "$NetBSD: hdc9224.c,v 1.30 2003/07/15 02:15:06 lukem Exp $"); 55 56 #include <sys/param.h> 57 #include <sys/systm.h> 58 #include <sys/kernel.h> 59 #include <sys/conf.h> 60 #include <sys/file.h> 61 #include <sys/stat.h> 62 #include <sys/ioctl.h> 63 #include <sys/buf.h> 64 #include <sys/proc.h> 65 #include <sys/user.h> 66 #include <sys/device.h> 67 #include <sys/disklabel.h> 68 #include <sys/disk.h> 69 #include <sys/syslog.h> 70 #include <sys/reboot.h> 71 72 #include <uvm/uvm_extern.h> 73 74 #include <ufs/ufs/dinode.h> /* For BBSIZE */ 75 #include <ufs/ffs/fs.h> 76 77 #include <machine/pte.h> 78 #include <machine/sid.h> 79 #include <machine/cpu.h> 80 #include <machine/uvax.h> 81 #include <machine/ka410.h> 82 #include <machine/vsbus.h> 83 #include <machine/rpb.h> 84 #include <machine/scb.h> 85 86 #include <dev/mscp/mscp.h> /* For DEC disk encoding */ 87 88 #include <vax/vsa/hdc9224.h> 89 90 #include "ioconf.h" 91 #include "locators.h" 92 93 94 /* 95 * on-disk geometry block 96 */ 97 #define _aP __attribute__ ((packed)) /* force byte-alignment */ 98 struct rdgeom { 99 char mbz[10]; /* 10 bytes of zero */ 100 long xbn_count _aP; /* number of XBNs */ 101 long dbn_count _aP; /* number of DBNs */ 102 long lbn_count _aP; /* number of LBNs (Logical-Block-Numbers) */ 103 long rbn_count _aP; /* number of RBNs (Replacement-Block-Numbers) */ 104 short nspt; /* number of sectors per track */ 105 short ntracks; /* number of tracks */ 106 short ncylinders; /* number of cylinders */ 107 short precomp; /* first cylinder for write precompensation */ 108 short reduced; /* first cylinder for reduced write current */ 109 short seek_rate; /* seek rate or zero for buffered seeks */ 110 short crc_eec; /* 0 if CRC, 1 if ECC is being used */ 111 short rct; /* "replacement control table" (RCT) */ 112 short rct_ncopies; /* number of copies of the RCT */ 113 long media_id _aP; /* media identifier */ 114 short interleave; /* sector-to-sector interleave */ 115 short headskew; /* head-to-head skew */ 116 short cylskew; /* cylinder-to-cylinder skew */ 117 short gap0_size; /* size of GAP 0 in the MFM format */ 118 short gap1_size; /* size of GAP 1 in the MFM format */ 119 short gap2_size; /* size of GAP 2 in the MFM format */ 120 short gap3_size; /* size of GAP 3 in the MFM format */ 121 short sync_value; /* sync value used when formatting */ 122 char reserved[32]; /* reserved for use by the RQDX formatter */ 123 short serial_number; /* serial number */ 124 #if 0 /* we don't need these 412 useless bytes ... */ 125 char fill[412-2]; /* Filler bytes to the end of the block */ 126 short checksum; /* checksum over the XBN */ 127 #endif 128 }; 129 130 /* 131 * Software status 132 */ 133 struct rdsoftc { 134 struct device sc_dev; /* must be here! (pseudo-OOP:) */ 135 struct disk sc_disk; /* disklabel etc. */ 136 struct rdgeom sc_xbn; /* on-disk geometry information */ 137 int sc_drive; /* physical unit number */ 138 }; 139 140 struct hdcsoftc { 141 struct device sc_dev; /* must be here (pseudo-OOP:) */ 142 struct evcnt sc_intrcnt; 143 struct vsbus_dma sc_vd; 144 vaddr_t sc_regs; /* register addresses */ 145 struct bufq_state sc_q; 146 struct buf *sc_active; 147 struct hdc9224_UDCreg sc_creg; /* (command) registers to be written */ 148 struct hdc9224_UDCreg sc_sreg; /* (status) registers being read */ 149 caddr_t sc_dmabase; /* */ 150 int sc_dmasize; 151 caddr_t sc_bufaddr; /* Current in-core address */ 152 int sc_diskblk; /* Current block on disk */ 153 int sc_bytecnt; /* How much left to transfer */ 154 int sc_xfer; /* Current transfer size */ 155 int sc_retries; 156 volatile u_char sc_status; /* last status from interrupt */ 157 char sc_intbit; 158 }; 159 160 struct hdc_attach_args { 161 int ha_drive; 162 }; 163 164 /* 165 * prototypes for (almost) all the internal routines 166 */ 167 static int hdcmatch(struct device *, struct cfdata *, void *); 168 static void hdcattach(struct device *, struct device *, void *); 169 static int hdcprint(void *, const char *); 170 static int rdmatch(struct device *, struct cfdata *, void *); 171 static void rdattach(struct device *, struct device *, void *); 172 static void hdcintr(void *); 173 static int hdc_command(struct hdcsoftc *, int); 174 static void rd_readgeom(struct hdcsoftc *, struct rdsoftc *); 175 #ifdef RDDEBUG 176 static void hdc_printgeom( struct rdgeom *); 177 #endif 178 static void hdc_writeregs(struct hdcsoftc *); 179 static void hdcstart(struct hdcsoftc *, struct buf *); 180 static int hdc_rdselect(struct hdcsoftc *, int); 181 static void rdmakelabel(struct disklabel *, struct rdgeom *); 182 static void hdc_writeregs(struct hdcsoftc *); 183 static void hdc_readregs(struct hdcsoftc *); 184 static void hdc_qstart(void *); 185 186 CFATTACH_DECL(hdc, sizeof(struct hdcsoftc), 187 hdcmatch, hdcattach, NULL, NULL); 188 189 CFATTACH_DECL(rd, sizeof(struct rdsoftc), 190 rdmatch, rdattach, NULL, NULL); 191 192 dev_type_open(rdopen); 193 dev_type_close(rdclose); 194 dev_type_read(rdread); 195 dev_type_write(rdwrite); 196 dev_type_ioctl(rdioctl); 197 dev_type_strategy(rdstrategy); 198 dev_type_size(rdsize); 199 200 const struct bdevsw rd_bdevsw = { 201 rdopen, rdclose, rdstrategy, rdioctl, nulldump, rdsize, D_DISK 202 }; 203 204 const struct cdevsw rd_cdevsw = { 205 rdopen, rdclose, rdread, rdwrite, rdioctl, 206 nostop, notty, nopoll, nommap, nokqfilter, D_DISK 207 }; 208 209 /* At least 0.7 uS between register accesses */ 210 static int rd_dmasize, inq = 0; 211 static int u; 212 #define WAIT asm("movl %0,%0;movl %0,%0;movl %0,%0; movl %0,%0" :: "m"(u)) 213 214 #define HDC_WREG(x) *(volatile char *)(sc->sc_regs) = (x) 215 #define HDC_RREG *(volatile char *)(sc->sc_regs) 216 #define HDC_WCMD(x) *(volatile char *)(sc->sc_regs + 4) = (x) 217 #define HDC_RSTAT *(volatile char *)(sc->sc_regs + 4) 218 219 /* 220 * new-config's hdcmatch() is similiar to old-config's hdcprobe(), 221 * thus we probe for the existence of the controller and reset it. 222 * NB: we can't initialize the controller yet, since space for hdcsoftc 223 * is not yet allocated. Thus we do this in hdcattach()... 224 */ 225 int 226 hdcmatch(struct device *parent, struct cfdata *cf, void *aux) 227 { 228 struct vsbus_attach_args *va = aux; 229 volatile char *hdc_csr = (char *)va->va_addr; 230 int i; 231 232 u = 8; /* !!! - GCC */ 233 234 if (vax_boardtype == VAX_BTYP_49 || vax_boardtype == VAX_BTYP_46 235 || vax_boardtype == VAX_BTYP_48 || vax_boardtype == VAX_BTYP_53) 236 return 0; 237 238 hdc_csr[4] = DKC_CMD_RESET; /* reset chip */ 239 for (i = 0; i < 1000; i++) { 240 DELAY(1000); 241 if (hdc_csr[4] & DKC_ST_DONE) 242 break; 243 } 244 if (i == 100) 245 return 0; /* No response to reset */ 246 247 hdc_csr[4] = DKC_CMD_SETREGPTR|UDC_TERM; 248 WAIT; 249 hdc_csr[0] = UDC_TC_CRCPRE|UDC_TC_INTDONE; 250 WAIT; 251 hdc_csr[4] = DKC_CMD_DRDESELECT; /* Should be harmless */ 252 DELAY(1000); 253 return (1); 254 } 255 256 int 257 hdcprint(void *aux, const char *name) 258 { 259 struct hdc_attach_args *ha = aux; 260 261 if (name) 262 aprint_normal ("RD?? at %s drive %d", name, ha->ha_drive); 263 return UNCONF; 264 } 265 266 /* 267 * hdc_attach() probes for all possible devices 268 */ 269 void 270 hdcattach(struct device *parent, struct device *self, void *aux) 271 { 272 struct vsbus_attach_args *va = aux; 273 struct hdcsoftc *sc = (void *)self; 274 struct hdc_attach_args ha; 275 int status, i; 276 277 printf ("\n"); 278 /* 279 * Get interrupt vector, enable instrumentation. 280 */ 281 scb_vecalloc(va->va_cvec, hdcintr, sc, SCB_ISTACK, &sc->sc_intrcnt); 282 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 283 self->dv_xname, "intr"); 284 285 sc->sc_regs = vax_map_physmem(va->va_paddr, 1); 286 sc->sc_dmabase = (caddr_t)va->va_dmaaddr; 287 sc->sc_dmasize = va->va_dmasize; 288 sc->sc_intbit = va->va_maskno; 289 rd_dmasize = min(MAXPHYS, sc->sc_dmasize); /* Used in rd_minphys */ 290 291 sc->sc_vd.vd_go = hdc_qstart; 292 sc->sc_vd.vd_arg = sc; 293 /* 294 * Reset controller. 295 */ 296 HDC_WCMD(DKC_CMD_RESET); 297 DELAY(1000); 298 status = HDC_RSTAT; 299 if (status != (DKC_ST_DONE|DKC_TC_SUCCESS)) { 300 printf("%s: RESET failed, status 0x%x\n", 301 sc->sc_dev.dv_xname, status); 302 return; 303 } 304 bufq_alloc(&sc->sc_q, BUFQ_DISKSORT|BUFQ_SORT_CYLINDER); 305 306 /* 307 * now probe for all possible hard drives 308 */ 309 for (i = 0; i < 4; i++) { 310 if (i == 2) /* Floppy, needs special handling */ 311 continue; 312 HDC_WCMD(DKC_CMD_DRSELECT | i); 313 DELAY(1000); 314 status = HDC_RSTAT; 315 ha.ha_drive = i; 316 if ((status & DKC_ST_TERMCOD) == DKC_TC_SUCCESS) 317 config_found(self, (void *)&ha, hdcprint); 318 } 319 } 320 321 /* 322 * rdmatch() probes for the existence of a RD-type disk/floppy 323 */ 324 int 325 rdmatch(parent, cf, aux) 326 struct device *parent; 327 struct cfdata *cf; 328 void *aux; 329 { 330 struct hdc_attach_args *ha = aux; 331 332 if (cf->cf_loc[HDCCF_DRIVE] != HDCCF_DRIVE_DEFAULT && 333 cf->cf_loc[HDCCF_DRIVE] != ha->ha_drive) 334 return 0; 335 336 if (ha->ha_drive == 2) /* Always floppy, not supported */ 337 return 0; 338 339 return 1; 340 } 341 342 void 343 rdattach(struct device *parent, struct device *self, void *aux) 344 { 345 struct hdcsoftc *sc = (void*)parent; 346 struct rdsoftc *rd = (void*)self; 347 struct hdc_attach_args *ha = aux; 348 struct disklabel *dl; 349 const char *msg; 350 351 rd->sc_drive = ha->ha_drive; 352 /* 353 * Initialize and attach the disk structure. 354 */ 355 rd->sc_disk.dk_name = rd->sc_dev.dv_xname; 356 disk_attach(&rd->sc_disk); 357 358 /* 359 * if it's not a floppy then evaluate the on-disk geometry. 360 * if necessary correct the label... 361 */ 362 rd_readgeom(sc, rd); 363 disk_printtype(rd->sc_drive, rd->sc_xbn.media_id); 364 dl = rd->sc_disk.dk_label; 365 rdmakelabel(dl, &rd->sc_xbn); 366 printf("%s", rd->sc_dev.dv_xname); 367 msg = readdisklabel(MAKEDISKDEV(cdevsw_lookup_major(&rd_cdevsw), 368 rd->sc_dev.dv_unit, RAW_PART), 369 rdstrategy, dl, NULL); 370 if (msg) 371 printf(": %s", msg); 372 printf(": size %d sectors\n", dl->d_secperunit); 373 #ifdef RDDEBUG 374 hdc_printgeom(&rd->sc_xbn); 375 #endif 376 } 377 378 void 379 hdcintr(void *arg) 380 { 381 struct hdcsoftc *sc = arg; 382 struct buf *bp; 383 384 sc->sc_status = HDC_RSTAT; 385 if (sc->sc_active == 0) 386 return; /* Complain? */ 387 388 if ((sc->sc_status & (DKC_ST_INTPEND|DKC_ST_DONE)) != 389 (DKC_ST_INTPEND|DKC_ST_DONE)) 390 return; /* Why spurious ints sometimes??? */ 391 392 bp = sc->sc_active; 393 sc->sc_active = 0; 394 if ((sc->sc_status & DKC_ST_TERMCOD) != DKC_TC_SUCCESS) { 395 int i; 396 u_char *g = (u_char *)&sc->sc_sreg; 397 398 if (sc->sc_retries++ < 3) { /* Allow 3 retries */ 399 hdcstart(sc, bp); 400 return; 401 } 402 printf("%s: failed, status 0x%x\n", 403 sc->sc_dev.dv_xname, sc->sc_status); 404 hdc_readregs(sc); 405 for (i = 0; i < 10; i++) 406 printf("%i: %x\n", i, g[i]); 407 bp->b_flags |= B_ERROR; 408 bp->b_error = ENXIO; 409 bp->b_resid = bp->b_bcount; 410 biodone(bp); 411 vsbus_dma_intr(); 412 return; 413 } 414 415 if (bp->b_flags & B_READ) { 416 vsbus_copytoproc(bp->b_proc, sc->sc_dmabase, sc->sc_bufaddr, 417 sc->sc_xfer); 418 } 419 sc->sc_diskblk += (sc->sc_xfer/DEV_BSIZE); 420 sc->sc_bytecnt -= sc->sc_xfer; 421 sc->sc_bufaddr += sc->sc_xfer; 422 423 if (sc->sc_bytecnt == 0) { /* Finished transfer */ 424 biodone(bp); 425 vsbus_dma_intr(); 426 } else 427 hdcstart(sc, bp); 428 } 429 430 /* 431 * 432 */ 433 void 434 rdstrategy(struct buf *bp) 435 { 436 struct rdsoftc *rd; 437 struct hdcsoftc *sc; 438 struct disklabel *lp; 439 int unit, s; 440 441 unit = DISKUNIT(bp->b_dev); 442 if (unit > rd_cd.cd_ndevs || (rd = rd_cd.cd_devs[unit]) == NULL) { 443 bp->b_error = ENXIO; 444 bp->b_flags |= B_ERROR; 445 goto done; 446 } 447 sc = (void *)rd->sc_dev.dv_parent; 448 449 lp = rd->sc_disk.dk_label; 450 if ((bounds_check_with_label(&rd->sc_disk, bp, 1)) <= 0) 451 goto done; 452 453 if (bp->b_bcount == 0) 454 goto done; 455 456 bp->b_rawblkno = 457 bp->b_blkno + lp->d_partitions[DISKPART(bp->b_dev)].p_offset; 458 bp->b_cylinder = bp->b_rawblkno / lp->d_secpercyl; 459 460 s = splbio(); 461 BUFQ_PUT(&sc->sc_q, bp); 462 if (inq == 0) { 463 inq = 1; 464 vsbus_dma_start(&sc->sc_vd); 465 } 466 splx(s); 467 return; 468 469 done: biodone(bp); 470 } 471 472 void 473 hdc_qstart(void *arg) 474 { 475 struct hdcsoftc *sc = arg; 476 477 inq = 0; 478 479 hdcstart(sc, 0); 480 if (BUFQ_PEEK(&sc->sc_q)) { 481 vsbus_dma_start(&sc->sc_vd); /* More to go */ 482 inq = 1; 483 } 484 } 485 486 void 487 hdcstart(struct hdcsoftc *sc, struct buf *ob) 488 { 489 struct hdc9224_UDCreg *p = &sc->sc_creg; 490 struct disklabel *lp; 491 struct rdsoftc *rd; 492 struct buf *bp; 493 int cn, sn, tn, bn, blks; 494 volatile char ch; 495 496 if (sc->sc_active) 497 return; /* Already doing something */ 498 499 500 if (ob == 0) { 501 bp = BUFQ_GET(&sc->sc_q); 502 if (bp == NULL) 503 return; /* Nothing to do */ 504 sc->sc_bufaddr = bp->b_data; 505 sc->sc_diskblk = bp->b_rawblkno; 506 sc->sc_bytecnt = bp->b_bcount; 507 sc->sc_retries = 0; 508 bp->b_resid = 0; 509 } else 510 bp = ob; 511 512 rd = rd_cd.cd_devs[DISKUNIT(bp->b_dev)]; 513 hdc_rdselect(sc, rd->sc_drive); 514 sc->sc_active = bp; 515 516 bn = sc->sc_diskblk; 517 lp = rd->sc_disk.dk_label; 518 if (bn) { 519 cn = bn / lp->d_secpercyl; 520 sn = bn % lp->d_secpercyl; 521 tn = sn / lp->d_nsectors; 522 sn = sn % lp->d_nsectors; 523 } else 524 cn = sn = tn = 0; 525 526 cn++; /* first cylinder is reserved */ 527 528 bzero(p, sizeof(struct hdc9224_UDCreg)); 529 530 /* 531 * Tricky thing: the controller do itself only increase the sector 532 * number, not the track or cylinder number. Therefore the driver 533 * is not allowed to have transfers that crosses track boundaries. 534 */ 535 blks = sc->sc_bytecnt/DEV_BSIZE; 536 if ((sn + blks) > lp->d_nsectors) 537 blks = lp->d_nsectors - sn; 538 539 p->udc_dsect = sn; 540 p->udc_dcyl = cn & 0xff; 541 p->udc_dhead = ((cn >> 4) & 0x70) | tn; 542 p->udc_scnt = blks; 543 544 p->udc_rtcnt = UDC_RC_RTRYCNT; 545 p->udc_mode = UDC_MD_HDD; 546 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWRFLT; 547 hdc_writeregs(sc); 548 549 /* Count up vars */ 550 sc->sc_xfer = blks * DEV_BSIZE; 551 552 ch = HDC_RSTAT; /* Avoid pending interrupts */ 553 WAIT; 554 vsbus_clrintr(sc->sc_intbit); /* Clear pending int's */ 555 556 if (bp->b_flags & B_READ) { 557 HDC_WCMD(DKC_CMD_READ_HDD); 558 } else { 559 vsbus_copyfromproc(bp->b_proc, sc->sc_bufaddr, sc->sc_dmabase, 560 sc->sc_xfer); 561 HDC_WCMD(DKC_CMD_WRITE_HDD); 562 } 563 } 564 565 void 566 rd_readgeom(struct hdcsoftc *sc, struct rdsoftc *rd) 567 { 568 struct hdc9224_UDCreg *p = &sc->sc_creg; 569 570 hdc_rdselect(sc, rd->sc_drive); /* select drive right now */ 571 572 bzero(p, sizeof(struct hdc9224_UDCreg)); 573 574 p->udc_scnt = 1; 575 p->udc_rtcnt = UDC_RC_RTRYCNT; 576 p->udc_mode = UDC_MD_HDD; 577 p->udc_term = UDC_TC_CRCPRE|UDC_TC_INTDONE|UDC_TC_TDELDAT|UDC_TC_TWPROT; 578 hdc_writeregs(sc); 579 sc->sc_status = 0; 580 HDC_WCMD(DKC_CMD_READ_HDD|2); 581 while ((sc->sc_status & DKC_ST_INTPEND) == 0) 582 ; 583 bcopy(sc->sc_dmabase, &rd->sc_xbn, sizeof(struct rdgeom)); 584 } 585 586 #ifdef RDDEBUG 587 /* 588 * display the contents of the on-disk geometry structure 589 */ 590 void 591 hdc_printgeom(p) 592 struct rdgeom *p; 593 { 594 printf ("**DiskData** XBNs: %ld, DBNs: %ld, LBNs: %ld, RBNs: %ld\n", 595 p->xbn_count, p->dbn_count, p->lbn_count, p->rbn_count); 596 printf ("sec/track: %d, tracks: %d, cyl: %d, precomp/reduced: %d/%d\n", 597 p->nspt, p->ntracks, p->ncylinders, p->precomp, p->reduced); 598 printf ("seek-rate: %d, crc/eec: %s, RCT: %d, RCT-copies: %d\n", 599 p->seek_rate, p->crc_eec?"EEC":"CRC", p->rct, p->rct_ncopies); 600 printf ("media-ID: %lx, interleave: %d, headskew: %d, cylskew: %d\n", 601 p->media_id, p->interleave, p->headskew, p->cylskew); 602 printf ("gap0: %d, gap1: %d, gap2: %d, gap3: %d, sync-value: %d\n", 603 p->gap0_size, p->gap1_size, p->gap2_size, p->gap3_size, 604 p->sync_value); 605 } 606 #endif 607 608 /* 609 * Return the size of a partition, if known, or -1 if not. 610 */ 611 int 612 rdsize(dev_t dev) 613 { 614 struct rdsoftc *rd; 615 int unit = DISKUNIT(dev); 616 int size; 617 618 if (unit >= rd_cd.cd_ndevs || rd_cd.cd_devs[unit] == 0) 619 return -1; 620 rd = rd_cd.cd_devs[unit]; 621 size = rd->sc_disk.dk_label->d_partitions[DISKPART(dev)].p_size * 622 (rd->sc_disk.dk_label->d_secsize / DEV_BSIZE); 623 624 return (size); 625 } 626 627 /* 628 * 629 */ 630 int 631 rdopen(dev_t dev, int flag, int fmt, struct proc *p) 632 { 633 struct rdsoftc *rd; 634 int unit, part; 635 636 unit = DISKUNIT(dev); 637 if (unit >= rd_cd.cd_ndevs) 638 return ENXIO; 639 rd = rd_cd.cd_devs[unit]; 640 if (rd == 0) 641 return ENXIO; 642 643 part = DISKPART(dev); 644 if (part >= rd->sc_disk.dk_label->d_npartitions) 645 return ENXIO; 646 647 switch (fmt) { 648 case S_IFCHR: 649 rd->sc_disk.dk_copenmask |= (1 << part); 650 break; 651 case S_IFBLK: 652 rd->sc_disk.dk_bopenmask |= (1 << part); 653 break; 654 } 655 rd->sc_disk.dk_openmask = 656 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask; 657 658 return 0; 659 } 660 661 /* 662 * 663 */ 664 int 665 rdclose(dev_t dev, int flag, int fmt, struct proc *p) 666 { 667 struct rdsoftc *rd; 668 int part; 669 670 rd = rd_cd.cd_devs[DISKUNIT(dev)]; 671 part = DISKPART(dev); 672 673 switch (fmt) { 674 case S_IFCHR: 675 rd->sc_disk.dk_copenmask &= ~(1 << part); 676 break; 677 case S_IFBLK: 678 rd->sc_disk.dk_bopenmask &= ~(1 << part); 679 break; 680 } 681 rd->sc_disk.dk_openmask = 682 rd->sc_disk.dk_copenmask | rd->sc_disk.dk_bopenmask; 683 684 return (0); 685 } 686 687 /* 688 * 689 */ 690 int 691 rdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 692 { 693 struct rdsoftc *rd = rd_cd.cd_devs[DISKUNIT(dev)]; 694 struct disklabel *lp = rd->sc_disk.dk_label; 695 int err = 0; 696 697 switch (cmd) { 698 case DIOCGDINFO: 699 bcopy(lp, addr, sizeof (struct disklabel)); 700 break; 701 702 case DIOCGPART: 703 ((struct partinfo *)addr)->disklab = lp; 704 ((struct partinfo *)addr)->part = 705 &lp->d_partitions[DISKPART(dev)]; 706 break; 707 708 case DIOCWDINFO: 709 case DIOCSDINFO: 710 if ((flag & FWRITE) == 0) 711 return EBADF; 712 else 713 err = (cmd == DIOCSDINFO ? 714 setdisklabel(lp, (struct disklabel *)addr, 0, 0) : 715 writedisklabel(dev, rdstrategy, lp, 0)); 716 break; 717 718 case DIOCGDEFLABEL: 719 bzero(lp, sizeof(struct disklabel)); 720 rdmakelabel(lp, &rd->sc_xbn); 721 break; 722 723 case DIOCWLABEL: 724 if ((flag & FWRITE) == 0) 725 err = EBADF; 726 break; 727 728 default: 729 err = ENOTTY; 730 } 731 return err; 732 } 733 734 /* 735 * 736 */ 737 int 738 rdread(dev_t dev, struct uio *uio, int flag) 739 { 740 return (physio (rdstrategy, NULL, dev, B_READ, minphys, uio)); 741 } 742 743 /* 744 * 745 */ 746 int 747 rdwrite(dev_t dev, struct uio *uio, int flag) 748 { 749 return (physio (rdstrategy, NULL, dev, B_WRITE, minphys, uio)); 750 } 751 752 /* 753 * we have to wait 0.7 usec between two accesses to any of the 754 * dkc-registers, on a VS2000 with 1 MIPS, this is roughly one 755 * instruction. Thus the loop-overhead will be enough... 756 */ 757 static void 758 hdc_readregs(struct hdcsoftc *sc) 759 { 760 int i; 761 char *p; 762 763 HDC_WCMD(DKC_CMD_SETREGPTR); 764 WAIT; 765 p = (void*)&sc->sc_sreg; 766 for (i=0; i<10; i++) { 767 *p++ = HDC_RREG; /* dkc_reg auto-increments */ 768 WAIT; 769 } 770 } 771 772 static void 773 hdc_writeregs(struct hdcsoftc *sc) 774 { 775 int i; 776 char *p; 777 778 HDC_WCMD(DKC_CMD_SETREGPTR); 779 p = (void*)&sc->sc_creg; 780 for (i=0; i<10; i++) { 781 HDC_WREG(*p++); /* dkc_reg auto-increments */ 782 WAIT; 783 } 784 } 785 786 /* 787 * hdc_command() issues a command and polls the intreq-register 788 * to find when command has completed 789 */ 790 int 791 hdc_command(struct hdcsoftc *sc, int cmd) 792 { 793 hdc_writeregs(sc); /* write the prepared registers */ 794 HDC_WCMD(cmd); 795 WAIT; 796 return (0); 797 } 798 799 int 800 hdc_rdselect(struct hdcsoftc *sc, int unit) 801 { 802 struct hdc9224_UDCreg *p = &sc->sc_creg; 803 int error; 804 805 /* 806 * bring "creg" in some known-to-work state and 807 * select the drive with the DRIVE SELECT command. 808 */ 809 bzero(p, sizeof(struct hdc9224_UDCreg)); 810 811 p->udc_rtcnt = UDC_RC_HDD_READ; 812 p->udc_mode = UDC_MD_HDD; 813 p->udc_term = UDC_TC_HDD; 814 815 error = hdc_command(sc, DKC_CMD_DRSEL_HDD | unit); 816 817 return (error); 818 } 819 820 void 821 rdmakelabel(struct disklabel *dl, struct rdgeom *g) 822 { 823 int n, p = 0; 824 825 dl->d_bbsize = BBSIZE; 826 dl->d_sbsize = SBLOCKSIZE; 827 dl->d_typename[p++] = MSCP_MID_CHAR(2, g->media_id); 828 dl->d_typename[p++] = MSCP_MID_CHAR(1, g->media_id); 829 if (MSCP_MID_ECH(0, g->media_id)) 830 dl->d_typename[p++] = MSCP_MID_CHAR(0, g->media_id); 831 n = MSCP_MID_NUM(g->media_id); 832 if (n > 99) { 833 dl->d_typename[p++] = '1'; 834 n -= 100; 835 } 836 if (n > 9) { 837 dl->d_typename[p++] = (n / 10) + '0'; 838 n %= 10; 839 } 840 dl->d_typename[p++] = n + '0'; 841 dl->d_typename[p] = 0; 842 dl->d_type = DTYPE_MSCP; /* XXX - what to use here??? */ 843 dl->d_rpm = 3600; 844 dl->d_secsize = DEV_BSIZE; 845 846 dl->d_secperunit = g->lbn_count; 847 dl->d_nsectors = g->nspt; 848 dl->d_ntracks = g->ntracks; 849 dl->d_secpercyl = dl->d_nsectors * dl->d_ntracks; 850 dl->d_ncylinders = dl->d_secperunit / dl->d_secpercyl; 851 852 dl->d_npartitions = MAXPARTITIONS; 853 dl->d_partitions[0].p_size = dl->d_partitions[2].p_size = 854 dl->d_secperunit; 855 dl->d_partitions[0].p_offset = dl->d_partitions[2].p_offset = 0; 856 dl->d_interleave = dl->d_headswitch = 1; 857 dl->d_magic = dl->d_magic2 = DISKMAGIC; 858 dl->d_checksum = dkcksum(dl); 859 } 860