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