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