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