1 /* 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Don Ahn. 7 * 8 * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu) 9 * aided by the Linux floppy driver modifications from David Bateman 10 * (dbateman@eng.uts.edu.au). 11 * 12 * Copyright (c) 1993, 1994 by 13 * jc@irbs.UUCP (John Capo) 14 * vak@zebub.msk.su (Serge Vakulenko) 15 * ache@astral.msk.su (Andrew A. Chernov) 16 * 17 * Copyright (c) 1993, 1994, 1995 by 18 * joerg_wunsch@uriah.sax.de (Joerg Wunsch) 19 * dufault@hda.com (Peter Dufault) 20 * 21 * Redistribution and use in source and binary forms, with or without 22 * modification, are permitted provided that the following conditions 23 * are met: 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 3. All advertising materials mentioning features or use of this software 30 * must display the following acknowledgement: 31 * This product includes software developed by the University of 32 * California, Berkeley and its contributors. 33 * 4. Neither the name of the University nor the names of its contributors 34 * may be used to endorse or promote products derived from this software 35 * without specific prior written permission. 36 * 37 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 40 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 47 * SUCH DAMAGE. 48 * 49 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 50 * $Id: fd.c,v 1.129 1998/12/14 16:29:58 bde Exp $ 51 * 52 */ 53 54 #include "fd.h" 55 #include "opt_devfs.h" 56 #include "opt_fdc.h" 57 58 #if NFDC > 0 59 60 #include <sys/param.h> 61 #include <sys/systm.h> 62 #include <sys/kernel.h> 63 #include <sys/conf.h> 64 #include <sys/fcntl.h> 65 #include <machine/clock.h> 66 #include <machine/ioctl_fd.h> 67 #include <sys/disklabel.h> 68 #include <sys/buf.h> 69 #include <sys/devicestat.h> 70 #include <sys/malloc.h> 71 #include <sys/proc.h> 72 #include <sys/syslog.h> 73 #include <i386/isa/isa.h> 74 #include <i386/isa/isa_device.h> 75 #include <i386/isa/fdreg.h> 76 #include <i386/isa/fdc.h> 77 #include <i386/isa/rtc.h> 78 #include <machine/stdarg.h> 79 #ifdef DEVFS 80 #include <sys/devfsext.h> 81 #endif /* DEVFS */ 82 83 /* misuse a flag to identify format operation */ 84 #define B_FORMAT B_XXX 85 86 /* configuration flags */ 87 #define FDC_PRETEND_D0 (1 << 0) /* pretend drive 0 to be there */ 88 #ifdef FDC_YE 89 #define FDC_IS_PCMCIA (1 << 1) /* if successful probe, then it's 90 a PCMCIA device */ 91 #endif 92 93 /* internally used only, not really from CMOS: */ 94 #define RTCFDT_144M_PRETENDED 0x1000 95 96 /* 97 * this biotab field doubles as a field for the physical unit number 98 * on the controller 99 */ 100 #define id_physid id_scsiid 101 102 /* error returns for fd_cmd() */ 103 #define FD_FAILED -1 104 #define FD_NOT_VALID -2 105 #define FDC_ERRMAX 100 /* do not log more */ 106 107 #define NUMTYPES 14 108 #define NUMDENS (NUMTYPES - 6) 109 110 /* These defines (-1) must match index for fd_types */ 111 #define F_TAPE_TYPE 0x020 /* bit for fd_types to indicate tape */ 112 #define NO_TYPE 0 /* must match NO_TYPE in ft.c */ 113 #define FD_1720 1 114 #define FD_1480 2 115 #define FD_1440 3 116 #define FD_1200 4 117 #define FD_820 5 118 #define FD_800 6 119 #define FD_720 7 120 #define FD_360 8 121 122 #define FD_1480in5_25 9 123 #define FD_1440in5_25 10 124 #define FD_820in5_25 11 125 #define FD_800in5_25 12 126 #define FD_720in5_25 13 127 #define FD_360in5_25 14 128 129 130 static struct fd_type fd_types[NUMTYPES] = 131 { 132 { 21,2,0xFF,0x04,82,3444,1,FDC_500KBPS,2,0x0C,2 }, /* 1.72M in HD 3.5in */ 133 { 18,2,0xFF,0x1B,82,2952,1,FDC_500KBPS,2,0x6C,1 }, /* 1.48M in HD 3.5in */ 134 { 18,2,0xFF,0x1B,80,2880,1,FDC_500KBPS,2,0x6C,1 }, /* 1.44M in HD 3.5in */ 135 { 15,2,0xFF,0x1B,80,2400,1,FDC_500KBPS,2,0x54,1 }, /* 1.2M in HD 5.25/3.5 */ 136 { 10,2,0xFF,0x10,82,1640,1,FDC_250KBPS,2,0x2E,1 }, /* 820K in HD 3.5in */ 137 { 10,2,0xFF,0x10,80,1600,1,FDC_250KBPS,2,0x2E,1 }, /* 800K in HD 3.5in */ 138 { 9,2,0xFF,0x20,80,1440,1,FDC_250KBPS,2,0x50,1 }, /* 720K in HD 3.5in */ 139 { 9,2,0xFF,0x2A,40, 720,1,FDC_250KBPS,2,0x50,1 }, /* 360K in DD 5.25in */ 140 141 { 18,2,0xFF,0x02,82,2952,1,FDC_500KBPS,2,0x02,2 }, /* 1.48M in HD 5.25in */ 142 { 18,2,0xFF,0x02,80,2880,1,FDC_500KBPS,2,0x02,2 }, /* 1.44M in HD 5.25in */ 143 { 10,2,0xFF,0x10,82,1640,1,FDC_300KBPS,2,0x2E,1 }, /* 820K in HD 5.25in */ 144 { 10,2,0xFF,0x10,80,1600,1,FDC_300KBPS,2,0x2E,1 }, /* 800K in HD 5.25in */ 145 { 9,2,0xFF,0x20,80,1440,1,FDC_300KBPS,2,0x50,1 }, /* 720K in HD 5.25in */ 146 { 9,2,0xFF,0x23,40, 720,2,FDC_300KBPS,2,0x50,1 }, /* 360K in HD 5.25in */ 147 }; 148 149 #define DRVS_PER_CTLR 2 /* 2 floppies */ 150 151 /***********************************************************************\ 152 * Per controller structure. * 153 \***********************************************************************/ 154 struct fdc_data fdc_data[NFDC]; 155 156 /***********************************************************************\ 157 * Per drive structure. * 158 * N per controller (DRVS_PER_CTLR) * 159 \***********************************************************************/ 160 static struct fd_data { 161 struct fdc_data *fdc; /* pointer to controller structure */ 162 int fdsu; /* this units number on this controller */ 163 int type; /* Drive type (FD_1440...) */ 164 struct fd_type *ft; /* pointer to the type descriptor */ 165 int flags; 166 #define FD_OPEN 0x01 /* it's open */ 167 #define FD_ACTIVE 0x02 /* it's active */ 168 #define FD_MOTOR 0x04 /* motor should be on */ 169 #define FD_MOTOR_WAIT 0x08 /* motor coming up */ 170 int skip; 171 int hddrv; 172 #define FD_NO_TRACK -2 173 int track; /* where we think the head is */ 174 int options; /* user configurable options, see ioctl_fd.h */ 175 struct callout_handle toffhandle; 176 struct callout_handle tohandle; 177 struct devstat device_stats; 178 #ifdef DEVFS 179 void *bdevs[1 + NUMDENS + MAXPARTITIONS]; 180 void *cdevs[1 + NUMDENS + MAXPARTITIONS]; 181 #endif 182 } fd_data[NFD]; 183 184 /***********************************************************************\ 185 * Throughout this file the following conventions will be used: * 186 * fd is a pointer to the fd_data struct for the drive in question * 187 * fdc is a pointer to the fdc_data struct for the controller * 188 * fdu is the floppy drive unit number * 189 * fdcu is the floppy controller unit number * 190 * fdsu is the floppy drive unit number on that controller. (sub-unit) * 191 \***********************************************************************/ 192 193 #ifdef FDC_YE 194 #include "card.h" 195 static int yeattach(struct isa_device *); 196 #endif 197 198 /* autoconfig functions */ 199 static int fdprobe(struct isa_device *); 200 static int fdattach(struct isa_device *); 201 202 /* needed for ft driver, thus exported */ 203 int in_fdc(fdcu_t); 204 int out_fdc(fdcu_t, int); 205 206 /* internal functions */ 207 static void set_motor(fdcu_t, int, int); 208 # define TURNON 1 209 # define TURNOFF 0 210 static timeout_t fd_turnoff; 211 static timeout_t fd_motor_on; 212 static void fd_turnon(fdu_t); 213 static void fdc_reset(fdc_p); 214 static int fd_in(fdcu_t, int *); 215 static void fdstart(fdcu_t); 216 static timeout_t fd_iotimeout; 217 static timeout_t fd_pseudointr; 218 static ointhand2_t fdintr; 219 static int fdstate(fdcu_t, fdc_p); 220 static int retrier(fdcu_t); 221 static int fdformat(dev_t, struct fd_formb *, struct proc *); 222 223 static int enable_fifo(fdc_p fdc); 224 225 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 226 227 228 #define DEVIDLE 0 229 #define FINDWORK 1 230 #define DOSEEK 2 231 #define SEEKCOMPLETE 3 232 #define IOCOMPLETE 4 233 #define RECALCOMPLETE 5 234 #define STARTRECAL 6 235 #define RESETCTLR 7 236 #define SEEKWAIT 8 237 #define RECALWAIT 9 238 #define MOTORWAIT 10 239 #define IOTIMEDOUT 11 240 #define RESETCOMPLETE 12 241 #ifdef FDC_YE 242 #define PIOREAD 13 243 #endif 244 245 #ifdef FDC_DEBUG 246 static char const * const fdstates[] = 247 { 248 "DEVIDLE", 249 "FINDWORK", 250 "DOSEEK", 251 "SEEKCOMPLETE", 252 "IOCOMPLETE", 253 "RECALCOMPLETE", 254 "STARTRECAL", 255 "RESETCTLR", 256 "SEEKWAIT", 257 "RECALWAIT", 258 "MOTORWAIT", 259 "IOTIMEDOUT", 260 "RESETCOMPLETE", 261 #ifdef FDC_YE 262 "PIOREAD", 263 #endif 264 }; 265 266 /* CAUTION: fd_debug causes huge amounts of logging output */ 267 static int volatile fd_debug = 0; 268 #define TRACE0(arg) if(fd_debug) printf(arg) 269 #define TRACE1(arg1, arg2) if(fd_debug) printf(arg1, arg2) 270 #else /* FDC_DEBUG */ 271 #define TRACE0(arg) 272 #define TRACE1(arg1, arg2) 273 #endif /* FDC_DEBUG */ 274 275 #ifdef FDC_YE 276 #if NCARD > 0 277 #include <sys/select.h> 278 #include <pccard/cardinfo.h> 279 #include <pccard/driver.h> 280 #include <pccard/slot.h> 281 282 /* 283 * PC-Card (PCMCIA) specific code. 284 */ 285 static int yeinit(struct pccard_devinfo *); /* init device */ 286 static void yeunload(struct pccard_devinfo *); /* Disable driver */ 287 static int yeintr(struct pccard_devinfo *); /* Interrupt handler */ 288 289 static struct pccard_device ye_info = { 290 "fdc", 291 yeinit, 292 yeunload, 293 yeintr, 294 0, /* Attributes - presently unused */ 295 &bio_imask /* Interrupt mask for device */ 296 }; 297 298 DATA_SET(pccarddrv_set, ye_info); 299 300 /* 301 * this is the secret PIO data port (offset from base) 302 */ 303 #define FDC_YE_DATAPORT 6 304 305 /* 306 * Initialize the device - called from Slot manager. 307 */ 308 static int yeinit(struct pccard_devinfo *devi) 309 { 310 fdc_p fdc = &fdc_data[devi->isahd.id_unit]; 311 312 /* validate unit number. */ 313 if (devi->isahd.id_unit >= NFDC) 314 return(ENODEV); 315 fdc->baseport = devi->isahd.id_iobase; 316 /* 317 * reset controller 318 */ 319 outb(fdc->baseport+FDOUT, 0); 320 DELAY(100); 321 outb(fdc->baseport+FDOUT, FDO_FRST); 322 323 /* 324 * wire into system 325 */ 326 if (yeattach(&devi->isahd) == 0) 327 return(ENXIO); 328 329 return(0); 330 } 331 332 /* 333 * yeunload - unload the driver and clear the table. 334 * XXX TODO: 335 * This is usually called when the card is ejected, but 336 * can be caused by a modunload of a controller driver. 337 * The idea is to reset the driver's view of the device 338 * and ensure that any driver entry points such as 339 * read and write do not hang. 340 */ 341 static void yeunload(struct pccard_devinfo *devi) 342 { 343 if (fd_data[devi->isahd.id_unit].type == NO_TYPE) 344 return; 345 346 /* 347 * this prevents Fdopen() and fdstrategy() from attempting 348 * to access unloaded controller 349 */ 350 fd_data[devi->isahd.id_unit].type = NO_TYPE; 351 352 printf("fdc%d: unload\n", devi->isahd.id_unit); 353 } 354 355 /* 356 * yeintr - Shared interrupt called from 357 * front end of PC-Card handler. 358 */ 359 static int yeintr(struct pccard_devinfo *devi) 360 { 361 fdintr((fdcu_t)devi->isahd.id_unit); 362 return(1); 363 } 364 #endif /* NCARD > 0 */ 365 #endif /* FDC_YE */ 366 367 368 /* autoconfig structure */ 369 370 struct isa_driver fdcdriver = { 371 fdprobe, fdattach, "fdc", 372 }; 373 374 static d_open_t Fdopen; /* NOTE, not fdopen */ 375 static d_read_t fdread; 376 static d_write_t fdwrite; 377 static d_close_t fdclose; 378 static d_ioctl_t fdioctl; 379 static d_strategy_t fdstrategy; 380 381 /* even if SLICE defined, these are needed for the ft support. */ 382 #define CDEV_MAJOR 9 383 #define BDEV_MAJOR 2 384 385 386 static struct cdevsw fd_cdevsw = { 387 Fdopen, fdclose, fdread, fdwrite, 388 fdioctl, nostop, nullreset, nodevtotty, 389 seltrue, nommap, fdstrategy, "fd", 390 NULL, -1, nodump, nopsize, 391 D_DISK, 0, -1 }; 392 393 394 static struct isa_device *fdcdevs[NFDC]; 395 396 397 static int 398 fdc_err(fdcu_t fdcu, const char *s) 399 { 400 fdc_data[fdcu].fdc_errs++; 401 if(s) { 402 if(fdc_data[fdcu].fdc_errs < FDC_ERRMAX) 403 printf("fdc%d: %s", fdcu, s); 404 else if(fdc_data[fdcu].fdc_errs == FDC_ERRMAX) 405 printf("fdc%d: too many errors, not logging any more\n", 406 fdcu); 407 } 408 409 return FD_FAILED; 410 } 411 412 /* 413 * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 414 * Unit number, 415 * # of output bytes, output bytes as ints ..., 416 * # of input bytes, input bytes as ints ... 417 */ 418 419 static int 420 fd_cmd(fdcu_t fdcu, int n_out, ...) 421 { 422 u_char cmd; 423 int n_in; 424 int n; 425 va_list ap; 426 427 va_start(ap, n_out); 428 cmd = (u_char)(va_arg(ap, int)); 429 va_end(ap); 430 va_start(ap, n_out); 431 for (n = 0; n < n_out; n++) 432 { 433 if (out_fdc(fdcu, va_arg(ap, int)) < 0) 434 { 435 char msg[50]; 436 snprintf(msg, sizeof(msg), 437 "cmd %x failed at out byte %d of %d\n", 438 cmd, n + 1, n_out); 439 return fdc_err(fdcu, msg); 440 } 441 } 442 n_in = va_arg(ap, int); 443 for (n = 0; n < n_in; n++) 444 { 445 int *ptr = va_arg(ap, int *); 446 if (fd_in(fdcu, ptr) < 0) 447 { 448 char msg[50]; 449 snprintf(msg, sizeof(msg), 450 "cmd %02x failed at in byte %d of %d\n", 451 cmd, n + 1, n_in); 452 return fdc_err(fdcu, msg); 453 } 454 } 455 456 return 0; 457 } 458 459 static int 460 enable_fifo(fdc_p fdc) 461 { 462 int i, j; 463 464 if ((fdc->flags & FDC_HAS_FIFO) == 0) { 465 466 /* 467 * XXX: 468 * Cannot use fd_cmd the normal way here, since 469 * this might be an invalid command. Thus we send the 470 * first byte, and check for an early turn of data directon. 471 */ 472 473 if (out_fdc(fdc->fdcu, I8207X_CONFIGURE) < 0) 474 return fdc_err(fdc->fdcu, "Enable FIFO failed\n"); 475 476 /* If command is invalid, return */ 477 j = 100000; 478 while ((i = inb(fdc->baseport + FDSTS) & (NE7_DIO | NE7_RQM)) 479 != NE7_RQM && j-- > 0) 480 if (i == (NE7_DIO | NE7_RQM)) { 481 fdc_reset(fdc); 482 return FD_FAILED; 483 } 484 if (j<0 || 485 fd_cmd(fdc->fdcu, 3, 486 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 487 fdc_reset(fdc); 488 return fdc_err(fdc->fdcu, "Enable FIFO failed\n"); 489 } 490 fdc->flags |= FDC_HAS_FIFO; 491 return 0; 492 } 493 if (fd_cmd(fdc->fdcu, 4, 494 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 495 return fdc_err(fdc->fdcu, "Re-enable FIFO failed\n"); 496 return 0; 497 } 498 499 static int 500 fd_sense_drive_status(fdc_p fdc, int *st3p) 501 { 502 int st3; 503 504 if (fd_cmd(fdc->fdcu, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 505 { 506 return fdc_err(fdc->fdcu, "Sense Drive Status failed\n"); 507 } 508 if (st3p) 509 *st3p = st3; 510 511 return 0; 512 } 513 514 static int 515 fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 516 { 517 int st0, cyl; 518 519 int ret = fd_cmd(fdc->fdcu, 1, NE7CMD_SENSEI, 1, &st0); 520 521 if (ret) 522 { 523 (void)fdc_err(fdc->fdcu, 524 "sense intr err reading stat reg 0\n"); 525 return ret; 526 } 527 528 if (st0p) 529 *st0p = st0; 530 531 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) 532 { 533 /* 534 * There doesn't seem to have been an interrupt. 535 */ 536 return FD_NOT_VALID; 537 } 538 539 if (fd_in(fdc->fdcu, &cyl) < 0) 540 { 541 return fdc_err(fdc->fdcu, "can't get cyl num\n"); 542 } 543 544 if (cylp) 545 *cylp = cyl; 546 547 return 0; 548 } 549 550 551 static int 552 fd_read_status(fdc_p fdc, int fdsu) 553 { 554 int i, ret; 555 556 for (i = 0; i < 7; i++) 557 { 558 /* 559 * XXX types are poorly chosen. Only bytes can by read 560 * from the hardware, but fdc->status[] wants u_ints and 561 * fd_in() gives ints. 562 */ 563 int status; 564 565 ret = fd_in(fdc->fdcu, &status); 566 fdc->status[i] = status; 567 if (ret != 0) 568 break; 569 } 570 571 if (ret == 0) 572 fdc->flags |= FDC_STAT_VALID; 573 else 574 fdc->flags &= ~FDC_STAT_VALID; 575 576 return ret; 577 } 578 579 /****************************************************************************/ 580 /* autoconfiguration stuff */ 581 /****************************************************************************/ 582 583 /* 584 * probe for existance of controller 585 */ 586 static int 587 fdprobe(struct isa_device *dev) 588 { 589 fdcu_t fdcu = dev->id_unit; 590 if(fdc_data[fdcu].flags & FDC_ATTACHED) 591 { 592 printf("fdc%d: unit used multiple times\n", fdcu); 593 return 0; 594 } 595 596 fdcdevs[fdcu] = dev; 597 fdc_data[fdcu].baseport = dev->id_iobase; 598 599 /* First - lets reset the floppy controller */ 600 outb(dev->id_iobase+FDOUT, 0); 601 DELAY(100); 602 outb(dev->id_iobase+FDOUT, FDO_FRST); 603 604 /* see if it can handle a command */ 605 if (fd_cmd(fdcu, 606 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 607 0)) 608 { 609 return(0); 610 } 611 #ifdef FDC_YE 612 /* 613 * don't succeed on probe; wait 614 * for PCCARD subsystem to do it 615 */ 616 if (dev->id_flags & FDC_IS_PCMCIA) 617 return(0); 618 #endif 619 return (IO_FDCSIZE); 620 } 621 622 /* 623 * wire controller into system, look for floppy units 624 */ 625 static int 626 fdattach(struct isa_device *dev) 627 { 628 unsigned fdt; 629 fdu_t fdu; 630 fdcu_t fdcu = dev->id_unit; 631 fdc_p fdc = fdc_data + fdcu; 632 fd_p fd; 633 int fdsu, st0, st3, i; 634 struct isa_device *fdup; 635 int ic_type = 0; 636 #ifdef DEVFS 637 int mynor; 638 int typemynor; 639 int typesize; 640 #endif 641 642 dev->id_ointr = fdintr; 643 fdc->fdcu = fdcu; 644 fdc->flags |= FDC_ATTACHED; 645 fdc->dmachan = dev->id_drq; 646 /* Acquire the DMA channel forever, The driver will do the rest */ 647 isa_dma_acquire(fdc->dmachan); 648 isa_dmainit(fdc->dmachan, 128 << 3 /* XXX max secsize */); 649 fdc->state = DEVIDLE; 650 /* reset controller, turn motor off, clear fdout mirror reg */ 651 outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); 652 bufq_init(&fdc->head); 653 654 /* check for each floppy drive */ 655 for (fdup = isa_biotab_fdc; fdup->id_driver != 0; fdup++) { 656 if (fdup->id_iobase != dev->id_iobase) 657 continue; 658 fdu = fdup->id_unit; 659 fd = &fd_data[fdu]; 660 if (fdu >= (NFD)) 661 continue; 662 fdsu = fdup->id_physid; 663 /* look up what bios thinks we have */ 664 switch (fdu) { 665 case 0: if (dev->id_flags & FDC_PRETEND_D0) 666 fdt = RTCFDT_144M | RTCFDT_144M_PRETENDED; 667 else 668 fdt = (rtcin(RTC_FDISKETTE) & 0xf0); 669 break; 670 case 1: fdt = ((rtcin(RTC_FDISKETTE) << 4) & 0xf0); 671 break; 672 default: fdt = RTCFDT_NONE; 673 break; 674 } 675 /* is there a unit? */ 676 if ((fdt == RTCFDT_NONE) 677 ) { 678 fd->type = NO_TYPE; 679 continue; 680 } 681 682 /* select it */ 683 set_motor(fdcu, fdsu, TURNON); 684 DELAY(1000000); /* 1 sec */ 685 686 if (ic_type == 0 && 687 fd_cmd(fdcu, 1, NE7CMD_VERSION, 1, &ic_type) == 0) 688 { 689 #ifdef FDC_PRINT_BOGUS_CHIPTYPE 690 printf("fdc%d: ", fdcu); 691 #endif 692 ic_type = (u_char)ic_type; 693 switch( ic_type ) { 694 case 0x80: 695 #ifdef FDC_PRINT_BOGUS_CHIPTYPE 696 printf("NEC 765\n"); 697 #endif 698 fdc->fdct = FDC_NE765; 699 break; 700 case 0x81: 701 #ifdef FDC_PRINT_BOGUS_CHIPTYPE 702 printf("Intel 82077\n"); 703 #endif 704 fdc->fdct = FDC_I82077; 705 break; 706 case 0x90: 707 #ifdef FDC_PRINT_BOGUS_CHIPTYPE 708 printf("NEC 72065B\n"); 709 #endif 710 fdc->fdct = FDC_NE72065; 711 break; 712 default: 713 #ifdef FDC_PRINT_BOGUS_CHIPTYPE 714 printf("unknown IC type %02x\n", ic_type); 715 #endif 716 fdc->fdct = FDC_UNKNOWN; 717 break; 718 } 719 if (fdc->fdct != FDC_NE765 && 720 fdc->fdct != FDC_UNKNOWN && 721 enable_fifo(fdc) == 0) { 722 printf("fdc%d: FIFO enabled", fdcu); 723 printf(", %d bytes threshold\n", 724 fifo_threshold); 725 } 726 } 727 if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) && 728 (st3 & NE7_ST3_T0)) { 729 /* if at track 0, first seek inwards */ 730 /* seek some steps: */ 731 (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0); 732 DELAY(300000); /* ...wait a moment... */ 733 (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 734 } 735 736 /* If we're at track 0 first seek inwards. */ 737 if ((fd_sense_drive_status(fdc, &st3) == 0) && 738 (st3 & NE7_ST3_T0)) { 739 /* Seek some steps... */ 740 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 741 /* ...wait a moment... */ 742 DELAY(300000); 743 /* make ctrlr happy: */ 744 (void)fd_sense_int(fdc, 0, 0); 745 } 746 } 747 748 for(i = 0; i < 2; i++) { 749 /* 750 * we must recalibrate twice, just in case the 751 * heads have been beyond cylinder 76, since most 752 * FDCs still barf when attempting to recalibrate 753 * more than 77 steps 754 */ 755 /* go back to 0: */ 756 if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 757 /* a second being enough for full stroke seek*/ 758 DELAY(i == 0? 1000000: 300000); 759 760 /* anything responding? */ 761 if (fd_sense_int(fdc, &st0, 0) == 0 && 762 (st0 & NE7_ST0_EC) == 0) 763 break; /* already probed succesfully */ 764 } 765 } 766 767 set_motor(fdcu, fdsu, TURNOFF); 768 769 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 770 continue; 771 772 fd->track = FD_NO_TRACK; 773 fd->fdc = fdc; 774 fd->fdsu = fdsu; 775 fd->options = 0; 776 callout_handle_init(&fd->toffhandle); 777 callout_handle_init(&fd->tohandle); 778 printf("fd%d: ", fdu); 779 780 switch (fdt) { 781 case RTCFDT_12M: 782 printf("1.2MB 5.25in\n"); 783 fd->type = FD_1200; 784 break; 785 case RTCFDT_144M | RTCFDT_144M_PRETENDED: 786 printf("config-pretended "); 787 fdt = RTCFDT_144M; 788 /* fallthrough */ 789 case RTCFDT_144M: 790 printf("1.44MB 3.5in\n"); 791 fd->type = FD_1440; 792 break; 793 case RTCFDT_288M: 794 case RTCFDT_288M_1: 795 printf("2.88MB 3.5in - 1.44MB mode\n"); 796 fd->type = FD_1440; 797 break; 798 case RTCFDT_360K: 799 printf("360KB 5.25in\n"); 800 fd->type = FD_360; 801 break; 802 case RTCFDT_720K: 803 printf("720KB 3.5in\n"); 804 fd->type = FD_720; 805 break; 806 default: 807 printf("unknown\n"); 808 fd->type = NO_TYPE; 809 continue; 810 } 811 #ifdef DEVFS 812 mynor = fdu << 6; 813 fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK, 814 UID_ROOT, GID_OPERATOR, 0640, 815 "fd%d", fdu); 816 fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR, 817 UID_ROOT, GID_OPERATOR, 0640, 818 "rfd%d", fdu); 819 for (i = 1; i < 1 + NUMDENS; i++) { 820 /* 821 * XXX this and the lookup in Fdopen() should be 822 * data driven. 823 */ 824 switch (fd->type) { 825 case FD_360: 826 if (i != FD_360) 827 continue; 828 break; 829 case FD_720: 830 if (i != FD_720 && i != FD_800 && i != FD_820) 831 continue; 832 break; 833 case FD_1200: 834 if (i != FD_360 && i != FD_720 && i != FD_800 835 && i != FD_820 && i != FD_1200 836 && i != FD_1440 && i != FD_1480) 837 continue; 838 break; 839 case FD_1440: 840 if (i != FD_720 && i != FD_800 && i != FD_820 841 && i != FD_1200 && i != FD_1440 842 && i != FD_1480 && i != FD_1720) 843 continue; 844 break; 845 } 846 typesize = fd_types[i - 1].size / 2; 847 /* 848 * XXX all these conversions give bloated code and 849 * confusing names. 850 */ 851 if (typesize == 1476) 852 typesize = 1480; 853 if (typesize == 1722) 854 typesize = 1720; 855 typemynor = mynor | i; 856 fd->bdevs[i] = 857 devfs_add_devswf(&fd_cdevsw, typemynor, DV_BLK, 858 UID_ROOT, GID_OPERATOR, 0640, 859 "fd%d.%d", fdu, typesize); 860 fd->cdevs[i] = 861 devfs_add_devswf(&fd_cdevsw, typemynor, DV_CHR, 862 UID_ROOT, GID_OPERATOR, 0640, 863 "rfd%d.%d", fdu, typesize); 864 } 865 866 for (i = 0; i < MAXPARTITIONS; i++) { 867 fd->bdevs[1 + NUMDENS + i] = devfs_makelink(fd->bdevs[0], 868 "fd%d%c", fdu, 'a' + i); 869 fd->cdevs[1 + NUMDENS + i] = 870 devfs_makelink(fd->cdevs[0], 871 "rfd%d%c", fdu, 'a' + i); 872 } 873 #endif /* DEVFS */ 874 /* 875 * Export the drive to the devstat interface. 876 */ 877 devstat_add_entry(&fd->device_stats, "fd", 878 fdu, 512, 879 DEVSTAT_NO_ORDERED_TAGS, 880 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER); 881 882 } 883 884 return (1); 885 } 886 887 888 889 #ifdef FDC_YE 890 /* 891 * this is a subset of fdattach() optimized for the Y-E Data 892 * PCMCIA floppy drive. 893 */ 894 static int yeattach(struct isa_device *dev) 895 { 896 fdcu_t fdcu = dev->id_unit; 897 fdc_p fdc = fdc_data + fdcu; 898 fdsu_t fdsu = 0; /* assume 1 drive per YE controller */ 899 fdu_t fdu; 900 fd_p fd; 901 int st0, st3, i; 902 #ifdef DEVFS 903 int mynor; 904 int typemynor; 905 int typesize; 906 #endif 907 fdc->fdcu = fdcu; 908 /* 909 * the FDC_PCMCIA flag is used to to indicate special PIO is used 910 * instead of DMA 911 */ 912 fdc->flags = FDC_ATTACHED|FDC_PCMCIA; 913 fdc->state = DEVIDLE; 914 /* reset controller, turn motor off, clear fdout mirror reg */ 915 outb(fdc->baseport + FDOUT, ((fdc->fdout = 0))); 916 bufq_init(&fdc->head); 917 /* 918 * assume 2 drives/ "normal" controller 919 */ 920 fdu = fdcu * 2; 921 if (fdu >= NFD) { 922 printf("fdu %d >= NFD\n",fdu); 923 return(0); 924 }; 925 fd = &fd_data[fdu]; 926 927 set_motor(fdcu, fdsu, TURNON); 928 DELAY(1000000); /* 1 sec */ 929 fdc->fdct = FDC_NE765; 930 931 if ((fd_cmd(fdcu, 2, NE7CMD_SENSED, fdsu, 1, &st3) == 0) && 932 (st3 & NE7_ST3_T0)) { 933 /* if at track 0, first seek inwards */ 934 /* seek some steps: */ 935 (void)fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0); 936 DELAY(300000); /* ...wait a moment... */ 937 (void)fd_sense_int(fdc, 0, 0); /* make ctrlr happy */ 938 } 939 940 /* If we're at track 0 first seek inwards. */ 941 if ((fd_sense_drive_status(fdc, &st3) == 0) && (st3 & NE7_ST3_T0)) { 942 /* Seek some steps... */ 943 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 944 /* ...wait a moment... */ 945 DELAY(300000); 946 /* make ctrlr happy: */ 947 (void)fd_sense_int(fdc, 0, 0); 948 } 949 } 950 951 for(i = 0; i < 2; i++) { 952 /* 953 * we must recalibrate twice, just in case the 954 * heads have been beyond cylinder 76, since most 955 * FDCs still barf when attempting to recalibrate 956 * more than 77 steps 957 */ 958 /* go back to 0: */ 959 if (fd_cmd(fdcu, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 960 /* a second being enough for full stroke seek*/ 961 DELAY(i == 0? 1000000: 300000); 962 963 /* anything responding? */ 964 if (fd_sense_int(fdc, &st0, 0) == 0 && 965 (st0 & NE7_ST0_EC) == 0) 966 break; /* already probed succesfully */ 967 } 968 } 969 970 set_motor(fdcu, fdsu, TURNOFF); 971 972 if (st0 & NE7_ST0_EC) /* no track 0 -> no drive present */ 973 return(0); 974 975 fd->track = FD_NO_TRACK; 976 fd->fdc = fdc; 977 fd->fdsu = fdsu; 978 fd->options = 0; 979 printf("fdc%d: 1.44MB 3.5in PCMCIA\n", fdcu); 980 fd->type = FD_1440; 981 982 #ifdef DEVFS 983 mynor = fdcu << 6; 984 fd->bdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_BLK, 985 UID_ROOT, GID_OPERATOR, 0640, 986 "fd%d", fdu); 987 fd->cdevs[0] = devfs_add_devswf(&fd_cdevsw, mynor, DV_CHR, 988 UID_ROOT, GID_OPERATOR, 0640, 989 "rfd%d", fdu); 990 /* 991 * XXX this and the lookup in Fdopen() should be 992 * data driven. 993 */ 994 typemynor = mynor | FD_1440; 995 typesize = fd_types[FD_1440 - 1].size / 2; 996 /* 997 * XXX all these conversions give bloated code and 998 * confusing names. 999 */ 1000 if (typesize == 1476) 1001 typesize = 1480; 1002 if (typesize == 1722) 1003 typesize = 1720; 1004 fd->bdevs[FD_1440] = devfs_add_devswf(&fd_cdevsw, typemynor, 1005 DV_BLK, UID_ROOT, GID_OPERATOR, 1006 0640, "fd%d.%d", fdu, typesize); 1007 fd->cdevs[FD_1440] = devfs_add_devswf(&fd_cdevsw, typemynor, 1008 DV_CHR, UID_ROOT, GID_OPERATOR, 1009 0640,"rfd%d.%d", fdu, typesize); 1010 for (i = 0; i < MAXPARTITIONS; i++) { 1011 fd->bdevs[1 + NUMDENS + i] = devfs_makelink(fd->bdevs[0], 1012 "fd%d%c", fdu, 'a' + i); 1013 fd->cdevs[1 + NUMDENS + i] = devfs_makelink(fd->cdevs[0], 1014 "rfd%d%c", fdu, 'a' + i); 1015 } 1016 #endif /* DEVFS */ 1017 return (1); 1018 } 1019 #endif 1020 1021 /****************************************************************************/ 1022 /* motor control stuff */ 1023 /* remember to not deselect the drive we're working on */ 1024 /****************************************************************************/ 1025 static void 1026 set_motor(fdcu_t fdcu, int fdsu, int turnon) 1027 { 1028 int fdout = fdc_data[fdcu].fdout; 1029 int needspecify = 0; 1030 1031 if(turnon) { 1032 fdout &= ~FDO_FDSEL; 1033 fdout |= (FDO_MOEN0 << fdsu) + fdsu; 1034 } else 1035 fdout &= ~(FDO_MOEN0 << fdsu); 1036 1037 if(!turnon 1038 && (fdout & (FDO_MOEN0+FDO_MOEN1+FDO_MOEN2+FDO_MOEN3)) == 0) 1039 /* gonna turn off the last drive, put FDC to bed */ 1040 fdout &= ~ (FDO_FRST|FDO_FDMAEN); 1041 else { 1042 /* make sure controller is selected and specified */ 1043 if((fdout & (FDO_FRST|FDO_FDMAEN)) == 0) 1044 needspecify = 1; 1045 fdout |= (FDO_FRST|FDO_FDMAEN); 1046 } 1047 1048 outb(fdc_data[fdcu].baseport+FDOUT, fdout); 1049 fdc_data[fdcu].fdout = fdout; 1050 TRACE1("[0x%x->FDOUT]", fdout); 1051 1052 if(needspecify) { 1053 /* 1054 * XXX 1055 * special case: since we have just woken up the FDC 1056 * from its sleep, we silently assume the command will 1057 * be accepted, and do not test for a timeout 1058 */ 1059 (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, 1060 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1061 0); 1062 if (fdc_data[fdcu].flags & FDC_HAS_FIFO) 1063 (void) enable_fifo(&fdc_data[fdcu]); 1064 } 1065 } 1066 1067 static void 1068 fd_turnoff(void *arg1) 1069 { 1070 fdu_t fdu = (fdu_t)arg1; 1071 int s; 1072 fd_p fd = fd_data + fdu; 1073 1074 TRACE1("[fd%d: turnoff]", fdu); 1075 1076 /* 1077 * Don't turn off the motor yet if the drive is active. 1078 * XXX shouldn't even schedule turnoff until drive is inactive 1079 * and nothing is queued on it. 1080 */ 1081 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fdu) { 1082 fd->toffhandle = timeout(fd_turnoff, arg1, 4 * hz); 1083 return; 1084 } 1085 1086 s = splbio(); 1087 fd->flags &= ~FD_MOTOR; 1088 set_motor(fd->fdc->fdcu, fd->fdsu, TURNOFF); 1089 splx(s); 1090 } 1091 1092 static void 1093 fd_motor_on(void *arg1) 1094 { 1095 fdu_t fdu = (fdu_t)arg1; 1096 int s; 1097 1098 fd_p fd = fd_data + fdu; 1099 s = splbio(); 1100 fd->flags &= ~FD_MOTOR_WAIT; 1101 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 1102 { 1103 fdintr(fd->fdc->fdcu); 1104 } 1105 splx(s); 1106 } 1107 1108 static void 1109 fd_turnon(fdu_t fdu) 1110 { 1111 fd_p fd = fd_data + fdu; 1112 if(!(fd->flags & FD_MOTOR)) 1113 { 1114 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 1115 set_motor(fd->fdc->fdcu, fd->fdsu, TURNON); 1116 timeout(fd_motor_on, (caddr_t)fdu, hz); /* in 1 sec its ok */ 1117 } 1118 } 1119 1120 static void 1121 fdc_reset(fdc_p fdc) 1122 { 1123 fdcu_t fdcu = fdc->fdcu; 1124 1125 /* Try a reset, keep motor on */ 1126 outb(fdc->baseport + FDOUT, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1127 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1128 DELAY(100); 1129 /* enable FDC, but defer interrupts a moment */ 1130 outb(fdc->baseport + FDOUT, fdc->fdout & ~FDO_FDMAEN); 1131 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 1132 DELAY(100); 1133 outb(fdc->baseport + FDOUT, fdc->fdout); 1134 TRACE1("[0x%x->FDOUT]", fdc->fdout); 1135 1136 /* XXX after a reset, silently believe the FDC will accept commands */ 1137 (void)fd_cmd(fdcu, 3, NE7CMD_SPECIFY, 1138 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1139 0); 1140 if (fdc->flags & FDC_HAS_FIFO) 1141 (void) enable_fifo(fdc); 1142 } 1143 1144 /****************************************************************************/ 1145 /* fdc in/out */ 1146 /****************************************************************************/ 1147 int 1148 in_fdc(fdcu_t fdcu) 1149 { 1150 int baseport = fdc_data[fdcu].baseport; 1151 int i, j = 100000; 1152 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) 1153 != (NE7_DIO|NE7_RQM) && j-- > 0) 1154 if (i == NE7_RQM) 1155 return fdc_err(fdcu, "ready for output in input\n"); 1156 if (j <= 0) 1157 return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); 1158 #ifdef FDC_DEBUG 1159 i = inb(baseport+FDDATA); 1160 TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1161 return(i); 1162 #else /* !FDC_DEBUG */ 1163 return inb(baseport+FDDATA); 1164 #endif /* FDC_DEBUG */ 1165 } 1166 1167 /* 1168 * fd_in: Like in_fdc, but allows you to see if it worked. 1169 */ 1170 static int 1171 fd_in(fdcu_t fdcu, int *ptr) 1172 { 1173 int baseport = fdc_data[fdcu].baseport; 1174 int i, j = 100000; 1175 while ((i = inb(baseport+FDSTS) & (NE7_DIO|NE7_RQM)) 1176 != (NE7_DIO|NE7_RQM) && j-- > 0) 1177 if (i == NE7_RQM) 1178 return fdc_err(fdcu, "ready for output in input\n"); 1179 if (j <= 0) 1180 return fdc_err(fdcu, bootverbose? "input ready timeout\n": 0); 1181 #ifdef FDC_DEBUG 1182 i = inb(baseport+FDDATA); 1183 TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1184 *ptr = i; 1185 return 0; 1186 #else /* !FDC_DEBUG */ 1187 i = inb(baseport+FDDATA); 1188 if (ptr) 1189 *ptr = i; 1190 return 0; 1191 #endif /* FDC_DEBUG */ 1192 } 1193 1194 int 1195 out_fdc(fdcu_t fdcu, int x) 1196 { 1197 int baseport = fdc_data[fdcu].baseport; 1198 int i; 1199 1200 /* Check that the direction bit is set */ 1201 i = 100000; 1202 while ((inb(baseport+FDSTS) & NE7_DIO) && i-- > 0); 1203 if (i <= 0) return fdc_err(fdcu, "direction bit not set\n"); 1204 1205 /* Check that the floppy controller is ready for a command */ 1206 i = 100000; 1207 while ((inb(baseport+FDSTS) & NE7_RQM) == 0 && i-- > 0); 1208 if (i <= 0) 1209 return fdc_err(fdcu, bootverbose? "output ready timeout\n": 0); 1210 1211 /* Send the command and return */ 1212 outb(baseport+FDDATA, x); 1213 TRACE1("[0x%x->FDDATA]", x); 1214 return (0); 1215 } 1216 1217 /****************************************************************************/ 1218 /* fdopen/fdclose */ 1219 /****************************************************************************/ 1220 int 1221 Fdopen(dev_t dev, int flags, int mode, struct proc *p) 1222 { 1223 fdu_t fdu = FDUNIT(minor(dev)); 1224 int type = FDTYPE(minor(dev)); 1225 fdc_p fdc; 1226 1227 /* check bounds */ 1228 if (fdu >= NFD) 1229 return(ENXIO); 1230 fdc = fd_data[fdu].fdc; 1231 if ((fdc == NULL) || (fd_data[fdu].type == NO_TYPE)) 1232 return(ENXIO); 1233 if (type > NUMDENS) 1234 return(ENXIO); 1235 if (type == 0) 1236 type = fd_data[fdu].type; 1237 else { 1238 /* 1239 * For each type of basic drive, make sure we are trying 1240 * to open a type it can do, 1241 */ 1242 if (type != fd_data[fdu].type) { 1243 switch (fd_data[fdu].type) { 1244 case FD_360: 1245 return(ENXIO); 1246 case FD_720: 1247 if ( type != FD_820 1248 && type != FD_800 1249 ) 1250 return(ENXIO); 1251 break; 1252 case FD_1200: 1253 switch (type) { 1254 case FD_1480: 1255 type = FD_1480in5_25; 1256 break; 1257 case FD_1440: 1258 type = FD_1440in5_25; 1259 break; 1260 case FD_820: 1261 type = FD_820in5_25; 1262 break; 1263 case FD_800: 1264 type = FD_800in5_25; 1265 break; 1266 case FD_720: 1267 type = FD_720in5_25; 1268 break; 1269 case FD_360: 1270 type = FD_360in5_25; 1271 break; 1272 default: 1273 return(ENXIO); 1274 } 1275 break; 1276 case FD_1440: 1277 if ( type != FD_1720 1278 && type != FD_1480 1279 && type != FD_1200 1280 && type != FD_820 1281 && type != FD_800 1282 && type != FD_720 1283 ) 1284 return(ENXIO); 1285 break; 1286 } 1287 } 1288 } 1289 fd_data[fdu].ft = fd_types + type - 1; 1290 fd_data[fdu].flags |= FD_OPEN; 1291 1292 return 0; 1293 } 1294 1295 int 1296 fdclose(dev_t dev, int flags, int mode, struct proc *p) 1297 { 1298 fdu_t fdu = FDUNIT(minor(dev)); 1299 1300 fd_data[fdu].flags &= ~FD_OPEN; 1301 fd_data[fdu].options &= ~FDOPT_NORETRY; 1302 1303 return(0); 1304 } 1305 1306 static int 1307 fdread(dev_t dev, struct uio *uio, int ioflag) 1308 { 1309 return (physio(fdstrategy, NULL, dev, 1, minphys, uio)); 1310 } 1311 1312 static int 1313 fdwrite(dev_t dev, struct uio *uio, int ioflag) 1314 { 1315 return (physio(fdstrategy, NULL, dev, 0, minphys, uio)); 1316 } 1317 1318 1319 /****************************************************************************/ 1320 /* fdstrategy */ 1321 /****************************************************************************/ 1322 void 1323 fdstrategy(struct buf *bp) 1324 { 1325 unsigned nblocks, blknum, cando; 1326 int s; 1327 fdcu_t fdcu; 1328 fdu_t fdu; 1329 fdc_p fdc; 1330 fd_p fd; 1331 size_t fdblk; 1332 1333 fdu = FDUNIT(minor(bp->b_dev)); 1334 fd = &fd_data[fdu]; 1335 fdc = fd->fdc; 1336 fdcu = fdc->fdcu; 1337 #ifdef FDC_YE 1338 if (fd->type == NO_TYPE) { 1339 bp->b_error = ENXIO; 1340 bp->b_flags |= B_ERROR; 1341 /* 1342 * I _refuse_ to use a goto 1343 */ 1344 biodone(bp); 1345 return; 1346 }; 1347 #endif 1348 1349 fdblk = 128 << (fd->ft->secsize); 1350 if (!(bp->b_flags & B_FORMAT)) { 1351 if ((fdu >= NFD) || (bp->b_blkno < 0)) { 1352 printf( 1353 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 1354 fdu, (u_long)bp->b_blkno, bp->b_bcount); 1355 bp->b_error = EINVAL; 1356 bp->b_flags |= B_ERROR; 1357 goto bad; 1358 } 1359 if ((bp->b_bcount % fdblk) != 0) { 1360 bp->b_error = EINVAL; 1361 bp->b_flags |= B_ERROR; 1362 goto bad; 1363 } 1364 } 1365 1366 /* 1367 * Set up block calculations. 1368 */ 1369 if (bp->b_blkno > 20000000) { 1370 /* 1371 * Reject unreasonably high block number, prevent the 1372 * multiplication below from overflowing. 1373 */ 1374 bp->b_error = EINVAL; 1375 bp->b_flags |= B_ERROR; 1376 goto bad; 1377 } 1378 blknum = (unsigned) bp->b_blkno * DEV_BSIZE/fdblk; 1379 nblocks = fd->ft->size; 1380 bp->b_resid = 0; 1381 if (blknum + (bp->b_bcount / fdblk) > nblocks) { 1382 if (blknum <= nblocks) { 1383 cando = (nblocks - blknum) * fdblk; 1384 bp->b_resid = bp->b_bcount - cando; 1385 if (cando == 0) 1386 goto bad; /* not actually bad but EOF */ 1387 } else { 1388 bp->b_error = EINVAL; 1389 bp->b_flags |= B_ERROR; 1390 goto bad; 1391 } 1392 } 1393 bp->b_pblkno = bp->b_blkno; 1394 s = splbio(); 1395 bufqdisksort(&fdc->head, bp); 1396 untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); /* a good idea */ 1397 1398 /* Tell devstat we are starting on the transaction */ 1399 devstat_start_transaction(&fd->device_stats); 1400 1401 fdstart(fdcu); 1402 splx(s); 1403 return; 1404 1405 bad: 1406 biodone(bp); 1407 } 1408 1409 /***************************************************************\ 1410 * fdstart * 1411 * We have just queued something.. if the controller is not busy * 1412 * then simulate the case where it has just finished a command * 1413 * So that it (the interrupt routine) looks on the queue for more* 1414 * work to do and picks up what we just added. * 1415 * If the controller is already busy, we need do nothing, as it * 1416 * will pick up our work when the present work completes * 1417 \***************************************************************/ 1418 static void 1419 fdstart(fdcu_t fdcu) 1420 { 1421 int s; 1422 1423 s = splbio(); 1424 if(fdc_data[fdcu].state == DEVIDLE) 1425 { 1426 fdintr(fdcu); 1427 } 1428 splx(s); 1429 } 1430 1431 static void 1432 fd_iotimeout(void *arg1) 1433 { 1434 fdc_p fdc; 1435 fdcu_t fdcu; 1436 int s; 1437 1438 fdcu = (fdcu_t)arg1; 1439 fdc = fdc_data + fdcu; 1440 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 1441 1442 /* 1443 * Due to IBM's brain-dead design, the FDC has a faked ready 1444 * signal, hardwired to ready == true. Thus, any command 1445 * issued if there's no diskette in the drive will _never_ 1446 * complete, and must be aborted by resetting the FDC. 1447 * Many thanks, Big Blue! 1448 * The FDC must not be reset directly, since that would 1449 * interfere with the state machine. Instead, pretend that 1450 * the command completed but was invalid. The state machine 1451 * will reset the FDC and retry once. 1452 */ 1453 s = splbio(); 1454 fdc->status[0] = NE7_ST0_IC_IV; 1455 fdc->flags &= ~FDC_STAT_VALID; 1456 fdc->state = IOTIMEDOUT; 1457 fdintr(fdcu); 1458 splx(s); 1459 } 1460 1461 /* just ensure it has the right spl */ 1462 static void 1463 fd_pseudointr(void *arg1) 1464 { 1465 fdcu_t fdcu = (fdcu_t)arg1; 1466 int s; 1467 1468 s = splbio(); 1469 fdintr(fdcu); 1470 splx(s); 1471 } 1472 1473 /***********************************************************************\ 1474 * fdintr * 1475 * keep calling the state machine until it returns a 0 * 1476 * ALWAYS called at SPLBIO * 1477 \***********************************************************************/ 1478 static void 1479 fdintr(fdcu_t fdcu) 1480 { 1481 fdc_p fdc = fdc_data + fdcu; 1482 while(fdstate(fdcu, fdc)) 1483 ; 1484 } 1485 1486 #ifdef FDC_YE 1487 /* 1488 * magic pseudo-DMA initialization for YE FDC. Sets count and 1489 * direction 1490 */ 1491 #define SET_BCDR(wr,cnt,port) outb(port,(((cnt)-1) & 0xff)); \ 1492 outb(port+1,((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))) 1493 1494 /* 1495 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy 1496 */ 1497 static int fdcpio(fdcu_t fdcu, long flags, caddr_t addr, u_int count) 1498 { 1499 u_char *cptr = (u_char *)addr; 1500 fdc_p fdc = &fdc_data[fdcu]; 1501 int io = fdc->baseport; 1502 1503 if (flags & B_READ) { 1504 if (fdc->state != PIOREAD) { 1505 fdc->state = PIOREAD; 1506 return(0); 1507 }; 1508 SET_BCDR(0,count,io); 1509 insb(io+FDC_YE_DATAPORT,cptr,count); 1510 } else { 1511 outsb(io+FDC_YE_DATAPORT,cptr,count); 1512 SET_BCDR(0,count,io); 1513 }; 1514 return(1); 1515 } 1516 #endif /* FDC_YE */ 1517 1518 /***********************************************************************\ 1519 * The controller state machine. * 1520 * if it returns a non zero value, it should be called again immediatly * 1521 \***********************************************************************/ 1522 static int 1523 fdstate(fdcu_t fdcu, fdc_p fdc) 1524 { 1525 int read, format, head, i, sec = 0, sectrac, st0, cyl, st3; 1526 unsigned blknum = 0, b_cylinder = 0; 1527 fdu_t fdu = fdc->fdu; 1528 fd_p fd; 1529 register struct buf *bp; 1530 struct fd_formb *finfo = NULL; 1531 size_t fdblk; 1532 1533 bp = bufq_first(&fdc->head); 1534 if(!bp) { 1535 /***********************************************\ 1536 * nothing left for this controller to do * 1537 * Force into the IDLE state, * 1538 \***********************************************/ 1539 fdc->state = DEVIDLE; 1540 if(fdc->fd) 1541 { 1542 printf("fd%d: unexpected valid fd pointer\n", 1543 fdc->fdu); 1544 fdc->fd = (fd_p) 0; 1545 fdc->fdu = -1; 1546 } 1547 TRACE1("[fdc%d IDLE]", fdcu); 1548 return(0); 1549 } 1550 fdu = FDUNIT(minor(bp->b_dev)); 1551 fd = fd_data + fdu; 1552 fdblk = 128 << fd->ft->secsize; 1553 if (fdc->fd && (fd != fdc->fd)) 1554 { 1555 printf("fd%d: confused fd pointers\n", fdu); 1556 } 1557 read = bp->b_flags & B_READ; 1558 format = bp->b_flags & B_FORMAT; 1559 if(format) { 1560 finfo = (struct fd_formb *)bp->b_data; 1561 fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 1562 - (char *)finfo; 1563 } 1564 if (fdc->state == DOSEEK || fdc->state == SEEKCOMPLETE) { 1565 blknum = (unsigned) bp->b_pblkno * DEV_BSIZE/fdblk + 1566 fd->skip/fdblk; 1567 b_cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 1568 } 1569 TRACE1("fd%d", fdu); 1570 TRACE1("[%s]", fdstates[fdc->state]); 1571 TRACE1("(0x%x)", fd->flags); 1572 untimeout(fd_turnoff, (caddr_t)fdu, fd->toffhandle); 1573 fd->toffhandle = timeout(fd_turnoff, (caddr_t)fdu, 4 * hz); 1574 switch (fdc->state) 1575 { 1576 case DEVIDLE: 1577 case FINDWORK: /* we have found new work */ 1578 fdc->retry = 0; 1579 fd->skip = 0; 1580 fdc->fd = fd; 1581 fdc->fdu = fdu; 1582 outb(fdc->baseport+FDCTL, fd->ft->trans); 1583 TRACE1("[0x%x->FDCTL]", fd->ft->trans); 1584 /*******************************************************\ 1585 * If the next drive has a motor startup pending, then * 1586 * it will start up in its own good time * 1587 \*******************************************************/ 1588 if(fd->flags & FD_MOTOR_WAIT) 1589 { 1590 fdc->state = MOTORWAIT; 1591 return(0); /* come back later */ 1592 } 1593 /*******************************************************\ 1594 * Maybe if it's not starting, it SHOULD be starting * 1595 \*******************************************************/ 1596 if (!(fd->flags & FD_MOTOR)) 1597 { 1598 fdc->state = MOTORWAIT; 1599 fd_turnon(fdu); 1600 return(0); 1601 } 1602 else /* at least make sure we are selected */ 1603 { 1604 set_motor(fdcu, fd->fdsu, TURNON); 1605 } 1606 if (fdc->flags & FDC_NEEDS_RESET) { 1607 fdc->state = RESETCTLR; 1608 fdc->flags &= ~FDC_NEEDS_RESET; 1609 } else 1610 fdc->state = DOSEEK; 1611 break; 1612 case DOSEEK: 1613 if (b_cylinder == (unsigned)fd->track) 1614 { 1615 fdc->state = SEEKCOMPLETE; 1616 break; 1617 } 1618 if (fd_cmd(fdcu, 3, NE7CMD_SEEK, 1619 fd->fdsu, b_cylinder * fd->ft->steptrac, 1620 0)) 1621 { 1622 /* 1623 * seek command not accepted, looks like 1624 * the FDC went off to the Saints... 1625 */ 1626 fdc->retry = 6; /* try a reset */ 1627 return(retrier(fdcu)); 1628 } 1629 fd->track = FD_NO_TRACK; 1630 fdc->state = SEEKWAIT; 1631 return(0); /* will return later */ 1632 case SEEKWAIT: 1633 /* allow heads to settle */ 1634 timeout(fd_pseudointr, (caddr_t)fdcu, hz / 16); 1635 fdc->state = SEEKCOMPLETE; 1636 return(0); /* will return later */ 1637 case SEEKCOMPLETE : /* SEEK DONE, START DMA */ 1638 /* Make sure seek really happened*/ 1639 if(fd->track == FD_NO_TRACK) 1640 { 1641 int descyl = b_cylinder * fd->ft->steptrac; 1642 do { 1643 /* 1644 * This might be a "ready changed" interrupt, 1645 * which cannot really happen since the 1646 * RDY pin is hardwired to + 5 volts. This 1647 * generally indicates a "bouncing" intr 1648 * line, so do one of the following: 1649 * 1650 * When running on an enhanced FDC that is 1651 * known to not go stuck after responding 1652 * with INVALID, fetch all interrupt states 1653 * until seeing either an INVALID or a 1654 * real interrupt condition. 1655 * 1656 * When running on a dumb old NE765, give 1657 * up immediately. The controller will 1658 * provide up to four dummy RC interrupt 1659 * conditions right after reset (for the 1660 * corresponding four drives), so this is 1661 * our only chance to get notice that it 1662 * was not the FDC that caused the interrupt. 1663 */ 1664 if (fd_sense_int(fdc, &st0, &cyl) 1665 == FD_NOT_VALID) 1666 return 0; 1667 if(fdc->fdct == FDC_NE765 1668 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1669 return 0; /* hope for a real intr */ 1670 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1671 1672 if (0 == descyl) 1673 { 1674 int failed = 0; 1675 /* 1676 * seek to cyl 0 requested; make sure we are 1677 * really there 1678 */ 1679 if (fd_sense_drive_status(fdc, &st3)) 1680 failed = 1; 1681 if ((st3 & NE7_ST3_T0) == 0) { 1682 printf( 1683 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 1684 fdu, st3, NE7_ST3BITS); 1685 failed = 1; 1686 } 1687 1688 if (failed) 1689 { 1690 if(fdc->retry < 3) 1691 fdc->retry = 3; 1692 return(retrier(fdcu)); 1693 } 1694 } 1695 1696 if (cyl != descyl) 1697 { 1698 printf( 1699 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 1700 fdu, descyl, cyl, st0); 1701 if (fdc->retry < 3) 1702 fdc->retry = 3; 1703 return(retrier(fdcu)); 1704 } 1705 } 1706 1707 fd->track = b_cylinder; 1708 #ifdef FDC_YE 1709 if (!(fdc->flags & FDC_PCMCIA)) 1710 #endif 1711 isa_dmastart(bp->b_flags, bp->b_data+fd->skip, 1712 format ? bp->b_bcount : fdblk, fdc->dmachan); 1713 sectrac = fd->ft->sectrac; 1714 sec = blknum % (sectrac * fd->ft->heads); 1715 head = sec / sectrac; 1716 sec = sec % sectrac + 1; 1717 fd->hddrv = ((head&1)<<2)+fdu; 1718 1719 if(format || !read) 1720 { 1721 /* make sure the drive is writable */ 1722 if(fd_sense_drive_status(fdc, &st3) != 0) 1723 { 1724 /* stuck controller? */ 1725 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1726 format ? bp->b_bcount : fdblk, 1727 fdc->dmachan); 1728 fdc->retry = 6; /* reset the beast */ 1729 return(retrier(fdcu)); 1730 } 1731 if(st3 & NE7_ST3_WP) 1732 { 1733 /* 1734 * XXX YES! this is ugly. 1735 * in order to force the current operation 1736 * to fail, we will have to fake an FDC 1737 * error - all error handling is done 1738 * by the retrier() 1739 */ 1740 fdc->status[0] = NE7_ST0_IC_AT; 1741 fdc->status[1] = NE7_ST1_NW; 1742 fdc->status[2] = 0; 1743 fdc->status[3] = fd->track; 1744 fdc->status[4] = head; 1745 fdc->status[5] = sec; 1746 fdc->retry = 8; /* break out immediately */ 1747 fdc->state = IOTIMEDOUT; /* not really... */ 1748 return (1); 1749 } 1750 } 1751 1752 if(format) 1753 { 1754 #ifdef FDC_YE 1755 if (fdc->flags & FDC_PCMCIA) 1756 (void)fdcpio(fdcu,bp->b_flags, 1757 bp->b_data+fd->skip, 1758 bp->b_bcount); 1759 #endif 1760 /* formatting */ 1761 if(fd_cmd(fdcu, 6, 1762 NE7CMD_FORMAT, 1763 head << 2 | fdu, 1764 finfo->fd_formb_secshift, 1765 finfo->fd_formb_nsecs, 1766 finfo->fd_formb_gaplen, 1767 finfo->fd_formb_fillbyte, 1768 0)) 1769 { 1770 /* controller fell over */ 1771 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1772 format ? bp->b_bcount : fdblk, 1773 fdc->dmachan); 1774 fdc->retry = 6; 1775 return(retrier(fdcu)); 1776 } 1777 } 1778 else 1779 { 1780 #ifdef FDC_YE 1781 if (fdc->flags & FDC_PCMCIA) { 1782 /* 1783 * this seems to be necessary even when 1784 * reading data 1785 */ 1786 SET_BCDR(1,fdblk,fdc->baseport); 1787 1788 /* 1789 * perform the write pseudo-DMA before 1790 * the WRITE command is sent 1791 */ 1792 if (!read) 1793 (void)fdcpio(fdcu,bp->b_flags, 1794 bp->b_data+fd->skip, 1795 fdblk); 1796 } 1797 #endif 1798 if (fd_cmd(fdcu, 9, 1799 (read ? NE7CMD_READ : NE7CMD_WRITE), 1800 head << 2 | fdu, /* head & unit */ 1801 fd->track, /* track */ 1802 head, 1803 sec, /* sector + 1 */ 1804 fd->ft->secsize, /* sector size */ 1805 sectrac, /* sectors/track */ 1806 fd->ft->gap, /* gap size */ 1807 fd->ft->datalen, /* data length */ 1808 0)) 1809 { 1810 /* the beast is sleeping again */ 1811 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1812 format ? bp->b_bcount : fdblk, 1813 fdc->dmachan); 1814 fdc->retry = 6; 1815 return(retrier(fdcu)); 1816 } 1817 } 1818 #ifdef FDC_YE 1819 if (fdc->flags & FDC_PCMCIA) 1820 /* 1821 * if this is a read, then simply await interrupt 1822 * before performing PIO 1823 */ 1824 if (read && !fdcpio(fdcu,bp->b_flags, 1825 bp->b_data+fd->skip,fdblk)) { 1826 fd->tohandle = timeout(fd_iotimeout, 1827 (caddr_t)fdcu, hz); 1828 return(0); /* will return later */ 1829 }; 1830 1831 /* 1832 * write (or format) operation will fall through and 1833 * await completion interrupt 1834 */ 1835 #endif 1836 fdc->state = IOCOMPLETE; 1837 fd->tohandle = timeout(fd_iotimeout, (caddr_t)fdcu, hz); 1838 return(0); /* will return later */ 1839 #ifdef FDC_YE 1840 case PIOREAD: 1841 /* 1842 * actually perform the PIO read. The IOCOMPLETE case 1843 * removes the timeout for us. 1844 */ 1845 (void)fdcpio(fdcu,bp->b_flags,bp->b_data+fd->skip,fdblk); 1846 fdc->state = IOCOMPLETE; 1847 /* FALLTHROUGH */ 1848 #endif 1849 case IOCOMPLETE: /* IO DONE, post-analyze */ 1850 untimeout(fd_iotimeout, (caddr_t)fdcu, fd->tohandle); 1851 1852 if (fd_read_status(fdc, fd->fdsu)) 1853 { 1854 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1855 format ? bp->b_bcount : fdblk, 1856 fdc->dmachan); 1857 if (fdc->retry < 6) 1858 fdc->retry = 6; /* force a reset */ 1859 return retrier(fdcu); 1860 } 1861 1862 fdc->state = IOTIMEDOUT; 1863 1864 /* FALLTHROUGH */ 1865 1866 case IOTIMEDOUT: 1867 #ifdef FDC_YE 1868 if (!(fdc->flags & FDC_PCMCIA)) 1869 #endif 1870 isa_dmadone(bp->b_flags, bp->b_data + fd->skip, 1871 format ? bp->b_bcount : fdblk, fdc->dmachan); 1872 if (fdc->status[0] & NE7_ST0_IC) 1873 { 1874 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 1875 && fdc->status[1] & NE7_ST1_OR) { 1876 /* 1877 * DMA overrun. Someone hogged the bus 1878 * and didn't release it in time for the 1879 * next FDC transfer. 1880 * Just restart it, don't increment retry 1881 * count. (vak) 1882 */ 1883 fdc->state = SEEKCOMPLETE; 1884 return (1); 1885 } 1886 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 1887 && fdc->retry < 6) 1888 fdc->retry = 6; /* force a reset */ 1889 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 1890 && fdc->status[2] & NE7_ST2_WC 1891 && fdc->retry < 3) 1892 fdc->retry = 3; /* force recalibrate */ 1893 return(retrier(fdcu)); 1894 } 1895 /* All OK */ 1896 fd->skip += fdblk; 1897 if (!format && fd->skip < bp->b_bcount - bp->b_resid) 1898 { 1899 /* set up next transfer */ 1900 fdc->state = DOSEEK; 1901 } 1902 else 1903 { 1904 /* ALL DONE */ 1905 fd->skip = 0; 1906 bufq_remove(&fdc->head, bp); 1907 /* Tell devstat we have finished with the transaction */ 1908 devstat_end_transaction(&fd->device_stats, 1909 bp->b_bcount - bp->b_resid, 1910 DEVSTAT_TAG_NONE, 1911 (bp->b_flags & B_READ) ? 1912 DEVSTAT_READ : DEVSTAT_WRITE); 1913 biodone(bp); 1914 fdc->fd = (fd_p) 0; 1915 fdc->fdu = -1; 1916 fdc->state = FINDWORK; 1917 } 1918 return(1); 1919 case RESETCTLR: 1920 fdc_reset(fdc); 1921 fdc->retry++; 1922 fdc->state = RESETCOMPLETE; 1923 return (0); 1924 case RESETCOMPLETE: 1925 /* 1926 * Discard all the results from the reset so that they 1927 * can't cause an unexpected interrupt later. 1928 */ 1929 for (i = 0; i < 4; i++) 1930 (void)fd_sense_int(fdc, &st0, &cyl); 1931 fdc->state = STARTRECAL; 1932 /* Fall through. */ 1933 case STARTRECAL: 1934 if(fd_cmd(fdcu, 1935 2, NE7CMD_RECAL, fdu, 1936 0)) /* Recalibrate Function */ 1937 { 1938 /* arrgl */ 1939 fdc->retry = 6; 1940 return(retrier(fdcu)); 1941 } 1942 fdc->state = RECALWAIT; 1943 return(0); /* will return later */ 1944 case RECALWAIT: 1945 /* allow heads to settle */ 1946 timeout(fd_pseudointr, (caddr_t)fdcu, hz / 8); 1947 fdc->state = RECALCOMPLETE; 1948 return(0); /* will return later */ 1949 case RECALCOMPLETE: 1950 do { 1951 /* 1952 * See SEEKCOMPLETE for a comment on this: 1953 */ 1954 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 1955 return 0; 1956 if(fdc->fdct == FDC_NE765 1957 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 1958 return 0; /* hope for a real intr */ 1959 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 1960 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 1961 { 1962 if(fdc->retry > 3) 1963 /* 1964 * a recalibrate from beyond cylinder 77 1965 * will "fail" due to the FDC limitations; 1966 * since people used to complain much about 1967 * the failure message, try not logging 1968 * this one if it seems to be the first 1969 * time in a line 1970 */ 1971 printf("fd%d: recal failed ST0 %b cyl %d\n", 1972 fdu, st0, NE7_ST0BITS, cyl); 1973 if(fdc->retry < 3) fdc->retry = 3; 1974 return(retrier(fdcu)); 1975 } 1976 fd->track = 0; 1977 /* Seek (probably) necessary */ 1978 fdc->state = DOSEEK; 1979 return(1); /* will return immediatly */ 1980 case MOTORWAIT: 1981 if(fd->flags & FD_MOTOR_WAIT) 1982 { 1983 return(0); /* time's not up yet */ 1984 } 1985 if (fdc->flags & FDC_NEEDS_RESET) { 1986 fdc->state = RESETCTLR; 1987 fdc->flags &= ~FDC_NEEDS_RESET; 1988 } else { 1989 /* 1990 * If all motors were off, then the controller was 1991 * reset, so it has lost track of the current 1992 * cylinder. Recalibrate to handle this case. 1993 */ 1994 fdc->state = STARTRECAL; 1995 } 1996 return(1); /* will return immediatly */ 1997 default: 1998 printf("fdc%d: Unexpected FD int->", fdcu); 1999 if (fd_read_status(fdc, fd->fdsu) == 0) 2000 printf("FDC status :%x %x %x %x %x %x %x ", 2001 fdc->status[0], 2002 fdc->status[1], 2003 fdc->status[2], 2004 fdc->status[3], 2005 fdc->status[4], 2006 fdc->status[5], 2007 fdc->status[6] ); 2008 else 2009 printf("No status available "); 2010 if (fd_sense_int(fdc, &st0, &cyl) != 0) 2011 { 2012 printf("[controller is dead now]\n"); 2013 return(0); 2014 } 2015 printf("ST0 = %x, PCN = %x\n", st0, cyl); 2016 return(0); 2017 } 2018 /*XXX confusing: some branches return immediately, others end up here*/ 2019 return(1); /* Come back immediatly to new state */ 2020 } 2021 2022 static int 2023 retrier(fdcu) 2024 fdcu_t fdcu; 2025 { 2026 fdc_p fdc = fdc_data + fdcu; 2027 register struct buf *bp; 2028 2029 bp = bufq_first(&fdc->head); 2030 2031 if(fd_data[FDUNIT(minor(bp->b_dev))].options & FDOPT_NORETRY) 2032 goto fail; 2033 switch(fdc->retry) 2034 { 2035 case 0: case 1: case 2: 2036 fdc->state = SEEKCOMPLETE; 2037 break; 2038 case 3: case 4: case 5: 2039 fdc->state = STARTRECAL; 2040 break; 2041 case 6: 2042 fdc->state = RESETCTLR; 2043 break; 2044 case 7: 2045 break; 2046 default: 2047 fail: 2048 { 2049 dev_t sav_b_dev = bp->b_dev; 2050 /* Trick diskerr */ 2051 bp->b_dev = makedev(major(bp->b_dev), 2052 (FDUNIT(minor(bp->b_dev))<<3)|RAW_PART); 2053 diskerr(bp, "fd", "hard error", LOG_PRINTF, 2054 fdc->fd->skip / DEV_BSIZE, 2055 (struct disklabel *)NULL); 2056 bp->b_dev = sav_b_dev; 2057 if (fdc->flags & FDC_STAT_VALID) 2058 { 2059 printf( 2060 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2061 fdc->status[0], NE7_ST0BITS, 2062 fdc->status[1], NE7_ST1BITS, 2063 fdc->status[2], NE7_ST2BITS, 2064 fdc->status[3], fdc->status[4], 2065 fdc->status[5]); 2066 } 2067 else 2068 printf(" (No status)\n"); 2069 } 2070 bp->b_flags |= B_ERROR; 2071 bp->b_error = EIO; 2072 bp->b_resid += bp->b_bcount - fdc->fd->skip; 2073 bufq_remove(&fdc->head, bp); 2074 2075 /* Tell devstat we have finished with the transaction */ 2076 devstat_end_transaction(&fdc->fd->device_stats, 2077 bp->b_bcount - bp->b_resid, 2078 DEVSTAT_TAG_NONE, 2079 (bp->b_flags & B_READ) ? DEVSTAT_READ : 2080 DEVSTAT_WRITE); 2081 fdc->fd->skip = 0; 2082 biodone(bp); 2083 fdc->state = FINDWORK; 2084 fdc->flags |= FDC_NEEDS_RESET; 2085 fdc->fd = (fd_p) 0; 2086 fdc->fdu = -1; 2087 return(1); 2088 } 2089 fdc->retry++; 2090 return(1); 2091 } 2092 2093 static int 2094 fdformat(dev, finfo, p) 2095 dev_t dev; 2096 struct fd_formb *finfo; 2097 struct proc *p; 2098 { 2099 fdu_t fdu; 2100 fd_p fd; 2101 2102 struct buf *bp; 2103 int rv = 0, s; 2104 size_t fdblk; 2105 2106 fdu = FDUNIT(minor(dev)); 2107 fd = &fd_data[fdu]; 2108 fdblk = 128 << fd->ft->secsize; 2109 2110 /* set up a buffer header for fdstrategy() */ 2111 bp = (struct buf *)malloc(sizeof(struct buf), M_TEMP, M_NOWAIT); 2112 if(bp == 0) 2113 return ENOBUFS; 2114 /* 2115 * keep the process from being swapped 2116 */ 2117 p->p_flag |= P_PHYSIO; 2118 bzero((void *)bp, sizeof(struct buf)); 2119 bp->b_flags = B_BUSY | B_PHYS | B_FORMAT; 2120 bp->b_proc = p; 2121 2122 /* 2123 * calculate a fake blkno, so fdstrategy() would initiate a 2124 * seek to the requested cylinder 2125 */ 2126 bp->b_blkno = (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) 2127 + finfo->head * fd->ft->sectrac) * fdblk / DEV_BSIZE; 2128 2129 bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs; 2130 bp->b_data = (caddr_t)finfo; 2131 2132 /* now do the format */ 2133 bp->b_dev = dev; 2134 fdstrategy(bp); 2135 2136 /* ...and wait for it to complete */ 2137 s = splbio(); 2138 while(!(bp->b_flags & B_DONE)) 2139 { 2140 rv = tsleep((caddr_t)bp, PRIBIO, "fdform", 20 * hz); 2141 if(rv == EWOULDBLOCK) 2142 break; 2143 } 2144 splx(s); 2145 2146 if(rv == EWOULDBLOCK) { 2147 /* timed out */ 2148 rv = EIO; 2149 biodone(bp); 2150 } 2151 if(bp->b_flags & B_ERROR) 2152 rv = bp->b_error; 2153 /* 2154 * allow the process to be swapped 2155 */ 2156 p->p_flag &= ~P_PHYSIO; 2157 free(bp, M_TEMP); 2158 return rv; 2159 } 2160 2161 /* 2162 * TODO: don't allocate buffer on stack. 2163 */ 2164 2165 static int 2166 fdioctl(dev, cmd, addr, flag, p) 2167 dev_t dev; 2168 u_long cmd; 2169 caddr_t addr; 2170 int flag; 2171 struct proc *p; 2172 { 2173 fdu_t fdu = FDUNIT(minor(dev)); 2174 fd_p fd = &fd_data[fdu]; 2175 size_t fdblk; 2176 2177 struct fd_type *fdt; 2178 struct disklabel *dl; 2179 char buffer[DEV_BSIZE]; 2180 int error = 0; 2181 2182 fdblk = 128 << fd->ft->secsize; 2183 2184 switch (cmd) 2185 { 2186 case DIOCGDINFO: 2187 bzero(buffer, sizeof (buffer)); 2188 dl = (struct disklabel *)buffer; 2189 dl->d_secsize = fdblk; 2190 fdt = fd_data[FDUNIT(minor(dev))].ft; 2191 dl->d_secpercyl = fdt->size / fdt->tracks; 2192 dl->d_type = DTYPE_FLOPPY; 2193 2194 if (readdisklabel(dkmodpart(dev, RAW_PART), fdstrategy, dl) 2195 == NULL) 2196 error = 0; 2197 else 2198 error = EINVAL; 2199 2200 *(struct disklabel *)addr = *dl; 2201 break; 2202 2203 case DIOCSDINFO: 2204 if ((flag & FWRITE) == 0) 2205 error = EBADF; 2206 break; 2207 2208 case DIOCWLABEL: 2209 if ((flag & FWRITE) == 0) 2210 error = EBADF; 2211 break; 2212 2213 case DIOCWDINFO: 2214 if ((flag & FWRITE) == 0) 2215 { 2216 error = EBADF; 2217 break; 2218 } 2219 2220 dl = (struct disklabel *)addr; 2221 2222 if ((error = setdisklabel((struct disklabel *)buffer, dl, 2223 (u_long)0)) != 0) 2224 break; 2225 2226 error = writedisklabel(dev, fdstrategy, 2227 (struct disklabel *)buffer); 2228 break; 2229 case FD_FORM: 2230 if((flag & FWRITE) == 0) 2231 error = EBADF; /* must be opened for writing */ 2232 else if(((struct fd_formb *)addr)->format_version != 2233 FD_FORMAT_VERSION) 2234 error = EINVAL; /* wrong version of formatting prog */ 2235 else 2236 error = fdformat(dev, (struct fd_formb *)addr, p); 2237 break; 2238 2239 case FD_GTYPE: /* get drive type */ 2240 *(struct fd_type *)addr = *fd->ft; 2241 break; 2242 2243 case FD_STYPE: /* set drive type */ 2244 /* this is considered harmful; only allow for superuser */ 2245 if(suser(p->p_ucred, &p->p_acflag) != 0) 2246 return EPERM; 2247 *fd->ft = *(struct fd_type *)addr; 2248 break; 2249 2250 case FD_GOPTS: /* get drive options */ 2251 *(int *)addr = fd->options; 2252 break; 2253 2254 case FD_SOPTS: /* set drive options */ 2255 fd->options = *(int *)addr; 2256 break; 2257 2258 default: 2259 error = ENOTTY; 2260 break; 2261 } 2262 return (error); 2263 } 2264 2265 2266 static fd_devsw_installed = 0; 2267 2268 static void fd_drvinit(void *notused ) 2269 { 2270 2271 if( ! fd_devsw_installed ) { 2272 cdevsw_add_generic(BDEV_MAJOR,CDEV_MAJOR, &fd_cdevsw); 2273 fd_devsw_installed = 1; 2274 } 2275 } 2276 2277 SYSINIT(fddev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,fd_drvinit,NULL) 2278 2279 2280 #endif 2281 2282 /* 2283 * Hello emacs, these are the 2284 * Local Variables: 2285 * c-indent-level: 8 2286 * c-continued-statement-offset: 8 2287 * c-continued-brace-offset: 0 2288 * c-brace-offset: -8 2289 * c-brace-imaginary-offset: 0 2290 * c-argdecl-indent: 8 2291 * c-label-offset: -8 2292 * c++-hanging-braces: 1 2293 * c++-access-specifier-offset: -8 2294 * c++-empty-arglist-indent: 8 2295 * c++-friend-offset: 0 2296 * End: 2297 */ 2298