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