xref: /freebsd-13-stable/sys/dev/fdc/fdc.c (revision 3bc80996974a61a4223eae4c1ccd47b6ee32a48a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2004 Poul-Henning Kamp
5  * Copyright (c) 1990 The Regents of the University of California.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Don Ahn.
10  *
11  * Libretto PCMCIA floppy support by David Horwitt (dhorwitt@ucsd.edu)
12  * aided by the Linux floppy driver modifications from David Bateman
13  * (dbateman@eng.uts.edu.au).
14  *
15  * Copyright (c) 1993, 1994 by
16  *  jc@irbs.UUCP (John Capo)
17  *  vak@zebub.msk.su (Serge Vakulenko)
18  *  ache@astral.msk.su (Andrew A. Chernov)
19  *
20  * Copyright (c) 1993, 1994, 1995 by
21  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
22  *  dufault@hda.com (Peter Dufault)
23  *
24  * Copyright (c) 2001 Joerg Wunsch,
25  *  joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch)
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions
29  * are met:
30  * 1. Redistributions of source code must retain the above copyright
31  *    notice, this list of conditions and the following disclaimer.
32  * 2. Redistributions in binary form must reproduce the above copyright
33  *    notice, this list of conditions and the following disclaimer in the
34  *    documentation and/or other materials provided with the distribution.
35  * 3. Neither the name of the University nor the names of its contributors
36  *    may be used to endorse or promote products derived from this software
37  *    without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49  * SUCH DAMAGE.
50  *
51  *	from:	@(#)fd.c	7.4 (Berkeley) 5/25/91
52  *
53  */
54 
55 #include <sys/cdefs.h>
56 #include "opt_fdc.h"
57 
58 #include <sys/param.h>
59 #include <sys/bio.h>
60 #include <sys/bus.h>
61 #include <sys/devicestat.h>
62 #include <sys/disk.h>
63 #include <sys/fcntl.h>
64 #include <sys/fdcio.h>
65 #include <sys/filio.h>
66 #include <sys/kernel.h>
67 #include <sys/kthread.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/module.h>
71 #include <sys/mutex.h>
72 #include <sys/priv.h>
73 #include <sys/proc.h>
74 #include <sys/rman.h>
75 #include <sys/sysctl.h>
76 #include <sys/systm.h>
77 
78 #include <geom/geom.h>
79 
80 #include <machine/bus.h>
81 #include <machine/clock.h>
82 #include <machine/stdarg.h>
83 
84 #include <isa/isavar.h>
85 #include <isa/isareg.h>
86 #include <isa/rtc.h>
87 #include <dev/fdc/fdcvar.h>
88 
89 #include <dev/ic/nec765.h>
90 
91 /*
92  * Runtime configuration hints/flags
93  */
94 
95 /* configuration flags for fd */
96 #define FD_TYPEMASK	0x0f	/* drive type, matches enum
97 				 * fd_drivetype; on i386 machines, if
98 				 * given as 0, use RTC type for fd0
99 				 * and fd1 */
100 #define	FD_NO_CHLINE	0x10	/* drive does not support changeline
101 				 * aka. unit attention */
102 #define FD_NO_PROBE	0x20	/* don't probe drive (seek test), just
103 				 * assume it is there */
104 
105 /*
106  * Things that could conceiveably considered parameters or tweakables
107  */
108 
109 /*
110  * Maximal number of bytes in a cylinder.
111  * This is used for ISADMA bouncebuffer allocation and sets the max
112  * xfersize we support.
113  *
114  * 2.88M format has 2 x 36 x 512, allow for hacked up density.
115  */
116 #define MAX_BYTES_PER_CYL	(2 * 40 * 512)
117 
118 /*
119  * Timeout value for the PIO loops to wait until the FDC main status
120  * register matches our expectations (request for master, direction
121  * bit).  This is supposed to be a number of microseconds, although
122  * timing might actually not be very accurate.
123  *
124  * Timeouts of 100 msec are believed to be required for some broken
125  * (old) hardware.
126  */
127 #define	FDSTS_TIMEOUT	100000
128 
129 /*
130  * After this many errors, stop whining.  Close will reset this count.
131  */
132 #define FDC_ERRMAX	100
133 
134 /*
135  * AutoDensity search lists for each drive type.
136  */
137 
138 static struct fd_type fd_searchlist_360k[] = {
139 	{ FDF_5_360 },
140 	{ 0 }
141 };
142 
143 static struct fd_type fd_searchlist_12m[] = {
144 	{ FDF_5_1200 | FL_AUTO },
145 	{ FDF_5_400 | FL_AUTO },
146 	{ FDF_5_360 | FL_2STEP | FL_AUTO},
147 	{ 0 }
148 };
149 
150 static struct fd_type fd_searchlist_720k[] = {
151 	{ FDF_3_720 },
152 	{ 0 }
153 };
154 
155 static struct fd_type fd_searchlist_144m[] = {
156 	{ FDF_3_1440 | FL_AUTO},
157 	{ FDF_3_720 | FL_AUTO},
158 	{ 0 }
159 };
160 
161 static struct fd_type fd_searchlist_288m[] = {
162 	{ FDF_3_1440 | FL_AUTO },
163 #if 0
164 	{ FDF_3_2880 | FL_AUTO }, /* XXX: probably doesn't work */
165 #endif
166 	{ FDF_3_720 | FL_AUTO},
167 	{ 0 }
168 };
169 
170 /*
171  * Order must match enum fd_drivetype in <sys/fdcio.h>.
172  */
173 static struct fd_type *fd_native_types[] = {
174 	NULL,				/* FDT_NONE */
175 	fd_searchlist_360k, 		/* FDT_360K */
176 	fd_searchlist_12m, 		/* FDT_12M */
177 	fd_searchlist_720k, 		/* FDT_720K */
178 	fd_searchlist_144m, 		/* FDT_144M */
179 	fd_searchlist_288m,		/* FDT_288M_1 (mapped to FDT_288M) */
180 	fd_searchlist_288m, 		/* FDT_288M */
181 };
182 
183 /*
184  * Internals start here
185  */
186 
187 /* registers */
188 #define	FDOUT	2	/* Digital Output Register (W) */
189 #define	FDO_FDSEL	0x03	/*  floppy device select */
190 #define	FDO_FRST	0x04	/*  floppy controller reset */
191 #define	FDO_FDMAEN	0x08	/*  enable floppy DMA and Interrupt */
192 #define	FDO_MOEN0	0x10	/*  motor enable drive 0 */
193 #define	FDO_MOEN1	0x20	/*  motor enable drive 1 */
194 #define	FDO_MOEN2	0x40	/*  motor enable drive 2 */
195 #define	FDO_MOEN3	0x80	/*  motor enable drive 3 */
196 
197 #define	FDSTS	4	/* NEC 765 Main Status Register (R) */
198 #define FDDSR	4	/* Data Rate Select Register (W) */
199 #define	FDDATA	5	/* NEC 765 Data Register (R/W) */
200 #define	FDCTL	7	/* Control Register (W) */
201 
202 /*
203  * The YE-DATA PC Card floppies use PIO to read in the data rather
204  * than DMA due to the wild variability of DMA for the PC Card
205  * devices.  DMA was deleted from the PC Card specification in version
206  * 7.2 of the standard, but that post-dates the YE-DATA devices by many
207  * years.
208  *
209  * In addition, if we cannot setup the DMA resources for the ISA
210  * attachment, we'll use this same offset for data transfer.  However,
211  * that almost certainly won't work.
212  *
213  * For this mode, offset 0 and 1 must be used to setup the transfer
214  * for this floppy.  This is OK for PC Card YE Data devices, but for
215  * ISA this is likely wrong.  These registers are only available on
216  * those systems that map them to the floppy drive.  Newer systems do
217  * not do this, and we should likely prohibit access to them (or
218  * disallow NODMA to be set).
219  */
220 #define FDBCDR		0	/* And 1 */
221 #define FD_YE_DATAPORT	6	/* Drive Data port */
222 
223 #define	FDI_DCHG	0x80	/* diskette has been changed */
224 				/* requires drive and motor being selected */
225 				/* is cleared by any step pulse to drive */
226 
227 /*
228  * We have three private BIO commands.
229  */
230 #define BIO_PROBE	BIO_CMD0
231 #define BIO_RDID	BIO_CMD1
232 #define BIO_FMT		BIO_CMD2
233 
234 /*
235  * Per drive structure (softc).
236  */
237 struct fd_data {
238 	u_char 	*fd_ioptr;	/* IO pointer */
239 	u_int	fd_iosize;	/* Size of IO chunks */
240 	u_int	fd_iocount;	/* Outstanding requests */
241 	struct	fdc_data *fdc;	/* pointer to controller structure */
242 	int	fdsu;		/* this units number on this controller */
243 	enum	fd_drivetype type; /* drive type */
244 	struct	fd_type *ft;	/* pointer to current type descriptor */
245 	struct	fd_type fts;	/* type descriptors */
246 	int	sectorsize;
247 	int	flags;
248 #define	FD_WP		(1<<0)	/* Write protected	*/
249 #define	FD_MOTOR	(1<<1)	/* motor should be on	*/
250 #define	FD_MOTORWAIT	(1<<2)	/* motor should be on	*/
251 #define	FD_EMPTY	(1<<3)	/* no media		*/
252 #define	FD_NEWDISK	(1<<4)	/* media changed	*/
253 #define	FD_ISADMA	(1<<5)	/* isa dma started 	*/
254 	int	track;		/* where we think the head is */
255 #define FD_NO_TRACK	 -2
256 	int	options;	/* FDOPT_* */
257 	struct	callout toffhandle;
258 	struct g_geom *fd_geom;
259 	struct g_provider *fd_provider;
260 	device_t dev;
261 	struct bio_queue_head fd_bq;
262 };
263 
264 #define FD_NOT_VALID -2
265 
266 static driver_intr_t fdc_intr;
267 static driver_filter_t fdc_intr_fast;
268 static void fdc_reset(struct fdc_data *);
269 static int fd_probe_disk(struct fd_data *, int *);
270 
271 static SYSCTL_NODE(_debug, OID_AUTO, fdc, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
272     "fdc driver");
273 
274 static int fifo_threshold = 8;
275 SYSCTL_INT(_debug_fdc, OID_AUTO, fifo, CTLFLAG_RW, &fifo_threshold, 0,
276 	"FIFO threshold setting");
277 
278 static int debugflags = 0;
279 SYSCTL_INT(_debug_fdc, OID_AUTO, debugflags, CTLFLAG_RW, &debugflags, 0,
280 	"Debug flags");
281 
282 static int retries = 10;
283 SYSCTL_INT(_debug_fdc, OID_AUTO, retries, CTLFLAG_RW, &retries, 0,
284 	"Number of retries to attempt");
285 
286 static int spec1 = NE7_SPEC_1(6, 240);
287 SYSCTL_INT(_debug_fdc, OID_AUTO, spec1, CTLFLAG_RW, &spec1, 0,
288 	"Specification byte one (step-rate + head unload)");
289 
290 static int spec2 = NE7_SPEC_2(16, 0);
291 SYSCTL_INT(_debug_fdc, OID_AUTO, spec2, CTLFLAG_RW, &spec2, 0,
292 	"Specification byte two (head load time + no-dma)");
293 
294 static int settle;
295 SYSCTL_INT(_debug_fdc, OID_AUTO, settle, CTLFLAG_RW, &settle, 0,
296 	"Head settling time in sec/hz");
297 
298 static void
fdprinttype(struct fd_type * ft)299 fdprinttype(struct fd_type *ft)
300 {
301 
302 	printf("(%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,0x%x)",
303 	    ft->sectrac, ft->secsize, ft->datalen, ft->gap, ft->tracks,
304 	    ft->size, ft->trans, ft->heads, ft->f_gap, ft->f_inter,
305 	    ft->offset_side2, ft->flags);
306 }
307 
308 static void
fdsettype(struct fd_data * fd,struct fd_type * ft)309 fdsettype(struct fd_data *fd, struct fd_type *ft)
310 {
311 	fd->ft = ft;
312 	ft->size = ft->sectrac * ft->heads * ft->tracks;
313 	fd->sectorsize = 128 << fd->ft->secsize;
314 }
315 
316 /*
317  * Bus space handling (access to low-level IO).
318  */
319 static inline void
fdregwr(struct fdc_data * fdc,int reg,uint8_t v)320 fdregwr(struct fdc_data *fdc, int reg, uint8_t v)
321 {
322 
323 	bus_space_write_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg], v);
324 }
325 
326 static inline uint8_t
fdregrd(struct fdc_data * fdc,int reg)327 fdregrd(struct fdc_data *fdc, int reg)
328 {
329 
330 	return bus_space_read_1(fdc->iot, fdc->ioh[reg], fdc->ioff[reg]);
331 }
332 
333 static void
fdctl_wr(struct fdc_data * fdc,u_int8_t v)334 fdctl_wr(struct fdc_data *fdc, u_int8_t v)
335 {
336 
337 	fdregwr(fdc, FDCTL, v);
338 }
339 
340 static void
fdout_wr(struct fdc_data * fdc,u_int8_t v)341 fdout_wr(struct fdc_data *fdc, u_int8_t v)
342 {
343 
344 	fdregwr(fdc, FDOUT, v);
345 }
346 
347 static u_int8_t
fdsts_rd(struct fdc_data * fdc)348 fdsts_rd(struct fdc_data *fdc)
349 {
350 
351 	return fdregrd(fdc, FDSTS);
352 }
353 
354 static void
fddsr_wr(struct fdc_data * fdc,u_int8_t v)355 fddsr_wr(struct fdc_data *fdc, u_int8_t v)
356 {
357 
358 	fdregwr(fdc, FDDSR, v);
359 }
360 
361 static void
fddata_wr(struct fdc_data * fdc,u_int8_t v)362 fddata_wr(struct fdc_data *fdc, u_int8_t v)
363 {
364 
365 	fdregwr(fdc, FDDATA, v);
366 }
367 
368 static u_int8_t
fddata_rd(struct fdc_data * fdc)369 fddata_rd(struct fdc_data *fdc)
370 {
371 
372 	return fdregrd(fdc, FDDATA);
373 }
374 
375 static u_int8_t
fdin_rd(struct fdc_data * fdc)376 fdin_rd(struct fdc_data *fdc)
377 {
378 
379 	return fdregrd(fdc, FDCTL);
380 }
381 
382 /*
383  * Magic pseudo-DMA initialization for YE FDC. Sets count and
384  * direction.
385  */
386 static void
fdbcdr_wr(struct fdc_data * fdc,int iswrite,uint16_t count)387 fdbcdr_wr(struct fdc_data *fdc, int iswrite, uint16_t count)
388 {
389 	fdregwr(fdc, FDBCDR, (count - 1) & 0xff);
390 	fdregwr(fdc, FDBCDR + 1,
391 	    (iswrite ? 0x80 : 0) | (((count - 1) >> 8) & 0x7f));
392 }
393 
394 static int
fdc_err(struct fdc_data * fdc,const char * s)395 fdc_err(struct fdc_data *fdc, const char *s)
396 {
397 	fdc->fdc_errs++;
398 	if (s) {
399 		if (fdc->fdc_errs < FDC_ERRMAX)
400 			device_printf(fdc->fdc_dev, "%s", s);
401 		else if (fdc->fdc_errs == FDC_ERRMAX)
402 			device_printf(fdc->fdc_dev, "too many errors, not "
403 						    "logging any more\n");
404 	}
405 
406 	return (1);
407 }
408 
409 /*
410  * FDC IO functions, take care of the main status register, timeout
411  * in case the desired status bits are never set.
412  *
413  * These PIO loops initially start out with short delays between
414  * each iteration in the expectation that the required condition
415  * is usually met quickly, so it can be handled immediately.
416  */
417 static int
fdc_in(struct fdc_data * fdc,int * ptr)418 fdc_in(struct fdc_data *fdc, int *ptr)
419 {
420 	int i, j, step;
421 
422 	step = 1;
423 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
424 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
425 	        if (i == (NE7_DIO|NE7_RQM)) {
426 			i = fddata_rd(fdc);
427 			if (ptr)
428 				*ptr = i;
429 			return (0);
430 		}
431 		if (i == NE7_RQM)
432 			return (fdc_err(fdc, "ready for output in input\n"));
433 		step += step;
434 		DELAY(step);
435 	}
436 	return (fdc_err(fdc, bootverbose? "input ready timeout\n": 0));
437 }
438 
439 static int
fdc_out(struct fdc_data * fdc,int x)440 fdc_out(struct fdc_data *fdc, int x)
441 {
442 	int i, j, step;
443 
444 	step = 1;
445 	for (j = 0; j < FDSTS_TIMEOUT; j += step) {
446 	        i = fdsts_rd(fdc) & (NE7_DIO | NE7_RQM);
447 	        if (i == NE7_RQM) {
448 			fddata_wr(fdc, x);
449 			return (0);
450 		}
451 		if (i == (NE7_DIO|NE7_RQM))
452 			return (fdc_err(fdc, "ready for input in output\n"));
453 		step += step;
454 		DELAY(step);
455 	}
456 	return (fdc_err(fdc, bootverbose? "output ready timeout\n": 0));
457 }
458 
459 /*
460  * fdc_cmd: Send a command to the chip.
461  * Takes a varargs with this structure:
462  *	# of output bytes
463  *	output bytes as int [...]
464  *	# of input bytes
465  *	input bytes as int* [...]
466  */
467 static int
fdc_cmd(struct fdc_data * fdc,int n_out,...)468 fdc_cmd(struct fdc_data *fdc, int n_out, ...)
469 {
470 	u_char cmd = 0;
471 	int n_in;
472 	int n, i;
473 	va_list ap;
474 
475 	va_start(ap, n_out);
476 	for (n = 0; n < n_out; n++) {
477 		i = va_arg(ap, int);
478 		if (n == 0)
479 			cmd = i;
480 		if (fdc_out(fdc, i) < 0) {
481 			char msg[50];
482 			snprintf(msg, sizeof(msg),
483 				"cmd %x failed at out byte %d of %d\n",
484 				cmd, n + 1, n_out);
485 			fdc->flags |= FDC_NEEDS_RESET;
486 			va_end(ap);
487 			return fdc_err(fdc, msg);
488 		}
489 	}
490 	n_in = va_arg(ap, int);
491 	for (n = 0; n < n_in; n++) {
492 		int *ptr = va_arg(ap, int *);
493 		if (fdc_in(fdc, ptr) != 0) {
494 			char msg[50];
495 			snprintf(msg, sizeof(msg),
496 				"cmd %02x failed at in byte %d of %d\n",
497 				cmd, n + 1, n_in);
498 			fdc->flags |= FDC_NEEDS_RESET;
499 			va_end(ap);
500 			return fdc_err(fdc, msg);
501 		}
502 	}
503 	va_end(ap);
504 	return (0);
505 }
506 
507 static void
fdc_reset(struct fdc_data * fdc)508 fdc_reset(struct fdc_data *fdc)
509 {
510 	int i, r[10];
511 
512 	if (fdc->fdct == FDC_ENHANCED) {
513 		/* Try a software reset, default precomp, and 500 kb/s */
514 		fddsr_wr(fdc, I8207X_DSR_SR);
515 	} else {
516 		/* Try a hardware reset, keep motor on */
517 		fdout_wr(fdc, fdc->fdout & ~(FDO_FRST|FDO_FDMAEN));
518 		DELAY(100);
519 		/* enable FDC, but defer interrupts a moment */
520 		fdout_wr(fdc, fdc->fdout & ~FDO_FDMAEN);
521 	}
522 	DELAY(100);
523 	fdout_wr(fdc, fdc->fdout);
524 
525 	/* XXX after a reset, silently believe the FDC will accept commands */
526 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, spec1, spec2, 0))
527 		device_printf(fdc->fdc_dev, " SPECIFY failed in reset\n");
528 
529 	if (fdc->fdct == FDC_ENHANCED) {
530 		if (fdc_cmd(fdc, 4,
531 		    I8207X_CONFIG,
532 		    0,
533 		    /* 0x40 | */		/* Enable Implied Seek -
534 						 * breaks 2step! */
535 		    0x10 |			/* Polling disabled */
536 		    (fifo_threshold - 1),	/* Fifo threshold */
537 		    0x00,			/* Precomp track */
538 		    0))
539 			device_printf(fdc->fdc_dev,
540 			    " CONFIGURE failed in reset\n");
541 		if (debugflags & 1) {
542 			if (fdc_cmd(fdc, 1,
543 			    I8207X_DUMPREG,
544 			    10, &r[0], &r[1], &r[2], &r[3], &r[4],
545 			    &r[5], &r[6], &r[7], &r[8], &r[9]))
546 				device_printf(fdc->fdc_dev,
547 				    " DUMPREG failed in reset\n");
548 			for (i = 0; i < 10; i++)
549 				printf(" %02x", r[i]);
550 			printf("\n");
551 		}
552 	}
553 }
554 
555 static int
fdc_sense_drive(struct fdc_data * fdc,int * st3p)556 fdc_sense_drive(struct fdc_data *fdc, int *st3p)
557 {
558 	int st3;
559 
560 	if (fdc_cmd(fdc, 2, NE7CMD_SENSED, fdc->fd->fdsu, 1, &st3))
561 		return (fdc_err(fdc, "Sense Drive Status failed\n"));
562 	if (st3p)
563 		*st3p = st3;
564 	return (0);
565 }
566 
567 static int
fdc_sense_int(struct fdc_data * fdc,int * st0p,int * cylp)568 fdc_sense_int(struct fdc_data *fdc, int *st0p, int *cylp)
569 {
570 	int cyl, st0, ret;
571 
572 	ret = fdc_cmd(fdc, 1, NE7CMD_SENSEI, 1, &st0);
573 	if (ret) {
574 		(void)fdc_err(fdc, "sense intr err reading stat reg 0\n");
575 		return (ret);
576 	}
577 
578 	if (st0p)
579 		*st0p = st0;
580 
581 	if ((st0 & NE7_ST0_IC) == NE7_ST0_IC_IV) {
582 		/*
583 		 * There doesn't seem to have been an interrupt.
584 		 */
585 		return (FD_NOT_VALID);
586 	}
587 
588 	if (fdc_in(fdc, &cyl) != 0)
589 		return fdc_err(fdc, "can't get cyl num\n");
590 
591 	if (cylp)
592 		*cylp = cyl;
593 
594 	return (0);
595 }
596 
597 static int
fdc_read_status(struct fdc_data * fdc)598 fdc_read_status(struct fdc_data *fdc)
599 {
600 	int i, ret, status;
601 
602 	for (i = ret = 0; i < 7; i++) {
603 		ret = fdc_in(fdc, &status);
604 		fdc->status[i] = status;
605 		if (ret != 0)
606 			break;
607 	}
608 
609 	if (ret == 0)
610 		fdc->flags |= FDC_STAT_VALID;
611 	else
612 		fdc->flags &= ~FDC_STAT_VALID;
613 
614 	return ret;
615 }
616 
617 /*
618  * Select this drive
619  */
620 static void
fd_select(struct fd_data * fd)621 fd_select(struct fd_data *fd)
622 {
623 	struct fdc_data *fdc;
624 
625 	/* XXX: lock controller */
626 	fdc = fd->fdc;
627 	fdc->fdout &= ~FDO_FDSEL;
628 	fdc->fdout |= FDO_FDMAEN | FDO_FRST | fd->fdsu;
629 	fdout_wr(fdc, fdc->fdout);
630 }
631 
632 static void
fd_turnon(void * arg)633 fd_turnon(void *arg)
634 {
635 	struct fd_data *fd;
636 	struct bio *bp;
637 	int once;
638 
639 	fd = arg;
640 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
641 	fd->flags &= ~FD_MOTORWAIT;
642 	fd->flags |= FD_MOTOR;
643 	once = 0;
644 	for (;;) {
645 		bp = bioq_takefirst(&fd->fd_bq);
646 		if (bp == NULL)
647 			break;
648 		bioq_disksort(&fd->fdc->head, bp);
649 		once = 1;
650 	}
651 	if (once)
652 		wakeup(&fd->fdc->head);
653 }
654 
655 static void
fd_motor(struct fd_data * fd,int turnon)656 fd_motor(struct fd_data *fd, int turnon)
657 {
658 	struct fdc_data *fdc;
659 
660 	fdc = fd->fdc;
661 /*
662 	mtx_assert(&fdc->fdc_mtx, MA_OWNED);
663 */
664 	if (turnon) {
665 		fd->flags |= FD_MOTORWAIT;
666 		fdc->fdout |= (FDO_MOEN0 << fd->fdsu);
667 		callout_reset(&fd->toffhandle, hz, fd_turnon, fd);
668 	} else {
669 		callout_stop(&fd->toffhandle);
670 		fd->flags &= ~(FD_MOTOR|FD_MOTORWAIT);
671 		fdc->fdout &= ~(FDO_MOEN0 << fd->fdsu);
672 	}
673 	fdout_wr(fdc, fdc->fdout);
674 }
675 
676 static void
fd_turnoff(void * xfd)677 fd_turnoff(void *xfd)
678 {
679 	struct fd_data *fd = xfd;
680 
681 	mtx_assert(&fd->fdc->fdc_mtx, MA_OWNED);
682 	fd_motor(fd, 0);
683 }
684 
685 /*
686  * fdc_intr - wake up the worker thread.
687  */
688 
689 static void
fdc_intr(void * arg)690 fdc_intr(void *arg)
691 {
692 
693 	wakeup(arg);
694 }
695 
696 static int
fdc_intr_fast(void * arg)697 fdc_intr_fast(void *arg)
698 {
699 
700 	wakeup(arg);
701 	return(FILTER_HANDLED);
702 }
703 
704 /*
705  * fdc_pio(): perform programmed IO read/write for YE PCMCIA floppy.
706  */
707 static void
fdc_pio(struct fdc_data * fdc)708 fdc_pio(struct fdc_data *fdc)
709 {
710 	u_char *cptr;
711 	struct bio *bp;
712 	u_int count;
713 
714 	bp = fdc->bp;
715 	cptr = fdc->fd->fd_ioptr;
716 	count = fdc->fd->fd_iosize;
717 
718 	if (bp->bio_cmd == BIO_READ) {
719 		fdbcdr_wr(fdc, 0, count);
720 		bus_space_read_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
721 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
722 	} else {
723 		bus_space_write_multi_1(fdc->iot, fdc->ioh[FD_YE_DATAPORT],
724 		    fdc->ioff[FD_YE_DATAPORT], cptr, count);
725 		fdbcdr_wr(fdc, 0, count);	/* needed? */
726 	}
727 }
728 
729 static int
fdc_biodone(struct fdc_data * fdc,int error)730 fdc_biodone(struct fdc_data *fdc, int error)
731 {
732 	struct fd_data *fd;
733 	struct bio *bp;
734 
735 	fd = fdc->fd;
736 	bp = fdc->bp;
737 
738 	mtx_lock(&fdc->fdc_mtx);
739 	if (--fd->fd_iocount == 0)
740 		callout_reset(&fd->toffhandle, 4 * hz, fd_turnoff, fd);
741 	fdc->bp = NULL;
742 	fdc->fd = NULL;
743 	mtx_unlock(&fdc->fdc_mtx);
744 	if (bp->bio_to != NULL) {
745 		if ((debugflags & 2) && fd->fdc->retry > 0)
746 			printf("retries: %d\n", fd->fdc->retry);
747 		g_io_deliver(bp, error);
748 		return (0);
749 	}
750 	bp->bio_error = error;
751 	bp->bio_flags |= BIO_DONE;
752 	wakeup(bp);
753 	return (0);
754 }
755 
756 static int retry_line;
757 
758 static int
fdc_worker(struct fdc_data * fdc)759 fdc_worker(struct fdc_data *fdc)
760 {
761 	struct fd_data *fd;
762 	struct bio *bp;
763 	int i, nsect;
764 	int st0, st3, cyl, mfm, steptrac, cylinder, descyl, sec;
765 	int head;
766 	int override_error;
767 	static int need_recal;
768 	struct fdc_readid *idp;
769 	struct fd_formb *finfo;
770 
771 	override_error = 0;
772 
773 	/* Have we exhausted our retries ? */
774 	bp = fdc->bp;
775 	fd = fdc->fd;
776 	if (bp != NULL &&
777 		(fdc->retry >= retries || (fd->options & FDOPT_NORETRY))) {
778 		if ((debugflags & 4))
779 			printf("Too many retries (EIO)\n");
780 		if (fdc->flags & FDC_NEEDS_RESET) {
781 			mtx_lock(&fdc->fdc_mtx);
782 			fd->flags |= FD_EMPTY;
783 			mtx_unlock(&fdc->fdc_mtx);
784 		}
785 		return (fdc_biodone(fdc, EIO));
786 	}
787 
788 	/* Disable ISADMA if we bailed while it was active */
789 	if (fd != NULL && (fd->flags & FD_ISADMA)) {
790 		isa_dmadone(
791 		    bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
792 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
793 		mtx_lock(&fdc->fdc_mtx);
794 		fd->flags &= ~FD_ISADMA;
795 		mtx_unlock(&fdc->fdc_mtx);
796 	}
797 
798 	/* Unwedge the controller ? */
799 	if (fdc->flags & FDC_NEEDS_RESET) {
800 		fdc->flags &= ~FDC_NEEDS_RESET;
801 		fdc_reset(fdc);
802 		if (cold)
803 			DELAY(1000000);
804 		else
805 			tsleep(fdc, PRIBIO, "fdcrst", hz);
806 		/* Discard results */
807 		for (i = 0; i < 4; i++)
808 			fdc_sense_int(fdc, &st0, &cyl);
809 		/* All drives must recal */
810 		need_recal = 0xf;
811 	}
812 
813 	/* Pick up a request, if need be wait for it */
814 	if (fdc->bp == NULL) {
815 		mtx_lock(&fdc->fdc_mtx);
816 		do {
817 			fdc->bp = bioq_takefirst(&fdc->head);
818 			if (fdc->bp == NULL)
819 				msleep(&fdc->head, &fdc->fdc_mtx,
820 				    PRIBIO, "-", 0);
821 		} while (fdc->bp == NULL &&
822 		    (fdc->flags & FDC_KTHREAD_EXIT) == 0);
823 		mtx_unlock(&fdc->fdc_mtx);
824 
825 		if (fdc->bp == NULL)
826 			/*
827 			 * Nothing to do, worker thread has been
828 			 * requested to stop.
829 			 */
830 			return (0);
831 
832 		bp = fdc->bp;
833 		fd = fdc->fd = bp->bio_driver1;
834 		fdc->retry = 0;
835 		fd->fd_ioptr = bp->bio_data;
836 		if (bp->bio_cmd == BIO_FMT) {
837 			i = offsetof(struct fd_formb, fd_formb_cylno(0));
838 			fd->fd_ioptr += i;
839 			fd->fd_iosize = bp->bio_length - i;
840 		}
841 	}
842 
843 	/* Select drive, setup params */
844 	fd_select(fd);
845 	if (fdc->fdct == FDC_ENHANCED)
846 		fddsr_wr(fdc, fd->ft->trans);
847 	else
848 		fdctl_wr(fdc, fd->ft->trans);
849 
850 	if (bp->bio_cmd == BIO_PROBE) {
851 		if ((!(device_get_flags(fd->dev) & FD_NO_CHLINE) &&
852 		    !(fdin_rd(fdc) & FDI_DCHG) &&
853 		    !(fd->flags & FD_EMPTY)) ||
854 		    fd_probe_disk(fd, &need_recal) == 0)
855 			return (fdc_biodone(fdc, 0));
856 		return (1);
857 	}
858 
859 	/*
860 	 * If we are dead just flush the requests
861 	 */
862 	if (fd->flags & FD_EMPTY)
863 		return (fdc_biodone(fdc, ENXIO));
864 
865 	/* Check if we lost our media */
866 	if (fdin_rd(fdc) & FDI_DCHG) {
867 		if (debugflags & 0x40)
868 			printf("Lost disk\n");
869 		mtx_lock(&fdc->fdc_mtx);
870 		fd->flags |= FD_EMPTY;
871 		fd->flags |= FD_NEWDISK;
872 		mtx_unlock(&fdc->fdc_mtx);
873 		g_topology_lock();
874 		g_orphan_provider(fd->fd_provider, ENXIO);
875 		fd->fd_provider->flags |= G_PF_WITHER;
876 		fd->fd_provider =
877 		    g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
878 		g_error_provider(fd->fd_provider, 0);
879 		g_topology_unlock();
880 		return (fdc_biodone(fdc, ENXIO));
881 	}
882 
883 	/* Check if the floppy is write-protected */
884 	if (bp->bio_cmd == BIO_FMT || bp->bio_cmd == BIO_WRITE) {
885 		retry_line = __LINE__;
886 		if(fdc_sense_drive(fdc, &st3) != 0)
887 			return (1);
888 		if(st3 & NE7_ST3_WP)
889 			return (fdc_biodone(fdc, EROFS));
890 	}
891 
892 	mfm = (fd->ft->flags & FL_MFM)? NE7CMD_MFM: 0;
893 	steptrac = (fd->ft->flags & FL_2STEP)? 2: 1;
894 	i = fd->ft->sectrac * fd->ft->heads;
895 	cylinder = bp->bio_pblkno / i;
896 	descyl = cylinder * steptrac;
897 	sec = bp->bio_pblkno % i;
898 	nsect = i - sec;
899 	head = sec / fd->ft->sectrac;
900 	sec = sec % fd->ft->sectrac + 1;
901 
902 	/* If everything is going swimmingly, use multisector xfer */
903 	if (fdc->retry == 0 &&
904 	    (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
905 		fd->fd_iosize = imin(nsect * fd->sectorsize, bp->bio_resid);
906 		nsect = fd->fd_iosize / fd->sectorsize;
907 	} else if (bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE) {
908 		fd->fd_iosize = fd->sectorsize;
909 		nsect = 1;
910 	}
911 
912 	/* Do RECAL if we need to or are going to track zero anyway */
913 	if ((need_recal & (1 << fd->fdsu)) ||
914 	    (cylinder == 0 && fd->track != 0) ||
915 	    fdc->retry > 2) {
916 		retry_line = __LINE__;
917 		if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
918 			return (1);
919 		tsleep(fdc, PRIBIO, "fdrecal", hz);
920 		retry_line = __LINE__;
921 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
922 			return (1); /* XXX */
923 		retry_line = __LINE__;
924 		if ((st0 & 0xc0) || cyl != 0)
925 			return (1);
926 		need_recal &= ~(1 << fd->fdsu);
927 		fd->track = 0;
928 		/* let the heads settle */
929 		if (settle)
930 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
931 	}
932 
933 	/*
934 	 * SEEK to where we want to be
935 	 */
936 	if (cylinder != fd->track) {
937 		retry_line = __LINE__;
938 		if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, descyl, 0))
939 			return (1);
940 		tsleep(fdc, PRIBIO, "fdseek", hz);
941 		retry_line = __LINE__;
942 		if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
943 			return (1); /* XXX */
944 		retry_line = __LINE__;
945 		if ((st0 & 0xc0) || cyl != descyl) {
946 			need_recal |= (1 << fd->fdsu);
947 			return (1);
948 		}
949 		/* let the heads settle */
950 		if (settle)
951 			tsleep(fdc->fd, PRIBIO, "fdhdstl", settle);
952 	}
953 	fd->track = cylinder;
954 
955 	if (debugflags & 8)
956 		printf("op %x bn %ju siz %u ptr %p retry %d\n",
957 		    bp->bio_cmd, bp->bio_pblkno, fd->fd_iosize,
958 		    fd->fd_ioptr, fdc->retry);
959 
960 	/* Setup ISADMA if we need it and have it */
961 	if ((bp->bio_cmd == BIO_READ ||
962 		bp->bio_cmd == BIO_WRITE ||
963 		bp->bio_cmd == BIO_FMT)
964 	     && !(fdc->flags & FDC_NODMA)) {
965 		isa_dmastart(
966 		    bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
967 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
968 		mtx_lock(&fdc->fdc_mtx);
969 		fd->flags |= FD_ISADMA;
970 		mtx_unlock(&fdc->fdc_mtx);
971 	}
972 
973 	/* Do PIO if we have to */
974 	if (fdc->flags & FDC_NODMA) {
975 		if (bp->bio_cmd == BIO_READ ||
976 		    bp->bio_cmd == BIO_WRITE ||
977 		    bp->bio_cmd == BIO_FMT)
978 			fdbcdr_wr(fdc, 1, fd->fd_iosize);
979 		if (bp->bio_cmd == BIO_WRITE ||
980 		    bp->bio_cmd == BIO_FMT)
981 			fdc_pio(fdc);
982 	}
983 
984 	switch(bp->bio_cmd) {
985 	case BIO_FMT:
986 		/* formatting */
987 		finfo = (struct fd_formb *)bp->bio_data;
988 		retry_line = __LINE__;
989 		if (fdc_cmd(fdc, 6,
990 		    NE7CMD_FORMAT | mfm,
991 		    head << 2 | fd->fdsu,
992 		    finfo->fd_formb_secshift,
993 		    finfo->fd_formb_nsecs,
994 		    finfo->fd_formb_gaplen,
995 		    finfo->fd_formb_fillbyte, 0))
996 			return (1);
997 		break;
998 	case BIO_RDID:
999 		retry_line = __LINE__;
1000 		if (fdc_cmd(fdc, 2,
1001 		    NE7CMD_READID | mfm,
1002 		    head << 2 | fd->fdsu, 0))
1003 			return (1);
1004 		break;
1005 	case BIO_READ:
1006 		retry_line = __LINE__;
1007 		if (fdc_cmd(fdc, 9,
1008 		    NE7CMD_READ | NE7CMD_SK | mfm | NE7CMD_MT,
1009 		    head << 2 | fd->fdsu,	/* head & unit */
1010 		    fd->track,			/* track */
1011 		    head,			/* head */
1012 		    sec,			/* sector + 1 */
1013 		    fd->ft->secsize,		/* sector size */
1014 		    fd->ft->sectrac,		/* sectors/track */
1015 		    fd->ft->gap,		/* gap size */
1016 		    fd->ft->datalen,		/* data length */
1017 		    0))
1018 			return (1);
1019 		break;
1020 	case BIO_WRITE:
1021 		retry_line = __LINE__;
1022 		if (fdc_cmd(fdc, 9,
1023 		    NE7CMD_WRITE | mfm | NE7CMD_MT,
1024 		    head << 2 | fd->fdsu,	/* head & unit */
1025 		    fd->track,			/* track */
1026 		    head,			/* head */
1027 		    sec,			/* sector + 1 */
1028 		    fd->ft->secsize,		/* sector size */
1029 		    fd->ft->sectrac,		/* sectors/track */
1030 		    fd->ft->gap,		/* gap size */
1031 		    fd->ft->datalen,		/* data length */
1032 		    0))
1033 			return (1);
1034 		break;
1035 	default:
1036 		KASSERT(0 == 1, ("Wrong bio_cmd %x\n", bp->bio_cmd));
1037 	}
1038 
1039 	/* Wait for interrupt */
1040 	i = tsleep(fdc, PRIBIO, "fddata", hz);
1041 
1042 	/* PIO if the read looks good */
1043 	if (i == 0 && (fdc->flags & FDC_NODMA) && (bp->bio_cmd == BIO_READ))
1044 		fdc_pio(fdc);
1045 
1046 	/* Finish DMA */
1047 	if (fd->flags & FD_ISADMA) {
1048 		isa_dmadone(
1049 		    bp->bio_cmd == BIO_READ ? ISADMA_READ : ISADMA_WRITE,
1050 		    fd->fd_ioptr, fd->fd_iosize, fdc->dmachan);
1051 		mtx_lock(&fdc->fdc_mtx);
1052 		fd->flags &= ~FD_ISADMA;
1053 		mtx_unlock(&fdc->fdc_mtx);
1054 	}
1055 
1056 	if (i != 0) {
1057 		/*
1058 		 * Timeout.
1059 		 *
1060 		 * Due to IBM's brain-dead design, the FDC has a faked ready
1061 		 * signal, hardwired to ready == true. Thus, any command
1062 		 * issued if there's no diskette in the drive will _never_
1063 		 * complete, and must be aborted by resetting the FDC.
1064 		 * Many thanks, Big Blue!
1065 		 */
1066 		retry_line = __LINE__;
1067 		fdc->flags |= FDC_NEEDS_RESET;
1068 		return (1);
1069 	}
1070 
1071 	retry_line = __LINE__;
1072 	if (fdc_read_status(fdc))
1073 		return (1);
1074 
1075 	if (debugflags & 0x10)
1076 		printf("  -> %x %x %x %x\n",
1077 		    fdc->status[0], fdc->status[1],
1078 		    fdc->status[2], fdc->status[3]);
1079 
1080 	st0 = fdc->status[0] & NE7_ST0_IC;
1081 	if (st0 != 0) {
1082 		retry_line = __LINE__;
1083 		if (st0 == NE7_ST0_IC_AT && fdc->status[1] & NE7_ST1_OR) {
1084 			/*
1085 			 * DMA overrun. Someone hogged the bus and
1086 			 * didn't release it in time for the next
1087 			 * FDC transfer.
1088 			 */
1089 			return (1);
1090 		}
1091 		retry_line = __LINE__;
1092 		if(st0 == NE7_ST0_IC_IV) {
1093 			fdc->flags |= FDC_NEEDS_RESET;
1094 			return (1);
1095 		}
1096 		retry_line = __LINE__;
1097 		if(st0 == NE7_ST0_IC_AT && fdc->status[2] & NE7_ST2_WC) {
1098 			need_recal |= (1 << fd->fdsu);
1099 			return (1);
1100 		}
1101 		if (debugflags & 0x20) {
1102 			printf("status %02x %02x %02x %02x %02x %02x\n",
1103 			    fdc->status[0], fdc->status[1], fdc->status[2],
1104 			    fdc->status[3], fdc->status[4], fdc->status[5]);
1105 		}
1106 		retry_line = __LINE__;
1107 		if (fd->options & FDOPT_NOERROR)
1108 			override_error = 1;
1109 		else
1110 			return (1);
1111 	}
1112 	/* All OK */
1113 	switch(bp->bio_cmd) {
1114 	case BIO_RDID:
1115 		/* copy out ID field contents */
1116 		idp = (struct fdc_readid *)bp->bio_data;
1117 		idp->cyl = fdc->status[3];
1118 		idp->head = fdc->status[4];
1119 		idp->sec = fdc->status[5];
1120 		idp->secshift = fdc->status[6];
1121 		if (debugflags & 0x40)
1122 			printf("c %d h %d s %d z %d\n",
1123 			    idp->cyl, idp->head, idp->sec, idp->secshift);
1124 		break;
1125 	case BIO_READ:
1126 	case BIO_WRITE:
1127 		bp->bio_pblkno += nsect;
1128 		bp->bio_resid -= fd->fd_iosize;
1129 		bp->bio_completed += fd->fd_iosize;
1130 		fd->fd_ioptr += fd->fd_iosize;
1131 		if (override_error) {
1132 			if ((debugflags & 4))
1133 				printf("FDOPT_NOERROR: returning bad data\n");
1134 		} else {
1135 			/* Since we managed to get something done,
1136 			 * reset the retry */
1137 			fdc->retry = 0;
1138 			if (bp->bio_resid > 0)
1139 				return (0);
1140 		}
1141 		break;
1142 	case BIO_FMT:
1143 		break;
1144 	}
1145 	return (fdc_biodone(fdc, 0));
1146 }
1147 
1148 static void
fdc_thread(void * arg)1149 fdc_thread(void *arg)
1150 {
1151 	struct fdc_data *fdc;
1152 
1153 	fdc = arg;
1154 	int i;
1155 
1156 	mtx_lock(&fdc->fdc_mtx);
1157 	fdc->flags |= FDC_KTHREAD_ALIVE;
1158 	while ((fdc->flags & FDC_KTHREAD_EXIT) == 0) {
1159 		mtx_unlock(&fdc->fdc_mtx);
1160 		i = fdc_worker(fdc);
1161 		if (i && debugflags & 0x20) {
1162 			if (fdc->bp != NULL)
1163 				g_print_bio("", fdc->bp, "");
1164 			printf("Retry line %d\n", retry_line);
1165 		}
1166 		fdc->retry += i;
1167 		mtx_lock(&fdc->fdc_mtx);
1168 	}
1169 	fdc->flags &= ~(FDC_KTHREAD_EXIT | FDC_KTHREAD_ALIVE);
1170 	mtx_unlock(&fdc->fdc_mtx);
1171 
1172 	kproc_exit(0);
1173 }
1174 
1175 /*
1176  * Enqueue a request.
1177  */
1178 static void
fd_enqueue(struct fd_data * fd,struct bio * bp)1179 fd_enqueue(struct fd_data *fd, struct bio *bp)
1180 {
1181 	struct fdc_data *fdc;
1182 	int call;
1183 
1184 	call = 0;
1185 	fdc = fd->fdc;
1186 	mtx_lock(&fdc->fdc_mtx);
1187 	/* If we go from idle, cancel motor turnoff */
1188 	if (fd->fd_iocount++ == 0)
1189 		callout_stop(&fd->toffhandle);
1190 	if (fd->flags & FD_MOTOR) {
1191 		/* The motor is on, send it directly to the controller */
1192 		bioq_disksort(&fdc->head, bp);
1193 		wakeup(&fdc->head);
1194 	} else {
1195 		/* Queue it on the drive until the motor has started */
1196 		bioq_insert_tail(&fd->fd_bq, bp);
1197 		if (!(fd->flags & FD_MOTORWAIT))
1198 			fd_motor(fd, 1);
1199 	}
1200 	mtx_unlock(&fdc->fdc_mtx);
1201 }
1202 
1203 /*
1204  * Try to find out if we have a disk in the drive.
1205  */
1206 static int
fd_probe_disk(struct fd_data * fd,int * recal)1207 fd_probe_disk(struct fd_data *fd, int *recal)
1208 {
1209 	struct fdc_data *fdc;
1210 	int st0, st3, cyl;
1211 	int oopts, ret;
1212 
1213 	fdc = fd->fdc;
1214 	oopts = fd->options;
1215 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1216 	ret = 1;
1217 
1218 	/*
1219 	 * First recal, then seek to cyl#1, this clears the old condition on
1220 	 * the disk change line so we can examine it for current status.
1221 	 */
1222 	if (debugflags & 0x40)
1223 		printf("New disk in probe\n");
1224 	mtx_lock(&fdc->fdc_mtx);
1225 	fd->flags |= FD_NEWDISK;
1226 	mtx_unlock(&fdc->fdc_mtx);
1227 	if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fd->fdsu, 0))
1228 		goto done;
1229 	tsleep(fdc, PRIBIO, "fdrecal", hz);
1230 	if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1231 		goto done;	/* XXX */
1232 	if ((st0 & 0xc0) || cyl != 0)
1233 		goto done;
1234 
1235 	/* Seek to track 1 */
1236 	if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fd->fdsu, 1, 0))
1237 		goto done;
1238 	tsleep(fdc, PRIBIO, "fdseek", hz);
1239 	if (fdc_sense_int(fdc, &st0, &cyl) == FD_NOT_VALID)
1240 		goto done;	/* XXX */
1241 	*recal |= (1 << fd->fdsu);
1242 	if (fdin_rd(fdc) & FDI_DCHG) {
1243 		if (debugflags & 0x40)
1244 			printf("Empty in probe\n");
1245 		mtx_lock(&fdc->fdc_mtx);
1246 		fd->flags |= FD_EMPTY;
1247 		mtx_unlock(&fdc->fdc_mtx);
1248 	} else {
1249 		if (fdc_sense_drive(fdc, &st3) != 0)
1250 			goto done;
1251 		if (debugflags & 0x40)
1252 			printf("Got disk in probe\n");
1253 		mtx_lock(&fdc->fdc_mtx);
1254 		fd->flags &= ~FD_EMPTY;
1255 		if (st3 & NE7_ST3_WP)
1256 			fd->flags |= FD_WP;
1257 		else
1258 			fd->flags &= ~FD_WP;
1259 		mtx_unlock(&fdc->fdc_mtx);
1260 	}
1261 	ret = 0;
1262 
1263 done:
1264 	fd->options = oopts;
1265 	return (ret);
1266 }
1267 
1268 static int
fdmisccmd(struct fd_data * fd,u_int cmd,void * data)1269 fdmisccmd(struct fd_data *fd, u_int cmd, void *data)
1270 {
1271 	struct bio *bp;
1272 	struct fd_formb *finfo;
1273 	struct fdc_readid *idfield;
1274 	int error;
1275 
1276 	bp = malloc(sizeof(struct bio), M_TEMP, M_WAITOK | M_ZERO);
1277 
1278 	/*
1279 	 * Set up a bio request for fdstrategy().  bio_offset is faked
1280 	 * so that fdstrategy() will seek to the requested
1281 	 * cylinder, and use the desired head.
1282 	 */
1283 	bp->bio_cmd = cmd;
1284 	if (cmd == BIO_FMT) {
1285 		finfo = (struct fd_formb *)data;
1286 		bp->bio_pblkno =
1287 		    (finfo->cyl * fd->ft->heads + finfo->head) *
1288 		    fd->ft->sectrac;
1289 		bp->bio_length = sizeof *finfo;
1290 	} else if (cmd == BIO_RDID) {
1291 		idfield = (struct fdc_readid *)data;
1292 		bp->bio_pblkno =
1293 		    (idfield->cyl * fd->ft->heads + idfield->head) *
1294 		    fd->ft->sectrac;
1295 		bp->bio_length = sizeof(struct fdc_readid);
1296 	} else if (cmd == BIO_PROBE) {
1297 		/* nothing */
1298 	} else
1299 		panic("wrong cmd in fdmisccmd()");
1300 	bp->bio_offset = bp->bio_pblkno * fd->sectorsize;
1301 	bp->bio_data = data;
1302 	bp->bio_driver1 = fd;
1303 	bp->bio_flags = 0;
1304 
1305 	fd_enqueue(fd, bp);
1306 
1307 	do {
1308 		tsleep(bp, PRIBIO, "fdwait", hz);
1309 	} while (!(bp->bio_flags & BIO_DONE));
1310 	error = bp->bio_error;
1311 
1312 	free(bp, M_TEMP);
1313 	return (error);
1314 }
1315 
1316 /*
1317  * Try figuring out the density of the media present in our device.
1318  */
1319 static int
fdautoselect(struct fd_data * fd)1320 fdautoselect(struct fd_data *fd)
1321 {
1322 	struct fd_type *fdtp;
1323 	struct fdc_readid id;
1324 	int oopts, rv;
1325 
1326 	if (!(fd->ft->flags & FL_AUTO))
1327 		return (0);
1328 
1329 	fdtp = fd_native_types[fd->type];
1330 	fdsettype(fd, fdtp);
1331 	if (!(fd->ft->flags & FL_AUTO))
1332 		return (0);
1333 
1334 	/*
1335 	 * Try reading sector ID fields, first at cylinder 0, head 0,
1336 	 * then at cylinder 2, head N.  We don't probe cylinder 1,
1337 	 * since for 5.25in DD media in a HD drive, there are no data
1338 	 * to read (2 step pulses per media cylinder required).  For
1339 	 * two-sided media, the second probe always goes to head 1, so
1340 	 * we can tell them apart from single-sided media.  As a
1341 	 * side-effect this means that single-sided media should be
1342 	 * mentioned in the search list after two-sided media of an
1343 	 * otherwise identical density.  Media with a different number
1344 	 * of sectors per track but otherwise identical parameters
1345 	 * cannot be distinguished at all.
1346 	 *
1347 	 * If we successfully read an ID field on both cylinders where
1348 	 * the recorded values match our expectation, we are done.
1349 	 * Otherwise, we try the next density entry from the table.
1350 	 *
1351 	 * Stepping to cylinder 2 has the side-effect of clearing the
1352 	 * unit attention bit.
1353 	 */
1354 	oopts = fd->options;
1355 	fd->options |= FDOPT_NOERRLOG | FDOPT_NORETRY;
1356 	for (; fdtp->heads; fdtp++) {
1357 		fdsettype(fd, fdtp);
1358 
1359 		id.cyl = id.head = 0;
1360 		rv = fdmisccmd(fd, BIO_RDID, &id);
1361 		if (rv != 0)
1362 			continue;
1363 		if (id.cyl != 0 || id.head != 0 || id.secshift != fdtp->secsize)
1364 			continue;
1365 		id.cyl = 2;
1366 		id.head = fd->ft->heads - 1;
1367 		rv = fdmisccmd(fd, BIO_RDID, &id);
1368 		if (id.cyl != 2 || id.head != fdtp->heads - 1 ||
1369 		    id.secshift != fdtp->secsize)
1370 			continue;
1371 		if (rv == 0)
1372 			break;
1373 	}
1374 
1375 	fd->options = oopts;
1376 	if (fdtp->heads == 0) {
1377 		if (debugflags & 0x40)
1378 			device_printf(fd->dev, "autoselection failed\n");
1379 		fdsettype(fd, fd_native_types[fd->type]);
1380 		return (-1);
1381 	} else {
1382 		if (debugflags & 0x40) {
1383 			device_printf(fd->dev,
1384 			    "autoselected %d KB medium\n",
1385 			    fd->ft->size / 2);
1386 			fdprinttype(fd->ft);
1387 		}
1388 		return (0);
1389 	}
1390 }
1391 
1392 /*
1393  * GEOM class implementation
1394  */
1395 
1396 static g_access_t	fd_access;
1397 static g_start_t	fd_start;
1398 static g_ioctl_t	fd_ioctl;
1399 
1400 struct g_class g_fd_class = {
1401 	.name =		"FD",
1402 	.version =	G_VERSION,
1403 	.start =	fd_start,
1404 	.access =	fd_access,
1405 	.ioctl =	fd_ioctl,
1406 };
1407 
1408 static int
fd_access(struct g_provider * pp,int r,int w,int e)1409 fd_access(struct g_provider *pp, int r, int w, int e)
1410 {
1411 	struct fd_data *fd;
1412 	struct fdc_data *fdc;
1413 	int ar, aw, ae;
1414 	int busy;
1415 
1416 	fd = pp->geom->softc;
1417 	fdc = fd->fdc;
1418 
1419 	/*
1420 	 * If our provider is withering, we can only get negative requests
1421 	 * and we don't want to even see them
1422 	 */
1423 	if (pp->flags & G_PF_WITHER)
1424 		return (0);
1425 
1426 	ar = r + pp->acr;
1427 	aw = w + pp->acw;
1428 	ae = e + pp->ace;
1429 
1430 	if (ar == 0 && aw == 0 && ae == 0) {
1431 		fd->options &= ~(FDOPT_NORETRY | FDOPT_NOERRLOG | FDOPT_NOERROR);
1432 		device_unbusy(fd->dev);
1433 		return (0);
1434 	}
1435 
1436 	busy = 0;
1437 	if (pp->acr == 0 && pp->acw == 0 && pp->ace == 0) {
1438 		if (fdmisccmd(fd, BIO_PROBE, NULL))
1439 			return (ENXIO);
1440 		if (fd->flags & FD_EMPTY)
1441 			return (ENXIO);
1442 		if (fd->flags & FD_NEWDISK) {
1443 			if (fdautoselect(fd) != 0 &&
1444 			    (device_get_flags(fd->dev) & FD_NO_CHLINE)) {
1445 				mtx_lock(&fdc->fdc_mtx);
1446 				fd->flags |= FD_EMPTY;
1447 				mtx_unlock(&fdc->fdc_mtx);
1448 				return (ENXIO);
1449 			}
1450 			mtx_lock(&fdc->fdc_mtx);
1451 			fd->flags &= ~FD_NEWDISK;
1452 			mtx_unlock(&fdc->fdc_mtx);
1453 		}
1454 		device_busy(fd->dev);
1455 		busy = 1;
1456 	}
1457 
1458 	if (w > 0 && (fd->flags & FD_WP)) {
1459 		if (busy)
1460 			device_unbusy(fd->dev);
1461 		return (EROFS);
1462 	}
1463 
1464 	pp->sectorsize = fd->sectorsize;
1465 	pp->stripesize = fd->ft->heads * fd->ft->sectrac * fd->sectorsize;
1466 	pp->mediasize = pp->stripesize * fd->ft->tracks;
1467 	return (0);
1468 }
1469 
1470 static void
fd_start(struct bio * bp)1471 fd_start(struct bio *bp)
1472 {
1473  	struct fdc_data *	fdc;
1474  	struct fd_data *	fd;
1475 
1476 	fd = bp->bio_to->geom->softc;
1477 	fdc = fd->fdc;
1478 	bp->bio_driver1 = fd;
1479 	if (bp->bio_cmd == BIO_GETATTR) {
1480 		if (g_handleattr_int(bp, "GEOM::fwsectors", fd->ft->sectrac))
1481 			return;
1482 		if (g_handleattr_int(bp, "GEOM::fwheads", fd->ft->heads))
1483 			return;
1484 		g_io_deliver(bp, ENOIOCTL);
1485 		return;
1486 	}
1487 	if (!(bp->bio_cmd == BIO_READ || bp->bio_cmd == BIO_WRITE)) {
1488 		g_io_deliver(bp, EOPNOTSUPP);
1489 		return;
1490 	}
1491 	bp->bio_pblkno = bp->bio_offset / fd->sectorsize;
1492 	bp->bio_resid = bp->bio_length;
1493 	fd_enqueue(fd, bp);
1494 	return;
1495 }
1496 
1497 static int
fd_ioctl(struct g_provider * pp,u_long cmd,void * data,int fflag,struct thread * td)1498 fd_ioctl(struct g_provider *pp, u_long cmd, void *data, int fflag, struct thread *td)
1499 {
1500 	struct fd_data *fd;
1501 	struct fdc_status *fsp;
1502 	struct fdc_readid *rid;
1503 	int error;
1504 
1505 	fd = pp->geom->softc;
1506 
1507 	switch (cmd) {
1508 	case FD_GTYPE:                  /* get drive type */
1509 		*(struct fd_type *)data = *fd->ft;
1510 		return (0);
1511 
1512 	case FD_STYPE:                  /* set drive type */
1513 		/*
1514 		 * Allow setting drive type temporarily iff
1515 		 * currently unset.  Used for fdformat so any
1516 		 * user can set it, and then start formatting.
1517 		 */
1518 		fd->fts = *(struct fd_type *)data;
1519 		if (fd->fts.sectrac) {
1520 			/* XXX: check for rubbish */
1521 			fdsettype(fd, &fd->fts);
1522 		} else {
1523 			fdsettype(fd, fd_native_types[fd->type]);
1524 		}
1525 		if (debugflags & 0x40)
1526 			fdprinttype(fd->ft);
1527 		return (0);
1528 
1529 	case FD_GOPTS:			/* get drive options */
1530 		*(int *)data = fd->options;
1531 		return (0);
1532 
1533 	case FD_SOPTS:			/* set drive options */
1534 		fd->options = *(int *)data;
1535 		return (0);
1536 
1537 	case FD_CLRERR:
1538 		error = priv_check(td, PRIV_DRIVER);
1539 		if (error)
1540 			return (error);
1541 		fd->fdc->fdc_errs = 0;
1542 		return (0);
1543 
1544 	case FD_GSTAT:
1545 		fsp = (struct fdc_status *)data;
1546 		if ((fd->fdc->flags & FDC_STAT_VALID) == 0)
1547 			return (EINVAL);
1548 		memcpy(fsp->status, fd->fdc->status, 7 * sizeof(u_int));
1549 		return (0);
1550 
1551 	case FD_GDTYPE:
1552 		*(enum fd_drivetype *)data = fd->type;
1553 		return (0);
1554 
1555 	case FD_FORM:
1556 		if (!(fflag & FWRITE))
1557 			return (EPERM);
1558 		if (((struct fd_formb *)data)->format_version !=
1559 		    FD_FORMAT_VERSION)
1560 			return (EINVAL); /* wrong version of formatting prog */
1561 		error = fdmisccmd(fd, BIO_FMT, data);
1562 		mtx_lock(&fd->fdc->fdc_mtx);
1563 		fd->flags |= FD_NEWDISK;
1564 		mtx_unlock(&fd->fdc->fdc_mtx);
1565 		break;
1566 
1567 	case FD_READID:
1568 		rid = (struct fdc_readid *)data;
1569 		if (rid->cyl > 85 || rid->head > 1)
1570 			return (EINVAL);
1571 		error = fdmisccmd(fd, BIO_RDID, data);
1572 		break;
1573 
1574 	case FIONBIO:
1575 	case FIOASYNC:
1576 		/* For backwards compat with old fd*(8) tools */
1577 		error = 0;
1578 		break;
1579 
1580 	default:
1581 		if (debugflags & 0x80)
1582 			printf("Unknown ioctl %lx\n", cmd);
1583 		error = ENOIOCTL;
1584 		break;
1585 	}
1586 	return (error);
1587 };
1588 
1589 
1590 
1591 /*
1592  * Configuration/initialization stuff, per controller.
1593  */
1594 
1595 devclass_t fdc_devclass;
1596 static devclass_t fd_devclass;
1597 
1598 struct fdc_ivars {
1599 	int	fdunit;
1600 	int	fdtype;
1601 };
1602 
1603 void
fdc_release_resources(struct fdc_data * fdc)1604 fdc_release_resources(struct fdc_data *fdc)
1605 {
1606 	device_t dev;
1607 	struct resource *last;
1608 	int i;
1609 
1610 	dev = fdc->fdc_dev;
1611 	if (fdc->fdc_intr)
1612 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1613 	fdc->fdc_intr = NULL;
1614 	if (fdc->res_irq != NULL)
1615 		bus_release_resource(dev, SYS_RES_IRQ, fdc->rid_irq,
1616 		    fdc->res_irq);
1617 	fdc->res_irq = NULL;
1618 	last = NULL;
1619 	for (i = 0; i < FDC_MAXREG; i++) {
1620 		if (fdc->resio[i] != NULL && fdc->resio[i] != last) {
1621 			bus_release_resource(dev, SYS_RES_IOPORT,
1622 			    fdc->ridio[i], fdc->resio[i]);
1623 			last = fdc->resio[i];
1624 			fdc->resio[i] = NULL;
1625 		}
1626 	}
1627 	if (fdc->res_drq != NULL)
1628 		bus_release_resource(dev, SYS_RES_DRQ, fdc->rid_drq,
1629 		    fdc->res_drq);
1630 	fdc->res_drq = NULL;
1631 }
1632 
1633 int
fdc_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)1634 fdc_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1635 {
1636 	struct fdc_ivars *ivars = device_get_ivars(child);
1637 
1638 	switch (which) {
1639 	case FDC_IVAR_FDUNIT:
1640 		*result = ivars->fdunit;
1641 		break;
1642 	case FDC_IVAR_FDTYPE:
1643 		*result = ivars->fdtype;
1644 		break;
1645 	default:
1646 		return (ENOENT);
1647 	}
1648 	return (0);
1649 }
1650 
1651 int
fdc_write_ivar(device_t dev,device_t child,int which,uintptr_t value)1652 fdc_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1653 {
1654 	struct fdc_ivars *ivars = device_get_ivars(child);
1655 
1656 	switch (which) {
1657 	case FDC_IVAR_FDUNIT:
1658 		ivars->fdunit = value;
1659 		break;
1660 	case FDC_IVAR_FDTYPE:
1661 		ivars->fdtype = value;
1662 		break;
1663 	default:
1664 		return (ENOENT);
1665 	}
1666 	return (0);
1667 }
1668 
1669 int
fdc_initial_reset(device_t dev,struct fdc_data * fdc)1670 fdc_initial_reset(device_t dev, struct fdc_data *fdc)
1671 {
1672 	int ic_type, part_id;
1673 
1674 	/*
1675 	 * A status value of 0xff is very unlikely, but not theoretically
1676 	 * impossible, but it is far more likely to indicate an empty bus.
1677 	 */
1678 	if (fdsts_rd(fdc) == 0xff)
1679 		return (ENXIO);
1680 
1681 	/*
1682 	 * Assert a reset to the floppy controller and check that the status
1683 	 * register goes to zero.
1684 	 */
1685 	fdout_wr(fdc, 0);
1686 	fdout_wr(fdc, 0);
1687 	if (fdsts_rd(fdc) != 0)
1688 		return (ENXIO);
1689 
1690 	/*
1691 	 * Clear the reset and see it come ready.
1692 	 */
1693 	fdout_wr(fdc, FDO_FRST);
1694 	DELAY(100);
1695 	if (fdsts_rd(fdc) != 0x80)
1696 		return (ENXIO);
1697 
1698 	/* Then, see if it can handle a command. */
1699 	if (fdc_cmd(fdc, 3, NE7CMD_SPECIFY, NE7_SPEC_1(6, 240),
1700 	    NE7_SPEC_2(31, 0), 0))
1701 		return (ENXIO);
1702 
1703 	/*
1704 	 * Try to identify the chip.
1705 	 *
1706 	 * The i8272 datasheet documents that unknown commands
1707 	 * will return ST0 as 0x80.  The i8272 is supposedly identical
1708 	 * to the NEC765.
1709 	 * The i82077SL datasheet says 0x90 for the VERSION command,
1710 	 * and several "superio" chips emulate this.
1711 	 */
1712 	if (fdc_cmd(fdc, 1, NE7CMD_VERSION, 1, &ic_type))
1713 		return (ENXIO);
1714 	if (fdc_cmd(fdc, 1, 0x18, 1, &part_id))
1715 		return (ENXIO);
1716 	if (bootverbose)
1717 		device_printf(dev,
1718 		    "ic_type %02x part_id %02x\n", ic_type, part_id);
1719 	switch (ic_type & 0xff) {
1720 	case 0x80:
1721 		device_set_desc(dev, "NEC 765 or clone");
1722 		fdc->fdct = FDC_NE765;
1723 		break;
1724 	case 0x81:
1725 	case 0x90:
1726 		device_set_desc(dev,
1727 		    "Enhanced floppy controller");
1728 		fdc->fdct = FDC_ENHANCED;
1729 		break;
1730 	default:
1731 		device_set_desc(dev, "Generic floppy controller");
1732 		fdc->fdct = FDC_UNKNOWN;
1733 		break;
1734 	}
1735 	return (0);
1736 }
1737 
1738 int
fdc_detach(device_t dev)1739 fdc_detach(device_t dev)
1740 {
1741 	struct	fdc_data *fdc;
1742 	int	error;
1743 
1744 	fdc = device_get_softc(dev);
1745 
1746 	/* have our children detached first */
1747 	if ((error = bus_generic_detach(dev)))
1748 		return (error);
1749 
1750 	if (fdc->fdc_intr)
1751 		bus_teardown_intr(dev, fdc->res_irq, fdc->fdc_intr);
1752 	fdc->fdc_intr = NULL;
1753 
1754 	/* kill worker thread */
1755 	mtx_lock(&fdc->fdc_mtx);
1756 	fdc->flags |= FDC_KTHREAD_EXIT;
1757 	wakeup(&fdc->head);
1758 	while ((fdc->flags & FDC_KTHREAD_ALIVE) != 0)
1759 		msleep(fdc->fdc_thread, &fdc->fdc_mtx, PRIBIO, "fdcdet", 0);
1760 	mtx_unlock(&fdc->fdc_mtx);
1761 
1762 	/* reset controller, turn motor off */
1763 	fdout_wr(fdc, 0);
1764 
1765 	if (!(fdc->flags & FDC_NODMA))
1766 		isa_dma_release(fdc->dmachan);
1767 	fdc_release_resources(fdc);
1768 	mtx_destroy(&fdc->fdc_mtx);
1769 	return (0);
1770 }
1771 
1772 /*
1773  * Add a child device to the fdc controller.  It will then be probed etc.
1774  */
1775 device_t
fdc_add_child(device_t dev,const char * name,int unit)1776 fdc_add_child(device_t dev, const char *name, int unit)
1777 {
1778 	struct fdc_ivars *ivar;
1779 	device_t child;
1780 
1781 	ivar = malloc(sizeof *ivar, M_DEVBUF /* XXX */, M_NOWAIT | M_ZERO);
1782 	if (ivar == NULL)
1783 		return (NULL);
1784 	child = device_add_child(dev, name, unit);
1785 	if (child == NULL) {
1786 		free(ivar, M_DEVBUF);
1787 		return (NULL);
1788 	}
1789 	device_set_ivars(child, ivar);
1790 	ivar->fdunit = unit;
1791 	ivar->fdtype = FDT_NONE;
1792 	if (resource_disabled(name, unit))
1793 		device_disable(child);
1794 	return (child);
1795 }
1796 
1797 int
fdc_attach(device_t dev)1798 fdc_attach(device_t dev)
1799 {
1800 	struct	fdc_data *fdc;
1801 	int	error;
1802 
1803 	fdc = device_get_softc(dev);
1804 	fdc->fdc_dev = dev;
1805 	error = fdc_initial_reset(dev, fdc);
1806 	if (error) {
1807 		device_printf(dev, "does not respond\n");
1808 		return (error);
1809 	}
1810 	error = bus_setup_intr(dev, fdc->res_irq,
1811 	    INTR_TYPE_BIO | INTR_ENTROPY |
1812 	    ((fdc->flags & FDC_NOFAST) ? INTR_MPSAFE : 0),
1813             ((fdc->flags & FDC_NOFAST) ? NULL : fdc_intr_fast),
1814 	    ((fdc->flags & FDC_NOFAST) ? fdc_intr : NULL),
1815 			       fdc, &fdc->fdc_intr);
1816 	if (error) {
1817 		device_printf(dev, "cannot setup interrupt\n");
1818 		return (error);
1819 	}
1820 	if (!(fdc->flags & FDC_NODMA)) {
1821 		error = isa_dma_acquire(fdc->dmachan);
1822 		if (!error) {
1823 			error = isa_dma_init(fdc->dmachan,
1824 			    MAX_BYTES_PER_CYL, M_WAITOK);
1825 			if (error)
1826 				isa_dma_release(fdc->dmachan);
1827 		}
1828 		if (error)
1829 			return (error);
1830 	}
1831 	fdc->fdcu = device_get_unit(dev);
1832 	fdc->flags |= FDC_NEEDS_RESET;
1833 
1834 	mtx_init(&fdc->fdc_mtx, "fdc lock", NULL, MTX_DEF);
1835 
1836 	/* reset controller, turn motor off, clear fdout mirror reg */
1837 	fdout_wr(fdc, fdc->fdout = 0);
1838 	bioq_init(&fdc->head);
1839 
1840 	settle = hz / 8;
1841 
1842 	return (0);
1843 }
1844 
1845 void
fdc_start_worker(device_t dev)1846 fdc_start_worker(device_t dev)
1847 {
1848 	struct	fdc_data *fdc;
1849 
1850 	fdc = device_get_softc(dev);
1851 	kproc_create(fdc_thread, fdc, &fdc->fdc_thread, 0, 0,
1852 	    "fdc%d", device_get_unit(dev));
1853 }
1854 
1855 int
fdc_hints_probe(device_t dev)1856 fdc_hints_probe(device_t dev)
1857 {
1858 	const char *name, *dname;
1859 	int i, error, dunit;
1860 
1861 	/*
1862 	 * Probe and attach any children.  We should probably detect
1863 	 * devices from the BIOS unless overridden.
1864 	 */
1865 	name = device_get_nameunit(dev);
1866 	i = 0;
1867 	while ((resource_find_match(&i, &dname, &dunit, "at", name)) == 0) {
1868 		resource_int_value(dname, dunit, "drive", &dunit);
1869 		fdc_add_child(dev, dname, dunit);
1870 	}
1871 
1872 	if ((error = bus_generic_attach(dev)) != 0)
1873 		return (error);
1874 	return (0);
1875 }
1876 
1877 int
fdc_print_child(device_t me,device_t child)1878 fdc_print_child(device_t me, device_t child)
1879 {
1880 	int retval = 0, flags;
1881 
1882 	retval += bus_print_child_header(me, child);
1883 	retval += printf(" on %s drive %d", device_get_nameunit(me),
1884 	       fdc_get_fdunit(child));
1885 	if ((flags = device_get_flags(me)) != 0)
1886 		retval += printf(" flags %#x", flags);
1887 	retval += printf("\n");
1888 
1889 	return (retval);
1890 }
1891 
1892 /*
1893  * Configuration/initialization, per drive.
1894  */
1895 static int
fd_probe(device_t dev)1896 fd_probe(device_t dev)
1897 {
1898 #if defined(__i386__) || defined(__amd64__)
1899 	int	unit;
1900 #endif
1901 	int	i;
1902 	u_int	st0, st3;
1903 	struct	fd_data *fd;
1904 	struct	fdc_data *fdc;
1905 	int	fdsu;
1906 	int	flags, type;
1907 
1908 	fdsu = fdc_get_fdunit(dev);
1909 	fd = device_get_softc(dev);
1910 	fdc = device_get_softc(device_get_parent(dev));
1911 	flags = device_get_flags(dev);
1912 
1913 	fd->dev = dev;
1914 	fd->fdc = fdc;
1915 	fd->fdsu = fdsu;
1916 
1917 	/* Auto-probe if fdinfo is present, but always allow override. */
1918 	type = flags & FD_TYPEMASK;
1919 	if (type == FDT_NONE && (type = fdc_get_fdtype(dev)) != FDT_NONE) {
1920 		fd->type = type;
1921 		goto done;
1922 	} else {
1923 		/* make sure fdautoselect() will be called */
1924 		fd->flags = FD_EMPTY;
1925 		fd->type = type;
1926 	}
1927 
1928 #if defined(__i386__) || defined(__amd64__)
1929 	unit = device_get_unit(dev);
1930 	if (fd->type == FDT_NONE && (unit == 0 || unit == 1)) {
1931 		/* Look up what the BIOS thinks we have. */
1932 		if (unit == 0)
1933 			fd->type = (rtcin(RTC_FDISKETTE) & 0xf0) >> 4;
1934 		else
1935 			fd->type = rtcin(RTC_FDISKETTE) & 0x0f;
1936 		if (fd->type == FDT_288M_1)
1937 			fd->type = FDT_288M;
1938 	}
1939 #endif /* __i386__ || __amd64__ */
1940 	/* is there a unit? */
1941 	if (fd->type == FDT_NONE)
1942 		return (ENXIO);
1943 
1944 	mtx_lock(&fdc->fdc_mtx);
1945 
1946 	/* select it */
1947 	fd_select(fd);
1948 	fd_motor(fd, 1);
1949 	fdc->fd = fd;
1950 	fdc_reset(fdc);		/* XXX reset, then unreset, etc. */
1951 	DELAY(1000000);	/* 1 sec */
1952 
1953 	if ((flags & FD_NO_PROBE) == 0) {
1954 		/* If we're at track 0 first seek inwards. */
1955 		if ((fdc_sense_drive(fdc, &st3) == 0) &&
1956 		    (st3 & NE7_ST3_T0)) {
1957 			/* Seek some steps... */
1958 			if (fdc_cmd(fdc, 3, NE7CMD_SEEK, fdsu, 10, 0) == 0) {
1959 				/* ...wait a moment... */
1960 				DELAY(300000);
1961 				/* make ctrlr happy: */
1962 				fdc_sense_int(fdc, NULL, NULL);
1963 			}
1964 		}
1965 
1966 		for (i = 0; i < 2; i++) {
1967 			/*
1968 			 * we must recalibrate twice, just in case the
1969 			 * heads have been beyond cylinder 76, since
1970 			 * most FDCs still barf when attempting to
1971 			 * recalibrate more than 77 steps
1972 			 */
1973 			/* go back to 0: */
1974 			if (fdc_cmd(fdc, 2, NE7CMD_RECAL, fdsu, 0) == 0) {
1975 				/* a second being enough for full stroke seek*/
1976 				DELAY(i == 0 ? 1000000 : 300000);
1977 
1978 				/* anything responding? */
1979 				if (fdc_sense_int(fdc, &st0, NULL) == 0 &&
1980 				    (st0 & NE7_ST0_EC) == 0)
1981 					break; /* already probed successfully */
1982 			}
1983 		}
1984 	}
1985 
1986 	fd_motor(fd, 0);
1987 	fdc->fd = NULL;
1988 	mtx_unlock(&fdc->fdc_mtx);
1989 
1990 	if ((flags & FD_NO_PROBE) == 0 &&
1991 	    (st0 & NE7_ST0_EC) != 0) /* no track 0 -> no drive present */
1992 		return (ENXIO);
1993 
1994 done:
1995 
1996 	switch (fd->type) {
1997 	case FDT_12M:
1998 		device_set_desc(dev, "1200-KB 5.25\" drive");
1999 		break;
2000 	case FDT_144M:
2001 		device_set_desc(dev, "1440-KB 3.5\" drive");
2002 		break;
2003 	case FDT_288M:
2004 		device_set_desc(dev, "2880-KB 3.5\" drive (in 1440-KB mode)");
2005 		break;
2006 	case FDT_360K:
2007 		device_set_desc(dev, "360-KB 5.25\" drive");
2008 		break;
2009 	case FDT_720K:
2010 		device_set_desc(dev, "720-KB 3.5\" drive");
2011 		break;
2012 	default:
2013 		return (ENXIO);
2014 	}
2015 	fd->track = FD_NO_TRACK;
2016 	fd->fdc = fdc;
2017 	fd->fdsu = fdsu;
2018 	fd->options = 0;
2019 	callout_init_mtx(&fd->toffhandle, &fd->fdc->fdc_mtx, 0);
2020 
2021 	/* initialize densities for subdevices */
2022 	fdsettype(fd, fd_native_types[fd->type]);
2023 	return (0);
2024 }
2025 
2026 /*
2027  * We have to do this in a geom event because GEOM is not running
2028  * when fd_attach() is.
2029  * XXX: move fd_attach after geom like ata/scsi disks
2030  */
2031 static void
fd_attach2(void * arg,int flag)2032 fd_attach2(void *arg, int flag)
2033 {
2034 	struct	fd_data *fd;
2035 
2036 	fd = arg;
2037 
2038 	fd->fd_geom = g_new_geomf(&g_fd_class,
2039 	    "fd%d", device_get_unit(fd->dev));
2040 	fd->fd_provider = g_new_providerf(fd->fd_geom, "%s", fd->fd_geom->name);
2041 	fd->fd_geom->softc = fd;
2042 	g_error_provider(fd->fd_provider, 0);
2043 }
2044 
2045 static int
fd_attach(device_t dev)2046 fd_attach(device_t dev)
2047 {
2048 	struct	fd_data *fd;
2049 
2050 	fd = device_get_softc(dev);
2051 	g_post_event(fd_attach2, fd, M_WAITOK, NULL);
2052 	fd->flags |= FD_EMPTY;
2053 	bioq_init(&fd->fd_bq);
2054 
2055 	return (0);
2056 }
2057 
2058 static void
fd_detach_geom(void * arg,int flag)2059 fd_detach_geom(void *arg, int flag)
2060 {
2061 	struct	fd_data *fd = arg;
2062 
2063 	g_topology_assert();
2064 	g_wither_geom(fd->fd_geom, ENXIO);
2065 }
2066 
2067 static int
fd_detach(device_t dev)2068 fd_detach(device_t dev)
2069 {
2070 	struct	fd_data *fd;
2071 
2072 	fd = device_get_softc(dev);
2073 	g_waitfor_event(fd_detach_geom, fd, M_WAITOK, NULL);
2074 	while (device_get_state(dev) == DS_BUSY)
2075 		tsleep(fd, PZERO, "fdd", hz/10);
2076 	callout_drain(&fd->toffhandle);
2077 
2078 	return (0);
2079 }
2080 
2081 static device_method_t fd_methods[] = {
2082 	/* Device interface */
2083 	DEVMETHOD(device_probe,		fd_probe),
2084 	DEVMETHOD(device_attach,	fd_attach),
2085 	DEVMETHOD(device_detach,	fd_detach),
2086 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
2087 	DEVMETHOD(device_suspend,	bus_generic_suspend), /* XXX */
2088 	DEVMETHOD(device_resume,	bus_generic_resume), /* XXX */
2089 	{ 0, 0 }
2090 };
2091 
2092 static driver_t fd_driver = {
2093 	"fd",
2094 	fd_methods,
2095 	sizeof(struct fd_data)
2096 };
2097 
2098 static int
fdc_modevent(module_t mod,int type,void * data)2099 fdc_modevent(module_t mod, int type, void *data)
2100 {
2101 
2102 	return (g_modevent(NULL, type, &g_fd_class));
2103 }
2104 
2105 DRIVER_MODULE(fd, fdc, fd_driver, fd_devclass, fdc_modevent, 0);
2106