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