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 * Copyright (c) 2001 Joerg Wunsch, 22 * joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) 23 * 24 * Redistribution and use in source and binary forms, with or without 25 * modification, are permitted provided that the following conditions 26 * are met: 27 * 1. Redistributions of source code must retain the above copyright 28 * notice, this list of conditions and the following disclaimer. 29 * 2. Redistributions in binary form must reproduce the above copyright 30 * notice, this list of conditions and the following disclaimer in the 31 * documentation and/or other materials provided with the distribution. 32 * 3. All advertising materials mentioning features or use of this software 33 * must display the following acknowledgement: 34 * This product includes software developed by the University of 35 * California, Berkeley and its contributors. 36 * 4. Neither the name of the University nor the names of its contributors 37 * may be used to endorse or promote products derived from this software 38 * without specific prior written permission. 39 * 40 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 43 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 50 * SUCH DAMAGE. 51 * 52 * from: @(#)fd.c 7.4 (Berkeley) 5/25/91 53 * $FreeBSD$ 54 */ 55 56 #include "opt_fdc.h" 57 #include "card.h" 58 59 #include <sys/param.h> 60 #include <sys/systm.h> 61 #include <sys/bio.h> 62 #include <sys/bus.h> 63 #include <sys/conf.h> 64 #include <sys/devicestat.h> 65 #include <sys/disk.h> 66 #include <sys/disklabel.h> 67 #include <sys/fcntl.h> 68 #include <sys/fdcio.h> 69 #include <sys/filio.h> 70 #include <sys/kernel.h> 71 #include <sys/lock.h> 72 #include <sys/malloc.h> 73 #include <sys/module.h> 74 #include <sys/mutex.h> 75 #include <sys/proc.h> 76 #include <sys/syslog.h> 77 78 #include <machine/bus.h> 79 #include <sys/rman.h> 80 81 #include <machine/clock.h> 82 #include <machine/resource.h> 83 #include <machine/stdarg.h> 84 85 #include <isa/isavar.h> 86 #include <isa/isareg.h> 87 #include <isa/fdreg.h> 88 #include <isa/rtc.h> 89 90 enum fdc_type 91 { 92 FDC_NE765, FDC_ENHANCED, FDC_UNKNOWN = -1 93 }; 94 95 enum fdc_states { 96 DEVIDLE, 97 FINDWORK, 98 DOSEEK, 99 SEEKCOMPLETE , 100 IOCOMPLETE, 101 RECALCOMPLETE, 102 STARTRECAL, 103 RESETCTLR, 104 SEEKWAIT, 105 RECALWAIT, 106 MOTORWAIT, 107 IOTIMEDOUT, 108 RESETCOMPLETE, 109 PIOREAD 110 }; 111 112 #ifdef FDC_DEBUG 113 static char const * const fdstates[] = { 114 "DEVIDLE", 115 "FINDWORK", 116 "DOSEEK", 117 "SEEKCOMPLETE", 118 "IOCOMPLETE", 119 "RECALCOMPLETE", 120 "STARTRECAL", 121 "RESETCTLR", 122 "SEEKWAIT", 123 "RECALWAIT", 124 "MOTORWAIT", 125 "IOTIMEDOUT", 126 "RESETCOMPLETE", 127 "PIOREAD" 128 }; 129 #endif 130 131 /* 132 * Per controller structure (softc). 133 */ 134 struct fdc_data 135 { 136 int fdcu; /* our unit number */ 137 int dmachan; 138 int flags; 139 #define FDC_ATTACHED 0x01 140 #define FDC_STAT_VALID 0x08 141 #define FDC_HAS_FIFO 0x10 142 #define FDC_NEEDS_RESET 0x20 143 #define FDC_NODMA 0x40 144 #define FDC_ISPNP 0x80 145 #define FDC_ISPCMCIA 0x100 146 struct fd_data *fd; 147 int fdu; /* the active drive */ 148 enum fdc_states state; 149 int retry; 150 int fdout; /* mirror of the w/o digital output reg */ 151 u_int status[7]; /* copy of the registers */ 152 enum fdc_type fdct; /* chip version of FDC */ 153 int fdc_errs; /* number of logged errors */ 154 int dma_overruns; /* number of DMA overruns */ 155 struct bio_queue_head head; 156 struct bio *bp; /* active buffer */ 157 struct resource *res_ioport, *res_ctl, *res_irq, *res_drq; 158 int rid_ioport, rid_ctl, rid_irq, rid_drq; 159 int port_off; 160 bus_space_tag_t portt; 161 bus_space_handle_t porth; 162 bus_space_tag_t ctlt; 163 bus_space_handle_t ctlh; 164 void *fdc_intr; 165 struct device *fdc_dev; 166 void (*fdctl_wr)(struct fdc_data *fdc, u_int8_t v); 167 }; 168 169 #define BIO_FORMAT BIO_CMD2 170 171 typedef int fdu_t; 172 typedef int fdcu_t; 173 typedef int fdsu_t; 174 typedef struct fd_data *fd_p; 175 typedef struct fdc_data *fdc_p; 176 typedef enum fdc_type fdc_t; 177 178 #define FDUNIT(s) (((s) >> 6) & 3) 179 #define FDNUMTOUNIT(n) (((n) & 3) << 6) 180 #define FDTYPE(s) ((s) & 0x3f) 181 182 /* 183 * fdc maintains a set (1!) of ivars per child of each controller. 184 */ 185 enum fdc_device_ivars { 186 FDC_IVAR_FDUNIT, 187 }; 188 189 /* 190 * Simple access macros for the ivars. 191 */ 192 #define FDC_ACCESSOR(A, B, T) \ 193 static __inline T fdc_get_ ## A(device_t dev) \ 194 { \ 195 uintptr_t v; \ 196 BUS_READ_IVAR(device_get_parent(dev), dev, FDC_IVAR_ ## B, &v); \ 197 return (T) v; \ 198 } 199 FDC_ACCESSOR(fdunit, FDUNIT, int) 200 201 /* configuration flags for fdc */ 202 #define FDC_NO_FIFO (1 << 2) /* do not enable FIFO */ 203 204 /* error returns for fd_cmd() */ 205 #define FD_FAILED -1 206 #define FD_NOT_VALID -2 207 #define FDC_ERRMAX 100 /* do not log more */ 208 /* 209 * Stop retrying after this many DMA overruns. Since each retry takes 210 * one revolution, with 300 rpm., 25 retries take approximately 5 211 * seconds which the read attempt will block in case the DMA overrun 212 * is persistent. 213 */ 214 #define FDC_DMAOV_MAX 25 215 216 /* 217 * Timeout value for the PIO loops to wait until the FDC main status 218 * register matches our expectations (request for master, direction 219 * bit). This is supposed to be a number of microseconds, although 220 * timing might actually not be very accurate. 221 * 222 * Timeouts of 100 msec are believed to be required for some broken 223 * (old) hardware. 224 */ 225 #define FDSTS_TIMEOUT 100000 226 227 /* 228 * Number of subdevices that can be used for different density types. 229 * By now, the lower 6 bit of the minor number are reserved for this, 230 * allowing for up to 64 subdevices, but we only use 16 out of this. 231 * Density #0 is used for automatic format detection, the other 232 * densities are available as programmable densities (for assignment 233 * by fdcontrol(8)). 234 * The upper 2 bits of the minor number are reserved for the subunit 235 * (drive #) per controller. 236 */ 237 #define NUMDENS 16 238 239 #define BIO_RDSECTID BIO_CMD1 240 241 /* 242 * List of native drive densities. Order must match enum fd_drivetype 243 * in <sys/fdcio.h>. Upon attaching the drive, each of the 244 * programmable subdevices is initialized with the native density 245 * definition. 246 */ 247 static struct fd_type fd_native_types[] = 248 { 249 { 0 }, /* FDT_NONE */ 250 { 9,2,0xFF,0x2A,40, 720,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_360K */ 251 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* FDT_12M */ 252 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* FDT_720K */ 253 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 254 #if 0 /* we currently don't handle 2.88 MB */ 255 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /*FDT_288M*/ 256 #else 257 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* FDT_144M */ 258 #endif 259 }; 260 261 /* 262 * 360 KB 5.25" and 720 KB 3.5" drives don't have automatic density 263 * selection, they just start out with their native density (or lose). 264 * So 1.2 MB 5.25", 1.44 MB 3.5", and 2.88 MB 3.5" drives have their 265 * respective lists of densities to search for. 266 */ 267 static struct fd_type fd_searchlist_12m[] = { 268 { 15,2,0xFF,0x1B,80,2400,FDC_500KBPS,2,0x54,1,0,FL_MFM }, /* 1.2M */ 269 { 9,2,0xFF,0x23,40, 720,FDC_300KBPS,2,0x50,1,0,FL_MFM|FL_2STEP }, /* 360K */ 270 { 9,2,0xFF,0x20,80,1440,FDC_300KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 271 }; 272 273 static struct fd_type fd_searchlist_144m[] = { 274 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 275 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 276 }; 277 278 /* We search for 1.44M first since this is the most common case. */ 279 static struct fd_type fd_searchlist_288m[] = { 280 { 18,2,0xFF,0x1B,80,2880,FDC_500KBPS,2,0x6C,1,0,FL_MFM }, /* 1.44M */ 281 #if 0 282 { 36,2,0xFF,0x1B,80,5760,FDC_1MBPS, 2,0x4C,1,1,FL_MFM|FL_PERPND } /* 2.88M */ 283 #endif 284 { 9,2,0xFF,0x20,80,1440,FDC_250KBPS,2,0x50,1,0,FL_MFM }, /* 720K */ 285 }; 286 287 #define MAX_SEC_SIZE (128 << 3) 288 #define MAX_CYLINDER 85 /* some people really stress their drives 289 * up to cyl 82 */ 290 #define MAX_HEAD 1 291 292 static devclass_t fdc_devclass; 293 294 /* 295 * Per drive structure (softc). 296 */ 297 struct fd_data { 298 struct fdc_data *fdc; /* pointer to controller structure */ 299 int fdsu; /* this units number on this controller */ 300 enum fd_drivetype type; /* drive type */ 301 struct fd_type *ft; /* pointer to current type descriptor */ 302 struct fd_type fts[NUMDENS]; /* type descriptors */ 303 int flags; 304 #define FD_OPEN 0x01 /* it's open */ 305 #define FD_NONBLOCK 0x02 /* O_NONBLOCK set */ 306 #define FD_ACTIVE 0x04 /* it's active */ 307 #define FD_MOTOR 0x08 /* motor should be on */ 308 #define FD_MOTOR_WAIT 0x10 /* motor coming up */ 309 #define FD_UA 0x20 /* force unit attention */ 310 int skip; 311 int hddrv; 312 #define FD_NO_TRACK -2 313 int track; /* where we think the head is */ 314 int options; /* user configurable options, see fdcio.h */ 315 struct callout_handle toffhandle; 316 struct callout_handle tohandle; 317 struct devstat device_stats; 318 eventhandler_tag clonetag; 319 dev_t masterdev; 320 dev_t clonedevs[NUMDENS - 1]; 321 device_t dev; 322 fdu_t fdu; 323 }; 324 325 struct fdc_ivars { 326 int fdunit; 327 }; 328 static devclass_t fd_devclass; 329 330 /* configuration flags for fd */ 331 #define FD_TYPEMASK 0x0f /* drive type, matches enum 332 * fd_drivetype; on i386 machines, if 333 * given as 0, use RTC type for fd0 334 * and fd1 */ 335 #define FD_DTYPE(flags) ((flags) & FD_TYPEMASK) 336 #define FD_NO_CHLINE 0x10 /* drive does not support changeline 337 * aka. unit attention */ 338 #define FD_NO_PROBE 0x20 /* don't probe drive (seek test), just 339 * assume it is there */ 340 341 /* 342 * Throughout this file the following conventions will be used: 343 * 344 * fd is a pointer to the fd_data struct for the drive in question 345 * fdc is a pointer to the fdc_data struct for the controller 346 * fdu is the floppy drive unit number 347 * fdcu is the floppy controller unit number 348 * fdsu is the floppy drive unit number on that controller. (sub-unit) 349 */ 350 351 /* 352 * Function declarations, same (chaotic) order as they appear in the 353 * file. Re-ordering is too late now, it would only obfuscate the 354 * diffs against old and offspring versions (like the PC98 one). 355 * 356 * Anyone adding functions here, please keep this sequence the same 357 * as below -- makes locating a particular function in the body much 358 * easier. 359 */ 360 static void fdout_wr(fdc_p, u_int8_t); 361 static u_int8_t fdsts_rd(fdc_p); 362 static void fddata_wr(fdc_p, u_int8_t); 363 static u_int8_t fddata_rd(fdc_p); 364 static void fdctl_wr_isa(fdc_p, u_int8_t); 365 #if NCARD > 0 366 static void fdctl_wr_pcmcia(fdc_p, u_int8_t); 367 #endif 368 #if 0 369 static u_int8_t fdin_rd(fdc_p); 370 #endif 371 static int fdc_err(struct fdc_data *, const char *); 372 static int fd_cmd(struct fdc_data *, int, ...); 373 static int enable_fifo(fdc_p fdc); 374 static int fd_sense_drive_status(fdc_p, int *); 375 static int fd_sense_int(fdc_p, int *, int *); 376 static int fd_read_status(fdc_p); 377 static int fdc_alloc_resources(struct fdc_data *); 378 static void fdc_release_resources(struct fdc_data *); 379 static int fdc_read_ivar(device_t, device_t, int, uintptr_t *); 380 static int fdc_probe(device_t); 381 #if NCARD > 0 382 static int fdc_pccard_probe(device_t); 383 #endif 384 static int fdc_detach(device_t dev); 385 static void fdc_add_child(device_t, const char *, int); 386 static int fdc_attach(device_t); 387 static int fdc_print_child(device_t, device_t); 388 static void fd_clone (void *, char *, int, dev_t *); 389 static int fd_probe(device_t); 390 static int fd_attach(device_t); 391 static int fd_detach(device_t); 392 static void set_motor(struct fdc_data *, int, int); 393 # define TURNON 1 394 # define TURNOFF 0 395 static timeout_t fd_turnoff; 396 static timeout_t fd_motor_on; 397 static void fd_turnon(struct fd_data *); 398 static void fdc_reset(fdc_p); 399 static int fd_in(struct fdc_data *, int *); 400 static int out_fdc(struct fdc_data *, int); 401 /* 402 * The open function is named Fdopen() to avoid confusion with fdopen() 403 * in fd(4). The difference is now only meaningful for debuggers. 404 */ 405 static d_open_t Fdopen; 406 static d_close_t fdclose; 407 static d_strategy_t fdstrategy; 408 static void fdstart(struct fdc_data *); 409 static timeout_t fd_iotimeout; 410 static timeout_t fd_pseudointr; 411 static driver_intr_t fdc_intr; 412 static int fdcpio(fdc_p, long, caddr_t, u_int); 413 static int fdautoselect(dev_t); 414 static int fdstate(struct fdc_data *); 415 static int retrier(struct fdc_data *); 416 static void fdbiodone(struct bio *); 417 static int fdmisccmd(dev_t, u_int, void *); 418 static d_ioctl_t fdioctl; 419 420 static int fifo_threshold = 8; /* XXX: should be accessible via sysctl */ 421 422 #ifdef FDC_DEBUG 423 /* CAUTION: fd_debug causes huge amounts of logging output */ 424 static int volatile fd_debug = 0; 425 #define TRACE0(arg) do { if (fd_debug) printf(arg); } while (0) 426 #define TRACE1(arg1, arg2) do { if (fd_debug) printf(arg1, arg2); } while (0) 427 #else /* FDC_DEBUG */ 428 #define TRACE0(arg) do { } while (0) 429 #define TRACE1(arg1, arg2) do { } while (0) 430 #endif /* FDC_DEBUG */ 431 432 /* 433 * Bus space handling (access to low-level IO). 434 */ 435 static void 436 fdout_wr(fdc_p fdc, u_int8_t v) 437 { 438 bus_space_write_1(fdc->portt, fdc->porth, FDOUT+fdc->port_off, v); 439 } 440 441 static u_int8_t 442 fdsts_rd(fdc_p fdc) 443 { 444 return bus_space_read_1(fdc->portt, fdc->porth, FDSTS+fdc->port_off); 445 } 446 447 static void 448 fddata_wr(fdc_p fdc, u_int8_t v) 449 { 450 bus_space_write_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off, v); 451 } 452 453 static u_int8_t 454 fddata_rd(fdc_p fdc) 455 { 456 return bus_space_read_1(fdc->portt, fdc->porth, FDDATA+fdc->port_off); 457 } 458 459 static void 460 fdctl_wr_isa(fdc_p fdc, u_int8_t v) 461 { 462 bus_space_write_1(fdc->ctlt, fdc->ctlh, 0, v); 463 } 464 465 #if NCARD > 0 466 static void 467 fdctl_wr_pcmcia(fdc_p fdc, u_int8_t v) 468 { 469 bus_space_write_1(fdc->portt, fdc->porth, FDCTL+fdc->port_off, v); 470 } 471 #endif 472 473 static u_int8_t 474 fdin_rd(fdc_p fdc) 475 { 476 return bus_space_read_1(fdc->portt, fdc->porth, FDIN); 477 } 478 479 #define CDEV_MAJOR 9 480 static struct cdevsw fd_cdevsw = { 481 /* open */ Fdopen, 482 /* close */ fdclose, 483 /* read */ physread, 484 /* write */ physwrite, 485 /* ioctl */ fdioctl, 486 /* poll */ nopoll, 487 /* mmap */ nommap, 488 /* strategy */ fdstrategy, 489 /* name */ "fd", 490 /* maj */ CDEV_MAJOR, 491 /* dump */ nodump, 492 /* psize */ nopsize, 493 /* flags */ D_DISK, 494 }; 495 496 /* 497 * Auxiliary functions. Well, some only. Others are scattered 498 * throughout the entire file. 499 */ 500 static int 501 fdc_err(struct fdc_data *fdc, const char *s) 502 { 503 fdc->fdc_errs++; 504 if (s) { 505 if (fdc->fdc_errs < FDC_ERRMAX) 506 device_printf(fdc->fdc_dev, "%s", s); 507 else if (fdc->fdc_errs == FDC_ERRMAX) 508 device_printf(fdc->fdc_dev, "too many errors, not " 509 "logging any more\n"); 510 } 511 512 return FD_FAILED; 513 } 514 515 /* 516 * fd_cmd: Send a command to the chip. Takes a varargs with this structure: 517 * Unit number, 518 * # of output bytes, output bytes as ints ..., 519 * # of input bytes, input bytes as ints ... 520 */ 521 static int 522 fd_cmd(struct fdc_data *fdc, int n_out, ...) 523 { 524 u_char cmd; 525 int n_in; 526 int n; 527 va_list ap; 528 529 va_start(ap, n_out); 530 cmd = (u_char)(va_arg(ap, int)); 531 va_end(ap); 532 va_start(ap, n_out); 533 for (n = 0; n < n_out; n++) 534 { 535 if (out_fdc(fdc, va_arg(ap, int)) < 0) 536 { 537 char msg[50]; 538 snprintf(msg, sizeof(msg), 539 "cmd %x failed at out byte %d of %d\n", 540 cmd, n + 1, n_out); 541 return fdc_err(fdc, msg); 542 } 543 } 544 n_in = va_arg(ap, int); 545 for (n = 0; n < n_in; n++) 546 { 547 int *ptr = va_arg(ap, int *); 548 if (fd_in(fdc, ptr) < 0) 549 { 550 char msg[50]; 551 snprintf(msg, sizeof(msg), 552 "cmd %02x failed at in byte %d of %d\n", 553 cmd, n + 1, n_in); 554 return fdc_err(fdc, msg); 555 } 556 } 557 558 return 0; 559 } 560 561 static int 562 enable_fifo(fdc_p fdc) 563 { 564 int i, j; 565 566 if ((fdc->flags & FDC_HAS_FIFO) == 0) { 567 568 /* 569 * Cannot use fd_cmd the normal way here, since 570 * this might be an invalid command. Thus we send the 571 * first byte, and check for an early turn of data directon. 572 */ 573 574 if (out_fdc(fdc, I8207X_CONFIGURE) < 0) 575 return fdc_err(fdc, "Enable FIFO failed\n"); 576 577 /* If command is invalid, return */ 578 j = FDSTS_TIMEOUT; 579 while ((i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM)) 580 != NE7_RQM && j-- > 0) { 581 if (i == (NE7_DIO | NE7_RQM)) { 582 fdc_reset(fdc); 583 return FD_FAILED; 584 } 585 DELAY(1); 586 } 587 if (j<0 || 588 fd_cmd(fdc, 3, 589 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) { 590 fdc_reset(fdc); 591 return fdc_err(fdc, "Enable FIFO failed\n"); 592 } 593 fdc->flags |= FDC_HAS_FIFO; 594 return 0; 595 } 596 if (fd_cmd(fdc, 4, 597 I8207X_CONFIGURE, 0, (fifo_threshold - 1) & 0xf, 0, 0) < 0) 598 return fdc_err(fdc, "Re-enable FIFO failed\n"); 599 return 0; 600 } 601 602 static int 603 fd_sense_drive_status(fdc_p fdc, int *st3p) 604 { 605 int st3; 606 607 if (fd_cmd(fdc, 2, NE7CMD_SENSED, fdc->fdu, 1, &st3)) 608 { 609 return fdc_err(fdc, "Sense Drive Status failed\n"); 610 } 611 if (st3p) 612 *st3p = st3; 613 614 return 0; 615 } 616 617 static int 618 fd_sense_int(fdc_p fdc, int *st0p, int *cylp) 619 { 620 int cyl, st0, ret; 621 622 ret = fd_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0); 623 if (ret) { 624 (void)fdc_err(fdc, 625 "sense intr err reading stat reg 0\n"); 626 return ret; 627 } 628 629 if (st0p) 630 *st0p = st0; 631 632 if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) { 633 /* 634 * There doesn't seem to have been an interrupt. 635 */ 636 return FD_NOT_VALID; 637 } 638 639 if (fd_in(fdc, &cyl) < 0) { 640 return fdc_err(fdc, "can't get cyl num\n"); 641 } 642 643 if (cylp) 644 *cylp = cyl; 645 646 return 0; 647 } 648 649 650 static int 651 fd_read_status(fdc_p fdc) 652 { 653 int i, ret; 654 655 for (i = ret = 0; i < 7; i++) { 656 /* 657 * XXX types are poorly chosen. Only bytes can be read 658 * from the hardware, but fdc->status[] wants u_ints and 659 * fd_in() gives ints. 660 */ 661 int status; 662 663 ret = fd_in(fdc, &status); 664 fdc->status[i] = status; 665 if (ret != 0) 666 break; 667 } 668 669 if (ret == 0) 670 fdc->flags |= FDC_STAT_VALID; 671 else 672 fdc->flags &= ~FDC_STAT_VALID; 673 674 return ret; 675 } 676 677 static int 678 fdc_alloc_resources(struct fdc_data *fdc) 679 { 680 device_t dev; 681 int ispnp, ispcmcia, nports; 682 683 dev = fdc->fdc_dev; 684 ispnp = (fdc->flags & FDC_ISPNP) != 0; 685 ispcmcia = (fdc->flags & FDC_ISPCMCIA) != 0; 686 fdc->rid_ioport = fdc->rid_irq = fdc->rid_drq = 0; 687 fdc->res_ioport = fdc->res_irq = fdc->res_drq = 0; 688 689 /* 690 * On standard ISA, we don't just use an 8 port range 691 * (e.g. 0x3f0-0x3f7) since that covers an IDE control 692 * register at 0x3f6. 693 * 694 * Isn't PC hardware wonderful. 695 * 696 * The Y-E Data PCMCIA FDC doesn't have this problem, it 697 * uses the register with offset 6 for pseudo-DMA, and the 698 * one with offset 7 as control register. 699 */ 700 nports = ispcmcia ? 8 : (ispnp ? 1 : 6); 701 fdc->res_ioport = bus_alloc_resource(dev, SYS_RES_IOPORT, 702 &fdc->rid_ioport, 0ul, ~0ul, 703 nports, RF_ACTIVE); 704 if (fdc->res_ioport == 0) { 705 device_printf(dev, "cannot reserve I/O port range (%d ports)\n", 706 nports); 707 return ENXIO; 708 } 709 fdc->portt = rman_get_bustag(fdc->res_ioport); 710 fdc->porth = rman_get_bushandle(fdc->res_ioport); 711 712 if (!ispcmcia) { 713 /* 714 * Some BIOSen report the device at 0x3f2-0x3f5,0x3f7 715 * and some at 0x3f0-0x3f5,0x3f7. We detect the former 716 * by checking the size and adjust the port address 717 * accordingly. 718 */ 719 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 0) == 4) 720 fdc->port_off = -2; 721 722 /* 723 * Register the control port range as rid 1 if it 724 * isn't there already. Most PnP BIOSen will have 725 * already done this but non-PnP configurations don't. 726 * 727 * And some (!!) report 0x3f2-0x3f5 and completely 728 * leave out the control register! It seems that some 729 * non-antique controller chips have a different 730 * method of programming the transfer speed which 731 * doesn't require the control register, but it's 732 * mighty bogus as the chip still responds to the 733 * address for the control register. 734 */ 735 if (bus_get_resource_count(dev, SYS_RES_IOPORT, 1) == 0) { 736 u_long ctlstart; 737 738 /* Find the control port, usually 0x3f7 */ 739 ctlstart = rman_get_start(fdc->res_ioport) + 740 fdc->port_off + 7; 741 742 bus_set_resource(dev, SYS_RES_IOPORT, 1, ctlstart, 1); 743 } 744 745 /* 746 * Now (finally!) allocate the control port. 747 */ 748 fdc->rid_ctl = 1; 749 fdc->res_ctl = bus_alloc_resource(dev, SYS_RES_IOPORT, 750 &fdc->rid_ctl, 751 0ul, ~0ul, 1, RF_ACTIVE); 752 if (fdc->res_ctl == 0) { 753 device_printf(dev, 754 "cannot reserve control I/O port range (control port)\n"); 755 return ENXIO; 756 } 757 fdc->ctlt = rman_get_bustag(fdc->res_ctl); 758 fdc->ctlh = rman_get_bushandle(fdc->res_ctl); 759 } 760 761 fdc->res_irq = bus_alloc_resource(dev, SYS_RES_IRQ, 762 &fdc->rid_irq, 0ul, ~0ul, 1, 763 RF_ACTIVE); 764 if (fdc->res_irq == 0) { 765 device_printf(dev, "cannot reserve interrupt line\n"); 766 return ENXIO; 767 } 768 769 if ((fdc->flags & FDC_NODMA) == 0) { 770 fdc->res_drq = bus_alloc_resource(dev, SYS_RES_DRQ, 771 &fdc->rid_drq, 0ul, ~0ul, 1, 772 RF_ACTIVE); 773 if (fdc->res_drq == 0) { 774 device_printf(dev, "cannot reserve DMA request line\n"); 775 return ENXIO; 776 } 777 fdc->dmachan = fdc->res_drq->r_start; 778 } 779 780 return 0; 781 } 782 783 static void 784 fdc_release_resources(struct fdc_data *fdc) 785 { 786 device_t dev; 787 788 dev = fdc->fdc_dev; 789 if (fdc->res_irq != 0) { 790 bus_deactivate_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 791 fdc->res_irq); 792 bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq, 793 fdc->res_irq); 794 } 795 if (fdc->res_ctl != 0) { 796 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 797 fdc->res_ctl); 798 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ctl, 799 fdc->res_ctl); 800 } 801 if (fdc->res_ioport != 0) { 802 bus_deactivate_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 803 fdc->res_ioport); 804 bus_release_resource(dev, SYS_RES_IOPORT, fdc->rid_ioport, 805 fdc->res_ioport); 806 } 807 if (fdc->res_drq != 0) { 808 bus_deactivate_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 809 fdc->res_drq); 810 bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq, 811 fdc->res_drq); 812 } 813 } 814 815 /* 816 * Configuration/initialization stuff, per controller. 817 */ 818 819 static struct isa_pnp_id fdc_ids[] = { 820 {0x0007d041, "PC standard floppy disk controller"}, /* PNP0700 */ 821 {0x0107d041, "Standard floppy controller supporting MS Device Bay Spec"}, /* PNP0701 */ 822 {0} 823 }; 824 825 static int 826 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result) 827 { 828 struct fdc_ivars *ivars = device_get_ivars(child); 829 830 switch (which) { 831 case FDC_IVAR_FDUNIT: 832 *result = ivars->fdunit; 833 break; 834 default: 835 return ENOENT; 836 } 837 return 0; 838 } 839 840 static int 841 fdc_probe(device_t dev) 842 { 843 int error, ic_type; 844 struct fdc_data *fdc; 845 846 fdc = device_get_softc(dev); 847 bzero(fdc, sizeof *fdc); 848 fdc->fdc_dev = dev; 849 fdc->fdctl_wr = fdctl_wr_isa; 850 851 /* Check pnp ids */ 852 error = ISA_PNP_PROBE(device_get_parent(dev), dev, fdc_ids); 853 if (error == ENXIO) 854 return ENXIO; 855 if (error == 0) 856 fdc->flags |= FDC_ISPNP; 857 858 /* Attempt to allocate our resources for the duration of the probe */ 859 error = fdc_alloc_resources(fdc); 860 if (error) 861 goto out; 862 863 /* First - lets reset the floppy controller */ 864 fdout_wr(fdc, 0); 865 DELAY(100); 866 fdout_wr(fdc, FDO_FRST); 867 868 /* see if it can handle a command */ 869 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 870 NE7_SPEC_2(2, 0), 0)) { 871 error = ENXIO; 872 goto out; 873 } 874 875 if (fd_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type) == 0) { 876 ic_type = (u_char)ic_type; 877 switch (ic_type) { 878 case 0x80: 879 device_set_desc(dev, "NEC 765 or clone"); 880 fdc->fdct = FDC_NE765; 881 break; 882 case 0x81: /* not mentioned in any hardware doc */ 883 case 0x90: 884 device_set_desc(dev, 885 "enhanced floppy controller (i82077, NE72065 or clone)"); 886 fdc->fdct = FDC_ENHANCED; 887 break; 888 default: 889 device_set_desc(dev, "generic floppy controller"); 890 fdc->fdct = FDC_UNKNOWN; 891 break; 892 } 893 } 894 895 out: 896 fdc_release_resources(fdc); 897 return (error); 898 } 899 900 #if NCARD > 0 901 902 static int 903 fdc_pccard_probe(device_t dev) 904 { 905 int error; 906 struct fdc_data *fdc; 907 908 fdc = device_get_softc(dev); 909 bzero(fdc, sizeof *fdc); 910 fdc->fdc_dev = dev; 911 fdc->fdctl_wr = fdctl_wr_pcmcia; 912 913 fdc->flags |= FDC_ISPCMCIA | FDC_NODMA; 914 915 /* Attempt to allocate our resources for the duration of the probe */ 916 error = fdc_alloc_resources(fdc); 917 if (error) 918 goto out; 919 920 /* First - lets reset the floppy controller */ 921 fdout_wr(fdc, 0); 922 DELAY(100); 923 fdout_wr(fdc, FDO_FRST); 924 925 /* see if it can handle a command */ 926 if (fd_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(3, 240), 927 NE7_SPEC_2(2, 0), 0)) { 928 error = ENXIO; 929 goto out; 930 } 931 932 device_set_desc(dev, "Y-E Data PCMCIA floppy"); 933 fdc->fdct = FDC_NE765; 934 935 out: 936 fdc_release_resources(fdc); 937 return (error); 938 } 939 940 #endif /* NCARD > 0 */ 941 942 static int 943 fdc_detach(device_t dev) 944 { 945 struct fdc_data *fdc; 946 int error; 947 948 fdc = device_get_softc(dev); 949 950 /* have our children detached first */ 951 if ((error = bus_generic_detach(dev))) 952 return (error); 953 954 /* reset controller, turn motor off */ 955 fdout_wr(fdc, 0); 956 957 if ((fdc->flags & FDC_NODMA) == 0) 958 isa_dma_release(fdc->dmachan); 959 960 if ((fdc->flags & FDC_ATTACHED) == 0) { 961 device_printf(dev, "already unloaded\n"); 962 return (0); 963 } 964 fdc->flags &= ~FDC_ATTACHED; 965 966 BUS_TEARDOWN_INTR(device_get_parent(dev), dev, fdc->res_irq, 967 fdc->fdc_intr); 968 fdc_release_resources(fdc); 969 device_printf(dev, "unload\n"); 970 return (0); 971 } 972 973 /* 974 * Add a child device to the fdc controller. It will then be probed etc. 975 */ 976 static void 977 fdc_add_child(device_t dev, const char *name, int unit) 978 { 979 int disabled, flags; 980 struct fdc_ivars *ivar; 981 device_t child; 982 983 ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO); 984 if (ivar == NULL) 985 return; 986 if (resource_int_value(name, unit, "drive", &ivar->fdunit) != 0) 987 ivar->fdunit = 0; 988 child = device_add_child(dev, name, unit); 989 if (child == NULL) 990 return; 991 device_set_ivars(child, ivar); 992 if (resource_int_value(name, unit, "flags", &flags) == 0) 993 device_set_flags(child, flags); 994 if (resource_int_value(name, unit, "disabled", &disabled) == 0 995 && disabled != 0) 996 device_disable(child); 997 } 998 999 static int 1000 fdc_attach(device_t dev) 1001 { 1002 struct fdc_data *fdc; 1003 const char *name, *dname; 1004 int i, error, dunit; 1005 1006 fdc = device_get_softc(dev); 1007 error = fdc_alloc_resources(fdc); 1008 if (error) { 1009 device_printf(dev, "cannot re-acquire resources\n"); 1010 return error; 1011 } 1012 error = BUS_SETUP_INTR(device_get_parent(dev), dev, fdc->res_irq, 1013 INTR_TYPE_BIO | INTR_ENTROPY, fdc_intr, fdc, 1014 &fdc->fdc_intr); 1015 if (error) { 1016 device_printf(dev, "cannot setup interrupt\n"); 1017 return error; 1018 } 1019 fdc->fdcu = device_get_unit(dev); 1020 fdc->flags |= FDC_ATTACHED | FDC_NEEDS_RESET; 1021 1022 if ((fdc->flags & FDC_NODMA) == 0) { 1023 /* 1024 * Acquire the DMA channel forever, the driver will do 1025 * the rest 1026 * XXX should integrate with rman 1027 */ 1028 isa_dma_acquire(fdc->dmachan); 1029 isa_dmainit(fdc->dmachan, MAX_SEC_SIZE); 1030 } 1031 fdc->state = DEVIDLE; 1032 1033 /* reset controller, turn motor off, clear fdout mirror reg */ 1034 fdout_wr(fdc, fdc->fdout = 0); 1035 bioq_init(&fdc->head); 1036 1037 /* 1038 * Probe and attach any children. We should probably detect 1039 * devices from the BIOS unless overridden. 1040 */ 1041 name = device_get_nameunit(dev); 1042 i = 0; 1043 while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) 1044 fdc_add_child(dev, dname, dunit); 1045 1046 if ((error = bus_generic_attach(dev)) != 0) 1047 return (error); 1048 1049 return (0); 1050 } 1051 1052 static int 1053 fdc_print_child(device_t me, device_t child) 1054 { 1055 int retval = 0, flags; 1056 1057 retval += bus_print_child_header(me, child); 1058 retval += printf(" on %s drive %d", device_get_nameunit(me), 1059 fdc_get_fdunit(child)); 1060 if ((flags = device_get_flags(me)) != 0) 1061 retval += printf(" flags %#x", flags); 1062 retval += printf("\n"); 1063 1064 return (retval); 1065 } 1066 1067 static device_method_t fdc_methods[] = { 1068 /* Device interface */ 1069 DEVMETHOD(device_probe, fdc_probe), 1070 DEVMETHOD(device_attach, fdc_attach), 1071 DEVMETHOD(device_detach, fdc_detach), 1072 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1073 DEVMETHOD(device_suspend, bus_generic_suspend), 1074 DEVMETHOD(device_resume, bus_generic_resume), 1075 1076 /* Bus interface */ 1077 DEVMETHOD(bus_print_child, fdc_print_child), 1078 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 1079 /* Our children never use any other bus interface methods. */ 1080 1081 { 0, 0 } 1082 }; 1083 1084 static driver_t fdc_driver = { 1085 "fdc", 1086 fdc_methods, 1087 sizeof(struct fdc_data) 1088 }; 1089 1090 DRIVER_MODULE(fdc, isa, fdc_driver, fdc_devclass, 0, 0); 1091 DRIVER_MODULE(fdc, acpi, fdc_driver, fdc_devclass, 0, 0); 1092 1093 #if NCARD > 0 1094 1095 static device_method_t fdc_pccard_methods[] = { 1096 /* Device interface */ 1097 DEVMETHOD(device_probe, fdc_pccard_probe), 1098 DEVMETHOD(device_attach, fdc_attach), 1099 DEVMETHOD(device_detach, fdc_detach), 1100 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1101 DEVMETHOD(device_suspend, bus_generic_suspend), 1102 DEVMETHOD(device_resume, bus_generic_resume), 1103 1104 /* Bus interface */ 1105 DEVMETHOD(bus_print_child, fdc_print_child), 1106 DEVMETHOD(bus_read_ivar, fdc_read_ivar), 1107 /* Our children never use any other bus interface methods. */ 1108 1109 { 0, 0 } 1110 }; 1111 1112 static driver_t fdc_pccard_driver = { 1113 "fdc", 1114 fdc_pccard_methods, 1115 sizeof(struct fdc_data) 1116 }; 1117 1118 DRIVER_MODULE(fdc, pccard, fdc_pccard_driver, fdc_devclass, 0, 0); 1119 1120 #endif /* NCARD > 0 */ 1121 1122 /* 1123 * Create a clone device upon request by devfs. 1124 */ 1125 static void 1126 fd_clone(void *arg, char *name, int namelen, dev_t *dev) 1127 { 1128 struct fd_data *fd; 1129 int i, u; 1130 char *n; 1131 size_t l; 1132 1133 fd = (struct fd_data *)arg; 1134 if (*dev != NODEV) 1135 return; 1136 if (dev_stdclone(name, &n, "fd", &u) != 2) 1137 return; 1138 l = strlen(n); 1139 if (l == 1 && *n >= 'a' && *n <= 'h') { 1140 /* 1141 * Trailing letters a through h denote 1142 * pseudo-partitions. We don't support true 1143 * (UFS-style) partitions, so we just implement them 1144 * as symlinks if someone asks us nicely. 1145 */ 1146 *dev = make_dev_alias(fd->masterdev, name); 1147 return; 1148 } 1149 if (l >= 2 && l <= 5 && *n == '.') { 1150 /* 1151 * Trailing numbers, preceded by a dot, denote 1152 * subdevices for different densities. Historically, 1153 * they have been named by density (like fd0.1440), 1154 * but we allow arbitrary numbers between 1 and 4 1155 * digits, so fd0.1 through fd0.15 are possible as 1156 * well. 1157 */ 1158 for (i = 1; i < l; i++) 1159 if (n[i] < '0' || n[i] > '9') 1160 return; 1161 for (i = 0; i < NUMDENS - 1; i++) 1162 if (fd->clonedevs[i] == NODEV) { 1163 *dev = make_dev(&fd_cdevsw, 1164 FDNUMTOUNIT(u) + i + 1, 1165 UID_ROOT, GID_OPERATOR, 0640, 1166 name); 1167 fd->clonedevs[i] = *dev; 1168 return; 1169 } 1170 } 1171 } 1172 1173 /* 1174 * Configuration/initialization, per drive. 1175 */ 1176 static int 1177 fd_probe(device_t dev) 1178 { 1179 int i; 1180 u_int st0, st3; 1181 struct fd_data *fd; 1182 struct fdc_data *fdc; 1183 fdsu_t fdsu; 1184 int flags; 1185 1186 fdsu = *(int *)device_get_ivars(dev); /* xxx cheat a bit... */ 1187 fd = device_get_softc(dev); 1188 fdc = device_get_softc(device_get_parent(dev)); 1189 flags = device_get_flags(dev); 1190 1191 bzero(fd, sizeof *fd); 1192 fd->dev = dev; 1193 fd->fdc = fdc; 1194 fd->fdsu = fdsu; 1195 fd->fdu = device_get_unit(dev); 1196 fd->flags = FD_UA; /* make sure fdautoselect() will be called */ 1197 1198 fd->type = FD_DTYPE(flags); 1199 /* 1200 * XXX I think using __i386__ is wrong here since we actually want to probe 1201 * for the machine type, not the CPU type (so non-PC arch's like the PC98 will 1202 * fail the probe). However, for whatever reason, testing for _MACHINE_ARCH 1203 * == i386 breaks the test on FreeBSD/Alpha. 1204 */ 1205 #ifdef __i386__ 1206 if (fd->type == FDT_NONE && (fd->fdu == 0 || fd->fdu == 1)) { 1207 /* Look up what the BIOS thinks we have. */ 1208 if (fd->fdu == 0) { 1209 if ((fdc->flags & FDC_ISPCMCIA)) 1210 /* 1211 * Somewhat special. No need to force the 1212 * user to set device flags, since the Y-E 1213 * Data PCMCIA floppy is always a 1.44 MB 1214 * device. 1215 */ 1216 fd->type = FDT_144M; 1217 else 1218 fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4; 1219 } else { 1220 fd->type = rtcin(RTC_FDISKETTE) & 0x0f; 1221 } 1222 if (fd->type == FDT_288M_1) 1223 fd->type = FDT_288M; 1224 } 1225 #endif /* __i386__ */ 1226 /* is there a unit? */ 1227 if (fd->type == FDT_NONE) 1228 return (ENXIO); 1229 1230 /* select it */ 1231 set_motor(fdc, fdsu, TURNON); 1232 fdc_reset(fdc); /* XXX reset, then unreset, etc. */ 1233 DELAY(1000000); /* 1 sec */ 1234 1235 /* XXX This doesn't work before the first set_motor() */ 1236 if ((fdc->flags & FDC_HAS_FIFO) == 0 && 1237 fdc->fdct == FDC_ENHANCED && 1238 (device_get_flags(fdc->fdc_dev) & FDC_NO_FIFO) == 0 && 1239 enable_fifo(fdc) == 0) { 1240 device_printf(device_get_parent(dev), 1241 "FIFO enabled, %d bytes threshold\n", fifo_threshold); 1242 } 1243 1244 if ((flags & FD_NO_PROBE) == 0) { 1245 /* If we're at track 0 first seek inwards. */ 1246 if ((fd_sense_drive_status(fdc, &st3) == 0) && 1247 (st3 & NE7_ST3_T0)) { 1248 /* Seek some steps... */ 1249 if (fd_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) { 1250 /* ...wait a moment... */ 1251 DELAY(300000); 1252 /* make ctrlr happy: */ 1253 fd_sense_int(fdc, 0, 0); 1254 } 1255 } 1256 1257 for (i = 0; i < 2; i++) { 1258 /* 1259 * we must recalibrate twice, just in case the 1260 * heads have been beyond cylinder 76, since 1261 * most FDCs still barf when attempting to 1262 * recalibrate more than 77 steps 1263 */ 1264 /* go back to 0: */ 1265 if (fd_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) { 1266 /* a second being enough for full stroke seek*/ 1267 DELAY(i == 0 ? 1000000 : 300000); 1268 1269 /* anything responding? */ 1270 if (fd_sense_int(fdc, &st0, 0) == 0 && 1271 (st0 & NE7_ST0_EC) == 0) 1272 break; /* already probed succesfully */ 1273 } 1274 } 1275 } 1276 1277 set_motor(fdc, fdsu, TURNOFF); 1278 1279 if ((flags & FD_NO_PROBE) == 0 && 1280 (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */ 1281 return (ENXIO); 1282 1283 switch (fd->type) { 1284 case FDT_12M: 1285 device_set_desc(dev, "1200-KB 5.25\" drive"); 1286 fd->type = FDT_12M; 1287 break; 1288 case FDT_144M: 1289 device_set_desc(dev, "1440-KB 3.5\" drive"); 1290 fd->type = FDT_144M; 1291 break; 1292 case FDT_288M: 1293 device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)"); 1294 fd->type = FDT_288M; 1295 break; 1296 case FDT_360K: 1297 device_set_desc(dev, "360-KB 5.25\" drive"); 1298 fd->type = FDT_360K; 1299 break; 1300 case FDT_720K: 1301 device_set_desc(dev, "720-KB 3.5\" drive"); 1302 fd->type = FDT_720K; 1303 break; 1304 default: 1305 return (ENXIO); 1306 } 1307 fd->track = FD_NO_TRACK; 1308 fd->fdc = fdc; 1309 fd->fdsu = fdsu; 1310 fd->options = 0; 1311 callout_handle_init(&fd->toffhandle); 1312 callout_handle_init(&fd->tohandle); 1313 1314 /* initialize densities for subdevices */ 1315 for (i = 0; i < NUMDENS; i++) 1316 memcpy(fd->fts + i, fd_native_types + fd->type, 1317 sizeof(struct fd_type)); 1318 return (0); 1319 } 1320 1321 static int 1322 fd_attach(device_t dev) 1323 { 1324 struct fd_data *fd; 1325 static int cdevsw_add_done; 1326 int i; 1327 1328 if (!cdevsw_add_done) { 1329 cdevsw_add(&fd_cdevsw); /* XXX */ 1330 cdevsw_add_done = 1; 1331 } 1332 fd = device_get_softc(dev); 1333 fd->clonetag = EVENTHANDLER_REGISTER(dev_clone, fd_clone, fd, 1000); 1334 fd->masterdev = make_dev(&fd_cdevsw, fd->fdu << 6, 1335 UID_ROOT, GID_OPERATOR, 0640, "fd%d", fd->fdu); 1336 for (i = 0; i < NUMDENS - 1; i++) 1337 fd->clonedevs[i] = NODEV; 1338 devstat_add_entry(&fd->device_stats, device_get_name(dev), 1339 device_get_unit(dev), 0, DEVSTAT_NO_ORDERED_TAGS, 1340 DEVSTAT_TYPE_FLOPPY | DEVSTAT_TYPE_IF_OTHER, 1341 DEVSTAT_PRIORITY_FD); 1342 return (0); 1343 } 1344 1345 static int 1346 fd_detach(device_t dev) 1347 { 1348 struct fd_data *fd; 1349 int i; 1350 1351 fd = device_get_softc(dev); 1352 untimeout(fd_turnoff, fd, fd->toffhandle); 1353 devstat_remove_entry(&fd->device_stats); 1354 destroy_dev(fd->masterdev); 1355 for (i = 0; i < NUMDENS - 1; i++) 1356 if (fd->clonedevs[i] != NODEV) 1357 destroy_dev(fd->clonedevs[i]); 1358 cdevsw_remove(&fd_cdevsw); 1359 EVENTHANDLER_DEREGISTER(dev_clone, fd->clonetag); 1360 1361 return (0); 1362 } 1363 1364 static device_method_t fd_methods[] = { 1365 /* Device interface */ 1366 DEVMETHOD(device_probe, fd_probe), 1367 DEVMETHOD(device_attach, fd_attach), 1368 DEVMETHOD(device_detach, fd_detach), 1369 DEVMETHOD(device_shutdown, bus_generic_shutdown), 1370 DEVMETHOD(device_suspend, bus_generic_suspend), /* XXX */ 1371 DEVMETHOD(device_resume, bus_generic_resume), /* XXX */ 1372 1373 { 0, 0 } 1374 }; 1375 1376 static driver_t fd_driver = { 1377 "fd", 1378 fd_methods, 1379 sizeof(struct fd_data) 1380 }; 1381 1382 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, 0, 0); 1383 1384 /* 1385 * More auxiliary functions. 1386 */ 1387 /* 1388 * Motor control stuff. 1389 * Remember to not deselect the drive we're working on. 1390 */ 1391 static void 1392 set_motor(struct fdc_data *fdc, int fdsu, int turnon) 1393 { 1394 int fdout; 1395 1396 fdout = fdc->fdout; 1397 if (turnon) { 1398 fdout &= ~FDO_FDSEL; 1399 fdout |= (FDO_MOEN0 << fdsu) | FDO_FDMAEN | FDO_FRST | fdsu; 1400 } else 1401 fdout &= ~(FDO_MOEN0 << fdsu); 1402 fdc->fdout = fdout; 1403 fdout_wr(fdc, fdout); 1404 TRACE1("[0x%x->FDOUT]", fdout); 1405 } 1406 1407 static void 1408 fd_turnoff(void *xfd) 1409 { 1410 int s; 1411 fd_p fd = xfd; 1412 1413 TRACE1("[fd%d: turnoff]", fd->fdu); 1414 1415 s = splbio(); 1416 /* 1417 * Don't turn off the motor yet if the drive is active. 1418 * 1419 * If we got here, this could only mean we missed an interrupt. 1420 * This can e. g. happen on the Y-E Date PCMCIA floppy controller 1421 * after a controller reset. Just schedule a pseudo-interrupt 1422 * so the state machine gets re-entered. 1423 */ 1424 if (fd->fdc->state != DEVIDLE && fd->fdc->fdu == fd->fdu) { 1425 fdc_intr(fd->fdc); 1426 splx(s); 1427 return; 1428 } 1429 1430 fd->flags &= ~FD_MOTOR; 1431 set_motor(fd->fdc, fd->fdsu, TURNOFF); 1432 splx(s); 1433 } 1434 1435 static void 1436 fd_motor_on(void *xfd) 1437 { 1438 int s; 1439 fd_p fd = xfd; 1440 1441 s = splbio(); 1442 fd->flags &= ~FD_MOTOR_WAIT; 1443 if((fd->fdc->fd == fd) && (fd->fdc->state == MOTORWAIT)) 1444 { 1445 fdc_intr(fd->fdc); 1446 } 1447 splx(s); 1448 } 1449 1450 static void 1451 fd_turnon(fd_p fd) 1452 { 1453 if(!(fd->flags & FD_MOTOR)) 1454 { 1455 fd->flags |= (FD_MOTOR + FD_MOTOR_WAIT); 1456 set_motor(fd->fdc, fd->fdsu, TURNON); 1457 timeout(fd_motor_on, fd, hz); /* in 1 sec its ok */ 1458 } 1459 } 1460 1461 static void 1462 fdc_reset(fdc_p fdc) 1463 { 1464 /* Try a reset, keep motor on */ 1465 fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1466 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~(FDO_FRST|FDO_FDMAEN)); 1467 DELAY(100); 1468 /* enable FDC, but defer interrupts a moment */ 1469 fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN); 1470 TRACE1("[0x%x->FDOUT]", fdc->fdout & ~FDO_FDMAEN); 1471 DELAY(100); 1472 fdout_wr(fdc, fdc->fdout); 1473 TRACE1("[0x%x->FDOUT]", fdc->fdout); 1474 1475 /* XXX after a reset, silently believe the FDC will accept commands */ 1476 (void)fd_cmd(fdc, 3, NE7CMD_SPECIFY, 1477 NE7_SPEC_1(3, 240), NE7_SPEC_2(2, 0), 1478 0); 1479 if (fdc->flags & FDC_HAS_FIFO) 1480 (void) enable_fifo(fdc); 1481 } 1482 1483 /* 1484 * FDC IO functions, take care of the main status register, timeout 1485 * in case the desired status bits are never set. 1486 * 1487 * These PIO loops initially start out with short delays between 1488 * each iteration in the expectation that the required condition 1489 * is usually met quickly, so it can be handled immediately. After 1490 * about 1 ms, stepping is increased to achieve a better timing 1491 * accuracy in the calls to DELAY(). 1492 */ 1493 static int 1494 fd_in(struct fdc_data *fdc, int *ptr) 1495 { 1496 int i, j, step; 1497 1498 for (j = 0, step = 1; 1499 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != (NE7_DIO|NE7_RQM) && 1500 j < FDSTS_TIMEOUT; 1501 j += step) { 1502 if (i == NE7_RQM) 1503 return (fdc_err(fdc, "ready for output in input\n")); 1504 if (j == 1000) 1505 step = 1000; 1506 DELAY(step); 1507 } 1508 if (j >= FDSTS_TIMEOUT) 1509 return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0)); 1510 #ifdef FDC_DEBUG 1511 i = fddata_rd(fdc); 1512 TRACE1("[FDDATA->0x%x]", (unsigned char)i); 1513 *ptr = i; 1514 return (0); 1515 #else /* !FDC_DEBUG */ 1516 i = fddata_rd(fdc); 1517 if (ptr) 1518 *ptr = i; 1519 return (0); 1520 #endif /* FDC_DEBUG */ 1521 } 1522 1523 static int 1524 out_fdc(struct fdc_data *fdc, int x) 1525 { 1526 int i, j, step; 1527 1528 for (j = 0, step = 1; 1529 (i = fdsts_rd(fdc) & (NE7_DIO|NE7_RQM)) != NE7_RQM && 1530 j < FDSTS_TIMEOUT; 1531 j += step) { 1532 if (i == (NE7_DIO|NE7_RQM)) 1533 return (fdc_err(fdc, "ready for input in output\n")); 1534 if (j == 1000) 1535 step = 1000; 1536 DELAY(step); 1537 } 1538 if (j >= FDSTS_TIMEOUT) 1539 return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0)); 1540 1541 /* Send the command and return */ 1542 fddata_wr(fdc, x); 1543 TRACE1("[0x%x->FDDATA]", x); 1544 return (0); 1545 } 1546 1547 /* 1548 * Block device driver interface functions (interspersed with even more 1549 * auxiliary functions). 1550 */ 1551 static int 1552 Fdopen(dev_t dev, int flags, int mode, struct thread *td) 1553 { 1554 fdu_t fdu = FDUNIT(minor(dev)); 1555 int type = FDTYPE(minor(dev)); 1556 fd_p fd; 1557 fdc_p fdc; 1558 int rv, unitattn, dflags; 1559 1560 if ((fd = devclass_get_softc(fd_devclass, fdu)) == 0) 1561 return (ENXIO); 1562 fdc = fd->fdc; 1563 if ((fdc == NULL) || (fd->type == FDT_NONE)) 1564 return (ENXIO); 1565 if (type > NUMDENS) 1566 return (ENXIO); 1567 dflags = device_get_flags(fd->dev); 1568 /* 1569 * This is a bit bogus. It's still possible that e. g. a 1570 * descriptor gets inherited to a child, but then it's at 1571 * least for the same subdevice. By checking FD_OPEN here, we 1572 * can ensure that a device isn't attempted to be opened with 1573 * different densities at the same time where the second open 1574 * could clobber the settings from the first one. 1575 */ 1576 if (fd->flags & FD_OPEN) 1577 return (EBUSY); 1578 1579 if (type == 0) { 1580 if (flags & FNONBLOCK) { 1581 /* 1582 * Unfortunately, physio(9) discards its ioflag 1583 * argument, thus preventing us from seeing the 1584 * IO_NDELAY bit. So we need to keep track 1585 * ourselves. 1586 */ 1587 fd->flags |= FD_NONBLOCK; 1588 fd->ft = 0; 1589 } else { 1590 /* 1591 * Figure out a unit attention condition. 1592 * 1593 * If UA has been forced, proceed. 1594 * 1595 * If motor is off, turn it on for a moment 1596 * and select our drive, in order to read the 1597 * UA hardware signal. 1598 * 1599 * If motor is on, and our drive is currently 1600 * selected, just read the hardware bit. 1601 * 1602 * If motor is on, but active for another 1603 * drive on that controller, we are lost. We 1604 * cannot risk to deselect the other drive, so 1605 * we just assume a forced UA condition to be 1606 * on the safe side. 1607 */ 1608 unitattn = 0; 1609 if ((dflags & FD_NO_CHLINE) != 0 || 1610 (fd->flags & FD_UA) != 0) { 1611 unitattn = 1; 1612 fd->flags &= ~FD_UA; 1613 } else if (fdc->fdout & (FDO_MOEN0 | FDO_MOEN1 | 1614 FDO_MOEN2 | FDO_MOEN3)) { 1615 if ((fdc->fdout & FDO_FDSEL) == fd->fdsu) 1616 unitattn = fdin_rd(fdc) & FDI_DCHG; 1617 else 1618 unitattn = 1; 1619 } else { 1620 set_motor(fdc, fd->fdsu, TURNON); 1621 unitattn = fdin_rd(fdc) & FDI_DCHG; 1622 set_motor(fdc, fd->fdsu, TURNOFF); 1623 } 1624 if (unitattn && (rv = fdautoselect(dev)) != 0) 1625 return (rv); 1626 } 1627 } else { 1628 fd->ft = fd->fts + type; 1629 } 1630 fd->flags |= FD_OPEN; 1631 /* 1632 * Clearing the DMA overrun counter at open time is a bit messy. 1633 * Since we're only managing one counter per controller, opening 1634 * the second drive could mess it up. Anyway, if the DMA overrun 1635 * condition is really persistent, it will eventually time out 1636 * still. OTOH, clearing it here will ensure we'll at least start 1637 * trying again after a previous (maybe even long ago) failure. 1638 * Also, this is merely a stop-gap measure only that should not 1639 * happen during normal operation, so we can tolerate it to be a 1640 * bit sloppy about this. 1641 */ 1642 fdc->dma_overruns = 0; 1643 1644 return 0; 1645 } 1646 1647 static int 1648 fdclose(dev_t dev, int flags, int mode, struct thread *td) 1649 { 1650 fdu_t fdu = FDUNIT(minor(dev)); 1651 struct fd_data *fd; 1652 1653 fd = devclass_get_softc(fd_devclass, fdu); 1654 fd->flags &= ~(FD_OPEN | FD_NONBLOCK); 1655 fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR); 1656 1657 return (0); 1658 } 1659 1660 static void 1661 fdstrategy(struct bio *bp) 1662 { 1663 long blknum, nblocks; 1664 int s; 1665 fdu_t fdu; 1666 fdc_p fdc; 1667 fd_p fd; 1668 size_t fdblk; 1669 1670 fdu = FDUNIT(minor(bp->bio_dev)); 1671 fd = devclass_get_softc(fd_devclass, fdu); 1672 if (fd == 0) 1673 panic("fdstrategy: buf for nonexistent device (%#lx, %#lx)", 1674 (u_long)major(bp->bio_dev), (u_long)minor(bp->bio_dev)); 1675 fdc = fd->fdc; 1676 if (fd->type == FDT_NONE || fd->ft == 0) { 1677 bp->bio_error = ENXIO; 1678 bp->bio_flags |= BIO_ERROR; 1679 goto bad; 1680 } 1681 fdblk = 128 << (fd->ft->secsize); 1682 if (bp->bio_cmd != BIO_FORMAT && bp->bio_cmd != BIO_RDSECTID) { 1683 if (fd->flags & FD_NONBLOCK) { 1684 bp->bio_error = EAGAIN; 1685 bp->bio_flags |= BIO_ERROR; 1686 goto bad; 1687 } 1688 if (bp->bio_blkno < 0) { 1689 printf( 1690 "fd%d: fdstrat: bad request blkno = %lu, bcount = %ld\n", 1691 fdu, (u_long)bp->bio_blkno, bp->bio_bcount); 1692 bp->bio_error = EINVAL; 1693 bp->bio_flags |= BIO_ERROR; 1694 goto bad; 1695 } 1696 if ((bp->bio_bcount % fdblk) != 0) { 1697 bp->bio_error = EINVAL; 1698 bp->bio_flags |= BIO_ERROR; 1699 goto bad; 1700 } 1701 } 1702 1703 /* 1704 * Set up block calculations. 1705 */ 1706 if (bp->bio_blkno > 20000000) { 1707 /* 1708 * Reject unreasonably high block number, prevent the 1709 * multiplication below from overflowing. 1710 */ 1711 bp->bio_error = EINVAL; 1712 bp->bio_flags |= BIO_ERROR; 1713 goto bad; 1714 } 1715 blknum = bp->bio_blkno * DEV_BSIZE / fdblk; 1716 nblocks = fd->ft->size; 1717 if (blknum + bp->bio_bcount / fdblk > nblocks) { 1718 if (blknum >= nblocks) { 1719 if (bp->bio_cmd == BIO_READ) 1720 bp->bio_resid = bp->bio_bcount; 1721 else { 1722 bp->bio_error = ENOSPC; 1723 bp->bio_flags |= BIO_ERROR; 1724 } 1725 goto bad; /* not always bad, but EOF */ 1726 } 1727 bp->bio_bcount = (nblocks - blknum) * fdblk; 1728 } 1729 bp->bio_pblkno = blknum; 1730 s = splbio(); 1731 bioqdisksort(&fdc->head, bp); 1732 untimeout(fd_turnoff, fd, fd->toffhandle); /* a good idea */ 1733 devstat_start_transaction(&fd->device_stats); 1734 device_busy(fd->dev); 1735 fdstart(fdc); 1736 splx(s); 1737 return; 1738 1739 bad: 1740 biodone(bp); 1741 } 1742 1743 /* 1744 * fdstart 1745 * 1746 * We have just queued something. If the controller is not busy 1747 * then simulate the case where it has just finished a command 1748 * So that it (the interrupt routine) looks on the queue for more 1749 * work to do and picks up what we just added. 1750 * 1751 * If the controller is already busy, we need do nothing, as it 1752 * will pick up our work when the present work completes. 1753 */ 1754 static void 1755 fdstart(struct fdc_data *fdc) 1756 { 1757 int s; 1758 1759 s = splbio(); 1760 if(fdc->state == DEVIDLE) 1761 { 1762 fdc_intr(fdc); 1763 } 1764 splx(s); 1765 } 1766 1767 static void 1768 fd_iotimeout(void *xfdc) 1769 { 1770 fdc_p fdc; 1771 int s; 1772 1773 fdc = xfdc; 1774 TRACE1("fd%d[fd_iotimeout()]", fdc->fdu); 1775 1776 /* 1777 * Due to IBM's brain-dead design, the FDC has a faked ready 1778 * signal, hardwired to ready == true. Thus, any command 1779 * issued if there's no diskette in the drive will _never_ 1780 * complete, and must be aborted by resetting the FDC. 1781 * Many thanks, Big Blue! 1782 * The FDC must not be reset directly, since that would 1783 * interfere with the state machine. Instead, pretend that 1784 * the command completed but was invalid. The state machine 1785 * will reset the FDC and retry once. 1786 */ 1787 s = splbio(); 1788 fdc->status[0] = NE7_ST0_IC_IV; 1789 fdc->flags &= ~FDC_STAT_VALID; 1790 fdc->state = IOTIMEDOUT; 1791 fdc_intr(fdc); 1792 splx(s); 1793 } 1794 1795 /* Just ensure it has the right spl. */ 1796 static void 1797 fd_pseudointr(void *xfdc) 1798 { 1799 int s; 1800 1801 s = splbio(); 1802 fdc_intr(xfdc); 1803 splx(s); 1804 } 1805 1806 /* 1807 * fdc_intr 1808 * 1809 * Keep calling the state machine until it returns a 0. 1810 * Always called at splbio. 1811 */ 1812 static void 1813 fdc_intr(void *xfdc) 1814 { 1815 fdc_p fdc = xfdc; 1816 while(fdstate(fdc)) 1817 ; 1818 } 1819 1820 /* 1821 * Magic pseudo-DMA initialization for YE FDC. Sets count and 1822 * direction. 1823 */ 1824 #define SET_BCDR(fdc,wr,cnt,port) \ 1825 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port, \ 1826 ((cnt)-1) & 0xff); \ 1827 bus_space_write_1(fdc->portt, fdc->porth, fdc->port_off + port + 1, \ 1828 ((wr ? 0x80 : 0) | ((((cnt)-1) >> 8) & 0x7f))); 1829 1830 /* 1831 * fdcpio(): perform programmed IO read/write for YE PCMCIA floppy. 1832 */ 1833 static int 1834 fdcpio(fdc_p fdc, long flags, caddr_t addr, u_int count) 1835 { 1836 u_char *cptr = (u_char *)addr; 1837 1838 if (flags == BIO_READ) { 1839 if (fdc->state != PIOREAD) { 1840 fdc->state = PIOREAD; 1841 return(0); 1842 } 1843 SET_BCDR(fdc, 0, count, 0); 1844 bus_space_read_multi_1(fdc->portt, fdc->porth, fdc->port_off + 1845 FDC_YE_DATAPORT, cptr, count); 1846 } else { 1847 bus_space_write_multi_1(fdc->portt, fdc->porth, fdc->port_off + 1848 FDC_YE_DATAPORT, cptr, count); 1849 SET_BCDR(fdc, 0, count, 0); 1850 } 1851 return(1); 1852 } 1853 1854 /* 1855 * Try figuring out the density of the media present in our device. 1856 */ 1857 static int 1858 fdautoselect(dev_t dev) 1859 { 1860 fdu_t fdu; 1861 fd_p fd; 1862 struct fd_type *fdtp; 1863 struct fdc_readid id; 1864 int i, n, oopts, rv; 1865 1866 fdu = FDUNIT(minor(dev)); 1867 fd = devclass_get_softc(fd_devclass, fdu); 1868 1869 switch (fd->type) { 1870 default: 1871 return (ENXIO); 1872 1873 case FDT_360K: 1874 case FDT_720K: 1875 /* no autoselection on those drives */ 1876 fd->ft = fd_native_types + fd->type; 1877 return (0); 1878 1879 case FDT_12M: 1880 fdtp = fd_searchlist_12m; 1881 n = sizeof fd_searchlist_12m / sizeof(struct fd_type); 1882 break; 1883 1884 case FDT_144M: 1885 fdtp = fd_searchlist_144m; 1886 n = sizeof fd_searchlist_144m / sizeof(struct fd_type); 1887 break; 1888 1889 case FDT_288M: 1890 fdtp = fd_searchlist_288m; 1891 n = sizeof fd_searchlist_288m / sizeof(struct fd_type); 1892 break; 1893 } 1894 1895 /* 1896 * Try reading sector ID fields, first at cylinder 0, head 0, 1897 * then at cylinder 2, head N. We don't probe cylinder 1, 1898 * since for 5.25in DD media in a HD drive, there are no data 1899 * to read (2 step pulses per media cylinder required). For 1900 * two-sided media, the second probe always goes to head 1, so 1901 * we can tell them apart from single-sided media. As a 1902 * side-effect this means that single-sided media should be 1903 * mentioned in the search list after two-sided media of an 1904 * otherwise identical density. Media with a different number 1905 * of sectors per track but otherwise identical parameters 1906 * cannot be distinguished at all. 1907 * 1908 * If we successfully read an ID field on both cylinders where 1909 * the recorded values match our expectation, we are done. 1910 * Otherwise, we try the next density entry from the table. 1911 * 1912 * Stepping to cylinder 2 has the side-effect of clearing the 1913 * unit attention bit. 1914 */ 1915 oopts = fd->options; 1916 fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY; 1917 for (i = 0; i < n; i++, fdtp++) { 1918 fd->ft = fdtp; 1919 1920 id.cyl = id.head = 0; 1921 rv = fdmisccmd(dev, BIO_RDSECTID, &id); 1922 if (rv != 0) 1923 continue; 1924 if (id.cyl != 0 || id.head != 0 || 1925 id.secshift != fdtp->secsize) 1926 continue; 1927 id.cyl = 2; 1928 id.head = fd->ft->heads - 1; 1929 rv = fdmisccmd(dev, BIO_RDSECTID, &id); 1930 if (id.cyl != 2 || id.head != fdtp->heads - 1 || 1931 id.secshift != fdtp->secsize) 1932 continue; 1933 if (rv == 0) 1934 break; 1935 } 1936 1937 fd->options = oopts; 1938 if (i == n) { 1939 if (bootverbose) 1940 device_printf(fd->dev, "autoselection failed\n"); 1941 fd->ft = 0; 1942 return (EIO); 1943 } else { 1944 if (bootverbose) 1945 device_printf(fd->dev, "autoselected %d KB medium\n", 1946 fd->ft->size / 2); 1947 return (0); 1948 } 1949 } 1950 1951 1952 /* 1953 * The controller state machine. 1954 * 1955 * If it returns a non zero value, it should be called again immediately. 1956 */ 1957 static int 1958 fdstate(fdc_p fdc) 1959 { 1960 struct fdc_readid *idp; 1961 int read, format, rdsectid, cylinder, head, i, sec = 0, sectrac; 1962 int st0, cyl, st3, idf, ne7cmd, mfm, steptrac; 1963 unsigned long blknum; 1964 fdu_t fdu = fdc->fdu; 1965 fd_p fd; 1966 register struct bio *bp; 1967 struct fd_formb *finfo = NULL; 1968 size_t fdblk; 1969 1970 bp = fdc->bp; 1971 if (bp == NULL) { 1972 bp = bioq_first(&fdc->head); 1973 if (bp != NULL) { 1974 bioq_remove(&fdc->head, bp); 1975 fdc->bp = bp; 1976 } 1977 } 1978 if (bp == NULL) { 1979 /* 1980 * Nothing left for this controller to do, 1981 * force into the IDLE state. 1982 */ 1983 fdc->state = DEVIDLE; 1984 if (fdc->fd) { 1985 device_printf(fdc->fdc_dev, 1986 "unexpected valid fd pointer\n"); 1987 fdc->fd = (fd_p) 0; 1988 fdc->fdu = -1; 1989 } 1990 TRACE1("[fdc%d IDLE]", fdc->fdcu); 1991 return (0); 1992 } 1993 fdu = FDUNIT(minor(bp->bio_dev)); 1994 fd = devclass_get_softc(fd_devclass, fdu); 1995 fdblk = 128 << fd->ft->secsize; 1996 if (fdc->fd && (fd != fdc->fd)) 1997 device_printf(fd->dev, "confused fd pointers\n"); 1998 read = bp->bio_cmd == BIO_READ; 1999 mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0; 2000 steptrac = (fd->ft->flags & FL_2STEP)? 2: 1; 2001 if (read) 2002 idf = ISADMA_READ; 2003 else 2004 idf = ISADMA_WRITE; 2005 format = bp->bio_cmd == BIO_FORMAT; 2006 rdsectid = bp->bio_cmd == BIO_RDSECTID; 2007 if (format) 2008 finfo = (struct fd_formb *)bp->bio_data; 2009 TRACE1("fd%d", fdu); 2010 TRACE1("[%s]", fdstates[fdc->state]); 2011 TRACE1("(0x%x)", fd->flags); 2012 untimeout(fd_turnoff, fd, fd->toffhandle); 2013 fd->toffhandle = timeout(fd_turnoff, fd, 4 * hz); 2014 switch (fdc->state) 2015 { 2016 case DEVIDLE: 2017 case FINDWORK: /* we have found new work */ 2018 fdc->retry = 0; 2019 fd->skip = 0; 2020 fdc->fd = fd; 2021 fdc->fdu = fdu; 2022 fdc->fdctl_wr(fdc, fd->ft->trans); 2023 TRACE1("[0x%x->FDCTL]", fd->ft->trans); 2024 /* 2025 * If the next drive has a motor startup pending, then 2026 * it will start up in its own good time. 2027 */ 2028 if(fd->flags & FD_MOTOR_WAIT) { 2029 fdc->state = MOTORWAIT; 2030 return (0); /* will return later */ 2031 } 2032 /* 2033 * Maybe if it's not starting, it SHOULD be starting. 2034 */ 2035 if (!(fd->flags & FD_MOTOR)) 2036 { 2037 fdc->state = MOTORWAIT; 2038 fd_turnon(fd); 2039 return (0); /* will return later */ 2040 } 2041 else /* at least make sure we are selected */ 2042 { 2043 set_motor(fdc, fd->fdsu, TURNON); 2044 } 2045 if (fdc->flags & FDC_NEEDS_RESET) { 2046 fdc->state = RESETCTLR; 2047 fdc->flags &= ~FDC_NEEDS_RESET; 2048 } else 2049 fdc->state = DOSEEK; 2050 return (1); /* will return immediately */ 2051 2052 case DOSEEK: 2053 blknum = bp->bio_pblkno + fd->skip / fdblk; 2054 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 2055 if (cylinder == fd->track) 2056 { 2057 fdc->state = SEEKCOMPLETE; 2058 return (1); /* will return immediately */ 2059 } 2060 if (fd_cmd(fdc, 3, NE7CMD_SEEK, 2061 fd->fdsu, cylinder * steptrac, 0)) 2062 { 2063 /* 2064 * Seek command not accepted, looks like 2065 * the FDC went off to the Saints... 2066 */ 2067 fdc->retry = 6; /* try a reset */ 2068 return(retrier(fdc)); 2069 } 2070 fd->track = FD_NO_TRACK; 2071 fdc->state = SEEKWAIT; 2072 return(0); /* will return later */ 2073 2074 case SEEKWAIT: 2075 /* allow heads to settle */ 2076 timeout(fd_pseudointr, fdc, hz / 16); 2077 fdc->state = SEEKCOMPLETE; 2078 return(0); /* will return later */ 2079 2080 case SEEKCOMPLETE : /* seek done, start DMA */ 2081 blknum = bp->bio_pblkno + fd->skip / fdblk; 2082 cylinder = blknum / (fd->ft->sectrac * fd->ft->heads); 2083 2084 /* Make sure seek really happened. */ 2085 if(fd->track == FD_NO_TRACK) { 2086 int descyl = cylinder * steptrac; 2087 do { 2088 /* 2089 * This might be a "ready changed" interrupt, 2090 * which cannot really happen since the 2091 * RDY pin is hardwired to + 5 volts. This 2092 * generally indicates a "bouncing" intr 2093 * line, so do one of the following: 2094 * 2095 * When running on an enhanced FDC that is 2096 * known to not go stuck after responding 2097 * with INVALID, fetch all interrupt states 2098 * until seeing either an INVALID or a 2099 * real interrupt condition. 2100 * 2101 * When running on a dumb old NE765, give 2102 * up immediately. The controller will 2103 * provide up to four dummy RC interrupt 2104 * conditions right after reset (for the 2105 * corresponding four drives), so this is 2106 * our only chance to get notice that it 2107 * was not the FDC that caused the interrupt. 2108 */ 2109 if (fd_sense_int(fdc, &st0, &cyl) 2110 == FD_NOT_VALID) 2111 return (0); /* will return later */ 2112 if(fdc->fdct == FDC_NE765 2113 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2114 return (0); /* hope for a real intr */ 2115 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 2116 2117 if (0 == descyl) { 2118 int failed = 0; 2119 /* 2120 * seek to cyl 0 requested; make sure we are 2121 * really there 2122 */ 2123 if (fd_sense_drive_status(fdc, &st3)) 2124 failed = 1; 2125 if ((st3 & NE7_ST3_T0) == 0) { 2126 printf( 2127 "fd%d: Seek to cyl 0, but not really there (ST3 = %b)\n", 2128 fdu, st3, NE7_ST3BITS); 2129 failed = 1; 2130 } 2131 2132 if (failed) { 2133 if(fdc->retry < 3) 2134 fdc->retry = 3; 2135 return (retrier(fdc)); 2136 } 2137 } 2138 2139 if (cyl != descyl) { 2140 printf( 2141 "fd%d: Seek to cyl %d failed; am at cyl %d (ST0 = 0x%x)\n", 2142 fdu, descyl, cyl, st0); 2143 if (fdc->retry < 3) 2144 fdc->retry = 3; 2145 return (retrier(fdc)); 2146 } 2147 } 2148 2149 fd->track = cylinder; 2150 if (format) 2151 fd->skip = (char *)&(finfo->fd_formb_cylno(0)) 2152 - (char *)finfo; 2153 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 2154 isa_dmastart(idf, bp->bio_data+fd->skip, 2155 format ? bp->bio_bcount : fdblk, fdc->dmachan); 2156 blknum = bp->bio_pblkno + fd->skip / fdblk; 2157 sectrac = fd->ft->sectrac; 2158 sec = blknum % (sectrac * fd->ft->heads); 2159 head = sec / sectrac; 2160 sec = sec % sectrac + 1; 2161 if (head != 0 && fd->ft->offset_side2 != 0) 2162 sec += fd->ft->offset_side2; 2163 fd->hddrv = ((head&1)<<2)+fdu; 2164 2165 if(format || !(read || rdsectid)) 2166 { 2167 /* make sure the drive is writable */ 2168 if(fd_sense_drive_status(fdc, &st3) != 0) 2169 { 2170 /* stuck controller? */ 2171 if (!(fdc->flags & FDC_NODMA)) 2172 isa_dmadone(idf, 2173 bp->bio_data + fd->skip, 2174 format ? bp->bio_bcount : fdblk, 2175 fdc->dmachan); 2176 fdc->retry = 6; /* reset the beast */ 2177 return (retrier(fdc)); 2178 } 2179 if(st3 & NE7_ST3_WP) 2180 { 2181 /* 2182 * XXX YES! this is ugly. 2183 * in order to force the current operation 2184 * to fail, we will have to fake an FDC 2185 * error - all error handling is done 2186 * by the retrier() 2187 */ 2188 fdc->status[0] = NE7_ST0_IC_AT; 2189 fdc->status[1] = NE7_ST1_NW; 2190 fdc->status[2] = 0; 2191 fdc->status[3] = fd->track; 2192 fdc->status[4] = head; 2193 fdc->status[5] = sec; 2194 fdc->retry = 8; /* break out immediately */ 2195 fdc->state = IOTIMEDOUT; /* not really... */ 2196 return (1); /* will return immediately */ 2197 } 2198 } 2199 2200 if (format) { 2201 ne7cmd = NE7CMD_FORMAT | mfm; 2202 if (fdc->flags & FDC_NODMA) { 2203 /* 2204 * This seems to be necessary for 2205 * whatever obscure reason; if we omit 2206 * it, we end up filling the sector ID 2207 * fields of the newly formatted track 2208 * entirely with garbage, causing 2209 * `wrong cylinder' errors all over 2210 * the place when trying to read them 2211 * back. 2212 * 2213 * Umpf. 2214 */ 2215 SET_BCDR(fdc, 1, bp->bio_bcount, 0); 2216 2217 (void)fdcpio(fdc,bp->bio_cmd, 2218 bp->bio_data+fd->skip, 2219 bp->bio_bcount); 2220 2221 } 2222 /* formatting */ 2223 if(fd_cmd(fdc, 6, ne7cmd, head << 2 | fdu, 2224 finfo->fd_formb_secshift, 2225 finfo->fd_formb_nsecs, 2226 finfo->fd_formb_gaplen, 2227 finfo->fd_formb_fillbyte, 0)) { 2228 /* controller fell over */ 2229 if (!(fdc->flags & FDC_NODMA)) 2230 isa_dmadone(idf, 2231 bp->bio_data + fd->skip, 2232 format ? bp->bio_bcount : fdblk, 2233 fdc->dmachan); 2234 fdc->retry = 6; 2235 return (retrier(fdc)); 2236 } 2237 } else if (rdsectid) { 2238 ne7cmd = NE7CMD_READID | mfm; 2239 if (fd_cmd(fdc, 2, ne7cmd, head << 2 | fdu, 0)) { 2240 /* controller jamming */ 2241 fdc->retry = 6; 2242 return (retrier(fdc)); 2243 } 2244 } else { 2245 /* read or write operation */ 2246 ne7cmd = (read ? NE7CMD_READ | NE7CMD_SK : NE7CMD_WRITE) | mfm; 2247 if (fdc->flags & FDC_NODMA) { 2248 /* 2249 * This seems to be necessary even when 2250 * reading data. 2251 */ 2252 SET_BCDR(fdc, 1, fdblk, 0); 2253 2254 /* 2255 * Perform the write pseudo-DMA before 2256 * the WRITE command is sent. 2257 */ 2258 if (!read) 2259 (void)fdcpio(fdc,bp->bio_cmd, 2260 bp->bio_data+fd->skip, 2261 fdblk); 2262 } 2263 if (fd_cmd(fdc, 9, 2264 ne7cmd, 2265 head << 2 | fdu, /* head & unit */ 2266 fd->track, /* track */ 2267 head, 2268 sec, /* sector + 1 */ 2269 fd->ft->secsize, /* sector size */ 2270 sectrac, /* sectors/track */ 2271 fd->ft->gap, /* gap size */ 2272 fd->ft->datalen, /* data length */ 2273 0)) { 2274 /* the beast is sleeping again */ 2275 if (!(fdc->flags & FDC_NODMA)) 2276 isa_dmadone(idf, 2277 bp->bio_data + fd->skip, 2278 format ? bp->bio_bcount : fdblk, 2279 fdc->dmachan); 2280 fdc->retry = 6; 2281 return (retrier(fdc)); 2282 } 2283 } 2284 if (!rdsectid && (fdc->flags & FDC_NODMA)) 2285 /* 2286 * If this is a read, then simply await interrupt 2287 * before performing PIO. 2288 */ 2289 if (read && !fdcpio(fdc,bp->bio_cmd, 2290 bp->bio_data+fd->skip,fdblk)) { 2291 fd->tohandle = timeout(fd_iotimeout, fdc, hz); 2292 return(0); /* will return later */ 2293 } 2294 2295 /* 2296 * Write (or format) operation will fall through and 2297 * await completion interrupt. 2298 */ 2299 fdc->state = IOCOMPLETE; 2300 fd->tohandle = timeout(fd_iotimeout, fdc, hz); 2301 return (0); /* will return later */ 2302 2303 case PIOREAD: 2304 /* 2305 * Actually perform the PIO read. The IOCOMPLETE case 2306 * removes the timeout for us. 2307 */ 2308 (void)fdcpio(fdc,bp->bio_cmd,bp->bio_data+fd->skip,fdblk); 2309 fdc->state = IOCOMPLETE; 2310 /* FALLTHROUGH */ 2311 case IOCOMPLETE: /* IO done, post-analyze */ 2312 untimeout(fd_iotimeout, fdc, fd->tohandle); 2313 2314 if (fd_read_status(fdc)) { 2315 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 2316 isa_dmadone(idf, bp->bio_data + fd->skip, 2317 format ? bp->bio_bcount : fdblk, 2318 fdc->dmachan); 2319 if (fdc->retry < 6) 2320 fdc->retry = 6; /* force a reset */ 2321 return (retrier(fdc)); 2322 } 2323 2324 fdc->state = IOTIMEDOUT; 2325 2326 /* FALLTHROUGH */ 2327 case IOTIMEDOUT: 2328 if (!rdsectid && !(fdc->flags & FDC_NODMA)) 2329 isa_dmadone(idf, bp->bio_data + fd->skip, 2330 format ? bp->bio_bcount : fdblk, fdc->dmachan); 2331 if (fdc->status[0] & NE7_ST0_IC) { 2332 if ((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 2333 && fdc->status[1] & NE7_ST1_OR) { 2334 /* 2335 * DMA overrun. Someone hogged the bus and 2336 * didn't release it in time for the next 2337 * FDC transfer. 2338 * 2339 * We normally restart this without bumping 2340 * the retry counter. However, in case 2341 * something is seriously messed up (like 2342 * broken hardware), we rather limit the 2343 * number of retries so the IO operation 2344 * doesn't block indefinately. 2345 */ 2346 if (fdc->dma_overruns++ < FDC_DMAOV_MAX) { 2347 fdc->state = SEEKCOMPLETE; 2348 return (1);/* will return immediately */ 2349 } /* else fall through */ 2350 } 2351 if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_IV 2352 && fdc->retry < 6) 2353 fdc->retry = 6; /* force a reset */ 2354 else if((fdc->status[0] & NE7_ST0_IC) == NE7_ST0_IC_AT 2355 && fdc->status[2] & NE7_ST2_WC 2356 && fdc->retry < 3) 2357 fdc->retry = 3; /* force recalibrate */ 2358 return (retrier(fdc)); 2359 } 2360 /* All OK */ 2361 if (rdsectid) { 2362 /* copy out ID field contents */ 2363 idp = (struct fdc_readid *)bp->bio_data; 2364 idp->cyl = fdc->status[3]; 2365 idp->head = fdc->status[4]; 2366 idp->sec = fdc->status[5]; 2367 idp->secshift = fdc->status[6]; 2368 } 2369 /* Operation successful, retry DMA overruns again next time. */ 2370 fdc->dma_overruns = 0; 2371 fd->skip += fdblk; 2372 if (!rdsectid && !format && fd->skip < bp->bio_bcount) { 2373 /* set up next transfer */ 2374 fdc->state = DOSEEK; 2375 } else { 2376 /* ALL DONE */ 2377 fd->skip = 0; 2378 bp->bio_resid = 0; 2379 fdc->bp = NULL; 2380 device_unbusy(fd->dev); 2381 biofinish(bp, &fd->device_stats, 0); 2382 fdc->fd = (fd_p) 0; 2383 fdc->fdu = -1; 2384 fdc->state = FINDWORK; 2385 } 2386 return (1); /* will return immediately */ 2387 2388 case RESETCTLR: 2389 fdc_reset(fdc); 2390 fdc->retry++; 2391 fdc->state = RESETCOMPLETE; 2392 return (0); /* will return later */ 2393 2394 case RESETCOMPLETE: 2395 /* 2396 * Discard all the results from the reset so that they 2397 * can't cause an unexpected interrupt later. 2398 */ 2399 for (i = 0; i < 4; i++) 2400 (void)fd_sense_int(fdc, &st0, &cyl); 2401 fdc->state = STARTRECAL; 2402 /* FALLTHROUGH */ 2403 case STARTRECAL: 2404 if(fd_cmd(fdc, 2, NE7CMD_RECAL, fdu, 0)) { 2405 /* arrgl */ 2406 fdc->retry = 6; 2407 return (retrier(fdc)); 2408 } 2409 fdc->state = RECALWAIT; 2410 return (0); /* will return later */ 2411 2412 case RECALWAIT: 2413 /* allow heads to settle */ 2414 timeout(fd_pseudointr, fdc, hz / 8); 2415 fdc->state = RECALCOMPLETE; 2416 return (0); /* will return later */ 2417 2418 case RECALCOMPLETE: 2419 do { 2420 /* 2421 * See SEEKCOMPLETE for a comment on this: 2422 */ 2423 if (fd_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID) 2424 return (0); /* will return later */ 2425 if(fdc->fdct == FDC_NE765 2426 && (st0 & NE7_ST0_IC) == NE7_ST0_IC_RC) 2427 return (0); /* hope for a real intr */ 2428 } while ((st0 & NE7_ST0_IC) == NE7_ST0_IC_RC); 2429 if ((st0 & NE7_ST0_IC) != NE7_ST0_IC_NT || cyl != 0) 2430 { 2431 if(fdc->retry > 3) 2432 /* 2433 * A recalibrate from beyond cylinder 77 2434 * will "fail" due to the FDC limitations; 2435 * since people used to complain much about 2436 * the failure message, try not logging 2437 * this one if it seems to be the first 2438 * time in a line. 2439 */ 2440 printf("fd%d: recal failed ST0 %b cyl %d\n", 2441 fdu, st0, NE7_ST0BITS, cyl); 2442 if(fdc->retry < 3) fdc->retry = 3; 2443 return (retrier(fdc)); 2444 } 2445 fd->track = 0; 2446 /* Seek (probably) necessary */ 2447 fdc->state = DOSEEK; 2448 return (1); /* will return immediately */ 2449 2450 case MOTORWAIT: 2451 if(fd->flags & FD_MOTOR_WAIT) 2452 { 2453 return (0); /* time's not up yet */ 2454 } 2455 if (fdc->flags & FDC_NEEDS_RESET) { 2456 fdc->state = RESETCTLR; 2457 fdc->flags &= ~FDC_NEEDS_RESET; 2458 } else 2459 fdc->state = DOSEEK; 2460 return (1); /* will return immediately */ 2461 2462 default: 2463 device_printf(fdc->fdc_dev, "unexpected FD int->"); 2464 if (fd_read_status(fdc) == 0) 2465 printf("FDC status :%x %x %x %x %x %x %x ", 2466 fdc->status[0], 2467 fdc->status[1], 2468 fdc->status[2], 2469 fdc->status[3], 2470 fdc->status[4], 2471 fdc->status[5], 2472 fdc->status[6] ); 2473 else 2474 printf("No status available "); 2475 if (fd_sense_int(fdc, &st0, &cyl) != 0) 2476 { 2477 printf("[controller is dead now]\n"); 2478 return (0); /* will return later */ 2479 } 2480 printf("ST0 = %x, PCN = %x\n", st0, cyl); 2481 return (0); /* will return later */ 2482 } 2483 /* noone should ever get here */ 2484 } 2485 2486 static int 2487 retrier(struct fdc_data *fdc) 2488 { 2489 struct bio *bp; 2490 struct fd_data *fd; 2491 int fdu; 2492 2493 bp = fdc->bp; 2494 2495 /* XXX shouldn't this be cached somewhere? */ 2496 fdu = FDUNIT(minor(bp->bio_dev)); 2497 fd = devclass_get_softc(fd_devclass, fdu); 2498 if (fd->options & FDOPT_NORETRY) 2499 goto fail; 2500 2501 switch (fdc->retry) { 2502 case 0: case 1: case 2: 2503 fdc->state = SEEKCOMPLETE; 2504 break; 2505 case 3: case 4: case 5: 2506 fdc->state = STARTRECAL; 2507 break; 2508 case 6: 2509 fdc->state = RESETCTLR; 2510 break; 2511 case 7: 2512 break; 2513 default: 2514 fail: 2515 if ((fd->options & FDOPT_NOERRLOG) == 0) { 2516 disk_err(bp, "hard error", 2517 fdc->fd->skip / DEV_BSIZE, 0); 2518 if (fdc->flags & FDC_STAT_VALID) { 2519 printf( 2520 " (ST0 %b ST1 %b ST2 %b cyl %u hd %u sec %u)\n", 2521 fdc->status[0], NE7_ST0BITS, 2522 fdc->status[1], NE7_ST1BITS, 2523 fdc->status[2], NE7_ST2BITS, 2524 fdc->status[3], fdc->status[4], 2525 fdc->status[5]); 2526 } 2527 else 2528 printf(" (No status)\n"); 2529 } 2530 if ((fd->options & FDOPT_NOERROR) == 0) { 2531 bp->bio_flags |= BIO_ERROR; 2532 bp->bio_error = EIO; 2533 bp->bio_resid = bp->bio_bcount - fdc->fd->skip; 2534 } else 2535 bp->bio_resid = 0; 2536 fdc->bp = NULL; 2537 fdc->fd->skip = 0; 2538 device_unbusy(fd->dev); 2539 biofinish(bp, &fdc->fd->device_stats, 0); 2540 fdc->state = FINDWORK; 2541 fdc->flags |= FDC_NEEDS_RESET; 2542 fdc->fd = (fd_p) 0; 2543 fdc->fdu = -1; 2544 return (1); 2545 } 2546 fdc->retry++; 2547 return (1); 2548 } 2549 2550 static void 2551 fdbiodone(struct bio *bp) 2552 { 2553 wakeup(bp); 2554 } 2555 2556 static int 2557 fdmisccmd(dev_t dev, u_int cmd, void *data) 2558 { 2559 fdu_t fdu; 2560 fd_p fd; 2561 struct bio *bp; 2562 struct fd_formb *finfo; 2563 struct fdc_readid *idfield; 2564 size_t fdblk; 2565 int error; 2566 2567 fdu = FDUNIT(minor(dev)); 2568 fd = devclass_get_softc(fd_devclass, fdu); 2569 fdblk = 128 << fd->ft->secsize; 2570 finfo = (struct fd_formb *)data; 2571 idfield = (struct fdc_readid *)data; 2572 2573 bp = malloc(sizeof(struct bio), M_TEMP, M_ZERO); 2574 2575 /* 2576 * Set up a bio request for fdstrategy(). bio_blkno is faked 2577 * so that fdstrategy() will seek to the the requested 2578 * cylinder, and use the desired head. 2579 */ 2580 bp->bio_cmd = cmd; 2581 if (cmd == BIO_FORMAT) { 2582 bp->bio_blkno = 2583 (finfo->cyl * (fd->ft->sectrac * fd->ft->heads) + 2584 finfo->head * fd->ft->sectrac) * 2585 fdblk / DEV_BSIZE; 2586 bp->bio_bcount = sizeof(struct fd_idfield_data) * 2587 finfo->fd_formb_nsecs; 2588 } else if (cmd == BIO_RDSECTID) { 2589 bp->bio_blkno = 2590 (idfield->cyl * (fd->ft->sectrac * fd->ft->heads) + 2591 idfield->head * fd->ft->sectrac) * 2592 fdblk / DEV_BSIZE; 2593 bp->bio_bcount = sizeof(struct fdc_readid); 2594 } else 2595 panic("wrong cmd in fdmisccmd()"); 2596 bp->bio_data = data; 2597 bp->bio_dev = dev; 2598 bp->bio_done = fdbiodone; 2599 bp->bio_flags = 0; 2600 2601 /* Now run the command. */ 2602 fdstrategy(bp); 2603 error = biowait(bp, "fdcmd"); 2604 2605 free(bp, M_TEMP); 2606 return (error); 2607 } 2608 2609 static int 2610 fdioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 2611 { 2612 fdu_t fdu; 2613 fd_p fd; 2614 struct fd_type *fdt; 2615 struct disklabel *lp; 2616 struct fdc_status *fsp; 2617 struct fdc_readid *rid; 2618 size_t fdblk; 2619 int error, type; 2620 2621 fdu = FDUNIT(minor(dev)); 2622 type = FDTYPE(minor(dev)); 2623 fd = devclass_get_softc(fd_devclass, fdu); 2624 2625 /* 2626 * First, handle everything that could be done with 2627 * FD_NONBLOCK still being set. 2628 */ 2629 switch (cmd) { 2630 case FIONBIO: 2631 if (*(int *)addr != 0) 2632 fd->flags |= FD_NONBLOCK; 2633 else { 2634 if (fd->ft == 0) { 2635 /* 2636 * No drive type has been selected yet, 2637 * cannot turn FNONBLOCK off. 2638 */ 2639 return (EINVAL); 2640 } 2641 fd->flags &= ~FD_NONBLOCK; 2642 } 2643 return (0); 2644 2645 case FIOASYNC: 2646 /* keep the generic fcntl() code happy */ 2647 return (0); 2648 2649 case FD_GTYPE: /* get drive type */ 2650 if (fd->ft == 0) 2651 /* no type known yet, return the native type */ 2652 *(struct fd_type *)addr = fd_native_types[fd->type]; 2653 else 2654 *(struct fd_type *)addr = *fd->ft; 2655 return (0); 2656 2657 case FD_STYPE: /* set drive type */ 2658 if (type == 0) { 2659 /* 2660 * Allow setting drive type temporarily iff 2661 * currently unset. Used for fdformat so any 2662 * user can set it, and then start formatting. 2663 */ 2664 if (fd->ft) 2665 return (EINVAL); /* already set */ 2666 fd->ft = fd->fts; 2667 *fd->ft = *(struct fd_type *)addr; 2668 fd->flags |= FD_UA; 2669 } else { 2670 /* 2671 * Set density definition permanently. Only 2672 * allow for superuser. 2673 */ 2674 if (suser(td) != 0) 2675 return (EPERM); 2676 fd->fts[type] = *(struct fd_type *)addr; 2677 } 2678 return (0); 2679 2680 case FD_GOPTS: /* get drive options */ 2681 *(int *)addr = fd->options + (type == 0? FDOPT_AUTOSEL: 0); 2682 return (0); 2683 2684 case FD_SOPTS: /* set drive options */ 2685 fd->options = *(int *)addr & ~FDOPT_AUTOSEL; 2686 return (0); 2687 2688 #ifdef FDC_DEBUG 2689 case FD_DEBUG: 2690 if ((fd_debug != 0) != (*(int *)addr != 0)) { 2691 fd_debug = (*(int *)addr != 0); 2692 printf("fd%d: debugging turned %s\n", 2693 fd->fdu, fd_debug ? "on" : "off"); 2694 } 2695 return (0); 2696 #endif 2697 2698 case FD_CLRERR: 2699 if (suser(td) != 0) 2700 return (EPERM); 2701 fd->fdc->fdc_errs = 0; 2702 return (0); 2703 2704 case FD_GSTAT: 2705 fsp = (struct fdc_status *)addr; 2706 if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2707 return (EINVAL); 2708 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 2709 return (0); 2710 2711 case FD_GDTYPE: 2712 *(enum fd_drivetype *)addr = fd->type; 2713 return (0); 2714 } 2715 2716 /* 2717 * Now handle everything else. Make sure we have a valid 2718 * drive type. 2719 */ 2720 if (fd->flags & FD_NONBLOCK) 2721 return (EAGAIN); 2722 if (fd->ft == 0) 2723 return (ENXIO); 2724 fdblk = 128 << fd->ft->secsize; 2725 error = 0; 2726 2727 switch (cmd) { 2728 case DIOCGDINFO: 2729 lp = malloc(sizeof(*lp), M_TEMP, M_ZERO); 2730 lp->d_secsize = fdblk; 2731 fdt = fd->ft; 2732 lp->d_secpercyl = fdt->size / fdt->tracks; 2733 lp->d_type = DTYPE_FLOPPY; 2734 if (readdisklabel(dev, lp) != NULL) 2735 error = EINVAL; 2736 else 2737 *(struct disklabel *)addr = *lp; 2738 free(lp, M_TEMP); 2739 break; 2740 2741 case DIOCSDINFO: 2742 if ((flag & FWRITE) == 0) 2743 return (EBADF); 2744 /* 2745 * XXX perhaps should call setdisklabel() to do error checking 2746 * although there is nowhere to "set" the result. Perhaps 2747 * should always just fail. 2748 */ 2749 break; 2750 2751 case DIOCWLABEL: 2752 if ((flag & FWRITE) == 0) 2753 return (EBADF); 2754 break; 2755 2756 case DIOCWDINFO: 2757 if ((flag & FWRITE) == 0) 2758 return (EBADF); 2759 lp = malloc(DEV_BSIZE, M_TEMP, M_ZERO); 2760 error = setdisklabel(lp, (struct disklabel *)addr, (u_long)0); 2761 if (error != 0) 2762 error = writedisklabel(dev, lp); 2763 free(lp, M_TEMP); 2764 break; 2765 2766 case FD_FORM: 2767 if ((flag & FWRITE) == 0) 2768 return (EBADF); /* must be opened for writing */ 2769 if (((struct fd_formb *)addr)->format_version != 2770 FD_FORMAT_VERSION) 2771 return (EINVAL); /* wrong version of formatting prog */ 2772 error = fdmisccmd(dev, BIO_FORMAT, addr); 2773 break; 2774 2775 case FD_GTYPE: /* get drive type */ 2776 *(struct fd_type *)addr = *fd->ft; 2777 break; 2778 2779 case FD_STYPE: /* set drive type */ 2780 /* this is considered harmful; only allow for superuser */ 2781 if (suser(td) != 0) 2782 return (EPERM); 2783 *fd->ft = *(struct fd_type *)addr; 2784 break; 2785 2786 case FD_GOPTS: /* get drive options */ 2787 *(int *)addr = fd->options; 2788 break; 2789 2790 case FD_SOPTS: /* set drive options */ 2791 fd->options = *(int *)addr; 2792 break; 2793 2794 #ifdef FDC_DEBUG 2795 case FD_DEBUG: 2796 if ((fd_debug != 0) != (*(int *)addr != 0)) { 2797 fd_debug = (*(int *)addr != 0); 2798 printf("fd%d: debugging turned %s\n", 2799 fd->fdu, fd_debug ? "on" : "off"); 2800 } 2801 break; 2802 #endif 2803 2804 case FD_CLRERR: 2805 if (suser(td) != 0) 2806 return (EPERM); 2807 fd->fdc->fdc_errs = 0; 2808 break; 2809 2810 case FD_GSTAT: 2811 fsp = (struct fdc_status *)addr; 2812 if ((fd->fdc->flags & FDC_STAT_VALID) == 0) 2813 return (EINVAL); 2814 memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int)); 2815 break; 2816 2817 case FD_READID: 2818 rid = (struct fdc_readid *)addr; 2819 if (rid->cyl > MAX_CYLINDER || rid->head > MAX_HEAD) 2820 return (EINVAL); 2821 error = fdmisccmd(dev, BIO_RDSECTID, addr); 2822 break; 2823 2824 default: 2825 error = ENOTTY; 2826 break; 2827 } 2828 return (error); 2829 } 2830