xref: /netbsd-src/sys/dev/isa/fd.c (revision 4264bd36d7529f35ec6adb652e89163ce6cd088b)
1 /*	$NetBSD: fd.c,v 1.117 2022/09/25 17:11:48 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2003, 2008 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1990 The Regents of the University of California.
34  * All rights reserved.
35  *
36  * This code is derived from software contributed to Berkeley by
37  * Don Ahn.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the University nor the names of its contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  *
63  *	@(#)fd.c	7.4 (Berkeley) 5/25/91
64  */
65 
66 /*
67  * Floppy formatting facilities merged from FreeBSD fd.c driver:
68  *	Id: fd.c,v 1.53 1995/03/12 22:40:56 joerg Exp
69  * which carries the same copyright/redistribution notice as shown above with
70  * the addition of the following statement before the "Redistribution and
71  * use ..." clause:
72  *
73  * Copyright (c) 1993, 1994 by
74  *  jc@irbs.UUCP (John Capo)
75  *  vak@zebub.msk.su (Serge Vakulenko)
76  *  ache@astral.msk.su (Andrew A. Chernov)
77  *
78  * Copyright (c) 1993, 1994, 1995 by
79  *  joerg_wunsch@uriah.sax.de (Joerg Wunsch)
80  *  dufault@hda.com (Peter Dufault)
81  */
82 
83 #include <sys/cdefs.h>
84 __KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.117 2022/09/25 17:11:48 thorpej Exp $");
85 
86 #include "opt_ddb.h"
87 
88 /*
89  * XXX This driver should be properly MI'd some day, but this allows us
90  * XXX to eliminate a lot of code duplication for now.
91  */
92 #if !defined(alpha) && !defined(algor) && !defined(atari) && \
93     !defined(bebox) && !defined(evbmips) && !defined(i386) && \
94     !defined(prep) && !defined(sandpoint) && !defined(x86_64) && \
95     !defined(mvmeppc) && !defined(ofppc)
96 #error platform not supported by this driver, yet
97 #endif
98 
99 #include <sys/param.h>
100 #include <sys/systm.h>
101 #include <sys/callout.h>
102 #include <sys/kernel.h>
103 #include <sys/file.h>
104 #include <sys/ioctl.h>
105 #include <sys/device.h>
106 #include <sys/disklabel.h>
107 #include <sys/disk.h>
108 #include <sys/buf.h>
109 #include <sys/bufq.h>
110 #include <sys/kmem.h>
111 #include <sys/uio.h>
112 #include <sys/syslog.h>
113 #include <sys/queue.h>
114 #include <sys/proc.h>
115 #include <sys/fdio.h>
116 #include <sys/conf.h>
117 #include <sys/vnode.h>
118 #include <sys/rndsource.h>
119 
120 #include <prop/proplib.h>
121 
122 #include <dev/cons.h>
123 
124 #include <sys/cpu.h>
125 #include <sys/bus.h>
126 
127 #include "locators.h"
128 
129 #if defined(atari)
130 /*
131  * On the atari, it is configured as fdcisa
132  */
133 #define	FDCCF_DRIVE		FDCISACF_DRIVE
134 #define	FDCCF_DRIVE_DEFAULT	FDCISACF_DRIVE_DEFAULT
135 
136 #define	fd_cd	fdisa_cd
137 #endif /* atari */
138 
139 #include <sys/intr.h>
140 
141 #include <dev/isa/isavar.h>
142 #include <dev/isa/isadmavar.h>
143 
144 #include <dev/isa/fdreg.h>
145 #include <dev/isa/fdcvar.h>
146 
147 #if defined(i386) || defined(x86_64)
148 
149 #include <dev/ic/mc146818reg.h>			/* for NVRAM access */
150 #include <i386/isa/nvram.h>
151 
152 #if defined(i386)
153 #include "mca.h"
154 #if NMCA > 0
155 #include <machine/mca_machdep.h>		/* for MCA_system */
156 #endif
157 #endif
158 
159 #endif /* i386 || x86_64 */
160 
161 #include <dev/isa/fdvar.h>
162 
163 #define FDUNIT(dev)	(minor(dev) / 8)
164 #define FDTYPE(dev)	(minor(dev) % 8)
165 
166 /* (mis)use device use flag to identify format operation */
167 #define B_FORMAT B_DEVPRIVATE
168 
169 /* controller driver configuration */
170 int fdprint(void *, const char *);
171 
172 #if NMCA > 0
173 /* MCA - specific entries */
174 const struct fd_type mca_fd_types[] = {
175 	{ 18,2,36,2,0xff,0x0f,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"    }, /* 1.44MB diskette - XXX try 16ms step rate */
176 	{  9,2,18,2,0xff,0x4f,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette - XXX try 24ms step rate */
177 };
178 #endif /* NMCA > 0 */
179 
180 /* The order of entries in the following table is important -- BEWARE! */
181 
182 #if defined(atari)
183 const struct fd_type fd_types[] = {
184 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
185 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
186 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
187 };
188 #else
189 const struct fd_type fd_types[] = {
190 	{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB"   }, /* 1.44MB diskette */
191 	{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS,0xf6,1, "1.2MB"    }, /* 1.2 MB AT-diskettes */
192 	{  9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS,0xf6,1, "360KB/AT" }, /* 360kB in 1.2MB drive */
193 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS,0xf6,1, "360KB/PC" }, /* 360kB PC diskettes */
194 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB"    }, /* 3.5 inch 720kB diskette */
195 	{  9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS,0xf6,1, "720KB/x"  }, /* 720kB in 1.2MB drive */
196 	{  9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x"  }, /* 360kB in 720kB drive */
197 };
198 #endif /* defined(atari) */
199 
200 void fdcfinishattach(device_t);
201 int fdprobe(device_t, cfdata_t, void *);
202 void fdattach(device_t, device_t, void *);
203 static int fddetach(device_t, int);
204 static int fdcintr1(struct fdc_softc *);
205 static void fdcintrcb(void *);
206 static bool fdcsuspend(device_t, const pmf_qual_t *);
207 static bool fdcresume(device_t, const pmf_qual_t *);
208 
209 extern struct cfdriver fd_cd;
210 
211 #ifdef atari
212 CFATTACH_DECL_NEW(fdisa, sizeof(struct fd_softc),
213     fdprobe, fdattach, fddetach, NULL);
214 #else
215 CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc),
216     fdprobe, fdattach, fddetach, NULL);
217 #endif
218 
219 dev_type_open(fdopen);
220 dev_type_close(fdclose);
221 dev_type_read(fdread);
222 dev_type_write(fdwrite);
223 dev_type_ioctl(fdioctl);
224 dev_type_strategy(fdstrategy);
225 
226 const struct bdevsw fd_bdevsw = {
227 	.d_open = fdopen,
228 	.d_close = fdclose,
229 	.d_strategy = fdstrategy,
230 	.d_ioctl = fdioctl,
231 	.d_dump = nodump,
232 	.d_psize = nosize,
233 	.d_discard = nodiscard,
234 	.d_flag = D_DISK
235 };
236 
237 const struct cdevsw fd_cdevsw = {
238 	.d_open = fdopen,
239 	.d_close = fdclose,
240 	.d_read = fdread,
241 	.d_write = fdwrite,
242 	.d_ioctl = fdioctl,
243 	.d_stop = nostop,
244 	.d_tty = notty,
245 	.d_poll = nopoll,
246 	.d_mmap = nommap,
247 	.d_kqfilter = nokqfilter,
248 	.d_discard = nodiscard,
249 	.d_flag = D_DISK
250 };
251 
252 void fdgetdisklabel(struct fd_softc *);
253 int fd_get_parms(struct fd_softc *);
254 void fdstart(struct fd_softc *);
255 
256 struct dkdriver fddkdriver = {
257 	.d_strategy = fdstrategy,
258 	.d_minphys = minphys
259 };
260 
261 #if defined(i386) || defined(x86_64)
262 const struct fd_type *fd_nvtotype(const char *, int, int);
263 #endif /* i386 || x86_64 */
264 void fd_set_motor(struct fdc_softc *fdc, int reset);
265 void fd_motor_off(void *arg);
266 void fd_motor_on(void *arg);
267 int fdcresult(struct fdc_softc *fdc);
268 void fdcstart(struct fdc_softc *fdc);
269 void fdcstatus(device_t, int, const char *);
270 void fdctimeout(void *arg);
271 void fdcretry(struct fdc_softc *fdc);
272 void fdfinish(struct fd_softc *fd, struct buf *bp);
273 static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
274 int fdformat(dev_t, struct ne7_fd_formb *, struct lwp *);
275 static void fd_set_geometry(struct fd_softc *fd);
276 
277 void	fd_mountroot_hook(device_t);
278 
279 /*
280  * Arguments passed between fdcattach and fdprobe.
281  */
282 struct fdc_attach_args {
283 	int fa_drive;
284 	const struct fd_type *fa_deftype;
285 };
286 
287 /*
288  * Print the location of a disk drive (called just before attaching the
289  * the drive).  If `fdc' is not NULL, the drive was found but was not
290  * in the system config file; print the drive name as well.
291  * Return QUIET (config_find ignores this if the device was configured) to
292  * avoid printing `fdN not configured' messages.
293  */
294 int
fdprint(void * aux,const char * fdc)295 fdprint(void *aux, const char *fdc)
296 {
297 	struct fdc_attach_args *fa = aux;
298 
299 	if (!fdc)
300 		aprint_normal(" drive %d", fa->fa_drive);
301 	return QUIET;
302 }
303 
304 static bool
fdcresume(device_t self,const pmf_qual_t * qual)305 fdcresume(device_t self, const pmf_qual_t *qual)
306 {
307 	struct fdc_softc *fdc = device_private(self);
308 
309 	mutex_enter(&fdc->sc_mtx);
310 	(void)fdcintr1(fdc);
311 	mutex_exit(&fdc->sc_mtx);
312 	return true;
313 }
314 
315 static bool
fdcsuspend(device_t self,const pmf_qual_t * qual)316 fdcsuspend(device_t self, const pmf_qual_t *qual)
317 {
318 	struct fdc_softc *fdc = device_private(self);
319 	int drive;
320 	struct fd_softc *fd;
321 
322 	mutex_enter(&fdc->sc_mtx);
323 	while (fdc->sc_state != DEVIDLE)
324 		cv_wait(&fdc->sc_cv, &fdc->sc_mtx);
325 	for (drive = 0; drive < 4; drive++) {
326 		if ((fd = fdc->sc_fd[drive]) == NULL)
327 			continue;
328 		fd->sc_flags &= ~(FD_MOTOR|FD_MOTOR_WAIT);
329 	}
330 	fd_set_motor(fdc, 0);
331 	mutex_exit(&fdc->sc_mtx);
332 	return true;
333 }
334 
335 void
fdc_childdet(device_t self,device_t child)336 fdc_childdet(device_t self, device_t child)
337 {
338 	struct fdc_softc *fdc = device_private(self);
339 	struct fd_softc *fd = device_private(child);
340 	int drive = fd->sc_drive;
341 
342 	KASSERT(fdc->sc_fd[drive] == fd); /* but the kid is not my son */
343 	fdc->sc_fd[drive] = NULL;
344 }
345 
346 int
fdcdetach(device_t self,int flags)347 fdcdetach(device_t self, int flags)
348 {
349 	int rc;
350 	struct fdc_softc *fdc = device_private(self);
351 
352 	if ((rc = config_detach_children(self, flags)) != 0)
353 		return rc;
354 
355 	pmf_device_deregister(self);
356 
357 	isa_dmamap_destroy(fdc->sc_ic, fdc->sc_drq);
358 	isa_drq_free(fdc->sc_ic, fdc->sc_drq);
359 
360 	callout_destroy(&fdc->sc_intr_ch);
361 	callout_destroy(&fdc->sc_timo_ch);
362 
363 	cv_destroy(&fdc->sc_cv);
364 	mutex_destroy(&fdc->sc_mtx);
365 
366 	return 0;
367 }
368 
369 void
fdcattach(struct fdc_softc * fdc)370 fdcattach(struct fdc_softc *fdc)
371 {
372 	mutex_init(&fdc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
373 	cv_init(&fdc->sc_cv, "fdcwake");
374 	callout_init(&fdc->sc_timo_ch, 0);
375 	callout_init(&fdc->sc_intr_ch, 0);
376 
377 	fdc->sc_state = DEVIDLE;
378 	TAILQ_INIT(&fdc->sc_drives);
379 
380 	fdc->sc_maxiosize = isa_dmamaxsize(fdc->sc_ic, fdc->sc_drq);
381 
382 	if (isa_drq_alloc(fdc->sc_ic, fdc->sc_drq) != 0) {
383 		aprint_normal_dev(fdc->sc_dev, "can't reserve drq %d\n",
384 		    fdc->sc_drq);
385 		return;
386 	}
387 
388 	if (isa_dmamap_create(fdc->sc_ic, fdc->sc_drq, fdc->sc_maxiosize,
389 	    BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
390 		aprint_normal_dev(fdc->sc_dev, "can't set up ISA DMA map\n");
391 		return;
392 	}
393 
394 	config_interrupts(fdc->sc_dev, fdcfinishattach);
395 
396 	if (!pmf_device_register(fdc->sc_dev, fdcsuspend, fdcresume)) {
397 		aprint_error_dev(fdc->sc_dev,
398 		    "cannot set power mgmt handler\n");
399 	}
400 }
401 
402 void
fdcfinishattach(device_t self)403 fdcfinishattach(device_t self)
404 {
405 	struct fdc_softc *fdc = device_private(self);
406 	bus_space_tag_t iot = fdc->sc_iot;
407 	bus_space_handle_t ioh = fdc->sc_ioh;
408 	struct fdc_attach_args fa;
409 
410 	/*
411 	 * Reset the controller to get it into a known state. Not all
412 	 * probes necessarily need do this to discover the controller up
413 	 * front, so don't assume anything.
414 	 */
415 
416 	bus_space_write_1(iot, ioh, fdout, 0);
417 	delay(100);
418 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
419 
420 	/* see if it can handle a command */
421 	if (out_fdc(iot, ioh, NE7CMD_SPECIFY) < 0) {
422 		aprint_normal_dev(fdc->sc_dev, "can't reset controller\n");
423 		return;
424 	}
425 	out_fdc(iot, ioh, 0xdf);
426 	out_fdc(iot, ioh, 2);
427 
428 #if defined(i386) || defined(x86_64)
429 	/*
430 	 * The NVRAM info only tells us about the first two disks on the
431 	 * `primary' floppy controller.
432 	 */
433 	/* XXX device_unit() abuse */
434 	if (device_unit(fdc->sc_dev) == 0) {
435 		int type = mc146818_read(NULL, NVRAM_DISKETTE); /* XXX softc */
436 		fdc->sc_known = 1;
437 		fdc->sc_knownfds[0] = fd_nvtotype(device_xname(fdc->sc_dev),
438 		    type, 0);
439 		if (fdc->sc_knownfds[0] != NULL)
440 			fdc->sc_present |= 1;
441 		fdc->sc_knownfds[1] = fd_nvtotype(device_xname(fdc->sc_dev),
442 		    type, 1);
443 		if (fdc->sc_knownfds[1] != NULL)
444 			fdc->sc_present |= 2;
445 	}
446 #endif /* i386 || x86_64 */
447 
448 	/* physical limit: four drives per controller. */
449 	fdc->sc_state = PROBING;
450 	for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
451 		if (fdc->sc_known) {
452 			if (fdc->sc_present & (1 << fa.fa_drive)) {
453 				fa.fa_deftype = fdc->sc_knownfds[fa.fa_drive];
454 				config_found(fdc->sc_dev, (void *)&fa,
455 				    fdprint, CFARGS_NONE);
456 			}
457 		} else {
458 #if defined(atari)
459 			/*
460 			 * Atari has a different ordening, defaults to 1.44
461 			 */
462 			fa.fa_deftype = &fd_types[2];
463 			 /* Atari also configures ISA fdc(4) as "fdcisa" */
464 			config_found(fdc->sc_dev, &fa, fdprint,
465 			    CFARGS(.iattr = "fdcisa"));
466 #else
467 			/*
468 			 * Default to 1.44MB on Alpha and BeBox.  How do we tell
469 			 * on these platforms?
470 			 */
471 			fa.fa_deftype = &fd_types[0];
472 			config_found(fdc->sc_dev, &fa, fdprint,
473 			    CFARGS(.iattr = "fdc"));
474 #endif
475 		}
476 	}
477 	fdc->sc_state = DEVIDLE;
478 }
479 
480 int
fdprobe(device_t parent,cfdata_t match,void * aux)481 fdprobe(device_t parent, cfdata_t match, void *aux)
482 {
483 	struct fdc_softc *fdc = device_private(parent);
484 	cfdata_t cf = match;
485 	struct fdc_attach_args *fa = aux;
486 	int drive = fa->fa_drive;
487 	bus_space_tag_t iot = fdc->sc_iot;
488 	bus_space_handle_t ioh = fdc->sc_ioh;
489 	int n;
490 
491 	if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
492 	    cf->cf_loc[FDCCF_DRIVE] != drive)
493 		return 0;
494 	/*
495 	 * XXX
496 	 * This is to work around some odd interactions between this driver
497 	 * and SMC Ethernet cards.
498 	 */
499 	if (cf->cf_loc[FDCCF_DRIVE] == FDCCF_DRIVE_DEFAULT && drive >= 2)
500 		return 0;
501 
502 	/* Use PNP information if available */
503 	if (fdc->sc_known)
504 		return 1;
505 
506 	mutex_enter(&fdc->sc_mtx);
507 	/* toss any interrupt status */
508 	for (n = 0; n < 4; n++) {
509 		out_fdc(iot, ioh, NE7CMD_SENSEI);
510 		(void) fdcresult(fdc);
511 	}
512 	/* select drive and turn on motor */
513 	bus_space_write_1(iot, ioh, fdout, drive | FDO_FRST | FDO_MOEN(drive));
514 	/* wait for motor to spin up */
515 	/* XXX check sc_probe */
516 	(void) cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, hz / 4);
517 	out_fdc(iot, ioh, NE7CMD_RECAL);
518 	out_fdc(iot, ioh, drive);
519 	/* wait for recalibrate, up to 2s */
520 	/* XXX check sc_probe */
521 	if (cv_timedwait(&fdc->sc_cv, &fdc->sc_mtx, 2 * hz) != EWOULDBLOCK){
522 #ifdef FD_DEBUG
523 		/* XXX */
524 		printf("fdprobe: got intr\n");
525 #endif
526 	}
527 	out_fdc(iot, ioh, NE7CMD_SENSEI);
528 	n = fdcresult(fdc);
529 #ifdef FD_DEBUG
530 	{
531 		int i;
532 		printf("fdprobe: status");
533 		for (i = 0; i < n; i++)
534 			printf(" %x", fdc->sc_status[i]);
535 		printf("\n");
536 	}
537 #endif
538 	/* turn off motor */
539 	bus_space_write_1(iot, ioh, fdout, FDO_FRST);
540 	mutex_exit(&fdc->sc_mtx);
541 
542 #if defined(bebox)	/* XXX What is this about? --thorpej@NetBSD.org */
543 	if (n != 2 || (fdc->sc_status[1] != 0))
544 		return 0;
545 #else
546 	if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
547 		return 0;
548 #endif /* bebox */
549 
550 	return 1;
551 }
552 
553 /*
554  * Controller is working, and drive responded.  Attach it.
555  */
556 void
fdattach(device_t parent,device_t self,void * aux)557 fdattach(device_t parent, device_t self, void *aux)
558 {
559 	struct fdc_softc *fdc = device_private(parent);
560 	struct fd_softc *fd = device_private(self);
561 	struct fdc_attach_args *fa = aux;
562 	const struct fd_type *type = fa->fa_deftype;
563 	int drive = fa->fa_drive;
564 
565 	fd->sc_dev = self;
566 
567 	callout_init(&fd->sc_motoron_ch, 0);
568 	callout_init(&fd->sc_motoroff_ch, 0);
569 
570 	/* XXX Allow `flags' to override device type? */
571 
572 	if (type)
573 		aprint_normal(": %s, %d cyl, %d head, %d sec\n", type->name,
574 		    type->cyls, type->heads, type->sectrac);
575 	else
576 		aprint_normal(": density unknown\n");
577 
578 	bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
579 	fd->sc_cylin = -1;
580 	fd->sc_drive = drive;
581 	fd->sc_deftype = type;
582 	fdc->sc_fd[drive] = fd;
583 
584 	/*
585 	 * Initialize and attach the disk structure.
586 	 */
587 	disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
588 	disk_attach(&fd->sc_dk);
589 
590 	/*
591 	 * Establish a mountroot hook.
592 	 */
593 	fd->sc_roothook =
594 	    mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
595 
596 	rnd_attach_source(&fd->rnd_source, device_xname(fd->sc_dev),
597 			  RND_TYPE_DISK, RND_FLAG_DEFAULT);
598 
599 	fd_set_geometry(fd);
600 
601 	if (!pmf_device_register(self, NULL, NULL))
602 		aprint_error_dev(self, "cannot set power mgmt handler\n");
603 }
604 
605 static int
fddetach(device_t self,int flags)606 fddetach(device_t self, int flags)
607 {
608 	struct fd_softc *fd = device_private(self);
609 	int bmaj, cmaj, i, mn;
610 
611 	fd_motor_off(fd);
612 
613 	/* locate the major number */
614 	bmaj = bdevsw_lookup_major(&fd_bdevsw);
615 	cmaj = cdevsw_lookup_major(&fd_cdevsw);
616 
617 	/* Nuke the vnodes for any open instances. */
618 	for (i = 0; i < MAXPARTITIONS; i++) {
619 		mn = DISKMINOR(device_unit(self), i);
620 		vdevgone(bmaj, mn, mn, VBLK);
621 		vdevgone(cmaj, mn, mn, VCHR);
622 	}
623 
624 	pmf_device_deregister(self);
625 
626 #if 0 /* XXX need to undo at detach? */
627 	fd_set_geometry(fd);
628 #endif
629 
630 	rnd_detach_source(&fd->rnd_source);
631 
632 	disk_detach(&fd->sc_dk);
633 	disk_destroy(&fd->sc_dk);
634 
635 	/* Kill off any queued buffers. */
636 	bufq_drain(fd->sc_q);
637 
638 	bufq_free(fd->sc_q);
639 
640 	callout_destroy(&fd->sc_motoroff_ch);
641 	callout_destroy(&fd->sc_motoron_ch);
642 
643 	return 0;
644 }
645 
646 #if defined(i386) || defined(x86_64)
647 /*
648  * Translate nvram type into internal data structure.  Return NULL for
649  * none/unknown/unusable.
650  */
651 const struct fd_type *
fd_nvtotype(const char * fdc,int nvraminfo,int drive)652 fd_nvtotype(const char *fdc, int nvraminfo, int drive)
653 {
654 	int type;
655 
656 	type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
657 	switch (type) {
658 	case NVRAM_DISKETTE_NONE:
659 		return NULL;
660 	case NVRAM_DISKETTE_12M:
661 		return &fd_types[1];
662 	case NVRAM_DISKETTE_TYPE5:
663 	case NVRAM_DISKETTE_TYPE6:
664 		/* XXX We really ought to handle 2.88MB format. */
665 	case NVRAM_DISKETTE_144M:
666 #if NMCA > 0
667 		if (MCA_system)
668 			return &mca_fd_types[0];
669 		else
670 #endif /* NMCA > 0 */
671 			return &fd_types[0];
672 	case NVRAM_DISKETTE_360K:
673 		return &fd_types[3];
674 	case NVRAM_DISKETTE_720K:
675 #if NMCA > 0
676 		if (MCA_system)
677 			return &mca_fd_types[1];
678 		else
679 #endif /* NMCA > 0 */
680 			return &fd_types[4];
681 	default:
682 		printf("%s: drive %d: unknown device type 0x%x\n",
683 		    fdc, drive, type);
684 		return NULL;
685 	}
686 }
687 #endif /* i386 || x86_64 */
688 
689 static const struct fd_type *
fd_dev_to_type(struct fd_softc * fd,dev_t dev)690 fd_dev_to_type(struct fd_softc *fd, dev_t dev)
691 {
692 	u_int type = FDTYPE(dev);
693 
694 	if (type > __arraycount(fd_types))
695 		return NULL;
696 	return type ? &fd_types[type - 1] : fd->sc_deftype;
697 }
698 
699 void
fdstrategy(struct buf * bp)700 fdstrategy(struct buf *bp)
701 {
702 	struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
703 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
704 	int sz;
705 
706 	/* Valid unit, controller, and request? */
707 	if (bp->b_blkno < 0 ||
708 	    ((bp->b_bcount % FDC_BSIZE) != 0 &&
709 	     (bp->b_flags & B_FORMAT) == 0)) {
710 		bp->b_error = EINVAL;
711 		goto done;
712 	}
713 
714 	/* If it's a null transfer, return immediately. */
715 	if (bp->b_bcount == 0)
716 		goto done;
717 
718 	sz = howmany(bp->b_bcount, FDC_BSIZE);
719 
720 	if (bp->b_blkno + sz > fd->sc_type->size) {
721 		sz = fd->sc_type->size - bp->b_blkno;
722 		if (sz == 0) {
723 			/* If exactly at end of disk, return EOF. */
724 			goto done;
725 		}
726 		if (sz < 0) {
727 			/* If past end of disk, return EINVAL. */
728 			bp->b_error = EINVAL;
729 			goto done;
730 		}
731 		/* Otherwise, truncate request. */
732 		bp->b_bcount = sz << DEV_BSHIFT;
733 	}
734 
735 	bp->b_rawblkno = bp->b_blkno;
736  	bp->b_cylinder =
737 	    bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
738 
739 #ifdef FD_DEBUG
740 	printf("fdstrategy: b_blkno %llu b_bcount %d blkno %llu cylin %d "
741 	    "sz %d\n", (unsigned long long)bp->b_blkno, bp->b_bcount,
742 	    (unsigned long long)fd->sc_blkno, bp->b_cylinder, sz);
743 #endif
744 
745 	/* Queue transfer on drive, activate drive and controller if idle. */
746 	mutex_enter(&fdc->sc_mtx);
747 	bufq_put(fd->sc_q, bp);
748 	callout_stop(&fd->sc_motoroff_ch);		/* a good idea */
749 	if (fd->sc_active == 0)
750 		fdstart(fd);
751 #ifdef DIAGNOSTIC
752 	else {
753 		if (fdc->sc_state == DEVIDLE) {
754 			printf("fdstrategy: controller inactive\n");
755 			fdcstart(fdc);
756 		}
757 	}
758 #endif
759 	mutex_exit(&fdc->sc_mtx);
760 	return;
761 
762 done:
763 	/* Toss transfer; we're done early. */
764 	bp->b_resid = bp->b_bcount;
765 	biodone(bp);
766 }
767 
768 void
fdstart(struct fd_softc * fd)769 fdstart(struct fd_softc *fd)
770 {
771 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
772 	int active = !TAILQ_EMPTY(&fdc->sc_drives);
773 
774 	KASSERT(mutex_owned(&fdc->sc_mtx));
775 	/* Link into controller queue. */
776 	fd->sc_active = 1;
777 	TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
778 
779 	/* If controller not already active, start it. */
780 	if (!active)
781 		fdcstart(fdc);
782 }
783 
784 void
fdfinish(struct fd_softc * fd,struct buf * bp)785 fdfinish(struct fd_softc *fd, struct buf *bp)
786 {
787 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
788 
789 	/*
790 	 * Move this drive to the end of the queue to give others a `fair'
791 	 * chance.  We only force a switch if N operations are completed while
792 	 * another drive is waiting to be serviced, since there is a long motor
793 	 * startup delay whenever we switch.
794 	 */
795 	(void)bufq_get(fd->sc_q);
796 	if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
797 		fd->sc_ops = 0;
798 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
799 		if (bufq_peek(fd->sc_q) != NULL)
800 			TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
801 		else
802 			fd->sc_active = 0;
803 	}
804 	bp->b_resid = fd->sc_bcount;
805 	fd->sc_skip = 0;
806 
807 	rnd_add_uint32(&fd->rnd_source, bp->b_blkno);
808 
809 	biodone(bp);
810 	/* turn off motor 5s from now */
811 	callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
812 	fdc->sc_state = DEVIDLE;
813 }
814 
815 int
fdread(dev_t dev,struct uio * uio,int flags)816 fdread(dev_t dev, struct uio *uio, int flags)
817 {
818 
819 	return (physio(fdstrategy, NULL, dev, B_READ, minphys, uio));
820 }
821 
822 int
fdwrite(dev_t dev,struct uio * uio,int flags)823 fdwrite(dev_t dev, struct uio *uio, int flags)
824 {
825 
826 	return (physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio));
827 }
828 
829 void
fd_set_motor(struct fdc_softc * fdc,int reset)830 fd_set_motor(struct fdc_softc *fdc, int reset)
831 {
832 	struct fd_softc *fd;
833 	u_char status;
834 	int n;
835 
836 	if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
837 		status = fd->sc_drive;
838 	else
839 		status = 0;
840 	if (!reset)
841 		status |= FDO_FRST | FDO_FDMAEN;
842 	for (n = 0; n < 4; n++)
843 		if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
844 			status |= FDO_MOEN(n);
845 	bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, fdout, status);
846 }
847 
848 void
fd_motor_off(void * arg)849 fd_motor_off(void *arg)
850 {
851 	struct fd_softc *fd = arg;
852 	struct fdc_softc *fdc;
853 
854 	fdc = device_private(device_parent(fd->sc_dev));
855 
856 	mutex_enter(&fdc->sc_mtx);
857 	fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
858 	fd_set_motor(fdc, 0);
859 	mutex_exit(&fdc->sc_mtx);
860 }
861 
862 void
fd_motor_on(void * arg)863 fd_motor_on(void *arg)
864 {
865 	struct fd_softc *fd = arg;
866 	struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
867 
868 	mutex_enter(&fdc->sc_mtx);
869 	fd->sc_flags &= ~FD_MOTOR_WAIT;
870 	if (TAILQ_FIRST(&fdc->sc_drives) == fd && fdc->sc_state == MOTORWAIT)
871 		(void)fdcintr1(fdc);
872 	mutex_exit(&fdc->sc_mtx);
873 }
874 
875 int
fdcresult(struct fdc_softc * fdc)876 fdcresult(struct fdc_softc *fdc)
877 {
878 	bus_space_tag_t iot = fdc->sc_iot;
879 	bus_space_handle_t ioh = fdc->sc_ioh;
880 	u_char i;
881 	u_int j = 100000,
882 	      n = 0;
883 
884 	for (; j; j--) {
885 		i = bus_space_read_1(iot, ioh, fdsts) &
886 		    (NE7_DIO | NE7_RQM | NE7_CB);
887 		if (i == NE7_RQM)
888 			return n;
889 		if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
890 			if (n >= sizeof(fdc->sc_status)) {
891 				log(LOG_ERR, "fdcresult: overrun\n");
892 				return -1;
893 			}
894 			fdc->sc_status[n++] =
895 			    bus_space_read_1(iot, ioh, fddata);
896 		}
897 		delay(10);
898 	}
899 	log(LOG_ERR, "fdcresult: timeout\n");
900 	return -1;
901 }
902 
903 int
out_fdc(bus_space_tag_t iot,bus_space_handle_t ioh,u_char x)904 out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, u_char x)
905 {
906 	u_char i;
907 	u_int j = 100000;
908 
909 	for (; j; j--) {
910 		i = bus_space_read_1(iot, ioh, fdsts) &
911 		    (NE7_DIO | NE7_RQM);
912 		if (i == NE7_RQM) {
913 			bus_space_write_1(iot, ioh, fddata, x);
914 			return 0;
915 		}
916 		delay(10);
917 	}
918 	return -1;
919 }
920 
921 int
fdopen(dev_t dev,int flags,int mode,struct lwp * l)922 fdopen(dev_t dev, int flags, int mode, struct lwp *l)
923 {
924 	struct fd_softc *fd;
925 	const struct fd_type *type;
926 
927 	fd = device_lookup_private(&fd_cd, FDUNIT(dev));
928 	if (fd == NULL)
929 		return (ENXIO);
930 
931 	type = fd_dev_to_type(fd, dev);
932 	if (type == NULL)
933 		return ENXIO;
934 
935 	if ((fd->sc_flags & FD_OPEN) != 0 &&
936 	    memcmp(fd->sc_type, type, sizeof(*type)))
937 		return EBUSY;
938 
939 	fd->sc_type_copy = *type;
940 	fd->sc_type = &fd->sc_type_copy;
941 	fd->sc_cylin = -1;
942 	fd->sc_flags |= FD_OPEN;
943 
944 	fd_set_geometry(fd);
945 
946 	return 0;
947 }
948 
949 int
fdclose(dev_t dev,int flags,int mode,struct lwp * l)950 fdclose(dev_t dev, int flags, int mode, struct lwp *l)
951 {
952 	struct fd_softc *fd =
953 	    device_lookup_private(&fd_cd, FDUNIT(dev));
954 
955 	fd->sc_flags &= ~FD_OPEN;
956 	fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
957 	return 0;
958 }
959 
960 void
fdcstart(struct fdc_softc * fdc)961 fdcstart(struct fdc_softc *fdc)
962 {
963 
964 	KASSERT(mutex_owned(&fdc->sc_mtx));
965 
966 	if (!device_is_active(fdc->sc_dev))
967 		return;
968 
969 #ifdef DIAGNOSTIC
970 	/* only got here if controller's drive queue was inactive; should
971 	   be in idle state */
972 	if (fdc->sc_state != DEVIDLE) {
973 		printf("fdcstart: not idle\n");
974 		return;
975 	}
976 #endif
977 	(void)fdcintr1(fdc);
978 }
979 
980 static void
fdcpstatus(int n,struct fdc_softc * fdc)981 fdcpstatus(int n, struct fdc_softc *fdc)
982 {
983 	char bits[64];
984 
985 	switch (n) {
986 	case 0:
987 		printf("\n");
988 		break;
989 	case 2:
990 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
991 		printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]);
992 		break;
993 	case 7:
994 		snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
995 		printf(" (st0 %s", bits);
996 		snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]);
997 		printf(" st1 %s", bits);
998 		snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]);
999 		printf(" st2 %s", bits);
1000 		printf(" cyl %d head %d sec %d)\n",
1001 		    fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
1002 		break;
1003 #ifdef DIAGNOSTIC
1004 	default:
1005 		printf("\nfdcstatus: weird size");
1006 		break;
1007 #endif
1008 	}
1009 }
1010 
1011 void
fdcstatus(device_t dv,int n,const char * s)1012 fdcstatus(device_t dv, int n, const char *s)
1013 {
1014 	struct fdc_softc *fdc = device_private(device_parent(dv));
1015 
1016 	if (n == 0) {
1017 		out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
1018 		(void) fdcresult(fdc);
1019 		n = 2;
1020 	}
1021 	fdcpstatus(n, fdc);
1022 
1023 	aprint_normal_dev(dv, "%s", s);
1024 
1025 }
1026 
1027 void
fdctimeout(void * arg)1028 fdctimeout(void *arg)
1029 {
1030 	struct fdc_softc *fdc = arg;
1031 	struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
1032 
1033 	mutex_enter(&fdc->sc_mtx);
1034 #ifdef DEBUG
1035 	log(LOG_ERR, "fdctimeout: state %d\n", fdc->sc_state);
1036 #endif
1037 	fdcstatus(fd->sc_dev, 0, "timeout");
1038 
1039 	if (bufq_peek(fd->sc_q) != NULL)
1040 		fdc->sc_state++;
1041 	else
1042 		fdc->sc_state = DEVIDLE;
1043 
1044 	(void)fdcintr1(fdc);
1045 	mutex_exit(&fdc->sc_mtx);
1046 }
1047 
1048 static int
fdcintr1(struct fdc_softc * fdc)1049 fdcintr1(struct fdc_softc *fdc)
1050 {
1051 #define	st0	fdc->sc_status[0]
1052 #define	cyl	fdc->sc_status[1]
1053 	struct fd_softc *fd;
1054 	struct buf *bp;
1055 	bus_space_tag_t iot = fdc->sc_iot;
1056 	bus_space_handle_t ioh = fdc->sc_ioh;
1057 	int read, head, sec, i, nblks;
1058 	struct fd_type *type;
1059 	struct ne7_fd_formb *finfo = NULL;
1060 
1061 	KASSERT(mutex_owned(&fdc->sc_mtx));
1062 	if (fdc->sc_state == PROBING) {
1063 #ifdef DEBUG
1064 		printf("fdcintr: got probe interrupt\n");
1065 #endif
1066 		fdc->sc_probe++;
1067 		goto out;
1068 	}
1069 
1070 loop:
1071 	/* Is there a drive for the controller to do a transfer with? */
1072 	fd = TAILQ_FIRST(&fdc->sc_drives);
1073 	if (fd == NULL) {
1074 		fdc->sc_state = DEVIDLE;
1075  		goto out;
1076 	}
1077 
1078 	/* Is there a transfer to this drive?  If not, deactivate drive. */
1079 	bp = bufq_peek(fd->sc_q);
1080 	if (bp == NULL) {
1081 		fd->sc_ops = 0;
1082 		TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
1083 		fd->sc_active = 0;
1084 		goto loop;
1085 	}
1086 
1087 	if (bp->b_flags & B_FORMAT)
1088 		finfo = (struct ne7_fd_formb *)bp->b_data;
1089 
1090 	switch (fdc->sc_state) {
1091 	case DEVIDLE:
1092 		fdc->sc_errors = 0;
1093 		fd->sc_skip = 0;
1094 		fd->sc_bcount = bp->b_bcount;
1095 		fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
1096 		callout_stop(&fd->sc_motoroff_ch);
1097 		if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
1098 			fdc->sc_state = MOTORWAIT;
1099 			return 1;
1100 		}
1101 		if ((fd->sc_flags & FD_MOTOR) == 0) {
1102 			/* Turn on the motor, being careful about pairing. */
1103 			struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
1104 			if (ofd && ofd->sc_flags & FD_MOTOR) {
1105 				callout_stop(&ofd->sc_motoroff_ch);
1106 				ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
1107 			}
1108 			fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
1109 			fd_set_motor(fdc, 0);
1110 			fdc->sc_state = MOTORWAIT;
1111 			/* Allow .25s for motor to stabilize. */
1112 			callout_reset(&fd->sc_motoron_ch, hz / 4,
1113 			    fd_motor_on, fd);
1114 			return 1;
1115 		}
1116 		/* Make sure the right drive is selected. */
1117 		fd_set_motor(fdc, 0);
1118 
1119 		/* fall through */
1120 	case DOSEEK:
1121 	doseek:
1122 		if (fd->sc_cylin == bp->b_cylinder)
1123 			goto doio;
1124 
1125 		out_fdc(iot, ioh, NE7CMD_SPECIFY);/* specify command */
1126 		out_fdc(iot, ioh, fd->sc_type->steprate);
1127 		out_fdc(iot, ioh, 6);		/* XXX head load time == 6ms */
1128 
1129 		out_fdc(iot, ioh, NE7CMD_SEEK);	/* seek function */
1130 		out_fdc(iot, ioh, fd->sc_drive); /* drive number */
1131 		out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
1132 
1133 		fd->sc_cylin = -1;
1134 		fdc->sc_state = SEEKWAIT;
1135 
1136 		iostat_seek(fd->sc_dk.dk_stats);
1137 		disk_busy(&fd->sc_dk);
1138 
1139 		callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
1140 		return 1;
1141 
1142 	case DOIO:
1143 	doio:
1144 		type = fd->sc_type;
1145 		if (finfo)
1146 			fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
1147 				      (char *)finfo;
1148 		sec = fd->sc_blkno % type->seccyl;
1149 		nblks = type->seccyl - sec;
1150 		nblks = uimin(nblks, fd->sc_bcount / FDC_BSIZE);
1151 		nblks = uimin(nblks, fdc->sc_maxiosize / FDC_BSIZE);
1152 		fd->sc_nblks = nblks;
1153 		fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FDC_BSIZE;
1154 		head = sec / type->sectrac;
1155 		sec -= head * type->sectrac;
1156 #ifdef DIAGNOSTIC
1157 		{
1158 			int block;
1159 			block = (fd->sc_cylin * type->heads + head)
1160 			    * type->sectrac + sec;
1161 			if (block != fd->sc_blkno) {
1162 				printf("fdcintr: block %d != blkno "
1163 				    "%" PRId64 "\n", block, fd->sc_blkno);
1164 #ifdef DDB
1165 				 Debugger();
1166 #endif
1167 			}
1168 		}
1169 #endif
1170 		read = bp->b_flags & B_READ ? DMAMODE_READ : DMAMODE_WRITE;
1171 		isa_dmastart(fdc->sc_ic, fdc->sc_drq,
1172 		    (char *)bp->b_data + fd->sc_skip, fd->sc_nbytes,
1173 		    NULL, read | DMAMODE_DEMAND, BUS_DMA_NOWAIT);
1174 		bus_space_write_1(iot, fdc->sc_fdctlioh, 0, type->rate);
1175 #ifdef FD_DEBUG
1176 		printf("fdcintr: %s drive %d track %d head %d sec %d nblks %d\n",
1177 			read ? "read" : "write", fd->sc_drive, fd->sc_cylin,
1178 			head, sec, nblks);
1179 #endif
1180 		if (finfo) {
1181 			/* formatting */
1182 			if (out_fdc(iot, ioh, NE7CMD_FORMAT) < 0) {
1183 				fdc->sc_errors = 4;
1184 				fdcretry(fdc);
1185 				goto loop;
1186 			}
1187 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1188 			out_fdc(iot, ioh, finfo->fd_formb_secshift);
1189 			out_fdc(iot, ioh, finfo->fd_formb_nsecs);
1190 			out_fdc(iot, ioh, finfo->fd_formb_gaplen);
1191 			out_fdc(iot, ioh, finfo->fd_formb_fillbyte);
1192 		} else {
1193 			if (read)
1194 				out_fdc(iot, ioh, NE7CMD_READ);	/* READ */
1195 			else
1196 				out_fdc(iot, ioh, NE7CMD_WRITE); /* WRITE */
1197 			out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
1198 			out_fdc(iot, ioh, fd->sc_cylin); /* track */
1199 			out_fdc(iot, ioh, head);
1200 			out_fdc(iot, ioh, sec + 1);	 /* sector +1 */
1201 			out_fdc(iot, ioh, type->secsize);/* sector size */
1202 			out_fdc(iot, ioh, type->sectrac);/* sectors/track */
1203 			out_fdc(iot, ioh, type->gap1);	 /* gap1 size */
1204 			out_fdc(iot, ioh, type->datalen);/* data length */
1205 		}
1206 		fdc->sc_state = IOCOMPLETE;
1207 
1208 		disk_busy(&fd->sc_dk);
1209 
1210 		/* allow 2 seconds for operation */
1211 		callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
1212 		return 1;				/* will return later */
1213 
1214 	case SEEKWAIT:
1215 		callout_stop(&fdc->sc_timo_ch);
1216 		fdc->sc_state = SEEKCOMPLETE;
1217 		/* allow 1/50 second for heads to settle */
1218 		callout_reset(&fdc->sc_intr_ch, hz / 50, fdcintrcb, fdc);
1219 		return 1;
1220 
1221 	case SEEKCOMPLETE:
1222 		/* no data on seek */
1223 		disk_unbusy(&fd->sc_dk, 0, 0);
1224 
1225 		/* Make sure seek really happened. */
1226 		out_fdc(iot, ioh, NE7CMD_SENSEI);
1227 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
1228 		    cyl != bp->b_cylinder * fd->sc_type->step) {
1229 #ifdef FD_DEBUG
1230 			fdcstatus(fd->sc_dev, 2, "seek failed");
1231 #endif
1232 			fdcretry(fdc);
1233 			goto loop;
1234 		}
1235 		fd->sc_cylin = bp->b_cylinder;
1236 		goto doio;
1237 
1238 	case IOTIMEDOUT:
1239 		isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1240 		/* FALLTHROUGH */
1241 	case SEEKTIMEDOUT:
1242 	case RECALTIMEDOUT:
1243 	case RESETTIMEDOUT:
1244 		fdcretry(fdc);
1245 		goto loop;
1246 
1247 	case IOCOMPLETE: /* IO DONE, post-analyze */
1248 		callout_stop(&fdc->sc_timo_ch);
1249 
1250 		disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
1251 		    (bp->b_flags & B_READ));
1252 
1253 		if (fdcresult(fdc) != 7 || (st0 & 0xf8) != 0) {
1254 			isa_dmaabort(fdc->sc_ic, fdc->sc_drq);
1255 #ifdef FD_DEBUG
1256 			fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ?
1257 			    "read failed" : "write failed");
1258 			printf("blkno %llu nblks %d\n",
1259 			    (unsigned long long)fd->sc_blkno, fd->sc_nblks);
1260 #endif
1261 			fdcretry(fdc);
1262 			goto loop;
1263 		}
1264 		isa_dmadone(fdc->sc_ic, fdc->sc_drq);
1265 		if (fdc->sc_errors) {
1266 			diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
1267 			    fd->sc_skip / FDC_BSIZE, NULL);
1268 			printf("\n");
1269 			fdc->sc_errors = 0;
1270 		}
1271 		fd->sc_blkno += fd->sc_nblks;
1272 		fd->sc_skip += fd->sc_nbytes;
1273 		fd->sc_bcount -= fd->sc_nbytes;
1274 		if (!finfo && fd->sc_bcount > 0) {
1275 			bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
1276 			goto doseek;
1277 		}
1278 		fdfinish(fd, bp);
1279 		goto loop;
1280 
1281 	case DORESET:
1282 		/* try a reset, keep motor on */
1283 		fd_set_motor(fdc, 1);
1284 		delay(100);
1285 		fd_set_motor(fdc, 0);
1286 		fdc->sc_state = RESETCOMPLETE;
1287 		callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
1288 		return 1;			/* will return later */
1289 
1290 	case RESETCOMPLETE:
1291 		callout_stop(&fdc->sc_timo_ch);
1292 		/* clear the controller output buffer */
1293 		for (i = 0; i < 4; i++) {
1294 			out_fdc(iot, ioh, NE7CMD_SENSEI);
1295 			(void) fdcresult(fdc);
1296 		}
1297 
1298 		/* fall through */
1299 	case DORECAL:
1300 		out_fdc(iot, ioh, NE7CMD_RECAL); /* recalibrate function */
1301 		out_fdc(iot, ioh, fd->sc_drive);
1302 		fdc->sc_state = RECALWAIT;
1303 		callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
1304 		return 1;			/* will return later */
1305 
1306 	case RECALWAIT:
1307 		callout_stop(&fdc->sc_timo_ch);
1308 		fdc->sc_state = RECALCOMPLETE;
1309 		/* allow 1/30 second for heads to settle */
1310 		callout_reset(&fdc->sc_intr_ch, hz / 30, fdcintrcb, fdc);
1311 		return 1;			/* will return later */
1312 
1313 	case RECALCOMPLETE:
1314 		out_fdc(iot, ioh, NE7CMD_SENSEI);
1315 		if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
1316 #ifdef FD_DEBUG
1317 			fdcstatus(fd->sc_dev, 2, "recalibrate failed");
1318 #endif
1319 			fdcretry(fdc);
1320 			goto loop;
1321 		}
1322 		fd->sc_cylin = 0;
1323 		goto doseek;
1324 
1325 	case MOTORWAIT:
1326 		if (fd->sc_flags & FD_MOTOR_WAIT)
1327 			return 1;		/* time's not up yet */
1328 		goto doseek;
1329 
1330 	default:
1331 		fdcstatus(fd->sc_dev, 0, "stray interrupt");
1332 		return 1;
1333 	}
1334 #undef	st0
1335 #undef	cyl
1336 
1337 out:
1338 	cv_signal(&fdc->sc_cv);
1339 	return 1;
1340 }
1341 
1342 static void
fdcintrcb(void * arg)1343 fdcintrcb(void *arg)
1344 {
1345 	(void)fdcintr(arg);
1346 }
1347 
1348 int
fdcintr(void * arg)1349 fdcintr(void *arg)
1350 {
1351 	int rc;
1352 	struct fdc_softc *fdc = arg;
1353 
1354 	mutex_enter(&fdc->sc_mtx);
1355 	rc = fdcintr1(fdc);
1356 	mutex_exit(&fdc->sc_mtx);
1357 	return rc;
1358 }
1359 
1360 void
fdcretry(struct fdc_softc * fdc)1361 fdcretry(struct fdc_softc *fdc)
1362 {
1363 	struct fd_softc *fd;
1364 	struct buf *bp;
1365 
1366 	fd = TAILQ_FIRST(&fdc->sc_drives);
1367 	bp = bufq_peek(fd->sc_q);
1368 
1369 	if (fd->sc_opts & FDOPT_NORETRY)
1370 	    goto fail;
1371 	switch (fdc->sc_errors) {
1372 	case 0:
1373 		/* try again */
1374 		fdc->sc_state = DOSEEK;
1375 		break;
1376 
1377 	case 1: case 2: case 3:
1378 		/* didn't work; try recalibrating */
1379 		fdc->sc_state = DORECAL;
1380 		break;
1381 
1382 	case 4:
1383 		/* still no go; reset the bastard */
1384 		fdc->sc_state = DORESET;
1385 		break;
1386 
1387 	default:
1388 	fail:
1389 		if ((fd->sc_opts & FDOPT_SILENT) == 0) {
1390 			diskerr(bp, "fd", "hard error", LOG_PRINTF,
1391 				fd->sc_skip / FDC_BSIZE, NULL);
1392 			fdcpstatus(7, fdc);
1393 		}
1394 
1395 		bp->b_error = EIO;
1396 		fdfinish(fd, bp);
1397 	}
1398 	fdc->sc_errors++;
1399 }
1400 
1401 int
fdioctl(dev_t dev,u_long cmd,void * addr,int flag,struct lwp * l)1402 fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1403 {
1404 	struct fd_softc *fd =
1405 	    device_lookup_private(&fd_cd, FDUNIT(dev));
1406 	struct fdformat_parms *form_parms;
1407 	struct fdformat_cmd *form_cmd;
1408 	struct ne7_fd_formb *fd_formb;
1409 	struct disklabel *lp = fd->sc_dk.dk_label;
1410 	int error;
1411 	unsigned int scratch;
1412 	int il[FD_MAX_NSEC + 1];
1413 	int i, j;
1414 #ifdef __HAVE_OLD_DISKLABEL
1415 	struct disklabel newlabel;
1416 #endif
1417 
1418 	switch (cmd) {
1419 	case DIOCGPARTINFO:
1420 	case DIOCGDINFO:
1421 #ifdef __HAVE_OLD_DISKLABEL
1422 	case ODIOCGDINFO:
1423 #endif
1424 		memset(lp, 0, sizeof(*lp));
1425 
1426 		lp->d_type = DKTYPE_FLOPPY;
1427 		lp->d_secsize = FDC_BSIZE;
1428 		lp->d_nsectors = fd->sc_type->sectrac;
1429 		lp->d_ntracks = fd->sc_type->heads;
1430 		lp->d_ncylinders = fd->sc_type->cyls;
1431 		lp->d_secpercyl = fd->sc_type->seccyl;
1432 		lp->d_secperunit = fd->sc_type->size;
1433 
1434 		if (readdisklabel(dev, fdstrategy, lp, NULL) != NULL)
1435 			return EINVAL;
1436 		break;
1437 	}
1438 
1439 	error = disk_ioctl(&fd->sc_dk, dev, cmd, addr, flag, l);
1440 	if (error != EPASSTHROUGH)
1441 		return error;
1442 
1443 	switch (cmd) {
1444 	case DIOCWLABEL:
1445 		if ((flag & FWRITE) == 0)
1446 			return EBADF;
1447 		/* XXX do something */
1448 		return 0;
1449 
1450 	case DIOCWDINFO:
1451 #ifdef __HAVE_OLD_DISKLABEL
1452 	case ODIOCWDINFO:
1453 #endif
1454 	{
1455 		if ((flag & FWRITE) == 0)
1456 			return EBADF;
1457 #ifdef __HAVE_OLD_DISKLABEL
1458 		if (cmd == ODIOCWDINFO) {
1459 			memset(&newlabel, 0, sizeof newlabel);
1460 			memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1461 			addr = &newlabel;
1462 		}
1463 #endif
1464 		error = setdisklabel(lp, addr, 0, NULL);
1465 		if (error)
1466 			return error;
1467 
1468 		error = writedisklabel(dev, fdstrategy, lp, NULL);
1469 		return error;
1470 	}
1471 
1472 	case FDIOCGETFORMAT:
1473 		form_parms = (struct fdformat_parms *)addr;
1474 		form_parms->fdformat_version = FDFORMAT_VERSION;
1475 		form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
1476 		form_parms->ncyl = fd->sc_type->cyls;
1477 		form_parms->nspt = fd->sc_type->sectrac;
1478 		form_parms->ntrk = fd->sc_type->heads;
1479 		form_parms->stepspercyl = fd->sc_type->step;
1480 		form_parms->gaplen = fd->sc_type->gap2;
1481 		form_parms->fillbyte = fd->sc_type->fillbyte;
1482 		form_parms->interleave = fd->sc_type->interleave;
1483 		switch (fd->sc_type->rate) {
1484 		case FDC_500KBPS:
1485 			form_parms->xfer_rate = 500 * 1024;
1486 			break;
1487 		case FDC_300KBPS:
1488 			form_parms->xfer_rate = 300 * 1024;
1489 			break;
1490 		case FDC_250KBPS:
1491 			form_parms->xfer_rate = 250 * 1024;
1492 			break;
1493 		default:
1494 			return EINVAL;
1495 		}
1496 		return 0;
1497 
1498 	case FDIOCSETFORMAT:
1499 		if((flag & FWRITE) == 0)
1500 			return EBADF;	/* must be opened for writing */
1501 		form_parms = (struct fdformat_parms *)addr;
1502 		if (form_parms->fdformat_version != FDFORMAT_VERSION)
1503 			return EINVAL;	/* wrong version of formatting prog */
1504 
1505 		scratch = form_parms->nbps >> 7;
1506 		if ((form_parms->nbps & 0x7f) || ffs(scratch) == 0 ||
1507 		    scratch & ~(1 << (ffs(scratch)-1)))
1508 			/* not a power-of-two multiple of 128 */
1509 			return EINVAL;
1510 
1511 		switch (form_parms->xfer_rate) {
1512 		case 500 * 1024:
1513 			fd->sc_type->rate = FDC_500KBPS;
1514 			break;
1515 		case 300 * 1024:
1516 			fd->sc_type->rate = FDC_300KBPS;
1517 			break;
1518 		case 250 * 1024:
1519 			fd->sc_type->rate = FDC_250KBPS;
1520 			break;
1521 		default:
1522 			return EINVAL;
1523 		}
1524 
1525 		if (form_parms->nspt > FD_MAX_NSEC ||
1526 		    form_parms->fillbyte > 0xff ||
1527 		    form_parms->interleave > 0xff)
1528 			return EINVAL;
1529 		fd->sc_type->sectrac = form_parms->nspt;
1530 		if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
1531 			return EINVAL;
1532 		fd->sc_type->heads = form_parms->ntrk;
1533 		fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
1534 		fd->sc_type->secsize = ffs(scratch)-1;
1535 		fd->sc_type->gap2 = form_parms->gaplen;
1536 		fd->sc_type->cyls = form_parms->ncyl;
1537 		fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
1538 		    form_parms->nbps / DEV_BSIZE;
1539 		fd->sc_type->step = form_parms->stepspercyl;
1540 		fd->sc_type->fillbyte = form_parms->fillbyte;
1541 		fd->sc_type->interleave = form_parms->interleave;
1542 		return 0;
1543 
1544 	case FDIOCFORMAT_TRACK:
1545 		if((flag & FWRITE) == 0)
1546 			return EBADF;	/* must be opened for writing */
1547 		form_cmd = (struct fdformat_cmd *)addr;
1548 		if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
1549 			return EINVAL;	/* wrong version of formatting prog */
1550 
1551 		if (form_cmd->head >= fd->sc_type->heads ||
1552 		    form_cmd->cylinder >= fd->sc_type->cyls) {
1553 			return EINVAL;
1554 		}
1555 
1556 		fd_formb = kmem_alloc(sizeof(*fd_formb), KM_SLEEP);
1557 		fd_formb->head = form_cmd->head;
1558 		fd_formb->cyl = form_cmd->cylinder;
1559 		fd_formb->transfer_rate = fd->sc_type->rate;
1560 		fd_formb->fd_formb_secshift = fd->sc_type->secsize;
1561 		fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
1562 		fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
1563 		fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
1564 
1565 		memset(il, 0, sizeof il);
1566 		for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
1567 			while (il[(j%fd_formb->fd_formb_nsecs)+1])
1568 				j++;
1569 			il[(j%fd_formb->fd_formb_nsecs)+1] = i;
1570 			j += fd->sc_type->interleave;
1571 		}
1572 		for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
1573 			fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
1574 			fd_formb->fd_formb_headno(i) = form_cmd->head;
1575 			fd_formb->fd_formb_secno(i) = il[i+1];
1576 			fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
1577 		}
1578 
1579 		error = fdformat(dev, fd_formb, l);
1580 		kmem_free(fd_formb, sizeof(*fd_formb));
1581 		return error;
1582 
1583 	case FDIOCGETOPTS:		/* get drive options */
1584 		*(int *)addr = fd->sc_opts;
1585 		return 0;
1586 
1587 	case FDIOCSETOPTS:		/* set drive options */
1588 		fd->sc_opts = *(int *)addr;
1589 		return 0;
1590 
1591 	default:
1592 		return ENOTTY;
1593 	}
1594 
1595 #ifdef DIAGNOSTIC
1596 	panic("fdioctl: impossible");
1597 #endif
1598 }
1599 
1600 int
fdformat(dev_t dev,struct ne7_fd_formb * finfo,struct lwp * l)1601 fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct lwp *l)
1602 {
1603 	int rv = 0;
1604 	struct fd_softc *fd =
1605 	    device_lookup_private(&fd_cd, FDUNIT(dev));
1606 	struct fd_type *type = fd->sc_type;
1607 	struct buf *bp;
1608 
1609 	/* set up a buffer header for fdstrategy() */
1610 	bp = getiobuf(NULL, false);
1611 	if (bp == NULL)
1612 		return ENOBUFS;
1613 
1614 	bp->b_cflags = BC_BUSY;
1615 	bp->b_flags = B_PHYS | B_FORMAT;
1616 	bp->b_proc = l->l_proc;
1617 	bp->b_dev = dev;
1618 
1619 	/*
1620 	 * calculate a fake blkno, so fdstrategy() would initiate a
1621 	 * seek to the requested cylinder
1622 	 */
1623 	bp->b_blkno = (finfo->cyl * (type->sectrac * type->heads)
1624 		       + finfo->head * type->sectrac) * FDC_BSIZE / DEV_BSIZE;
1625 
1626 	bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
1627 	bp->b_data = (void *)finfo;
1628 
1629 #ifdef FD_DEBUG
1630 	printf("fdformat: blkno %" PRIx64 " count %x\n",
1631 	    bp->b_blkno, bp->b_bcount);
1632 #endif
1633 
1634 	/* now do the format */
1635 	fdstrategy(bp);
1636 
1637 	/* ...and wait for it to complete */
1638 	rv = biowait(bp);
1639 	putiobuf(bp);
1640 	return rv;
1641 }
1642 
1643 /*
1644  * Mountroot hook: prompt the user to enter the root file system
1645  * floppy.
1646  */
1647 void
fd_mountroot_hook(device_t dev)1648 fd_mountroot_hook(device_t dev)
1649 {
1650 	int c;
1651 
1652 	printf("Insert filesystem floppy and press return.");
1653 	cnpollc(1);
1654 	for (;;) {
1655 		c = cngetc();
1656 		if ((c == '\r') || (c == '\n')) {
1657 			printf("\n");
1658 			break;
1659 		}
1660 	}
1661 	cnpollc(0);
1662 }
1663 
1664 static void
fd_set_geometry(struct fd_softc * fd)1665 fd_set_geometry(struct fd_softc *fd)
1666 {
1667 	const struct fd_type *fdt;
1668 
1669 	fdt = fd->sc_type;
1670 	if (fdt == NULL) {
1671 		fdt = fd->sc_deftype;
1672 		if (fdt == NULL)
1673 			return;
1674 	}
1675 
1676 	struct disk_geom *dg = &fd->sc_dk.dk_geom;
1677 
1678 	memset(dg, 0, sizeof(*dg));
1679 	dg->dg_secperunit = fdt->size;
1680 	dg->dg_nsectors = fdt->sectrac;
1681 	switch (fdt->secsize) {
1682 	case 2:
1683 		dg->dg_secsize = 512;
1684 		break;
1685 	case 3:
1686 		dg->dg_secsize = 1024;
1687 		break;
1688 	default:
1689 		break;
1690 	}
1691 	dg->dg_ntracks = fdt->heads;
1692 	dg->dg_ncylinders = fdt->cyls;
1693 	disk_set_info(fd->sc_dev, &fd->sc_dk, NULL);
1694 }
1695